diff options
-rw-r--r-- | src/ir.c | 85 | ||||
-rw-r--r-- | src/main.c | 1 |
2 files changed, 86 insertions, 0 deletions
diff --git a/src/ir.c b/src/ir.c new file mode 100644 index 0000000..4c85792 --- /dev/null +++ b/src/ir.c | |||
@@ -0,0 +1,85 @@ | |||
1 | typedef enum Operator { | ||
2 | // Arithmetic ops. | ||
3 | OP_ADD, | ||
4 | OP_SUB, | ||
5 | OP_MUL, | ||
6 | OP_DIV, | ||
7 | OP_MOD, | ||
8 | |||
9 | // Load/store/copy operations. | ||
10 | OP_LD8, | ||
11 | OP_LD16, | ||
12 | OP_LD32, | ||
13 | OP_LD64, | ||
14 | OP_ST8, | ||
15 | OP_ST16, | ||
16 | OP_ST32, | ||
17 | OP_ST64, | ||
18 | OP_CP8, | ||
19 | OP_CP16, | ||
20 | OP_CP32, | ||
21 | OP_CP64, | ||
22 | |||
23 | // Bit fiddling operations. | ||
24 | OP_NOT, | ||
25 | OP_AND, | ||
26 | OP_OR, | ||
27 | OP_XOR, | ||
28 | OP_LSHIFT, | ||
29 | OP_RSHIFT, | ||
30 | OP_LROT, | ||
31 | OP_RROT, | ||
32 | |||
33 | // (Un)conditional jump operations. | ||
34 | OP_LABEL, | ||
35 | OP_JMP, | ||
36 | OP_JMP_EQ, | ||
37 | OP_JMP_NEQ, | ||
38 | OP_JMP_GT, | ||
39 | OP_JMP_LT, | ||
40 | OP_JMP_GE, | ||
41 | OP_JMP_LE, | ||
42 | } Operator; | ||
43 | |||
44 | typedef enum OperandType { | ||
45 | OP_TYPE_REG, | ||
46 | OP_TYPE_CONST, | ||
47 | OP_TYPE_LABEL, | ||
48 | } OperandType; | ||
49 | |||
50 | typedef struct Operand { | ||
51 | OperandType type; | ||
52 | union { | ||
53 | struct { | ||
54 | size_t num; | ||
55 | } reg; | ||
56 | |||
57 | struct { | ||
58 | size_t num; | ||
59 | } label; | ||
60 | |||
61 | struct { | ||
62 | union { | ||
63 | u64 uval; | ||
64 | s64 sval; | ||
65 | }; | ||
66 | } const; | ||
67 | }; | ||
68 | } Operand; | ||
69 | |||
70 | typedef struct Instruction { | ||
71 | Operator op; | ||
72 | Operand dst; | ||
73 | Operand src_a; | ||
74 | Operand src_b; | ||
75 | } Instruction; | ||
76 | |||
77 | typedef struct LineInfo { | ||
78 | size_t line; | ||
79 | size_t col; | ||
80 | } LineInfo; | ||
81 | |||
82 | typedef struct ProgramBASM { | ||
83 | Instruction *inst; | ||
84 | LineInfo *lines; | ||
85 | } ProgramBASM; | ||
@@ -10,6 +10,7 @@ | |||
10 | #include "nodes.c" | 10 | #include "nodes.c" |
11 | #include "parser.c" | 11 | #include "parser.c" |
12 | #include "semantic.c" | 12 | #include "semantic.c" |
13 | #include "ir.c" | ||
13 | #include "viz.c" | 14 | #include "viz.c" |
14 | 15 | ||
15 | typedef enum ExecMode { | 16 | typedef enum ExecMode { |