# dmd.target

Handles `target`-specific parameters

#### Discussion

In order to allow for cross compilation, when the compiler produces a binary for a different platform than it is running on, `target` information needs to be abstracted. This is done in this module, primarily through `Target`.

Note: While DMD itself does not support cross-compilation, GDC and LDC do. Hence, this module is (sometimes heavily) modified by them, and contributors should review how their changes affect them.

#### See Also

- Target Triplets
- LDC repository
- GDC repository

#### Authors

Walter Bright

#### License

Boost License 1.0

Source: target.d

Documentation: https://dlang.org/phobos/dmd_target.html

Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/target.d

- `Target`

  #### Declaration

  `struct `Target`;`

  Describes a back-end target. At present it is incomplete, but in the future it should grow to contain most or all target machine and target O/S specific information.

  #### Discussion

  In many cases, calls to sizeof() can't be used directly for getting data type sizes since cross compiling is supported and would end up using the host sizes rather than the target sizes.

  - `OS`

    #### Declaration

    `enum `OS`: ubyte;`

    Bit decoding of the Target.`OS`

  - `ptrsize`

    #### Declaration

    `ubyte `ptrsize`;`

    size of a pointer in bytes

  - `realsize`

    #### Declaration

    `ubyte `realsize`;`

    size a real consumes in memory

  - `realpad`

    #### Declaration

    `ubyte `realpad`;`

    padding added to the CPU real size to bring it up to realsize

  - `realalignsize`

    #### Declaration

    `ubyte `realalignsize`;`

    alignment for reals

  - `classinfosize`

    #### Declaration

    `ubyte `classinfosize`;`

    size of `ClassInfo`

  - `maxStaticDataSize`

    #### Declaration

    `ulong `maxStaticDataSize`;`

    maximum size of static data

  - `c`

    #### Declaration

    `TargetC `c`;`

    C ABI

  - `cpp`

    #### Declaration

    `TargetCPP `cpp`;`

    C++ ABI

  - `objc`

    #### Declaration

    `TargetObjC `objc`;`

    Objective-C ABI

  - `architectureName`

    #### Declaration

    `const(char)[] `architectureName`;`

    Architecture name

  - `obj_ext`

    #### Declaration

    `const(char)[] `obj_ext`;`

    extension for object files

  - `lib_ext`

    #### Declaration

    `const(char)[] `lib_ext`;`

    extension for static library files

  - `dll_ext`

    #### Declaration

    `const(char)[] `dll_ext`;`

    extension for dynamic library files

  - `run_noext`

    #### Declaration

    `bool `run_noext`;`

    allow -run sources without extensions

  - `FPTypeProperties`

    #### Declaration

    `struct `FPTypeProperties`(T);`

    Values representing all properties for floating point types

    - `max`

      #### Declaration

      `real_t `max`;`

      largest representable value that's not infinity

    - `min_normal`

      #### Declaration

      `real_t `min_normal`;`

      smallest representable normalized value that's not 0

    - `nan`

      #### Declaration

      `real_t `nan`;`

      NaN value

    - `infinity`

      #### Declaration

      `real_t `infinity`;`

      `infinity` value

    - `epsilon`

      #### Declaration

      `real_t `epsilon`;`

      smallest increment to the value 1

    - `dig`

      #### Declaration

      `d_int64 `dig`;`

      number of decimal digits of precision

    - `mant_dig`

      #### Declaration

      `d_int64 `mant_dig`;`

      number of bits in mantissa

    - `max_exp`

      #### Declaration

      `d_int64 `max_exp`;`

      maximum int value such that 2 is representable

    - `min_exp`

      #### Declaration

      `d_int64 `min_exp`;`

      minimum int value such that 2 is representable as a normalized value

    - `max_10_exp`

      #### Declaration

      `d_int64 `max_10_exp`;`

      maximum int value such that 10

    - `min_10_exp`

      #### Declaration

      `d_int64 `min_10_exp`;`

      minimum int value such that 10 is representable as a normalized value

  - `FloatProperties`

    #### Declaration

    `FPTypeProperties!float `FloatProperties`;`

  - `DoubleProperties`

    #### Declaration

    `FPTypeProperties!double `DoubleProperties`;`

  - `RealProperties`

    #### Declaration

    `FPTypeProperties!real_t `RealProperties`;`

  - `_init`

    #### Declaration

    `void `_init`(const ref Param `params`);`

    Initialize the Target

  - `setCPU`

    #### Declaration

    `void `setCPU`();`

    Determine the instruction set to be used

  - `addPredefinedGlobalIdentifiers`

    #### Declaration

    `const void `addPredefinedGlobalIdentifiers`();`

    Add predefined global identifiers that are determied by the target

  - `deinitialize`

    #### Declaration

    `void `deinitialize`();`

    Deinitializes the global state of the compiler.

    #### Discussion

    This can be used to restore the state set by `_init` to its original state.

  - `alignsize`

    #### Declaration

    `uint `alignsize`(Type `type`);`

    Requested target memory alignment size of the given `type`.

    #### Parameters

    `Type `type`` | `type` to inspect

    #### Return Value

    alignment in bytes

  - `fieldalign`

    #### Declaration

    `uint `fieldalign`(Type `type`);`

    Requested target field alignment size of the given `type`.

    #### Parameters

    `Type `type`` | `type` to inspect

    #### Return Value

    alignment in bytes

  - `va_listType`

    #### Declaration

    `Type `va_listType`(const ref Loc `loc`, Scope* `sc`);`

    Type for the `va_list` type for the target; e.g., required for `_argptr` declarations.

    NOTE: For Posix/x86_64 this returns the type which will really be used for passing an argument of type va_list.

    #### Return Value

    `Type` that represents `va_list`.

  - `isVectorTypeSupported`

    #### Declaration

    `int `isVectorTypeSupported`(int `sz`, Type `type`);`

    Checks whether the target supports a vector `type`.

    #### Parameters

    `int `sz`` | vector `type` size in bytes
    `Type `type`` | vector element `type`

    #### Return Value

    0 vector `type` is supported, 1 vector `type` is not supported on the target at all 2 vector element `type` is not supported 3 vector size is not supported

  - `isVectorOpSupported`

    #### Declaration

    `bool `isVectorOpSupported`(Type `type`, uint `op`, Type `t2` = null);`

    Checks whether the target supports the given operation for vectors.

    #### Parameters

    `Type `type`` | target `type` of operation
    `uint `op`` | the unary or binary `op` being done on the ``type``
    `Type `t2`` | `type` of second operand if ``op`` is a binary operation

    #### Return Value

    `true` if the operation is supported or `type` is not a vector

  - `systemLinkage`

    #### Declaration

    `LINK `systemLinkage`();`

    Default system linkage for the target.

    #### Return Value

    `LINK` to use for `extern(System)`

  - `toArgTypes`

    #### Declaration

    `TypeTuple `toArgTypes`(Type `t`);`

    Describes how an argument type is passed to a function on target.

    #### Parameters

    `Type `t`` | type to break down

    #### Return Value

    tuple of types if type is passed in one or more registers empty tuple if type is always passed on the stack `null` if the type is a `void` or argtypes aren'`t` supported by the target

  - `isReturnOnStack`

    #### Declaration

    `bool `isReturnOnStack`(TypeFunction `tf`, bool `needsThis`);`

    Determine return style of function - whether in registers or through a hidden pointer to the caller's stack.

    #### Parameters

    `TypeFunction `tf`` | function type to check
    `bool `needsThis`` | `true` if the function type is for a non-static member function

    #### Return Value

    `true` if return value from function is on the stack

  - `parameterSize`

    #### Declaration

    `ulong `parameterSize`(const ref Loc `loc`, Type `t`);`

    Determine the size a value of type ``t`` will be when it is passed on the function parameter stack.

    #### Parameters

    `Loc `loc`` | location to use for error messages
    `Type `t`` | type of parameter

    #### Return Value

    size used on parameter stack

  - `preferPassByRef`

    #### Declaration

    `bool `preferPassByRef`(Type `t`);`

    Decides whether an `in` parameter of the specified POD type is to be passed by reference or by value. To be used with `-preview=in` only!

    #### Parameters

    `Type `t`` | type of the `in` parameter, must be a POD

    #### Return Value

    `true` if the `in` parameter is to be passed by reference

  - `getTargetInfo`

    #### Declaration

    `Expression `getTargetInfo`(const(char)* `name`, const ref Loc `loc`);`

    Get targetInfo by key

    #### Parameters

    `const(char)* `name`` | `name` of targetInfo to get
    `Loc `loc`` | location to use for error messages

    #### Return Value

    Expression for the requested targetInfo

  - `isCalleeDestroyingArgs`

    #### Declaration

    `bool `isCalleeDestroyingArgs`(TypeFunction `tf`);`

    #### Parameters

    `TypeFunction `tf`` | type of function being called

    #### Return Value

    `true` if the callee invokes destructors for arguments.

  - `libraryObjectMonitors`

    #### Declaration

    `bool `libraryObjectMonitors`(FuncDeclaration `fd`, Statement `fbody`);`

    Returns `true` if the implementation for object monitors is always defined in the D runtime library (rt/monitor_.d).

    #### Parameters

    `FuncDeclaration `fd`` | function with `synchronized` storage class.
    `Statement `fbody`` | entire function body of ``fd``

    #### Return Value

    `false` if the target backend handles synchronizing monitors.

  - `isXmmSupported`

    #### Declaration

    `bool `isXmmSupported`();`

    #### Return Value

    `true` if xmm usage is supported

  - `isPOSIX`

    #### Declaration

    `const nothrow @nogc @property scope bool `isPOSIX`();`

    #### Return Value

    `true` if generating code for POSIX

- `TargetC`

  #### Declaration

  `struct `TargetC`;`

  Functions and variables specific to interfacing with extern(C) ABI.

  - `longsize`

    #### Declaration

    `ubyte `longsize`;`

    size of a C `long` or `unsigned long` type

  - `long_doublesize`

    #### Declaration

    `ubyte `long_doublesize`;`

    size of a C `long double`

  - `wchar_tsize`

    #### Declaration

    `ubyte `wchar_tsize`;`

    size of a C `wchar_t` type

  - `runtime`

    #### Declaration

    `Runtime `runtime`;`

    vendor of the C `runtime` to link against

  - `bitFieldStyle`

    #### Declaration

    `BitFieldStyle `bitFieldStyle`;`

    different C compilers do it differently

- `TargetCPP`

  #### Declaration

  `struct `TargetCPP`;`

  Functions and variables specific to interface with extern(C++) ABI.

  - `reverseOverloads`

    #### Declaration

    `bool `reverseOverloads`;`

    set if overloaded functions are grouped and in reverse order (such as in dmc and cl)

  - `exceptions`

    #### Declaration

    `bool `exceptions`;`

    set if catching C++ `exceptions` is supported

  - `twoDtorInVtable`

    #### Declaration

    `bool `twoDtorInVtable`;`

    target C++ ABI puts deleting and non-deleting destructor into vtable

  - `wrapDtorInExternD`

    #### Declaration

    `bool `wrapDtorInExternD`;`

    set if C++ dtors require a D wrapper to be callable from runtime

  - `runtime`

    #### Declaration

    `Runtime `runtime`;`

    vendor of the C++ `runtime` to link against

  - `toMangle`

    #### Declaration

    `const(char)* `toMangle`(Dsymbol `s`);`

    Mangle the given symbol for C++ ABI.

    #### Parameters

    `Dsymbol `s`` | declaration with C++ linkage

    #### Return Value

    string mangling of symbol

  - `typeInfoMangle`

    #### Declaration

    `const(char)* `typeInfoMangle`(ClassDeclaration `cd`);`

    Get RTTI mangling of the given class declaration for C++ ABI.

    #### Parameters

    `ClassDeclaration `cd`` | class with C++ linkage

    #### Return Value

    string mangling of C++ typeinfo

  - `thunkMangle`

    #### Declaration

    `const(char)* `thunkMangle`(FuncDeclaration `fd`, int `offset`);`

    Get mangle name of a this-adjusting thunk to the given function declaration for C++ ABI.

    #### Parameters

    `FuncDeclaration `fd`` | function with C++ linkage
    `int `offset`` | call `offset` to the vptr

    #### Return Value

    string mangling of C++ thunk, or `null` if unhandled

  - `typeMangle`

    #### Declaration

    `const(char)* `typeMangle`(Type `t`);`

    Gets vendor-specific type mangling for C++ ABI.

    #### Parameters

    `Type `t`` | type to inspect

    #### Return Value

    string if type is mangled specially on target `null` if unhandled

  - `parameterType`

    #### Declaration

    `Type `parameterType`(Parameter `p`);`

    Get the type that will really be used for passing the given argument to an `extern(C++)` function.

    #### Parameters

    `Parameter `p`` | parameter to be passed.

    #### Return Value

    `Type` to use for parameter ``p``.

  - `fundamentalType`

    #### Declaration

    `bool `fundamentalType`(const Type `t`, ref bool `isFundamental`);`

    Checks whether type is a vendor-specific fundamental type.

    #### Parameters

    `Type `t`` | type to inspect
    `bool `isFundamental`` | where to store result

    #### Return Value

    `true` if `isFundamental` was set by function

  - `derivedClassOffset`

    #### Declaration

    `uint `derivedClassOffset`(ClassDeclaration `baseClass`);`

    Get the starting offset position for fields of an `extern(C++)` class that is derived from the given base class.

    #### Parameters

    `ClassDeclaration `baseClass`` | base class with C++ linkage

    #### Return Value

    starting offset to lay out derived class fields

- `TargetObjC`

  #### Declaration

  `struct `TargetObjC`;`

  Functions and variables specific to interface with extern(Objective-C) ABI.

  - `supported`

    #### Declaration

    `bool `supported`;`

    set if compiler can interface with Objective-C

- `Triple`

  #### Declaration

  `struct `Triple`;`

  Sets CPU Operating System, and optionally C/C++ runtime environment from the given triple e.g. x86_64+avx2-apple-darwin20.3.0 x86-unknown-linux-musl-clang x64-windows-msvc x64-pc-windows-msvc

- `target`

  #### Declaration

  `Target `target`;`