[LLVMdev] variable sized structs in LLVM

Carl Friedrich Bolz cfbolz at gmx.de
Mon Jun 20 04:03:25 PDT 2005


Hi LLVM-dev!

I'm having problems figuring out how to do variable sized structs in 
LLVM (which are neccessary for PyPy's LLVM backend, on which I'm 
working). I'm trying to do the equivalent of

struct array {
         long refcount;
         long length;
         long items[1];
};

in LLVM, where the items array can be arbitrarily long. I guess that the 
struct definition should be something like:

%array = type {int, int, [1 x int]}

but how would I allocate such a thing, with the items array being, say, 
9 items long? In C I would do something like:

malloc(sizeof(struct array) + ((9 - 1) * sizeof(long)));

but there is no sizeof in LLVM, right? If I try compile C code like that 
with the LLVM C frontend, I get

%struct.array = type { int, int, [1 x int] }

...

%tmp.0 = malloc [44 x sbyte]
%tmp.5 = cast [44 x sbyte]* %tmp.0 to %struct.array*


It is clear what happens here, but I don't know how I would reproduce 
that, because I can't easily find out the length in bytes of the array.

I can think of workarounds (like using the struct module of the Python 
standard library to calculate the sizes) but I have the feeling that I'm 
missing something and there is a simple way to do that in LLVM.

Thanks for any help,

Carl Friedrich




More information about the llvm-dev mailing list