aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2022-04-22 19:38:09 -0300
committerBad Diode <bd@badd10de.dev>2022-04-22 19:38:09 -0300
commit268ad7e0db894c8fbc445a0ec64bb50d0d0b7c48 (patch)
treea4e1549e957d976ef7dc02beb4e1852895f35ef4
parentc0dbfec91dd9781afa867675dd7a1d9cdc034c74 (diff)
downloadbdl-268ad7e0db894c8fbc445a0ec64bb50d0d0b7c48.tar.gz
bdl-268ad7e0db894c8fbc445a0ec64bb50d0d0b7c48.zip
Introduce scaffolding for basm IR
-rw-r--r--src/ir.c85
-rw-r--r--src/main.c1
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 @@
1typedef 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
44typedef enum OperandType {
45 OP_TYPE_REG,
46 OP_TYPE_CONST,
47 OP_TYPE_LABEL,
48} OperandType;
49
50typedef 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
70typedef struct Instruction {
71 Operator op;
72 Operand dst;
73 Operand src_a;
74 Operand src_b;
75} Instruction;
76
77typedef struct LineInfo {
78 size_t line;
79 size_t col;
80} LineInfo;
81
82typedef struct ProgramBASM {
83 Instruction *inst;
84 LineInfo *lines;
85} ProgramBASM;
diff --git a/src/main.c b/src/main.c
index 7878c57..0fbabdb 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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
15typedef enum ExecMode { 16typedef enum ExecMode {