[LLVMdev] Question about variable sized arrays

Gordon Henriksen gordonhenriksen at me.com
Wed Feb 25 18:56:47 PST 2009


You should bitcast the storage type of the constant (which includes  
the actual array bound) to the logical type (which includes the  
flexible array member instead). That is, bitcast { i32, [3 x i8] }*  
@foo to { i32, [0 x i8] }* before storing to %ptr.

This is somewhat analogous to C array type decay which allows you to  
pass "abc" (type const char[4]) to strlen(const char*). (However, in  
the case of array -> pointer conversions, a 'gep 0, 0' is sufficient.  
That technique doesn't work for flexible array constants.)

On 2009-02-25, at 21:09, Robin Sommer wrote:

> I'd like to represent strings "Pascal-style" with a length field
> followed by the actual bytes. Reading the reference manual, I think
> using a zero-length array should work in a struct like this:
>
>      { i32, [0 x int8] }
>
> However, I get a problem with referencing constants. The following
> code doesn't compie:
>
>    @foo = constant { i32, [3 x i8] } { i32 3, [3 x i8] c"foo" }
>
>    define void @main( { i32, [0 x i8] }* %ptr) {
>        store { i32, [3 x i8] }* @foo, { i32, [0 x i8] }** %ptr
>    }
>
> llvm-as reports:
>
>    Can't store '{ i32, [3 x i8] } *' into space of type '{ i32, [0 x  
> i8] } *'
>
> I'm wondering what the right way to do this is? I suppose I could
> cast the pointer but is that really necessary? I'm pretty new to
> LLVM so please forgive me if I'm missing something obvious ...
>
> Thanks a lot!
>
> Robin
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev



— Gordon





More information about the llvm-dev mailing list