-
-
Notifications
You must be signed in to change notification settings - Fork 610
/
target.d
755 lines (704 loc) · 24.1 KB
/
target.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/**
* Compiler implementation of the
* $(LINK2 http://www.dlang.org, D programming language).
*
* Copyright: Copyright (C) 1999-2018 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 http://www.digitalmars.com, Walter Bright)
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/target.d, _target.d)
* Documentation: https://dlang.org/phobos/dmd_target.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/target.d
*/
module dmd.target;
import dmd.argtypes;
import core.stdc.string : strlen;
import dmd.cppmangle;
import dmd.cppmanglewin;
import dmd.dclass;
import dmd.declaration;
import dmd.dstruct;
import dmd.dsymbol;
import dmd.expression;
import dmd.func;
import dmd.globals;
import dmd.id;
import dmd.identifier;
import dmd.mtype;
import dmd.typesem;
import dmd.tokens : TOK;
import dmd.utils : toDString;
import dmd.root.ctfloat;
import dmd.root.outbuffer;
/**
* 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.
*
* 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.
*/
struct Target
{
extern (C++) __gshared
{
// D ABI
uint ptrsize; /// size of a pointer in bytes
uint realsize; /// size a real consumes in memory
uint realpad; /// padding added to the CPU real size to bring it up to realsize
uint realalignsize; /// alignment for reals
uint classinfosize; /// size of `ClassInfo`
ulong maxStaticDataSize; /// maximum size of static data
// C ABI
uint c_longsize; /// size of a C `long` or `unsigned long` type
uint c_long_doublesize; /// size of a C `long double`
// C++ ABI
bool reverseCppOverloads; /// set if overloaded functions are grouped and in reverse order (such as in dmc and cl)
bool cppExceptions; /// set if catching C++ exceptions is supported
bool twoDtorInVtable; /// target C++ ABI puts deleting and non-deleting destructor into vtable
}
/**
* Values representing all properties for floating point types
*/
extern (C++) struct FPTypeProperties(T)
{
__gshared
{
real_t max; /// largest representable value that's not infinity
real_t min_normal; /// smallest representable normalized value that's not 0
real_t nan; /// NaN value
real_t snan; /// signalling NaN value
real_t infinity; /// infinity value
real_t epsilon; /// smallest increment to the value 1
d_int64 dig = T.dig; /// number of decimal digits of precision
d_int64 mant_dig = T.mant_dig; /// number of bits in mantissa
d_int64 max_exp = T.max_exp; /// maximum int value such that 2$(SUPERSCRIPT `max_exp-1`) is representable
d_int64 min_exp = T.min_exp; /// minimum int value such that 2$(SUPERSCRIPT `min_exp-1`) is representable as a normalized value
d_int64 max_10_exp = T.max_10_exp; /// maximum int value such that 10$(SUPERSCRIPT `max_10_exp` is representable)
d_int64 min_10_exp = T.min_10_exp; /// minimum int value such that 10$(SUPERSCRIPT `min_10_exp`) is representable as a normalized value
}
static void _init()
{
max = T.max;
min_normal = T.min_normal;
nan = T.nan;
snan = T.init;
infinity = T.infinity;
epsilon = T.epsilon;
}
}
///
alias FloatProperties = FPTypeProperties!float;
///
alias DoubleProperties = FPTypeProperties!double;
///
alias RealProperties = FPTypeProperties!real_t;
/**
* Initialize the Target
*/
extern (C++) static void _init()
{
FloatProperties._init();
DoubleProperties._init();
RealProperties._init();
// These have default values for 32 bit code, they get
// adjusted for 64 bit code.
ptrsize = 4;
classinfosize = 0x4C; // 76
/* gcc uses int.max for 32 bit compilations, and long.max for 64 bit ones.
* Set to int.max for both, because the rest of the compiler cannot handle
* 2^64-1 without some pervasive rework. The trouble is that much of the
* front and back end uses 32 bit ints for sizes and offsets. Since C++
* silently truncates 64 bit ints to 32, finding all these dependencies will be a problem.
*/
maxStaticDataSize = int.max;
if (global.params.isLP64)
{
ptrsize = 8;
classinfosize = 0x98; // 152
}
if (global.params.isLinux || global.params.isFreeBSD || global.params.isOpenBSD || global.params.isDragonFlyBSD || global.params.isSolaris)
{
realsize = 12;
realpad = 2;
realalignsize = 4;
c_longsize = 4;
twoDtorInVtable = true;
}
else if (global.params.isOSX)
{
realsize = 16;
realpad = 6;
realalignsize = 16;
c_longsize = 4;
twoDtorInVtable = true;
}
else if (global.params.isWindows)
{
realsize = 10;
realpad = 0;
realalignsize = 2;
reverseCppOverloads = true;
twoDtorInVtable = false;
c_longsize = 4;
if (ptrsize == 4)
{
/* Optlink cannot deal with individual data chunks
* larger than 16Mb
*/
maxStaticDataSize = 0x100_0000; // 16Mb
}
}
else
assert(0);
if (global.params.is64bit)
{
if (global.params.isLinux || global.params.isFreeBSD || global.params.isDragonFlyBSD || global.params.isSolaris)
{
realsize = 16;
realpad = 6;
realalignsize = 16;
c_longsize = 8;
}
else if (global.params.isOSX)
{
c_longsize = 8;
}
}
c_long_doublesize = realsize;
if (global.params.is64bit && global.params.isWindows)
c_long_doublesize = 8;
cppExceptions = global.params.isLinux || global.params.isFreeBSD ||
global.params.isDragonFlyBSD || global.params.isOSX;
}
/**
* Requested target memory alignment size of the given type.
* Params:
* type = type to inspect
* Returns:
* alignment in bytes
*/
extern (C++) static uint alignsize(Type type)
{
assert(type.isTypeBasic());
switch (type.ty)
{
case Tfloat80:
case Timaginary80:
case Tcomplex80:
return Target.realalignsize;
case Tcomplex32:
if (global.params.isLinux || global.params.isOSX || global.params.isFreeBSD || global.params.isOpenBSD ||
global.params.isDragonFlyBSD || global.params.isSolaris)
return 4;
break;
case Tint64:
case Tuns64:
case Tfloat64:
case Timaginary64:
case Tcomplex64:
if (global.params.isLinux || global.params.isOSX || global.params.isFreeBSD || global.params.isOpenBSD ||
global.params.isDragonFlyBSD || global.params.isSolaris)
return global.params.is64bit ? 8 : 4;
break;
default:
break;
}
return cast(uint)type.size(Loc.initial);
}
/**
* Requested target field alignment size of the given type.
* Params:
* type = type to inspect
* Returns:
* alignment in bytes
*/
extern (C++) static uint fieldalign(Type type)
{
const size = type.alignsize();
if ((global.params.is64bit || global.params.isOSX) && (size == 16 || size == 32))
return size;
return (8 < size) ? 8 : size;
}
/**
* Size of the target OS critical section.
* Returns:
* size in bytes
*/
extern (C++) static uint critsecsize()
{
if (global.params.isWindows)
{
// sizeof(CRITICAL_SECTION) for Windows.
return global.params.isLP64 ? 40 : 24;
}
else if (global.params.isLinux)
{
// sizeof(pthread_mutex_t) for Linux.
if (global.params.is64bit)
return global.params.isLP64 ? 40 : 32;
else
return global.params.isLP64 ? 40 : 24;
}
else if (global.params.isFreeBSD)
{
// sizeof(pthread_mutex_t) for FreeBSD.
return global.params.isLP64 ? 8 : 4;
}
else if (global.params.isOpenBSD)
{
// sizeof(pthread_mutex_t) for OpenBSD.
return global.params.isLP64 ? 8 : 4;
}
else if (global.params.isDragonFlyBSD)
{
// sizeof(pthread_mutex_t) for DragonFlyBSD.
return global.params.isLP64 ? 8 : 4;
}
else if (global.params.isOSX)
{
// sizeof(pthread_mutex_t) for OSX.
return global.params.isLP64 ? 64 : 44;
}
else if (global.params.isSolaris)
{
// sizeof(pthread_mutex_t) for Solaris.
return 24;
}
assert(0);
}
/**
* Type for the `va_list` type for the target.
* NOTE: For Posix/x86_64 this returns the type which will really
* be used for passing an argument of type va_list.
* Returns:
* `Type` that represents `va_list`.
*/
extern (C++) static Type va_listType()
{
if (global.params.isWindows)
{
return Type.tchar.pointerTo();
}
else if (global.params.isLinux || global.params.isFreeBSD || global.params.isOpenBSD || global.params.isDragonFlyBSD ||
global.params.isSolaris || global.params.isOSX)
{
if (global.params.is64bit)
{
return (new TypeIdentifier(Loc.initial, Identifier.idPool("__va_list_tag"))).pointerTo();
}
else
{
return Type.tchar.pointerTo();
}
}
else
{
assert(0);
}
}
/**
* Checks whether the target supports a vector type.
* Params:
* sz = vector type size in bytes
* type = vector element type
* Returns:
* 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
*/
extern (C++) static int isVectorTypeSupported(int sz, Type type)
{
if (!global.params.is64bit && !global.params.isOSX)
return 1; // not supported
switch (type.ty)
{
case Tvoid:
case Tint8:
case Tuns8:
case Tint16:
case Tuns16:
case Tint32:
case Tuns32:
case Tfloat32:
case Tint64:
case Tuns64:
case Tfloat64:
break;
default:
return 2; // wrong base type
}
if (sz != 16 && !(global.params.cpu >= CPU.avx && sz == 32))
return 3; // wrong size
return 0;
}
/**
* Checks whether the target supports the given operation for vectors.
* Params:
* type = target type of operation
* op = the unary or binary op being done on the `type`
* t2 = type of second operand if `op` is a binary operation
* Returns:
* true if the operation is supported or type is not a vector
*/
extern (C++) static bool isVectorOpSupported(Type type, TOK op, Type t2 = null)
{
import dmd.tokens;
if (type.ty != Tvector)
return true; // not a vector op
auto tvec = cast(TypeVector) type;
bool supported;
switch (op)
{
case TOK.negate, TOK.uadd:
supported = tvec.isscalar();
break;
case TOK.lessThan, TOK.greaterThan, TOK.lessOrEqual, TOK.greaterOrEqual, TOK.equal, TOK.notEqual, TOK.identity, TOK.notIdentity:
supported = false;
break;
case TOK.leftShift, TOK.leftShiftAssign, TOK.rightShift, TOK.rightShiftAssign, TOK.unsignedRightShift, TOK.unsignedRightShiftAssign:
supported = false;
break;
case TOK.add, TOK.addAssign, TOK.min, TOK.minAssign:
supported = tvec.isscalar();
break;
case TOK.mul, TOK.mulAssign:
// only floats and short[8]/ushort[8] (PMULLW)
if (tvec.isfloating() || tvec.elementType().size(Loc.initial) == 2 ||
// int[4]/uint[4] with SSE4.1 (PMULLD)
global.params.cpu >= CPU.sse4_1 && tvec.elementType().size(Loc.initial) == 4)
supported = true;
else
supported = false;
break;
case TOK.div, TOK.divAssign:
supported = tvec.isfloating();
break;
case TOK.mod, TOK.modAssign:
supported = false;
break;
case TOK.and, TOK.andAssign, TOK.or, TOK.orAssign, TOK.xor, TOK.xorAssign:
supported = tvec.isintegral();
break;
case TOK.not:
supported = false;
break;
case TOK.tilde:
supported = tvec.isintegral();
break;
case TOK.pow, TOK.powAssign:
supported = false;
break;
default:
// import std.stdio : stderr, writeln;
// stderr.writeln(op);
assert(0, "unhandled op " ~ Token.toString(op));
}
return supported;
}
/**
* Mangle the given symbol for C++ ABI.
* Params:
* s = declaration with C++ linkage
* Returns:
* string mangling of symbol
*/
extern (C++) static const(char)* toCppMangle(Dsymbol s)
{
static if (TARGET.Linux || TARGET.OSX || TARGET.FreeBSD || TARGET.OpenBSD || TARGET.DragonFlyBSD || TARGET.Solaris)
return toCppMangleItanium(s);
else static if (TARGET.Windows)
return toCppMangleMSVC(s);
else
static assert(0, "fix this");
}
/**
* Get RTTI mangling of the given class declaration for C++ ABI.
* Params:
* cd = class with C++ linkage
* Returns:
* string mangling of C++ typeinfo
*/
extern (C++) static const(char)* cppTypeInfoMangle(ClassDeclaration cd)
{
static if (TARGET.Linux || TARGET.OSX || TARGET.FreeBSD || TARGET.OpenBSD || TARGET.Solaris || TARGET.DragonFlyBSD)
return cppTypeInfoMangleItanium(cd);
else static if (TARGET.Windows)
return cppTypeInfoMangleMSVC(cd);
else
static assert(0, "fix this");
}
/**
* Gets vendor-specific type mangling for C++ ABI.
* Params:
* t = type to inspect
* Returns:
* string if type is mangled specially on target
* null if unhandled
*/
extern (C++) static const(char)* cppTypeMangle(Type t)
{
return null;
}
/**
* Get the type that will really be used for passing the given argument
* to an `extern(C++)` function.
* Params:
* p = parameter to be passed.
* Returns:
* `Type` to use for parameter `p`.
*/
extern (C++) static Type cppParameterType(Parameter p)
{
Type t = p.type.merge2();
if (p.storageClass & (STC.out_ | STC.ref_))
t = t.referenceTo();
else if (p.storageClass & STC.lazy_)
{
// Mangle as delegate
Type td = new TypeFunction(ParameterList(), t, LINK.d);
td = new TypeDelegate(td);
t = merge(t);
}
return t;
}
/**
* Default system linkage for the target.
* Returns:
* `LINK` to use for `extern(System)`
*/
extern (C++) static LINK systemLinkage()
{
return global.params.isWindows ? LINK.windows : LINK.c;
}
/**
* Describes how an argument type is passed to a function on target.
* Params:
* t = type to break down
* Returns:
* tuple of types if type is passed in one or more registers
* empty tuple if type is always passed on the stack
*/
extern (C++) static TypeTuple toArgTypes(Type t)
{
return .toArgTypes(t);
}
/**
* Determine return style of function - whether in registers or
* through a hidden pointer to the caller's stack.
* Params:
* tf = function type to check
* needsThis = true if the function type is for a non-static member function
* Returns:
* true if return value from function is on the stack
*/
extern (C++) static bool isReturnOnStack(TypeFunction tf, bool needsThis)
{
if (tf.isref)
{
//printf(" ref false\n");
return false; // returns a pointer
}
Type tn = tf.next.toBasetype();
//printf("tn = %s\n", tn.toChars());
d_uns64 sz = tn.size();
Type tns = tn;
if (global.params.isWindows && global.params.is64bit)
{
// http://msdn.microsoft.com/en-us/library/7572ztz4.aspx
if (tns.ty == Tcomplex32)
return true;
if (tns.isscalar())
return false;
tns = tns.baseElemOf();
if (tns.ty == Tstruct)
{
StructDeclaration sd = (cast(TypeStruct)tns).sym;
if (tf.linkage == LINK.cpp && needsThis)
return true;
if (!sd.isPOD() || sz > 8)
return true;
if (sd.fields.dim == 0)
return true;
}
if (sz <= 16 && !(sz & (sz - 1)))
return false;
return true;
}
else if (global.params.isWindows && global.params.mscoff)
{
Type tb = tns.baseElemOf();
if (tb.ty == Tstruct)
{
if (tf.linkage == LINK.cpp && needsThis)
return true;
}
}
Lagain:
if (tns.ty == Tsarray)
{
tns = tns.baseElemOf();
if (tns.ty != Tstruct)
{
L2:
if (global.params.isLinux && tf.linkage != LINK.d && !global.params.is64bit)
{
// 32 bit C/C++ structs always on stack
}
else
{
switch (sz)
{
case 1:
case 2:
case 4:
case 8:
//printf(" sarray false\n");
return false; // return small structs in regs
// (not 3 byte structs!)
default:
break;
}
}
//printf(" sarray true\n");
return true;
}
}
if (tns.ty == Tstruct)
{
StructDeclaration sd = (cast(TypeStruct)tns).sym;
if (global.params.isLinux && tf.linkage != LINK.d && !global.params.is64bit)
{
//printf(" 2 true\n");
return true; // 32 bit C/C++ structs always on stack
}
if (global.params.isWindows && tf.linkage == LINK.cpp && !global.params.is64bit &&
sd.isPOD() && sd.ctor)
{
// win32 returns otherwise POD structs with ctors via memory
return true;
}
if (sd.arg1type && !sd.arg2type)
{
tns = sd.arg1type;
if (tns.ty != Tstruct)
goto L2;
goto Lagain;
}
else if (global.params.is64bit && !sd.arg1type && !sd.arg2type)
return true;
else if (sd.isPOD())
{
switch (sz)
{
case 1:
case 2:
case 4:
case 8:
//printf(" 3 false\n");
return false; // return small structs in regs
// (not 3 byte structs!)
case 16:
if (!global.params.isWindows && global.params.is64bit)
return false;
break;
default:
break;
}
}
//printf(" 3 true\n");
return true;
}
else if ((global.params.isLinux || global.params.isOSX ||
global.params.isFreeBSD || global.params.isSolaris ||
global.params.isDragonFlyBSD) &&
tf.linkage == LINK.c &&
tns.iscomplex())
{
if (tns.ty == Tcomplex32)
return false; // in EDX:EAX, not ST1:ST0
else
return true;
}
else
{
//assert(sz <= 16);
//printf(" 4 false\n");
return false;
}
}
/***
* Determine the size a value of type `t` will be when it
* is passed on the function parameter stack.
* Params:
* loc = location to use for error messages
* t = type of parameter
* Returns:
* size used on parameter stack
*/
extern (C++) static ulong parameterSize(const ref Loc loc, Type t)
{
if (!global.params.is64bit &&
(global.params.isFreeBSD || global.params.isOSX))
{
/* These platforms use clang, which regards a struct
* with size 0 as being of size 0 on the parameter stack,
* even while sizeof(struct) is 1.
* It's an ABI incompatibility with gcc.
*/
if (t.ty == Tstruct)
{
auto ts = cast(TypeStruct)t;
if (ts.sym.hasNoFields)
return 0;
}
}
const sz = t.size(loc);
return global.params.is64bit ? (sz + 7) & ~7 : (sz + 3) & ~3;
}
// this guarantees `getTargetInfo` and `allTargetInfos` remain in sync
private enum TargetInfoKeys
{
cppRuntimeLibrary,
floatAbi,
objectFormat,
}
/**
* Get targetInfo by key
* Params:
* name = name of targetInfo to get
* loc = location to use for error messages
* Returns:
* Expression for the requested targetInfo
*/
extern (C++) static Expression getTargetInfo(const(char)* name, const ref Loc loc)
{
StringExp stringExp(const(char)[] sval)
{
return new StringExp(loc, cast(void*)sval.ptr, sval.length);
}
switch (name.toDString) with (TargetInfoKeys)
{
case objectFormat.stringof:
if (global.params.isWindows)
return stringExp(global.params.mscoff ? "coff" : "omf");
else if (global.params.isOSX)
return stringExp("macho");
else
return stringExp("elf");
case floatAbi.stringof:
return stringExp("hard");
case cppRuntimeLibrary.stringof:
if (global.params.isWindows)
{
if (global.params.mscoff)
return stringExp(global.params.mscrtlib ? global.params.mscrtlib.toDString : "");
return stringExp("snn");
}
return stringExp("");
default:
return null;
}
}
}