fix(TPC/includes): 泛型特化 linkonce_odr + None 全局 common + includes 前缀
- TPC HandlesFunctions/HandlesClassDef: 泛型特化类方法(类名含 '[')设 linkonce_odr,消除 GSList[NameEntry].__before_init__ 等 duplicate symbol - TPC HandlesAssign: None 初值模块级全局用 common linkage,合并 _g_gen 等 跨模块同名全局重复定义 - includes: binascii/hashlib 裸调用加 stdlib./string. 前缀(malloc/free/memset/memcpy) - llvmlite: FunctionPrint 泛型特化方法输出 define linkonce_odr;__module 调整 - 删除 wiki/01-overview.md
This commit is contained in:
1 parent
b4567ffb9a
commit
8ccf5d2c18
10 files changed
+70
-384
No files matched your search
+11
-10
@@ -1,4 +1,5 @@
|
||||
import t, c
|
||||
import t, c
|
||||
import stdlib
|
||||
|
||||
|
||||
HEX_CHARS: t.CArray[t.CChar, None] = "0123456789abcdef"
|
||||
@@ -122,7 +123,7 @@ def crc_hqx(data: str, length: t.CInt, crc: t.CUnsignedShort) -> t.CUnsignedShor
|
||||
|
||||
def hexlify(data: str, length: t.CInt) -> str:
|
||||
if not data or length < 0: return None
|
||||
out: str = str(malloc(length * 2 + 1))
|
||||
out: str = str(stdlib.malloc(length * 2 + 1))
|
||||
if not out: return None
|
||||
oi: t.CInt = 0
|
||||
for i in range(length):
|
||||
@@ -140,14 +141,14 @@ def b2a_hex(data: str, length: t.CInt) -> str:
|
||||
def a2b_hex(hexstr: str, length: t.CInt) -> str:
|
||||
if not hexstr or length < 0 or length % 2 != 0: return None
|
||||
out_len: t.CInt = length // 2
|
||||
out: str = str(malloc(out_len + 1))
|
||||
out: str = str(stdlib.malloc(out_len + 1))
|
||||
if not out: return None
|
||||
oi: t.CInt = 0
|
||||
for i in range(0, length, 2):
|
||||
hi: t.CInt = HEX_VALS[t.CUnsignedChar(hexstr[i])]
|
||||
lo: t.CInt = HEX_VALS[t.CUnsignedChar(hexstr[i + 1])]
|
||||
if hi < 0 or lo < 0:
|
||||
free(out)
|
||||
stdlib.free(out)
|
||||
return None
|
||||
out[oi] = t.CChar((hi << 4) | lo)
|
||||
oi += 1
|
||||
@@ -163,7 +164,7 @@ def b2a_base64(data: str, length: t.CInt) -> str:
|
||||
if length % 3 != 0:
|
||||
olen += 4
|
||||
olen += 1
|
||||
out: str = str(malloc(olen + 1))
|
||||
out: str = str(stdlib.malloc(olen + 1))
|
||||
if not out: return None
|
||||
oi: t.CInt = 0
|
||||
for i in range(0, length, 3):
|
||||
@@ -198,7 +199,7 @@ def a2b_base64(data: str, length: t.CInt) -> str:
|
||||
olen -= 1
|
||||
if length >= 4 and data[length - 2] == '=':
|
||||
olen -= 1
|
||||
out: str = str(malloc(olen + 1))
|
||||
out: str = str(stdlib.malloc(olen + 1))
|
||||
if not out: return None
|
||||
oi: t.CInt = 0
|
||||
for i in range(0, length, 4):
|
||||
@@ -233,7 +234,7 @@ def a2b_base64(data: str, length: t.CInt) -> str:
|
||||
|
||||
def b2a_uu(data: str, length: t.CInt) -> str:
|
||||
if not data or length < 1 or length > 45: return None
|
||||
out: str = str(malloc(length * 2 + 2))
|
||||
out: str = str(stdlib.malloc(length * 2 + 2))
|
||||
if not out: return None
|
||||
oi: t.CInt = 0
|
||||
out[oi] = UU_CHARS[length]
|
||||
@@ -264,7 +265,7 @@ def a2b_uu(data: str) -> str:
|
||||
if not data or data[0] == ' ' or data[0] == '`': return None
|
||||
length: t.CInt = t.CInt(data[0]) - 32
|
||||
if length < 0 or length > 45: return None
|
||||
out: str = str(malloc(length + 1))
|
||||
out: str = str(stdlib.malloc(length + 1))
|
||||
if not out: return None
|
||||
oi: t.CInt = 0
|
||||
i: t.CInt = 1
|
||||
@@ -294,7 +295,7 @@ def a2b_uu(data: str) -> str:
|
||||
def b2a_hqx(data: str, length: t.CInt, crc: t.CUnsignedShort = 0) -> tuple[str, t.CUnsignedShort]:
|
||||
if not data or length != 3:
|
||||
return None, crc
|
||||
out: str = str(malloc(5))
|
||||
out: str = str(stdlib.malloc(5))
|
||||
if not out:
|
||||
return None, crc
|
||||
# 3字节 → 4字节 HQX
|
||||
@@ -324,7 +325,7 @@ def a2b_hqx(data: str, crc: t.CUnsignedShort = 0) -> tuple[str, t.CUnsignedShort
|
||||
m: t.CUnsignedInt = 0
|
||||
for i in range(4):
|
||||
m = (m << 6) | t.CUnsignedInt(HQX_VALS[t.CUnsignedChar(data[i])])
|
||||
out: str = str(malloc(4))
|
||||
out: str = str(stdlib.malloc(4))
|
||||
if not out:
|
||||
return None, crc
|
||||
out[0] = t.CChar((m >> 16) & 0xFF)
|
||||
|
||||
@@ -87,7 +87,7 @@ class sha1:
|
||||
if idx > 0:
|
||||
left: t.CSizeT = SHA1_BLOCK_LEN - idx
|
||||
if length >= left:
|
||||
memcpy(self.buf + idx, data, left)
|
||||
string.memcpy(self.buf + idx, data, left)
|
||||
self.transform(self.buf)
|
||||
data += left
|
||||
length -= left
|
||||
@@ -97,7 +97,7 @@ class sha1:
|
||||
data += SHA1_BLOCK_LEN
|
||||
length -= SHA1_BLOCK_LEN
|
||||
if length > 0:
|
||||
memcpy(self.buf + idx, data, length)
|
||||
string.memcpy(self.buf + idx, data, length)
|
||||
def final(self, out: t.CArray[t.CUInt8T, SHA1_DIGEST_LEN]):
|
||||
idx: t.CSizeT = self.count % SHA1_BLOCK_LEN
|
||||
self.buf[idx] = 0x80
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import t, c
|
||||
import t, c
|
||||
import string
|
||||
|
||||
|
||||
# ==============================================
|
||||
@@ -34,7 +35,7 @@ class sha256:
|
||||
count: t.CUInt64T
|
||||
buf: t.CArray[t.CUInt8T, SHA256_BLOCK_LEN]
|
||||
def __init__(self):
|
||||
memset(self, 0, sha256.__sizeof__())
|
||||
string.memset(self, 0, sha256.__sizeof__())
|
||||
self.state[0] = 0x6a09e667
|
||||
self.state[1] = 0xbb67ae85
|
||||
self.state[2] = 0x3c6ef372
|
||||
@@ -44,7 +45,7 @@ class sha256:
|
||||
self.state[6] = 0x1f83d9ab
|
||||
self.state[7] = 0x5be0cd19
|
||||
self.count = 0
|
||||
# memset(self.buf, 0, SHA256_BLOCK_LEN)
|
||||
# string.memset(self.buf, 0, SHA256_BLOCK_LEN)
|
||||
# SHA256 single-group compression transform
|
||||
def transform(self, block: t.CUInt8T | t.CPtr):
|
||||
w: t.CArray[t.CUInt32T, 64]
|
||||
@@ -91,7 +92,7 @@ class sha256:
|
||||
if idx > 0:
|
||||
left: t.CSizeT = SHA256_BLOCK_LEN - idx
|
||||
if length >= left:
|
||||
memcpy(self.buf + idx, data, left)
|
||||
string.memcpy(self.buf + idx, data, left)
|
||||
self.transform(self.buf)
|
||||
data += left
|
||||
length -= left
|
||||
@@ -101,16 +102,16 @@ class sha256:
|
||||
data += SHA256_BLOCK_LEN
|
||||
length -= SHA256_BLOCK_LEN
|
||||
if length > 0:
|
||||
memcpy(self.buf + idx, data, length)
|
||||
string.memcpy(self.buf + idx, data, length)
|
||||
def final(self, out: t.CArray[t.CUInt8T, SHA256_DIGEST_LEN]):
|
||||
idx: t.CSizeT = self.count % SHA256_BLOCK_LEN
|
||||
self.buf[idx] = 0x80
|
||||
idx += 1
|
||||
if idx > SHA256_BLOCK_LEN - 8:
|
||||
memset(self.buf + idx, 0, SHA256_BLOCK_LEN - idx)
|
||||
string.memset(self.buf + idx, 0, SHA256_BLOCK_LEN - idx)
|
||||
self.transform(self.buf)
|
||||
idx = 0
|
||||
memset(self.buf + idx, 0, SHA256_BLOCK_LEN - 8 - idx)
|
||||
string.memset(self.buf + idx, 0, SHA256_BLOCK_LEN - 8 - idx)
|
||||
bits: t.CUInt64T = self.count << 3
|
||||
for i in range(8):
|
||||
self.buf[SHA256_BLOCK_LEN-8+i] = (bits >> (56 - i*8)) & 0xFF
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import t, c
|
||||
import t, c
|
||||
import string
|
||||
|
||||
|
||||
# ==============================================
|
||||
@@ -45,7 +46,7 @@ class sha512:
|
||||
count: t.CArray[t.CUInt64T, 2]
|
||||
buf: t.CArray[t.CUInt8T, SHA512_BLOCK_LEN]
|
||||
def __init__(self):
|
||||
memset(self, 0, sha512.__sizeof__())
|
||||
string.memset(self, 0, sha512.__sizeof__())
|
||||
self.state[0] = 0x6a09e667f3bcc908
|
||||
self.state[1] = 0xbb67ae8584caa73b
|
||||
self.state[2] = 0x3c6ef372fe94f82b
|
||||
@@ -110,7 +111,7 @@ class sha512:
|
||||
if idx > 0:
|
||||
left: t.CSizeT = SHA512_BLOCK_LEN - idx
|
||||
if length >= left:
|
||||
memcpy(self.buf + idx, data, left)
|
||||
string.memcpy(self.buf + idx, data, left)
|
||||
self.transform(self.buf)
|
||||
data += left
|
||||
length -= left
|
||||
@@ -120,7 +121,7 @@ class sha512:
|
||||
data += SHA512_BLOCK_LEN
|
||||
length -= SHA512_BLOCK_LEN
|
||||
if length > 0:
|
||||
memcpy(self.buf + idx, data, length)
|
||||
string.memcpy(self.buf + idx, data, length)
|
||||
def final(self, out: t.CArray[t.CUInt8T, SHA512_DIGEST_LEN]):
|
||||
bits_lo: t.CUInt64T = self.count[0] << 3
|
||||
bits_hi: t.CUInt64T = (self.count[1] << 3) | (self.count[0] >> 61)
|
||||
@@ -128,10 +129,10 @@ class sha512:
|
||||
self.buf[idx] = 0x80
|
||||
idx += 1
|
||||
if idx > SHA512_BLOCK_LEN - 16:
|
||||
memset(self.buf + idx, 0, SHA512_BLOCK_LEN - idx)
|
||||
string.memset(self.buf + idx, 0, SHA512_BLOCK_LEN - idx)
|
||||
self.transform(self.buf)
|
||||
idx = 0
|
||||
memset(self.buf + idx, 0, SHA512_BLOCK_LEN - 16 - idx)
|
||||
string.memset(self.buf + idx, 0, SHA512_BLOCK_LEN - 16 - idx)
|
||||
for i in range(8):
|
||||
self.buf[SHA512_BLOCK_LEN - 16 + i] = t.CUInt8T(bits_hi >> (56 - i * 8))
|
||||
for i in range(8):
|
||||
|
||||
@@ -618,7 +618,13 @@ def FunctionPrint(buf: t.CChar | t.CPtr, size: t.CSizeT,
|
||||
if func.IsDeclared == 1:
|
||||
_append_cstr(buf, size, "declare ")
|
||||
else:
|
||||
_append_cstr(buf, size, "define ")
|
||||
# 泛型特化类方法(如 GSList[NameEntry].__before_init__):多个模块各自
|
||||
# 特化生成同名定义 → ld.lld duplicate symbol warning。特化方法代码由
|
||||
# 相同模板+类型参数生成,定义相同,输出 linkonce_odr 让链接器合并。
|
||||
if func.Name is not None and string.strchr(func.Name, 91) is not None:
|
||||
_append_cstr(buf, size, "define linkonce_odr ")
|
||||
else:
|
||||
_append_cstr(buf, size, "define ")
|
||||
ty_buf[0] = '\0'
|
||||
TypePrint(ty_buf, 128, func.RetTy, pool)
|
||||
_append_cstr(buf, size, ty_buf)
|
||||
|
||||
@@ -2,6 +2,7 @@ import t, c
|
||||
from stdint import *
|
||||
import string
|
||||
import stdio
|
||||
import stdlib
|
||||
import viperlib
|
||||
import memhub
|
||||
from linkedlist import GSList, GSListNode
|
||||
@@ -114,7 +115,16 @@ class LLVMModule:
|
||||
return
|
||||
# 去重检查(opaque 声明可被完整定义替换)
|
||||
cur: NamedTypeNode | t.CPtr = self.NamedTypeHead
|
||||
cnt_dedup: t.CInt = 0
|
||||
while cur is not None:
|
||||
# 步数守卫:NamedTypeHead 链表成环(内存损坏/插入 bug)会导致
|
||||
# 无限遍历挂起(G3 编译 LinkedNode.prepend 挂起根因)。
|
||||
# 正常链表节点数远小于此上限,超限即链表损坏 → 立即报错终止。
|
||||
cnt_dedup += 1
|
||||
if cnt_dedup > 100000:
|
||||
stdio.printf("[ANT-LOOP] NamedTypeHead 链表疑似成环, name=%s, 遍历节点=%d, 强制终止\n",
|
||||
name if name is not None else "(null)", cnt_dedup)
|
||||
stdlib.exit(1)
|
||||
if cur.Ty is not None:
|
||||
existing_name: t.CChar | t.CPtr = get_struct_name(cur.Ty)
|
||||
if existing_name is not None:
|
||||
@@ -158,7 +168,14 @@ class LLVMModule:
|
||||
if self is None or name is None:
|
||||
return 0
|
||||
cur: NamedTypeNode | t.CPtr = self.NamedTypeHead
|
||||
cnt_tdm: t.CInt = 0
|
||||
while cur is not None:
|
||||
# 步数守卫:同上,防 NamedTypeHead 链表成环无限遍历
|
||||
cnt_tdm += 1
|
||||
if cnt_tdm > 100000:
|
||||
stdio.printf("[TDM-LOOP] NamedTypeHead 链表疑似成环, name=%s, 强制终止\n",
|
||||
name if name is not None else "(null)")
|
||||
stdlib.exit(1)
|
||||
if cur.Ty is not None:
|
||||
existing_name: t.CChar | t.CPtr = get_struct_name(cur.Ty)
|
||||
if existing_name is not None:
|
||||
|
||||
Reference in new issue
Block a user