-
-
Notifications
You must be signed in to change notification settings - Fork 594
/
backconfig.c
481 lines (441 loc) · 12.7 KB
/
backconfig.c
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
/**
* Compiler implementation of the
* $(LINK2 http://www.dlang.org, D programming language).
*
* Copyright: Copyright (c) 2000-2017 by Digital Mars, All Rights Reserved
* Authors: $(LINK2 http://www.digitalmars.com, Walter Bright)
* License: Distributed under the Boost Software License, Version 1.0.
* http://www.boost.org/LICENSE_1_0.txt
* Source: https://github.com/dlang/dmd/blob/master/src/ddmd/backend/backconfig.c
*/
// Configure the back end (optimizer and code generator)
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "cc.h"
#include "global.h"
#include "oper.h"
#include "code.h"
#include "type.h"
#include "dt.h"
#include "cgcv.h"
static char __file__[] = __FILE__; /* for tassert.h */
#include "tassert.h"
#if MARS
extern void ph_init();
#endif
/**************************************
* Initialize configuration variables.
*/
void out_config_init(
int model, // 32: 32 bit code
// 64: 64 bit code
// Windows: set bit 0 to generate MS-COFF instead of OMF
bool exe, // true: exe file
// false: dll or shared library (generate PIC code)
bool trace, // add profiling code
bool nofloat, // do not pull in floating point code
bool verbose, // verbose compile
bool optimize, // optimize code
int symdebug, // add symbolic debug information
// 1: D
// 2: fake it with C symbolic debug info
bool alwaysframe, // always create standard function frame
bool stackstomp, // add stack stomping code
bool avx, // use AVX instruction set
bool betterC // implement "Better C"
)
{
#if MARS
//printf("out_config_init()\n");
if (!config.target_cpu)
{ config.target_cpu = TARGET_PentiumPro;
config.target_scheduler = config.target_cpu;
}
config.fulltypes = CVNONE;
config.fpxmmregs = FALSE;
config.inline8087 = 1;
config.memmodel = 0;
config.flags |= CFGuchar; // make sure TYchar is unsigned
tytab[TYchar] |= TYFLuns;
bool mscoff = model & 1;
model &= 32 | 64;
#if TARGET_WINDOS
if (model == 64)
{ config.exe = EX_WIN64;
config.fpxmmregs = TRUE;
config.avx = avx;
config.ehmethod = EH_DM;
// Not sure we really need these two lines, try removing them later
config.flags |= CFGnoebp;
config.flags |= CFGalwaysframe;
config.flags |= CFGromable; // put switch tables in code segment
config.objfmt = OBJ_MSCOFF;
}
else
{
config.exe = EX_WIN32;
config.ehmethod = EH_WIN32;
config.objfmt = mscoff ? OBJ_MSCOFF : OBJ_OMF;
}
if (exe)
config.wflags |= WFexe; // EXE file only optimizations
config.flags4 |= CFG4underscore;
#endif
#if TARGET_LINUX
if (model == 64)
{ config.exe = EX_LINUX64;
config.ehmethod = EH_DWARF;
config.fpxmmregs = TRUE;
config.avx = avx;
}
else
{
config.exe = EX_LINUX;
config.ehmethod = EH_DWARF;
if (!exe)
config.flags |= CFGromable; // put switch tables in code segment
}
config.flags |= CFGnoebp;
if (!exe)
{
config.flags3 |= CFG3pic;
config.flags |= CFGalwaysframe; // PIC needs a frame for TLS fixups
}
config.objfmt = OBJ_ELF;
#endif
#if TARGET_OSX
config.fpxmmregs = TRUE;
config.avx = avx;
if (model == 64)
{ config.exe = EX_OSX64;
config.fpxmmregs = TRUE;
config.ehmethod = EH_DWARF;
}
else
{
config.exe = EX_OSX;
config.ehmethod = EH_DWARF;
}
config.flags |= CFGnoebp;
if (!exe)
{
config.flags3 |= CFG3pic;
config.flags |= CFGalwaysframe; // PIC needs a frame for TLS fixups
}
config.flags |= CFGromable; // put switch tables in code segment
config.objfmt = OBJ_MACH;
#endif
#if TARGET_FREEBSD
if (model == 64)
{ config.exe = EX_FREEBSD64;
config.ehmethod = EH_DWARF;
config.fpxmmregs = TRUE;
config.avx = avx;
}
else
{
config.exe = EX_FREEBSD;
config.ehmethod = EH_DWARF;
if (!exe)
config.flags |= CFGromable; // put switch tables in code segment
}
config.flags |= CFGnoebp;
if (!exe)
{
config.flags3 |= CFG3pic;
config.flags |= CFGalwaysframe; // PIC needs a frame for TLS fixups
}
config.objfmt = OBJ_ELF;
#endif
#if TARGET_OPENBSD
if (model == 64)
{ config.exe = EX_OPENBSD64;
config.fpxmmregs = TRUE;
config.avx = avx;
}
else
{
config.exe = EX_OPENBSD;
if (!exe)
config.flags |= CFGromable; // put switch tables in code segment
}
config.flags |= CFGnoebp;
config.flags |= CFGalwaysframe;
if (!exe)
config.flags3 |= CFG3pic;
config.objfmt = OBJ_ELF;
config.ehmethod = EH_DM;
#endif
#if TARGET_SOLARIS
if (model == 64)
{ config.exe = EX_SOLARIS64;
config.fpxmmregs = TRUE;
config.avx = avx;
}
else
{
config.exe = EX_SOLARIS;
if (!exe)
config.flags |= CFGromable; // put switch tables in code segment
}
config.flags |= CFGnoebp;
config.flags |= CFGalwaysframe;
if (!exe)
config.flags3 |= CFG3pic;
config.objfmt = OBJ_ELF;
config.ehmethod = EH_DM;
#endif
config.flags2 |= CFG2nodeflib; // no default library
config.flags3 |= CFG3eseqds;
#if 0
if (env->getEEcontext()->EEcompile != 2)
config.flags4 |= CFG4allcomdat;
if (env->nochecks())
config.flags4 |= CFG4nochecks; // no runtime checking
#elif TARGET_OSX
#else
config.flags4 |= CFG4allcomdat;
#endif
if (trace)
config.flags |= CFGtrace; // turn on profiler
if (nofloat)
config.flags3 |= CFG3wkfloat;
configv.verbose = verbose;
if (optimize)
go_flag((char *)"-o");
if (symdebug)
{
#if SYMDEB_DWARF
configv.addlinenumbers = 1;
config.fulltypes = (symdebug == 1) ? CVDWARF_D : CVDWARF_C;
#endif
#if SYMDEB_CODEVIEW
if (config.objfmt == OBJ_MSCOFF)
{
configv.addlinenumbers = 1;
config.fulltypes = CV8;
if(symdebug > 1)
config.flags2 |= CFG2gms;
}
else
{
configv.addlinenumbers = 1;
config.fulltypes = CV4;
}
#endif
if (!optimize)
config.flags |= CFGalwaysframe;
}
else
{
configv.addlinenumbers = 0;
config.fulltypes = CVNONE;
//config.flags &= ~CFGalwaysframe;
}
if (alwaysframe)
config.flags |= CFGalwaysframe;
if (stackstomp)
config.flags2 |= CFG2stomp;
config.betterC = betterC;
ph_init();
block_init();
cod3_setdefault();
if (model == 64)
{
util_set64();
type_init();
cod3_set64();
}
else
{
util_set32();
type_init();
cod3_set32();
}
rtlsym_init(); // uses fregsaved, so must be after it's set inside cod3_set*
#endif
}
#ifdef DEBUG
/****************************
* Transmit internal compiler debugging flags.
*/
void out_config_debug(
bool b,
bool c,
bool f,
bool r,
bool w,
bool x,
bool y
)
{
debugb = b;
debugc = c;
debugf = f;
debugr = r;
debugw = w;
debugx = x;
debugy = y;
}
#endif
/*************************************
*/
void util_set16()
{
// The default is 16 bits
_tysize[TYldouble] = 10;
_tysize[TYildouble] = 10;
_tysize[TYcldouble] = 20;
_tyalignsize[TYldouble] = 2;
_tyalignsize[TYildouble] = 2;
_tyalignsize[TYcldouble] = 2;
}
/*******************************
* Redo tables from 8086/286 to 386/486.
*/
void util_set32()
{
_tyrelax[TYenum] = TYlong;
_tyrelax[TYint] = TYlong;
_tyrelax[TYuint] = TYlong;
tyequiv[TYint] = TYlong;
tyequiv[TYuint] = TYulong;
_tysize[TYenum] = LONGSIZE;
_tysize[TYint ] = LONGSIZE;
_tysize[TYuint] = LONGSIZE;
_tysize[TYnullptr] = LONGSIZE;
_tysize[TYnptr] = LONGSIZE;
_tysize[TYnref] = LONGSIZE;
#if TARGET_LINUX || TARGET_FREEBSD || TARGET_OPENBSD || TARGET_SOLARIS
_tysize[TYldouble] = 12;
_tysize[TYildouble] = 12;
_tysize[TYcldouble] = 24;
#elif TARGET_OSX
_tysize[TYldouble] = 16;
_tysize[TYildouble] = 16;
_tysize[TYcldouble] = 32;
#elif TARGET_WINDOS
_tysize[TYldouble] = 10;
_tysize[TYildouble] = 10;
_tysize[TYcldouble] = 20;
#else
assert(0);
#endif
_tysize[TYsptr] = LONGSIZE;
_tysize[TYcptr] = LONGSIZE;
_tysize[TYfptr] = 6; // NOTE: There are codgen test that check
_tysize[TYvptr] = 6; // _tysize[x] == _tysize[TYfptr] so don't set
_tysize[TYfref] = 6; // _tysize[TYfptr] to _tysize[TYnptr]
_tyalignsize[TYenum] = LONGSIZE;
_tyalignsize[TYint ] = LONGSIZE;
_tyalignsize[TYuint] = LONGSIZE;
_tyalignsize[TYnullptr] = LONGSIZE;
_tyalignsize[TYnref] = LONGSIZE;
_tyalignsize[TYnptr] = LONGSIZE;
#if TARGET_LINUX || TARGET_FREEBSD || TARGET_OPENBSD || TARGET_SOLARIS
_tyalignsize[TYldouble] = 4;
_tyalignsize[TYildouble] = 4;
_tyalignsize[TYcldouble] = 4;
#elif TARGET_OSX
_tyalignsize[TYldouble] = 16;
_tyalignsize[TYildouble] = 16;
_tyalignsize[TYcldouble] = 16;
#elif TARGET_WINDOS
_tyalignsize[TYldouble] = 2;
_tyalignsize[TYildouble] = 2;
_tyalignsize[TYcldouble] = 2;
#else
assert(0);
#endif
_tyalignsize[TYsptr] = LONGSIZE;
_tyalignsize[TYcptr] = LONGSIZE;
}
/*******************************
* Redo tables from 8086/286 to I64.
*/
void util_set64()
{
_tyrelax[TYenum] = TYlong;
_tyrelax[TYint] = TYlong;
_tyrelax[TYuint] = TYlong;
tyequiv[TYint] = TYlong;
tyequiv[TYuint] = TYulong;
_tysize[TYenum] = LONGSIZE;
_tysize[TYint ] = LONGSIZE;
_tysize[TYuint] = LONGSIZE;
_tysize[TYnullptr] = 8;
_tysize[TYnptr] = 8;
_tysize[TYnref] = 8;
#if TARGET_LINUX || TARGET_FREEBSD || TARGET_OPENBSD || TARGET_SOLARIS || TARGET_OSX
_tysize[TYldouble] = 16;
_tysize[TYildouble] = 16;
_tysize[TYcldouble] = 32;
#elif TARGET_WINDOS
_tysize[TYldouble] = 10;
_tysize[TYildouble] = 10;
_tysize[TYcldouble] = 20;
#else
assert(0);
#endif
_tysize[TYsptr] = 8;
_tysize[TYcptr] = 8;
_tysize[TYfptr] = 10; // NOTE: There are codgen test that check
_tysize[TYvptr] = 10; // _tysize[x] == _tysize[TYfptr] so don't set
_tysize[TYfref] = 10; // _tysize[TYfptr] to _tysize[TYnptr]
_tyalignsize[TYenum] = LONGSIZE;
_tyalignsize[TYint ] = LONGSIZE;
_tyalignsize[TYuint] = LONGSIZE;
_tyalignsize[TYnullptr] = 8;
_tyalignsize[TYnptr] = 8;
_tyalignsize[TYnref] = 8;
#if TARGET_LINUX || TARGET_FREEBSD || TARGET_OPENBSD || TARGET_SOLARIS
_tyalignsize[TYldouble] = 16;
_tyalignsize[TYildouble] = 16;
_tyalignsize[TYcldouble] = 16;
#elif TARGET_OSX
_tyalignsize[TYldouble] = 16;
_tyalignsize[TYildouble] = 16;
_tyalignsize[TYcldouble] = 16;
#elif TARGET_WINDOS
_tyalignsize[TYldouble] = 2;
_tyalignsize[TYildouble] = 2;
_tyalignsize[TYcldouble] = 2;
#else
assert(0);
#endif
_tyalignsize[TYsptr] = 8;
_tyalignsize[TYcptr] = 8;
_tyalignsize[TYfptr] = 8;
_tyalignsize[TYvptr] = 8;
_tyalignsize[TYfref] = 8;
tytab[TYjfunc] &= ~TYFLpascal; // set so caller cleans the stack (as in C)
TYptrdiff = TYllong;
TYsize = TYullong;
TYsize_t = TYullong;
TYdelegate = TYcent;
TYdarray = TYucent;
}
// cc.d
unsigned Srcpos::sizeCheck() { return sizeof(Srcpos); }
unsigned Pstate::sizeCheck() { return sizeof(Pstate); }
unsigned Cstate::sizeCheck() { return sizeof(Cstate); }
unsigned Blockx::sizeCheck() { return sizeof(Blockx); }
unsigned block::sizeCheck() { return sizeof(block); }
unsigned func_t::sizeCheck() { return sizeof(func_t); }
unsigned baseclass_t::sizeCheck() { return sizeof(baseclass_t); }
unsigned template_t::sizeCheck() { return sizeof(template_t); }
unsigned struct_t::sizeCheck() { return sizeof(struct_t); }
unsigned Symbol::sizeCheck() { return sizeof(Symbol); }
unsigned param_t::sizeCheck() { return sizeof(param_t); }
unsigned Declar::sizeCheck() { return sizeof(Declar); }
unsigned dt_t::sizeCheck() { return sizeof(dt_t); }
// cdef.d
unsigned Config::sizeCheck() { return sizeof(Config); }
unsigned Configv::sizeCheck() { return sizeof(Configv); }
unsigned eve::sizeCheck() { return sizeof(eve); }
// el.d
// type.d
unsigned TYPE::sizeCheck() { return sizeof(type); }