-
-
Notifications
You must be signed in to change notification settings - Fork 594
/
codebuilder.d
371 lines (321 loc) · 8.37 KB
/
codebuilder.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
/**
* 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/codebuilder.d, backend/_codebuilder.d)
* Documentation: https://dlang.org/phobos/dmd_backend_codebuilder.html
*/
module dmd.backend.codebuilder;
import core.stdc.stdio;
import core.stdc.string;
import dmd.backend.cc;
import dmd.backend.cdef;
import dmd.backend.code;
import dmd.backend.code_x86;
import dmd.backend.mem;
import dmd.backend.outbuf;
import dmd.backend.ty;
import dmd.backend.type;
extern (C++) struct CodeBuilder
{
private:
code *head;
code **pTail;
public:
//this() { pTail = &head; }
//this(code *c);
void ctor()
{
pTail = &head;
}
void ctor(code* c)
{
head = c;
pTail = c ? &code_last(c).next : &head;
}
code *finish()
{
return head;
}
code *peek() { return head; } // non-destructively look at the list
void reset() { head = null; pTail = &head; }
void append(ref CodeBuilder cdb)
{
if (cdb.head)
{
*pTail = cdb.head;
pTail = cdb.pTail;
}
}
void append(ref CodeBuilder cdb1, ref CodeBuilder cdb2)
{
append(cdb1);
append(cdb2);
}
void append(ref CodeBuilder cdb1, ref CodeBuilder cdb2, ref CodeBuilder cdb3)
{
append(cdb1);
append(cdb2);
append(cdb3);
}
void append(ref CodeBuilder cdb1, ref CodeBuilder cdb2, ref CodeBuilder cdb3, ref CodeBuilder cdb4)
{
append(cdb1);
append(cdb2);
append(cdb3);
append(cdb4);
}
void append(ref CodeBuilder cdb1, ref CodeBuilder cdb2, ref CodeBuilder cdb3, ref CodeBuilder cdb4, ref CodeBuilder cdb5)
{
append(cdb1);
append(cdb2);
append(cdb3);
append(cdb4);
append(cdb5);
}
void append(code *c)
{
if (c)
{
CodeBuilder cdb = void;
cdb.ctor(c);
append(cdb);
}
}
void gen(code *cs)
{
/* this is a high usage routine */
debug assert(cs);
assert(I64 || cs.Irex == 0);
code* ce = code_malloc();
*ce = *cs;
//printf("ce = %p %02x\n", ce, ce.Iop);
//code_print(ce);
ccheck(ce);
simplify_code(ce);
ce.next = null;
*pTail = ce;
pTail = &ce.next;
}
void gen1(opcode_t op)
{
code *ce = code_calloc();
ce.Iop = op;
ccheck(ce);
assert(op != LEA);
*pTail = ce;
pTail = &ce.next;
}
void gen2(opcode_t op, uint rm)
{
code *ce = code_calloc();
ce.Iop = op;
ce.Iea = rm;
ccheck(ce);
*pTail = ce;
pTail = &ce.next;
}
/***************************************
* Generate floating point instruction.
*/
void genf2(opcode_t op, uint rm)
{
genfwait(this);
gen2(op, rm);
}
void gen2sib(opcode_t op, uint rm, uint sib)
{
code *ce = code_calloc();
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);
*pTail = ce;
pTail = &ce.next;
}
/********************************
* Generate an ASM sequence.
*/
void genasm(char *s, uint slen)
{
code *ce = code_calloc();
ce.Iop = ASM;
ce.IFL1 = FLasm;
ce.IEV1.len = slen;
ce.IEV1.bytes = cast(char *) mem_malloc(slen);
memcpy(ce.IEV1.bytes,s,slen);
*pTail = ce;
pTail = &ce.next;
}
version (MARS)
{
void genasm(_LabelDsymbol *label)
{
code *ce = code_calloc();
ce.Iop = ASM;
ce.Iflags = CFaddrsize;
ce.IFL1 = FLblockoff;
ce.IEV1.Vsym = cast(Symbol*)label;
*pTail = ce;
pTail = &ce.next;
}
}
void genasm(block *label)
{
code *ce = code_calloc();
ce.Iop = ASM;
ce.Iflags = CFaddrsize;
ce.IFL1 = FLblockoff;
ce.IEV1.Vblock = label;
label.Bflags |= BFLlabel;
*pTail = ce;
pTail = &ce.next;
}
void gencs(opcode_t op, uint ea, uint FL2, Symbol *s)
{
code cs;
cs.Iop = op;
cs.Iflags = 0;
cs.Iea = ea;
ccheck(&cs);
cs.IFL2 = cast(ubyte)FL2;
cs.IEV2.Vsym = s;
cs.IEV2.Voffset = 0;
gen(&cs);
}
void genc2(opcode_t op, uint ea, targ_size_t EV2)
{
code cs;
cs.Iop = op;
cs.Iflags = 0;
cs.Iea = ea;
ccheck(&cs);
cs.Iflags = CFoff;
cs.IFL2 = FLconst;
cs.IEV2.Vsize_t = EV2;
gen(&cs);
}
void genc1(opcode_t op, uint ea, uint FL1, targ_size_t EV1)
{
code cs;
assert(FL1 < FLMAX);
cs.Iop = op;
cs.Iflags = CFoff;
cs.Iea = ea;
ccheck(&cs);
cs.IFL1 = cast(ubyte)FL1;
cs.IEV1.Vsize_t = EV1;
gen(&cs);
}
void genc(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;
gen(&cs);
}
/********************************
* Generate 'instruction' which is actually a line number.
*/
void genlinnum(Srcpos srcpos)
{
code cs;
//srcpos.print("genlinnum");
cs.Iop = ESCAPE | ESClinnum;
cs.Iflags = 0;
cs.Iea = 0;
cs.IEV1.Vsrcpos = srcpos;
gen(&cs);
}
/********************************
* Generate 'instruction' which tells the address resolver that the stack has
* changed.
*/
void genadjesp(int offset)
{
if (!I16 && offset)
{
code cs;
cs.Iop = ESCAPE | ESCadjesp;
cs.Iflags = 0;
cs.Iea = 0;
cs.IEV1.Vint = offset;
gen(&cs);
}
}
/********************************
* Generate 'instruction' which tells the scheduler that the fpu stack has
* changed.
*/
void genadjfpu(int offset)
{
if (!I16 && offset)
{
code cs;
cs.Iop = ESCAPE | ESCadjfpu;
cs.Iflags = 0;
cs.Iea = 0;
cs.IEV1.Vint = offset;
gen(&cs);
}
}
void gennop()
{
gen1(NOP);
}
/**************************
* Generate code to deal with floatreg.
*/
void genfltreg(opcode_t opcode,uint reg,targ_size_t offset)
{
floatreg = true;
reflocal = true;
if ((opcode & ~7) == 0xD8)
genfwait(this);
genc1(opcode,modregxrm(2,reg,BPRM),FLfltreg,offset);
}
void genxmmreg(opcode_t opcode,uint xreg,targ_size_t offset, tym_t tym)
{
assert(xreg >= XMM0);
floatreg = true;
reflocal = true;
genc1(opcode,modregxrm(2,xreg - XMM0,BPRM),FLfltreg,offset);
checkSetVex(last(), tym);
}
/*****************
* Returns:
* code that pTail points to
*/
code *last()
{
// g++ and clang++ complain about offsetof() because of the code::code() constructor.
// return (code *)((char *)pTail - offsetof(code, next));
// So do our own.
return cast(code *)(cast(void *)pTail - (cast(void*)&(*pTail).next - cast(void*)*pTail));
}
/*************************************
* Handy function to answer the question: who the heck is generating this piece of code?
*/
static void ccheck(code *cs)
{
// if (cs.Iop == LEA && (cs.Irm & 0x3F) == 0x34 && cs.Isib == 7) *(char*)0=0;
// if (cs.Iop == 0x31) *(char*)0=0;
// if (cs.Irm == 0x3D) *(char*)0=0;
// if (cs.Iop == LEA && cs.Irm == 0xCB) *(char*)0=0;
}
}