/
irstate.d
267 lines (247 loc) · 7.24 KB
/
irstate.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
/**
* Compiler implementation of the
* $(LINK2 http://www.dlang.org, D programming language).
*
* Copyright: Copyright (C) 1999-2018 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/irstate.d, _irstate.d)
* Documentation: https://dlang.org/phobos/dmd_irstate.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/irstate.d
*/
module dmd.irstate;
import dmd.root.array;
import dmd.arraytypes;
import dmd.backend.type;
import dmd.dclass;
import dmd.dmodule;
import dmd.dsymbol;
import dmd.func;
import dmd.identifier;
import dmd.statement;
import dmd.globals;
import dmd.mtype;
import dmd.backend.cc;
import dmd.backend.el;
/****************************************
* Our label symbol, with vector to keep track of forward references.
*/
struct Label
{
block *lblock; // The block to which the label is defined.
block *fwdrefs; // The first use of the label before it is defined.
}
/***********************************************************
*/
struct IRState
{
IRState* prev;
Statement statement;
Module m; // module
private FuncDeclaration symbol; // function that code is being generate for
Identifier ident;
Symbol* shidden; // hidden parameter to function
Symbol* sthis; // 'this' parameter to function (member and nested)
Symbol* sclosure; // pointer to closure instance
Blockx* blx;
Dsymbols* deferToObj; // array of Dsymbol's to run toObjFile(bool multiobj) on later
elem* ehidden; // transmit hidden pointer to CallExp::toElem()
Symbol* startaddress;
Array!(elem*)* varsInScope; // variables that are in scope that will need destruction later
Label*[void*]* labels; // table of labels used/declared in function
bool mayThrow; // the expression being evaluated may throw
block* breakBlock;
block* contBlock;
block* switchBlock;
block* defaultBlock;
block* finallyBlock;
this(IRState* irs, Statement s)
{
prev = irs;
statement = s;
if (irs)
{
m = irs.m;
shidden = irs.shidden;
sclosure = irs.sclosure;
sthis = irs.sthis;
blx = irs.blx;
deferToObj = irs.deferToObj;
varsInScope = irs.varsInScope;
labels = irs.labels;
mayThrow = irs.mayThrow;
}
}
this(Module m, FuncDeclaration fd, Array!(elem*)* varsInScope, Dsymbols* deferToObj, Label*[void*]* labels)
{
this.m = m;
this.symbol = fd;
this.varsInScope = varsInScope;
this.deferToObj = deferToObj;
this.labels = labels;
mayThrow = global.params.useExceptions
&& ClassDeclaration.throwable
&& !(fd && fd.eh_none);
}
/****
* Access labels AA from C++ code.
* Params:
* s = key
* Returns:
* pointer to value if it's there, null if not
*/
Label** lookupLabel(Statement s)
{
return cast(void*)s in *labels;
}
/****
* Access labels AA from C++ code.
* Params:
* s = key
* label = value
*/
void insertLabel(Statement s, Label* label)
{
(*labels)[cast(void*)s] = label;
}
block* getBreakBlock(Identifier ident)
{
IRState* bc;
if (ident)
{
Statement related = null;
block* ret = null;
for (bc = &this; bc; bc = bc.prev)
{
// The label for a breakBlock may actually be some levels up (e.g.
// on a try/finally wrapping a loop). We'll see if this breakBlock
// is the one to return once we reach that outer statement (which
// in many cases will be this same statement).
if (bc.breakBlock)
{
related = bc.statement.getRelatedLabeled();
ret = bc.breakBlock;
}
if (bc.statement == related && bc.prev.ident == ident)
return ret;
}
}
else
{
for (bc = &this; bc; bc = bc.prev)
{
if (bc.breakBlock)
return bc.breakBlock;
}
}
return null;
}
block* getContBlock(Identifier ident)
{
IRState* bc;
if (ident)
{
block* ret = null;
for (bc = &this; bc; bc = bc.prev)
{
// The label for a contBlock may actually be some levels up (e.g.
// on a try/finally wrapping a loop). We'll see if this contBlock
// is the one to return once we reach that outer statement (which
// in many cases will be this same statement).
if (bc.contBlock)
{
ret = bc.contBlock;
}
if (bc.prev && bc.prev.ident == ident)
return ret;
}
}
else
{
for (bc = &this; bc; bc = bc.prev)
{
if (bc.contBlock)
return bc.contBlock;
}
}
return null;
}
block* getSwitchBlock()
{
IRState* bc;
for (bc = &this; bc; bc = bc.prev)
{
if (bc.switchBlock)
return bc.switchBlock;
}
return null;
}
block* getDefaultBlock()
{
IRState* bc;
for (bc = &this; bc; bc = bc.prev)
{
if (bc.defaultBlock)
return bc.defaultBlock;
}
return null;
}
block* getFinallyBlock()
{
IRState* bc;
for (bc = &this; bc; bc = bc.prev)
{
if (bc.finallyBlock)
return bc.finallyBlock;
}
return null;
}
FuncDeclaration getFunc()
{
for (auto bc = &this; 1; bc = bc.prev)
{
if (!bc.prev)
return bc.symbol;
}
}
/**********************
* Returns:
* true if do array bounds checking for the current function
*/
bool arrayBoundsCheck()
{
bool result;
final switch (global.params.useArrayBounds)
{
case CHECKENABLE.off:
result = false;
break;
case CHECKENABLE.on:
result = true;
break;
case CHECKENABLE.safeonly:
{
result = false;
FuncDeclaration fd = getFunc();
if (fd)
{
Type t = fd.type;
if (t.ty == Tfunction && (cast(TypeFunction)t).trust == TRUST.safe)
result = true;
}
break;
}
case CHECKENABLE._default:
assert(0);
}
return result;
}
/****************************
* Returns:
* true if in a nothrow section of code
*/
bool isNothrow()
{
return !mayThrow;
}
}