d: Merge upstream dmd 568496d5b, druntime 178c44ff, phobos 574bf883b.
[official-gcc.git] / gcc / d / dmd / target.d
blob16739addf57355b2a5b70f52f2638bd4040bae27
1 /**
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`.
8 * Note:
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.
13 * See_Also:
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
26 module dmd.target;
28 import dmd.globals : Param;
30 enum CPU
32 x87,
33 mmx,
34 sse,
35 sse2,
36 sse3,
37 ssse3,
38 sse4_1,
39 sse4_2,
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 ////////////////////////////////////////////////////////////////////////////////
50 /**
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
53 * information.
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;
70 /// Bit decoding of the Target.OS
71 enum OS : ubyte
73 /* These are mutually exclusive; one and only one is set.
74 * Match spelling and casing of corresponding version identifiers
76 Freestanding = 0,
77 linux = 1,
78 Windows = 2,
79 OSX = 4,
80 OpenBSD = 8,
81 FreeBSD = 0x10,
82 Solaris = 0x20,
83 DragonFlyBSD = 0x40,
85 // Combination masks
86 all = linux | Windows | OSX | OpenBSD | FreeBSD | Solaris | DragonFlyBSD,
87 Posix = linux | OSX | OpenBSD | FreeBSD | Solaris | DragonFlyBSD,
90 OS os;
91 ubyte osMajor;
93 // D ABI
94 ubyte ptrsize; /// size of a pointer in bytes
95 ubyte realsize; /// size a real consumes in memory
96 ubyte realpad; /// padding added to the CPU real size to bring it up to realsize
97 ubyte realalignsize; /// alignment for reals
98 ubyte classinfosize; /// size of `ClassInfo`
99 ulong maxStaticDataSize; /// maximum size of static data
101 /// C ABI
102 TargetC c;
104 /// C++ ABI
105 TargetCPP cpp;
107 /// Objective-C ABI
108 TargetObjC objc;
110 /// Architecture name
111 const(char)[] architectureName;
112 CPU cpu = CPU.baseline; // CPU instruction set to target
113 bool is64bit; // generate 64 bit code for x86_64; true by default for 64 bit dmd
114 bool isLP64; // pointers are 64 bits
116 // Environmental
117 const(char)[] obj_ext; /// extension for object files
118 const(char)[] lib_ext; /// extension for static library files
119 const(char)[] dll_ext; /// extension for dynamic library files
120 bool run_noext; /// allow -run sources without extensions
121 bool mscoff = false; // for Win32: write MsCoff object files instead of OMF
123 * Values representing all properties for floating point types
125 extern (C++) struct FPTypeProperties(T)
127 real_t max; /// largest representable value that's not infinity
128 real_t min_normal; /// smallest representable normalized value that's not 0
129 real_t nan; /// NaN value
130 real_t infinity; /// infinity value
131 real_t epsilon; /// smallest increment to the value 1
133 d_int64 dig; /// number of decimal digits of precision
134 d_int64 mant_dig; /// number of bits in mantissa
135 d_int64 max_exp; /// maximum int value such that 2$(SUPERSCRIPT `max_exp-1`) is representable
136 d_int64 min_exp; /// minimum int value such that 2$(SUPERSCRIPT `min_exp-1`) is representable as a normalized value
137 d_int64 max_10_exp; /// maximum int value such that 10$(SUPERSCRIPT `max_10_exp` is representable)
138 d_int64 min_10_exp; /// minimum int value such that 10$(SUPERSCRIPT `min_10_exp`) is representable as a normalized value
141 FPTypeProperties!float FloatProperties; ///
142 FPTypeProperties!double DoubleProperties; ///
143 FPTypeProperties!real_t RealProperties; ///
145 private Type tvalist; // cached lazy result of va_listType()
147 private const(Param)* params; // cached reference to global.params
150 * Initialize the Target
152 extern (C++) void _init(ref const Param params);
156 * Deinitializes the global state of the compiler.
158 * This can be used to restore the state set by `_init` to its original
159 * state.
161 void deinitialize()
163 this = this.init;
167 * Requested target memory alignment size of the given type.
168 * Params:
169 * type = type to inspect
170 * Returns:
171 * alignment in bytes
173 extern (C++) uint alignsize(Type type);
176 * Requested target field alignment size of the given type.
177 * Params:
178 * type = type to inspect
179 * Returns:
180 * alignment in bytes
182 extern (C++) uint fieldalign(Type type);
185 * Type for the `va_list` type for the target; e.g., required for `_argptr`
186 * declarations.
187 * NOTE: For Posix/x86_64 this returns the type which will really
188 * be used for passing an argument of type va_list.
189 * Returns:
190 * `Type` that represents `va_list`.
192 extern (C++) Type va_listType(const ref Loc loc, Scope* sc);
195 * Checks whether the target supports a vector type.
196 * Params:
197 * sz = vector type size in bytes
198 * type = vector element type
199 * Returns:
200 * 0 vector type is supported,
201 * 1 vector type is not supported on the target at all
202 * 2 vector element type is not supported
203 * 3 vector size is not supported
205 extern (C++) int isVectorTypeSupported(int sz, Type type);
208 * Checks whether the target supports the given operation for vectors.
209 * Params:
210 * type = target type of operation
211 * op = the unary or binary op being done on the `type`
212 * t2 = type of second operand if `op` is a binary operation
213 * Returns:
214 * true if the operation is supported or type is not a vector
216 extern (C++) bool isVectorOpSupported(Type type, uint op, Type t2 = null);
219 * Default system linkage for the target.
220 * Returns:
221 * `LINK` to use for `extern(System)`
223 extern (C++) LINK systemLinkage();
226 * Describes how an argument type is passed to a function on target.
227 * Params:
228 * t = type to break down
229 * Returns:
230 * tuple of types if type is passed in one or more registers
231 * empty tuple if type is always passed on the stack
232 * null if the type is a `void` or argtypes aren't supported by the target
234 extern (C++) TypeTuple toArgTypes(Type t);
237 * Determine return style of function - whether in registers or
238 * through a hidden pointer to the caller's stack.
239 * Params:
240 * tf = function type to check
241 * needsThis = true if the function type is for a non-static member function
242 * Returns:
243 * true if return value from function is on the stack
245 extern (C++) bool isReturnOnStack(TypeFunction tf, bool needsThis);
247 /***
248 * Determine the size a value of type `t` will be when it
249 * is passed on the function parameter stack.
250 * Params:
251 * loc = location to use for error messages
252 * t = type of parameter
253 * Returns:
254 * size used on parameter stack
256 extern (C++) ulong parameterSize(const ref Loc loc, Type t);
259 * Decides whether an `in` parameter of the specified POD type is to be
260 * passed by reference or by value. To be used with `-preview=in` only!
261 * Params:
262 * t = type of the `in` parameter, must be a POD
263 * Returns:
264 * `true` if the `in` parameter is to be passed by reference
266 extern(C++) bool preferPassByRef(Type t);
269 * Get targetInfo by key
270 * Params:
271 * name = name of targetInfo to get
272 * loc = location to use for error messages
273 * Returns:
274 * Expression for the requested targetInfo
276 extern (C++) Expression getTargetInfo(const(char)* name, const ref Loc loc);
279 * Params:
280 * tf = type of function being called
281 * Returns: `true` if the callee invokes destructors for arguments.
283 extern (C++) bool isCalleeDestroyingArgs(TypeFunction tf);
286 * Returns true if the implementation for object monitors is always defined
287 * in the D runtime library (rt/monitor_.d).
288 * Params:
289 * fd = function with `synchronized` storage class.
290 * fbody = entire function body of `fd`
291 * Returns:
292 * `false` if the target backend handles synchronizing monitors.
294 extern (C++) bool libraryObjectMonitors(FuncDeclaration fd, Statement fbody);
297 ////////////////////////////////////////////////////////////////////////////////
299 * Functions and variables specific to interfacing with extern(C) ABI.
301 struct TargetC
303 enum Runtime : ubyte
305 Unspecified,
306 Bionic,
307 DigitalMars,
308 Glibc,
309 Microsoft,
310 Musl,
311 Newlib,
312 UClibc,
313 WASI,
316 enum BitFieldStyle : ubyte
318 Unspecified,
319 DM, /// Digital Mars 32 bit C compiler
320 MS, /// Microsoft 32 and 64 bit C compilers
321 /// https://docs.microsoft.com/en-us/cpp/c-language/c-bit-fields?view=msvc-160
322 /// https://docs.microsoft.com/en-us/cpp/cpp/cpp-bit-fields?view=msvc-160
323 Gcc_Clang, /// gcc and clang
326 ubyte longsize; /// size of a C `long` or `unsigned long` type
327 ubyte long_doublesize; /// size of a C `long double`
328 ubyte wchar_tsize; /// size of a C `wchar_t` type
329 Runtime runtime; /// vendor of the C runtime to link against
330 BitFieldStyle bitFieldStyle; /// different C compilers do it differently
333 ////////////////////////////////////////////////////////////////////////////////
335 * Functions and variables specific to interface with extern(C++) ABI.
337 struct TargetCPP
339 import dmd.dsymbol : Dsymbol;
340 import dmd.dclass : ClassDeclaration;
341 import dmd.func : FuncDeclaration;
342 import dmd.mtype : Parameter, Type;
344 enum Runtime : ubyte
346 Unspecified,
347 Clang,
348 DigitalMars,
349 Gcc,
350 Microsoft,
353 bool reverseOverloads; /// set if overloaded functions are grouped and in reverse order (such as in dmc and cl)
354 bool exceptions; /// set if catching C++ exceptions is supported
355 bool twoDtorInVtable; /// target C++ ABI puts deleting and non-deleting destructor into vtable
356 bool wrapDtorInExternD; /// set if C++ dtors require a D wrapper to be callable from runtime
357 Runtime runtime; /// vendor of the C++ runtime to link against
360 * Mangle the given symbol for C++ ABI.
361 * Params:
362 * s = declaration with C++ linkage
363 * Returns:
364 * string mangling of symbol
366 extern (C++) const(char)* toMangle(Dsymbol s);
369 * Get RTTI mangling of the given class declaration for C++ ABI.
370 * Params:
371 * cd = class with C++ linkage
372 * Returns:
373 * string mangling of C++ typeinfo
375 extern (C++) const(char)* typeInfoMangle(ClassDeclaration cd);
378 * Get mangle name of a this-adjusting thunk to the given function
379 * declaration for C++ ABI.
380 * Params:
381 * fd = function with C++ linkage
382 * offset = call offset to the vptr
383 * Returns:
384 * string mangling of C++ thunk, or null if unhandled
386 extern (C++) const(char)* thunkMangle(FuncDeclaration fd, int offset);
389 * Gets vendor-specific type mangling for C++ ABI.
390 * Params:
391 * t = type to inspect
392 * Returns:
393 * string if type is mangled specially on target
394 * null if unhandled
396 extern (C++) const(char)* typeMangle(Type t);
399 * Get the type that will really be used for passing the given argument
400 * to an `extern(C++)` function.
401 * Params:
402 * p = parameter to be passed.
403 * Returns:
404 * `Type` to use for parameter `p`.
406 extern (C++) Type parameterType(Parameter p);
409 * Checks whether type is a vendor-specific fundamental type.
410 * Params:
411 * t = type to inspect
412 * isFundamental = where to store result
413 * Returns:
414 * true if isFundamental was set by function
416 extern (C++) bool fundamentalType(const Type t, ref bool isFundamental);
419 * Get the starting offset position for fields of an `extern(C++)` class
420 * that is derived from the given base class.
421 * Params:
422 * baseClass = base class with C++ linkage
423 * Returns:
424 * starting offset to lay out derived class fields
426 extern (C++) uint derivedClassOffset(ClassDeclaration baseClass);
429 ////////////////////////////////////////////////////////////////////////////////
431 * Functions and variables specific to interface with extern(Objective-C) ABI.
433 struct TargetObjC
435 bool supported; /// set if compiler can interface with Objective-C
438 ////////////////////////////////////////////////////////////////////////////////
439 extern (C++) __gshared Target target;