Actually, it looks like the compiler is generating correct code; it's just
that the  debugger actually steps through several lines of instructions on
that single line

return 1;

This is what the disassembly looks like using opt 1:

UInt16 err;

00000000: 4E56 0000          link      a6,#0
00000004: 2F03               move.l    d3,-(a7)
  err = 0;
  if (err != 0)
00000006: 7600               moveq     #0,d3
    return 1;
  else
   return 0;

00000008: 6704               beq.s     *+6            ; 0x0000000e
0000000A: 7001               moveq     #1,d0
0000000C: 6002               bra.s     *+4            ; 0x00000010
0000000E: 7000               moveq     #0,d0

Viewing in Mixed mode in the debugger window, the debugger will reach

00000008: 6704               beq.s     *+6            ; 0x0000000e

and then branch to

0000000E: 7000               moveq     #0,d0

which returns 0 - exactly what was intended.

Rgds,
Jun-Kiat Lam
Metrowerks Technical Support

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of William
Brodie
Sent: Thursday, July 06, 2000 3:16 PM
To: Palm Developer Forum
Subject: CodeWarrior optimizer: incorrect code for simple conditionals


With the default optimization settings, CodeWarrior generates incorrect code
for simple conditional statements.  (We observed the problem in CodeWarrior
Version 4.0.1, Build 0436 but have also tested it using the 3.5 SDK with
CodeWarrior 6.)  The following sample produces the symptom:

#include <Pilot.h>

DWord PilotMain( Word cmd, Ptr cmdPBP, Word launchFlags)
{

  UInt16 err;

  err = 0;
  if (err != 0)
    return 1;
  else
   return 0;

}

With the default global optimization settings (optimize for speed, level 1),
this will cause PilotMain to bypass the test and return 1.  With
optimization turned off (optimize for speed, level 0), it correctly returns
0.  The project was created from a virgin "Palm VII Stationery" C project.

Of course, we can get rid of the problem by always compiling with
optimizations turned off, but that is undesirable.  Is this a known problem?
It seems like a pretty basic one.




--
For information on using the Palm Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/tech/support/forums/


-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to