1) WIN32_FIND_DATAA 等空类(FieldCount=0)字段访问:模块限定路径补 FC==0 容忍(返回 None 交 pending,对齐裸名路径);ClassGenDef 空类不再 pending 延迟(多遍死锁),改为中和旧 entry 走完整注册重建。2) 用户要求移除 [ATTR](FuncGenBase/FuncGenDef)与 [DET](CallGenCtor)无条件调试打印。3) 拓宽 [DBG-REG]/[DBG-ADDF] 过滤器到 WIN32_FIND_DATAA 定位空类注册来源。
400 lines
19 KiB
Plaintext
400 lines
19 KiB
Plaintext
import t, c
|
||
from stdint import *
|
||
import ast
|
||
import llvmlite
|
||
import memhub
|
||
import string
|
||
import viperlib
|
||
import stdio
|
||
import stdlib
|
||
import lib.core.VLogger as VLogger
|
||
import lib.core.Gen.Translator as HT
|
||
import lib.core.Gen.Call.CallGenBase as CallGenBase
|
||
|
||
# ============================================================
|
||
# FuncGenBase - FuncGenMixin 拆分(阶段 D):函数属性 / 命名 / mangle / create_function 基础工具
|
||
#
|
||
# 继承 CallGenBase(1 级,TPV self 调用仅支持 1 级父类)。
|
||
# 跨 Mixin 调用统一走 HandlesExprCall 委托(经 _g_gen)。
|
||
# @t.NoVTable:静态绑定。
|
||
# ============================================================
|
||
@t.NoVTable
|
||
class FuncGenBase(CallGenBase.CallGenBase):
|
||
"""函数属性 / 命名 / mangle / create_function 基础工具"""
|
||
|
||
# ============================================================
|
||
# extract_func_attrs - 从 decorator_list 提取 c.Attribute 属性
|
||
#
|
||
# 支持 @c.Attribute(t.attr.xxx()) 和 @c.Attribute(t.attr.xxx) 形式
|
||
# 也支持 @c.Attribute(t.attr.llvm.xxx) 形式
|
||
#
|
||
# 返回 LLVM IR 属性字符串(如 "alwaysinline nounwind"),无属性返回 None
|
||
# ============================================================
|
||
def extract_func_attrs(self, pool: memhub.MemBuddy | t.CPtr,
|
||
decorator_list: list[ast.AST | t.CPtr] | t.CPtr,
|
||
imported_modules: str) -> str:
|
||
"""从 decorator_list 提取 c.Attribute 属性,返回 LLVM IR 属性字符串"""
|
||
if decorator_list is None:
|
||
return None
|
||
dn: t.CSizeT = len(decorator_list)
|
||
if dn == 0:
|
||
return None
|
||
|
||
attrs_buf: str = pool.alloc(256)
|
||
if attrs_buf is None:
|
||
return None
|
||
attrs_buf[0] = '\0'
|
||
found_any: int = 0
|
||
|
||
for di in range(dn):
|
||
deco: ast.AST | t.CPtr = decorator_list.get(di)
|
||
if deco is None:
|
||
continue
|
||
dk: int = deco.kind()
|
||
if dk != ast.ASTKind.Call:
|
||
continue
|
||
call_node: ast.Call | t.CPtr = (ast.Call | t.CPtr)(deco)
|
||
if call_node.func is None:
|
||
continue
|
||
cfk: int = call_node.func.kind()
|
||
if cfk != ast.ASTKind.Attribute:
|
||
continue
|
||
|
||
# 检测 c.Attribute
|
||
func_attr: ast.Attribute | t.CPtr = (ast.Attribute | t.CPtr)(call_node.func)
|
||
if func_attr.attr is None:
|
||
continue
|
||
if string.strcmp(func_attr.attr, "Attribute") != 0:
|
||
continue
|
||
if func_attr.value is None:
|
||
continue
|
||
fvk: int = func_attr.value.kind()
|
||
if fvk != ast.ASTKind.Name:
|
||
continue
|
||
c_name: ast.Name | t.CPtr = (ast.Name | t.CPtr)(func_attr.value)
|
||
if c_name.id is None:
|
||
continue
|
||
if string.strcmp(c_name.id, "c") != 0:
|
||
continue
|
||
imp_c: int = self.Gen.ImportsGen.is_module_imported(imported_modules, "c")
|
||
if imp_c == 0:
|
||
continue
|
||
|
||
# 遍历参数提取属性
|
||
if call_node.args is None:
|
||
continue
|
||
args_list: list[ast.AST | t.CPtr] | t.CPtr = call_node.args
|
||
an: t.CSizeT = len(args_list)
|
||
for ai in range(an):
|
||
arg_node: ast.AST | t.CPtr = args_list.get(ai)
|
||
if arg_node is None:
|
||
continue
|
||
# 提取属性名(t.attr.xxx / t.attr.xxx() / t.attr.llvm.xxx)
|
||
attr_name: str = self._get_attr_name_from_node(arg_node)
|
||
if attr_name is None:
|
||
continue
|
||
# 映射到 LLVM 属性名并追加
|
||
ar: int = self._append_llvm_attr(attrs_buf, attr_name)
|
||
if ar != 0:
|
||
found_any = 1
|
||
|
||
if found_any == 0:
|
||
return None
|
||
return attrs_buf
|
||
def _get_attr_name_from_node(self, node: ast.AST | t.CPtr) -> str:
|
||
"""从 AST 节点提取属性名
|
||
|
||
支持:
|
||
- Call(func=Attribute(...)): t.attr.always_inline() -> "always_inline"
|
||
- Attribute: t.attr.packed -> "packed"
|
||
- Attribute(t.attr.llvm.xxx): t.attr.llvm.nounwind -> "nounwind"
|
||
"""
|
||
if node is None:
|
||
return None
|
||
k: int = node.kind()
|
||
|
||
# Call 节点: t.attr.always_inline()
|
||
if k == ast.ASTKind.Call:
|
||
call: ast.Call | t.CPtr = (ast.Call | t.CPtr)(node)
|
||
if call.func is None or call.func.kind() != ast.ASTKind.Attribute:
|
||
return None
|
||
at: ast.Attribute | t.CPtr = (ast.Attribute | t.CPtr)(call.func)
|
||
return at.attr
|
||
|
||
# Attribute 节点: t.attr.packed 或 t.attr.llvm.nounwind
|
||
if k == ast.ASTKind.Attribute:
|
||
at2: ast.Attribute | t.CPtr = (ast.Attribute | t.CPtr)(node)
|
||
return at2.attr
|
||
|
||
return None
|
||
def _append_llvm_attr(self, attrs_buf: str, attr_name: str) -> int:
|
||
"""将属性名映射到 LLVM 属性名并追加到缓冲区
|
||
|
||
返回 1 表示已追加,0 表示不支持该属性
|
||
"""
|
||
if attrs_buf is None or attr_name is None:
|
||
return 0
|
||
|
||
# 先确定映射的 LLVM 属性名
|
||
llvm_name: str = None
|
||
if string.strcmp(attr_name, "always_inline") == 0:
|
||
llvm_name = "alwaysinline"
|
||
elif string.strcmp(attr_name, "noinline") == 0:
|
||
llvm_name = "noinline"
|
||
elif string.strcmp(attr_name, "noreturn") == 0:
|
||
llvm_name = "noreturn"
|
||
elif string.strcmp(attr_name, "pure") == 0:
|
||
llvm_name = "readonly"
|
||
elif string.strcmp(attr_name, "const") == 0:
|
||
llvm_name = "readnone"
|
||
elif string.strcmp(attr_name, "nounwind") == 0:
|
||
llvm_name = "nounwind"
|
||
elif string.strcmp(attr_name, "noredzone") == 0:
|
||
llvm_name = "noredzone"
|
||
elif string.strcmp(attr_name, "stackrealign") == 0:
|
||
# 动态重对齐栈(LLVM 序言 and rsp,-16)。入口函数(_start)由 bootloader
|
||
# jmp 进入(rsp≡0 mod 16),而 llc 按 call 约定假设入口 rsp≡8 → 帧整体
|
||
# 偏 8 → 所有调用 rsp≡8 → 变参函数保存 xmm 的 movaps #GP。stackrealign
|
||
# 使序言动态对齐,与入口约定无关(sched started 后 movaps #GP 根因)。
|
||
# 注意:stackrealign 是 LLVM 字符串属性,必须带引号("stackrealign"),
|
||
# 与 noinline/nounwind 等枚举属性(无引号)区分,否则 llc 报
|
||
# "expected '{' in function body"。
|
||
llvm_name = '"stackrealign"'
|
||
elif string.strcmp(attr_name, "willreturn") == 0:
|
||
llvm_name = "willreturn"
|
||
elif string.strcmp(attr_name, "mustprogress") == 0:
|
||
llvm_name = "mustprogress"
|
||
else:
|
||
# packed/aligned/section/visibility/weak 等不支持作为函数内联属性
|
||
return 0
|
||
|
||
# 支持的属性:如果缓冲区非空,先追加空格分隔
|
||
if attrs_buf[0] != '\0':
|
||
self._append_str(attrs_buf, " ", 256)
|
||
self._append_str(attrs_buf, llvm_name, 256)
|
||
return 1
|
||
def _append_str(self, buf: str, s: str,
|
||
max_len: t.CSizeT = 256) -> None:
|
||
"""将字符串追加到 buf 末尾(有界,防 strcpy 越界写穿定长缓冲)"""
|
||
if buf is None or s is None:
|
||
return
|
||
# 防御:TPV 方法默认参数注册存在缺陷(缺参调用 max_len 落到 0),
|
||
# 显式兜底为 256,避免有界写入检查误判丢弃全部属性文本。
|
||
if max_len == 0:
|
||
max_len = 256
|
||
cur_len: t.CSizeT = string.strlen(buf)
|
||
s_len: t.CSizeT = string.strlen(s)
|
||
if cur_len + s_len + 1 > max_len:
|
||
return # 无剩余容量:丢弃本次追加,避免越界写
|
||
string.strcpy(buf + cur_len, s)
|
||
# ============================================================
|
||
# _mangle_name - 为名称添加 SHA1 前缀
|
||
#
|
||
# 返回 "sha1.name" 格式的混淆名。若 ModuleSha1 为 None 或名称已带
|
||
# SHA1 前缀(16 hex + '.'),则原样返回。
|
||
# ============================================================
|
||
def _mangle_name(self, trans: HT.Translator | t.CPtr, name: str) -> str:
|
||
"""为任意名称添加 SHA1 前缀(不做 main/export 检查)"""
|
||
if trans is None or name is None:
|
||
return name
|
||
if trans.ModuleSha1 is None:
|
||
return name
|
||
name_len: t.CSizeT = string.strlen(name)
|
||
# 已带 SHA1 前缀(16 hex + '.')则跳过
|
||
if name_len > 17:
|
||
is_sha1: int = 1
|
||
for i in range(16):
|
||
c: t.CChar = name[i]
|
||
if not (('0' <= c <= '9') or ('a' <= c <= 'f')):
|
||
is_sha1 = 0
|
||
break
|
||
if is_sha1 != 0 and name[16] == '.':
|
||
return name
|
||
sha1: str = trans.ModuleSha1
|
||
sha1_len: t.CSizeT = string.strlen(sha1)
|
||
mangled: str = stdlib.malloc(sha1_len + name_len + 2)
|
||
if mangled is None:
|
||
return name
|
||
string.strcpy(mangled, sha1)
|
||
mangled[sha1_len] = '.'
|
||
string.strcpy(mangled + sha1_len + 1, name)
|
||
return mangled
|
||
# ============================================================
|
||
# _mangle_name_with_sha1 — 用指定 SHA1 为名称添加前缀
|
||
#
|
||
# 用于跨模块 vtable 继承: 继承方法需要用父模块的 SHA1 做 mangling,
|
||
# 而非当前模块的 SHA1。
|
||
# ============================================================
|
||
def _mangle_name_with_sha1(self, sha1: str, name: str) -> str:
|
||
"""用指定 SHA1 为名称添加前缀(不做 main/export 检查)"""
|
||
if sha1 is None or name is None:
|
||
return name
|
||
name_len: t.CSizeT = string.strlen(name)
|
||
# 已带 SHA1 前缀(16 hex + '.')则跳过
|
||
if name_len > 17:
|
||
is_sha1: int = 1
|
||
for i in range(16):
|
||
c2: t.CChar = name[i]
|
||
if not (('0' <= c2 <= '9') or ('a' <= c2 <= 'f')):
|
||
is_sha1 = 0
|
||
break
|
||
if is_sha1 != 0 and name[16] == '.':
|
||
return name
|
||
sha1_len: t.CSizeT = string.strlen(sha1)
|
||
mangled: str = stdlib.malloc(sha1_len + name_len + 2)
|
||
if mangled is None:
|
||
return name
|
||
string.strcpy(mangled, sha1)
|
||
mangled[sha1_len] = '.'
|
||
string.strcpy(mangled + sha1_len + 1, name)
|
||
return mangled
|
||
# ============================================================
|
||
# _mangle_func_name - 为函数名添加 SHA1 前缀(含 main/export 检查)
|
||
#
|
||
# 规则:
|
||
# - main 函数不加前缀(程序入口点)
|
||
# - has_export 非 0 时不加前缀(t.CExport 导出函数)
|
||
# - 已带 SHA1 前缀的不再加
|
||
# - ModuleSha1 为 None 时不加前缀
|
||
# ============================================================
|
||
def _mangle_func_name(self, trans: HT.Translator | t.CPtr, name: str,
|
||
has_export: int) -> str:
|
||
"""为函数名添加 SHA1 前缀(检查 main/export)"""
|
||
if trans is None or name is None:
|
||
return name
|
||
if has_export != 0:
|
||
return name
|
||
if string.strcmp(name, "main") == 0:
|
||
return name
|
||
return self._mangle_name(trans, name)
|
||
# ============================================================
|
||
# _body_has_raise - 函数体是否含 raise(非 StopIteration)
|
||
#
|
||
# 对齐 TPC HandlesFunctions.py L216-243 _body_contains_raise:函数签名
|
||
# 追加 __eh_msg_out__/__eh_code_out__ 输出参数的条件。v1 只扫函数体
|
||
# 顶层 raise(嵌套 if/for/try 内的 raise 未覆盖,后续扩展)。
|
||
# ============================================================
|
||
def _body_has_raise(self, fd: ast.FunctionDef | t.CPtr) -> int:
|
||
"""函数体顶层是否含 raise(非 StopIteration),1=是 / 0=否"""
|
||
if fd is None or fd.children is None:
|
||
return 0
|
||
body_f: list[ast.AST | t.CPtr] | t.CPtr = fd.children
|
||
bn_f: t.CSizeT = len(body_f)
|
||
for bi_f in range(bn_f):
|
||
stmt_f: ast.AST | t.CPtr = body_f.get(bi_f)
|
||
if stmt_f is None:
|
||
continue
|
||
if stmt_f.kind() != ast.ASTKind.Raise:
|
||
continue
|
||
rn_f: ast.Raise | t.CPtr = (ast.Raise | t.CPtr)(stmt_f)
|
||
if rn_f is None or rn_f.exc is None:
|
||
continue
|
||
ek_f: int = rn_f.exc.kind()
|
||
is_stop_f: int = 0
|
||
if ek_f == ast.ASTKind.Name:
|
||
enm_f: ast.Name | t.CPtr = (ast.Name | t.CPtr)(rn_f.exc)
|
||
if enm_f.id is not None and string.strcmp(enm_f.id, "StopIteration") == 0:
|
||
is_stop_f = 1
|
||
elif ek_f == ast.ASTKind.Call:
|
||
ecl_f: ast.Call | t.CPtr = (ast.Call | t.CPtr)(rn_f.exc)
|
||
if ecl_f.func is not None and ecl_f.func.kind() == ast.ASTKind.Name:
|
||
ecn_f: ast.Name | t.CPtr = (ast.Name | t.CPtr)(ecl_f.func)
|
||
if ecn_f.id is not None and string.strcmp(ecn_f.id, "StopIteration") == 0:
|
||
is_stop_f = 1
|
||
if is_stop_f == 0:
|
||
return 1
|
||
return 0
|
||
# ============================================================
|
||
# _resolve_creturn_ret_ty - @c.CReturn(ty1, ty2, ...) → 多返回 struct 类型
|
||
#
|
||
# 对齐 TPC DeclarationGenerator:CReturn 声明函数返回多个值(如
|
||
# GetMemoryStats @c.CReturn(t.CUInt64T, t.CUInt64T, t.CUInt64T) →
|
||
# {i64, i64, i64})。pre_scan 前向声明与 translate_function_def 定义
|
||
# 两阶段必须一致,否则 declare i32 + define {i64,i64,i64} → llc
|
||
# "ret doesn't match function result type"。
|
||
# ============================================================
|
||
def _resolve_creturn_ret_ty(self, pool: memhub.MemBuddy | t.CPtr,
|
||
fd: ast.FunctionDef | t.CPtr,
|
||
imported_modules: str,
|
||
from_imports: str,
|
||
trans: HT.Translator | t.CPtr) -> llvmlite.LLVMType | t.CPtr:
|
||
"""解析 @c.CReturn 装饰器为 struct 返回类型;无 CReturn 返回 None"""
|
||
if fd is None or fd.decorator_list is None:
|
||
return None
|
||
cr_head: llvmlite.ParamNode | t.CPtr = None
|
||
cr_last: llvmlite.ParamNode | t.CPtr = None
|
||
cr_count: int = 0
|
||
cr_dn: t.CSizeT = len(fd.decorator_list)
|
||
for cdi in range(cr_dn):
|
||
deco_cr: ast.AST | t.CPtr = fd.decorator_list.get(cdi)
|
||
if deco_cr is None or deco_cr.kind() != ast.ASTKind.Call:
|
||
continue
|
||
deco_call_cr: ast.Call | t.CPtr = (ast.Call | t.CPtr)(deco_cr)
|
||
if deco_call_cr.func is None or deco_call_cr.func.kind() != ast.ASTKind.Attribute:
|
||
continue
|
||
deco_fa_cr: ast.Attribute | t.CPtr = (ast.Attribute | t.CPtr)(deco_call_cr.func)
|
||
if deco_fa_cr.attr is None or string.strcmp(deco_fa_cr.attr, "CReturn") != 0:
|
||
continue
|
||
cr_args: list[ast.AST | t.CPtr] | t.CPtr = deco_call_cr.args
|
||
if cr_args is None:
|
||
continue
|
||
cr_an: t.CSizeT = len(cr_args)
|
||
for cai in range(cr_an):
|
||
cr_aty: llvmlite.LLVMType | t.CPtr = self.Gen.TypeGenResolve.resolve_annotation_type(
|
||
pool, cr_args.get(cai), imported_modules, from_imports, trans, 0)
|
||
if cr_aty is None:
|
||
continue
|
||
cr_pn: llvmlite.ParamNode | t.CPtr = llvmlite.new_param_node(pool, cr_aty)
|
||
if cr_pn is not None:
|
||
if cr_head is None:
|
||
cr_head = cr_pn
|
||
else:
|
||
cr_last.Next = cr_pn
|
||
cr_last = cr_pn
|
||
cr_count += 1
|
||
if cr_head is not None and cr_count > 0:
|
||
return llvmlite.Struct(pool, cr_head, cr_count, None)
|
||
return None
|
||
# ============================================================
|
||
# 创建 LLVM 函数(简单版本,仅声明)
|
||
# ============================================================
|
||
def create_function(self, pool: memhub.MemBuddy | t.CPtr,
|
||
mod: llvmlite.LLVMModule | t.CPtr,
|
||
name: str,
|
||
args_node: ast.AST | t.CPtr,
|
||
ret_ty: llvmlite.LLVMType | t.CPtr) -> llvmlite.Function | t.CPtr:
|
||
"""创建 LLVM 函数并添加参数"""
|
||
if name is None or mod is None:
|
||
return None
|
||
|
||
func: llvmlite.Function | t.CPtr = llvmlite.create_function(pool, mod, name, ret_ty)
|
||
if func is None:
|
||
return None
|
||
|
||
if args_node is not None:
|
||
ags: ast.Arguments | t.CPtr = (ast.Arguments | t.CPtr)(args_node)
|
||
if ags.args is not None:
|
||
alist: list[ast.AST | t.CPtr] | t.CPtr = ags.args
|
||
an: t.CSizeT = len(alist)
|
||
i32_ty: llvmlite.LLVMType | t.CPtr = llvmlite.Int32(pool)
|
||
for ai in range(an):
|
||
arg: ast.Arg | t.CPtr = (ast.Arg | t.CPtr)(alist.get(ai))
|
||
if arg is not None and arg.arg is not None:
|
||
# t.CVoid 表示空参:跳过
|
||
if arg.annotation is not None:
|
||
if arg.annotation.kind() == ast.ASTKind.Attribute:
|
||
at_cf: ast.Attribute | t.CPtr = (ast.Attribute | t.CPtr)(arg.annotation)
|
||
if at_cf.attr is not None and string.strcmp(at_cf.attr, "CVoid") == 0:
|
||
continue
|
||
param_ty: llvmlite.LLVMType | t.CPtr = i32_ty
|
||
if arg.annotation is not None:
|
||
resolved: llvmlite.LLVMType | t.CPtr = self.Gen.TypeGenResolve.resolve_annotation_type(
|
||
pool, arg.annotation, None, None, None, 0)
|
||
if resolved is not None:
|
||
param_ty = resolved
|
||
pname: str = pool.alloc(32)
|
||
if pname is not None:
|
||
viperlib.snprintf(pname, 32, "%%%s", arg.arg)
|
||
llvmlite.add_param(pool, func, param_ty, pname)
|
||
|
||
return func |