Debian Codegraph @

js/src/jit/BaselineFrame.h

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 #ifndef jit_BaselineFrame_h
8 #define jit_BaselineFrame_h
9
10 #include <algorithm>
11
12 #include "jit/CalleeToken.h"
13 #include "jit/JitFrames.h"
14 #include "jit/ScriptFromCalleeToken.h"
15 #include "vm/Stack.h"
16
17 namespace js {
18 namespace jit {
19
20 class ICEntry;
21 class ICScript;
22 class JSJitFrameIter;
23
24 // The stack looks like this, fp is the frame pointer:
25 //
26 // fp+y arguments
27 // fp => JitFrameLayout (frame header)
28 // fp-x BaselineFrame
29 // locals
30 // stack values
31
32 class BaselineFrame {
33 public:
34 enum Flags : uint32_t {
35 // The frame has a valid return value. See also InterpreterFrame::HAS_RVAL.
36 HAS_RVAL = 1 << 0,
37
38 // The frame is running in the Baseline interpreter instead of JIT.
39 RUNNING_IN_INTERPRETER = 1 << 1,
40
41 // An initial environment has been pushed on the environment chain for
42 // function frames that need a CallObject or eval frames that need a
43 // VarEnvironmentObject.
44 HAS_INITIAL_ENV = 1 << 2,
45
46 // Frame has an arguments object, argsObj_.
47 HAS_ARGS_OBJ = 1 << 4,
48
49 // See InterpreterFrame::PREV_UP_TO_DATE.
50 PREV_UP_TO_DATE = 1 << 5,
51
52 // Frame has execution observed by a Debugger.
53 //
54 // See comment above 'isDebuggee' in vm/Realm.h for explanation
55 // of invariants of debuggee compartments, scripts, and frames.
56 DEBUGGEE = 1 << 6,
57
58 // Frame is executing Realm-independent Jitcode, which requires
59 // a valid interpreterScript_ field.
60 REALM_INDEPENDENT = 1 << 7,
61 };
62
63 protected: // Silence Clang warning about unused private fields.
64 // The fields below are only valid if RUNNING_IN_INTERPRETER or
65 // isRealmIndependent().
66 JSScript* interpreterScript_;
67 // The fields below are only valid if RUNNING_IN_INTERPRETER.
68 jsbytecode* interpreterPC_;
69 ICEntry* interpreterICEntry_;
70
71 JSObject* envChain_; // Environment chain (always initialized).
72 ICScript* icScript_; // IC script (initialized if Warp is enabled).
73 ArgumentsObject* argsObj_; // If HAS_ARGS_OBJ, the arguments object.
74
75 // We need to split the Value into 2 fields of 32 bits, otherwise the C++
76 // compiler may add some padding between the fields.
77 uint32_t loScratchValue_;
78 uint32_t hiScratchValue_;
79 uint32_t flags_;
80 #ifdef DEBUG
81 // Size of the frame. Stored in DEBUG builds when calling into C++. This is
82 // BaselineFrame::Size() + the size of the local and expression stack Values.
83 //
84 // We don't store this in release builds because it's redundant with the frame
85 // size computed from the frame pointers. In debug builds it's still useful
86 // for assertions.
87 uint32_t debugFrameSize_;
88 #else
89 uint32_t unused_;
90 #endif
91 uint32_t loReturnValue_; // If HAS_RVAL, the frame's return value.
92 uint32_t hiReturnValue_;
93
94 public:
95 [[nodiscard]] bool initForOsr(InterpreterFrame* fp, uint32_t numStackValues);
96
97 #ifdef DEBUG
98 uint32_t debugFrameSize() const { return debugFrameSize_; }
99 void setDebugFrameSize(uint32_t frameSize) { debugFrameSize_ = frameSize; }
100 #endif
101
102 JSObject* environmentChain() const { return envChain_; }
103 void setEnvironmentChain(JSObject* envChain) { envChain_ = envChain; }
104
105 template <typename SpecificEnvironment>
106 inline void pushOnEnvironmentChain(SpecificEnvironment& env);
107 template <typename SpecificEnvironment>
108 inline void popOffEnvironmentChain();
109 inline void replaceInnermostEnvironment(EnvironmentObject& env);
110
111 CalleeToken calleeToken() const { return framePrefix()->calleeToken(); }
112 void replaceCalleeToken(CalleeToken token) {
113 framePrefix()->replaceCalleeToken(token);
114 }
115 bool isConstructing() const {
116 return CalleeTokenIsConstructing(calleeToken());
117 }
118 JSScript* script() const {
119 return MaybeForwardedScriptFromCalleeToken(calleeToken());
120 }
121 JSFunction* callee() const { return CalleeTokenToFunction(calleeToken()); }
122 Value calleev() const { return ObjectValue(*callee()); }
123
124 size_t numValueSlots(size_t frameSize) const {
125 MOZ_ASSERT(frameSize == debugFrameSize());
126
127 MOZ_ASSERT(frameSize >= BaselineFrame::Size());
128 frameSize -= BaselineFrame::Size();
129
130 MOZ_ASSERT((frameSize % sizeof(Value)) == 0);
131 return frameSize / sizeof(Value);
132 }
133
134 Value newTarget() const {
135 MOZ_ASSERT(isFunctionFrame());
136 MOZ_ASSERT(!callee()->isArrow());
137
138 if (isConstructing()) {
139 unsigned pushedArgs = std::max(numFormalArgs(), numActualArgs());
140 return argv()[pushedArgs];
141 }
142 return UndefinedValue();
143 }
144
145 #ifdef DEBUG
146 size_t debugNumValueSlots() const { return numValueSlots(debugFrameSize()); }
147 #endif
148
149 Value* valueSlot(size_t slot) const {
150 #ifndef ENABLE_PORTABLE_BASELINE_INTERP
151 // Assert that we're within the frame, but only if the "debug
152 // frame size" has been set. Ordinarily if we are in C++ code
153 // looking upward at a baseline frame, it will be, because it is
154 // set for the *previous* frame when we push an exit frame and
155 // call back into C++ from generated baseline code. However, the
156 // portable baseline interpreter uses accessors on BaselineFrame
157 // directly within the active frame and so the "debug frame size"
158 // hasn't been set (and it would be expensive to constantly update
159 // it). Because this is only used for assertions, and is not
160 // needed for correctness, we can disable this check below when
161 // PBL is enabled.
162 MOZ_ASSERT(slot < debugNumValueSlots());
163 #endif
164 return (Value*)this - (slot + 1);
165 }
166
167 static size_t frameSizeForNumValueSlots(size_t numValueSlots) {
168 return BaselineFrame::Size() + numValueSlots * sizeof(Value);
169 }
170
171 Value& unaliasedFormal(
172 unsigned i, MaybeCheckAliasing checkAliasing = CHECK_ALIASING) const {
173 MOZ_ASSERT(i < numFormalArgs());
174 MOZ_ASSERT_IF(checkAliasing, !script()->argsObjAliasesFormals() &&
175 !script()->formalIsAliased(i));
176 return argv()[i];
177 }
178
179 Value& unaliasedActual(
180 unsigned i, MaybeCheckAliasing checkAliasing = CHECK_ALIASING) const {
181 MOZ_ASSERT(i < numActualArgs());
182 MOZ_ASSERT_IF(checkAliasing, !script()->argsObjAliasesFormals());
183 MOZ_ASSERT_IF(checkAliasing && i < numFormalArgs(),
184 !script()->formalIsAliased(i));
185 return argv()[i];
186 }
187
188 Value& unaliasedLocal(uint32_t i) const {
189 MOZ_ASSERT(i < script()->nfixed());
190 return *valueSlot(i);
191 }
192
193 unsigned numActualArgs() const { return framePrefix()->numActualArgs(); }
194 unsigned numFormalArgs() const { return script()->function()->nargs(); }
195 Value& thisArgument() const {
196 MOZ_ASSERT(isFunctionFrame());
197 return framePrefix()->thisv();
198 }
199 Value* argv() const { return framePrefix()->actualArgs(); }
200
201 [[nodiscard]] bool saveGeneratorSlots(JSContext* cx, unsigned nslots,
202 ArrayObject* dest) const;
203
204 public:
205 void prepareForBaselineInterpreterToJitOSR() {
206 // Clearing the RUNNING_IN_INTERPRETER flag is sufficient, but we also null
207 // out the interpreter fields to ensure we don't use stale values.
208 flags_ &= ~RUNNING_IN_INTERPRETER;
209 interpreterPC_ = nullptr;
210 }
211
212 private:
213 bool uninlineIsProfilerSamplingEnabled(JSContext* cx);
214
215 public:
216 // Switch a JIT frame on the stack to Interpreter mode. The caller is
217 // responsible for patching the return address into this frame to a location
218 // in the interpreter code. Also assert profiler sampling has been suppressed
219 // so the sampler thread doesn't see an inconsistent state while we are
220 // patching frames.
221 void switchFromJitToInterpreter(JSContext* cx, jsbytecode* pc) {
222 MOZ_ASSERT(!uninlineIsProfilerSamplingEnabled(cx));
223 MOZ_ASSERT(!runningInInterpreter());
224 flags_ |= RUNNING_IN_INTERPRETER;
225 setInterpreterFields(pc);
226 }
227 void switchFromJitToInterpreterAtPrologue(JSContext* cx) {
228 MOZ_ASSERT(!uninlineIsProfilerSamplingEnabled(cx));
229 MOZ_ASSERT(!runningInInterpreter());
230 flags_ |= RUNNING_IN_INTERPRETER;
231 setInterpreterFieldsForPrologue(script());
232 }
233
234 // Like switchFromJitToInterpreter, but set the interpreterICEntry_ field to
235 // nullptr. Initializing this field requires a binary search on the
236 // JitScript's ICEntry list but the exception handler never returns to this
237 // pc anyway so we can avoid the overhead.
238 void switchFromJitToInterpreterForExceptionHandler(JSContext* cx,
239 jsbytecode* pc) {
240 MOZ_ASSERT(!uninlineIsProfilerSamplingEnabled(cx));
241 MOZ_ASSERT(!runningInInterpreter());
242 flags_ |= RUNNING_IN_INTERPRETER;
243 interpreterScript_ = script();
244 interpreterPC_ = pc;
245 interpreterICEntry_ = nullptr;
246 }
247
248 bool runningInInterpreter() const { return flags_ & RUNNING_IN_INTERPRETER; }
249 bool isRealmIndependent() const { return flags_ & REALM_INDEPENDENT; }
250
251 JSScript* interpreterScript() const {
252 MOZ_ASSERT(runningInInterpreter());
253 return interpreterScript_;
254 }
255
256 jsbytecode* interpreterPC() const {
257 MOZ_ASSERT(runningInInterpreter());
258 return interpreterPC_;
259 }
260 jsbytecode*& interpreterPC() {
261 MOZ_ASSERT(runningInInterpreter());
262 return interpreterPC_;
263 }
264
265 ICEntry* interpreterICEntry() const {
266 MOZ_ASSERT(runningInInterpreter());
267 return interpreterICEntry_;
268 }
269 ICEntry*& interpreterICEntry() {
270 MOZ_ASSERT(runningInInterpreter());
271 return interpreterICEntry_;
272 }
273
274 void setInterpreterFields(JSScript* script, jsbytecode* pc);
275
276 void setInterpreterFields(jsbytecode* pc) {
277 setInterpreterFields(script(), pc);
278 }
279
280 // Initialize interpreter fields for resuming in the prologue (before the
281 // argument type check ICs).
282 void setInterpreterFieldsForPrologue(JSScript* script);
283
284 ICScript* icScript() const { return icScript_; }
285 void setICScript(ICScript* icScript) { icScript_ = icScript; }
286
287 // The script that owns the current ICScript.
288 JSScript* outerScript() const;
289
290 bool hasReturnValue() const { return flags_ & HAS_RVAL; }
291 MutableHandleValue returnValue() {
292 if (!hasReturnValue()) {
293 addressOfReturnValue()->setUndefined();
294 }
295 return MutableHandleValue::fromMarkedLocation(addressOfReturnValue());
296 }
297 void setReturnValue(const Value& v) {
298 returnValue().set(v);
299 flags_ |= HAS_RVAL;
300 }
301 inline Value* addressOfReturnValue() {
302 return reinterpret_cast<Value*>(&loReturnValue_);
303 }
304
305 bool hasInitialEnvironment() const { return flags_ & HAS_INITIAL_ENV; }
306
307 inline CallObject& callObj() const;
308
309 void setFlag(uint32_t flag) { flags_ |= flag; }
310 void setFlags(uint32_t flags) { flags_ = flags; }
311
312 [[nodiscard]] inline bool pushLexicalEnvironment(JSContext* cx,
313 Handle<LexicalScope*> scope);
314 template <bool IsDebuggee>
315 [[nodiscard]] inline bool freshenLexicalEnvironment(
316 JSContext* cx, const jsbytecode* pc = nullptr);
317 template <bool IsDebuggee>
318 [[nodiscard]] inline bool recreateLexicalEnvironment(
319 JSContext* cx, const jsbytecode* pc = nullptr);
320
321 [[nodiscard]] bool initFunctionEnvironmentObjects(JSContext* cx);
322 [[nodiscard]] bool pushClassBodyEnvironment(JSContext* cx,
323 Handle<ClassBodyScope*> scope);
324 [[nodiscard]] bool pushVarEnvironment(JSContext* cx, Handle<Scope*> scope);
325
326 void initArgsObjUnchecked(ArgumentsObject& argsobj) {
327 flags_ |= HAS_ARGS_OBJ;
328 argsObj_ = &argsobj;
329 }
330 void initArgsObj(ArgumentsObject& argsobj) {
331 MOZ_ASSERT(script()->needsArgsObj());
332 initArgsObjUnchecked(argsobj);
333 }
334 bool hasArgsObj() const { return flags_ & HAS_ARGS_OBJ; }
335 ArgumentsObject& argsObj() const {
336 MOZ_ASSERT(hasArgsObj());
337 MOZ_ASSERT(script()->needsArgsObj());
338 return *argsObj_;
339 }
340
341 bool prevUpToDate() const { return flags_ & PREV_UP_TO_DATE; }
342 void setPrevUpToDate() { flags_ |= PREV_UP_TO_DATE; }
343 void unsetPrevUpToDate() { flags_ &= ~PREV_UP_TO_DATE; }
344
345 bool isDebuggee() const { return flags_ & DEBUGGEE; }
346 void setIsDebuggee() { flags_ |= DEBUGGEE; }
347 inline void unsetIsDebuggee();
348
349 void trace(JSTracer* trc, const JSJitFrameIter& frame);
350
351 bool isGlobalFrame() const { return script()->isGlobalCode(); }
352 bool isModuleFrame() const { return script()->isModule(); }
353 bool isEvalFrame() const { return script()->isForEval(); }
354 bool isFunctionFrame() const {
355 return CalleeTokenIsFunction(calleeToken()) && !isModuleFrame();
356 }
357 bool isDebuggerEvalFrame() const { return false; }
358
359 JitFrameLayout* framePrefix() const {
360 uint8_t* fp = (uint8_t*)this + Size();
361 return (JitFrameLayout*)fp;
362 }
363
364 static size_t Size() { return sizeof(BaselineFrame); }
365
366 // The reverseOffsetOf methods below compute the offset relative to the
367 // frame's base pointer. Since the stack grows down, these offsets are
368 // negative.
369
370 #ifdef DEBUG
371 static int reverseOffsetOfDebugFrameSize() {
372 return -int(Size()) + offsetof(BaselineFrame, debugFrameSize_);
373 }
374 #endif
375
376 // The scratch value slot can either be used as a Value slot or as two
377 // separate 32-bit integer slots.
378 static int reverseOffsetOfScratchValueLow32() {
379 return -int(Size()) + offsetof(BaselineFrame, loScratchValue_);
380 }
381 static int reverseOffsetOfScratchValueHigh32() {
382 return -int(Size()) + offsetof(BaselineFrame, hiScratchValue_);
383 }
384 static int reverseOffsetOfScratchValue() {
385 return reverseOffsetOfScratchValueLow32();
386 }
387
388 static int reverseOffsetOfEnvironmentChain() {
389 return -int(Size()) + offsetof(BaselineFrame, envChain_);
390 }
391 static int reverseOffsetOfArgsObj() {
392 return -int(Size()) + offsetof(BaselineFrame, argsObj_);
393 }
394 static int reverseOffsetOfFlags() {
395 return -int(Size()) + offsetof(BaselineFrame, flags_);
396 }
397 static int reverseOffsetOfReturnValue() {
398 return -int(Size()) + offsetof(BaselineFrame, loReturnValue_);
399 }
400 static int reverseOffsetOfInterpreterScript() {
401 return -int(Size()) + offsetof(BaselineFrame, interpreterScript_);
402 }
403 static int reverseOffsetOfInterpreterPC() {
404 return -int(Size()) + offsetof(BaselineFrame, interpreterPC_);
405 }
406 static int reverseOffsetOfInterpreterICEntry() {
407 return -int(Size()) + offsetof(BaselineFrame, interpreterICEntry_);
408 }
409 static int reverseOffsetOfICScript() {
410 return -int(Size()) + offsetof(BaselineFrame, icScript_);
411 }
412 static int reverseOffsetOfLocal(size_t index) {
413 return -int(Size()) - (index + 1) * sizeof(Value);
414 }
415 };
416
417 // Ensure the frame is 8-byte aligned (required on ARM).
418 static_assert((sizeof(BaselineFrame) % 8) == 0, "frame must be 8-byte aligned");
419
420 } // namespace jit
421 } // namespace js
422
423 #endif /* jit_BaselineFrame_h */