public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Wohlferd <dw@LimeGreenSocks.com>
To: gcc-help@gcc.gnu.org
Subject: Re: Local variables used inside an asm block are not recognized as used
Date: Sun, 05 Oct 2014 21:08:00 -0000	[thread overview]
Message-ID: <5431B357.9080503@LimeGreenSocks.com> (raw)
In-Reply-To: <CANMdSOyzEQbm8mgXSYYug54-17Z2aLifiTGf_m9H8iciiM4qJg@mail.gmail.com>

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

  reply	other threads:[~2014-10-05 21:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-05 18:13 Daniel Kamil Kozar
2014-10-05 21:08 ` David Wohlferd [this message]
2014-10-05 22:16 Daniel Kamil Kozar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5431B357.9080503@LimeGreenSocks.com \
    --to=dw@limegreensocks.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).