[LLVMdev] variable sized structs in LLVM

Aaron Gray angray at beeb.net
Fri Jun 24 13:59:20 PDT 2005


> This is almost exactly what you want to do.  Please make the array be [0 x 
> int] though, as it is undefined in llvm to access past the end of an array 
> with non-zero length.  I added a note about zero-length arrays here:
>
> http://llvm.cs.uiuc.edu/docs/LangRef.html#i_getelementptr
> http://llvm.cs.uiuc.edu/docs/LangRef.html#t_array
>
> As you mention above, you should use malloc and cast the return value.  If 
> you want to get the desired size in a target-independent way (always 
> good), you should do something like this:
>
> %array = type { int, int, [0 x int] }
> implementation
> %array *%NewArray(int %Length) {
>   ;; Get the offset of the 'Length' element of the array from the null
>   ;; pointer.
>   %Size = getelementptr %array* null, int 0, uint 2, int %Length
>   %SizeU = cast int* %Size to uint
>   %Ptr = malloc sbyte, uint %SizeU
>   %Result = cast sbyte* %Ptr to %array*
>   ret %array* %Result
> }

Chris you should maybe add this example to the getelementptr documentation.

Aaron





More information about the llvm-dev mailing list