Subject: -O2 vax compiler bug?
To: None <port-vax@netbsd.org>
From: Chuck Cranor <chuck@research.att.com>
List: tech-toolchain
Date: 09/10/2001 17:21:45
hi-

    while attempting to compile a -current vax, i discovered
that the /bin/cp command no longer works.   i boiled it down
to the code segment included below.   it fails with both 1.5.2
and -current.

    can anyone fix it?   my vax needs a working /bin/cp.


vax[17]> cat utils.c 
#include <sys/stat.h>

int main() {
        struct stat st, *fs;
        int to_fd;
        
        st.st_size = 13216;

        fs = &st;

        if (to_fd == -1) {              /* NEED THIS BLOCK */
                return (1);
        }

        if (fs->st_size > 0) {

                if (fs->st_size <= 8 * 1048576) {
                        printf("THIS SHOULD PRINT\n");
                } else {
                        printf("THIS SHOULD NOT PRINT\n");
                }
        }

        printf("DONE - SOMETHING SHOULD HAVE PRINTED BEFORE THIS\n");
}

vax[18]> cc -o x utils.c
vax[19]> ./x
THIS SHOULD PRINT
DONE - SOMETHING SHOULD HAVE PRINTED BEFORE THIS
vax[20]> cc -O -o x utils.c
vax[21]> ./x
THIS SHOULD PRINT
DONE - SOMETHING SHOULD HAVE PRINTED BEFORE THIS
vax[22]> cc -O2 -o x utils.c
vax[23]> ./x
DONE - SOMETHING SHOULD HAVE PRINTED BEFORE THIS
vax[24]> gcc -v
Using builtin specs.
gcc version egcs-2.91.66 19990314 (egcs-1.1.2 release)
vax[25]> 


the difference between the working -O version and the broken
-O2 version is:

vax[45]> diff utils-o.s utils-o2.s
19c19
<       movab -96(fp),r0
---
>       movab -96(fp),r1
25c25
<       tstl 56(r0)
---
>       movl 56(r1),r0
28c28
<       tstl 52(r0)
---
>       movl 52(r1),r0
30c30
<       cmpl 52(r0),$8388608
---
>       cmpl r0,$8388608
vax[46]> 


the problem is with changing the tstl's into movl's and the effect
on the condition codes.   the web pages below claim that tstl clears
both "C" and "V" condition codes, while "movl" clears "V" but 
preserves "C"

(see:
http://www.openvms.compaq.com:8000/73final/4515/4515pro_018.html#index_x_704
http://www.openvms.compaq.com:8000/73final/4515/4515pro_017.html#index_x_681 )


it then uses blequ:
0x1846 <main+50>:       movl 52(r1),r0
0x184a <main+54>:       blequ 0x1866

http://www.openvms.compaq.com:8000/73final/4515/4515pro_019.html#index_x_759
says blequ tests both C and Z to determine if it should jump.

something is wrong with the handling of C.



chuck