-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmsc.c
408 lines (357 loc) · 9.46 KB
/
msc.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
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include "mars.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"
extern void ph_init();
extern Global global;
extern int REALSIZE;
Config config;
Configv configv;
struct Environment;
/**************************************
* Initialize config variables.
*/
void out_config_init()
{
//printf("out_config_init()\n");
Param *params = &global.params;
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;
#if TARGET_WINDOS
if (params->is64bit)
config.exe = EX_WIN64;
else
config.exe = EX_NT;
// Win32 eh
config.flags2 |= CFG2seh;
if (params->run)
config.wflags |= WFexe; // EXE file only optimizations
else if (params->link && !global.params.deffile)
config.wflags |= WFexe; // EXE file only optimizations
else if (params->exefile) // if writing out EXE file
{ size_t len = strlen(params->exefile);
if (len >= 4 && stricmp(params->exefile + len - 3, "exe") == 0)
config.wflags |= WFexe;
}
config.flags4 |= CFG4underscore;
#endif
#if TARGET_LINUX
if (params->is64bit)
{ config.exe = EX_LINUX64;
config.fpxmmregs = TRUE;
}
else
config.exe = EX_LINUX;
config.flags |= CFGnoebp;
config.flags |= CFGalwaysframe;
if (params->pic)
config.flags3 |= CFG3pic;
#endif
#if TARGET_OSX
config.fpxmmregs = TRUE;
if (params->is64bit)
{ config.exe = EX_OSX64;
config.fpxmmregs = TRUE;
}
else
config.exe = EX_OSX;
config.flags |= CFGnoebp;
config.flags |= CFGalwaysframe;
if (params->pic)
config.flags3 |= CFG3pic;
#endif
#if TARGET_FREEBSD
if (params->is64bit)
{ config.exe = EX_FREEBSD64;
config.fpxmmregs = TRUE;
}
else
config.exe = EX_FREEBSD;
config.flags |= CFGnoebp;
config.flags |= CFGalwaysframe;
if (params->pic)
config.flags3 |= CFG3pic;
#endif
#if TARGET_OPENBSD
if (params->is64bit)
{ config.exe = EX_OPENBSD64;
config.fpxmmregs = TRUE;
}
else
config.exe = EX_OPENBSD;
config.flags |= CFGnoebp;
config.flags |= CFGalwaysframe;
if (params->pic)
config.flags3 |= CFG3pic;
#endif
#if TARGET_SOLARIS
if (params->is64bit)
{ config.exe = EX_SOLARIS64;
config.fpxmmregs = TRUE;
}
else
config.exe = EX_SOLARIS;
config.flags |= CFGnoebp;
config.flags |= CFGalwaysframe;
if (params->pic)
config.flags3 |= CFG3pic;
#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 (params->trace)
config.flags |= CFGtrace; // turn on profiler
if (params->nofloat)
config.flags3 |= CFG3wkfloat;
configv.verbose = params->verbose;
if (params->optimize)
go_flag((char *)"-o");
if (params->symdebug)
{
#if ELFOBJ || MACHOBJ
configv.addlinenumbers = 1;
config.fulltypes = (params->symdebug == 1) ? CVDWARF_D : CVDWARF_C;
#endif
#if OMFOBJ
configv.addlinenumbers = 1;
config.fulltypes = CV4;
#endif
if (!params->optimize)
config.flags |= CFGalwaysframe;
}
else
{
configv.addlinenumbers = 0;
config.fulltypes = CVNONE;
//config.flags &= ~CFGalwaysframe;
}
if (params->alwaysframe)
config.flags &= ~CFGalwaysframe;
#ifdef DEBUG
debugb = params->debugb;
debugc = params->debugc;
debugf = params->debugf;
debugr = params->debugr;
debugw = params->debugw;
debugx = params->debugx;
debugy = params->debugy;
#endif
}
/*******************************
* Redo tables from 8086/286 to ILP32
*/
void util_set32()
{
_tyrelax[TYenum] = TYlong;
_tyrelax[TYint] = TYlong;
_tyrelax[TYuint] = TYlong;
tyequiv[TYint] = TYlong;
tyequiv[TYuint] = TYulong;
for (int i = 0; i < 1; ++i)
{ tysize[TYenum + i] = LONGSIZE;
tysize[TYint + i] = LONGSIZE;
tysize[TYuint + i] = LONGSIZE;
tysize[TYjhandle + i] = LONGSIZE;
tysize[TYnptr + i] = LONGSIZE;
tysize[TYnref + i] = LONGSIZE;
}
for (int i = 0; i < 1; ++i)
{ tyalignsize[TYenum + i] = LONGSIZE;
tyalignsize[TYint + i] = LONGSIZE;
tyalignsize[TYuint + i] = LONGSIZE;
tyalignsize[TYnullptr + i] = LONGSIZE;
tyalignsize[TYnptr + i] = LONGSIZE;
tyalignsize[TYnref + i] = LONGSIZE;
}
}
/*******************************
* Redo tables from 8086/286 to LP64.
*/
void util_set64()
{
_tyrelax[TYenum] = TYlong;
_tyrelax[TYint] = TYlong;
_tyrelax[TYuint] = TYlong;
tyequiv[TYint] = TYlong;
tyequiv[TYuint] = TYulong;
for (int i = 0; i < 1; ++i)
{ tysize[TYenum + i] = LONGSIZE;
tysize[TYint + i] = LONGSIZE;
tysize[TYuint + i] = LONGSIZE;
tysize[TYnptr + i] = 8;
tysize[TYnref + i] = 8;
tysize[TYldouble + i] = REALSIZE;
tysize[TYildouble + i] = REALSIZE;
tysize[TYcldouble + i] = 2 * REALSIZE;
tyalignsize[TYenum + i] = LONGSIZE;
tyalignsize[TYint + i] = LONGSIZE;
tyalignsize[TYuint + i] = LONGSIZE;
tyalignsize[TYnullptr + i] = 8;
tyalignsize[TYnptr + i] = 8;
tyalignsize[TYnref + i] = 8;
#if TARGET_LINUX || TARGET_FREEBSD || TARGET_OPENBSD || TARGET_SOLARIS || TARGET_OSX
tyalignsize[TYldouble + i] = 16;
tyalignsize[TYildouble + i] = 16;
tyalignsize[TYcldouble + i] = 16;
#else
assert(0);
#endif
tytab[TYjfunc + i] &= ~TYFLpascal; // set so caller cleans the stack (as in C)
}
TYptrdiff = TYllong;
TYsize = TYullong;
TYsize_t = TYullong;
}
/***********************************
* Return aligned 'offset' if it is of size 'size'.
*/
targ_size_t align(targ_size_t size, targ_size_t offset)
{
switch (size)
{
case 1:
break;
case 2:
case 4:
case 8:
offset = (offset + size - 1) & ~(size - 1);
break;
default:
if (size >= 16)
offset = (offset + 15) & ~15;
else
offset = (offset + REGSIZE - 1) & ~(REGSIZE - 1);
break;
}
return offset;
}
/*******************************
* Get size of ty
*/
targ_size_t size(tym_t ty)
{
int sz = (tybasic(ty) == TYvoid) ? 1 : tysize(ty);
#ifdef DEBUG
if (sz == -1)
WRTYxx(ty);
#endif
assert(sz!= -1);
return sz;
}
/*******************************
* Replace (e) with ((stmp = e),stmp)
*/
elem *exp2_copytotemp(elem *e)
{
//printf("exp2_copytotemp()\n");
elem_debug(e);
Symbol *stmp = symbol_genauto(e);
elem *eeq = el_bin(OPeq,e->Ety,el_var(stmp),e);
elem *er = el_bin(OPcomma,e->Ety,eeq,el_var(stmp));
if (tybasic(e->Ety) == TYstruct || tybasic(e->Ety) == TYarray)
{
eeq->Eoper = OPstreq;
eeq->ET = e->ET;
eeq->E1->ET = e->ET;
er->ET = e->ET;
er->E2->ET = e->ET;
}
return er;
}
/****************************
* Generate symbol of type ty at DATA:offset
*/
symbol *symboldata(targ_size_t offset,tym_t ty)
{
symbol *s = symbol_generate(SClocstat, type_fake(ty));
s->Sfl = FLdata;
s->Soffset = offset;
s->Sseg = DATA;
symbol_keep(s); // keep around
return s;
}
/************************************
* Add symbol to slist.
*/
static list_t slist;
void slist_add(Symbol *s)
{
list_prepend(&slist,s);
}
/*************************************
*/
void slist_reset()
{
//printf("slist_reset()\n");
for (list_t sl = slist; sl; sl = list_next(sl))
{ Symbol *s = list_symbol(sl);
#if MACHOBJ
s->Soffset = 0;
#endif
s->Sxtrnnum = 0;
s->Stypidx = 0;
s->Sflags &= ~(STRoutdef | SFLweak);
if (s->Sclass == SCglobal || s->Sclass == SCcomdat ||
s->Sfl == FLudata || s->Sclass == SCstatic)
{ s->Sclass = SCextern;
s->Sfl = FLextern;
}
}
}
/**************************************
*/
void backend_init()
{
ph_init();
block_init();
if (global.params.is64bit)
{
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*
out_config_init();
}
void backend_term()
{
}