Some simple information syncing
This commit is contained in:
@@ -847,10 +847,28 @@ def build_ui2fp(builder: IRBuilder | t.CPtr, val: Value | t.CPtr,
|
||||
# ============================================================
|
||||
# GEP 指令
|
||||
# ============================================================
|
||||
def _is_ptr_type(ty: LLVMType | t.CPtr) -> int:
|
||||
"""检查 ty 是否是 Ptr 类型(独立函数,避免嵌套 match 的编译器 BUG)"""
|
||||
if ty is None:
|
||||
return 0
|
||||
match ty:
|
||||
case LLVMType.Ptr(pointee):
|
||||
return 1
|
||||
case _:
|
||||
return 0
|
||||
|
||||
|
||||
def build_gep(builder: IRBuilder | t.CPtr, elem_ty: LLVMType | t.CPtr,
|
||||
ptr: Value | t.CPtr, idx: Value | t.CPtr) -> Value | t.CPtr:
|
||||
"""%N = getelementptr <elem_ty>, <ptr_ty> <ptr>, <idx_ty> <idx>"""
|
||||
if builder is None or ptr is None or idx is None: return None
|
||||
# 防御性检查: ptr.Ty 必须是指针类型,否则 llc 报错
|
||||
# "base of getelementptr must be a pointer"
|
||||
# 当 translate_value 返回非指针值(如 i32 常量 0)时,
|
||||
# 直接 GEP 会导致 llc 编译失败
|
||||
if _is_ptr_type(ptr.Ty) == 0:
|
||||
stdio.printf("[GEP-ERR] base not pointer, skip GEP\n")
|
||||
return None
|
||||
pool: memhub.MemBuddy | t.CPtr = builder.Pool
|
||||
name: t.CChar | t.CPtr = _alloc_ssa_name(builder)
|
||||
elem_ty_s: t.CChar | t.CPtr = _type_str(builder, elem_ty)
|
||||
@@ -918,7 +936,6 @@ def build_gep_struct(builder: IRBuilder | t.CPtr, struct_ty: LLVMType | t.CPtr,
|
||||
line: t.CChar | t.CPtr = pool.alloc(2048)
|
||||
if line is None:
|
||||
stdio.printf("[BGS] line alloc None\n")
|
||||
stdio.fflush(0)
|
||||
return None
|
||||
viperlib.snprintf(line, 2048, "%s = getelementptr %s, %s %s, i32 0, i32 %d",
|
||||
name, struct_ty_s, ptr_ty_s, ptr.Name, field_idx)
|
||||
@@ -928,14 +945,12 @@ def build_gep_struct(builder: IRBuilder | t.CPtr, struct_ty: LLVMType | t.CPtr,
|
||||
result_ty: LLVMType | t.CPtr = pool.alloc(LLVMType.__sizeof__())
|
||||
if result_ty is None:
|
||||
stdio.printf("[BGS] result_ty alloc None\n")
|
||||
stdio.fflush(0)
|
||||
else:
|
||||
string.memset(result_ty, 0, LLVMType.__sizeof__())
|
||||
c.DerefAs(result_ty, LLVMType.Ptr(field_ty))
|
||||
rv: Value | t.CPtr = SSAValue(pool, result_ty, name)
|
||||
if rv is None:
|
||||
stdio.printf("[BGS] SSAValue None\n")
|
||||
stdio.fflush(0)
|
||||
return rv
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user