/
cgen.d
543 lines (468 loc) · 12.1 KB
/
cgen.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
/**
* Compiler implementation of the
* $(LINK2 http://www.dlang.org, D programming language).
*
* Copyright: Copyright (C) 1985-1998 by Symantec
* Copyright (C) 2000-2019 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/backend/cgen.d, backend/cgen.d)
*/
module dmd.backend.cgen;
version (SCPP)
version = COMPILE;
version (MARS)
version = COMPILE;
version (COMPILE)
{
import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.string;
import dmd.backend.cc;
import dmd.backend.cdef;
import dmd.backend.code;
import dmd.backend.code_x86;
import dmd.backend.codebuilder;
import dmd.backend.mem;
import dmd.backend.el;
import dmd.backend.global;
import dmd.backend.obj;
import dmd.backend.ty;
import dmd.backend.type;
version (SCPP)
{
import msgs2;
}
extern (C++):
nothrow:
dt_t *dt_get_nzeros(uint n);
extern __gshared CGstate cgstate;
/*****************************
* Find last code in list.
*/
code *code_last(code *c)
{
if (c)
{ while (c.next)
c = c.next;
}
return c;
}
/*****************************
* Set flag bits on last code in list.
*/
void code_orflag(code *c,uint flag)
{
if (flag && c)
{ while (c.next)
c = c.next;
c.Iflags |= flag;
}
}
/*****************************
* Set rex bits on last code in list.
*/
void code_orrex(code *c,uint rex)
{
if (rex && c)
{ while (c.next)
c = c.next;
c.Irex |= rex;
}
}
/*****************************
* Concatenate two code lists together. Return pointer to result.
*/
code *cat(code *c1,code *c2)
{ code **pc;
if (!c1)
return c2;
for (pc = &c1.next; *pc; pc = &(*pc).next)
{ }
*pc = c2;
return c1;
}
/*****************************
* Add code to end of linked list.
* Note that unused operands are garbage.
* gen1() and gen2() are shortcut routines.
* Input:
* c -> linked list that code is to be added to end of
* cs -> data for the code
* Returns:
* pointer to start of code list
*/
code *gen(code *c,code *cs)
{
debug assert(cs);
assert(I64 || cs.Irex == 0);
code* ce = code_malloc();
*ce = *cs;
//printf("ce = %p %02x\n", ce, ce.Iop);
//ccheck(ce);
simplify_code(ce);
ce.next = null;
if (c)
{ code* cstart = c;
while (code_next(c)) c = code_next(c); /* find end of list */
c.next = ce; /* link into list */
return cstart;
}
return ce;
}
code *gen1(code *c,opcode_t op)
{
code* ce;
code* cstart;
ce = code_calloc();
ce.Iop = op;
//ccheck(ce);
assert(op != LEA);
if (c)
{ cstart = c;
while (code_next(c)) c = code_next(c); /* find end of list */
c.next = ce; /* link into list */
return cstart;
}
return ce;
}
code *gen2(code *c,opcode_t op,uint rm)
{
code* ce;
code* cstart;
cstart = ce = code_calloc();
/*cxcalloc++;*/
ce.Iop = op;
ce.Iea = rm;
//ccheck(ce);
if (c)
{ cstart = c;
while (code_next(c)) c = code_next(c); /* find end of list */
c.next = ce; /* link into list */
}
return cstart;
}
code *gen2sib(code *c,opcode_t op,uint rm,uint sib)
{
code* ce;
code* cstart;
cstart = ce = code_calloc();
/*cxcalloc++;*/
ce.Iop = op;
ce.Irm = cast(ubyte)rm;
ce.Isib = cast(ubyte)sib;
ce.Irex = cast(ubyte)((rm | (sib & (REX_B << 16))) >> 16);
if (sib & (REX_R << 16))
ce.Irex |= REX_X;
//ccheck(ce);
if (c)
{ cstart = c;
while (code_next(c)) c = code_next(c); /* find end of list */
c.next = ce; /* link into list */
}
return cstart;
}
code *genc2(code *c,opcode_t op,uint ea,targ_size_t EV2)
{ code cs;
cs.Iop = op;
cs.Iea = ea;
//ccheck(&cs);
cs.Iflags = CFoff;
cs.IFL2 = FLconst;
cs.IEV2.Vsize_t = EV2;
return gen(c,&cs);
}
/*****************
* Generate code.
*/
code *genc(code *c,opcode_t op,uint ea,uint FL1,targ_size_t EV1,uint FL2,targ_size_t EV2)
{ code cs;
assert(FL1 < FLMAX);
cs.Iop = op;
cs.Iea = ea;
//ccheck(&cs);
cs.Iflags = CFoff;
cs.IFL1 = cast(ubyte)FL1;
cs.IEV1.Vsize_t = EV1;
assert(FL2 < FLMAX);
cs.IFL2 = cast(ubyte)FL2;
cs.IEV2.Vsize_t = EV2;
return gen(c,&cs);
}
/********************************
* Generate 'instruction' which is actually a line number.
*/
code *genlinnum(code *c,Srcpos srcpos)
{ code cs;
//srcpos.print("genlinnum");
cs.Iop = ESCAPE | ESClinnum;
cs.IEV1.Vsrcpos = srcpos;
return gen(c,&cs);
}
/*****************************
* Prepend line number to existing code.
*/
void cgen_prelinnum(code **pc,Srcpos srcpos)
{
*pc = cat(genlinnum(null,srcpos),*pc);
}
/********************************
* Generate 'instruction' which tells the scheduler that the fpu stack has
* changed.
*/
code *genadjfpu(code *c, int offset)
{ code cs;
if (!I16 && offset)
{
cs.Iop = ESCAPE | ESCadjfpu;
cs.IEV1.Vint = offset;
return gen(c,&cs);
}
else
return c;
}
/********************************
* Generate 'nop'
*/
code *gennop(code *c)
{
return gen1(c,NOP);
}
/****************************************
* Clean stack after call to codelem().
*/
void gencodelem(ref CodeBuilder cdb,elem *e,regm_t *pretregs,bool constflag)
{
if (e)
{
uint stackpushsave;
int stackcleansave;
stackpushsave = stackpush;
stackcleansave = cgstate.stackclean;
cgstate.stackclean = 0; // defer cleaning of stack
codelem(cdb,e,pretregs,constflag);
assert(cgstate.stackclean == 0);
cgstate.stackclean = stackcleansave;
genstackclean(cdb,stackpush - stackpushsave,*pretregs); // do defered cleaning
}
}
/**********************************
* Determine if one of the registers in regm has value in it.
* If so, return !=0 and set *preg to which register it is.
*/
bool reghasvalue(regm_t regm,targ_size_t value,reg_t *preg)
{
//printf("reghasvalue(%s, %llx)\n", regm_str(regm), cast(ulong)value);
/* See if another register has the right value */
reg_t r = 0;
for (regm_t mreg = regcon.immed.mval; mreg; mreg >>= 1)
{
if (mreg & regm & 1 && regcon.immed.value[r] == value)
{ *preg = r;
return true;
}
r++;
regm >>= 1;
}
return false;
}
/**************************************
* Load a register from the mask regm with value.
* Output:
* *preg the register selected
*/
void regwithvalue(ref CodeBuilder cdb,regm_t regm,targ_size_t value,reg_t *preg,regm_t flags)
{
//printf("regwithvalue(value = %lld)\n", (long long)value);
reg_t reg;
if (!preg)
preg = ®
// If we don't already have a register with the right value in it
if (!reghasvalue(regm,value,preg))
{
regm_t save = regcon.immed.mval;
allocreg(cdb,®m,preg,TYint); // allocate register
regcon.immed.mval = save;
movregconst(cdb,*preg,value,flags); // store value into reg
}
}
/************************
* When we don't know whether a function symbol is defined or not
* within this module, we stuff it in an array of references to be
* fixed up later.
*/
struct Fixup
{
Symbol *sym; // the referenced Symbol
int seg; // where the fixup is going (CODE or DATA, never UDATA)
int flags; // CFxxxx
targ_size_t offset; // addr of reference to Symbol
targ_size_t val; // value to add into location
static if (TARGET_OSX)
{
Symbol *funcsym; // function the Symbol goes in
}
}
struct FixupArray
{
nothrow:
Fixup *ptr;
size_t dim, cap;
void push(ref Fixup e)
{
if (dim == cap)
{
// 0x800 determined experimentally to minimize reallocations
cap = cap
? (3 * cap) / 2 // use 'Tau' of 1.5
: 0x800;
ptr = cast(Fixup *)mem_realloc(ptr, cap * Fixup.sizeof);
}
ptr[dim++] = e;
}
ref Fixup opIndex(size_t idx)
{
assert(idx < dim);
return ptr[idx];
}
void clear()
{
dim = 0;
}
}
private __gshared FixupArray fixups;
/****************************
* Add to the fix list.
*/
size_t addtofixlist(Symbol *s,targ_size_t offset,int seg,targ_size_t val,int flags)
{
static immutable ubyte[8] zeros = 0;
//printf("addtofixlist(%p '%s')\n",s,s.Sident);
assert(I32 || flags);
Fixup f;
f.sym = s;
f.offset = offset;
f.seg = seg;
f.flags = flags;
f.val = val;
static if (TARGET_OSX)
{
f.funcsym = funcsym_p;
}
fixups.push(f);
size_t numbytes;
static if (TARGET_SEGMENTED)
{
switch (flags & (CFoff | CFseg))
{
case CFoff: numbytes = tysize(TYnptr); break;
case CFseg: numbytes = 2; break;
case CFoff | CFseg: numbytes = tysize(TYfptr); break;
default: assert(0);
}
}
else
{
numbytes = tysize(TYnptr);
if (I64 && !(flags & CFoffset64))
numbytes = 4;
static if (TARGET_WINDOS)
{
/* This can happen when generating CV8 data
*/
if (flags & CFseg)
numbytes += 2;
}
}
debug assert(numbytes <= zeros.sizeof);
objmod.bytes(seg,offset,cast(uint)numbytes,cast(ubyte*)zeros.ptr);
return numbytes;
}
static if (0)
{
void searchfixlist (Symbol *s )
{
//printf("searchfixlist(%s)\n", s.Sident);
}
}
/****************************
* Output fixups as references to external or static Symbol.
* First emit data for still undefined static Symbols or mark non-static Symbols as SCextern.
*/
private void outfixup(ref Fixup f)
{
symbol_debug(f.sym);
//printf("outfixup '%s' offset %04x\n", f.sym.Sident, f.offset);
static if (TARGET_SEGMENTED)
{
if (tybasic(f.sym.ty()) == TYf16func)
{
Obj.far16thunk(f.sym); /* make it into a thunk */
objmod.reftoident(f.seg, f.offset, f.sym, f.val, f.flags);
return;
}
}
if (f.sym.Sxtrnnum == 0)
{
if (f.sym.Sclass == SCstatic)
{
version (SCPP)
{
if (f.sym.Sdt)
{
outdata(f.sym);
}
else if (f.sym.Sseg == UNKNOWN)
synerr(EM_no_static_def,prettyident(f.sym)); // no definition found for static
}
else // MARS
{
// OBJ_OMF does not set Sxtrnnum for static Symbols, so check
// whether the Symbol was assigned to a segment instead, compare
// outdata(Symbol *s)
if (f.sym.Sseg == UNKNOWN)
{
printf("Error: no definition for static %s\n", prettyident(f.sym)); // no definition found for static
err_exit(); // BUG: do better
}
}
}
else if (f.sym.Sflags & SFLwasstatic)
{
// Put it in BSS
f.sym.Sclass = SCstatic;
f.sym.Sfl = FLunde;
f.sym.Sdt = dt_get_nzeros(cast(uint)type_size(f.sym.Stype));
outdata(f.sym);
}
else if (f.sym.Sclass != SCsinline)
{
f.sym.Sclass = SCextern; /* make it external */
objmod.external(f.sym);
if (f.sym.Sflags & SFLweak)
objmod.wkext(f.sym, null);
}
}
static if (TARGET_OSX)
{
Symbol *funcsymsave = funcsym_p;
funcsym_p = f.funcsym;
objmod.reftoident(f.seg, f.offset, f.sym, f.val, f.flags);
funcsym_p = funcsymsave;
}
else
{
objmod.reftoident(f.seg, f.offset, f.sym, f.val, f.flags);
}
}
/****************************
* End of module. Output fixups as references
* to external Symbols.
*/
void outfixlist()
{
for (size_t i = 0; i < fixups.dim; ++i)
outfixup(fixups[i]);
fixups.clear();
}
}