RE: [cc65] Unexpected behaviour with equality

From: Shawn Jefferson <sjefferson1shaw.ca>
Date: 2014-10-11 19:06:55
I took a quick look at the code generated, and on the version of cc65 I'm
running 2.13.14, it works without optimizations on.  The code generated in
each case is quite different though.

Without optimizations:
000005r 1               ; printf("bad: %u\n", (0 == (joy_state & 1)) );
000005r 1               ;
000005r 1  A9 rr        	lda     #<(L0004)
000007r 1  A2 rr        	ldx     #>(L0004)
000009r 1  20 rr rr     	jsr     pushax
00000Cr 1  A2 00        	ldx     #$00
00000Er 1  A9 00        	lda     #$00
000010r 1  20 rr rr     	jsr     pushax
000013r 1  A0 04        	ldy     #$04
000015r 1  A2 00        	ldx     #$00
000017r 1  B1 rr        	lda     (sp),y
000019r 1  A2 00        	ldx     #$00
00001Br 1  29 01        	and     #$01
00001Dr 1  20 rr rr     	jsr     toseqax
000020r 1  20 rr rr     	jsr     pushax
000023r 1  A0 04        	ldy     #$04
000025r 1  20 rr rr     	jsr     _printf
000028r 1               ;
000028r 1               ; printf("good: %u\n", (joy_state & 1) == 0 );
000028r 1               ;
000028r 1  A9 rr        	lda     #<(L000A)
00002Ar 1  A2 rr        	ldx     #>(L000A)
00002Cr 1  20 rr rr     	jsr     pushax
00002Fr 1  A0 02        	ldy     #$02
000031r 1  A2 00        	ldx     #$00
000033r 1  B1 rr        	lda     (sp),y
000035r 1  A2 00        	ldx     #$00
000037r 1  29 01        	and     #$01
000039r 1  E0 00        	cpx     #$00
00003Br 1  D0 02        	bne     L000E
00003Dr 1  C9 00        	cmp     #$00
00003Fr 1  20 rr rr     L000E:	jsr     booleq
000042r 1  20 rr rr     	jsr     pushax
000045r 1  A0 04        	ldy     #$04
000047r 1  20 rr rr     	jsr     _printf

With optimizations:
000005r 1               ; printf("bad: %u\n", 0 == (joy_state & 1) );
000005r 1               ;
000005r 1  A9 rr        	lda     #<(L0004)
000007r 1  A2 rr        	ldx     #>(L0004)
000009r 1  20 rr rr     	jsr     pushax
00000Cr 1  A0 02        	ldy     #$02
00000Er 1  B1 rr        	lda     (sp),y
000010r 1  29 01        	and     #$01
000012r 1  E0 00        	cpx     #$00
000014r 1  D0 02        	bne     L0010
000016r 1  C9 00        	cmp     #$00
000018r 1  20 rr rr     L0010:	jsr     booleq
00001Br 1  20 rr rr     	jsr     pusha0
00001Er 1  A0 04        	ldy     #$04
000020r 1  20 rr rr     	jsr     _printf
000023r 1               ;
000023r 1               ; printf("good: %u\n", (joy_state & 1) == 0 );
000023r 1               ;
000023r 1  A9 rr        	lda     #<(L0009)
000025r 1  A2 rr        	ldx     #>(L0009)
000027r 1  20 rr rr     	jsr     pushax
00002Ar 1  A0 02        	ldy     #$02
00002Cr 1  B1 rr        	lda     (sp),y
00002Er 1  29 01        	and     #$01
000030r 1  20 rr rr     	jsr     booleq
000033r 1  20 rr rr     	jsr     pusha0
000036r 1  A0 04        	ldy     #$04
000038r 1  20 rr rr     	jsr     _printf


-----Original Message-----
From: owner-cc65@musoftware.de [mailto:owner-cc65@musoftware.de] On Behalf
Of Neil Stockbridge
Sent: Friday, October 10, 2014 4:17 PM

Hello,

I seem to be getting different results from an equality comparison depending
on whether the constant is on the left-hand side or not with
cc65 V2.13.3.

Here is an example that demonstrates the behaviour:

    #include <stdio.h>
    #include <stdint.h>

    #define  JOY_UP  1

    int main ( void)
    {
      uint8_t  joy_state = 0x7e;
      printf("bad: %u\n", 0 == (joy_state & 1) );
      printf("good: %u\n", (joy_state & 1) == 0 );

      return 0;
    }

This results in:

    bad: 0
    good: 1

<snip>

----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Sat Oct 11 19:09:59 2014

This archive was generated by hypermail 2.1.8 : 2014-10-11 19:10:01 CEST