stage2 修复全局变量未完成的问题 提供错误处理解析

This commit is contained in:
zzy committed 2026-09-25 13:46:07 +08:00
1 parent c0a86ea552
commit 0b8f649818
11 files changed
+317 -18

No files matched your search

+37 -1
View File
@@ -82,6 +82,14 @@ type ConstLit = enum {
Float: f64,
Char: u8,
Fn: usize,
Global: usize, // 全局变量下标(Ir.globals);值 = 指向该全局的 *T
}
// 模块级全局变量:编译期分配的静态存储,引用时是一个 *T 指针常量(同 LLVM GlobalVariable)。
type Global = struct {
tid: usize,
zero: bool, // true = 零初始化(BSS,不携带字节)
data: Vec, // u8 初始字节(zero=false 时使用)
}
type IrBody = enum {
@@ -128,6 +136,7 @@ type IrFunc = struct {
type Ir = @self;
funcs: Vec, // IrFunc
globals: Vec, // Global
// ---- 生命周期 ----
@@ -141,7 +150,8 @@ fn new_entry(): IrEntry {
}
fn new(): Ir {
var ir = Ir { .funcs = Vec.blank(@sizeof(IrFunc), @alignof(IrFunc)) };
var ir = Ir { .funcs = Vec.blank(@sizeof(IrFunc), @alignof(IrFunc)),
.globals = Vec.blank(@sizeof(Global), @alignof(Global)) };
var sentinel = IrFunc {
.name = ByteView.empty(),
.attrs = Vec.blank(@sizeof(usize), @alignof(usize)),
@@ -180,6 +190,25 @@ fn drop(self: *Ir) {
i = i + 1;
}
Vec.drop(&self.funcs);
var g: usize = 0;
while g < self.globals.len {
var ge = (self.globals.data as *Global) + g;
Vec.drop(&ge.data);
g = g + 1;
}
Vec.drop(&self.globals);
}
// 追加一个全局变量;返回其下标(gidx)。bytes 的所有权转移进表。
fn add_global(self: *Ir, tid: usize, zero: bool, bytes: Vec): usize {
var g = Global { .tid = tid, .zero = zero, .data = bytes };
match Vec.push(&self.globals, &g as *u8) { .Ok => {}, .Err(error) => { ret 0; } }
ret self.globals.len - 1;
}
fn global_at(self: *Ir, gidx: usize): *Global {
if gidx >= self.globals.len { ret null; }
ret (self.globals.data as *Global) + gidx;
}
fn alloc_fn(self: *Ir): usize {
@@ -394,6 +423,13 @@ fn dump_body(out: *ByteBuffer, ty: *TypeTable, e: *IrEntry): bool {
},
.Const(c) => {
if !dump_type(out, ty, c.tid) { ret false; }
match c.lit {
.Global(g) => {
if !out_str(out, " global#") { ret false; }
ret out_u(out, g);
},
_ => {},
}
var te = TypeTable.node(ty, c.tid);
var is_float = false;
var is_fn = false;