forked from facebook/hhvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathirgen-basic.cpp
More file actions
464 lines (406 loc) · 13.1 KB
/
irgen-basic.cpp
File metadata and controls
464 lines (406 loc) · 13.1 KB
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
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-2016 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 |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/base/stats.h"
#include "hphp/runtime/base/strings.h"
#include "hphp/runtime/vm/jit/irgen-interpone.h"
#include "hphp/runtime/vm/jit/irgen-exit.h"
#include "hphp/runtime/vm/jit/irgen-internal.h"
namespace HPHP { namespace jit { namespace irgen {
namespace {
//////////////////////////////////////////////////////////////////////
void implAGet(IRGS& env, SSATmp* classSrc) {
if (classSrc->type() <= TStr) {
push(env, ldCls(env, classSrc));
return;
}
push(env, gen(env, LdObjClass, classSrc));
}
const StaticString s_FATAL_NULL_THIS(Strings::FATAL_NULL_THIS);
void checkThis(IRGS& env, SSATmp* ctx) {
ifThen(
env,
[&] (Block* taken) {
gen(env, CheckCtxThis, taken, ctx);
},
[&] {
hint(env, Block::Hint::Unlikely);
auto const err = cns(env, s_FATAL_NULL_THIS.get());
gen(env, RaiseError, err);
}
);
}
//////////////////////////////////////////////////////////////////////
}
void emitAGetC(IRGS& env) {
auto const name = topC(env);
if (name->type().subtypeOfAny(TObj, TStr)) {
popC(env);
implAGet(env, name);
decRef(env, name);
} else {
interpOne(env, TCls, 1);
}
}
void emitAGetL(IRGS& env, int32_t id) {
auto const ldrefExit = makeExit(env);
auto const ldPMExit = makePseudoMainExit(env);
auto const src = ldLocInner(env, id, ldrefExit, ldPMExit, DataTypeSpecific);
if (src->type().subtypeOfAny(TObj, TStr)) {
implAGet(env, src);
} else {
PUNT(AGetL);
}
}
void emitCGetL(IRGS& env, int32_t id) {
auto const ldrefExit = makeExit(env);
auto const ldPMExit = makePseudoMainExit(env);
auto const loc = ldLocInnerWarn(
env,
id,
ldrefExit,
ldPMExit,
DataTypeCountnessInit
);
pushIncRef(env, loc);
}
void emitCGetQuietL(IRGS& env, int32_t id) {
auto const ldrefExit = makeExit(env);
auto const ldPMExit = makePseudoMainExit(env);
auto const loc = ldLocInner(
env,
id,
ldrefExit,
ldPMExit,
DataTypeCountnessInit
);
pushIncRef(env, loc);
}
void emitCUGetL(IRGS& env, int32_t id) {
auto const ldrefExit = makeExit(env);
auto const ldPMExit = makePseudoMainExit(env);
pushIncRef(env, ldLocInner(env, id, ldrefExit, ldPMExit, DataTypeGeneric));
}
void emitPushL(IRGS& env, int32_t id) {
assertTypeLocal(env, id, TInitCell); // bytecode invariant
auto* locVal = ldLoc(env, id, makeExit(env), DataTypeGeneric);
push(env, locVal);
stLocRaw(env, id, fp(env), cns(env, TUninit));
}
void emitCGetL2(IRGS& env, int32_t id) {
auto const ldrefExit = makeExit(env);
auto const ldPMExit = makePseudoMainExit(env);
auto const oldTop = pop(env, DataTypeGeneric);
auto const val = ldLocInnerWarn(
env,
id,
ldrefExit,
ldPMExit,
DataTypeCountnessInit
);
pushIncRef(env, val);
push(env, oldTop);
}
template<class F>
SSATmp* boxHelper(IRGS& env, SSATmp* value, F rewrite) {
auto const t = value->type();
if (t <= TCell) {
if (t <= TUninit) {
value = cns(env, TInitNull);
}
value = gen(env, Box, value);
rewrite(value);
} else if (t.maybe(TCell)) {
value = cond(env,
[&](Block* taken) {
auto const ret = gen(env, CheckType, TBoxedInitCell,
taken, value);
env.irb->constrainValue(ret, DataTypeSpecific);
return ret;
},
[&](SSATmp* box) { // Next: value is Boxed
return box;
},
[&] { // Taken: value is not Boxed
auto const tmpType = t - TBoxedInitCell;
assertx(tmpType <= TCell);
auto const tmp = gen(env, AssertType, tmpType, value);
auto const ret = gen(env, Box, tmp);
rewrite(ret);
return ret;
});
}
return value;
}
void emitVGetL(IRGS& env, int32_t id) {
auto const value = ldLoc(env, id, makeExit(env), DataTypeCountnessInit);
auto const boxed = boxHelper(
env,
gen(env, AssertType, TCell | TBoxedInitCell, value),
[&] (SSATmp* v) {
stLocRaw(env, id, fp(env), v);
});
pushIncRef(env, boxed);
}
void emitBox(IRGS& env) {
push(env, gen(env, Box, pop(env, DataTypeGeneric)));
}
void emitBoxR(IRGS& env) {
auto const value = pop(env, DataTypeGeneric);
auto const boxed = boxHelper(
env,
gen(env, AssertType, TCell | TBoxedInitCell, value),
[] (SSATmp* ) {});
push(env, boxed);
}
void emitUnsetL(IRGS& env, int32_t id) {
auto const prev = ldLoc(env, id, makeExit(env), DataTypeCountness);
stLocRaw(env, id, fp(env), cns(env, TUninit));
decRef(env, prev);
}
void emitBindL(IRGS& env, int32_t id) {
if (curFunc(env)->isPseudoMain()) {
interpOne(env, TBoxedInitCell, 1);
return;
}
auto const ldPMExit = makePseudoMainExit(env);
auto const newValue = popV(env);
// Note that the IncRef must happen first, for correctness in a
// pseudo-main: the destructor could decref the value again after
// we've stored it into the local.
pushIncRef(env, newValue);
auto const oldValue = ldLoc(env, id, ldPMExit, DataTypeSpecific);
stLocRaw(env, id, fp(env), newValue);
decRef(env, oldValue);
}
void emitSetL(IRGS& env, int32_t id) {
auto const ldrefExit = makeExit(env);
auto const ldPMExit = makePseudoMainExit(env);
// since we're just storing the value in a local, this function doesn't care
// about the type of the value. stLoc needs to IncRef the value so it may
// constrain it further.
auto const src = popC(env, DataTypeGeneric);
pushStLoc(env, id, ldrefExit, ldPMExit, src);
}
void emitInitThisLoc(IRGS& env, int32_t id) {
if (!curClass(env)) {
// Do nothing if this is null
return;
}
auto const ldrefExit = makeExit(env);
auto const oldLoc = ldLoc(env, id, ldrefExit, DataTypeCountness);
auto const ctx = gen(env, LdCtx, fp(env));
gen(env, CheckCtxThis, makeExitSlow(env), ctx);
auto const this_ = gen(env, CastCtxThis, ctx);
gen(env, IncRef, this_);
stLocRaw(env, id, fp(env), this_);
decRef(env, oldLoc);
}
void emitPrint(IRGS& env) {
auto const type = topC(env)->type();
if (!type.subtypeOfAny(TInt, TBool, TNull, TStr)) {
interpOne(env, TInt, 1);
return;
}
auto const cell = popC(env);
Opcode op;
if (type <= TStr) {
op = PrintStr;
} else if (type <= TInt) {
op = PrintInt;
} else if (type <= TBool) {
op = PrintBool;
} else {
assertx(type <= TNull);
op = Nop;
}
// the print helpers decref their arg, so don't decref pop'ed value
if (op != Nop) {
gen(env, op, cell);
}
push(env, cns(env, 1));
}
void emitUnbox(IRGS& env) {
auto const exit = makeExit(env);
auto const srcBox = popV(env);
auto const unboxed = unbox(env, srcBox, exit);
pushIncRef(env, unboxed);
decRef(env, srcBox);
}
void emitThis(IRGS& env) {
auto const ctx = gen(env, LdCtx, fp(env));
checkThis(env, ctx);
auto const this_ = gen(env, CastCtxThis, ctx);
pushIncRef(env, this_);
}
void emitCheckThis(IRGS& env) {
auto const ctx = gen(env, LdCtx, fp(env));
checkThis(env, ctx);
}
void emitBareThis(IRGS& env, BareThisOp subop) {
if (!curClass(env)) {
interpOne(env, TInitNull, 0); // will raise notice and push null
return;
}
auto const ctx = gen(env, LdCtx, fp(env));
if (subop == BareThisOp::NeverNull) {
env.irb->fs().setThisAvailable();
} else {
gen(env, CheckCtxThis, makeExitSlow(env), ctx);
}
pushIncRef(env, gen(env, CastCtxThis, ctx));
}
void emitClone(IRGS& env) {
if (!topC(env)->isA(TObj)) PUNT(Clone-NonObj);
auto const obj = popC(env);
push(env, gen(env, Clone, obj));
decRef(env, obj);
}
void emitLateBoundCls(IRGS& env) {
auto const clss = curClass(env);
if (!clss) {
// no static context class, so this will raise an error
interpOne(env, TCls, 0);
return;
}
auto const ctx = ldCtx(env);
push(env, gen(env, LdClsCtx, ctx));
}
void emitSelf(IRGS& env) {
auto const clss = curClass(env);
if (clss == nullptr) {
interpOne(env, TCls, 0);
} else {
push(env, cns(env, clss));
}
}
void emitParent(IRGS& env) {
auto const clss = curClass(env);
if (clss == nullptr || clss->parent() == nullptr) {
interpOne(env, TCls, 0);
} else {
push(env, cns(env, clss->parent()));
}
}
void emitNameA(IRGS& env) {
push(env, gen(env, LdClsName, popA(env)));
}
//////////////////////////////////////////////////////////////////////
void emitCastArray(IRGS& env) {
auto const src = popC(env);
push(
env,
[&] {
if (src->isA(TArr)) {
return cond(
env,
[&](Block* taken) {
return gen(
env,
CheckType,
Type::Array(ArrayData::kVecKind),
taken,
src
);
},
[&](SSATmp* vec) { return gen(env, ConvVecToArr, vec); },
[&] {
return cond(
env,
[&](Block* taken) {
return gen(
env,
CheckType,
Type::Array(ArrayData::kDictKind),
taken,
src
);
},
[&](SSATmp* dict) { return gen(env, ConvDictToArr, dict); },
[&] { return src; }
);
}
);
}
if (src->isA(TNull)) return cns(env, staticEmptyArray());
if (src->isA(TBool)) return gen(env, ConvBoolToArr, src);
if (src->isA(TDbl)) return gen(env, ConvDblToArr, src);
if (src->isA(TInt)) return gen(env, ConvIntToArr, src);
if (src->isA(TStr)) return gen(env, ConvStrToArr, src);
if (src->isA(TObj)) return gen(env, ConvObjToArr, src);
return gen(env, ConvCellToArr, src);
}()
);
}
void emitCastBool(IRGS& env) {
auto const src = popC(env);
push(env, gen(env, ConvCellToBool, src));
decRef(env, src);
}
void emitCastDouble(IRGS& env) {
auto const src = popC(env);
push(env, gen(env, ConvCellToDbl, src));
decRef(env, src);
}
void emitCastInt(IRGS& env) {
auto const src = popC(env);
push(env, gen(env, ConvCellToInt, src));
decRef(env, src);
}
void emitCastObject(IRGS& env) {
auto const src = popC(env);
push(env, gen(env, ConvCellToObj, src));
}
void emitCastString(IRGS& env) {
auto const src = popC(env);
push(env, gen(env, ConvCellToStr, src));
decRef(env, src);
}
void emitIncStat(IRGS& env, int32_t counter, int32_t value) {
if (!Stats::enabled()) return;
gen(env, IncStat, cns(env, counter), cns(env, value), cns(env, false));
}
//////////////////////////////////////////////////////////////////////
void emitPopA(IRGS& env) { popA(env); }
void emitPopC(IRGS& env) { popDecRef(env, DataTypeGeneric); }
void emitPopV(IRGS& env) { popDecRef(env, DataTypeGeneric); }
void emitPopR(IRGS& env) { popDecRef(env, DataTypeGeneric); }
void emitDir(IRGS& env) { push(env, cns(env, curUnit(env)->dirpath())); }
void emitFile(IRGS& env) { push(env, cns(env, curUnit(env)->filepath())); }
void emitDup(IRGS& env) { pushIncRef(env, topC(env)); }
//////////////////////////////////////////////////////////////////////
void emitArray(IRGS& env, const ArrayData* x) { push(env, cns(env, x)); }
void emitString(IRGS& env, const StringData* s) { push(env, cns(env, s)); }
void emitInt(IRGS& env, int64_t val) { push(env, cns(env, val)); }
void emitDouble(IRGS& env, double val) { push(env, cns(env, val)); }
void emitTrue(IRGS& env) { push(env, cns(env, true)); }
void emitFalse(IRGS& env) { push(env, cns(env, false)); }
void emitNull(IRGS& env) { push(env, cns(env, TInitNull)); }
void emitNullUninit(IRGS& env) { push(env, cns(env, TUninit)); }
//////////////////////////////////////////////////////////////////////
void emitNop(IRGS&) {}
void emitBoxRNop(IRGS& env) {
assertTypeStack(env, BCSPRelOffset{0}, TBoxedCell);
}
void emitUnboxRNop(IRGS& env) {
assertTypeStack(env, BCSPRelOffset{0}, TCell);
}
void emitRGetCNop(IRGS&) {}
void emitFPassC(IRGS&, int32_t) {}
void emitFPassVNop(IRGS&, int32_t) {}
void emitDefClsNop(IRGS&, Id) {}
void emitBreakTraceHint(IRGS&) {}
//////////////////////////////////////////////////////////////////////
}}}