[LLVMdev] variable sized structs in LLVM

Reid Spencer reid at x10sys.com
Mon Jun 20 07:08:19 PDT 2005


Carl,

The thing you're missing is that LLVM's primitive types have well known,
fixed sizes that are not target dependent. A ulong is 8 bytes. A uint is
4 bytes. A ushort is 2 bytes. Etc. and always.

There are also methods in LLVM to help you deal with the size of a type
in bits and bytes. In particular you might want to note the following
methods:

Type::isSized
Type::getPrimitiveSize
Type::getPrimitiveSizeInBites

Reid

On Mon, 2005-06-20 at 13:03 +0200, Carl Friedrich Bolz wrote:
> 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
> 
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://mail.cs.uiuc.edu/mailman/listinfo/llvmdev
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20050620/73f4693f/attachment.sig>


More information about the llvm-dev mailing list