public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Local variables used inside an asm block are not recognized as used
@ 2014-10-05 18:13 Daniel Kamil Kozar
  2014-10-05 21:08 ` David Wohlferd
  0 siblings, 1 reply; 3+ messages in thread
From: Daniel Kamil Kozar @ 2014-10-05 18:13 UTC (permalink / raw)
  To: gcc-help

Hello,
Long time ago, I wrote the following code snippet in order to
demonstrate how to call system calls directly from gcc via inline
assembly in amd64 systems, as opposed to using the libc wrapper
functions.

#include <unistd.h>

int main(void)
{
    const char hello[] = "Hello World!\n";
    const size_t hello_size = sizeof(hello);
    ssize_t ret;
    asm
    (
        "movl $1, %%eax\n\t"
        "movl $1, %%edi\n\t"
        "movq %1, %%rsi\n\t"
        "movl %2, %%edx\n\t"
        "syscall"
        : "=a"(ret)
        : "g"(hello), "g"(hello_size)
        : "%rdi", "%rsi", "%rdx", "%rcx", "%r11"
    );
    return 0;
}

Unfortunately, this snippet does not work anymore with gcc 4.9.1. An
inspection of gcc's result when run with -S shows that the "hello"
variable is not even created. Adding "static" to the variable's
declaration fixes the issue, however I'm still wondering what's wrong
with the original code and why gcc does not seem to see that the local
variable is actually used by the asm block.

Thanks,
-dkk

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Local variables used inside an asm block are not recognized as used
  2014-10-05 18:13 Local variables used inside an asm block are not recognized as used Daniel Kamil Kozar
@ 2014-10-05 21:08 ` David Wohlferd
  0 siblings, 0 replies; 3+ messages in thread
From: David Wohlferd @ 2014-10-05 21:08 UTC (permalink / raw)
  To: gcc-help

Well, as written, gcc discards the whole asm statement as unneeded when 
using any optimization since none of the outputs (ie ret) get used and 
the statement is not volatile.

I believe that adding "m" (hello) as an input will resolve your other 
problem.  Note that you may not easily see the string in the asm output 
since gcc may encode this using something like this:

    movabsq $8022916924116329800, %rax
     movq    %rax, 32(%rsp)
     movl    $10, %eax
     movw    %ax, 44(%rsp)

Also, I'd probably write this statement as something more like this 
(untested):

     asm volatile
     (
         "syscall"
         : "=a" (ret)
         : "0" (1), "S" (hello), "d" (hello_size), "D" (1), "m" (hello)
         : "rcx", "r11", "memory", "cc"
     );

This lets gcc do as much of the work as possible, which generally 
produces better code.  And are you sure rcx and r11 get clobbered? Seems 
odd.

dw

On 10/5/2014 11:13 AM, Daniel Kamil Kozar wrote:
> #include <unistd.h>
>
> int main(void)
> {
>      const char hello[] = "Hello World!\n";
>      const size_t hello_size = sizeof(hello);
>      ssize_t ret;
>      asm
>      (
>          "movl $1, %%eax\n\t"
>          "movl $1, %%edi\n\t"
>          "movq %1, %%rsi\n\t"
>          "movl %2, %%edx\n\t"
>          "syscall"
>          : "=a"(ret)
>          : "g"(hello), "g"(hello_size)
>          : "%rdi", "%rsi", "%rdx", "%rcx", "%r11"
>      );
>      return 0;
> }
>
> Unfortunately, this snippet does not work anymore with gcc 4.9.1. An
> inspection of gcc's result when run with -S shows that the "hello"
> variable is not even created. Adding "static" to the variable's
> declaration fixes the issue, however I'm still wondering what's wrong
> with the original code and why gcc does not seem to see that the local
> variable is actually used by the asm block.
>
> Thanks,
> -dkk

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Local variables used inside an asm block are not recognized as used
@ 2014-10-05 22:16 Daniel Kamil Kozar
  0 siblings, 0 replies; 3+ messages in thread
From: Daniel Kamil Kozar @ 2014-10-05 22:16 UTC (permalink / raw)
  To: gcc-help

On 5 October 2014 23:08, David Wohlferd <dw@limegreensocks.com> wrote:
> Well, as written, gcc discards the whole asm statement as unneeded when
> using any optimization since none of the outputs (ie ret) get used and the
> statement is not volatile.
>
> I believe that adding "m" (hello) as an input will resolve your other
> problem.  Note that you may not easily see the string in the asm output
> since gcc may encode this using something like this:
>
>    movabsq $8022916924116329800, %rax
>     movq    %rax, 32(%rsp)
>     movl    $10, %eax
>     movw    %ax, 44(%rsp)
>
> Also, I'd probably write this statement as something more like this
> (untested):
>
>     asm volatile
>     (
>         "syscall"
>         : "=a" (ret)
>         : "0" (1), "S" (hello), "d" (hello_size), "D" (1), "m" (hello)
>         : "rcx", "r11", "memory", "cc"
>     );
>

Thanks a lot! This version is certainly much better.

> This lets gcc do as much of the work as possible, which generally produces
> better code.  And are you sure rcx and r11 get clobbered? Seems odd.

Agreed. I'm positive about rcx and r11, since the ABI for Linux amd64
systems specifies that "The kernel destroys
registers %rcx and %r11.". Thus, I guess it's safer to put them on the
clobber list.

>
> dw

Thanks again,
dkk

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-10-05 22:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-05 18:13 Local variables used inside an asm block are not recognized as used Daniel Kamil Kozar
2014-10-05 21:08 ` David Wohlferd
2014-10-05 22:16 Daniel Kamil Kozar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).