From 8e2a4b534263db8f8d9ddf7b528af32784b3d481 Mon Sep 17 00:00:00 2001 From: Shaw Summa Date: Sat, 26 Oct 2024 04:17:00 -0400 Subject: [PATCH] tmp Signed-off-by: Shaw Summa --- main/minivm.c | 5 +- main/primes.py | 4 +- vendor/mimalloc | 2 +- vendor/tree-sitter | 2 +- vm/ast/build.c | 1 + vm/ast/comp.c | 6 +- vm/backend/backend.c | 287 +++++++++++++++++++++++-------------------- vm/backend/backend.h | 1 - vm/errors.c | 1 + vm/errors.h | 3 +- vm/gc.c | 6 +- vm/gc.h | 2 +- vm/io.c | 4 +- vm/io.h | 6 +- vm/ir.c | 3 +- vm/ir.h | 7 +- vm/lib.c | 50 +++++++- vm/lib.h | 65 +--------- vm/lua/repl.c | 7 +- vm/lua/repl.h | 2 +- vm/math.h | 2 +- vm/obj.c | 4 + vm/obj.h | 2 +- vm/obj.inc | 2 - vm/primes.inc | 2 + vm/std.c | 5 + vm/std.h | 2 +- vm/vm.c | 6 +- 28 files changed, 258 insertions(+), 231 deletions(-) diff --git a/main/minivm.c b/main/minivm.c index 7c5c8b0b..134d4807 100644 --- a/main/minivm.c +++ b/main/minivm.c @@ -1,11 +1,12 @@ -#include "../vm/ast/comp.h" #include "../vm/backend/backend.h" #include "../vm/vm.h" #include "../vm/ir.h" #include "../vm/io.h" -#include "../vm/gc.h" #include "../vm/std.h" #include "../vm/lua/repl.h" +#include "../vm/lib.h" + +#include #if VM_USE_SPALL #define SPALL_AUTO_IMPLEMENTATION diff --git a/main/primes.py b/main/primes.py index 1332cc48..0b2fcc41 100644 --- a/main/primes.py +++ b/main/primes.py @@ -1,9 +1,8 @@ import sympy -import math max_width = 60 n = 53 -ls = [1, 3, 7, 13, 29] +ls = [2, 5, 11, 23] with open('vm/primes.inc', 'w') as f: p32 = 2 ** 32 @@ -18,6 +17,7 @@ ml = max(map(len, map(str, ls))) n = (max_width - 5) // (ml + 2) f.write(f'// generated by: python3 main/primes.py\n\n') + f.write(f'#include \n\n') f.write(f'static uint32_t vm_primes_table[{len(ls)}] = ' + '{\n ') for i, p in enumerate(ls): s = str(p) diff --git a/vendor/mimalloc b/vendor/mimalloc index e55ae0ae..532904c8 160000 --- a/vendor/mimalloc +++ b/vendor/mimalloc @@ -1 +1 @@ -Subproject commit e55ae0aeb76f5205edce57b97031be6aa80f962a +Subproject commit 532904c85c23f777f29650eb48bd61f1115e2bab diff --git a/vendor/tree-sitter b/vendor/tree-sitter index c070c927..25c71891 160000 --- a/vendor/tree-sitter +++ b/vendor/tree-sitter @@ -1 +1 @@ -Subproject commit c070c92722c943d7eb6215dc1a97c833a30bc3e5 +Subproject commit 25c7189180849be27b1e552d27f0488e3bd5900d diff --git a/vm/ast/build.c b/vm/ast/build.c index 67d30793..532094df 100644 --- a/vm/ast/build.c +++ b/vm/ast/build.c @@ -1,4 +1,5 @@ +#include #include "build.h" #define VM_MACRO_SELECT(_0, _1, _2, NAME, ...) NAME diff --git a/vm/ast/comp.c b/vm/ast/comp.c index 4f42c981..8749534b 100644 --- a/vm/ast/comp.c +++ b/vm/ast/comp.c @@ -1,8 +1,10 @@ +#include + #include "comp.h" #include "ast.h" #include "build.h" -#include "print.h" +#include "../ir.h" #include "../gc.h" #include "../errors.h" @@ -116,10 +118,12 @@ static vm_ir_arg_t *vm_ast_args(size_t nargs, ...) { } static vm_ir_block_t *vm_ast_comp_new_block(vm_ast_comp_t *comp) { + static const int64_t no_code[1] = { 0 }; vm_ir_block_t *block = vm_malloc(sizeof(vm_ir_block_t)); *block = (vm_ir_block_t){ .id = (uint32_t)comp->vm->nblocks++, .range = comp->range, + .code = &no_code[0], // .nregs = VM_NREGS, }; vm_gc_add(comp->vm, vm_obj_of_block(block)); diff --git a/vm/backend/backend.c b/vm/backend/backend.c index 2e8646ef..06133846 100644 --- a/vm/backend/backend.c +++ b/vm/backend/backend.c @@ -1,9 +1,14 @@ +#include +#include + +#include "../lib.h" #include "../gc.h" #include "../ir.h" #include "../obj.h" #include "../vm.h" #include "../math.h" +#include "../io.h" #define VM_INLINE inline @@ -34,6 +39,11 @@ #define VM_OPCODE_DEBUG(s) VM_OPCODE_SPALL_BEGIN(s); #endif + +typedef ptrdiff_t vm_run_repl_label_t; +#define vm_run_repl_ref(name) (&&name - &&VM_RUN_REPL_BASE) +#define vm_run_repl_goto(ptr) goto *((ptr) + &&VM_RUN_REPL_BASE) + #define vm_backend_return(v) ({ \ vm_obj_t return_ = (v); \ VM_OPCODE_SPALL_END(); \ @@ -41,7 +51,7 @@ }) #define vm_run_repl_jump() \ VM_OPCODE_SPALL_END(); \ - goto *vm_run_repl_read(void *) + vm_run_repl_goto(vm_run_repl_read(vm_run_repl_label_t)) #define vm_backend_new_block() \ VM_OPCODE_SPALL_END(); \ goto new_block @@ -54,58 +64,58 @@ enum { VM_OP_TABLE_NEW, VM_OP_TABLE_LEN, - VM_OP_MOVE_I, + VM_OP_MOVE_C, VM_OP_MOVE_R, - VM_OP_ADD_RI, - VM_OP_ADD_IR, + VM_OP_ADD_RC, + VM_OP_ADD_CR, VM_OP_ADD_RR, - VM_OP_SUB_RI, - VM_OP_SUB_IR, + VM_OP_SUB_RC, + VM_OP_SUB_CR, VM_OP_SUB_RR, - VM_OP_MUL_RI, - VM_OP_MUL_IR, + VM_OP_MUL_RC, + VM_OP_MUL_CR, VM_OP_MUL_RR, - VM_OP_DIV_RI, - VM_OP_DIV_IR, + VM_OP_DIV_RC, + VM_OP_DIV_CR, VM_OP_DIV_RR, - VM_OP_IDIV_RI, - VM_OP_IDIV_IR, + VM_OP_IDIV_RC, + VM_OP_IDIV_CR, VM_OP_IDIV_RR, - VM_OP_MOD_RI, - VM_OP_MOD_IR, + VM_OP_MOD_RC, + VM_OP_MOD_CR, VM_OP_MOD_RR, - VM_OP_POW_RI, - VM_OP_POW_IR, + VM_OP_POW_RC, + VM_OP_POW_CR, VM_OP_POW_RR, - VM_OP_CONCAT_RI, - VM_OP_CONCAT_IR, + VM_OP_CONCAT_RC, + VM_OP_CONCAT_CR, VM_OP_CONCAT_RR, VM_OP_JUMP, VM_OP_BB_R, - VM_OP_BLT_RI, - VM_OP_BLT_IR, + VM_OP_BLT_RC, + VM_OP_BLT_CR, VM_OP_BLT_RR, - VM_OP_BLE_RI, - VM_OP_BLE_IR, + VM_OP_BLE_RC, + VM_OP_BLE_CR, VM_OP_BLE_RR, - VM_OP_BEQ_RI, - VM_OP_BEQ_IR, + VM_OP_BEQ_RC, + VM_OP_BEQ_CR, VM_OP_BEQ_RR, - VM_OP_RET_I, + VM_OP_RET_C, VM_OP_RET_R, VM_OP_LOAD, @@ -208,9 +218,9 @@ static VM_INLINE vm_obj_t vm_interp_concat(vm_t *vm, vm_obj_t v1, vm_obj_t v2) { }) #endif -#define vm_interp_push_op(v) vm_interp_push(void *, ptrs[v]) +#define vm_interp_push_op(v) vm_interp_push(vm_run_repl_label_t, ptrs[v]) -void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_ir_block_t *block) { +void *vm_interp_renumber_block(vm_t *vm, const vm_run_repl_label_t *ptrs, vm_ir_block_t *block) { size_t alloc = 32; size_t len = 0; uint8_t *code = vm_malloc(sizeof(uint8_t) * alloc); @@ -222,7 +232,7 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_ir_block_t *block) { } case VM_IR_INSTR_OPCODE_MOVE: { if (instr.args[0].type == VM_IR_ARG_TYPE_LIT) { - vm_interp_push_op(VM_OP_MOVE_I); + vm_interp_push_op(VM_OP_MOVE_C); vm_interp_push(vm_obj_t, instr.args[0].lit); vm_interp_push(vm_interp_reg_t, instr.out.reg); } else if (instr.args[0].type == VM_IR_ARG_TYPE_REG) { @@ -239,16 +249,16 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_ir_block_t *block) { vm_obj_t v1 = instr.args[0].lit; vm_obj_t v2 = instr.args[1].lit; vm_obj_t v3 = vm_interp_add(vm, v1, v2); - vm_interp_push_op(VM_OP_MOVE_I); + vm_interp_push_op(VM_OP_MOVE_C); vm_interp_push(vm_obj_t, v3); vm_interp_push(vm_interp_reg_t, instr.out.reg); } else if (instr.args[0].type == VM_IR_ARG_TYPE_REG && instr.args[1].type == VM_IR_ARG_TYPE_LIT) { - vm_interp_push_op(VM_OP_ADD_RI); + vm_interp_push_op(VM_OP_ADD_RC); vm_interp_push(vm_interp_reg_t, instr.args[0].reg); vm_interp_push(vm_obj_t, instr.args[1].lit); vm_interp_push(vm_interp_reg_t, instr.out.reg); } else if (instr.args[0].type == VM_IR_ARG_TYPE_LIT && instr.args[1].type == VM_IR_ARG_TYPE_REG) { - vm_interp_push_op(VM_OP_ADD_IR); + vm_interp_push_op(VM_OP_ADD_CR); vm_interp_push(vm_obj_t, instr.args[0].lit); vm_interp_push(vm_interp_reg_t, instr.args[1].reg); vm_interp_push(vm_interp_reg_t, instr.out.reg); @@ -267,16 +277,16 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_ir_block_t *block) { vm_obj_t v1 = instr.args[0].lit; vm_obj_t v2 = instr.args[1].lit; vm_obj_t v3 = vm_interp_sub(vm, v1, v2); - vm_interp_push_op(VM_OP_MOVE_I); + vm_interp_push_op(VM_OP_MOVE_C); vm_interp_push(vm_obj_t, v3); vm_interp_push(vm_interp_reg_t, instr.out.reg); } else if (instr.args[0].type == VM_IR_ARG_TYPE_REG && instr.args[1].type == VM_IR_ARG_TYPE_LIT) { - vm_interp_push_op(VM_OP_SUB_RI); + vm_interp_push_op(VM_OP_SUB_RC); vm_interp_push(vm_interp_reg_t, instr.args[0].reg); vm_interp_push(vm_obj_t, instr.args[1].lit); vm_interp_push(vm_interp_reg_t, instr.out.reg); } else if (instr.args[0].type == VM_IR_ARG_TYPE_LIT && instr.args[1].type == VM_IR_ARG_TYPE_REG) { - vm_interp_push_op(VM_OP_SUB_IR); + vm_interp_push_op(VM_OP_SUB_CR); vm_interp_push(vm_obj_t, instr.args[0].lit); vm_interp_push(vm_interp_reg_t, instr.args[1].reg); vm_interp_push(vm_interp_reg_t, instr.out.reg); @@ -295,16 +305,16 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_ir_block_t *block) { vm_obj_t v1 = instr.args[0].lit; vm_obj_t v2 = instr.args[1].lit; vm_obj_t v3 = vm_interp_mul(vm, v1, v2); - vm_interp_push_op(VM_OP_MOVE_I); + vm_interp_push_op(VM_OP_MOVE_C); vm_interp_push(vm_obj_t, v3); vm_interp_push(vm_interp_reg_t, instr.out.reg); } else if (instr.args[0].type == VM_IR_ARG_TYPE_REG && instr.args[1].type == VM_IR_ARG_TYPE_LIT) { - vm_interp_push_op(VM_OP_MUL_RI); + vm_interp_push_op(VM_OP_MUL_RC); vm_interp_push(vm_interp_reg_t, instr.args[0].reg); vm_interp_push(vm_obj_t, instr.args[1].lit); vm_interp_push(vm_interp_reg_t, instr.out.reg); } else if (instr.args[0].type == VM_IR_ARG_TYPE_LIT && instr.args[1].type == VM_IR_ARG_TYPE_REG) { - vm_interp_push_op(VM_OP_MUL_IR); + vm_interp_push_op(VM_OP_MUL_CR); vm_interp_push(vm_obj_t, instr.args[0].lit); vm_interp_push(vm_interp_reg_t, instr.args[1].reg); vm_interp_push(vm_interp_reg_t, instr.out.reg); @@ -323,16 +333,16 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_ir_block_t *block) { vm_obj_t v1 = instr.args[0].lit; vm_obj_t v2 = instr.args[1].lit; vm_obj_t v3 = vm_interp_div(vm, v1, v2); - vm_interp_push_op(VM_OP_MOVE_I); + vm_interp_push_op(VM_OP_MOVE_C); vm_interp_push(vm_obj_t, v3); vm_interp_push(vm_interp_reg_t, instr.out.reg); } else if (instr.args[0].type == VM_IR_ARG_TYPE_REG && instr.args[1].type == VM_IR_ARG_TYPE_LIT) { - vm_interp_push_op(VM_OP_DIV_RI); + vm_interp_push_op(VM_OP_DIV_RC); vm_interp_push(vm_interp_reg_t, instr.args[0].reg); vm_interp_push(vm_obj_t, instr.args[1].lit); vm_interp_push(vm_interp_reg_t, instr.out.reg); } else if (instr.args[0].type == VM_IR_ARG_TYPE_LIT && instr.args[1].type == VM_IR_ARG_TYPE_REG) { - vm_interp_push_op(VM_OP_DIV_IR); + vm_interp_push_op(VM_OP_DIV_CR); vm_interp_push(vm_obj_t, instr.args[0].lit); vm_interp_push(vm_interp_reg_t, instr.args[1].reg); vm_interp_push(vm_interp_reg_t, instr.out.reg); @@ -351,16 +361,16 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_ir_block_t *block) { vm_obj_t v1 = instr.args[0].lit; vm_obj_t v2 = instr.args[1].lit; vm_obj_t v3 = vm_interp_idiv(vm, v1, v2); - vm_interp_push_op(VM_OP_MOVE_I); + vm_interp_push_op(VM_OP_MOVE_C); vm_interp_push(vm_obj_t, v3); vm_interp_push(vm_interp_reg_t, instr.out.reg); } else if (instr.args[0].type == VM_IR_ARG_TYPE_REG && instr.args[1].type == VM_IR_ARG_TYPE_LIT) { - vm_interp_push_op(VM_OP_IDIV_RI); + vm_interp_push_op(VM_OP_IDIV_RC); vm_interp_push(vm_interp_reg_t, instr.args[0].reg); vm_interp_push(vm_obj_t, instr.args[1].lit); vm_interp_push(vm_interp_reg_t, instr.out.reg); } else if (instr.args[0].type == VM_IR_ARG_TYPE_LIT && instr.args[1].type == VM_IR_ARG_TYPE_REG) { - vm_interp_push_op(VM_OP_IDIV_IR); + vm_interp_push_op(VM_OP_IDIV_CR); vm_interp_push(vm_obj_t, instr.args[0].lit); vm_interp_push(vm_interp_reg_t, instr.args[1].reg); vm_interp_push(vm_interp_reg_t, instr.out.reg); @@ -379,16 +389,16 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_ir_block_t *block) { vm_obj_t v1 = instr.args[0].lit; vm_obj_t v2 = instr.args[1].lit; vm_obj_t v3 = vm_interp_mod(vm, v1, v2); - vm_interp_push_op(VM_OP_MOVE_I); + vm_interp_push_op(VM_OP_MOVE_C); vm_interp_push(vm_obj_t, v3); vm_interp_push(vm_interp_reg_t, instr.out.reg); } else if (instr.args[0].type == VM_IR_ARG_TYPE_REG && instr.args[1].type == VM_IR_ARG_TYPE_LIT) { - vm_interp_push_op(VM_OP_MOD_RI); + vm_interp_push_op(VM_OP_MOD_RC); vm_interp_push(vm_interp_reg_t, instr.args[0].reg); vm_interp_push(vm_obj_t, instr.args[1].lit); vm_interp_push(vm_interp_reg_t, instr.out.reg); } else if (instr.args[0].type == VM_IR_ARG_TYPE_LIT && instr.args[1].type == VM_IR_ARG_TYPE_REG) { - vm_interp_push_op(VM_OP_MOD_IR); + vm_interp_push_op(VM_OP_MOD_CR); vm_interp_push(vm_obj_t, instr.args[0].lit); vm_interp_push(vm_interp_reg_t, instr.args[1].reg); vm_interp_push(vm_interp_reg_t, instr.out.reg); @@ -407,16 +417,16 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_ir_block_t *block) { vm_obj_t v1 = instr.args[0].lit; vm_obj_t v2 = instr.args[1].lit; vm_obj_t v3 = vm_interp_pow(vm, v1, v2); - vm_interp_push_op(VM_OP_MOVE_I); + vm_interp_push_op(VM_OP_MOVE_C); vm_interp_push(vm_obj_t, v3); vm_interp_push(vm_interp_reg_t, instr.out.reg); } else if (instr.args[0].type == VM_IR_ARG_TYPE_REG && instr.args[1].type == VM_IR_ARG_TYPE_LIT) { - vm_interp_push_op(VM_OP_POW_RI); + vm_interp_push_op(VM_OP_POW_RC); vm_interp_push(vm_interp_reg_t, instr.args[0].reg); vm_interp_push(vm_obj_t, instr.args[1].lit); vm_interp_push(vm_interp_reg_t, instr.out.reg); } else if (instr.args[0].type == VM_IR_ARG_TYPE_LIT && instr.args[1].type == VM_IR_ARG_TYPE_REG) { - vm_interp_push_op(VM_OP_POW_IR); + vm_interp_push_op(VM_OP_POW_CR); vm_interp_push(vm_obj_t, instr.args[0].lit); vm_interp_push(vm_interp_reg_t, instr.args[1].reg); vm_interp_push(vm_interp_reg_t, instr.out.reg); @@ -435,16 +445,16 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_ir_block_t *block) { vm_obj_t v1 = instr.args[0].lit; vm_obj_t v2 = instr.args[1].lit; vm_obj_t v3 = vm_interp_concat(vm, v1, v2); - vm_interp_push_op(VM_OP_MOVE_I); + vm_interp_push_op(VM_OP_MOVE_C); vm_interp_push(vm_obj_t, v3); vm_interp_push(vm_interp_reg_t, instr.out.reg); } else if (instr.args[0].type == VM_IR_ARG_TYPE_REG && instr.args[1].type == VM_IR_ARG_TYPE_LIT) { - vm_interp_push_op(VM_OP_CONCAT_RI); + vm_interp_push_op(VM_OP_CONCAT_RC); vm_interp_push(vm_interp_reg_t, instr.args[0].reg); vm_interp_push(vm_obj_t, instr.args[1].lit); vm_interp_push(vm_interp_reg_t, instr.out.reg); } else if (instr.args[0].type == VM_IR_ARG_TYPE_LIT && instr.args[1].type == VM_IR_ARG_TYPE_REG) { - vm_interp_push_op(VM_OP_CONCAT_IR); + vm_interp_push_op(VM_OP_CONCAT_CR); vm_interp_push(vm_obj_t, instr.args[0].lit); vm_interp_push(vm_interp_reg_t, instr.args[1].reg); vm_interp_push(vm_interp_reg_t, instr.out.reg); @@ -533,13 +543,13 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_ir_block_t *block) { vm_interp_push(vm_ir_block_t *, branch.targets[1]); } } else if (branch.args[0].type == VM_IR_ARG_TYPE_REG && branch.args[1].type == VM_IR_ARG_TYPE_LIT) { - vm_interp_push_op(VM_OP_BLT_RI); + vm_interp_push_op(VM_OP_BLT_RC); vm_interp_push(vm_interp_reg_t, branch.args[0].reg); vm_interp_push(vm_obj_t, branch.args[1].lit); vm_interp_push(vm_ir_block_t *, branch.targets[0]); vm_interp_push(vm_ir_block_t *, branch.targets[1]); } else if (branch.args[0].type == VM_IR_ARG_TYPE_LIT && branch.args[1].type == VM_IR_ARG_TYPE_REG) { - vm_interp_push_op(VM_OP_BLT_IR); + vm_interp_push_op(VM_OP_BLT_CR); vm_interp_push(vm_obj_t, branch.args[0].lit); vm_interp_push(vm_interp_reg_t, branch.args[1].reg); vm_interp_push(vm_ir_block_t *, branch.targets[0]); @@ -567,13 +577,13 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_ir_block_t *block) { vm_interp_push(vm_ir_block_t *, branch.targets[1]); } } else if (branch.args[0].type == VM_IR_ARG_TYPE_REG && branch.args[1].type == VM_IR_ARG_TYPE_LIT) { - vm_interp_push_op(VM_OP_BLE_RI); + vm_interp_push_op(VM_OP_BLE_RC); vm_interp_push(vm_interp_reg_t, branch.args[0].reg); vm_interp_push(vm_obj_t, branch.args[1].lit); vm_interp_push(vm_ir_block_t *, branch.targets[0]); vm_interp_push(vm_ir_block_t *, branch.targets[1]); } else if (branch.args[0].type == VM_IR_ARG_TYPE_LIT && branch.args[1].type == VM_IR_ARG_TYPE_REG) { - vm_interp_push_op(VM_OP_BLE_IR); + vm_interp_push_op(VM_OP_BLE_CR); vm_interp_push(vm_obj_t, branch.args[0].lit); vm_interp_push(vm_interp_reg_t, branch.args[1].reg); vm_interp_push(vm_ir_block_t *, branch.targets[0]); @@ -601,13 +611,13 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_ir_block_t *block) { vm_interp_push(vm_ir_block_t *, branch.targets[1]); } } else if (branch.args[0].type == VM_IR_ARG_TYPE_REG && branch.args[1].type == VM_IR_ARG_TYPE_LIT) { - vm_interp_push_op(VM_OP_BEQ_RI); + vm_interp_push_op(VM_OP_BEQ_RC); vm_interp_push(vm_interp_reg_t, branch.args[0].reg); vm_interp_push(vm_obj_t, branch.args[1].lit); vm_interp_push(vm_ir_block_t *, branch.targets[0]); vm_interp_push(vm_ir_block_t *, branch.targets[1]); } else if (branch.args[0].type == VM_IR_ARG_TYPE_LIT && branch.args[1].type == VM_IR_ARG_TYPE_REG) { - vm_interp_push_op(VM_OP_BEQ_IR); + vm_interp_push_op(VM_OP_BEQ_CR); vm_interp_push(vm_obj_t, branch.args[0].lit); vm_interp_push(vm_interp_reg_t, branch.args[1].reg); vm_interp_push(vm_ir_block_t *, branch.targets[0]); @@ -625,7 +635,7 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_ir_block_t *block) { } case VM_IR_BRANCH_OPCODE_RETURN: { if (branch.args[0].type == VM_IR_ARG_TYPE_LIT) { - vm_interp_push_op(VM_OP_RET_I); + vm_interp_push_op(VM_OP_RET_C); vm_interp_push(vm_obj_t, branch.args[0].lit); } else if (branch.args[0].type == VM_IR_ARG_TYPE_REG) { vm_interp_push_op(VM_OP_RET_R); @@ -730,9 +740,9 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_ir_block_t *block) { #define vm_run_repl_out(value) (regs[vm_run_repl_read(vm_interp_reg_t)] = (value)) -size_t vm_interp_renumber_blocks(vm_t *vm, void **ptrs, vm_ir_block_t *block) { +size_t vm_interp_renumber_blocks(vm_t *vm, const vm_run_repl_label_t *ptrs, vm_ir_block_t *block) { size_t nregs = block->nregs; - if (block->code == NULL) { + if (* (const vm_run_repl_label_t *) block->code == 0) { block->code = vm_interp_renumber_block(vm, &ptrs[0], block); for (size_t i = 0; i < 2; i++) { vm_ir_block_t *target = block->branch.targets[i]; @@ -749,59 +759,59 @@ size_t vm_interp_renumber_blocks(vm_t *vm, void **ptrs, vm_ir_block_t *block) { return nregs; } + vm_obj_t vm_run_repl_inner(vm_t *vm, vm_ir_block_t *block) { vm_obj_t *regs = vm->regs; - static void *ptrs[VM_MAX_OP] = { - [VM_OP_TABLE_SET] = &&VM_OP_TABLE_SET, - [VM_OP_TABLE_NEW] = &&VM_OP_TABLE_NEW, - [VM_OP_TABLE_LEN] = &&VM_OP_TABLE_LEN, - [VM_OP_MOVE_I] = &&VM_OP_MOVE_I, - [VM_OP_MOVE_R] = &&VM_OP_MOVE_R, - [VM_OP_ADD_RI] = &&VM_OP_ADD_RI, - [VM_OP_ADD_IR] = &&VM_OP_ADD_IR, - [VM_OP_ADD_RR] = &&VM_OP_ADD_RR, - [VM_OP_SUB_RI] = &&VM_OP_SUB_RI, - [VM_OP_SUB_IR] = &&VM_OP_SUB_IR, - [VM_OP_SUB_RR] = &&VM_OP_SUB_RR, - [VM_OP_MUL_RI] = &&VM_OP_MUL_RI, - [VM_OP_MUL_IR] = &&VM_OP_MUL_IR, - [VM_OP_MUL_RR] = &&VM_OP_MUL_RR, - [VM_OP_DIV_RI] = &&VM_OP_DIV_RI, - [VM_OP_DIV_IR] = &&VM_OP_DIV_IR, - [VM_OP_DIV_RR] = &&VM_OP_DIV_RR, - [VM_OP_IDIV_RI] = &&VM_OP_IDIV_RI, - [VM_OP_IDIV_IR] = &&VM_OP_IDIV_IR, - [VM_OP_IDIV_RR] = &&VM_OP_IDIV_RR, - [VM_OP_MOD_RI] = &&VM_OP_MOD_RI, - [VM_OP_MOD_IR] = &&VM_OP_MOD_IR, - [VM_OP_MOD_RR] = &&VM_OP_MOD_RR, - [VM_OP_POW_RI] = &&VM_OP_POW_RI, - [VM_OP_POW_IR] = &&VM_OP_POW_IR, - [VM_OP_POW_RR] = &&VM_OP_POW_RR, - [VM_OP_CONCAT_RI] = &&VM_OP_CONCAT_RI, - [VM_OP_CONCAT_IR] = &&VM_OP_CONCAT_IR, - [VM_OP_CONCAT_RR] = &&VM_OP_CONCAT_RR, - [VM_OP_JUMP] = &&VM_OP_JUMP, - [VM_OP_BB_R] = &&VM_OP_BB_R, - [VM_OP_BLT_RI] = &&VM_OP_BLT_RI, - [VM_OP_BLT_IR] = &&VM_OP_BLT_IR, - [VM_OP_BLT_RR] = &&VM_OP_BLT_RR, - [VM_OP_BLE_RI] = &&VM_OP_BLE_RI, - [VM_OP_BLE_IR] = &&VM_OP_BLE_IR, - [VM_OP_BLE_RR] = &&VM_OP_BLE_RR, - [VM_OP_BEQ_RI] = &&VM_OP_BEQ_RI, - [VM_OP_BEQ_IR] = &&VM_OP_BEQ_IR, - [VM_OP_BEQ_RR] = &&VM_OP_BEQ_RR, - [VM_OP_RET_I] = &&VM_OP_RET_I, - [VM_OP_RET_R] = &&VM_OP_RET_R, - [VM_OP_LOAD] = &&VM_OP_LOAD, - [VM_OP_GET] = &&VM_OP_GET, - [VM_OP_CALL] = &&VM_OP_CALL, + static const vm_run_repl_label_t ptrs[VM_MAX_OP] = { + [VM_OP_TABLE_SET] = vm_run_repl_ref(VM_OP_TABLE_SET), + [VM_OP_TABLE_NEW] = vm_run_repl_ref(VM_OP_TABLE_NEW), + [VM_OP_TABLE_LEN] = vm_run_repl_ref(VM_OP_TABLE_LEN), + [VM_OP_MOVE_C] = vm_run_repl_ref(VM_OP_MOVE_C), + [VM_OP_MOVE_R] = vm_run_repl_ref(VM_OP_MOVE_R), + [VM_OP_ADD_RC] = vm_run_repl_ref(VM_OP_ADD_RC), + [VM_OP_ADD_CR] = vm_run_repl_ref(VM_OP_ADD_CR), + [VM_OP_ADD_RR] = vm_run_repl_ref(VM_OP_ADD_RR), + [VM_OP_SUB_RC] = vm_run_repl_ref(VM_OP_SUB_RC), + [VM_OP_SUB_CR] = vm_run_repl_ref(VM_OP_SUB_CR), + [VM_OP_SUB_RR] = vm_run_repl_ref(VM_OP_SUB_RR), + [VM_OP_MUL_RC] = vm_run_repl_ref(VM_OP_MUL_RC), + [VM_OP_MUL_CR] = vm_run_repl_ref(VM_OP_MUL_CR), + [VM_OP_MUL_RR] = vm_run_repl_ref(VM_OP_MUL_RR), + [VM_OP_DIV_RC] = vm_run_repl_ref(VM_OP_DIV_RC), + [VM_OP_DIV_CR] = vm_run_repl_ref(VM_OP_DIV_CR), + [VM_OP_DIV_RR] = vm_run_repl_ref(VM_OP_DIV_RR), + [VM_OP_IDIV_RC] = vm_run_repl_ref(VM_OP_IDIV_RC), + [VM_OP_IDIV_CR] = vm_run_repl_ref(VM_OP_IDIV_CR), + [VM_OP_IDIV_RR] = vm_run_repl_ref(VM_OP_IDIV_RR), + [VM_OP_MOD_RC] = vm_run_repl_ref(VM_OP_MOD_RC), + [VM_OP_MOD_CR] = vm_run_repl_ref(VM_OP_MOD_CR), + [VM_OP_MOD_RR] = vm_run_repl_ref(VM_OP_MOD_RR), + [VM_OP_POW_RC] = vm_run_repl_ref(VM_OP_POW_RC), + [VM_OP_POW_CR] = vm_run_repl_ref(VM_OP_POW_CR), + [VM_OP_POW_RR] = vm_run_repl_ref(VM_OP_POW_RR), + [VM_OP_CONCAT_RC] = vm_run_repl_ref(VM_OP_CONCAT_RC), + [VM_OP_CONCAT_CR] = vm_run_repl_ref(VM_OP_CONCAT_CR), + [VM_OP_CONCAT_RR] = vm_run_repl_ref(VM_OP_CONCAT_RR), + [VM_OP_JUMP] = vm_run_repl_ref(VM_OP_JUMP), + [VM_OP_BB_R] = vm_run_repl_ref(VM_OP_BB_R), + [VM_OP_BLT_RC] = vm_run_repl_ref(VM_OP_BLT_RC), + [VM_OP_BLT_CR] = vm_run_repl_ref(VM_OP_BLT_CR), + [VM_OP_BLT_RR] = vm_run_repl_ref(VM_OP_BLT_RR), + [VM_OP_BLE_RC] = vm_run_repl_ref(VM_OP_BLE_RC), + [VM_OP_BLE_CR] = vm_run_repl_ref(VM_OP_BLE_CR), + [VM_OP_BLE_RR] = vm_run_repl_ref(VM_OP_BLE_RR), + [VM_OP_BEQ_RC] = vm_run_repl_ref(VM_OP_BEQ_RC), + [VM_OP_BEQ_CR] = vm_run_repl_ref(VM_OP_BEQ_CR), + [VM_OP_BEQ_RR] = vm_run_repl_ref(VM_OP_BEQ_RR), + [VM_OP_RET_C] = vm_run_repl_ref(VM_OP_RET_C), + [VM_OP_RET_R] = vm_run_repl_ref(VM_OP_RET_R), + [VM_OP_LOAD] = vm_run_repl_ref(VM_OP_LOAD), + [VM_OP_GET] = vm_run_repl_ref(VM_OP_GET), + [VM_OP_CALL] = vm_run_repl_ref(VM_OP_CALL), }; - - size_t nregs = vm_interp_renumber_blocks(vm, ptrs, block); - vm_obj_t *next_regs = ®s[nregs]; + vm_obj_t *next_regs = ®s[block->nregs]; + #if VM_DEBUG_BACKEND_BLOCKS { vm_io_buffer_t *buf = vm_io_buffer_new(); @@ -822,10 +832,15 @@ new_block:; #endif new_block_no_print:; + const uint8_t *code = (const void *) block->code; - uint8_t *code = block->code; + vm_run_repl_goto(vm_run_repl_read(vm_run_repl_label_t)); - goto *vm_run_repl_read(void *); +VM_RUN_REPL_BASE:; + size_t nregs = vm_interp_renumber_blocks(vm, ptrs, block); + code = (const void *) block->code; + next_regs = ®s[block->nregs]; + vm_run_repl_goto(vm_run_repl_read(vm_run_repl_label_t)); VM_OP_TABLE_SET:; VM_OPCODE_DEBUG(table_set) { @@ -853,7 +868,7 @@ VM_OP_TABLE_LEN:; vm_run_repl_out(vm_obj_of_number(vm_obj_get_table(v1)->len)); vm_run_repl_jump(); } -VM_OP_MOVE_I:; +VM_OP_MOVE_C:; VM_OPCODE_DEBUG(move_i) { vm_obj_t v1 = vm_run_repl_lit(); vm_run_repl_out(v1); @@ -865,7 +880,7 @@ VM_OP_MOVE_R:; vm_run_repl_out(v1); vm_run_repl_jump(); } -VM_OP_ADD_RI:; +VM_OP_ADD_RC:; VM_OPCODE_DEBUG(add_ri) { vm_obj_t v1 = vm_run_repl_reg(); vm_obj_t v2 = vm_run_repl_lit(); @@ -876,7 +891,7 @@ VM_OP_ADD_RI:; vm_run_repl_out(v3); vm_run_repl_jump(); } -VM_OP_ADD_IR:; +VM_OP_ADD_CR:; VM_OPCODE_DEBUG(add_ir) { vm_obj_t v1 = vm_run_repl_lit(); vm_obj_t v2 = vm_run_repl_reg(); @@ -898,7 +913,7 @@ VM_OP_ADD_RR:; vm_run_repl_out(v3); vm_run_repl_jump(); } -VM_OP_SUB_RI:; +VM_OP_SUB_RC:; VM_OPCODE_DEBUG(sub_ri) { vm_obj_t v1 = vm_run_repl_reg(); vm_obj_t v2 = vm_run_repl_lit(); @@ -909,7 +924,7 @@ VM_OP_SUB_RI:; vm_run_repl_out(v3); vm_run_repl_jump(); } -VM_OP_SUB_IR:; +VM_OP_SUB_CR:; VM_OPCODE_DEBUG(sub_ir) { vm_obj_t v1 = vm_run_repl_lit(); vm_obj_t v2 = vm_run_repl_reg(); @@ -931,7 +946,7 @@ VM_OP_SUB_RR:; vm_run_repl_out(v3); vm_run_repl_jump(); } -VM_OP_MUL_RI:; +VM_OP_MUL_RC:; VM_OPCODE_DEBUG(mul_ri) { vm_obj_t v1 = vm_run_repl_reg(); vm_obj_t v2 = vm_run_repl_lit(); @@ -942,7 +957,7 @@ VM_OP_MUL_RI:; vm_run_repl_out(v3); vm_run_repl_jump(); } -VM_OP_MUL_IR:; +VM_OP_MUL_CR:; VM_OPCODE_DEBUG(mul_ir) { vm_obj_t v1 = vm_run_repl_lit(); vm_obj_t v2 = vm_run_repl_reg(); @@ -964,7 +979,7 @@ VM_OP_MUL_RR:; vm_run_repl_out(v3); vm_run_repl_jump(); } -VM_OP_DIV_RI:; +VM_OP_DIV_RC:; VM_OPCODE_DEBUG(div_ri) { vm_obj_t v1 = vm_run_repl_reg(); vm_obj_t v2 = vm_run_repl_lit(); @@ -975,7 +990,7 @@ VM_OP_DIV_RI:; vm_run_repl_out(v3); vm_run_repl_jump(); } -VM_OP_DIV_IR:; +VM_OP_DIV_CR:; VM_OPCODE_DEBUG(div_ir) { vm_obj_t v1 = vm_run_repl_lit(); vm_obj_t v2 = vm_run_repl_reg(); @@ -997,7 +1012,7 @@ VM_OP_DIV_RR:; vm_run_repl_out(v3); vm_run_repl_jump(); } -VM_OP_IDIV_RI:; +VM_OP_IDIV_RC:; VM_OPCODE_DEBUG(idiv_ri) { vm_obj_t v1 = vm_run_repl_reg(); vm_obj_t v2 = vm_run_repl_lit(); @@ -1008,7 +1023,7 @@ VM_OP_IDIV_RI:; vm_run_repl_out(v3); vm_run_repl_jump(); } -VM_OP_IDIV_IR:; +VM_OP_IDIV_CR:; VM_OPCODE_DEBUG(idiv_ir) { vm_obj_t v1 = vm_run_repl_lit(); vm_obj_t v2 = vm_run_repl_reg(); @@ -1030,7 +1045,7 @@ VM_OP_IDIV_RR:; vm_run_repl_out(v3); vm_run_repl_jump(); } -VM_OP_MOD_RI:; +VM_OP_MOD_RC:; VM_OPCODE_DEBUG(mod_ri) { vm_obj_t v1 = vm_run_repl_reg(); vm_obj_t v2 = vm_run_repl_lit(); @@ -1041,7 +1056,7 @@ VM_OP_MOD_RI:; vm_run_repl_out(v3); vm_run_repl_jump(); } -VM_OP_MOD_IR:; +VM_OP_MOD_CR:; VM_OPCODE_DEBUG(mod_ir) { vm_obj_t v1 = vm_run_repl_lit(); vm_obj_t v2 = vm_run_repl_reg(); @@ -1063,7 +1078,7 @@ VM_OP_MOD_RR:; vm_run_repl_out(v3); vm_run_repl_jump(); } -VM_OP_POW_RI:; +VM_OP_POW_RC:; VM_OPCODE_DEBUG(mod_ri) { vm_obj_t v1 = vm_run_repl_reg(); vm_obj_t v2 = vm_run_repl_lit(); @@ -1074,7 +1089,7 @@ VM_OP_POW_RI:; vm_run_repl_out(v3); vm_run_repl_jump(); } -VM_OP_POW_IR:; +VM_OP_POW_CR:; VM_OPCODE_DEBUG(mod_ir) { vm_obj_t v1 = vm_run_repl_lit(); vm_obj_t v2 = vm_run_repl_reg(); @@ -1096,7 +1111,7 @@ VM_OP_POW_RR:; vm_run_repl_out(v3); vm_run_repl_jump(); } -VM_OP_CONCAT_RI:; +VM_OP_CONCAT_RC:; VM_OPCODE_DEBUG(mod_ri) { vm_obj_t v1 = vm_run_repl_reg(); vm_obj_t v2 = vm_run_repl_lit(); @@ -1107,7 +1122,7 @@ VM_OP_CONCAT_RI:; vm_run_repl_out(v3); vm_run_repl_jump(); } -VM_OP_CONCAT_IR:; +VM_OP_CONCAT_CR:; VM_OPCODE_DEBUG(mod_ir) { vm_obj_t v1 = vm_run_repl_lit(); vm_obj_t v2 = vm_run_repl_reg(); @@ -1145,7 +1160,7 @@ VM_OP_BB_R:; } vm_backend_new_block(); } -VM_OP_BLT_RI:; +VM_OP_BLT_RC:; VM_OPCODE_DEBUG(blt_ri) { vm_obj_t v1 = vm_run_repl_reg(); vm_obj_t v2 = vm_run_repl_lit(); @@ -1157,7 +1172,7 @@ VM_OP_BLT_RI:; } vm_backend_new_block(); } -VM_OP_BLT_IR:; +VM_OP_BLT_CR:; VM_OPCODE_DEBUG(blt_ir) { vm_obj_t v1 = vm_run_repl_lit(); vm_obj_t v2 = vm_run_repl_reg(); @@ -1181,7 +1196,7 @@ VM_OP_BLT_RR:; } vm_backend_new_block(); } -VM_OP_BLE_RI:; +VM_OP_BLE_RC:; VM_OPCODE_DEBUG(ble_ri) { vm_obj_t v1 = vm_run_repl_reg(); vm_obj_t v2 = vm_run_repl_lit(); @@ -1193,7 +1208,7 @@ VM_OP_BLE_RI:; } vm_backend_new_block(); } -VM_OP_BLE_IR:; +VM_OP_BLE_CR:; VM_OPCODE_DEBUG(ble_ir) { vm_obj_t v1 = vm_run_repl_lit(); vm_obj_t v2 = vm_run_repl_reg(); @@ -1217,7 +1232,7 @@ VM_OP_BLE_RR:; } vm_backend_new_block(); } -VM_OP_BEQ_RI:; +VM_OP_BEQ_RC:; VM_OPCODE_DEBUG(beq_ri) { vm_obj_t v1 = vm_run_repl_reg(); vm_obj_t v2 = vm_run_repl_lit(); @@ -1229,7 +1244,7 @@ VM_OP_BEQ_RI:; } vm_backend_new_block(); } -VM_OP_BEQ_IR:; +VM_OP_BEQ_CR:; VM_OPCODE_DEBUG(beq_ir) { vm_obj_t v1 = vm_run_repl_lit(); vm_obj_t v2 = vm_run_repl_reg(); @@ -1253,7 +1268,7 @@ VM_OP_BEQ_RR:; } vm_backend_new_block(); } -VM_OP_RET_I:; +VM_OP_RET_C:; VM_OPCODE_DEBUG(ret_i) { vm_obj_t v1 = vm_run_repl_lit(); vm_backend_return(v1); diff --git a/vm/backend/backend.h b/vm/backend/backend.h index f4004b1a..d5cbc4a2 100644 --- a/vm/backend/backend.h +++ b/vm/backend/backend.h @@ -3,7 +3,6 @@ #define VM_HEADER_BACKEND_INTERP #include "../vm.h" -#include "../ir.h" vm_obj_t vm_run_main(vm_t *vm, vm_ir_block_t *entry); vm_obj_t vm_run_repl(vm_t *vm, vm_ir_block_t *entry); diff --git a/vm/errors.c b/vm/errors.c index 1185126b..9de43e9b 100644 --- a/vm/errors.c +++ b/vm/errors.c @@ -1,4 +1,5 @@ +#include "lib.h" #include "errors.h" #include "io.h" diff --git a/vm/errors.h b/vm/errors.h index f27d0f92..9874f1a0 100644 --- a/vm/errors.h +++ b/vm/errors.h @@ -2,7 +2,8 @@ #if !defined(VM_HEADER_ERRORS) #define VM_HEADER_ERRORS -#include "lib.h" +#include "vm.h" +#include struct vm_location_t; struct vm_location_range_t; diff --git a/vm/gc.c b/vm/gc.c index df723358..a12b25a3 100644 --- a/vm/gc.c +++ b/vm/gc.c @@ -1,6 +1,8 @@ #include "gc.h" #include "ir.h" +#include "lib.h" +#include struct vm_gc_objs_t; struct vm_gc_t; @@ -170,7 +172,9 @@ void vm_gc_sweep(vm_t *vm) { } vm_free(block->instrs); vm_free(block->branch.args); - vm_free(block->code); + if (* (const int64_t *) block->code != 0) { + vm_free(block->code); + } vm_free(block); } else { block->mark = false; diff --git a/vm/gc.h b/vm/gc.h index c0bb6322..15150fc6 100644 --- a/vm/gc.h +++ b/vm/gc.h @@ -1,7 +1,7 @@ #if !defined(VM_HEADER_GC) #define VM_HEADER_GC -#include "lib.h" +#include "vm.h" void vm_gc_run(vm_t *vm, vm_obj_t *top); void vm_gc_init(vm_t *vm); diff --git a/vm/io.c b/vm/io.c index 64817ed2..258d48a6 100644 --- a/vm/io.c +++ b/vm/io.c @@ -1,7 +1,9 @@ #include "io.h" -#include "ir.h" #include "math.h" +#include "lib.h" + +#include void vm_io_buffer_vformat(vm_io_buffer_t *buf, const char *fmt, va_list ap) { if (buf->buf == NULL) { diff --git a/vm/io.h b/vm/io.h index 9019d2d3..95317bfa 100644 --- a/vm/io.h +++ b/vm/io.h @@ -2,11 +2,13 @@ #if !defined(VM_HEADER_STD_LIBS_IO) #define VM_HEADER_STD_LIBS_IO +#include + +#include "vm.h" + struct vm_io_debug_t; typedef struct vm_io_debug_t vm_io_debug_t; -#include "obj.h" - struct vm_io_debug_t { vm_io_debug_t *next; vm_obj_t value; diff --git a/vm/ir.c b/vm/ir.c index f898d209..3b951d06 100644 --- a/vm/ir.c +++ b/vm/ir.c @@ -1,6 +1,7 @@ -#include "ir.h" #include "io.h" +#include "ir.h" +#include "lib.h" void vm_block_realloc(vm_ir_block_t *block, vm_ir_instr_t instr) { if (block->len + 4 >= block->alloc) { diff --git a/vm/ir.h b/vm/ir.h index f9fdf975..17a29204 100644 --- a/vm/ir.h +++ b/vm/ir.h @@ -10,8 +10,6 @@ typedef struct vm_ir_arg_t vm_ir_arg_t; typedef struct vm_ir_branch_t vm_ir_branch_t; typedef struct vm_ir_instr_t vm_ir_instr_t; -#include "lib.h" -#include "io.h" #include "errors.h" enum { @@ -96,10 +94,11 @@ struct vm_ir_block_t { vm_ir_branch_t branch; - void *code; + const void *code; - uint32_t nregs: 32; + uint32_t nregs: 31; uint32_t alloc: 30; + bool done: 1; bool isfunc: 1; uint8_t mark: 1; }; diff --git a/vm/lib.c b/vm/lib.c index a5f2c46d..06dbeab6 100644 --- a/vm/lib.c +++ b/vm/lib.c @@ -1,6 +1,50 @@ -#include "lib.h" +#include "vm.h" -#if defined(__TINYC__) -void *end; +#if VM_MALLOC_MI +#include "../vendor/mimalloc/include/mimalloc.h" + +void *vm_malloc(size_t x) { + return mi_malloc(x); +} + +void *vm_calloc(size_t x) { + return mi_calloc(x, 1); +} + +void *vm_realloc(void *x, size_t y) { + return mi_realloc(x, y); +} + +void vm_free(const void *x) { + mi_free((void *) (x)); +} + +char *vm_strdup(const char *x) { + return mi_strdup(x); +} #endif + +#if VM_MALLOC_SYS +#include + +void *vm_malloc(size_t x) { + return malloc(x); +} + +void *vm_calloc(size_t x) { + return calloc(x, 1); +} + +void *vm_realloc(void *x, size_t y) { + return realloc(x, y); +} + +void vm_free(const void *x) { + free((void *) (x)); +} + +char *vm_strdup(const char *x) { + return strdup(x); +} +#endif \ No newline at end of file diff --git a/vm/lib.h b/vm/lib.h index c7693054..ed65a620 100644 --- a/vm/lib.h +++ b/vm/lib.h @@ -6,68 +6,13 @@ #define _CRT_SECURE_NO_DEPRECATE #endif -#if defined(__TINYC__) -#define __builtin_trap() exit(1) -#define __builtin_unreachable() exit(1) -#define __pure2 __attribute__((__const__)) -#define __unused __attribute__((__unused__)) -#define __used __attribute__((__used__)) -#define __packed __attribute__((__packed__)) -#define __aligned(x) __attribute__((__aligned__(x))) -#define __section(x) __attribute__((__section__(x))) -#endif - -#define VM_ARCH_IS_AMD64 0 -#define VM_ARCH_IS_ARM64 0 -#define VM_ARCH_IS_OTHER 0 - -#if defined(__x86_64__) || defined(_M_AMD64) -#undef VM_ARCH_IS_AMD64 -#define VM_ARCH_IS_AMD64 1 -#elif defined(__aarch64__) -#undef VM_ARCH_IS_ARM64 -#define VM_ARCH_IS_ARM64 1 -#else -#undef VM_ARCH_IS_OTHER -#define VM_ARCH_IS_OTHER 1 -#endif - -#include -#include -#include -#include -#include -#include -#include #include -#include -#include -#include -#include "vm.h" +void *vm_malloc(size_t size); +void *vm_calloc(size_t size); +void *vm_realloc(void *ptr, size_t size); +void vm_free(const void *ptr); +char *vm_strdup(const char *str); -#if 0 -#define __builtin_trap() \ - printf("file %s, line %zu\n", __FILE__, (size_t)__LINE__); \ - exit(1); -#endif - -#include "../vendor/mimalloc/include/mimalloc.h" - -#if VM_MALLOC_MI -#define vm_malloc(x) mi_malloc(x) -#define vm_calloc(x) mi_calloc(x, 1) -#define vm_realloc(x, y) mi_realloc(x, y) -#define vm_free(x) mi_free((void *) (x)) -#define vm_strdup(x) mi_strdup(x) -#endif - -#if VM_MALLOC_SYS -#define vm_malloc(x) malloc(x) -#define vm_calloc(x) calloc(x, 1) -#define vm_realloc(x, y) realloc(x, y) -#define vm_free(x) free((void *) (x)) -#define vm_strdup(x) strdup(x) -#endif #endif diff --git a/vm/lua/repl.c b/vm/lua/repl.c index 0754061b..b501abed 100644 --- a/vm/lua/repl.c +++ b/vm/lua/repl.c @@ -1,23 +1,22 @@ +#include + #include "repl.h" #include "../ir.h" #include "../io.h" #include "../vm.h" -#include "../obj.h" #include "../ast/ast.h" -#include "../ast/comp.h" #include "../backend/backend.h" #include "../../vendor/tree-sitter/lib/include/tree_sitter/api.h" #include "../../vendor/isocline/include/isocline.h" -#include "../../vendor/isocline/src/completions.h" const TSLanguage *tree_sitter_lua(void); vm_ast_node_t vm_lang_lua_parse(vm_t *vm, const char *str, const char *file); void vm_lang_lua_repl_completer(ic_completion_env_t *cenv, const char *prefix) { - vm_t *vm = cenv->arg; + vm_t *vm = ic_completion_arg(cenv); const char *last_word; { ptrdiff_t len = strlen(prefix); diff --git a/vm/lua/repl.h b/vm/lua/repl.h index f0cccf4e..850e37af 100644 --- a/vm/lua/repl.h +++ b/vm/lua/repl.h @@ -1,7 +1,7 @@ #if !defined(VM_HEADER_LANG_LUA_REPL) #define VM_HEADER_LANG_LUA_REPL -#include "../lib.h" +#include "../vm.h" void vm_lang_lua_repl(vm_t *vm); diff --git a/vm/math.h b/vm/math.h index d0f8b6a2..f0bafcb5 100644 --- a/vm/math.h +++ b/vm/math.h @@ -2,7 +2,7 @@ #if !defined(VM_HEADER_MATH) #define VM_HEADER_MATH -#include "lib.h" +#include "vm.h" bool vm_obj_unsafe_eq(vm_obj_t v1, vm_obj_t v2); bool vm_obj_unsafe_lt(vm_obj_t v1, vm_obj_t v2); diff --git a/vm/obj.c b/vm/obj.c index cf3b846a..dbb16c44 100644 --- a/vm/obj.c +++ b/vm/obj.c @@ -1,7 +1,11 @@ + +#include + #include "obj.h" #include "gc.h" #include "io.h" #include "math.h" +#include "lib.h" vm_obj_t vm_obj_of_string(vm_t *vm, const char *str) { vm_obj_t ret = vm_obj_of_buffer(vm_io_buffer_from_str(str)); diff --git a/vm/obj.h b/vm/obj.h index baf2782c..44f89302 100644 --- a/vm/obj.h +++ b/vm/obj.h @@ -1,7 +1,7 @@ #if !defined(VM_HEADER_OBJ) #define VM_HEADER_OBJ -#include "lib.h" +#include "vm.h" vm_obj_t vm_obj_of_string(vm_t *vm, const char *str); uint32_t vm_obj_hash(vm_obj_t value); diff --git a/vm/obj.inc b/vm/obj.inc index 32262ba8..8c23a523 100644 --- a/vm/obj.inc +++ b/vm/obj.inc @@ -1,6 +1,4 @@ -#include "vm.h" - static inline bool vm_obj_is_nil(vm_obj_t o) { return nanbox_is_empty(o); } diff --git a/vm/primes.inc b/vm/primes.inc index 2d98ac17..2a934303 100644 --- a/vm/primes.inc +++ b/vm/primes.inc @@ -1,5 +1,7 @@ // generated by: python3 main/primes.py +#include + static uint32_t vm_primes_table[32] = { 1, 3, 7, 13, 29, 53, 89, 157, 283, 491, diff --git a/vm/std.c b/vm/std.c index cdf1e37a..e2d7795f 100644 --- a/vm/std.c +++ b/vm/std.c @@ -1,4 +1,8 @@ +#include +#include +#include + #include "std.h" #include "backend/backend.h" @@ -7,6 +11,7 @@ #include "obj.h" #include "gc.h" #include "io.h" +#include "lib.h" void vm_std_os_exit(vm_t *vm, size_t nargs, vm_obj_t *args) { (void)vm; diff --git a/vm/std.h b/vm/std.h index 05745d9c..f8c4f687 100644 --- a/vm/std.h +++ b/vm/std.h @@ -1,7 +1,7 @@ #if !defined(VM_HEADER_STD_STD) #define VM_HEADER_STD_STD -#include "io.h" +#include "vm.h" #define VM_STD_REF(vm, x) (&(x)) diff --git a/vm/vm.c b/vm/vm.c index 9b07cb05..2f068767 100644 --- a/vm/vm.c +++ b/vm/vm.c @@ -1,10 +1,10 @@ -#include "lib.h" +#include + #include "vm.h" +#include "lib.h" -#include "ir.h" #include "std.h" -#include "backend/backend.h" #include "gc.h" vm_t *vm_state_new(void) {