I'm working on a compiler that translates a Java-like language to LLVM and I'm having problems with the getelementptr
instruction.
Say I have classes A and B defined as follows:
class A {int val;}
class B {A a;}
This gets translated to LLVM types:
%A = type {%A_vtable_type*, i32}
%B = type {%B_vtable_type*, %A*}
where the first elements are pointers to virtual tables and aren't important for this question.
I want to translate the following code:
A a;
B b;
b = new B;
b.a = new A;
b.a.val = 42;
The first four instructions work just fine and the fifth one gets translated to something like this:
%b = alloca %B*
...
%1 = load %B*, %B** %b
%2 = getelementptr %B, %B* %1, i32 0, i32 1, i32 1
store i32 42, i32* %2
My understanding of these indices is this:
- The 0 is dereferencing of the
%1
of type %B*
- The first 1 is the field
a
of struct %B*
- The second 1 is the field
val
of type %A*
Now, I read https://llvm.org/docs/GetElementPtr.html and from what I understood, one can do multiple address computations in one GEP without needing to dereference the subsequent pointer types, however after running
llvm-as-6.0 classes.ll -o classes.bc
I get
llvm-as-6.0: classes.ll:40:31: error: invalid getelementptr indices
The error goes away after removing the i32 0
, but for example when I tried to create even more nested classes, then I get a type error.
question from:
https://stackoverflow.com/questions/65626436/llvm-error-invalid-getelementptr-indices 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…