This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Forgetting return values
- From: Andrew Haley <aph at redhat dot com>
- To: Jamie Prescott <jpresss at yahoo dot com>
- Cc: "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Thu, 28 May 2009 19:46:48 +0100
- Subject: Re: Forgetting return values
- References: <516867.32690.qm@web111606.mail.gq1.yahoo.com>
[redirect to gcc-help]
Jamie Prescott wrote:
> What am I missing?
> I have a simple:
>
> static inline int set_prop(char const *path, char const *name,
> void const *data, int size)
> {
> int error;
>
> asm volatile ("int\t11\n\t"
> : "=a0" (error): "a0" (path), "a1" (name), "a2" (data),
> "a3" (size));
>
> return error;
> }
>
> extern int calc(int);
>
> int proc(int i)
> {
> int j = calc(i);
>
> return set_prop(0, 0, &j, sizeof(int));
> }
>
> The aX classes maps to the rX registers, no problem for GCC in there.
> The code above, compiled with GCC 4.4.0 and -O3 produces:
>
>
> _proc:
> push FP
> mov SP,FP
> sub SP,4,SP
> call _calc
> mov 0,r0
> mov 4,r3
> add FP,-4,r2
> mov r0,r1
> ; APP
> ; 69 "xxxx.c" 1
> int 11
>
> ; NO_APP
> add SP,4,SP
> pop FP
> ret
>
> GCC forgets about the calc() return value, and passes an address (FP-4) that
> has never been written into.
> buf if I add a "memory" clobber to the asm inline, everything comes back to
> normal:
>
> _proc:
> push FP
> mov SP,FP
> sub SP,4,SP
> call _calc
> str.w r0,FP[-4]
> mov 0,r0
> mov 4,r3
> add FP,-4,r2
> mov r0,r1
> ; APP
> ; 69 "xxxx.c" 1
> int 11
>
> ; NO_APP
> add SP,4,SP
> pop FP
> ret
>
> Why is the memory clobber required, and why GCC does not understand to
> sync the value to memory when passing the address to a function?
Because your asm doesn't access memory at all, so gcc knows that there is
no need to sync the value to memory. As far as gcc is concerned, the only
inputs to your asm is size. If your asm needed memory, you would have said
so. And, indeed, saying so fixes it.
Andrew.