refactor(tpv): rowptr[T] 迁移 + D 表定长缓冲改精确分配 + SHA1 前缀判定收敛(D1/D3/D4/D5/D6/D7/C10)

This commit is contained in:
TermiNexus committed 2026-09-12 11:09:35 +08:00
1 parent 7471cc64f9
commit 82eea2719f
66 files changed
+2709 -2604

No files matched your search

+38 -41
View File
@@ -1,5 +1,6 @@
import lib.core.Gen.GenTypes as GenTypes
import lib.core.Gen.Generator as Generator
import lib.core.Gen.GenApi as GenApi
import t, c
from stdint import *
import ast
@@ -129,7 +130,7 @@ class CallGenBase:
return StubMergerStore.LookupSha1ByModNameSuffix(mod_suffix)
def _module_package_from_sha1(self, pool: memhub.MemBuddy | t.CPtr,
def _module_package_from_sha1(self, pool: rowptr[memhub.MemBuddy],
mod_sha1: str) -> str:
"""从 SHA1 存储器的 rel_path 计算模块所属包名(目录部分,'/' → '.')"""
if pool is None or mod_sha1 is None:
@@ -164,7 +165,7 @@ class CallGenBase:
return None
def _lookup_reexport_from_vpi(self, pool: memhub.MemBuddy | t.CPtr,
def _lookup_reexport_from_vpi(self, pool: rowptr[memhub.MemBuddy],
mod_sha1: str, func_name: str) -> str:
"""re-export 表未命中时,从模块 .vpi 按需解析 from-import 定位定义模块
@@ -439,7 +440,7 @@ class CallGenBase:
return 0
def _is_state_func_from_vpi(self, pool: memhub.MemBuddy | t.CPtr,
def _is_state_func_from_vpi(self, pool: rowptr[memhub.MemBuddy],
sha1: str, func_name: str) -> int:
"""vpi 返回注解含 t.State → 外部 C 函数(定义端用裸名,跨模块调用无 sha1 头)
@@ -461,7 +462,7 @@ class CallGenBase:
return 0
def _lookup_mod_sha1(self, pool: memhub.MemBuddy | t.CPtr,
def _lookup_mod_sha1(self, pool: rowptr[memhub.MemBuddy],
from_imports: str, class_name: str,
allow_star_fallback: int = 1) -> str:
"""从 from_imports 查找类所属模块的 SHA1,返回 None=未找到
@@ -475,7 +476,7 @@ class CallGenBase:
return self._lookup_sha1_from_modname_raw(pool, mod_name_raw)
def _lookup_sha1_from_modname_raw(self, pool: memhub.MemBuddy | t.CPtr,
def _lookup_sha1_from_modname_raw(self, pool: rowptr[memhub.MemBuddy],
mod_name_raw: str) -> str:
"""从原始模块名查找 SHA1,返回 None=未找到"""
# 手动找到模块名结束位置(空格、null、或 ':' 别名分隔符),复制到新缓冲区
@@ -603,7 +604,7 @@ class CallGenBase:
# 当 found_func 为 None(stub 未注入或跨模块调用)时,
# 在当前模块中创建函数声明,避免 llc 报 undefined value。
# ============================================================
def _ensure_method_declare(self, pool: memhub.MemBuddy | t.CPtr,
def _ensure_method_declare(self, pool: rowptr[memhub.MemBuddy],
mod: llvmlite.LLVMModule | t.CPtr,
call_name: str,
ret_ty: llvmlite.LLVMType | t.CPtr,
@@ -700,34 +701,30 @@ class CallGenBase:
cls_d: str = None
meth_d: str = None
if call_name is not None:
cl_len_d: sizet = string.strlen(call_name)
if cl_len_d >= 17 and call_name[16] == '.':
sha1_ok_d: int = 1
si_d: sizet
for si_d in range(16):
cc_d: int = call_name[si_d]
if not ((cc_d >= 48 and cc_d <= 57) or (cc_d >= 97 and cc_d <= 102)):
sha1_ok_d = 0
break
if sha1_ok_d != 0:
sha1_buf_d: bytes = stdlib.malloc(17)
if sha1_buf_d is not None:
string.strncpy(sha1_buf_d, call_name, 16)
sha1_buf_d[16] = '\0'
sha1_d = sha1_buf_d
rest_d: str = call_name + 17
dot2_d: str = string.strchr(rest_d, '.')
if dot2_d is not None:
cls_len_d: sizet = uint64(dot2_d) - uint64(rest_d)
if cls_len_d > 0:
cls_buf_d: bytes = stdlib.malloc(cls_len_d + 1)
if cls_buf_d is not None:
string.strncpy(cls_buf_d, rest_d, cls_len_d)
cls_buf_d[cls_len_d] = '\0'
cls_d = cls_buf_d
meth_d = dot2_d + 1
else:
meth_d = rest_d
# [C10 收敛] SHA1 前缀判定走唯一真源 `GenApi.sha1_prefix_len`
# (旧实现自带一份 hex+'.' 校验,判据 `>= 17` 与真源一致);
# 前缀长度取返回值,不再硬编码 17/16。
sha1_pfx_d: int = GenApi.sha1_prefix_len(call_name)
if sha1_pfx_d != 0:
pfx_len_d: sizet = sizet(sha1_pfx_d)
sha1_buf_d: bytes = stdlib.malloc(pfx_len_d)
if sha1_buf_d is not None:
string.strncpy(sha1_buf_d, call_name, pfx_len_d - 1)
sha1_buf_d[pfx_len_d - 1] = '\0'
sha1_d = sha1_buf_d
rest_d: str = call_name + pfx_len_d
dot2_d: str = string.strchr(rest_d, '.')
if dot2_d is not None:
cls_len_d: sizet = uint64(dot2_d) - uint64(rest_d)
if cls_len_d > 0:
cls_buf_d: bytes = stdlib.malloc(cls_len_d + 1)
if cls_buf_d is not None:
string.strncpy(cls_buf_d, rest_d, cls_len_d)
cls_buf_d[cls_len_d] = '\0'
cls_d = cls_buf_d
meth_d = dot2_d + 1
else:
meth_d = rest_d
prm_annots_d: str = None
if sha1_d is not None and meth_d is not None:
prm_annots_d = self._vpi_method_param_annot(pool, sha1_d, cls_d, meth_d)
@@ -774,7 +771,7 @@ class CallGenBase:
# ============================================================
# ============================================================
def _lookup_method_ret_ty_via_vpi(self, pool: memhub.MemBuddy | t.CPtr,
def _lookup_method_ret_ty_via_vpi(self, pool: rowptr[memhub.MemBuddy],
cls_sha1: str,
cls_name: str,
method_name: str) -> llvmlite.LLVMType | t.CPtr:
@@ -803,7 +800,7 @@ class CallGenBase:
# 调用方据此补全 declare/call 缺失参数(默认参数零值兜底)。
# 未命中返回 None。
# ============================================================
def _vpi_method_param_annot(self, pool: memhub.MemBuddy | t.CPtr,
def _vpi_method_param_annot(self, pool: rowptr[memhub.MemBuddy],
cls_sha1: str,
cls_name: str,
method_name: str) -> str:
@@ -821,7 +818,7 @@ class CallGenBase:
# 与 _lookup_method_ret_ty_via_vpi 的返回注解解析同源;解析失败返回
# None,调用方以 i8* 兜底(ABI 兼容,参数槽位宽度一致)。
# ============================================================
def _vpi_resolve_param_annot_ty(self, pool: memhub.MemBuddy | t.CPtr,
def _vpi_resolve_param_annot_ty(self, pool: rowptr[memhub.MemBuddy],
seg: str) -> llvmlite.LLVMType | t.CPtr:
"""解析单个 vpi 参数注解段为 LLVMType,失败返回 None"""
if pool is None or seg is None:
@@ -843,7 +840,7 @@ class CallGenBase:
# ============================================================
# ============================================================
def _special_method_ret_ty(self, pool: memhub.MemBuddy | t.CPtr,
def _special_method_ret_ty(self, pool: rowptr[memhub.MemBuddy],
class_name: str,
method_name: str) -> llvmlite.LLVMType | t.CPtr:
"""编译器内置特殊方法的固定返回类型;非特殊返回 None"""
@@ -927,7 +924,7 @@ class CallGenBase:
# ============================================================
# ============================================================
def _ParseLLVMRetTyStr(self, pool: memhub.MemBuddy | t.CPtr,
def _ParseLLVMRetTyStr(self, pool: rowptr[memhub.MemBuddy],
buf: bytes,
start: sizet,
end: sizet) -> llvmlite.LLVMType | t.CPtr:
@@ -1066,7 +1063,7 @@ class CallGenBase:
# ============================================================
# ============================================================
def _LookupMethodRetTyFromTextLL(self, pool: memhub.MemBuddy | t.CPtr,
def _LookupMethodRetTyFromTextLL(self, pool: rowptr[memhub.MemBuddy],
class_sha1: str,
class_name: str,
method_name: str,
@@ -1206,7 +1203,7 @@ class CallGenBase:
# ============================================================
# ============================================================
def _dep_text_ll_exists(self, pool: memhub.MemBuddy | t.CPtr,
def _dep_text_ll_exists(self, pool: rowptr[memhub.MemBuddy],
sha1: str) -> int:
"""探测 {temp_dir}/{sha1[0:2]}/{sha1}.text.ll 是否存在,1=存在"""
if self.Gen.CallGenBase.TempDir is None or self.Gen.CallGenBase.TempDirLen == 0 or sha1 is None: