【发布时间】:2015-09-07 18:46:20
【问题描述】:
这是一个简单的 C 程序:
struct S {char c; short arr[16]; char dummy2;};
extern struct S A[20];
extern short* p;
int main() {
p = &A[10].arr[6];
return 0;
}
这是 LLVM IR:
%struct.S = type { i8, [16 x i16], i8 }
@A = external global [20 x %struct.S]
@p = external global i16*
; Function Attrs: nounwind
define i32 @main() #0 {
entry:
store i16* getelementptr inbounds ([20 x %struct.S]* @A, i64 0, i64 10, i32 1, i64 6), i16** @p, align 8, !tbaa !1
ret i32 0
}
如何计算getelementptr 添加到@A 的字节偏移量?
我可以很容易地循环并打印出 GEP:
auto& P = *GEP.getPointerOperand();
Out << "GEP(";
GEP.getType()->print(Out); // return type
Out << ", ";
P.printAsOperand(Out); // base
for (auto i=0U; i<GEP.getNumIndices(); i++) {
Out << ", ";
GEP.getOperand(i+1)->printAsOperand(Out); // index i
}
Out << ")\n";
打印出来:
GEP(i16*, [20 x %struct.S]* @A, i64 0, i64 10, i32 1, i64 6)
假设所有索引都是常量整数,你如何确定相对于基指针的字节偏移量?
【问题讨论】:
-
你最后找到解决办法了吗?
标签: c++ c compiler-construction llvm llvm-ir