[cfe-dev] time of inline assembler evaluation in template specialization

Titus von Boxberg titus at v9g.de
Wed Jun 22 23:52:35 PDT 2011


I tried to use template specialization for selecting
the right inline assembly code.

Example:
-----------------
template<int CPU>
struct A {
};

template<>
struct A<8> {
  void f(void) {
    asm("ldr r4,=1\n\t" ::: "r4");
  }
};

template<>
struct A<1> {
  void f(void) {
    asm("movl $1, %%eax\n\t" ::: "eax");
  }
};


int main(void)
{
 A<1>	a;
 a.f();
}
---------------
clang says:

error: unknown register name 'r4' in asm
   asm("ldr r4,=1\n\t" ::: "r4");

g++ compiles the code, both for arm (A<8>) and x86.

Even if the register name r4 in the clobber list would really
be invalid, I'd expect that the compiler would only analyze
the syntactical correctness of the asm statement itself while
parsing the template.
Otherwise this application of template specialization cannot work.

Is this assumption correct?

Apparently the check being performed is only for the clobber list.
When I remove this list, the code behaves as expected:
clang compiles it for A<1>, but rejects A<8>
(message "error: unknown token in expression").

What makes the clobber list special, and what is the intention
of clang analyzing it so early?

Regards,
Titus



More information about the cfe-dev mailing list