schmidt%crimee.ics.uci.edu@PARIS.ICS.UCI.EDU ("Douglas C. Schmidt") (01/16/89)
Hi,
The following problem is either a gcc 1.32 bug on the sun3,
or an error in my understanding of inline asm (probably the latter!).
I'm attempting to use an asm statement to exchange 2 values.
Here's my approach:
----------------------------------------
main () {
int i = 10;
int j = 11;
printf ( "i = %d, j = %d\n", i, j);
/* exchange the values of i and j */
asm ("exg %0,%2" : "=d" (i) : "0" (i), "=d" (j), "2" (j));
printf ( "i = %d, j = %d\n", i, j);
}
----------------------------------------
This works fine when -O is not used. However, when -O *is*
used, it fails. Examining the assembly code shows why:
----------------------------------------
#NO_APP
gcc_compiled.:
.text
LC0:
.ascii "i = %d, j = %d\12\0"
.even
.globl _main
_main:
link a6,#0
moveml #0x3020,sp@-
moveq #10,d2
moveq #11,d3
movel d3,sp@-
movel d2,sp@-
pea LC0
lea _printf,a2
jbsr a2@
movel d3,sp@- # error, this is pushed to soon onto stack before swap.
#APP
exg d2,d3
#NO_APP
# movel d3,sp@- however, if moved down here the program works.
movel d2,sp@-
pea LC0
jbsr a2@
moveml a6@(-12),#0x40c
unlk a6
rts
----------------------------------------
Now, I'm not sure if this is a bug, or if I've failed to specify the
constraints properly. The underlying question is, ``how does one
correctly indicate that both operands to an asm statement are
`read-write'?''
thanks,
Doug
----------------------------------------
p.s. I think there's a typo in gcc.texinfo. The following patch
corrects it.
*** gnu/gcc-1.32/gcc.texinfo.~1~ Sat Jan 14 11:12:11 1989
--- gnu/gcc-1.32/gcc.texinfo Sun Jan 15 23:22:47 1989
***************
*** 2205,2211 ****
Here @code{angle} is the C expression for the input operand while
@code{result} is that of the output operand. Each has @samp{"f"} as its
operand constraint, saying that a floating-point register is required. The
! @samp{=} in @samp{=r} indicates that the operand is an output; all output
operands' constraints must use @samp{=}. The constraints use the same
language used in the machine description (@pxref{Constraints}).
--- 2205,2211 ----
Here @code{angle} is the C expression for the input operand while
@code{result} is that of the output operand. Each has @samp{"f"} as its
operand constraint, saying that a floating-point register is required. The
! @samp{=} in @samp{=f} indicates that the operand is an output; all output
operands' constraints must use @samp{=}. The constraints use the same
language used in the machine description (@pxref{Constraints}).