stage1 整理代码 提供更多错误信息

This commit is contained in:
zzy
2026-07-21 09:56:19 +08:00
parent 459b6188a9
commit 7c5bb41e09
8 changed files with 129 additions and 72 deletions

View File

@@ -231,3 +231,49 @@ void spl_emit_store_init(spl_comp_t *ctx, int var_offset, int var_type_idx) {
emit_store_to_laddr(&ctx->emit, var_offset, bt);
}
}
void spl_emit_defer(spl_comp_t *ctx) {
if (ctx->defer_count >= DEFER_MAX)
return;
spl_val_t jmp_skip = emit_jmp_here(&ctx->emit);
spl_defer_entry_t *e = &ctx->defer_stack[ctx->defer_count];
e->body_start = jmp_skip + 1;
e->jmp_exit = 0;
e->depth = ctx->scope_depth;
e->count_at_decl = ctx->defer_count;
ctx->defer_count++;
}
void spl_emit_defer_epilogue(spl_comp_t *ctx, int depth) {
for (int i = 0; i < ctx->defer_count; i++) {
if (ctx->defer_stack[i].depth != depth)
continue;
spl_defer_entry_t *e = &ctx->defer_stack[i];
spl_val_t skip_addr = e->body_start - 1;
spl_val_t skip_target = e->jmp_exit + 1;
emit_patch(&ctx->emit, skip_addr, skip_target - skip_addr - 1);
}
for (int i = ctx->defer_count - 1; i >= 0; i--) {
if (ctx->defer_stack[i].depth != depth)
continue;
spl_defer_entry_t *e = &ctx->defer_stack[i];
spl_val_t here = vec_size(ctx->prog.insns);
spl_val_t jmp_offset = (spl_val_t)((isize)e->body_start - (isize)here - 1);
emit_jmp(&ctx->emit, jmp_offset);
if (e->jmp_exit > 0)
emit_patch(&ctx->emit, e->jmp_exit, here + 1 - e->jmp_exit - 1);
}
int new_count = 0;
for (int i = 0; i < ctx->defer_count; i++) {
if (ctx->defer_stack[i].depth != depth)
ctx->defer_stack[new_count++] = ctx->defer_stack[i];
}
ctx->defer_count = new_count;
}