From 76dccc96c91d8f7e291ca70e1990bcfbfcd0bfb9 Mon Sep 17 00:00:00 2001 From: Shaw Summa Date: Wed, 24 Jul 2024 04:15:23 -0400 Subject: [PATCH 1/2] IR cleanup --- .gitignore | 1 + main/driver.mjs | 208 ------------- main/minivm.c | 6 +- main/test/globals.lua | 18 -- makefile | 4 +- test/closure/funcret.lua | 4 +- test/test.lua | 20 ++ {vm => vendor/nanbox}/nanbox.h | 0 vm/ast/comp.c | 536 ++++++++++++++++----------------- vm/ast/comp.h | 2 +- vm/backend/backend.c | 341 ++++++++++----------- vm/backend/backend.h | 4 +- vm/gc.c | 18 +- vm/io.c | 28 +- vm/ir.c | 76 ++--- vm/ir.h | 117 ++++--- vm/lib.h | 15 + vm/lua/ast.c | 4 +- vm/lua/repl.c | 18 +- vm/lua/repl.h | 2 +- vm/std.c | 4 +- vm/vm.h | 22 +- 22 files changed, 631 insertions(+), 817 deletions(-) delete mode 100644 main/driver.mjs delete mode 100644 main/test/globals.lua create mode 100644 test/test.lua rename {vm => vendor/nanbox}/nanbox.h (100%) diff --git a/.gitignore b/.gitignore index c49f1ed7..5b812d6e 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,4 @@ package-lock.json web/minivm.js web-raw/minivm.js web-raw/minivm.wasm +.DS_Store diff --git a/main/driver.mjs b/main/driver.mjs deleted file mode 100644 index 426ab400..00000000 --- a/main/driver.mjs +++ /dev/null @@ -1,208 +0,0 @@ - -import { default as MiniVM } from '../build/bin/minivm.mjs'; -import { default as BoxModule } from '../vendor/llvm-box/bin/llvm-box.mjs'; -import { openSync, readSync } from 'fs'; - -const unlinkSync = (path) => {}; - -const mkdirSync = (path) => { - box.FS.mkdir(path); -}; - -const writeFileSync = (path, data) => { - box.FS.writeFile(path, data); -}; - -const readFileSync = (path) => { - return box.FS.readFile(path); -}; - -const boxSpawn = (cmd, ...cmdArgs) => { - const args = [...cmdArgs]; - switch (cmd) { - case 'wasm-ld': - args.unshift('lld', 'wasm-ld'); - break; - case 'clang': - args.unshift('clang', 'clang'); - break; - default: - throw new Error(`command: ${cmd}`); - } - const argc = args.length; - const argv = box._malloc((argc + 1) * 4); - let argv_ptr = argv; - for (const arg of args) { - const arr = new TextEncoder().encode(arg); - const str = box._malloc(arr.length + 1); - for (let i = 0; i < arr.length; i++) { - box.HEAPU8[str + i] = arr[i]; - } - box.HEAPU8[str + arr.length] = 0; - box.HEAPU32[argv_ptr >> 2] = str; - argv_ptr += 4 - }; - box.HEAPU32[argv_ptr >> 2] = 0; - return box._main(argc, argv); -}; - -const execSync = (str) => { - try { - boxSpawn(...str.split(/ +/)); - } catch (e) { - if (!('status' in e) && e.status !== 0) { - throw e; - } - } -}; - -const cc = process.env['VM_CC'] ?? 'clang'; -const ld = process.env['VM_LD'] ?? 'wasm-ld'; - -const join = (c, ld) => { - const ldj = ld.split(' ').map(x => `-Wl,${x}`).join(' '); - return `${c} ${ldj}`; -} - -let ldFlagsBase = '-O0 --import-memory --strip-debug --export-dynamic --experimental-pic -shared'; -let cFlagsBase = '-O2 -nostdlib -target wasm32-unknown-emscripten -fPIC -fvisibility=default'; -const cFlags = join(cFlagsBase, ldFlagsBase); - -const time = Number(process.env['VM_TIME']); - -const timeBegin = (name) => { - if (time) { - console.time(name); - } -}; - -const timeEnd = (name) => { - if (time) { - console.timeEnd(name); - } -}; - -const watch = (f) => { - return (...args) => { - try { - // console.log('(', f.name, args, ')'); - const got = f(...args); - // console.log(' => ', got); - return got; - } catch (e) { - console.error(e); - process.exit(1); - } - } -}; - -const stdinFunc = () => { - const buf = Buffer.alloc(1); - while (true) { - try { - readSync(openSync('/dev/stdin'), buf, 0, 1, null); - break; - } catch (e) { - console.error(e); - continue; - } - } - return new Uint8Array(buf)[0]; -}; - -const stdoutFunc = (c) => { - process.stdout.write(new Uint8Array([c])); -}; - -const stderrFunc = (c) => { - process.stderr.write(new Uint8Array([c])); -}; - -timeBegin('llvm'); - -const box = await BoxModule({ - noInitialRun: true, - - stdout(c) { - process.stdout.write(new Uint8Array([c])); - }, - - stderr(c) { - process.stderr.write(new Uint8Array([c])); - }, -}); - -const cache = '/work'; -mkdirSync(cache); - -timeEnd('llvm'); - -export const run = async (args) => { - timeBegin('run'); - process.stdin.setRawMode(true); - - let last = 'mod'; - - timeBegin(last); - - const mod = await MiniVM({ - noInitialRun: true, - stdin: watch(stdinFunc), - stdout: watch(stdoutFunc), - stderr: watch(stderrFunc), - - _vm_lang_lua_repl_sync() {}, - - _vm_compile_c_to_wasm: (n) => { - timeEnd(last); - const cSrc = mod.FS.readFile(`/in${n}.c`); - let wasm = null; - const inFile = `${cache}/in${n}.c`; - const outFile = `${cache}/out${n}.wasm`; - writeFileSync(inFile, cSrc); - if (ld === 'none') { - timeBegin(cc); - execSync(`${cc} ${inFile} ${cFlags} -o ${outFile}`); - timeEnd(cc); - unlinkSync(inFile); - } else { - const midFile = `${cache}/mid${n}.o`; - timeBegin(cc); - execSync(`${cc} -c -w ${inFile} ${cFlagsBase} -o ${midFile}`); - timeEnd(cc); - unlinkSync(inFile); - timeBegin(ld); - execSync(`${ld} --no-entry --whole-archive ${midFile} ${ldFlagsBase} -o ${outFile}`); - timeEnd(ld); - unlinkSync(midFile); - } - wasm = readFileSync(outFile); - unlinkSync(outFile); - mod.FS.writeFile(`/out${n}.so`, wasm); - last = `after${n}`; - timeBegin(last); - }, - }); - - mod.FS.mkdir('/dir'); - mod.FS.mount(mod.NODEFS, {root : '.'}, '/dir'); - mod.FS.chdir('/dir'); - - timeEnd(last); - last = 'main'; - timeBegin(last); - - mod.callMain(args); - - timeEnd(last); - - process.stdin.setRawMode(false); - - timeEnd('run'); -}; - -try { - await run(process.argv.slice(2)); -} catch (e) { - console.error(e); -} diff --git a/main/minivm.c b/main/minivm.c index 43410671..1f731f29 100644 --- a/main/minivm.c +++ b/main/minivm.c @@ -43,7 +43,7 @@ __attribute__((no_instrument_function)) int main(int argc, char **argv) { } else if (!strcmp(arg, "--no-dump-ir")) { vm->dump_ir = false; } else if (!strcmp(arg, "--repl")) { - vm_repl(vm); + vm_lang_lua_repl(vm); isrepl = false; } else { isrepl = false; @@ -70,7 +70,7 @@ __attribute__((no_instrument_function)) int main(int argc, char **argv) { exit(1); } - vm_block_t *entry = vm_compile(vm, src, name ? name : "__expr__"); + vm_ir_block_t *entry = vm_lang_lua_compile(vm, src, name ? name : "__expr__"); vm_obj_t value = vm_run_main(vm, entry); if (vm_obj_is_error(value)) { @@ -91,7 +91,7 @@ __attribute__((no_instrument_function)) int main(int argc, char **argv) { } if (isrepl) { - vm_repl(vm); + vm_lang_lua_repl(vm); } #if VM_USE_SPALL diff --git a/main/test/globals.lua b/main/test/globals.lua deleted file mode 100644 index b61bb543..00000000 --- a/main/test/globals.lua +++ /dev/null @@ -1,18 +0,0 @@ - -for k,v in pairs(_G) do - if type(v) == 'function' then - print(k) - end -end - -for k,v in pairs(_G) do - if k ~= '_G' then - if type(v) == 'table' then - for l, w in pairs(v) do - if type(w) == 'function' then - print(k .. '.' .. l) - end - end - end - end -end diff --git a/makefile b/makefile index a1f5b1a5..505ae58c 100644 --- a/makefile +++ b/makefile @@ -25,7 +25,9 @@ MAKE_INCLUDE ?= # setup targets default: all -all: build/bin/minivm${EXE} +all: minivm + +minivm: build/bin/minivm${EXE} # specific builds clean: .dummy diff --git a/test/closure/funcret.lua b/test/closure/funcret.lua index 33d1e095..a28b2952 100644 --- a/test/closure/funcret.lua +++ b/test/closure/funcret.lua @@ -5,8 +5,10 @@ local add = function(x) end end +local max = tonumber(arg and arg[1]) or 1000000 + local i = 0 -while i < 1000 * 1000 * 10 do +while i < max do i = add(i)(1) end diff --git a/test/test.lua b/test/test.lua new file mode 100644 index 00000000..5d777261 --- /dev/null +++ b/test/test.lua @@ -0,0 +1,20 @@ +local function run(x, y) + print('-- ' .. tostring(x) .. ' and ' .. tostring(y)) + print(x and y) + print( + do + local tmp = x + if x then + y + else + x + end + end + ) +end + +run(true, true) +run(true, false) +run(false, true) +run(false, false) + diff --git a/vm/nanbox.h b/vendor/nanbox/nanbox.h similarity index 100% rename from vm/nanbox.h rename to vendor/nanbox/nanbox.h diff --git a/vm/ast/comp.c b/vm/ast/comp.c index 652594dd..a1a11b01 100644 --- a/vm/ast/comp.c +++ b/vm/ast/comp.c @@ -15,9 +15,9 @@ struct vm_ast_comp_names_t; typedef struct vm_ast_comp_names_t vm_ast_comp_names_t; struct vm_ast_comp_t { - vm_block_t *cur; + vm_ir_block_t *cur; vm_ast_comp_names_t *names; - vm_block_t *on_break; + vm_ir_block_t *on_break; vm_location_range_t range; vm_t *vm; }; @@ -79,15 +79,15 @@ static void vm_ast_names_free(vm_ast_comp_names_t *names) { vm_free(names); } -static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node); -static vm_arg_t vm_ast_comp_br_raw(vm_ast_comp_t *comp, vm_ast_node_t node, vm_block_t *iftrue, vm_block_t *iffalse); +static vm_ir_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node); +static vm_ir_arg_t vm_ast_comp_br_raw(vm_ast_comp_t *comp, vm_ast_node_t node, vm_ir_block_t *iftrue, vm_ir_block_t *iffalse); #define vm_ast_comp_to_rec(comp, node) ({ \ vm_ast_comp_t *comp_ = comp; \ vm_ast_node_t node_ = node; \ - vm_arg_t ret = vm_ast_comp_to_raw(comp_, node_); \ - if (ret.type == VM_ARG_ERROR && node_.info.range.start.byte != node_.info.range.stop.byte) { \ - return (vm_arg_t){ \ - .type = VM_ARG_ERROR, \ + vm_ir_arg_t ret = vm_ast_comp_to_raw(comp_, node_); \ + if (ret.type == VM_IR_ARG_TYPE_ERROR && node_.info.range.start.byte != node_.info.range.stop.byte) { \ + return (vm_ir_arg_t){ \ + .type = VM_IR_ARG_TYPE_ERROR, \ .error = vm_error_from_error(node_.info.range, ret.error), \ }; \ } \ @@ -97,10 +97,10 @@ static vm_arg_t vm_ast_comp_br_raw(vm_ast_comp_t *comp, vm_ast_node_t node, vm_b #define vm_ast_comp_br_rec(comp, node, iftrue, iffalse) ({ \ vm_ast_comp_t *comp_ = (comp); \ vm_ast_node_t node_ = (node); \ - vm_arg_t ret = vm_ast_comp_br_raw(comp_, node_, (iftrue), (iffalse)); \ - if (ret.type == VM_ARG_ERROR) { \ - return (vm_arg_t){ \ - .type = VM_ARG_ERROR, \ + vm_ir_arg_t ret = vm_ast_comp_br_raw(comp_, node_, (iftrue), (iffalse)); \ + if (ret.type == VM_IR_ARG_TYPE_ERROR) { \ + return (vm_ir_arg_t){ \ + .type = VM_IR_ARG_TYPE_ERROR, \ .error = vm_error_from_error(node_.info.range, ret.error), \ }; \ } \ @@ -110,23 +110,23 @@ static vm_arg_t vm_ast_comp_br_raw(vm_ast_comp_t *comp, vm_ast_node_t node, vm_b extern void vm_std_vm_closure(vm_t *vm, vm_obj_t *args); extern void vm_std_vm_concat(vm_t *vm, vm_obj_t *args); -static vm_arg_t *vm_ast_args(size_t nargs, ...) { +static vm_ir_arg_t *vm_ast_args(size_t nargs, ...) { va_list ap; va_start(ap, nargs); - vm_arg_t *ret = vm_malloc(sizeof(vm_arg_t) * (nargs + 1)); + vm_ir_arg_t *ret = vm_malloc(sizeof(vm_ir_arg_t) * (nargs + 1)); for (size_t i = 0; i < nargs; i++) { - ret[i] = va_arg(ap, vm_arg_t); + ret[i] = va_arg(ap, vm_ir_arg_t); } va_end(ap); - ret[nargs] = (vm_arg_t){ - .type = VM_ARG_NONE, + ret[nargs] = (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_NONE, }; return ret; } -static vm_block_t *vm_ast_comp_new_block(vm_ast_comp_t *comp) { - vm_block_t *block = vm_malloc(sizeof(vm_block_t)); - *block = (vm_block_t){ +static vm_ir_block_t *vm_ast_comp_new_block(vm_ast_comp_t *comp) { + vm_ir_block_t *block = vm_malloc(sizeof(vm_ir_block_t)); + *block = (vm_ir_block_t){ .id = (uint32_t)comp->vm->nblocks++, // .nregs = VM_NREGS, }; @@ -134,7 +134,7 @@ static vm_block_t *vm_ast_comp_new_block(vm_ast_comp_t *comp) { return block; } -static vm_arg_t vm_ast_comp_reg_named(vm_ast_comp_t *comp, const char *name) { +static vm_ir_arg_t vm_ast_comp_reg_named(vm_ast_comp_t *comp, const char *name) { size_t reg = comp->names->regs.len++; if (reg + 1 >= comp->names->regs.alloc) { comp->names->regs.alloc = (reg + 1) * 2; @@ -145,24 +145,24 @@ static vm_arg_t vm_ast_comp_reg_named(vm_ast_comp_t *comp, const char *name) { } else { comp->names->regs.ptr[reg] = vm_strdup(name); } - return (vm_arg_t){ - .type = VM_ARG_REG, + return (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_REG, .reg = reg, }; } -static vm_arg_t vm_ast_comp_reg(vm_ast_comp_t *comp) { +static vm_ir_arg_t vm_ast_comp_reg(vm_ast_comp_t *comp) { return vm_ast_comp_reg_named(comp, NULL); } -static void vm_ast_blocks_instr(vm_ast_comp_t *comp, vm_instr_t instr) { +static void vm_ast_blocks_instr(vm_ast_comp_t *comp, vm_ir_instr_t instr) { instr.range = comp->range; vm_block_realloc(comp->cur, instr); } -static void vm_ast_blocks_branch(vm_ast_comp_t *comp, vm_branch_t branch) { +static void vm_ast_blocks_branch(vm_ast_comp_t *comp, vm_ir_branch_t branch) { branch.range = comp->range; - if (comp->cur->branch.op != VM_BOP_FALL) { + if (comp->cur->branch.op != VM_IR_BRANCH_OPCODE_FALL) { __builtin_trap(); } comp->cur->branch = branch; @@ -179,19 +179,19 @@ static size_t vm_ast_comp_get_local(vm_ast_comp_names_t *names, const char *name return SIZE_MAX; } -static vm_arg_t vm_ast_comp_get_var(vm_ast_comp_t *comp, const char *name) { +static vm_ir_arg_t vm_ast_comp_get_var(vm_ast_comp_t *comp, const char *name) { if (name == NULL) { __builtin_trap(); } size_t got = vm_ast_comp_get_local(comp->names, name); if (got != SIZE_MAX) { - return (vm_arg_t){ - .type = VM_ARG_REG, + return (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_REG, .reg = got, }; }; - vm_arg_t cap = (vm_arg_t){ - .type = VM_ARG_REG, + vm_ir_arg_t cap = (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_REG, .reg = 0, }; ptrdiff_t slotnum = -1; @@ -213,16 +213,16 @@ static vm_arg_t vm_ast_comp_get_var(vm_ast_comp_t *comp, const char *name) { .slot = slotnum, }; } - vm_arg_t reg = vm_ast_comp_reg(comp); - vm_block_t *next = vm_ast_comp_new_block(comp); - vm_arg_t slot = (vm_arg_t){ - .type = VM_ARG_LIT, + vm_ir_arg_t reg = vm_ast_comp_reg(comp); + vm_ir_block_t *next = vm_ast_comp_new_block(comp); + vm_ir_arg_t slot = (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_LIT, .lit = vm_obj_of_number(slotnum - 1), }; vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_LOAD, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_LOAD, .out = reg, .args = vm_ast_args(2, cap, slot), .targets[0] = next, @@ -232,18 +232,18 @@ static vm_arg_t vm_ast_comp_get_var(vm_ast_comp_t *comp, const char *name) { return reg; } -static vm_arg_t vm_ast_comp_br_raw(vm_ast_comp_t *comp, vm_ast_node_t node, vm_block_t *iftrue, vm_block_t *iffalse) { +static vm_ir_arg_t vm_ast_comp_br_raw(vm_ast_comp_t *comp, vm_ast_node_t node, vm_ir_block_t *iftrue, vm_ir_block_t *iffalse) { switch (node.type) { case VM_AST_NODE_FORM: { vm_ast_form_t form = node.value.form; switch (form.type) { case VM_AST_FORM_LT: { - vm_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); - vm_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); + vm_ir_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); + vm_ir_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_BLT, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_BLT, .args = vm_ast_args(2, arg1, arg2), .targets[0] = iftrue, .targets[1] = iffalse, @@ -252,12 +252,12 @@ static vm_arg_t vm_ast_comp_br_raw(vm_ast_comp_t *comp, vm_ast_node_t node, vm_b goto ret_ok; } case VM_AST_FORM_GT: { - vm_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); - vm_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); + vm_ir_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); + vm_ir_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_BLT, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_BLT, .args = vm_ast_args(2, arg2, arg1), .targets[0] = iftrue, .targets[1] = iffalse, @@ -266,12 +266,12 @@ static vm_arg_t vm_ast_comp_br_raw(vm_ast_comp_t *comp, vm_ast_node_t node, vm_b goto ret_ok; } case VM_AST_FORM_LE: { - vm_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); - vm_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); + vm_ir_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); + vm_ir_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_BLE, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_BLE, .args = vm_ast_args(2, arg1, arg2), .targets[0] = iftrue, .targets[1] = iffalse, @@ -280,12 +280,12 @@ static vm_arg_t vm_ast_comp_br_raw(vm_ast_comp_t *comp, vm_ast_node_t node, vm_b goto ret_ok; } case VM_AST_FORM_GE: { - vm_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); - vm_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); + vm_ir_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); + vm_ir_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_BLE, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_BLE, .args = vm_ast_args(2, arg2, arg1), .targets[0] = iftrue, .targets[1] = iffalse, @@ -294,12 +294,12 @@ static vm_arg_t vm_ast_comp_br_raw(vm_ast_comp_t *comp, vm_ast_node_t node, vm_b goto ret_ok; } case VM_AST_FORM_EQ: { - vm_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); - vm_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); + vm_ir_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); + vm_ir_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_BEQ, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_BEQ, .args = vm_ast_args(2, arg1, arg2), .targets[0] = iftrue, .targets[1] = iffalse, @@ -308,12 +308,12 @@ static vm_arg_t vm_ast_comp_br_raw(vm_ast_comp_t *comp, vm_ast_node_t node, vm_b goto ret_ok; } case VM_AST_FORM_NE: { - vm_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); - vm_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); + vm_ir_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); + vm_ir_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_BEQ, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_BEQ, .args = vm_ast_args(2, arg1, arg2), .targets[0] = iffalse, .targets[1] = iftrue, @@ -322,14 +322,14 @@ static vm_arg_t vm_ast_comp_br_raw(vm_ast_comp_t *comp, vm_ast_node_t node, vm_b goto ret_ok; } case VM_AST_FORM_AND: { - vm_block_t *mid = vm_ast_comp_new_block(comp); + vm_ir_block_t *mid = vm_ast_comp_new_block(comp); vm_ast_comp_br_rec(comp, form.args[0], mid, iffalse); comp->cur = mid; vm_ast_comp_br_rec(comp, form.args[1], iftrue, iffalse); goto ret_ok; } case VM_AST_FORM_OR: { - vm_block_t *mid = vm_ast_comp_new_block(comp); + vm_ir_block_t *mid = vm_ast_comp_new_block(comp); vm_ast_comp_br_rec(comp, form.args[0], iftrue, mid); comp->cur = mid; vm_ast_comp_br_rec(comp, form.args[1], iftrue, iffalse); @@ -345,15 +345,15 @@ static vm_arg_t vm_ast_comp_br_raw(vm_ast_comp_t *comp, vm_ast_node_t node, vm_b case VM_AST_NODE_LITERAL: { vm_obj_t value = node.value.literal; if (vm_obj_is_error(value)) { - return (vm_arg_t){ - .type = VM_ARG_ERROR, + return (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_ERROR, .error = vm_obj_get_error(value), }; } vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_JUMP, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_JUMP, .args = vm_ast_args(0), .targets[0] = iftrue, } @@ -365,11 +365,11 @@ static vm_arg_t vm_ast_comp_br_raw(vm_ast_comp_t *comp, vm_ast_node_t node, vm_b break; } } - vm_arg_t bb_arg = vm_ast_comp_to_rec(comp, node); + vm_ir_arg_t bb_arg = vm_ast_comp_to_rec(comp, node); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_BB, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_BB, .args = vm_ast_args(1, bb_arg), .targets[0] = iftrue, .targets[1] = iffalse, @@ -377,10 +377,10 @@ static vm_arg_t vm_ast_comp_br_raw(vm_ast_comp_t *comp, vm_ast_node_t node, vm_b ); goto ret_ok; ret_ok:; - return (vm_arg_t){.type = VM_ARG_NONE}; + return (vm_ir_arg_t){.type = VM_IR_ARG_TYPE_NONE}; } -static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { +static vm_ir_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { // { // vm_io_buffer_t *buf = vm_io_buffer_new(); // vm_ast_print_node(buf, 0, "ast = ", node); @@ -394,15 +394,15 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { vm_ast_form_t form = node.value.form; switch (form.type) { case VM_AST_FORM_AND: { - vm_arg_t out = vm_ast_comp_reg(comp); - vm_block_t *iftrue = vm_ast_comp_new_block(comp); - vm_block_t *iffalse = vm_ast_comp_new_block(comp); - vm_block_t *after = vm_ast_comp_new_block(comp); - vm_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); + vm_ir_arg_t out = vm_ast_comp_reg(comp); + vm_ir_block_t *iftrue = vm_ast_comp_new_block(comp); + vm_ir_block_t *iffalse = vm_ast_comp_new_block(comp); + vm_ir_block_t *after = vm_ast_comp_new_block(comp); + vm_ir_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_BB, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_BB, .args = vm_ast_args(1, arg1), .targets[0] = iftrue, .targets[1] = iffalse, @@ -411,34 +411,34 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { comp->cur = iffalse; vm_ast_blocks_instr( comp, - (vm_instr_t){ - .op = VM_IOP_MOVE, + (vm_ir_instr_t){ + .op = VM_IR_INSTR_OPCODE_MOVE, .out = out, .args = vm_ast_args(1, arg1), } ); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_JUMP, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_JUMP, .args = vm_ast_args(0), .targets[0] = after, } ); comp->cur = iftrue; - vm_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); + vm_ir_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); vm_ast_blocks_instr( comp, - (vm_instr_t){ - .op = VM_IOP_MOVE, + (vm_ir_instr_t){ + .op = VM_IR_INSTR_OPCODE_MOVE, .out = out, .args = vm_ast_args(1, arg2), } ); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_JUMP, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_JUMP, .args = vm_ast_args(0), .targets[0] = after, } @@ -447,15 +447,15 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { return out; } case VM_AST_FORM_OR: { - vm_arg_t out = vm_ast_comp_reg(comp); - vm_block_t *iftrue = vm_ast_comp_new_block(comp); - vm_block_t *iffalse = vm_ast_comp_new_block(comp); - vm_block_t *after = vm_ast_comp_new_block(comp); - vm_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); + vm_ir_arg_t out = vm_ast_comp_reg(comp); + vm_ir_block_t *iftrue = vm_ast_comp_new_block(comp); + vm_ir_block_t *iffalse = vm_ast_comp_new_block(comp); + vm_ir_block_t *after = vm_ast_comp_new_block(comp); + vm_ir_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_BB, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_BB, .args = vm_ast_args(1, arg1), .targets[0] = iftrue, .targets[1] = iffalse, @@ -464,34 +464,34 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { comp->cur = iftrue; vm_ast_blocks_instr( comp, - (vm_instr_t){ - .op = VM_IOP_MOVE, + (vm_ir_instr_t){ + .op = VM_IR_INSTR_OPCODE_MOVE, .out = out, .args = vm_ast_args(1, arg1), } ); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_JUMP, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_JUMP, .args = vm_ast_args(0), .targets[0] = after, } ); comp->cur = iffalse; - vm_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); + vm_ir_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); vm_ast_blocks_instr( comp, - (vm_instr_t){ - .op = VM_IOP_MOVE, + (vm_ir_instr_t){ + .op = VM_IR_INSTR_OPCODE_MOVE, .out = out, .args = vm_ast_args(1, arg2), } ); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_JUMP, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_JUMP, .args = vm_ast_args(0), .targets[0] = after, } @@ -506,49 +506,49 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { case VM_AST_FORM_LE: case VM_AST_FORM_GE: case VM_AST_FORM_NOT: { - vm_arg_t out = vm_ast_comp_reg(comp); - vm_block_t *iftrue = vm_ast_comp_new_block(comp); - vm_block_t *iffalse = vm_ast_comp_new_block(comp); - vm_block_t *after = vm_ast_comp_new_block(comp); + vm_ir_arg_t out = vm_ast_comp_reg(comp); + vm_ir_block_t *iftrue = vm_ast_comp_new_block(comp); + vm_ir_block_t *iffalse = vm_ast_comp_new_block(comp); + vm_ir_block_t *after = vm_ast_comp_new_block(comp); vm_ast_comp_br_rec(comp, node, iftrue, iffalse); comp->cur = iftrue; - vm_arg_t true_value = (vm_arg_t){ - .type = VM_ARG_LIT, + vm_ir_arg_t true_value = (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_LIT, .lit = vm_obj_of_boolean(true), }; vm_ast_blocks_instr( comp, - (vm_instr_t){ - .op = VM_IOP_MOVE, + (vm_ir_instr_t){ + .op = VM_IR_INSTR_OPCODE_MOVE, .out = out, .args = vm_ast_args(1, true_value), } ); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_JUMP, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_JUMP, .args = vm_ast_args(0), .targets[0] = after, } ); comp->cur = iffalse; - vm_arg_t false_value = (vm_arg_t){ - .type = VM_ARG_LIT, + vm_ir_arg_t false_value = (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_LIT, .lit = vm_obj_of_boolean(false), }; vm_ast_blocks_instr( comp, - (vm_instr_t){ - .op = VM_IOP_MOVE, + (vm_ir_instr_t){ + .op = VM_IR_INSTR_OPCODE_MOVE, .out = out, .args = vm_ast_args(1, false_value), } ); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_JUMP, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_JUMP, .args = vm_ast_args(0), .targets[0] = after, } @@ -558,17 +558,17 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { } case VM_AST_FORM_DO: { vm_ast_comp_to_rec(comp, form.args[0]); - vm_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); + vm_ir_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); return arg2; } case VM_AST_FORM_LOCAL: { - vm_arg_t value_arg = vm_ast_comp_to_rec(comp, form.args[1]); + vm_ir_arg_t value_arg = vm_ast_comp_to_rec(comp, form.args[1]); vm_ast_node_t target = form.args[0]; - vm_arg_t local = vm_ast_comp_reg_named(comp, target.value.ident); + vm_ir_arg_t local = vm_ast_comp_reg_named(comp, target.value.ident); vm_ast_blocks_instr( comp, - (vm_instr_t){ - .op = VM_IOP_MOVE, + (vm_ir_instr_t){ + .op = VM_IR_INSTR_OPCODE_MOVE, .out = local, .args = vm_ast_args(1, value_arg), } @@ -576,24 +576,24 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { return vm_arg_nil(); } case VM_AST_FORM_SET: { - vm_arg_t value_arg = vm_ast_comp_to_rec(comp, form.args[1]); + vm_ir_arg_t value_arg = vm_ast_comp_to_rec(comp, form.args[1]); vm_ast_node_t target = form.args[0]; if (target.type == VM_AST_NODE_IDENT) { size_t local = vm_ast_comp_get_local(comp->names, target.value.ident); if (local == SIZE_MAX) { vm_ast_node_t node = vm_ast_build_ident(vm_strdup("_ENV")); - vm_arg_t env_arg = vm_ast_comp_to_rec(comp, node); + vm_ir_arg_t env_arg = vm_ast_comp_to_rec(comp, node); vm_ast_free_node(node); - vm_arg_t key_arg = (vm_arg_t){ - .type = VM_ARG_LIT, + vm_ir_arg_t key_arg = (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_LIT, .lit = vm_str(comp->vm, target.value.ident), }; vm_ast_blocks_instr( comp, - (vm_instr_t){ - .op = VM_IOP_TABLE_SET, - .out = (vm_arg_t){ - .type = VM_ARG_NONE, + (vm_ir_instr_t){ + .op = VM_IR_INSTR_OPCODE_TABLE_SET, + .out = (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_NONE, }, .args = vm_ast_args(3, env_arg, key_arg, value_arg), } @@ -602,10 +602,10 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { } else { vm_ast_blocks_instr( comp, - (vm_instr_t){ - .op = VM_IOP_MOVE, - .out = (vm_arg_t){ - .type = VM_ARG_REG, + (vm_ir_instr_t){ + .op = VM_IR_INSTR_OPCODE_MOVE, + .out = (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_REG, .reg = local, }, .args = vm_ast_args(1, value_arg), @@ -615,16 +615,16 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { } } if (target.type == VM_AST_NODE_FORM && target.value.form.type == VM_AST_FORM_LOAD) { - vm_arg_t table = vm_ast_comp_to_rec(comp, target.value.form.args[0]); - vm_arg_t key = vm_ast_comp_to_rec(comp, target.value.form.args[1]); - vm_arg_t val = vm_ast_comp_to_rec(comp, form.args[1]); + vm_ir_arg_t table = vm_ast_comp_to_rec(comp, target.value.form.args[0]); + vm_ir_arg_t key = vm_ast_comp_to_rec(comp, target.value.form.args[1]); + vm_ir_arg_t val = vm_ast_comp_to_rec(comp, form.args[1]); vm_ast_blocks_instr( comp, - (vm_instr_t){ - .op = VM_IOP_TABLE_SET, + (vm_ir_instr_t){ + .op = VM_IR_INSTR_OPCODE_TABLE_SET, .args = vm_ast_args(3, table, key, val), - .out = (vm_arg_t){ - .type = VM_ARG_NONE, + .out = (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_NONE, }, } ); @@ -633,11 +633,11 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { break; } case VM_AST_FORM_ENV: { - vm_arg_t out = vm_ast_comp_reg(comp); + vm_ir_arg_t out = vm_ast_comp_reg(comp); vm_ast_blocks_instr( comp, - (vm_instr_t){ - .op = VM_IOP_STD, + (vm_ir_instr_t){ + .op = VM_IR_INSTR_OPCODE_STD, .out = out, .args = vm_ast_args(0), } @@ -645,11 +645,11 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { return out; } case VM_AST_FORM_NEW: { - vm_arg_t out = vm_ast_comp_reg(comp); + vm_ir_arg_t out = vm_ast_comp_reg(comp); vm_ast_blocks_instr( comp, - (vm_instr_t){ - .op = VM_IOP_TABLE_NEW, + (vm_ir_instr_t){ + .op = VM_IR_INSTR_OPCODE_TABLE_NEW, .out = out, .args = vm_ast_args(0), } @@ -657,12 +657,12 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { return out; } case VM_AST_FORM_LEN: { - vm_arg_t out = vm_ast_comp_reg(comp); - vm_arg_t in = vm_ast_comp_to_rec(comp, form.args[0]); + vm_ir_arg_t out = vm_ast_comp_reg(comp); + vm_ir_arg_t in = vm_ast_comp_to_rec(comp, form.args[0]); vm_ast_blocks_instr( comp, - (vm_instr_t){ - .op = VM_IOP_TABLE_LEN, + (vm_ir_instr_t){ + .op = VM_IR_INSTR_OPCODE_TABLE_LEN, .args = vm_ast_args(1, in), .out = out, } @@ -670,14 +670,14 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { return out; } case VM_AST_FORM_LOAD: { - vm_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); - vm_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); - vm_arg_t out = vm_ast_comp_reg(comp); - vm_block_t *next = vm_ast_comp_new_block(comp); + vm_ir_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); + vm_ir_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); + vm_ir_arg_t out = vm_ast_comp_reg(comp); + vm_ir_block_t *next = vm_ast_comp_new_block(comp); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_GET, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_GET, .out = out, .args = vm_ast_args(2, arg1, arg2), .targets[0] = next, @@ -687,21 +687,21 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { return out; } case VM_AST_FORM_CALL: { - vm_arg_t func = vm_ast_comp_to_rec(comp, form.args[0]); - vm_arg_t *args = vm_malloc(sizeof(vm_arg_t) * (form.len + 1)); + vm_ir_arg_t func = vm_ast_comp_to_rec(comp, form.args[0]); + vm_ir_arg_t *args = vm_malloc(sizeof(vm_ir_arg_t) * (form.len + 1)); args[0] = func; for (size_t i = 1; i < form.len; i++) { args[i] = vm_ast_comp_to_rec(comp, form.args[i]); } - args[form.len] = (vm_arg_t){ - .type = VM_ARG_NONE, + args[form.len] = (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_NONE, }; - vm_arg_t out = vm_ast_comp_reg(comp); - vm_block_t *next = vm_ast_comp_new_block(comp); + vm_ir_arg_t out = vm_ast_comp_reg(comp); + vm_ir_block_t *next = vm_ast_comp_new_block(comp); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_CALL, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_CALL, .out = out, .args = args, .targets[0] = next, @@ -711,26 +711,26 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { return out; } case VM_AST_FORM_CONCAT: { - vm_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); - vm_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); - vm_block_t *with_result = vm_ast_comp_new_block(comp); + vm_ir_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); + vm_ir_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); + vm_ir_block_t *with_result = vm_ast_comp_new_block(comp); - vm_arg_t *call_args = vm_malloc(sizeof(vm_arg_t) * 4); - call_args[0] = (vm_arg_t){ - .type = VM_ARG_LIT, + vm_ir_arg_t *call_args = vm_malloc(sizeof(vm_ir_arg_t) * 4); + call_args[0] = (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_LIT, .lit = vm_obj_of_ffi(&vm_std_vm_concat), }; call_args[1] = arg1; call_args[2] = arg2; - call_args[3] = (vm_arg_t){ - .type = VM_ARG_NONE, + call_args[3] = (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_NONE, }; - vm_arg_t out = vm_ast_comp_reg(comp); + vm_ir_arg_t out = vm_ast_comp_reg(comp); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_CALL, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_CALL, .out = out, .args = call_args, .targets[0] = with_result, @@ -741,26 +741,26 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { return out; } case VM_AST_FORM_POW: { - vm_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); - vm_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); - vm_block_t *with_result = vm_ast_comp_new_block(comp); + vm_ir_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); + vm_ir_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); + vm_ir_block_t *with_result = vm_ast_comp_new_block(comp); - vm_arg_t *call_args = vm_malloc(sizeof(vm_arg_t) * 4); - call_args[0] = (vm_arg_t){ - .type = VM_ARG_LIT, + vm_ir_arg_t *call_args = vm_malloc(sizeof(vm_ir_arg_t) * 4); + call_args[0] = (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_LIT, .lit = vm_obj_of_ffi(&vm_lua_comp_op_std_pow), }; call_args[1] = arg1; call_args[2] = arg2; - call_args[3] = (vm_arg_t){ - .type = VM_ARG_NONE, + call_args[3] = (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_NONE, }; - vm_arg_t out = vm_ast_comp_reg(comp); + vm_ir_arg_t out = vm_ast_comp_reg(comp); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_CALL, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_CALL, .out = out, .args = call_args, .targets[0] = with_result, @@ -776,39 +776,39 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { case VM_AST_FORM_DIV: case VM_AST_FORM_IDIV: case VM_AST_FORM_MOD: { - vm_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); - vm_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); - vm_arg_t out = vm_ast_comp_reg(comp); + vm_ir_arg_t arg1 = vm_ast_comp_to_rec(comp, form.args[0]); + vm_ir_arg_t arg2 = vm_ast_comp_to_rec(comp, form.args[1]); + vm_ir_arg_t out = vm_ast_comp_reg(comp); uint8_t op; switch (form.type) { case VM_AST_FORM_ADD: { - op = VM_IOP_ADD; + op = VM_IR_INSTR_OPCODE_ADD; break; } case VM_AST_FORM_SUB: { - op = VM_IOP_SUB; + op = VM_IR_INSTR_OPCODE_SUB; break; } case VM_AST_FORM_MUL: { - op = VM_IOP_MUL; + op = VM_IR_INSTR_OPCODE_MUL; break; } case VM_AST_FORM_DIV: { - op = VM_IOP_DIV; + op = VM_IR_INSTR_OPCODE_DIV; break; } case VM_AST_FORM_IDIV: { - op = VM_IOP_IDIV; + op = VM_IR_INSTR_OPCODE_IDIV; break; } case VM_AST_FORM_MOD: { - op = VM_IOP_MOD; + op = VM_IR_INSTR_OPCODE_MOD; break; } } vm_ast_blocks_instr( comp, - (vm_instr_t){ + (vm_ir_instr_t){ .op = op, .args = vm_ast_args(2, arg1, arg2), .out = out, @@ -817,43 +817,43 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { return out; } case VM_AST_FORM_IF: { - vm_arg_t out = vm_ast_comp_reg(comp); - vm_block_t *iftrue = vm_ast_comp_new_block(comp); - vm_block_t *iffalse = vm_ast_comp_new_block(comp); - vm_block_t *after = vm_ast_comp_new_block(comp); + vm_ir_arg_t out = vm_ast_comp_reg(comp); + vm_ir_block_t *iftrue = vm_ast_comp_new_block(comp); + vm_ir_block_t *iffalse = vm_ast_comp_new_block(comp); + vm_ir_block_t *after = vm_ast_comp_new_block(comp); vm_ast_comp_br_rec(comp, form.args[0], iftrue, iffalse); comp->cur = iftrue; - vm_arg_t true_value = vm_ast_comp_to_rec(comp, form.args[1]); + vm_ir_arg_t true_value = vm_ast_comp_to_rec(comp, form.args[1]); vm_ast_blocks_instr( comp, - (vm_instr_t){ - .op = VM_IOP_MOVE, + (vm_ir_instr_t){ + .op = VM_IR_INSTR_OPCODE_MOVE, .out = out, .args = vm_ast_args(1, true_value), } ); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_JUMP, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_JUMP, .args = vm_ast_args(0), .targets[0] = after, } ); comp->cur = iffalse; - vm_arg_t false_value = vm_ast_comp_to_rec(comp, form.args[2]); + vm_ir_arg_t false_value = vm_ast_comp_to_rec(comp, form.args[2]); vm_ast_blocks_instr( comp, - (vm_instr_t){ - .op = VM_IOP_MOVE, + (vm_ir_instr_t){ + .op = VM_IR_INSTR_OPCODE_MOVE, .out = out, .args = vm_ast_args(1, false_value), } ); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_JUMP, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_JUMP, .args = vm_ast_args(0), .targets[0] = after, } @@ -864,8 +864,8 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { case VM_AST_FORM_LAMBDA: { vm_ast_comp_names_push(comp); - vm_block_t *old_cur = comp->cur; - vm_block_t *body = vm_ast_comp_new_block(comp); + vm_ir_block_t *old_cur = comp->cur; + vm_ir_block_t *body = vm_ast_comp_new_block(comp); comp->cur = body; body->isfunc = true; @@ -885,15 +885,15 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { vm_ast_comp_reg_named(comp, arg.value.ident); } - vm_block_t *old_break = comp->on_break; + vm_ir_block_t *old_break = comp->on_break; comp->on_break = NULL; vm_ast_comp_to_rec(comp, form.args[2]); comp->on_break = old_break; vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_RET, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_RET, .args = vm_ast_args(1, vm_arg_nil()), } ); @@ -901,38 +901,38 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { vm_ast_comp_names_t *names = vm_ast_comp_names_pop(comp); comp->cur = old_cur; - vm_arg_t out = vm_ast_comp_reg(comp); + vm_ir_arg_t out = vm_ast_comp_reg(comp); - vm_block_t *with_lambda = vm_ast_comp_new_block(comp); + vm_ir_block_t *with_lambda = vm_ast_comp_new_block(comp); - vm_arg_t *call_args = vm_malloc(sizeof(vm_arg_t) * (names->caps.len + 3)); - call_args[0] = (vm_arg_t){ - .type = VM_ARG_LIT, + vm_ir_arg_t *call_args = vm_malloc(sizeof(vm_ir_arg_t) * (names->caps.len + 3)); + call_args[0] = (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_LIT, .lit = vm_obj_of_ffi(&vm_std_vm_closure), }; - call_args[1] = (vm_arg_t){ - .type = VM_ARG_LIT, + call_args[1] = (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_LIT, .lit = vm_obj_of_block(body), }; for (size_t i = 0; i < names->caps.len; i++) { vm_ast_comp_cap_t cap = names->caps.ptr[i]; - vm_arg_t got = vm_ast_comp_get_var(comp, cap.name); - if (got.type != VM_ARG_NONE) { + vm_ir_arg_t got = vm_ast_comp_get_var(comp, cap.name); + if (got.type != VM_IR_ARG_TYPE_NONE) { call_args[i + 2] = got; } else { call_args[i + 2] = vm_arg_nil(); } } - call_args[names->caps.len + 2] = (vm_arg_t){ - .type = VM_ARG_NONE, + call_args[names->caps.len + 2] = (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_NONE, }; vm_ast_names_free(names); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_CALL, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_CALL, .out = out, .args = call_args, .targets[0] = with_lambda, @@ -944,11 +944,11 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { return out; } case VM_AST_FORM_WHILE: { - vm_block_t *body = vm_ast_comp_new_block(comp); - vm_block_t *after = vm_ast_comp_new_block(comp); + vm_ir_block_t *body = vm_ast_comp_new_block(comp); + vm_ir_block_t *after = vm_ast_comp_new_block(comp); vm_ast_comp_br_rec(comp, form.args[0], body, after); comp->cur = body; - vm_block_t *old_break = comp->on_break; + vm_ir_block_t *old_break = comp->on_break; comp->on_break = after; vm_ast_comp_to_rec(comp, form.args[1]); comp->on_break = old_break; @@ -958,16 +958,16 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { } case VM_AST_FORM_BREAK: { if (comp->on_break == NULL) { - return (vm_arg_t){ - .type = VM_ARG_ERROR, + return (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_ERROR, .error = vm_error_from_msg(vm_location_range_unknown, "break not in block"), }; } - vm_block_t *after = vm_ast_comp_new_block(comp); + vm_ir_block_t *after = vm_ast_comp_new_block(comp); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_JUMP, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_JUMP, .args = vm_ast_args(0), .targets[0] = comp->on_break, } @@ -976,11 +976,11 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { return vm_arg_nil(); } case VM_AST_FORM_RETURN: { - vm_arg_t arg = vm_ast_comp_to_rec(comp, form.args[0]); + vm_ir_arg_t arg = vm_ast_comp_to_rec(comp, form.args[0]); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_RET, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_RET, .args = vm_ast_args(1, arg), } ); @@ -989,7 +989,7 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { } case VM_AST_FORM_SCOPE: { size_t count = comp->names->regs.len; - vm_arg_t ret = vm_ast_comp_to_rec(comp, form.args[0]); + vm_ir_arg_t ret = vm_ast_comp_to_rec(comp, form.args[0]); for (size_t i = count; i < comp->names->regs.len; i++) { vm_free(comp->names->regs.ptr[i]); } @@ -1002,13 +1002,13 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { case VM_AST_NODE_LITERAL: { vm_obj_t num = node.value.literal; if (vm_obj_is_error(num)) { - return (vm_arg_t){ - .type = VM_ARG_ERROR, + return (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_ERROR, .error = vm_obj_get_error(num), }; } else { - return (vm_arg_t){ - .type = VM_ARG_LIT, + return (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_LIT, .lit = num, }; } @@ -1025,23 +1025,23 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { } } if (is_local) { - vm_arg_t got = vm_ast_comp_get_var(comp, lit); - if (got.type != VM_ARG_NONE) { + vm_ir_arg_t got = vm_ast_comp_get_var(comp, lit); + if (got.type != VM_IR_ARG_TYPE_NONE) { return got; } } else { - vm_arg_t got = vm_ast_comp_get_var(comp, "_ENV"); - if (got.type != VM_ARG_NONE) { - vm_arg_t env_key = (vm_arg_t){ - .type = VM_ARG_LIT, + vm_ir_arg_t got = vm_ast_comp_get_var(comp, "_ENV"); + if (got.type != VM_IR_ARG_TYPE_NONE) { + vm_ir_arg_t env_key = (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_LIT, .lit = vm_str(comp->vm, lit), }; - vm_arg_t out = vm_ast_comp_reg(comp); - vm_block_t *next = vm_ast_comp_new_block(comp); + vm_ir_arg_t out = vm_ast_comp_reg(comp); + vm_ir_block_t *next = vm_ast_comp_new_block(comp); vm_ast_blocks_branch( comp, - (vm_branch_t){ - .op = VM_BOP_GET, + (vm_ir_branch_t){ + .op = VM_IR_BRANCH_OPCODE_GET, .out = out, .args = vm_ast_args(2, got, env_key), .targets[0] = next, @@ -1053,13 +1053,13 @@ static vm_arg_t vm_ast_comp_to_raw(vm_ast_comp_t *comp, vm_ast_node_t node) { } } } - return (vm_arg_t){ - .type = VM_ARG_ERROR, + return (vm_ir_arg_t){ + .type = VM_IR_ARG_TYPE_ERROR, .error = vm_error_from_msg(node.info.range, "internal error: invalid or unhandled ast node type"), }; } -vm_block_t *vm_ast_comp_more(vm_t *vm, vm_ast_node_t node) { +vm_ir_block_t *vm_ast_comp_more(vm_t *vm, vm_ast_node_t node) { vm_ast_comp_t comp = (vm_ast_comp_t){ .names = NULL, .cur = NULL, @@ -1067,19 +1067,19 @@ vm_block_t *vm_ast_comp_more(vm_t *vm, vm_ast_node_t node) { .vm = vm, }; vm_ast_comp_names_push(&comp); - vm_block_t *entry = vm_ast_comp_new_block(&comp); + vm_ir_block_t *entry = vm_ast_comp_new_block(&comp); size_t start = entry->id; comp.cur = entry; vm_ast_blocks_instr( &comp, - (vm_instr_t){ - .op = VM_IOP_STD, + (vm_ir_instr_t){ + .op = VM_IR_INSTR_OPCODE_STD, .out = vm_ast_comp_reg_named(&comp, "_ENV"), .args = vm_ast_args(0), } ); - vm_arg_t result_arg = vm_ast_comp_to_raw(&comp, node); - if (result_arg.type == VM_ARG_ERROR) { + vm_ir_arg_t result_arg = vm_ast_comp_to_raw(&comp, node); + if (result_arg.type == VM_IR_ARG_TYPE_ERROR) { for (vm_error_t *error = result_arg.error; error != NULL; error = error->child) { if (error->child != NULL) { fprintf(stderr, "range: %zu .. %zu\n", error->range.start.byte, error->range.stop.byte); diff --git a/vm/ast/comp.h b/vm/ast/comp.h index 881b8ef4..b3fa1c0d 100644 --- a/vm/ast/comp.h +++ b/vm/ast/comp.h @@ -5,6 +5,6 @@ #include "ast.h" #include "../ir.h" -vm_block_t *vm_ast_comp_more(vm_t *vm, vm_ast_node_t node); +vm_ir_block_t *vm_ast_comp_more(vm_t *vm, vm_ast_node_t node); #endif diff --git a/vm/backend/backend.c b/vm/backend/backend.c index 946a87b2..eea3bb5a 100644 --- a/vm/backend/backend.c +++ b/vm/backend/backend.c @@ -10,13 +10,12 @@ #include "../../vendor/spall/auto.h" -static int x = 0; - -#define VM_OPCODE_SPALL_BEGIN(s) \ - x++; \ - spall_auto_buffer_begin(#s, strlen(#s), "", 0) +#define VM_OPCODE_SPALL_BEGIN(s) ({ \ + char buf[64]; \ + signed long n = snprintf(buf, 64, "line %" PRIi32 ": %s", block->id, #s); \ + spall_auto_buffer_begin(buf, n, "", 0); \ +}) #define VM_OPCODE_SPALL_END() \ - x--; \ spall_auto_buffer_end() #else @@ -27,7 +26,9 @@ static int x = 0; #endif #if f -#define VM_OPCODE_DEBUG(s) VM_OPCODE_SPALL_BEGIN(s); printf("%s\n", #s); +#define VM_OPCODE_DEBUG(s) \ + VM_OPCODE_SPALL_BEGIN(s); \ + printf("%s\n", #s); #else #define VM_OPCODE_DEBUG(s) VM_OPCODE_SPALL_BEGIN(s); #endif @@ -37,8 +38,12 @@ static int x = 0; VM_OPCODE_SPALL_END(); \ return v; \ }) -#define vm_run_repl_jump() VM_OPCODE_SPALL_END(); goto *vm_run_repl_read(void *) -#define vm_backend_new_block() VM_OPCODE_SPALL_END(); goto new_block +#define vm_run_repl_jump() \ + VM_OPCODE_SPALL_END(); \ + goto *vm_run_repl_read(void *) +#define vm_backend_new_block() \ + VM_OPCODE_SPALL_END(); \ + goto new_block typedef uint8_t vm_interp_tag_t; typedef uint32_t vm_interp_reg_t; @@ -218,22 +223,22 @@ static VM_INLINE vm_obj_t vm_interp_mod(vm_obj_t v1, vm_obj_t v2) { #define vm_interp_push_op(v) vm_interp_push(void *, ptrs[v]) -void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { +void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_ir_block_t *block) { size_t alloc = 32; size_t len = 0; uint8_t *code = vm_malloc(sizeof(uint8_t) * alloc); for (size_t i = 0; i < block->len; i++) { - vm_instr_t instr = block->instrs[i]; + vm_ir_instr_t instr = block->instrs[i]; switch (instr.op) { - case VM_IOP_NOP: { + case VM_IR_INSTR_OPCODE_NOP: { break; } - case VM_IOP_MOVE: { - if (instr.args[0].type == VM_ARG_LIT) { + 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(vm_obj_t, instr.args[0].lit); vm_interp_push(vm_interp_reg_t, instr.out.reg); - } else if (instr.args[0].type == VM_ARG_REG) { + } else if (instr.args[0].type == VM_IR_ARG_TYPE_REG) { vm_interp_push_op(VM_OP_MOVE_R); vm_interp_push(vm_interp_reg_t, instr.args[0].reg); vm_interp_push(vm_interp_reg_t, instr.out.reg); @@ -242,25 +247,25 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { } break; } - case VM_IOP_ADD: { - if (instr.args[0].type == VM_ARG_LIT && instr.args[1].type == VM_ARG_LIT) { + case VM_IR_INSTR_OPCODE_ADD: { + if (instr.args[0].type == VM_IR_ARG_TYPE_LIT && instr.args[1].type == VM_IR_ARG_TYPE_LIT) { vm_obj_t v1 = instr.args[0].lit; vm_obj_t v2 = instr.args[1].lit; vm_obj_t v3 = vm_interp_add(v1, v2); vm_interp_push_op(VM_OP_MOVE_I); vm_interp_push(vm_obj_t, v3); vm_interp_push(vm_interp_reg_t, instr.out.reg); - } else if (instr.args[0].type == VM_ARG_REG && instr.args[1].type == VM_ARG_LIT) { + } 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(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_ARG_LIT && instr.args[1].type == VM_ARG_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(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); - } else if (instr.args[0].type == VM_ARG_REG && instr.args[1].type == VM_ARG_REG) { + } else if (instr.args[0].type == VM_IR_ARG_TYPE_REG && instr.args[1].type == VM_IR_ARG_TYPE_REG) { vm_interp_push_op(VM_OP_ADD_RR); vm_interp_push(vm_interp_reg_t, instr.args[0].reg); vm_interp_push(vm_interp_reg_t, instr.args[1].reg); @@ -270,25 +275,25 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { } break; } - case VM_IOP_SUB: { - if (instr.args[0].type == VM_ARG_LIT && instr.args[1].type == VM_ARG_LIT) { + case VM_IR_INSTR_OPCODE_SUB: { + if (instr.args[0].type == VM_IR_ARG_TYPE_LIT && instr.args[1].type == VM_IR_ARG_TYPE_LIT) { vm_obj_t v1 = instr.args[0].lit; vm_obj_t v2 = instr.args[1].lit; vm_obj_t v3 = vm_interp_sub(v1, v2); vm_interp_push_op(VM_OP_MOVE_I); vm_interp_push(vm_obj_t, v3); vm_interp_push(vm_interp_reg_t, instr.out.reg); - } else if (instr.args[0].type == VM_ARG_REG && instr.args[1].type == VM_ARG_LIT) { + } 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(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_ARG_LIT && instr.args[1].type == VM_ARG_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(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); - } else if (instr.args[0].type == VM_ARG_REG && instr.args[1].type == VM_ARG_REG) { + } else if (instr.args[0].type == VM_IR_ARG_TYPE_REG && instr.args[1].type == VM_IR_ARG_TYPE_REG) { vm_interp_push_op(VM_OP_SUB_RR); vm_interp_push(vm_interp_reg_t, instr.args[0].reg); vm_interp_push(vm_interp_reg_t, instr.args[1].reg); @@ -298,25 +303,25 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { } break; } - case VM_IOP_MUL: { - if (instr.args[0].type == VM_ARG_LIT && instr.args[1].type == VM_ARG_LIT) { + case VM_IR_INSTR_OPCODE_MUL: { + if (instr.args[0].type == VM_IR_ARG_TYPE_LIT && instr.args[1].type == VM_IR_ARG_TYPE_LIT) { vm_obj_t v1 = instr.args[0].lit; vm_obj_t v2 = instr.args[1].lit; vm_obj_t v3 = vm_interp_mul(v1, v2); vm_interp_push_op(VM_OP_MOVE_I); vm_interp_push(vm_obj_t, v3); vm_interp_push(vm_interp_reg_t, instr.out.reg); - } else if (instr.args[0].type == VM_ARG_REG && instr.args[1].type == VM_ARG_LIT) { + } 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(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_ARG_LIT && instr.args[1].type == VM_ARG_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(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); - } else if (instr.args[0].type == VM_ARG_REG && instr.args[1].type == VM_ARG_REG) { + } else if (instr.args[0].type == VM_IR_ARG_TYPE_REG && instr.args[1].type == VM_IR_ARG_TYPE_REG) { vm_interp_push_op(VM_OP_MUL_RR); vm_interp_push(vm_interp_reg_t, instr.args[0].reg); vm_interp_push(vm_interp_reg_t, instr.args[1].reg); @@ -326,25 +331,25 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { } break; } - case VM_IOP_DIV: { - if (instr.args[0].type == VM_ARG_LIT && instr.args[1].type == VM_ARG_LIT) { + case VM_IR_INSTR_OPCODE_DIV: { + if (instr.args[0].type == VM_IR_ARG_TYPE_LIT && instr.args[1].type == VM_IR_ARG_TYPE_LIT) { vm_obj_t v1 = instr.args[0].lit; vm_obj_t v2 = instr.args[1].lit; vm_obj_t v3 = vm_interp_div(v1, v2); vm_interp_push_op(VM_OP_MOVE_I); vm_interp_push(vm_obj_t, v3); vm_interp_push(vm_interp_reg_t, instr.out.reg); - } else if (instr.args[0].type == VM_ARG_REG && instr.args[1].type == VM_ARG_LIT) { + } 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(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_ARG_LIT && instr.args[1].type == VM_ARG_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(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); - } else if (instr.args[0].type == VM_ARG_REG && instr.args[1].type == VM_ARG_REG) { + } else if (instr.args[0].type == VM_IR_ARG_TYPE_REG && instr.args[1].type == VM_IR_ARG_TYPE_REG) { vm_interp_push_op(VM_OP_DIV_RR); vm_interp_push(vm_interp_reg_t, instr.args[0].reg); vm_interp_push(vm_interp_reg_t, instr.args[1].reg); @@ -354,25 +359,25 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { } break; } - case VM_IOP_IDIV: { - if (instr.args[0].type == VM_ARG_LIT && instr.args[1].type == VM_ARG_LIT) { + case VM_IR_INSTR_OPCODE_IDIV: { + if (instr.args[0].type == VM_IR_ARG_TYPE_LIT && instr.args[1].type == VM_IR_ARG_TYPE_LIT) { vm_obj_t v1 = instr.args[0].lit; vm_obj_t v2 = instr.args[1].lit; vm_obj_t v3 = vm_interp_idiv(v1, v2); vm_interp_push_op(VM_OP_MOVE_I); vm_interp_push(vm_obj_t, v3); vm_interp_push(vm_interp_reg_t, instr.out.reg); - } else if (instr.args[0].type == VM_ARG_REG && instr.args[1].type == VM_ARG_LIT) { + } 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(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_ARG_LIT && instr.args[1].type == VM_ARG_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(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); - } else if (instr.args[0].type == VM_ARG_REG && instr.args[1].type == VM_ARG_REG) { + } else if (instr.args[0].type == VM_IR_ARG_TYPE_REG && instr.args[1].type == VM_IR_ARG_TYPE_REG) { vm_interp_push_op(VM_OP_IDIV_RR); vm_interp_push(vm_interp_reg_t, instr.args[0].reg); vm_interp_push(vm_interp_reg_t, instr.args[1].reg); @@ -382,25 +387,25 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { } break; } - case VM_IOP_MOD: { - if (instr.args[0].type == VM_ARG_LIT && instr.args[1].type == VM_ARG_LIT) { + case VM_IR_INSTR_OPCODE_MOD: { + if (instr.args[0].type == VM_IR_ARG_TYPE_LIT && instr.args[1].type == VM_IR_ARG_TYPE_LIT) { vm_obj_t v1 = instr.args[0].lit; vm_obj_t v2 = instr.args[1].lit; vm_obj_t v3 = vm_interp_mod(v1, v2); vm_interp_push_op(VM_OP_MOVE_I); vm_interp_push(vm_obj_t, v3); vm_interp_push(vm_interp_reg_t, instr.out.reg); - } else if (instr.args[0].type == VM_ARG_REG && instr.args[1].type == VM_ARG_LIT) { + } 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(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_ARG_LIT && instr.args[1].type == VM_ARG_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(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); - } else if (instr.args[0].type == VM_ARG_REG && instr.args[1].type == VM_ARG_REG) { + } else if (instr.args[0].type == VM_IR_ARG_TYPE_REG && instr.args[1].type == VM_IR_ARG_TYPE_REG) { vm_interp_push_op(VM_OP_MOD_RR); vm_interp_push(vm_interp_reg_t, instr.args[0].reg); vm_interp_push(vm_interp_reg_t, instr.args[1].reg); @@ -410,11 +415,11 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { } break; } - case VM_IOP_TABLE_SET: { + case VM_IR_INSTR_OPCODE_TABLE_SET: { vm_interp_push_op(VM_OP_TABLE_SET); for (size_t i = 0; i < 3; i++) { vm_interp_push(vm_interp_tag_t, instr.args[i].type); - if (instr.args[i].type == VM_ARG_LIT) { + if (instr.args[i].type == VM_IR_ARG_TYPE_LIT) { vm_interp_push(vm_obj_t, instr.args[i].lit); } else { vm_interp_push(vm_interp_reg_t, instr.args[i].reg); @@ -422,15 +427,15 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { } break; } - case VM_IOP_TABLE_NEW: { + case VM_IR_INSTR_OPCODE_TABLE_NEW: { vm_interp_push_op(VM_OP_TABLE_NEW); vm_interp_push(vm_interp_reg_t, instr.out.reg); break; } - case VM_IOP_TABLE_LEN: { + case VM_IR_INSTR_OPCODE_TABLE_LEN: { vm_interp_push_op(VM_OP_TABLE_LEN); vm_interp_push(vm_interp_tag_t, instr.args[0].type); - if (instr.args[0].type == VM_ARG_LIT) { + if (instr.args[0].type == VM_IR_ARG_TYPE_LIT) { vm_interp_push(vm_obj_t, instr.args[0].lit); } else { vm_interp_push(vm_interp_reg_t, instr.args[0].reg); @@ -438,7 +443,7 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { vm_interp_push(vm_interp_reg_t, instr.out.reg); break; } - case VM_IOP_STD: { + case VM_IR_INSTR_OPCODE_STD: { vm_interp_push_op(VM_OP_MOVE_I); vm_interp_push(vm_obj_t, vm->std); vm_interp_push(vm_interp_reg_t, instr.out.reg); @@ -449,143 +454,143 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { } } } - vm_branch_t branch = block->branch; + vm_ir_branch_t branch = block->branch; switch (branch.op) { - case VM_BOP_FALL: { + case VM_IR_BRANCH_OPCODE_FALL: { break; } - case VM_BOP_JUMP: { + case VM_IR_BRANCH_OPCODE_JUMP: { vm_interp_push_op(VM_OP_JUMP); - vm_interp_push(vm_block_t *, branch.targets[0]); + vm_interp_push(vm_ir_block_t *, branch.targets[0]); break; } - case VM_BOP_BB: { - if (branch.args[0].type == VM_ARG_LIT) { + case VM_IR_BRANCH_OPCODE_BB: { + if (branch.args[0].type == VM_IR_ARG_TYPE_LIT) { vm_obj_t v1 = branch.args[0].lit; if (!vm_obj_is_nil(v1) && (!vm_obj_is_boolean(v1) || vm_obj_get_boolean(v1))) { vm_interp_push_op(VM_OP_JUMP); - vm_interp_push(vm_block_t *, branch.targets[0]); + vm_interp_push(vm_ir_block_t *, branch.targets[0]); } else { vm_interp_push_op(VM_OP_JUMP); - vm_interp_push(vm_block_t *, branch.targets[1]); + vm_interp_push(vm_ir_block_t *, branch.targets[1]); } - } else if (branch.args[0].type == VM_ARG_REG) { + } else if (branch.args[0].type == VM_IR_ARG_TYPE_REG) { vm_interp_push_op(VM_OP_BB_R); vm_interp_push(vm_interp_reg_t, branch.args[0].reg); - vm_interp_push(vm_block_t *, branch.targets[0]); - vm_interp_push(vm_block_t *, branch.targets[1]); + vm_interp_push(vm_ir_block_t *, branch.targets[0]); + vm_interp_push(vm_ir_block_t *, branch.targets[1]); } else { __builtin_trap(); } break; } - case VM_BOP_BLT: { - if (branch.args[0].type == VM_ARG_LIT && branch.args[1].type == VM_ARG_LIT) { + case VM_IR_BRANCH_OPCODE_BLT: { + if (branch.args[0].type == VM_IR_ARG_TYPE_LIT && branch.args[1].type == VM_IR_ARG_TYPE_LIT) { vm_obj_t v1 = branch.args[0].lit; vm_obj_t v2 = branch.args[1].lit; if (vm_interp_value_lt(v1, v2)) { vm_interp_push_op(VM_OP_JUMP); - vm_interp_push(vm_block_t *, branch.targets[0]); + vm_interp_push(vm_ir_block_t *, branch.targets[0]); } else { vm_interp_push_op(VM_OP_JUMP); - vm_interp_push(vm_block_t *, branch.targets[1]); + vm_interp_push(vm_ir_block_t *, branch.targets[1]); } - } else if (branch.args[0].type == VM_ARG_REG && branch.args[1].type == VM_ARG_LIT) { + } 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(vm_interp_reg_t, branch.args[0].reg); vm_interp_push(vm_obj_t, branch.args[1].lit); - vm_interp_push(vm_block_t *, branch.targets[0]); - vm_interp_push(vm_block_t *, branch.targets[1]); - } else if (branch.args[0].type == VM_ARG_LIT && branch.args[1].type == VM_ARG_REG) { + 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(vm_obj_t, branch.args[0].lit); vm_interp_push(vm_interp_reg_t, branch.args[1].reg); - vm_interp_push(vm_block_t *, branch.targets[0]); - vm_interp_push(vm_block_t *, branch.targets[1]); - } else if (branch.args[0].type == VM_ARG_REG && branch.args[1].type == VM_ARG_REG) { + 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_REG && branch.args[1].type == VM_IR_ARG_TYPE_REG) { vm_interp_push_op(VM_OP_BLT_RR); vm_interp_push(vm_interp_reg_t, branch.args[0].reg); vm_interp_push(vm_interp_reg_t, branch.args[1].reg); - vm_interp_push(vm_block_t *, branch.targets[0]); - vm_interp_push(vm_block_t *, branch.targets[1]); + vm_interp_push(vm_ir_block_t *, branch.targets[0]); + vm_interp_push(vm_ir_block_t *, branch.targets[1]); } else { __builtin_trap(); } break; } - case VM_BOP_BLE: { - if (branch.args[0].type == VM_ARG_LIT && branch.args[1].type == VM_ARG_LIT) { + case VM_IR_BRANCH_OPCODE_BLE: { + if (branch.args[0].type == VM_IR_ARG_TYPE_LIT && branch.args[1].type == VM_IR_ARG_TYPE_LIT) { vm_obj_t v1 = branch.args[0].lit; vm_obj_t v2 = branch.args[1].lit; if (vm_interp_value_le(v1, v2)) { vm_interp_push_op(VM_OP_JUMP); - vm_interp_push(vm_block_t *, branch.targets[0]); + vm_interp_push(vm_ir_block_t *, branch.targets[0]); } else { vm_interp_push_op(VM_OP_JUMP); - vm_interp_push(vm_block_t *, branch.targets[1]); + vm_interp_push(vm_ir_block_t *, branch.targets[1]); } - } else if (branch.args[0].type == VM_ARG_REG && branch.args[1].type == VM_ARG_LIT) { + } 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(vm_interp_reg_t, branch.args[0].reg); vm_interp_push(vm_obj_t, branch.args[1].lit); - vm_interp_push(vm_block_t *, branch.targets[0]); - vm_interp_push(vm_block_t *, branch.targets[1]); - } else if (branch.args[0].type == VM_ARG_LIT && branch.args[1].type == VM_ARG_REG) { + 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(vm_obj_t, branch.args[0].lit); vm_interp_push(vm_interp_reg_t, branch.args[1].reg); - vm_interp_push(vm_block_t *, branch.targets[0]); - vm_interp_push(vm_block_t *, branch.targets[1]); - } else if (branch.args[0].type == VM_ARG_REG && branch.args[1].type == VM_ARG_REG) { + 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_REG && branch.args[1].type == VM_IR_ARG_TYPE_REG) { vm_interp_push_op(VM_OP_BLE_RR); vm_interp_push(vm_interp_reg_t, branch.args[0].reg); vm_interp_push(vm_interp_reg_t, branch.args[1].reg); - vm_interp_push(vm_block_t *, branch.targets[0]); - vm_interp_push(vm_block_t *, branch.targets[1]); + vm_interp_push(vm_ir_block_t *, branch.targets[0]); + vm_interp_push(vm_ir_block_t *, branch.targets[1]); } else { __builtin_trap(); } break; } - case VM_BOP_BEQ: { - if (branch.args[0].type == VM_ARG_LIT && branch.args[1].type == VM_ARG_LIT) { + case VM_IR_BRANCH_OPCODE_BEQ: { + if (branch.args[0].type == VM_IR_ARG_TYPE_LIT && branch.args[1].type == VM_IR_ARG_TYPE_LIT) { vm_obj_t v1 = branch.args[0].lit; vm_obj_t v2 = branch.args[1].lit; if (vm_interp_value_eq(v1, v2)) { vm_interp_push_op(VM_OP_JUMP); - vm_interp_push(vm_block_t *, branch.targets[0]); + vm_interp_push(vm_ir_block_t *, branch.targets[0]); } else { vm_interp_push_op(VM_OP_JUMP); - vm_interp_push(vm_block_t *, branch.targets[1]); + vm_interp_push(vm_ir_block_t *, branch.targets[1]); } - } else if (branch.args[0].type == VM_ARG_REG && branch.args[1].type == VM_ARG_LIT) { + } 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(vm_interp_reg_t, branch.args[0].reg); vm_interp_push(vm_obj_t, branch.args[1].lit); - vm_interp_push(vm_block_t *, branch.targets[0]); - vm_interp_push(vm_block_t *, branch.targets[1]); - } else if (branch.args[0].type == VM_ARG_LIT && branch.args[1].type == VM_ARG_REG) { + 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(vm_obj_t, branch.args[0].lit); vm_interp_push(vm_interp_reg_t, branch.args[1].reg); - vm_interp_push(vm_block_t *, branch.targets[0]); - vm_interp_push(vm_block_t *, branch.targets[1]); - } else if (branch.args[0].type == VM_ARG_REG && branch.args[1].type == VM_ARG_REG) { + 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_REG && branch.args[1].type == VM_IR_ARG_TYPE_REG) { vm_interp_push_op(VM_OP_BEQ_RR); vm_interp_push(vm_interp_reg_t, branch.args[0].reg); vm_interp_push(vm_interp_reg_t, branch.args[1].reg); - vm_interp_push(vm_block_t *, branch.targets[0]); - vm_interp_push(vm_block_t *, branch.targets[1]); + vm_interp_push(vm_ir_block_t *, branch.targets[0]); + vm_interp_push(vm_ir_block_t *, branch.targets[1]); } else { __builtin_trap(); } break; } - case VM_BOP_RET: { - if (branch.args[0].type == VM_ARG_LIT) { + case VM_IR_BRANCH_OPCODE_RET: { + if (branch.args[0].type == VM_IR_ARG_TYPE_LIT) { vm_interp_push_op(VM_OP_RET_I); vm_interp_push(vm_obj_t, branch.args[0].lit); - } else if (branch.args[0].type == VM_ARG_REG) { + } else if (branch.args[0].type == VM_IR_ARG_TYPE_REG) { vm_interp_push_op(VM_OP_RET_R); vm_interp_push(vm_interp_reg_t, branch.args[0].reg); } else { @@ -593,58 +598,58 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { } break; } - case VM_BOP_LOAD: { + case VM_IR_BRANCH_OPCODE_LOAD: { vm_interp_push_op(VM_OP_LOAD); vm_interp_push(vm_interp_tag_t, branch.args[0].type); - if (branch.args[0].type == VM_ARG_LIT) { + if (branch.args[0].type == VM_IR_ARG_TYPE_LIT) { vm_interp_push(vm_obj_t, branch.args[0].lit); } else { vm_interp_push(vm_interp_reg_t, branch.args[0].reg); } vm_interp_push(vm_interp_tag_t, branch.args[1].type); - if (branch.args[1].type == VM_ARG_LIT) { + if (branch.args[1].type == VM_IR_ARG_TYPE_LIT) { vm_interp_push(vm_obj_t, branch.args[1].lit); } else { vm_interp_push(vm_interp_reg_t, branch.args[1].reg); } vm_interp_push(vm_interp_reg_t, branch.out.reg); - vm_interp_push(vm_block_t *, branch.targets[0]); + vm_interp_push(vm_ir_block_t *, branch.targets[0]); break; } - case VM_BOP_GET: { + case VM_IR_BRANCH_OPCODE_GET: { vm_interp_push_op(VM_OP_GET); vm_interp_push(vm_interp_tag_t, branch.args[0].type); - if (branch.args[0].type == VM_ARG_LIT) { + if (branch.args[0].type == VM_IR_ARG_TYPE_LIT) { vm_interp_push(vm_obj_t, branch.args[0].lit); } else { vm_interp_push(vm_interp_reg_t, branch.args[0].reg); } vm_interp_push(vm_interp_tag_t, branch.args[1].type); - if (branch.args[1].type == VM_ARG_LIT) { + if (branch.args[1].type == VM_IR_ARG_TYPE_LIT) { vm_interp_push(vm_obj_t, branch.args[1].lit); } else { vm_interp_push(vm_interp_reg_t, branch.args[1].reg); } vm_interp_push(vm_interp_reg_t, branch.out.reg); - vm_interp_push(vm_block_t *, branch.targets[0]); + vm_interp_push(vm_ir_block_t *, branch.targets[0]); break; } - case VM_BOP_CALL: { + case VM_IR_BRANCH_OPCODE_CALL: { vm_interp_push_op(VM_OP_CALL); - for (size_t i = 0; branch.args[i].type != VM_ARG_NONE; i++) { - if (branch.args[i].type == VM_ARG_LIT) { - vm_interp_push(vm_interp_tag_t, VM_ARG_LIT); + for (size_t i = 0; branch.args[i].type != VM_IR_ARG_TYPE_NONE; i++) { + if (branch.args[i].type == VM_IR_ARG_TYPE_LIT) { + vm_interp_push(vm_interp_tag_t, VM_IR_ARG_TYPE_LIT); vm_interp_push(vm_obj_t, branch.args[i].lit); - } else if (branch.args[i].type == VM_ARG_REG) { - vm_interp_push(vm_interp_tag_t, VM_ARG_REG); + } else if (branch.args[i].type == VM_IR_ARG_TYPE_REG) { + vm_interp_push(vm_interp_tag_t, VM_IR_ARG_TYPE_REG); vm_interp_push(vm_interp_reg_t, branch.args[i].reg); } else { __builtin_trap(); } } - vm_interp_push(vm_interp_tag_t, VM_ARG_NONE); + vm_interp_push(vm_interp_tag_t, VM_IR_ARG_TYPE_NONE); vm_interp_push(vm_interp_reg_t, branch.out.reg); - vm_interp_push(vm_block_t *, branch.targets[0]); + vm_interp_push(vm_ir_block_t *, branch.targets[0]); break; } default: { @@ -674,13 +679,13 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { #if 0 #define vm_run_repl_arg() ( \ - vm_run_repl_read(vm_interp_tag_t) == VM_ARG_LIT \ + vm_run_repl_read(vm_interp_tag_t) == VM_IR_ARG_TYPE_LIT \ ? (printf(":LIT\n"), vm_run_repl_lit()) \ : (printf(":REG\n"), vm_run_repl_reg()) \ ) #else #define vm_run_repl_arg() ( \ - vm_run_repl_read(vm_interp_tag_t) == VM_ARG_LIT \ + vm_run_repl_read(vm_interp_tag_t) == VM_IR_ARG_TYPE_LIT \ ? vm_run_repl_lit() \ : vm_run_repl_reg() \ ) @@ -688,7 +693,7 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { #define vm_run_repl_out(value) (regs[vm_run_repl_read(vm_interp_reg_t)] = (value)) -vm_obj_t vm_run_repl_inner(vm_t *vm, vm_block_t *block) { +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, @@ -999,17 +1004,17 @@ VM_OP_MOD_RR:; } VM_OP_JUMP:; VM_OPCODE_DEBUG(jump) { - block = vm_run_repl_read(vm_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); vm_backend_new_block(); } VM_OP_BB_R:; VM_OPCODE_DEBUG(bb_r) { vm_obj_t v1 = vm_run_repl_reg(); if (!vm_obj_is_nil(v1) && (!vm_obj_is_boolean(v1) || vm_obj_get_boolean(v1))) { - block = vm_run_repl_read(vm_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); } else { - vm_run_repl_read(vm_block_t *); - block = vm_run_repl_read(vm_block_t *); + vm_run_repl_read(vm_ir_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); } vm_backend_new_block(); } @@ -1018,10 +1023,10 @@ VM_OP_BLT_RI:; vm_obj_t v1 = vm_run_repl_reg(); vm_obj_t v2 = vm_run_repl_lit(); if (vm_interp_value_lt(v1, v2)) { - block = vm_run_repl_read(vm_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); } else { - vm_run_repl_read(vm_block_t *); - block = vm_run_repl_read(vm_block_t *); + vm_run_repl_read(vm_ir_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); } vm_backend_new_block(); } @@ -1030,10 +1035,10 @@ VM_OP_BLT_IR:; vm_obj_t v1 = vm_run_repl_lit(); vm_obj_t v2 = vm_run_repl_reg(); if (vm_interp_value_lt(v1, v2)) { - block = vm_run_repl_read(vm_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); } else { - vm_run_repl_read(vm_block_t *); - block = vm_run_repl_read(vm_block_t *); + vm_run_repl_read(vm_ir_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); } vm_backend_new_block(); } @@ -1042,10 +1047,10 @@ VM_OP_BLT_RR:; vm_obj_t v1 = vm_run_repl_reg(); vm_obj_t v2 = vm_run_repl_reg(); if (vm_interp_value_lt(v1, v2)) { - block = vm_run_repl_read(vm_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); } else { - vm_run_repl_read(vm_block_t *); - block = vm_run_repl_read(vm_block_t *); + vm_run_repl_read(vm_ir_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); } vm_backend_new_block(); } @@ -1054,10 +1059,10 @@ VM_OP_BLE_RI:; vm_obj_t v1 = vm_run_repl_reg(); vm_obj_t v2 = vm_run_repl_lit(); if (vm_interp_value_le(v1, v2)) { - block = vm_run_repl_read(vm_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); } else { - vm_run_repl_read(vm_block_t *); - block = vm_run_repl_read(vm_block_t *); + vm_run_repl_read(vm_ir_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); } vm_backend_new_block(); } @@ -1066,10 +1071,10 @@ VM_OP_BLE_IR:; vm_obj_t v1 = vm_run_repl_lit(); vm_obj_t v2 = vm_run_repl_reg(); if (vm_interp_value_le(v1, v2)) { - block = vm_run_repl_read(vm_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); } else { - vm_run_repl_read(vm_block_t *); - block = vm_run_repl_read(vm_block_t *); + vm_run_repl_read(vm_ir_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); } vm_backend_new_block(); } @@ -1078,10 +1083,10 @@ VM_OP_BLE_RR:; vm_obj_t v1 = vm_run_repl_reg(); vm_obj_t v2 = vm_run_repl_reg(); if (vm_interp_value_le(v1, v2)) { - block = vm_run_repl_read(vm_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); } else { - vm_run_repl_read(vm_block_t *); - block = vm_run_repl_read(vm_block_t *); + vm_run_repl_read(vm_ir_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); } vm_backend_new_block(); } @@ -1090,10 +1095,10 @@ VM_OP_BEQ_RI:; vm_obj_t v1 = vm_run_repl_reg(); vm_obj_t v2 = vm_run_repl_lit(); if (vm_obj_eq(v1, v2)) { - block = vm_run_repl_read(vm_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); } else { - vm_run_repl_read(vm_block_t *); - block = vm_run_repl_read(vm_block_t *); + vm_run_repl_read(vm_ir_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); } vm_backend_new_block(); } @@ -1102,10 +1107,10 @@ VM_OP_BEQ_IR:; vm_obj_t v1 = vm_run_repl_lit(); vm_obj_t v2 = vm_run_repl_reg(); if (vm_obj_eq(v1, v2)) { - block = vm_run_repl_read(vm_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); } else { - vm_run_repl_read(vm_block_t *); - block = vm_run_repl_read(vm_block_t *); + vm_run_repl_read(vm_ir_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); } vm_backend_new_block(); } @@ -1114,10 +1119,10 @@ VM_OP_BEQ_RR:; vm_obj_t v1 = vm_run_repl_reg(); vm_obj_t v2 = vm_run_repl_reg(); if (vm_obj_eq(v1, v2)) { - block = vm_run_repl_read(vm_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); } else { - vm_run_repl_read(vm_block_t *); - block = vm_run_repl_read(vm_block_t *); + vm_run_repl_read(vm_ir_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); } vm_backend_new_block(); } @@ -1137,7 +1142,7 @@ VM_OP_LOAD:; vm_obj_t v2 = vm_run_repl_arg(); vm_obj_t v3 = vm_obj_get_closure(v1)->values[(int32_t)vm_obj_get_number(v2)]; vm_run_repl_out(v3); - block = vm_run_repl_read(vm_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); vm_backend_new_block(); } VM_OP_GET:; @@ -1153,7 +1158,7 @@ VM_OP_GET:; }; vm_table_get_pair(vm_obj_get_table(v1), &pair); vm_run_repl_out(pair.value); - block = vm_run_repl_read(vm_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); vm_backend_new_block(); } VM_OP_CALL:; @@ -1165,14 +1170,14 @@ VM_OP_CALL:; call_closure_next_arg:; uint8_t type = vm_run_repl_read(vm_interp_tag_t); switch (type) { - case VM_ARG_NONE: { + case VM_IR_ARG_TYPE_NONE: { goto call_closure_end; } - case VM_ARG_REG: { + case VM_IR_ARG_TYPE_REG: { next_regs[j++] = vm_run_repl_reg(); goto call_closure_next_arg; } - case VM_ARG_LIT: { + case VM_IR_ARG_TYPE_LIT: { next_regs[j++] = vm_run_repl_lit(); goto call_closure_next_arg; } @@ -1190,21 +1195,21 @@ VM_OP_CALL:; vm_backend_return(vm_obj_of_error(vm_error_from_error(block->range, vm_obj_get_error(got)))); } vm_run_repl_out(got); - block = vm_run_repl_read(vm_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); vm_backend_new_block(); - } else if (vm_obj_is_ffi(v1)) { + } else if (vm_obj_is_ffi(v1)) { size_t j = 0; call_ffi_next_arg:; uint8_t type = vm_run_repl_read(vm_interp_tag_t); switch (type) { - case VM_ARG_NONE: { + case VM_IR_ARG_TYPE_NONE: { goto call_ffi_end; } - case VM_ARG_REG: { + case VM_IR_ARG_TYPE_REG: { next_regs[j++] = vm_run_repl_reg(); goto call_ffi_next_arg; } - case VM_ARG_LIT: { + case VM_IR_ARG_TYPE_LIT: { next_regs[j++] = vm_run_repl_lit(); goto call_ffi_next_arg; } @@ -1224,7 +1229,7 @@ VM_OP_CALL:; } vm_run_repl_out(next_regs[0]); vm_gc_run(vm, next_regs); - block = vm_run_repl_read(vm_block_t *); + block = vm_run_repl_read(vm_ir_block_t *); vm_backend_new_block(); } else { vm_backend_return(vm_obj_of_error(vm_error_from_msg(block->range, "can only call functions"))); @@ -1233,8 +1238,8 @@ VM_OP_CALL:; } } -vm_obj_t vm_run_repl(vm_t *vm, vm_block_t *entry) { - vm_blocks_t blocks = (vm_blocks_t) { +vm_obj_t vm_run_repl(vm_t *vm, vm_ir_block_t *entry) { + vm_ir_blocks_t blocks = (vm_ir_blocks_t){ .next = vm->blocks, .block = entry, }; @@ -1244,7 +1249,7 @@ vm_obj_t vm_run_repl(vm_t *vm, vm_block_t *entry) { return ret; } -vm_obj_t vm_run_main(vm_t *vm, vm_block_t *entry) { +vm_obj_t vm_run_main(vm_t *vm, vm_ir_block_t *entry) { vm_obj_t val = vm_run_repl(vm, entry); if (vm_obj_is_error(val)) { vm_error_report(vm_obj_get_error(val), stderr); diff --git a/vm/backend/backend.h b/vm/backend/backend.h index adc49257..f4004b1a 100644 --- a/vm/backend/backend.h +++ b/vm/backend/backend.h @@ -5,7 +5,7 @@ #include "../vm.h" #include "../ir.h" -vm_obj_t vm_run_main(vm_t *vm, vm_block_t *entry); -vm_obj_t vm_run_repl(vm_t *vm, vm_block_t *entry); +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); #endif diff --git a/vm/gc.c b/vm/gc.c index 2c47c994..33cd997f 100644 --- a/vm/gc.c +++ b/vm/gc.c @@ -29,25 +29,25 @@ struct vm_gc_t { static void vm_gc_mark_obj(vm_obj_t obj); -static void vm_gc_mark_arg(vm_arg_t arg) { - if (arg.type == VM_ARG_LIT) { +static void vm_gc_mark_arg(vm_ir_arg_t arg) { + if (arg.type == VM_IR_ARG_TYPE_LIT) { vm_gc_mark_obj(arg.lit); } } -static void vm_gc_mark_block(vm_block_t *block) { +static void vm_gc_mark_block(vm_ir_block_t *block) { if (!block->mark) { block->mark = true; for (size_t j = 0; j < block->len; j++) { - vm_instr_t *instr = &block->instrs[j]; - for (size_t k = 0; instr->args[k].type != VM_ARG_NONE; k++) { + vm_ir_instr_t *instr = &block->instrs[j]; + for (size_t k = 0; instr->args[k].type != VM_IR_ARG_TYPE_NONE; k++) { vm_gc_mark_arg(instr->args[k]); } } for (size_t j = 0; j < 2 && block->branch.targets[j] != NULL; j++) { vm_gc_mark_block(block->branch.targets[j]); } - for (size_t k = 0; block->branch.args[k].type != VM_ARG_NONE; k++) { + for (size_t k = 0; block->branch.args[k].type != VM_IR_ARG_TYPE_NONE; k++) { vm_gc_mark_arg(block->branch.args[k]); } vm_gc_mark_arg(block->branch.out); @@ -82,7 +82,7 @@ static void vm_gc_mark_obj(vm_obj_t obj) { } void vm_gc_mark(vm_t *vm, vm_obj_t *top) { - for (vm_blocks_t *blocks = vm->blocks; blocks; blocks = blocks->next) { + for (vm_ir_blocks_t *blocks = vm->blocks; blocks; blocks = blocks->next) { vm_gc_mark_block(blocks->block); } vm_gc_mark_obj(vm->std); @@ -125,10 +125,10 @@ void vm_gc_sweep(vm_t *vm) { gc->objs.objs[write++] = obj; } } else if (vm_obj_is_block(obj)) { - vm_block_t *block = vm_obj_get_block(obj); + vm_ir_block_t *block = vm_obj_get_block(obj); if (!block->mark) { for (size_t j = 0; j < block->len; j++) { - vm_instr_t instr = block->instrs[j]; + vm_ir_instr_t instr = block->instrs[j]; vm_free(instr.args); } vm_free(block->instrs); diff --git a/vm/io.c b/vm/io.c index f8e7e598..8aaaa122 100644 --- a/vm/io.c +++ b/vm/io.c @@ -85,7 +85,7 @@ char *vm_io_read(const char *buf) { return ops; } -static void vm_indent(vm_io_buffer_t *out, size_t indent, const char *prefix) { +static void vm_io_indent(vm_io_buffer_t *out, size_t indent, const char *prefix) { while (indent-- > 0) { vm_io_buffer_format(out, " "); } @@ -114,7 +114,7 @@ void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_obj_ size_t up = 1; while (link != NULL) { if (vm_obj_eq(value, link->value)) { - vm_indent(out, indent, prefix); + vm_io_indent(out, indent, prefix); vm_io_buffer_format(out, "\n", up); return; } @@ -126,11 +126,11 @@ void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_obj_ .value = value, }; if (vm_obj_is_nil(value)) { - vm_indent(out, indent, prefix); + vm_io_indent(out, indent, prefix); vm_io_buffer_format(out, "nil\n"); } if (vm_obj_is_boolean(value)) { - vm_indent(out, indent, prefix); + vm_io_indent(out, indent, prefix); if (vm_obj_get_boolean(value)) { vm_io_buffer_format(out, "true\n"); } else { @@ -138,28 +138,28 @@ void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_obj_ } } if (vm_obj_is_number(value)) { - vm_indent(out, indent, prefix); + vm_io_indent(out, indent, prefix); vm_io_buffer_format(out, VM_FORMAT_FLOAT "\n", vm_obj_get_number(value)); } if (vm_obj_is_string(value)) { - vm_indent(out, indent, prefix); + vm_io_indent(out, indent, prefix); vm_io_buffer_format(out, "\"%s\"\n", vm_obj_get_string(value)->buf); } if (vm_obj_is_closure(value)) { - vm_indent(out, indent, prefix); + vm_io_indent(out, indent, prefix); vm_io_buffer_format(out, "\n", vm_obj_get_closure(value)); } if (vm_obj_is_block(value)) { - vm_indent(out, indent, prefix); + vm_io_indent(out, indent, prefix); vm_io_buffer_format(out, "\n", vm_obj_get_block(value)); } if (vm_obj_is_table(value)) { vm_table_t *tab = vm_obj_get_table(value); if (tab == NULL) { - vm_indent(out, indent, prefix); + vm_io_indent(out, indent, prefix); vm_io_buffer_format(out, "table(NULL)\n"); } else { - vm_indent(out, indent, prefix); + vm_io_indent(out, indent, prefix); vm_io_buffer_format(out, "table(%p) {\n", tab); size_t len = 1 << tab->alloc; for (size_t i = 0; i < len; i++) { @@ -183,20 +183,20 @@ void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_obj_ vm_io_debug(out, indent + 1, buf.buf, p.value, &next); vm_free(buf.buf); } else { - vm_indent(out, indent + 1, ""); + vm_io_indent(out, indent + 1, ""); vm_io_buffer_format(out, "pair {\n"); vm_io_debug(out, indent + 2, "key = ", p.key, &next); vm_io_debug(out, indent + 2, "val = ", p.value, &next); - vm_indent(out, indent + 1, ""); + vm_io_indent(out, indent + 1, ""); vm_io_buffer_format(out, "}\n"); } } } - vm_indent(out, indent, ""); + vm_io_indent(out, indent, ""); vm_io_buffer_format(out, "}\n"); } if (vm_obj_is_ffi(value)) { - vm_indent(out, indent, prefix); + vm_io_indent(out, indent, prefix); vm_io_buffer_format(out, "\n", vm_obj_get_ffi(value)); } } diff --git a/vm/ir.c b/vm/ir.c index 9c9294e1..1d5f1228 100644 --- a/vm/ir.c +++ b/vm/ir.c @@ -2,76 +2,76 @@ #include "ir.h" #include "io.h" -void vm_block_realloc(vm_block_t *block, vm_instr_t instr) { +void vm_block_realloc(vm_ir_block_t *block, vm_ir_instr_t instr) { if (block->len + 4 >= block->alloc) { block->alloc = (block->len + 4) * 4; block->instrs = - vm_realloc(block->instrs, sizeof(vm_instr_t) * block->alloc); + vm_realloc(block->instrs, sizeof(vm_ir_instr_t) * block->alloc); } block->instrs[block->len++] = instr; } -void vm_io_format_arg(vm_io_buffer_t *out, vm_arg_t val) { +void vm_io_format_arg(vm_io_buffer_t *out, vm_ir_arg_t val) { switch (val.type) { - case VM_ARG_LIT: { + case VM_IR_ARG_TYPE_LIT: { vm_io_print_lit(out, val.lit); break; } - case VM_ARG_REG: { + case VM_IR_ARG_TYPE_REG: { vm_io_buffer_format(out, "%%%zu", (size_t)val.reg); break; } } } -void vm_io_format_branch(vm_io_buffer_t *out, vm_branch_t val) { - if (val.out.type != VM_ARG_NONE) { +void vm_io_format_branch(vm_io_buffer_t *out, vm_ir_branch_t val) { + if (val.out.type != VM_IR_ARG_TYPE_NONE) { vm_io_format_arg(out, val.out); vm_io_buffer_format(out, " <- "); } switch (val.op) { - case VM_BOP_JUMP: { + case VM_IR_BRANCH_OPCODE_JUMP: { vm_io_buffer_format(out, "jump"); break; } - case VM_BOP_BB: { + case VM_IR_BRANCH_OPCODE_BB: { vm_io_buffer_format(out, "bb"); break; } - case VM_BOP_BLT: { + case VM_IR_BRANCH_OPCODE_BLT: { vm_io_buffer_format(out, "blt"); break; } - case VM_BOP_BLE: { + case VM_IR_BRANCH_OPCODE_BLE: { vm_io_buffer_format(out, "ble"); break; } - case VM_BOP_BEQ: { + case VM_IR_BRANCH_OPCODE_BEQ: { vm_io_buffer_format(out, "beq"); break; } - case VM_BOP_RET: { + case VM_IR_BRANCH_OPCODE_RET: { vm_io_buffer_format(out, "ret"); break; } - case VM_BOP_GET: { + case VM_IR_BRANCH_OPCODE_GET: { vm_io_buffer_format(out, "get"); break; } - case VM_BOP_LOAD: { + case VM_IR_BRANCH_OPCODE_LOAD: { vm_io_buffer_format(out, "load"); break; } - case VM_BOP_CALL: { + case VM_IR_BRANCH_OPCODE_CALL: { vm_io_buffer_format(out, "call"); break; } } - if (val.op == VM_BOP_CALL) { + if (val.op == VM_IR_BRANCH_OPCODE_CALL) { vm_io_buffer_format(out, " "); vm_io_format_arg(out, val.args[0]); vm_io_buffer_format(out, "("); - for (size_t i = 1; val.args[i].type != VM_ARG_NONE; i++) { + for (size_t i = 1; val.args[i].type != VM_IR_ARG_TYPE_NONE; i++) { if (i != 1) { vm_io_buffer_format(out, ", "); } @@ -79,12 +79,12 @@ void vm_io_format_branch(vm_io_buffer_t *out, vm_branch_t val) { } vm_io_buffer_format(out, ")"); } else { - for (size_t i = 0; val.args[i].type != VM_ARG_NONE; i++) { + for (size_t i = 0; val.args[i].type != VM_IR_ARG_TYPE_NONE; i++) { vm_io_buffer_format(out, " "); vm_io_format_arg(out, val.args[i]); } } - if (val.op == VM_BOP_LOAD || val.op == VM_BOP_GET || val.op == VM_BOP_CALL) { + if (val.op == VM_IR_BRANCH_OPCODE_LOAD || val.op == VM_IR_BRANCH_OPCODE_GET || val.op == VM_IR_BRANCH_OPCODE_CALL) { vm_io_buffer_format(out, " then .%zi", (size_t)val.targets[0]->id); } else { if (val.targets[0]) { @@ -96,57 +96,57 @@ void vm_io_format_branch(vm_io_buffer_t *out, vm_branch_t val) { } } -void vm_io_format_instr(vm_io_buffer_t *out, vm_instr_t val) { - if (val.op == VM_IOP_NOP) { +void vm_io_format_instr(vm_io_buffer_t *out, vm_ir_instr_t val) { + if (val.op == VM_IR_INSTR_OPCODE_NOP) { vm_io_buffer_format(out, "nop"); return; } - if (val.out.type != VM_ARG_NONE) { + if (val.out.type != VM_IR_ARG_TYPE_NONE) { vm_io_format_arg(out, val.out); vm_io_buffer_format(out, " <- "); } switch (val.op) { - case VM_IOP_MOVE: { + case VM_IR_INSTR_OPCODE_MOVE: { vm_io_buffer_format(out, "move"); break; } - case VM_IOP_ADD: { + case VM_IR_INSTR_OPCODE_ADD: { vm_io_buffer_format(out, "add"); break; } - case VM_IOP_SUB: { + case VM_IR_INSTR_OPCODE_SUB: { vm_io_buffer_format(out, "sub"); break; } - case VM_IOP_MUL: { + case VM_IR_INSTR_OPCODE_MUL: { vm_io_buffer_format(out, "mul"); break; } - case VM_IOP_DIV: { + case VM_IR_INSTR_OPCODE_DIV: { vm_io_buffer_format(out, "div"); break; } - case VM_IOP_IDIV: { + case VM_IR_INSTR_OPCODE_IDIV: { vm_io_buffer_format(out, "idiv"); break; } - case VM_IOP_MOD: { + case VM_IR_INSTR_OPCODE_MOD: { vm_io_buffer_format(out, "mod"); break; } - case VM_IOP_TABLE_SET: { + case VM_IR_INSTR_OPCODE_TABLE_SET: { vm_io_buffer_format(out, "set"); break; } - case VM_IOP_TABLE_NEW: { + case VM_IR_INSTR_OPCODE_TABLE_NEW: { vm_io_buffer_format(out, "new"); break; } - case VM_IOP_STD: { + case VM_IR_INSTR_OPCODE_STD: { vm_io_buffer_format(out, "std"); break; } - case VM_IOP_TABLE_LEN: { + case VM_IR_INSTR_OPCODE_TABLE_LEN: { vm_io_buffer_format(out, "len"); break; } @@ -155,26 +155,26 @@ void vm_io_format_instr(vm_io_buffer_t *out, vm_instr_t val) { break; } } - for (size_t i = 0; val.args[i].type != VM_ARG_NONE; i++) { + for (size_t i = 0; val.args[i].type != VM_IR_ARG_TYPE_NONE; i++) { vm_io_buffer_format(out, " "); vm_io_format_arg(out, val.args[i]); } } -void vm_io_format_block(vm_io_buffer_t *out, vm_block_t *val) { +void vm_io_format_block(vm_io_buffer_t *out, vm_ir_block_t *val) { if (val == NULL) { printf("\n"); } vm_io_buffer_format(out, ".%zi:\n", (ptrdiff_t)val->id); for (size_t i = 0; i < val->len; i++) { - if (val->instrs[i].op == VM_IOP_NOP) { + if (val->instrs[i].op == VM_IR_INSTR_OPCODE_NOP) { continue; } vm_io_buffer_format(out, " "); vm_io_format_instr(out, val->instrs[i]); vm_io_buffer_format(out, "\n"); } - if (val->branch.op != VM_BOP_FALL) { + if (val->branch.op != VM_IR_BRANCH_OPCODE_FALL) { vm_io_buffer_format(out, " "); vm_io_format_branch(out, val->branch); vm_io_buffer_format(out, "\n"); diff --git a/vm/ir.h b/vm/ir.h index c6559951..c6efb576 100644 --- a/vm/ir.h +++ b/vm/ir.h @@ -2,15 +2,15 @@ #if !defined(VM_HEADER_IR) #define VM_HEADER_IR -struct vm_arg_t; -struct vm_block_t; -struct vm_branch_t; -struct vm_instr_t; +struct vm_ir_arg_t; +struct vm_ir_block_t; +struct vm_ir_branch_t; +struct vm_ir_instr_t; -typedef struct vm_arg_t vm_arg_t; -typedef struct vm_block_t vm_block_t; -typedef struct vm_branch_t vm_branch_t; -typedef struct vm_instr_t vm_instr_t; +typedef struct vm_ir_arg_t vm_ir_arg_t; +typedef struct vm_ir_block_t vm_ir_block_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" @@ -18,48 +18,46 @@ typedef struct vm_instr_t vm_instr_t; enum { // there are no more args - VM_ARG_NONE, + VM_IR_ARG_TYPE_NONE, // there was an error generating this arg - VM_ARG_ERROR, + VM_IR_ARG_TYPE_ERROR, // normal args - VM_ARG_REG, - VM_ARG_LIT, + VM_IR_ARG_TYPE_REG, + VM_IR_ARG_TYPE_LIT, }; enum { - VM_BOP_FALL, - VM_BOP_JUMP, - VM_BOP_BB, - VM_BOP_BLT, - VM_BOP_BLE, - VM_BOP_BEQ, - VM_BOP_RET, - VM_BOP_LOAD, - VM_BOP_GET, - VM_BOP_CALL, - VM_MAX_BOP, + VM_IR_BRANCH_OPCODE_FALL, + VM_IR_BRANCH_OPCODE_JUMP, + VM_IR_BRANCH_OPCODE_BB, + VM_IR_BRANCH_OPCODE_BLT, + VM_IR_BRANCH_OPCODE_BLE, + VM_IR_BRANCH_OPCODE_BEQ, + VM_IR_BRANCH_OPCODE_RET, + VM_IR_BRANCH_OPCODE_LOAD, + VM_IR_BRANCH_OPCODE_GET, + VM_IR_BRANCH_OPCODE_CALL, }; enum { - VM_IOP_NOP, - VM_IOP_MOVE, - VM_IOP_ADD, - VM_IOP_SUB, - VM_IOP_MUL, - VM_IOP_DIV, - VM_IOP_IDIV, - VM_IOP_MOD, - VM_IOP_TABLE_SET, - VM_IOP_TABLE_NEW, - VM_IOP_TABLE_LEN, - VM_IOP_STD, - VM_MAX_IOP, + VM_IR_INSTR_OPCODE_NOP, + VM_IR_INSTR_OPCODE_MOVE, + VM_IR_INSTR_OPCODE_ADD, + VM_IR_INSTR_OPCODE_SUB, + VM_IR_INSTR_OPCODE_MUL, + VM_IR_INSTR_OPCODE_DIV, + VM_IR_INSTR_OPCODE_IDIV, + VM_IR_INSTR_OPCODE_MOD, + VM_IR_INSTR_OPCODE_TABLE_SET, + VM_IR_INSTR_OPCODE_TABLE_NEW, + VM_IR_INSTR_OPCODE_TABLE_LEN, + VM_IR_INSTR_OPCODE_STD, }; -struct vm_arg_t { +struct vm_ir_arg_t { union { vm_obj_t lit; - vm_block_t *func; + vm_ir_block_t *func; vm_error_t *error; uint64_t reg; @@ -68,36 +66,36 @@ struct vm_arg_t { uint8_t type; }; -struct vm_branch_t { +struct vm_ir_branch_t { vm_location_range_t range; - vm_block_t *targets[2]; + vm_ir_block_t *targets[2]; - vm_arg_t *args; - vm_arg_t out; + vm_ir_arg_t *args; + vm_ir_arg_t out; uint8_t op; }; -struct vm_instr_t { +struct vm_ir_instr_t { vm_location_range_t range; - vm_arg_t *args; - vm_arg_t out; + vm_ir_arg_t *args; + vm_ir_arg_t out; uint8_t op; }; -struct vm_block_t { +struct vm_ir_block_t { vm_location_range_t range; uint32_t id; uint32_t alloc; uint32_t len; - vm_instr_t *instrs; + vm_ir_instr_t *instrs; - vm_branch_t branch; + vm_ir_branch_t branch; void *code; @@ -106,23 +104,20 @@ struct vm_block_t { }; -struct vm_blocks_t { - vm_block_t *block; - vm_blocks_t *next; +struct vm_ir_blocks_t { + vm_ir_block_t *block; + vm_ir_blocks_t *next; }; -void vm_block_realloc(vm_block_t *block, vm_instr_t instr); +void vm_block_realloc(vm_ir_block_t *block, vm_ir_instr_t instr); -void vm_io_format_arg(vm_io_buffer_t *out, vm_arg_t val); -void vm_io_format_branch(vm_io_buffer_t *out, vm_branch_t val); -void vm_io_format_instr(vm_io_buffer_t *out, vm_instr_t val); -void vm_io_format_block(vm_io_buffer_t *out, vm_block_t *val); +void vm_io_format_arg(vm_io_buffer_t *out, vm_ir_arg_t val); +void vm_io_format_branch(vm_io_buffer_t *out, vm_ir_branch_t val); +void vm_io_format_instr(vm_io_buffer_t *out, vm_ir_instr_t val); +void vm_io_format_block(vm_io_buffer_t *out, vm_ir_block_t *val); -void vm_free_block_sub(vm_block_t *block); -void vm_free_block(vm_block_t *block); +#define vm_arg_nil() ((vm_ir_arg_t){.type = (VM_IR_ARG_TYPE_LIT), .lit = vm_obj_of_nil()}) -#define vm_arg_nil() ((vm_arg_t){.type = (VM_ARG_LIT), .lit = vm_obj_of_nil()}) - -vm_block_t *vm_compile(vm_t *vm, const char *src, const char *file); +vm_ir_block_t *vm_lang_lua_compile(vm_t *vm, const char *src, const char *file); #endif diff --git a/vm/lib.h b/vm/lib.h index 92386f34..0899fcb6 100644 --- a/vm/lib.h +++ b/vm/lib.h @@ -17,6 +17,21 @@ #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 diff --git a/vm/lua/ast.c b/vm/lua/ast.c index 11c31ecb..8759000e 100644 --- a/vm/lua/ast.c +++ b/vm/lua/ast.c @@ -624,10 +624,10 @@ vm_ast_node_t vm_lang_lua_parse(vm_t *vm, const char *str, const char *file) { return vm_ast_build_return(res); } -vm_block_t *vm_compile(vm_t *vm, const char *src, const char *file) { +vm_ir_block_t *vm_lang_lua_compile(vm_t *vm, const char *src, const char *file) { vm_ast_node_t ast = vm_lang_lua_parse(vm, src, file); // size_t len = vm->blocks->len; - vm_block_t *block = vm_ast_comp_more(vm, ast); + vm_ir_block_t *block = vm_ast_comp_more(vm, ast); vm_ast_free_node(ast); // if (vm->dump_ir) { // vm_io_buffer_t *buf = vm_io_buffer_new(); diff --git a/vm/lua/repl.c b/vm/lua/repl.c index 7e4742c3..6cffbde8 100644 --- a/vm/lua/repl.c +++ b/vm/lua/repl.c @@ -16,7 +16,7 @@ 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_repl_completer(ic_completion_env_t *cenv, const char *prefix) { +void vm_lang_lua_repl_completer(ic_completion_env_t *cenv, const char *prefix) { vm_t *vm = cenv->arg; ptrdiff_t len = strlen(prefix); ptrdiff_t head = len - 1; @@ -68,7 +68,7 @@ with_new_std:; ret:; } -void vm_repl_highlight_walk(ic_highlight_env_t *henv, size_t *depth, TSNode node) { +void vm_lang_lua_repl_highlight_walk(ic_highlight_env_t *henv, size_t *depth, TSNode node) { const char *type = ts_node_type(node); size_t start = ts_node_start_byte(node); size_t end = ts_node_end_byte(node); @@ -106,11 +106,11 @@ void vm_repl_highlight_walk(ic_highlight_env_t *henv, size_t *depth, TSNode node size_t num_children = ts_node_child_count(node); for (size_t i = 0; i < num_children; i++) { TSNode sub = ts_node_child(node, i); - vm_repl_highlight_walk(henv, depth, sub); + vm_lang_lua_repl_highlight_walk(henv, depth, sub); } } -void vm_repl_highlight(ic_highlight_env_t *henv, const char *input, void *arg) { +void vm_lang_lua_repl_highlight(ic_highlight_env_t *henv, const char *input, void *arg) { (void)arg; TSParser *parser = ts_parser_new(); ts_parser_set_language(parser, tree_sitter_lua()); @@ -122,20 +122,20 @@ void vm_repl_highlight(ic_highlight_env_t *henv, const char *input, void *arg) { ); TSNode root_node = ts_tree_root_node(tree); size_t depth = 0; - vm_repl_highlight_walk(henv, &depth, root_node); + vm_lang_lua_repl_highlight_walk(henv, &depth, root_node); ts_tree_delete(tree); ts_parser_delete(parser); } -void vm_repl(vm_t *vm) { +void vm_lang_lua_repl(vm_t *vm) { ic_set_history(".minivm-history", 2000); while (true) { char *input = ic_readline_ex( "lua", - vm_repl_completer, + vm_lang_lua_repl_completer, vm, - vm_repl_highlight, + vm_lang_lua_repl_highlight, vm ); @@ -145,7 +145,7 @@ void vm_repl(vm_t *vm) { ic_history_add(input); - vm_block_t *entry = vm_compile(vm, input, "__repl__"); + vm_ir_block_t *entry = vm_lang_lua_compile(vm, input, "__repl__"); free(input); diff --git a/vm/lua/repl.h b/vm/lua/repl.h index 4cd0741d..f0cccf4e 100644 --- a/vm/lua/repl.h +++ b/vm/lua/repl.h @@ -3,6 +3,6 @@ #include "../lib.h" -void vm_repl(vm_t *vm); +void vm_lang_lua_repl(vm_t *vm); #endif diff --git a/vm/std.c b/vm/std.c index 1a10bae7..cd2a510f 100644 --- a/vm/std.c +++ b/vm/std.c @@ -37,7 +37,7 @@ void vm_std_load(vm_t *vm, vm_obj_t *args) { return; } const char *str = vm_obj_get_string(args[0])->buf; - vm_block_t *entry = vm_compile(vm, str, "__load__"); + vm_ir_block_t *entry = vm_lang_lua_compile(vm, str, "__load__"); vm_closure_t *closure = vm_malloc(sizeof(vm_closure_t)); closure->block = entry; @@ -446,7 +446,7 @@ void vm_std_vm_import(vm_t *vm, vm_obj_t *args) { args[0] = vm_str(vm, "import() no such file"); return; } - vm_block_t *block = vm_compile(vm, src, vm_obj_get_string(args[0])->buf); + vm_ir_block_t *block = vm_lang_lua_compile(vm, src, vm_obj_get_string(args[0])->buf); args[0] = vm_run_repl(vm, block); vm_free(src); return; diff --git a/vm/vm.h b/vm/vm.h index df62faaa..a85a2eaf 100644 --- a/vm/vm.h +++ b/vm/vm.h @@ -10,9 +10,9 @@ #define VM_NREGS 256 -#define VM_GC_MIN (1 << 14) +#define VM_GC_MIN (1 << 8) // #define VM_GC_MIN 16 -#define VM_GC_FACTOR 1.5 +#define VM_GC_FACTOR 2 #define VM_DEBUG_BACKEND_BLOCKS 0 #define VM_DEBUG_BACKEND_OPCODES 0 @@ -27,8 +27,8 @@ #define VM_OBJ_FIELD_TAG _tag ## __COUNTER__ struct vm_t; -struct vm_block_t; -struct vm_blocks_t; +struct vm_ir_block_t; +struct vm_ir_blocks_t; struct vm_closure_t; struct vm_externs_t; union vm_value_t; @@ -39,7 +39,7 @@ struct vm_io_buffer_t; typedef struct vm_io_buffer_t vm_io_buffer_t; typedef struct vm_t vm_t; -typedef struct vm_blocks_t vm_blocks_t; +typedef struct vm_ir_blocks_t vm_ir_blocks_t; typedef struct vm_closure_t vm_closure_t; typedef struct vm_externs_t vm_externs_t; typedef union vm_value_t vm_value_t; @@ -48,7 +48,7 @@ typedef struct vm_table_pair_t vm_table_pair_t; #if VM_OBJ_FAST -#include "nanbox.h" +#include "../vendor/nanbox/nanbox.h" #define VM_EMPTY_BYTE NANBOX_EMPTY_BYTE @@ -83,7 +83,7 @@ typedef void vm_ffi_t(vm_t *closure, vm_obj_t *args); #define vm_obj_get_table(o) ((vm_table_t *) nanbox_to_aux(o)) #define vm_obj_get_closure(o) ((vm_closure_t *) nanbox_to_aux(o)) #define vm_obj_get_ffi(o) ((vm_ffi_t *) nanbox_to_aux(o)) -#define vm_obj_get_block(o) ((vm_block_t *) nanbox_to_aux(o)) +#define vm_obj_get_block(o) ((vm_ir_block_t *) nanbox_to_aux(o)) #define vm_obj_get_error(o) ((vm_error_t *) nanbox_to_pointer(o)) #else @@ -116,7 +116,7 @@ union vm_value_t { vm_io_buffer_t *str; vm_table_t *table; vm_closure_t *closure; - struct vm_block_t *fun; + struct vm_ir_block_t *fun; struct vm_error_t *error; }; @@ -141,7 +141,7 @@ struct vm_table_t { }; struct vm_closure_t { - struct vm_block_t *block; + struct vm_ir_block_t *block; bool mark: 1; uint32_t len: 31; vm_obj_t values[]; @@ -162,7 +162,7 @@ struct vm_io_buffer_t { struct vm_t { vm_externs_t *externs; - vm_blocks_t *blocks; + vm_ir_blocks_t *blocks; uint8_t use_num; @@ -181,7 +181,7 @@ struct vm_t { vm_t *vm_state_new(void); void vm_state_delete(vm_t *vm); -void vm_repl(vm_t *vm); +void vm_lang_lua_repl(vm_t *vm); vm_obj_t vm_str(vm_t *vm, const char *str); #if VM_OBJ_FAST From 230658ee94f81bcf133777d902c4f37a018fac85 Mon Sep 17 00:00:00 2001 From: Shaw Summa Date: Wed, 24 Jul 2024 04:29:36 -0400 Subject: [PATCH 2/2] more renames, once per function code check --- vm/backend/backend.c | 19 +++++++-- vm/gc.c | 18 ++++----- vm/gc.h | 2 +- vm/io.c | 4 +- vm/io.h | 2 +- vm/lua/repl.c | 2 +- vm/obj.c | 34 +++++++--------- vm/obj.h | 12 +++--- vm/std.c | 34 ++++++++-------- vm/std.h | 2 +- vm/vm.h | 96 ++++---------------------------------------- 11 files changed, 75 insertions(+), 150 deletions(-) diff --git a/vm/backend/backend.c b/vm/backend/backend.c index eea3bb5a..134b7bfa 100644 --- a/vm/backend/backend.c +++ b/vm/backend/backend.c @@ -693,6 +693,18 @@ 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)) +void vm_interp_renumber_blocks(vm_t *vm, void **ptrs, vm_ir_block_t *block) { + if (block->code == NULL) { + block->code = vm_interp_renumber_block(vm, &ptrs[0], block); + if (block->branch.targets[0] != NULL) { + vm_interp_renumber_blocks(vm, ptrs, block->branch.targets[0]); + } + if (block->branch.targets[1] != NULL) { + vm_interp_renumber_blocks(vm, ptrs, block->branch.targets[1]); + } + } +} + 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] = { @@ -736,6 +748,9 @@ vm_obj_t vm_run_repl_inner(vm_t *vm, vm_ir_block_t *block) { [VM_OP_GET] = &&VM_OP_GET, [VM_OP_CALL] = &&VM_OP_CALL, }; + + vm_interp_renumber_blocks(vm, ptrs, block); + vm_obj_t *next_regs = ®s[VM_NREGS]; #if VM_DEBUG_BACKEND_BLOCKS { @@ -759,10 +774,6 @@ new_block:; new_block_no_print:; uint8_t *code = block->code; - if (block->code == NULL) { - code = vm_interp_renumber_block(vm, &ptrs[0], block); - block->code = code; - } goto *vm_run_repl_read(void *); diff --git a/vm/gc.c b/vm/gc.c index 33cd997f..bd348d7e 100644 --- a/vm/gc.c +++ b/vm/gc.c @@ -19,7 +19,7 @@ struct vm_gc_objs_t { struct vm_gc_table_cache_t { size_t alloc; size_t len; - vm_table_t **tables; + vm_obj_table_t **tables; }; struct vm_gc_t { @@ -59,7 +59,7 @@ static void vm_gc_mark_obj(vm_obj_t obj) { vm_io_buffer_t *buffer = vm_obj_get_string(obj); buffer->mark = true; } else if (vm_obj_is_closure(obj)) { - vm_closure_t *closure = vm_obj_get_closure(obj); + vm_obj_closure_t *closure = vm_obj_get_closure(obj); if (!closure->mark) { closure->mark = true; for (size_t i = 0; i < closure->len; i++) { @@ -68,7 +68,7 @@ static void vm_gc_mark_obj(vm_obj_t obj) { } vm_gc_mark_block(closure->block); } else if (vm_obj_is_table(obj)) { - vm_table_t *table = vm_obj_get_table(obj); + vm_obj_table_t *table = vm_obj_get_table(obj); if (!table->mark) { table->mark = true; for (size_t i = 0; i < (1 << table->alloc); i++) { @@ -106,7 +106,7 @@ void vm_gc_sweep(vm_t *vm) { gc->objs.objs[write++] = obj; } } else if (vm_obj_is_closure(obj)) { - vm_closure_t *closure = vm_obj_get_closure(obj); + vm_obj_closure_t *closure = vm_obj_get_closure(obj); if (!closure->mark) { vm_free(closure); } else { @@ -114,7 +114,7 @@ void vm_gc_sweep(vm_t *vm) { gc->objs.objs[write++] = obj; } } else if (vm_obj_is_table(obj)) { - vm_table_t *table = vm_obj_get_table(obj); + vm_obj_table_t *table = vm_obj_get_table(obj); if (!table->mark) { if (!table->pairs_auto) { vm_free(table->pairs); @@ -148,11 +148,9 @@ void vm_gc_sweep(vm_t *vm) { } } -void vm_table_init_size(vm_table_t *ret, size_t pow2); - -vm_table_t *vm_table_new_size(vm_t *vm, size_t pow2) { +vm_obj_table_t *vm_table_new_size(vm_t *vm, size_t pow2) { vm_gc_t *gc = vm->gc; - vm_table_t *ret = vm_malloc(sizeof(vm_table_t) + sizeof(vm_table_pair_t) * (1 << pow2)); + vm_obj_table_t *ret = vm_malloc(sizeof(vm_obj_table_t) + sizeof(vm_table_pair_t) * (1 << pow2)); ret->pairs = (vm_table_pair_t *)&ret[1]; memset(ret->pairs, VM_EMPTY_BYTE, sizeof(vm_table_pair_t) * (1 << pow2)); ret->alloc = pow2; @@ -164,7 +162,7 @@ vm_table_t *vm_table_new_size(vm_t *vm, size_t pow2) { return ret; } -vm_table_t *vm_table_new(vm_t *vm) { +vm_obj_table_t *vm_table_new(vm_t *vm) { return vm_table_new_size(vm, 2); } diff --git a/vm/gc.h b/vm/gc.h index 8e574014..f72a4d32 100644 --- a/vm/gc.h +++ b/vm/gc.h @@ -12,6 +12,6 @@ void vm_gc_deinit(vm_t *vm); void vm_gc_add(vm_t *vm, vm_obj_t obj); -vm_table_t *vm_table_new_size(vm_t *vm, size_t pow2); +vm_obj_table_t *vm_table_new_size(vm_t *vm, size_t pow2); #endif diff --git a/vm/io.c b/vm/io.c index 8aaaa122..73f6d6e5 100644 --- a/vm/io.c +++ b/vm/io.c @@ -154,7 +154,7 @@ void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_obj_ vm_io_buffer_format(out, "\n", vm_obj_get_block(value)); } if (vm_obj_is_table(value)) { - vm_table_t *tab = vm_obj_get_table(value); + vm_obj_table_t *tab = vm_obj_get_table(value); if (tab == NULL) { vm_io_indent(out, indent, prefix); vm_io_buffer_format(out, "table(NULL)\n"); @@ -201,7 +201,7 @@ void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_obj_ } } -void vm_value_buffer_tostring(vm_io_buffer_t *buf, vm_obj_t value) { +void vm_obj_buffer_tostring(vm_io_buffer_t *buf, vm_obj_t value) { if (vm_obj_is_nil(value)) { vm_io_buffer_format(buf, "nil"); } diff --git a/vm/io.h b/vm/io.h index 2d74ba00..7224f647 100644 --- a/vm/io.h +++ b/vm/io.h @@ -21,7 +21,7 @@ void vm_io_buffer_vformat(vm_io_buffer_t *buf, const char *fmt, va_list ap); void vm_io_buffer_format(vm_io_buffer_t *buf, const char *fmt, ...); char *vm_io_vformat(const char *fmt, va_list ap); char *vm_io_format(const char *fmt, ...); -void vm_value_buffer_tostring(vm_io_buffer_t *buf, vm_obj_t value); +void vm_obj_buffer_tostring(vm_io_buffer_t *buf, vm_obj_t value); vm_io_buffer_t *vm_io_buffer_from_str(const char *str); #endif diff --git a/vm/lua/repl.c b/vm/lua/repl.c index 6cffbde8..194245eb 100644 --- a/vm/lua/repl.c +++ b/vm/lua/repl.c @@ -25,7 +25,7 @@ void vm_lang_lua_repl_completer(ic_completion_env_t *cenv, const char *prefix) { } head += 1; const char *last_word = &prefix[head]; - vm_table_t *std = vm_obj_get_table(vm->std); + vm_obj_table_t *std = vm_obj_get_table(vm->std); with_new_std:; for (size_t i = 0; i < ((size_t)1 << std->alloc); i++) { vm_table_pair_t *pair = &std->pairs[i]; diff --git a/vm/obj.c b/vm/obj.c index c6d269e3..1e1152d8 100644 --- a/vm/obj.c +++ b/vm/obj.c @@ -22,7 +22,7 @@ bool vm_obj_eq(vm_obj_t v1, vm_obj_t v2) { } } -size_t vm_value_hash(vm_obj_t value) { +size_t vm_obj_hash(vm_obj_t value) { if (vm_obj_is_nil(value)) { return 0; } @@ -59,10 +59,10 @@ size_t vm_value_hash(vm_obj_t value) { // return 0; } -vm_table_pair_t *vm_table_lookup(vm_table_t *table, vm_obj_t key) { +vm_table_pair_t *vm_table_lookup(vm_obj_table_t *table, vm_obj_t key) { size_t len = 1 << table->alloc; size_t and = len - 1; - size_t stop = vm_value_hash(key) & and; + size_t stop = vm_obj_hash(key) & and; size_t next = stop; do { vm_table_pair_t *pair = &table->pairs[next]; @@ -79,23 +79,13 @@ vm_table_pair_t *vm_table_lookup(vm_table_t *table, vm_obj_t key) { return NULL; } -void vm_table_init_size(vm_table_t *ret, size_t pow2) { - ret->pairs = vm_malloc(sizeof(vm_table_pair_t) * (1 << pow2)); - memset(ret->pairs, VM_EMPTY_BYTE, sizeof(vm_table_pair_t) * (1 << pow2)); - ret->alloc = pow2; - ret->used = 0; - ret->len = 0; - ret->mark = false; - ret->pairs_auto = false; -} - -void vm_table_set(vm_table_t *restrict table, vm_obj_t key, vm_obj_t value) { +void vm_table_set(vm_obj_table_t *restrict table, vm_obj_t key, vm_obj_t value) { if (table->alloc == 0) { return; } size_t len = 1 << table->alloc; size_t and = len - 1; - size_t stop = vm_value_hash(key) & and; + size_t stop = vm_obj_hash(key) & and; size_t next = stop & and; do { vm_table_pair_t *pair = &table->pairs[next]; @@ -140,8 +130,14 @@ void vm_table_set(vm_table_t *restrict table, vm_obj_t key, vm_obj_t value) { } } } else if ((table->used + 1) * 100 >= ((uint32_t)1 << table->alloc) * 76) { - vm_table_t ret; - vm_table_init_size(&ret, table->alloc + 1); + vm_obj_table_t ret; + ret.pairs = vm_malloc(sizeof(vm_table_pair_t) * (2 << table->alloc)); + memset(ret.pairs, VM_EMPTY_BYTE, sizeof(vm_table_pair_t) * (2 << table->alloc)); + ret.alloc = table->alloc + 1; + ret.used = 0; + ret.len = 0; + ret.mark = false; + ret.pairs_auto = false; for (size_t i = 0; i < len; i++) { vm_table_pair_t *in_pair = &table->pairs[i]; if (!vm_obj_is_empty(in_pair->key) && !vm_obj_is_nil(in_pair->key)) { @@ -173,11 +169,11 @@ void vm_table_set(vm_table_t *restrict table, vm_obj_t key, vm_obj_t value) { } } -void vm_table_set_pair(vm_table_t *table, vm_table_pair_t *pair) { +void vm_table_set_pair(vm_obj_table_t *table, vm_table_pair_t *pair) { vm_table_set(table, pair->key, pair->value); } -void vm_table_get_pair(vm_table_t *table, vm_table_pair_t *out) { +void vm_table_get_pair(vm_obj_table_t *table, vm_table_pair_t *out) { vm_table_pair_t *pair = vm_table_lookup(table, out->key); if (pair != NULL) { out->value = pair->value; diff --git a/vm/obj.h b/vm/obj.h index c3f95ab2..fb6b5aea 100644 --- a/vm/obj.h +++ b/vm/obj.h @@ -7,12 +7,12 @@ struct vm_table_pair_t; typedef struct vm_table_pair_t vm_table_pair_t; bool vm_obj_eq(vm_obj_t lhs, vm_obj_t rhs); -bool vm_value_is_int(vm_obj_t val); +bool vm_obj_is_int(vm_obj_t val); -vm_table_t *vm_table_new(vm_t *vm); -vm_table_pair_t *vm_table_lookup(vm_table_t *table, vm_obj_t key); -void vm_table_set(vm_table_t *table, vm_obj_t key, vm_obj_t value); -void vm_table_set_pair(vm_table_t *table, vm_table_pair_t *pair); -void vm_table_get_pair(vm_table_t *table, vm_table_pair_t *pair); +vm_obj_table_t *vm_table_new(vm_t *vm); +vm_table_pair_t *vm_table_lookup(vm_obj_table_t *table, vm_obj_t key); +void vm_table_set(vm_obj_table_t *table, vm_obj_t key, vm_obj_t value); +void vm_table_set_pair(vm_obj_table_t *table, vm_table_pair_t *pair); +void vm_table_get_pair(vm_obj_table_t *table, vm_table_pair_t *pair); #endif diff --git a/vm/std.c b/vm/std.c index cd2a510f..7e66f912 100644 --- a/vm/std.c +++ b/vm/std.c @@ -39,7 +39,7 @@ void vm_std_load(vm_t *vm, vm_obj_t *args) { const char *str = vm_obj_get_string(args[0])->buf; vm_ir_block_t *entry = vm_lang_lua_compile(vm, str, "__load__"); - vm_closure_t *closure = vm_malloc(sizeof(vm_closure_t)); + vm_obj_closure_t *closure = vm_malloc(sizeof(vm_obj_closure_t)); closure->block = entry; closure->len = 0; vm_obj_t ret = vm_obj_of_closure(closure); @@ -86,7 +86,7 @@ void vm_std_vm_closure(vm_t *vm, vm_obj_t *args) { *args = vm_obj_of_nil(); return; } - vm_closure_t *closure = vm_malloc(sizeof(vm_closure_t) + sizeof(vm_obj_t) * (nargs - 1)); + vm_obj_closure_t *closure = vm_malloc(sizeof(vm_obj_closure_t) + sizeof(vm_obj_t) * (nargs - 1)); closure->block = vm_obj_get_block(args[0]); closure->len = nargs - 1; for (size_t i = 1; !vm_obj_is_empty(args[i]); i++) { @@ -162,7 +162,7 @@ void vm_std_type(vm_t *vm, vm_obj_t *args) { void vm_std_tostring(vm_t *vm, vm_obj_t *args) { (void)vm; vm_io_buffer_t out = {0}; - vm_value_buffer_tostring(&out, *args); + vm_obj_buffer_tostring(&out, *args); *args = vm_str(vm, out.buf); vm_free(out.buf); } @@ -190,7 +190,7 @@ void vm_std_print(vm_t *vm, vm_obj_t *args) { if (!first) { vm_io_buffer_format(&out, "\t"); } - vm_value_buffer_tostring(&out, *args++); + vm_obj_buffer_tostring(&out, *args++); first = false; } fprintf(stdout, "%.*s\n", (int)out.len, out.buf); @@ -204,7 +204,7 @@ void vm_std_io_write(vm_t *vm, vm_obj_t *args) { vm_obj_t *ret = args; vm_io_buffer_t out = {0}; while (!vm_obj_is_empty(args[0])) { - vm_value_buffer_tostring(&out, *args++); + vm_obj_buffer_tostring(&out, *args++); } fprintf(stdout, "%.*s", (int)out.len, out.buf); *ret = vm_obj_of_nil(); @@ -378,7 +378,7 @@ void vm_std_string_format(vm_t *vm, vm_obj_t *args) { } void vm_std_set_arg(vm_t *vm, const char *prog, const char *file, int argc, char **argv) { - vm_table_t *arg = vm_table_new(vm); + vm_obj_table_t *arg = vm_table_new(vm); if (prog != NULL) { vm_table_set(arg, vm_obj_of_number(-1), vm_str(vm, prog)); } @@ -397,8 +397,8 @@ void vm_std_table_keys(vm_t *vm, vm_obj_t *args) { *args = vm_obj_of_error(vm_error_from_msg(VM_LOCATION_RANGE_FUNC, "table.values: expect a table")); return; } - vm_table_t *ret = vm_table_new(vm); - vm_table_t *tab = vm_obj_get_table(args[0]); + vm_obj_table_t *ret = vm_table_new(vm); + vm_obj_table_t *tab = vm_obj_get_table(args[0]); size_t len = 1 << tab->alloc; size_t write_head = 1; for (size_t i = 0; i < len; i++) { @@ -418,8 +418,8 @@ void vm_std_table_values(vm_t *vm, vm_obj_t *args) { *args = vm_obj_of_error(vm_error_from_msg(VM_LOCATION_RANGE_FUNC, "table.values: expect a table")); return; } - vm_table_t *ret = vm_table_new(vm); - vm_table_t *tab = vm_obj_get_table(args[0]); + vm_obj_table_t *ret = vm_table_new(vm); + vm_obj_table_t *tab = vm_obj_get_table(args[0]); size_t len = 1 << tab->alloc; size_t write_head = 1; for (size_t i = 0; i < len; i++) { @@ -453,24 +453,24 @@ void vm_std_vm_import(vm_t *vm, vm_obj_t *args) { } void vm_std_new(vm_t *vm) { - vm_table_t *std = vm_table_new(vm); + vm_obj_table_t *std = vm_table_new(vm); srand(0); { - vm_table_t *io = vm_table_new(vm); + vm_obj_table_t *io = vm_table_new(vm); vm_table_set(std, vm_str(vm, "io"), vm_obj_of_table(io)); vm_table_set(io, vm_str(vm, "write"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_io_write))); } { - vm_table_t *string = vm_table_new(vm); + vm_obj_table_t *string = vm_table_new(vm); vm_table_set(std, vm_str(vm, "string"), vm_obj_of_table(string)); vm_table_set(string, vm_str(vm, "format"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_string_format))); } { - vm_table_t *tvm = vm_table_new(vm); + vm_obj_table_t *tvm = vm_table_new(vm); vm_table_set(std, vm_str(vm, "vm"), vm_obj_of_table(tvm)); vm_table_set(tvm, vm_str(vm, "import"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_vm_import))); vm_table_set(tvm, vm_str(vm, "gc"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_vm_gc))); @@ -480,19 +480,19 @@ void vm_std_new(vm_t *vm) { } { - vm_table_t *math = vm_table_new(vm); + vm_obj_table_t *math = vm_table_new(vm); vm_table_set(std, vm_str(vm, "math"), vm_obj_of_table(math)); vm_table_set(math, vm_str(vm, "randint"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_math_rand_int))); } { - vm_table_t *os = vm_table_new(vm); + vm_obj_table_t *os = vm_table_new(vm); vm_table_set(std, vm_str(vm, "os"), vm_obj_of_table(os)); vm_table_set(os, vm_str(vm, "exit"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_os_exit))); } { - vm_table_t *table = vm_table_new(vm); + vm_obj_table_t *table = vm_table_new(vm); vm_table_set(std, vm_str(vm, "table"), vm_obj_of_table(table)); vm_table_set(table, vm_str(vm, "keys"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_table_keys))); vm_table_set(table, vm_str(vm, "values"), vm_obj_of_ffi(VM_STD_REF(vm, vm_std_table_values))); diff --git a/vm/std.h b/vm/std.h index 95f08d21..a814524f 100644 --- a/vm/std.h +++ b/vm/std.h @@ -5,7 +5,7 @@ #define VM_STD_REF(vm, x) (vm_config_add_extern((vm), &(x)), &(x)) -void vm_value_buffer_tostring(vm_io_buffer_t *buf, vm_obj_t value); +void vm_obj_buffer_tostring(vm_io_buffer_t *buf, vm_obj_t value); void vm_std_set_arg(vm_t *vm, const char *prog, const char *file, int argc, char **argv); void vm_std_new(vm_t *vm); diff --git a/vm/vm.h b/vm/vm.h index a85a2eaf..7f3c4923 100644 --- a/vm/vm.h +++ b/vm/vm.h @@ -29,10 +29,9 @@ struct vm_t; struct vm_ir_block_t; struct vm_ir_blocks_t; -struct vm_closure_t; +struct vm_obj_closure_t; struct vm_externs_t; -union vm_value_t; -struct vm_table_t; +struct vm_obj_table_t; struct vm_table_pair_t; struct vm_table_pair_t; struct vm_io_buffer_t; @@ -40,14 +39,11 @@ struct vm_io_buffer_t; typedef struct vm_io_buffer_t vm_io_buffer_t; typedef struct vm_t vm_t; typedef struct vm_ir_blocks_t vm_ir_blocks_t; -typedef struct vm_closure_t vm_closure_t; +typedef struct vm_obj_closure_t vm_obj_closure_t; typedef struct vm_externs_t vm_externs_t; -typedef union vm_value_t vm_value_t; -typedef struct vm_table_t vm_table_t; +typedef struct vm_obj_table_t vm_obj_table_t; typedef struct vm_table_pair_t vm_table_pair_t; -#if VM_OBJ_FAST - #include "../vendor/nanbox/nanbox.h" #define VM_EMPTY_BYTE NANBOX_EMPTY_BYTE @@ -80,58 +76,18 @@ typedef void vm_ffi_t(vm_t *closure, vm_obj_t *args); #define vm_obj_get_boolean(o) nanbox_to_boolean(o) #define vm_obj_get_number(o) nanbox_to_double(o) #define vm_obj_get_string(o) ((vm_io_buffer_t *) nanbox_to_aux(o)) -#define vm_obj_get_table(o) ((vm_table_t *) nanbox_to_aux(o)) -#define vm_obj_get_closure(o) ((vm_closure_t *) nanbox_to_aux(o)) +#define vm_obj_get_table(o) ((vm_obj_table_t *) nanbox_to_aux(o)) +#define vm_obj_get_closure(o) ((vm_obj_closure_t *) nanbox_to_aux(o)) #define vm_obj_get_ffi(o) ((vm_ffi_t *) nanbox_to_aux(o)) #define vm_obj_get_block(o) ((vm_ir_block_t *) nanbox_to_aux(o)) #define vm_obj_get_error(o) ((vm_error_t *) nanbox_to_pointer(o)) -#else - -struct vm_obj_t; -typedef struct vm_obj_t vm_obj_t; - -enum { - VM_TAG_UNK, - VM_TAG_NIL, - VM_TAG_BOOL, - VM_TAG_NUMBER, - VM_TAG_STR, - VM_TAG_CLOSURE, - VM_TAG_FUN, - VM_TAG_TAB, - VM_TAG_FFI, - VM_TAG_ERROR, - VM_TAG_MAX, -}; - -typedef uint8_t vm_tag_t; - -typedef void vm_ffi_t(vm_t *closure, vm_obj_t *args); - -union vm_value_t { - bool boolean; - double f64; - vm_ffi_t *ffi; - vm_io_buffer_t *str; - vm_table_t *table; - vm_closure_t *closure; - struct vm_ir_block_t *fun; - struct vm_error_t *error; -}; - -struct vm_obj_t { - vm_value_t VM_OBJ_FIELD_VALUE; - vm_tag_t VM_OBJ_FIELD_TAG; -}; -#endif - struct vm_table_pair_t { vm_obj_t key; vm_obj_t value; }; -struct vm_table_t { +struct vm_obj_table_t { vm_table_pair_t *pairs; uint32_t len; uint32_t used; @@ -140,7 +96,7 @@ struct vm_table_t { bool pairs_auto: 1; }; -struct vm_closure_t { +struct vm_obj_closure_t { struct vm_ir_block_t *block; bool mark: 1; uint32_t len: 31; @@ -184,40 +140,4 @@ void vm_state_delete(vm_t *vm); void vm_lang_lua_repl(vm_t *vm); vm_obj_t vm_str(vm_t *vm, const char *str); -#if VM_OBJ_FAST -#else -#define VM_EMPTY_BYTE 0x00 - -#define vm_obj_of_empty() ((vm_obj_t) {.VM_OBJ_FIELD_TAG = VM_TAG_UNK}) -#define vm_obj_of_nil() ((vm_obj_t) {.VM_OBJ_FIELD_TAG = VM_TAG_NIL}) -#define vm_obj_of_boolean(b) ((vm_obj_t) {.VM_OBJ_FIELD_TAG = VM_TAG_BOOL, .VM_OBJ_FIELD_VALUE.boolean = (b)}) -#define vm_obj_of_number(n) ((vm_obj_t) {.VM_OBJ_FIELD_TAG = VM_TAG_NUMBER, .VM_OBJ_FIELD_VALUE.f64 = (n)}) -#define vm_obj_of_string(o) ((vm_obj_t) {.VM_OBJ_FIELD_TAG = VM_TAG_STR, .VM_OBJ_FIELD_VALUE.str = (o)}) -#define vm_obj_of_table(o) ((vm_obj_t) {.VM_OBJ_FIELD_TAG = VM_TAG_TAB, .VM_OBJ_FIELD_VALUE.table = (o)}) -#define vm_obj_of_closure(o) ((vm_obj_t) {.VM_OBJ_FIELD_TAG = VM_TAG_CLOSURE, .VM_OBJ_FIELD_VALUE.closure = (o)}) -#define vm_obj_of_ffi(o) ((vm_obj_t) {.VM_OBJ_FIELD_TAG = VM_TAG_FFI, .VM_OBJ_FIELD_VALUE.ffi = (o)}) -#define vm_obj_of_block(o) ((vm_obj_t) {.VM_OBJ_FIELD_TAG = VM_TAG_FUN, .VM_OBJ_FIELD_VALUE.fun = (o)}) -#define vm_obj_of_error(o) ((vm_obj_t) {.VM_OBJ_FIELD_TAG = VM_TAG_FUN, .VM_OBJ_FIELD_VALUE.error = (o)}) - -#define vm_obj_get_boolean(o) ((o).VM_OBJ_FIELD_VALUE.boolean) -#define vm_obj_get_number(o) ((o).VM_OBJ_FIELD_VALUE.f64) -#define vm_obj_get_string(o) ((o).VM_OBJ_FIELD_VALUE.str) -#define vm_obj_get_table(o) ((o).VM_OBJ_FIELD_VALUE.table) -#define vm_obj_get_closure(o) ((o).VM_OBJ_FIELD_VALUE.closure) -#define vm_obj_get_ffi(o) ((o).VM_OBJ_FIELD_VALUE.ffi) -#define vm_obj_get_block(o) ((o).VM_OBJ_FIELD_VALUE.fun) -#define vm_obj_get_error(o) ((o).VM_OBJ_FIELD_VALUE.error) - -#define vm_obj_is_empty(o) ((o).VM_OBJ_FIELD_TAG == VM_TAG_UNK) -#define vm_obj_is_nil(o) ((o).VM_OBJ_FIELD_TAG == VM_TAG_NIL) -#define vm_obj_is_boolean(o) ((o).VM_OBJ_FIELD_TAG == VM_TAG_BOOL) -#define vm_obj_is_number(o) ((o).VM_OBJ_FIELD_TAG == VM_TAG_NUMBER) -#define vm_obj_is_string(o) ((o).VM_OBJ_FIELD_TAG == VM_TAG_STR) -#define vm_obj_is_table(o) ((o).VM_OBJ_FIELD_TAG == VM_TAG_TAB) -#define vm_obj_is_closure(o) ((o).VM_OBJ_FIELD_TAG == VM_TAG_CLOSURE) -#define vm_obj_is_ffi(o) ((o).VM_OBJ_FIELD_TAG == VM_TAG_FFI) -#define vm_obj_is_block(o) ((o).VM_OBJ_FIELD_TAG == VM_TAG_FUN) -#define vm_obj_is_error(o) ((o).VM_OBJ_FIELD_TAG == VM_TAG_ERROR) -#endif - #endif