简化了一些表达,最后仍使用 fb 缓冲区的版本(新版本使用 栈f-string)
This commit is contained in:
1 parent
c542442987
commit
54462320d2
24 files changed
+175
-341
No files matched your search
@@ -12,31 +12,13 @@ import lib.core.VLogger as VLogger
|
||||
import lib.core.Handles.HandlesTranslator as HT
|
||||
import lib.core.Handles.HandlesExpr as HandlesExpr
|
||||
import lib.Stub.StubMerger.StubMergerStore as StubMergerStore
|
||||
|
||||
import lib.core.Gen.GenTypes as GenTypes
|
||||
import lib.core.Gen.Generator as Generator
|
||||
import lib.core.Gen.TypeGenCType as TypeGenCType
|
||||
import lib.core.Gen.ExprGenAttr as ExprGenAttr
|
||||
import lib.core.Gen.CallGenBase as CallGenBase
|
||||
import lib.core.Gen.CallGenFd as CallGenFd
|
||||
import lib.core.Gen.CallGenVt as CallGenVt
|
||||
|
||||
import lib.Stub.StubGen.VpiReader as VpiReader
|
||||
|
||||
# ============================================================
|
||||
# AssignGenMixin - 赋值翻译 Mixin(P6 方法化)
|
||||
#
|
||||
# 从 HandlesAssign/HandlesAnnAssign/HandlesAugAssign 迁移:
|
||||
# 模块级无状态函数(trans/pool 首参)→ 类方法;全局状态
|
||||
# (HandlesAnnAssign._g_recover_found)→ 本类字段 RecoverFound。
|
||||
#
|
||||
# 迁移原则(与 P1-P5 一致,行为零变化):
|
||||
# 1. 方法保留原函数签名(含 trans/pool 首参),仅函数体内的
|
||||
# 本模块函数调用改为 self.xxx(...)
|
||||
# 2. 外部模块调用(HandlesType.xxx/HandlesExpr.xxx 等)保留模块前缀
|
||||
# 3. 原 Handles 模块函数保留为"委托":本地模块级单例 _g_gen
|
||||
# (BuildPipeline/main 注入)cast 成 Generator 类型
|
||||
#
|
||||
# @t.NoVTable:静态绑定(方法沿 MRO 直接调用 + this 调整)。
|
||||
# ============================================================
|
||||
|
||||
@@ -46,11 +28,10 @@ class AssignGenMixin:
|
||||
Gen: Generator.Generator | t.CPtr
|
||||
"""赋值翻译 Mixin(P6 方法化)"""
|
||||
|
||||
# --- 原 HandlesAnnAssign._g_recover_found 模块级全局(迁移为实例字段) ---
|
||||
RecoverFound: int
|
||||
|
||||
# ============================================================
|
||||
# is_cdefine_annotation - 检测注解是否为 t.CDefine(模块级函数)
|
||||
# 检测注解是否为 t.CDefine(模块级函数)
|
||||
#
|
||||
# 支持两种形式:
|
||||
# 1. Attribute(Name('t'), 'CDefine')
|
||||
@@ -58,8 +39,7 @@ class AssignGenMixin:
|
||||
# ============================================================
|
||||
def is_cdefine_annotation(self, annot: ast.AST | t.CPtr) -> int:
|
||||
"""检测注解是否为 t.CDefine,返回 1 表示是,0 表示否"""
|
||||
if annot is None:
|
||||
return 0
|
||||
if annot is None: return 0
|
||||
k: int = annot.kind()
|
||||
if k == ast.ASTKind.Attribute:
|
||||
at: ast.Attribute | t.CPtr = (ast.Attribute | t.CPtr)(annot)
|
||||
@@ -68,7 +48,7 @@ class AssignGenMixin:
|
||||
return 0
|
||||
if k == ast.ASTKind.BinOp:
|
||||
bop: ast.BinOp | t.CPtr = (ast.BinOp | t.CPtr)(annot)
|
||||
if self.is_cdefine_annotation(bop.left) != 0:
|
||||
if self.is_cdefine_annotation(bop.left) != 0:
|
||||
return 1
|
||||
if self.is_cdefine_annotation(bop.right) != 0:
|
||||
return 1
|
||||
@@ -109,31 +89,19 @@ class AssignGenMixin:
|
||||
# Case 2: BinOp — 如 FOREGROUND_RED | FOREGROUND_GREEN、(1 << ZDEFLATE_WINDOW_BITS)
|
||||
if k == ast.ASTKind.BinOp:
|
||||
bop: ast.BinOp | t.CPtr = (ast.BinOp | t.CPtr)(val_node)
|
||||
left_val: t.CInt64T = self.extract_cdefine_int_value(bop.left)
|
||||
left_val: t.CInt64T = self.extract_cdefine_int_value(bop.left)
|
||||
right_val: t.CInt64T = self.extract_cdefine_int_value(bop.right)
|
||||
if bop.op == ast.OpKind.BitOr:
|
||||
return left_val | right_val
|
||||
if bop.op == ast.OpKind.BitAnd:
|
||||
return left_val & right_val
|
||||
if bop.op == ast.OpKind.BitXor:
|
||||
return left_val ^ right_val
|
||||
if bop.op == ast.OpKind.LShift:
|
||||
return left_val << right_val
|
||||
if bop.op == ast.OpKind.RShift:
|
||||
return left_val >> right_val
|
||||
if bop.op == ast.OpKind.Add:
|
||||
return left_val + right_val
|
||||
if bop.op == ast.OpKind.Sub:
|
||||
return left_val - right_val
|
||||
if bop.op == ast.OpKind.Mult:
|
||||
return left_val * right_val
|
||||
if bop.op == ast.OpKind.Div:
|
||||
# 整数常量除法:与 FloorDiv 一致(对齐 TPC ConstEvaluator L354-355)
|
||||
return left_val // right_val
|
||||
if bop.op == ast.OpKind.FloorDiv:
|
||||
return left_val // right_val
|
||||
if bop.op == ast.OpKind.Mod:
|
||||
return left_val % right_val
|
||||
if bop.op == ast.OpKind.BitOr: return left_val | right_val
|
||||
if bop.op == ast.OpKind.BitAnd: return left_val & right_val
|
||||
if bop.op == ast.OpKind.BitXor: return left_val ^ right_val
|
||||
if bop.op == ast.OpKind.LShift: return left_val << right_val
|
||||
if bop.op == ast.OpKind.RShift: return left_val >> right_val
|
||||
if bop.op == ast.OpKind.Add: return left_val + right_val
|
||||
if bop.op == ast.OpKind.Sub: return left_val - right_val
|
||||
if bop.op == ast.OpKind.Mult: return left_val * right_val
|
||||
if bop.op == ast.OpKind.Div: return left_val // right_val
|
||||
if bop.op == ast.OpKind.FloorDiv: return left_val // right_val
|
||||
if bop.op == ast.OpKind.Mod: return left_val % right_val
|
||||
# 未知 BinOp 运算符:直接报错终止(fail-fast)
|
||||
self.Gen.TypeGenErr.fatal_error(val_node, "CDefine 值提取失败:无法从表达式解析编译期常量", "TypeError")
|
||||
|
||||
@@ -141,7 +109,7 @@ class AssignGenMixin:
|
||||
if k == ast.ASTKind.Name:
|
||||
nm: ast.Name | t.CPtr = (ast.Name | t.CPtr)(val_node)
|
||||
if nm.id is not None:
|
||||
# True/False 布尔字面量 → 1/0(对齐 TPC:bool 在 CDefine 语境按整数处理)
|
||||
# True/False 布尔字面量 → 1/0(bool 在 CDefine 语境按整数处理)
|
||||
if string.strcmp(nm.id, "True") == 0:
|
||||
return 1
|
||||
if string.strcmp(nm.id, "False") == 0:
|
||||
@@ -169,12 +137,9 @@ class AssignGenMixin:
|
||||
if k == ast.ASTKind.UnaryOp:
|
||||
uop: ast.UnaryOp | t.CPtr = (ast.UnaryOp | t.CPtr)(val_node)
|
||||
operand_val: t.CInt64T = self.extract_cdefine_int_value(uop.operand)
|
||||
if uop.op == ast.OpKind.USub:
|
||||
return -operand_val
|
||||
if uop.op == ast.OpKind.UAdd:
|
||||
return operand_val
|
||||
if uop.op == ast.OpKind.Invert:
|
||||
return ~operand_val
|
||||
if uop.op == ast.OpKind.USub: return -operand_val
|
||||
if uop.op == ast.OpKind.UAdd: return operand_val
|
||||
if uop.op == ast.OpKind.Invert: return ~operand_val
|
||||
# 未知 UnaryOp 运算符:直接报错终止(fail-fast)
|
||||
self.Gen.TypeGenErr.fatal_error(val_node, "CDefine 值提取失败:无法从表达式解析编译期常量", "TypeError")
|
||||
|
||||
@@ -649,16 +614,11 @@ class AssignGenMixin:
|
||||
# 将两个操作数转换为目标浮点类型(int→float via si2fp, float→float via fpext/fptrunc)
|
||||
lhs = self.Gen.ExprGenBase.coerce_to_type(builder, lhs, target_float_ty, node)
|
||||
rhs = self.Gen.ExprGenBase.coerce_to_type(builder, rhs, target_float_ty, node)
|
||||
if op == ast.OpKind.Add:
|
||||
return llvmlite.build_fadd(builder, lhs, rhs)
|
||||
if op == ast.OpKind.Sub:
|
||||
return llvmlite.build_fsub(builder, lhs, rhs)
|
||||
if op == ast.OpKind.Mult:
|
||||
return llvmlite.build_fmul(builder, lhs, rhs)
|
||||
if op == ast.OpKind.Div:
|
||||
return llvmlite.build_fdiv(builder, lhs, rhs)
|
||||
if op == ast.OpKind.Mod:
|
||||
return llvmlite.build_frem(builder, lhs, rhs)
|
||||
if op == ast.OpKind.Add: return llvmlite.build_fadd(builder, lhs, rhs)
|
||||
if op == ast.OpKind.Sub: return llvmlite.build_fsub(builder, lhs, rhs)
|
||||
if op == ast.OpKind.Mult: return llvmlite.build_fmul(builder, lhs, rhs)
|
||||
if op == ast.OpKind.Div: return llvmlite.build_fdiv(builder, lhs, rhs)
|
||||
if op == ast.OpKind.Mod: return llvmlite.build_frem(builder, lhs, rhs)
|
||||
return None
|
||||
|
||||
# 指针算术: ptr += int / ptr -= int → getelementptr(按元素大小缩放)
|
||||
@@ -674,26 +634,16 @@ class AssignGenMixin:
|
||||
elif rhs_bits > lhs_bits and lhs_bits > 0:
|
||||
lhs = self.Gen.ExprGenBase.coerce_to_type(builder, lhs, rhs.Ty, node)
|
||||
|
||||
if op == ast.OpKind.Add:
|
||||
return llvmlite.build_add(builder, lhs, rhs)
|
||||
elif op == ast.OpKind.Sub:
|
||||
return llvmlite.build_sub(builder, lhs, rhs)
|
||||
elif op == ast.OpKind.Mult:
|
||||
return llvmlite.build_mul(builder, lhs, rhs)
|
||||
elif op == ast.OpKind.Div:
|
||||
return llvmlite.build_sdiv(builder, lhs, rhs)
|
||||
elif op == ast.OpKind.FloorDiv:
|
||||
return llvmlite.build_sdiv(builder, lhs, rhs)
|
||||
elif op == ast.OpKind.Mod:
|
||||
return llvmlite.build_srem(builder, lhs, rhs)
|
||||
elif op == ast.OpKind.BitAnd:
|
||||
return llvmlite.build_and(builder, lhs, rhs)
|
||||
elif op == ast.OpKind.BitOr:
|
||||
return llvmlite.build_or(builder, lhs, rhs)
|
||||
elif op == ast.OpKind.BitXor:
|
||||
return llvmlite.build_xor(builder, lhs, rhs)
|
||||
elif op == ast.OpKind.LShift:
|
||||
return llvmlite.build_shl(builder, lhs, rhs)
|
||||
if op == ast.OpKind.Add: return llvmlite.build_add(builder, lhs, rhs)
|
||||
elif op == ast.OpKind.Sub: return llvmlite.build_sub(builder, lhs, rhs)
|
||||
elif op == ast.OpKind.Mult: return llvmlite.build_mul(builder, lhs, rhs)
|
||||
elif op == ast.OpKind.Div: return llvmlite.build_sdiv(builder, lhs, rhs)
|
||||
elif op == ast.OpKind.FloorDiv: return llvmlite.build_sdiv(builder, lhs, rhs)
|
||||
elif op == ast.OpKind.Mod: return llvmlite.build_srem(builder, lhs, rhs)
|
||||
elif op == ast.OpKind.BitAnd: return llvmlite.build_and(builder, lhs, rhs)
|
||||
elif op == ast.OpKind.BitOr: return llvmlite.build_or(builder, lhs, rhs)
|
||||
elif op == ast.OpKind.BitXor: return llvmlite.build_xor(builder, lhs, rhs)
|
||||
elif op == ast.OpKind.LShift: return llvmlite.build_shl(builder, lhs, rhs)
|
||||
elif op == ast.OpKind.RShift:
|
||||
# 无符号 >> → lshr(逻辑右移),有符号 >> → ashr(算术右移)
|
||||
# 对齐 TPC ExprGen.emit_binary_op:i8/i16 默认无符号;其余查操作数无符号性
|
||||
@@ -1867,7 +1817,7 @@ class AssignGenMixin:
|
||||
continue
|
||||
attr_node: ast.Attribute | t.CPtr = (ast.Attribute | t.CPtr)(target)
|
||||
attr_name: str = "(unknown)"
|
||||
obj_name: str = "(unknown)"
|
||||
obj_name: str = "(unknown)"
|
||||
if attr_node is not None:
|
||||
attr_name = attr_node.attr
|
||||
if attr_node.value is not None and attr_node.value.kind() == ast.ASTKind.Name:
|
||||
@@ -2173,21 +2123,21 @@ class AssignGenMixin:
|
||||
# ============================================================
|
||||
def _op_to_dunder(self, op: int) -> str:
|
||||
"""BinOp/UnaryOp 运算符 → dunder 方法名,无映射返回 None"""
|
||||
if op == ast.OpKind.Add: return "__add__"
|
||||
if op == ast.OpKind.Sub: return "__sub__"
|
||||
if op == ast.OpKind.Mult: return "__mul__"
|
||||
if op == ast.OpKind.Div: return "__truediv__"
|
||||
if op == ast.OpKind.Add: return "__add__"
|
||||
if op == ast.OpKind.Sub: return "__sub__"
|
||||
if op == ast.OpKind.Mult: return "__mul__"
|
||||
if op == ast.OpKind.Div: return "__truediv__"
|
||||
if op == ast.OpKind.FloorDiv: return "__floordiv__"
|
||||
if op == ast.OpKind.Mod: return "__mod__"
|
||||
if op == ast.OpKind.Pow: return "__pow__"
|
||||
if op == ast.OpKind.BitAnd: return "__and__"
|
||||
if op == ast.OpKind.BitOr: return "__or__"
|
||||
if op == ast.OpKind.BitXor: return "__xor__"
|
||||
if op == ast.OpKind.LShift: return "__lshift__"
|
||||
if op == ast.OpKind.RShift: return "__rshift__"
|
||||
if op == ast.OpKind.USub: return "__neg__"
|
||||
if op == ast.OpKind.UAdd: return "__pos__"
|
||||
if op == ast.OpKind.Invert: return "__invert__"
|
||||
if op == ast.OpKind.Mod: return "__mod__"
|
||||
if op == ast.OpKind.Pow: return "__pow__"
|
||||
if op == ast.OpKind.BitAnd: return "__and__"
|
||||
if op == ast.OpKind.BitOr: return "__or__"
|
||||
if op == ast.OpKind.BitXor: return "__xor__"
|
||||
if op == ast.OpKind.LShift: return "__lshift__"
|
||||
if op == ast.OpKind.RShift: return "__rshift__"
|
||||
if op == ast.OpKind.USub: return "__neg__"
|
||||
if op == ast.OpKind.UAdd: return "__pos__"
|
||||
if op == ast.OpKind.Invert: return "__invert__"
|
||||
return None
|
||||
|
||||
# ============================================================
|
||||
|
||||
Reference in new issue
Block a user