diff options
author | Jo-Philipp Wich <jo@mein.io> | 2022-01-07 20:03:17 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2022-01-18 10:58:11 +0100 |
commit | 725bb75b7b66dd1e0a381908e831cede0402cb6e (patch) | |
tree | b8ee8c737198e5b5bad6b809457c59be2a7f8bb5 /vm.c | |
parent | 6b2e79af9fe6e7d05d31245fc9049540a96d5d31 (diff) |
compiler, vm: use a program wide constant list
Instead of storing constant values per function, maintain a global program
wide list for all constant values within the current compilation unit.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'vm.c')
-rw-r--r-- | vm.c | 41 |
1 files changed, 18 insertions, 23 deletions
@@ -24,6 +24,7 @@ #include "ucode/vm.h" #include "ucode/compiler.h" +#include "ucode/program.h" #include "ucode/lib.h" /* uc_error_context_format() */ #undef __insn @@ -201,16 +202,22 @@ uc_vm_frame_chunk(uc_callframe_t *frame) return frame->closure ? &frame->closure->function->chunk : NULL; } +static uc_program_t * +uc_vm_frame_program(uc_callframe_t *frame) +{ + return frame->closure ? frame->closure->function->program : NULL; +} + static uc_callframe_t * uc_vm_current_frame(uc_vm_t *vm) { return uc_vector_last(&vm->callframes); } -static uc_chunk_t * -uc_vm_current_chunk(uc_vm_t *vm) +static uc_program_t * +uc_vm_current_program(uc_vm_t *vm) { - return uc_vm_frame_chunk(uc_vm_current_frame(vm)); + return uc_vm_frame_program(uc_vm_current_frame(vm)); } static bool @@ -319,18 +326,6 @@ uc_vm_frame_dump(uc_vm_t *vm, uc_callframe_t *frame) uc_vm_format_val(vm, frame->ctx)); if (chunk) { - fprintf(stderr, " |- %zu constants\n", - chunk->constants.isize); - - for (i = 0; i < chunk->constants.isize; i++) { - v = uc_chunk_get_constant(chunk, i); - - fprintf(stderr, " | [%zu] %s\n", - i, uc_vm_format_val(vm, v)); - - ucv_put(v); - } - closure = frame->closure; function = closure->function; @@ -649,7 +644,7 @@ uc_dump_insn(uc_vm_t *vm, uint8_t *pos, uc_vm_insn_t insn) case I_LOAD: case I_LVAR: case I_SVAR: - cnst = uc_chunk_get_constant(uc_vm_frame_chunk(uc_vector_last(&vm->callframes)), vm->arg.u32); + cnst = uc_program_get_constant(uc_vm_frame_program(uc_vector_last(&vm->callframes)), vm->arg.u32); fprintf(stderr, "\t; %s", cnst ? uc_vm_format_val(vm, cnst) : "(?)"); @@ -676,7 +671,7 @@ uc_dump_insn(uc_vm_t *vm, uint8_t *pos, uc_vm_insn_t insn) case I_UVAR: if (!cnst) - cnst = uc_chunk_get_constant(uc_vm_frame_chunk(uc_vector_last(&vm->callframes)), vm->arg.u32 & 0x00ffffff); + cnst = uc_program_get_constant(uc_vm_frame_program(uc_vector_last(&vm->callframes)), vm->arg.u32 & 0x00ffffff); fprintf(stderr, "\t; %s (%s)", cnst ? uc_vm_format_val(vm, cnst) : "(?)", @@ -915,7 +910,7 @@ uc_vm_insn_load(uc_vm_t *vm, uc_vm_insn_t insn) { switch (insn) { case I_LOAD: - uc_vm_stack_push(vm, uc_chunk_get_constant(uc_vm_current_chunk(vm), vm->arg.u32)); + uc_vm_stack_push(vm, uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32)); break; case I_LOAD8: @@ -938,7 +933,7 @@ uc_vm_insn_load(uc_vm_t *vm, uc_vm_insn_t insn) static void uc_vm_insn_load_regexp(uc_vm_t *vm, uc_vm_insn_t insn) { - uc_value_t *re, *jstr = uc_chunk_get_constant(uc_vm_current_chunk(vm), vm->arg.u32); + uc_value_t *re, *jstr = uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32); bool icase = false, newline = false, global = false; char *str, *err = NULL; @@ -987,7 +982,7 @@ uc_vm_insn_load_var(uc_vm_t *vm, uc_vm_insn_t insn) bool found; scope = vm->globals; - name = uc_chunk_get_constant(uc_vm_current_chunk(vm), vm->arg.u32); + name = uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32); while (ucv_type(name) == UC_STRING) { val = ucv_object_get(scope, ucv_string_get(name), &found); @@ -1128,7 +1123,7 @@ static void uc_vm_insn_load_closure(uc_vm_t *vm, uc_vm_insn_t insn) { uc_callframe_t *frame = uc_vm_current_frame(vm); - uc_value_t *fno = uc_chunk_get_constant(uc_vm_current_chunk(vm), vm->arg.u32); + uc_value_t *fno = uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32); uc_function_t *function = (uc_function_t *)fno; uc_closure_t *closure = (uc_closure_t *)ucv_closure_new(vm, function, insn == I_ARFN); volatile int32_t uv; @@ -1163,7 +1158,7 @@ uc_vm_insn_store_var(uc_vm_t *vm, uc_vm_insn_t insn) bool found; scope = vm->globals; - name = uc_chunk_get_constant(uc_vm_current_chunk(vm), vm->arg.u32); + name = uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32); while (ucv_type(name) == UC_STRING) { ucv_object_get(scope, ucv_string_get(name), &found); @@ -1562,7 +1557,7 @@ uc_vm_insn_update_var(uc_vm_t *vm, uc_vm_insn_t insn) bool found; scope = vm->globals; - name = uc_chunk_get_constant(uc_vm_current_chunk(vm), vm->arg.u32 & 0x00FFFFFF); + name = uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32 & 0x00FFFFFF); assert(ucv_type(name) == UC_STRING); |