Title: Common sub-expression elimination bug
We found a bug in the C/C++ optimizer in CodeWarrior R5 on the Mac. This bug is probably present in the Windows version as well since it's in the compiler, which is probably very similar on both platforms. Turning on the common sub-expression elimination optimization can cause the compiler to generate incorrect code.

Scenario: Create a new empty project with a single file containing the following code:

    typedef struct
          {
                       long foo1, foo2, foo3, foo4;
            } FooType;
     
        FooType *FooPtrs[4], Foo;
      
        static void TestCSE(unsigned short i)
   {
               Foo = *(FooPtrs[i]);
           
                FooPtrs[i]->foo1 = 0;
   }

Set the target settings as follows:

        Enable Global Optimizations = ON
        Global Register Allocation = OFF
        Common Sub-Expressions = ON
     Optimize Speed = OFF

Leave all other target settings at their default values. Now make the project. You'll get a link error since there is no 'main' routine. Ignore it. Now disassemble the program. You'll get:
       
        00000000: 4E56 0000          link      a6,#0
    00000004: 2F0A               move.l    a2,-(a7)
00000006: 7000               moveq     #0,d0
    00000008: 302E 0008          move.w    8(a6),d0
0000000C: E588               lsl.l     #2,d0
    0000000E: 41ED 0000          lea       FooPtrs,a0
       00000012: 24B0 0800          move.l    (a0,d0.l),(a2)
   00000016: 41ED 0000          lea       Foo,a0
   0000001A: 43D2               lea       (a2),a1
                                      ^----------------------should be move.l
   0000001C: 20D9               move.l    (a1)+,(a0)+
      0000001E: 20D9               move.l    (a1)+,(a0)+
      00000020: 20D9               move.l    (a1)+,(a0)+
      00000022: 20D9               move.l    (a1)+,(a0)+
      00000024: 4292               clr.l     (a2)
     00000026: 245F               movea.l   (a7)+,a2
00000028: 4E5E               unlk      a6
       0000002A: 4E75               rts

The result of this error is that the source for the structure copy is incorrect.
-
Danny Epstein     *     mailto:[EMAIL PROTECTED]
Applied Thought Corporation     *     http://www.appliedthought.com
Flytrap for PalmOS     *     http://www.appliedthought.com/flytrap

Reply via email to