Debian Codegraph @

gcc/d/dmd/target.d

raw
1 /**
2 * Handles target-specific parameters
3 *
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`.
7 *
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.
12 *
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)
17 *
18 * Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved
19 * Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
20 * License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
21 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/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/compiler/src/dmd/target.d
24 */
25
26 module dmd.target;
27
28 import core.stdc.stdio;
29
30 import dmd.astenums : CHECKENABLE;
31 import dmd.globals : Param;
32
33 enum CPU : ubyte
34 {
35 x87,
36 mmx,
37 sse,
38 sse2,
39 sse3,
40 ssse3,
41 sse4_1,
42 sse4_2,
43 avx, // AVX1 instruction set
44 avx2, // AVX2 instruction set
45 avx512, // AVX-512 instruction set
46
47 // Special values that don't survive past the command line processing
48 baseline, // (default) the minimum capability CPU
49 native // the machine the compiler is being run on
50 }
51
52 ////////////////////////////////////////////////////////////////////////////////
53 /**
54 * Describes a back-end target. At present it is incomplete, but in the future
55 * it should grow to contain most or all target machine and target O/S specific
56 * information.
57 *
58 * In many cases, calls to sizeof() can't be used directly for getting data type
59 * sizes since cross compiling is supported and would end up using the host
60 * sizes rather than the target sizes.
61 */
62 extern (C++) struct Target
63 {
64 import dmd.dscope : Scope;
65 import dmd.expression : Expression;
66 import dmd.func : FuncDeclaration;
67 import dmd.location;
68 import dmd.astenums : LINK, TY;
69 import dmd.mtype : Type, TypeFunction, TypeTuple;
70 import dmd.root.ctfloat : real_t;
71 import dmd.statement : Statement;
72 import dmd.tokens : EXP;
73
74 /// Bit decoding of the Target.OS
75 enum OS : ubyte
76 {
77 /* These are mutually exclusive; one and only one is set.
78 * Match spelling and casing of corresponding version identifiers
79 */
80 none = 0,
81 linux = 1,
82 Windows = 2,
83 OSX = 4,
84 OpenBSD = 8,
85 FreeBSD = 0x10,
86 Solaris = 0x20,
87 DragonFlyBSD = 0x40,
88
89 // Combination masks
90 all = linux | Windows | OSX | OpenBSD | FreeBSD | Solaris | DragonFlyBSD,
91 Posix = linux | OSX | OpenBSD | FreeBSD | Solaris | DragonFlyBSD,
92 }
93
94 OS os;
95 ubyte osMajor;
96
97 // D ABI
98 ubyte ptrsize; /// size of a pointer in bytes
99 ubyte realsize; /// size a real consumes in memory
100 ubyte realpad; /// padding added to the CPU real size to bring it up to realsize
101 ubyte realalignsize; /// alignment for reals
102 ubyte classinfosize; /// size of `ClassInfo`
103 ulong maxStaticDataSize; /// maximum size of static data
104
105 /// C ABI
106 TargetC c;
107
108 /// C++ ABI
109 TargetCPP cpp;
110
111 /// Objective-C ABI
112 TargetObjC objc;
113
114 /// Architecture name
115 const(char)[] architectureName;
116 CPU cpu; // CPU instruction set to target
117 bool isAArch64; // generate 64 bit Arm code
118 bool isX86_64; // generate 64 bit code for x86_64; true by default for 64 bit dmd
119 bool isX86; // generate 32 bit Intel x86 code
120 bool isLP64; // pointers are 64 bits
121
122 // Environmental
123 const(char)[] obj_ext; /// extension for object files
124 const(char)[] lib_ext; /// extension for static library files
125 const(char)[] dll_ext; /// extension for dynamic library files
126 bool run_noext; /// allow -run sources without extensions
127 /**
128 * Values representing all properties for floating point types
129 */
130 extern (C++) struct FPTypeProperties(T)
131 {
132 real_t max; /// largest representable value that's not infinity
133 real_t min_normal; /// smallest representable normalized value that's not 0
134 real_t nan; /// NaN value
135 real_t infinity; /// infinity value
136 real_t epsilon; /// smallest increment to the value 1
137
138 long dig; /// number of decimal digits of precision
139 long mant_dig; /// number of bits in mantissa
140 long max_exp; /// maximum int value such that 2$(SUPERSCRIPT `max_exp-1`) is representable
141 long min_exp; /// minimum int value such that 2$(SUPERSCRIPT `min_exp-1`) is representable as a normalized value
142 long max_10_exp; /// maximum int value such that 10$(SUPERSCRIPT `max_10_exp` is representable)
143 long min_10_exp; /// minimum int value such that 10$(SUPERSCRIPT `min_10_exp`) is representable as a normalized value
144 }
145
146 FPTypeProperties!float FloatProperties; ///
147 FPTypeProperties!double DoubleProperties; ///
148 FPTypeProperties!real_t RealProperties; ///
149
150 private Type tvalist; // cached lazy result of va_listType()
151
152 private const(Param)* params; // cached reference to global.params
153
154 /**
155 * Initialize the Target
156 */
157 extern (C++) void _init(ref const Param params);
158
159
160 /**
161 * Deinitializes the global state of the compiler.
162 *
163 * This can be used to restore the state set by `_init` to its original
164 * state.
165 */
166 void deinitialize() @safe
167 {
168 this = this.init;
169 }
170
171 /**
172 * Requested target memory alignment size of the given type.
173 * Params:
174 * type = type to inspect
175 * Returns:
176 * alignment in bytes
177 */
178 extern (C++) uint alignsize(Type type);
179
180 /**
181 * Requested target field alignment size of the given type.
182 * Params:
183 * type = type to inspect
184 * Returns:
185 * alignment in bytes
186 */
187 extern (C++) uint fieldalign(Type type);
188
189 /**
190 * Type for the `va_list` type for the target; e.g., required for `_argptr`
191 * declarations.
192 * NOTE: For Posix/x86_64 this returns the type which will really
193 * be used for passing an argument of type va_list.
194 * Returns:
195 * `Type` that represents `va_list`.
196 */
197 extern (C++) Type va_listType(Loc loc, Scope* sc);
198
199 /**
200 * Checks whether the target supports a vector type.
201 * Params:
202 * sz = vector type size in bytes
203 * type = vector element type
204 * Returns:
205 * 0 vector type is supported,
206 * 1 vector type is not supported on the target at all
207 * 2 vector element type is not supported
208 * 3 vector size is not supported
209 */
210 extern (C++) int isVectorTypeSupported(int sz, Type type) @safe;
211
212 /**
213 * Checks whether the target supports the given operation for vectors.
214 * Params:
215 * type = target type of operation
216 * op = the unary or binary op being done on the `type`
217 * t2 = type of second operand if `op` is a binary operation
218 * Returns:
219 * true if the operation is supported or type is not a vector
220 */
221 extern (C++) bool isVectorOpSupported(Type type, EXP op, Type t2 = null);
222
223 /**
224 * Default system linkage for the target.
225 * Returns:
226 * `LINK` to use for `extern(System)`
227 */
228 extern (C++) LINK systemLinkage() @safe;
229
230 /**
231 * Describes how an argument type is passed to a function on target.
232 * Params:
233 * t = type to break down
234 * Returns:
235 * tuple of types if type is passed in one or more registers
236 * empty tuple if type is always passed on the stack
237 * null if the type is a `void` or argtypes aren't supported by the target
238 */
239 extern (C++) TypeTuple toArgTypes(Type t);
240
241 /**
242 * Determine return style of function - whether in registers or
243 * through a hidden pointer to the caller's stack.
244 * Params:
245 * tf = function type to check
246 * needsThis = true if the function type is for a non-static member function
247 * Returns:
248 * true if return value from function is on the stack
249 */
250 extern (C++) bool isReturnOnStack(TypeFunction tf, bool needsThis);
251
252 /**
253 * Decides whether an `in` parameter of the specified POD type is to be
254 * passed by reference or by value. To be used with `-preview=in` only!
255 * Params:
256 * t = type of the `in` parameter, must be a POD
257 * Returns:
258 * `true` if the `in` parameter is to be passed by reference
259 */
260 extern(C++) bool preferPassByRef(Type t);
261
262 /**
263 * Get targetInfo by key
264 * Params:
265 * name = name of targetInfo to get
266 * loc = location to use for error messages
267 * Returns:
268 * Expression for the requested targetInfo
269 */
270 extern (C++) Expression getTargetInfo(const(char)* name, Loc loc);
271
272 /**
273 * Params:
274 * tf = type of function being called
275 * Returns: `true` if the callee invokes destructors for arguments.
276 */
277 extern (C++) bool isCalleeDestroyingArgs(TypeFunction tf) @safe;
278
279 /**
280 * Returns true if the implementation for object monitors is always defined
281 * in the D runtime library (rt/monitor_.d).
282 * Params:
283 * fd = function with `synchronized` storage class.
284 * fbody = entire function body of `fd`
285 * Returns:
286 * `false` if the target backend handles synchronizing monitors.
287 */
288 extern (C++) bool libraryObjectMonitors(FuncDeclaration fd, Statement fbody);
289
290 /**
291 * Returns true if the target supports `pragma(linkerDirective)`.
292 * Returns:
293 * `false` if the target does not support `pragma(linkerDirective)`.
294 */
295 extern (C++) bool supportsLinkerDirective() const @safe;
296 }
297
298 ////////////////////////////////////////////////////////////////////////////////
299 /**
300 * Functions and variables specific to interfacing with extern(C) ABI.
301 */
302 struct TargetC
303 {
304 import dmd.declaration : BitFieldDeclaration;
305
306 enum Runtime : ubyte
307 {
308 Unspecified,
309 Bionic,
310 Glibc,
311 Microsoft,
312 Musl,
313 Newlib,
314 UClibc,
315 WASI,
316 }
317
318 enum BitFieldStyle : ubyte
319 {
320 Unspecified,
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
325 }
326 bool crtDestructorsSupported = true; /// Not all platforms support crt_destructor
327 ubyte boolsize; /// size of a C `_Bool` type
328 ubyte shortsize; /// size of a C `short` or `unsigned short` type
329 ubyte intsize; /// size of a C `int` or `unsigned int` type
330 ubyte longsize; /// size of a C `long` or `unsigned long` type
331 ubyte long_longsize; /// size of a C `long long` or `unsigned long long` type
332 ubyte long_doublesize; /// size of a C `long double`
333 ubyte wchar_tsize; /// size of a C `wchar_t` type
334 Runtime runtime; /// vendor of the C runtime to link against
335 BitFieldStyle bitFieldStyle; /// different C compilers do it differently
336
337 /**
338 * Indicates whether the specified bit-field contributes to the alignment
339 * of the containing aggregate.
340 * E.g., (not all) ARM ABIs do NOT ignore anonymous (incl. 0-length)
341 * bit-fields.
342 */
343 extern (C++) bool contributesToAggregateAlignment(BitFieldDeclaration bfd);
344 }
345
346 ////////////////////////////////////////////////////////////////////////////////
347 /**
348 * Functions and variables specific to interface with extern(C++) ABI.
349 */
350 struct TargetCPP
351 {
352 import dmd.dsymbol : Dsymbol;
353 import dmd.dclass : ClassDeclaration;
354 import dmd.func : FuncDeclaration;
355 import dmd.mtype : Type;
356
357 enum Runtime : ubyte
358 {
359 Unspecified,
360 LLVM,
361 GNU,
362 Microsoft,
363 Sun
364 }
365 bool reverseOverloads; /// set if overloaded functions are grouped and in reverse order (such as in dmc and cl)
366 bool exceptions; /// set if catching C++ exceptions is supported
367 bool twoDtorInVtable; /// target C++ ABI puts deleting and non-deleting destructor into vtable
368 bool splitVBasetable; /// set if C++ ABI uses separate tables for virtual functions and virtual bases
369 bool wrapDtorInExternD; /// set if C++ dtors require a D wrapper to be callable from runtime
370 Runtime runtime; /// vendor of the C++ runtime to link against
371
372 /**
373 * Mangle the given symbol for C++ ABI.
374 * Params:
375 * s = declaration with C++ linkage
376 * Returns:
377 * string mangling of symbol
378 */
379 extern (C++) const(char)* toMangle(Dsymbol s);
380
381 /**
382 * Get RTTI mangling of the given class declaration for C++ ABI.
383 * Params:
384 * cd = class with C++ linkage
385 * Returns:
386 * string mangling of C++ typeinfo
387 */
388 extern (C++) const(char)* typeInfoMangle(ClassDeclaration cd);
389
390 /**
391 * Get mangle name of a this-adjusting thunk to the given function
392 * declaration for C++ ABI.
393 * Params:
394 * fd = function with C++ linkage
395 * offset = call offset to the vptr
396 * Returns:
397 * string mangling of C++ thunk, or null if unhandled
398 */
399 extern (C++) const(char)* thunkMangle(FuncDeclaration fd, int offset);
400
401 /**
402 * Gets vendor-specific type mangling for C++ ABI.
403 * Params:
404 * t = type to inspect
405 * Returns:
406 * string if type is mangled specially on target
407 * null if unhandled
408 */
409 extern (C++) const(char)* typeMangle(Type t);
410
411 /**
412 * Get the type that will really be used for passing the given argument
413 * to an `extern(C++)` function, or `null` if unhandled.
414 * Params:
415 * t = type to be passed.
416 * Returns:
417 * `Type` to use for type `t`.
418 */
419 extern (C++) Type parameterType(Type t);
420
421 /**
422 * Checks whether type is a vendor-specific fundamental type.
423 * Params:
424 * t = type to inspect
425 * isFundamental = where to store result
426 * Returns:
427 * true if isFundamental was set by function
428 */
429 extern (C++) bool fundamentalType(const Type t, ref bool isFundamental);
430
431 /**
432 * Get the starting offset position for fields of an `extern(C++)` class
433 * that is derived from the given base class.
434 * Params:
435 * baseClass = base class with C++ linkage
436 * Returns:
437 * starting offset to lay out derived class fields
438 */
439 extern (C++) uint derivedClassOffset(ClassDeclaration baseClass);
440 }
441
442 ////////////////////////////////////////////////////////////////////////////////
443 /**
444 * Functions and variables specific to interface with extern(Objective-C) ABI.
445 */
446 struct TargetObjC
447 {
448 bool supported; /// set if compiler can interface with Objective-C
449 }
450
451 ////////////////////////////////////////////////////////////////////////////////
452 extern (C++) __gshared Target target;