This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Really, really const

Op vr 26-09-2003, om 09:59 schreef Andrew Haley:
>  > | The memory a[0] is read twice.  Is there any attrribute I can use to
>  > | stop gcc from doing this?
>  > 
>  > GCC should be able to get that right without resorting to attributte
>  > hackery. 
> I think the C standard says that gcc has to read the memory twice.
> Just because that memory is declared const here that doesn't mean that
> it is const everywhere else; it just means that it's read-only here.
> It might change.

If you are right, then GCC is buggy.  If a[0] might change, then that
would have to happen in the call to f(), so in a modified version of
your example:

extern const int a[];
extern void f();

int foo ()
  int t = a[0];
  int n = t;
  n += t;
  return n;

the assignment to t should happen before the call to f().  But GCC 3.2.2
produces this:

        .file   "t.c"
        .p2align 2,,3
.globl foo
        .type   foo,@function
        subl    $12, %esp
        call    f
        movl    a, %eax
        sall    $1, %eax
        addl    $12, %esp
        .size   foo,.Lfe1-foo
        .ident  "GCC: (GNU) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)"

In other words we assume that a[0] has not changed after the call.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]