#include "spl_ir2c.h" #include #include #include /* 每个 IR ref 生成一个 C 局部变量(交给 C 编译器做 mem2reg),控制流用 goto 标签; 聚合值统一表示为「指向存储的指针」:u64 sN[..]; void* vN。 类型/布局全部走 layout,生成期不做运行时分发。非 extern 函数名 f。 */ 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 开始编号 */ bool failed; } ir2c_t; static void buf_append(spl_ir2c_buf_t *b, const char *s, usize n) { if (n == 0) return; if (b->size + n > b->cap) { usize cap = b->cap ? b->cap : 256; while (cap < b->size + n) cap *= 2; vec_realloc((*b), cap); } memcpy(b->data + b->size, s, n); b->size += 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) { 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 void put_i(ir2c_t *c, long long v) { char buf[32]; int n = snprintf(buf, sizeof buf, "%lld", v); if (n > 0) buf_append(c->out, buf, (usize)n); } static void put_unsupported(ir2c_t *c, const char *what) { c->failed = true; put(c, "/* unsupported: "); put(c, what); put(c, " */\n"); } static const spl_layout_info_t *info_of(ir2c_t *c, spl_type_id_t tid) { return spl_layout_info_of(c->layout, tid); } static usize type_size(ir2c_t *c, spl_type_id_t tid) { const spl_layout_info_t *info = info_of(c, tid); if (!info) return 0; return (info->size_bits + 7) / 8; } static usize type_align(ir2c_t *c, spl_type_id_t tid) { const spl_layout_info_t *info = info_of(c, tid); if (!info) return 1; usize a = info->align_bits / 8; return a ? a : 1; } static bool is_aggregate(ir2c_t *c, spl_type_id_t tid) { spl_type_node_t *te = spl_type_node(c->type, tid); if (!te) return false; switch (te->kind) { case SPL_TYPE_ARRAY: case SPL_TYPE_STRUCT: case SPL_TYPE_UNION: case SPL_TYPE_ENUM: case SPL_TYPE_SHAPE: return true; default: return false; } } static bool tid_is_bool(ir2c_t *c, spl_type_id_t tid) { spl_type_node_t *te = spl_type_node(c->type, tid); return te && te->kind == SPL_TYPE_BOOL; } static bool tid_is_unit(ir2c_t *c, spl_type_id_t tid) { if (tid == 0) return true; spl_type_node_t *te = spl_type_node(c->type, tid); if (!te) return true; return te->kind == SPL_TYPE_UNIT; } static spl_type_id_t fn_ret_type(ir2c_t *c, spl_type_id_t fn_tid) { spl_type_node_t *te = spl_type_node(c->type, fn_tid); if (!te || te->kind != SPL_TYPE_FN) return 0; return te->fn_type.ret; } static spl_type_id_t make_ptr(ir2c_t *c, spl_type_id_t pointee) { return spl_type_builder_ptr((spl_type_t *)c->type, pointee); } static spl_type_id_t make_bool(ir2c_t *c) { return spl_type_builder_simple((spl_type_t *)c->type, SPL_TYPE_BOOL); } static spl_type_id_t make_usize(ir2c_t *c) { return spl_type_builder_int((spl_type_t *)c->type, 64, 0); } static bool put_type(ir2c_t *c, spl_type_id_t tid) { spl_type_node_t *te = spl_type_node(c->type, tid); if (!te) return false; switch (te->kind) { case SPL_TYPE_UNIT: put(c, "void"); return true; case SPL_TYPE_NEVER: put(c, "void"); return true; case SPL_TYPE_BOOL: put(c, "u8"); return true; case SPL_TYPE_INT: if (te->int_type.bits == 8) put(c, te->int_type.is_signed ? "i8" : "u8"); else if (te->int_type.bits == 16) put(c, te->int_type.is_signed ? "i16" : "u16"); else if (te->int_type.bits == 32) put(c, te->int_type.is_signed ? "i32" : "u32"); else put(c, te->int_type.is_signed ? "i64" : "u64"); return true; case SPL_TYPE_FLOAT: put(c, te->float_type.bits == 32 ? "float" : "double"); return true; case SPL_TYPE_PTR: put(c, "void*"); return true; case SPL_TYPE_FN: put(c, "void*"); return true; /* 聚合值统一以「指向存储的指针」表示 */ case SPL_TYPE_ARRAY: case SPL_TYPE_STRUCT: case SPL_TYPE_UNION: case SPL_TYPE_ENUM: case SPL_TYPE_SHAPE: put(c, "void*"); return true; default: return false; } } static bool fn_is_extern(ir2c_t *c, const spl_ir_func_t *f) { return spl_ir_fn_extern_attr(c->attr, f) != NULL; } static void c_fn_name(ir2c_t *c, spl_ir_func_ref_t fid) { const spl_ir_func_t *f = spl_ir_func((spl_ir_t *)c->ir, fid); if (f && fn_is_extern(c, f)) { if (f->name) put_n(c, f->name, strlen(f->name)); return; } put(c, "f"); 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; } } /* 函数常量节点(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 spl_type_id_t field_tid(ir2c_t *c, spl_type_id_t tid, usize idx) { spl_type_node_t *te = spl_type_node(c->type, tid); if (!te) return 0; switch (te->kind) { case SPL_TYPE_ENUM: if (idx == SPL_ENUM_TAG_FIELD_IDX) return te->agg_type.tag_type; idx -= 1; /* fallthrough */ case SPL_TYPE_STRUCT: case SPL_TYPE_UNION: case SPL_TYPE_SHAPE: if (idx < vec_size(te->agg_type.variants)) return vec_at(te->agg_type.variants, idx).type_id; return 0; case SPL_TYPE_ARRAY: return te->array_type.element; default: return 0; } } static usize field_off(ir2c_t *c, spl_type_id_t tid, usize idx) { spl_type_node_t *te = spl_type_node(c->type, tid); if (!te) return 0; if (te->kind == SPL_TYPE_ARRAY) return idx * type_size(c, te->array_type.element); const spl_layout_info_t *info = info_of(c, tid); if (!info || idx >= vec_size(info->field_bit_offsets)) return 0; return vec_at(info->field_bit_offsets, idx) / 8; } /* 节点的「值类型」;0 表示无结果。 */ static spl_type_id_t result_tid(ir2c_t *c, const spl_ir_node_t *n) { 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: return n->arith.tid; 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: return make_bool(c); 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: 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: case SPL_IR_TYPE_OFFSETOF: case SPL_IR_TYPE_FIELD_COUNT: return make_usize(c); case SPL_IR_AGG_CONSTRUCT: return n->agg_construct.tid; case SPL_IR_AGG_EXTRACT: return n->agg_extract.field_tid; case SPL_IR_AGG_INSERT: return n->agg_insert.tid; case SPL_IR_CONTROL_SELECT: return n->control_select.tid; case SPL_IR_CONTROL_CALL: return n->control_call.tid; case SPL_IR_CONTROL_PARAM: return n->control_param.tid; default: return 0; } } static void bind_agg(ir2c_t *c, usize ref) { put(c, "v"); put_u(c, ref); put(c, " = (void*)s"); put_u(c, ref); put(c, ";\n"); } static void declare_ref(ir2c_t *c, usize ref, spl_type_id_t tid) { if (is_aggregate(c, tid)) { usize size = type_size(c, tid); usize words = (size + 7) / 8; if (words == 0) words = 1; put(c, "u64 s"); put_u(c, ref); put(c, "["); put_u(c, words); put(c, "]; void* v"); put_u(c, ref); put(c, ";\n"); return; } if (!put_type(c, tid)) put(c, "void*"); put(c, " v"); put_u(c, ref); put(c, ";\n"); } static void emit_agg_construct(ir2c_t *c, usize ref, const spl_ir_node_t *n) { if (!is_aggregate(c, n->agg_construct.tid)) { put_unsupported(c, "agg construct non-agg"); return; } 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_type_id_t ftid = field_tid(c, agg_t, i); if (!ftid) continue; usize foff = field_off(c, agg_t, i); if (is_aggregate(c, ftid)) { usize fsz = type_size(c, ftid); put(c, "memcpy((u8*)v"); put_u(c, ref); put(c, " + "); put_u(c, foff); put(c, ", v"); put_u(c, fref); put(c, ", "); put_u(c, fsz); put(c, ");\n"); } else { put(c, "*("); put_type(c, ftid); put(c, "*)((u8*)v"); put_u(c, ref); put(c, " + "); put_u(c, foff); put(c, ") = v"); put_u(c, fref); put(c, ";\n"); } } } static void emit_agg_extract(ir2c_t *c, usize ref, const spl_ir_node_t *n) { usize foff = field_off(c, n->agg_extract.tid, n->agg_extract.field_idx); if (is_aggregate(c, n->agg_extract.field_tid)) { usize fsz = type_size(c, n->agg_extract.field_tid); bind_agg(c, ref); put(c, "memcpy(v"); put_u(c, ref); put(c, ", (u8*)v"); put_u(c, n->agg_extract.val); put(c, " + "); put_u(c, foff); put(c, ", "); put_u(c, fsz); put(c, ");\n"); } else { put(c, "v"); put_u(c, ref); put(c, " = *("); put_type(c, n->agg_extract.field_tid); put(c, "*)((u8*)v"); put_u(c, n->agg_extract.val); put(c, " + "); put_u(c, foff); put(c, ");\n"); } } static void emit_agg_insert(ir2c_t *c, usize ref, const spl_ir_node_t *n) { bind_agg(c, ref); spl_type_id_t agg_t = n->agg_insert.tid; usize sz = type_size(c, agg_t); put(c, "memcpy(v"); put_u(c, ref); put(c, ", v"); put_u(c, n->agg_insert.agg); put(c, ", "); put_u(c, sz); put(c, ");\n"); spl_type_id_t ftid = field_tid(c, agg_t, n->agg_insert.field_idx); usize foff = field_off(c, agg_t, n->agg_insert.field_idx); if (!ftid) return; if (is_aggregate(c, ftid)) { usize fsz = type_size(c, ftid); put(c, "memcpy((u8*)v"); put_u(c, ref); put(c, " + "); put_u(c, foff); put(c, ", v"); put_u(c, n->agg_insert.field); put(c, ", "); put_u(c, fsz); put(c, ");\n"); } else { put(c, "*("); put_type(c, ftid); put(c, "*)((u8*)v"); put_u(c, ref); put(c, " + "); put_u(c, foff); put(c, ") = v"); put_u(c, n->agg_insert.field); put(c, ";\n"); } } static void emit_arith(ir2c_t *c, usize ref, const spl_ir_node_t *n) { put(c, "v"); put_u(c, ref); put(c, " = "); switch (n->kind) { case SPL_IR_ARITH_ADD: put(c, "(v"); put_u(c, n->arith.left); put(c, " + v"); put_u(c, n->arith.right); put(c, ")"); break; case SPL_IR_ARITH_SUB: put(c, "(v"); put_u(c, n->arith.left); put(c, " - v"); put_u(c, n->arith.right); put(c, ")"); break; case SPL_IR_ARITH_MUL: put(c, "(v"); put_u(c, n->arith.left); put(c, " * v"); put_u(c, n->arith.right); put(c, ")"); break; case SPL_IR_ARITH_DIV: put(c, "(v"); put_u(c, n->arith.left); put(c, " / v"); put_u(c, n->arith.right); put(c, ")"); break; case SPL_IR_ARITH_REM: put(c, "(v"); put_u(c, n->arith.left); put(c, " % v"); put_u(c, n->arith.right); put(c, ")"); break; case SPL_IR_ARITH_AND: put(c, "(v"); put_u(c, n->arith.left); put(c, " & v"); put_u(c, n->arith.right); put(c, ")"); break; case SPL_IR_ARITH_OR: put(c, "(v"); put_u(c, n->arith.left); put(c, " | v"); put_u(c, n->arith.right); put(c, ")"); break; case SPL_IR_ARITH_XOR: put(c, "(v"); put_u(c, n->arith.left); put(c, " ^ v"); put_u(c, n->arith.right); put(c, ")"); break; case SPL_IR_ARITH_SHL: put(c, "(v"); put_u(c, n->arith.left); put(c, " << v"); put_u(c, n->arith.right); put(c, ")"); break; case SPL_IR_ARITH_SHR: put(c, "(v"); put_u(c, n->arith.left); put(c, " >> v"); put_u(c, n->arith.right); put(c, ")"); break; case SPL_IR_ARITH_NEG: put(c, "(-v"); put_u(c, n->arith.left); put(c, ")"); break; case SPL_IR_ARITH_NOT: put(c, tid_is_bool(c, n->arith.tid) ? "(!v" : "(~v"); put_u(c, n->arith.left); put(c, ")"); break; case SPL_IR_ARITH_ABS: put(c, "(v"); put_u(c, n->arith.left); put(c, " < 0 ? -v"); put_u(c, n->arith.left); put(c, " : v"); put_u(c, n->arith.left); put(c, ")"); break; default: put(c, "0"); break; } put(c, ";\n"); } static void emit_cmp(ir2c_t *c, usize ref, const spl_ir_node_t *n) { put(c, "v"); put_u(c, ref); put(c, " = (v"); put_u(c, n->cmp.a); switch (n->kind) { case SPL_IR_CMP_EQ: put(c, " == v"); break; case SPL_IR_CMP_NE: put(c, " != v"); break; case SPL_IR_CMP_LT: put(c, " < v"); break; case SPL_IR_CMP_LE: put(c, " <= v"); break; case SPL_IR_CMP_GT: put(c, " > v"); break; case SPL_IR_CMP_GE: put(c, " >= v"); break; default: put(c, " == v"); break; } put_u(c, n->cmp.b); put(c, ");\n"); } static void emit_cast(ir2c_t *c, usize ref, const spl_ir_node_t *n) { put(c, "v"); put_u(c, ref); put(c, " = "); bool from_is_ptr = false; bool from_is_bool = false; bool to_is_ptr = false; spl_type_node_t *te = spl_type_node(c->type, n->cast.from_tid); if (te) { from_is_ptr = te->kind == SPL_TYPE_PTR; from_is_bool = te->kind == SPL_TYPE_BOOL; } spl_type_node_t *tte = spl_type_node(c->type, n->cast.to_tid); if (tte) to_is_ptr = tte->kind == SPL_TYPE_PTR; if (to_is_ptr) { put(c, "(void*)(usize)v"); put_u(c, n->cast.val); } else if (from_is_ptr) { put(c, "(usize)v"); put_u(c, n->cast.val); } else if (from_is_bool) { put(c, "(v"); put_u(c, n->cast.val); put(c, " ? 1 : 0)"); } else { put(c, "v"); put_u(c, n->cast.val); } 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) { switch (n->kind) { case SPL_IR_MEM_ALLOCA: put(c, "v"); put_u(c, ref); put(c, " = (void*)s"); put_u(c, ref); put(c, ";\n"); break; case SPL_IR_MEM_LOAD: if (is_aggregate(c, n->mem_load.tid)) { usize sz = type_size(c, n->mem_load.tid); bind_agg(c, ref); put(c, "memcpy(v"); put_u(c, ref); put(c, ", v"); put_u(c, n->mem_load.ptr); put(c, ", "); put_u(c, sz); put(c, ");\n"); } else { put(c, "v"); put_u(c, ref); put(c, " = *("); put_type(c, n->mem_load.tid); put(c, "*)v"); put_u(c, n->mem_load.ptr); put(c, ";\n"); } break; case SPL_IR_MEM_STORE: if (is_aggregate(c, n->mem_store.tid)) { usize sz = type_size(c, n->mem_store.tid); put(c, "memcpy(v"); put_u(c, n->mem_store.ptr); put(c, ", v"); put_u(c, n->mem_store.val); put(c, ", "); put_u(c, sz); put(c, ");\n"); } else { put(c, "*("); put_type(c, n->mem_store.tid); put(c, "*)v"); put_u(c, n->mem_store.ptr); put(c, " = v"); put_u(c, n->mem_store.val); put(c, ";\n"); } break; case SPL_IR_MEM_OFFSET: { usize size = type_size(c, n->mem_offset.tid); if (size == 0) size = 1; put(c, "v"); put_u(c, ref); put(c, " = (void*)((u8*)v"); put_u(c, n->mem_offset.ptr); put(c, " + (u64)v"); put_u(c, n->mem_offset.offset); put(c, " * "); put_u(c, size); put(c, ");\n"); break; } case SPL_IR_MEM_FIELD_PTR: { usize foff = field_off(c, n->mem_field_ptr.tid, n->mem_field_ptr.field_idx); put(c, "v"); put_u(c, ref); put(c, " = (void*)((u8*)v"); put_u(c, n->mem_field_ptr.agg); put(c, " + "); put_u(c, foff); put(c, ");\n"); 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) { usize v = 0; switch (n->kind) { case SPL_IR_TYPE_SIZEOF: v = type_size(c, n->ir_sizeof.tid); break; case SPL_IR_TYPE_BITSIZEOF: { const spl_layout_info_t *info = info_of(c, n->bitsizeof.tid); if (info) v = info->size_bits; break; } case SPL_IR_TYPE_ALIGNOF: v = type_align(c, n->ir_alignof.tid); break; case SPL_IR_TYPE_OFFSETOF: { /* field_idx 是一个常量节点 ref */ usize idx = const_node(c, f, n->ir_offsetof.field_idx); v = field_off(c, n->ir_offsetof.tid, idx); break; } case SPL_IR_TYPE_FIELD_COUNT: { spl_type_node_t *te = spl_type_node(c->type, n->field_count.tid); if (te) { switch (te->kind) { case SPL_TYPE_STRUCT: case SPL_TYPE_UNION: case SPL_TYPE_ENUM: case SPL_TYPE_SHAPE: v = vec_size(te->agg_type.variants); break; case SPL_TYPE_ARRAY: v = te->array_type.len; break; default: break; } } break; } default: return; } put(c, "v"); put_u(c, ref); put(c, " = "); put_u(c, v); put(c, ";\n"); } 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); bool ret_agg = is_aggregate(c, n->control_call.tid); if (callee != 0) { if (ret_agg) { bind_agg(c, ref); } else if (!tid_is_unit(c, n->control_call.tid)) { put(c, "v"); put_u(c, ref); put(c, " = "); } c_fn_name(c, callee); put(c, "("); if (ret_agg) { put(c, "v"); put_u(c, ref); } for (usize k = 0; k < vec_size(n->control_call.params); ++k) { if (k > 0 || ret_agg) put(c, ", "); put(c, "v"); put_u(c, vec_at(n->control_call.params, k)); } put(c, ");\n"); return; } /* 间接调用:从 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_node_t *ftnode = spl_type_node(c->type, func_tid); if (!ftnode || ftnode->kind != SPL_TYPE_FN) { put_unsupported(c, "indirect call type"); return; } if (ret_agg) { bind_agg(c, ref); } else if (!tid_is_unit(c, n->control_call.tid)) { put(c, "v"); put_u(c, ref); put(c, " = "); } put(c, "(("); if (ret_agg || tid_is_unit(c, n->control_call.tid)) put(c, "void"); else if (!put_type(c, n->control_call.tid)) put(c, "void"); put(c, " (*)("); bool need_comma = false; if (ret_agg) { put(c, "void*"); need_comma = true; } usize np = vec_size(ftnode->fn_type.params); for (usize kp = 0; kp < np; ++kp) { if (need_comma) put(c, ", "); spl_type_id_t pt = vec_at(ftnode->fn_type.params, kp); if (is_aggregate(c, pt)) put(c, "void*"); else if (!put_type(c, pt)) put(c, "void*"); need_comma = true; } if (!need_comma) put(c, "void"); put(c, "))v"); put_u(c, n->control_call.func); put(c, ")("); if (ret_agg) { put(c, "v"); put_u(c, ref); } for (usize k = 0; k < vec_size(n->control_call.params); ++k) { if (k > 0 || ret_agg) put(c, ", "); put(c, "v"); put_u(c, vec_at(n->control_call.params, k)); } put(c, ");\n"); } static void emit_control(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_CONTROL_PARAM: { usize idx = const_node(c, f, n->control_param.idx); if (is_aggregate(c, n->control_param.tid)) { put(c, "v"); put_u(c, ref); put(c, " = a"); put_u(c, idx); put(c, ";\n"); } else { put(c, "v"); put_u(c, ref); put(c, " = ("); put_type(c, n->control_param.tid); put(c, ")a"); put_u(c, idx); put(c, ";\n"); } break; } case SPL_IR_CONTROL_RET: if (is_aggregate(c, ret_tid)) { if (n->control_ret.val) { usize sz = type_size(c, ret_tid); put(c, "memcpy(ret__, v"); put_u(c, n->control_ret.val); put(c, ", "); put_u(c, sz); put(c, ");\n"); } put(c, "return;\n"); } else if (tid_is_unit(c, ret_tid)) { put(c, "return;\n"); } else { put(c, "return v"); put_u(c, n->control_ret.val); put(c, ";\n"); } break; case SPL_IR_CONTROL_BR: put(c, "if (v"); put_u(c, n->control_br.cond); put(c, ") goto L"); put_u(c, n->control_br.true_label); put(c, ";\ngoto L"); put_u(c, n->control_br.false_label); put(c, ";\n"); break; case SPL_IR_CONTROL_JMP: put(c, "goto L"); put_u(c, n->control_jmp.label); put(c, ";\n"); break; case SPL_IR_CONTROL_CALL: emit_call(c, f, ref, n); break; default: break; } } 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: case SPL_IR_ARITH_DIV: case SPL_IR_ARITH_REM: 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_NEG: case SPL_IR_ARITH_NOT: case SPL_IR_ARITH_ABS: emit_arith(c, ref, n); 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: emit_cmp(c, ref, n); 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: emit_cast(c, ref, n); break; case SPL_IR_MEM_ALLOCA: case SPL_IR_MEM_LOAD: 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; case SPL_IR_AGG_CONSTRUCT: emit_agg_construct(c, ref, n); break; case SPL_IR_AGG_EXTRACT: emit_agg_extract(c, ref, n); break; case SPL_IR_AGG_INSERT: emit_agg_insert(c, ref, n); break; case SPL_IR_TYPE_SIZEOF: case SPL_IR_TYPE_BITSIZEOF: case SPL_IR_TYPE_ALIGNOF: case SPL_IR_TYPE_OFFSETOF: case SPL_IR_TYPE_FIELD_COUNT: emit_typeop(c, f, ref, n); break; case SPL_IR_CONTROL_PARAM: case SPL_IR_CONTROL_RET: case SPL_IR_CONTROL_BR: case SPL_IR_CONTROL_JMP: case SPL_IR_CONTROL_CALL: emit_control(c, f, ref, n, ret_tid); break; case SPL_IR_CONTROL_SELECT: put(c, "v"); put_u(c, ref); put(c, " = v"); put_u(c, n->control_select.cond); put(c, " ? v"); put_u(c, n->control_select.true_val); put(c, " : v"); put_u(c, n->control_select.false_val); put(c, ";\n"); break; case SPL_IR_CONTROL_TRAP: case SPL_IR_CONTROL_UNREACHABLE: put(c, "spl_trap();\n"); break; case SPL_IR_DBG_BREAKPOINT: break; case SPL_IR_DBG_PANIC: { put(c, "spl_panic("); put(c, "\""); const char *msg = n->dbg_panic.msg; usize len = n->dbg_panic.len; for (usize i = 0; i < len; ++i) { unsigned char b = (unsigned char)msg[i]; if (b == '"') put(c, "\\\""); else if (b == '\\') put(c, "\\\\"); else if (b == '\n') put(c, "\\n"); else if (b == '\r') put(c, "\\r"); else if (b == '\t') put(c, "\\t"); else if (b >= 32 && b < 127) put_byte(c, (char)b); else { char esc[8]; int n = snprintf(esc, sizeof esc, "\\x%02x", b); if (n > 0) buf_append(c->out, esc, (usize)n); } } put(c, "\");\n"); break; } default: put_unsupported(c, "node kind"); break; } } static void declare_all(ir2c_t *c, const spl_ir_func_t *f) { 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); if (count == 0) count = 1; spl_type_id_t pointee = n->mem_alloc.tid; if (is_aggregate(c, pointee)) { usize esz = type_size(c, pointee); usize words = (esz * count + 7) / 8; if (words == 0) words = 1; put(c, "u64 s"); put_u(c, i); put(c, "["); put_u(c, words); put(c, "]; void* v"); put_u(c, i); put(c, ";\n"); } else { if (!put_type(c, pointee)) put(c, "void*"); put(c, " s"); put_u(c, i); put(c, "["); put_u(c, count); put(c, "]; void* v"); put_u(c, i); put(c, ";\n"); } continue; } spl_type_id_t tid = result_tid(c, n); if (tid != 0 && !tid_is_unit(c, tid)) declare_ref(c, i, tid); } } static void emit_fn_sig(ir2c_t *c, spl_ir_func_ref_t fid, bool with_names) { const spl_ir_func_t *f = spl_ir_func((spl_ir_t *)c->ir, fid); spl_type_id_t ret_tid = fn_ret_type(c, f->fn_tid); bool ret_agg = is_aggregate(c, ret_tid); if (ret_agg) put(c, "void"); else if (!put_type(c, ret_tid)) put(c, "void"); put(c, " "); c_fn_name(c, fid); put(c, "("); bool need = false; if (ret_agg) { put(c, "void* ret__"); need = true; } spl_type_node_t *te = spl_type_node(c->type, f->fn_tid); if (te && te->kind == SPL_TYPE_FN) { usize nargs = vec_size(te->fn_type.params); for (usize k = 0; k < nargs; ++k) { if (need) put(c, ", "); spl_type_id_t pt = vec_at(te->fn_type.params, k); if (is_aggregate(c, pt)) put(c, "void*"); else if (!put_type(c, pt)) put(c, "void*"); if (with_names) { put(c, " a"); put_u(c, k); } need = true; } } if (!need) put(c, "void"); put(c, ")"); } static void emit_proto(ir2c_t *c, spl_ir_func_ref_t fid) { emit_fn_sig(c, fid, false); put(c, ";\n"); } static void emit_def(ir2c_t *c, spl_ir_func_ref_t fid) { const spl_ir_func_t *f = spl_ir_func((spl_ir_t *)c->ir, fid); spl_type_id_t ret_tid = fn_ret_type(c, f->fn_tid); emit_fn_sig(c, fid, true); put(c, " {\n"); declare_all(c, f); for (usize i = 1; i < vec_size(f->nodes); ++i) { put(c, "L"); put_u(c, i); put(c, ": ;\n"); emit_node(c, f, i, &vec_at(f->nodes, i), ret_tid); } put(c, "spl_trap();\n}\n"); } static spl_ir_func_ref_t find_main(ir2c_t *c) { for (spl_ir_func_ref_t i = 1; i < vec_size(c->ir->funcs); ++i) { const spl_ir_func_t *f = spl_ir_func((spl_ir_t *)c->ir, i); if (f && !fn_is_extern(c, f) && f->name && strcmp(f->name, "main") == 0) return i; } return 0; } static void run(ir2c_t *c) { put(c, "// AUTO-GENERATED\n"); put(c, "#include \"spl_rt.h\"\n"); 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)) emit_proto(c, i); } 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)) emit_def(c, i); } spl_ir_func_ref_t m = find_main(c); if (m != 0) { const spl_ir_func_t *f = spl_ir_func((spl_ir_t *)c->ir, m); usize nargs = 0; spl_type_node_t *te = spl_type_node(c->type, f->fn_tid); 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"); } else { put(c, "int main(int argc, char** argv) { "); if (!tid_is_unit(c, fn_ret_type(c, f->fn_tid))) { put(c, "return (int)f"); put_u(c, m); put(c, "(argc, (void*)argv); }\n"); } else { put(c, "f"); put_u(c, m); put(c, "(argc, (void*)argv); return 0; }\n"); } } } } bool spl_ir2c_run(const spl_ir_t *ir, const spl_type_t *type, spl_layout_t *layout, const spl_attr_t *attr, spl_ir2c_buf_t *out) { ir2c_t c = { .ir = ir, .type = type, .layout = layout, .attr = attr, .out = out, .gdata_n = 1, .failed = false, }; run(&c); return !c.failed; }