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

             Bug #: 50314
           Summary: GCC changes order of code so it does not work as
                    intended
    Classification: Unclassified
           Product: gcc
           Version: 4.3.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: nickpar...@eaton.com


While timing a piece of code I discovered the following.  GCC reordered my code
such that the assembler is not doing things as the C code suggests.
Regards, Nick

C CODE:

  cli();
  time = BGndTimerReadNowIsr();
  result_u2  =  MulU2U2( ZERO_DEGC_IN_DEGK, manPres_u2) / (airTemp_u2 +
ZERO_DEGC_IN_DEGK);
  time = BGndTimerReadNowIsr() - time;
  sei();


Note in ASM code, the timer read is called PRIOR to the division. In the C code
above this is not how the intent is. This may not be a bug, but I certainly
didnt expect GCC to break my C statements and order them as it wishes!

ASM CODE:
113 0092 F894              cli
 114                    ;  0 "" 2
 115                   .LM16:
 116                   /* #NOAPP */
 117 0094 0E94 0000         call BGndTimerReadNowIsr
 118 0098 9A83              std Y+2,r25
 119 009a 8983              std Y+1,r24
 120                   .LM17:
 121 009c 82E1              ldi r24,lo8(4370)
 122 009e 91E1              ldi r25,hi8(4370)
 123 00a0 69E9              ldi r22,lo8(4505)
 124 00a2 71E1              ldi r23,hi8(4505)
 125 00a4 0E94 0000         call MulU2U2
 126 00a8 7B01              movw r14,r22
 127 00aa 8C01              movw r16,r24
 128                   .LM18:
 129 00ac 0E94 0000         call BGndTimerReadNowIsr
 130 00b0 2981              ldd r18,Y+1
 131 00b2 3A81              ldd r19,Y+2
 132 00b4 821B              sub r24,r18
 133 00b6 930B              sbc r25,r19
 134                   .LVL4:
 135 00b8 9A83              std Y+2,r25
 136 00ba 8983              std Y+1,r24
 137                   .LM19:
 138                   /* #APP */
 139                    ;  232 "tests.c" 1
 140 00bc 7894              sei
 141                    ;  0 "" 2
 142                   .LM20:
 143                   /* #NOAPP */
 144 00be 80E0              ldi r24,lo8(0)
 145                   .LVL5:
 146 00c0 60E0              ldi r22,lo8(__c.3250)
 147 00c2 70E0              ldi r23,hi8(__c.3250)
 148 00c4 0E94 0000         call PutFlashString
 149                   .LM21:
 150 00c8 80E0              ldi r24,lo8(0)
 151 00ca 60E0              ldi r22,lo8(__c.3252)
 152 00cc 70E0              ldi r23,hi8(__c.3252)
 153 00ce 0E94 0000         call PutFlashString
 154                   .LM22:
 155 00d2 C801              movw r24,r16
 156 00d4 B701              movw r22,r14
 157 00d6 22E3              ldi r18,lo8(5170)
 158 00d8 34E1              ldi r19,hi8(5170)
 159 00da 40E0              ldi r20,hlo8(5170)
 160 00dc 50E0              ldi r21,hhi8(5170)
 161 00de 0E94 0000         call __udivmodsi4

Reply via email to