【发布时间】:2019-12-15 11:33:50
【问题描述】:
我正在学习 llvm getelementptr 指令,并尝试从结构中获取元素。我的结构是这样的,
struct Foo {
int32_t a;
int32_t b;
int32_t c;
};
对应的llvm类型:
Type *getType() {
vector<Type *> tps;
tps.push_back(Type::getInt32Ty(TheContext));
tps.push_back(Type::getInt32Ty(TheContext));
tps.push_back(Type::getInt32Ty(TheContext));
StructType *tp = StructType::create(TheContext, tps, "foo_type");
return tp;
}
还有一个测试函数,
%foo_type = type { i32, i32, i32 }
define i32 @_func_(%foo_type) {
entry:
%1 = alloca %foo_type
store %foo_type %0, %foo_type* %1
%2 = getelementptr %foo_type, %foo_type* %1, i32 0, i32 1
%3 = load i32, i32* %2
ret i32 %3
}
但是通过运行编译函数,我总是得到第三个元素,即 Foo::c,而不是 Foo::b,那么我的代码有什么问题?我认为问题可能出在商店指令上。
编辑 2019.12.13
通过将指针作为参数传递,我得到了正确的答案
define i32 @_func_(%foo_type*) {
entry:
%1 = alloca %foo_type*
store %foo_type* %0, %foo_type** %1
%ptr = load %foo_type*, %foo_type** %1
%2 = getelementptr %foo_type, %foo_type* %ptr, i32 0, i32 1
%3 = load i32, i32* %2
ret i32 %3
}
所以问题一定是FP(f)实际上并没有将f传递给之前版本中的编译函数。
【问题讨论】:
-
如果这解决了您的问题。请添加它作为答案:)