| |
 | Posted by Ali Çehreli in reply to Philippe Sigaud | Permalink Reply |
|
Ali Çehreli 
Posted in reply to Philippe Sigaud
| On 01/30/2013 11:08 AM, Philippe Sigaud wrote:
> On Wed, Jan 30, 2013 at 4:01 PM, Ali Çehreli<acehreli@yahoo.com> wrote:
>> A friend of mine is trying to figure out the D equivalent of using macros
>> with asm blocks in C:
>>
>> #define NEXT() __asm__("jmp *%0"::"r"((++ip)->jmp)); goto *ip->jmp
>>
>> D's asm blocks are very restrictive: mixins are not allowed. What do you do
>> in D?
>
> Is it possible to enclose an asm statement into a mixin?
>
> mixin("
> asm {
>
> ....
>
> code to include
>
> }");
Thanks!
It is almost there, except that one needs to use a compile-time "macro expansion" solution, which I am too lazy to try now. :) I am confident that the following program will work after doing that. (Of course also after replacing 'writeln' with 'mixin'.)
import std.stdio;
import std.regex;
string expandNexts(string input)
{
enum e = ctRegex!("Next;", "g");
// NOTE: This is an example. Of course, the second string below should be
// whatever the "macro" should be expanded as.
return replace(input, e, "cmp EAX,0;");
}
void main()
{
writeln (expandNexts(q{
asm
{
mov EAX, 10000;
Next;
Next;
Next;
Next;
Next;
}
}));
}
The program prints
asm
{
mov EAX, 10000;
cmp EAX,0;
cmp EAX,0;
cmp EAX,0;
cmp EAX,0;
cmp EAX,0;
}
Ali
|