www.digitalmars.com
Last update Wed Nov 15 02:42:23 2006

D Application Binary Interface

A D implementation that conforms to the D ABI (Application Binary Interface) will be able to generate libraries, DLL's, etc., that can interoperate with D binaries built by other implementations.

Most of this specification remains TBD (To Be Defined).

C ABI

The C ABI referred to in this specification means the C Application Binary Interface of the target system. C and D code should be freely linkable together, in particular, D code shall have access to the entire C ABI runtime library.

Basic Types

TBD

Structs

Conforms to the target's C ABI struct layout.

Classes

An object consists of:

offset contents
0 pointer to vtable
4 monitor
8... non-static members

The vtable consists of:

offset contents
0 pointer to instance of ClassInfo
4... pointers to virtual member functions

The class definition:

class XXXX
{
    ....
};
Generates the following:

Interfaces

TBD

Arrays

A dynamic array consists of:

offset contents
0 array dimension
4 pointer to array data

A dynamic array is declared as:

type array[];
whereas a static array is declared as:
type array[dimension];
Thus, a static array always has the dimension statically available as part of the type, and so it is implemented like in C. Static array's and Dynamic arrays can be easily converted back and forth to each other.

Associative Arrays

TBD

Reference Types

D has reference types, but they are implicit. For example, classes are always referred to by reference; this means that class instances can never reside on the stack or be passed as function parameters.

When passing a static array to a function, the result, although declared as a static array, will actually be a reference to a static array. For example:

int[3] abc;
Passing abc to functions results in these implicit conversions:
void func(int[3] array); // actually <reference to><array[3] of><int>
void func(int* p);       // abc is converted to a pointer
			 // to the first element
void func(int[] array);	 // abc is converted to a dynamic array

Name Mangling

D accomplishes typesafe linking by mangling a D identifier to include scope and type information.
MangledName:
    _D QualifiedName Type

QualifiedName:
    SymbolName
    SymbolName QualifiedName

SymbolName:
    LName
    TemplateInstanceName
Template Instance Names have the types and values of its parameters encoded into it:
TemplateInstanceName:
     __T LName TemplateArgs Z

TemplateArgs:
    TemplateArg
    TemplateArg TemplateArgs

TemplateArg:
    T Type
    V Type Value
    S LName

Value:
    n
    Number
    N Number
    e 20HexDigits
    c 20HexDigits 20HexDigits

HexDigits:
    HexDigit
    HexDigit HexDigits

HexDigit:
    Digit
    a
    b
    c
    d
    e
    f
n is for null arguments.
Number is for positive numeric literals (including character literals).
N Number is for negative numeric literals.
e 20HexDigits is for real and imaginary floating point literals.
c 20HexDigits 20HexDigits is for complex floating point literals.
Width Number _ HexDigits: Width is whether the characters are 1 byte (a), 2 bytes (w) or 4 bytes (d) in size. Number is the number of characters in the string. The HexDigits are the hex data for the string.
Name:
    Namestart
    Namestart Namechars

Namestart:
    _
    Alpha

Namechar:
    Namestart
    Digit

Namechars:
    Namechar
    Namechar Namechars
Type:
	TBD
A Name is a standard D identifier.
LName:
    Number Name

Number:
    Digit
    Digit Number

Digit:
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
An LName is a name preceded by a Number giving the number of characters in the Name.

Function Calling Conventions

The extern (C) calling convention matches the C calling convention used by the supported C compiler on the host system. The extern (D) calling convention for x86 is described here.

Register Conventions

Return Value

Parameters

The parameters to the non-variadic function:

	foo(a1, a2, ..., an);

are passed as follows:

a1
a2
...
an
hidden
this

where hidden is present if needed to return a struct value, and this is present if needed as the this pointer for a member function or the context pointer for a nested function.

The last parameter is passed in EAX rather than being pushed on the stack if the following conditions are met:

Parameters are always pushed as multiples of 4 bytes, rounding upwards, so the stack is always aligned on 4 byte boundaries. They are pushed most significant first. out and inout are passed as pointers. Static arrays are passed as pointers to their first element. On Windows, a real is pushed as a 10 byte quantity, a creal is pushed as a 20 byte quantity. On Linux, a real is pushed as a 12 byte quantity, a creal is pushed as two 12 byte quantities. The extra two bytes of pad occupy the 'most significant' position.

The callee cleans the stack.

The parameters to the variadic function:

	void foo(int p1, int p2, int[] p3...)
	foo(a1, a2, ..., an);

are passed as follows:

p1
p2
a3
hidden
this

The variadic part is converted to a dynamic array and the rest is the same as for non-variadic functions.

The parameters to the variadic function:

	void foo(int p1, int p2, ...)
	foo(a1, a2, a3, ..., an);

are passed as follows:

an
...
a3
a2
a1
_arguments
hidden
this

The caller is expected to clean the stack. _argptr is not passed, it is computed by the callee.

Exception Handling

Windows

Conforms to the Microsoft Windows Structured Exception Handling conventions. TBD

Linux

Uses static address range/handler tables. TBD

Garbage Collection

TBD

Runtime Helper Functions

TBD

Module Initialization and Termination

TBD

Unit Testing

TBD