https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92395
Bug ID: 92395
Summary: m68k-linux-gnu-gcc generates wrong code when the
-mshort option is used
Product: gcc
Version: 7.4.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: [email protected]
Target Milestone: ---
Created attachment 47189
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=47189&action=edit
C source file
The option -mshort makes GCC m68k to use int size = 16-bit instead of 32-bit.
But the pointers are also affected by this option and the asm code generated is
buggy. Compiling the following test case gives:
file a.c
void f(char *begin, char *end)
{
do
{
*end-- = 0;
}
while (end > begin);
}
m68k-linux-gnu-gcc -S -O2 -fomit-frame-pointer a.c -o -
.file "a.c"
.text
.align 2
.globl f
.type f, @function
f:
move.l 4(%sp),%d0
move.l 8(%sp),%a0
.L2:
clr.b (%a0)
subq.l #1,%a0
cmp.l %d0,%a0
jhi .L2
rts
.size f, .-f
.ident "GCC: (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0"
.section .note.GNU-stack,"",@progbits
m68k-linux-gnu-gcc -mshort -S -O2 -fomit-frame-pointer a.c -o -
.file "a.c"
.text
.align 2
.globl f
.type f, @function
f:
move.l 4(%sp),%d1
move.l 8(%sp),%a0
.L2:
clr.b (%a0)
move.l %a0,%d0
subq.l #1,%d0
add.l #65535,%a0 // Wrong code!
cmp.l %d1,%d0
jhi .L2
rts
.size f, .-f
.ident "GCC: (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0"
.section .note.GNU-stack,"",@progbits
Another example with itoa function attached:
m68k-linux-gnu-gcc -c itoa.c -Os -m68000 -Wall -mshort -fdata-sections
-ffunction-sections -fomit-frame-pointer -fno-builtin -fno-ident -S -o -
.file "itoa.c"
.text
.section .text.itoa,"ax",@progbits
.align 2
.globl itoa
.type itoa, @function
itoa:
movem.l #14384,-(%sp)
move.w 24(%sp),%d0
move.l 26(%sp),%a0
move.w 30(%sp),%d1
move.w %d1,%d2
subq.w #2,%d2
cmp.w #14,%d2
jhi .L9
cmp.w #10,%d1
jne .L10
move.w %d0,%d3
jge .L4
neg.w %d3
.L4:
move.w %d0,%d2
move.w %d3,%d0
.L3:
move.l %a0,%a2
moveq #0,%d3 // moveq #-1,%d3 -> 32-bit OK
not.w %d3 // remove this instruction
lea digits.1019,%a3
.L5:
ext.l %d0
divs.w %d1,%d0
move.l %d0,%d4
swap %d4
lea (1,%a2),%a1
move.b (%a3,%d4.w),(%a1,%d3.l)
tst.w %d0
jne .L11
tst.w %d2
jge .L6
lea (2,%a2),%a1
move.b #45,1(%a2)
.L6:
lea (-1,%a1),%a2
move.l %a0,%a3
moveq #0,%d0 // moveq #-1,%d0 -> 32-bit OK
not.w %d0 // remove this instruction
.L7:
cmp.l %a2,%a3
jcs .L8
.L2:
clr.b (%a1)
move.l %a0,%d0
movem.l (%sp)+,#3100
rts
.L10:
clr.w %d2
jra .L3
.L11:
move.l %a1,%a2
jra .L5
.L8:
move.b (%a2),%d1
move.b (%a3)+,(%a2)
move.b %d1,(%a3,%d0.l)
add.l #65535,%a2 // add.l #-1,%a2 -> 32-bit OK
jra .L7
.L9:
move.l %a0,%a1
jra .L2
.size itoa, .-itoa
.section .rodata.digits.1019,"a",@progbits
.type digits.1019, @object
.size digits.1019, 17
digits.1019:
.string "0123456789abcdef"
.section .note.GNU-stack,"",@progbits