> In any case when you > want to obtain a base pointer to a "derived" object (where derivation > is implemented as you showed - with nesting) it's simply a matter of > using getelementptr to obtain a pointer to the nested base object and > using that.Umm ok, but i've the strange feeling that i'm missing something.. First a simple question: getelementptr with index 0 just give me the same pointer, right? Ok, if it's so the example you gave me worked becouse the first element of "derived" is the "base" portion, thus having the same pointer. But what if it was not the first? For example with multiple derivation only one class can be in the first position: %base1 = type { int } %base2 = type { sbyte } %derived = type { %base1, %base2, long } void myProc(base2 obj) { } Now if you use get elementptr to obtain "base2" member of "derived" to pass it to myProc, then you're basically passing a different pointer, breaking instance identity among other things: public Derived global_var = new ....; main() { myProc(d); } void myProc(base2 obj) { boolean b = (global_var == obj); // this would be false! Derived alias = (Derived) obj; // this would break too btw } So what am i missing ? maybe to deal with multiple ereditation one need some more "manadgement" code ? and does the optimizer still "see" all relations and code features? Thank you!
Alkis Evlogimenos
2005-Aug-26 15:44 UTC
[LLVMdev] Mapping of class derivated and interfaces
On Fri, 2005-08-26 at 09:24 +0200, Nicola Lugato wrote:> > In any case when you > > want to obtain a base pointer to a "derived" object (where derivation > > is implemented as you showed - with nesting) it's simply a matter of > > using getelementptr to obtain a pointer to the nested base object and > > using that. > > Umm ok, but i've the strange feeling that i'm missing something.. > First a simple question: getelementptr with index 0 just give me the > same pointer, right? > > Ok, if it's so the example you gave me worked becouse the first > element of "derived" is the "base" portion, thus having the same > pointer. But what if it was not the first? For example with multiple > derivation only one class can be in the first position: > > %base1 = type { int } > %base2 = type { sbyte } > %derived = type { %base1, %base2, long }This will not work with multiple inheritance but I assume we are talking about Java, right?> void myProc(base2 obj) > { > } > > Now if you use get elementptr to obtain "base2" member of "derived" to > pass it to myProc, then you're basically passing a different pointer, > breaking instance identity among other things: > > public Derived global_var = new ....; > main() > { > myProc(d); > } > > void myProc(base2 obj) > { > boolean b = (global_var == obj); // this would be false! > > Derived alias = (Derived) obj; // this would break too btw > } > > So what am i missing ? maybe to deal with multiple ereditation one > need some more "manadgement" code ? and does the optimizer still "see" > all relations and code features?I don't see why casting to a base class pointer will change the equality test at all. global_var == obj will evaluate to true and alias will point to global_var as expected. The fact that a pointer is "annotated" with a type does not change the fact that it is a memory address :-) -- Alkis
Hi, I am interested in obtaining LLVM IR without any optimization performed on it.( IR obtained from cfrontend's AST). Is this LLVM IR in SSA form? Secondly, I want to make a transformation on this unoptimized IR, and convert it back to C. I believe llc -c does that. Thirdly, is it possible to use LLVM tool suite on LLVM IR that's not in SSA form, if we have such LLVM so.
> Umm ok, but i've the strange feeling that i'm missing something.. > First a simple question: getelementptr with index 0 just give me the > same pointer, right?the first index parameter in the previous example was 'int 0' - this is just to get the first object pointed to by the pointer. If no other index parameters were passed to getelementptr, then yes it woudl just return the same pointer it was passed. But there was a second argument 'uint 0' - this is the part used to index the field within the struct - so if the base object was the second element of the derived object (for multiple inheritence or whatever other reason you might imagine) then this second parameter could be 'uint 1' The rest of your message seems to be based on the idea that the derived pointer value was just begin converted to a base pointer directly, which wasn't the case - a pointer to the base object within the derived object was obtained. It just so happened that the base object was the first one within the derived object, though this is in no way a requirement. David -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS d+@ s++: a-- C++++ ULS++ P L++ !E W++ N+ o? K? w(+) O? M@ V? PS+ PE@ Y+ PGP- t(+) 5 X+ R tv+ b+ DI++ D++ G+ e h! r y-(-) ------END GEEK CODE BLOCK------