yidabu 2007-4-26 16:16
32 D 应用程序二进制接口 D Application Binary Interface
32 D 应用程序二进制接口 D Application Binary Interface知识若不分享 实在没有意义 http://www.d-programming-language-china.org 20070426点击下面网址查看原文:http://www.d-programming-language-china.org by: D语言论坛 http://www.d-programming-language-china.org from: http://www.digitalmars.com/d/abi.html version: 基于D 1.013 (Apr 19, 2007) 如果 D 实现符合 D ABI (应用程序二进制接口)的话,就可以生成可以同其他实现互操作的库、DLL等。 这份规范的大部分都处于 TBD(尚未完成)状态。C ABI 本规范中提到的 C ABI 指的是目标系统中的 C 应用程序二进制接口。C 和 D 代码应该能够自由地链接到一起,尤其是,D 代码应该能够访问所有的 C ABI 运行时库。Basic Types TBDStructs结构 符合目标平台上的 C ABI 结构分布。Classes类 一个对象的组成如下: offset contents 0 pointer to vtable ptrsize monitor ptrsize*2... non-static members vtable 的组成为:( 本文出处: http://www.d-programming-language-china.org ) offset contents 0 pointer to instance of ClassInfo ptrsize... pointers to virtual member functions 类定义: [Copy to clipboard] [ - ]CODE: class XXXX { .... }; 生成了: 一个叫做 ClassXXXX 的 Class 类的实例。 一个叫做 StaticClassXXXX 的定义所有静态成员的类型。 一个叫做 StaticXXXX 的 StaticClassXXXX 的实例,用于静态成员。Interfaces TBDArrays 动态数组的组成为: offset contents 0 array dimension size_t pointer to array data 动态数组的声明:( 本文出处: http://www.d-programming-language-china.org ) [Copy to clipboard] [ - ]CODE: type[] array; 静态数组的声明: [Copy to clipboard] [ - ]CODE: type[dimension] array; 因此,静态数组总是静态的将大小作为型别本身的一部分,所以它的实现跟 C 中的实现相同。静态数组和动态数组之间可以轻松的相互转换。Associative Arrays关联数组 Associative arrays consist of a pointer to an opaque, implementation defined type. The current implementation is contained in phobos/internal/aaA.d.Reference Types引用型别 D 有引用型别,但是它们是隐式的。例如,类总是通过引用访问的;这意味着类的实例决不会位于堆栈上或者作为函数参数被传递。 当给函数传递一个静态数组时,将会隐式的把它转化为静态数组的引用。例如: [Copy to clipboard] [ - ]CODE: int[3] abc; 当将 abc 传递给函数时,会造成下面的隐式转换:( 本文出处: http://www.d-programming-language-china.org ) [Copy to clipboard] [ - ]CODE: 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. QUOTE: MangledName: _D QualifiedName Type _D QualifiedName M Type QualifiedName: SymbolName SymbolName QualifiedName SymbolName: LName TemplateInstanceName The M means that the symbol is a function that requires a this pointer. Template Instance Names have the types and values of its parameters encoded into it: QUOTE: TemplateInstanceName: __T LName TemplateArgs Z TemplateArgs: TemplateArg TemplateArg TemplateArgs TemplateArg: T Type V Type Value S LName Value: n Number N Number e HexFloat c HexFloat c HexFloat A Number Value... HexFloat: NAN INF NINF N HexDigits P Exponent HexDigits P Exponent Exponent: N Number Number 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 HexFloat is for real and imaginary floating point literals. c HexFloat c HexFloat 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. A Number Value... An array literal. Value is repeated Number times. QUOTE: Name: Namestart Namestart Namechars Namestart: _ Alpha Namechar: Namestart Digit Namechars: Namechar Namechar Namechars A Name is a standard D identifier.( 本文出处: http://www.d-programming-language-china.org ) QUOTE: 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.Type Mangling Types are mangled using a simple linear scheme: QUOTE: Type: TypeArray TypeSarray TypeAarray TypePointer TypeFunction TypeIdent TypeClass TypeStruct TypeEnum TypeTypedef TypeDelegate TypeNone TypeVoid TypeByte TypeUbyte TypeShort TypeUshort TypeInt TypeUint TypeLong TypeUlong TypeFloat TypeDouble TypeReal TypeIfloat TypeIdouble TypeIreal TypeCfloat TypeCdouble TypeCreal TypeBool TypeChar TypeWchar TypeDchar TypeTuple TypeArray: A Type TypeSarray: G Number Type TypeAarray: H Type Type TypePointer: P Type TypeFunction: CallConvention Arguments ArgClose Type CallConvention: F U W V R Arguments: Argument Argument Arguments Argument: Type J Type K Type L Type ArgClose X Y Z TypeIdent: I LName TypeClass: C LName TypeStruct: S LName TypeEnum: E LName TypeTypedef: T LName TypeDelegate: D TypeFunction TypeNone: n TypeVoid: v TypeByte: g TypeUbyte: h TypeShort: s TypeUshort: t TypeInt: i TypeUint: k TypeLong: l TypeUlong: m TypeFloat: f TypeDouble: d TypeReal: e TypeIfloat: o TypeIdouble: p TypeIreal: j TypeCfloat: q TypeCdouble: r TypeCreal: c TypeBool: b TypeChar: a TypeWchar: u TypeDchar: w TypeTuple: B Number Arguments 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. 1 Register Conventions EAX, ECX, EDX are scratch registers and can be destroyed by a function. EBX, ESI, EDI, EBP must be preserved across function calls. EFLAGS is assumed destroyed across function calls, except for the direction flag which must be forward. The FPU stack must be empty when calling a function. The FPU control word must be preserved across function calls. Floating point return values are returned on the FPU stack. These must be cleaned off by the caller, even if they are not used. 2 Return Value The types bool, byte, ubyte, short, ushort, int, uint, pointer, Object, and interfaces are returned in EAX. long and ulong are returned in EDX,EAX, where EDX gets the most significant half. float, double, real, ifloat, idouble, ireal are returned in ST0. cfloat, cdouble, creal are returned in ST1,ST0 where ST1 is the real part and ST0 is the imaginary part. Dynamic arrays are returned with the pointer in EDX and the length in EAX. Associative arrays are returned in EAX with garbage returned in EDX. The EDX value will probably be removed in the future; it's there for backwards compatibility with an earlier implementation of AA's. Delegates are returned with the pointer to the function in EDX and the context pointer in EAX. For Windows, 1, 2 and 4 byte structs are returned in EAX. For Windows, 8 byte structs are returned in EDX,EAX, where EDX gets the most significant half. For other struct sizes, and for all structs on Linux, the return value is stored through a hidden pointer passed as an argument to the function. Constructors return the this pointer in EAX.( 本文出处: http://www.d-programming-language-china.org ) 3 Parameters The parameters to the non-variadic function: [Copy to clipboard] [ - ]CODE: foo(a1, a2, ..., an); are passed as follows: a1 a2 ... an hidden this( 本文出处: http://www.d-programming-language-china.org ) 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:( 本文出处: http://www.d-programming-language-china.org ) It fits in EAX. It is not a 3 byte struct. It is not a floating point type. 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 ref 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: [Copy to clipboard] [ - ]CODE: void foo(int p1, int p2, int[] p3...) foo(a1, a2, ..., an); are passed as follows: p1 p2 a3 hidden this( 本文出处: http://www.d-programming-language-china.org ) 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: [Copy to clipboard] [ - ]CODE: 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.( 本文出处: http://www.d-programming-language-china.org )Exception Handling异常处理 1 Windows Conforms to the Microsoft Windows Structured Exception Handling conventions. 2 Linux Uses static address range/handler tables. TBDGarbage Collection The interface to this is found in phobos/internal/gc.Runtime Helper Functions运行时辅助函数 These are found in phobos/internal.Module Initialization and Termination模块初始化和终止 TBDUnit Testing单元测试 TBDSymbolic Debugging D has types that are not represented in existing C or C++ debuggers. These are dynamic arrays, associative arrays, and delegates. Representing these types as structs causes problems because function calling conventions for structs are often different than that for these types, which causes C/C++ debuggers to misrepresent things. For these debuggers, they are represented as a C type which does match the calling conventions for the type. The dmd compiler will generate only C symbolic type info with the -gc compiler switch. Types for C Debuggers D type C representation dynamic array unsigned long long associative array void* delegate long long For debuggers that can be modified to accept new types, the following extensions help them fully support the types. Codeview Debugger Extensions D makes use of the Codeview OEM generic type record indicated by LF_OEM (0x0015). The format is: Codeview OEM Extensions for D field size 2 2 2 2 2 2 D Type Leaf Index OEM Identifier recOEM num indices type index type index dynamic array LF_OEM OEM 1 2 @index @element associative array LF_OEM OEM 2 2 @key @element delegate LF_OEM OEM 3 2 @this @function OEM 0x42 index type index of array index key type index of key element type index of array element this type index of context pointer function type index of function( 本文出处: http://www.d-programming-language-china.org ) These extensions can be pretty-printed by obj2asm. The Ddbg debugger supports them. Dwarf Debugger Extensions The following leaf types are added: Dwarf Extensions for D D type Identifier Value Format dynamic array DW_TAG_darray_type 0x41 DW_AT_type is element type associative array DW_TAG_aarray_type 0x42 DW_AT_type, is element type, DW_AT_containing_type key type delegate DW_TAG_delegate_type 0x43 DW_AT_type, is function type, DW_AT_containing_type is 'this' type These extensions can be pretty-printed by dumpobj. http://www.digitalmars.com/ctg/dumpobj.html( 本文出处: http://www.d-programming-language-china.org ) The ZeroBUGS debugger supports them. http://www.zerobugs.org/( lastupdate:20070426 最新文章请访问http://www.d-programming-language-china.org )关于一大步成功社区:yidabu提倡在交流中学习,在分享中提高收集感兴趣的知识,写下心得,通过网络与别人一起分享理解一点就实践一步,收获什么就分享什么,成功就是这样一点点一步步累积起来的网络只是一个工具,只有自己身心提高才是实实在在的。d-programming-language-china.org为大家提供一个学习交流各种知识的平台