diff options
author | Jo-Philipp Wich <jo@mein.io> | 2024-10-17 12:27:37 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2024-10-18 14:33:31 +0200 |
commit | e5fe6b1b822bddeac7e6a6657e6c4885aef2f2d0 (patch) | |
tree | 64b2728b5b5624dfa0b3dc761c22c498547c8460 | |
parent | 20307eecd5a8ccbf334aaa97129e2405cd1f2b22 (diff) |
treewide: refactor vector usage code
Utilize uc_vector_push() and uc_vector_foreach() where applicable.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r-- | chunk.c | 45 | ||||
-rw-r--r-- | compiler.c | 86 | ||||
-rw-r--r-- | lexer.c | 6 | ||||
-rw-r--r-- | lib/ubus.c | 4 | ||||
-rw-r--r-- | source.c | 10 | ||||
-rw-r--r-- | vm.c | 53 |
6 files changed, 86 insertions, 118 deletions
@@ -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 * @@ -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); } @@ -153,11 +153,13 @@ emit_op(uc_lexer_t *lex, ssize_t pos, int type, uc_value_t *uv) static uc_token_t * emit_buffer(uc_lexer_t *lex, ssize_t pos, int type, const char *strip_trailing_chars) { uc_token_t *rv = NULL; + char *p; if (lex->buffer.count) { if (strip_trailing_chars) - while (lex->buffer.count > 0 && strchr(strip_trailing_chars, *uc_vector_last(&lex->buffer))) - lex->buffer.count--; + for (p = uc_vector_last(&lex->buffer); + p && strchr(strip_trailing_chars, *p); + lex->buffer.count--, p = uc_vector_last(&lex->buffer)); rv = emit_op(lex, pos, type, ucv_string_new_length(uc_vector_first(&lex->buffer), lex->buffer.count)); @@ -724,6 +724,7 @@ uc_ubus_defer(uc_vm_t *vm, size_t nargs) uc_ubus_deferred_t *defer; uc_ubus_connection_t *c; enum ubus_msg_status rv; + uc_callframe_t *frame; uint32_t id; conn_get(vm, &c); @@ -762,7 +763,8 @@ uc_ubus_defer(uc_vm_t *vm, size_t nargs) uloop_timeout_set(&defer->timeout, c->timeout * 1000); res = uc_resource_new(defer_type, defer); - conn = uc_vector_last(&vm->callframes)->ctx; + frame = uc_vector_last(&vm->callframes); + conn = frame ? frame->ctx : NULL; defer->registry_index = request_reg_add(vm, ucv_get(res), ucv_get(replycb), ucv_get(conn)); @@ -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++; } } } @@ -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(); |