Interactions between function inlining and inline assembly

Segher Boessenkool segher@kernel.crashing.org
Tue Jan 14 12:33:00 GMT 2020


Hi!

On Tue, Jan 14, 2020 at 11:54:00AM +0000, RECOULES Frederic wrote:
> Let use the following (dumb) snippet as an example:
> 
> static int foo (int x)
> {
>     int r;
>     __asm__ ("movl %1, %0"  : "=r" (r) : "m" (x));
>     return r;
> }
> 
> int v;
> 
> int main (int argc, char *argv[])
> {
>     return foo(v);
> }
> 
> Which when compiled with GCC 9.2 (as long as I know, version does not matter)
> -m32 -O3 produce the following code:
> 
> main:
>   subl $16, %esp
>   movl v, %eax
>   movl %eax, 12(%esp)
> #APP
>   movl 12(%esp), %eax
> #NO_APP
>   addl $16, %esp
>   ret
> 
> 
> So, even if the function call is inlined, it continues to pass the argument v
> by the stack while, in fact, I would have expected it to forward the
> address like this:
> 
> main:
> #APP
>   movl v, %eax
> #NO_APP
>   ret

But you said that assembler argument has to be in memory!  Try this:

static int foo (int x)
{
    int r;
    __asm__ ("movl %1, %0"  : "=r" (r) : "rm" (x));
    return r;
}

You could also use "rmi" (which can be written "g"), here.


Segher



More information about the Gcc-help mailing list