Debian Codegraph @

js/src/jit/Jit.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "jit/Jit.h"
8
9 #include "jit/BaselineJIT.h"
10 #include "jit/CalleeToken.h"
11 #include "jit/Ion.h"
12 #include "jit/JitCommon.h"
13 #include "jit/JitRuntime.h"
14 #include "js/friend/StackLimits.h" // js::AutoCheckRecursionLimit
15 #include "vm/Interpreter.h"
16 #include "vm/JitActivation.h"
17 #include "vm/JSContext.h"
18 #include "vm/PortableBaselineInterpret.h"
19 #include "vm/Realm.h"
20
21 #include "vm/Activation-inl.h"
22 #include "vm/JSScript-inl.h"
23
24 using namespace js;
25 using namespace js::jit;
26
27 static EnterJitStatus JS_HAZ_JSNATIVE_CALLER EnterJit(JSContext* cx,
28 RunState& state,
29 uint8_t* code) {
30 // We don't want to call the interpreter stub here (because
31 // C++ -> interpreterStub -> C++ is slower than staying in C++).
32 MOZ_ASSERT(code);
33 #ifndef ENABLE_PORTABLE_BASELINE_INTERP
34 MOZ_ASSERT(code != cx->runtime()->jitRuntime()->interpreterStub().value);
35 MOZ_ASSERT(IsBaselineInterpreterEnabled());
36 #else
37 MOZ_ASSERT(IsBaselineInterpreterEnabled() ||
38 IsPortableBaselineInterpreterEnabled());
39 #endif
40
41 AutoCheckRecursionLimit recursion(cx);
42 if (!recursion.check(cx)) {
43 return EnterJitStatus::Error;
44 }
45
46 // jit::Bailout(), jit::InvalidationBailout(), and jit::HandleException()
47 // reset the counter to zero, so assert here it's also zero when we enter
48 // JIT code.
49 MOZ_ASSERT(!cx->isInUnsafeRegion());
50
51 #ifdef DEBUG
52 // Assert we don't GC before entering JIT code. A GC could discard JIT code
53 // or move the function stored in the CalleeToken (it won't be traced at
54 // this point). We use Maybe<> here so we can call reset() to call the
55 // AutoAssertNoGC destructor before we enter JIT code.
56 mozilla::Maybe<JS::AutoAssertNoGC> nogc;
57 nogc.emplace(cx);
58 #endif
59
60 JSScript* script = state.script();
61 size_t numActualArgs;
62 bool constructing;
63 size_t maxArgc;
64 Value* maxArgv;
65 JSObject* envChain;
66 CalleeToken calleeToken;
67
68 unsigned numFormals = 0;
69 if (state.isInvoke()) {
70 const CallArgs& args = state.asInvoke()->args();
71 numActualArgs = args.length();
72
73 if (TooManyActualArguments(numActualArgs)) {
74 // Fall back to the C++ interpreter to avoid running out of stack space.
75 return EnterJitStatus::NotEntered;
76 }
77
78 constructing = state.asInvoke()->constructing();
79 maxArgc = args.length() + 1;
80 maxArgv = args.array() - 1; // -1 to include |this|
81 envChain = nullptr;
82 calleeToken = CalleeToToken(&args.callee().as<JSFunction>(), constructing);
83
84 numFormals = script->function()->nargs();
85 if (numFormals > numActualArgs) {
86 #ifndef ENABLE_PORTABLE_BASELINE_INTERP
87 code = cx->runtime()->jitRuntime()->getArgumentsRectifier().value;
88 #endif
89 }
90 } else {
91 numActualArgs = 0;
92 constructing = false;
93 maxArgc = 0;
94 maxArgv = nullptr;
95 envChain = state.asExecute()->environmentChain();
96 calleeToken = CalleeToToken(state.script());
97 }
98
99 // Caller must construct |this| before invoking the function.
100 MOZ_ASSERT_IF(constructing, maxArgv[0].isObject() ||
101 maxArgv[0].isMagic(JS_UNINITIALIZED_LEXICAL));
102
103 RootedValue result(cx, Int32Value(numActualArgs));
104 {
105 AssertRealmUnchanged aru(cx);
106 JitActivation activation(cx);
107
108 #ifndef ENABLE_PORTABLE_BASELINE_INTERP
109 EnterJitCode enter = cx->runtime()->jitRuntime()->enterJit();
110
111 # ifdef DEBUG
112 nogc.reset();
113 # endif
114 CALL_GENERATED_CODE(enter, code, maxArgc, maxArgv, /* osrFrame = */ nullptr,
115 calleeToken, envChain, /* osrNumStackValues = */ 0,
116 result.address());
117 #else // !ENABLE_PORTABLE_BASELINE_INTERP
118 (void)code;
119 # ifdef DEBUG
120 nogc.reset();
121 # endif
122 if (!pbl::PortablebaselineInterpreterStackCheck(cx, state, numActualArgs)) {
123 return EnterJitStatus::NotEntered;
124 }
125 if (!pbl::PortableBaselineTrampoline(cx, maxArgc, maxArgv, numFormals,
126 numActualArgs, calleeToken, envChain,
127 result.address())) {
128 return EnterJitStatus::Error;
129 }
130 #endif // ENABLE_PORTABLE_BASELINE_INTERP
131 }
132
133 // Ensure the counter was reset to zero after exiting from JIT code.
134 MOZ_ASSERT(!cx->isInUnsafeRegion());
135
136 // Release temporary buffer used for OSR into Ion.
137 if (!IsPortableBaselineInterpreterEnabled()) {
138 cx->runtime()->jitRuntime()->freeIonOsrTempData();
139 }
140
141 if (result.isMagic()) {
142 MOZ_ASSERT(result.isMagic(JS_ION_ERROR));
143 return EnterJitStatus::Error;
144 }
145
146 // Jit callers wrap primitive constructor return, except for derived
147 // class constructors, which are forced to do it themselves.
148 if (constructing && result.isPrimitive()) {
149 MOZ_ASSERT(maxArgv[0].isObject());
150 result = maxArgv[0];
151 }
152
153 state.setReturnValue(result);
154 return EnterJitStatus::Ok;
155 }
156
157 // Call the per-script interpreter entry trampoline.
158 bool js::jit::EnterInterpreterEntryTrampoline(uint8_t* code, JSContext* cx,
159 RunState* state) {
160 using EnterTrampolineCodePtr = bool (*)(JSContext* cx, RunState*);
161 auto funcPtr = JS_DATA_TO_FUNC_PTR(EnterTrampolineCodePtr, code);
162 return CALL_GENERATED_2(funcPtr, cx, state);
163 }
164
165 EnterJitStatus js::jit::MaybeEnterJit(JSContext* cx, RunState& state) {
166 if (!IsBaselineInterpreterEnabled()
167 #ifdef ENABLE_PORTABLE_BASELINE_INTERP
168 && !IsPortableBaselineInterpreterEnabled()
169 #endif
170 ) {
171 // All JITs are disabled.
172 return EnterJitStatus::NotEntered;
173 }
174
175 // JITs do not respect the debugger's OnNativeCall hook, so JIT execution is
176 // disabled if this hook might need to be called.
177 if (cx->realm()->debuggerObservesNativeCall()) {
178 return EnterJitStatus::NotEntered;
179 }
180
181 JSScript* script = state.script();
182
183 uint8_t* code = script->jitCodeRaw();
184
185 #ifdef JS_CACHEIR_SPEW
186 cx->spewer().enableSpewing();
187 #endif
188
189 do {
190 // Make sure we can enter Baseline Interpreter code. Note that the prologue
191 // has warm-up checks to tier up if needed.
192 if (script->hasJitScript() && code) {
193 break;
194 }
195
196 script->incWarmUpCounter();
197
198 #ifndef ENABLE_PORTABLE_BASELINE_INTERP
199 // Try to Ion-compile.
200 if (jit::IsIonEnabled(cx)) {
201 jit::MethodStatus status = jit::CanEnterIon(cx, state);
202 if (status == jit::Method_Error) {
203 return EnterJitStatus::Error;
204 }
205 if (status == jit::Method_Compiled) {
206 code = script->jitCodeRaw();
207 break;
208 }
209 }
210
211 // Try to Baseline-compile.
212 if (jit::IsBaselineJitEnabled(cx)) {
213 jit::MethodStatus status =
214 jit::CanEnterBaselineMethod<BaselineTier::Compiler>(cx, state);
215 if (status == jit::Method_Error) {
216 return EnterJitStatus::Error;
217 }
218 if (status == jit::Method_Compiled) {
219 code = script->jitCodeRaw();
220 break;
221 }
222 }
223
224 // Try to enter the Baseline Interpreter.
225 if (IsBaselineInterpreterEnabled()) {
226 jit::MethodStatus status =
227 jit::CanEnterBaselineMethod<BaselineTier::Interpreter>(cx, state);
228 if (status == jit::Method_Error) {
229 return EnterJitStatus::Error;
230 }
231 if (status == jit::Method_Compiled) {
232 code = script->jitCodeRaw();
233 break;
234 }
235 }
236
237 #else // !ENABLE_PORTABLE_BASELINE_INTERP
238
239 // Try to enter the Portable Baseline Interpreter.
240 if (IsPortableBaselineInterpreterEnabled()) {
241 jit::MethodStatus status =
242 pbl::CanEnterPortableBaselineInterpreter(cx, state);
243 if (status == jit::Method_Error) {
244 return EnterJitStatus::Error;
245 }
246 if (status == jit::Method_Compiled) {
247 code = script->jitCodeRaw();
248 break;
249 }
250 }
251 #endif
252
253 return EnterJitStatus::NotEntered;
254 } while (false);
255
256 #ifdef JS_CACHEIR_SPEW
257 cx->spewer().disableSpewing();
258 #endif
259
260 return EnterJit(cx, state, code);
261 }