Stack operating code that generated by gcc have an error. In some operating
system that saved the thread's context in stack when schedule, it will be have
critical problem.

Enabling the compiler optimalisation (-O2 option) the error will be occuring,
and don't occur when using option -O1 or -O0.

--- sample ---
array.c:
int find_num(int i)
{
    const int arr[5] = {0, 1, 2, 3, 4};
    return arr[i];
}
using command:
  ppc-eabi-gcc -c -O2 array.c -o array.o
  ppc-eabi-objdump -S array.o
the disassemble code is:
00000000 <find_num>:
   0:   3d 60 00 00     lis     r11,0
   4:   94 21 ff d0     stwu    r1,-48(r1)
   8:   39 2b 00 00     addi    r9,r11,0
   c:   81 0b 00 00     lwz     r8,0(r11)
  10:   80 e9 00 10     lwz     r7,16(r9)
  14:   54 63 10 3a     rlwinm  r3,r3,2,0,29
  18:   80 09 00 04     lwz     r0,4(r9)
  1c:   81 69 00 08     lwz     r11,8(r9)
  20:   81 49 00 0c     lwz     r10,12(r9)
  24:   7d 21 1a 14     add     r9,r1,r3
  28:   91 01 00 10     stw     r8,16(r1)
  2c:   90 01 00 14     stw     r0,20(r1)
  30:   91 61 00 18     stw     r11,24(r1)
  34:   91 41 00 1c     stw     r10,28(r1)
  38:   90 e1 00 20     stw     r7,32(r1)
  3c:   38 21 00 30     addi    r1,r1,48
  40:   80 69 00 10     lwz     r3,16(r9)
  44:   4e 80 00 20     blr

The instruction 3c let stack pointer back, and instruction 40 load data from
the stack, this is error. The true code must be:
    lwz     r3,16(r9)
    addi    r1,r1,48
And when you using option -O1, the code generated is true.

Release:
GCC v3.4.6 ppc-eabi cross compiler in cygwin

Environment:
Reading specs from
/cygdrive/d/Compiler/gcc3.4.6-ppc-eabi/bin/../lib/gcc/ppc-eab
i/3.4.6/specs
Configured with: ../configure --target=ppc-eabi
--prefix=/usr/local/gcc3.4.6-ppc
-eabi --enable-languages=c
Thread model: single
gcc version 3.4.6

How-To-Repeat:
Using my sample.


-- 
           Summary: Optimization flag -O2 generate error stack operating
                    code
           Product: gcc
           Version: 3.4.6
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: chenkb at ruijie dot com dot cn


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30282

Reply via email to