diff --git a/main/minivm.c b/main/minivm.c index a5689ced..0609e6f5 100644 --- a/main/minivm.c +++ b/main/minivm.c @@ -18,7 +18,7 @@ int main(int argc, char **argv) { vm_t *vm = vm_malloc(sizeof(vm_t)); *vm = (vm_t) { .use_num = VM_USE_NUM_F64, - .regs = vm_malloc(sizeof(vm_std_value_t) * 65536), + .regs = vm_malloc(sizeof(vm_obj_t) * 65536), }; vm_std_new(vm); @@ -38,6 +38,10 @@ int main(int argc, char **argv) { echo = true; } else if (!strcmp(arg, "--no-echo")) { echo = false; + } else if (!strcmp(arg, "--dump-ir")) { + vm->dump_ir = true; + } else if (!strcmp(arg, "--no-dump-ir")) { + vm->dump_ir = false; } else if (!strcmp(arg, "--repl")) { vm_repl(vm); isrepl = false; @@ -111,7 +115,7 @@ int main(int argc, char **argv) { vm_block_t *entry = vm_compile(vm, src); - vm_std_value_t value = vm_run_main(vm, entry); + vm_obj_t value = vm_run_main(vm, entry); if (value.tag == VM_TAG_ERROR) { return 1; } diff --git a/makefile b/makefile index 936a8a2c..30222d55 100644 --- a/makefile +++ b/makefile @@ -20,8 +20,8 @@ mac: .dummy WINDOWS_RAYLIB_OBJS = $(RAYLIB_DIR)/rcore.o $(RAYLIB_DIR)/rglfw.o $(RAYLIB_DIR)/rshapes.o $(RAYLIB_DIR)/rtextures.o $(RAYLIB_DIR)/rtext.o $(RAYLIB_DIR)/rmodels.o $(RAYLIB_DIR)/raudio.o $(RAYLIB_DIR)/utils.o windows: .dummy - $(PRE) make -Bj$(J) -C vendor/raylib/src CC="$(RAYLIB_CC)" LDFLAGS="$(OPT)" CFLAGS="-w -DPLATFORM_DESKTOP -DDWIN32_LEAN_AND_MEAN $(OPT) $(CLFAGS)" PLATFORM=PLATFORM_DESKTOP OS=WINDOWS_NT - $(PRE) make -Bj$(J) -f tool/core.mak $(TARGET) OS=WINDOWS CC="$(CC)" EXE=.exe TEST_LUA="$(TEST_LUA)" CFLAGS="-DVM_USE_RAYLIB -DVM_NO_GC -DWIN32_LEAN_AND_MEAN $(OPT) $(CFLAGS)" LDFLAGS="$(RAYLIB_DIR)/libraylib.a -lopengl32 -lgdi32 $(OPT) $(LDFLAGS)" + $(PRE) make -Bj$(J) -C vendor/raylib/src CC="$(RAYLIB_CC)" LDFLAGS="$(OPT)" CFLAGS="-w -DPLATFORM_DESKTOP -DWIN32_LEAN_AND_MEAN $(OPT) $(CLFAGS)" PLATFORM=PLATFORM_DESKTOP OS=WINDOWS_NT + $(PRE) make -Bj$(J) -f tool/core.mak $(TARGET) OS=WINDOWS CC="$(CC)" EXE=.exe TEST_LUA="$(TEST_LUA)" CFLAGS="-DVM_USE_RAYLIB -DVM_NO_GC -DWIN32_LEAN_AND_MEAN $(OPT) $(CFLAGS)" LDFLAGS="$(RAYLIB_DIR)/libraylib.a -lopengl32 -lgdi32 -lwinmm $(OPT) $(LDFLAGS)" freebsd: .dummy $(PRE) gmake -Bj$(J) -C vendor/raylib/src CC="$(RAYLIB_CC)" LDFLAGS="$(OPT)" CFLAGS="-w $(OPT) $(CLFAGS) -DPLATFORM_DESKTOP" PLATFORM=PLATFORM_DESKTOP diff --git a/vm/ast/ast.c b/vm/ast/ast.c index 0d99bebf..d71088d9 100644 --- a/vm/ast/ast.c +++ b/vm/ast/ast.c @@ -11,7 +11,7 @@ void vm_ast_free_ident(const char *ident) { vm_free(ident); } -void vm_ast_free_literal(vm_std_value_t literal) { +void vm_ast_free_literal(vm_obj_t literal) { switch (literal.tag) { case VM_TAG_STR: { vm_free(literal.value.str); diff --git a/vm/ast/ast.h b/vm/ast/ast.h index f8039329..5bd70357 100644 --- a/vm/ast/ast.h +++ b/vm/ast/ast.h @@ -77,7 +77,7 @@ struct vm_ast_form_t { union vm_ast_node_value_t { vm_ast_form_t form; const char *ident; - vm_std_value_t literal; + vm_obj_t literal; }; struct vm_ast_node_t { @@ -88,7 +88,7 @@ struct vm_ast_node_t { const char *vm_ast_format(vm_ast_node_t *node); void vm_ast_free_form(vm_ast_form_t node); void vm_ast_free_ident(const char *node); -void vm_ast_free_literal(vm_std_value_t node); +void vm_ast_free_literal(vm_obj_t node); void vm_ast_free_node(vm_ast_node_t node); #endif \ No newline at end of file diff --git a/vm/ast/build.c b/vm/ast/build.c index 8628ead4..c66f96fe 100644 --- a/vm/ast/build.c +++ b/vm/ast/build.c @@ -242,7 +242,7 @@ vm_ast_node_t vm_ast_build_block(size_t len, ...) { vm_ast_node_t vm_ast_build_error(const char *str) { return (vm_ast_node_t){ .type = VM_AST_NODE_LITERAL, - .value.literal = (vm_std_value_t){ + .value.literal = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = str, }, @@ -252,7 +252,7 @@ vm_ast_node_t vm_ast_build_error(const char *str) { vm_ast_node_t vm_ast_build_nil(void) { return (vm_ast_node_t){ .type = VM_AST_NODE_LITERAL, - .value.literal = (vm_std_value_t){ + .value.literal = (vm_obj_t){ .tag = VM_TAG_NIL, }, }; diff --git a/vm/ast/build.h b/vm/ast/build.h index 70c94a51..1fb8eca2 100644 --- a/vm/ast/build.h +++ b/vm/ast/build.h @@ -69,7 +69,7 @@ vm_ast_node_t vm_ast_build_nil(void); #define vm_ast_build_literal(TYPE_, VALUE_) \ ((vm_ast_node_t){ \ .type = VM_AST_NODE_LITERAL, \ - .value.literal = VM_STD_VALUE_LITERAL(TYPE_, VALUE_), \ + .value.literal = VM_OBJ_LITERAL(TYPE_, VALUE_), \ }) #define vm_ast_build_ident(STR_) \ diff --git a/vm/ast/comp.c b/vm/ast/comp.c index b23d781f..2296ccca 100644 --- a/vm/ast/comp.c +++ b/vm/ast/comp.c @@ -42,50 +42,50 @@ struct vm_ast_comp_names_t { vm_ast_comp_names_t *next; }; -void vm_lua_comp_op_std_pow(vm_t *vm, vm_std_value_t *args) { - vm_std_value_t *ret = args; +void vm_lua_comp_op_std_pow(vm_t *vm, vm_obj_t *args) { + vm_obj_t *ret = args; double v = vm_value_to_f64(*args++); while (args->tag != VM_TAG_UNK) { v = pow(v, vm_value_to_f64(*args++)); } switch (vm->use_num) { case VM_USE_NUM_I8: { - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_I8, .value.i8 = (int8_t)v, }; return; } case VM_USE_NUM_I16: { - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_I16, .value.i16 = (int16_t)v, }; return; } case VM_USE_NUM_I32: { - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_I32, .value.i32 = (int32_t)v, }; return; } case VM_USE_NUM_I64: { - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_I64, .value.i64 = (int64_t)v, }; return; } case VM_USE_NUM_F32: { - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_F32, .value.f32 = (float)v, }; return; } case VM_USE_NUM_F64: { - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_F64, .value.f64 = (double)v, }; @@ -123,8 +123,8 @@ static void vm_ast_names_free(vm_ast_comp_names_t *names) { static vm_arg_t vm_ast_comp_to(vm_ast_comp_t *comp, vm_ast_node_t node); static void vm_ast_comp_br(vm_ast_comp_t *comp, vm_ast_node_t node, vm_block_t *iftrue, vm_block_t *iffalse); -extern void vm_std_vm_closure(vm_t *vm, vm_std_value_t *args); -extern void vm_std_vm_concat(vm_t *vm, vm_std_value_t *args); +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, ...) { va_list ap; @@ -234,7 +234,7 @@ static vm_arg_t vm_ast_comp_get_var(vm_ast_comp_t *comp, const char *name) { vm_block_t *next = vm_ast_comp_new_block(comp); vm_arg_t slot = (vm_arg_t){ .type = VM_ARG_LIT, - .lit = (vm_std_value_t){ + .lit = (vm_obj_t){ .tag = VM_TAG_I32, .value.i32 = slotnum, }}; @@ -362,7 +362,7 @@ static void vm_ast_comp_br(vm_ast_comp_t *comp, vm_ast_node_t node, vm_block_t * break; } case VM_AST_NODE_LITERAL: { - vm_std_value_t value = node.value.literal; + vm_obj_t value = node.value.literal; if (value.tag == VM_TAG_ERROR) { comp->is_error = true; return; @@ -524,7 +524,7 @@ static vm_arg_t vm_ast_comp_to(vm_ast_comp_t *comp, vm_ast_node_t node) { comp->cur = iftrue; vm_arg_t true_value = (vm_arg_t){ .type = VM_ARG_LIT, - .lit = (vm_std_value_t){ + .lit = (vm_obj_t){ .value.b = true, .tag = VM_TAG_BOOL, }, @@ -548,7 +548,7 @@ static vm_arg_t vm_ast_comp_to(vm_ast_comp_t *comp, vm_ast_node_t node) { comp->cur = iffalse; vm_arg_t false_value = (vm_arg_t){ .type = VM_ARG_LIT, - .lit = (vm_std_value_t){ + .lit = (vm_obj_t){ .value.b = false, .tag = VM_TAG_BOOL, }, @@ -602,7 +602,7 @@ static vm_arg_t vm_ast_comp_to(vm_ast_comp_t *comp, vm_ast_node_t node) { vm_ast_free_node(node); vm_arg_t key_arg = (vm_arg_t){ .type = VM_ARG_LIT, - .lit = (vm_std_value_t){ + .lit = (vm_obj_t){ .tag = VM_TAG_STR, .value.str = vm_strdup(target.value.ident), }, @@ -737,7 +737,7 @@ static vm_arg_t vm_ast_comp_to(vm_ast_comp_t *comp, vm_ast_node_t node) { vm_arg_t *call_args = vm_malloc(sizeof(vm_arg_t) * 4); call_args[0] = (vm_arg_t){ .type = VM_ARG_LIT, - .lit = (vm_std_value_t){ + .lit = (vm_obj_t){ .tag = VM_TAG_FFI, .value.ffi = &vm_std_vm_concat, }, @@ -770,7 +770,7 @@ static vm_arg_t vm_ast_comp_to(vm_ast_comp_t *comp, vm_ast_node_t node) { vm_arg_t *call_args = vm_malloc(sizeof(vm_arg_t) * 4); call_args[0] = (vm_arg_t){ .type = VM_ARG_LIT, - .lit = (vm_std_value_t){ + .lit = (vm_obj_t){ .tag = VM_TAG_FFI, .value.ffi = &vm_lua_comp_op_std_pow, }, @@ -934,7 +934,7 @@ static vm_arg_t vm_ast_comp_to(vm_ast_comp_t *comp, vm_ast_node_t node) { 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, - .lit = (vm_std_value_t){ + .lit = (vm_obj_t){ .tag = VM_TAG_FFI, .value.ffi = &vm_std_vm_closure, }, @@ -1026,13 +1026,13 @@ static vm_arg_t vm_ast_comp_to(vm_ast_comp_t *comp, vm_ast_node_t node) { break; } case VM_AST_NODE_LITERAL: { - vm_std_value_t num = node.value.literal; + vm_obj_t num = node.value.literal; if (num.tag == VM_TAG_NIL) { return vm_arg_nil(); } else if (num.tag == VM_TAG_STR) { vm_arg_t str = (vm_arg_t){ .type = VM_ARG_LIT, - .lit = (vm_std_value_t){ + .lit = (vm_obj_t){ .tag = VM_TAG_STR, .value.str = vm_strdup(num.value.str), }, @@ -1071,7 +1071,7 @@ static vm_arg_t vm_ast_comp_to(vm_ast_comp_t *comp, vm_ast_node_t node) { if (got.type != VM_ARG_NONE) { vm_arg_t env_key = (vm_arg_t){ .type = VM_ARG_LIT, - .lit = (vm_std_value_t){ + .lit = (vm_obj_t){ .tag = VM_TAG_STR, .value.str = vm_strdup(lit), }, diff --git a/vm/backend/backend.c b/vm/backend/backend.c index 563157fb..b88f2975 100644 --- a/vm/backend/backend.c +++ b/vm/backend/backend.c @@ -78,7 +78,7 @@ enum { VM_MAX_OP, }; -static VM_INLINE bool vm_interp_value_eq(vm_std_value_t v1, vm_std_value_t v2) { +static VM_INLINE bool vm_interp_value_eq(vm_obj_t v1, vm_obj_t v2) { #define OP(x, y) ((x)==(y)) #define OP_S(x, y) (strcmp((x), (y)) == 0) #define WRITE return @@ -87,7 +87,7 @@ static VM_INLINE bool vm_interp_value_eq(vm_std_value_t v1, vm_std_value_t v2) { __builtin_trap(); } -static VM_INLINE bool vm_interp_value_lt(vm_std_value_t v1, vm_std_value_t v2) { +static VM_INLINE bool vm_interp_value_lt(vm_obj_t v1, vm_obj_t v2) { #define OP(x, y) ((x)<(y)) #define OP_S(x, y) (strcmp((x), (y)) < 0) #define WRITE return @@ -96,7 +96,7 @@ static VM_INLINE bool vm_interp_value_lt(vm_std_value_t v1, vm_std_value_t v2) { __builtin_trap(); } -static VM_INLINE bool vm_interp_value_le(vm_std_value_t v1, vm_std_value_t v2) { +static VM_INLINE bool vm_interp_value_le(vm_obj_t v1, vm_obj_t v2) { #define OP(x, y) ((x)<=(y)) #define OP_S(x, y) (strcmp((x), (y)) <= 0) #define WRITE return @@ -105,35 +105,35 @@ static VM_INLINE bool vm_interp_value_le(vm_std_value_t v1, vm_std_value_t v2) { __builtin_trap(); } -static VM_INLINE vm_std_value_t vm_interp_add(vm_std_value_t v1, vm_std_value_t v2) { +static VM_INLINE vm_obj_t vm_interp_add(vm_obj_t v1, vm_obj_t v2) { #define OP(x, y) ((x)+(y)) #define WRITE return #define NAME ADD #include "binop.inc" } -static VM_INLINE vm_std_value_t vm_interp_sub(vm_std_value_t v1, vm_std_value_t v2) { +static VM_INLINE vm_obj_t vm_interp_sub(vm_obj_t v1, vm_obj_t v2) { #define OP(x, y) ((x)-(y)) #define WRITE return #define NAME SUB #include "binop.inc" } -static VM_INLINE vm_std_value_t vm_interp_mul(vm_std_value_t v1, vm_std_value_t v2) { +static VM_INLINE vm_obj_t vm_interp_mul(vm_obj_t v1, vm_obj_t v2) { #define OP(x, y) ((x)*(y)) #define WRITE return #define NAME MUL #include "binop.inc" } -static VM_INLINE vm_std_value_t vm_interp_div(vm_std_value_t v1, vm_std_value_t v2) { +static VM_INLINE vm_obj_t vm_interp_div(vm_obj_t v1, vm_obj_t v2) { #define OP(x, y) ((x)/(y)) #define WRITE return #define NAME DIV #include "binop.inc" } -static VM_INLINE vm_std_value_t vm_interp_idiv(vm_std_value_t v1, vm_std_value_t v2) { +static VM_INLINE vm_obj_t vm_interp_idiv(vm_obj_t v1, vm_obj_t v2) { #define OP(x, y) ((x)/(y)) #define OP_F(x, y) floor((x)/(y)) #define WRITE return @@ -141,7 +141,7 @@ static VM_INLINE vm_std_value_t vm_interp_idiv(vm_std_value_t v1, vm_std_value_t #include "binop.inc" } -static VM_INLINE vm_std_value_t vm_interp_mod(vm_std_value_t v1, vm_std_value_t v2) { +static VM_INLINE vm_obj_t vm_interp_mod(vm_obj_t v1, vm_obj_t v2) { #define OP(x, y) ((x)%(y)) #define OP_F(x, y) fmod((x),(y)) #define WRITE return @@ -209,9 +209,9 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { } case VM_IOP_ADD: { if (instr.args[0].type == VM_ARG_LIT && instr.args[1].type == VM_ARG_LIT) { - vm_std_value_t v1 = instr.args[0].lit; - vm_std_value_t v2 = instr.args[1].lit; - vm_std_value_t v3 = vm_interp_add(v1, v2); + 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_interp_tag_t, v3.tag); vm_interp_push(vm_value_t, v3.value); @@ -240,9 +240,9 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { } case VM_IOP_SUB: { if (instr.args[0].type == VM_ARG_LIT && instr.args[1].type == VM_ARG_LIT) { - vm_std_value_t v1 = instr.args[0].lit; - vm_std_value_t v2 = instr.args[1].lit; - vm_std_value_t v3 = vm_interp_sub(v1, v2); + 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_interp_tag_t, v3.tag); vm_interp_push(vm_value_t, v3.value); @@ -271,9 +271,9 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { } case VM_IOP_MUL: { if (instr.args[0].type == VM_ARG_LIT && instr.args[1].type == VM_ARG_LIT) { - vm_std_value_t v1 = instr.args[0].lit; - vm_std_value_t v2 = instr.args[1].lit; - vm_std_value_t v3 = vm_interp_mul(v1, v2); + 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_interp_tag_t, v3.tag); vm_interp_push(vm_value_t, v3.value); @@ -302,9 +302,9 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { } case VM_IOP_DIV: { if (instr.args[0].type == VM_ARG_LIT && instr.args[1].type == VM_ARG_LIT) { - vm_std_value_t v1 = instr.args[0].lit; - vm_std_value_t v2 = instr.args[1].lit; - vm_std_value_t v3 = vm_interp_div(v1, v2); + 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_interp_tag_t, v3.tag); vm_interp_push(vm_value_t, v3.value); @@ -333,9 +333,9 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { } case VM_IOP_IDIV: { if (instr.args[0].type == VM_ARG_LIT && instr.args[1].type == VM_ARG_LIT) { - vm_std_value_t v1 = instr.args[0].lit; - vm_std_value_t v2 = instr.args[1].lit; - vm_std_value_t v3 = vm_interp_idiv(v1, v2); + 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_interp_tag_t, v3.tag); vm_interp_push(vm_value_t, v3.value); @@ -364,9 +364,9 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { } case VM_IOP_MOD: { if (instr.args[0].type == VM_ARG_LIT && instr.args[1].type == VM_ARG_LIT) { - vm_std_value_t v1 = instr.args[0].lit; - vm_std_value_t v2 = instr.args[1].lit; - vm_std_value_t v3 = vm_interp_mod(v1, v2); + 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_interp_tag_t, v3.tag); vm_interp_push(vm_value_t, v3.value); @@ -446,7 +446,7 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { } case VM_BOP_BB: { if (branch.args[0].type == VM_ARG_LIT) { - vm_std_value_t v1 = branch.args[0].lit; + vm_obj_t v1 = branch.args[0].lit; if (v1.tag != VM_TAG_NIL && (v1.tag != VM_TAG_BOOL || v1.value.b)) { vm_interp_push_op(VM_OP_JUMP); vm_interp_push(vm_block_t *, vm->blocks->blocks[branch.targets[0]->id]); @@ -466,8 +466,8 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { } case VM_BOP_BLT: { if (branch.args[0].type == VM_ARG_LIT && branch.args[1].type == VM_ARG_LIT) { - vm_std_value_t v1 = branch.args[0].lit; - vm_std_value_t v2 = branch.args[1].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 *, vm->blocks->blocks[branch.targets[0]->id]); @@ -502,8 +502,8 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { } case VM_BOP_BLE: { if (branch.args[0].type == VM_ARG_LIT && branch.args[1].type == VM_ARG_LIT) { - vm_std_value_t v1 = branch.args[0].lit; - vm_std_value_t v2 = branch.args[1].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 *, vm->blocks->blocks[branch.targets[0]->id]); @@ -538,8 +538,8 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { } case VM_BOP_BEQ: { if (branch.args[0].type == VM_ARG_LIT && branch.args[1].type == VM_ARG_LIT) { - vm_std_value_t v1 = branch.args[0].lit; - vm_std_value_t v2 = branch.args[1].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 *, vm->blocks->blocks[branch.targets[0]->id]); @@ -673,7 +673,7 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { #define vm_run_repl_lit() ({ \ vm_tag_t tag = vm_run_repl_read(vm_interp_tag_t); \ vm_value_t value = vm_run_repl_read(vm_value_t); \ - (vm_std_value_t) { \ + (vm_obj_t) { \ .tag = tag, \ .value = value, \ }; \ @@ -698,8 +698,8 @@ void *vm_interp_renumber_block(vm_t *vm, void **ptrs, vm_block_t *block) { #define vm_run_repl_jump() goto *vm_run_repl_read(void *); -vm_std_value_t vm_run_repl(vm_t *vm, vm_block_t *block) { - vm_std_value_t *regs = vm->regs; +vm_obj_t vm_run_repl(vm_t *vm, vm_block_t *block) { + vm_obj_t *regs = vm->regs; static void *ptrs[VM_MAX_OP] = { [VM_OP_TABLE_SET] = &&VM_OP_TABLE_SET, [VM_OP_TABLE_NEW] = &&VM_OP_TABLE_NEW, @@ -741,7 +741,7 @@ vm_std_value_t vm_run_repl(vm_t *vm, vm_block_t *block) { [VM_OP_GET] = &&VM_OP_GET, [VM_OP_CALL] = &&VM_OP_CALL, }; - vm_std_value_t *next_regs = ®s[block->nregs]; + vm_obj_t *next_regs = ®s[block->nregs]; // { // vm_io_buffer_t *buf = vm_io_buffer_new(); @@ -769,11 +769,11 @@ new_block_no_print:; vm_run_repl_jump(); VM_OP_TABLE_SET:; VM_OPCODE_DEBUG(table_set) { - vm_std_value_t v1 = vm_run_repl_arg(); - vm_std_value_t v2 = vm_run_repl_arg(); - vm_std_value_t v3 = vm_run_repl_arg(); + vm_obj_t v1 = vm_run_repl_arg(); + vm_obj_t v2 = vm_run_repl_arg(); + vm_obj_t v3 = vm_run_repl_arg(); if (v1.tag != VM_TAG_TAB) { - return (vm_std_value_t) { + return (vm_obj_t) { .tag = VM_TAG_ERROR, .value.str = "can only set index on tables", }; @@ -782,156 +782,156 @@ new_block_no_print:; vm_run_repl_jump(); } VM_OP_TABLE_NEW:; VM_OPCODE_DEBUG(table_new) { - vm_run_repl_out(((vm_std_value_t) { + vm_run_repl_out(((vm_obj_t) { .tag = VM_TAG_TAB, .value.table = vm_table_new(), })); vm_run_repl_jump(); } VM_OP_TABLE_LEN:; VM_OPCODE_DEBUG(table_len) { - vm_std_value_t v1 = vm_run_repl_arg(); + vm_obj_t v1 = vm_run_repl_arg(); if (v1.tag != VM_TAG_TAB) { - return (vm_std_value_t) { + return (vm_obj_t) { .tag = VM_TAG_ERROR, .value.str = "can only get length on tables", }; } - vm_run_repl_out(VM_STD_VALUE_NUMBER(vm, v1.value.table->len)); + vm_run_repl_out(VM_OBJ_NUMBER(vm, v1.value.table->len)); vm_run_repl_jump(); } VM_OP_MOVE_I:; VM_OPCODE_DEBUG(move_i) { - vm_std_value_t v1 = vm_run_repl_lit(); + vm_obj_t v1 = vm_run_repl_lit(); vm_run_repl_out(v1); vm_run_repl_jump(); } VM_OP_MOVE_R:; VM_OPCODE_DEBUG(move_r) { - vm_std_value_t v1 = vm_run_repl_reg(); + vm_obj_t v1 = vm_run_repl_reg(); vm_run_repl_out(v1); vm_run_repl_jump(); } VM_OP_ADD_RI:; VM_OPCODE_DEBUG(add_ri) { - vm_std_value_t v1 = vm_run_repl_reg(); - vm_std_value_t v2 = vm_run_repl_lit(); - vm_std_value_t v3 = vm_interp_add(v1, v2); + vm_obj_t v1 = vm_run_repl_reg(); + vm_obj_t v2 = vm_run_repl_lit(); + vm_obj_t v3 = vm_interp_add(v1, v2); vm_run_repl_out(v3); vm_run_repl_jump(); } VM_OP_ADD_IR:; VM_OPCODE_DEBUG(add_ir) { - vm_std_value_t v1 = vm_run_repl_lit(); - vm_std_value_t v2 = vm_run_repl_reg(); - vm_std_value_t v3 = vm_interp_add(v1, v2); + vm_obj_t v1 = vm_run_repl_lit(); + vm_obj_t v2 = vm_run_repl_reg(); + vm_obj_t v3 = vm_interp_add(v1, v2); vm_run_repl_out(v3); vm_run_repl_jump(); } VM_OP_ADD_RR:; VM_OPCODE_DEBUG(add_rr) { - vm_std_value_t v1 = vm_run_repl_reg(); - vm_std_value_t v2 = vm_run_repl_reg(); - vm_std_value_t v3 = vm_interp_add(v1, v2); + vm_obj_t v1 = vm_run_repl_reg(); + vm_obj_t v2 = vm_run_repl_reg(); + vm_obj_t v3 = vm_interp_add(v1, v2); vm_run_repl_out(v3); vm_run_repl_jump(); } VM_OP_SUB_RI:; VM_OPCODE_DEBUG(sub_ri) { - vm_std_value_t v1 = vm_run_repl_reg(); - vm_std_value_t v2 = vm_run_repl_lit(); - vm_std_value_t v3 = vm_interp_sub(v1, v2); + vm_obj_t v1 = vm_run_repl_reg(); + vm_obj_t v2 = vm_run_repl_lit(); + vm_obj_t v3 = vm_interp_sub(v1, v2); vm_run_repl_out(v3); vm_run_repl_jump(); } VM_OP_SUB_IR:; VM_OPCODE_DEBUG(sub_ir) { - vm_std_value_t v1 = vm_run_repl_lit(); - vm_std_value_t v2 = vm_run_repl_reg(); - vm_std_value_t v3 = vm_interp_sub(v1, v2); + vm_obj_t v1 = vm_run_repl_lit(); + vm_obj_t v2 = vm_run_repl_reg(); + vm_obj_t v3 = vm_interp_sub(v1, v2); vm_run_repl_out(v3); vm_run_repl_jump(); } VM_OP_SUB_RR:; VM_OPCODE_DEBUG(sub_rr) { - vm_std_value_t v1 = vm_run_repl_reg(); - vm_std_value_t v2 = vm_run_repl_reg(); - vm_std_value_t v3 = vm_interp_sub(v1, v2); + vm_obj_t v1 = vm_run_repl_reg(); + vm_obj_t v2 = vm_run_repl_reg(); + vm_obj_t v3 = vm_interp_sub(v1, v2); vm_run_repl_out(v3); vm_run_repl_jump(); } VM_OP_MUL_RI:; VM_OPCODE_DEBUG(mul_ri) { - vm_std_value_t v1 = vm_run_repl_reg(); - vm_std_value_t v2 = vm_run_repl_lit(); - vm_std_value_t v3 = vm_interp_mul(v1, v2); + vm_obj_t v1 = vm_run_repl_reg(); + vm_obj_t v2 = vm_run_repl_lit(); + vm_obj_t v3 = vm_interp_mul(v1, v2); vm_run_repl_out(v3); vm_run_repl_jump(); } VM_OP_MUL_IR:; VM_OPCODE_DEBUG(mul_ir) { - vm_std_value_t v1 = vm_run_repl_lit(); - vm_std_value_t v2 = vm_run_repl_reg(); - vm_std_value_t v3 = vm_interp_mul(v1, v2); + vm_obj_t v1 = vm_run_repl_lit(); + vm_obj_t v2 = vm_run_repl_reg(); + vm_obj_t v3 = vm_interp_mul(v1, v2); vm_run_repl_out(v3); vm_run_repl_jump(); } VM_OP_MUL_RR:; VM_OPCODE_DEBUG(mul_rr) { - vm_std_value_t v1 = vm_run_repl_reg(); - vm_std_value_t v2 = vm_run_repl_reg(); - vm_std_value_t v3 = vm_interp_mul(v1, v2); + vm_obj_t v1 = vm_run_repl_reg(); + vm_obj_t v2 = vm_run_repl_reg(); + vm_obj_t v3 = vm_interp_mul(v1, v2); vm_run_repl_out(v3); vm_run_repl_jump(); } VM_OP_DIV_RI:; VM_OPCODE_DEBUG(div_ri) { - vm_std_value_t v1 = vm_run_repl_reg(); - vm_std_value_t v2 = vm_run_repl_lit(); - vm_std_value_t v3 = vm_interp_div(v1, v2); + vm_obj_t v1 = vm_run_repl_reg(); + vm_obj_t v2 = vm_run_repl_lit(); + vm_obj_t v3 = vm_interp_div(v1, v2); vm_run_repl_out(v3); vm_run_repl_jump(); } VM_OP_DIV_IR:; VM_OPCODE_DEBUG(div_ir) { - vm_std_value_t v1 = vm_run_repl_lit(); - vm_std_value_t v2 = vm_run_repl_reg(); - vm_std_value_t v3 = vm_interp_div(v1, v2); + vm_obj_t v1 = vm_run_repl_lit(); + vm_obj_t v2 = vm_run_repl_reg(); + vm_obj_t v3 = vm_interp_div(v1, v2); vm_run_repl_out(v3); vm_run_repl_jump(); } VM_OP_DIV_RR:; VM_OPCODE_DEBUG(div_rr) { - vm_std_value_t v1 = vm_run_repl_reg(); - vm_std_value_t v2 = vm_run_repl_reg(); - vm_std_value_t v3 = vm_interp_div(v1, v2); + vm_obj_t v1 = vm_run_repl_reg(); + vm_obj_t v2 = vm_run_repl_reg(); + vm_obj_t v3 = vm_interp_div(v1, v2); vm_run_repl_out(v3); vm_run_repl_jump(); } VM_OP_IDIV_RI:; VM_OPCODE_DEBUG(idiv_ri) { - vm_std_value_t v1 = vm_run_repl_reg(); - vm_std_value_t v2 = vm_run_repl_lit(); - vm_std_value_t v3 = vm_interp_idiv(v1, v2); + vm_obj_t v1 = vm_run_repl_reg(); + vm_obj_t v2 = vm_run_repl_lit(); + vm_obj_t v3 = vm_interp_idiv(v1, v2); vm_run_repl_out(v3); vm_run_repl_jump(); } VM_OP_IDIV_IR:; VM_OPCODE_DEBUG(idiv_ir) { - vm_std_value_t v1 = vm_run_repl_lit(); - vm_std_value_t v2 = vm_run_repl_reg(); - vm_std_value_t v3 = vm_interp_idiv(v1, v2); + vm_obj_t v1 = vm_run_repl_lit(); + vm_obj_t v2 = vm_run_repl_reg(); + vm_obj_t v3 = vm_interp_idiv(v1, v2); vm_run_repl_out(v3); vm_run_repl_jump(); } VM_OP_IDIV_RR:; VM_OPCODE_DEBUG(idiv_rr) { - vm_std_value_t v1 = vm_run_repl_reg(); - vm_std_value_t v2 = vm_run_repl_reg(); - vm_std_value_t v3 = vm_interp_idiv(v1, v2); + vm_obj_t v1 = vm_run_repl_reg(); + vm_obj_t v2 = vm_run_repl_reg(); + vm_obj_t v3 = vm_interp_idiv(v1, v2); vm_run_repl_out(v3); vm_run_repl_jump(); } VM_OP_MOD_RI:; VM_OPCODE_DEBUG(mod_ri) { - vm_std_value_t v1 = vm_run_repl_reg(); - vm_std_value_t v2 = vm_run_repl_lit(); - vm_std_value_t v3 = vm_interp_mod(v1, v2); + vm_obj_t v1 = vm_run_repl_reg(); + vm_obj_t v2 = vm_run_repl_lit(); + vm_obj_t v3 = vm_interp_mod(v1, v2); vm_run_repl_out(v3); vm_run_repl_jump(); } VM_OP_MOD_IR:; VM_OPCODE_DEBUG(mod_ir) { - vm_std_value_t v1 = vm_run_repl_lit(); - vm_std_value_t v2 = vm_run_repl_reg(); - vm_std_value_t v3 = vm_interp_mod(v1, v2); + vm_obj_t v1 = vm_run_repl_lit(); + vm_obj_t v2 = vm_run_repl_reg(); + vm_obj_t v3 = vm_interp_mod(v1, v2); vm_run_repl_out(v3); vm_run_repl_jump(); } VM_OP_MOD_RR:; VM_OPCODE_DEBUG(mod_rr) { - vm_std_value_t v1 = vm_run_repl_reg(); - vm_std_value_t v2 = vm_run_repl_reg(); - vm_std_value_t v3 = vm_interp_mod(v1, v2); + vm_obj_t v1 = vm_run_repl_reg(); + vm_obj_t v2 = vm_run_repl_reg(); + vm_obj_t v3 = vm_interp_mod(v1, v2); vm_run_repl_out(v3); vm_run_repl_jump(); } @@ -940,7 +940,7 @@ new_block_no_print:; goto new_block; } VM_OP_BB_R:; VM_OPCODE_DEBUG(bb_r) { - vm_std_value_t v1 = vm_run_repl_reg(); + vm_obj_t v1 = vm_run_repl_reg(); if (v1.tag != VM_TAG_NIL && (v1.tag != VM_TAG_BOOL || v1.value.b)) { block = vm_run_repl_read(vm_block_t *); } else { @@ -950,8 +950,8 @@ new_block_no_print:; goto new_block; } VM_OP_BLT_RI:; VM_OPCODE_DEBUG(blt_ri) { - vm_std_value_t v1 = vm_run_repl_reg(); - vm_std_value_t v2 = vm_run_repl_lit(); + 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 *); } else { @@ -961,8 +961,8 @@ new_block_no_print:; goto new_block; } VM_OP_BLT_IR:; VM_OPCODE_DEBUG(blt_ir) { - vm_std_value_t v1 = vm_run_repl_lit(); - vm_std_value_t v2 = vm_run_repl_reg(); + 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 *); } else { @@ -972,8 +972,8 @@ new_block_no_print:; goto new_block; } VM_OP_BLT_RR:; VM_OPCODE_DEBUG(blt_rr) { - vm_std_value_t v1 = vm_run_repl_reg(); - vm_std_value_t v2 = vm_run_repl_reg(); + 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 *); } else { @@ -983,8 +983,8 @@ new_block_no_print:; goto new_block; } VM_OP_BLE_RI:; VM_OPCODE_DEBUG(ble_ri) { - vm_std_value_t v1 = vm_run_repl_reg(); - vm_std_value_t v2 = vm_run_repl_lit(); + 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 *); } else { @@ -994,8 +994,8 @@ new_block_no_print:; goto new_block; } VM_OP_BLE_IR:; VM_OPCODE_DEBUG(ble_ir) { - vm_std_value_t v1 = vm_run_repl_lit(); - vm_std_value_t v2 = vm_run_repl_reg(); + 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 *); } else { @@ -1005,8 +1005,8 @@ new_block_no_print:; goto new_block; } VM_OP_BLE_RR:; VM_OPCODE_DEBUG(ble_rr) { - vm_std_value_t v1 = vm_run_repl_reg(); - vm_std_value_t v2 = vm_run_repl_reg(); + 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 *); } else { @@ -1016,8 +1016,8 @@ new_block_no_print:; goto new_block; } VM_OP_BEQ_RI:; VM_OPCODE_DEBUG(beq_ri) { - vm_std_value_t v1 = vm_run_repl_reg(); - vm_std_value_t v2 = vm_run_repl_lit(); + 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 *); } else { @@ -1027,8 +1027,8 @@ new_block_no_print:; goto new_block; } VM_OP_BEQ_IR:; VM_OPCODE_DEBUG(beq_ir) { - vm_std_value_t v1 = vm_run_repl_lit(); - vm_std_value_t v2 = vm_run_repl_reg(); + 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 *); } else { @@ -1038,8 +1038,8 @@ new_block_no_print:; goto new_block; } VM_OP_BEQ_RR:; VM_OPCODE_DEBUG(beq_rr) { - vm_std_value_t v1 = vm_run_repl_reg(); - vm_std_value_t v2 = vm_run_repl_reg(); + 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 *); } else { @@ -1049,17 +1049,17 @@ new_block_no_print:; goto new_block; } VM_OP_RET_I:; VM_OPCODE_DEBUG(ret_i) { - vm_std_value_t v1 = vm_run_repl_lit(); + vm_obj_t v1 = vm_run_repl_lit(); return v1; } VM_OP_RET_R:; VM_OPCODE_DEBUG(ret_r) { - vm_std_value_t v1 = vm_run_repl_reg(); + vm_obj_t v1 = vm_run_repl_reg(); return v1; } VM_OP_LOAD:; VM_OPCODE_DEBUG(load) { - vm_std_value_t v1 = vm_run_repl_arg(); - vm_std_value_t v2 = vm_run_repl_arg(); - vm_std_value_t v3; + vm_obj_t v1 = vm_run_repl_arg(); + vm_obj_t v2 = vm_run_repl_arg(); + vm_obj_t v3; switch (v2.tag) { case VM_TAG_I8: { v3 = v1.value.closure[v2.value.i8]; @@ -1096,20 +1096,20 @@ new_block_no_print:; } VM_OP_GET:; VM_OPCODE_DEBUG(get) { uint8_t *c0 = code; - vm_std_value_t v1 = vm_run_repl_arg(); - vm_std_value_t v2 = vm_run_repl_arg(); + vm_obj_t v1 = vm_run_repl_arg(); + vm_obj_t v2 = vm_run_repl_arg(); vm_table_pair_t pair = (vm_table_pair_t) { .key_tag = v2.tag, .key_val = v2.value, }; if (v1.tag != VM_TAG_TAB) { - return (vm_std_value_t) { + return (vm_obj_t) { .tag = VM_TAG_ERROR, .value.str = "can only index tables", }; } vm_table_get_pair(v1.value.table, &pair); - vm_std_value_t v3 = (vm_std_value_t) { + vm_obj_t v3 = (vm_obj_t) { .tag = pair.val_tag, .value = pair.val_val, }; @@ -1118,7 +1118,7 @@ new_block_no_print:; goto new_block; } VM_OP_CALL:; VM_OPCODE_DEBUG(call) { - vm_std_value_t v1 = vm_run_repl_arg(); + vm_obj_t v1 = vm_run_repl_arg(); switch (v1.tag) { case VM_TAG_CLOSURE: { next_regs[0] = v1; @@ -1143,9 +1143,9 @@ new_block_no_print:; } call_closure_end:; next_regs[j].tag = VM_TAG_UNK; - vm_std_value_t *last_regs = regs; + vm_obj_t *last_regs = regs; vm->regs = next_regs; - vm_std_value_t got = vm_run_repl(vm, vm->blocks->blocks[v1.value.closure[0].value.i32]); + vm_obj_t got = vm_run_repl(vm, vm->blocks->blocks[v1.value.closure[0].value.i32]); vm->regs = last_regs; if (got.tag == VM_TAG_ERROR) { return got; @@ -1176,11 +1176,11 @@ new_block_no_print:; } call_ffi_end:; next_regs[j].tag = VM_TAG_UNK; - vm_std_value_t *last_regs = regs; + vm_obj_t *last_regs = regs; vm->regs = next_regs; v1.value.ffi(vm, next_regs); vm->regs = last_regs; - vm_std_value_t got = next_regs[0]; + vm_obj_t got = next_regs[0]; if (got.tag == VM_TAG_ERROR) { return got; } @@ -1189,7 +1189,7 @@ new_block_no_print:; goto new_block; } default: { - return (vm_std_value_t) { + return (vm_obj_t) { .tag = VM_TAG_ERROR, .value.str = "can only call functions", }; @@ -1199,8 +1199,8 @@ new_block_no_print:; } } -vm_std_value_t vm_run_main(vm_t *vm, vm_block_t *entry) { - vm_std_value_t val = vm_run_repl(vm, entry); +vm_obj_t vm_run_main(vm_t *vm, vm_block_t *entry) { + vm_obj_t val = vm_run_repl(vm, entry); if (val.tag == VM_TAG_ERROR) { fprintf(stderr, "error: %s\n", val.value.str); } diff --git a/vm/backend/backend.h b/vm/backend/backend.h index 91027506..e87738ec 100644 --- a/vm/backend/backend.h +++ b/vm/backend/backend.h @@ -5,7 +5,7 @@ #include "../vm.h" #include "../ir.h" -vm_std_value_t vm_run_main(vm_t *config, vm_block_t *entry); -vm_std_value_t vm_run_repl(vm_t *config, vm_block_t *entry); +vm_obj_t vm_run_main(vm_t *config, vm_block_t *entry); +vm_obj_t vm_run_repl(vm_t *config, vm_block_t *entry); #endif diff --git a/vm/backend/binop.inc b/vm/backend/binop.inc index b4f2d1b1..ab806491 100644 --- a/vm/backend/binop.inc +++ b/vm/backend/binop.inc @@ -40,252 +40,252 @@ LABEL(VM_TAG_TAB, VM_TAG_FFI) __builtin_trap(); } LABEL(VM_TAG_I8, VM_TAG_I8) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_I8, .value.i8 = OP(v1.value.i8, v2.value.i8), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I8, VM_TAG_I16) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_I16, .value.i16 = (int16_t) OP(v1.value.i8, v2.value.i16), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I8, VM_TAG_I32) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_I32, .value.i32 = (int32_t) OP(v1.value.i8, v2.value.i32), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I8, VM_TAG_I64) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_I64, .value.i64 = (int64_t) OP(v1.value.i8, v2.value.i64), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I8, VM_TAG_F32) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_F32, .value.f32 = OP_F((float) v1.value.i8, v2.value.f32), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I8, VM_TAG_F64) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_F64, .value.f64 = OP_F((double) v1.value.i8, v2.value.f64), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I16, VM_TAG_I8) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_I16, .value.i16 = OP(v1.value.i16, (int16_t) v2.value.i8), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I16, VM_TAG_I16) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_I16, .value.i16 = OP(v1.value.i16, v2.value.i16), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I16, VM_TAG_I32) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_I32, .value.i32 = (int32_t) OP(v1.value.i16, v2.value.i32), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I16, VM_TAG_I64) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_I64, .value.i64 = (int64_t) OP(v1.value.i16, v2.value.i64), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I16, VM_TAG_F32) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_F32, .value.f32 = OP_F((float) v1.value.i16, v2.value.f32), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I16, VM_TAG_F64) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_F64, .value.f64 = OP_F((double) v1.value.i16, v2.value.f64), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I32, VM_TAG_I8) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_I32, .value.i16 = OP(v1.value.i32, (int32_t) v2.value.i8), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I32, VM_TAG_I16) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_I32, .value.i16 = OP(v1.value.i32, (int32_t) v2.value.i16), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I32, VM_TAG_I32) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_I32, .value.i32 = OP(v1.value.i32, v2.value.i32), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I32, VM_TAG_I64) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_I64, .value.i64 = (int64_t) OP(v1.value.i32, v2.value.i64), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I32, VM_TAG_F32) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_F32, .value.f32 = OP_F((float) v1.value.i32, v2.value.f32), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I32, VM_TAG_F64) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_F64, .value.f64 = OP_F((double) v1.value.i32, v2.value.f64), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I64, VM_TAG_I8) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_I64, .value.i16 = OP(v1.value.i64, (int64_t) v2.value.i8), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I64, VM_TAG_I16) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_I64, .value.i16 = OP(v1.value.i64, (int64_t) v2.value.i16), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I64, VM_TAG_I32) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_I64, .value.i32 = OP(v1.value.i64, (int64_t) v2.value.i32), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I64, VM_TAG_I64) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_I64, .value.i64 = OP(v1.value.i64, v2.value.i64), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I64, VM_TAG_F32) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_F32, .value.f32 = OP_F((float) v1.value.i64, v2.value.f32), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_I64, VM_TAG_F64) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_F64, .value.f64 = OP_F((double) v1.value.i64, v2.value.f64), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_F32, VM_TAG_I8) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_F32, .value.f32 = OP_F(v1.value.f32, (float) v2.value.i8), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_F32, VM_TAG_I16) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_F32, .value.f32 = OP_F(v1.value.f32, (float) v2.value.i16), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_F32, VM_TAG_I32) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_F32, .value.f32 = OP_F(v1.value.f32, (float) v2.value.i32), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_F32, VM_TAG_I64) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_F32, .value.f32 = OP_F(v1.value.f32, (float) v2.value.i64), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_F32, VM_TAG_F32) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_F32, .value.f32 = OP_F(v1.value.f32, v2.value.f32), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_F32, VM_TAG_F64) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_F64, .value.f64 = OP_F((double) v1.value.f32, v2.value.f64), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_F64, VM_TAG_I8) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_F64, .value.f32 = OP_F(v1.value.f64, (double) v2.value.i8), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_F64, VM_TAG_I16) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_F64, .value.f32 = OP_F(v1.value.f64, (double) v2.value.i16), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_F64, VM_TAG_I32) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_F64, .value.f32 = OP_F(v1.value.f64, (double) v2.value.i32), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_F64, VM_TAG_I64) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_F64, .value.f32 = OP_F(v1.value.f64, (double) v2.value.i64), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_F64, VM_TAG_F32) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_F64, .value.f32 = OP_F(v1.value.f64, (double) v2.value.f32), }; goto CONCAT(NAME, END); } LABEL(VM_TAG_F64, VM_TAG_F64) { - WRITE (vm_std_value_t) { + WRITE (vm_obj_t) { .tag = VM_TAG_F64, .value.f64 = OP_F(v1.value.f64, v2.value.f64), }; diff --git a/vm/io.c b/vm/io.c index e717864f..ef5ccb45 100644 --- a/vm/io.c +++ b/vm/io.c @@ -91,7 +91,7 @@ static void vm_indent(vm_io_buffer_t *out, size_t indent, const char *prefix) { vm_io_buffer_format(out, "%s", prefix); } -void vm_io_print_lit(vm_io_buffer_t *out, vm_std_value_t value) { +void vm_io_print_lit(vm_io_buffer_t *out, vm_obj_t value) { switch (value.tag) { case VM_TAG_NIL: { vm_io_buffer_format(out, "nil"); @@ -136,7 +136,7 @@ void vm_io_print_lit(vm_io_buffer_t *out, vm_std_value_t value) { } } -void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_std_value_t value, vm_io_debug_t *link) { +void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_obj_t value, vm_io_debug_t *link) { size_t up = 1; while (link != NULL) { if (value.tag == link->value.tag) { @@ -233,7 +233,7 @@ void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_std_ __builtin_trap(); } case VM_TAG_BOOL: { - vm_std_value_t val = (vm_std_value_t){ + vm_obj_t val = (vm_obj_t){ .tag = p.val_tag, .value = p.val_val, }; @@ -245,7 +245,7 @@ void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_std_ break; } case VM_TAG_I8: { - vm_std_value_t val = (vm_std_value_t){ + vm_obj_t val = (vm_obj_t){ .tag = p.val_tag, .value = p.val_val, }; @@ -255,7 +255,7 @@ void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_std_ break; } case VM_TAG_I16: { - vm_std_value_t val = (vm_std_value_t){ + vm_obj_t val = (vm_obj_t){ .tag = p.val_tag, .value = p.val_val, }; @@ -265,7 +265,7 @@ void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_std_ break; } case VM_TAG_I32: { - vm_std_value_t val = (vm_std_value_t){ + vm_obj_t val = (vm_obj_t){ .tag = p.val_tag, .value = p.val_val, }; @@ -275,7 +275,7 @@ void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_std_ break; } case VM_TAG_I64: { - vm_std_value_t val = (vm_std_value_t){ + vm_obj_t val = (vm_obj_t){ .tag = p.val_tag, .value = p.val_val, }; @@ -285,7 +285,7 @@ void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_std_ break; } case VM_TAG_F64: { - vm_std_value_t val = (vm_std_value_t){ + vm_obj_t val = (vm_obj_t){ .tag = p.val_tag, .value = p.val_val, }; @@ -295,7 +295,7 @@ void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_std_ break; } case VM_TAG_STR: { - vm_std_value_t val = (vm_std_value_t){ + vm_obj_t val = (vm_obj_t){ .tag = p.val_tag, .value = p.val_val, }; @@ -308,12 +308,12 @@ void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_std_ default: { vm_indent(out, indent + 1, ""); vm_io_buffer_format(out, "pair {\n"); - vm_std_value_t key = (vm_std_value_t){ + vm_obj_t key = (vm_obj_t){ .tag = p.key_tag, .value = p.key_val, }; vm_io_debug(out, indent + 2, "key = ", key, &next); - vm_std_value_t val = (vm_std_value_t){ + vm_obj_t val = (vm_obj_t){ .tag = p.val_tag, .value = p.val_val, }; @@ -340,7 +340,7 @@ void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_std_ } } -void vm_value_buffer_tostring(vm_io_buffer_t *buf, vm_std_value_t value) { +void vm_value_buffer_tostring(vm_io_buffer_t *buf, vm_obj_t value) { switch (value.tag) { case VM_TAG_NIL: { vm_io_buffer_format(buf, "nil"); diff --git a/vm/io.h b/vm/io.h index 66a0026b..3f688100 100644 --- a/vm/io.h +++ b/vm/io.h @@ -12,7 +12,7 @@ typedef struct vm_io_buffer_t vm_io_buffer_t; struct vm_io_debug_t { vm_io_debug_t *next; - vm_std_value_t value; + vm_obj_t value; }; struct vm_io_buffer_t { @@ -23,13 +23,13 @@ struct vm_io_buffer_t { vm_io_buffer_t *vm_io_buffer_new(void); char *vm_io_buffer_get(vm_io_buffer_t *buf); -void vm_io_print_lit(vm_io_buffer_t *out, vm_std_value_t value); -void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_std_value_t value, vm_io_debug_t *link); +void vm_io_print_lit(vm_io_buffer_t *out, vm_obj_t value); +void vm_io_debug(vm_io_buffer_t *out, size_t indent, const char *prefix, vm_obj_t value, vm_io_debug_t *link); char *vm_io_read(const char *filename); 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_std_value_t value); +void vm_value_buffer_tostring(vm_io_buffer_t *buf, vm_obj_t value); #endif diff --git a/vm/ir.h b/vm/ir.h index 5c3f78c5..c01ac39a 100644 --- a/vm/ir.h +++ b/vm/ir.h @@ -61,7 +61,7 @@ enum { struct vm_arg_t { union { - vm_std_value_t lit; + vm_obj_t lit; vm_block_t *func; struct { diff --git a/vm/lua/ast.c b/vm/lua/ast.c index db6f1ed3..e25d931a 100644 --- a/vm/lua/ast.c +++ b/vm/lua/ast.c @@ -634,7 +634,8 @@ vm_ast_node_t vm_lang_lua_parse(vm_t *vm, const char *str) { ts_parser_delete(parser); - return vm_ast_build_do(res, vm_ast_build_return(vm_ast_build_nil())); + // return vm_ast_build_do(res, vm_ast_build_return(vm_ast_build_nil())); + return vm_ast_build_return(res); } vm_block_t *vm_compile(vm_t *vm, const char *src) { @@ -645,5 +646,17 @@ vm_block_t *vm_compile(vm_t *vm, const char *src) { .src = src, }; vm->blocks->srcs = next; - return vm_ast_comp_more(vm, ast); + size_t len = vm->blocks->len; + vm_block_t *block = vm_ast_comp_more(vm, ast); + if (vm->dump_ir) { + vm_io_buffer_t *buf = vm_io_buffer_new(); + for (size_t i = len; i < vm->blocks->len; i++) { + vm_io_format_block(buf, vm->blocks->blocks[i]); + vm_io_buffer_format(buf, "\n"); + } + fprintf(stdout, "%s\n", buf->buf); + vm_free(buf->buf); + vm_free(buf); + } + return block; } diff --git a/vm/lua/repl.c b/vm/lua/repl.c index 5f527a1c..87d4eae4 100644 --- a/vm/lua/repl.c +++ b/vm/lua/repl.c @@ -17,20 +17,20 @@ const TSLanguage *tree_sitter_lua(void); vm_ast_node_t vm_lang_lua_parse(vm_t *vm, const char *str); -vm_std_value_t vm_repl_table_get(vm_table_t *table, const char *key) { +vm_obj_t vm_repl_table_get(vm_table_t *table, const char *key) { vm_table_pair_t pair = (vm_table_pair_t){ .key_tag = VM_TAG_STR, .key_val.str = key, }; vm_table_get_pair(table, &pair); - return (vm_std_value_t){ + return (vm_obj_t){ .value = pair.val_val, .tag = pair.val_tag, }; } bool vm_repl_table_get_bool(vm_table_t *table, const char *key) { - vm_std_value_t got = vm_repl_table_get(table, key); + vm_obj_t got = vm_repl_table_get(table, key); return got.tag != VM_TAG_NIL && (got.tag != VM_TAG_BOOL || got.value.b); } @@ -174,7 +174,7 @@ void vm_repl(vm_t *vm) { vm_block_t *entry = vm_compile(vm, input); - vm_std_value_t value = vm_run_repl(vm, entry); + vm_obj_t value = vm_run_repl(vm, entry); if (value.tag == VM_TAG_ERROR) { fprintf(stderr, "error: %s\n", value.value.str); @@ -183,7 +183,7 @@ void vm_repl(vm_t *vm) { vm_io_debug(&buf, 0, "", value, NULL); printf("%.*s", (int)buf.len, buf.buf); } - + if (vm->save_file != NULL) { vm_save_t save = vm_save_value(vm); FILE *f = fopen(vm->save_file, "wb"); diff --git a/vm/obj.c b/vm/obj.c index 54de4d8a..01546953 100644 --- a/vm/obj.c +++ b/vm/obj.c @@ -1,6 +1,6 @@ #include "./obj.h" -int64_t vm_value_to_i64(vm_std_value_t arg) { +int64_t vm_value_to_i64(vm_obj_t arg) { switch (arg.tag) { case VM_TAG_I8: { return (int64_t)arg.value.i8; @@ -26,7 +26,7 @@ int64_t vm_value_to_i64(vm_std_value_t arg) { } } -double vm_value_to_f64(vm_std_value_t arg) { +double vm_value_to_f64(vm_obj_t arg) { switch (arg.tag) { case VM_TAG_I8: { return (double)arg.value.i8; @@ -52,11 +52,11 @@ double vm_value_to_f64(vm_std_value_t arg) { } } -bool vm_value_can_to_n64(vm_std_value_t val) { +bool vm_value_can_to_n64(vm_obj_t val) { return val.tag == VM_TAG_I8 || val.tag == VM_TAG_I16 || val.tag == VM_TAG_I32 || val.tag == VM_TAG_I64 || val.tag == VM_TAG_F32 || val.tag == VM_TAG_F64; } -bool vm_value_is_int(vm_std_value_t val) { +bool vm_value_is_int(vm_obj_t val) { switch (val.tag) { case VM_TAG_I8: { return true; @@ -90,7 +90,7 @@ bool vm_value_is_int(vm_std_value_t val) { } } -bool vm_obj_eq(vm_std_value_t lhs, vm_std_value_t rhs) { +bool vm_obj_eq(vm_obj_t lhs, vm_obj_t rhs) { switch (lhs.tag) { case VM_TAG_NIL: { return rhs.tag == VM_TAG_NIL; @@ -260,7 +260,7 @@ bool vm_obj_eq(vm_std_value_t lhs, vm_std_value_t rhs) { } } -size_t vm_value_hash(vm_std_value_t value) { +size_t vm_value_hash(vm_obj_t value) { switch (value.tag) { case VM_TAG_NIL: { return SIZE_MAX - 2; @@ -334,7 +334,7 @@ vm_table_t *vm_table_new(void) { } vm_table_pair_t *vm_table_lookup(vm_table_t *table, vm_value_t key_val, vm_tag_t key_tag) { - vm_std_value_t key = (vm_std_value_t){ + vm_obj_t key = (vm_obj_t){ .tag = key_tag, .value = key_val, }; @@ -344,7 +344,7 @@ vm_table_pair_t *vm_table_lookup(vm_table_t *table, vm_value_t key_val, vm_tag_t size_t next = stop; do { vm_table_pair_t *pair = &table->pairs[next]; - vm_std_value_t value = (vm_std_value_t){ + vm_obj_t value = (vm_obj_t){ .tag = pair->key_tag, .value = pair->key_val, }; @@ -368,7 +368,7 @@ void vm_table_set(vm_table_t *table, vm_value_t key_val, vm_value_t val_val, vm_ if (table->alloc == 0) { return; } - vm_std_value_t key = (vm_std_value_t){ + vm_obj_t key = (vm_obj_t){ .tag = key_tag, .value = key_val, }; @@ -381,7 +381,7 @@ void vm_table_set(vm_table_t *table, vm_value_t key_val, vm_value_t val_val, vm_ if (pair->key_tag == VM_TAG_UNK || pair->key_tag == VM_TAG_NIL) { break; } - vm_std_value_t check = (vm_std_value_t){ + vm_obj_t check = (vm_obj_t){ .tag = pair->key_tag, .value = pair->key_val, }; @@ -421,7 +421,7 @@ void vm_table_set(vm_table_t *table, vm_value_t key_val, vm_value_t val_val, vm_ .val_val = val_val, }; - vm_std_value_t nv = (vm_std_value_t) { + vm_obj_t nv = (vm_obj_t) { .tag = key_tag, .value = key_val, }; @@ -439,7 +439,7 @@ void vm_table_set(vm_table_t *table, vm_value_t key_val, vm_value_t val_val, vm_ .val_tag = val_tag, .val_val = val_val, }; - vm_std_value_t vlen = (vm_std_value_t){ + vm_obj_t vlen = (vm_obj_t){ .tag = VM_TAG_I32, .value.i32 = table->len + 1, }; diff --git a/vm/obj.h b/vm/obj.h index 88606fe0..2d57c1e6 100644 --- a/vm/obj.h +++ b/vm/obj.h @@ -6,11 +6,11 @@ struct vm_table_pair_t; typedef struct vm_table_pair_t vm_table_pair_t; -bool vm_obj_eq(vm_std_value_t lhs, vm_std_value_t rhs); -bool vm_value_is_int(vm_std_value_t val); -int64_t vm_value_to_i64(vm_std_value_t arg); -double vm_value_to_f64(vm_std_value_t arg); -bool vm_value_can_to_n64(vm_std_value_t val); +bool vm_obj_eq(vm_obj_t lhs, vm_obj_t rhs); +bool vm_value_is_int(vm_obj_t val); +int64_t vm_value_to_i64(vm_obj_t arg); +double vm_value_to_f64(vm_obj_t arg); +bool vm_value_can_to_n64(vm_obj_t val); void vm_free_table(vm_table_t *table); vm_table_t *vm_table_new(void); @@ -35,42 +35,42 @@ void vm_table_get_pair(vm_table_t *table, vm_table_pair_t *pair); #define VM_VALUE_LITERAL_TYPE_TO_TAG_CONCAT2_IMPL(X_, Y_) X_##Y_ #define VM_VALUE_LITERAL_TYPE_TO_TAG_CONCAT2(X_, Y_) VM_VALUE_LITERAL_TYPE_TO_TAG_CONCAT2_IMPL(X_, Y_) -#define VM_STD_VALUE_LITERAL(TYPE_, VALUE_) \ - (vm_std_value_t) { \ +#define VM_OBJ_LITERAL(TYPE_, VALUE_) \ + (vm_obj_t) { \ .tag = VM_VALUE_LITERAL_TYPE_TO_TAG_CONCAT2(VM_VALUE_LITERAL_TYPE_TO_TAG_, TYPE_)(), \ .value = (vm_value_t){ \ .TYPE_ = (VALUE_), \ }, \ } -#define VM_STD_VALUE_NIL ((vm_std_value_t){.tag = (VM_TAG_NIL)}) +#define VM_OBJ_NIL ((vm_obj_t){.tag = (VM_TAG_NIL)}) -#define VM_STD_VALUE_NUMBER(CONFIG_, VALUE_) ({ \ +#define VM_OBJ_NUMBER(CONFIG_, VALUE_) ({ \ vm_t *config_ = (CONFIG_); \ - vm_std_value_t ret_; \ + vm_obj_t ret_; \ switch (config_->use_num) { \ case VM_USE_NUM_I8: { \ - ret_ = VM_STD_VALUE_LITERAL(i8, (int8_t)(VALUE_)); \ + ret_ = VM_OBJ_LITERAL(i8, (int8_t)(VALUE_)); \ break; \ } \ case VM_USE_NUM_I16: { \ - ret_ = VM_STD_VALUE_LITERAL(i16, (int16_t)(VALUE_)); \ + ret_ = VM_OBJ_LITERAL(i16, (int16_t)(VALUE_)); \ break; \ } \ case VM_USE_NUM_I32: { \ - ret_ = VM_STD_VALUE_LITERAL(i32, (int32_t)(VALUE_)); \ + ret_ = VM_OBJ_LITERAL(i32, (int32_t)(VALUE_)); \ break; \ } \ case VM_USE_NUM_I64: { \ - ret_ = VM_STD_VALUE_LITERAL(i64, (int64_t)(VALUE_)); \ + ret_ = VM_OBJ_LITERAL(i64, (int64_t)(VALUE_)); \ break; \ } \ case VM_USE_NUM_F32: { \ - ret_ = VM_STD_VALUE_LITERAL(f32, (float)(VALUE_)); \ + ret_ = VM_OBJ_LITERAL(f32, (float)(VALUE_)); \ break; \ } \ case VM_USE_NUM_F64: { \ - ret_ = VM_STD_VALUE_LITERAL(f64, (double)(VALUE_)); \ + ret_ = VM_OBJ_LITERAL(f64, (double)(VALUE_)); \ break; \ } \ } \ @@ -79,15 +79,15 @@ void vm_table_get_pair(vm_table_t *table, vm_table_pair_t *pair); #define VM_TABLE_SET_VALUE(TABLE_, KEY_, VALUE_) ({ \ vm_table_t *table_ = (TABLE_); \ - vm_std_value_t key_ = (KEY_); \ - vm_std_value_t value_ = (VALUE_); \ + vm_obj_t key_ = (KEY_); \ + vm_obj_t value_ = (VALUE_); \ vm_table_set(table_, key_.value, value_.value, key_.tag, value_.tag); \ }) #define VM_TABLE_SET(TABLE_, KEY_TYPE_, KEY_, VALUE_TYPE_, VALUE_) ({ \ vm_table_t *table_ = (TABLE_); \ - vm_std_value_t key_ = VM_STD_VALUE_LITERAL(KEY_TYPE_, KEY_); \ - vm_std_value_t value_ = VM_STD_VALUE_LITERAL(VALUE_TYPE_, VALUE_); \ + vm_obj_t key_ = VM_OBJ_LITERAL(KEY_TYPE_, KEY_); \ + vm_obj_t value_ = VM_OBJ_LITERAL(VALUE_TYPE_, VALUE_); \ vm_table_set(table_, key_.value, value_.value, key_.tag, value_.tag); \ }) diff --git a/vm/save/read.c b/vm/save/read.c index 609f95bf..4809c0ea 100644 --- a/vm/save/read.c +++ b/vm/save/read.c @@ -14,7 +14,7 @@ typedef struct vm_save_value_t vm_save_value_t; struct vm_save_value_t { size_t start; - vm_std_value_t value; + vm_obj_t value; }; struct vm_save_read_t { @@ -144,8 +144,8 @@ void vm_load_value(vm_t *vm, vm_save_t save) { } case VM_TAG_CLOSURE: { uint64_t len = vm_save_read_uleb(&read); - vm_std_value_t *closure = vm_malloc(sizeof(vm_std_value_t) * (len + 1)); - closure[0] = (vm_std_value_t){ + vm_obj_t *closure = vm_malloc(sizeof(vm_obj_t) * (len + 1)); + closure[0] = (vm_obj_t){ .tag = VM_TAG_I32, .value.i32 = (int32_t)(uint32_t)len, }; @@ -177,7 +177,7 @@ void vm_load_value(vm_t *vm, vm_save_t save) { } read.values.ptr[read.values.len++] = (vm_save_value_t){ .start = start, - .value = (vm_std_value_t){ + .value = (vm_obj_t){ .tag = tag, .value = value, }, @@ -193,7 +193,7 @@ outer:; switch (tag) { case VM_TAG_CLOSURE: { uint64_t len = vm_save_read_uleb(&read); - vm_std_value_t *closure = value.closure; + vm_obj_t *closure = value.closure; for (uint64_t i = 0; i < len; i++) { size_t value_index = vm_save_read_uleb(&read); closure[i] = read.values.ptr[value_index].value; diff --git a/vm/save/value.h b/vm/save/value.h index 20849a01..06fbcbd8 100644 --- a/vm/save/value.h +++ b/vm/save/value.h @@ -8,7 +8,7 @@ typedef struct vm_save_loaded_t vm_save_loaded_t; struct vm_save_loaded_t { vm_blocks_t *blocks; - vm_std_value_t env; + vm_obj_t env; }; diff --git a/vm/save/write.c b/vm/save/write.c index 552ccf3b..23b2083a 100644 --- a/vm/save/write.c +++ b/vm/save/write.c @@ -17,12 +17,12 @@ struct vm_save_write_t { struct { size_t read; size_t write; - vm_std_value_t *buf; + vm_obj_t *buf; size_t alloc; } values; }; -static size_t vm_save_write_push(vm_save_write_t *write, vm_std_value_t value) { +static size_t vm_save_write_push(vm_save_write_t *write, vm_obj_t value) { for (size_t i = 0; i < write->values.write; i++) { if (vm_obj_eq(write->values.buf[i], value)) { return i; @@ -31,13 +31,13 @@ static size_t vm_save_write_push(vm_save_write_t *write, vm_std_value_t value) { size_t index = write->values.write++; if (index + 1 >= write->values.alloc) { write->values.alloc = (index + 1) * 2; - write->values.buf = vm_realloc(write->values.buf, sizeof(vm_std_value_t) * write->values.alloc); + write->values.buf = vm_realloc(write->values.buf, sizeof(vm_obj_t) * write->values.alloc); } write->values.buf[index] = value; return index; } -static vm_std_value_t vm_save_write_shift(vm_save_write_t *write) { +static vm_obj_t vm_save_write_shift(vm_save_write_t *write) { return write->values.buf[write->values.read++]; } @@ -93,7 +93,7 @@ vm_save_t vm_save_value(vm_t *vm) { vm_save_write_push(&write, vm->std); while (!vm_save_write_is_done(&write)) { // printf("object #%zu at [0x%zX]\n", write.values.read, write.buf.len); - vm_std_value_t value = vm_save_write_shift(&write); + vm_obj_t value = vm_save_write_shift(&write); vm_save_write_byte(&write, value.tag); switch (value.tag) { case VM_TAG_UNK: { @@ -142,7 +142,7 @@ vm_save_t vm_save_value(vm_t *vm) { break; } case VM_TAG_CLOSURE: { - vm_std_value_t *closure = value.value.closure; + vm_obj_t *closure = value.value.closure; uint32_t len = closure[-1].value.i32; vm_save_write_uleb(&write, (uint64_t)len); for (uint32_t i = 0; i < len; i++) { @@ -158,8 +158,8 @@ vm_save_t vm_save_value(vm_t *vm) { for (size_t i = 0; i < len; i++) { vm_table_pair_t pair = table->pairs[i]; if (pair.key_tag != VM_TAG_UNK) { - vm_save_write_push(&write, (vm_std_value_t){.tag = pair.key_tag, .value = pair.key_val}); - vm_save_write_push(&write, (vm_std_value_t){.tag = pair.val_tag, .value = pair.val_val}); + vm_save_write_push(&write, (vm_obj_t){.tag = pair.key_tag, .value = pair.key_val}); + vm_save_write_push(&write, (vm_obj_t){.tag = pair.val_tag, .value = pair.val_val}); real += 1; } } @@ -167,8 +167,8 @@ vm_save_t vm_save_value(vm_t *vm) { for (size_t i = 0; i < len; i++) { vm_table_pair_t pair = table->pairs[i]; if (pair.key_tag != VM_TAG_UNK) { - size_t key = vm_save_write_push(&write, (vm_std_value_t){.tag = pair.key_tag, .value = pair.key_val}); - size_t value = vm_save_write_push(&write, (vm_std_value_t){.tag = pair.val_tag, .value = pair.val_val}); + size_t key = vm_save_write_push(&write, (vm_obj_t){.tag = pair.key_tag, .value = pair.key_val}); + size_t value = vm_save_write_push(&write, (vm_obj_t){.tag = pair.val_tag, .value = pair.val_val}); vm_save_write_uleb(&write, (uint64_t)key); vm_save_write_uleb(&write, (uint64_t)value); } diff --git a/vm/std.c b/vm/std.c index 7853c75e..3459af0f 100644 --- a/vm/std.c +++ b/vm/std.c @@ -9,7 +9,7 @@ #define VM_PAIR_VALUE(PAIR_) ({ \ vm_table_pair_t pair_ = (PAIR_); \ - (vm_std_value_t) { \ + (vm_obj_t) { \ .tag = pair_.val_tag, \ .value = pair_.val_val, \ }; \ @@ -18,8 +18,8 @@ #define VM_PAIR_PTR_VALUE(PPAIR_) ({ \ vm_table_pair_t *pair_ = (PPAIR_); \ pair_ == NULL \ - ? VM_STD_VALUE_NIL \ - : (vm_std_value_t) { \ + ? VM_OBJ_NIL \ + : (vm_obj_t) { \ .tag = pair_->val_tag, \ .value = pair_->val_val, \ }; \ @@ -39,16 +39,16 @@ static inline void vm_config_add_extern(vm_t *vm, void *value) { vm->externs = next; } -void vm_std_os_exit(vm_t *vm, vm_std_value_t *args) { +void vm_std_os_exit(vm_t *vm, vm_obj_t *args) { (void)vm; exit((int)vm_value_to_i64(args[0])); return; } -void vm_std_load(vm_t *vm, vm_std_value_t *args) { +void vm_std_load(vm_t *vm, vm_obj_t *args) { (void)vm; if (args[0].tag == VM_TAG_STR) { - *args = (vm_std_value_t){ + *args = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = "cannot load non-string value", }; @@ -56,75 +56,75 @@ void vm_std_load(vm_t *vm, vm_std_value_t *args) { const char *str = args[0].value.str; vm_block_t *entry = vm_compile(vm, str); - vm_std_value_t *vals = vm_malloc(sizeof(vm_std_value_t) * 2); - vals[0] = (vm_std_value_t){ + vm_obj_t *vals = vm_malloc(sizeof(vm_obj_t) * 2); + vals[0] = (vm_obj_t){ .tag = VM_TAG_I32, .value.i32 = 1, }; vals += 1; - vals[0] = (vm_std_value_t){ + vals[0] = (vm_obj_t){ .tag = VM_TAG_FUN, .value.i32 = (int32_t)entry->id, }; - *args = (vm_std_value_t){ + *args = (vm_obj_t){ .tag = VM_TAG_CLOSURE, .value.closure = vals, }; return; } -void vm_std_assert(vm_t *vm, vm_std_value_t *args) { +void vm_std_assert(vm_t *vm, vm_obj_t *args) { (void)vm; - vm_std_value_t val = args[0]; + vm_obj_t val = args[0]; if (val.tag == VM_TAG_NIL || (val.tag == VM_TAG_BOOL && !val.value.b)) { - vm_std_value_t msg = args[1]; + vm_obj_t msg = args[1]; vm_io_buffer_t buf = {0}; vm_io_debug(&buf, 0, "assert failed with mesage: ", msg, NULL); - *args = (vm_std_value_t){ + *args = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = buf.buf, }; return; } else { - *args = (vm_std_value_t){ + *args = (vm_obj_t){ .tag = VM_TAG_NIL, }; return; } } -void vm_std_error(vm_t *vm, vm_std_value_t *args) { +void vm_std_error(vm_t *vm, vm_obj_t *args) { if (args[0].tag == VM_TAG_STR) { - *args = (vm_std_value_t){ + *args = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = args[0].value.str, }; return; } - vm_std_value_t msg = args[0]; + vm_obj_t msg = args[0]; vm_io_buffer_t buf = {0}; vm_io_debug(&buf, 0, "", msg, NULL); - *args = (vm_std_value_t){ + *args = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = buf.buf, }; return; } -void vm_std_vm_closure(vm_t *vm, vm_std_value_t *args) { +void vm_std_vm_closure(vm_t *vm, vm_obj_t *args) { (void)vm; int64_t nargs = 0; for (size_t i = 0; args[i].tag != VM_TAG_UNK; i++) { nargs += 1; } if (nargs == 0 || args[0].tag != VM_TAG_FUN) { - *args = (vm_std_value_t){ + *args = (vm_obj_t){ .tag = VM_TAG_NIL, }; return; } - vm_std_value_t *vals = vm_malloc(sizeof(vm_std_value_t) * (nargs + 1)); - vals[0] = (vm_std_value_t){ + vm_obj_t *vals = vm_malloc(sizeof(vm_obj_t) * (nargs + 1)); + vals[0] = (vm_obj_t){ .tag = VM_TAG_I32, .value.i32 = (int32_t)nargs, }; @@ -132,26 +132,26 @@ void vm_std_vm_closure(vm_t *vm, vm_std_value_t *args) { for (size_t i = 0; args[i].tag != VM_TAG_UNK; i++) { vals[i] = args[i]; } - *args = (vm_std_value_t){ + *args = (vm_obj_t){ .tag = VM_TAG_CLOSURE, .value.closure = vals, }; return; } -void vm_std_vm_print(vm_t *vm, vm_std_value_t *args) { +void vm_std_vm_print(vm_t *vm, vm_obj_t *args) { (void)vm; for (size_t i = 0; args[i].tag != VM_TAG_UNK; i++) { vm_io_buffer_t buf = {0}; vm_io_debug(&buf, 0, "", args[i], NULL); printf("%.*s", (int)buf.len, buf.buf); } - args[0] = (vm_std_value_t){ + args[0] = (vm_obj_t){ .tag = VM_TAG_NIL, }; } -void vm_std_vm_concat(vm_t *vm, vm_std_value_t *args) { +void vm_std_vm_concat(vm_t *vm, vm_obj_t *args) { (void)vm; size_t len = 1; for (size_t i = 0; args[i].tag == VM_TAG_STR; i++) { @@ -168,17 +168,17 @@ void vm_std_vm_concat(vm_t *vm, vm_std_value_t *args) { head += len; } buf[len - 1] = '\0'; - *args = (vm_std_value_t){ + *args = (vm_obj_t){ .tag = VM_TAG_STR, .value.str = buf, }; } -void vm_std_math_rand_int(vm_t *vm, vm_std_value_t *args) { - args[0] = VM_STD_VALUE_NUMBER(vm, rand()); +void vm_std_math_rand_int(vm_t *vm, vm_obj_t *args) { + args[0] = VM_OBJ_NUMBER(vm, rand()); } -void vm_std_type(vm_t *vm, vm_std_value_t *args) { +void vm_std_type(vm_t *vm, vm_obj_t *args) { (void)vm; const char *ret = "unknown"; switch (args[0].tag) { @@ -239,77 +239,77 @@ void vm_std_type(vm_t *vm, vm_std_value_t *args) { break; } } - *args = (vm_std_value_t){ + *args = (vm_obj_t){ .tag = VM_TAG_STR, .value.str = ret, }; } -void vm_std_tostring(vm_t *vm, vm_std_value_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); - *args = (vm_std_value_t){ + *args = (vm_obj_t){ .tag = VM_TAG_STR, .value.str = out.buf, }; } -void vm_std_tonumber(vm_t *vm, vm_std_value_t *args) { +void vm_std_tonumber(vm_t *vm, vm_obj_t *args) { switch (args[0].tag) { case VM_TAG_I8: { - args[0] = VM_STD_VALUE_NUMBER(vm, args[0].value.i8); + args[0] = VM_OBJ_NUMBER(vm, args[0].value.i8); return; } case VM_TAG_I16: { - args[0] = VM_STD_VALUE_NUMBER(vm, args[0].value.i16); + args[0] = VM_OBJ_NUMBER(vm, args[0].value.i16); return; } case VM_TAG_I32: { - args[0] = VM_STD_VALUE_NUMBER(vm, args[0].value.i32); + args[0] = VM_OBJ_NUMBER(vm, args[0].value.i32); return; } case VM_TAG_I64: { - args[0] = VM_STD_VALUE_NUMBER(vm, args[0].value.i64); + args[0] = VM_OBJ_NUMBER(vm, args[0].value.i64); return; } case VM_TAG_F32: { - args[0] = VM_STD_VALUE_NUMBER(vm, args[0].value.f32); + args[0] = VM_OBJ_NUMBER(vm, args[0].value.f32); return; } case VM_TAG_F64: { - args[0] = VM_STD_VALUE_NUMBER(vm, args[0].value.f64); + args[0] = VM_OBJ_NUMBER(vm, args[0].value.f64); return; } case VM_TAG_STR: { if (vm->use_num == VM_USE_NUM_F32 || vm->use_num == VM_USE_NUM_F64) { double num; if (sscanf(args[0].value.str, "%lf", &num) == 0) { - args[0] = VM_STD_VALUE_NIL; + args[0] = VM_OBJ_NIL; return; } - args[0] = VM_STD_VALUE_NUMBER(vm, num); + args[0] = VM_OBJ_NUMBER(vm, num); return; } else { int64_t num; if (sscanf(args[0].value.str, "%" SCNi64, &num) == 0) { - args[0] = VM_STD_VALUE_NIL; + args[0] = VM_OBJ_NIL; return; } - args[0] = VM_STD_VALUE_NUMBER(vm, num); + args[0] = VM_OBJ_NUMBER(vm, num); return; } } default: { - args[0] = VM_STD_VALUE_NIL; + args[0] = VM_OBJ_NIL; return; } } } -void vm_std_print(vm_t *vm, vm_std_value_t *args) { +void vm_std_print(vm_t *vm, vm_obj_t *args) { (void)vm; - vm_std_value_t *ret = args; + vm_obj_t *ret = args; vm_io_buffer_t out = {0}; bool first = true; while (args->tag != VM_TAG_UNK) { @@ -320,31 +320,31 @@ void vm_std_print(vm_t *vm, vm_std_value_t *args) { first = false; } fprintf(stdout, "%.*s\n", (int)out.len, out.buf); - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_NIL, }; } -void vm_std_io_write(vm_t *vm, vm_std_value_t *args) { +void vm_std_io_write(vm_t *vm, vm_obj_t *args) { (void)vm; - vm_std_value_t *ret = args; + vm_obj_t *ret = args; vm_io_buffer_t out = {0}; while (args->tag != VM_TAG_UNK) { vm_value_buffer_tostring(&out, *args++); } fprintf(stdout, "%.*s", (int)out.len, out.buf); - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_NIL, }; } -void vm_std_string_format(vm_t *vm, vm_std_value_t *args) { +void vm_std_string_format(vm_t *vm, vm_obj_t *args) { (void)vm; - vm_std_value_t *ret = args; + vm_obj_t *ret = args; vm_io_buffer_t *out = vm_io_buffer_new(); - vm_std_value_t fmt = *args++; + vm_obj_t fmt = *args++; if (fmt.tag == VM_TAG_STR) { - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = "invalid format (not a string)", }; @@ -378,7 +378,7 @@ void vm_std_string_format(vm_t *vm, vm_std_value_t *args) { str++; } if ('0' < *str && *str <= '9') { - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = "invalid format (width > 99)", }; @@ -394,7 +394,7 @@ void vm_std_string_format(vm_t *vm, vm_std_value_t *args) { } } if ('0' < *str && *str <= '9') { - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = "invalid format (precision > 99)", }; @@ -402,7 +402,7 @@ void vm_std_string_format(vm_t *vm, vm_std_value_t *args) { } ptrdiff_t len = str - head; if (!(0 < len || len > 48)) { - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = "invalid format (too long to handle)", }; @@ -410,9 +410,9 @@ void vm_std_string_format(vm_t *vm, vm_std_value_t *args) { } char format[64]; strncpy(format, head, str - head); - vm_std_value_t arg = *args++; + vm_obj_t arg = *args++; if (arg.tag == VM_TAG_UNK) { - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = "too few args", }; @@ -422,7 +422,7 @@ void vm_std_string_format(vm_t *vm, vm_std_value_t *args) { switch (fc) { case 'c': { if (!vm_value_can_to_n64(arg)) { - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = "expected a number for %c format", }; @@ -435,7 +435,7 @@ void vm_std_string_format(vm_t *vm, vm_std_value_t *args) { case 'd': case 'i': { if (!vm_value_can_to_n64(arg)) { - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = "expected a number for integer format", }; @@ -447,7 +447,7 @@ void vm_std_string_format(vm_t *vm, vm_std_value_t *args) { } case 'o': { if (!vm_value_can_to_n64(arg)) { - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = "expected a number for %o format", }; @@ -459,7 +459,7 @@ void vm_std_string_format(vm_t *vm, vm_std_value_t *args) { } case 'u': { if (!vm_value_can_to_n64(arg)) { - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = "expected a number for %u format", }; @@ -471,7 +471,7 @@ void vm_std_string_format(vm_t *vm, vm_std_value_t *args) { } case 'x': { if (!vm_value_can_to_n64(arg)) { - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = "expected a number for %x format", }; @@ -483,7 +483,7 @@ void vm_std_string_format(vm_t *vm, vm_std_value_t *args) { } case 'X': { if (!vm_value_can_to_n64(arg)) { - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = "expected a number for %X format", }; @@ -500,7 +500,7 @@ void vm_std_string_format(vm_t *vm, vm_std_value_t *args) { case 'g': case 'G': { if (!vm_value_can_to_n64(arg)) { - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = "expected a number for float format", }; @@ -512,7 +512,7 @@ void vm_std_string_format(vm_t *vm, vm_std_value_t *args) { break; } case 'q': { - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = "unimplemented %q", }; @@ -526,7 +526,7 @@ void vm_std_string_format(vm_t *vm, vm_std_value_t *args) { strcpy(&format[len], "f"); vm_io_buffer_format(out, format, vm_value_to_f64(arg)); } else { - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = "unimplemented %s for a type", }; @@ -535,7 +535,7 @@ void vm_std_string_format(vm_t *vm, vm_std_value_t *args) { break; } default: { - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = "unknown format type", }; @@ -544,7 +544,7 @@ void vm_std_string_format(vm_t *vm, vm_std_value_t *args) { } } } - *ret = (vm_std_value_t){ + *ret = (vm_obj_t){ .tag = VM_TAG_STR, .value.str = vm_io_buffer_get(out), }; @@ -553,20 +553,20 @@ void vm_std_string_format(vm_t *vm, vm_std_value_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(); if (prog != NULL) { - VM_TABLE_SET_VALUE(arg, VM_STD_VALUE_NUMBER(vm, -1), VM_STD_VALUE_LITERAL(str, prog)); + VM_TABLE_SET_VALUE(arg, VM_OBJ_NUMBER(vm, -1), VM_OBJ_LITERAL(str, prog)); } if (file != NULL) { - VM_TABLE_SET_VALUE(arg, VM_STD_VALUE_NUMBER(vm, 0), VM_STD_VALUE_LITERAL(str, file)); + VM_TABLE_SET_VALUE(arg, VM_OBJ_NUMBER(vm, 0), VM_OBJ_LITERAL(str, file)); } for (int64_t i = 0; i < argc; i++) { - VM_TABLE_SET_VALUE(arg, VM_STD_VALUE_NUMBER(vm, i + 1), VM_STD_VALUE_LITERAL(str, argv[i])); + VM_TABLE_SET_VALUE(arg, VM_OBJ_NUMBER(vm, i + 1), VM_OBJ_LITERAL(str, argv[i])); } VM_TABLE_SET(vm->std.value.table, str, "arg", table, arg); } -void vm_std_vm_typename(vm_t *vm, vm_std_value_t *args) { +void vm_std_vm_typename(vm_t *vm, vm_obj_t *args) { if (args[0].tag != VM_TAG_STR) { - *args = (vm_std_value_t){ + *args = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = "vm.type: expected string", }; @@ -574,68 +574,68 @@ void vm_std_vm_typename(vm_t *vm, vm_std_value_t *args) { } const char *str = args[0].value.str; if (!strcmp(str, "nil")) { - *args = VM_STD_VALUE_NUMBER(vm, VM_TAG_NIL); + *args = VM_OBJ_NUMBER(vm, VM_TAG_NIL); return; } if (!strcmp(str, "bool")) { - *args = VM_STD_VALUE_NUMBER(vm, VM_TAG_BOOL); + *args = VM_OBJ_NUMBER(vm, VM_TAG_BOOL); return; } if (!strcmp(str, "i8")) { - *args = VM_STD_VALUE_NUMBER(vm, VM_TAG_I8); + *args = VM_OBJ_NUMBER(vm, VM_TAG_I8); return; } if (!strcmp(str, "i16")) { - *args = VM_STD_VALUE_NUMBER(vm, VM_TAG_I16); + *args = VM_OBJ_NUMBER(vm, VM_TAG_I16); return; } if (!strcmp(str, "i32")) { - *args = VM_STD_VALUE_NUMBER(vm, VM_TAG_I32); + *args = VM_OBJ_NUMBER(vm, VM_TAG_I32); return; } if (!strcmp(str, "i64")) { - *args = VM_STD_VALUE_NUMBER(vm, VM_TAG_I64); + *args = VM_OBJ_NUMBER(vm, VM_TAG_I64); return; } if (!strcmp(str, "f32")) { - *args = VM_STD_VALUE_NUMBER(vm, VM_TAG_F32); + *args = VM_OBJ_NUMBER(vm, VM_TAG_F32); return; } if (!strcmp(str, "f64")) { - *args = VM_STD_VALUE_NUMBER(vm, VM_TAG_F64); + *args = VM_OBJ_NUMBER(vm, VM_TAG_F64); return; } if (!strcmp(str, "str")) { - *args = VM_STD_VALUE_NUMBER(vm, VM_TAG_STR); + *args = VM_OBJ_NUMBER(vm, VM_TAG_STR); return; } if (!strcmp(str, "tab")) { - *args = VM_STD_VALUE_NUMBER(vm, VM_TAG_TAB); + *args = VM_OBJ_NUMBER(vm, VM_TAG_TAB); return; } if (!strcmp(str, "vm")) { - *args = VM_STD_VALUE_NUMBER(vm, VM_TAG_CLOSURE); + *args = VM_OBJ_NUMBER(vm, VM_TAG_CLOSURE); return; } if (!strcmp(str, "ffi")) { - *args = VM_STD_VALUE_NUMBER(vm, VM_TAG_FFI); + *args = VM_OBJ_NUMBER(vm, VM_TAG_FFI); return; } if (!strcmp(str, "error")) { - *args = VM_STD_VALUE_NUMBER(vm, VM_TAG_ERROR); + *args = VM_OBJ_NUMBER(vm, VM_TAG_ERROR); return; } - *args = VM_STD_VALUE_NIL; + *args = VM_OBJ_NIL; return; } -void vm_std_vm_typeof(vm_t *vm, vm_std_value_t *args) { - args[0] = VM_STD_VALUE_NUMBER(vm, args[0].tag); +void vm_std_vm_typeof(vm_t *vm, vm_obj_t *args) { + args[0] = VM_OBJ_NUMBER(vm, args[0].tag); } -void vm_std_table_keys(vm_t *vm, vm_std_value_t *args) { +void vm_std_table_keys(vm_t *vm, vm_obj_t *args) { if (args[0].tag != VM_TAG_TAB) { - *args = (vm_std_value_t){ + *args = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = "table.values: expect a table", }; @@ -648,8 +648,8 @@ void vm_std_table_keys(vm_t *vm, vm_std_value_t *args) { for (size_t i = 0; i < len; i++) { vm_table_pair_t *pair = &tab->pairs[i]; if (pair->key_tag != VM_TAG_UNK) { - vm_std_value_t key = VM_STD_VALUE_NUMBER(vm, write_head); - vm_std_value_t value = (vm_std_value_t){ + vm_obj_t key = VM_OBJ_NUMBER(vm, write_head); + vm_obj_t value = (vm_obj_t){ .tag = pair->key_tag, .value = pair->key_val, }; @@ -657,16 +657,16 @@ void vm_std_table_keys(vm_t *vm, vm_std_value_t *args) { write_head++; } } - *args = (vm_std_value_t){ + *args = (vm_obj_t){ .tag = VM_TAG_TAB, .value.table = ret, }; return; } -void vm_std_table_values(vm_t *vm, vm_std_value_t *args) { +void vm_std_table_values(vm_t *vm, vm_obj_t *args) { if (args[0].tag != VM_TAG_TAB) { - *args = (vm_std_value_t){ + *args = (vm_obj_t){ .tag = VM_TAG_ERROR, .value.str = "table.values: expect a table", }; @@ -679,8 +679,8 @@ void vm_std_table_values(vm_t *vm, vm_std_value_t *args) { for (size_t i = 0; i < len; i++) { vm_table_pair_t *pair = &tab->pairs[i]; if (pair->key_tag != VM_TAG_UNK) { - vm_std_value_t key = VM_STD_VALUE_NUMBER(vm, write_head); - vm_std_value_t value = (vm_std_value_t){ + vm_obj_t key = VM_OBJ_NUMBER(vm, write_head); + vm_obj_t value = (vm_obj_t){ .tag = pair->val_tag, .value = pair->val_val, }; @@ -688,18 +688,18 @@ void vm_std_table_values(vm_t *vm, vm_std_value_t *args) { write_head++; } } - *args = (vm_std_value_t){ + *args = (vm_obj_t){ .tag = VM_TAG_TAB, .value.table = ret, }; return; } -void vm_lua_comp_op_std_pow(vm_t *vm, vm_std_value_t *args); +void vm_lua_comp_op_std_pow(vm_t *vm, vm_obj_t *args); -void vm_std_vm_import(vm_t *vm, vm_std_value_t *args) { +void vm_std_vm_import(vm_t *vm, vm_obj_t *args) { if (args[0].tag != VM_TAG_STR) { - args[0] = (vm_std_value_t) { + args[0] = (vm_obj_t) { .tag = VM_TAG_ERROR, .value.str = "import() must take a string" }; @@ -707,7 +707,7 @@ void vm_std_vm_import(vm_t *vm, vm_std_value_t *args) { } const char *src = vm_io_read(args[0].value.str); if (src == NULL) { - args[0] = (vm_std_value_t) { + args[0] = (vm_obj_t) { .tag = VM_TAG_ERROR, .value.str = "import() no such file", }; @@ -738,7 +738,7 @@ static int vm_std_app_repl(void *arg) { return 0; } -static Color vm_value_to_color(vm_std_value_t arg) { +static Color vm_value_to_color(vm_obj_t arg) { if (arg.tag == VM_TAG_I8 || arg.tag == VM_TAG_I16 || arg.tag == VM_TAG_I32 || arg.tag == VM_TAG_I64 || arg.tag == VM_TAG_F32 || arg.tag == VM_TAG_F64) { uint8_t c = vm_value_to_i64(arg); return (Color) { @@ -763,14 +763,14 @@ static Color vm_value_to_color(vm_std_value_t arg) { } } -static Color vm_value_field_to_color(vm_std_value_t arg, const char *field) { +static Color vm_value_field_to_color(vm_obj_t arg, const char *field) { if (arg.tag != VM_TAG_TAB) { return BLACK; } return vm_value_to_color(VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(arg.value.table, field))); } -static Vector2 vm_value_to_vector2(vm_std_value_t arg) { +static Vector2 vm_value_to_vector2(vm_obj_t arg) { if (arg.tag != VM_TAG_TAB) { return (Vector2) {0, 0}; } @@ -782,19 +782,19 @@ static Vector2 vm_value_to_vector2(vm_std_value_t arg) { }; } -static Vector2 vm_value_field_to_vector2(vm_std_value_t arg, const char *field) { +static Vector2 vm_value_field_to_vector2(vm_obj_t arg, const char *field) { if (arg.tag != VM_TAG_TAB) { return (Vector2) {0, 0}; } return vm_value_to_vector2(VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(arg.value.table, field))); } -static void vm_std_app_draw_tree(vm_t *vm, Rectangle rect, vm_std_value_t arg); +static void vm_std_app_draw_tree(vm_t *vm, Rectangle rect, vm_obj_t arg); static void vm_std_app_draw_tree_children(vm_t *vm, Rectangle rect, vm_table_t *tree) { int32_t i = 1; while (true) { - vm_std_value_t child = VM_PAIR_PTR_VALUE(vm_table_lookup(tree, (vm_value_t) { .i32 = i }, VM_TAG_I32)); + vm_obj_t child = VM_PAIR_PTR_VALUE(vm_table_lookup(tree, (vm_value_t) { .i32 = i }, VM_TAG_I32)); if (child.tag != VM_TAG_TAB) { break; } @@ -817,7 +817,7 @@ static void vm_std_app_draw_tree_children_list(vm_t *vm, Rectangle rect, vm_tabl }; int32_t i = 1; while (true) { - vm_std_value_t child = VM_PAIR_PTR_VALUE(vm_table_lookup(tree, (vm_value_t) { .i32 = i }, VM_TAG_I32)); + vm_obj_t child = VM_PAIR_PTR_VALUE(vm_table_lookup(tree, (vm_value_t) { .i32 = i }, VM_TAG_I32)); if (child.tag != VM_TAG_TAB) { break; } @@ -841,7 +841,7 @@ static void vm_std_app_draw_tree_children_split(vm_t *vm, Rectangle rect, vm_tab }; int32_t i = 1; while (true) { - vm_std_value_t child = VM_PAIR_PTR_VALUE(vm_table_lookup(tree, (vm_value_t) { .i32 = i }, VM_TAG_I32)); + vm_obj_t child = VM_PAIR_PTR_VALUE(vm_table_lookup(tree, (vm_value_t) { .i32 = i }, VM_TAG_I32)); if (child.tag != VM_TAG_TAB) { break; } @@ -851,12 +851,12 @@ static void vm_std_app_draw_tree_children_split(vm_t *vm, Rectangle rect, vm_tab } } -static void vm_std_app_draw_tree(vm_t *vm, Rectangle rect, vm_std_value_t arg) { +static void vm_std_app_draw_tree(vm_t *vm, Rectangle rect, vm_obj_t arg) { if (arg.tag != VM_TAG_TAB) { return; } vm_table_t *tree = arg.value.table; - vm_std_value_t std_type = VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(tree, "type")); + vm_obj_t std_type = VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(tree, "type")); if (std_type.tag == VM_TAG_NIL) { size_t n = (1 << tree->alloc); for (size_t i = 0; i < n; i++) { @@ -864,7 +864,7 @@ static void vm_std_app_draw_tree(vm_t *vm, Rectangle rect, vm_std_value_t arg) { if (pair->key_tag == VM_TAG_UNK) { continue; } - vm_std_app_draw_tree(vm, rect, (vm_std_value_t){ + vm_std_app_draw_tree(vm, rect, (vm_obj_t){ .tag = pair->val_tag, .value = pair->val_val, }); @@ -880,8 +880,8 @@ static void vm_std_app_draw_tree(vm_t *vm, Rectangle rect, vm_std_value_t arg) { vm_std_app_draw_tree_children_split(vm, rect, tree); } else if (!strcmp(type, "click")) { vm_std_app_draw_tree_children(vm, rect, tree); - vm_std_value_t button = VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(arg.value.table, "button")); - vm_std_value_t run = VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(arg.value.table, "run")); + vm_obj_t button = VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(arg.value.table, "button")); + vm_obj_t run = VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(arg.value.table, "run")); if (button.tag == VM_TAG_STR) { const char *name = button.value.str; if ((!strcmp(name, "left") && IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) @@ -894,8 +894,8 @@ static void vm_std_app_draw_tree(vm_t *vm, Rectangle rect, vm_std_value_t arg) { } } else if (!strcmp(type, "drag")) { vm_std_app_draw_tree_children(vm, rect, tree); - vm_std_value_t button = VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(arg.value.table, "button")); - vm_std_value_t run = VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(arg.value.table, "run")); + vm_obj_t button = VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(arg.value.table, "button")); + vm_obj_t run = VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(arg.value.table, "run")); if (button.tag == VM_TAG_STR) { const char *name = button.value.str; if ((!strcmp(name, "left") && IsMouseButtonDown(MOUSE_LEFT_BUTTON)) @@ -907,7 +907,7 @@ static void vm_std_app_draw_tree(vm_t *vm, Rectangle rect, vm_std_value_t arg) { } } } else if (!strcmp(type, "code")) { - vm_std_value_t run = VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(arg.value.table, "run")); + vm_obj_t run = VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(arg.value.table, "run")); if (run.tag == VM_TAG_CLOSURE) { vm->regs[0] = run; vm_run_repl(vm, vm->blocks->blocks[run.value.closure[0].value.i32]); @@ -917,8 +917,8 @@ static void vm_std_app_draw_tree(vm_t *vm, Rectangle rect, vm_std_value_t arg) { int64_t height = vm_value_to_i64(VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(arg.value.table, "height"))); SetWindowSize(width, height); } else if (!strcmp(type, "keydown") || !strcmp(type, "keyup") || !strcmp(type, "keypressed") || !strcmp(type, "keyreleased")) { - vm_std_value_t run = VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(arg.value.table, "run")); - vm_std_value_t key_obj = VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(arg.value.table, "key")); + vm_obj_t run = VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(arg.value.table, "run")); + vm_obj_t key_obj = VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(arg.value.table, "key")); if (key_obj.tag == VM_TAG_STR) { const char *key = key_obj.value.str; bool (*func)(int) = NULL; @@ -999,7 +999,7 @@ EM_JS(void, vm_std_app_frame_loop, (vm_t *vm), { void EMSCRIPTEN_KEEPALIVE vm_std_app_frame(vm_t *vm) { BeginDrawing(); ClearBackground(RAYWHITE); - vm_std_value_t tree = VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(vm->std.value.table, "draw")); + vm_obj_t tree = VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(vm->std.value.table, "draw")); Rectangle rect = (Rectangle) { 0, 0, @@ -1017,7 +1017,7 @@ void EMSCRIPTEN_KEEPALIVE vm_std_app_sync(vm_t *vm) { fclose(f); } -void vm_std_app_init(vm_t *vm, vm_std_value_t *args) { +void vm_std_app_init(vm_t *vm, vm_obj_t *args) { SetTraceLogLevel(LOG_WARNING); SetTargetFPS(60); InitWindow(960, 540, "MiniVM"); @@ -1031,12 +1031,12 @@ void vm_std_app_init(vm_t *vm, vm_std_value_t *args) { } } vm_std_app_frame_loop(vm); - args[0] = VM_STD_VALUE_NIL; + args[0] = VM_OBJ_NIL; // vm_std_app_repl(vm); } #else -void vm_std_app_init(vm_t *vm, vm_std_value_t *args) { +void vm_std_app_init(vm_t *vm, vm_obj_t *args) { SetTraceLogLevel(LOG_WARNING); SetTargetFPS(60); InitWindow(960, 540, "MiniVM"); @@ -1052,7 +1052,7 @@ void vm_std_app_init(vm_t *vm, vm_std_value_t *args) { while (!WindowShouldClose()) { BeginDrawing(); ClearBackground(RAYWHITE); - vm_std_value_t tree = VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(vm->std.value.table, "draw")); + vm_obj_t tree = VM_PAIR_PTR_VALUE(VM_TABLE_LOOKUP_STR(vm->std.value.table, "draw")); Rectangle rect = (Rectangle) { 0, 0, @@ -1144,7 +1144,7 @@ void vm_std_new(vm_t *vm) { } #endif - vm->std = (vm_std_value_t) { + vm->std = (vm_obj_t) { .tag = VM_TAG_TAB, .value.table = std, }; diff --git a/vm/std.h b/vm/std.h index dfe296ce..cdec9be1 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_std_value_t value); +void vm_value_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 27cacfcf..44063429 100644 --- a/vm/vm.h +++ b/vm/vm.h @@ -11,7 +11,7 @@ struct vm_t; struct vm_blocks_t; struct vm_externs_t; -struct vm_std_value_t; +struct vm_obj_t; union vm_value_t; struct vm_table_t; struct vm_table_pair_t; @@ -19,7 +19,7 @@ struct vm_table_pair_t; typedef struct vm_t vm_t; typedef struct vm_blocks_t vm_blocks_t; typedef struct vm_externs_t vm_externs_t; -typedef struct vm_std_value_t vm_std_value_t; +typedef struct vm_obj_t vm_obj_t; typedef union vm_value_t vm_value_t; typedef struct vm_table_t vm_table_t; typedef struct vm_table_pair_t vm_table_pair_t; @@ -54,6 +54,8 @@ enum { typedef uint8_t vm_tag_t; +typedef void vm_ffi_t(vm_t *closure, vm_obj_t *args); + union vm_value_t { void *all; bool b; @@ -65,11 +67,11 @@ union vm_value_t { double f64; const char *str; vm_table_t *table; - vm_std_value_t *closure; - void (*ffi)(vm_t *closure, vm_std_value_t *args); + vm_obj_t *closure; + vm_ffi_t *ffi; }; -struct vm_std_value_t { +struct vm_obj_t { vm_value_t value; vm_tag_t tag; }; @@ -101,11 +103,13 @@ struct vm_t { uint8_t use_num; - vm_std_value_t std; + vm_obj_t std; void *mutex; - vm_std_value_t *regs; + vm_obj_t *regs; + + bool dump_ir: 1; }; void vm_repl(vm_t *config);