-
Notifications
You must be signed in to change notification settings - Fork 0
Documentation
LDC has a few differences from the D specification and some extra features. These will be documented here.
The behavior of the output command line options may not be completely intuitive.
LDC looks for ldc.conf in the follow directories in order:
- current working directory
- ~/.ldc
- user home directory (Windows-only)
- install-prefix (Windows-only)
- install-prefix/etc (non-Windows)
- install-prefix/etc/ldc (non-Windows)
- /etc (non-Windows)
- /etc/ldc (non-Windows)
- next to binary
LDC uses a slightly different runtime interface compared to DMD, it has beginning documentation on its own page
LDC fixes some bugs that still exist in DMD. This can make code that works with DMD fail with LDC and vice versa.
Some parts of the D specification are hard or impossible to implement with LLVM, they should be listed here.
Almost everything works, but there are a few open issues. For instance the D spec isn't clear about at all is how asm blocks mixed with normal D code (for example code between two asm blocks) interacts.
Specific issues are:
(TBD)
In short, LLVM inline assembler is not allowed to control program flow outside of the asm blocks, see below for a bit more information.
For labels inside inline asm blocks, the D spec says "They can be the target of goto statements.", this is not supported at the moment. Basically, LLVM does not allow jumping in to or out of an asm block. We work around this for jumping out of asm by converting these branches to assignments to a temporary that is then used in a switch statement right after the inline asm block to jump to the final destination. This same workaround could be applied for jumping into inline assembly.
The D spec says that T[0] val;
does not require storage but has an address. LDC currently allocates one bit for such a construct.
The D spec only specifies an ABI for x86 processors on Windows and Linux. On other architectures and platforms LDC is free to do as it pleases, and does. However, on x86 the only parts of the ABI currently implemented is:
- the callee clears any parameters from the stack
- floating point values are returned on the x87 FPU stack
- reversing order of parameters
- returning delegates and dynamic arrays in EAX/EDX.
- passing last argument in EAX
- returning small structs in EAX
This is still a work in progress and will most likely improve a lot during the coming months. The LLVM developers have so far been very helpful in explaining what is needed to implement this ABI properly.
LDC support an LLVM specific variant of GCC's extended inline assembly expressions. See the inline assembly expressions page for more information.
Besides the predefined versions from the D 1.0 spec, LDC provides a few more.
- LLVM - always defined
- LLVM64 - compiling for a 64bit target
- LDC - always defined
- D_InlineAsm_X86 - compiling for x86-32 with inline asm support
- D_InlineAsm_X86_64 - compiling for x86-64 with inline asm support
- PPC - target is 32bit PowerPC
- PPC64 - target is 64bit PowerPC
- ARM - target is ARM
- Thumb - target is Thumb
- OSX - when compiling for Mac OS X
- FreeBSD - when compiling for FreeBSD
- Solaris - when compiling for Solaris
- Posix - target is POSIX-compliant
The version identifiers '''darwin, freebsd, solaris''' are also defined where appropriate for GDC compatibility.
LDC provides pragmas to access internal functions and can be used to tweak certain behaviour, they are also subject to change!
The intrinsic
pragma provides access to LLVM's built-in intrinsic functions. It requires a single string literal parameter with full name of the intrinsic. For example "llvm.sqrt.f32".
- It can only be used on function declarations or funtion template declarations.
- Any affected function declarations are not allowed to have bodies.
- The functions must translate to the same signature as the intrinsic.
- You may not take the address of intrinsics.
Example:
// provide square root intrinsics
pragma(intrinsic, "llvm.sqrt.f32")
float sqrt(float);
pragma(intrinsic, "llvm.sqrt.f64")
double sqrt(double);
pragma(intrinsic, "llvm.sqrt.f80")
real sqrt(real); // x86 only
Overloaded intrinsics can also be accessed more easily with a templated version instead, currently only one overloaded type is supported.
Example:
// templated atomic swap intrinsic
pragma(intrinsic, "llvm.atomic.swap.i#.p0i#")
T llvm_atomic_swap(T)(T* ptr, T val);
The # mark in the name is replaced with the size in bits of the type of the template parameter.
You can use this pragma to stop typeinfo from being implicitly generated for a declaration.
Example:
pragma(no_typeinfo)
{
struct Opaque {}
}
=== no_moduleinfo === You can use this pragma to stop moduleinfo from being implicitly generated for a declaration.
=== alloca === This pragma allows you to access the alloca instruction of LLVM directly. It only applies to function declarations and the final LLVM type for that declaration must be: '''i8* (i32/i64)'''. The size parameter will be truncated to '''i32''' if necessary.
Example:
pragma(alloca)
void* alloca(size_t);
Example:
alias void* va_list;
pragma(va_start)
void va_start(T)(va_list ap, ref T);
pragma(va_arg)
T va_arg(T)(va_list ap);
pragma(va_end)
void va_end(va_list args);
pragma(va_copy)
void va_copy(va_list dst, va_list src);
Use this pragma statement inside a non-naked function using inline asm. This will tell the optimizers that it is safe to inline this function.
Example:
int add(int a, int b) {
pragma(allow_inline);
asm { mov EAX, a; add EAX, b; }
}
These pragmas provide access to the LLVM vector instructions.
Example:
pragma(extractelement)
float llvm_extractelement(float4, int);
pragma(insertelement)
float4 llvm_insertelement(float4, float, int);
pragma(shufflevector)
float4 llvm_shufflevector(float4, float4, int, int, int, int);