[LLVMdev] Question about the old C back-end

Dmitri Gribenko gribozavr at gmail.com
Thu Oct 11 06:44:50 PDT 2012


On Thu, Oct 11, 2012 at 4:18 PM, Roel Jordans <r.jordans at tue.nl> wrote:
> Hello all,
>
> 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?

If I understand this correctly (I don't know what kind of code CBE
generates), the comment says that CBE generates code that wants to
treat arrays as values.  But in C arrays don't have this property
(partly because array in most contexts "decays" to a pointer to the
first element):

  int a[10];
  int b[10];
  a = b; // error
  int *c = a; // ok

But this can be sidestepped if one wraps the array into a struct.

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/



More information about the llvm-dev mailing list