修正了 TPC 的一些错误,包括 Test 维护后 TPV 无法重新编译或重编译后越界的部分问题

This commit is contained in:
2026-07-20 11:12:30 +08:00
parent ab73420b4f
commit a277ded8d4
476 changed files with 4000 additions and 3439 deletions

View File

@@ -271,20 +271,10 @@ class _str:
return has_char
def split(self, delimiter: str) -> list[str]:
stdio.printf("DBG split: entry\n")
stdio.fflush(None)
result = list[str](self.__mbuddy__)
stdio.printf("DBG split: list created\n")
stdio.fflush(None)
src: str = self.__data__
stdio.printf("DBG split: src=%s\n", src)
stdio.fflush(None)
length: t.CSizeT = len(src)
stdio.printf("DBG split: length=%lu\n", length)
stdio.fflush(None)
delim_len: t.CSizeT = string.strlen(delimiter)
stdio.printf("DBG split: delim_len=%lu\n", delim_len)
stdio.fflush(None)
start: t.CSizeT = 0
i: t.CSizeT = 0
cur: str = src

View File

@@ -1,7 +1,6 @@
import t, c
import memhub
import string
import stdio
from stdint import *
@@ -25,11 +24,7 @@ class list[T]:
__iter_index__: t.CSizeT
def __new__(self, pool: memhub.MemManager | t.CPtr, elem_size: t.CSizeT = 0):
stdio.printf("DBG __new__: entry\n")
stdio.fflush(None)
ret = pool.alloc(48)
stdio.printf("DBG __new__: alloc returned %p\n", ret)
stdio.fflush(None)
return ret
def __init__(self, pool: memhub.MemManager | t.CPtr, elem_size: t.CSizeT = 0):

View File

@@ -245,17 +245,18 @@ def param_set_attrs(param: Param | t.CPtr, attrs: t.CChar | t.CPtr):
# ============================================================
# _ll_name_needs_quote - 检查 LLVM 标识符是否需要加引号
#
# LLVM IR 规范:标识符以数字开头或含特殊字符(如 '.')时必须
# 用双引号包围。SHA1 命名空间的函数名格式为 "sha1hex.funcname"
# (含 '.'),以 hex 字符开头(可能以数字开头),因此需要加引号
# 普通函数名(如 printf/malloc/main不含特殊字符不加引号。
# LLVM IR 规范:标识符合法字符集为 [-a-zA-Z$._][-a-zA-Z$._0-9]*
# 以数字开头或含非合法字符(如 '['、']'、' '、'@' 等)时必须用双引号包围。
# '.' 是合法字符,不需要加引号(如 @.str、@llvm.memcpy 都不加引号)
# SHA1 命名空间的函数名(如 "4dad2ba72ed7ba09.GSListNode[Param]")含 '['、']'
# 需要加引号;但纯 hex+点 的名字(如 "abc.def")不需要。
# ============================================================
def _ll_name_needs_quote(name: str) -> t.CInt:
"""检查 LLVM 标识符是否需要加引号(以数字开头或含 '.'
"""检查 LLVM 标识符是否需要加引号(以数字开头或含非合法字符
例外: LLVM 内联函数名(如 llvm.memcpy/llvm.memset'llvm.' 开头,
虽含 '.',但不能加引号——加引号后 LLVM 解析器不识别为内联函数,
导致 declare/call 签名不匹配Intrinsic called with incompatible signature
LLVM IR 合法字符a-z, A-Z, 0-9, -, $, ., _
首字符是数字时需要加引号LLVM 要求局部/全局标识符首字符非数字,除非加引号)。
含其他字符(如 '['']'、空格)时需要加引号
"""
if name is None:
return 0
@@ -263,13 +264,13 @@ def _ll_name_needs_quote(name: str) -> t.CInt:
if c0 >= '0' and c0 <= '9':
return 1
name_len: t.CSizeT = string.strlen(name)
# 豁免 llvm.* 前缀的内联函数名
if name_len >= 5:
if name[0] == 'l' and name[1] == 'l' and name[2] == 'v' and name[3] == 'm' and name[4] == '.':
return 0
i: t.CSizeT = 0
while i < name_len:
if name[i] == '.':
ch: t.CChar = name[i]
# 合法字符a-z, A-Z, 0-9, -, $, ., _
if not ((ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z') or
(ch >= '0' and ch <= '9') or ch == '-' or ch == '$' or
ch == '.' or ch == '_'):
return 1
i += 1
return 0
@@ -329,8 +330,8 @@ def FunctionPrint(buf: t.CChar | t.CPtr, size: t.CSizeT,
ty_buf[0] = '\0'
TypePrint(ty_buf, 128, cur.Ty, pool)
_append_cstr(buf, size, ty_buf)
# declare 声明不打印参数名
if func.IsDeclared == 0 and cur.Name is not None:
# declare 和 define 都打印参数名(如果有)
if cur.Name is not None:
_append_cstr(buf, size, " ")
# LLVM IR 要求参数名以 % 开头
if cur.Name[0] != '%':

View File

@@ -255,6 +255,10 @@ class MemSlab(MemManager):
# 线程安全 (自旋锁), 支持合并
# ============================================================
class MemBuddy(MemManager):
# 覆盖父类 __provides__MemBuddy 同时提供 __mbuddy__ 和 __memmgr__
# 使 with MemBuddy(...) 上下文内的 __requires__=['__mbuddy__'] 类(如 _str
# 能通过 _find_provider 自动注入
__provides__: list[str] = ['__mbuddy__', '__memmgr__']
max_order: t.CInt # 最大阶数
free_lists: t.CUInt64T | t.CPtr # 空闲链头数组 (位于 arena 开头)
lock_val: t.CVolatile | t.CInt # 自旋锁
@@ -414,28 +418,17 @@ class MemBuddy(MemManager):
# === 虚函数覆盖 ===
def alloc(self, size: t.CSizeT) -> t.CVoid | t.CPtr:
stdio.printf("DBG MemBuddy.alloc: size=%lu\n", size)
stdio.fflush(None)
self._lock()
result: t.CVoid | t.CPtr = None
if self.usable is not None:
stdio.printf("DBG MemBuddy.alloc: usable=%p\n", self.usable)
stdio.fflush(None)
if size != 0:
needed: t.CSizeT = size + MEMBUDDY_HEADER_SIZE
order: t.CInt = self._order_for_size(needed)
stdio.printf("DBG MemBuddy.alloc: order=%d max_order=%d\n", order, self.max_order)
stdio.fflush(None)
if order <= self.max_order:
block: t.CVoid | t.CPtr = self._split_to_order(order)
stdio.printf("DBG MemBuddy.alloc: block=%p\n", block)
stdio.fflush(None)
if block is not None:
c.DerefAs(block, t.CVoid(t.CUInt64T((order << 1) | 1), t.CPtr))
result = t.CVoid(t.CUInt64T(block) + MEMBUDDY_HEADER_SIZE, t.CPtr)
else:
stdio.printf("DBG MemBuddy.alloc: usable is None!\n")
stdio.fflush(None)
self._unlock()
return result