修复了 TPV 的一些错误,包括闭包等

This commit is contained in:
2026-07-22 14:05:38 +08:00
parent 6eb3d22eba
commit 92a381f003
78 changed files with 24965 additions and 200 deletions

View File

@@ -44,56 +44,32 @@ SRC_BUF_SIZE: t.CDefine = 1048576
def RunPhase1(mb: memhub.MemBuddy | t.CPtr, includes_dir: str, temp_dir: str,
log: VLogger.Logger | t.CPtr) -> int:
"""Phase1: 扫描 includes 目录,按需翻译并生成 stub"""
stdio.printf("[P1 ENTER] mb=%p inc=%p temp=%p log=%p\n", mb, includes_dir, temp_dir, log)
stdio.fflush(0)
if includes_dir is None or temp_dir is None:
stdio.printf("[Phase1] includes_dir 或 temp_dir 为空,跳过\n")
return 1
stdio.printf("[P1] before log.banner\n")
stdio.fflush(0)
if log is not None:
log.banner("Phase1: 扫描 includes按需翻译")
stdio.printf("[P1] after log.banner\n")
stdio.fflush(0)
# 扫描 includes 目录
stdio.printf("[P1] before scan_includes: mb=%p inc=%s\n", mb, includes_dir)
stdio.fflush(0)
result: IncludesScanner.ScanResult | t.CPtr = IncludesScanner.scan_includes(mb, includes_dir)
stdio.printf("[P1] after scan_includes: result=%p\n", result)
stdio.fflush(0)
if result is None:
stdio.printf("[Phase1] 扫描失败\n")
return 1
stdio.printf("[P1] result.Count=%d\n", result.Count)
stdio.fflush(0)
# 读取 _sha1_map.txt 获取需要的 includes SHA1 集合
# 优先使用 Projectrans.py 生成的 _sha1_map.txt含依赖分析只包含需要的 includes
# 若不存在,则从扫描结果生成(包含所有 includes可能导致结构体表溢出
sha1_set: str = stdlib.malloc(StubMerger.MAX_INCLUDES_SHA1 * 17)
stdio.printf("[P1] sha1_set=%p\n", sha1_set)
stdio.fflush(0)
if sha1_set is None:
stdio.printf("[Phase1] sha1_set 分配失败\n")
return 1
string.memset(sha1_set, 0, StubMerger.MAX_INCLUDES_SHA1 * 17)
stdio.printf("[P1] before _load_includes_sha1_set\n")
stdio.fflush(0)
set_count: int = StubMerger._load_includes_sha1_set(mb, temp_dir, sha1_set)
stdio.printf("[P1] set_count=%d\n", set_count)
stdio.fflush(0)
if set_count <= 0:
stdio.printf("[Phase1] _sha1_map.txt 不存在或为空,从扫描结果生成\n")
stdio.fflush(0)
stdio.printf("[P1] before WriteIncludesSha1Map\n")
stdio.fflush(0)
StubMerger.WriteIncludesSha1Map(mb, temp_dir, result, None, 0)
stdio.printf("[P1] after WriteIncludesSha1Map, before _load_includes_sha1_set\n")
stdio.fflush(0)
set_count = StubMerger._load_includes_sha1_set(mb, temp_dir, sha1_set)
stdio.printf("[P1] set_count2=%d\n", set_count)
stdio.fflush(0)
if set_count < 0:
stdio.printf("[Phase1] 无法加载 _sha1_map.txt跳过 Phase1\n")
return 1
@@ -105,17 +81,9 @@ def RunPhase1(mb: memhub.MemBuddy | t.CPtr, includes_dir: str, temp_dir: str,
p1_sha1_arr: bytes = stdlib.malloc(StubMerger.MAX_INCLUDES * 17)
p1_mod_arr: bytes = stdlib.malloc(StubMerger.MAX_INCLUDES * 64)
p1_inc_count: int = 0
stdio.printf("[P1] before _BuildIncludesSha1Map\n")
stdio.fflush(0)
if p1_sha1_arr is not None and p1_mod_arr is not None:
p1_inc_count = StubMerger._BuildIncludesSha1Map(temp_dir, td_len_p1map, p1_sha1_arr, p1_mod_arr)
stdio.printf("[P1] after _BuildIncludesSha1Map: inc=%d\n", p1_inc_count)
stdio.fflush(0)
stdio.printf("[P1] before set_module_sha1_map\n")
stdio.fflush(0)
HandlesExprCall.set_module_sha1_map(p1_sha1_arr, p1_mod_arr, p1_inc_count)
stdio.printf("[P1] after set_module_sha1_map\n")
stdio.fflush(0)
# 初始化 AST 表(只需一次)
ast._init_tables(mb)
@@ -276,13 +244,21 @@ def RunPhase1(mb: memhub.MemBuddy | t.CPtr, includes_dir: str, temp_dir: str,
p1a_fl: int = 0
for i in range(result.Count):
stdio.printf("[Phase1a] iter=%d/%d\n", i, result.Count)
stdio.fflush(0)
entry_addr_r: t.CUInt64T = t.CUInt64T(result.Entries) + i * entry_size
stdio.printf("[Phase1a] iter=%d entry_addr=%d\n", i, entry_addr_r)
stdio.fflush(0)
entry_r: IncludesScanner.FileEntry | t.CPtr = (IncludesScanner.FileEntry | t.CPtr)(t.CVoid(entry_addr_r, t.CPtr))
stdio.printf("[Phase1a] iter=%d entry_r=%d\n", i, entry_r)
stdio.fflush(0)
if entry_r is None:
p1a_fl += 1
continue
sha1_r: str = entry_r.Sha1
stdio.printf("[Phase1a] iter=%d sha1=%s\n", i, sha1_r)
stdio.fflush(0)
if sha1_r is None:
p1a_fl += 1
continue
@@ -299,6 +275,8 @@ def RunPhase1(mb: memhub.MemBuddy | t.CPtr, includes_dir: str, temp_dir: str,
# 读取文件内容
file_path_r: str = entry_r.Path
stdio.printf("[Phase1a] 处理: sha1=%s path=%s\n", sha1_r, file_path_r)
stdio.fflush(0)
f_r: fileio.File | t.CPtr = fileio.File(file_path_r, fileio.MODE.R)
if f_r.closed:
p1a_fl += 1
@@ -347,21 +325,67 @@ def RunPhase1(mb: memhub.MemBuddy | t.CPtr, includes_dir: str, temp_dir: str,
HandlesType.set_current_file(file_path_r)
HandlesType.clear_cdefine_constants()
HandlesStruct.reset_visible_structs(mb, 0)
stdio.printf("[Phase1a] 翻译开始: sha1=%s\n", sha1_r)
stdio.fflush(0)
ret_r: int = tr_r.translate(tree_r)
stdio.printf("[Phase1a] 翻译完成: sha1=%s ret=%d\n", sha1_r, ret_r)
stdio.fflush(0)
if ret_r != 0:
p1a_fl += 1
else:
p1a_reg += 1
# 释放 Translator 的 C malloc 资源
stdio.printf("[Phase1a] 释放开始: sha1=%s\n", sha1_r)
stdio.fflush(0)
if tr_r._global_names is not None:
stdio.printf("[Phase1a] free(_global_names) 前: sha1=%s\n", sha1_r)
stdio.fflush(0)
stdlib.free(tr_r._global_names)
stdio.printf("[Phase1a] free(_global_names) 后: sha1=%s\n", sha1_r)
stdio.fflush(0)
if tr_r._nonlocal_names is not None:
stdio.printf("[Phase1a] free(_nonlocal_names) 前: sha1=%s\n", sha1_r)
stdio.fflush(0)
stdlib.free(tr_r._nonlocal_names)
stdio.printf("[Phase1a] free(_nonlocal_names) 后: sha1=%s\n", sha1_r)
stdio.fflush(0)
stdio.printf("[Phase1a] free(src_buf_r) 前: sha1=%s\n", sha1_r)
stdio.fflush(0)
stdlib.free(src_buf_r)
stdio.printf("[Phase1a] free(src_buf_r) 后: sha1=%s\n", sha1_r)
stdio.fflush(0)
stdio.printf("[Phase1a] 循环已退出\n")
stdio.fflush(0)
test_val: int = 42
stdio.printf("[Phase1a] test_val=%d p1a_reg=%d p1a_skp=%d p1a_fl=%d\n", test_val, p1a_reg, p1a_skp, p1a_fl)
stdio.fflush(0)
stdio.printf("[Phase1a] done simple\n")
stdio.fflush(0)
stdio.printf("[Phase1a] done ascii reg=%d skp=%d fl=%d\n", p1a_reg, p1a_skp, p1a_fl)
stdio.fflush(0)
test_ptr: bytes = stdlib.malloc(64)
if test_ptr is not None:
stdio.printf("[Phase1a] malloc test ok ptr=%d\n", test_ptr)
stdio.fflush(0)
stdlib.free(test_ptr)
stdio.printf("[Phase1a] free test ok\n")
stdio.fflush(0)
stdio.printf("[Phase1a] 测试中文=%d\n", 42)
stdio.fflush(0)
stdio.printf("[Phase1a] 完成: 注册=%d 跳过=%d 失败=%d\n", 15, 78, 0)
stdio.fflush(0)
stdio.printf("[Phase1a] before_var_check ok\n")
stdio.fflush(0)
stdio.printf("[Phase1a] 完成: 注册=%d\n", p1a_reg)
stdio.fflush(0)
stdio.printf("[Phase1a] 完成: 注册=%d 跳过=%d\n", p1a_reg, p1a_skp)
stdio.fflush(0)
stdio.printf("[Phase1a] 完成: 注册=%d 跳过=%d 失败=%d\n", p1a_reg, p1a_skp, p1a_fl)
stdio.fflush(0)
stdio.printf("[Phase1a] ALL_DIAG_OK\n")
stdio.fflush(0)
# ============================================================
# Phase 1b: 全量翻译struct 已注册,走 existing 路径翻译方法体)
@@ -371,40 +395,70 @@ def RunPhase1(mb: memhub.MemBuddy | t.CPtr, includes_dir: str, temp_dir: str,
skipped: int = 0
failed: int = 0
stdio.printf("[Phase1b] 开始: Count=%d entry_size=%d\n", result.Count, entry_size)
stdio.fflush(0)
for i in range(result.Count):
stdio.printf("[Phase1b] iter=%d/%d\n", i, result.Count)
stdio.fflush(0)
# 获取 entry
entry_addr: t.CUInt64T = t.CUInt64T(result.Entries) + i * entry_size
stdio.printf("[Phase1b] iter=%d entry_addr=%d\n", i, entry_addr)
stdio.fflush(0)
entry: IncludesScanner.FileEntry | t.CPtr = (IncludesScanner.FileEntry | t.CPtr)(t.CVoid(entry_addr, t.CPtr))
stdio.printf("[Phase1b] iter=%d entry=%d\n", i, entry)
stdio.fflush(0)
if entry is None:
failed += 1
continue
sha1: str = entry.Sha1
stdio.printf("[Phase1b] iter=%d sha1=%s\n", i, sha1)
stdio.fflush(0)
if sha1 is None:
failed += 1
continue
# 检查 SHA1 是否在翻译集合中(按需翻译:只翻译可达的文件)
in_set: int = 0
if use_reachable != 0:
if StubMerger._is_in_sha1_set(sha1, reachable_set, reachable_count) == 0:
skipped += 1
continue
in_set = StubMerger._is_in_sha1_set(sha1, reachable_set, reachable_count)
else:
if StubMerger._is_in_sha1_set(sha1, sha1_set, set_count) == 0:
skipped += 1
continue
in_set = StubMerger._is_in_sha1_set(sha1, sha1_set, set_count)
stdio.printf("[Phase1b] iter=%d in_set=%d use_reachable=%d\n", i, in_set, use_reachable)
stdio.fflush(0)
if in_set == 0:
skipped += 1
continue
# 构造 stub 路径: {temp_dir}/{sha1}.stub.ll
stdio.printf("[Phase1b] iter=%d strlen 前\n", i)
stdio.fflush(0)
dir_len: t.CSizeT = string.strlen(temp_dir)
sha1_len: t.CSizeT = string.strlen(sha1)
stdio.printf("[Phase1b] iter=%d strlen 后 dir_len=%d sha1_len=%d\n", i, dir_len, sha1_len)
stdio.fflush(0)
stub_path: bytes = stdlib.malloc(dir_len + sha1_len + 16)
if stub_path is None:
failed += 1
continue
stdio.printf("[Phase1b] iter=%d snprintf 前 stub_path=%d\n", i, stub_path)
stdio.fflush(0)
viperlib.snprintf(stub_path, dir_len + sha1_len + 16, "%s/%s.stub.ll", temp_dir, sha1)
stdio.printf("[Phase1b] iter=%d snprintf 后\n", i)
stdio.fflush(0)
# 检查 stub 是否已存在(按需翻译:跳过已存在的)
stdio.printf("[Phase1b] iter=%d File() 前 stub_path=%d\n", i, stub_path)
stdio.fflush(0)
sf: fileio.File | t.CPtr = fileio.File(stub_path, fileio.MODE.R)
stdio.printf("[Phase1b] iter=%d File() 后 sf=%d\n", i, sf)
stdio.fflush(0)
if sf is None:
stdio.printf("[Phase1b] iter=%d sf is None, 翻译\n", i)
stdio.fflush(0)
else:
stdio.printf("[Phase1b] iter=%d sf.closed=%d\n", i, sf.closed)
stdio.fflush(0)
if not sf.closed:
sf.close()
# 检查 text.ll 是否也存在(虚表扫描需要 text.ll
@@ -419,31 +473,71 @@ def RunPhase1(mb: memhub.MemBuddy | t.CPtr, includes_dir: str, temp_dir: str,
continue
stdlib.free(text_path)
# text.ll 不存在,需要重新翻译
stdio.printf("[Phase1] text.ll 不存在,重新翻译: %s (sha1=%s)\n", entry.RelPath, sha1)
stdio.printf("[Phase1b] iter=%d RelPath 前\n", i)
stdio.fflush(0)
rp: str = entry.RelPath
stdio.printf("[Phase1b] iter=%d RelPath=%s\n", i, rp)
stdio.fflush(0)
stdio.printf("[Phase1] text.ll 不存在,重新翻译: %s (sha1=%s)\n", rp, sha1)
else:
# stub 不存在,需要翻译
stdio.printf("[Phase1] 翻译: %s (sha1=%s)\n", entry.RelPath, sha1)
stdio.printf("[Phase1b] iter=%d else 分支 RelPath 前\n", i)
stdio.fflush(0)
rp: str = entry.RelPath
stdio.printf("[Phase1b] iter=%d else RelPath=%s\n", i, rp)
stdio.fflush(0)
stdio.printf("[Phase1] 翻译: %s (sha1=%s)\n", rp, sha1)
stdio.fflush(0)
stdio.printf("[Phase1b] iter=%d free(stub_path) 前\n", i)
stdio.fflush(0)
stdlib.free(stub_path)
stdio.printf("[Phase1b] iter=%d free(stub_path) 后\n", i)
stdio.fflush(0)
# 读取文件内容
stdio.printf("[Phase1b] iter=%d entry.Path 前\n", i)
stdio.fflush(0)
file_path: str = entry.Path
stdio.printf("[Phase1b] iter=%d file_path=%s\n", i, file_path)
stdio.fflush(0)
stdio.printf("[Phase1b] iter=%d File(file_path) 前\n", i)
stdio.fflush(0)
f: fileio.File | t.CPtr = fileio.File(file_path, fileio.MODE.R)
stdio.printf("[Phase1b] iter=%d File(file_path) 后 f=%d\n", i, f)
stdio.fflush(0)
if f is None:
stdio.printf("[Phase1] 无法打开(None): %s\n", file_path)
stdio.fflush(0)
failed += 1
continue
stdio.printf("[Phase1b] iter=%d f.closed=%d\n", i, f.closed)
stdio.fflush(0)
if f.closed:
stdio.printf("[Phase1] 无法打开: %s\n", file_path)
stdio.fflush(0)
failed += 1
continue
stdio.printf("[Phase1b] iter=%d malloc(src_buf) 前\n", i)
stdio.fflush(0)
src_buf: bytes = stdlib.malloc(SRC_BUF_SIZE)
stdio.printf("[Phase1b] iter=%d malloc(src_buf) 后=%d\n", i, src_buf)
stdio.fflush(0)
if src_buf is None:
f.close()
failed += 1
continue
stdio.printf("[Phase1b] iter=%d read_all 前\n", i)
stdio.fflush(0)
bytes_read: LONG = f.read_all(src_buf, SRC_BUF_SIZE)
stdio.printf("[Phase1b] iter=%d read_all 后=%d\n", i, bytes_read)
stdio.fflush(0)
f.close()
if bytes_read <= 0:
stdio.printf("[Phase1] 读取失败: %s\n", file_path)
stdio.fflush(0)
stdlib.free(src_buf)
failed += 1
continue
@@ -452,33 +546,67 @@ def RunPhase1(mb: memhub.MemBuddy | t.CPtr, includes_dir: str, temp_dir: str,
else:
src_buf[SRC_BUF_SIZE - 1] = 0
stdio.printf("[Phase1b] iter=%d AST 前\n", i)
stdio.fflush(0)
# 解析 AST
stdio.printf("[Phase1b] iter=%d new_lexer 前\n", i)
stdio.fflush(0)
lx: ast.Lexer | t.CPtr = ast.new_lexer(mb)
stdio.printf("[Phase1b] iter=%d new_lexer 后=%d\n", i, lx)
stdio.fflush(0)
if lx is None:
stdlib.free(src_buf)
failed += 1
continue
stdio.printf("[Phase1b] iter=%d _lexer_init 前\n", i)
stdio.fflush(0)
ast._lexer_init(lx, src_buf, mb)
stdio.printf("[Phase1b] iter=%d _lexer_init 后\n", i)
stdio.fflush(0)
stdio.printf("[Phase1b] iter=%d tokenize 前\n", i)
stdio.fflush(0)
tokens: ast.Token | t.CPtr = ast.tokenize(lx)
stdio.printf("[Phase1b] iter=%d tokenize 后=%d\n", i, tokens)
stdio.fflush(0)
stdio.printf("[Phase1b] iter=%d parse_tokens 前\n", i)
stdio.fflush(0)
tree: ast.AST | t.CPtr = ast.parse_tokens(mb, tokens)
stdio.printf("[Phase1b] iter=%d parse_tokens 后=%d\n", i, tree)
stdio.fflush(0)
if tree is None:
stdio.printf("[Phase1] AST 解析失败: %s\n", file_path)
stdio.fflush(0)
stdlib.free(src_buf)
failed += 1
continue
# 翻译 AST → LLVM IR
stdio.printf("[Phase1b] iter=%d Translator() 前\n", i)
stdio.fflush(0)
tr: HandlesTranslator.Translator | t.CPtr = HandlesTranslator.Translator()
stdio.printf("[Phase1b] iter=%d Translator() 后=%d\n", i, tr)
stdio.fflush(0)
if tr is None:
stdlib.free(src_buf)
failed += 1
continue
stdio.printf("[Phase1b] iter=%d ModuleSha1 前\n", i)
stdio.fflush(0)
tr.ModuleSha1 = sha1
stdio.printf("[Phase1b] iter=%d CurrentPackage 前\n", i)
stdio.fflush(0)
tr.CurrentPackage = HandlesImports.compute_package_from_relpath(mb, entry.RelPath)
stdio.printf("[Phase1b] iter=%d set_current_file 前\n", i)
stdio.fflush(0)
HandlesType.set_current_file(file_path)
HandlesType.clear_cdefine_constants()
HandlesStruct.reset_visible_structs(mb, 0)
stdio.printf("[Phase1b] iter=%d translate 前\n", i)
stdio.fflush(0)
ret: int = tr.translate(tree)
stdio.printf("[Phase1b] iter=%d translate 后=%d\n", i, ret)
stdio.fflush(0)
if ret != 0:
stdio.printf("[Phase1] 翻译失败: %s\n", file_path)
stdlib.free(src_buf)