2 * Handles target-specific parameters
4 * In order to allow for cross compilation, when the compiler produces a binary
5 * for a different platform than it is running on, target information needs
6 * to be abstracted. This is done in this module, primarily through `Target`.
9 * While DMD itself does not support cross-compilation, GDC and LDC do.
10 * Hence, this module is (sometimes heavily) modified by them,
11 * and contributors should review how their changes affect them.
14 * - $(LINK2 https://wiki.osdev.org/Target_Triplet, Target Triplets)
15 * - $(LINK2 https://github.com/ldc-developers/ldc, LDC repository)
16 * - $(LINK2 https://github.com/D-Programming-GDC/gcc, GDC repository)
18 * Copyright: Copyright (C) 1999-2021 by The D Language Foundation, All Rights Reserved
19 * Authors: $(LINK2 http://www.digitalmars.com, Walter Bright)
20 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
21 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/target.d, _target.d)
22 * Documentation: https://dlang.org/phobos/dmd_target.html
23 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/target.d
28 import dmd.globals : Param;
40 avx, // AVX1 instruction set
41 avx2, // AVX2 instruction set
42 avx512, // AVX-512 instruction set
44 // Special values that don't survive past the command line processing
45 baseline, // (default) the minimum capability CPU
46 native // the machine the compiler is being run on
49 ////////////////////////////////////////////////////////////////////////////////
51 * Describes a back-end target. At present it is incomplete, but in the future
52 * it should grow to contain most or all target machine and target O/S specific
55 * In many cases, calls to sizeof() can't be used directly for getting data type
56 * sizes since cross compiling is supported and would end up using the host
57 * sizes rather than the target sizes.
59 extern (C++) struct Target
61 import dmd.dscope : Scope;
62 import dmd.expression : Expression;
63 import dmd.func : FuncDeclaration;
64 import dmd.globals : LINK, Loc, d_int64;
65 import dmd.astenums : TY;
66 import dmd.mtype : Type, TypeFunction, TypeTuple;
67 import dmd.root.ctfloat : real_t;
68 import dmd.statement : Statement;
69 import dmd.tokens : EXP;
71 /// Bit decoding of the Target.OS
74 /* These are mutually exclusive; one and only one is set.
75 * Match spelling and casing of corresponding version identifiers
87 all = linux | Windows | OSX | OpenBSD | FreeBSD | Solaris | DragonFlyBSD,
88 Posix = linux | OSX | OpenBSD | FreeBSD | Solaris | DragonFlyBSD,
95 ubyte ptrsize; /// size of a pointer in bytes
96 ubyte realsize; /// size a real consumes in memory
97 ubyte realpad; /// padding added to the CPU real size to bring it up to realsize
98 ubyte realalignsize; /// alignment for reals
99 ubyte classinfosize; /// size of `ClassInfo`
100 ulong maxStaticDataSize; /// maximum size of static data
111 /// Architecture name
112 const(char)[] architectureName;
113 CPU cpu = CPU.baseline; // CPU instruction set to target
114 bool is64bit; // generate 64 bit code for x86_64; true by default for 64 bit dmd
115 bool isLP64; // pointers are 64 bits
118 const(char)[] obj_ext; /// extension for object files
119 const(char)[] lib_ext; /// extension for static library files
120 const(char)[] dll_ext; /// extension for dynamic library files
121 bool run_noext; /// allow -run sources without extensions
122 bool mscoff = false; // for Win32: write MsCoff object files instead of OMF
124 * Values representing all properties for floating point types
126 extern (C++) struct FPTypeProperties(T)
128 real_t max; /// largest representable value that's not infinity
129 real_t min_normal; /// smallest representable normalized value that's not 0
130 real_t nan; /// NaN value
131 real_t infinity; /// infinity value
132 real_t epsilon; /// smallest increment to the value 1
134 d_int64 dig; /// number of decimal digits of precision
135 d_int64 mant_dig; /// number of bits in mantissa
136 d_int64 max_exp; /// maximum int value such that 2$(SUPERSCRIPT `max_exp-1`) is representable
137 d_int64 min_exp; /// minimum int value such that 2$(SUPERSCRIPT `min_exp-1`) is representable as a normalized value
138 d_int64 max_10_exp; /// maximum int value such that 10$(SUPERSCRIPT `max_10_exp` is representable)
139 d_int64 min_10_exp; /// minimum int value such that 10$(SUPERSCRIPT `min_10_exp`) is representable as a normalized value
142 FPTypeProperties!float FloatProperties; ///
143 FPTypeProperties!double DoubleProperties; ///
144 FPTypeProperties!real_t RealProperties; ///
146 private Type tvalist; // cached lazy result of va_listType()
148 private const(Param)* params; // cached reference to global.params
151 * Initialize the Target
153 extern (C++) void _init(ref const Param params);
157 * Deinitializes the global state of the compiler.
159 * This can be used to restore the state set by `_init` to its original
168 * Requested target memory alignment size of the given type.
170 * type = type to inspect
174 extern (C++) uint alignsize(Type type);
177 * Requested target field alignment size of the given type.
179 * type = type to inspect
183 extern (C++) uint fieldalign(Type type);
186 * Type for the `va_list` type for the target; e.g., required for `_argptr`
188 * NOTE: For Posix/x86_64 this returns the type which will really
189 * be used for passing an argument of type va_list.
191 * `Type` that represents `va_list`.
193 extern (C++) Type va_listType(const ref Loc loc, Scope* sc);
196 * Checks whether the target supports a vector type.
198 * sz = vector type size in bytes
199 * type = vector element type
201 * 0 vector type is supported,
202 * 1 vector type is not supported on the target at all
203 * 2 vector element type is not supported
204 * 3 vector size is not supported
206 extern (C++) int isVectorTypeSupported(int sz, Type type);
209 * Checks whether the target supports the given operation for vectors.
211 * type = target type of operation
212 * op = the unary or binary op being done on the `type`
213 * t2 = type of second operand if `op` is a binary operation
215 * true if the operation is supported or type is not a vector
217 extern (C++) bool isVectorOpSupported(Type type, EXP op, Type t2 = null);
220 * Default system linkage for the target.
222 * `LINK` to use for `extern(System)`
224 extern (C++) LINK systemLinkage();
227 * Describes how an argument type is passed to a function on target.
229 * t = type to break down
231 * tuple of types if type is passed in one or more registers
232 * empty tuple if type is always passed on the stack
233 * null if the type is a `void` or argtypes aren't supported by the target
235 extern (C++) TypeTuple toArgTypes(Type t);
238 * Determine return style of function - whether in registers or
239 * through a hidden pointer to the caller's stack.
241 * tf = function type to check
242 * needsThis = true if the function type is for a non-static member function
244 * true if return value from function is on the stack
246 extern (C++) bool isReturnOnStack(TypeFunction tf, bool needsThis);
249 * Determine the size a value of type `t` will be when it
250 * is passed on the function parameter stack.
252 * loc = location to use for error messages
253 * t = type of parameter
255 * size used on parameter stack
257 extern (C++) ulong parameterSize(const ref Loc loc, Type t);
260 * Decides whether an `in` parameter of the specified POD type is to be
261 * passed by reference or by value. To be used with `-preview=in` only!
263 * t = type of the `in` parameter, must be a POD
265 * `true` if the `in` parameter is to be passed by reference
267 extern(C++) bool preferPassByRef(Type t);
270 * Get targetInfo by key
272 * name = name of targetInfo to get
273 * loc = location to use for error messages
275 * Expression for the requested targetInfo
277 extern (C++) Expression getTargetInfo(const(char)* name, const ref Loc loc);
281 * tf = type of function being called
282 * Returns: `true` if the callee invokes destructors for arguments.
284 extern (C++) bool isCalleeDestroyingArgs(TypeFunction tf);
287 * Returns true if the implementation for object monitors is always defined
288 * in the D runtime library (rt/monitor_.d).
290 * fd = function with `synchronized` storage class.
291 * fbody = entire function body of `fd`
293 * `false` if the target backend handles synchronizing monitors.
295 extern (C++) bool libraryObjectMonitors(FuncDeclaration fd, Statement fbody);
298 ////////////////////////////////////////////////////////////////////////////////
300 * Functions and variables specific to interfacing with extern(C) ABI.
317 enum BitFieldStyle : ubyte
320 DM, /// Digital Mars 32 bit C compiler
321 MS, /// Microsoft 32 and 64 bit C compilers
322 /// https://docs.microsoft.com/en-us/cpp/c-language/c-bit-fields?view=msvc-160
323 /// https://docs.microsoft.com/en-us/cpp/cpp/cpp-bit-fields?view=msvc-160
324 Gcc_Clang, /// gcc and clang
327 ubyte longsize; /// size of a C `long` or `unsigned long` type
328 ubyte long_doublesize; /// size of a C `long double`
329 ubyte wchar_tsize; /// size of a C `wchar_t` type
330 Runtime runtime; /// vendor of the C runtime to link against
331 BitFieldStyle bitFieldStyle; /// different C compilers do it differently
334 ////////////////////////////////////////////////////////////////////////////////
336 * Functions and variables specific to interface with extern(C++) ABI.
340 import dmd.dsymbol : Dsymbol;
341 import dmd.dclass : ClassDeclaration;
342 import dmd.func : FuncDeclaration;
343 import dmd.mtype : Parameter, Type;
354 bool reverseOverloads; /// set if overloaded functions are grouped and in reverse order (such as in dmc and cl)
355 bool exceptions; /// set if catching C++ exceptions is supported
356 bool twoDtorInVtable; /// target C++ ABI puts deleting and non-deleting destructor into vtable
357 bool wrapDtorInExternD; /// set if C++ dtors require a D wrapper to be callable from runtime
358 Runtime runtime; /// vendor of the C++ runtime to link against
361 * Mangle the given symbol for C++ ABI.
363 * s = declaration with C++ linkage
365 * string mangling of symbol
367 extern (C++) const(char)* toMangle(Dsymbol s);
370 * Get RTTI mangling of the given class declaration for C++ ABI.
372 * cd = class with C++ linkage
374 * string mangling of C++ typeinfo
376 extern (C++) const(char)* typeInfoMangle(ClassDeclaration cd);
379 * Get mangle name of a this-adjusting thunk to the given function
380 * declaration for C++ ABI.
382 * fd = function with C++ linkage
383 * offset = call offset to the vptr
385 * string mangling of C++ thunk, or null if unhandled
387 extern (C++) const(char)* thunkMangle(FuncDeclaration fd, int offset);
390 * Gets vendor-specific type mangling for C++ ABI.
392 * t = type to inspect
394 * string if type is mangled specially on target
397 extern (C++) const(char)* typeMangle(Type t);
400 * Get the type that will really be used for passing the given argument
401 * to an `extern(C++)` function.
403 * p = parameter to be passed.
405 * `Type` to use for parameter `p`.
407 extern (C++) Type parameterType(Parameter p);
410 * Checks whether type is a vendor-specific fundamental type.
412 * t = type to inspect
413 * isFundamental = where to store result
415 * true if isFundamental was set by function
417 extern (C++) bool fundamentalType(const Type t, ref bool isFundamental);
420 * Get the starting offset position for fields of an `extern(C++)` class
421 * that is derived from the given base class.
423 * baseClass = base class with C++ linkage
425 * starting offset to lay out derived class fields
427 extern (C++) uint derivedClassOffset(ClassDeclaration baseClass);
430 ////////////////////////////////////////////////////////////////////////////////
432 * Functions and variables specific to interface with extern(Objective-C) ABI.
436 bool supported; /// set if compiler can interface with Objective-C
439 ////////////////////////////////////////////////////////////////////////////////
440 extern (C++) __gshared Target target;