Re: [cc65] optimizer or me?

From: Ullrich von Bassewitz <uz1musoftware.de>
Date: 2004-07-15 16:26:31
On Thu, Jul 15, 2004 at 07:09:22AM -0700, Shawn Jefferson wrote:
> Oh I forgot to mention, the listing is wrong when it
> optimizes in this way.  Not the assembly code, but the
> C statements comments attached to that code.

Not really:-)

The following C code:

------------------------------------------------------------------------------
void ConfuseTheOptimizer (void)
{
}
int main (void)
{
    char a;
    *(unsigned char*)0xD104  = 1;
    ConfuseTheOptimizer ();
    if ((*(unsigned char*)0xD104) == 1) {
        a = 2;
    } else {
        a = 1;
    }
    return 1;
}
------------------------------------------------------------------------------

Is translated into:

------------------------------------------------------------------------------
;
; *(unsigned char*)0xD104  = 1;
;
        lda     #$01
        sta     $D104
;
; ConfuseTheOptimizer ();
;
        jsr     _ConfuseTheOptimizer
;
; if ((*(unsigned char*)0xD104) == 1) {
;
        lda     $D104
        cmp     #$01
        bne     L0007
;
; a = 2;
;
        lda     #$02
;
; } else {
;
        jmp     L0011
;
; a = 1;
;
L0007:  lda     #$01
L0011:  ldy     #$00
        sta     (sp),y
;
; return 1;
;
        ldx     #$00
        lda     #$01
;
; }
;
        jmp     incsp1
------------------------------------------------------------------------------

As you can see, the optimizer merges the stores into the variable a. This is
the reason why in your code snippet, it says "a = 2" in the comment while
actually storing 1 there. The code for the store is actually the code that was
generated for the "a = 2" statement. It is the store from the "a = 1"
statement that was removed.

Things like this do happen often when working with the optimizer and are also
true for other compilers. If you debug programs compiled by gcc using
optimization, you will often notice that the debugger points to lines of code
that should not execute at this point. There is no easy way to fix that.

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz@musoftware.de
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Thu Jul 15 16:26:37 2004

This archive was generated by hypermail 2.1.8 : 2004-07-15 16:26:45 CEST