Python 如何将数组指针传递给LLVM/llvmpy中的函数?

Python 如何将数组指针传递给LLVM/llvmpy中的函数?,python,types,compiler-construction,llvm,llvm-py,Python,Types,Compiler Construction,Llvm,Llvm Py,我正在使用llvmpy生成IR代码。但是,我一直在使用printf和int8数组 以下是给我带来问题的摘录: # Defining the print function. # ----- fntype = Type.function(Type.void(), [Type.pointer(Type.int(8))]) myprint = module.add_function(fntype, 'print') cb = CBuilder(myprint) x = cb.printf("%s\n",

我正在使用
llvmpy
生成IR代码。但是,我一直在使用
printf
int8
数组

以下是给我带来问题的摘录:

# Defining the print function.
# -----
fntype = Type.function(Type.void(), [Type.pointer(Type.int(8))])
myprint = module.add_function(fntype, 'print')
cb = CBuilder(myprint)
x = cb.printf("%s\n", cb.args[0])
cb.ret()
cb.close()

# Creating the string.
# -----
chartype = Type.int(8)
chars = [Constant.int(chartype, ord(c)) for c in "Hello World!"]
string = Constant.array(chartype, chars)
ptr = string.gep([Constant.int(Type.int(8)), 0])

# Calling the print function.
# -----
cfn = exe.get_ctype_function(module.get_function_named('print'), 
                             None, ct.c_char_p)
cfn(ptr)
当我运行此代码时,我收到

ctypes.ArgumentError:参数1::错误 类型

我做错了什么?我觉得我对
.gep()
的使用是错误的,但我不确定用什么方法。还是有什么我不明白的


另外,是否有办法从函数中获取预期类型?

是的,您使用的
gep
不正确:

  • gep
    方法接收一组索引,因此不确定类型在那里做什么
  • gep
    方法的接收器必须是指针(或指针向量),而接收器是数组
  • 但是这里的基本问题是,您试图获取编译时常量的地址,即从未分配任何内存的某个对象的地址

    正确的方法是创建一个

  • 初始化为您的“hello world”和
  • 标为常数的
  • 这样一个变量被分配一个地址(类型为指向i8数组的指针),然后您可以使用
    gep
    bitcast
    常量表达式来获取
    i8*
    ,并将其发送到打印函数


    例如,尝试打开,您将看到字符串文字被放置在这样一个全局变量中。

    谢谢!实际上我想要运行时常量,但你的解释对这两个都是完美的。