Conditional compilation inside asm and enum declarations

Don nospam at nospam.com
Tue Jul 14 01:11:20 PDT 2009


Julian Salazar wrote:
> Hi, I'm new here to the community but I've been using D for a while now, 
> and I have to say that it's a great programming language. I'd like to 
> get involved in this community and help shape this language.

Welcome!

> I'm just wondering about a minor issue: why are conditional blocks 
> invalid within expressions such as enum and asm? I mean, in trivial 
> cases it's fine, but in instances where code duplication is a big 
> maintainability nightmare, making conditional compilation more flexible 
> would have benefits for developers.
> 
> Something like (I know it's a trivial example, but you get the point):
> 
> asm {
>    version(x86) mov EAX, 1;
>    else version(x86_64) mov EAX, 2;
> }
> 
> would trigger an error. 

A much more convincing example is with position-independent code for 
Linux shared libraries.
In this case you may have a long function, with only a single 
instruction right in the middle which needs to be changed.
This example is taken from my bigint library. In the middle of a 50-line 
function is this single PIC-dependent instruction:

asm {
     :
     :
     adc ECX, EDX;
version (D_PIC) {} else { // the next trick can't be done in PIC mode.
       mov storagenop, EDX; // make #uops in loop a multiple of 3
}
     mul int ptr [ESP + 8];
     :
}

But I'd class this as a minor annoyance rather than a significant problem.



More information about the Digitalmars-d mailing list