Skip to content

Commit 3e04e5d

Browse files
committed
Fix the push_inst_ functions and add ir.h module
1 parent 7e2d7ad commit 3e04e5d

File tree

10 files changed

+128
-98
lines changed

10 files changed

+128
-98
lines changed

compiler/compiler.c

+32-8
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ KaosIR* compile(ASTRoot* ast_root)
6161
compileStmt(program, stmt);
6262
}
6363

64+
push_inst_(program, HLT);
6465
program->hlt_count++;
6566
fillCallJumps(program);
6667
return program;
@@ -353,6 +354,7 @@ unsigned short compileExpr(KaosIR* program, Expr* expr)
353354
case V_BOOL:
354355
break;
355356
case V_INT:
357+
push_inst_r_i(program, MOVI, R0, expr->v.basic_lit->value.i);
356358
break;
357359
case V_FLOAT:
358360
break;
@@ -1414,23 +1416,45 @@ unsigned short compileSpec(KaosIR* program, Spec* spec)
14141416
return 0;
14151417
}
14161418

1417-
void push_instr(KaosIR* program, i64 el)
1419+
void push_inst_(KaosIR* program, enum IROpCode op_code)
14181420
{
1419-
pushProgram(program, el);
1420-
// pushProgram(program->ast_ref, ast_ref);
1421+
KaosInst* inst = malloc(sizeof *inst);
1422+
inst->op_code = op_code;
1423+
1424+
pushProgram(program, inst);
1425+
}
1426+
1427+
void push_inst_r_i(KaosIR* program, enum IROpCode op_code, enum IRRegister reg, i64 i)
1428+
{
1429+
KaosOp* op1 = malloc(sizeof *op1);
1430+
op1->type = IR_REG;
1431+
op1->reg = reg;
1432+
1433+
KaosOp* op2 = malloc(sizeof *op2);
1434+
op2->type = IR_VAL;
1435+
union IRValue value2;
1436+
value2.i = i;
1437+
op2->value = value2;
1438+
1439+
KaosInst* inst = malloc(sizeof *inst);
1440+
inst->op_code = op_code;
1441+
inst->op1 = op1;
1442+
inst->op2 = op2;
1443+
1444+
pushProgram(program, inst);
14211445
}
14221446

1423-
void pushProgram(KaosIR* program, i64 el)
1447+
void pushProgram(KaosIR* program, KaosInst* inst)
14241448
{
14251449
if (program->capacity == 0) {
1426-
program->arr = (KaosInst*)malloc((++program->capacity) * sizeof(KaosInst));
1450+
program->arr = (KaosInst**)malloc((++program->capacity) * sizeof(KaosInst*));
14271451
} else {
1428-
program->arr = (KaosInst*)realloc(program->arr, (++program->capacity) * sizeof(KaosInst));
1452+
program->arr = (KaosInst**)realloc(program->arr, (++program->capacity) * sizeof(KaosInst*));
14291453
}
1430-
// program->arr[program->size++] = el;
1454+
program->arr[program->size++] = inst;
14311455
}
14321456

1433-
KaosInst popProgram(KaosIR* program)
1457+
KaosInst* popProgram(KaosIR* program)
14341458
{
14351459
return program->arr[program->size--];
14361460
}

compiler/compiler.h

+6-38
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,10 @@
2323
#ifndef KAOS_COMPILER_H
2424
#define KAOS_COMPILER_H
2525

26-
#include "../ast/ast.h"
2726
#include "../vm/cpu.h"
27+
#include "../ast/ast.h"
2828
#include "../interpreter/module_new.h"
2929

30-
typedef struct KaosIR KaosIR;
31-
typedef struct KaosInst KaosInst;
32-
typedef struct KaosOp KaosOp;
33-
34-
typedef struct KaosIR {
35-
KaosInst* arr;
36-
i64 capacity;
37-
i64 size;
38-
i64 hlt_count;
39-
} KaosIR;
40-
41-
typedef struct KaosInst {
42-
i64 op_code;
43-
KaosOp* op1;
44-
KaosOp* op2;
45-
KaosOp* op3;
46-
void* ast_ref;
47-
} KaosInst;
48-
49-
50-
enum IRType { IR_REG, IR_VAL };
51-
enum IRValueType { IR_INT, IR_FLOAT, IR_STRING };
52-
53-
typedef struct KaosOp {
54-
enum IRType type;
55-
i64 reg;
56-
enum IRValueType value_type;
57-
union IRValue {
58-
i64 i;
59-
f64 f;
60-
byte *s;
61-
} value;
62-
} KaosOp;
63-
6430
KaosIR* compile(ASTRoot* ast_root);
6531
void initCallJumps();
6632
void fillCallJumps(KaosIR* program);
@@ -72,9 +38,11 @@ void compileDecl(KaosIR* program, Decl* decl);
7238
void compileSpecList(KaosIR* program, SpecList* spec_list);
7339
unsigned short compileSpec(KaosIR* program, Spec* spec);
7440

75-
void push_instr(KaosIR* program, i64 el);
76-
void pushProgram(KaosIR* program, i64 el);
77-
KaosInst popProgram(KaosIR* program);
41+
void push_inst_(KaosIR* program, enum IROpCode op_code);
42+
void push_inst_r_i(KaosIR* program, enum IROpCode op_code, enum IRRegister reg, i64 i);
43+
44+
void pushProgram(KaosIR* program, KaosInst* el);
45+
KaosInst* popProgram(KaosIR* program);
7846
void freeProgram(KaosIR* program);
7947
KaosIR* initProgram();
8048
void shift_registers(KaosIR* program, size_t shift);

compiler/compiler_emit.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void print_cpu(cpu *c, i64 hlt_count)
4343
do {
4444
fetch(c);
4545
emitBytecode(c);
46-
} while (c->inst != HLT);
46+
} while (c->inst->op_code != HLT);
4747
}
4848
printf("\n");
4949
}

interpreter/extension.h

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ typedef void (*lib_func)();
4141
#endif
4242

4343
#include "../vm/types.h"
44-
#include "../vm/instructions.h"
4544
#include "../vm/cpu.h"
4645
#include "function.h"
4746
#include "../Chaos.h"

parser/parser.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ void compile_interactive()
268268
compiling_a_function = false;
269269
current_file_index = 0;
270270

271-
push_instr(interactive_program, HLT);
271+
push_inst_(interactive_program, HLT);
272272
interactive_program->hlt_count++;
273273
interactive_c->ic = interactive_program->size - 1;
274274
if (interactive_c->debug_level > 1) {
@@ -288,7 +288,7 @@ void compile_interactive()
288288
compiling_a_function = true;
289289
compileStmt(interactive_program, stmt);
290290
compiling_a_function = false;
291-
push_instr(interactive_program, HLT);
291+
push_inst_(interactive_program, HLT);
292292
interactive_program->hlt_count++;
293293
if (!is_function)
294294
fillCallJumps(interactive_program);

vm/cpu.c

+13-13
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ struct jit *_jit;
2828
#include "cpu.h"
2929

3030
char *reg_names[] = {
31-
"R0A", "R1A", "R2A", "R3A", "R4A", "R5A", "R6A", "R7A",
32-
"R0B", "R1B", "R2B", "R3B", "R4B", "R5B", "R6B", "R7B"
31+
"R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7",
32+
"R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15"
3333
};
3434

3535
i64* ast_stack = NULL;
@@ -39,6 +39,7 @@ cpu *new_cpu(KaosIR* program, unsigned short debug_level)
3939
{
4040
cpu *c = malloc(sizeof(cpu));
4141
c->program = program;
42+
c->ic = 0;
4243

4344
ast_stack = (i64*)malloc(USHRT_MAX * 256 * sizeof(i64));
4445
return c;
@@ -65,7 +66,7 @@ void run_cpu(cpu *c)
6566
do {
6667
fetch(c);
6768
execute(c);
68-
} while (c->inst != HLT);
69+
} while (c->inst->op_code != HLT);
6970

7071
jit_retr(_jit, R(0));
7172
jit_generate_code(_jit);
@@ -76,24 +77,23 @@ void run_cpu(cpu *c)
7677
void eat_until_hlt(cpu *c)
7778
{
7879
do {
79-
} while (c->inst != HLT);
80+
} while (c->inst->op_code != HLT);
8081
}
8182

8283
void fetch(cpu *c)
8384
{
84-
c->ic++;
85+
c->inst = c->program->arr[c->ic++];
8586
}
8687

8788
void execute(cpu *c)
8889
{
8990
// i64 ic_start = c->ic;
9091

91-
// switch (c->inst) {
92-
// case LII:
93-
// jit_movi(_jit, R(0), c->src);
94-
// c->ic += 2;
95-
// break;
96-
// default:
97-
// break;
98-
// }
92+
switch (c->inst->op_code) {
93+
case MOVI:
94+
jit_movi(_jit, R(c->inst->op1->reg), c->inst->op2->value.i);
95+
break;
96+
default:
97+
break;
98+
}
9999
}

vm/cpu.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,11 @@
2828
#include <stdbool.h>
2929
#include <math.h>
3030

31-
#include "types.h"
32-
#include "instructions.h"
31+
#include "ir.h"
3332

3433
#include "../enums.h"
3534
#include "../utilities/helpers.h"
3635

37-
typedef struct KaosIR KaosIR;
38-
3936
i64* ast_stack;
4037
i64 ast_stack_p;
4138

vm/instructions.h

-31
This file was deleted.

vm/ir.h

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Description: Intermediate Representation module of the Chaos Programming Language's source
3+
*
4+
* Copyright (c) 2019-2021 Chaos Language Development Authority <[email protected]>
5+
*
6+
* License: GNU General Public License v3.0
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>
19+
*
20+
* Authors: M. Mert Yildiran <[email protected]>
21+
*/
22+
23+
#ifndef IR_H
24+
#define IR_H
25+
26+
#include "types.h"
27+
28+
enum IROpCode {
29+
MOVI,
30+
HLT,
31+
NUM_INSTRUCTIONS
32+
};
33+
34+
typedef struct KaosIR KaosIR;
35+
typedef struct KaosInst KaosInst;
36+
typedef struct KaosOp KaosOp;
37+
38+
typedef struct KaosIR {
39+
KaosInst** arr;
40+
i64 capacity;
41+
i64 size;
42+
i64 hlt_count;
43+
} KaosIR;
44+
45+
typedef struct KaosInst {
46+
i64 op_code;
47+
KaosOp* op1;
48+
KaosOp* op2;
49+
KaosOp* op3;
50+
void* ast_ref;
51+
} KaosInst;
52+
53+
enum IRType { IR_REG, IR_VAL };
54+
enum IRValueType { IR_INT, IR_FLOAT, IR_STRING };
55+
enum IRRegister {
56+
R0, R1, R2, R3, R4, R5, R6, R7,
57+
R8, R9, R10, R11, R12, R13, R14, R15,
58+
IR_NUM_REGISTERS
59+
};
60+
61+
typedef struct KaosOp {
62+
enum IRType type;
63+
enum IRRegister reg;
64+
enum IRValueType value_type;
65+
union IRValue {
66+
i64 i;
67+
f64 f;
68+
byte *s;
69+
} value;
70+
} KaosOp;
71+
72+
#endif

vm/types.h

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
typedef struct KaosIR KaosIR;
3232
typedef struct KaosInst KaosInst;
3333

34+
3435
typedef struct {
3536
KaosIR* program;
3637

0 commit comments

Comments
 (0)