fix(compiler): 修复模块级全局初始化数据丢失(-1 与字符数组全 0)

This commit is contained in:
TermiNexus committed 2026-08-30 07:27:40 +08:00
1 parent 23e7f5b1fb
commit 4db68ff5c4
1 file changed
+29 -2
+29 -2
View File
@@ -342,7 +342,10 @@ class TranslatorGenMixin:
ek: int = elem_node.kind()
if ek == ast.ASTKind.Constant:
ec: ast.Constant | t.CPtr = (ast.Constant | t.CPtr)(elem_node)
if ec is not None and ec.const_kind == ast.CONST_INT:
# CONST_CHAR 字符字面量(如 `_ps2_set1 = ['\x1b', '1', ...]`):
# int_val 即字符码,此前只认 CONST_INT → 字符元素被跳过全编成
# 0(128 字节 PS/2 扫描码表全 0 → 键盘输入失效根因)。
if ec is not None and (ec.const_kind == ast.CONST_INT or ec.const_kind == ast.CONST_CHAR):
elem_val = ec.int_val
elif ek == ast.ASTKind.UnaryOp:
# 负数: UnaryOp(USub, Constant)
@@ -553,8 +556,21 @@ class TranslatorGenMixin:
resolved_stg_ty = resolved
resolved_stg_entry = gv_entry
resolved = llvmlite.Ptr(pool, resolved)
elif resolved.kind() == llvmlite.LLVMType.Ptr:
# 对象类联合注解(T | t.CPtr → T*,如 Gargantua `_v3_pool:
# memhub.MemPool | t.CPtr`、`CAM: V3 | t.CPtr`):同样创建 storage。
# 否则 @g = global T* null → 入口/方法虚调用 load @g 得 NULL →
# 读 [NULL] 拿 vtable → #PF cr2=0x0(Gargantua/Terminal 启动即崩根因)。
# var_ty 保持 Ptr(联合注解语义:指针全局),storage 承载对象实例。
pointee_uv: llvmlite.LLVMType | t.CPtr = resolved.Pointee
if pointee_uv is not None and pointee_uv.kind() == llvmlite.LLVMType.Struct:
gv_entry_u: GenTypes.StructEntry | t.CPtr = self.Gen.StructGen.find_struct_by_type(pointee_uv)
if gv_entry_u is not None \
and (gv_entry_u.HasNew != 0 or gv_entry_u.HasVTable != 0 or gv_entry_u.IsOOP != 0):
resolved_stg_ty = pointee_uv
resolved_stg_entry = gv_entry_u
var_ty = resolved
# 解析初始值(支持整数、列表字面量、字符串字面量)
# 解析初始值(支持整数、负数字面量、列表字面量、字符串字面量)
if aa.value is not None:
val_kind: int = aa.value.kind()
if val_kind == ast.ASTKind.Constant:
@@ -565,6 +581,13 @@ class TranslatorGenMixin:
elif cn.const_kind == ast.CONST_STR:
init_str_val = cn.str_val
init_kind = 3
elif val_kind == ast.ASTKind.UnaryOp:
# 负数/按位取反字面量(如 `_vblank_ok: t.CInt = -1`):
# AST 是 UnaryOp(USub, Constant),此前未处理 → init_kind 保持 0
# → 全局编译成 zeroinitializer(desktop 7 个 -1 初始化全部丢失
# → _drag_idx=0 无条件拖拽窗口 0 / _focused_win=0 事件错路由根因)。
init_val = self.Gen.AssignGen.extract_cdefine_int_value(aa.value)
init_kind = 1
elif val_kind == ast.ASTKind.List:
init_list_node = (ast.List | t.CPtr)(aa.value)
init_kind = 2
@@ -590,6 +613,10 @@ class TranslatorGenMixin:
elif cn2.const_kind == ast.CONST_STR:
init_str_val = cn2.str_val
init_kind = 3
elif val_kind2 == ast.ASTKind.UnaryOp:
# 负数字面量(同 AnnAssign 分支,见上):UnaryOp(USub, Constant)
init_val = self.Gen.AssignGen.extract_cdefine_int_value(asgn.value)
init_kind = 1
elif val_kind2 == ast.ASTKind.List:
init_list_node = (ast.List | t.CPtr)(asgn.value)
init_kind = 2