From 480945a92f60ccc052e2eaf1f00dba8e7792b935 Mon Sep 17 00:00:00 2001 From: zzy <2450266535@qq.com> Date: Wed, 2 Dec 2026 22:41:20 +0800 Subject: [PATCH] ai: stage1 unify IR values and globals - add spl_ir_value_t (INST/CONST/GLOBAL) and global table - remove mem.global_alloc and global.ref instructions - dedup byte constants in a program-level pool - zero-init aggregate literals in ast2ir; honor undefined - C backend: fix zero-arg main return, extern prototypes/varargs - fix dangling inline-module method names (0018 flake) --- stage1/backend/c/spl_ir2c.c | 401 ++++++++------ stage1/backend/vm/spl_ir2vm.c | 246 +++++---- stage1/include/spl_ast2ir.h | 12 +- stage1/include/spl_ir.h | 324 +++++++---- stage1/include/spl_ir2vm.h | 1 + stage1/ir/ir_opt.c | 264 ++++----- stage1/ir/spl_ir.c | 537 ++++++++++++------- stage1/lower/spl_ast2ir.c | 976 ++++++++++++++++++++-------------- stage1/sema/spl_sema.c | 6 +- 9 files changed, 1657 insertions(+), 1110 deletions(-) diff --git a/stage1/backend/c/spl_ir2c.c b/stage1/backend/c/spl_ir2c.c index 8116699..9366ac5 100644 --- a/stage1/backend/c/spl_ir2c.c +++ b/stage1/backend/c/spl_ir2c.c @@ -8,16 +8,27 @@ 聚合值统一表示为「指向存储的指针」:u64 sN[..]; void* vN。 类型/布局全部走 layout,生成期不做运行时分发。非 extern 函数名 f。 */ +typedef struct { + spl_ir_value_kind_t kind; + spl_type_id_t tid; + uint64_t bits; + usize slot; +} cconst_t; +typedef VEC(cconst_t) cconst_vec_t; + typedef struct { const spl_ir_t *ir; const spl_type_t *type; spl_layout_t *layout; const spl_attr_t *attr; spl_ir2c_buf_t *out; - usize gdata_n; /* 每个 mem.global_alloc 生成一块静态数据,从 1 开始编号 */ + cconst_vec_t cconsts; /* 当前函数的 CONST 操作数槽 */ bool failed; } ir2c_t; +/* 常量操作数占用 v 的高位编号,避免与指令结果 v1..vN 冲突。 */ +#define SPL_IR2C_CONST_BASE ((usize)1 << 30) + static void buf_append(spl_ir2c_buf_t *b, const char *s, usize n) { if (n == 0) return; @@ -34,12 +45,24 @@ static void buf_append(spl_ir2c_buf_t *b, const char *s, usize n) { static void put(ir2c_t *c, const char *s) { buf_append(c->out, s, strlen(s)); } static void put_n(ir2c_t *c, const char *s, usize n) { buf_append(c->out, s, n); } static void put_byte(ir2c_t *c, char b) { buf_append(c->out, &b, 1); } -static void put_u(ir2c_t *c, usize v) { +static void put_u_raw(ir2c_t *c, usize v) { char buf[32]; int n = snprintf(buf, sizeof buf, "%llu", (unsigned long long)v); if (n > 0) buf_append(c->out, buf, (usize)n); } +static usize const_slot_lookup(ir2c_t *c, spl_ir_value_t v); + +static void put_value_id(ir2c_t *c, spl_ir_value_t v) { + if (v.kind == SPL_IR_VALUE_INST) { + put_u_raw(c, v.inst); + return; + } + put_u_raw(c, SPL_IR2C_CONST_BASE + const_slot_lookup(c, v)); +} +/* usize 走原路径;Value 常量走 v。 */ +#define put_u(c_, x_) _Generic((x_), spl_ir_value_t: put_value_id, default: put_u_raw)((c_), (x_)) + static void put_i(ir2c_t *c, long long v) { char buf[32]; int n = snprintf(buf, sizeof buf, "%lld", v); @@ -181,41 +204,31 @@ static void c_fn_name(ir2c_t *c, spl_ir_func_ref_t fid) { put_u(c, fid); } -/* 读取 type.const 的整数值;按 tid 决定落在哪个 union 成员(同 ir2vm 约定)。 */ -static usize const_node(ir2c_t *c, const spl_ir_func_t *f, spl_ir_node_ref_t ref) { - if (ref == 0 || ref >= vec_size(f->nodes)) - return 0; - const spl_ir_node_t *n = &vec_at(f->nodes, ref); - if (n->kind != SPL_IR_TYPE_CONST) - return 0; - spl_type_node_t *te = spl_type_node(c->type, n->type_const.tid); - if (!te) - return 0; - switch (te->kind) { - case SPL_TYPE_BOOL: - return n->type_const.bool_lit ? 1 : 0; - case SPL_TYPE_INT: - if (te->int_type.bits == 8) - return (usize)(unsigned char)n->type_const.ch_lit; - return n->type_const.int_lit; - case SPL_TYPE_PTR: - return (usize)n->type_const.ptr_lit; - default: - return 0; - } +/* 常量操作数按 (tid, 位模式) 共享一个 v 变量。 */ +static uint64_t value_bits(spl_ir_value_t v) { + uint64_t bits = 0; + memcpy(&bits, &v.int_bits, sizeof bits); + return bits; } -/* 函数常量节点(Fn lit)持有的函数 ref;非函数常量返回 0。 */ -static spl_ir_func_ref_t const_fn_ref(ir2c_t *c, const spl_ir_func_t *f, spl_ir_node_ref_t ref) { - if (ref == 0 || ref >= vec_size(f->nodes)) - return 0; - const spl_ir_node_t *n = &vec_at(f->nodes, ref); - if (n->kind != SPL_IR_TYPE_CONST) - return 0; - spl_type_node_t *te = spl_type_node(c->type, n->type_const.tid); - if (!te || te->kind != SPL_TYPE_FN) - return 0; - return n->type_const.fn; +static usize const_slot_lookup(ir2c_t *c, spl_ir_value_t v) { + uint64_t bits = value_bits(v); + for (usize i = 0; i < vec_size(c->cconsts); ++i) { + cconst_t *e = &vec_at(c->cconsts, i); + if (e->kind == v.kind && e->tid == v.tid && e->bits == bits) + return e->slot; + } + cconst_t e = {.kind = v.kind, .tid = v.tid, .bits = bits, .slot = vec_size(c->cconsts)}; + vec_push(c->cconsts, e); + return e.slot; +} + +static usize value_int(spl_ir_value_t v) { + return v.kind == SPL_IR_VALUE_CONST ? (usize)v.int_bits : 0; +} + +static spl_ir_func_ref_t value_fn(spl_ir_value_t v) { + return v.kind == SPL_IR_VALUE_CONST ? v.fn : 0; } static spl_type_id_t field_tid(ir2c_t *c, spl_type_id_t tid, usize idx) { @@ -291,16 +304,12 @@ static spl_type_id_t result_tid(ir2c_t *c, const spl_ir_node_t *n) { return n->cast.to_tid; case SPL_IR_MEM_ALLOCA: return make_ptr(c, n->mem_alloc.tid); - case SPL_IR_MEM_GLOBAL_ALLOC: - return n->mem_global_alloc.tid; /* 已是 *u8 */ case SPL_IR_MEM_LOAD: return n->mem_load.tid; case SPL_IR_MEM_OFFSET: return make_ptr(c, n->mem_offset.tid); case SPL_IR_MEM_FIELD_PTR: return make_ptr(c, field_tid(c, n->mem_field_ptr.tid, n->mem_field_ptr.field_idx)); - case SPL_IR_TYPE_CONST: - return n->type_const.tid; case SPL_IR_TYPE_BITSIZEOF: case SPL_IR_TYPE_SIZEOF: case SPL_IR_TYPE_ALIGNOF: @@ -324,6 +333,14 @@ static spl_type_id_t result_tid(ir2c_t *c, const spl_ir_node_t *n) { } } +static spl_type_id_t value_type(ir2c_t *c, const spl_ir_func_t *f, spl_ir_value_t v) { + if (v.kind != SPL_IR_VALUE_INST) + return v.tid; + if (v.inst == 0 || v.inst >= vec_size(f->nodes)) + return 0; + return result_tid(c, &vec_at(f->nodes, v.inst)); +} + static void bind_agg(ir2c_t *c, usize ref) { put(c, "v"); put_u(c, ref); @@ -362,7 +379,9 @@ static void emit_agg_construct(ir2c_t *c, usize ref, const spl_ir_node_t *n) { bind_agg(c, ref); spl_type_id_t agg_t = n->agg_construct.tid; for (usize i = 0; i < vec_size(n->agg_construct.fields); ++i) { - spl_ir_node_ref_t fref = vec_at(n->agg_construct.fields, i); + spl_ir_value_t fref = vec_at(n->agg_construct.fields, i); + if (spl_ir_value_is_none(fref)) + continue; spl_type_id_t ftid = field_tid(c, agg_t, i); if (!ftid) continue; @@ -623,68 +642,8 @@ static void emit_cast(ir2c_t *c, usize ref, const spl_ir_node_t *n) { put(c, ";\n"); } -static void emit_const(ir2c_t *c, usize ref, const spl_ir_node_t *n) { - spl_type_id_t tid = n->type_const.tid; - spl_type_node_t *te = spl_type_node(c->type, tid); - if (!te) - return; - if (te->kind == SPL_TYPE_UNIT) - return; - put(c, "v"); - put_u(c, ref); - put(c, " = "); - switch (te->kind) { - case SPL_TYPE_BOOL: - put(c, n->type_const.bool_lit ? "1" : "0"); - break; - case SPL_TYPE_INT: { - usize raw = (te->int_type.bits == 8) ? (usize)(unsigned char)n->type_const.ch_lit - : n->type_const.int_lit; - if (te->int_type.is_signed) { - long long sv = (long long)raw; - if (sv == (-9223372036854775807LL - 1)) - put(c, "(-9223372036854775807 - 1)"); - else - put_i(c, sv); - } else { - put_u(c, raw); - if (te->int_type.bits >= 64) - put(c, "ull"); - } - break; - } - case SPL_TYPE_PTR: - put(c, "(void*)"); - put_u(c, (usize)n->type_const.ptr_lit); - break; - /* 字节常量(字符串/文件名):只被 mem.global_alloc 通过字段引用,vN 是死值。 - 若输出宿主指针会导致生成物随编译进程地址而变,这里固定为 0。 */ - case SPL_TYPE_ARRAY: - put(c, "(void*)0"); - break; - case SPL_TYPE_FN: - put(c, "(void*)&"); - c_fn_name(c, n->type_const.fn); - break; - case SPL_TYPE_FLOAT: { - double d = n->type_const.float_lit; - if (isfinite(d)) { - char buf[64]; - snprintf(buf, sizeof buf, "%.17g", d); - put(c, buf); - } else { - put(c, "0.0"); - } - break; - } - default: - put(c, "0"); - break; - } - put(c, ";\n"); -} - static void emit_mem(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir_node_t *n) { + (void)f; switch (n->kind) { case SPL_IR_MEM_ALLOCA: @@ -766,62 +725,13 @@ static void emit_mem(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir_ break; } - case SPL_IR_MEM_GLOBAL_ALLOC: { - spl_type_id_t ctid = 0; - const void *pv = NULL; - if (n->mem_global_alloc.const_node > 0 && - n->mem_global_alloc.const_node < vec_size(f->nodes)) { - const spl_ir_node_t *cn = &vec_at(f->nodes, n->mem_global_alloc.const_node); - if (cn->kind == SPL_IR_TYPE_CONST) { - ctid = cn->type_const.tid; - pv = cn->type_const.ptr_lit; - } - } - if (!pv) { - put(c, "v"); - put_u(c, ref); - put(c, " = (void*)0;\n"); - break; - } - /* size 为 0(空数组)时只生成一个零词,避免越界读取宿主内存导致生成物不确定。 */ - usize size = type_size(c, ctid); - usize gid = c->gdata_n++; - usize words = (size + 7) / 8; - if (words == 0) - words = 1; - put(c, "static const u64 g"); - put_u(c, gid); - put(c, "["); - put_u(c, words); - put(c, "] = {"); - for (usize j = 0; j < words; ++j) { - if (j > 0) - put(c, ", "); - uint64_t w = 0; - for (usize k = 0; k < 8; ++k) { - usize idx = j * 8 + k; - uint64_t b = 0; - if (idx < size) - b = (uint64_t)((const unsigned char *)pv)[idx]; - w |= b << (k * 8); - } - put_u(c, (usize)w); - put(c, "ull"); - } - put(c, "};\nv"); - put_u(c, ref); - put(c, " = (void*)g"); - put_u(c, gid); - put(c, ";\n"); - break; - } - default: break; } } static void emit_typeop(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir_node_t *n) { + (void)f; usize v = 0; switch (n->kind) { case SPL_IR_TYPE_SIZEOF: @@ -838,7 +748,7 @@ static void emit_typeop(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ break; case SPL_IR_TYPE_OFFSETOF: { /* field_idx 是一个常量节点 ref */ - usize idx = const_node(c, f, n->ir_offsetof.field_idx); + usize idx = value_int(n->ir_offsetof.field_idx); v = field_off(c, n->ir_offsetof.tid, idx); break; } @@ -872,7 +782,7 @@ static void emit_typeop(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ } static void emit_call(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir_node_t *n) { - spl_ir_func_ref_t callee = const_fn_ref(c, f, n->control_call.func); + spl_ir_func_ref_t callee = value_fn(n->control_call.func); bool ret_agg = is_aggregate(c, n->control_call.tid); if (callee != 0) { if (ret_agg) { @@ -899,9 +809,7 @@ static void emit_call(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir } /* 间接调用:从 func 值的类型构造函数指针类型 */ - spl_type_id_t func_tid = 0; - if (n->control_call.func < vec_size(f->nodes)) - func_tid = result_tid(c, &vec_at(f->nodes, n->control_call.func)); + spl_type_id_t func_tid = value_type(c, f, n->control_call.func); spl_type_node_t *ftnode = spl_type_node(c->type, func_tid); if (!ftnode || ftnode->kind != SPL_TYPE_FN) { put_unsupported(c, "indirect call type"); @@ -959,7 +867,7 @@ static void emit_control(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl spl_type_id_t ret_tid) { switch (n->kind) { case SPL_IR_CONTROL_PARAM: { - usize idx = const_node(c, f, n->control_param.idx); + usize idx = value_int(n->control_param.idx); if (is_aggregate(c, n->control_param.tid)) { put(c, "v"); put_u(c, ref); @@ -980,7 +888,7 @@ static void emit_control(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl case SPL_IR_CONTROL_RET: if (is_aggregate(c, ret_tid)) { - if (n->control_ret.val) { + if (!spl_ir_value_is_none(n->control_ret.val)) { usize sz = type_size(c, ret_tid); put(c, "memcpy(ret__, v"); put_u(c, n->control_ret.val); @@ -1026,10 +934,6 @@ static void emit_control(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl static void emit_node(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir_node_t *n, spl_type_id_t ret_tid) { switch (n->kind) { - case SPL_IR_TYPE_CONST: - emit_const(c, ref, n); - break; - case SPL_IR_ARITH_ADD: case SPL_IR_ARITH_SUB: case SPL_IR_ARITH_MUL: @@ -1074,7 +978,6 @@ static void emit_node(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir case SPL_IR_MEM_STORE: case SPL_IR_MEM_OFFSET: case SPL_IR_MEM_FIELD_PTR: - case SPL_IR_MEM_GLOBAL_ALLOC: emit_mem(c, f, ref, n); break; @@ -1160,11 +1063,106 @@ static void emit_node(ir2c_t *c, const spl_ir_func_t *f, usize ref, const spl_ir } } +static void emit_const_literal(ir2c_t *c, spl_ir_value_t v) { + if (v.kind == SPL_IR_VALUE_GLOBAL) { + put(c, "(void*)gg"); + put_u_raw(c, v.global_id); + return; + } + spl_type_node_t *te = spl_type_node(c->type, v.tid); + if (!te) { + put(c, "0"); + return; + } + switch (te->kind) { + case SPL_TYPE_BOOL: + put(c, v.int_bits ? "1" : "0"); + break; + case SPL_TYPE_INT: { + uint64_t raw = v.int_bits; + if (te->int_type.bits == 8) + raw = (unsigned char)raw; + if (te->int_type.is_signed) { + long long sv = (long long)raw; + if (sv == (-9223372036854775807LL - 1)) + put(c, "(-9223372036854775807LL - 1)"); + else { + put(c, "("); + put_i(c, sv); + put(c, ")"); + } + } else { + put(c, "("); + put_u_raw(c, (usize)raw); + if (te->int_type.bits >= 64) + put(c, "ull"); + put(c, ")"); + } + break; + } + case SPL_TYPE_PTR: + if (v.global_id) { + put(c, "(void*)gg"); + put_u_raw(c, v.global_id); + } else { + put(c, "(void*)0"); + } + break; + case SPL_TYPE_FN: + if (v.fn == 0) { + put(c, "(void*)0"); + } else { + put(c, "(void*)&"); + c_fn_name(c, v.fn); + } + break; + case SPL_TYPE_FLOAT: { + char buf[64]; + if (isfinite(v.float_bits)) + snprintf(buf, sizeof buf, "%.17g", v.float_bits); + else + snprintf(buf, sizeof buf, "0.0"); + put(c, buf); + break; + } + default: + put(c, "0"); + break; + } +} + +static void collect_const_cb(void *ud, spl_ir_value_t v) { + ir2c_t *c = ud; + if (v.kind != SPL_IR_VALUE_INST) + (void)const_slot_lookup(c, v); +} + +static void declare_consts(ir2c_t *c) { + for (usize i = 0; i < vec_size(c->cconsts); ++i) { + cconst_t *e = &vec_at(c->cconsts, i); + if (!put_type(c, e->tid)) + put(c, "void*"); + put(c, " v"); + put_u_raw(c, SPL_IR2C_CONST_BASE + e->slot); + put(c, " = "); + spl_ir_value_t v = {0}; + v.kind = e->kind; + v.tid = e->tid; + memcpy(&v.int_bits, &e->bits, sizeof e->bits); + emit_const_literal(c, v); + put(c, ";\n"); + } +} + static void declare_all(ir2c_t *c, const spl_ir_func_t *f) { + c->cconsts.size = 0; + for (usize i = 1; i < vec_size(f->nodes); ++i) + spl_ir_node_visit_values(&vec_at(f->nodes, i), collect_const_cb, c); + for (usize i = 1; i < vec_size(f->nodes); ++i) { const spl_ir_node_t *n = &vec_at(f->nodes, i); if (n->kind == SPL_IR_MEM_ALLOCA) { - usize count = const_node(c, f, n->mem_alloc.count); + usize count = value_int(n->mem_alloc.count); if (count == 0) count = 1; spl_type_id_t pointee = n->mem_alloc.tid; @@ -1197,6 +1195,7 @@ static void declare_all(ir2c_t *c, const spl_ir_func_t *f) { if (tid != 0 && !tid_is_unit(c, tid)) declare_ref(c, i, tid); } + declare_consts(c); } static void emit_fn_sig(ir2c_t *c, spl_ir_func_ref_t fid, bool with_names) { @@ -1222,6 +1221,13 @@ static void emit_fn_sig(ir2c_t *c, spl_ir_func_ref_t fid, bool with_names) { if (need) put(c, ", "); spl_type_id_t pt = vec_at(te->fn_type.params, k); + spl_type_node_t *ptn = spl_type_node(c->type, pt); + if (ptn && ptn->kind == SPL_TYPE_UNIT) { + /* varargs 标记在 IR 里落成 unit 类型。 */ + put(c, "..."); + need = true; + continue; + } if (is_aggregate(c, pt)) put(c, "void*"); else if (!put_type(c, pt)) @@ -1267,14 +1273,63 @@ static spl_ir_func_ref_t find_main(ir2c_t *c) { return 0; } +static void emit_globals(ir2c_t *c) { + for (usize i = 1; i < vec_size(c->ir->globals); ++i) { + const spl_ir_global_t *g = &vec_at(c->ir->globals, i); + usize size = type_size(c, g->tid); + usize words = (size + 7) / 8; + if (words == 0) + words = 1; + put(c, "static "); + if (g->readonly) + put(c, "const "); + put(c, "u64 gg"); + put_u_raw(c, i); + put(c, "["); + put_u_raw(c, words); + put(c, "] = {"); + for (usize w = 0; w < words; ++w) { + if (w > 0) + put(c, ", "); + uint64_t word = 0; + if (g->init.kind == SPL_IR_VALUE_CONST) { + spl_type_node_t *it = spl_type_node(c->type, g->init.tid); + if (it && it->kind == SPL_TYPE_ARRAY) { + spl_ir_const_t *k = + spl_ir_const((spl_ir_t *)c->ir, g->init.const_id); + if (k) { + for (usize b = 0; b < 8; ++b) { + usize idx = w * 8 + b; + if (idx < k->bytes.len) + word |= (uint64_t)k->bytes.data[idx] << (b * 8); + } + } + } else if (w == 0) { + memcpy(&word, &g->init.int_bits, sizeof word); + } + } + put_u_raw(c, (usize)word); + put(c, "ull"); + } + put(c, "};"); + if (g->name) { + put(c, " /* "); + put(c, g->name); + put(c, " */"); + } + put(c, "\n"); + } +} + static void run(ir2c_t *c) { put(c, "// AUTO-GENERATED\n"); put(c, "#include \"spl_rt.h\"\n"); + emit_globals(c); usize nf = vec_size(c->ir->funcs); for (spl_ir_func_ref_t i = 1; i < nf; ++i) { const spl_ir_func_t *f = spl_ir_func((spl_ir_t *)c->ir, i); - if (f && !fn_is_extern(c, f)) + if (f) emit_proto(c, i); } for (spl_ir_func_ref_t i = 1; i < nf; ++i) { @@ -1291,9 +1346,16 @@ static void run(ir2c_t *c) { if (te && te->kind == SPL_TYPE_FN) nargs = vec_size(te->fn_type.params); if (nargs == 0) { - put(c, "int main(void) { f"); - put_u(c, m); - put(c, "(); return 0; }\n"); + put(c, "int main(void) { "); + if (!tid_is_unit(c, fn_ret_type(c, f->fn_tid))) { + put(c, "return (int)f"); + put_u(c, m); + put(c, "(); }\n"); + } else { + put(c, "f"); + put_u(c, m); + put(c, "(); return 0; }\n"); + } } else { put(c, "int main(int argc, char** argv) { "); if (!tid_is_unit(c, fn_ret_type(c, f->fn_tid))) { @@ -1317,9 +1379,10 @@ bool spl_ir2c_run(const spl_ir_t *ir, const spl_type_t *type, spl_layout_t *layo .layout = layout, .attr = attr, .out = out, - .gdata_n = 1, .failed = false, }; + vec_init(c.cconsts); run(&c); + vec_free(c.cconsts); return !c.failed; } diff --git a/stage1/backend/vm/spl_ir2vm.c b/stage1/backend/vm/spl_ir2vm.c index 353d1bf..0eb620f 100644 --- a/stage1/backend/vm/spl_ir2vm.c +++ b/stage1/backend/vm/spl_ir2vm.c @@ -125,11 +125,11 @@ static uint8_t vm_kind_of(spl_ir2vm_t *ctx, spl_type_id_t tid) { } } -/* 计算 IR 节点的"值类型",两处用途: +/* 计算 IR 节点的值类型,两处用途: * 1) frame_build 判断该节点是否占用结果槽(只看是否非 0); * 2) CALL 推导实参的 LOAD/STORE kind。 * mem.offset/mem.field 的 .tid 存的是 pointee/aggregate,须转成指针类型。 - * cmp/type.* 这里刻意返回操作数/被测量类型的 tid 而非 bool/usize: + * cmp/type.* 这里刻意返回操作数被测量类型的 tid 而非 bool/usize; * CALL 实参 kind 依赖此点,改动会改变生成字节码,diff 测试据此守门。 */ static spl_type_id_t ir_result_tid(spl_ir2vm_t *ctx, const spl_ir_node_t *n) { switch (n->kind) { @@ -168,8 +168,6 @@ static spl_type_id_t ir_result_tid(spl_ir2vm_t *ctx, const spl_ir_node_t *n) { return n->cast.to_tid; case SPL_IR_MEM_ALLOCA: return spl_type_builder_ptr((spl_type_t *)ctx->type, n->mem_alloc.tid); - case SPL_IR_MEM_GLOBAL_ALLOC: - return n->mem_global_alloc.tid; case SPL_IR_MEM_LOAD: return n->mem_load.tid; case SPL_IR_MEM_OFFSET: @@ -179,8 +177,6 @@ static spl_type_id_t ir_result_tid(spl_ir2vm_t *ctx, const spl_ir_node_t *n) { ir_agg_field_tid(ctx, n->mem_field_ptr.tid, n->mem_field_ptr.field_idx); return spl_type_builder_ptr((spl_type_t *)ctx->type, field); } - case SPL_IR_TYPE_CONST: - return n->type_const.tid; case SPL_IR_TYPE_BITSIZEOF: return n->bitsizeof.tid; case SPL_IR_TYPE_SIZEOF: @@ -226,16 +222,28 @@ typedef struct { /* panic 数据区:fmt slice + msg slice,各 2 words(ptr + len)。 */ enum { IR2VM_PANIC_WORDS = 4 }; +static spl_type_id_t value_tid(spl_ir2vm_t *ctx, const spl_ir_func_t *f, spl_ir_value_t v) { + if (v.kind != SPL_IR_VALUE_INST) + return v.tid; + if (v.inst == 0 || v.inst >= vec_size(f->nodes)) + return 0; + return ir_result_tid(ctx, &vec_at(f->nodes, v.inst)); +} + +static usize value_int(spl_ir_value_t v) { + return v.kind == SPL_IR_VALUE_CONST ? (usize)v.int_bits : 0; +} + +static spl_ir_func_ref_t value_fn(spl_ir_value_t v) { + return v.kind == SPL_IR_VALUE_CONST ? v.fn : 0; +} + /* 结果槽存标量;聚合/大对象额外分配一块数据区(data_off)。 */ static usize node_data_size(spl_ir2vm_t *ctx, const spl_ir_func_t *f, const spl_ir_node_t *node) { + (void)f; switch (node->kind) { case SPL_IR_MEM_ALLOCA: { - usize count = 1; - if (node->mem_alloc.count < vec_size(f->nodes)) { - const spl_ir_node_t *cn = &vec_at(f->nodes, node->mem_alloc.count); - if (cn->kind == SPL_IR_TYPE_CONST) - count = (usize)cn->type_const.int_lit; - } + usize count = value_int(node->mem_alloc.count); if (count == 0) count = 1; usize sz = ir_type_size(ctx, node->mem_alloc.tid); @@ -280,7 +288,7 @@ static void frame_build(frame_layout_t *fl, spl_ir2vm_t *ctx, spl_ir_func_ref_t for (usize i = 0; i < n; ++i) vec_push(fl->slots, (slot_t){0}); - /* 聚合返回时 fp+0 是 caller 传入的 sret 指针,占用一个隐藏参数槽 */ + /* 聚合返回时 fp+0 是 caller 传入的 sret 指针,占用一个隐藏参数槽。 */ usize nargs = fn_nargs(ctx, f->fn_tid); usize hidden_sret = ir_is_aggregate(ctx, fn_ret_type(ctx, f->fn_tid)) ? 1 : 0; fl->args_bytes = (nargs + hidden_sret) * sizeof(spl_vm_val_t); @@ -343,8 +351,14 @@ static void CG_OP(cgen_t *cg, uint8_t op, uint8_t k) { emit(cg, op, k, 0); } static void CG_SWAP(cgen_t *cg) { emit(cg, SPL_SWAP, SPL_VOID, 0); } static void CG_GADDR(cgen_t *cg, spl_vm_val_t idx) { emit(cg, SPL_GADDR, SPL_VOID, idx); } -static void cg_load_slot(cgen_t *cg, usize ref, uint8_t k) { - CG_LADDR(cg, vec_at(cg->frame.slots, ref).result_off); +static void cg_push_const(cgen_t *cg, spl_ir_value_t v, uint8_t k); + +static void cg_load_slot(cgen_t *cg, spl_ir_value_t v, uint8_t k) { + if (v.kind != SPL_IR_VALUE_INST) { + cg_push_const(cg, v, k); + return; + } + CG_LADDR(cg, vec_at(cg->frame.slots, v.inst).result_off); CG_LOAD(cg, k); } @@ -364,6 +378,7 @@ typedef struct { bool indirect; usize off; usize base; + int gdata; /* >0:全局数据区索引,直接 GADDR */ } data_loc_t; static data_loc_t direct_data(usize off) { @@ -372,16 +387,19 @@ static data_loc_t direct_data(usize off) { static data_loc_t direct_data_base(usize off, usize base) { return (data_loc_t){.indirect = false, .off = off, .base = base}; } -static data_loc_t ptr_slot(usize slot_off) { - return (data_loc_t){.indirect = true, .off = slot_off, .base = 0}; -} static data_loc_t ptr_slot_base(usize slot_off, usize base) { return (data_loc_t){.indirect = true, .off = slot_off, .base = base}; } static void cg_push_addr(cgen_t *cg, data_loc_t loc, usize extra) { usize delta = loc.base + extra; - if (loc.indirect) { + if (loc.gdata > 0) { + emit(cg, SPL_GADDR, SPL_VOID, (spl_vm_val_t)loc.gdata); + if (delta) { + CG_PUSH(cg, SPL_U64, delta); + CG_OP(cg, SPL_ADD, SPL_PTR); + } + } else if (loc.indirect) { CG_LADDR(cg, loc.off); CG_LOAD(cg, SPL_PTR); if (delta) { @@ -393,6 +411,18 @@ static void cg_push_addr(cgen_t *cg, data_loc_t loc, usize extra) { } } +/* 聚合值的存储位置:指令槽指针,或全局数据区地址。 */ +static data_loc_t value_ptr_loc(cgen_t *cg, spl_ir_value_t v, usize base) { + if (v.kind == SPL_IR_VALUE_GLOBAL) { + usize gid = v.global_id; + int idx = 0; + if (gid > 0 && gid < vec_size(cg->ctx->global_data_idx)) + idx = (int)vec_at(cg->ctx->global_data_idx, gid); + return (data_loc_t){.gdata = idx, .base = base}; + } + return ptr_slot_base(vec_at(cg->frame.slots, v.inst).result_off, base); +} + static void cg_copy_agg(cgen_t *cg, data_loc_t src, data_loc_t dst, spl_type_id_t tid) { usize sz = ir_type_size(cg->ctx, tid); usize done = 0; @@ -442,54 +472,57 @@ static void cg_cmp(cgen_t *cg, usize ref, const spl_ir_node_t *n, uint8_t op) { cg_store_slot(cg, ref, SPL_BOOL); } -static void cg_const(cgen_t *cg, usize ref, const spl_ir_node_t *n) { +static void cg_push_const(cgen_t *cg, spl_ir_value_t v, uint8_t k) { spl_ir2vm_t *ctx = cg->ctx; - spl_type_id_t tid = n->type_const.tid; - spl_type_node_t *tn = spl_type_node(ctx->type, tid); + + if (v.kind == SPL_IR_VALUE_GLOBAL) { + int gi = (v.global_id > 0 && v.global_id < vec_size(ctx->global_data_idx)) + ? (int)vec_at(ctx->global_data_idx, v.global_id) + : 0; + if (gi > 0) + emit(cg, SPL_GADDR, SPL_VOID, (spl_vm_val_t)gi); + else + CG_PUSH(cg, SPL_PTR, 0); + return; + } + + spl_type_node_t *tn = spl_type_node(ctx->type, v.tid); if (!tn) return; if (tn->kind == SPL_TYPE_FN) { isize nat_idx = -1; - if (n->type_const.fn > 0) - map_get(ctx->extern_native, n->type_const.fn, &nat_idx); + if (v.fn > 0) + map_get(ctx->extern_native, v.fn, &nat_idx); if (nat_idx >= 0) { CG_PUSH(cg, SPL_U64, (spl_vm_val_t)nat_idx); } else { usize idx = ip_of(cg); CG_PUSH(cg, SPL_PTR, 0); - cg_add_fix(cg, idx, FIX_FNADDR, 0, n->type_const.fn); + cg_add_fix(cg, idx, FIX_FNADDR, 0, v.fn); } - cg_store_slot(cg, ref, SPL_PTR); return; } - uint8_t k = vm_kind_of(ctx, tid); spl_vm_val_t imm = 0; switch (tn->kind) { case SPL_TYPE_BOOL: - imm = n->type_const.bool_lit ? 1 : 0; + imm = v.int_bits ? 1 : 0; break; case SPL_TYPE_INT: - if (tn->int_type.bits == 8) - imm = (spl_vm_val_t)(unsigned char)n->type_const.ch_lit; - else - imm = (spl_vm_val_t)n->type_const.int_lit; + imm = (spl_vm_val_t)v.int_bits; break; case SPL_TYPE_FLOAT: - memcpy(&imm, &n->type_const.float_lit, sizeof(double)); + memcpy(&imm, &v.float_bits, sizeof(double)); break; case SPL_TYPE_PTR: - imm = (spl_vm_val_t)(usize)n->type_const.ptr_lit; + imm = 0; break; - case SPL_TYPE_UNIT: - return; default: break; } CG_PUSH(cg, k, imm); - cg_store_slot(cg, ref, k); } static void cg_cast(cgen_t *cg, usize ref, const spl_ir_node_t *n) { @@ -528,26 +561,11 @@ static void cg_mem(cgen_t *cg, usize ref, const spl_ir_node_t *n) { break; } - case SPL_IR_MEM_GLOBAL_ALLOC: { - const spl_ir_node_t *cn = &vec_at(cg->irf->nodes, n->mem_global_alloc.const_node); - int idx = 0; - if (cn->kind == SPL_IR_TYPE_CONST && cn->type_const.ptr_lit) { - usize size = ir_type_size(ctx, cn->type_const.tid); - idx = spl_prog_add_data(&ctx->prog, cn->type_const.ptr_lit, size); - } - if (idx > 0) - emit(cg, SPL_GADDR, SPL_VOID, (spl_vm_val_t)idx); - else - CG_PUSH(cg, SPL_PTR, 0); - cg_store_slot(cg, ref, SPL_PTR); - break; - } - case SPL_IR_MEM_LOAD: { spl_type_id_t tid = n->mem_load.tid; if (ir_is_aggregate(ctx, tid)) { usize data = vec_at(cg->frame.slots, ref).data_off; - cg_copy_agg(cg, ptr_slot(vec_at(cg->frame.slots, n->mem_load.ptr).result_off), + cg_copy_agg(cg, value_ptr_loc(cg, n->mem_load.ptr, 0), direct_data(data), tid); CG_LADDR(cg, data); cg_store_slot(cg, ref, SPL_PTR); @@ -563,8 +581,8 @@ static void cg_mem(cgen_t *cg, usize ref, const spl_ir_node_t *n) { case SPL_IR_MEM_STORE: { spl_type_id_t tid = n->mem_store.tid; if (ir_is_aggregate(ctx, tid)) { - cg_copy_agg(cg, ptr_slot(vec_at(cg->frame.slots, n->mem_store.val).result_off), - ptr_slot(vec_at(cg->frame.slots, n->mem_store.ptr).result_off), tid); + cg_copy_agg(cg, value_ptr_loc(cg, n->mem_store.val, 0), + value_ptr_loc(cg, n->mem_store.ptr, 0), tid); } else { uint8_t k = vm_kind_of(ctx, tid); cg_load_slot(cg, n->mem_store.ptr, SPL_PTR); @@ -613,14 +631,16 @@ static void cg_agg(cgen_t *cg, usize ref, const spl_ir_node_t *n) { spl_type_id_t agg_t = n->agg_construct.tid; for (usize i = 0; i < vec_size(n->agg_construct.fields); ++i) { - spl_ir_node_ref_t fref = vec_at(n->agg_construct.fields, i); + spl_ir_value_t fref = vec_at(n->agg_construct.fields, i); + if (spl_ir_value_is_none(fref)) + continue; spl_type_id_t ftid = ir_agg_field_tid(ctx, agg_t, i); if (!ftid) continue; usize foff = ir_agg_field_off(ctx, agg_t, i); if (ir_is_aggregate(ctx, ftid)) { - cg_copy_agg(cg, ptr_slot(vec_at(cg->frame.slots, fref).result_off), + cg_copy_agg(cg, value_ptr_loc(cg, fref, 0), direct_data_base(base, foff), ftid); } else { uint8_t fk = vm_kind_of(ctx, ftid); @@ -641,7 +661,7 @@ static void cg_agg(cgen_t *cg, usize ref, const spl_ir_node_t *n) { if (ir_is_aggregate(ctx, ftid)) { usize data = vec_at(cg->frame.slots, ref).data_off; cg_copy_agg(cg, - ptr_slot_base(vec_at(cg->frame.slots, n->agg_extract.val).result_off, foff), + value_ptr_loc(cg, n->agg_extract.val, foff), direct_data(data), ftid); CG_LADDR(cg, data); cg_store_slot(cg, ref, SPL_PTR); @@ -662,14 +682,14 @@ static void cg_agg(cgen_t *cg, usize ref, const spl_ir_node_t *n) { usize base = vec_at(cg->frame.slots, ref).data_off; spl_type_id_t agg_t = n->agg_insert.tid; - cg_copy_agg(cg, ptr_slot(vec_at(cg->frame.slots, n->agg_insert.agg).result_off), + cg_copy_agg(cg, value_ptr_loc(cg, n->agg_insert.agg, 0), direct_data(base), agg_t); usize foff = ir_agg_field_off(ctx, agg_t, n->agg_insert.field_idx); spl_type_id_t ftid = ir_agg_field_tid(ctx, agg_t, n->agg_insert.field_idx); if (ftid) { if (ir_is_aggregate(ctx, ftid)) { - cg_copy_agg(cg, ptr_slot(vec_at(cg->frame.slots, n->agg_insert.field).result_off), + cg_copy_agg(cg, value_ptr_loc(cg, n->agg_insert.field, 0), direct_data_base(base, foff), ftid); } else { uint8_t fk = vm_kind_of(ctx, ftid); @@ -704,12 +724,7 @@ static void cg_typeop(cgen_t *cg, usize ref, const spl_ir_node_t *n) { v = ir_type_align(ctx, n->ir_alignof.tid); break; case SPL_IR_TYPE_OFFSETOF: { - usize idx = 0; - if (n->ir_offsetof.field_idx < vec_size(cg->irf->nodes)) { - const spl_ir_node_t *idx_node = &vec_at(cg->irf->nodes, n->ir_offsetof.field_idx); - if (idx_node->kind == SPL_IR_TYPE_CONST) - idx = (usize)idx_node->type_const.int_lit; - } + usize idx = value_int(n->ir_offsetof.field_idx); v = ir_agg_field_off(ctx, n->ir_offsetof.tid, idx); break; } @@ -733,13 +748,8 @@ static void cg_control(cgen_t *cg, usize ref, const spl_ir_node_t *n) { case SPL_IR_CONTROL_PARAM: { uint8_t k = vm_kind_of(ctx, n->control_param.tid); - usize idx = 0; - if (n->control_param.idx < vec_size(cg->irf->nodes)) { - const spl_ir_node_t *idx_node = &vec_at(cg->irf->nodes, n->control_param.idx); - if (idx_node->kind == SPL_IR_TYPE_CONST) - idx = (usize)idx_node->type_const.int_lit; - } - /* 聚合返回时 fp+0 是 sret 指针,实参从 fp+8 起 */ + usize idx = value_int(n->control_param.idx); + /* 聚合返回时 fp+0 是 sret 指针,实参从 fp+8 起。 */ usize shift = ir_is_aggregate(ctx, fn_ret_type(ctx, cg->irf->fn_tid)) ? 1 : 0; CG_LADDR(cg, (idx + shift) * sizeof(spl_vm_val_t)); CG_LOAD(cg, k); @@ -750,8 +760,8 @@ static void cg_control(cgen_t *cg, usize ref, const spl_ir_node_t *n) { case SPL_IR_CONTROL_RET: { spl_type_id_t ret_tid = n->control_ret.tid; if (ir_is_aggregate(ctx, ret_tid)) { - /* sret:把返回值写入 *(fp+0),然后以 void 返回 */ - if (n->control_ret.val) { + /* sret:把返回值写入 *(fp+0),然后以 void 返回。 */ + if (!spl_ir_value_is_none(n->control_ret.val)) { usize sz = ir_type_size(ctx, ret_tid); CG_LADDR(cg, 0); CG_LOAD(cg, SPL_PTR); @@ -763,7 +773,7 @@ static void cg_control(cgen_t *cg, usize ref, const spl_ir_node_t *n) { break; } uint8_t k = vm_kind_of(ctx, ret_tid); - if (k != SPL_VOID && n->control_ret.val) + if (k != SPL_VOID && !spl_ir_value_is_none(n->control_ret.val)) cg_load_slot(cg, n->control_ret.val, k); emit(cg, SPL_RET, k, 0); break; @@ -788,13 +798,7 @@ static void cg_control(cgen_t *cg, usize ref, const spl_ir_node_t *n) { } case SPL_IR_CONTROL_CALL: { - spl_ir_func_ref_t callee_ref = 0; - spl_ir_node_ref_t fref = n->control_call.func; - if (fref < vec_size(cg->irf->nodes)) { - const spl_ir_node_t *fn_node = &vec_at(cg->irf->nodes, fref); - if (fn_node->kind == SPL_IR_TYPE_CONST) - callee_ref = fn_node->type_const.fn; - } + spl_ir_func_ref_t callee_ref = value_fn(n->control_call.func); isize nat_idx = -1; if (callee_ref > 0) @@ -804,9 +808,8 @@ static void cg_control(cgen_t *cg, usize ref, const spl_ir_node_t *n) { if (nat_idx >= 0) { for (usize i = 0; i < nargs; ++i) { - spl_ir_node_ref_t pref = vec_at(n->control_call.params, i); - const spl_ir_node_t *pn = &vec_at(cg->irf->nodes, pref); - uint8_t pk = vm_kind_of(ctx, ir_result_tid(ctx, pn)); + spl_ir_value_t pref = vec_at(n->control_call.params, i); + uint8_t pk = vm_kind_of(ctx, value_tid(ctx, cg->irf, pref)); cg_load_slot(cg, pref, pk); } CG_PUSH(cg, SPL_U64, (spl_vm_val_t)nat_idx); @@ -831,17 +834,16 @@ static void cg_control(cgen_t *cg, usize ref, const spl_ir_node_t *n) { CG_LADDR(cg, sret_off); } for (usize i = 0; i < nargs; ++i) { - spl_ir_node_ref_t pref = vec_at(n->control_call.params, i); - const spl_ir_node_t *pn = &vec_at(cg->irf->nodes, pref); - uint8_t pk = vm_kind_of(ctx, ir_result_tid(ctx, pn)); + spl_ir_value_t pref = vec_at(n->control_call.params, i); + uint8_t pk = vm_kind_of(ctx, value_tid(ctx, cg->irf, pref)); cg_load_slot(cg, pref, pk); } - cg_load_slot(cg, fref, SPL_PTR); + cg_load_slot(cg, n->control_call.func, SPL_PTR); spl_vm_val_t actual_nargs = nargs + (ret_is_sret ? 1 : 0); emit(cg, SPL_CALL, SPL_VOID, actual_nargs); if (ret_is_sret) { - /* result slot 保存 sret 指针 */ + /* result slot 保存 sret 指针。 */ CG_LADDR(cg, sret_off); cg_store_slot(cg, ref, SPL_PTR); } else { @@ -865,10 +867,6 @@ static void cg_control(cgen_t *cg, usize ref, const spl_ir_node_t *n) { static void cg_node(cgen_t *cg, usize ref, const spl_ir_node_t *n) { switch (n->kind) { - case SPL_IR_TYPE_CONST: - cg_const(cg, ref, n); - break; - case SPL_IR_ARITH_ADD: cg_binop(cg, ref, n, SPL_ADD); break; @@ -970,7 +968,6 @@ static void cg_node(cgen_t *cg, usize ref, const spl_ir_node_t *n) { break; case SPL_IR_MEM_ALLOCA: - case SPL_IR_MEM_GLOBAL_ALLOC: case SPL_IR_MEM_LOAD: case SPL_IR_MEM_STORE: case SPL_IR_MEM_OFFSET: @@ -1011,19 +1008,19 @@ static void cg_node(cgen_t *cg, usize ref, const spl_ir_node_t *n) { if (msg_idx <= 0 || fmt_idx <= 0) break; - /* fmt slice = {ptr, len=2} 写到 data+0 */ + /* fmt slice = {ptr, len=2} 写到 data+0。 */ CG_GADDR(cg, (spl_vm_val_t)fmt_idx); cg_store_at(cg, data + 0, SPL_PTR); CG_PUSH(cg, SPL_U64, 2); cg_store_at(cg, data + 8, SPL_U64); - /* msg slice = {ptr, len} 写到 data+16 */ + /* msg slice = {ptr, len} 写到 data+16。 */ CG_GADDR(cg, (spl_vm_val_t)msg_idx); cg_store_at(cg, data + 16, SPL_PTR); CG_PUSH(cg, SPL_U64, n->dbg_panic.len); cg_store_at(cg, data + 24, SPL_U64); - /* push fmt slice 指针,push msg slice 指针 */ + /* push fmt slice 指针,push msg slice 指针。 */ CG_LADDR(cg, data + 0); CG_LADDR(cg, data + 16); @@ -1040,7 +1037,7 @@ static void cg_node(cgen_t *cg, usize ref, const spl_ir_node_t *n) { break; default: - /* 前端不发射 select/mem.copy/mem.set/abs;出现即后端缺口,直接报错。 */ +/* 前端不发射 select/mem.copy/mem.set/abs;出现即后端缺口,直接报错。 */ Panic("ir2vm: unsupported IR node kind %d", (int)n->kind); break; } @@ -1121,6 +1118,40 @@ static void register_externs(spl_ir2vm_t *ctx) { static usize spl_map_hash(usize k) { return MAP_HASH_INT(k); } static int spl_map_cmp(usize a, usize b) { return MAP_CMP_INT(a, b); } +static void prepare_globals(spl_ir2vm_t *ctx) { + vec_push(ctx->global_data_idx, 0); + for (usize i = 1; i < vec_size(ctx->ir->globals); ++i) { + const spl_ir_global_t *g = &vec_at(ctx->ir->globals, i); + usize size = ir_type_size(ctx, g->tid); + if (size == 0) + size = 1; + unsigned char *buf = calloc(1, size); + if (!buf) { + vec_push(ctx->global_data_idx, 0); + continue; + } + if (g->init.kind == SPL_IR_VALUE_CONST) { + spl_type_node_t *it = spl_type_node(ctx->type, g->init.tid); + if (it && it->kind == SPL_TYPE_ARRAY) { + spl_ir_const_t *k = spl_ir_const((spl_ir_t *)ctx->ir, g->init.const_id); + if (k) { + usize n = size < k->bytes.len ? size : k->bytes.len; + memcpy(buf, k->bytes.data, n); + } + } else { + uint64_t bits = 0; + memcpy(&bits, &g->init.int_bits, sizeof bits); + usize n = size < sizeof(bits) ? size : sizeof(bits); + for (usize j = 0; j < n; ++j) + buf[j] = (unsigned char)(bits >> (j * 8)); + } + } + int idx = spl_prog_add_data(&ctx->prog, buf, size); + vec_push(ctx->global_data_idx, idx); + free(buf); + } +} + void spl_ir2vm_init(spl_ir2vm_t *ctx, const spl_ir_t *ir, const spl_type_t *type, spl_layout_t *layout, const spl_attr_t *attr) { Assert(ir != NULL && type != NULL && attr != NULL); @@ -1129,6 +1160,7 @@ void spl_ir2vm_init(spl_ir2vm_t *ctx, const spl_ir_t *ir, const spl_type_t *type ctx->attr = attr; ctx->layout = layout; map_init(ctx->extern_native, spl_map_hash, spl_map_cmp); + vec_init(ctx->global_data_idx); spl_prog_init(&ctx->prog); ctx->diag = NULL; @@ -1136,6 +1168,7 @@ void spl_ir2vm_init(spl_ir2vm_t *ctx, const spl_ir_t *ir, const spl_type_t *type void spl_ir2vm_drop(spl_ir2vm_t *ctx) { spl_prog_drop(&ctx->prog); + vec_free(ctx->global_data_idx); map_free(ctx->extern_native); } @@ -1145,12 +1178,13 @@ int spl_ir2vm_run(spl_ir2vm_t *ctx) { return 0; register_externs(ctx); + prepare_globals(ctx); - /* 保留 insn 0 作为哨兵:确保真实函数入口地址非零, - 从而 0 可唯一表示 null 函数指针(函数指针与 null 比较)。 */ +/* 保留 insn 0 作为哨兵:确认真实函数入口地址非零, + 从而使 0 唯一表示 null 函数指针(函数指针与 null 比较)。 */ spl_prog_add_instr(&ctx->prog, SPL_HALT, SPL_VOID, 0); - - /* Pass 2:翻译非 extern 的函数 */ +/* Pass 2:翻译非 extern 的函数。 */ + /* Pass 2锛氱炕璇戦潪 extern 鐨勫嚱鏁?*/ usize *fid_ip = calloc(n, sizeof(usize)); fix_vec_t all_fixups; vec_init(all_fixups); @@ -1161,8 +1195,8 @@ int spl_ir2vm_run(spl_ir2vm_t *ctx) { continue; fid_ip[i] = translate_function(ctx, i, &all_fixups); } - - /* FNADDR 回填 */ +/* FNADDR 回填。 */ + /* FNADDR 鍥炲~ */ for (usize i = 0; i < vec_size(all_fixups); ++i) { fix_t *fx = &vec_at(all_fixups, i); if (fx->kind != FIX_FNADDR) diff --git a/stage1/include/spl_ast2ir.h b/stage1/include/spl_ast2ir.h index fb117f7..02b4e39 100644 --- a/stage1/include/spl_ast2ir.h +++ b/stage1/include/spl_ast2ir.h @@ -5,26 +5,34 @@ #include "spl_ir.h" #include "spl_sema.h" -typedef MAP(spl_def_id_t, spl_ir_node_ref_t) spl_def2ir_map_t; +typedef MAP(spl_def_id_t, spl_ir_value_t) spl_def2ir_map_t; typedef struct { usize cont_blk; usize defer_base; const char *label; spl_ir_node_ref_vec_t breaks; - spl_ir_node_ref_t result_slot; + spl_ir_value_t result_slot; spl_type_id_t result_tid; } spl_loop_ctx_t; typedef VEC(spl_loop_ctx_t) spl_loop_ctx_vec_t; typedef VEC(spl_ast_node_ref_t) spl_defer_stack_t; +typedef struct { + spl_ast_t *ast; + spl_ast_node_ref_t decl; +} spl_module_init_t; +typedef VEC(spl_module_init_t) spl_module_init_vec_t; + typedef struct { const spl_sema_t *sema; spl_ir_builder_t *ir; spl_ast_t *ast; spl_layout_t *layout; /* 借用 splc0 的 layout */ spl_def2ir_map_t def2ir_map; + MAP(spl_def_id_t, usize) global_map; + spl_module_init_vec_t module_inits; spl_loop_ctx_vec_t loop_stack; spl_defer_stack_t defer_stack; spl_lang_type_id_t cur_ret_lang; diff --git a/stage1/include/spl_ir.h b/stage1/include/spl_ir.h index 46e87ab..97c79b2 100644 --- a/stage1/include/spl_ir.h +++ b/stage1/include/spl_ir.h @@ -39,14 +39,12 @@ X(case.int2float, V0, SPL_IR_CASE_INT2FLOAT) \ X(case.float2int, V0, SPL_IR_CASE_FLOAT2INT) \ X(mem.alloca, V0, SPL_IR_MEM_ALLOCA) \ - X(mem.global_alloc, V0, SPL_IR_MEM_GLOBAL_ALLOC) \ X(mem.load, V0, SPL_IR_MEM_LOAD) \ X(mem.store, V0, SPL_IR_MEM_STORE) \ X(mem.offset, V0, SPL_IR_MEM_OFFSET) \ X(mem.field, V0, SPL_IR_MEM_FIELD_PTR) \ X(mem.copy, V0, SPL_IR_MEM_COPY) \ X(mem.set, V0, SPL_IR_MEM_SET) \ - X(type.const, V0, SPL_IR_TYPE_CONST) \ X(type.bitsizeof, V0, SPL_IR_TYPE_BITSIZEOF) \ X(type.sizeof, V0, SPL_IR_TYPE_SIZEOF) \ X(type.alignof, V0, SPL_IR_TYPE_ALIGNOF) \ @@ -65,6 +63,7 @@ X(control.trap, V0, SPL_IR_CONTROL_TRAP) \ X(dbg.breakpoint, V0, SPL_IR_DBG_BREAKPOINT) \ X(dbg.panic, V0, SPL_IR_DBG_PANIC) \ +/* clang-format on */ typedef enum { #ifdef X @@ -74,13 +73,129 @@ typedef enum { SPL_IR_FN_TABLE #undef X } spl_ir_kind_t; -/* clang-format on */ typedef usize spl_ir_node_ref_t; /* 0 is error */ typedef VEC(spl_ir_node_ref_t) spl_ir_node_ref_vec_t; typedef usize spl_ir_func_ref_t; /* 0 is error */ +/* 抽象机器的值:指令结果、常量或全局实体句柄。常量按 tid 的 SPL_TYPE_* 解释: + * BOOL/INT(<=64) -> int_bits,FLOAT -> float_bits,FN -> fn, + * PTR -> 仅表示 null,其余需要去重的量 -> const_id。 + * GLOBAL 表示模块级全局实体的地址,载荷是 global_id。 */ +typedef enum { + SPL_IR_VALUE_INST, + SPL_IR_VALUE_CONST, + SPL_IR_VALUE_GLOBAL, +} spl_ir_value_kind_t; + +typedef struct { + spl_ir_value_kind_t kind; + spl_type_id_t tid; /* CONST 时有效;INST 必须为 0 */ + union { + spl_ir_node_ref_t inst; + uint64_t int_bits; + double float_bits; + spl_ir_func_ref_t fn; + usize global_id; + usize const_id; + }; +} spl_ir_value_t; +typedef VEC(spl_ir_value_t) spl_ir_value_vec_t; + +static inline spl_ir_value_t spl_ir_value_none(void) { + spl_ir_value_t v = {0}; + return v; +} + +static inline bool spl_ir_value_is_none(spl_ir_value_t v) { + return v.kind == SPL_IR_VALUE_INST && v.inst == 0; +} + +static inline spl_ir_value_t spl_ir_value_inst(spl_ir_node_ref_t ref) { + spl_ir_value_t v = {0}; + v.kind = SPL_IR_VALUE_INST; + v.inst = ref; + return v; +} + +static inline spl_ir_value_t spl_ir_value_const_int(spl_type_id_t tid, uint64_t bits) { + spl_ir_value_t v = {0}; + v.kind = SPL_IR_VALUE_CONST; + v.tid = tid; + v.int_bits = bits; + return v; +} + +static inline spl_ir_value_t spl_ir_value_const_float(spl_type_id_t tid, double bits) { + spl_ir_value_t v = {0}; + v.kind = SPL_IR_VALUE_CONST; + v.tid = tid; + v.float_bits = bits; + return v; +} + +static inline spl_ir_value_t spl_ir_value_const_fn(spl_type_id_t tid, spl_ir_func_ref_t fn) { + spl_ir_value_t v = {0}; + v.kind = SPL_IR_VALUE_CONST; + v.tid = tid; + v.fn = fn; + return v; +} + +static inline spl_ir_value_t spl_ir_value_const_null(spl_type_id_t tid) { + spl_ir_value_t v = {0}; + v.kind = SPL_IR_VALUE_CONST; + v.tid = tid; + v.global_id = 0; + return v; +} + +static inline spl_ir_value_t spl_ir_value_global(spl_type_id_t ptr_tid, usize gid) { + spl_ir_value_t v = {0}; + v.kind = SPL_IR_VALUE_GLOBAL; + v.tid = ptr_tid; + v.global_id = gid; + return v; +} + +static inline spl_ir_value_t spl_ir_value_const_pool(spl_type_id_t tid, usize const_id) { + spl_ir_value_t v = {0}; + v.kind = SPL_IR_VALUE_CONST; + v.tid = tid; + v.const_id = const_id; + return v; +} + +static inline spl_ir_node_ref_t spl_ir_value_inst_ref(spl_ir_value_t v) { + return v.kind == SPL_IR_VALUE_INST ? v.inst : 0; +} + +/* 常量池项:只放需要去重的量,标量直接内联在 Value 里。 */ +typedef struct { + spl_type_id_t tid; + union { + struct { + unsigned char *data; + usize len; + } bytes; + struct { + uint64_t *limbs; + usize count; + } big_int; + }; +} spl_ir_const_t; +typedef VEC(spl_ir_const_t) spl_ir_const_vec_t; + +/* 全局存储声明。引用是 PTR 常量(global_id),不是指令。 */ +typedef struct { + const char *name; + spl_type_id_t tid; /* 对象类型 */ + spl_ir_value_t init; + bool readonly; +} spl_ir_global_t; +typedef VEC(spl_ir_global_t) spl_ir_global_vec_t; + #define SPL_ENUM_TAG_FIELD_IDX 0 typedef struct { @@ -91,70 +206,54 @@ typedef struct { spl_type_id_t tid; struct { spl_type_id_t tid; - spl_ir_node_ref_t left; - spl_ir_node_ref_t right; + spl_ir_value_t left; + spl_ir_value_t right; } arith; struct { spl_type_id_t tid; - spl_ir_node_ref_t a; - spl_ir_node_ref_t b; + spl_ir_value_t a; + spl_ir_value_t b; } cmp; struct { spl_type_id_t from_tid; spl_type_id_t to_tid; - spl_ir_node_ref_t val; + spl_ir_value_t val; } cast; struct { spl_type_id_t tid; - spl_ir_node_ref_t count; + spl_ir_value_t count; } mem_alloc; struct { spl_type_id_t tid; - spl_ir_node_ref_t const_node; - } mem_global_alloc; - struct { - spl_type_id_t tid; - spl_ir_node_ref_t ptr; + spl_ir_value_t ptr; } mem_load; struct { spl_type_id_t tid; - spl_ir_node_ref_t ptr; - spl_ir_node_ref_t val; + spl_ir_value_t ptr; + spl_ir_value_t val; } mem_store; struct { spl_type_id_t tid; - spl_ir_node_ref_t ptr; - spl_ir_node_ref_t offset; + spl_ir_value_t ptr; + spl_ir_value_t offset; } mem_offset; struct { spl_type_id_t tid; - spl_ir_node_ref_t agg; + spl_ir_value_t agg; usize field_idx; } mem_field_ptr; struct { spl_type_id_t tid; - spl_ir_node_ref_t dst; - spl_ir_node_ref_t src; - spl_ir_node_ref_t size; + spl_ir_value_t dst; + spl_ir_value_t src; + spl_ir_value_t size; } mem_copy; struct { spl_type_id_t tid; - spl_ir_node_ref_t dst; - spl_ir_node_ref_t val; - spl_ir_node_ref_t size; + spl_ir_value_t dst; + spl_ir_value_t val; + spl_ir_value_t size; } mem_set; - struct { - spl_type_id_t tid; - union { - bool bool_lit; - void *ptr_lit; - usize int_lit; - double float_lit; - // const char *cstr_lit; - char ch_lit; - spl_ir_func_ref_t fn; - }; - } type_const; struct { spl_type_id_t tid; } bitsizeof; @@ -166,7 +265,7 @@ typedef struct { } ir_alignof; struct { spl_type_id_t tid; - spl_ir_node_ref_t field_idx; + spl_ir_value_t field_idx; } ir_offsetof; struct { spl_type_id_t tid; @@ -174,29 +273,29 @@ typedef struct { struct { spl_type_id_t tid; - spl_ir_node_ref_vec_t fields; + spl_ir_value_vec_t fields; } agg_construct; struct { spl_type_id_t tid; spl_type_id_t field_tid; usize field_idx; - spl_ir_node_ref_t val; + spl_ir_value_t val; } agg_extract; struct { spl_type_id_t tid; usize field_idx; - spl_ir_node_ref_t agg; - spl_ir_node_ref_t field; + spl_ir_value_t agg; + spl_ir_value_t field; } agg_insert; struct { spl_type_id_t tid; - spl_ir_node_ref_t cond; - spl_ir_node_ref_t true_val; - spl_ir_node_ref_t false_val; + spl_ir_value_t cond; + spl_ir_value_t true_val; + spl_ir_value_t false_val; } control_select; struct { - spl_ir_node_ref_t cond; + spl_ir_value_t cond; spl_ir_node_ref_t true_label; spl_ir_node_ref_t false_label; } control_br; @@ -205,16 +304,16 @@ typedef struct { } control_jmp; struct { spl_type_id_t tid; - spl_ir_node_ref_t func; - spl_ir_node_ref_vec_t params; + spl_ir_value_t func; + spl_ir_value_vec_t params; } control_call; struct { spl_type_id_t tid; - spl_ir_node_ref_t idx; + spl_ir_value_t idx; } control_param; struct { spl_type_id_t tid; - spl_ir_node_ref_t val; + spl_ir_value_t val; } control_ret; struct { @@ -243,8 +342,17 @@ typedef struct { } spl_ir_fix_t; typedef VEC(spl_ir_fix_t) spl_ir_fix_vec_t; +typedef struct { + spl_type_id_t tid; + const unsigned char *data; + usize len; +} spl_ir_const_key_t; + typedef struct { spl_ir_func_vec_t funcs; + spl_ir_const_vec_t consts; + spl_ir_global_vec_t globals; + MAP(spl_ir_const_key_t, usize) const_index; } spl_ir_t; void spl_ir_init(spl_ir_t *ir); @@ -255,6 +363,12 @@ spl_ir_func_ref_t spl_ir_alloc_fn(spl_ir_t *ir); spl_ir_node_t *spl_ir_node(spl_ir_t *ir, spl_ir_func_ref_t fn_id, spl_ir_node_ref_t node_id); spl_ir_func_t *spl_ir_func(spl_ir_t *ir, spl_ir_func_ref_t fn_id); +spl_ir_const_t *spl_ir_const(spl_ir_t *ir, usize const_id); +/* INST 返回对应节点;CONST 返回 NULL。 */ +spl_ir_node_t *spl_ir_value_node(spl_ir_t *ir, spl_ir_func_ref_t fn_id, spl_ir_value_t v); +/* 遍历节点里所有值型操作数(块标签除外)。 */ +typedef void (*spl_ir_value_cb_t)(void *ud, spl_ir_value_t v); +void spl_ir_node_visit_values(spl_ir_node_t *n, spl_ir_value_cb_t cb, void *ud); /* f 是否声明为 extern(属性表内名为 "extern" 的属性);命中返回该属性节点,否则 NULL。 供各后端复用,避免 "extern" 字符串判断散落多处。 */ @@ -323,102 +437,98 @@ usize spl_ir_builder_block_begin(spl_ir_builder_t *b); void spl_ir_builder_block_end(spl_ir_builder_t *b); #define X(name, kind) \ - spl_ir_node_ref_t spl_ir_builder_##name(spl_ir_builder_t *b, spl_type_id_t tid, \ - spl_ir_node_ref_t left, spl_ir_node_ref_t right); + spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *b, spl_type_id_t tid, \ + spl_ir_value_t left, spl_ir_value_t right); SPL_IR_BIN_ARITH_TABLE #undef X #define X(name, kind) \ - spl_ir_node_ref_t spl_ir_builder_##name(spl_ir_builder_t *b, spl_type_id_t tid, \ - spl_ir_node_ref_t val); + spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *b, spl_type_id_t tid, \ + spl_ir_value_t val); SPL_IR_UN_ARITH_TABLE #undef X #define X(name, kind) \ - spl_ir_node_ref_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t tid, \ - spl_ir_node_ref_t a, spl_ir_node_ref_t b); + spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t tid, \ + spl_ir_value_t a, spl_ir_value_t b); SPL_IR_CMP_TABLE #undef X #define X(name, kind) \ - spl_ir_node_ref_t spl_ir_builder_##name(spl_ir_builder_t *b, spl_type_id_t from_tid, \ - spl_type_id_t to_tid, spl_ir_node_ref_t val); + spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *b, spl_type_id_t from_tid, \ + spl_type_id_t to_tid, spl_ir_value_t val); SPL_IR_CAST_TABLE #undef X /* memory */ -spl_ir_node_ref_t spl_ir_builder_mem_alloca(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t count); -spl_ir_node_ref_t spl_ir_builder_mem_global_alloc(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t const_node); -spl_ir_node_ref_t spl_ir_builder_mem_load(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t ptr); +spl_ir_value_t spl_ir_builder_mem_alloca(spl_ir_builder_t *b, spl_type_id_t tid, + spl_ir_value_t count); +spl_ir_value_t spl_ir_builder_mem_load(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t ptr); spl_ir_node_ref_t spl_ir_builder_mem_store(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t ptr, spl_ir_node_ref_t val); -spl_ir_node_ref_t spl_ir_builder_mem_offset(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t ptr, spl_ir_node_ref_t offset); -spl_ir_node_ref_t spl_ir_builder_mem_field(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t agg, usize field_idx); -spl_ir_node_ref_t spl_ir_builder_mem_copy(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t dst, spl_ir_node_ref_t src, - spl_ir_node_ref_t size); -spl_ir_node_ref_t spl_ir_builder_mem_set(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t dst, spl_ir_node_ref_t val, - spl_ir_node_ref_t size); + spl_ir_value_t ptr, spl_ir_value_t val); +spl_ir_value_t spl_ir_builder_mem_offset(spl_ir_builder_t *b, spl_type_id_t tid, + spl_ir_value_t ptr, spl_ir_value_t offset); +spl_ir_value_t spl_ir_builder_mem_field(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t agg, + usize field_idx); +spl_ir_node_ref_t spl_ir_builder_mem_copy(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t dst, + spl_ir_value_t src, spl_ir_value_t size); +spl_ir_node_ref_t spl_ir_builder_mem_set(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t dst, + spl_ir_value_t val, spl_ir_value_t size); -/* type const */ +/* 常量:标量内联;需要去重的量进常量池并返回 const_id。 */ -spl_ir_node_ref_t spl_ir_builder_type_const_ptr(spl_ir_builder_t *b, spl_type_id_t tid, void *val); -spl_ir_node_ref_t spl_ir_builder_type_const_bool(spl_ir_builder_t *b, spl_type_id_t tid, bool val); -spl_ir_node_ref_t spl_ir_builder_type_const_int(spl_ir_builder_t *b, spl_type_id_t tid, usize val); -spl_ir_node_ref_t spl_ir_builder_type_const_float(spl_ir_builder_t *b, spl_type_id_t tid, - double val); -spl_ir_node_ref_t spl_ir_builder_type_const_bytes(spl_ir_builder_t *b, spl_type_id_t tid, - const void *data, usize len); -spl_ir_node_ref_t spl_ir_builder_type_const_char(spl_ir_builder_t *b, spl_type_id_t tid, char val); -spl_ir_node_ref_t spl_ir_builder_type_const_fn(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_func_ref_t val); +spl_ir_value_t spl_ir_builder_const_int(spl_type_id_t tid, uint64_t bits); +spl_ir_value_t spl_ir_builder_const_bool(spl_type_id_t tid, bool val); +spl_ir_value_t spl_ir_builder_const_float(spl_type_id_t tid, double val); +spl_ir_value_t spl_ir_builder_const_null(spl_type_id_t tid); +spl_ir_value_t spl_ir_builder_const_fn(spl_type_id_t tid, spl_ir_func_ref_t fn); +spl_ir_value_t spl_ir_builder_global_addr(spl_type_id_t ptr_tid, usize global_id); +spl_ir_value_t spl_ir_builder_const_bytes(spl_ir_builder_t *b, spl_type_id_t tid, const void *data, + usize len); + +usize spl_ir_builder_global_declare(spl_ir_builder_t *b, const char *name, spl_type_id_t tid, + spl_ir_value_t init, bool readonly); /* type */ -spl_ir_node_ref_t spl_ir_builder_type_bitsizeof(spl_ir_builder_t *b, spl_type_id_t tid); -spl_ir_node_ref_t spl_ir_builder_type_sizeof(spl_ir_builder_t *b, spl_type_id_t tid); -spl_ir_node_ref_t spl_ir_builder_type_alignof(spl_ir_builder_t *b, spl_type_id_t tid); -spl_ir_node_ref_t spl_ir_builder_type_offsetof(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t field_idx); -spl_ir_node_ref_t spl_ir_builder_type_field_count(spl_ir_builder_t *b, spl_type_id_t tid); +spl_ir_value_t spl_ir_builder_type_bitsizeof(spl_ir_builder_t *b, spl_type_id_t tid); +spl_ir_value_t spl_ir_builder_type_sizeof(spl_ir_builder_t *b, spl_type_id_t tid); +spl_ir_value_t spl_ir_builder_type_alignof(spl_ir_builder_t *b, spl_type_id_t tid); +spl_ir_value_t spl_ir_builder_type_offsetof(spl_ir_builder_t *b, spl_type_id_t tid, + spl_ir_value_t field_idx); +spl_ir_value_t spl_ir_builder_type_field_count(spl_ir_builder_t *b, spl_type_id_t tid); /* agg */ spl_ir_node_ref_t spl_ir_builder_agg_construct(spl_ir_builder_t *b, spl_type_id_t tid); void spl_ir_builder_agg_construct_field(spl_ir_builder_t *b, spl_ir_node_ref_t node, - spl_ir_node_ref_t field); -spl_ir_node_ref_t spl_ir_builder_agg_extract(spl_ir_builder_t *b, spl_type_id_t tid, - spl_type_id_t field_tid, usize field_idx, - spl_ir_node_ref_t val); -spl_ir_node_ref_t spl_ir_builder_agg_insert(spl_ir_builder_t *b, spl_type_id_t tid, usize field_idx, - spl_ir_node_ref_t agg, spl_ir_node_ref_t field); + spl_ir_value_t field); +spl_ir_value_t spl_ir_builder_agg_extract(spl_ir_builder_t *b, spl_type_id_t tid, + spl_type_id_t field_tid, usize field_idx, + spl_ir_value_t val); +spl_ir_value_t spl_ir_builder_agg_insert(spl_ir_builder_t *b, spl_type_id_t tid, usize field_idx, + spl_ir_value_t agg, spl_ir_value_t field); /* control */ -spl_ir_node_ref_t spl_ir_builder_control_select(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t cond, spl_ir_node_ref_t true_val, - spl_ir_node_ref_t false_val); -spl_ir_node_ref_t spl_ir_builder_control_br(spl_ir_builder_t *b, spl_ir_node_ref_t cond); +spl_ir_value_t spl_ir_builder_control_select(spl_ir_builder_t *b, spl_type_id_t tid, + spl_ir_value_t cond, spl_ir_value_t true_val, + spl_ir_value_t false_val); +spl_ir_node_ref_t spl_ir_builder_control_br(spl_ir_builder_t *b, spl_ir_value_t cond); void spl_ir_builder_control_br_fixup(spl_ir_builder_t *b, spl_ir_node_ref_t ref, usize true_block, usize false_block); spl_ir_node_ref_t spl_ir_builder_control_jmp(spl_ir_builder_t *b); void spl_ir_builder_control_jmp_fixup(spl_ir_builder_t *b, spl_ir_node_ref_t ref, usize target_block); spl_ir_node_ref_t spl_ir_builder_control_call(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t func); + spl_ir_value_t func); void spl_ir_builder_control_call_param(spl_ir_builder_t *b, spl_ir_node_ref_t call_node, - spl_ir_node_ref_t arg); -spl_ir_node_ref_t spl_ir_builder_control_param(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t idx); + spl_ir_value_t arg); +spl_ir_value_t spl_ir_builder_control_param(spl_ir_builder_t *b, spl_type_id_t tid, + spl_ir_value_t idx); spl_ir_node_ref_t spl_ir_builder_control_ret(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t val); + spl_ir_value_t val); spl_ir_node_ref_t spl_ir_builder_control_unreachable(spl_ir_builder_t *b); spl_ir_node_ref_t spl_ir_builder_control_trap(spl_ir_builder_t *b); diff --git a/stage1/include/spl_ir2vm.h b/stage1/include/spl_ir2vm.h index f9be821..6ee6486 100644 --- a/stage1/include/spl_ir2vm.h +++ b/stage1/include/spl_ir2vm.h @@ -15,6 +15,7 @@ typedef struct { const spl_type_t *type; const spl_attr_t *attr; spl_extern_idx_map_t extern_native; + VEC(isize) global_data_idx; spl_prog_t prog; spl_layout_t *layout; /* 借用 splc0 的 layout(size/align/offset 唯一来源) */ diff --git a/stage1/ir/ir_opt.c b/stage1/ir/ir_opt.c index 8e7a098..af18d69 100644 --- a/stage1/ir/ir_opt.c +++ b/stage1/ir/ir_opt.c @@ -12,62 +12,86 @@ typedef struct { spl_ir_t *ir; spl_ir_func_ref_t caller; spl_ir_func_ref_t callee; - spl_ir_node_ref_t shift; /* new = old + shift */ - usize nparams; - spl_ir_node_ref_t *param_ref; - spl_ir_node_ref_t *param_arg; + /* callee Ref -> caller Value;Param 直接映射到实参,其余映射到克隆节点。 */ + spl_ir_value_t *val_map; + usize val_map_size; } inline_ctx_t; static spl_ir_node_t *nd(spl_ir_t *ir, spl_ir_func_ref_t fid, spl_ir_node_ref_t ref) { return spl_ir_node(ir, fid, ref); } -/* ---- 引用重写(克隆体旧 ref -> 新 ref) ---- */ +/* ---- 引用重写(克隆体旧 ref -> 新 value) ---- */ -static spl_ir_node_ref_t rw(inline_ctx_t *c, spl_ir_node_ref_t r) { - if (r == 0) - return 0; - for (usize i = 0; i < c->nparams; i++) - if (c->param_ref[i] == r) - return c->param_arg[i]; - return r + c->shift; +static spl_ir_value_t rw_value(inline_ctx_t *c, spl_ir_value_t v) { + if (v.kind != SPL_IR_VALUE_INST || v.inst == 0) + return v; + if (v.inst < c->val_map_size) { + spl_ir_value_t mapped = c->val_map[v.inst]; + if (mapped.kind != SPL_IR_VALUE_INST || mapped.inst != 0) + return mapped; + } + Assert(false && "unmapped callee value ref"); + return v; } -typedef spl_ir_node_ref_t (*ref_xform_t)(void *ud, spl_ir_node_ref_t ref); +static spl_ir_node_ref_t rw_label(inline_ctx_t *c, spl_ir_node_ref_t ref) { + if (ref == 0) + return 0; + return spl_ir_value_inst_ref(rw_value(c, spl_ir_value_inst(ref))); +} -static void xform_vec(spl_ir_node_ref_vec_t *v, ref_xform_t fx, void *ud) { +typedef spl_ir_value_t (*value_xform_t)(void *ud, spl_ir_value_t v); + +static void xform_value_vec(spl_ir_value_vec_t *v, value_xform_t fx, void *ud) { for (usize i = 0; i < vec_size(*v); i++) vec_at(*v, i) = fx(ud, vec_at(*v, i)); } -/* 对节点的所有「值引用」字段应用 fx;块标签字段不在此列(由调用方单独处理)。 */ -static void xform_node_value_refs(spl_ir_node_t *n, ref_xform_t fx, void *ud) { +/* 对节点的所有「值」字段应用 fx;块标签字段由 rw_node 单独处理。 */ +static void xform_node_values(spl_ir_node_t *n, value_xform_t fx, void *ud) { switch (n->kind) { - case SPL_IR_ARITH_ADD: case SPL_IR_ARITH_SUB: case SPL_IR_ARITH_MUL: - case SPL_IR_ARITH_DIV: case SPL_IR_ARITH_REM: case SPL_IR_ARITH_NEG: - case SPL_IR_ARITH_ABS: case SPL_IR_ARITH_AND: case SPL_IR_ARITH_OR: - case SPL_IR_ARITH_XOR: case SPL_IR_ARITH_SHL: case SPL_IR_ARITH_SHR: + case SPL_IR_ARITH_ADD: + case SPL_IR_ARITH_SUB: + case SPL_IR_ARITH_MUL: + case SPL_IR_ARITH_DIV: + case SPL_IR_ARITH_REM: + case SPL_IR_ARITH_NEG: + case SPL_IR_ARITH_ABS: + case SPL_IR_ARITH_AND: + case SPL_IR_ARITH_OR: + case SPL_IR_ARITH_XOR: + case SPL_IR_ARITH_SHL: + case SPL_IR_ARITH_SHR: case SPL_IR_ARITH_NOT: n->arith.left = fx(ud, n->arith.left); n->arith.right = fx(ud, n->arith.right); break; - case SPL_IR_CMP_EQ: case SPL_IR_CMP_NE: case SPL_IR_CMP_LT: - case SPL_IR_CMP_LE: case SPL_IR_CMP_GT: case SPL_IR_CMP_GE: + case SPL_IR_CMP_EQ: + case SPL_IR_CMP_NE: + case SPL_IR_CMP_LT: + case SPL_IR_CMP_LE: + case SPL_IR_CMP_GT: + case SPL_IR_CMP_GE: n->cmp.a = fx(ud, n->cmp.a); n->cmp.b = fx(ud, n->cmp.b); break; - case SPL_IR_CAST_TRUNC: case SPL_IR_CAST_ZEXT: case SPL_IR_CAST_SEXT: - case SPL_IR_CAST_FEXT: case SPL_IR_CAST_FTRUNC: case SPL_IR_CAST_BITCAST: - case SPL_IR_CAST_PTR2INT: case SPL_IR_CAST_INT2PTR: case SPL_IR_CAST_BOOL2INT: - case SPL_IR_CASE_INT2FLOAT: case SPL_IR_CASE_FLOAT2INT: + case SPL_IR_CAST_TRUNC: + case SPL_IR_CAST_ZEXT: + case SPL_IR_CAST_SEXT: + case SPL_IR_CAST_FEXT: + case SPL_IR_CAST_FTRUNC: + case SPL_IR_CAST_BITCAST: + case SPL_IR_CAST_PTR2INT: + case SPL_IR_CAST_INT2PTR: + case SPL_IR_CAST_BOOL2INT: + case SPL_IR_CASE_INT2FLOAT: + case SPL_IR_CASE_FLOAT2INT: n->cast.val = fx(ud, n->cast.val); break; case SPL_IR_MEM_ALLOCA: n->mem_alloc.count = fx(ud, n->mem_alloc.count); break; - case SPL_IR_MEM_GLOBAL_ALLOC: - n->mem_global_alloc.const_node = fx(ud, n->mem_global_alloc.const_node); - break; case SPL_IR_MEM_LOAD: n->mem_load.ptr = fx(ud, n->mem_load.ptr); break; @@ -96,7 +120,7 @@ static void xform_node_value_refs(spl_ir_node_t *n, ref_xform_t fx, void *ud) { n->ir_offsetof.field_idx = fx(ud, n->ir_offsetof.field_idx); break; case SPL_IR_AGG_CONSTRUCT: - xform_vec(&n->agg_construct.fields, fx, ud); + xform_value_vec(&n->agg_construct.fields, fx, ud); break; case SPL_IR_AGG_EXTRACT: n->agg_extract.val = fx(ud, n->agg_extract.val); @@ -121,27 +145,24 @@ static void xform_node_value_refs(spl_ir_node_t *n, ref_xform_t fx, void *ud) { break; case SPL_IR_CONTROL_CALL: n->control_call.func = fx(ud, n->control_call.func); - xform_vec(&n->control_call.params, fx, ud); + xform_value_vec(&n->control_call.params, fx, ud); break; default: break; } } -static spl_ir_node_ref_t rw_thunk(void *ud, spl_ir_node_ref_t r) { - return rw((inline_ctx_t *)ud, r); -} +static spl_ir_value_t rw_thunk(void *ud, spl_ir_value_t v) { return rw_value((inline_ctx_t *)ud, v); } -/* 克隆体旧 ref -> 新 ref;块标签也参与平移。 */ static void rw_node(inline_ctx_t *c, spl_ir_node_t *n) { - xform_node_value_refs(n, rw_thunk, c); + xform_node_values(n, rw_thunk, c); switch (n->kind) { case SPL_IR_CONTROL_BR: - n->control_br.true_label = rw(c, n->control_br.true_label); - n->control_br.false_label = rw(c, n->control_br.false_label); + n->control_br.true_label = rw_label(c, n->control_br.true_label); + n->control_br.false_label = rw_label(c, n->control_br.false_label); break; case SPL_IR_CONTROL_JMP: - n->control_jmp.label = rw(c, n->control_jmp.label); + n->control_jmp.label = rw_label(c, n->control_jmp.label); break; default: break; @@ -166,6 +187,22 @@ static void clone_ref_vec(spl_ir_node_ref_vec_t *dst, const spl_ir_node_ref_vec_ dst->cap = n; } +static void clone_value_vec(spl_ir_value_vec_t *dst, const spl_ir_value_vec_t *src) { + dst->size = 0; + dst->cap = 0; + dst->data = NULL; + usize n = vec_size(*src); + if (n == 0) + return; + spl_ir_value_t *p = (spl_ir_value_t *)malloc(sizeof(spl_ir_value_t) * n); + if (!p) + return; + memcpy(p, src->data, sizeof(spl_ir_value_t) * n); + dst->data = p; + dst->size = n; + dst->cap = n; +} + static void clone_node(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t dst_ref, const spl_ir_node_t *src) { spl_ir_node_t *dst = nd(ir, caller, dst_ref); @@ -177,18 +214,20 @@ static void clone_node(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t clone_ref_vec((spl_ir_node_ref_vec_t *)&dst->attrs, (const spl_ir_node_ref_vec_t *)&src->attrs); if (src->kind == SPL_IR_AGG_CONSTRUCT) - clone_ref_vec(&dst->agg_construct.fields, &src->agg_construct.fields); + clone_value_vec(&dst->agg_construct.fields, &src->agg_construct.fields); else if (src->kind == SPL_IR_CONTROL_CALL) - clone_ref_vec(&dst->control_call.params, &src->control_call.params); + clone_value_vec(&dst->control_call.params, &src->control_call.params); } /* ---- 调用图可达性(防环) ---- */ static spl_ir_func_ref_t call_callee(spl_ir_t *ir, spl_ir_func_ref_t fid, spl_ir_node_t *call) { - spl_ir_node_t *fn = nd(ir, fid, call->control_call.func); - if (!fn || fn->kind != SPL_IR_TYPE_CONST) + (void)ir; + (void)fid; + spl_ir_value_t fn = call->control_call.func; + if (fn.kind != SPL_IR_VALUE_CONST) return 0; - return fn->type_const.fn; + return fn.fn; } static bool reaches(spl_ir_t *ir, spl_ir_func_ref_t from, spl_ir_func_ref_t target, @@ -234,38 +273,27 @@ static bool reaches_succ(spl_ir_t *ir, spl_ir_func_ref_t from, spl_ir_func_ref_t typedef struct { spl_ir_node_ref_t clone_ref; - spl_ir_node_ref_t val; + spl_ir_value_t val; } ret_rec_t; -static bool param_index(spl_ir_t *ir, spl_ir_func_ref_t fid, spl_ir_node_t *p, usize *out) { - spl_ir_node_t *idx = nd(ir, fid, p->control_param.idx); - if (!idx || idx->kind != SPL_IR_TYPE_CONST) +static bool param_index(spl_ir_node_t *p, usize *out) { + spl_ir_value_t idx = p->control_param.idx; + if (idx.kind != SPL_IR_VALUE_CONST) return false; - *out = idx->type_const.int_lit; + *out = (usize)idx.int_bits; return true; } -static spl_ir_node_ref_t find_const_src(spl_ir_t *ir, spl_ir_func_ref_t fid) { - spl_ir_func_t *f = spl_ir_func(ir, fid); - if (!f) - return 0; - for (usize i = 1; i < vec_size(f->nodes); i++) { - spl_ir_node_t *x = nd(ir, fid, i); - if (x && x->kind == SPL_IR_TYPE_CONST) - return i; - } - return 0; -} - -/* 值引用定点替换:等于 ci 的改为 ret_new。 */ typedef struct { spl_ir_node_ref_t ci; - spl_ir_node_ref_t ret_new; + spl_ir_value_t ret_new; } replace_one_t; -static spl_ir_node_ref_t replace_one(void *ud, spl_ir_node_ref_t r) { +static spl_ir_value_t replace_one(void *ud, spl_ir_value_t v) { replace_one_t *p = ud; - return r == p->ci ? p->ret_new : r; + if (v.kind == SPL_IR_VALUE_INST && v.inst == p->ci) + return p->ret_new; + return v; } static bool try_inline(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t ci, @@ -285,7 +313,6 @@ static bool try_inline(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t usize nargs = vec_size(call->control_call.params); - /* 统计参数与返回,分配动态映射。 */ usize cnodes = vec_size(calf->nodes); usize in_params = 0; usize in_rets = 0; @@ -306,100 +333,85 @@ static bool try_inline(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t c.ir = ir; c.caller = caller; c.callee = callee; - c.nparams = in_params; - c.param_ref = (spl_ir_node_ref_t *)malloc(sizeof(spl_ir_node_ref_t) * (in_params ? in_params : 1)); - c.param_arg = (spl_ir_node_ref_t *)malloc(sizeof(spl_ir_node_ref_t) * (in_params ? in_params : 1)); + c.val_map_size = cnodes; + c.val_map = (spl_ir_value_t *)calloc(cnodes ? cnodes : 1, sizeof(spl_ir_value_t)); ret_rec_t *recs = (ret_rec_t *)malloc(sizeof(ret_rec_t) * (in_rets ? in_rets : 1)); - if (!c.param_ref || !c.param_arg || !recs) { - free(c.param_ref); - free(c.param_arg); + if (!c.val_map || !recs) { + free(c.val_map); free(recs); return false; } - spl_type_id_t ret_tid = call->control_call.tid; + bool ok = true; - /* 参数映射(旧 ref -> 实参 ref)。 */ - usize pi = 0; - for (usize i = 1; i < cnodes; i++) { + /* 参数:直接映射到实参 Value。 */ + for (usize i = 1; i < cnodes && ok; i++) { spl_ir_node_t *x = nd(ir, callee, i); if (x && x->kind == SPL_IR_CONTROL_PARAM) { usize idx = 0; - if (!param_index(ir, callee, x, &idx) || idx >= nargs) { - free(c.param_ref); - free(c.param_arg); - free(recs); - return false; + if (!param_index(x, &idx) || idx >= nargs) { + ok = false; + break; } - c.param_ref[pi] = i; - c.param_arg[pi] = vec_at(call->control_call.params, idx); - pi++; + c.val_map[i] = vec_at(call->control_call.params, idx); } } + if (!ok) { + free(c.val_map); + free(recs); + return false; + } bool has_val = false; for (usize i = 1; i < cnodes; i++) { spl_ir_node_t *x = nd(ir, callee, i); - if (x && x->kind == SPL_IR_CONTROL_RET && x->control_ret.val != 0) + if (x && x->kind == SPL_IR_CONTROL_RET && !spl_ir_value_is_none(x->control_ret.val)) has_val = true; } - spl_ir_node_ref_t const_src = find_const_src(ir, callee); - if (has_val && const_src == 0) { - free(c.param_ref); - free(c.param_arg); - free(recs); - return false; - } - /* 释放调用点原 params(随后 ci 改为 jmp) */ + spl_type_id_t ret_tid = call->control_call.tid; + + /* 释放调用点原 params(实参已映射进 val_map)。 */ vec_free(call->control_call.params); - /* 克隆体入口前置:常量 1 + ret 槽 */ spl_ir_node_ref_t body_entry = 0; spl_ir_node_ref_t slot_ref = 0; if (has_val) { - spl_ir_node_ref_t c_ref = spl_ir_alloc_node(ir, caller); - clone_node(ir, caller, c_ref, nd(ir, callee, const_src)); - spl_ir_node_t *cn = nd(ir, caller, c_ref); - cn->kind = SPL_IR_TYPE_CONST; - cn->type_const.int_lit = 1; - slot_ref = spl_ir_alloc_node(ir, caller); spl_ir_node_t *sn = nd(ir, caller, slot_ref); memset(sn, 0, sizeof(*sn)); sn->kind = SPL_IR_MEM_ALLOCA; sn->mem_alloc.tid = ret_tid; - sn->mem_alloc.count = c_ref; - - body_entry = c_ref; + sn->mem_alloc.count = spl_ir_value_const_int(0, 1); + body_entry = slot_ref; } - c.shift = vec_size(cf->nodes) - 1; - if (!has_val) - body_entry = vec_size(cf->nodes); - - /* 克隆体 */ - usize nrec = 0; + /* 第一遍:为非 Param 节点分配目标下标,保证前向块标签可解析。 */ for (usize i = 1; i < cnodes; i++) { + spl_ir_node_t *src = nd(ir, callee, i); + if (!src || src->kind == SPL_IR_CONTROL_PARAM) + continue; spl_ir_node_ref_t dst_ref = spl_ir_alloc_node(ir, caller); if (dst_ref == 0) { - free(c.param_ref); - free(c.param_arg); + free(c.val_map); free(recs); return false; } + c.val_map[i] = spl_ir_value_inst(dst_ref); + if (body_entry == 0) + body_entry = dst_ref; + } + + /* 第二遍:复制内容、重写引用,并把 ret 改成 jmp 占位。 */ + usize nrec = 0; + for (usize i = 1; i < cnodes; i++) { spl_ir_node_t *src = nd(ir, callee, i); + if (!src || src->kind == SPL_IR_CONTROL_PARAM) + continue; + spl_ir_node_ref_t dst_ref = spl_ir_value_inst_ref(c.val_map[i]); clone_node(ir, caller, dst_ref, src); rw_node(&c, nd(ir, caller, dst_ref)); spl_ir_node_t *x = nd(ir, caller, dst_ref); - /* 克隆进来的 Param 使用处已替换为实参,节点本身不能留在调用者里 - * 改变其参数个数;退化为无害常量。 */ - if (src->kind == SPL_IR_CONTROL_PARAM) { - x->kind = SPL_IR_TYPE_CONST; - x->type_const.tid = 0; - x->type_const.int_lit = 0; - continue; - } if (x->kind == SPL_IR_CONTROL_RET) { recs[nrec].clone_ref = dst_ref; recs[nrec].val = x->control_ret.val; @@ -409,16 +421,15 @@ static bool try_inline(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t } } - spl_ir_node_ref_t ret_new = 0; + spl_ir_value_t ret_new = spl_ir_value_none(); if (has_val) { spl_ir_node_ref_t join = spl_ir_alloc_node(ir, caller); spl_ir_node_t *jn = nd(ir, caller, join); memset(jn, 0, sizeof(*jn)); jn->kind = SPL_IR_MEM_LOAD; jn->mem_load.tid = ret_tid; - jn->mem_load.ptr = slot_ref; + jn->mem_load.ptr = spl_ir_value_inst(slot_ref); - /* join 之后跳回调用点的后继(原代码此处靠 fallthrough)。 */ spl_ir_node_ref_t jc = spl_ir_alloc_node(ir, caller); spl_ir_node_t *jcn = nd(ir, caller, jc); memset(jcn, 0, sizeof(*jcn)); @@ -431,7 +442,7 @@ static bool try_inline(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t memset(stn, 0, sizeof(*stn)); stn->kind = SPL_IR_MEM_STORE; stn->mem_store.tid = ret_tid; - stn->mem_store.ptr = slot_ref; + stn->mem_store.ptr = spl_ir_value_inst(slot_ref); stn->mem_store.val = recs[k].val; spl_ir_node_ref_t jr = spl_ir_alloc_node(ir, caller); @@ -443,7 +454,7 @@ static bool try_inline(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t spl_ir_node_t *rn = nd(ir, caller, recs[k].clone_ref); rn->control_jmp.label = st; } - ret_new = join; + ret_new = spl_ir_value_inst(join); } else { for (usize k = 0; k < nrec; k++) { spl_ir_node_t *rn = nd(ir, caller, recs[k].clone_ref); @@ -461,11 +472,10 @@ static bool try_inline(spl_ir_t *ir, spl_ir_func_ref_t caller, spl_ir_node_ref_t for (usize j = 1; j < orig_len; j++) { spl_ir_node_t *x = nd(ir, caller, j); if (x) - xform_node_value_refs(x, replace_one, &rep); + xform_node_values(x, replace_one, &rep); } - free(c.param_ref); - free(c.param_arg); + free(c.val_map); free(recs); return true; } diff --git a/stage1/ir/spl_ir.c b/stage1/ir/spl_ir.c index ed742a0..df421e9 100644 --- a/stage1/ir/spl_ir.c +++ b/stage1/ir/spl_ir.c @@ -1,4 +1,6 @@ #include "spl_ir.h" +#include +#include const char *spl_ir_kind_name(spl_ir_kind_t kind) { static const char *const names[] = { @@ -11,9 +13,29 @@ const char *spl_ir_kind_name(spl_ir_kind_t kind) { return "?"; } +static usize const_key_hash(spl_ir_const_key_t k) { + usize h = MAP_HASH_INT((usize)k.tid); + for (usize i = 0; i < k.len; ++i) + h = ((h << 5) + h) ^ (usize)k.data[i]; + return h; +} + +static int const_key_cmp(spl_ir_const_key_t a, spl_ir_const_key_t b) { + if (a.tid != b.tid || a.len != b.len) + return 1; + if (a.len == 0) + return 0; + return memcmp(a.data, b.data, a.len); +} + void spl_ir_init(spl_ir_t *ir) { vec_init(ir->funcs); vec_push(ir->funcs, (spl_ir_func_t){0}); + vec_init(ir->consts); + vec_push(ir->consts, (spl_ir_const_t){0}); + vec_init(ir->globals); + vec_push(ir->globals, (spl_ir_global_t){0}); + map_init(ir->const_index, const_key_hash, const_key_cmp); } static void node_drop_vecs(spl_ir_node_t *n) { @@ -43,8 +65,19 @@ void spl_ir_drop(spl_ir_t *ir) { } vec_free(f->attrs); vec_free(f->nodes); + free((char *)f->name); } vec_free(ir->funcs); + for (usize i = 1; i < vec_size(ir->consts); ++i) { + spl_ir_const_t *c = &vec_at(ir->consts, i); + /* 当前池里只有字节块;大整数分支尚未产生,避免 union 双重释放。 */ + free(c->bytes.data); + } + vec_free(ir->consts); + for (usize i = 1; i < vec_size(ir->globals); ++i) + free((char *)vec_at(ir->globals, i).name); + vec_free(ir->globals); + map_free(ir->const_index); } spl_ir_func_ref_t spl_ir_alloc_fn(spl_ir_t *ir) { @@ -86,6 +119,125 @@ spl_ir_func_t *spl_ir_func(spl_ir_t *ir, spl_ir_func_ref_t fn_id) { return &vec_at(ir->funcs, fn_id); } +spl_ir_const_t *spl_ir_const(spl_ir_t *ir, usize const_id) { + if (!ir || const_id == 0 || const_id >= vec_size(ir->consts)) + return NULL; + return &vec_at(ir->consts, const_id); +} + +spl_ir_node_t *spl_ir_value_node(spl_ir_t *ir, spl_ir_func_ref_t fn_id, spl_ir_value_t v) { + if (v.kind != SPL_IR_VALUE_INST || v.inst == 0) + return NULL; + return spl_ir_node(ir, fn_id, v.inst); +} + +void spl_ir_node_visit_values(spl_ir_node_t *n, spl_ir_value_cb_t cb, void *ud) { + if (!n || !cb) + return; + switch (n->kind) { + case SPL_IR_ARITH_ADD: + case SPL_IR_ARITH_SUB: + case SPL_IR_ARITH_MUL: + case SPL_IR_ARITH_DIV: + case SPL_IR_ARITH_REM: + case SPL_IR_ARITH_NEG: + case SPL_IR_ARITH_ABS: + case SPL_IR_ARITH_AND: + case SPL_IR_ARITH_OR: + case SPL_IR_ARITH_XOR: + case SPL_IR_ARITH_SHL: + case SPL_IR_ARITH_SHR: + case SPL_IR_ARITH_NOT: + cb(ud, n->arith.left); + cb(ud, n->arith.right); + break; + case SPL_IR_CMP_EQ: + case SPL_IR_CMP_NE: + case SPL_IR_CMP_LT: + case SPL_IR_CMP_LE: + case SPL_IR_CMP_GT: + case SPL_IR_CMP_GE: + cb(ud, n->cmp.a); + cb(ud, n->cmp.b); + break; + case SPL_IR_CAST_TRUNC: + case SPL_IR_CAST_ZEXT: + case SPL_IR_CAST_SEXT: + case SPL_IR_CAST_FEXT: + case SPL_IR_CAST_FTRUNC: + case SPL_IR_CAST_BITCAST: + case SPL_IR_CAST_PTR2INT: + case SPL_IR_CAST_INT2PTR: + case SPL_IR_CAST_BOOL2INT: + case SPL_IR_CASE_INT2FLOAT: + case SPL_IR_CASE_FLOAT2INT: + cb(ud, n->cast.val); + break; + case SPL_IR_MEM_ALLOCA: + cb(ud, n->mem_alloc.count); + break; + case SPL_IR_MEM_LOAD: + cb(ud, n->mem_load.ptr); + break; + case SPL_IR_MEM_STORE: + cb(ud, n->mem_store.ptr); + cb(ud, n->mem_store.val); + break; + case SPL_IR_MEM_OFFSET: + cb(ud, n->mem_offset.ptr); + cb(ud, n->mem_offset.offset); + break; + case SPL_IR_MEM_FIELD_PTR: + cb(ud, n->mem_field_ptr.agg); + break; + case SPL_IR_MEM_COPY: + cb(ud, n->mem_copy.dst); + cb(ud, n->mem_copy.src); + cb(ud, n->mem_copy.size); + break; + case SPL_IR_MEM_SET: + cb(ud, n->mem_set.dst); + cb(ud, n->mem_set.val); + cb(ud, n->mem_set.size); + break; + case SPL_IR_TYPE_OFFSETOF: + cb(ud, n->ir_offsetof.field_idx); + break; + case SPL_IR_AGG_CONSTRUCT: + for (usize i = 0; i < vec_size(n->agg_construct.fields); ++i) + cb(ud, vec_at(n->agg_construct.fields, i)); + break; + case SPL_IR_AGG_EXTRACT: + cb(ud, n->agg_extract.val); + break; + case SPL_IR_AGG_INSERT: + cb(ud, n->agg_insert.agg); + cb(ud, n->agg_insert.field); + break; + case SPL_IR_CONTROL_SELECT: + cb(ud, n->control_select.cond); + cb(ud, n->control_select.true_val); + cb(ud, n->control_select.false_val); + break; + case SPL_IR_CONTROL_BR: + cb(ud, n->control_br.cond); + break; + case SPL_IR_CONTROL_PARAM: + cb(ud, n->control_param.idx); + break; + case SPL_IR_CONTROL_RET: + cb(ud, n->control_ret.val); + break; + case SPL_IR_CONTROL_CALL: + cb(ud, n->control_call.func); + for (usize i = 0; i < vec_size(n->control_call.params); ++i) + cb(ud, vec_at(n->control_call.params, i)); + break; + default: + break; + } +} + spl_attr_node_t *spl_ir_fn_extern_attr(const spl_attr_t *attr, const spl_ir_func_t *f) { if (!attr || !f) return NULL; @@ -111,6 +263,30 @@ static inline void ir_dump_ref(spl_ir_node_ref_t ref) { } } +static inline void ir_dump_value(const spl_type_t *ty, spl_ir_value_t v) { + if (v.kind == SPL_IR_VALUE_INST) { + ir_dump_ref(v.inst); + return; + } + if (v.kind == SPL_IR_VALUE_GLOBAL) { + printf(" g#%zu", v.global_id); + return; + } + spl_type_node_t *t = spl_type_node((spl_type_t *)ty, v.tid); + if (t && t->kind == SPL_TYPE_FLOAT) + printf(" float=%f", v.float_bits); + else if (t && t->kind == SPL_TYPE_FN) + printf(" fn#%zu", v.fn); + else if (t && t->kind == SPL_TYPE_PTR) + printf(" null"); + else if (t && t->kind == SPL_TYPE_INT && t->int_type.bits == 8) + printf(" char='%c'", (char)v.int_bits); + else if (t && (t->kind == SPL_TYPE_ARRAY)) + printf(" c#%zu", v.const_id); + else + printf(" int=%llu", (unsigned long long)v.int_bits); +} + static void ir_dump_node(const spl_type_t *ty, spl_ir_node_t *n) { switch (n->kind) { case SPL_IR_ARITH_ADD: @@ -124,14 +300,14 @@ static void ir_dump_node(const spl_type_t *ty, spl_ir_node_t *n) { case SPL_IR_ARITH_SHL: case SPL_IR_ARITH_SHR: ir_dump_type(ty, n->arith.tid); - ir_dump_ref(n->arith.left); - ir_dump_ref(n->arith.right); + ir_dump_value(ty, n->arith.left); + ir_dump_value(ty, n->arith.right); break; case SPL_IR_ARITH_NEG: case SPL_IR_ARITH_ABS: case SPL_IR_ARITH_NOT: ir_dump_type(ty, n->arith.tid); - ir_dump_ref(n->arith.left); + ir_dump_value(ty, n->arith.left); break; case SPL_IR_CMP_EQ: case SPL_IR_CMP_NE: @@ -140,8 +316,8 @@ static void ir_dump_node(const spl_type_t *ty, spl_ir_node_t *n) { case SPL_IR_CMP_GT: case SPL_IR_CMP_GE: ir_dump_type(ty, n->cmp.tid); - ir_dump_ref(n->cmp.a); - ir_dump_ref(n->cmp.b); + ir_dump_value(ty, n->cmp.a); + ir_dump_value(ty, n->cmp.b); break; case SPL_IR_CAST_TRUNC: case SPL_IR_CAST_ZEXT: @@ -158,64 +334,43 @@ static void ir_dump_node(const spl_type_t *ty, spl_ir_node_t *n) { spl_type_pure_dump((spl_type_t *)ty, n->cast.from_tid); printf(" -> "); spl_type_pure_dump((spl_type_t *)ty, n->cast.to_tid); - ir_dump_ref(n->cast.val); + ir_dump_value(ty, n->cast.val); break; case SPL_IR_MEM_ALLOCA: ir_dump_type(ty, n->mem_alloc.tid); - ir_dump_ref(n->mem_alloc.count); - break; - case SPL_IR_MEM_GLOBAL_ALLOC: - ir_dump_type(ty, n->mem_global_alloc.tid); - printf(" g#%zu", n->mem_global_alloc.const_node); + ir_dump_value(ty, n->mem_alloc.count); break; case SPL_IR_MEM_LOAD: ir_dump_type(ty, n->mem_load.tid); - ir_dump_ref(n->mem_load.ptr); + ir_dump_value(ty, n->mem_load.ptr); break; case SPL_IR_MEM_STORE: ir_dump_type(ty, n->mem_store.tid); - ir_dump_ref(n->mem_store.ptr); - ir_dump_ref(n->mem_store.val); + ir_dump_value(ty, n->mem_store.ptr); + ir_dump_value(ty, n->mem_store.val); break; case SPL_IR_MEM_OFFSET: ir_dump_type(ty, n->mem_offset.tid); - ir_dump_ref(n->mem_offset.ptr); - ir_dump_ref(n->mem_offset.offset); + ir_dump_value(ty, n->mem_offset.ptr); + ir_dump_value(ty, n->mem_offset.offset); break; case SPL_IR_MEM_FIELD_PTR: ir_dump_type(ty, n->mem_field_ptr.tid); - ir_dump_ref(n->mem_field_ptr.agg); + ir_dump_value(ty, n->mem_field_ptr.agg); printf(" #%zu", n->mem_field_ptr.field_idx); break; case SPL_IR_MEM_COPY: ir_dump_type(ty, n->mem_copy.tid); - ir_dump_ref(n->mem_copy.dst); - ir_dump_ref(n->mem_copy.src); - ir_dump_ref(n->mem_copy.size); + ir_dump_value(ty, n->mem_copy.dst); + ir_dump_value(ty, n->mem_copy.src); + ir_dump_value(ty, n->mem_copy.size); break; case SPL_IR_MEM_SET: ir_dump_type(ty, n->mem_set.tid); - ir_dump_ref(n->mem_set.dst); - ir_dump_ref(n->mem_set.val); - ir_dump_ref(n->mem_set.size); + ir_dump_value(ty, n->mem_set.dst); + ir_dump_value(ty, n->mem_set.val); + ir_dump_value(ty, n->mem_set.size); break; - case SPL_IR_TYPE_CONST: { - ir_dump_type(ty, n->type_const.tid); - spl_type_node_t *t = spl_type_node((spl_type_t *)ty, n->type_const.tid); - if (t && t->kind == SPL_TYPE_FLOAT) { - printf(" float=%f", n->type_const.float_lit); - } else if (t && t->kind == SPL_TYPE_FN) { - printf(" fn#%zu", n->type_const.fn); - } else if (t && t->kind == SPL_TYPE_ARRAY) { - /* 字节常量:不打印宿主指针(不确定),只标记 bytes。 */ - printf(" bytes"); - } else if (t && t->kind == SPL_TYPE_INT && t->int_type.bits == 8) { - printf(" char='%c'", n->type_const.ch_lit); - } else { - printf(" int=%zu", n->type_const.int_lit); - } - break; - } case SPL_IR_TYPE_BITSIZEOF: ir_dump_type(ty, n->bitsizeof.tid); break; @@ -227,34 +382,34 @@ static void ir_dump_node(const spl_type_t *ty, spl_ir_node_t *n) { break; case SPL_IR_TYPE_OFFSETOF: ir_dump_type(ty, n->ir_offsetof.tid); - ir_dump_ref(n->ir_offsetof.field_idx); + ir_dump_value(ty, n->ir_offsetof.field_idx); break; case SPL_IR_TYPE_FIELD_COUNT: ir_dump_type(ty, n->field_count.tid); break; case SPL_IR_AGG_CONSTRUCT: ir_dump_type(ty, n->agg_construct.tid); - vec_for(n->agg_construct.fields, i) ir_dump_ref(vec_at(n->agg_construct.fields, i)); + vec_for(n->agg_construct.fields, i) ir_dump_value(ty, vec_at(n->agg_construct.fields, i)); break; case SPL_IR_AGG_EXTRACT: ir_dump_type(ty, n->agg_extract.tid); printf(" #%zu", n->agg_extract.field_idx); - ir_dump_ref(n->agg_extract.val); + ir_dump_value(ty, n->agg_extract.val); break; case SPL_IR_AGG_INSERT: ir_dump_type(ty, n->agg_insert.tid); printf(" #%zu", n->agg_insert.field_idx); - ir_dump_ref(n->agg_insert.agg); - ir_dump_ref(n->agg_insert.field); + ir_dump_value(ty, n->agg_insert.agg); + ir_dump_value(ty, n->agg_insert.field); break; case SPL_IR_CONTROL_SELECT: ir_dump_type(ty, n->control_select.tid); - ir_dump_ref(n->control_select.cond); - ir_dump_ref(n->control_select.true_val); - ir_dump_ref(n->control_select.false_val); + ir_dump_value(ty, n->control_select.cond); + ir_dump_value(ty, n->control_select.true_val); + ir_dump_value(ty, n->control_select.false_val); break; case SPL_IR_CONTROL_BR: - ir_dump_ref(n->control_br.cond); + ir_dump_value(ty, n->control_br.cond); ir_dump_ref(n->control_br.true_label); ir_dump_ref(n->control_br.false_label); break; @@ -263,16 +418,16 @@ static void ir_dump_node(const spl_type_t *ty, spl_ir_node_t *n) { break; case SPL_IR_CONTROL_CALL: ir_dump_type(ty, n->control_call.tid); - ir_dump_ref(n->control_call.func); - vec_for(n->control_call.params, i) ir_dump_ref(vec_at(n->control_call.params, i)); + ir_dump_value(ty, n->control_call.func); + vec_for(n->control_call.params, i) ir_dump_value(ty, vec_at(n->control_call.params, i)); break; case SPL_IR_CONTROL_PARAM: ir_dump_type(ty, n->control_param.tid); - ir_dump_ref(n->control_param.idx); + ir_dump_value(ty, n->control_param.idx); break; case SPL_IR_CONTROL_RET: ir_dump_type(ty, n->control_ret.tid); - ir_dump_ref(n->control_ret.val); + ir_dump_value(ty, n->control_ret.val); break; case SPL_IR_CONTROL_UNREACHABLE: case SPL_IR_CONTROL_TRAP: @@ -297,6 +452,27 @@ static void ir_dump_attr_list(const spl_attr_t *attr, spl_attr_id_vec_t *ids) { } void spl_ir_dump(spl_ir_t *ir, const spl_type_t *ty, const spl_attr_t *attr) { + for (usize i = 1; i < vec_size(ir->consts); ++i) { + spl_ir_const_t *c = &vec_at(ir->consts, i); + printf("const c#%zu ", i); + spl_type_pure_dump((spl_type_t *)ty, c->tid); + spl_type_node_t *tn = spl_type_node((spl_type_t *)ty, c->tid); + if (tn && tn->kind == SPL_TYPE_ARRAY) + printf(" bytes=%zu", c->bytes.len); + printf("\n"); + } + for (usize i = 1; i < vec_size(ir->globals); ++i) { + spl_ir_global_t *g = &vec_at(ir->globals, i); + printf("global g#%zu %s ", i, g->readonly ? "const" : "var"); + spl_type_pure_dump((spl_type_t *)ty, g->tid); + if (g->name) + printf(" %s", g->name); + if (!spl_ir_value_is_none(g->init)) { + printf(" ="); + ir_dump_value(ty, g->init); + } + printf("\n"); + } vec_for(ir->funcs, i) { if (i == 0) continue; @@ -367,7 +543,7 @@ spl_ir_func_ref_t spl_ir_builder_fn_begin(spl_ir_builder_t *b, const char *name, } spl_ir_func_ref_t fid = spl_ir_alloc_fn(&b->ir); spl_ir_func_t *f = spl_ir_func(&b->ir, fid); - f->name = name; + f->name = name ? strdup(name) : NULL; vec_init(f->attrs); f->fn_tid = fn_tid; @@ -383,7 +559,7 @@ spl_ir_func_ref_t spl_ir_builder_fn_reserve(spl_ir_builder_t *b, const char *nam spl_type_id_t fn_tid) { spl_ir_func_ref_t fid = spl_ir_alloc_fn(&b->ir); spl_ir_func_t *f = spl_ir_func(&b->ir, fid); - f->name = name; + f->name = name ? strdup(name) : NULL; vec_init(f->attrs); f->fn_tid = fn_tid; return fid; @@ -458,86 +634,141 @@ void spl_ir_builder_block_end(spl_ir_builder_t *b) { } #define X(name, kind) \ - spl_ir_node_ref_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t x_tid, \ - spl_ir_node_ref_t x_left, spl_ir_node_ref_t x_right) { \ + spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t x_tid, \ + spl_ir_value_t x_left, spl_ir_value_t x_right) { \ spl_ir_node_ref_t ref = builder_alloc_node(builder, kind); \ spl_ir_node_t *n = spl_ir_node(&builder->ir, builder->current_fn, ref); \ n->arith.tid = x_tid; \ n->arith.left = x_left; \ n->arith.right = x_right; \ - return ref; \ + return spl_ir_value_inst(ref); \ } SPL_IR_BIN_ARITH_TABLE #undef X #define X(name, kind) \ - spl_ir_node_ref_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t x_tid, \ - spl_ir_node_ref_t x_val) { \ + spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t x_tid, \ + spl_ir_value_t x_val) { \ spl_ir_node_ref_t ref = builder_alloc_node(builder, kind); \ spl_ir_node_t *n = spl_ir_node(&builder->ir, builder->current_fn, ref); \ n->arith.tid = x_tid; \ n->arith.left = x_val; \ - n->arith.right = 0; \ - return ref; \ + n->arith.right = spl_ir_value_none(); \ + return spl_ir_value_inst(ref); \ } SPL_IR_UN_ARITH_TABLE #undef X #define X(name, kind) \ - spl_ir_node_ref_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t x_tid, \ - spl_ir_node_ref_t x_a, spl_ir_node_ref_t x_b) { \ + spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t x_tid, \ + spl_ir_value_t x_a, spl_ir_value_t x_b) { \ spl_ir_node_ref_t ref = builder_alloc_node(builder, kind); \ spl_ir_node_t *n = spl_ir_node(&builder->ir, builder->current_fn, ref); \ n->cmp.tid = x_tid; \ n->cmp.a = x_a; \ n->cmp.b = x_b; \ - return ref; \ + return spl_ir_value_inst(ref); \ } SPL_IR_CMP_TABLE #undef X #define X(name, kind) \ - spl_ir_node_ref_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t x_from, \ - spl_type_id_t x_to, spl_ir_node_ref_t x_val) { \ + spl_ir_value_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t x_from, \ + spl_type_id_t x_to, spl_ir_value_t x_val) { \ spl_ir_node_ref_t ref = builder_alloc_node(builder, kind); \ spl_ir_node_t *n = spl_ir_node(&builder->ir, builder->current_fn, ref); \ n->cast.from_tid = x_from; \ n->cast.to_tid = x_to; \ n->cast.val = x_val; \ - return ref; \ + return spl_ir_value_inst(ref); \ } SPL_IR_CAST_TABLE #undef X -spl_ir_node_ref_t spl_ir_builder_mem_alloca(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t count) { +spl_ir_value_t spl_ir_builder_mem_alloca(spl_ir_builder_t *b, spl_type_id_t tid, + spl_ir_value_t count) { spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_ALLOCA); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); n->mem_alloc.tid = tid; n->mem_alloc.count = count; - return ref; + return spl_ir_value_inst(ref); } -spl_ir_node_ref_t spl_ir_builder_mem_global_alloc(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t const_node) { - spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_GLOBAL_ALLOC); - spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); - n->mem_global_alloc.tid = tid; - n->mem_global_alloc.const_node = const_node; - return ref; +static usize const_pool_add(spl_ir_t *ir, spl_type_id_t tid, const void *data, usize len) { + spl_ir_const_key_t key = {.tid = tid, .data = data, .len = len}; + usize id = 0; + if (map_get(ir->const_index, key, &id)) + return id; + + spl_ir_const_t entry = {0}; + entry.tid = tid; + entry.bytes.len = len; + entry.bytes.data = malloc(len ? len : 1); + if (!entry.bytes.data) + return 0; + if (len) + memcpy(entry.bytes.data, data, len); + vec_push(ir->consts, entry); + id = vec_size(ir->consts) - 1; + + /* 去重键必须指向池内稳定副本,调用方的指针可能马上失效。 */ + key.data = entry.bytes.data; + map_put(ir->const_index, key, id); + return id; } -spl_ir_node_ref_t spl_ir_builder_mem_load(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t ptr) { +spl_ir_value_t spl_ir_builder_const_bytes(spl_ir_builder_t *b, spl_type_id_t tid, const void *data, + usize len) { + usize id = const_pool_add(&b->ir, tid, data, len); + return spl_ir_value_const_pool(tid, id); +} + +spl_ir_value_t spl_ir_builder_const_int(spl_type_id_t tid, uint64_t bits) { + return spl_ir_value_const_int(tid, bits); +} + +spl_ir_value_t spl_ir_builder_const_bool(spl_type_id_t tid, bool val) { + return spl_ir_value_const_int(tid, val ? 1 : 0); +} + +spl_ir_value_t spl_ir_builder_const_float(spl_type_id_t tid, double val) { + return spl_ir_value_const_float(tid, val); +} + +spl_ir_value_t spl_ir_builder_const_null(spl_type_id_t tid) { + return spl_ir_value_const_null(tid); +} + +spl_ir_value_t spl_ir_builder_const_fn(spl_type_id_t tid, spl_ir_func_ref_t fn) { + return spl_ir_value_const_fn(tid, fn); +} + +spl_ir_value_t spl_ir_builder_global_addr(spl_type_id_t ptr_tid, usize global_id) { + return spl_ir_value_global(ptr_tid, global_id); +} + +usize spl_ir_builder_global_declare(spl_ir_builder_t *b, const char *name, spl_type_id_t tid, + spl_ir_value_t init, bool readonly) { + spl_ir_global_t g = { + .name = name ? strdup(name) : NULL, + .tid = tid, + .init = init, + .readonly = readonly, + }; + vec_push(b->ir.globals, g); + return vec_size(b->ir.globals) - 1; +} + +spl_ir_value_t spl_ir_builder_mem_load(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t ptr) { spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_LOAD); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); n->mem_load.tid = tid; n->mem_load.ptr = ptr; - return ref; + return spl_ir_value_inst(ref); } spl_ir_node_ref_t spl_ir_builder_mem_store(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t ptr, spl_ir_node_ref_t val) { + spl_ir_value_t ptr, spl_ir_value_t val) { spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_STORE); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); n->mem_store.tid = tid; @@ -546,29 +777,28 @@ spl_ir_node_ref_t spl_ir_builder_mem_store(spl_ir_builder_t *b, spl_type_id_t ti return ref; } -spl_ir_node_ref_t spl_ir_builder_mem_offset(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t ptr, spl_ir_node_ref_t offset) { +spl_ir_value_t spl_ir_builder_mem_offset(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t ptr, + spl_ir_value_t offset) { spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_OFFSET); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); n->mem_offset.tid = tid; n->mem_offset.ptr = ptr; n->mem_offset.offset = offset; - return ref; + return spl_ir_value_inst(ref); } -spl_ir_node_ref_t spl_ir_builder_mem_field(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t agg, usize field_idx) { +spl_ir_value_t spl_ir_builder_mem_field(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t agg, + usize field_idx) { spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_FIELD_PTR); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); n->mem_field_ptr.tid = tid; n->mem_field_ptr.agg = agg; n->mem_field_ptr.field_idx = field_idx; - return ref; + return spl_ir_value_inst(ref); } -spl_ir_node_ref_t spl_ir_builder_mem_copy(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t dst, spl_ir_node_ref_t src, - spl_ir_node_ref_t size) { +spl_ir_node_ref_t spl_ir_builder_mem_copy(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t dst, + spl_ir_value_t src, spl_ir_value_t size) { spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_COPY); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); n->mem_copy.tid = tid; @@ -578,9 +808,8 @@ spl_ir_node_ref_t spl_ir_builder_mem_copy(spl_ir_builder_t *b, spl_type_id_t tid return ref; } -spl_ir_node_ref_t spl_ir_builder_mem_set(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t dst, spl_ir_node_ref_t val, - spl_ir_node_ref_t size) { +spl_ir_node_ref_t spl_ir_builder_mem_set(spl_ir_builder_t *b, spl_type_id_t tid, spl_ir_value_t dst, + spl_ir_value_t val, spl_ir_value_t size) { spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_MEM_SET); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); n->mem_set.tid = tid; @@ -590,101 +819,41 @@ spl_ir_node_ref_t spl_ir_builder_mem_set(spl_ir_builder_t *b, spl_type_id_t tid, return ref; } -spl_ir_node_ref_t spl_ir_builder_type_const_bool(spl_ir_builder_t *b, spl_type_id_t tid, bool val) { - spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_CONST); - spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); - n->type_const.tid = tid; - n->type_const.bool_lit = val; - return ref; -} - -spl_ir_node_ref_t spl_ir_builder_type_const_ptr(spl_ir_builder_t *b, spl_type_id_t tid, void *val) { - spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_CONST); - spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); - n->type_const.tid = tid; - n->type_const.ptr_lit = val; - return ref; -} - -spl_ir_node_ref_t spl_ir_builder_type_const_int(spl_ir_builder_t *b, spl_type_id_t tid, usize val) { - spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_CONST); - spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); - n->type_const.tid = tid; - n->type_const.int_lit = val; - return ref; -} - -spl_ir_node_ref_t spl_ir_builder_type_const_float(spl_ir_builder_t *b, spl_type_id_t tid, - double val) { - spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_CONST); - spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); - n->type_const.tid = tid; - n->type_const.float_lit = val; - return ref; -} - -spl_ir_node_ref_t spl_ir_builder_type_const_bytes(spl_ir_builder_t *b, spl_type_id_t tid, - const void *data, usize len) { - (void)len; - spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_CONST); - spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); - n->type_const.tid = tid; - n->type_const.ptr_lit = (void *)data; - return ref; -} - -spl_ir_node_ref_t spl_ir_builder_type_const_char(spl_ir_builder_t *b, spl_type_id_t tid, char val) { - spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_CONST); - spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); - n->type_const.tid = tid; - n->type_const.ch_lit = val; - return ref; -} - -spl_ir_node_ref_t spl_ir_builder_type_const_fn(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_func_ref_t val) { - spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_CONST); - spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); - n->type_const.tid = tid; - n->type_const.fn = val; - return ref; -} - -spl_ir_node_ref_t spl_ir_builder_type_bitsizeof(spl_ir_builder_t *b, spl_type_id_t tid) { +spl_ir_value_t spl_ir_builder_type_bitsizeof(spl_ir_builder_t *b, spl_type_id_t tid) { spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_BITSIZEOF); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); n->bitsizeof.tid = tid; - return ref; + return spl_ir_value_inst(ref); } -spl_ir_node_ref_t spl_ir_builder_type_sizeof(spl_ir_builder_t *b, spl_type_id_t tid) { +spl_ir_value_t spl_ir_builder_type_sizeof(spl_ir_builder_t *b, spl_type_id_t tid) { spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_SIZEOF); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); n->ir_sizeof.tid = tid; - return ref; + return spl_ir_value_inst(ref); } -spl_ir_node_ref_t spl_ir_builder_type_alignof(spl_ir_builder_t *b, spl_type_id_t tid) { +spl_ir_value_t spl_ir_builder_type_alignof(spl_ir_builder_t *b, spl_type_id_t tid) { spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_ALIGNOF); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); n->ir_alignof.tid = tid; - return ref; + return spl_ir_value_inst(ref); } -spl_ir_node_ref_t spl_ir_builder_type_offsetof(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t field_idx) { +spl_ir_value_t spl_ir_builder_type_offsetof(spl_ir_builder_t *b, spl_type_id_t tid, + spl_ir_value_t field_idx) { spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_OFFSETOF); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); n->ir_offsetof.tid = tid; n->ir_offsetof.field_idx = field_idx; - return ref; + return spl_ir_value_inst(ref); } -spl_ir_node_ref_t spl_ir_builder_type_field_count(spl_ir_builder_t *b, spl_type_id_t tid) { +spl_ir_value_t spl_ir_builder_type_field_count(spl_ir_builder_t *b, spl_type_id_t tid) { spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_TYPE_FIELD_COUNT); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); n->field_count.tid = tid; - return ref; + return spl_ir_value_inst(ref); } spl_ir_node_ref_t spl_ir_builder_agg_construct(spl_ir_builder_t *b, spl_type_id_t tid) { @@ -696,48 +865,48 @@ spl_ir_node_ref_t spl_ir_builder_agg_construct(spl_ir_builder_t *b, spl_type_id_ } void spl_ir_builder_agg_construct_field(spl_ir_builder_t *b, spl_ir_node_ref_t node, - spl_ir_node_ref_t field) { + spl_ir_value_t field) { spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, node); Assert(n != NULL); vec_push(n->agg_construct.fields, field); } -spl_ir_node_ref_t spl_ir_builder_agg_extract(spl_ir_builder_t *b, spl_type_id_t tid, - spl_type_id_t field_tid, usize field_idx, - spl_ir_node_ref_t val) { +spl_ir_value_t spl_ir_builder_agg_extract(spl_ir_builder_t *b, spl_type_id_t tid, + spl_type_id_t field_tid, usize field_idx, + spl_ir_value_t val) { spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_AGG_EXTRACT); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); n->agg_extract.tid = tid; n->agg_extract.field_tid = field_tid; n->agg_extract.field_idx = field_idx; n->agg_extract.val = val; - return ref; + return spl_ir_value_inst(ref); } -spl_ir_node_ref_t spl_ir_builder_agg_insert(spl_ir_builder_t *b, spl_type_id_t tid, usize field_idx, - spl_ir_node_ref_t agg, spl_ir_node_ref_t field) { +spl_ir_value_t spl_ir_builder_agg_insert(spl_ir_builder_t *b, spl_type_id_t tid, usize field_idx, + spl_ir_value_t agg, spl_ir_value_t field) { spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_AGG_INSERT); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); n->agg_insert.tid = tid; n->agg_insert.field_idx = field_idx; n->agg_insert.agg = agg; n->agg_insert.field = field; - return ref; + return spl_ir_value_inst(ref); } -spl_ir_node_ref_t spl_ir_builder_control_select(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t cond, spl_ir_node_ref_t true_val, - spl_ir_node_ref_t false_val) { +spl_ir_value_t spl_ir_builder_control_select(spl_ir_builder_t *b, spl_type_id_t tid, + spl_ir_value_t cond, spl_ir_value_t true_val, + spl_ir_value_t false_val) { spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_CONTROL_SELECT); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); n->control_select.tid = tid; n->control_select.cond = cond; n->control_select.true_val = true_val; n->control_select.false_val = false_val; - return ref; + return spl_ir_value_inst(ref); } -spl_ir_node_ref_t spl_ir_builder_control_br(spl_ir_builder_t *b, spl_ir_node_ref_t cond) { +spl_ir_node_ref_t spl_ir_builder_control_br(spl_ir_builder_t *b, spl_ir_value_t cond) { spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_CONTROL_BR); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); n->control_br.cond = cond; @@ -778,7 +947,7 @@ void spl_ir_builder_control_jmp_fixup(spl_ir_builder_t *b, spl_ir_node_ref_t ref } spl_ir_node_ref_t spl_ir_builder_control_call(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t func) { + spl_ir_value_t func) { spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_CONTROL_CALL); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); n->control_call.tid = tid; @@ -788,23 +957,23 @@ spl_ir_node_ref_t spl_ir_builder_control_call(spl_ir_builder_t *b, spl_type_id_t } void spl_ir_builder_control_call_param(spl_ir_builder_t *b, spl_ir_node_ref_t call_node, - spl_ir_node_ref_t arg) { + spl_ir_value_t arg) { spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, call_node); Assert(n != NULL); vec_push(n->control_call.params, arg); } -spl_ir_node_ref_t spl_ir_builder_control_param(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t idx) { +spl_ir_value_t spl_ir_builder_control_param(spl_ir_builder_t *b, spl_type_id_t tid, + spl_ir_value_t idx) { spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_CONTROL_PARAM); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); n->control_param.tid = tid; n->control_param.idx = idx; - return ref; + return spl_ir_value_inst(ref); } spl_ir_node_ref_t spl_ir_builder_control_ret(spl_ir_builder_t *b, spl_type_id_t tid, - spl_ir_node_ref_t val) { + spl_ir_value_t val) { spl_ir_node_ref_t ref = builder_alloc_node(b, SPL_IR_CONTROL_RET); spl_ir_node_t *n = spl_ir_node(&b->ir, b->current_fn, ref); n->control_ret.tid = tid; diff --git a/stage1/lower/spl_ast2ir.c b/stage1/lower/spl_ast2ir.c index 7864396..49d7d5a 100644 --- a/stage1/lower/spl_ast2ir.c +++ b/stage1/lower/spl_ast2ir.c @@ -1,5 +1,6 @@ #include "spl_ast2ir.h" #include "spl_intrin.h" +#include static spl_type_id_t to_ir_type(spl_ast2ir_t *a, spl_lang_type_id_t lang) { if (!lang) @@ -15,17 +16,17 @@ static spl_type_id_t node_ir(spl_ast2ir_t *a, spl_ast_node_t *n) { return to_ir_type(a, node_lang(n)); } -static spl_ir_node_ref_t const_usize(spl_ast2ir_t *a, usize v) { +static spl_ir_value_t const_usize(spl_ast2ir_t *a, usize v) { spl_lang_type_id_t t = spl_lang_make_int(a->sema->type, sizeof(void *) * 8, 0); - return spl_ir_builder_type_const_int(a->ir, to_ir_type(a, t), v); + return spl_ir_value_const_int(to_ir_type(a, t), v); } -static spl_ir_node_ref_t emit_if_like(spl_ast2ir_t *a, spl_ast_node_ref_t ref); -static spl_ir_node_ref_t emit_loop_like(spl_ast2ir_t *a, spl_ast_node_ref_t ref); +static spl_ir_value_t emit_if_like(spl_ast2ir_t *a, spl_ast_node_ref_t ref); +static spl_ir_value_t emit_loop_like(spl_ast2ir_t *a, spl_ast_node_ref_t ref); static void emit_break_stmt(spl_ast2ir_t *a, spl_ast_node_t *n); -static spl_ir_node_ref_t emit_expr(spl_ast2ir_t *a, spl_ast_node_ref_t ref); -static spl_ir_node_ref_t emit_lvalue(spl_ast2ir_t *a, spl_ast_node_ref_t ref); +static spl_ir_value_t emit_expr(spl_ast2ir_t *a, spl_ast_node_ref_t ref); +static spl_ir_value_t emit_lvalue(spl_ast2ir_t *a, spl_ast_node_ref_t ref); static void emit_stmt(spl_ast2ir_t *a, spl_ast_node_ref_t ref); /* Emit one exit path without consuming the lexical registrations needed by * sibling paths. A private stack also allows cleanup bodies to register defer. */ @@ -48,60 +49,60 @@ static void emit_deferred_until(spl_ast2ir_t *a, usize base) { } static void emit_decl(spl_ast2ir_t *a, spl_ast_node_ref_t ref); -static spl_ir_node_ref_t emit_var_decl(spl_ast2ir_t *a, spl_ast_node_t *n); +static spl_ir_value_t emit_var_decl(spl_ast2ir_t *a, spl_ast_node_t *n); static void emit_pattern_bind(spl_ast2ir_t *a, spl_ast_node_ref_t pat_ref, - spl_ir_node_ref_t src_ptr, spl_type_id_t tid, int place_mode); + spl_ir_value_t src_ptr, spl_type_id_t tid, int place_mode); static int pattern_has_ref(spl_ast2ir_t *a, spl_ast_node_ref_t pat_ref); -static spl_ir_node_ref_t emit_match_check(spl_ast2ir_t *a, spl_ast_node_ref_t pat_ref, - spl_type_id_t scrut_ir, spl_ir_node_ref_t scrut_slot); +static spl_ir_value_t emit_match_check(spl_ast2ir_t *a, spl_ast_node_ref_t pat_ref, + spl_type_id_t scrut_ir, spl_ir_value_t scrut_slot); static void emit_fn_reserve(spl_ast2ir_t *a, spl_ast_node_t *n); static void emit_fn_body(spl_ast2ir_t *a, spl_ast_node_t *n); static void emit_container_reserve(spl_ast2ir_t *a, spl_ast_node_ref_t ref); static void emit_container_body(spl_ast2ir_t *a, spl_ast_node_ref_t ref); -static spl_ir_node_ref_t ast2ir_emit_cast_builtin(spl_ast2ir_t *a, spl_ast_node_t *n, - spl_intrin_t in, spl_type_id_t ft, - spl_type_id_t tt, spl_ir_node_ref_t v); -static spl_ir_node_ref_t emit_cast(spl_ast2ir_t *a, spl_ast_node_t *n); -static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref); -static spl_ir_node_ref_t emit_implicit_cast(spl_ast2ir_t *a, spl_ast_node_t *n, spl_type_id_t ft, - spl_type_id_t tt, spl_ir_node_ref_t v); +static spl_ir_value_t ast2ir_emit_cast_builtin(spl_ast2ir_t *a, spl_ast_node_t *n, + spl_intrin_t in, spl_type_id_t ft, spl_type_id_t tt, + spl_ir_value_t v); +static spl_ir_value_t emit_cast(spl_ast2ir_t *a, spl_ast_node_t *n); +static spl_ir_value_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref); +static spl_ir_value_t emit_implicit_cast(spl_ast2ir_t *a, spl_ast_node_t *n, spl_type_id_t ft, + spl_type_id_t tt, spl_ir_value_t v); -static spl_ir_node_ref_t ast2ir_intrin_call(spl_ast2ir_t *a, spl_ast_node_t *n, spl_intrin_t in, - spl_ast_node_ref_vec_t *args); -static spl_ir_node_ref_t ast2ir_intrin_field(spl_ast2ir_t *a, spl_ast_node_t *n, spl_intrin_t in); -static spl_ir_node_ref_t ast2ir_intrin_len(spl_ast2ir_t *a, spl_ast_node_t *n); -static spl_ir_node_ref_t ast2ir_intrin_pos(spl_ast2ir_t *a, spl_ast_node_t *n, int which); -static spl_ir_node_ref_t ast2ir_intrin_file_name(spl_ast2ir_t *a, spl_ast_node_t *n); -static spl_ir_node_ref_t ast2ir_intrin_fn_name(spl_ast2ir_t *a, spl_ast_node_t *n); +static spl_ir_value_t ast2ir_intrin_call(spl_ast2ir_t *a, spl_ast_node_t *n, spl_intrin_t in, + spl_ast_node_ref_vec_t *args); +static spl_ir_value_t ast2ir_intrin_field(spl_ast2ir_t *a, spl_ast_node_t *n, spl_intrin_t in); +static spl_ir_value_t ast2ir_intrin_len(spl_ast2ir_t *a, spl_ast_node_t *n); +static spl_ir_value_t ast2ir_intrin_pos(spl_ast2ir_t *a, spl_ast_node_t *n, int which); +static spl_ir_value_t ast2ir_intrin_file_name(spl_ast2ir_t *a, spl_ast_node_t *n); +static spl_ir_value_t ast2ir_intrin_fn_name(spl_ast2ir_t *a, spl_ast_node_t *n); -static spl_ir_node_ref_t emit_zero_value(spl_ast2ir_t *a, spl_type_id_t tid) { +static spl_ir_value_t emit_zero_value(spl_ast2ir_t *a, spl_type_id_t tid) { spl_type_node_t *tn = spl_type_node(&a->sema->type->ir, tid); if (!tn) - return 0; + return spl_ir_value_none(); spl_type_node_kind_t tk = tn->kind; switch (tk) { case SPL_TYPE_BOOL: - return spl_ir_builder_type_const_bool(a->ir, tid, false); + return spl_ir_builder_const_bool(tid, false); case SPL_TYPE_INT: - return spl_ir_builder_type_const_int(a->ir, tid, 0); + return spl_ir_builder_const_int(tid, 0); case SPL_TYPE_FLOAT: - return spl_ir_builder_type_const_float(a->ir, tid, 0.0); + return spl_ir_builder_const_float(tid, 0.0); case SPL_TYPE_PTR: case SPL_TYPE_FN: - return spl_ir_builder_type_const_ptr(a->ir, tid, NULL); + return spl_ir_builder_const_null(tid); case SPL_TYPE_ARRAY: { usize arr_len = tn->array_type.len; spl_type_id_t arr_elem = tn->array_type.element; spl_ir_node_ref_t node = spl_ir_builder_agg_construct(a->ir, tid); for (usize i = 0; i < arr_len; ++i) { - spl_ir_node_ref_t f = emit_zero_value(a, arr_elem); - if (!f) - return 0; + spl_ir_value_t f = emit_zero_value(a, arr_elem); + if (spl_ir_value_is_none(f)) + return spl_ir_value_none(); spl_ir_builder_agg_construct_field(a->ir, node, f); } - return node; + return spl_ir_value_inst(node); } case SPL_TYPE_STRUCT: case SPL_TYPE_SHAPE: { @@ -113,30 +114,44 @@ static spl_ir_node_ref_t emit_zero_value(spl_ast2ir_t *a, spl_type_id_t tid) { spl_ir_node_ref_t node = spl_ir_builder_agg_construct(a->ir, tid); for (usize i = 0; i < nfield; ++i) { - spl_ir_node_ref_t f = emit_zero_value(a, vec_at(field_tids, i)); - if (!f) { + spl_ir_value_t f = emit_zero_value(a, vec_at(field_tids, i)); + if (spl_ir_value_is_none(f)) { vec_free(field_tids); - return 0; + return spl_ir_value_none(); } spl_ir_builder_agg_construct_field(a->ir, node, f); } vec_free(field_tids); - return node; + return spl_ir_value_inst(node); } case SPL_TYPE_UNION: { usize nfield = vec_size(tn->agg_type.variants); if (nfield == 0) - return spl_ir_builder_agg_construct(a->ir, tid); + return spl_ir_value_inst(spl_ir_builder_agg_construct(a->ir, tid)); spl_type_id_t first_tid = vec_at(tn->agg_type.variants, 0).type_id; spl_ir_node_ref_t node = spl_ir_builder_agg_construct(a->ir, tid); - spl_ir_node_ref_t f = emit_zero_value(a, first_tid); - if (!f) - return 0; + spl_ir_value_t f = emit_zero_value(a, first_tid); + if (spl_ir_value_is_none(f)) + return spl_ir_value_none(); spl_ir_builder_agg_construct_field(a->ir, node, f); - return node; + return spl_ir_value_inst(node); + } + case SPL_TYPE_ENUM: { + spl_ir_node_ref_t node = spl_ir_builder_agg_construct(a->ir, tid); + spl_ir_builder_agg_construct_field( + a->ir, node, spl_ir_value_const_int(tn->agg_type.tag_type, 0)); + usize nfield = vec_size(tn->agg_type.variants); + for (usize i = 0; i < nfield; ++i) { + spl_type_id_t ftid = vec_at(tn->agg_type.variants, i).type_id; + spl_ir_value_t f = ftid ? emit_zero_value(a, ftid) : spl_ir_value_none(); + if (ftid && spl_ir_value_is_none(f)) + return spl_ir_value_none(); + spl_ir_builder_agg_construct_field(a->ir, node, f); + } + return spl_ir_value_inst(node); } default: - return 0; + return spl_ir_value_none(); } } @@ -184,8 +199,8 @@ static int pattern_has_ref(spl_ast2ir_t *a, spl_ast_node_ref_t pat_ref) { UB 语义:不做 tag 检查,调用者保证变体正确。 */ static void emit_pattern_bind(spl_ast2ir_t *a, spl_ast_node_ref_t pat_ref, - spl_ir_node_ref_t src_ptr, spl_type_id_t tid, int place_mode) { - if (!pat_ref || !src_ptr) + spl_ir_value_t src_ptr, spl_type_id_t tid, int place_mode) { + if (!pat_ref || spl_ir_value_is_none(src_ptr)) return; spl_ast_node_t *pat = spl_ast_node(a->ast, pat_ref); if (!pat) @@ -196,9 +211,8 @@ static void emit_pattern_bind(spl_ast2ir_t *a, spl_ast_node_ref_t pat_ref, if (!pat->resolved_def_id) return; if (place_mode) { - spl_ir_node_ref_t slot = - spl_ir_builder_mem_alloca(a->ir, tid, const_usize(a, 1)); - spl_ir_node_ref_t v = spl_ir_builder_mem_load(a->ir, tid, src_ptr); + spl_ir_value_t slot = spl_ir_builder_mem_alloca(a->ir, tid, const_usize(a, 1)); + spl_ir_value_t v = spl_ir_builder_mem_load(a->ir, tid, src_ptr); spl_ir_builder_mem_store(a->ir, tid, slot, v); map_put(a->def2ir_map, pat->resolved_def_id, slot); } else { @@ -213,8 +227,7 @@ static void emit_pattern_bind(spl_ast2ir_t *a, spl_ast_node_ref_t pat_ref, spl_type_id_t ptr_tid = in ? node_ir(a, in) : 0; if (!ptr_tid) ptr_tid = spl_type_builder_ptr(&a->sema->type->ir, tid); - spl_ir_node_ref_t slot = - spl_ir_builder_mem_alloca(a->ir, ptr_tid, const_usize(a, 1)); + spl_ir_value_t slot = spl_ir_builder_mem_alloca(a->ir, ptr_tid, const_usize(a, 1)); spl_ir_builder_mem_store(a->ir, ptr_tid, slot, src_ptr); emit_pattern_bind(a, inner, slot, ptr_tid, 0); return; @@ -246,7 +259,7 @@ static void emit_pattern_bind(spl_ast2ir_t *a, spl_ast_node_ref_t pat_ref, return; for (usize i = 0; i < n; ++i) { spl_type_id_t ft = vec_at(tn->agg_type.variants, i).type_id; - spl_ir_node_ref_t fp = spl_ir_builder_mem_field(a->ir, tid, src_ptr, i); + spl_ir_value_t fp = spl_ir_builder_mem_field(a->ir, tid, src_ptr, i); emit_pattern_bind(a, vec_at(pat->pattern_tuple.patterns, i), fp, ft, place_mode); } return; @@ -263,7 +276,7 @@ static void emit_pattern_bind(spl_ast2ir_t *a, spl_ast_node_ref_t pat_ref, if (vidx >= vec_size(tn->agg_type.variants)) return; spl_type_id_t payload_ir = vec_at(tn->agg_type.variants, vidx).type_id; - spl_ir_node_ref_t pptr = spl_ir_builder_mem_field(a->ir, tid, src_ptr, vidx + 1); + spl_ir_value_t pptr = spl_ir_builder_mem_field(a->ir, tid, src_ptr, vidx + 1); if (pat->pattern_dot.sub_pattern) emit_pattern_bind(a, pat->pattern_dot.sub_pattern, pptr, payload_ir, place_mode); return; @@ -281,7 +294,7 @@ static void emit_pattern_bind(spl_ast2ir_t *a, spl_ast_node_ref_t pat_ref, if (idx >= vec_size(tn->agg_type.variants)) continue; spl_type_id_t ft = vec_at(tn->agg_type.variants, idx).type_id; - spl_ir_node_ref_t fp = spl_ir_builder_mem_field(a->ir, tid, src_ptr, idx); + spl_ir_value_t fp = spl_ir_builder_mem_field(a->ir, tid, src_ptr, idx); if (f->pattern_field.pattern) emit_pattern_bind(a, f->pattern_field.pattern, fp, ft, place_mode); } @@ -295,27 +308,29 @@ static void emit_pattern_bind(spl_ast2ir_t *a, spl_ast_node_ref_t pat_ref, /* 计算 pattern 对 scrutinee 的匹配条件。 返回 0 表示无条件命中(IDENT / UNDERSCORE),否则返回 bool 节点。 */ -static spl_ir_node_ref_t emit_match_check(spl_ast2ir_t *a, spl_ast_node_ref_t pat_ref, - spl_type_id_t scrut_ir, spl_ir_node_ref_t scrut_slot) { +static spl_ir_value_t emit_match_check(spl_ast2ir_t *a, spl_ast_node_ref_t pat_ref, + spl_type_id_t scrut_ir, spl_ir_value_t scrut_slot) { spl_ast_node_t *pat = spl_ast_node(a->ast, pat_ref); if (!pat) - return 0; + return spl_ir_value_none(); switch (pat->kind) { case SPL_AST_PATTERN_UNDERSCORE: case SPL_AST_PATTERN_IDENT: - return 0; + return spl_ir_value_none(); case SPL_AST_PATTERN_LIST: { spl_lang_type_id_t bt = spl_lang_make_bool(a->sema->type); spl_type_id_t bt_ir = to_ir_type(a, bt); - spl_ir_node_ref_t combined = 0; + spl_ir_value_t combined = spl_ir_value_none(); for (usize i = 0; i < vec_size(pat->pattern_list.patterns); ++i) { - spl_ir_node_ref_t c = + spl_ir_value_t c = emit_match_check(a, vec_at(pat->pattern_list.patterns, i), scrut_ir, scrut_slot); - if (!c) - return 0; - combined = combined ? spl_ir_builder_arith_or(a->ir, bt_ir, combined, c) : c; + if (spl_ir_value_is_none(c)) + return spl_ir_value_none(); + combined = spl_ir_value_is_none(combined) + ? c + : spl_ir_builder_arith_or(a->ir, bt_ir, combined, c); } return combined; } @@ -323,16 +338,16 @@ static spl_ir_node_ref_t emit_match_check(spl_ast2ir_t *a, spl_ast_node_ref_t pa case SPL_AST_PATTERN_DOT: { spl_type_node_t *tn = spl_type_node(&a->sema->type->ir, scrut_ir); if (!tn || tn->kind != SPL_TYPE_ENUM) - return 0; + return spl_ir_value_none(); usize vidx = pat->resolved_def_id; if (vidx == 0) - return 0; + return spl_ir_value_none(); vidx -= 1; spl_type_id_t tag_ir = tn->agg_type.tag_type; - spl_ir_node_ref_t tag_ptr = + spl_ir_value_t tag_ptr = spl_ir_builder_mem_field(a->ir, scrut_ir, scrut_slot, SPL_ENUM_TAG_FIELD_IDX); - spl_ir_node_ref_t tag_val = spl_ir_builder_mem_load(a->ir, tag_ir, tag_ptr); - spl_ir_node_ref_t vidx_const = spl_ir_builder_type_const_int(a->ir, tag_ir, vidx); + spl_ir_value_t tag_val = spl_ir_builder_mem_load(a->ir, tag_ir, tag_ptr); + spl_ir_value_t vidx_const = spl_ir_value_const_int(tag_ir, vidx); return spl_ir_builder_cmp_eq(a->ir, tag_ir, tag_val, vidx_const); } @@ -342,16 +357,16 @@ static spl_ir_node_ref_t emit_match_check(spl_ast2ir_t *a, spl_ast_node_ref_t pa case SPL_AST_EXPR_STRING_LIT: case SPL_AST_EXPR_BOOL_LIT: case SPL_AST_EXPR_NULL_LIT: { - spl_ir_node_ref_t val = emit_expr(a, pat_ref); - if (!val) - return 0; - spl_ir_node_ref_t loaded = spl_ir_builder_mem_load(a->ir, scrut_ir, scrut_slot); + spl_ir_value_t val = emit_expr(a, pat_ref); + if (spl_ir_value_is_none(val)) + return spl_ir_value_none(); + spl_ir_value_t loaded = spl_ir_builder_mem_load(a->ir, scrut_ir, scrut_slot); return spl_ir_builder_cmp_eq(a->ir, scrut_ir, loaded, val); } default: /* tuple / aggregate pattern 暂不作为 match 条件 */ - return 0; + return spl_ir_value_none(); } } @@ -359,34 +374,34 @@ static spl_ir_node_ref_t emit_match_check(spl_ast2ir_t *a, spl_ast_node_ref_t pa 返回指向变量内存的指针(*tid),调用者可用于读取初值。 若 pattern 含 `&` 引用绑定:源必须是可寻址左值,直接在源 place 上绑定 (值绑定拷贝子值,引用绑定取子位置地址),返回该 place。 */ -static spl_ir_node_ref_t emit_var_decl(spl_ast2ir_t *a, spl_ast_node_t *n) { +static spl_ir_value_t emit_var_decl(spl_ast2ir_t *a, spl_ast_node_t *n) { spl_type_id_t var_ir = node_ir(a, n); if (!var_ir) - return 0; + return spl_ir_value_none(); spl_ast_node_ref_t pat_ref = n->var_const_decl.pattern; if (pattern_has_ref(a, pat_ref)) { if (!n->var_const_decl.init_expr) { spl_diag_error(a->diag, SPL_DIAG_KIND_NO_LVALUE, &n->dbg, "reference binding requires an initialized place"); - return 0; + return spl_ir_value_none(); } - spl_ir_node_ref_t place = emit_lvalue(a, n->var_const_decl.init_expr); - if (!place) { + spl_ir_value_t place = emit_lvalue(a, n->var_const_decl.init_expr); + if (spl_ir_value_is_none(place)) { spl_ast_node_t *in = spl_ast_node(a->ast, n->var_const_decl.init_expr); spl_diag_error(a->diag, SPL_DIAG_KIND_NO_LVALUE, in ? &in->dbg : &n->dbg, "cannot bind by reference: initializer is not an lvalue"); - return 0; + return spl_ir_value_none(); } emit_pattern_bind(a, pat_ref, place, var_ir, 1); return place; } - spl_ir_node_ref_t slot = spl_ir_builder_mem_alloca(a->ir, var_ir, const_usize(a, 1)); + spl_ir_value_t slot = spl_ir_builder_mem_alloca(a->ir, var_ir, const_usize(a, 1)); if (n->var_const_decl.init_expr) { - spl_ir_node_ref_t v = emit_expr(a, n->var_const_decl.init_expr); - if (v) + spl_ir_value_t v = emit_expr(a, n->var_const_decl.init_expr); + if (!spl_ir_value_is_none(v)) spl_ir_builder_mem_store(a->ir, var_ir, slot, v); } @@ -414,17 +429,17 @@ static void copy_attrs_to_ir_func(spl_ast2ir_t *a, spl_ast_node_t *n, spl_ir_fun vec_push(irf->attrs, vec_at(n->attrs, i)); } -static spl_ir_node_ref_t ast2ir_intrin_cast(spl_ast2ir_t *a, spl_ast_node_t *n, spl_intrin_t in, - spl_ast_node_ref_vec_t *args) { +static spl_ir_value_t ast2ir_intrin_cast(spl_ast2ir_t *a, spl_ast_node_t *n, spl_intrin_t in, + spl_ast_node_ref_vec_t *args) { usize nargs = vec_size(*args); if (nargs < 1) { spl_diag_error(a->diag, SPL_DIAG_KIND_INTRIN_ARG_COUNT, &n->dbg, "cast builtin requires at least 1 argument"); - return 0; + return spl_ir_value_none(); } - spl_ir_node_ref_t v = emit_expr(a, vec_at(*args, 0)); - if (!v) - return 0; + spl_ir_value_t v = emit_expr(a, vec_at(*args, 0)); + if (spl_ir_value_is_none(v)) + return spl_ir_value_none(); spl_ast_node_t *src = spl_ast_node(a->ast, vec_at(*args, 0)); spl_type_id_t ft = node_ir(a, src); @@ -436,12 +451,12 @@ static spl_ir_node_ref_t ast2ir_intrin_cast(spl_ast2ir_t *a, spl_ast_node_t *n, tt = node_ir(a, n); } if (!ft || !tt) - return 0; + return spl_ir_value_none(); return ast2ir_emit_cast_builtin(a, n, in, ft, tt, v); } -static spl_ir_node_ref_t ast2ir_intrin_call(spl_ast2ir_t *a, spl_ast_node_t *n, spl_intrin_t in, - spl_ast_node_ref_vec_t *args) { +static spl_ir_value_t ast2ir_intrin_call(spl_ast2ir_t *a, spl_ast_node_t *n, spl_intrin_t in, + spl_ast_node_ref_vec_t *args) { switch (in) { case SPL_INTRIN_BITSIZEOF: { spl_ast_node_t *arg = spl_ast_node(a->ast, vec_at(*args, 0)); @@ -458,13 +473,13 @@ static spl_ir_node_ref_t ast2ir_intrin_call(spl_ast2ir_t *a, spl_ast_node_t *n, case SPL_INTRIN_ENUM2INT: { /* enum 值 -> 其 tag(判别值);field 0 即 tag。 */ spl_ast_node_t *src = spl_ast_node(a->ast, vec_at(*args, 0)); - spl_ir_node_ref_t v = emit_expr(a, vec_at(*args, 0)); - if (!v) - return 0; + spl_ir_value_t v = emit_expr(a, vec_at(*args, 0)); + if (spl_ir_value_is_none(v)) + return spl_ir_value_none(); spl_type_id_t agg_t = node_ir(a, src); spl_type_id_t tag_t = node_ir(a, n); if (!agg_t || !tag_t) - return 0; + return spl_ir_value_none(); return spl_ir_builder_agg_extract(a->ir, agg_t, tag_t, 0, v); } case SPL_INTRIN_FIELD_COUNT: { @@ -475,27 +490,27 @@ static spl_ir_node_ref_t ast2ir_intrin_call(spl_ast2ir_t *a, spl_ast_node_t *n, spl_ast_node_t *arg = spl_ast_node(a->ast, vec_at(*args, 0)); spl_type_id_t agg_t = node_ir(a, arg); if (!agg_t) - return 0; + return spl_ir_value_none(); spl_ast_node_t *fnode = spl_ast_node(a->ast, vec_at(*args, 1)); usize idx = 0; if (fnode && fnode->kind == SPL_AST_EXPR_INT_LIT) { idx = (usize)fnode->int_lit.bits; } else if (fnode && fnode->kind == SPL_AST_EXPR_IDENT) { if (!spl_type_agg_member_idx(&a->sema->type->ir, agg_t, fnode->ident.name, &idx)) - return 0; + return spl_ir_value_none(); } else { - return 0; + return spl_ir_value_none(); } - spl_ir_node_ref_t idx_node = const_usize(a, idx); + spl_ir_value_t idx_node = const_usize(a, idx); return spl_ir_builder_type_offsetof(a->ir, agg_t, idx_node); } case SPL_INTRIN_ASSERT: { - spl_ir_node_ref_t cond = emit_expr(a, vec_at(*args, 0)); - if (!cond) - return 0; + spl_ir_value_t cond = emit_expr(a, vec_at(*args, 0)); + if (spl_ir_value_is_none(cond)) + return spl_ir_value_none(); spl_lang_type_id_t b = spl_lang_make_bool(a->sema->type); - spl_ir_node_ref_t notc = spl_ir_builder_arith_not(a->ir, to_ir_type(a, b), cond); + spl_ir_value_t notc = spl_ir_builder_arith_not(a->ir, to_ir_type(a, b), cond); spl_ir_node_ref_t br = spl_ir_builder_control_br(a->ir, notc); spl_ir_builder_block_end(a->ir); @@ -530,7 +545,7 @@ static spl_ir_node_ref_t ast2ir_intrin_call(spl_ast2ir_t *a, spl_ast_node_t *n, usize cont_blk = spl_ir_builder_block_begin(a->ir); spl_ir_builder_control_br_fixup(a->ir, br, panic_blk, cont_blk); - return 0; + return spl_ir_value_none(); } case SPL_INTRIN_INT_CAST: case SPL_INTRIN_TRUNCATE: @@ -552,24 +567,24 @@ static spl_ir_node_ref_t ast2ir_intrin_call(spl_ast2ir_t *a, spl_ast_node_t *n, case SPL_INTRIN_FN_NAME: return ast2ir_intrin_fn_name(a, n); case SPL_INTRIN_BREAKPOINT: - return spl_ir_builder_dbg_breakpoint(a->ir); + return spl_ir_value_inst(spl_ir_builder_dbg_breakpoint(a->ir)); default: UNREACHABLE(); } - return 0; + return spl_ir_value_none(); } /* @line() / @col():把调用点位置降为 usize 常量。which: 0=line, 1=col。 */ -static spl_ir_node_ref_t ast2ir_intrin_pos(spl_ast2ir_t *a, spl_ast_node_t *n, int which) { +static spl_ir_value_t ast2ir_intrin_pos(spl_ast2ir_t *a, spl_ast_node_t *n, int which) { spl_lang_type_id_t usize_lang = spl_lang_make_int(a->sema->type, sizeof(void *) * 8, 0); spl_type_id_t usize_ir = to_ir_type(a, usize_lang); usize v = which == 0 ? (usize)n->dbg.line : (usize)n->dbg.col; - return spl_ir_builder_type_const_int(a->ir, usize_ir, v); + return spl_ir_value_const_int(usize_ir, v); } -/* 构造 []u8 值:global_alloc 拷贝字节 + len 常量,聚合为 slice。 */ -static spl_ir_node_ref_t make_slice_bytes(spl_ast2ir_t *a, spl_ast_node_t *n, const void *data, - usize len) { +/* 构造 []u8 值:只读全局槽保存字节,len 用常量,聚合为 slice。 */ +static spl_ir_value_t make_slice_bytes(spl_ast2ir_t *a, spl_ast_node_t *n, const void *data, + usize len) { spl_lang_type_id_t u8_lang = spl_lang_make_int(a->sema->type, 8, 0); spl_type_id_t u8_ir = to_ir_type(a, u8_lang); spl_type_id_t ptr_u8_ir = spl_type_builder_ptr(&a->sema->type->ir, u8_ir); @@ -578,25 +593,26 @@ static spl_ir_node_ref_t make_slice_bytes(spl_ast2ir_t *a, spl_ast_node_t *n, co spl_lang_type_id_t arr_lang = spl_lang_make_array(a->sema->type, u8_lang, len); spl_type_id_t arr_ir = to_ir_type(a, arr_lang); - spl_ir_node_ref_t raw = spl_ir_builder_type_const_bytes(a->ir, arr_ir, (void *)data, len); - spl_ir_node_ref_t ptr = spl_ir_builder_mem_global_alloc(a->ir, ptr_u8_ir, raw); - spl_ir_node_ref_t len_c = spl_ir_builder_type_const_int(a->ir, usize_ir, len); + spl_ir_value_t raw = spl_ir_builder_const_bytes(a->ir, arr_ir, data, len); + usize gid = spl_ir_builder_global_declare(a->ir, NULL, arr_ir, raw, true); + spl_ir_value_t ptr = spl_ir_builder_global_addr(ptr_u8_ir, gid); + spl_ir_value_t len_c = spl_ir_value_const_int(usize_ir, len); spl_type_id_t slice_ir = node_ir(a, n); spl_ir_node_ref_t node = spl_ir_builder_agg_construct(a->ir, slice_ir); spl_ir_builder_agg_construct_field(a->ir, node, ptr); spl_ir_builder_agg_construct_field(a->ir, node, len_c); - return node; + return spl_ir_value_inst(node); } /* @file_name():把调用点所在文件路径降为 []u8。 */ -static spl_ir_node_ref_t ast2ir_intrin_file_name(spl_ast2ir_t *a, spl_ast_node_t *n) { +static spl_ir_value_t ast2ir_intrin_file_name(spl_ast2ir_t *a, spl_ast_node_t *n) { const char *fname = n->dbg.fname ? n->dbg.fname : "?"; return make_slice_bytes(a, n, fname, strlen(fname)); } /* @fn_name():把当前函数名降为 []u8。 */ -static spl_ir_node_ref_t ast2ir_intrin_fn_name(spl_ast2ir_t *a, spl_ast_node_t *n) { +static spl_ir_value_t ast2ir_intrin_fn_name(spl_ast2ir_t *a, spl_ast_node_t *n) { const char *name = "?"; spl_ir_func_t *f = spl_ir_func(&a->ir->ir, a->ir->current_fn); if (f && f->name) @@ -604,22 +620,22 @@ static spl_ir_node_ref_t ast2ir_intrin_fn_name(spl_ast2ir_t *a, spl_ast_node_t * return make_slice_bytes(a, n, name, strlen(name)); } -static spl_ir_node_ref_t ast2ir_intrin_field(spl_ast2ir_t *a, spl_ast_node_t *n, spl_intrin_t in) { +static spl_ir_value_t ast2ir_intrin_field(spl_ast2ir_t *a, spl_ast_node_t *n, spl_intrin_t in) { switch (in) { case SPL_INTRIN_LEN: return ast2ir_intrin_len(a, n); default: UNREACHABLE(); } - return 0; + return spl_ir_value_none(); } -static spl_ir_node_ref_t ast2ir_intrin_len(spl_ast2ir_t *a, spl_ast_node_t *n) { +static spl_ir_value_t ast2ir_intrin_len(spl_ast2ir_t *a, spl_ast_node_t *n) { spl_ast_node_t *obj = spl_ast_node(a->ast, n->field.obj); spl_lang_type_id_t recv_lang = node_lang(obj); spl_lang_type_node_t *recv = spl_lang_type_node(a->sema->type, recv_lang); if (!recv) - return 0; + return spl_ir_value_none(); spl_lang_type_id_t usize_lang = spl_lang_make_int(a->sema->type, sizeof(void *) * 8, 0); spl_type_id_t usize_ir = to_ir_type(a, usize_lang); @@ -628,53 +644,58 @@ static spl_ir_node_ref_t ast2ir_intrin_len(spl_ast2ir_t *a, spl_ast_node_t *n) { case SPL_LANG_TYPE_ARRAY: { spl_type_node_t *arr = spl_type_node(&a->sema->type->ir, recv->ir_type_id); if (!arr) - return 0; - return spl_ir_builder_type_const_int(a->ir, usize_ir, arr->array_type.len); + return spl_ir_value_none(); + return spl_ir_value_const_int(usize_ir, arr->array_type.len); } case SPL_LANG_TYPE_SLICE: { - spl_ir_node_ref_t slice_val = emit_expr(a, n->field.obj); - if (!slice_val) - return 0; + spl_ir_value_t slice_val = emit_expr(a, n->field.obj); + if (spl_ir_value_is_none(slice_val)) + return spl_ir_value_none(); return spl_ir_builder_agg_extract(a->ir, recv->ir_type_id, usize_ir, SPL_SLICE_LEN_FIELD_IDX, slice_val); } default: - return 0; + return spl_ir_value_none(); } } -static spl_ir_node_ref_t emit_lvalue(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { +static spl_ir_value_t emit_lvalue(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { spl_ast_node_t *n = spl_ast_node(a->ast, ref); if (!n) - return 0; + return spl_ir_value_none(); switch (n->kind) { case SPL_AST_EXPR_IDENT: { spl_def_node_t *def = spl_def_node(a->sema->type, n->resolved_def_id); if (def && def->kind == SPL_DEF_FN) - return 0; - spl_ir_node_ref_t slot = 0; + return spl_ir_value_none(); + usize gid = 0; + if (map_get(a->global_map, n->resolved_def_id, &gid)) { + spl_type_id_t ptr_tid = spl_type_builder_ptr(&a->sema->type->ir, node_ir(a, n)); + return spl_ir_builder_global_addr(ptr_tid, gid); + } + spl_ir_value_t slot = spl_ir_value_none(); if (!map_get(a->def2ir_map, n->resolved_def_id, &slot)) - return 0; + return spl_ir_value_none(); return slot; } case SPL_AST_EXPR_UNARY: { if (n->unary.op != SPL_UNARY_OP_DEREF) - return 0; + return spl_ir_value_none(); return emit_expr(a, n->unary.operand); } case SPL_AST_EXPR_FIELD: { spl_ast_node_t *obj = spl_ast_node(a->ast, n->field.obj); if (!obj) - return 0; + return spl_ir_value_none(); spl_lang_type_id_t obj_lang = node_lang(obj); spl_lang_type_node_t *obj_ln = spl_lang_type_node(a->sema->type, obj_lang); if (!obj_ln) - return 0; + return spl_ir_value_none(); spl_lang_type_kind_t obj_kind = obj_ln->kind; spl_lang_type_id_t obj_elem = (obj_kind == SPL_LANG_TYPE_PTR) ? obj_ln->extra.elem : 0; - spl_ir_node_ref_t base = 0; + spl_ir_value_t base = spl_ir_value_none(); spl_lang_type_id_t agg_lang = 0; if (obj_kind == SPL_LANG_TYPE_PTR) { base = emit_expr(a, n->field.obj); @@ -683,16 +704,16 @@ static spl_ir_node_ref_t emit_lvalue(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { base = emit_lvalue(a, n->field.obj); agg_lang = obj_lang; } - if (!base) - return 0; + if (spl_ir_value_is_none(base)) + return spl_ir_value_none(); spl_type_id_t agg_ir = to_ir_type(a, agg_lang); if (!agg_ir) - return 0; + return spl_ir_value_none(); usize idx = 0; if (!spl_type_agg_member_idx(&a->sema->type->ir, agg_ir, n->field.field, &idx)) - return 0; + return spl_ir_value_none(); spl_lang_type_node_t *agg_ln = spl_lang_type_node(a->sema->type, agg_lang); if (agg_ln && agg_ln->kind == SPL_LANG_TYPE_ENUM) @@ -703,11 +724,11 @@ static spl_ir_node_ref_t emit_lvalue(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { case SPL_AST_EXPR_INDEX: { spl_ast_node_t *obj = spl_ast_node(a->ast, n->index.obj); if (!obj) - return 0; + return spl_ir_value_none(); spl_lang_type_id_t obj_lang = node_lang(obj); spl_lang_type_node_t *obj_ln = spl_lang_type_node(a->sema->type, obj_lang); if (!obj_ln) - return 0; + return spl_ir_value_none(); spl_lang_type_kind_t obj_kind = obj_ln->kind; spl_lang_type_id_t obj_elem = 0; @@ -718,18 +739,18 @@ static spl_ir_node_ref_t emit_lvalue(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { if (obj_kind == SPL_LANG_TYPE_TUPLE) { spl_ast_node_t *idx_node = spl_ast_node(a->ast, n->index.index); if (!idx_node || idx_node->kind != SPL_AST_EXPR_INT_LIT) - return 0; + return spl_ir_value_none(); usize i = (usize)idx_node->int_lit.bits; - spl_ir_node_ref_t base = emit_lvalue(a, n->index.obj); - if (!base) - return 0; + spl_ir_value_t base = emit_lvalue(a, n->index.obj); + if (spl_ir_value_is_none(base)) + return spl_ir_value_none(); spl_type_id_t tuple_ir = to_ir_type(a, obj_lang); if (!tuple_ir) - return 0; + return spl_ir_value_none(); return spl_ir_builder_mem_field(a->ir, tuple_ir, base, i); } - spl_ir_node_ref_t base = 0; + spl_ir_value_t base = spl_ir_value_none(); spl_lang_type_id_t elem_lang = 0; if (obj_kind == SPL_LANG_TYPE_PTR) { base = emit_expr(a, n->index.obj); @@ -738,38 +759,37 @@ static spl_ir_node_ref_t emit_lvalue(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { base = emit_lvalue(a, n->index.obj); elem_lang = obj_elem; } else if (obj_kind == SPL_LANG_TYPE_SLICE) { - spl_ir_node_ref_t slice_val = emit_expr(a, n->index.obj); - if (!slice_val) - return 0; + spl_ir_value_t slice_val = emit_expr(a, n->index.obj); + if (spl_ir_value_is_none(slice_val)) + return spl_ir_value_none(); spl_type_id_t ptr_ir = spl_type_builder_ptr(&a->sema->type->ir, to_ir_type(a, obj_elem)); - spl_ir_node_ref_t ptr = spl_ir_builder_agg_extract(a->ir, ptr_ir, ptr_ir, - SPL_SLICE_PTR_FIELD_IDX, slice_val); - base = ptr; + base = spl_ir_builder_agg_extract(a->ir, ptr_ir, ptr_ir, SPL_SLICE_PTR_FIELD_IDX, + slice_val); elem_lang = obj_elem; } else { - return 0; + return spl_ir_value_none(); } - if (!base) - return 0; + if (spl_ir_value_is_none(base)) + return spl_ir_value_none(); - spl_ir_node_ref_t idx = emit_expr(a, n->index.index); - if (!idx) - return 0; + spl_ir_value_t idx = emit_expr(a, n->index.index); + if (spl_ir_value_is_none(idx)) + return spl_ir_value_none(); spl_type_id_t elem_ir = to_ir_type(a, elem_lang); if (!elem_ir) - return 0; + return spl_ir_value_none(); return spl_ir_builder_mem_offset(a->ir, elem_ir, base, idx); } default: - return 0; + return spl_ir_value_none(); } } -static spl_ir_node_ref_t ast2ir_emit_cast_builtin(spl_ast2ir_t *a, spl_ast_node_t *n, - spl_intrin_t in, spl_type_id_t ft, - spl_type_id_t tt, spl_ir_node_ref_t v) { +static spl_ir_value_t ast2ir_emit_cast_builtin(spl_ast2ir_t *a, spl_ast_node_t *n, + spl_intrin_t in, spl_type_id_t ft, spl_type_id_t tt, + spl_ir_value_t v) { switch (in) { case SPL_INTRIN_INT_CAST: { spl_type_node_t *fs = spl_type_node(&a->sema->type->ir, ft); @@ -802,47 +822,47 @@ static spl_ir_node_ref_t ast2ir_emit_cast_builtin(spl_ast2ir_t *a, spl_ast_node_ return spl_ir_builder_cast_bitcast(a->ir, ft, tt, v); default: spl_diag_error(a->diag, SPL_DIAG_KIND_INTERNAL, &n->dbg, "ast2ir: unknown cast builtin"); - return 0; + return spl_ir_value_none(); } } -static spl_ir_node_ref_t emit_cast(spl_ast2ir_t *a, spl_ast_node_t *n) { - spl_ir_node_ref_t v = emit_expr_raw(a, n->cast.expr); - if (!v) - return 0; +static spl_ir_value_t emit_cast(spl_ast2ir_t *a, spl_ast_node_t *n) { + spl_ir_value_t v = emit_expr_raw(a, n->cast.expr); + if (spl_ir_value_is_none(v)) + return spl_ir_value_none(); spl_ast_node_t *src = spl_ast_node(a->ast, n->cast.expr); spl_ast_node_t *dst = spl_ast_node(a->ast, n->cast.type); spl_type_id_t ft = node_ir(a, src); spl_type_id_t tt = node_ir(a, dst); if (!ft || !tt) - return 0; + return spl_ir_value_none(); spl_intrin_t in = spl_intrin_lookup_cast(&a->sema->type->ir, ft, tt); if (in == SPL_INTRIN_NONE) { spl_diag_error(a->diag, SPL_DIAG_KIND_CAST_INVALID, &n->dbg, "as: no conversion between the given types"); - return 0; + return spl_ir_value_none(); } return ast2ir_emit_cast_builtin(a, n, in, ft, tt, v); } /* 字段访问的基对象是 rvalue(如函数调用结果)时的回退路径: * 先求值整个聚合值,再用 agg.extract 取出字段值(无需左值/临时槽)。 */ -static spl_ir_node_ref_t ast2ir_field_from_value(spl_ast2ir_t *a, spl_ast_node_t *n) { +static spl_ir_value_t ast2ir_field_from_value(spl_ast2ir_t *a, spl_ast_node_t *n) { spl_ast_node_t *obj = spl_ast_node(a->ast, n->field.obj); if (!obj) - return 0; + return spl_ir_value_none(); spl_lang_type_id_t obj_lang = node_lang(obj); spl_lang_type_node_t *obj_ln = spl_lang_type_node(a->sema->type, obj_lang); if (!obj_ln) - return 0; + return spl_ir_value_none(); spl_type_id_t agg_ir = to_ir_type(a, obj_lang); if (!agg_ir) - return 0; + return spl_ir_value_none(); usize idx = 0; if (!spl_type_agg_member_idx(&a->sema->type->ir, agg_ir, n->field.field, &idx)) - return 0; + return spl_ir_value_none(); usize field_idx = idx; if (obj_ln->kind == SPL_LANG_TYPE_ENUM) @@ -853,52 +873,53 @@ static spl_ir_node_ref_t ast2ir_field_from_value(spl_ast2ir_t *a, spl_ast_node_t if (agg_tn && field_idx < vec_size(agg_tn->agg_type.variants)) field_ir = vec_at(agg_tn->agg_type.variants, field_idx).type_id; - spl_ir_node_ref_t val = emit_expr(a, n->field.obj); - if (!val) - return 0; + spl_ir_value_t val = emit_expr(a, n->field.obj); + if (spl_ir_value_is_none(val)) + return spl_ir_value_none(); return spl_ir_builder_agg_extract(a->ir, agg_ir, field_ir, field_idx, val); } /* 限定枚举变体 Type.Variant:产出枚举常量(tag=变体下标;无载荷变体不带 payload)。 */ -static spl_ir_node_ref_t ast2ir_enum_variant(spl_ast2ir_t *a, spl_ast_node_t *n) { +static spl_ir_value_t ast2ir_enum_variant(spl_ast2ir_t *a, spl_ast_node_t *n) { spl_type_id_t t = node_ir(a, n); if (!t) - return 0; + return spl_ir_value_none(); spl_type_node_t *tn = spl_type_node(&a->sema->type->ir, t); if (!tn) - return 0; + return spl_ir_value_none(); usize vidx = 0; if (!spl_type_agg_member_idx(&a->sema->type->ir, t, n->field.field, &vidx)) - return 0; + return spl_ir_value_none(); spl_type_id_t tag_ir = tn->agg_type.tag_type; - spl_ir_node_ref_t eptr = spl_ir_builder_mem_alloca(a->ir, t, const_usize(a, 1)); - spl_ir_node_ref_t tptr = spl_ir_builder_mem_field(a->ir, t, eptr, SPL_ENUM_TAG_FIELD_IDX); - spl_ir_node_ref_t tag_val = spl_ir_builder_type_const_int(a->ir, tag_ir, vidx); + spl_ir_value_t eptr = spl_ir_builder_mem_alloca(a->ir, t, const_usize(a, 1)); + spl_ir_value_t tptr = spl_ir_builder_mem_field(a->ir, t, eptr, SPL_ENUM_TAG_FIELD_IDX); + spl_ir_value_t tag_val = spl_ir_value_const_int(tag_ir, vidx); spl_ir_builder_mem_store(a->ir, tag_ir, tptr, tag_val); if (vidx < vec_size(tn->agg_type.variants)) { spl_type_id_t payload_ir = vec_at(tn->agg_type.variants, vidx).type_id; if (payload_ir) { - spl_ir_node_ref_t pptr = spl_ir_builder_mem_field(a->ir, t, eptr, vidx + 1); - spl_ir_node_ref_t zero = emit_zero_value(a, payload_ir); + spl_ir_value_t pptr = spl_ir_builder_mem_field(a->ir, t, eptr, vidx + 1); + spl_ir_value_t zero = emit_zero_value(a, payload_ir); spl_ir_builder_mem_store(a->ir, payload_ir, pptr, zero); } } return eptr; } -static spl_ir_node_ref_t emit_expr(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { - spl_ir_node_ref_t val = emit_expr_raw(a, ref); - if (!val) - return 0; +static spl_ir_value_t emit_expr(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { + spl_ir_value_t val = emit_expr_raw(a, ref); + if (spl_ir_value_is_none(val)) + return spl_ir_value_none(); spl_ast_node_t *n = spl_ast_node(a->ast, ref); if (!n || n->expected_lang_tid == 0) return val; - copy_attrs_to_ir_node(a, n, val); + if (val.kind == SPL_IR_VALUE_INST) + copy_attrs_to_ir_node(a, n, val.inst); spl_lang_type_id_t from = n->resolved_lang_tid; spl_lang_type_id_t to = n->expected_lang_tid; @@ -913,14 +934,14 @@ static spl_ir_node_ref_t emit_expr(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { return emit_implicit_cast(a, n, from_ir, to_ir, val); } -static spl_ir_node_ref_t emit_expr_binary(spl_ast2ir_t *a, spl_ast_node_t *n) { +static spl_ir_value_t emit_expr_binary(spl_ast2ir_t *a, spl_ast_node_t *n) { spl_binary_op_t op = n->binary.op; if (op == SPL_OP_ASSIGN) { - spl_ir_node_ref_t ptr = emit_lvalue(a, n->binary.left); - spl_ir_node_ref_t v = emit_expr(a, n->binary.right); - if (!ptr || !v) - return 0; + spl_ir_value_t ptr = emit_lvalue(a, n->binary.left); + spl_ir_value_t v = emit_expr(a, n->binary.right); + if (spl_ir_value_is_none(ptr) || spl_ir_value_is_none(v)) + return spl_ir_value_none(); spl_ast_node_t *ln = spl_ast_node(a->ast, n->binary.left); spl_type_id_t t = node_ir(a, ln); spl_ir_builder_mem_store(a->ir, t, ptr, v); @@ -928,16 +949,16 @@ static spl_ir_node_ref_t emit_expr_binary(spl_ast2ir_t *a, spl_ast_node_t *n) { } if (op >= SPL_OP_ASSIGN_ADD && op <= SPL_OP_ASSIGN_RSHIFT) { - spl_ir_node_ref_t ptr = emit_lvalue(a, n->binary.left); - if (!ptr) - return 0; + spl_ir_value_t ptr = emit_lvalue(a, n->binary.left); + if (spl_ir_value_is_none(ptr)) + return spl_ir_value_none(); spl_ast_node_t *ln = spl_ast_node(a->ast, n->binary.left); spl_type_id_t t = node_ir(a, ln); - spl_ir_node_ref_t lv = spl_ir_builder_mem_load(a->ir, t, ptr); - spl_ir_node_ref_t rv = emit_expr(a, n->binary.right); - if (!lv || !rv) - return 0; - spl_ir_node_ref_t res = 0; + spl_ir_value_t lv = spl_ir_builder_mem_load(a->ir, t, ptr); + spl_ir_value_t rv = emit_expr(a, n->binary.right); + if (spl_ir_value_is_none(lv) || spl_ir_value_is_none(rv)) + return spl_ir_value_none(); + spl_ir_value_t res = spl_ir_value_none(); switch (op) { case SPL_OP_ASSIGN_ADD: res = spl_ir_builder_arith_add(a->ir, t, lv, rv); @@ -970,10 +991,10 @@ static spl_ir_node_ref_t emit_expr_binary(spl_ast2ir_t *a, spl_ast_node_t *n) { res = spl_ir_builder_arith_shr(a->ir, t, lv, rv); break; default: - return 0; + return spl_ir_value_none(); } - if (!res) - return 0; + if (spl_ir_value_is_none(res)) + return spl_ir_value_none(); spl_ir_builder_mem_store(a->ir, t, ptr, res); return res; } @@ -981,19 +1002,19 @@ static spl_ir_node_ref_t emit_expr_binary(spl_ast2ir_t *a, spl_ast_node_t *n) { if (op == SPL_OP_BOOL_OR || op == SPL_OP_BOOL_AND) { spl_lang_type_id_t b_lang = spl_lang_make_bool(a->sema->type); spl_type_id_t b_ir = to_ir_type(a, b_lang); - spl_ir_node_ref_t slot = spl_ir_builder_mem_alloca(a->ir, b_ir, const_usize(a, 1)); + spl_ir_value_t slot = spl_ir_builder_mem_alloca(a->ir, b_ir, const_usize(a, 1)); - spl_ir_node_ref_t l = emit_expr(a, n->binary.left); - if (!l) - return 0; + spl_ir_value_t l = emit_expr(a, n->binary.left); + if (spl_ir_value_is_none(l)) + return spl_ir_value_none(); spl_ir_builder_mem_store(a->ir, b_ir, slot, l); spl_ir_node_ref_t br = spl_ir_builder_control_br(a->ir, l); spl_ir_builder_block_end(a->ir); usize rhs_blk = spl_ir_builder_block_begin(a->ir); - spl_ir_node_ref_t r = emit_expr(a, n->binary.right); - if (r) + spl_ir_value_t r = emit_expr(a, n->binary.right); + if (!spl_ir_value_is_none(r)) spl_ir_builder_mem_store(a->ir, b_ir, slot, r); /* 右侧可能以 ret / trap 终止,此时不能再补 jmp */ spl_ir_node_ref_t rhs_end = 0; @@ -1014,10 +1035,10 @@ static spl_ir_node_ref_t emit_expr_binary(spl_ast2ir_t *a, spl_ast_node_t *n) { return spl_ir_builder_mem_load(a->ir, b_ir, slot); } - spl_ir_node_ref_t l = emit_expr(a, n->binary.left); - spl_ir_node_ref_t r = emit_expr(a, n->binary.right); - if (!l || !r) - return 0; + spl_ir_value_t l = emit_expr(a, n->binary.left); + spl_ir_value_t r = emit_expr(a, n->binary.right); + if (spl_ir_value_is_none(l) || spl_ir_value_is_none(r)) + return spl_ir_value_none(); spl_ast_node_t *ln = spl_ast_node(a->ast, n->binary.left); spl_lang_type_id_t lt = node_lang(ln); @@ -1028,7 +1049,7 @@ static spl_ir_node_ref_t emit_expr_binary(spl_ast2ir_t *a, spl_ast_node_t *n) { spl_type_id_t elem_ir = to_ir_type(a, elem_lang); spl_type_id_t rt_ir = node_ir(a, spl_ast_node(a->ast, n->binary.right)); - spl_ir_node_ref_t off = r; + spl_ir_value_t off = r; if (op == SPL_OP_SUB) off = spl_ir_builder_arith_neg(a->ir, rt_ir, r); @@ -1079,15 +1100,15 @@ static spl_ir_node_ref_t emit_expr_binary(spl_ast2ir_t *a, spl_ast_node_t *n) { case SPL_OP_CMP_GE: return spl_ir_builder_cmp_ge(a->ir, op_tid, l, r); default: - return 0; + return spl_ir_value_none(); } } default: - return 0; + return spl_ir_value_none(); } } -static spl_ir_node_ref_t emit_expr_call(spl_ast2ir_t *a, spl_ast_node_t *n) { +static spl_ir_value_t emit_expr_call(spl_ast2ir_t *a, spl_ast_node_t *n) { spl_ast_node_t *callee = spl_ast_node(a->ast, n->call.callee); if (callee && callee->kind == SPL_AST_EXPR_IDENT) { spl_intrin_t in = spl_intrin_lookup_call(callee->ident.name); @@ -1095,32 +1116,32 @@ static spl_ir_node_ref_t emit_expr_call(spl_ast2ir_t *a, spl_ast_node_t *n) { return ast2ir_intrin_call(a, n, in, &n->call.args); } - spl_ir_node_ref_t fn = emit_expr(a, n->call.callee); - if (!fn) - return 0; + spl_ir_value_t fn = emit_expr(a, n->call.callee); + if (spl_ir_value_is_none(fn)) + return spl_ir_value_none(); usize nargs = vec_size(n->call.args); - spl_ir_node_ref_vec_t arg_refs; - vec_init(arg_refs); + spl_ir_value_vec_t arg_vals; + vec_init(arg_vals); for (usize i = 0; i < nargs; ++i) - vec_push(arg_refs, emit_expr(a, vec_at(n->call.args, i))); + vec_push(arg_vals, emit_expr(a, vec_at(n->call.args, i))); spl_type_id_t ret_t = node_ir(a, n); spl_ir_node_ref_t call = spl_ir_builder_control_call(a->ir, ret_t, fn); if (!call) { - vec_free(arg_refs); - return 0; + vec_free(arg_vals); + return spl_ir_value_none(); } - for (usize i = 0; i < vec_size(arg_refs); ++i) - spl_ir_builder_control_call_param(a->ir, call, vec_at(arg_refs, i)); - vec_free(arg_refs); + for (usize i = 0; i < vec_size(arg_vals); ++i) + spl_ir_builder_control_call_param(a->ir, call, vec_at(arg_vals, i)); + vec_free(arg_vals); - return call; + return spl_ir_value_inst(call); } -static spl_ir_node_ref_t emit_expr_field(spl_ast2ir_t *a, spl_ast_node_t *n, - spl_ast_node_ref_t ref) { +static spl_ir_value_t emit_expr_field(spl_ast2ir_t *a, spl_ast_node_t *n, + spl_ast_node_ref_t ref) { spl_ast_node_t *obj = spl_ast_node(a->ast, n->field.obj); spl_lang_type_id_t obj_lang = node_lang(obj); spl_lang_type_node_t *obj_ln = spl_lang_type_node(a->sema->type, obj_lang); @@ -1133,10 +1154,10 @@ static spl_ir_node_ref_t emit_expr_field(spl_ast2ir_t *a, spl_ast_node_t *n, if (n->resolved_def_id) { spl_def_node_t *fd = spl_def_node(a->sema->type, n->resolved_def_id); if (fd && fd->kind == SPL_DEF_FN) { - spl_ir_node_ref_t fn_ref = 0; - if (!map_get(a->def2ir_map, n->resolved_def_id, &fn_ref)) - return 0; - return spl_ir_builder_type_const_fn(a->ir, node_ir(a, n), (spl_ir_func_ref_t)fn_ref); + spl_ir_value_t fn_val = spl_ir_value_none(); + if (!map_get(a->def2ir_map, n->resolved_def_id, &fn_val)) + return spl_ir_value_none(); + return fn_val; } } @@ -1150,23 +1171,22 @@ static spl_ir_node_ref_t emit_expr_field(spl_ast2ir_t *a, spl_ast_node_t *n, return ast2ir_enum_variant(a, n); } - spl_ir_node_ref_t ptr = emit_lvalue(a, ref); - if (ptr) + spl_ir_value_t ptr = emit_lvalue(a, ref); + if (!spl_ir_value_is_none(ptr)) return spl_ir_builder_mem_load(a->ir, node_ir(a, n), ptr); return ast2ir_field_from_value(a, n); } -static spl_ir_node_ref_t emit_expr_tuple(spl_ast2ir_t *a, spl_ast_node_t *n) { +static spl_ir_value_t emit_expr_tuple(spl_ast2ir_t *a, spl_ast_node_t *n) { spl_type_id_t t = node_ir(a, n); usize nelems = vec_size(n->tuple.elems); - spl_ir_node_ref_vec_t field_refs; - vec_init(field_refs); + spl_ir_value_vec_t field_vals; + vec_init(field_vals); for (usize i = 0; i < nelems; ++i) { - spl_ir_node_ref_t f = emit_expr(a, vec_at(n->tuple.elems, i)); - if (!f) - Panic("tuple element emit failed"); - vec_push(field_refs, f); + spl_ir_value_t f = emit_expr(a, vec_at(n->tuple.elems, i)); + /* undefined 元素保持未初始化。 */ + vec_push(field_vals, f); } spl_ir_node_ref_t node = spl_ir_builder_agg_construct(a->ir, t); @@ -1174,13 +1194,13 @@ static spl_ir_node_ref_t emit_expr_tuple(spl_ast2ir_t *a, spl_ast_node_t *n) { Panic("agg_construct failed for tuple"); for (usize i = 0; i < nelems; ++i) - spl_ir_builder_agg_construct_field(a->ir, node, vec_at(field_refs, i)); + spl_ir_builder_agg_construct_field(a->ir, node, vec_at(field_vals, i)); - vec_free(field_refs); - return node; + vec_free(field_vals); + return spl_ir_value_inst(node); } -static spl_ir_node_ref_t emit_expr_array_lit(spl_ast2ir_t *a, spl_ast_node_t *n) { +static spl_ir_value_t emit_expr_array_lit(spl_ast2ir_t *a, spl_ast_node_t *n) { spl_type_id_t t = node_ir(a, n); if (!t) Panic("array literal has no type after sema"); @@ -1197,54 +1217,64 @@ static spl_ir_node_ref_t emit_expr_array_lit(spl_ast2ir_t *a, spl_ast_node_t *n) spl_type_id_t usize_ir = to_ir_type(a, usize_lang); spl_type_id_t ptr_ir = spl_type_builder_ptr(&a->sema->type->ir, elem_ir); - spl_ir_node_ref_t base = 0; + spl_ir_value_t base = spl_ir_value_none(); if (nelems == 0) { - base = spl_ir_builder_type_const_ptr(a->ir, ptr_ir, NULL); + base = spl_ir_builder_const_null(ptr_ir); } else { spl_type_id_t arr_ir = spl_type_builder_array(&a->sema->type->ir, elem_ir, nelems); - spl_ir_node_ref_t slot = spl_ir_builder_mem_alloca(a->ir, arr_ir, const_usize(a, 1)); + spl_ir_value_t slot = spl_ir_builder_mem_alloca(a->ir, arr_ir, const_usize(a, 1)); for (usize i = 0; i < nelems; ++i) { - spl_ir_node_ref_t fptr = spl_ir_builder_mem_field(a->ir, arr_ir, slot, i); - spl_ir_node_ref_t v = emit_expr(a, vec_at(n->array_literal.elems, i)); - if (!v) - Panic("array element emit failed"); - spl_ir_builder_mem_store(a->ir, elem_ir, fptr, v); + spl_ir_value_t fptr = spl_ir_builder_mem_field(a->ir, arr_ir, slot, i); + spl_ir_value_t v = emit_expr(a, vec_at(n->array_literal.elems, i)); + /* undefined 元素保持未初始化。 */ + if (!spl_ir_value_is_none(v)) + spl_ir_builder_mem_store(a->ir, elem_ir, fptr, v); } base = spl_ir_builder_mem_field(a->ir, arr_ir, slot, 0); } - if (!base) + if (spl_ir_value_is_none(base)) Panic("array literal slice base failed"); - spl_ir_node_ref_t len = spl_ir_builder_type_const_int(a->ir, usize_ir, nelems); + spl_ir_value_t len = spl_ir_value_const_int(usize_ir, nelems); spl_ir_node_ref_t node = spl_ir_builder_agg_construct(a->ir, t); if (!node) Panic("agg_construct failed for array-to-slice"); spl_ir_builder_agg_construct_field(a->ir, node, base); spl_ir_builder_agg_construct_field(a->ir, node, len); - return node; + return spl_ir_value_inst(node); } - spl_ir_node_ref_vec_t field_refs; - vec_init(field_refs); - for (usize i = 0; i < nelems; ++i) { - spl_ir_node_ref_t f = emit_expr(a, vec_at(n->array_literal.elems, i)); - if (!f) - Panic("array element emit failed"); - vec_push(field_refs, f); + spl_type_node_t *atn = spl_type_node(&a->sema->type->ir, t); + usize arr_len = (atn && atn->kind == SPL_TYPE_ARRAY) ? atn->array_type.len : nelems; + spl_type_id_t elem_ir = 0; + if (atn && atn->kind == SPL_TYPE_ARRAY) + elem_ir = atn->array_type.element; + + spl_ir_value_vec_t field_vals; + vec_init(field_vals); + for (usize i = 0; i < arr_len; ++i) { + spl_ir_value_t f = spl_ir_value_none(); + if (i < nelems) + f = emit_expr(a, vec_at(n->array_literal.elems, i)); + else if (elem_ir) + f = emit_zero_value(a, elem_ir); + if (i >= nelems && spl_ir_value_is_none(f)) + Panic("cannot zero-init array element #%zu", i); + vec_push(field_vals, f); } spl_ir_node_ref_t node = spl_ir_builder_agg_construct(a->ir, t); if (!node) Panic("agg_construct failed for array literal"); - for (usize i = 0; i < nelems; ++i) - spl_ir_builder_agg_construct_field(a->ir, node, vec_at(field_refs, i)); + for (usize i = 0; i < arr_len; ++i) + spl_ir_builder_agg_construct_field(a->ir, node, vec_at(field_vals, i)); - vec_free(field_refs); - return node; + vec_free(field_vals); + return spl_ir_value_inst(node); } -static spl_ir_node_ref_t emit_expr_aggregate_lit(spl_ast2ir_t *a, spl_ast_node_t *n) { +static spl_ir_value_t emit_expr_aggregate_lit(spl_ast2ir_t *a, spl_ast_node_t *n) { spl_type_id_t t = node_ir(a, n); if (!t) Panic("aggregate literal has no type after sema"); @@ -1269,19 +1299,37 @@ static spl_ir_node_ref_t emit_expr_aggregate_lit(spl_ast2ir_t *a, spl_ast_node_t spl_type_id_t tag_ir = tn->agg_type.tag_type; - spl_ir_node_ref_t eptr = spl_ir_builder_mem_alloca(a->ir, t, const_usize(a, 1)); + bool has_payload = item->struct_init_item.expr != 0; + spl_ir_value_t payload = spl_ir_value_none(); + if (has_payload) + payload = emit_expr(a, item->struct_init_item.expr); + /* undefined 表示载荷不初始化,此时也不补零未激活变体。 */ + bool payload_defined = has_payload && !spl_ir_value_is_none(payload); - spl_ir_node_ref_t tptr = - spl_ir_builder_mem_field(a->ir, t, eptr, SPL_ENUM_TAG_FIELD_IDX); - spl_ir_node_ref_t tag_val = spl_ir_builder_type_const_int(a->ir, tag_ir, vidx); + spl_ir_value_t eptr = spl_ir_builder_mem_alloca(a->ir, t, const_usize(a, 1)); + + spl_ir_value_t tptr = spl_ir_builder_mem_field(a->ir, t, eptr, SPL_ENUM_TAG_FIELD_IDX); + spl_ir_value_t tag_val = spl_ir_value_const_int(tag_ir, vidx); spl_ir_builder_mem_store(a->ir, tag_ir, tptr, tag_val); - if (item->struct_init_item.expr) { + if (!has_payload || payload_defined) { + usize nvar = vec_size(tn->agg_type.variants); + for (usize j = 0; j < nvar; ++j) { + if (j == vidx) + continue; + spl_type_id_t ftid = vec_at(tn->agg_type.variants, j).type_id; + if (!ftid) + continue; + spl_ir_value_t zero = emit_zero_value(a, ftid); + if (spl_ir_value_is_none(zero)) + Panic("cannot zero-init inactive enum variant #%zu", j); + spl_ir_value_t pptr = spl_ir_builder_mem_field(a->ir, t, eptr, j + 1); + spl_ir_builder_mem_store(a->ir, ftid, pptr, zero); + } + } + if (payload_defined) { spl_type_id_t payload_ir = vec_at(tn->agg_type.variants, vidx).type_id; - spl_ir_node_ref_t pptr = spl_ir_builder_mem_field(a->ir, t, eptr, vidx + 1); - spl_ir_node_ref_t payload = emit_expr(a, item->struct_init_item.expr); - if (!payload) - Panic("enum payload emit failed"); + spl_ir_value_t pptr = spl_ir_builder_mem_field(a->ir, t, eptr, vidx + 1); spl_ir_builder_mem_store(a->ir, payload_ir, pptr, payload); } @@ -1294,10 +1342,17 @@ static spl_ir_node_ref_t emit_expr_aggregate_lit(spl_ast2ir_t *a, spl_ast_node_t for (usize i = 0; i < nfields; ++i) vec_push(field_tids, vec_at(tn->agg_type.variants, i).type_id); - spl_ir_node_ref_vec_t fields; + spl_ir_value_vec_t fields; vec_init(fields); for (usize i = 0; i < nfields; ++i) - vec_push(fields, 0); + vec_push(fields, spl_ir_value_none()); + + bool *given = calloc(nfields ? nfields : 1, sizeof(bool)); + if (!given) { + vec_free(field_tids); + vec_free(fields); + Panic("out of memory"); + } for (usize i = 0; i < vec_size(n->aggregate_literal.fields); ++i) { spl_ast_node_t *item = spl_ast_node(a->ast, vec_at(n->aggregate_literal.fields, i)); @@ -1309,16 +1364,16 @@ static spl_ir_node_ref_t emit_expr_aggregate_lit(spl_ast2ir_t *a, spl_ast_node_t Panic("aggregate field `%s` not found (sema should have caught this)", item->struct_init_item.name); - spl_ir_node_ref_t f = emit_expr(a, item->struct_init_item.expr); - if (!f) - Panic("aggregate field emit failed"); - vec_at(fields, idx) = f; + given[idx] = true; + /* undefined 的字段保持未初始化,不能补零。 */ + vec_at(fields, idx) = emit_expr(a, item->struct_init_item.expr); } for (usize i = 0; i < nfields; ++i) { - if (!vec_at(fields, i)) { - spl_ir_node_ref_t f = emit_zero_value(a, vec_at(field_tids, i)); - if (!f) { + if (!given[i]) { + spl_ir_value_t f = emit_zero_value(a, vec_at(field_tids, i)); + if (spl_ir_value_is_none(f)) { + free(given); vec_free(field_tids); vec_free(fields); Panic("cannot zero-init aggregate field #%zu", i); @@ -1329,35 +1384,30 @@ static spl_ir_node_ref_t emit_expr_aggregate_lit(spl_ast2ir_t *a, spl_ast_node_t spl_ir_node_ref_t node = spl_ir_builder_agg_construct(a->ir, t); if (!node) { + free(given); vec_free(field_tids); vec_free(fields); Panic("agg_construct failed for aggregate literal"); } - for (usize i = 0; i < nfields; ++i) { - spl_ir_node_ref_t f = vec_at(fields, i); - if (!f) { - vec_free(field_tids); - vec_free(fields); - Panic("aggregate field #%zu missing (sema should have caught this)", i); - } - spl_ir_builder_agg_construct_field(a->ir, node, f); - } + for (usize i = 0; i < nfields; ++i) + spl_ir_builder_agg_construct_field(a->ir, node, vec_at(fields, i)); + free(given); vec_free(field_tids); vec_free(fields); - return node; + return spl_ir_value_inst(node); } -static spl_ir_node_ref_t emit_expr_match(spl_ast2ir_t *a, spl_ast_node_t *n) { +static spl_ir_value_t emit_expr_match(spl_ast2ir_t *a, spl_ast_node_t *n) { spl_ast_node_ref_t matched_ref = n->match_expr.matched_expr; spl_ast_node_t *matched_node = spl_ast_node(a->ast, matched_ref); spl_lang_type_id_t matched_lang = node_lang(matched_node); spl_type_id_t matched_ir = to_ir_type(a, matched_lang); if (!matched_ir) - return 0; + return spl_ir_value_none(); - spl_ir_node_ref_t scrut_slot = 0; + spl_ir_value_t scrut_slot = spl_ir_value_none(); int matched_place_mode = 0; for (usize i = 0; i < vec_size(n->match_expr.arms); ++i) { spl_ast_node_t *arm = spl_ast_node(a->ast, vec_at(n->match_expr.arms, i)); @@ -1370,17 +1420,17 @@ static spl_ir_node_ref_t emit_expr_match(spl_ast2ir_t *a, spl_ast_node_t *n) { if (matched_place_mode) { scrut_slot = emit_lvalue(a, matched_ref); - if (!scrut_slot) { + if (spl_ir_value_is_none(scrut_slot)) { spl_diag_error(a->diag, SPL_DIAG_KIND_NO_LVALUE, matched_node ? &matched_node->dbg : &n->dbg, "cannot bind by reference: matched value is not an lvalue"); - return 0; + return spl_ir_value_none(); } } else { scrut_slot = spl_ir_builder_mem_alloca(a->ir, matched_ir, const_usize(a, 1)); - spl_ir_node_ref_t scrut_val = emit_expr(a, matched_ref); - if (!scrut_val) - return 0; + spl_ir_value_t scrut_val = emit_expr(a, matched_ref); + if (spl_ir_value_is_none(scrut_val)) + return spl_ir_value_none(); spl_ir_builder_mem_store(a->ir, matched_ir, scrut_slot, scrut_val); } @@ -1395,9 +1445,9 @@ static spl_ir_node_ref_t emit_expr_match(spl_ast2ir_t *a, spl_ast_node_t *n) { spl_ast_node_t *arm = spl_ast_node(a->ast, vec_at(n->match_expr.arms, i)); if (arm && arm->kind == SPL_AST_MATCH_ARM) { - spl_ir_node_ref_t cond = + spl_ir_value_t cond = emit_match_check(a, arm->match_arm.pattern, matched_ir, scrut_slot); - if (cond) { + if (!spl_ir_value_is_none(cond)) { spl_ir_node_ref_t br = spl_ir_builder_control_br(a->ir, cond); vec_push(arm_branches, br); } else { @@ -1472,15 +1522,15 @@ static spl_ir_node_ref_t emit_expr_match(spl_ast2ir_t *a, spl_ast_node_t *n) { vec_free(body_jmps); vec_free(body_blks); - return 0; + return spl_ir_value_none(); } -static spl_ir_node_ref_t emit_expr_slice(spl_ast2ir_t *a, spl_ast_node_t *n) { +static spl_ir_value_t emit_expr_slice(spl_ast2ir_t *a, spl_ast_node_t *n) { spl_ast_node_t *obj = spl_ast_node(a->ast, n->slice.obj); spl_lang_type_id_t obj_lang = node_lang(obj); spl_lang_type_node_t *obj_ln = spl_lang_type_node(a->sema->type, obj_lang); if (!obj_ln) - return 0; + return spl_ir_value_none(); spl_lang_type_kind_t obj_kind = obj_ln->kind; spl_lang_type_id_t obj_elem = obj_ln->extra.elem; @@ -1488,74 +1538,73 @@ static spl_ir_node_ref_t emit_expr_slice(spl_ast2ir_t *a, spl_ast_node_t *n) { spl_type_id_t slice_ir = node_ir(a, n); if (!slice_ir) - return 0; + return spl_ir_value_none(); spl_lang_type_id_t elem_lang = obj_elem; spl_type_id_t elem_ir = to_ir_type(a, elem_lang); spl_lang_type_id_t usize_lang = spl_lang_make_int(a->sema->type, sizeof(void *) * 8, 0); spl_type_id_t usize_ir = to_ir_type(a, usize_lang); - spl_ir_node_ref_t base = 0; + spl_ir_value_t base = spl_ir_value_none(); if (obj_kind == SPL_LANG_TYPE_PTR) { base = emit_expr(a, n->slice.obj); } else if (obj_kind == SPL_LANG_TYPE_SLICE) { - spl_ir_node_ref_t slice_val = emit_expr(a, n->slice.obj); - if (!slice_val) - return 0; + spl_ir_value_t slice_val = emit_expr(a, n->slice.obj); + if (spl_ir_value_is_none(slice_val)) + return spl_ir_value_none(); spl_type_id_t ptr_ir = spl_type_builder_ptr(&a->sema->type->ir, elem_ir); - base = spl_ir_builder_agg_extract(a->ir, obj_ir, ptr_ir, SPL_SLICE_PTR_FIELD_IDX, - slice_val); + base = spl_ir_builder_agg_extract(a->ir, obj_ir, ptr_ir, SPL_SLICE_PTR_FIELD_IDX, slice_val); } else { base = emit_lvalue(a, n->slice.obj); } - if (!base) - return 0; + if (spl_ir_value_is_none(base)) + return spl_ir_value_none(); - spl_ir_node_ref_t begin_v = 0; + spl_ir_value_t begin_v = spl_ir_value_none(); if (n->slice.begin) begin_v = emit_expr(a, n->slice.begin); else - begin_v = spl_ir_builder_type_const_int(a->ir, usize_ir, 0); - if (!begin_v) - return 0; + begin_v = spl_ir_value_const_int(usize_ir, 0); + if (spl_ir_value_is_none(begin_v)) + return spl_ir_value_none(); - spl_ir_node_ref_t ptr = spl_ir_builder_mem_offset(a->ir, elem_ir, base, begin_v); - if (!ptr) - return 0; + spl_ir_value_t ptr = spl_ir_builder_mem_offset(a->ir, elem_ir, base, begin_v); + if (spl_ir_value_is_none(ptr)) + return spl_ir_value_none(); - spl_ir_node_ref_t end_v = 0; + spl_ir_value_t end_v = spl_ir_value_none(); if (n->slice.end) { end_v = emit_expr(a, n->slice.end); } else if (obj_kind == SPL_LANG_TYPE_ARRAY) { spl_type_node_t *arr = spl_type_node(&a->sema->type->ir, obj_ir); if (!arr) - return 0; + return spl_ir_value_none(); usize arr_len = arr->array_type.len; - end_v = spl_ir_builder_type_const_int(a->ir, usize_ir, arr_len); + end_v = spl_ir_value_const_int(usize_ir, arr_len); } else if (obj_kind == SPL_LANG_TYPE_SLICE) { - spl_ir_node_ref_t slice_val = emit_expr(a, n->slice.obj); + spl_ir_value_t slice_val = emit_expr(a, n->slice.obj); end_v = spl_ir_builder_agg_extract(a->ir, obj_ir, usize_ir, SPL_SLICE_LEN_FIELD_IDX, slice_val); } else { - return 0; + return spl_ir_value_none(); } - if (!end_v) - return 0; + if (spl_ir_value_is_none(end_v)) + return spl_ir_value_none(); - spl_ir_node_ref_t len = spl_ir_builder_arith_sub(a->ir, usize_ir, end_v, begin_v); + spl_ir_value_t len = spl_ir_builder_arith_sub(a->ir, usize_ir, end_v, begin_v); spl_ir_node_ref_t node = spl_ir_builder_agg_construct(a->ir, slice_ir); if (!node) - return 0; + return spl_ir_value_none(); spl_ir_builder_agg_construct_field(a->ir, node, ptr); spl_ir_builder_agg_construct_field(a->ir, node, len); - return node; + return spl_ir_value_inst(node); } -static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { +static spl_ir_value_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { spl_ast_node_t *n = spl_ast_node(a->ast, ref); if (!n) - return 0; + return spl_ir_value_none(); switch (n->kind) { case SPL_AST_EXPR_INT_LIT: { @@ -1563,47 +1612,53 @@ static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref) spl_type_node_t *itn = spl_type_node(&a->sema->type->ir, it); if (itn && itn->kind == SPL_TYPE_FLOAT) { double d = (double)(int64_t)n->int_lit.bits; - return spl_ir_builder_type_const_float(a->ir, it, d); + return spl_ir_value_const_float(it, d); } - return spl_ir_builder_type_const_int(a->ir, it, (usize)n->int_lit.bits); + return spl_ir_value_const_int(it, (uint64_t)n->int_lit.bits); } case SPL_AST_EXPR_FLOAT_LIT: - return spl_ir_builder_type_const_float(a->ir, node_ir(a, n), n->float_lit.bits); + return spl_ir_value_const_float(node_ir(a, n), n->float_lit.bits); case SPL_AST_EXPR_CHAR_LIT: - return spl_ir_builder_type_const_char(a->ir, node_ir(a, n), n->char_lit.val); + return spl_ir_value_const_int(node_ir(a, n), (uint64_t)(unsigned char)n->char_lit.val); case SPL_AST_EXPR_STRING_LIT: return make_slice_bytes(a, n, n->string_lit.data, n->string_lit.len); case SPL_AST_EXPR_BOOL_LIT: - return spl_ir_builder_type_const_bool(a->ir, node_ir(a, n), n->bool_lit.val); + return spl_ir_builder_const_bool(node_ir(a, n), n->bool_lit.val); case SPL_AST_EXPR_NULL_LIT: - return spl_ir_builder_type_const_ptr(a->ir, node_ir(a, n), NULL); + return spl_ir_builder_const_null(node_ir(a, n)); case SPL_AST_EXPR_UNDEFINED_LIT: - return 0; + return spl_ir_value_none(); case SPL_AST_EXPR_IDENT: { spl_def_node_t *def = spl_def_node(a->sema->type, n->resolved_def_id); if (def && def->kind == SPL_DEF_FN) { - spl_ir_node_ref_t fn_ref = 0; - if (!map_get(a->def2ir_map, n->resolved_def_id, &fn_ref)) - return 0; - return spl_ir_builder_type_const_fn(a->ir, node_ir(a, n), (spl_ir_func_ref_t)fn_ref); + spl_ir_value_t fn_val = spl_ir_value_none(); + if (!map_get(a->def2ir_map, n->resolved_def_id, &fn_val)) + return spl_ir_value_none(); + return fn_val; } - spl_ir_node_ref_t slot = 0; + usize gid = 0; + if (map_get(a->global_map, n->resolved_def_id, &gid)) { + spl_type_id_t ptr_tid = spl_type_builder_ptr(&a->sema->type->ir, node_ir(a, n)); + spl_ir_value_t ptr = spl_ir_builder_global_addr(ptr_tid, gid); + return spl_ir_builder_mem_load(a->ir, node_ir(a, n), ptr); + } + spl_ir_value_t slot = spl_ir_value_none(); if (!map_get(a->def2ir_map, n->resolved_def_id, &slot)) - return 0; + return spl_ir_value_none(); return spl_ir_builder_mem_load(a->ir, node_ir(a, n), slot); } case SPL_AST_EXPR_UNARY: { if (n->unary.op == SPL_UNARY_OP_ADDRESS) return emit_lvalue(a, n->unary.operand); if (n->unary.op == SPL_UNARY_OP_DEREF) { - spl_ir_node_ref_t ptr = emit_lvalue(a, ref); - if (!ptr) - return 0; + spl_ir_value_t ptr = emit_lvalue(a, ref); + if (spl_ir_value_is_none(ptr)) + return spl_ir_value_none(); return spl_ir_builder_mem_load(a->ir, node_ir(a, n), ptr); } - spl_ir_node_ref_t v = emit_expr(a, n->unary.operand); - if (!v) - return 0; + spl_ir_value_t v = emit_expr(a, n->unary.operand); + if (spl_ir_value_is_none(v)) + return spl_ir_value_none(); spl_type_id_t t = node_ir(a, n); switch (n->unary.op) { case SPL_UNARY_OP_MINUS: @@ -1612,7 +1667,7 @@ static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref) case SPL_UNARY_OP_BIT_NOT: return spl_ir_builder_arith_not(a->ir, t, v); default: - return 0; + return spl_ir_value_none(); } } case SPL_AST_EXPR_BINARY: @@ -1625,9 +1680,9 @@ static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref) return emit_expr_field(a, n, ref); case SPL_AST_EXPR_INDEX: { - spl_ir_node_ref_t ptr = emit_lvalue(a, ref); - if (!ptr) - return 0; + spl_ir_value_t ptr = emit_lvalue(a, ref); + if (spl_ir_value_is_none(ptr)) + return spl_ir_value_none(); return spl_ir_builder_mem_load(a->ir, node_ir(a, n), ptr); } case SPL_AST_EXPR_CAST: @@ -1644,7 +1699,7 @@ static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref) usize defer_base = vec_size(a->defer_stack); for (usize i = 0; i < vec_size(n->block.stmts) && !a->ir->terminated; ++i) emit_stmt(a, vec_at(n->block.stmts, i)); - spl_ir_node_ref_t value = 0; + spl_ir_value_t value = spl_ir_value_none(); if (!a->ir->terminated && n->block.tail_expr) value = emit_expr(a, n->block.tail_expr); if (!a->ir->terminated) @@ -1664,37 +1719,37 @@ static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref) case SPL_AST_EXPR_FOR: spl_diag_fatal(a->diag, SPL_DIAG_KIND_UNHANDLED_NODE, &n->dbg, "for is not implemented yet"); - return 0; + return spl_ir_value_none(); case SPL_AST_EXPR_SLICE: return emit_expr_slice(a, n); case SPL_AST_EXPR_COMPILE_CALL: { spl_intrin_t in = spl_intrin_lookup_call(n->compile_call.name); if (in == SPL_INTRIN_NONE) - return 0; + return spl_ir_value_none(); return ast2ir_intrin_call(a, n, in, &n->compile_call.args); } default: TODO(); - return 0; + return spl_ir_value_none(); } } -static spl_ir_node_ref_t emit_if_like(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { +static spl_ir_value_t emit_if_like(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { spl_ast_node_t *n = spl_ast_node(a->ast, ref); if (!n) - return 0; + return spl_ir_value_none(); bool is_ifvar = (n->kind == SPL_AST_EXPR_IFVAR); spl_type_id_t result_t = node_ir(a, n); spl_type_node_t *rtn = result_t ? spl_type_node(&a->sema->type->ir, result_t) : NULL; bool need_result = rtn && rtn->kind != SPL_TYPE_UNIT; - spl_ir_node_ref_t result_slot = 0; + spl_ir_value_t result_slot = spl_ir_value_none(); if (need_result) result_slot = spl_ir_builder_mem_alloca(a->ir, result_t, const_usize(a, 1)); - spl_ir_node_ref_t cond = 0; + spl_ir_value_t cond = spl_ir_value_none(); spl_ast_node_ref_t then_ref = 0; spl_ast_node_ref_t else_ref = 0; @@ -1702,8 +1757,8 @@ static spl_ir_node_ref_t emit_if_like(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { spl_ast_node_t *vd = spl_ast_node(a->ast, n->ifvar_expr.var_decl); if (!vd) Panic("ifvar missing var_decl"); - spl_ir_node_ref_t slot = emit_var_decl(a, vd); - if (!slot) + spl_ir_value_t slot = emit_var_decl(a, vd); + if (spl_ir_value_is_none(slot)) Panic("ifvar var_decl emit failed"); cond = spl_ir_builder_mem_load(a->ir, node_ir(a, vd), slot); then_ref = n->ifvar_expr.then_block; @@ -1713,17 +1768,17 @@ static spl_ir_node_ref_t emit_if_like(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { then_ref = n->if_expr.then_block; else_ref = n->if_expr.else_branch; } - if (!cond) + if (spl_ir_value_is_none(cond)) Panic("if cond emit failed"); spl_ir_node_ref_t br = spl_ir_builder_control_br(a->ir, cond); spl_ir_builder_block_end(a->ir); usize then_blk = spl_ir_builder_block_begin(a->ir); - spl_ir_node_ref_t then_v = 0; + spl_ir_value_t then_v = spl_ir_value_none(); if (then_ref) then_v = emit_expr(a, then_ref); - if (result_slot && then_v) + if (need_result && !spl_ir_value_is_none(then_v)) spl_ir_builder_mem_store(a->ir, result_t, result_slot, then_v); spl_ir_node_ref_t then_end = 0; if (!a->ir->terminated) @@ -1731,10 +1786,10 @@ static spl_ir_node_ref_t emit_if_like(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { spl_ir_builder_block_end(a->ir); usize else_blk = spl_ir_builder_block_begin(a->ir); - spl_ir_node_ref_t else_v = 0; + spl_ir_value_t else_v = spl_ir_value_none(); if (else_ref) else_v = emit_expr(a, else_ref); - if (result_slot && else_v) + if (need_result && !spl_ir_value_is_none(else_v)) spl_ir_builder_mem_store(a->ir, result_t, result_slot, else_v); spl_ir_node_ref_t else_end = 0; if (!a->ir->terminated) @@ -1748,22 +1803,22 @@ static spl_ir_node_ref_t emit_if_like(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { if (else_end) spl_ir_builder_control_jmp_fixup(a->ir, else_end, merge_blk); - if (!result_slot) - return 0; + if (!need_result) + return spl_ir_value_none(); return spl_ir_builder_mem_load(a->ir, result_t, result_slot); } -static spl_ir_node_ref_t emit_loop_like(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { +static spl_ir_value_t emit_loop_like(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { spl_ast_node_t *n = spl_ast_node(a->ast, ref); if (!n) - return 0; + return spl_ir_value_none(); bool is_while = (n->kind == SPL_AST_EXPR_WHILE); spl_type_id_t result_t = node_ir(a, n); spl_type_node_t *rtn = result_t ? spl_type_node(&a->sema->type->ir, result_t) : NULL; bool need_result = rtn && rtn->kind != SPL_TYPE_UNIT; - spl_ir_node_ref_t result_slot = 0; + spl_ir_value_t result_slot = spl_ir_value_none(); if (need_result) result_slot = spl_ir_builder_mem_alloca(a->ir, result_t, const_usize(a, 1)); @@ -1777,8 +1832,8 @@ static spl_ir_node_ref_t emit_loop_like(spl_ast2ir_t *a, spl_ast_node_ref_t ref) if (is_while) { head_blk = spl_ir_builder_block_begin(a->ir); - spl_ir_node_ref_t cond = emit_expr(a, n->while_expr.cond); - if (!cond) + spl_ir_value_t cond = emit_expr(a, n->while_expr.cond); + if (spl_ir_value_is_none(cond)) Panic("while cond emit failed"); br = spl_ir_builder_control_br(a->ir, cond); spl_ir_builder_block_end(a->ir); @@ -1813,10 +1868,10 @@ static spl_ir_node_ref_t emit_loop_like(spl_ast2ir_t *a, spl_ast_node_ref_t ref) spl_ir_builder_block_end(a->ir); usize after_blk = spl_ir_builder_block_begin(a->ir); - spl_ir_node_ref_t else_v = 0; + spl_ir_value_t else_v = spl_ir_value_none(); if (is_while && n->while_expr.else_branch) else_v = emit_expr(a, n->while_expr.else_branch); - if (result_slot && else_v) + if (need_result && !spl_ir_value_is_none(else_v)) spl_ir_builder_mem_store(a->ir, result_t, result_slot, else_v); if (pre_jmp) @@ -1832,8 +1887,8 @@ static spl_ir_node_ref_t emit_loop_like(spl_ast2ir_t *a, spl_ast_node_ref_t ref) vec_free(top->breaks); (void)vec_pop(a->loop_stack); - if (!result_slot) - return 0; + if (!need_result) + return spl_ir_value_none(); return spl_ir_builder_mem_load(a->ir, result_t, result_slot); } @@ -1873,9 +1928,9 @@ static void emit_break_stmt(spl_ast2ir_t *a, spl_ast_node_t *n) { /* Emitting a value or a cleanup can grow/reallocate loop_stack. */ spl_loop_ctx_t target = vec_at(a->loop_stack, target_index); - if (n->statement_break.expr && target.result_slot) { - spl_ir_node_ref_t bv = emit_expr(a, n->statement_break.expr); - if (bv) + if (n->statement_break.expr && !spl_ir_value_is_none(target.result_slot)) { + spl_ir_value_t bv = emit_expr(a, n->statement_break.expr); + if (!spl_ir_value_is_none(bv)) spl_ir_builder_mem_store(a->ir, target.result_tid, target.result_slot, bv); } @@ -1895,7 +1950,7 @@ static void emit_stmt(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { case SPL_AST_STATEMENT_RET: { spl_type_id_t ret_ir = to_ir_type(a, a->cur_ret_lang); - spl_ir_node_ref_t v = 0; + spl_ir_value_t v = spl_ir_value_none(); if (n->statement_ret.expr) v = emit_expr(a, n->statement_ret.expr); @@ -1973,11 +2028,20 @@ static void emit_decl(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { } } +static bool ast_fn_is_extern(spl_ast2ir_t *a, spl_ast_node_t *n) { + for (usize i = 0; i < vec_size(n->attrs); ++i) { + spl_attr_node_t *attr = spl_attr_node(a->sema->attr, vec_at(n->attrs, i)); + if (attr && attr->info && attr->info->name && strcmp(attr->info->name, "extern") == 0) + return true; + } + return false; +} + static void emit_fn_reserve(spl_ast2ir_t *a, spl_ast_node_t *n) { if (!n || !n->resolved_def_id) return; - spl_ir_node_ref_t existing = 0; + spl_ir_value_t existing = spl_ir_value_none(); if (map_get(a->def2ir_map, n->resolved_def_id, &existing)) { spl_diag_error(a->diag, SPL_DIAG_KIND_INTERNAL, &n->dbg, "fn `%s` reserved twice (def#%zu);", n->fn_decl.name, n->resolved_def_id); @@ -1993,18 +2057,64 @@ static void emit_fn_reserve(spl_ast2ir_t *a, spl_ast_node_t *n) { if (!fn_ir) return; - spl_ir_func_ref_t fn_ref = spl_ir_builder_fn_reserve(a->ir, n->fn_decl.name, fn_ir); - map_put(a->def2ir_map, n->resolved_def_id, (spl_ir_node_ref_t)fn_ref); + char qbuf[1024]; + const char *ir_name = n->fn_decl.name; + bool root_main = strcmp(n->fn_decl.name, "main") == 0 && a->ast == a->sema->ast; + if (!root_main && !ast_fn_is_extern(a, n) && n->resolved_ns_id) { + if (spl_ns_symbol_qualified(a->sema->ns, n->resolved_ns_id, n->fn_decl.name, qbuf, + sizeof qbuf)) + ir_name = qbuf; + } + spl_ir_func_ref_t fn_ref = spl_ir_builder_fn_reserve(a->ir, ir_name, fn_ir); + map_put(a->def2ir_map, n->resolved_def_id, spl_ir_value_const_fn(fn_ir, fn_ref)); copy_attrs_to_ir_func(a, n, fn_ref); } +static void emit_module_inits(spl_ast2ir_t *a) { + for (usize i = 0; i < vec_size(a->module_inits); ++i) { + spl_ast_t *saved_ast = a->ast; + spl_module_init_t *init = &vec_at(a->module_inits, i); + a->ast = init->ast; + spl_ast_node_t *d = spl_ast_node(a->ast, init->decl); + if (!d || (d->kind != SPL_AST_VAR_DECL && d->kind != SPL_AST_CONST_DECL)) { + a->ast = saved_ast; + continue; + } + spl_ast_node_t *pat = spl_ast_node(a->ast, d->var_const_decl.pattern); + if (!pat || pat->kind != SPL_AST_PATTERN_IDENT) { + a->ast = saved_ast; + continue; + } + usize gid = 0; + if (!map_get(a->global_map, pat->resolved_def_id, &gid) || !d->var_const_decl.init_expr) { + a->ast = saved_ast; + continue; + } + spl_type_id_t tid = node_ir(a, d); + if (!tid) { + a->ast = saved_ast; + continue; + } + spl_ir_value_t val = emit_expr(a, d->var_const_decl.init_expr); + if (!spl_ir_value_is_none(val)) { + spl_type_id_t ptr_tid = spl_type_builder_ptr(&a->sema->type->ir, tid); + spl_ir_value_t ptr = spl_ir_builder_global_addr(ptr_tid, gid); + spl_ir_builder_mem_store(a->ir, tid, ptr, val); + } + a->ast = saved_ast; + } +} + static void emit_fn_body(spl_ast2ir_t *a, spl_ast_node_t *n) { if (!n || !n->resolved_def_id) return; - spl_ir_func_ref_t fn_ref = 0; - if (!map_get(a->def2ir_map, n->resolved_def_id, &fn_ref)) + spl_ir_value_t fn_val = spl_ir_value_none(); + if (!map_get(a->def2ir_map, n->resolved_def_id, &fn_val)) + return; + spl_ir_func_ref_t fn_ref = fn_val.fn; + if (!fn_ref) return; spl_lang_type_id_t fn_lang = spl_def_get_lang_id(a->sema->type, n->resolved_def_id); @@ -2043,15 +2153,18 @@ static void emit_fn_body(spl_ast2ir_t *a, spl_ast_node_t *n) { if (!p_ir) continue; - spl_ir_node_ref_t idx = const_usize(a, i); - spl_ir_node_ref_t pv = spl_ir_builder_control_param(a->ir, p_ir, idx); + spl_ir_value_t idx = const_usize(a, i); + spl_ir_value_t pv = spl_ir_builder_control_param(a->ir, p_ir, idx); - spl_ir_node_ref_t one = const_usize(a, 1); - spl_ir_node_ref_t slot = spl_ir_builder_mem_alloca(a->ir, p_ir, one); + spl_ir_value_t one = const_usize(a, 1); + spl_ir_value_t slot = spl_ir_builder_mem_alloca(a->ir, p_ir, one); spl_ir_builder_mem_store(a->ir, p_ir, slot, pv); map_put(a->def2ir_map, pd->resolved_def_id, slot); } + if (strcmp(n->fn_decl.name, "main") == 0 && a->ast == a->sema->ast) + emit_module_inits(a); + if (n->fn_decl.body) emit_expr(a, n->fn_decl.body); @@ -2060,9 +2173,9 @@ static void emit_fn_body(spl_ast2ir_t *a, spl_ast_node_t *n) { if (!a->ir->terminated) { if (ret_is_unit) { - spl_ir_builder_control_ret(a->ir, ret_ir, 0); + spl_ir_builder_control_ret(a->ir, ret_ir, spl_ir_value_none()); } else if (ret_ir) { - spl_ir_node_ref_t zero = emit_zero_value(a, ret_ir); + spl_ir_value_t zero = emit_zero_value(a, ret_ir); spl_ir_builder_control_ret(a->ir, ret_ir, zero); } else { spl_ir_builder_control_unreachable(a->ir); @@ -2113,6 +2226,36 @@ static void emit_aggregate_fns_body(spl_ast2ir_t *a, spl_ast_node_t *te) { } } +static void emit_global_var_reserve(spl_ast2ir_t *a, spl_ast_node_ref_t dref, + spl_ast_node_t *d) { + spl_ast_node_t *pat = spl_ast_node(a->ast, d->var_const_decl.pattern); + if (!pat || pat->kind != SPL_AST_PATTERN_IDENT) + return; + spl_def_id_t def = pat->resolved_def_id; + if (!def) + return; + usize existing = 0; + if (map_get(a->global_map, def, &existing)) + return; + + spl_type_id_t tid = node_ir(a, d); + if (!tid) + return; + + char name_buf[512]; + const char *local = pat->pattern_ident.name; + if (d->resolved_ns_id && + spl_ns_symbol_qualified(a->sema->ns, d->resolved_ns_id, local, name_buf, + sizeof name_buf)) + local = name_buf; + usize gid = spl_ir_builder_global_declare(a->ir, local, tid, spl_ir_value_none(), false); + if (!gid) + return; + map_put(a->global_map, def, gid); + spl_module_init_t init = {.ast = a->ast, .decl = dref}; + vec_push(a->module_inits, init); +} + static void emit_decls_reserve(spl_ast2ir_t *a, spl_ast_node_ref_vec_t *decls) { for (usize i = 0; i < vec_size(*decls); ++i) { spl_ast_node_ref_t dref = vec_at(*decls, i); @@ -2134,6 +2277,7 @@ static void emit_decls_reserve(spl_ast2ir_t *a, spl_ast_node_ref_vec_t *decls) { } case SPL_AST_VAR_DECL: case SPL_AST_CONST_DECL: + emit_global_var_reserve(a, dref, d); break; case SPL_AST_MOD_DECL: if (d->mod_decl.has_body) @@ -2202,8 +2346,8 @@ static void emit_container_body(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { emit_decls_body(a, &n->container_members.declarations); } -static spl_ir_node_ref_t emit_implicit_cast(spl_ast2ir_t *a, spl_ast_node_t *n, spl_type_id_t ft, - spl_type_id_t tt, spl_ir_node_ref_t v) { +static spl_ir_value_t emit_implicit_cast(spl_ast2ir_t *a, spl_ast_node_t *n, spl_type_id_t ft, + spl_type_id_t tt, spl_ir_value_t v) { if (!ft || !tt || ft == tt) return v; spl_type_node_t *fs = spl_type_node(&a->sema->type->ir, ft); @@ -2240,7 +2384,7 @@ static spl_ir_node_ref_t emit_implicit_cast(spl_ast2ir_t *a, spl_ast_node_t *n, spl_diag_fatal(a->diag, SPL_DIAG_KIND_IMPLICIT_CAST_FAIL, &n->dbg, "implicit cast: cannot convert type#%zu -> type#%zu", ft, tt); - return 0; + return spl_ir_value_none(); } static usize spl_int_map_hash(usize k) { return MAP_HASH_INT(k); } @@ -2253,6 +2397,8 @@ void spl_ast2ir_init(spl_ast2ir_t *ast2ir, spl_ir_builder_t *ir, spl_layout_t *l ast2ir->ast = sema->ast; ast2ir->layout = layout; map_init(ast2ir->def2ir_map, spl_int_map_hash, spl_int_map_cmp); + map_init(ast2ir->global_map, spl_int_map_hash, spl_int_map_cmp); + vec_init(ast2ir->module_inits); vec_init(ast2ir->loop_stack); vec_init(ast2ir->defer_stack); ast2ir->cur_ret_lang = 0; @@ -2266,7 +2412,9 @@ void spl_ast2ir_drop(spl_ast2ir_t *ast2ir) { vec_free(vec_at(ast2ir->loop_stack, i).breaks); vec_free(ast2ir->loop_stack); vec_free(ast2ir->defer_stack); + vec_free(ast2ir->module_inits); map_free(ast2ir->def2ir_map); + map_free(ast2ir->global_map); } void spl_ast2ir_run(spl_ast2ir_t *ast2ir, spl_ast_ptr_vec_t ast_vec) { diff --git a/stage1/sema/spl_sema.c b/stage1/sema/spl_sema.c index ec76c34..fa812e0 100644 --- a/stage1/sema/spl_sema.c +++ b/stage1/sema/spl_sema.c @@ -969,7 +969,11 @@ static spl_def_id_t sema_build_inline_prefixes(spl_sema_t *sema, const char *pat sema->ast, 0); } spl_def_id_t prefix_def = node->owner_id; - sema_ns_aggregate_add_method(sema, prefix_def, segs.data[i], child_def); + /* segs 指向临时 buf,方法名必须引用生命周期更长的命名空间名。 */ + spl_ns_id_t child_ns = spl_ns_child(sema->ns, prefix_ns, segs.data[i]); + const char *method_name = + child_ns ? vec_at(sema->ns->nodes, child_ns).name : segs.data[i]; + sema_ns_aggregate_add_method(sema, prefix_def, method_name, child_def); child_def = prefix_def; }