bartel@lan.informatik.tu-muenchen.dbp.de (Elmar Bartel) (09/21/88)
Hi folks! I have discovered two Bugs in the optimizer of the portable C-compiler for the iAPX386. Ok, I know that every optimizer has bugs, and to be save, i should'nt use the -O option. But the portable c-compiler generates bad code, and optim does a fairly good job - so I would be glad, if i could use it. Following two C-Files, which demonstrate the bugs. The files consist of very few lines of C-code. The bug is clearly isolated. In bug1.c semantical wrong assembly-code is generated (i believe it hard to be fixed), in bug2.c syntactical wrong assembly-code is generated (This should be easier to fix). Is someone outhere, who has solutions, or workarounds? Most appreciated would be patches to the sources of optim. It should be mentioned that I discovered this bugs when compiling automatically generated C-Code: I was compiling the web2c stuff. So it doesn't help if you say i shouldn't use such c-constructs, when i'am aware of these bugs. your waiting elmar. ---------- bug1.c ---- cut here ----- file bug1.c ---- cut here ------- /*********************************************************************** ** Bug in the Optimizer of the Portable C-compiler for iAPX386 ** ** The following C program will produce wrong code when compiled with ** cc -O ... bug1.c ** No wrong code is produced when compiled without -O. ** This C program was compiled with the protable C-compiler distributed ** with Microports Software Development System. ** ** Discoverd by ** ** Elmar Bartel ** Technische Universitaet Muenchen ** Institut fuer Informatik ** Arcisstrasse 21 ** 8000 Muenchen 2 ** W. Germany ***********************************************************************/ t(a,b) register int a,b; { register int c; while(1) { c = a - b; a = c + a; /* This line will be WRONG optimized */ } } /*********************************************************************** ** The wrong optimization does not occur when the surrounding do-while ** loop is missing, or no register variables are used. ** ** The generated assembler code - obtained with ** cc -O -S bug1.c ** follows: .file "bug1.c" .version "02.01" .data .text .align 4 .def t; .val t; .scl 2; .type 044; .endef .globl t t: pushl %ebp movl %esp,%ebp pushl %edi pushl %esi pushl %ebx movl 8(%ebp),%edi movl 12(%ebp),%esi .L16: movl %edi,%eax subl %esi,%eax movl %eax,%ebx / This is the statement c = a - b; movl %ebx,%edi addl %edi,%edi / This SHOULD be the statement a = c + a; jmp .L16 .align 4 .def t; .val .; .scl -1; .endef .data .text ***************************************************/ ---------- bug2.c ---- cut here ----- file bug2.c ---- cut here ------- /*********************************************************************** ** Bug in the Optimizer of the Portable C-compiler for iAPX386 ** ** The following C program will not compile when called with ** cc -O -c bug2.c ** The reason is, that the optimizer produces syntactical wrong ** assembly code. ** This C program was compiled with the protable C-compiler distributed ** with Microports Software Development System. ** ** Discoverd by ** ** Elmar Bartel ** Technische Universitaet Muenchen ** Institut fuer Informatik ** Arcisstrasse 21 ** 8000 Muenchen 2 ** W. Germany ***********************************************************************/ char a[256]; main() { char c; a [ c ] = c ; } /*********************************************************************** ** The error does not occur, when the array a is declared local to main ** ** The generated assembler code - obtained with ** cc -O -S bug2.c ** follows: .file "t.c" .version "02.01" .data .comm a,256 .text .align 4 .def main; .val main; .scl 2; .type 044; .endef .globl main main: pushl %ebp movl %esp,%ebp pushl %eax movsbl -1(%ebp),%eax movb %eax,%dl / THIS is syntactically wrong!! movb %dl,a(%eax) leave ret .def main; .val .; .scl -1; .endef .data .text ***************************************************/