Difference between revisions of "LDC inline assembly expressions"

From D Wiki
Jump to: navigation, search
(Add write system call example)
Line 71: Line 71:
 
   return __asm!int("movl $1, $0", "=a,*m", &dst);
 
   return __asm!int("movl $1, $0", "=a,*m", &dst);
 
}</source>
 
}</source>
 +
 +
== X86-64 ==
 +
 +
=== Examples ===
 +
 +
<source lang="d">
 +
// write system call
 +
ulong sys_write(long arg1, in void* arg2, long arg3) {
 +
    // The number of the syscall must be passed in rax (write = 1)
 +
    // Returning from the syscall rax contains the result
 +
    // The kernel clobbers rcx and r11
 +
    return __asm!ulong
 +
    (
 +
        "syscall",
 +
        "={rax}, {rax}, {rdi}, {rsi}, {rdx},
 +
        ~{rcx},~{r11}",     
 +
        1, arg1, arg2, arg3
 +
    );
 +
}
 +
</source>
 +
 +
See the [http://www.x86-64.org/documentation/abi.pdf X86-64 ABI specification]
  
 
== PPC 32 ==
 
== PPC 32 ==

Revision as of 13:29, 6 May 2015

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.

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);
}

X86-64

Examples

// write system call
ulong sys_write(long arg1, in void* arg2, long arg3) {
    // The number of the syscall must be passed in rax (write = 1)
    // Returning from the syscall rax contains the result
    // The kernel clobbers rcx and r11
    return __asm!ulong
    (
        "syscall", 
        "={rax}, {rax}, {rdi}, {rsi}, {rdx},
        ~{rcx},~{r11}",       
        1, arg1, arg2, arg3
    );
}

See the X86-64 ABI specification

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");
}