实现了 TPV 的 pyi 生成逻辑(部分),删除了直接拷贝自 CPython(TPC) 版本的死代码
This commit is contained in:
@@ -399,9 +399,6 @@ def translate_function_def(trans: HT.Translator | t.CPtr,
|
||||
if fd is None or fd.name is None:
|
||||
return 0
|
||||
|
||||
stdio.printf("[FD] enter name=%s\n", fd.name)
|
||||
stdio.fflush(0)
|
||||
|
||||
pool: memhub.MemBuddy | t.CPtr = trans.Pool
|
||||
mod: llvmlite.LLVMModule | t.CPtr = trans.Module
|
||||
imported_modules: str = trans._imported_modules
|
||||
@@ -412,8 +409,6 @@ def translate_function_def(trans: HT.Translator | t.CPtr,
|
||||
i32_ty: llvmlite.LLVMType | t.CPtr = llvmlite.Int32(pool)
|
||||
|
||||
# 推断返回类型:优先使用返回类型注解
|
||||
stdio.printf("[FD] %s 推断返回类型前\n", fd.name)
|
||||
stdio.fflush(0)
|
||||
ret_ty: llvmlite.LLVMType | t.CPtr = None
|
||||
if fd.returns is not None:
|
||||
ret_ty = HandlesType.resolve_annotation_type(
|
||||
@@ -430,9 +425,6 @@ def translate_function_def(trans: HT.Translator | t.CPtr,
|
||||
param_types_str: str = HandlesType.build_param_types_str(pool, fd.args)
|
||||
ret_ty = HandlesType.infer_return_type(
|
||||
pool, fd.children, param_types_str)
|
||||
stdio.printf("[FD] %s 推断返回类型后=%d\n", fd.name, ret_ty)
|
||||
stdio.fflush(0)
|
||||
|
||||
# 检测是否为外部声明函数(t.CExtern 或 t.State)
|
||||
# 语义:t.CExtern 忽略 body 体,仅生成 declare(由链接器解析符号)
|
||||
# t.State = t.CExtern + t.CExport(既是声明又是导出)
|
||||
@@ -449,9 +441,6 @@ def translate_function_def(trans: HT.Translator | t.CPtr,
|
||||
# t.CExtern/t.State 忽略 body 体,仅声明(无论 body 是否为 pass)
|
||||
if has_extern != 0 or has_state != 0:
|
||||
is_extern_decl = 1
|
||||
stdio.printf("[FD] %s extern=%d state=%d export=%d is_extern_decl=%d\n",
|
||||
fd.name, has_extern, has_state, has_export, is_extern_decl)
|
||||
stdio.fflush(0)
|
||||
|
||||
# SHA1 命名空间:t.CExtern/t.State/t.CExport 不加前缀,直接用裸名
|
||||
# (裸名映射到 C 标准库符号,带 sha1 前缀会导致 undefined reference)
|
||||
@@ -460,8 +449,6 @@ def translate_function_def(trans: HT.Translator | t.CPtr,
|
||||
mangled_name: str = fd.name
|
||||
else:
|
||||
mangled_name: str = _mangle_func_name(trans, fd.name, 0)
|
||||
stdio.printf("[FD] %s mangled_name=%s\n", fd.name, mangled_name)
|
||||
stdio.fflush(0)
|
||||
|
||||
# 注册 CExport 函数到全局表(供跨模块调用查表)
|
||||
# t.CExport 函数定义用裸名(@strlen),跨模块调用需查表确认用裸名而非 @{sha1}.func
|
||||
@@ -479,7 +466,6 @@ def translate_function_def(trans: HT.Translator | t.CPtr,
|
||||
func: llvmlite.Function | t.CPtr = llvmlite.create_declare(
|
||||
pool, mod, mangled_name, ret_ty)
|
||||
if func is None:
|
||||
stdio.printf("[FUNC] create_declare %s failed\n", fd.name)
|
||||
return 0
|
||||
|
||||
# 注册到函数表
|
||||
@@ -523,11 +509,7 @@ def translate_function_def(trans: HT.Translator | t.CPtr,
|
||||
args_node: ast.Arguments | t.CPtr = fd.args
|
||||
|
||||
# 检查是否已有前向声明(由 forward_declare_functions 创建)
|
||||
stdio.printf("[FD] %s find_func_in_module 前\n", fd.name)
|
||||
stdio.fflush(0)
|
||||
func: llvmlite.Function | t.CPtr = HandlesExprCall.find_func_in_module(mod, mangled_name)
|
||||
stdio.printf("[FD] %s find_func_in_module 后=%d\n", fd.name, func)
|
||||
stdio.fflush(0)
|
||||
if func is not None and llvmlite.function_is_declared(func) != 0:
|
||||
# 复用前向声明:清除 IsDeclared 标记,转为 define
|
||||
func.IsDeclared = 0
|
||||
@@ -535,15 +517,9 @@ def translate_function_def(trans: HT.Translator | t.CPtr,
|
||||
func_attrs_reuse: t.CChar | t.CPtr = extract_func_attrs(pool, fd.decorator_list, imported_modules)
|
||||
if func_attrs_reuse is not None:
|
||||
llvmlite.function_set_attrs(func, func_attrs_reuse)
|
||||
stdio.printf("[FD] %s 复用前向声明\n", fd.name)
|
||||
stdio.fflush(0)
|
||||
else:
|
||||
# 创建新的 LLVM 函数(使用 SHA1 混淆名)
|
||||
stdio.printf("[FD] %s create_function 前\n", fd.name)
|
||||
stdio.fflush(0)
|
||||
func = llvmlite.create_function(pool, mod, mangled_name, ret_ty)
|
||||
stdio.printf("[FD] %s create_function 后=%d\n", fd.name, func)
|
||||
stdio.fflush(0)
|
||||
if func is None:
|
||||
stdio.printf("[FUNC] create_function %s failed\n", fd.name)
|
||||
return 0
|
||||
@@ -581,38 +557,21 @@ def translate_function_def(trans: HT.Translator | t.CPtr,
|
||||
if pname is not None:
|
||||
viperlib.snprintf(pname, 32, "%%%s", arg.arg)
|
||||
llvmlite.add_param(pool, func, param_ty, pname)
|
||||
stdio.printf("[FD] %s 新函数创建完成\n", fd.name)
|
||||
stdio.fflush(0)
|
||||
|
||||
# 创建 entry 块
|
||||
stdio.printf("[FD] %s create_block 前\n", fd.name)
|
||||
stdio.fflush(0)
|
||||
entry_blk: llvmlite.BasicBlock | t.CPtr = llvmlite.create_block(pool, func, "entry")
|
||||
stdio.printf("[FD] %s create_block 后=%d\n", fd.name, entry_blk)
|
||||
stdio.fflush(0)
|
||||
if entry_blk is None:
|
||||
return 0
|
||||
|
||||
# 创建函数专属 builder
|
||||
stdio.printf("[FD] %s new_builder 前\n", fd.name)
|
||||
stdio.fflush(0)
|
||||
func_builder: llvmlite.IRBuilder | t.CPtr = llvmlite.new_builder(pool, func)
|
||||
stdio.printf("[FD] %s new_builder 后=%d\n", fd.name, func_builder)
|
||||
stdio.fflush(0)
|
||||
if func_builder is None:
|
||||
return 0
|
||||
llvmlite.position_at_end(func_builder, entry_blk)
|
||||
|
||||
# 进入函数作用域(嵌套符号表)
|
||||
stdio.printf("[FD] %s enter_scope 前\n", fd.name)
|
||||
stdio.fflush(0)
|
||||
HandlesVar.enter_scope(trans.SymTab, HandlesVar.SCOPE_FUNCTION)
|
||||
stdio.printf("[FD] %s enter_scope 后\n", fd.name)
|
||||
stdio.fflush(0)
|
||||
|
||||
# 为参数创建 alloca
|
||||
stdio.printf("[FD] %s 参数 alloca 前\n", fd.name)
|
||||
stdio.fflush(0)
|
||||
if args_node is not None:
|
||||
ags2: ast.Arguments | t.CPtr = (ast.Arguments | t.CPtr)(args_node)
|
||||
if ags2.args is not None:
|
||||
@@ -647,9 +606,6 @@ def translate_function_def(trans: HT.Translator | t.CPtr,
|
||||
param_val: llvmlite.Value | t.CPtr = llvmlite.SSAValue(
|
||||
pool, param_ty2, pname2)
|
||||
llvmlite.build_store(func_builder, param_val, alloca)
|
||||
stdio.printf("[FD] %s 参数 alloca 后\n", fd.name)
|
||||
stdio.fflush(0)
|
||||
|
||||
# 保存模块级作用域状态(仅非变量表相关)
|
||||
old_func: llvmlite.Function | t.CPtr = trans._cur_func
|
||||
old_builder: llvmlite.IRBuilder | t.CPtr = trans._cur_builder
|
||||
@@ -663,8 +619,6 @@ def translate_function_def(trans: HT.Translator | t.CPtr,
|
||||
HT.clear_scope_names(trans)
|
||||
|
||||
# 预扫描函数体:为局部变量提前创建 alloca
|
||||
stdio.printf("[FD] %s pre_scan_allocas 前\n", fd.name)
|
||||
stdio.fflush(0)
|
||||
body: list[ast.AST | t.CPtr] | t.CPtr = fd.children
|
||||
if body is not None:
|
||||
bn: t.CSizeT = body.__len__()
|
||||
@@ -672,23 +626,14 @@ def translate_function_def(trans: HT.Translator | t.CPtr,
|
||||
stmt: ast.AST | t.CPtr = body.get(bi)
|
||||
if stmt is not None:
|
||||
HandlesBody.pre_scan_allocas(trans, stmt)
|
||||
stdio.printf("[FD] %s pre_scan_allocas 后\n", fd.name)
|
||||
stdio.fflush(0)
|
||||
|
||||
# 翻译函数体
|
||||
stdio.printf("[FD] %s translate_stmt 前 body_len=%d\n", fd.name,
|
||||
body.__len__() if body is not None else 0)
|
||||
stdio.fflush(0)
|
||||
if body is not None:
|
||||
bn2: t.CSizeT = body.__len__()
|
||||
for bi2 in range(bn2):
|
||||
stmt2: ast.AST | t.CPtr = body.get(bi2)
|
||||
if stmt2 is not None:
|
||||
stdio.printf("[FD] %s translate_stmt bi2=%d kd=%d 前\n", fd.name, bi2, stmt2.kind())
|
||||
stdio.fflush(0)
|
||||
HandlesBody.translate_stmt(trans, stmt2)
|
||||
stdio.printf("[FD] %s translate_stmt bi2=%d 后\n", fd.name, bi2)
|
||||
stdio.fflush(0)
|
||||
|
||||
# 如果函数体最后一条语句不是 Return,添加隐式 ret
|
||||
last_is_return: int = 0
|
||||
|
||||
Reference in New Issue
Block a user