[LLVMdev] Global variable-length array

Chris Lattner sabre at nondot.org
Sat Apr 19 21:07:54 PDT 2008


On Apr 19, 2008, at 7:45 PM, Talin wrote:

> Question about "Pascal-style" arrays as mentioned in the reference  
> guide.
>
> Suppose I have a global variable which points to a constant, variable
> length array.
>
> The question is, how can I assign an array of type "{ i32, [5 x  
> float]}"
> to a global of type "{ i32, [0 x float]}"? From my experimentation, it
> appears you can't bitcast or call GEP on a constant aggregate - the  
> only
> I know of to get the address of a constant is to create a global,  
> but I
> need the address to create the global.
>
> The only way that I can think of to do it is to have two globals - an
> anonymous global which is of the same type as the actual array  
> constant,
> and a second global which is assigned the bitcast of the GEP of the
> first global. However, this seems overly complicated - is there an
> easier way to do it?

Do you mean something like this:

struct foo {
   int x;
   char c[];
};

struct foo F = { 4, {3, 2, 1, 0 }};

struct foo *G = &F;


$ llvm-gcc t.c -S -o - -emit-llvm

	%struct.foo = type { i32, [0 x i8] }
@F = global { i32, [4 x i8] } { i32 4, [4 x i8] c"\03\02\01\00" }		;  
<{ i32, [4 x i8] }*> [#uses=1]
@G = global %struct.foo* bitcast ({ i32, [4 x i8] }* @F to %struct.foo*)

?

-Chris



More information about the llvm-dev mailing list