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

+137 -127
View File
@@ -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;
}
+353 -184
View File
@@ -1,4 +1,6 @@
#include "spl_ir.h"
#include <stdlib.h>
#include <string.h>
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;