[LLVMdev] Indexing backwards through a structure

David Given dg at cowlark.com
Mon Aug 23 07:02:33 PDT 2010


Given a structure like this (using C syntax rather than LLVM because I'm
still not fluent with LLVM assembly):

struct Object
{
  int i1;
  int i2;
  int i3;
};

Then, if I have an int* pointer which I know is pointing to the i3
element, what's the best way of recovering a pointer to the structure as
a whole?

My fallback option is to cast the pointer to an int64, use getelementptr
to determine the offset of the i3 element, subtract, cast back to a
pointer, etc. However this feels very clunky to me and I'd like a
cleaner way of doing it.

Any suggestions?

(The actual use case: I have an object with embedded vtables; given a
pointer to one of the vtables, I need to get the pointer to the actual
object instance. Does LLVM has any built-in support for doing this sort
of thing?)

-- 
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│
│ life←{ ↑1 ⍵∨.^3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂⍵ }
│ --- Conway's Game Of Life, in one line of APL

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 262 bytes
Desc: OpenPGP digital signature
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20100823/51ed2d67/attachment.sig>


More information about the llvm-dev mailing list