From 0a0e82f14920e8ec8a9594a64d24520c0dc7cbd3 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Thu, 17 Oct 2024 12:27:37 +0200 Subject: [PATCH] treewide: refactor vector usage code Utilize uc_vector_push() and uc_vector_foreach() where applicable. Signed-off-by: Jo-Philipp Wich --- chunk.c | 45 ++++++++++++---------------- compiler.c | 86 ++++++++++++++++++++++-------------------------------- source.c | 10 ++----- vm.c | 53 +++++++++++++++------------------ 4 files changed, 79 insertions(+), 115 deletions(-) diff --git a/chunk.c b/chunk.c index 5dbd1a19..63a70648 100644 --- a/chunk.c +++ b/chunk.c @@ -67,9 +67,7 @@ uc_chunk_add(uc_chunk_t *chunk, uint8_t byte, size_t offset) uc_offsetinfo_t *offsets = &chunk->debuginfo.offsets; size_t i; - uc_vector_grow(chunk); - - chunk->entries[chunk->count] = byte; + uc_vector_push(chunk, byte); /* offset info is encoded in bytes, for each byte, the first three bits * specify the number of source text bytes to advance since the last entry @@ -81,13 +79,11 @@ uc_chunk_add(uc_chunk_t *chunk, uint8_t byte, size_t offset) * instructions each */ for (i = offset; i > OFFSETINFO_MAX_BYTES; i -= OFFSETINFO_MAX_BYTES) { /* advance by 7 bytes */ - uc_vector_grow(offsets); - offsets->entries[offsets->count++] = OFFSETINFO_ENCODE(OFFSETINFO_MAX_BYTES, 0); + uc_vector_push(offsets, OFFSETINFO_ENCODE(OFFSETINFO_MAX_BYTES, 0)); } /* advance by `i` bytes, count one instruction */ - uc_vector_grow(offsets); - offsets->entries[offsets->count++] = OFFSETINFO_ENCODE(i, 1); + uc_vector_push(offsets, OFFSETINFO_ENCODE(i, 1)); } /* update instruction count at current offset entry */ @@ -97,18 +93,18 @@ uc_chunk_add(uc_chunk_t *chunk, uint8_t byte, size_t offset) * emit another offset entry with the initial three bits set to zero */ if (OFFSETINFO_NUM_INSNS(offsets->entries[offsets->count - 1]) >= OFFSETINFO_MAX_INSNS) { /* advance by 0 bytes, count one instruction */ - uc_vector_grow(offsets); - offsets->entries[offsets->count++] = OFFSETINFO_ENCODE(0, 1); + uc_vector_push(offsets, OFFSETINFO_ENCODE(0, 1)); } else { - offsets->entries[offsets->count - 1] = OFFSETINFO_ENCODE( - OFFSETINFO_NUM_BYTES(offsets->entries[offsets->count - 1]), - OFFSETINFO_NUM_INSNS(offsets->entries[offsets->count - 1]) + 1 - ); + uint8_t *prev = uc_vector_last(offsets); + + *prev = OFFSETINFO_ENCODE( + OFFSETINFO_NUM_BYTES(*prev), + OFFSETINFO_NUM_INSNS(*prev) + 1); } } - return chunk->count++; + return chunk->count - 1; } void @@ -124,10 +120,9 @@ uc_chunk_pop(uc_chunk_t *chunk) n_insns = OFFSETINFO_NUM_INSNS(offsets->entries[offsets->count - 1]); if (n_insns > 0) { - offsets->entries[offsets->count - 1] = OFFSETINFO_ENCODE( - OFFSETINFO_NUM_BYTES(offsets->entries[offsets->count - 1]), - n_insns - 1 - ); + uint8_t *prev = uc_vector_last(offsets); + + *prev = OFFSETINFO_ENCODE(OFFSETINFO_NUM_BYTES(*prev), n_insns - 1); } else { offsets->count--; @@ -162,14 +157,12 @@ uc_chunk_debug_add_variable(uc_chunk_t *chunk, size_t from, size_t to, size_t sl if (upval) slot += (size_t)-1 / 2; - uc_vector_grow(variables); - - variables->entries[variables->count].nameidx = uc_vallist_add(varnames, name); - variables->entries[variables->count].slot = slot; - variables->entries[variables->count].from = from; - variables->entries[variables->count].to = to; - - variables->count++; + uc_vector_push(variables, { + .nameidx = uc_vallist_add(varnames, name), + .slot = slot, + .from = from, + .to = to + }); } uc_value_t * diff --git a/compiler.c b/compiler.c index b64537a4..6e417d2b 100644 --- a/compiler.c +++ b/compiler.c @@ -823,14 +823,13 @@ uc_compiler_declare_local(uc_compiler_t *compiler, uc_value_t *name, bool consta } } - uc_vector_grow(locals); - - locals->entries[locals->count].name = ucv_get(name); - locals->entries[locals->count].depth = -1; - locals->entries[locals->count].captured = false; - locals->entries[locals->count].from = chunk->count; - locals->entries[locals->count].constant = constant; - locals->count++; + uc_vector_push(locals, { + .name = ucv_get(name), + .depth = -1, + .captured = false, + .from = chunk->count, + .constant = constant + }); return -1; } @@ -896,16 +895,16 @@ uc_compiler_add_upval(uc_compiler_t *compiler, size_t idx, bool local, uc_value_ return -1; } - uc_vector_grow(upvals); - - upvals->entries[upvals->count].local = local; - upvals->entries[upvals->count].index = idx; - upvals->entries[upvals->count].name = ucv_get(name); - upvals->entries[upvals->count].constant = constant; + uc_vector_push(upvals, { + .local = local, + .index = idx, + .name = ucv_get(name), + .constant = constant + }); function->nupvals++; - return upvals->count++; + return upvals->count - 1; } static ssize_t @@ -1618,10 +1617,8 @@ uc_compiler_compile_call(uc_compiler_t *compiler) if (!uc_compiler_parse_check(compiler, TK_RPAREN)) { do { /* if this is a spread arg, remember the argument index */ - if (uc_compiler_parse_match(compiler, TK_ELLIP)) { - uc_vector_grow(&spreads); - spreads.entries[spreads.count++] = nargs; - } + if (uc_compiler_parse_match(compiler, TK_ELLIP)) + uc_vector_push(&spreads, nargs); /* compile argument expression */ uc_compiler_parse_precedence(compiler, P_ASSIGN); @@ -2175,14 +2172,14 @@ uc_compiler_declare_internal(uc_compiler_t *compiler, size_t srcpos, const char uc_chunk_t *chunk = uc_compiler_current_chunk(compiler); uc_locals_t *locals = &compiler->locals; - uc_vector_grow(locals); + uc_vector_push(locals, { + .name = ucv_string_new(name), + .depth = compiler->scope_depth, + .captured = false, + .from = chunk->count + }); - locals->entries[locals->count].name = ucv_string_new(name); - locals->entries[locals->count].depth = compiler->scope_depth; - locals->entries[locals->count].captured = false; - locals->entries[locals->count].from = chunk->count; - - return locals->count++; + return locals->count - 1; } static void @@ -2292,8 +2289,7 @@ uc_compiler_compile_if(uc_compiler_t *compiler) /* we just compiled an elsif block */ if (!expect_endif && type == TK_ELIF) { /* emit jump to skip to the end */ - uc_vector_grow(&elifs); - elifs.entries[elifs.count++] = uc_compiler_emit_jmp(compiler, 0); + uc_vector_push(&elifs, uc_compiler_emit_jmp(compiler, 0)); /* point previous conditional jump to beginning of branch */ uc_compiler_set_jmpaddr(compiler, jmpz_off, chunk->count); @@ -2310,8 +2306,7 @@ uc_compiler_compile_if(uc_compiler_t *compiler) } else if (!expect_endif && type == TK_ELSE) { /* emit jump to skip to the end */ - uc_vector_grow(&elifs); - elifs.entries[elifs.count++] = uc_compiler_emit_jmp(compiler, 0); + uc_vector_push(&elifs, uc_compiler_emit_jmp(compiler, 0)); /* point previous conditional jump to beginning of branch */ uc_compiler_set_jmpaddr(compiler, jmpz_off, chunk->count); @@ -2746,13 +2741,9 @@ uc_compiler_compile_switch(uc_compiler_t *compiler) * For the `default` case, beginning and end offsets of the * condition expression are equal. */ - uc_vector_grow(&cases); + uc_vector_extend(&cases, 3); cases.entries[cases.count++] = (locals->count - 1) - value_slot; - - uc_vector_grow(&cases); cases.entries[cases.count++] = chunk->count; - - uc_vector_grow(&cases); cases.entries[cases.count++] = chunk->count; } @@ -2770,13 +2761,9 @@ uc_compiler_compile_switch(uc_compiler_t *compiler) * 2) beginning of condition expression * 3) end of condition expression */ - uc_vector_grow(&cases); + uc_vector_extend(&cases, 3); cases.entries[cases.count++] = (locals->count - 1) - value_slot; - - uc_vector_grow(&cases); cases.entries[cases.count++] = skip_jmp + 5; - - uc_vector_grow(&cases); cases.entries[cases.count++] = uc_compiler_emit_jmp(compiler, 0); /* patch jump skipping over the case value */ @@ -2911,13 +2898,12 @@ uc_compiler_compile_try(uc_compiler_t *compiler) /* Catch block ---------------------------------------------------------- */ if (try_to > try_from) { - uc_vector_grow(ranges); - - ranges->entries[ranges->count].from = try_from; - ranges->entries[ranges->count].to = try_to; - ranges->entries[ranges->count].target = chunk->count; - ranges->entries[ranges->count].slot = ehvar_slot; - ranges->count++; + uc_vector_push(ranges, { + .from = try_from, + .to = try_to, + .target = chunk->count, + .slot = ehvar_slot + }); } uc_compiler_enter_scope(compiler); @@ -2983,10 +2969,8 @@ uc_compiler_compile_control(uc_compiler_t *compiler) uc_compiler_emit_insn(compiler, 0, locals->entries[i - 1].captured ? I_CUPV : I_POP); - uc_vector_grow(p); - - p->entries[p->count++] = - uc_compiler_emit_jmp_dest(compiler, pos, chunk->count + type); + uc_vector_push(p, + uc_compiler_emit_jmp_dest(compiler, pos, chunk->count + type)); uc_compiler_parse_consume(compiler, TK_SCOL); } diff --git a/source.c b/source.c index 39295f60..e23c24da 100644 --- a/source.c +++ b/source.c @@ -157,10 +157,7 @@ uc_source_type_test(uc_source_t *source) void uc_source_line_next(uc_source_t *source) { - uc_lineinfo_t *lines = &source->lineinfo; - - uc_vector_grow(lines); - lines->entries[lines->count++] = 0x80; + uc_vector_push(&source->lineinfo, 0x80); } void @@ -183,11 +180,8 @@ uc_source_line_update(uc_source_t *source, size_t off) while (off > 0) { n = (off > 0x7f) ? 0x7f : off; - uc_vector_grow(lines); - entry = uc_vector_last(lines); - entry[1] = n; + uc_vector_push(lines, n); off -= n; - lines->count++; } } } diff --git a/vm.c b/vm.c index bb6dc2f2..e4fe45db 100644 --- a/vm.c +++ b/vm.c @@ -431,11 +431,7 @@ uc_vm_resolve_upval(uc_vm_t *vm, uc_value_t *value) void uc_vm_stack_push(uc_vm_t *vm, uc_value_t *value) { - uc_vector_grow(&vm->stack); - - ucv_put(vm->stack.entries[vm->stack.count]); - vm->stack.entries[vm->stack.count] = uc_vm_resolve_upval(vm, value); - vm->stack.count++; + uc_vector_push(&vm->stack, value); if (vm->trace) { fprintf(stderr, " [+%zd] %s\n", @@ -488,14 +484,13 @@ uc_vm_call_native(uc_vm_t *vm, uc_value_t *ctx, uc_cfunction_t *fptr, bool mcall uc_callframe_t *frame; /* add new callframe */ - uc_vector_grow(&vm->callframes); - - frame = &vm->callframes.entries[vm->callframes.count++]; - frame->stackframe = vm->stack.count - nargs - 1; - frame->cfunction = fptr; - frame->closure = NULL; - frame->ctx = ctx; - frame->mcall = mcall; + frame = uc_vector_push(&vm->callframes, { + .stackframe = vm->stack.count - nargs - 1, + .cfunction = fptr, + .closure = NULL, + .ctx = ctx, + .mcall = mcall + }); if (vm->trace) uc_vm_frame_dump(vm, frame); @@ -641,16 +636,15 @@ uc_vm_call_function(uc_vm_t *vm, uc_value_t *ctx, uc_value_t *fno, bool mcall, s } } - uc_vector_grow(&vm->callframes); - - frame = &vm->callframes.entries[vm->callframes.count++]; - frame->stackframe = stackoff; - frame->cfunction = NULL; - frame->closure = closure; - frame->ctx = ctx; - frame->ip = function->chunk.entries; - frame->mcall = mcall; - frame->strict = function->strict; + frame = uc_vector_push(&vm->callframes, { + .stackframe = stackoff, + .cfunction = NULL, + .closure = closure, + .ctx = ctx, + .ip = function->chunk.entries, + .mcall = mcall, + .strict = function->strict + }); if (vm->trace) uc_vm_frame_dump(vm, frame); @@ -3032,13 +3026,12 @@ uc_vm_execute(uc_vm_t *vm, uc_program_t *program, uc_value_t **retval) uc_stringbuf_t *buf; uc_value_t *val; - uc_vector_grow(&vm->callframes); - - frame = &vm->callframes.entries[vm->callframes.count++]; - frame->closure = closure; - frame->stackframe = 0; - frame->ip = uc_vm_frame_chunk(frame)->entries; - frame->strict = fn->strict; + frame = uc_vector_push(&vm->callframes, { + .closure = closure, + .stackframe = 0, + .ip = closure->function->chunk.entries, + .strict = fn->strict + }); if (vm->trace) { buf = xprintbuf_new();