LDC inline assembler
Mike via digitalmars-d-ldc
digitalmars-d-ldc at puremagic.com
Tue May 5 22:35:16 PDT 2015
I'm trying to do a write a syscall in x86_64 Linux, but I'm
having trouble with the LDC inline assembler syntax. Here's my
cludgy implementation for DMD and GDC:
extern(C) c_int sys_write(c_int arg1, in void* arg2, c_int arg3)
nothrow
{
ssize_t result;
version(D_LP64)
{
version(DigitalMars)
{
asm nothrow
{
mov RAX, 1;
mov RDI, arg1;
mov RSI, arg2;
mov RDX, arg3;
syscall;
}
}
else version(GNU)
{
asm
{
"syscall" // make the request to the OS
: "=a" result // return result in rax ("a")
: "a" 1, // pass system call number in rax
("a")
"D" arg1, // pass arg1 in rdi ("D")
"S" arg2, // pass arg2 in rsi ("S")
"m" arg2, // arg2 is a pointer to memory
"d" arg3 // pass arg3 in rdx ("d")
: "memory", "cc", "rcx", "r11"; // announce to
the compiler that the memory and condition codes
// have been
modified, kernel may clopper rcx and r11
}
}
else
{
static assert(false, "sys_write only supports DMD and
GDC");
}
}
else
{
static assert(false, "sys_write only supports 64-bit");
}
// TODO: Not sure if this is right
return result;
}
I've read through
http://wiki.dlang.org/LDC_inline_assembly_expressions, but I'm
just not understanding the syntax. Could you translate the above
to LDC's syntax? If I can understand it, I'll document an
example on the wiki in return.
Thanks,
Mike
More information about the digitalmars-d-ldc
mailing list