Skip to content

Commit 7e2d7ad

Browse files
committed
Replace the bytecode structure with KaosIR
1 parent 726e209 commit 7e2d7ad

File tree

12 files changed

+176
-1166
lines changed

12 files changed

+176
-1166
lines changed

compiler/compiler.c

Lines changed: 85 additions & 95 deletions
Large diffs are not rendered by default.

compiler/compiler.h

Lines changed: 64 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -27,56 +27,78 @@
2727
#include "../vm/cpu.h"
2828
#include "../interpreter/module_new.h"
2929

30-
typedef struct i64_array i64_array;
30+
typedef struct KaosIR KaosIR;
31+
typedef struct KaosInst KaosInst;
32+
typedef struct KaosOp KaosOp;
3133

32-
typedef struct i64_array {
33-
i64* arr;
34+
typedef struct KaosIR {
35+
KaosInst* arr;
3436
i64 capacity;
3537
i64 size;
36-
i64 heap;
37-
i64 start;
3838
i64 hlt_count;
39-
i64_array* ast_ref;
40-
} i64_array;
39+
} KaosIR;
4140

42-
i64_array* compile(ASTRoot* ast_root);
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+
64+
KaosIR* compile(ASTRoot* ast_root);
4365
void initCallJumps();
44-
void fillCallJumps(i64_array* program);
45-
void compileImports(ASTRoot* ast_root, i64_array* program);
46-
void compileStmtList(i64_array* program, StmtList* stmt_list);
47-
void compileStmt(i64_array* program, Stmt* stmt);
48-
unsigned short compileExpr(i64_array* program, Expr* expr);
49-
void compileDecl(i64_array* program, Decl* decl);
50-
void compileSpecList(i64_array* program, SpecList* spec_list);
51-
unsigned short compileSpec(i64_array* program, Spec* spec);
52-
53-
void push_instr(i64_array* program, i64 el);
54-
void pushProgram(i64_array* program, i64 el);
55-
i64 popProgram(i64_array* program);
56-
void freeProgram(i64_array* program);
57-
i64_array* initProgram();
58-
void shift_registers(i64_array* program, size_t shift);
59-
60-
Symbol* store_bool(i64_array* program, char *name, bool is_any);
61-
Symbol* store_int(i64_array* program, char *name, bool is_any);
62-
Symbol* store_float(i64_array* program, char *name, bool is_any);
63-
Symbol* store_string(i64_array* program, char *name, size_t len, bool is_any, bool is_dynamic);
64-
Symbol* store_list(i64_array* program, char *name, size_t len, bool is_dynamic);
65-
Symbol* store_dict(i64_array* program, char *name, size_t len, bool is_dynamic);
66-
Symbol* store_any(i64_array* program, char *name);
67-
68-
void load_bool(i64_array* program, Symbol* symbol);
69-
void load_int(i64_array* program, Symbol* symbol);
70-
void load_float(i64_array* program, Symbol* symbol);
71-
void load_string(i64_array* program, Symbol* symbol);
72-
void load_list(i64_array* program, Symbol* symbol);
73-
void load_dict(i64_array* program, Symbol* symbol);
74-
void load_any(i64_array* program, Symbol* symbol);
66+
void fillCallJumps(KaosIR* program);
67+
void compileImports(ASTRoot* ast_root, KaosIR* program);
68+
void compileStmtList(KaosIR* program, StmtList* stmt_list);
69+
void compileStmt(KaosIR* program, Stmt* stmt);
70+
unsigned short compileExpr(KaosIR* program, Expr* expr);
71+
void compileDecl(KaosIR* program, Decl* decl);
72+
void compileSpecList(KaosIR* program, SpecList* spec_list);
73+
unsigned short compileSpec(KaosIR* program, Spec* spec);
74+
75+
void push_instr(KaosIR* program, i64 el);
76+
void pushProgram(KaosIR* program, i64 el);
77+
KaosInst popProgram(KaosIR* program);
78+
void freeProgram(KaosIR* program);
79+
KaosIR* initProgram();
80+
void shift_registers(KaosIR* program, size_t shift);
81+
82+
Symbol* store_bool(KaosIR* program, char *name, bool is_any);
83+
Symbol* store_int(KaosIR* program, char *name, bool is_any);
84+
Symbol* store_float(KaosIR* program, char *name, bool is_any);
85+
Symbol* store_string(KaosIR* program, char *name, size_t len, bool is_any, bool is_dynamic);
86+
Symbol* store_list(KaosIR* program, char *name, size_t len, bool is_dynamic);
87+
Symbol* store_dict(KaosIR* program, char *name, size_t len, bool is_dynamic);
88+
Symbol* store_any(KaosIR* program, char *name);
89+
90+
void load_bool(KaosIR* program, Symbol* symbol);
91+
void load_int(KaosIR* program, Symbol* symbol);
92+
void load_float(KaosIR* program, Symbol* symbol);
93+
void load_string(KaosIR* program, Symbol* symbol);
94+
void load_list(KaosIR* program, Symbol* symbol);
95+
void load_dict(KaosIR* program, Symbol* symbol);
96+
void load_any(KaosIR* program, Symbol* symbol);
7597

7698
char* compile_module_selector(Expr* module_selector);
77-
bool declare_function(Stmt* stmt, File* file, i64_array* program);
78-
void declare_functions(ASTRoot* ast_root, i64_array* program);
79-
void compile_functions(ASTRoot* ast_root, i64_array* program);
99+
bool declare_function(Stmt* stmt, File* file, KaosIR* program);
100+
void declare_functions(ASTRoot* ast_root, KaosIR* program);
101+
void compile_functions(ASTRoot* ast_root, KaosIR* program);
80102

81103
void strongly_type(Symbol* symbol_x, Symbol* symbol_y, _Function* function, Expr* expr, enum ValueType value_type);
82104
void strongly_type_basic_check(unsigned short code, char *str1, char *str2, enum Type type, enum ValueType value_type);

compiler/compiler_emit.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222

2323
#include "compiler_emit.h"
2424

25-
void emit(i64_array* program)
25+
void emit(KaosIR* program)
2626
{
27-
cpu *c = new_cpu(program->arr, program->heap, 0, program->ast_ref->arr, 0);
27+
cpu *c = new_cpu(program, 0);
2828
print_cpu(c, program->hlt_count);
2929
free_cpu(c);
3030
}
@@ -53,9 +53,9 @@ void emitBytecode(cpu *c)
5353
char str_pc[40];
5454
char str_inst[40];
5555

56-
sprintf(str_pc, "%lld", c->pc);
56+
sprintf(str_pc, "%lld", c->ic);
5757

58-
switch (c->inst) {
58+
switch (c->inst->op_code) {
5959
case HLT:
6060
sprintf(str_inst, "%s", "HLT");
6161
}

compiler/compiler_emit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include "compiler.h"
2727
#include "../vm/cpu.h"
2828

29-
void emit(i64_array* program);
29+
void emit(KaosIR* program);
3030
void print_cpu(cpu *c, i64 hlt_count);
3131
void emitBytecode(cpu *c);
3232

0 commit comments

Comments
 (0)