stage1 将数组和聚合类型使用byte offset而不是slot idx

This commit is contained in:
zzy
2026-07-07 11:38:31 +08:00
parent 3cf11f922e
commit 1df3e3bcb4
9 changed files with 103 additions and 86 deletions

View File

@@ -47,7 +47,7 @@ void spl_comp_reset(spl_comp_t *ctx) {
ctx->error_msg[0] = '\0';
ctx->current_func_idx = -1;
ctx->current_ret_type = NULL;
ctx->current_local_slot = 0;
ctx->current_local_bytes = 0;
ctx->in_loop = 0;
ctx->break_patch_count = 0;
ctx->break_patch_cap = 0;
@@ -126,17 +126,21 @@ int spl_declare_var(spl_comp_t *ctx, const char *name, spl_type_info_t *type, in
var.type = type;
var.is_const = is_const;
var.depth = ctx->scope_depth;
var.slot = ctx->current_local_slot;
var.offset = ctx->current_local_bytes;
usize slot_count = spl_type_slot_count(type);
ctx->current_local_slot += (int)slot_count;
usize var_size = spl_type_size(type);
/* Round up to sizeof(spl_val_t) alignment so params (pushed as spl_val_t) align */
usize aligned = (var_size + sizeof(spl_val_t) - 1) & ~(sizeof(spl_val_t) - 1);
if (aligned < sizeof(spl_val_t))
aligned = sizeof(spl_val_t);
ctx->current_local_bytes += (int)aligned;
/* Add to current scope */
if (vec_size(ctx->scopes) > 0) {
spl_scope_t *scope = &vec_at(ctx->scopes, vec_size(ctx->scopes) - 1);
vec_push(scope->vars, var);
}
return var.slot;
return var.offset;
}
spl_var_info_t *spl_lookup_var(spl_comp_t *ctx, const char *name) {
@@ -152,9 +156,9 @@ spl_var_info_t *spl_lookup_var(spl_comp_t *ctx, const char *name) {
return NULL;
}
int spl_get_var_slot(spl_comp_t *ctx, const char *name) {
int spl_get_var_offset(spl_comp_t *ctx, const char *name) {
spl_var_info_t *v = spl_lookup_var(ctx, name);
return v ? v->slot : -1;
return v ? v->offset : -1;
}
/* ---- Function management ---- */