From 268ad7e0db894c8fbc445a0ec64bb50d0d0b7c48 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Fri, 22 Apr 2022 19:38:09 -0300 Subject: Introduce scaffolding for basm IR --- src/ir.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 1 + 2 files changed, 86 insertions(+) create mode 100644 src/ir.c 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 @@ +typedef enum Operator { + // Arithmetic ops. + OP_ADD, + OP_SUB, + OP_MUL, + OP_DIV, + OP_MOD, + + // Load/store/copy operations. + OP_LD8, + OP_LD16, + OP_LD32, + OP_LD64, + OP_ST8, + OP_ST16, + OP_ST32, + OP_ST64, + OP_CP8, + OP_CP16, + OP_CP32, + OP_CP64, + + // Bit fiddling operations. + OP_NOT, + OP_AND, + OP_OR, + OP_XOR, + OP_LSHIFT, + OP_RSHIFT, + OP_LROT, + OP_RROT, + + // (Un)conditional jump operations. + OP_LABEL, + OP_JMP, + OP_JMP_EQ, + OP_JMP_NEQ, + OP_JMP_GT, + OP_JMP_LT, + OP_JMP_GE, + OP_JMP_LE, +} Operator; + +typedef enum OperandType { + OP_TYPE_REG, + OP_TYPE_CONST, + OP_TYPE_LABEL, +} OperandType; + +typedef struct Operand { + OperandType type; + union { + struct { + size_t num; + } reg; + + struct { + size_t num; + } label; + + struct { + union { + u64 uval; + s64 sval; + }; + } const; + }; +} Operand; + +typedef struct Instruction { + Operator op; + Operand dst; + Operand src_a; + Operand src_b; +} Instruction; + +typedef struct LineInfo { + size_t line; + size_t col; +} LineInfo; + +typedef struct ProgramBASM { + Instruction *inst; + LineInfo *lines; +} 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 @@ #include "nodes.c" #include "parser.c" #include "semantic.c" +#include "ir.c" #include "viz.c" typedef enum ExecMode { -- cgit v1.2.1