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

Konstantin Tokarev annulen at yandex.ru
Fri Jun 24 02:48:28 PDT 2011



24.06.2011, 12:09, "Titus von Boxberg" <titus at v9g.de>:
> Am 23.06.2011 um 22:11 schrieb Douglas Gregor:
>
>>  On Jun 23, 2011, at 12:19 PM, Titus von Boxberg wrote:
>>>  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?
>>  Diagnosing ill-formed code within a template definition is a quality-of-implementation issue. Clang's philosophy is to diagnose everything that is allowed and reasonable. It's certainly reasonable to check the constraints, but our architecture makes it a bit hard to check the actual statement. That's a QoI issue that could conceivably be fixed in the future.
>>
>>  Besides, in the example you give, we're not even talking about a template definition: this is actual code that presumably only works in GCC because one of the inline functions isn't ever emitted. I don't think it makes any sense whatsoever to accept this code.
>
> Sorry to insist, and sorry if I was unclear.
> There is no "ill formed", "invalid" or whatsoever bad C++ code in my example.

+1, from C++ point of view assembly is just a const char * passed to built-in asm() call.

-- 
Regards,
Konstantin



More information about the cfe-dev mailing list