我正在使用 LLVM 和 llvmpy 库。
我的目标是创建类似于以下 C 代码的内容:
int a[] = {1, 2};
int b[] = {1, 2, 3};
int c[] = {1};
int* ptrs[] = {a, b, c};
以下是上述摘录的输出IR:
%1 = alloca i32, align 4
%a = alloca [2 x i32], align 4
%b = alloca [3 x i32], align 4
%c = alloca [1 x i32], align 4
%ptrs = alloca [3 x i32*], align 16
store i32 0, i32* %1
%2 = bitcast [2 x i32]* %a to i8*
call void @llvm.memcpy.p0i8.p0i8.i64(i8* %2, i8* bitcast ([2 x i32]* @main.a to i8*), i64 8, i32 4, i1 false)
%3 = bitcast [3 x i32]* %b to i8*
call void @llvm.memcpy.p0i8.p0i8.i64(i8* %3, i8* bitcast ([3 x i32]* @main.b to i8*), i64 12, i32 4, i1 false)
%4 = bitcast [1 x i32]* %c to i8*
call void @llvm.memcpy.p0i8.p0i8.i64(i8* %4, i8* bitcast ([1 x i32]* @main.c to i8*), i64 4, i32 4, i1 false)
%5 = getelementptr inbounds [3 x i32*]* %ptrs, i64 0, i64 0
%6 = getelementptr inbounds [2 x i32]* %a, i32 0, i32 0
store i32* %6, i32** %5
%7 = getelementptr inbounds i32** %5, i64 1
%8 = getelementptr inbounds [3 x i32]* %b, i32 0, i32 0
store i32* %8, i32** %7
%9 = getelementptr inbounds i32** %7, i64 1
%10 = getelementptr inbounds [1 x i32]* %c, i32 0, i32 0
store i32* %10, i32** %9
%11 = load i32* %1
ret i32 %11
我相信我已经很好地理解了这一点 - 它只是抓取指向每个单独数组头部的指针,然后用于getelementptr
遍历存储数组并插入每个指针。
这是我想出的llvmpy
:
pointers = []
for array in arrays:
# Store each of the { 1, ... } arrays in memory.
type = Type.array(Type.int(), len(arrays))
ptr = builder.alloca_array(type, Constant.int(Type.int(), len(arrays)))
builder.store(array, ptr)
# And keep track of their pointers.
indices = [ Constant.int(Type.int(), 0), Constant.int(Type.int(), 0) ]
head = builder.gep(ptr, indices)
pointers.append(head)
# Finally, construct the array to hold the pointers.
type = Type.pointer(Type.int())
array = Constant.array(type, pointers)
return array
但我收到错误:
include/llvm/Support/Casting.h:219: typename cast_retty::ret_type llvm::cast_or_null(llvm::Value *): Assertion `isa(Val) && "cast_or_null() argument of in compatible type!"' 失败。
在包含代码的行上:
array = Constant.array(type, pointers)
当我检查指针的类型和数组的类型时,它们似乎都是int32*
. 我究竟做错了什么?而且,是否Value
需要强制转换通用占位符?