diff options
| author | Mistivia <i@mistivia.com> | 2025-03-25 17:31:08 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-03-25 17:31:08 +0800 |
| commit | 39e2a605f6d8ebcc3cb454daae3d0a4298df2eb6 (patch) | |
| tree | 9d0933cdcb0d4bb2d80316f106611a4aa9024dd4 /src/as_codegen.c | |
| parent | d1195499338ba4b6536f8a6a56564622eb07469f (diff) | |
add codegen
Diffstat (limited to 'src/as_codegen.c')
| -rw-r--r-- | src/as_codegen.c | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/src/as_codegen.c b/src/as_codegen.c new file mode 100644 index 0000000..98092d7 --- /dev/null +++ b/src/as_codegen.c @@ -0,0 +1,94 @@ +#include "as_codegen.h" +#include "as_op.h" +#include "fvm.h" + +struct bytearray *new_bytearray(struct allocator *alct); +void bytearray_emit8(struct bytearray *self, int8_t data); +void bytearray_emit64(struct bytearray *self, int64_t data); + + +int8_t op_bytecode(enum op op) { + if (op == OP_SSP) return (int8_t)FVM_OP_SSP; + if (op == OP_SP) return (int8_t)FVM_OP_SP; + if (op == OP_SBP) return (int8_t)FVM_OP_SBP; + if (op == OP_BP) return (int8_t)FVM_OP_BP; + if (op == OP_PC) return (int8_t)FVM_OP_PC; + if (op == OP_RV) return (int8_t)FVM_OP_RV; + if (op == OP_SRV) return (int8_t)FVM_OP_SRV; + + if (op == OP_DUP) return (int8_t)FVM_OP_DUP; + if (op == OP_POP) return (int8_t)FVM_OP_POP; + if (op == OP_SWAP) return (int8_t)FVM_OP_SWAP; + if (op == OP_OVER) return (int8_t)FVM_OP_OVER; + if (op == OP_ROT) return (int8_t)FVM_OP_ROT; + + if (op == OP_ADD) return (int8_t)FVM_OP_ADD; + if (op == OP_SUB) return (int8_t)FVM_OP_SUB; + if (op == OP_DIV) return (int8_t)FVM_OP_DIV; + if (op == OP_MUL) return (int8_t)FVM_OP_MUL; + if (op == OP_MOD) return (int8_t)FVM_OP_MOD; + + if (op == OP_SHR) return (int8_t)FVM_OP_SHR; + if (op == OP_SHL) return (int8_t)FVM_OP_SHL; + if (op == OP_SAR) return (int8_t)FVM_OP_SAR; + + if (op == OP_AND) return (int8_t)FVM_OP_AND; + if (op == OP_OR) return (int8_t)FVM_OP_OR; + if (op == OP_NOT) return (int8_t)FVM_OP_NOT; + + if (op == OP_BITAND) return (int8_t)FVM_OP_BITAND; + if (op == OP_BITOR) return (int8_t)FVM_OP_BITOR; + if (op == OP_XOR) return (int8_t)FVM_OP_XOR; + if (op == OP_INVERT) return (int8_t)FVM_OP_INVERT; + + if (op == OP_GT) return (int8_t)FVM_OP_GT; + if (op == OP_LT) return (int8_t)FVM_OP_LT; + if (op == OP_GE) return (int8_t)FVM_OP_GE; + if (op == OP_LE) return (int8_t)FVM_OP_LE; + if (op == OP_EQ) return (int8_t)FVM_OP_EQ; + if (op == OP_NEQ) return (int8_t)FVM_OP_NEQ; + + if (op == OP_RET) return (int8_t)FVM_OP_RET; + + if (op == OP_FADD) return (int8_t)FVM_OP_FADD; + if (op == OP_FSUB) return (int8_t)FVM_OP_FSUB; + if (op == OP_FDIV) return (int8_t)FVM_OP_FDIV; + if (op == OP_FMUL) return (int8_t)FVM_OP_FMUL; + + if (op == OP_FGT) return (int8_t)FVM_OP_FGT; + if (op == OP_FLT) return (int8_t)FVM_OP_FLT; + if (op == OP_FGE) return (int8_t)FVM_OP_FGE; + if (op == OP_FLE) return (int8_t)FVM_OP_FLE; + if (op == OP_FEQ) return (int8_t)FVM_OP_FEQ; + if (op == OP_FNEQ) return (int8_t)FVM_OP_FNEQ; + + if (op == OP_FTI) return (int8_t)FVM_OP_FTI; + if (op == OP_ITF) return (int8_t)FVM_OP_ITF; + + if (op == OP_EXIT) return (int8_t)FVM_OP_EXIT; + + return (int8_t)-1; +} + +struct result codegen(struct allocator *alct, struct prog *prog, struct sym_table tbl) { + struct stmt ** stmts = prog->stmts->stmts; + size_t offset = 0; + struct bytearray* output = new_bytearray(alct); + for (int i = 0; ; i++) { + if (stmts[i] == NULL) { + break; + } + struct instr *instruction = stmts[i]->instr; + enum op op = instruction->op; + if (op_size(op) == 1) { + int8_t code = op_bytecode(op); + bytearray_emit8(output, code); + offset += 1; + } else if (op_size(op) == 9) { + + } else if (op_size(op) == 10) { + + } + } + return ok(output); +} |
