-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplatform.d
270 lines (252 loc) · 6.08 KB
/
platform.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
// Written in the D programming language
// Author: Timon Gehr
// License: http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0
import std.traits,std.string,std.conv,std.exception;
import util;
enum Compiler{
TGD, // TODO: fix
DigitalMars,
GNU,
LDC,
SDC,
}
enum OS{
FreeStanding,
linux,
FreeBSD,
OpenBSD,
NetBSD,
DragonFlyBSD,
BSD,
Solaris,
AIX,
Haiku,
SkyOS,
SysV3,
SysV4,
Hurd,
Android,
Cygwin,
MinGW,
OSX,
Win32,
Win64,
}
enum CRuntime{
NA,
Bionic,
DigitalMars,
Glibc,
Microsoft,
}
enum Arch{
NA,
X86,
X86_64,
ARM,
AArch64,
Epiphany,
PPC,
PPC64,
IA64,
MIPS32,
MIPS64,
NVPTX,
NVPTX64,
SPARC,
SPARC64,
S390,
SystemZ,
HPPA,
HPPA64,
SH,
SH64,
Alpha,
}
size_t wordSize(Arch arch){
final switch(arch) with(Arch){
case X86: return 32;
case X86_64: return 64;
case ARM: return 32;
case AArch64: return 64;
case Epiphany: enforce(0,"TODO");
case PPC: return 32;
case PPC64: return 64;
case IA64: return 64;
case MIPS32: return 32;
case MIPS64: return 64;
case NVPTX: return 32;
case NVPTX64: return 64;
case SPARC: return 32;
case SPARC64: return 64;
case S390: return 32;
case SystemZ: return 32;
case HPPA: return 32;
case HPPA64: return 64;
case SH: return 32;
case SH64: return 64;
case Alpha: return 32; // TODO: check this
case NA: assert(0);
}
}
enum FloatABI{
NA,
SoftFloat,
SoftFP,
HardFloat
}
bool needFloatABI(Arch arch){
with(Arch) return util.among(arch,ARM,PPC,PPC64,MIPS32,MIPS64,SPARC,SPARC64,Alpha); // TODO: check this
}
enum MipsABI{
NA,
O32,
N32,
O64,
N64,
EABI,
}
enum Endianness{
LittleEndian,
BigEndian,
}
enum BinaryFormat{
NA,
ELFv1,
ELFv2,
}
enum InlineASM{
NA,
X86,
X86_64,
}
immutable reservedVersions={
string[] r=["unittest","assert","none","all"];
void add(T)(){
foreach(x;EnumMembers!T){ auto y=to!string(x); if(y!="NA") r~=to!string(y); }
}
add!Compiler;
add!OS; r~="Posix"; r~="Windows";
foreach(crt;EnumMembers!CRuntime){
if(crt==CRuntime.NA) continue;
r~="CRuntime_"~to!string(crt);
}
add!Arch;
foreach(arch;EnumMembers!Arch){
if(arch==Arch.PPC64) continue; // PPC
if(!needFloatABI(arch)) continue;
string str=arch.to!string;
with(Arch) if(util.among(arch,MIPS64,SPARC64)) continue;
with(Arch) if(util.among(arch,MIPS32)) str=str[0..$-2];
static assert(EnumMembers!FloatABI.length==4); // (might need to change)
foreach(x;EnumMembers!FloatABI){
if(x==FloatABI.NA) continue;
if(x==FloatABI.SoftFP && arch!=Arch.ARM) continue;
r~=str~"_"~to!string(x);
}
}
foreach(mabi;EnumMembers!MipsABI){
if(mabi==MipsABI.NA) continue;
r~="MIPS_"~to!string(mabi);
}
add!Endianness;
add!BinaryFormat;
foreach(iasm;EnumMembers!InlineASM){
if(iasm==InlineASM.NA) continue;
r~="D_InlineAsm_"~to!string(iasm);
}
foreach(m;__traits(allMembers,Platform)){
static if(is(typeof({auto r=__traits(getMember,Platform(),m); return r;}())==bool)){
static assert(m.startsWith("D_")||m=="ARM_Thumb"||m=="SPARC_V8Plus");
static if(!util.among(m,"D_Unittest","D_Assert")) r~=m;
}
}
r~=["darwin","Thumb","S390X"]; // deprecated
return r;
}();
//pragma(msg, reservedVersions);
import std.algorithm,std.array;
static assert(sort(reservedVersions.map!(x=>x[]).array)==sort(import("reservedversions").split('\n').array));
//pragma(msg,sort(reservedVersions.map!(x=>x[]).array).setDifference(sort(import("predefinedversions").split('\n').array)).array);
//pragma(msg,sort(import("predefinedversions").split('\n').array).setDifference(sort(reservedVersions.map!(x=>x[]).array)).array);
struct Platform{
enum compiler = Compiler.init;
auto os = OS.linux;
auto cruntime=CRuntime.Glibc;
auto arch = Arch.X86_64;
bool ARM_Thumb=false;
bool SPARC_V8Plus=false;
auto floatABI = FloatABI.NA;
auto mipsABI = MipsABI.NA;
auto endianness = Endianness.LittleEndian;
auto binaryFormat = BinaryFormat.ELFv2; // TODO
bool D_Coverage=false;
bool D_Ddoc=false;
bool D_Warnings=false;
@property D_InlineASM(){
switch(arch) with(Arch){
case X86: return InlineASM.X86;
case X86_64: return InlineASM.X86_64;
default: return InlineASM.NA;
}
}
bool D_X32=false;
@property size_t wordSize(){ return .wordSize(arch); }
@property size_t pointerSize(){
auto wsiz=wordSize;
if(D_X32){ assert(wsiz==64); return 32; }
return wsiz;
}
@property bool D_LP64(){ return pointerSize==64; }
bool D_HardFloat=true;
@property bool D_SoftFloat(){ return !D_HardFloat; }
bool D_PIC=false;
bool D_SIMD=true;
enum D_Version2=true;
bool D_NoBoundsChecks=false;
bool D_ObjectiveC=false;
bool D_Unittest=false;
bool D_Assert=true;
string[] predefinedVersions(){
string[] r=["all"];
r~=to!string(compiler);
r~=to!string(os);
if(os!=OS.Win32&&os!=OS.Win64) r~="Posix"; // TODO: fix
else r~="Windows";
r~=to!string(cruntime);
r~=to!string(arch);
if(needFloatABI(arch)){
assert(floatABI != FloatABI.NA);
auto str=to!string(arch);
with(Arch) if(util.among(arch,MIPS32,MIPS64,SPARC64)) str=str[0..$-2];
r~=str~"_"~to!string(floatABI);
}else assert(floatABI == FloatABI.NA);
assert(arch==Arch.ARM||!ARM_Thumb);
assert(arch==Arch.SPARC||arch==Arch.SPARC64||!SPARC_V8Plus);
assert(floatABI!=FloatABI.HardFloat||D_HardFloat);
assert(arch==Arch.MIPS32||arch==Arch.MIPS64||mipsABI==MipsABI.NA);
if(mipsABI!=MipsABI.NA){
r~=to!string(mipsABI);
if(mipsABI!=MipsABI.EABI){
with(MipsABI) assert(arch==Arch.MIPS32 && util.among(mipsABI,O32,N32)||
arch==Arch.MIPS64 && util.among(mipsABI,O64,N64)); // TODO: check this
}
}
r~=to!string(endianness);
if(binaryFormat!=BinaryFormat.NA) r~=to!string(binaryFormat);
if(D_InlineASM!=InlineASM.NA) r~="D_InlineASM_"~to!string(D_InlineASM);
foreach(m;__traits(allMembers,Platform)){
static if(is(typeof({auto r=__traits(getMember,this,m); return r;}())==bool)){
if(__traits(getMember,this,m)) if(!util.among(m,"D_Unittest","D_Assert")) r~=m;
}
}
if(D_Unittest) r~="unittest";
if(D_Assert) r~="assert";
// deprecated versions:
if(os==OS.OSX) r~="darwin";
if(ARM_Thumb) r~="Thumb";
if(arch==Arch.SystemZ) r~="S390X"; // TODO: check this
return r;
}
}
// pragma(msg,Platform().predefinedVersions);