LDC inline assembly expressions
LDC supports an LLVM-specific variant of GCC's extended inline assembly expressions. They are useful on platforms where the D asm statement is not yet available (i.e. non-x86), or when the limitations or it being a statement are problematic. Being an expression, extended inline expressions are able to return values!
Additionally issues regarding inlining of function containing inline asm are mostly not relevant for extended inline assembly expressions. Effectively, extended inline assembly expression can be used to efficiently implement new intrinsics in the compiler.
Contents
Interface
To use them you must import the module containing the magic declarations:
import ldc.llvmasm;
Three different forms exist:
No return value:
void __asm (char[] asmcode, char[] constraints, [ Arguments... ] );
Single return value:
template __asm(T) {
T __asm (char[] asmcode, char[] constraints, [ Arguments... ] );
}
Multiple return values:
struct __asmtuple_t(T...) {
T v;
}
template __asmtuple(T...) {
__asmtuple_t!(T) __asmtuple (char[] asmcode, char[] constraints, [ Arguments... ] );
}
In all cases the constraint list must match the return type and arguments.
Constraints is a comma seperated list of outputs, inputs and clobbers.
Output constraints must come first, then input constraints, then finally clobbers.
Common output constraints:
- =*m == memory output
- =r == general purpose register output
Common input constraints:
- *m == memory input
- r == general purpose register input
- i == immediate value input
Common clobbers:
- ~{memory} == clobbers memory
X86-32
X86-32 specific constraints
- a or {ax} or {eax} == EAX
- b or {bx} or {ebx} == EBX
- c or {cx} or {ecx} == ECX
- d or {dx} or {edx} == EDX
- A == EAX:EDX
- {flags} == EFLAGS
- {st} == ST(0)
- {st(N)} == ST(N)
- {fpsw} == floating point status word
Examples
// store val into dst
void store(ref int dst, int val) {
__asm("movl $1, $0", "=*m,r", &dst, val);
}
// load dst into EAX and return it
int load(ref int dst) {
return __asm!int("movl $1, $0", "=a,*m", &dst);
}
PPC 32
- {cc} == condition code register
Examples
// store val into dst, clobbering r4
void store(ref int dst, int val) {
__asm("ldw 4, $1 ; stw 4, $0", "=*m,r,~{r4}", &dst, val);
}
PPC 64
- {cc} == condition code register
Examples
// returning the floating point status and control register
uint getFPSCR()
{
double fspr = __asm!double("mffs 0", "={f0}");
return cast(uint) *cast(ulong*) &fspr;
}
MIPS 64
MIPS assembly languages uses $ to denote registers. You have to quote them with a second $.
Examples
// returning stack pointer
void* getStackTop()
{
return __asm!(void *)("move $0, $$sp", "=r");
}