stage2 修复大量AI生成的风格问题 stage1 修复对于vm abi的错误生成

This commit is contained in:
zzy committed 2026-10-03 15:05:55 +08:00
1 parent 0e38858785
commit 76f980654c
40 files changed
+619 -938

No files matched your search

+22 -50
View File
@@ -1,7 +1,6 @@
// Expect retcode = 0
// SIR builder (IrBuilder). Split from ir.spl: stage1 ast2ir
// infinitely recurses on same-file @self nesting; cross-file is fine.
// IR 构建器:单独成文件,以免 ast2ir 在同文件 @self 嵌套时无限递归。
type .{ .Error } = @import("utils/error.spl");
type .{ .Vec } = @import("utils/vec.spl");
@@ -18,8 +17,6 @@ fn list_at(v: Vec, i: usize): usize {
ret ((v.data as *usize) + i).*;
}
// ---- builder ----
type FixKind = enum { BrTrue, BrFalse, Jmp }
type Fix = struct {
@@ -46,13 +43,9 @@ fn builder_alloc(self: *IrBuilder, kind: IrKind): usize {
e.kind = kind;
e.dbg = self.dbg;
}
if self.current_block >= self.blocks.len { ret ref; }
match Vec.get(&self.blocks, self.current_block) {
.Ok(p) => {
var slot = p as *usize;
if slot.* == 0 { slot.* = ref; }
},
.Err(error) => {},
if self.current_block < self.blocks.len {
var slot = (self.blocks.data as *usize) + self.current_block;
if slot.* == 0 { slot.* = ref; }
}
ret ref;
}
@@ -113,25 +106,18 @@ fn fn_begin_at(self: *IrBuilder, fid: usize) {
fn fn_end(self: *IrBuilder) {
var k: usize = 0;
while k < self.fixups.len {
match Vec.get(&self.fixups, k) {
.Ok(fp) => {
var fx = fp as *Fix;
var e = Ir.node(&self.ir, self.current_fn, fx.node);
if e != null {
var target: usize = 0;
if fx.block < self.blocks.len { target = list_at(self.blocks, fx.block); }
if target != 0 {
if @enum2int(fx.slot) == @enum2int(FixKind.BrTrue) {
match e.body { .Br(&b) => { b.true_label = target; }, _ => {} }
} else if @enum2int(fx.slot) == @enum2int(FixKind.BrFalse) {
match e.body { .Br(&b) => { b.false_label = target; }, _ => {} }
} else {
match e.body { .Jmp(&j) => { j.label = target; }, _ => {} }
}
}
var fx = (self.fixups.data as *Fix) + k;
var e = Ir.node(&self.ir, self.current_fn, fx.node);
if e != null {
var target: usize = 0;
if fx.block < self.blocks.len { target = list_at(self.blocks, fx.block); }
if target != 0 {
match fx.slot {
.BrTrue => { match e.body { .Br(&b) => { b.true_label = target; }, _ => {} } },
.BrFalse => { match e.body { .Br(&b) => { b.false_label = target; }, _ => {} } },
.Jmp => { match e.body { .Jmp(&j) => { j.label = target; }, _ => {} } },
}
},
.Err(error) => {},
}
}
k = k + 1;
}
@@ -143,7 +129,7 @@ fn fn_end(self: *IrBuilder) {
fn block_begin(self: *IrBuilder): usize {
var zero: usize = 0;
match Vec.push(&self.blocks, &zero as *u8) { .Ok => {}, .Err(error) => {} }
Vec.push(&self.blocks, &zero as *u8);
var block = self.blocks.len - 1;
self.current_block = block;
self.terminated = false;
@@ -151,10 +137,9 @@ fn block_begin(self: *IrBuilder): usize {
}
fn block_end(self: *IrBuilder) {
// terminated 与空块校验在 stage1 为 Panic;这里保持同样约束但不终止进程。
// 空实现:本实现不做 terminated/空块校验(不终止进程)。
}
// ---- arith / cmp / cast ----
fn set_arith(self: *IrBuilder, kind: IrKind, tid: usize, left: usize, right: usize): usize {
var ref = builder_alloc(self, kind);
@@ -211,7 +196,6 @@ fn cast_bool2int(self: *IrBuilder, f: usize, t: usize, v: usize): usize { ret se
fn case_int2float(self: *IrBuilder, f: usize, t: usize, v: usize): usize { ret set_cast(self, .CaseInt2float, f, t, v); }
fn case_float2int(self: *IrBuilder, f: usize, t: usize, v: usize): usize { ret set_cast(self, .CaseFloat2int, f, t, v); }
// ---- memory ----
fn mem_alloca(self: *IrBuilder, tid: usize, count: usize): usize {
var ref = builder_alloc(self, .MemAlloca);
@@ -269,7 +253,6 @@ fn mem_set(self: *IrBuilder, tid: usize, dst: usize, val: usize, size: usize): u
ret ref;
}
// ---- type const ----
fn const_node(self: *IrBuilder, tid: usize, lit: ConstLit): usize {
var ref = builder_alloc(self, .TypeConst);
@@ -292,7 +275,6 @@ fn add_global(self: *IrBuilder, tid: usize, zero: bool, bytes: Vec): usize {
ret Ir.add_global(&self.ir, tid, zero, bytes);
}
// ---- type ops ----
fn type_bitsizeof(self: *IrBuilder, tid: usize): usize {
var ref = builder_alloc(self, .TypeBitsizeof);
@@ -329,7 +311,6 @@ fn type_field_count(self: *IrBuilder, tid: usize): usize {
ret ref;
}
// ---- agg ----
fn agg_construct(self: *IrBuilder, tid: usize): usize {
var ref = builder_alloc(self, .AggConstruct);
@@ -343,12 +324,8 @@ fn agg_construct(self: *IrBuilder, tid: usize): usize {
fn agg_construct_field(self: *IrBuilder, node_ref: usize, field: usize) {
var e = Ir.node(&self.ir, self.current_fn, node_ref);
if e == null { ret; }
// 载荷按值绑定:改副本后写回。
match e.body {
.AggConstruct(a) => {
match Vec.push(&a.fields, &field as *u8) { .Ok => {}, .Err(error) => {} }
e.body = .AggConstruct(a);
},
.AggConstruct(&a) => { Vec.push(&a.fields, &field as *u8); },
_ => {},
}
}
@@ -369,7 +346,6 @@ fn agg_insert(self: *IrBuilder, tid: usize, field_idx: usize, agg: usize, field:
ret ref;
}
// ---- control ----
fn control_select(self: *IrBuilder, tid: usize, cond: usize, tv: usize, fv: usize): usize {
var ref = builder_alloc(self, .ControlSelect);
@@ -388,9 +364,9 @@ fn control_br(self: *IrBuilder, cond: usize): usize {
fn control_br_fixup(self: *IrBuilder, ref: usize, true_block: usize, false_block: usize) {
var f1 = Fix { .node = ref, .slot = .BrTrue, .block = true_block };
match Vec.push(&self.fixups, &f1 as *u8) { .Ok => {}, .Err(error) => {} }
Vec.push(&self.fixups, &f1 as *u8);
var f2 = Fix { .node = ref, .slot = .BrFalse, .block = false_block };
match Vec.push(&self.fixups, &f2 as *u8) { .Ok => {}, .Err(error) => {} }
Vec.push(&self.fixups, &f2 as *u8);
}
fn control_jmp(self: *IrBuilder): usize {
@@ -403,7 +379,7 @@ fn control_jmp(self: *IrBuilder): usize {
fn control_jmp_fixup(self: *IrBuilder, ref: usize, target_block: usize) {
var fx = Fix { .node = ref, .slot = .Jmp, .block = target_block };
match Vec.push(&self.fixups, &fx as *u8) { .Ok => {}, .Err(error) => {} }
Vec.push(&self.fixups, &fx as *u8);
}
fn control_call(self: *IrBuilder, tid: usize, fn_ref: usize): usize {
@@ -419,12 +395,8 @@ fn control_call(self: *IrBuilder, tid: usize, fn_ref: usize): usize {
fn control_call_param(self: *IrBuilder, call_node: usize, arg: usize) {
var e = Ir.node(&self.ir, self.current_fn, call_node);
if e == null { ret; }
// 载荷按值绑定:改副本后写回。
match e.body {
.Call(c) => {
match Vec.push(&c.params, &arg as *u8) { .Ok => {}, .Err(error) => {} }
e.body = .Call(c);
},
.Call(&c) => { Vec.push(&c.params, &arg as *u8); },
_ => {},
}
}