Inline assembly and CTFE

Joseph Cassman jc7919 at outlook.com
Thu Oct 3 15:25:26 PDT 2013


As of DMD 2.063.2 it looks to me like inline assembly language 
blocks are not usable in compile-time code. For example, the 
following code

   void main() {
     enum a = abc();
   }
   ulong abc() {
     asm { mov RAX,1; }
     return 1;
   }

will produce the error "Error: asm statements cannot be 
interpreted at compile time".

Are there plans to eventually support the use of asm blocks in 
CTFE?

If it were possible then I could use such a function in a static 
if test. What I am looking to do is use a function that calls 
CPUID (etc.) in a static if statement to determine which code to 
generate, somewhat like the following.

   bool isAVXPossible() { asm { ... use CPUID, etc. ... } }
   void foo() {
     static if(isAVXPossible()) {
       asm { ... generate AVX version ... }
     else {
       asm { ... generate non AVX version ... }
     }
   }

I cannot use version(D_SIMD) because I need some more specificity 
in the versioning. Similarly, version(X86) and version(X86_64) 
are convenient but I would like to be more exact. Also, I tried 
the functionality in core.cpuid but it also seems only able to 
execute at run-time. When trying to use it in a static if 
statement I get the error "Error: static variable xfeatures 
cannot be read at compile time".

Any ideas on how to implement isAVXPossible() and its related 
functionality without using a runtime test are appreciated.

Thanks

Joseph


More information about the Digitalmars-d mailing list