2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include "hphp/runtime/vm/jit/irlower-internal.h"
19 #include "hphp/runtime/base/datatype.h"
20 #include "hphp/runtime/base/execution-context.h"
21 #include "hphp/runtime/base/object-data.h"
22 #include "hphp/runtime/base/rds.h"
23 #include "hphp/runtime/base/runtime-option.h"
24 #include "hphp/runtime/base/typed-value.h"
26 #include "hphp/runtime/vm/act-rec.h"
27 #include "hphp/runtime/vm/func.h"
28 #include "hphp/runtime/vm/method-lookup.h"
30 #include "hphp/runtime/vm/jit/types.h"
31 #include "hphp/runtime/vm/jit/abi.h"
32 #include "hphp/runtime/vm/jit/arg-group.h"
33 #include "hphp/runtime/vm/jit/bc-marker.h"
34 #include "hphp/runtime/vm/jit/call-spec.h"
35 #include "hphp/runtime/vm/jit/code-gen-cf.h"
36 #include "hphp/runtime/vm/jit/code-gen-helpers.h"
37 #include "hphp/runtime/vm/jit/extra-data.h"
38 #include "hphp/runtime/vm/jit/ir-instruction.h"
39 #include "hphp/runtime/vm/jit/ir-opcode.h"
40 #include "hphp/runtime/vm/jit/meth-profile.h"
41 #include "hphp/runtime/vm/jit/ssa-tmp.h"
42 #include "hphp/runtime/vm/jit/target-cache.h"
43 #include "hphp/runtime/vm/jit/translator-inline.h"
44 #include "hphp/runtime/vm/jit/type.h"
45 #include "hphp/runtime/vm/jit/vasm-gen.h"
46 #include "hphp/runtime/vm/jit/vasm-instr.h"
47 #include "hphp/runtime/vm/jit/vasm-reg.h"
49 #include "hphp/util/asm-x64.h"
50 #include "hphp/util/trace.h"
52 namespace HPHP
{ namespace jit
{ namespace irlower
{
54 TRACE_SET_MOD(irlower
);
56 ///////////////////////////////////////////////////////////////////////////////
58 void cgLdObjMethod(IRLS
& env
, const IRInstruction
* inst
) {
59 assertx(inst
->taken() && inst
->taken()->isCatch()); // must have catch block
60 using namespace MethodCache
;
62 auto const cls
= srcLoc(env
, inst
, 0).reg();
63 auto const fp
= srcLoc(env
, inst
, 1).reg();
64 auto const extra
= inst
->extra
<LdObjMethodData
>();
66 auto& vc
= vcold(env
);
68 // Allocate the request-local one-way method cache for this lookup.
69 auto const handle
= rds::alloc
<Entry
, sizeof(Entry
)>().handle();
70 if (RuntimeOption::EvalPerfDataMap
) {
71 rds::recordRds(handle
, sizeof(TypedValue
), "MethodCache",
72 inst
->marker().func()->fullName()->toCppString());
75 auto const mc_handler
= extra
->fatal
? tc::ustubs().handlePrimeCacheInitFatal
76 : tc::ustubs().handlePrimeCacheInit
;
79 * The `mcprep' instruction here creates a smashable move, which serves as
80 * the inline cache, or "prime cache" for the method lookup.
82 * On our first time through this codepath in the TC, we "prime" this cache
83 * (which holds across /all/ requests) by smashing the mov immediate to hold
84 * a Func* in the upper 32 bits, and a Class* in the lower 32 bits. This is
85 * not always possible (see handlePrimeCacheInit() for details), in which
86 * case we smash an immediate with some low bits set, so that we always miss
87 * on the inline cache when comparing against our live Class*.
89 * The inline cache is set up so that we always miss initially, and take the
90 * slow path to initialize it. After initialization, we also smash the slow
91 * path call to point instead to a lookup routine for the out-of-line method
92 * cache (allocated above). The inline cache is guaranteed to be set only
93 * once, but the one-way request-local method cache is updated on each miss.
95 auto func_class
= v
.makeReg();
96 v
<< mcprep
{func_class
};
98 // Get the Class* part of the cache line.
99 auto tmp
= v
.makeReg();
100 auto classptr
= v
.makeReg();
101 v
<< movtql
{func_class
, tmp
};
102 v
<< movzlq
{tmp
, classptr
};
104 // Check the inline cache.
105 auto const sf
= v
.makeReg();
106 v
<< cmpq
{classptr
, cls
, sf
};
110 [&] (Vout
& v
) { // then block (unlikely)
111 auto const args
= argGroup(env
, inst
)
112 .imm(safe_cast
<int32_t>(handle
))
113 .addr(fp
, cellsToBytes(extra
->offset
.offset
))
114 .immPtr(extra
->method
)
116 .immPtr(inst
->marker().func()->cls())
119 cgCallHelper(v
, env
, CallSpec::smashable(mc_handler
),
120 kVoidDest
, SyncOptions::Sync
, args
);
122 [&] (Vout
& v
) { // else block (likely)
123 auto const funcptr
= v
.makeReg();
124 v
<< shrqi
{32, func_class
, funcptr
, v
.makeReg()};
126 fp
[cellsToBytes(extra
->offset
.offset
) + AROFF(m_func
)]};
130 ///////////////////////////////////////////////////////////////////////////////
132 IMPL_OPCODE_CALL(LdClsCtor
)
134 template<bool forward
>
135 void lookupClsMethodHelper(Class
* cls
, StringData
* meth
,
136 ActRec
* ar
, ActRec
* fp
) {
139 auto const ctx
= fp
->m_func
->cls();
140 auto const obj
= ctx
&& fp
->hasThis() ? fp
->getThis() : nullptr;
141 auto const res
= lookupClsMethod(f
, cls
, meth
, obj
, ctx
, true);
145 if (res
== LookupResult::MethodFoundNoThis
||
146 res
== LookupResult::MagicCallStaticFound
) {
147 if (!f
->isStaticInPrologue()) {
148 raise_missing_this(f
);
150 if (forward
&& ctx
) {
152 cls
= fp
->getThis()->getVMClass();
154 cls
= fp
->getClass();
160 assertx(res
== LookupResult::MethodFoundWithThis
||
161 res
== LookupResult::MagicCallFound
);
166 if (res
== LookupResult::MagicCallFound
||
167 res
== LookupResult::MagicCallStaticFound
) {
168 ar
->setMagicDispatch(meth
);
172 *arPreliveOverwriteCells(ar
) = make_tv
<KindOfString
>(meth
);
177 void cgLookupClsMethod(IRLS
& env
, const IRInstruction
* inst
) {
178 auto const extra
= inst
->extra
<LookupClsMethod
>();
179 auto const sp
= srcLoc(env
, inst
, 2).reg();
181 auto const args
= argGroup(env
, inst
)
184 .addr(sp
, cellsToBytes(extra
->calleeAROffset
.offset
))
187 if (extra
->forward
) {
188 cgCallHelper(vmain(env
), env
,
189 CallSpec::direct(lookupClsMethodHelper
<true>),
190 callDest(env
, inst
), SyncOptions::Sync
, args
);
192 cgCallHelper(vmain(env
), env
,
193 CallSpec::direct(lookupClsMethodHelper
<false>),
194 callDest(env
, inst
), SyncOptions::Sync
, args
);
198 void cgProfileMethod(IRLS
& env
, const IRInstruction
* inst
) {
199 auto const extra
= inst
->extra
<ProfileMethodData
>();
200 auto const sp
= srcLoc(env
, inst
, 0).reg();
202 auto const args
= argGroup(env
, inst
)
203 .addr(rvmtl(), safe_cast
<int32_t>(extra
->handle
))
204 .addr(sp
, cellsToBytes(extra
->bcSPOff
.offset
))
207 cgCallHelper(vmain(env
), env
, CallSpec::method(&MethProfile::reportMeth
),
208 kVoidDest
, SyncOptions::None
, args
);
211 ///////////////////////////////////////////////////////////////////////////////
215 const char* ctxName(const BCMarker
& marker
) {
216 auto const ctx
= marker
.func()->cls();
217 return ctx
? ctx
->name()->data() : ":anonymous:";
222 ///////////////////////////////////////////////////////////////////////////////
224 void cgLookupClsMethodCache(IRLS
& env
, const IRInstruction
* inst
) {
225 auto const extra
= inst
->extra
<ClsMethodData
>();
226 auto const dst
= dstLoc(env
, inst
, 0).reg();
227 auto const fp
= srcLoc(env
, inst
, 0).reg();
228 auto& v
= vmain(env
);
230 auto const ch
= StaticMethodCache::alloc(
233 ctxName(inst
->marker())
236 if (false) { // typecheck
237 UNUSED TypedValue
* fake_fp
= nullptr;
238 const UNUSED Func
* f
= StaticMethodCache::lookup(
247 auto const args
= argGroup(env
, inst
)
249 .immPtr(extra
->namedEntity
)
250 .immPtr(extra
->clsName
)
251 .immPtr(extra
->methodName
)
254 // May raise an error if the class is undefined.
255 cgCallHelper(v
, env
, CallSpec::direct(StaticMethodCache::lookup
),
256 callDest(dst
), SyncOptions::Sync
, args
);
259 void cgLdClsMethodCacheFunc(IRLS
& env
, const IRInstruction
* inst
) {
260 auto const extra
= inst
->extra
<ClsMethodData
>();
261 auto const dst
= dstLoc(env
, inst
, 0).reg();
262 auto& v
= vmain(env
);
264 auto const ch
= StaticMethodCache::alloc(
267 ctxName(inst
->marker())
270 auto const sf
= checkRDSHandleInitialized(v
, ch
);
271 fwdJcc(v
, env
, CC_NE
, sf
, inst
->taken());
272 emitLdLowPtr(v
, rvmtl()[ch
+ offsetof(StaticMethodCache
, m_func
)],
273 dst
, sizeof(LowPtr
<const Func
>));
276 void cgLdClsMethodCacheCls(IRLS
& env
, const IRInstruction
* inst
) {
277 auto const extra
= inst
->extra
<ClsMethodData
>();
278 auto const dst
= dstLoc(env
, inst
, 0).reg();
279 auto& v
= vmain(env
);
281 auto const ch
= StaticMethodCache::alloc(
284 ctxName(inst
->marker())
287 // The StaticMethodCache here is guaranteed to already be initialized in RDS
288 // by the pre-conditions of this instruction.
289 emitLdLowPtr(v
, rvmtl()[ch
+ offsetof(StaticMethodCache
, m_cls
)],
290 dst
, sizeof(LowPtr
<const Class
>));
293 void cgLookupClsMethodFCache(IRLS
& env
, const IRInstruction
* inst
) {
294 auto const extra
= inst
->extra
<ClsMethodData
>();
295 auto const dst
= dstLoc(env
, inst
, 0).reg(0);
296 auto const cls
= inst
->src(0)->clsVal();
297 auto const fp
= srcLoc(env
, inst
, 1).reg();
298 auto& v
= vmain(env
);
300 auto const ch
= StaticMethodFCache::alloc(
303 ctxName(inst
->marker())
305 assertx(rds::isNormalHandle(ch
));
307 const Func
* (*lookup
)(rds::Handle
, const Class
*,
308 const StringData
*, TypedValue
*) =
309 StaticMethodFCache::lookup
;
311 auto const args
= argGroup(env
, inst
)
314 .immPtr(extra
->methodName
)
317 cgCallHelper(v
, env
, CallSpec::direct(lookup
),
318 callDest(dst
), SyncOptions::Sync
, args
);
321 void cgLdClsMethodFCacheFunc(IRLS
& env
, const IRInstruction
* inst
) {
322 auto const extra
= inst
->extra
<ClsMethodData
>();
323 auto const dst
= dstLoc(env
, inst
, 0).reg();
324 auto& v
= vmain(env
);
326 auto const ch
= StaticMethodFCache::alloc(
329 ctxName(inst
->marker())
332 auto const sf
= checkRDSHandleInitialized(v
, ch
);
333 fwdJcc(v
, env
, CC_NE
, sf
, inst
->taken());
334 emitLdLowPtr(v
, rvmtl()[ch
+ offsetof(StaticMethodFCache
, m_func
)],
335 dst
, sizeof(LowPtr
<const Func
>));
338 ///////////////////////////////////////////////////////////////////////////////
340 void cgCheckFuncStatic(IRLS
& env
, const IRInstruction
* inst
) {
341 auto const funcPtrReg
= srcLoc(env
, inst
, 0).reg();
342 auto& v
= vmain(env
);
344 auto const sf
= v
.makeReg();
345 v
<< testlim
{AttrStatic
, funcPtrReg
[Func::attrsOff()], sf
};
346 v
<< jcc
{CC_NZ
, sf
, {label(env
, inst
->next()), label(env
, inst
->taken())}};
349 void cgFwdCtxStaticCall(IRLS
& env
, const IRInstruction
* inst
) {
350 auto const dstCtx
= dstLoc(env
, inst
, 0).reg();
351 auto const srcCtx
= srcLoc(env
, inst
, 0).reg();
352 auto const ty
= inst
->src(0)->type();
354 auto& v
= vmain(env
);
356 auto ctx_from_this
= [] (Vout
& v
, Vreg rthis
, Vreg dst
) {
357 // Load (this->m_cls | 0x1) into `dst'.
358 auto const cls
= emitLdObjClass(v
, rthis
, v
.makeReg());
359 v
<< orqi
{ActRec::kHasClassBit
, cls
, dst
, v
.makeReg()};
364 v
<< copy
{srcCtx
, dstCtx
};
365 } else if (ty
<= TObj
) {
366 ctx_from_this(v
, srcCtx
, dstCtx
);
368 // If we don't know whether we have a $this, we need to check dynamically.
369 auto const sf
= v
.makeReg();
370 v
<< testqi
{ActRec::kHasClassBit
, srcCtx
, sf
};
372 v
, vcold(env
), CC_NZ
, sf
, dstCtx
, [&](Vout
& /*v*/) { return srcCtx
; },
373 [&](Vout
& v
) { return ctx_from_this(v
, srcCtx
, v
.makeReg()); });
377 ///////////////////////////////////////////////////////////////////////////////