/
target.d
1643 lines (1512 loc) · 53.9 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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Handles target-specific parameters
*
* 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:
* - $(LINK2 https://wiki.osdev.org/Target_Triplet, Target Triplets)
* - $(LINK2 https://github.com/ldc-developers/ldc, LDC repository)
* - $(LINK2 https://github.com/D-Programming-GDC/gcc, GDC repository)
*
* Copyright: Copyright (C) 1999-2024 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
* License: $(LINK2 https://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.astenums : CHECKENABLE;
import dmd.globals : Param;
enum CPU : ubyte
{
x87,
mmx,
sse,
sse2,
sse3,
ssse3,
sse4_1,
sse4_2,
avx, // AVX1 instruction set
avx2, // AVX2 instruction set
avx512, // AVX-512 instruction set
// Special values that don't survive past the command line processing
baseline, // (default) the minimum capability CPU
native // the machine the compiler is being run on
}
Target.OS defaultTargetOS() @safe
{
version (Windows)
return Target.OS.Windows;
else version (linux)
return Target.OS.linux;
else version (OSX)
return Target.OS.OSX;
else version (FreeBSD)
return Target.OS.FreeBSD;
else version (OpenBSD)
return Target.OS.OpenBSD;
else version (Solaris)
return Target.OS.Solaris;
else version (DragonFlyBSD)
return Target.OS.DragonFlyBSD;
else
static assert(0, "unknown TARGET");
}
ubyte defaultTargetOSMajor() @safe
{
version (FreeBSD)
{
version (TARGET_FREEBSD10)
return 10;
else version (TARGET_FREEBSD11)
return 11;
else version (TARGET_FREEBSD12)
return 12;
else version (TARGET_FREEBSD13)
return 13;
else version (TARGET_FREEBSD14)
return 14;
else
return 0;
}
else
return 0;
}
/**
* Add default `version` identifier for dmd, and set the
* target platform in `params`.
* https://dlang.org/spec/version.html#predefined-versions
*
* Needs to be run after all arguments parsing (command line, DFLAGS environment
* variable and config file) in order to add final flags (such as `X86_64` or
* the `CRuntime` used).
*
* Params:
* params = which target to compile for (set by `setTarget()`)
* tgt = target
*/
public
void addDefaultVersionIdentifiers(const ref Param params, const ref Target tgt)
{
import dmd.cond : VersionCondition;
import dmd.dmdparams : driverParams, PIC;
VersionCondition.addPredefinedGlobalIdent("DigitalMars");
VersionCondition.addPredefinedGlobalIdent("LittleEndian");
VersionCondition.addPredefinedGlobalIdent("D_Version2");
VersionCondition.addPredefinedGlobalIdent("all");
addPredefinedGlobalIdentifiers(tgt);
if (params.ddoc.doOutput)
VersionCondition.addPredefinedGlobalIdent("D_Ddoc");
if (params.cov)
VersionCondition.addPredefinedGlobalIdent("D_Coverage");
if (driverParams.pic != PIC.fixed)
VersionCondition.addPredefinedGlobalIdent(driverParams.pic == PIC.pic ? "D_PIC" : "D_PIE");
if (params.useUnitTests)
VersionCondition.addPredefinedGlobalIdent("unittest");
if (params.useAssert == CHECKENABLE.on)
VersionCondition.addPredefinedGlobalIdent("assert");
if (params.useIn == CHECKENABLE.on)
VersionCondition.addPredefinedGlobalIdent("D_PreConditions");
if (params.useOut == CHECKENABLE.on)
VersionCondition.addPredefinedGlobalIdent("D_PostConditions");
if (params.useInvariants == CHECKENABLE.on)
VersionCondition.addPredefinedGlobalIdent("D_Invariants");
if (params.useArrayBounds == CHECKENABLE.off)
VersionCondition.addPredefinedGlobalIdent("D_NoBoundsChecks");
if (params.betterC)
{
VersionCondition.addPredefinedGlobalIdent("D_BetterC");
}
else
{
if (params.useModuleInfo)
VersionCondition.addPredefinedGlobalIdent("D_ModuleInfo");
if (params.useExceptions)
VersionCondition.addPredefinedGlobalIdent("D_Exceptions");
if (params.useTypeInfo)
VersionCondition.addPredefinedGlobalIdent("D_TypeInfo");
}
VersionCondition.addPredefinedGlobalIdent("D_HardFloat");
if (params.tracegc)
VersionCondition.addPredefinedGlobalIdent("D_ProfileGC");
if (driverParams.optimize)
VersionCondition.addPredefinedGlobalIdent("D_Optimized");
}
// /**
// * Add predefined global identifiers that are determied by the target
// */
private
void addPredefinedGlobalIdentifiers(const ref Target tgt)
{
import dmd.cond : VersionCondition;
alias predef = VersionCondition.addPredefinedGlobalIdent;
if (tgt.cpu >= CPU.sse2)
{
predef("D_SIMD");
if (tgt.cpu >= CPU.avx)
predef("D_AVX");
if (tgt.cpu >= CPU.avx2)
predef("D_AVX2");
}
with (Target)
{
if (tgt.os & OS.Posix)
predef("Posix");
if (tgt.os & (OS.linux | OS.FreeBSD | OS.OpenBSD | OS.DragonFlyBSD | OS.Solaris))
predef("ELFv1");
switch (tgt.os)
{
case OS.none: { predef("FreeStanding"); break; }
case OS.linux: { predef("linux"); break; }
case OS.OpenBSD: { predef("OpenBSD"); break; }
case OS.DragonFlyBSD: { predef("DragonFlyBSD"); break; }
case OS.Solaris: { predef("Solaris"); break; }
case OS.Windows:
{
predef("Windows");
VersionCondition.addPredefinedGlobalIdent(tgt.isX86_64 ? "Win64" : "Win32");
break;
}
case OS.OSX:
{
predef("OSX");
// For legacy compatibility
predef("darwin");
break;
}
case OS.FreeBSD:
{
predef("FreeBSD");
if(tgt.osMajor != 0)
{
import core.stdc.stdio : snprintf;
char["FreeBSD_100".length + 1] buffer;
immutable len = snprintf(buffer.ptr, buffer.length, "FreeBSD_%u", uint(tgt.osMajor));
predef(buffer[0 .. len]);
}
break;
}
default: assert(0);
}
}
addCRuntimePredefinedGlobalIdent(tgt.c);
addCppRuntimePredefinedGlobalIdent(tgt.cpp);
if (tgt.isX86_64)
{
VersionCondition.addPredefinedGlobalIdent("D_InlineAsm_X86_64");
VersionCondition.addPredefinedGlobalIdent("X86_64");
}
else
{
VersionCondition.addPredefinedGlobalIdent("D_InlineAsm"); //legacy
VersionCondition.addPredefinedGlobalIdent("D_InlineAsm_X86");
VersionCondition.addPredefinedGlobalIdent("X86");
}
if (tgt.isLP64)
VersionCondition.addPredefinedGlobalIdent("D_LP64");
else if (tgt.isX86_64)
VersionCondition.addPredefinedGlobalIdent("X32");
}
private
void addCRuntimePredefinedGlobalIdent(const ref TargetC c)
{
import dmd.cond : VersionCondition;
alias predef = VersionCondition.addPredefinedGlobalIdent;
with (TargetC.Runtime) switch (c.runtime)
{
default:
case Unspecified: return;
case Bionic: return predef("CRuntime_Bionic");
case DigitalMars: return predef("CRuntime_DigitalMars");
case Glibc: return predef("CRuntime_Glibc");
case Microsoft: return predef("CRuntime_Microsoft");
case Musl: return predef("CRuntime_Musl");
case Newlib: return predef("CRuntime_Newlib");
case UClibc: return predef("CRuntime_UClibc");
case WASI: return predef("CRuntime_WASI");
}
}
private
void addCppRuntimePredefinedGlobalIdent(const ref TargetCPP cpp)
{
import dmd.cond : VersionCondition;
alias predef = VersionCondition.addPredefinedGlobalIdent;
with (TargetCPP.Runtime) switch (cpp.runtime)
{
default:
case Unspecified: return;
case Clang: return predef("CppRuntime_Clang");
case DigitalMars: return predef("CppRuntime_DigitalMars");
case Gcc: return predef("CppRuntime_Gcc");
case Microsoft: return predef("CppRuntime_Microsoft");
case Sun: return predef("CppRuntime_Sun");
}
}
////////////////////////////////////////////////////////////////////////////////
/**
* 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.
*/
extern (C++) struct Target
{
import dmd.dscope : Scope;
import dmd.expression : Expression;
import dmd.func : FuncDeclaration;
import dmd.location;
import dmd.astenums : LINK, TY;
import dmd.mtype : Type, TypeFunction, TypeTuple;
import dmd.typesem : pointerTo;
import dmd.root.ctfloat : real_t;
import dmd.statement : Statement;
import dmd.tokens : EXP;
/// Bit decoding of the Target.OS
enum OS : ubyte
{
/* These are mutually exclusive; one and only one is set.
* Match spelling and casing of corresponding version identifiers
*/
none = 0,
linux = 1,
Windows = 2,
OSX = 4,
OpenBSD = 8,
FreeBSD = 0x10,
Solaris = 0x20,
DragonFlyBSD = 0x40,
// Combination masks
all = linux | Windows | OSX | OpenBSD | FreeBSD | Solaris | DragonFlyBSD,
Posix = linux | OSX | OpenBSD | FreeBSD | Solaris | DragonFlyBSD,
}
extern(D) enum ObjectFormat : ubyte
{
elf,
macho,
coff,
omf
}
OS os;
ubyte osMajor;
// D ABI
ubyte ptrsize; /// size of a pointer in bytes
ubyte realsize; /// size a real consumes in memory
ubyte realpad; /// padding added to the CPU real size to bring it up to realsize
ubyte realalignsize; /// alignment for reals
ubyte classinfosize; /// size of `ClassInfo`
ulong maxStaticDataSize; /// maximum size of static data
/// C ABI
TargetC c;
/// C++ ABI
TargetCPP cpp;
/// Objective-C ABI
TargetObjC objc;
/// Architecture name
const(char)[] architectureName;
CPU cpu; // CPU instruction set to target
bool isX86_64; // generate 64 bit code for x86_64; true by default for 64 bit dmd
bool isLP64; // pointers are 64 bits
// Environmental
const(char)[] obj_ext; /// extension for object files
const(char)[] lib_ext; /// extension for static library files
const(char)[] dll_ext; /// extension for dynamic library files
bool run_noext; /// allow -run sources without extensions
bool omfobj; // for Win32: write OMF object files instead of MsCoff
/**
* Values representing all properties for floating point types
*/
extern (C++) struct FPTypeProperties(T)
{
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 infinity; /// infinity value
real_t epsilon; /// smallest increment to the value 1
long dig; /// number of decimal digits of precision
long mant_dig; /// number of bits in mantissa
long max_exp; /// maximum int value such that 2$(SUPERSCRIPT `max_exp-1`) is representable
long min_exp; /// minimum int value such that 2$(SUPERSCRIPT `min_exp-1`) is representable as a normalized value
long max_10_exp; /// maximum int value such that 10$(SUPERSCRIPT `max_10_exp` is representable)
long min_10_exp; /// minimum int value such that 10$(SUPERSCRIPT `min_10_exp`) is representable as a normalized value
extern (D) void initialize()
{
max = T.max;
min_normal = T.min_normal;
nan = T.nan;
infinity = T.infinity;
epsilon = T.epsilon;
dig = T.dig;
mant_dig = T.mant_dig;
max_exp = T.max_exp;
min_exp = T.min_exp;
max_10_exp = T.max_10_exp;
min_10_exp = T.min_10_exp;
}
}
FPTypeProperties!float FloatProperties; ///
FPTypeProperties!double DoubleProperties; ///
FPTypeProperties!real_t RealProperties; ///
private Type tvalist; // cached lazy result of va_listType()
private const(Param)* params; // cached reference to global.params
/**
* Initialize the Target
*/
extern (C++) void _init(ref const Param params)
{
// isX86_64, omfobj and cpu are initialized in parseCommandLine
this.params = ¶ms;
FloatProperties.initialize();
DoubleProperties.initialize();
RealProperties.initialize();
isLP64 = isX86_64;
// These have default values for 32 bit code, they get
// adjusted for 64 bit code.
ptrsize = 4;
classinfosize = 0x4C+16; // 92
/* 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 (isLP64)
{
ptrsize = 8;
classinfosize = 0x98+16; // 168
}
if (os & (Target.OS.linux | Target.OS.FreeBSD | Target.OS.OpenBSD | Target.OS.DragonFlyBSD | Target.OS.Solaris))
{
realsize = 12;
realpad = 2;
realalignsize = 4;
}
else if (os == Target.OS.OSX)
{
realsize = 16;
realpad = 6;
realalignsize = 16;
}
else if (os == Target.OS.Windows)
{
realsize = 10;
realpad = 0;
realalignsize = 2;
if (omfobj)
{
/* Optlink cannot deal with individual data chunks
* larger than 16Mb
*/
maxStaticDataSize = 0x100_0000; // 16Mb
}
}
else
assert(0);
if (isX86_64)
{
if (os & (Target.OS.linux | Target.OS.FreeBSD | Target.OS.OpenBSD | Target.OS.DragonFlyBSD | Target.OS.Solaris))
{
realsize = 16;
realpad = 6;
realalignsize = 16;
}
}
c.initialize(params, this);
cpp.initialize(params, this);
objc.initialize(params, this);
if (isX86_64)
architectureName = "X86_64";
else
architectureName = "X86";
if (os == Target.OS.Windows)
{
obj_ext = "obj";
lib_ext = "lib";
dll_ext = "dll";
run_noext = false;
}
else if (os & (Target.OS.linux | Target.OS.FreeBSD | Target.OS.OpenBSD | Target.OS.DragonFlyBSD | Target.OS.Solaris | Target.OS.OSX))
{
obj_ext = "o";
lib_ext = "a";
if (os == Target.OS.OSX)
dll_ext = "dylib";
else
dll_ext = "so";
run_noext = true;
}
else
assert(0, "unknown environment");
}
/**
Determine the object format to be used
*/
extern(D) Target.ObjectFormat objectFormat() @safe
{
if (os == Target.OS.OSX)
return Target.ObjectFormat.macho;
else if (os & Target.OS.Posix)
return Target.ObjectFormat.elf;
else if (os == Target.OS.Windows)
return omfobj ? Target.ObjectFormat.omf : Target.ObjectFormat.coff;
else
assert(0, "unkown object format");
}
/**
* Determine the instruction set to be used
*/
void setCPU() @safe
{
if(!isXmmSupported())
{
cpu = CPU.x87; // cannot support other instruction sets
return;
}
switch (cpu)
{
case CPU.baseline:
cpu = CPU.sse2;
break;
case CPU.native:
{
import core.cpuid;
cpu = core.cpuid.avx2 ? CPU.avx2 :
core.cpuid.avx ? CPU.avx :
CPU.sse2;
break;
}
default:
break;
}
}
/**
* Deinitializes the global state of the compiler.
*
* This can be used to restore the state set by `_init` to its original
* state.
*/
void deinitialize() @safe
{
this = this.init;
}
/**
* Requested target memory alignment size of the given type.
* Params:
* type = type to inspect
* Returns:
* alignment in bytes
*/
extern (C++) uint alignsize(Type type)
{
assert(type.isTypeBasic());
switch (type.ty)
{
case TY.Tfloat80:
case TY.Timaginary80:
case TY.Tcomplex80:
return target.realalignsize;
case TY.Tcomplex32:
if (os & Target.OS.Posix)
return 4;
break;
case TY.Tint64:
case TY.Tuns64:
case TY.Tfloat64:
case TY.Timaginary64:
case TY.Tcomplex64:
if (os & Target.OS.Posix)
return isX86_64 ? 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++) uint fieldalign(Type type)
{
const size = type.alignsize();
if ((isX86_64 || os == Target.OS.OSX) && (size == 16 || size == 32))
return size;
return (8 < size) ? 8 : size;
}
/**
* 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.
* Returns:
* `Type` that represents `va_list`.
*/
extern (C++) Type va_listType(const ref Loc loc, Scope* sc)
{
if (tvalist)
return tvalist;
if (os == Target.OS.Windows)
{
tvalist = Type.tchar.pointerTo();
}
else if (os & Target.OS.Posix)
{
if (isX86_64)
{
import dmd.identifier : Identifier;
import dmd.mtype : TypeIdentifier;
import dmd.typesem : typeSemantic;
tvalist = new TypeIdentifier(Loc.initial, Identifier.idPool("__va_list_tag")).pointerTo();
tvalist = typeSemantic(tvalist, loc, sc);
}
else
{
tvalist = Type.tchar.pointerTo();
}
}
else
{
assert(0);
}
return tvalist;
}
/**
* 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++) int isVectorTypeSupported(int sz, Type type) @safe
{
if (!isXmmSupported())
return 1; // not supported
switch (type.ty)
{
case TY.Tvoid:
case TY.Tint8:
case TY.Tuns8:
case TY.Tint16:
case TY.Tuns16:
case TY.Tint32:
case TY.Tuns32:
case TY.Tfloat32:
case TY.Tint64:
case TY.Tuns64:
case TY.Tfloat64:
break;
default:
return 2; // wrong base type
}
// Whether a vector is really supported depends on the CPU being targeted.
if (sz == 16)
{
switch (type.ty)
{
case TY.Tint32:
case TY.Tuns32:
case TY.Tfloat32:
if (cpu < CPU.sse)
return 3; // no SSE vector support
break;
case TY.Tvoid:
case TY.Tint8:
case TY.Tuns8:
case TY.Tint16:
case TY.Tuns16:
case TY.Tint64:
case TY.Tuns64:
case TY.Tfloat64:
if (cpu < CPU.sse2)
return 3; // no SSE2 vector support
break;
default:
assert(0);
}
}
else if (sz == 32)
{
if (cpu < CPU.avx)
return 3; // no AVX vector support
}
else
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++) bool isVectorOpSupported(Type type, EXP op, Type t2 = null)
{
import dmd.hdrgen : EXPtoString;
auto tvec = type.isTypeVector();
if (tvec is null)
return true; // not a vector op
const vecsize = cast(int)tvec.basetype.size();
const elemty = cast(int)tvec.elementType().ty;
// Only operations on these sizes are supported (see isVectorTypeSupported)
if (vecsize != 16 && vecsize != 32)
return false;
bool supported = false;
switch (op)
{
case EXP.uadd:
// Expression is a no-op, supported everywhere.
supported = tvec.isscalar();
break;
case EXP.negate:
if (vecsize == 16)
{
// float[4] negate needs SSE support ({V}SUBPS)
if (elemty == TY.Tfloat32 && cpu >= CPU.sse)
supported = true;
// double[2] negate needs SSE2 support ({V}SUBPD)
else if (elemty == TY.Tfloat64 && cpu >= CPU.sse2)
supported = true;
// (u)byte[16]/short[8]/int[4]/long[2] negate needs SSE2 support ({V}PSUB[BWDQ])
else if (tvec.isintegral() && cpu >= CPU.sse2)
supported = true;
}
else if (vecsize == 32)
{
// float[8]/double[4] negate needs AVX support (VSUBP[SD])
if (tvec.isfloating() && cpu >= CPU.avx)
supported = true;
// (u)byte[32]/short[16]/int[8]/long[4] negate needs AVX2 support (VPSUB[BWDQ])
else if (tvec.isintegral() && cpu >= CPU.avx2)
supported = true;
}
break;
case EXP.identity, EXP.notIdentity:
supported = false;
break;
case EXP.lessThan, EXP.greaterThan, EXP.lessOrEqual, EXP.greaterOrEqual:
case EXP.equal:
case EXP.notEqual:
if (vecsize == 16)
{
// float[4] comparison needs SSE support (CMP{EQ,NEQ,LT,LE}PS)
if (elemty == TY.Tfloat32 && cpu >= CPU.sse)
supported = true;
// double[2] comparison needs SSE2 support (CMP{EQ,NEQ,LT,LE}PD)
else if (elemty == TY.Tfloat64 && cpu >= CPU.sse2)
supported = true;
else if (tvec.isintegral())
{
if (elemty == TY.Tint64 || elemty == TY.Tuns64)
{
// (u)long[2] equality needs SSE4.1 support (PCMPEQQ)
if ((op == EXP.equal || op == EXP.notEqual) && cpu >= CPU.sse4_1)
supported = true;
// (u)long[2] comparison needs SSE4.2 support (PCMPGTQ)
else if (cpu >= CPU.sse4_2)
supported = true;
}
// (u)byte[16]/short[8]/int[4] comparison needs SSE2 support (PCMP{EQ,GT}[BWD])
else if (cpu >= CPU.sse2)
supported = true;
}
}
else if (vecsize == 32)
{
// float[8]/double[4] comparison needs AVX support (VCMP{EQ,NEQ,LT,LE}P[SD])
if (tvec.isfloating() && cpu >= CPU.avx)
supported = true;
// (u)byte[32]/short[16]/int[8]/long[4] comparison needs AVX2 support (VPCMP{EQ,GT}[BWDQ])
else if (tvec.isintegral() && cpu >= CPU.avx2)
supported = true;
}
break;
case EXP.leftShift, EXP.leftShiftAssign, EXP.rightShift, EXP.rightShiftAssign, EXP.unsignedRightShift, EXP.unsignedRightShiftAssign:
supported = false;
break;
case EXP.add, EXP.addAssign, EXP.min, EXP.minAssign:
if (vecsize == 16)
{
// float[4] add/sub needs SSE support ({V}ADDPS, {V}SUBPS)
if (elemty == TY.Tfloat32 && cpu >= CPU.sse)
supported = true;
// double[2] add/sub needs SSE2 support ({V}ADDPD, {V}SUBPD)
else if (elemty == TY.Tfloat64 && cpu >= CPU.sse2)
supported = true;
// (u)byte[16]/short[8]/int[4]/long[2] add/sub needs SSE2 support ({V}PADD[BWDQ], {V}PSUB[BWDQ])
else if (tvec.isintegral() && cpu >= CPU.sse2)
supported = true;
}
else if (vecsize == 32)
{
// float[8]/double[4] add/sub needs AVX support (VADDP[SD], VSUBP[SD])
if (tvec.isfloating() && cpu >= CPU.avx)
supported = true;
// (u)byte[32]/short[16]/int[8]/long[4] add/sub needs AVX2 support (VPADD[BWDQ], VPSUB[BWDQ])
else if (tvec.isintegral() && cpu >= CPU.avx2)
supported = true;
}
break;
case EXP.mul, EXP.mulAssign:
if (vecsize == 16)
{
// float[4] multiply needs SSE support ({V}MULPS)
if (elemty == TY.Tfloat32 && cpu >= CPU.sse)
supported = true;
// double[2] multiply needs SSE2 support ({V}MULPD)
else if (elemty == TY.Tfloat64 && cpu >= CPU.sse2)
supported = true;
// (u)short[8] multiply needs SSE2 support ({V}PMULLW)
else if ((elemty == TY.Tint16 || elemty == TY.Tuns16) && cpu >= CPU.sse2)
supported = true;
// (u)int[4] multiply needs SSE4.1 support ({V}PMULLD)
else if ((elemty == TY.Tint32 || elemty == TY.Tuns32) && cpu >= CPU.sse4_1)
supported = true;
}
else if (vecsize == 32)
{
// float[8]/double[4] multiply needs AVX support (VMULP[SD])
if (tvec.isfloating() && cpu >= CPU.avx)
supported = true;
// (u)short[16] multiply needs AVX2 support (VPMULLW)
else if ((elemty == TY.Tint16 || elemty == TY.Tuns16) && cpu >= CPU.avx2)
supported = true;
// (u)int[8] multiply needs AVX2 support (VPMULLD)
else if ((elemty == TY.Tint32 || elemty == TY.Tuns32) && cpu >= CPU.avx2)
supported = true;
}
break;
case EXP.div, EXP.divAssign:
if (vecsize == 16)
{
// float[4] divide needs SSE support ({V}DIVPS)
if (elemty == TY.Tfloat32 && cpu >= CPU.sse)
supported = true;
// double[2] divide needs SSE2 support ({V}DIVPD)
else if (elemty == TY.Tfloat64 && cpu >= CPU.sse2)
supported = true;
}
else if (vecsize == 32)
{
// float[8]/double[4] multiply needs AVX support (VDIVP[SD])
if (tvec.isfloating() && cpu >= CPU.avx)
supported = true;
}
break;
case EXP.mod, EXP.modAssign:
supported = false;
break;
case EXP.and, EXP.andAssign, EXP.or, EXP.orAssign, EXP.xor, EXP.xorAssign:
// (u)byte[16]/short[8]/int[4]/long[2] bitwise ops needs SSE2 support ({V}PAND, {V}POR, {V}PXOR)
if (vecsize == 16 && tvec.isintegral() && cpu >= CPU.sse2)
supported = true;
// (u)byte[32]/short[16]/int[8]/long[4] bitwise ops needs AVX2 support (VPAND, VPOR, VPXOR)
else if (vecsize == 32 && tvec.isintegral() && cpu >= CPU.avx2)
supported = true;
break;
case EXP.not:
supported = false;
break;
case EXP.tilde:
// (u)byte[16]/short[8]/int[4]/long[2] logical exclusive needs SSE2 support ({V}PXOR)
if (vecsize == 16 && tvec.isintegral() && cpu >= CPU.sse2)
supported = true;
// (u)byte[32]/short[16]/int[8]/long[4] logical exclusive needs AVX2 support (VPXOR)
else if (vecsize == 32 && tvec.isintegral() && cpu >= CPU.avx2)
supported = true;
break;
case EXP.pow, EXP.powAssign:
supported = false;
break;
default:
// import std.stdio : stderr, writeln;
// stderr.writeln(op);
assert(0, "unhandled op " ~ EXPtoString(cast(EXP)op));
}
return supported;
}
/**
* Default system linkage for the target.
* Returns:
* `LINK` to use for `extern(System)`
*/
extern (C++) LINK systemLinkage() @safe
{
return os == Target.OS.Windows ? 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
* null if the type is a `void` or argtypes aren't supported by the target
*/
extern (C++) TypeTuple toArgTypes(Type t)
{
import dmd.argtypes_x86 : toArgTypes_x86;
import dmd.argtypes_sysv_x64 : toArgTypes_sysv_x64;
if (isX86_64)
{
// no argTypes for Win64 yet
return isPOSIX ? toArgTypes_sysv_x64(t) : null;
}
return toArgTypes_x86(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++) bool isReturnOnStack(TypeFunction tf, bool needsThis)
{
import dmd.id : Id;
import dmd.argtypes_sysv_x64 : toArgTypes_sysv_x64;
import dmd.typesem : castMod;
if (tf.isref)
{
//printf(" ref false\n");
return false; // returns a pointer
}
Type tn = tf.next;
if (auto te = tn.isTypeEnum())
{
if (te.sym.isSpecial())
{
// Special enums with target-specific return style
if (te.sym.ident == Id.__c_complex_float)
tn = Type.tcomplex32.castMod(tn.mod);
else if (te.sym.ident == Id.__c_complex_double)
tn = Type.tcomplex64.castMod(tn.mod);
else if (te.sym.ident == Id.__c_complex_real)
tn = Type.tcomplex80.castMod(tn.mod);
}
}
tn = tn.toBasetype();