327 lines
15 KiB
Plaintext
327 lines
15 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.Handles.HandlesTranslator as HT
|
||
import lib.core.Handles.HandlesExpr as HandlesExpr
|
||
import lib.core.Handles.HandlesExprCall as HandlesExprCall
|
||
import lib.core.Gen.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 or deco.kind() != ast.ASTKind.Call:
|
||
continue
|
||
call_node: ast.Call | t.CPtr = (ast.Call | t.CPtr)(deco)
|
||
if call_node.func is None or call_node.func.kind() != 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 or string.strcmp(func_attr.attr, "Attribute") != 0:
|
||
continue
|
||
if func_attr.value is None or func_attr.value.kind() != ast.ASTKind.Name:
|
||
continue
|
||
c_name: ast.Name | t.CPtr = (ast.Name | t.CPtr)(func_attr.value)
|
||
if c_name.id is None or string.strcmp(c_name.id, "c") != 0:
|
||
continue
|
||
if self.Gen.ImportsGen.is_module_imported(imported_modules, "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 属性名并追加
|
||
if self._append_llvm_attr(attrs_buf, attr_name) != 0:
|
||
found_any = 1
|
||
# 追加成功后,如果不是最后一个属性,添加空格分隔
|
||
# 在下一次追加前由 _append_str 处理
|
||
|
||
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, "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, " ")
|
||
self._append_str(attrs_buf, llvm_name)
|
||
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
|
||
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
|
||
# ============================================================
|
||
# 创建 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)
|
||
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 |