forked from facebook/hhvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirgen-iter.cpp
352 lines (316 loc) · 10.5 KB
/
irgen-iter.cpp
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
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-2014 Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/vm/jit/normalized-instruction.h"
#include "hphp/runtime/vm/jit/irgen-exit.h"
#include "hphp/runtime/vm/jit/irgen-control.h"
#include "hphp/runtime/vm/jit/irgen-internal.h"
namespace HPHP { namespace jit { namespace irgen {
namespace {
//////////////////////////////////////////////////////////////////////
/*
* This function returns the offset of instruction i's branch target. This is
* normally the offset corresponding to the branch being taken. However, if i
* does not break a trace and it's followed in the trace by the instruction in
* the taken branch, then this function returns the offset of the i's
* fall-through instruction. In that case, the invertCond output argument is
* set to true; otherwise it's set to false.
*/
Offset iterBranchTarget(const NormalizedInstruction& i, bool& invertCond) {
assert(instrJumpOffset(reinterpret_cast<const Op*>(i.pc())) != nullptr);
auto targetOffset = i.offset() + i.imm[1].u_BA;
invertCond = false;
if (!i.endsRegion && i.nextOffset == targetOffset) {
invertCond = true;
targetOffset = i.offset() + instrLen((Op*)i.pc());
}
return targetOffset;
}
template<class Lambda>
void implMIterInit(HTS& env, Offset relOffset, Lambda genFunc) {
// TODO MIterInit doesn't check iterBranchTarget; this might be bug ...
auto const exit = makeExit(env);
auto const stack = spillStack(env);
env.irb->exceptionStackBoundary();
auto const pred = getStackInnerTypePrediction(stack, 0);
auto const src = topV(env);
if (!pred.subtypeOfAny(Type::Arr, Type::Obj)) {
PUNT(MIterInit-unsupportedSrcType);
}
// Guard the inner type before we call the helper.
gen(env, CheckRefInner, pred, exit, src);
auto const res = genFunc(src, pred);
auto const out = popV(env);
gen(env, DecRef, out);
implCondJmp(env, bcOff(env) + relOffset, true, res);
}
//////////////////////////////////////////////////////////////////////
}
void emitIterInit(HTS& env,
int32_t iterId,
Offset relOffset,
int32_t valLocalId) {
bool invertCond = false;
auto const targetOffset = iterBranchTarget(*env.currentNormalizedInstruction,
invertCond);
auto const src = popC(env);
if (!src->type().subtypeOfAny(Type::Arr, Type::Obj)) PUNT(IterInit);
auto const res = gen(
env,
IterInit,
Type::Bool,
IterData(iterId, -1, valLocalId),
src,
fp(env)
);
implCondJmp(env, targetOffset, !invertCond, res);
}
void emitIterInitK(HTS& env,
int32_t iterId,
Offset relOffset,
int32_t valLocalId,
int32_t keyLocalId) {
bool invertCond = false;
auto const targetOffset = iterBranchTarget(*env.currentNormalizedInstruction,
invertCond);
auto const src = popC(env);
if (!src->type().subtypeOfAny(Type::Arr, Type::Obj)) PUNT(IterInitK);
auto const res = gen(
env,
IterInitK,
Type::Bool,
IterData(iterId, keyLocalId, valLocalId),
src,
fp(env)
);
implCondJmp(env, targetOffset, !invertCond, res);
}
void emitIterNext(HTS& env,
int32_t iterId,
Offset relOffset,
int32_t valLocalId) {
surpriseCheck(env, relOffset);
bool invertCond = false;
auto const targetOffset = iterBranchTarget(*env.currentNormalizedInstruction,
invertCond);
auto const res = gen(
env,
IterNext,
Type::Bool,
IterData(iterId, -1, valLocalId),
fp(env)
);
implCondJmp(env, targetOffset, invertCond, res);
}
void emitIterNextK(HTS& env,
int32_t iterId,
Offset relOffset,
int32_t valLocalId,
int32_t keyLocalId) {
surpriseCheck(env, relOffset);
bool invertCond = false;
auto const targetOffset = iterBranchTarget(*env.currentNormalizedInstruction,
invertCond);
auto const res = gen(
env,
IterNextK,
Type::Bool,
IterData(iterId, keyLocalId, valLocalId),
fp(env)
);
implCondJmp(env, targetOffset, invertCond, res);
}
void emitWIterInit(HTS& env,
int32_t iterId,
Offset relOffset,
int32_t valLocalId) {
bool invertCond = false;
auto const targetOffset = iterBranchTarget(*env.currentNormalizedInstruction,
invertCond);
auto const src = popC(env);
if (!src->type().subtypeOfAny(Type::Arr, Type::Obj)) PUNT(WIterInit);
auto const res = gen(
env,
WIterInit,
Type::Bool,
IterData(iterId, -1, valLocalId),
src,
fp(env)
);
implCondJmp(env, targetOffset, !invertCond, res);
}
void emitWIterInitK(HTS& env,
int32_t iterId,
Offset relOffset,
int32_t valLocalId,
int32_t keyLocalId) {
bool invertCond = false;
auto const targetOffset = iterBranchTarget(*env.currentNormalizedInstruction,
invertCond);
auto const src = popC(env);
if (!src->type().subtypeOfAny(Type::Arr, Type::Obj)) PUNT(WIterInitK);
auto const res = gen(
env,
WIterInitK,
Type::Bool,
IterData(iterId, keyLocalId, valLocalId),
src,
fp(env)
);
implCondJmp(env, targetOffset, !invertCond, res);
}
void emitWIterNext(HTS& env,
int32_t iterId,
Offset relOffset,
int32_t valLocalId) {
surpriseCheck(env, relOffset);
bool invertCond = false;
auto const targetOffset = iterBranchTarget(*env.currentNormalizedInstruction,
invertCond);
auto const res = gen(
env,
WIterNext,
Type::Bool,
IterData(iterId, -1, valLocalId),
fp(env)
);
implCondJmp(env, targetOffset, invertCond, res);
}
void emitWIterNextK(HTS& env,
int32_t iterId,
Offset relOffset,
int32_t valLocalId,
int32_t keyLocalId) {
surpriseCheck(env, relOffset);
bool invertCond = false;
auto const targetOffset = iterBranchTarget(*env.currentNormalizedInstruction,
invertCond);
auto const res = gen(
env,
WIterNextK,
Type::Bool,
IterData(iterId, keyLocalId, valLocalId),
fp(env)
);
implCondJmp(env, targetOffset, invertCond, res);
}
void emitMIterInit(HTS& env,
int32_t iterId,
Offset relOffset,
int32_t valLocalId) {
implMIterInit(env, relOffset, [&] (SSATmp* src, Type type) {
return gen(
env,
MIterInit,
type,
IterData(iterId, -1, valLocalId),
src,
fp(env)
);
});
}
void emitMIterInitK(HTS& env,
int32_t iterId,
Offset relOffset,
int32_t valLocalId,
int32_t keyLocalId) {
implMIterInit(env, relOffset, [&] (SSATmp* src, Type type) {
return gen(
env,
MIterInitK,
type,
IterData(iterId, keyLocalId, valLocalId),
src,
fp(env)
);
});
}
void emitMIterNext(HTS& env,
int32_t iterId,
Offset relOffset,
int32_t valLocalId) {
surpriseCheck(env, relOffset);
auto const res = gen(
env,
MIterNext,
Type::Bool,
IterData(iterId, -1, valLocalId),
fp(env)
);
implCondJmp(env, bcOff(env) + relOffset, false, res);
}
void emitMIterNextK(HTS& env,
int32_t iterId,
Offset relOffset,
int32_t valLocalId,
int32_t keyLocalId) {
surpriseCheck(env, relOffset);
auto const res = gen(
env,
MIterNextK,
Type::Bool,
IterData(iterId, keyLocalId, valLocalId),
fp(env)
);
implCondJmp(env, bcOff(env) + relOffset, false, res);
}
void emitIterFree(HTS& env, int32_t iterId) {
gen(env, IterFree, IterId(iterId), fp(env));
}
void emitMIterFree(HTS& env, int32_t iterId) {
gen(env, MIterFree, IterId(iterId), fp(env));
}
void emitIterBreak(HTS& env,
const ImmVector& iv,
Offset relOffset) {
always_assert(env.currentNormalizedInstruction->endsRegion);
for (int iterIndex = 0; iterIndex < iv.size(); iterIndex += 2) {
IterKind iterKind = (IterKind)iv.vec32()[iterIndex];
Id iterId = iv.vec32()[iterIndex + 1];
switch (iterKind) {
case KindOfIter: gen(env, IterFree, IterId(iterId), fp(env)); break;
case KindOfMIter: gen(env, MIterFree, IterId(iterId), fp(env)); break;
case KindOfCIter: gen(env, CIterFree, IterId(iterId), fp(env)); break;
}
}
// Would need to change this if we support not ending regions on this:
gen(env, Jmp, makeExit(env, bcOff(env) + relOffset));
}
void emitDecodeCufIter(HTS& env, int32_t iterId, Offset relOffset) {
auto const src = popC(env);
auto const type = src->type();
if (type.subtypeOfAny(Type::Arr, Type::Str, Type::Obj)) {
auto const res = gen(
env,
DecodeCufIter,
Type::Bool,
IterId(iterId),
src,
fp(env)
);
gen(env, DecRef, src);
implCondJmp(env, bcOff(env) + relOffset, true, res);
} else {
gen(env, DecRef, src);
jmpImpl(env,
bcOff(env) + relOffset,
instrJmpFlags(*env.currentNormalizedInstruction));
}
}
void emitCIterFree(HTS& env, int32_t iterId) {
gen(env, CIterFree, IterId(iterId), fp(env));
}
//////////////////////////////////////////////////////////////////////
}}}