System programming in D (Was: The God Language)

Iain Buclaw ibuclaw at ubuntu.com
Thu Jan 5 14:43:28 PST 2012


On 5 January 2012 22:16, Manu <turkeyman at gmail.com> wrote:
> On 6 January 2012 00:10, Iain Buclaw <ibuclaw at ubuntu.com> wrote:
>>
>> The reasoning behind is more so that you can write asm statements on
>> all architectures, not just x86. And with GDC being a frontend of GCC,
>> seems a natural thing to support (this has actually been in GDC since
>> 2004, so I'm not sure why you should through all arms up about it
>> now).
>
>
> When I was first reading about D I read that the inline assembler syntax is
> built in and standardised in the language... and I gave a large sigh of
> relief.
> If that's not the case, there are competing asm syntax in D, well... that
> sucks. Am I version-ing my asm blocks for DMD and GDC now like I have to in
> C for VC and GCC?
> Surely D should settle on just one... If that happens to be the GCC syntax
> for compatibility, great...?

For all its intentions, I think D-style syntax is great, however for
GDC, all lines need to be translated into GCC-equivalent syntax when
emitting AST.

Example - what ARM assembly would look like in D:

asm {
    cmp R0, R1;
    blt Lbmax;
    mov R2, R0;
    b Lrest;
Lbmax:
    mov R2, R1;
Lrest:
}

In order to compile *correctly*, we must be able to tell GCC what are
outputs, what are inputs, what are labels, and what gets clobbered.
The backend needs to know this to ensure syntax is correct, and it
doesn't try to do anything odd that may invalidate what you are trying
to do.  ie:  Output operands, the compiler can check this that outputs
are lvalue.  Clobbers, tells the backend that a register is not free
to use as a place to store temporary values.  Labels, tells the
backend that this asm block of code could jmp to a given location,
meaning it should be protected from the usual dead code elimination
passes.

For this to work, requires the frontend to be *aware* of what assembly
language it is compiling, and be able to parse it, understand it
correctly.  Which would not be the most pleasant of things to
implement granted the number of support architectures.  Converting x86
Intel syntax assembly to x86 GCC syntax assembly is enough for me. :)


-- 
Iain Buclaw

*(p < e ? p++ : p) = (c & 0x0f) + '0';


More information about the Digitalmars-d mailing list