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)
This commit is contained in:
zzy committed 2026-12-02 22:41:20 +08:00
1 parent 663adf6cae
commit 480945a92f
9 files changed
+1657 -1110

No files matched your search

+232 -169
View File
@@ -8,16 +8,27 @@
聚合值统一表示为「指向存储的指针」:u64 sN[..]; void* vN。
类型/布局全部走 layout,生成期不做运行时分发。非 extern 函数名 f<fid>。 */
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<base+slot>。 */
#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<slot> 变量。 */
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;
}