We had ported gcc-3.4.2 to our own RISC, and meet a strange
case in optimization level 3 (-O3).
The compiler produce wrong assembly code in O3 and
correct result if we add -fno-inline flag.
It seems that there some problem in function in-lining.
What can I do and what should I do to solve this problem ?
Source : xyz.c
============================
...
... skip
...
#undef errno
int errno;
int _write (int fd, char *buf, int nbytes);
#ifdef REENTRANT_SYSCALLS_PROVIDED
volatile
int
_write_r (struct _reent *r, int fd, char *buf, int nbytes)
{
return _write(fd, buf, nbytes);
}
#endif
/*
* write -- write bytes to the serial port. Ignore fd, since
* stdout and stderr are the same. Since we have no filesystem,
* open will only return an error.
*/
int
_write (int fd, char *buf, int nbytes)
{
__asm__ volatile ("li r8,2"
:::"r8");
SemiHostedSyscall ();
}
Assembly file genereated with O3 optimization
==================================================
.text
.align 2
.globl _write
.ent _write
_write:
.frame r0,0,r3, 1 # vars= 0, regs= 0,
args= 0, gp= 0
.mask 0x00000000,0
#APP
.set volatile
li r8,2
sdbbp 0xb
mv r4, r4
mv r5, r11
#NO_APP
.set optimize
sw r5,errno
br! r3
.end _write
.align 2
.globl _write_r
.ent _write_r
_write_r:
.frame r0,0,r3, 1 # vars= 0, regs= 0,
args= 0, gp= 0
.mask 0x00000000,0
#APP
.set volatile
li r8,2
sdbbp 0xb ==> inline function call _write with worng arguments
mv r4, r4
mv r5, r11
#NO_APP
.set optimize
sw r5,errno
br! r3
.end _write_r
.comm errno,4,4
Assembly file genereated with O3 and -fno-inline flag
==================================================
.text
.align 2
.globl _write
.ent _write
_write:
.frame r0,0,r3, 1 # vars= 0, regs= 0,
args= 0, gp= 0
.mask 0x00000000,0
#APP
.set volatile
li r8,2
sdbbp 0xb
mv r4, r4
mv r5, r11
#NO_APP
.set optimize
sw r5,errno
br! r3
.end _write
.align 2
.globl _write_r
.ent _write_r
_write_r:
.frame r0,0,r3, 1 # vars= 0, regs= 0,
args= 0, gp= 0
.mask 0x00000000,0
mv! r4, r5
mv! r5, r6
mv! r6, r7
j _write
.end _write_r
.comm errno,4,4