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
610 lines (533 loc) · 17.8 KB
/
Copy pathirgen-basic.cpp
File metadata and controls
610 lines (533 loc) · 17.8 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
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-present 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"
#include "hphp/runtime/base/mixed-array.h"
#include "hphp/runtime/base/packed-array.h"
#include "hphp/runtime/base/set-array.h"
namespace HPHP { namespace jit { namespace irgen {
namespace {
//////////////////////////////////////////////////////////////////////
void implClsRefGet(IRGS& env, SSATmp* classSrc, uint32_t slot) {
auto const cls = (classSrc->type() <= TStr)
? ldCls(env, classSrc)
: gen(env, LdObjClass, classSrc);
putClsRef(env, slot, cls);
}
const StaticString s_FATAL_NULL_THIS(Strings::FATAL_NULL_THIS);
bool checkThis(IRGS& env, SSATmp* /*ctx*/) {
if (hasThis(env)) return true;
auto const err = cns(env, s_FATAL_NULL_THIS.get());
gen(env, RaiseError, err);
return false;
}
//////////////////////////////////////////////////////////////////////
}
void emitClsRefGetC(IRGS& env, uint32_t slot) {
auto const name = topC(env);
if (name->type().subtypeOfAny(TObj, TStr)) {
popC(env);
implClsRefGet(env, name, slot);
decRef(env, name);
} else {
interpOne(env, *env.currentNormalizedInstruction);
}
}
void emitClsRefGetL(IRGS& env, int32_t id, uint32_t slot) {
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)) {
implClsRefGet(env, src, slot);
} else {
PUNT(ClsRefGetL);
}
}
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,
DataTypeBoxAndCountnessInit
);
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,
DataTypeBoxAndCountnessInit
);
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,
DataTypeBoxAndCountnessInit
);
pushIncRef(env, val);
push(env, oldTop);
}
void emitVGetL(IRGS& env, int32_t id) {
auto const value = ldLoc(env, id, makeExit(env), DataTypeBoxAndCountnessInit);
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 emitUnsetL(IRGS& env, int32_t id) {
auto const prev = ldLoc(env, id, makeExit(env), DataTypeBoxAndCountness);
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 (!hasThis(env)) {
// Do nothing if this is null
return;
}
auto const ldrefExit = makeExit(env);
auto const ctx = ldCtx(env);
auto const oldLoc = ldLoc(env, id, ldrefExit, DataTypeBoxAndCountness);
auto const this_ = castCtxThis(env, 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 = ldCtx(env);
if (!checkThis(env, ctx)) {
// Unreachable
push(env, cns(env, TInitNull));
return;
}
auto const this_ = castCtxThis(env, ctx);
pushIncRef(env, this_);
}
void emitCheckThis(IRGS& env) {
auto const ctx = ldCtx(env);
checkThis(env, ctx);
}
void emitBareThis(IRGS& env, BareThisOp subop) {
auto const ctx = ldCtx(env);
if (!hasThis(env)) {
if (subop == BareThisOp::NoNotice) {
push(env, cns(env, TInitNull));
return;
}
assertx(subop != BareThisOp::NeverNull);
interpOne(env, TInitNull, 0); // will raise notice and push null
return;
}
pushIncRef(env, castCtxThis(env, 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, uint32_t slot) {
auto const clss = curClass(env);
if (!clss) {
// no static context class, so this will raise an error
interpOne(env, *env.currentNormalizedInstruction);
return;
}
auto const ctx = ldCtx(env);
putClsRef(env, slot, gen(env, LdClsCtx, ctx));
}
void emitSelf(IRGS& env, uint32_t slot) {
auto const clss = curClass(env);
if (clss == nullptr) {
interpOne(env, *env.currentNormalizedInstruction);
} else {
putClsRef(env, slot, cns(env, clss));
}
}
void emitParent(IRGS& env, uint32_t slot) {
auto const clss = curClass(env);
if (clss == nullptr || clss->parent() == nullptr) {
interpOne(env, *env.currentNormalizedInstruction);
} else {
putClsRef(env, slot, cns(env, clss->parent()));
}
}
void emitClsRefName(IRGS& env, uint32_t slot) {
auto const cls = takeClsRef(env, slot);
push(env, gen(env, LdClsName, cls));
}
//////////////////////////////////////////////////////////////////////
void emitCastArray(IRGS& env) {
auto const src = popC(env);
push(env, gen(env, ConvCellToArr, src));
}
void emitCastVArray(IRGS& env) {
auto const src = popC(env);
auto const raise = [&](const char* type) {
gen(
env,
ThrowInvalidOperation,
cns(
env,
makeStaticString(folly::sformat("{} to varray conversion", type))
)
);
return cns(env, TBottom);
};
push(
env,
[&] {
if (src->isA(TArr)) {
return cond(
env,
[&](Block* taken) { return gen(env, CheckVArray, taken, src); },
[&](SSATmp* varr) { return varr; },
[&]{ return gen(env, ConvArrToVArr, src); }
);
}
if (src->isA(TVec)) return gen(env, ConvVecToVArr, src);
if (src->isA(TDict)) return gen(env, ConvDictToVArr, src);
if (src->isA(TKeyset)) return gen(env, ConvKeysetToVArr, src);
if (src->isA(TObj)) return gen(env, ConvObjToVArr, src);
if (src->isA(TNull)) return raise("Null");
if (src->isA(TBool)) return raise("Bool");
if (src->isA(TInt)) return raise("Int");
if (src->isA(TDbl)) return raise("Double");
if (src->isA(TStr)) return raise("String");
if (src->isA(TRes)) return raise("Resource");
// Unexpected types may only be seen in unreachable code.
gen(env, Unreachable, ASSERT_REASON);
return cns(env, TBottom);
}()
);
}
void emitCastDArray(IRGS& env) {
auto const src = popC(env);
auto const raise = [&](const char* type) {
gen(
env,
ThrowInvalidOperation,
cns(
env,
makeStaticString(folly::sformat("{} to darray conversion", type))
)
);
return cns(env, TBottom);
};
push(
env,
[&] {
if (src->isA(TArr)) {
return cond(
env,
[&](Block* taken) { return gen(env, CheckDArray, taken, src); },
[&](SSATmp* darr) { return darr; },
[&]{ return gen(env, ConvArrToDArr, src); }
);
}
if (src->isA(TVec)) return gen(env, ConvVecToDArr, src);
if (src->isA(TDict)) return gen(env, ConvDictToDArr, src);
if (src->isA(TKeyset)) return gen(env, ConvKeysetToDArr, src);
if (src->isA(TObj)) return gen(env, ConvObjToDArr, src);
if (src->isA(TNull)) return raise("Null");
if (src->isA(TBool)) return raise("Bool");
if (src->isA(TInt)) return raise("Int");
if (src->isA(TDbl)) return raise("Double");
if (src->isA(TStr)) return raise("String");
if (src->isA(TRes)) return raise("Resource");
// Unexpected types may only be seen in unreachable code.
gen(env, Unreachable, ASSERT_REASON);
return cns(env, TBottom);
}()
);
}
void emitCastVec(IRGS& env) {
auto const src = popC(env);
auto const raise = [&](const char* type) {
gen(
env,
ThrowInvalidOperation,
cns(
env,
makeStaticString(folly::sformat("{} to vec conversion", type))
)
);
return cns(env, TBottom);
};
push(
env,
[&] {
if (src->isA(TVec)) return src;
if (src->isA(TArr)) return gen(env, ConvArrToVec, src);
if (src->isA(TDict)) return gen(env, ConvDictToVec, src);
if (src->isA(TKeyset)) return gen(env, ConvKeysetToVec, src);
if (src->isA(TObj)) return gen(env, ConvObjToVec, src);
if (src->isA(TNull)) return raise("Null");
if (src->isA(TBool)) return raise("Bool");
if (src->isA(TInt)) return raise("Int");
if (src->isA(TDbl)) return raise("Double");
if (src->isA(TStr)) return raise("String");
if (src->isA(TRes)) return raise("Resource");
// Unexpected types may only be seen in unreachable code.
gen(env, Unreachable, ASSERT_REASON);
return cns(env, TBottom);
}()
);
}
void emitCastDict(IRGS& env) {
auto const src = popC(env);
auto const raise = [&](const char* type) {
gen(
env,
ThrowInvalidOperation,
cns(
env,
makeStaticString(folly::sformat("{} to dict conversion", type))
)
);
return cns(env, TBottom);
};
push(
env,
[&] {
if (src->isA(TDict)) return src;
if (src->isA(TArr)) return gen(env, ConvArrToDict, src);
if (src->isA(TVec)) return gen(env, ConvVecToDict, src);
if (src->isA(TKeyset)) return gen(env, ConvKeysetToDict, src);
if (src->isA(TObj)) return gen(env, ConvObjToDict, src);
if (src->isA(TNull)) return raise("Null");
if (src->isA(TBool)) return raise("Bool");
if (src->isA(TInt)) return raise("Int");
if (src->isA(TDbl)) return raise("Double");
if (src->isA(TStr)) return raise("String");
if (src->isA(TRes)) return raise("Resource");
// Unexpected types may only be seen in unreachable code.
gen(env, Unreachable, ASSERT_REASON);
return cns(env, TBottom);
}()
);
}
void emitCastKeyset(IRGS& env) {
auto const src = popC(env);
auto const raise = [&](const char* type) {
gen(
env,
ThrowInvalidOperation,
cns(
env,
makeStaticString(folly::sformat("{} to keyset conversion", type))
)
);
return cns(env, TBottom);
};
push(
env,
[&] {
if (src->isA(TKeyset)) return src;
if (src->isA(TArr)) return gen(env, ConvArrToKeyset, src);
if (src->isA(TVec)) return gen(env, ConvVecToKeyset, src);
if (src->isA(TDict)) return gen(env, ConvDictToKeyset, src);
if (src->isA(TObj)) return gen(env, ConvObjToKeyset, src);
if (src->isA(TNull)) return raise("Null");
if (src->isA(TBool)) return raise("Bool");
if (src->isA(TInt)) return raise("Int");
if (src->isA(TDbl)) return raise("Double");
if (src->isA(TStr)) return raise("String");
if (src->isA(TRes)) return raise("Resource");
// Unexpected types may only be seen in unreachable code.
gen(env, Unreachable, ASSERT_REASON);
return cns(env, TBottom);
}()
);
}
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, uint32_t counter, uint32_t value) {
if (!Stats::enabled()) return;
gen(env, IncStat, cns(env, counter), cns(env, value), cns(env, false));
}
//////////////////////////////////////////////////////////////////////
void emitDiscardClsRef(IRGS& env, uint32_t slot) { killClsRef(env, slot); }
void emitPopC(IRGS& env) { popDecRef(env, DataTypeGeneric); }
void emitPopV(IRGS& env) { popDecRef(env, DataTypeGeneric); }
void emitPopR(IRGS& env) { popDecRef(env, DataTypeGeneric); }
void emitPopU(IRGS& env) { popU(env); }
void emitPopL(IRGS& env, int32_t id) {
auto const ldrefExit = makeExit(env);
auto const ldPMExit = makePseudoMainExit(env);
auto const src = popC(env, DataTypeGeneric);
stLocMove(env, id, ldrefExit, ldPMExit, src);
}
void emitDir(IRGS& env) { push(env, cns(env, curUnit(env)->dirpath())); }
void emitFile(IRGS& env) { push(env, cns(env, curUnit(env)->filepath())); }
void emitMethod(IRGS& env) { push(env, cns(env, curFunc(env)->fullName())); }
void emitDup(IRGS& env) { pushIncRef(env, topC(env)); }
//////////////////////////////////////////////////////////////////////
void emitArray(IRGS& env, const ArrayData* x) {
assertx(x->isPHPArray());
push(env, cns(env, x));
}
void emitVec(IRGS& env, const ArrayData* x) {
assertx(x->isVecArray());
push(env, cns(env, x));
}
void emitDict(IRGS& env, const ArrayData* x) {
assertx(x->isDict());
push(env, cns(env, x));
}
void emitKeyset(IRGS& env, const ArrayData* x) {
assertx(x->isKeyset());
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 emitEntryNop(IRGS&) {}
void emitBoxRNop(IRGS& env) {
assertTypeStack(env, BCSPRelOffset{0}, TBoxedCell);
}
void emitUnboxRNop(IRGS& env) {
assertTypeStack(env, BCSPRelOffset{0}, TCell);
}
void emitCGetCUNop(IRGS& env) {
auto const offset = offsetFromIRSP(env, BCSPRelOffset{0});
auto const knownType = env.irb->stack(offset, DataTypeSpecific).type;
assertTypeStack(env, BCSPRelOffset{0}, knownType & TInitCell);
}
void emitUGetCUNop(IRGS& env) {
assertTypeStack(env, BCSPRelOffset{0}, TUninit);
}
void emitRGetCNop(IRGS&) {}
void emitDefClsNop(IRGS&, uint32_t){}
void emitBreakTraceHint(IRGS&) {}
//////////////////////////////////////////////////////////////////////
}}}