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

Titus von Boxberg titus at v9g.de
Thu Jun 23 12:19:18 PDT 2011


Am 23.06.2011 um 18:45 schrieb John McCall:

> On Jun 22, 2011, at 11:52 PM, Titus von Boxberg wrote:
>> 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?
> 
> No.  A program is ill-formed, no diagnostic required, if it contains
> a template specialization with no valid instantiations.  You cannot
> rely on (non-dependent) code in dead template specializations
> to not be checked for validity.

Clear, but elegantly bypassing my question ;-)
What is - in this context - validity, then?
Why is it not the mere syntactical correctness of the asm statement?
asm statements are a nonstandard extension, anyway; so clang is free
to do it this or the other way.
If clang would handle it like gcc the combination of template specialization
and assembly language would work; and I'd recommend to follow gcc
here since there is no other standard.

Since you cut away my second question:
clang already behaves inconsistently (from the user perspective)
because it analyzes the register lists but not the statement itself.
That this might be (don't know if it can be with clang) explained
by the different stages of the compiler / assembler is no excuse.

And this behaviour is also present when the assembly code is dependent on
some type parameter.

What do you think?

Regards
Titus




More information about the cfe-dev mailing list