When do you use "if ( a = b )"? (was Re: Funny mistake)

Arto V. Viitanen av at kielo.uta.fi
Fri Mar 22 17:49:47 AEST 1991


>>>>> On 20 Mar 91 21:45:46 GMT, torek at elf.ee.lbl.gov (Chris Torek) said:
Chris> X-Local-Date: Wed, 20 Mar 91 13:45:47 PST

Chris> In article <AV.91Mar20084724 at kielo.uta.fi> av at uta.fi (Arto Viitanen) writes:
Chris> [gcc version 1.39 on Sun-3, compiling `if (a = b)' vs `a = b; if (a)']
>In GNU cc, it was other way round: without optimization, assembler code
>was same, but with optimization, they were different.

Chris> That is peculiar.  I tried the same thing, with the same gcc, on the
Chris> same machine, and got identical code when optimized, different (poorer)
Chris> code for the second when not, exactly as expected.  

Yes, you are right. I mixed somewhere result assembler codes; actually both
SUN's and GNU's generate different codes without optimization and similar
with optimization.

What is more interesting, is the difference between codes of SUN and GNU:

when C program is:

#include <stdio.h>
main () {
   int a,b;

   a = 1;

   if(a=b)
       printf("yes\n");

}

, SUN's C compiler (SUN 3, SunOS 4.0.3) generates

        .data
        .text
LL0:
        .proc
|#PROC# 04
        .data1
L29:
        .ascii  "yes\012\0"
        LF25    =       8
        LS25    =       0
        LFF25   =       8
        LSS25   =       0
        LV25    =       8
        .data
        .text
        .globl  _main
_main:
|#PROLOGUE# 0
        link    a6,#-8
|#PROLOGUE# 1
        moveq   #1,d1
        movl    d1,a6@(-4)
        movl    a6@(-8),a6@(-4)
        jeq     L27
        pea     L29
        jbsr    _printf
        addqw   #4,sp
L27:
        moveq   #0,d0
|#PROLOGUE# 2
        unlk    a6
|#PROLOGUE# 3
        rts

but

gcc (version 1.39) generates

#NO_APP
gcc_compiled.:
.text
LC0:
        .ascii "yes\12\0"
        .even
.globl _main
_main:
        link a6,#0
        tstl d0
        jeq L2
        pea LC0
        jbsr _printf
L2:
        unlk a6
        rts

It seems than GCC drops the whole test away !!

--
Arto V. Viitanen				         email: av at kielo.uta.fi
University Of Tampere,				   	    av at ohdake.cs.uta.fi
Finland



More information about the Comp.lang.c mailing list