https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64951

            Bug ID: 64951
           Summary: g++ generates incorrect code when using a register
                    variable within a template function
           Product: gcc
           Version: 4.9.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: inline-asm
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gdelu...@security-labs.org

Created attachment 34679
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=34679&action=edit
Test case for comparing register assignment in a normal function and a template
function

g++ doesn't generate the expected code when using a register variable bound to
a specific CPU register inside a template function.
For instance:

  template<typename T>
  inline void SetRdx(T value)
  {
      register auto __foo asm ("rdx") = value;
      asm volatile ( "" :: "r" (__foo) );
  }

Calling it with: SetRdx(0x42) will generate the following assembly code:

  mov    $0x42,%eax

instead of the expected:

  mov    $0x42,%edx

Moreover, specifying a non-existent CPU register inside the "asm" directive
still compiles and does not throw any error:

  register auto __foo asm ("foobar") = value; // compiles to mov $0x42, %eax

It looks as if the asm directive is completely ignored when used within a
template function.

This behavior has been reproduced on gcc 4.9.2, 4.8 and 4.7.

Reply via email to