gcc (GCC) 3.4.2 20041017 (Red Hat 3.4.2-6.fc3) and other versions
( gcc (GCC) 3.4.3 (Mandrakelinux 10.2 3.4.3-1mdk)
gcc (GCC) 3.4.1 (Mandrakelinux 10.1 3.4.1-4mdk)
gcc-3.4 debian sid )
generate invalid i386 assembly code involving some amd64 8 bits registers when
compiling the following code :
#include <stdio.h>
int main( int nb, char *argv[] )
{
unsigned char a = nb, b = nb + 1;
printf("%d %d\n", a, b);
a ^= b; b ^= a; a ^= b;
printf("%d %d\n", a, b);
return 0;
}
[EMAIL PROTECTED] ~]$ gcc -march=i386 -O2 -o test test.c
/tmp/ccmWc5X6.s: Assembler messages:
/tmp/ccmWc5X6.s:24: Error: bad register name `%sil'
[EMAIL PROTECTED] ~]$ gcc -march=i486 -O2 -o test test.c
/tmp/cccvo0qG.s: Assembler messages:
/tmp/cccvo0qG.s:26: Error: bad register name `%sil'
[EMAIL PROTECTED] ~]$ gcc -march=i586 -O2 -o test test.c
/tmp/ccnZhGXe.s: Assembler messages:
/tmp/ccnZhGXe.s:24: Error: bad register name `%sil'
[EMAIL PROTECTED] ~]$ gcc -march=i686 -O2 -o test test.c
[EMAIL PROTECTED] ~]$ gcc -march=pentium4 -O2 -o test test.c
/tmp/ccCEvFNV.s: Assembler messages:
/tmp/ccCEvFNV.s:21: Error: bad register name `%sil'
Example of assembly code produced :
.file "test.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "%d %d\n"
.text
.p2align 2,,3
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
pushl %esi
pushl %ebx
movl 8(%ebp), %ebx
movl %ebx, %esi
andl $-16, %esp
incl %ebx
subl $20, %esp
movl %esi, %edx
movzbl %bl, %eax
pushl %eax
movzbl %dl, %eax
pushl %eax
xchgb %bl, %sil
pushl $.LC0
call printf
movzbl %bl, %ebx
addl $12, %esp
movl %esi, %edx
pushl %ebx
movzbl %dl, %eax
pushl %eax
pushl $.LC0
call printf
leal -8(%ebp), %esp
popl %ebx
xorl %eax, %eax
popl %esi
leave
ret
.size main, .-main
.section .note.GNU-stack,"",@progbits
.ident "GCC: (GNU) 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)"
This has been reported on this address (french page) :
https://linuxfr.org/forums/31/5533.html
This might be related to old bugs :
http://gcc.gnu.org/ml/gcc-bugs/2002-04/msg00263.html
http://www.best-websource.com/gcc/source-20020120-1.htm
--
Summary: a^=b; b^=a; a^=b; produce bad i386 assembly code with O1
and O2 (%sil being used)
Product: gcc
Version: 3.4.2
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: target
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: k_guillaume at libertysurf dot fr
CC: gcc-bugs at gcc dot gnu dot org
GCC target triplet: i386-linux-gnu
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19026