[LLVMdev] Question about the old C back-end

Duncan Sands baldrick at free.fr
Thu Oct 11 06:47:00 PDT 2012


Hi Roel,

> When going through the internals of the old C back-end, I see that the CBE
> encapsulates arrays into a struct.  The source code has the following comment to
> explain this behaviour.
>
>       // Arrays are wrapped in structs to allow them to have normal
>       // value semantics (avoiding the array "decay").
>
> For example, the CBE translates:
>    @a = common global [10 x i32] zeroinitializer, align 16
>
> into:
>    struct { unsigned int array[10]; } a;
>
> However, the reason for this behaviour is not completely clear to me. Can anyone
> give me further explanation of that is meant by 'array decay' and why it is not
> possible (or easy) to generate normal C-style arrays?

an IR function might return an array, or have a parameter of array type.  Eg it
might be:
   declare [4 x i8] @foo([8 x i8] %x)
If you tried to turn that into the C
   char[4] foo(char[8] x);
then (1) the compiler would reject it, and (2) even if it didn't reject it it
would probably just return a pointer to the first element of the array rather
than the array elements themselves, in keeping with C's usual confusion between
arrays and pointers.  Wrapping the array in a struct gets around these problems.

Ciao, Duncan.





More information about the llvm-dev mailing list