0

我正在尝试转换这个原始 LLVM 代码:

%UnwEx = type { i64, i8*, i64, i64 }
@UnwEx.size = constant i64 ptrtoint (%UnwEx* getelementptr (%UnwEx* null, i32 1) to i64)

到 llvmlite:

unw_ex = context.get_identified_type("UnwEx")
unw_ex.elements = [int_, i8_ptr, int_, int_]
i32 = ir.IntType(32)
gep = builder.gep(ir.PointerType(unw_ex), [i32(1)], name="gep")
... # some extra code to cast gep to integer

这个想法是UnwEx在运行时获取结构化类型的大小。然而,命令的最后一行get提出了这个问题:

Traceback (most recent call last):
  File "C:/Users/DavidRagazzi/Desktop/mamba/mamba/core/runtime.py", line 495, in <module>
    generate(None, 64, 8)
  File "C:/Users/DavidRagazzi/Desktop/mamba/mamba/core/runtime.py", line 280, in generate
    gep = builder.gep(unw_ex_ptr, [i32(1)], name="gep")
  File "C:\Program Files\Python37\lib\site-packages\llvmlite\ir\builder.py", line 925, in gep
    inbounds=inbounds, name=name)
  File "C:\Program Files\Python37\lib\site-packages\llvmlite\ir\instructions.py", line 494, in __init__
    typ = ptr.type
AttributeError: 'PointerType' object has no attribute 'type'

这有什么问题?有没有更好的方法来获取类型的大小?

4

1 回答 1

1

gep的第一个参数应该是一个指向实例的值UnwEx。你传递的是一个类型,而不是一个值。获取该指针的方法是使用alloca或分配内存malloc(在这种情况下,您必须将返回的指针位转换为UnwEx*)。

获取UnwEx's 大小的另一种方法是使用它的get_abi_size成员。就像是

target = llvmlite.binding.Target.from_default_triple()
target_machine = target.create_target_machine()
unw_ex_size = unw_ex.get_abi_size(target_machine.target_data)

UnwEx为您提供一个实例将在您的主机平台上占用的字节大小。

于 2021-10-12T13:31:34.063 回答