Debian Codegraph @

js/src/jit/BaselineFrameInfo.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_BaselineFrameInfo_h
8 #define jit_BaselineFrameInfo_h
9
10 #include "mozilla/Attributes.h"
11 #include "mozilla/Maybe.h"
12
13 #include <new>
14
15 #include "jit/BaselineFrame.h"
16 #include "jit/BaselineJIT.h"
17 #include "jit/FixedList.h"
18 #include "jit/MacroAssembler.h"
19 #include "jit/SharedICRegisters.h"
20
21 namespace js {
22 namespace jit {
23
24 struct BytecodeInfo;
25 class MacroAssembler;
26
27 // [SMDOC] Baseline FrameInfo overview.
28 //
29 // FrameInfo is used by BaselineCodeGen to track values stored in the frame.
30 // There are two implementations:
31 //
32 // InterpreterFrameInfo
33 // --------------------
34 // The InterpreterFrameInfo class is used by the interpreter generator and is
35 // a very simple interface on top of the MacroAssembler, because the stack is
36 // always synced.
37 //
38 // CompilerFrameInfo
39 // -----------------
40 // The CompilerFrameInfo class is more complicated because it maintains a
41 // virtual stack to optimize some common stack operations. Locals and arguments
42 // are always fully synced. Stack values can either be synced, stored as
43 // constant, stored in a Value register or refer to a local slot. Syncing a
44 // StackValue ensures it's stored on the stack, e.g. kind == Stack.
45 //
46 // To see how this works, consider the following statement:
47 //
48 // var y = x + 9;
49 //
50 // Here two values are pushed: StackValue(LocalSlot(0)) and
51 // StackValue(Int32Value(9)). Only when we reach the ADD op, code is generated
52 // to load the operands directly into the right operand registers and sync all
53 // other stack values.
54 //
55 // For stack values, the following invariants hold (and are checked between
56 // ops):
57 //
58 // (1) If a value is synced (kind == Stack), all values below it must also be
59 // synced. In other words, values with kind other than Stack can only appear
60 // on top of the abstract stack.
61 //
62 // (2) When we call a stub or IC, all values still on the stack must be synced.
63
64 // Represents a value pushed on the stack. Note that StackValue is not used for
65 // locals or arguments since these are always fully synced.
66 class StackValue {
67 public:
68 enum Kind {
69 Constant,
70 Register,
71 Stack,
72 LocalSlot,
73 ArgSlot,
74 ThisSlot,
75 #ifdef DEBUG
76 // In debug builds, assert Kind is initialized.
77 Uninitialized,
78 #endif
79 };
80
81 private:
82 MOZ_INIT_OUTSIDE_CTOR Kind kind_;
83
84 MOZ_INIT_OUTSIDE_CTOR union Data {
85 JS::Value constant;
86 ValueOperand reg;
87 uint32_t localSlot;
88 uint32_t argSlot;
89
90 // |constant| has a non-trivial constructor and therefore MUST be
91 // placement-new'd into existence.
92 MOZ_PUSH_DISABLE_NONTRIVIAL_UNION_WARNINGS
93 Data() {}
94 MOZ_POP_DISABLE_NONTRIVIAL_UNION_WARNINGS
95 } data;
96
97 MOZ_INIT_OUTSIDE_CTOR JSValueType knownType_;
98
99 public:
100 StackValue() { reset(); }
101
102 Kind kind() const { return kind_; }
103 bool hasKnownType() const { return knownType_ != JSVAL_TYPE_UNKNOWN; }
104 bool hasKnownType(JSValueType type) const {
105 MOZ_ASSERT(type != JSVAL_TYPE_UNKNOWN);
106 return knownType_ == type;
107 }
108 JSValueType knownType() const {
109 MOZ_ASSERT(hasKnownType());
110 return knownType_;
111 }
112 void reset() {
113 #ifdef DEBUG
114 kind_ = Uninitialized;
115 knownType_ = JSVAL_TYPE_UNKNOWN;
116 #endif
117 }
118 Value constant() const {
119 MOZ_ASSERT(kind_ == Constant);
120 return data.constant;
121 }
122 ValueOperand reg() const {
123 MOZ_ASSERT(kind_ == Register);
124 return data.reg;
125 }
126 uint32_t localSlot() const {
127 MOZ_ASSERT(kind_ == LocalSlot);
128 return data.localSlot;
129 }
130 uint32_t argSlot() const {
131 MOZ_ASSERT(kind_ == ArgSlot);
132 return data.argSlot;
133 }
134
135 void setConstant(const Value& v) {
136 kind_ = Constant;
137 new (&data.constant) Value(v);
138 knownType_ = v.isDouble() ? JSVAL_TYPE_DOUBLE : v.extractNonDoubleType();
139 }
140 void setRegister(const ValueOperand& val,
141 JSValueType knownType = JSVAL_TYPE_UNKNOWN) {
142 kind_ = Register;
143 new (&data.reg) ValueOperand(val);
144 knownType_ = knownType;
145 }
146 void setLocalSlot(uint32_t slot) {
147 kind_ = LocalSlot;
148 new (&data.localSlot) uint32_t(slot);
149 knownType_ = JSVAL_TYPE_UNKNOWN;
150 }
151 void setArgSlot(uint32_t slot) {
152 kind_ = ArgSlot;
153 new (&data.argSlot) uint32_t(slot);
154 knownType_ = JSVAL_TYPE_UNKNOWN;
155 }
156 void setThis() {
157 kind_ = ThisSlot;
158 knownType_ = JSVAL_TYPE_UNKNOWN;
159 }
160 void setStack() {
161 kind_ = Stack;
162 knownType_ = JSVAL_TYPE_UNKNOWN;
163 }
164 };
165
166 enum StackAdjustment { AdjustStack, DontAdjustStack };
167
168 class FrameInfo {
169 protected:
170 MacroAssembler& masm;
171
172 public:
173 explicit FrameInfo(MacroAssembler& masm) : masm(masm) {}
174
175 Address addressOfLocal(size_t local) const {
176 return Address(FramePointer, BaselineFrame::reverseOffsetOfLocal(local));
177 }
178 Address addressOfArg(size_t arg) const {
179 return Address(FramePointer, JitFrameLayout::offsetOfActualArg(arg));
180 }
181 Address addressOfThis() const {
182 return Address(FramePointer, JitFrameLayout::offsetOfThis());
183 }
184 Address addressOfCalleeToken() const {
185 return Address(FramePointer, JitFrameLayout::offsetOfCalleeToken());
186 }
187 Address addressOfEnvironmentChain() const {
188 return Address(FramePointer,
189 BaselineFrame::reverseOffsetOfEnvironmentChain());
190 }
191 Address addressOfICScript() const {
192 return Address(FramePointer, BaselineFrame::reverseOffsetOfICScript());
193 }
194 Address addressOfFlags() const {
195 return Address(FramePointer, BaselineFrame::reverseOffsetOfFlags());
196 }
197 Address addressOfReturnValue() const {
198 return Address(FramePointer, BaselineFrame::reverseOffsetOfReturnValue());
199 }
200 Address addressOfArgsObj() const {
201 return Address(FramePointer, BaselineFrame::reverseOffsetOfArgsObj());
202 }
203 Address addressOfScratchValue() const {
204 return Address(FramePointer, BaselineFrame::reverseOffsetOfScratchValue());
205 }
206 Address addressOfScratchValueLow32() const {
207 return Address(FramePointer,
208 BaselineFrame::reverseOffsetOfScratchValueLow32());
209 }
210 Address addressOfScratchValueHigh32() const {
211 return Address(FramePointer,
212 BaselineFrame::reverseOffsetOfScratchValueHigh32());
213 }
214 #ifdef DEBUG
215 Address addressOfDebugFrameSize() const {
216 return Address(FramePointer,
217 BaselineFrame::reverseOffsetOfDebugFrameSize());
218 }
219 #endif
220 Address addressOfInterpreterScript() const {
221 return Address(FramePointer,
222 BaselineFrame::reverseOffsetOfInterpreterScript());
223 }
224 };
225
226 class CompilerFrameInfo : public FrameInfo {
227 friend class BaselinePerfSpewer;
228 JSScript* script;
229 FixedList<StackValue> stack;
230 size_t spIndex;
231
232 public:
233 CompilerFrameInfo(JSScript* script, MacroAssembler& masm)
234 : FrameInfo(masm), script(script), spIndex(0) {}
235 [[nodiscard]] bool init(TempAllocator& alloc);
236
237 size_t nlocals() const { return script->nfixed(); }
238 size_t nargs() const { return script->function()->nargs(); }
239
240 private:
241 inline StackValue* rawPush() {
242 StackValue* val = &stack[spIndex++];
243 val->reset();
244 return val;
245 }
246
247 inline StackValue* peek(int32_t index) const {
248 MOZ_ASSERT(index < 0);
249 return const_cast<StackValue*>(&stack[spIndex + index]);
250 }
251
252 public:
253 inline size_t stackDepth() const { return spIndex; }
254 inline void setStackDepth(uint32_t newDepth) {
255 if (newDepth <= stackDepth()) {
256 spIndex = newDepth;
257 } else {
258 uint32_t diff = newDepth - stackDepth();
259 for (uint32_t i = 0; i < diff; i++) {
260 StackValue* val = rawPush();
261 val->setStack();
262 }
263
264 MOZ_ASSERT(spIndex == newDepth);
265 }
266 }
267
268 void assertStackDepth(uint32_t depth) { MOZ_ASSERT(stackDepth() == depth); }
269 void incStackDepth(int32_t diff) { setStackDepth(stackDepth() + diff); }
270 bool hasKnownStackDepth(uint32_t depth) { return stackDepth() == depth; }
271
272 inline void pop(StackAdjustment adjust = AdjustStack);
273 inline void popn(uint32_t n, StackAdjustment adjust = AdjustStack);
274 inline void push(const Value& val) {
275 StackValue* sv = rawPush();
276 sv->setConstant(val);
277 }
278 inline void push(const ValueOperand& val,
279 JSValueType knownType = JSVAL_TYPE_UNKNOWN) {
280 StackValue* sv = rawPush();
281 sv->setRegister(val, knownType);
282 }
283 inline void pushLocal(uint32_t local) {
284 MOZ_ASSERT(local < nlocals());
285 StackValue* sv = rawPush();
286 sv->setLocalSlot(local);
287 }
288 inline void pushArg(uint32_t arg) {
289 StackValue* sv = rawPush();
290 sv->setArgSlot(arg);
291 }
292 inline void pushThis() {
293 StackValue* sv = rawPush();
294 sv->setThis();
295 }
296
297 inline void pushScratchValue() {
298 masm.pushValue(addressOfScratchValue());
299 StackValue* sv = rawPush();
300 sv->setStack();
301 }
302
303 Address addressOfLocal(size_t local) const {
304 MOZ_ASSERT(local < nlocals());
305 return FrameInfo::addressOfLocal(local);
306 }
307 Address addressOfArg(size_t arg) const {
308 MOZ_ASSERT(arg < nargs());
309 return FrameInfo::addressOfArg(arg);
310 }
311
312 Address addressOfStackValue(int32_t depth) const {
313 const StackValue* value = peek(depth);
314 MOZ_ASSERT(value->kind() == StackValue::Stack);
315 size_t slot = value - &stack[0];
316 MOZ_ASSERT(slot < stackDepth());
317 return Address(FramePointer,
318 BaselineFrame::reverseOffsetOfLocal(nlocals() + slot));
319 }
320
321 void popValue(ValueOperand dest);
322
323 void sync(StackValue* val);
324 void syncStack(uint32_t uses);
325 uint32_t numUnsyncedSlots();
326 void popRegsAndSync(uint32_t uses);
327
328 void assertSyncedStack() const {
329 MOZ_ASSERT_IF(stackDepth() > 0, peek(-1)->kind() == StackValue::Stack);
330 }
331
332 bool stackValueHasKnownType(int32_t depth, JSValueType type) const {
333 return peek(depth)->hasKnownType(type);
334 }
335
336 mozilla::Maybe<Value> knownStackValue(int32_t depth) const {
337 StackValue* val = peek(depth);
338 if (val->kind() == StackValue::Constant) {
339 return mozilla::Some(val->constant());
340 }
341 return mozilla::Nothing();
342 }
343
344 void storeStackValue(int32_t depth, const Address& dest,
345 const ValueOperand& scratch);
346
347 uint32_t frameSize() const {
348 return BaselineFrame::frameSizeForNumValueSlots(nlocals() + stackDepth());
349 }
350
351 #ifdef DEBUG
352 // Assert the state is valid before excuting "pc".
353 void assertValidState(const BytecodeInfo& info);
354 #else
355 inline void assertValidState(const BytecodeInfo& info) {}
356 #endif
357 };
358
359 class InterpreterFrameInfo : public FrameInfo {
360 public:
361 explicit InterpreterFrameInfo(MacroAssembler& masm) : FrameInfo(masm) {}
362
363 // These methods are no-ops in the interpreter, because we don't have a
364 // virtual stack there.
365 void syncStack(uint32_t uses) {}
366 void assertSyncedStack() const {}
367 void assertStackDepth(uint32_t depth) {}
368 void incStackDepth(int32_t diff) {}
369 bool hasKnownStackDepth(uint32_t depth) { return false; }
370 uint32_t numUnsyncedSlots() { return 0; }
371
372 bool stackValueHasKnownType(int32_t depth, JSValueType type) const {
373 return false;
374 }
375
376 mozilla::Maybe<Value> knownStackValue(int32_t depth) const {
377 return mozilla::Nothing();
378 }
379
380 Address addressOfStackValue(int depth) const {
381 MOZ_ASSERT(depth < 0);
382 return Address(masm.getStackPointer(),
383 masm.framePushed() + size_t(-(depth + 1)) * sizeof(Value));
384 }
385
386 BaseIndex addressOfStackValue(Register index, int32_t offset = 0) const {
387 return BaseIndex(masm.getStackPointer(), index, ValueScale, offset);
388 }
389
390 void popRegsAndSync(uint32_t uses);
391
392 inline void pop();
393
394 inline void popn(uint32_t n);
395
396 void popn(Register reg) {
397 // sp := sp + reg * sizeof(Value)
398 Register spReg = AsRegister(masm.getStackPointer());
399 masm.computeEffectiveAddress(BaseValueIndex(spReg, reg), spReg);
400 // On arm64, SP may be < PSP now (that's OK).
401 // eg testcase: tests/arguments/strict-args-generator-flushstack.js
402 }
403
404 void popValue(ValueOperand dest) { masm.popValue(dest); }
405
406 void push(const ValueOperand& val,
407 JSValueType knownType = JSVAL_TYPE_UNKNOWN) {
408 masm.pushValue(val);
409 }
410 void push(const Value& val) { masm.pushValue(val); }
411
412 void pushThis() { masm.pushValue(addressOfThis()); }
413 void pushScratchValue() { masm.pushValue(addressOfScratchValue()); }
414
415 void storeStackValue(int32_t depth, const Address& dest,
416 const ValueOperand& scratch) {
417 masm.loadValue(addressOfStackValue(depth), scratch);
418 masm.storeValue(scratch, dest);
419 }
420
421 void bumpInterpreterICEntry();
422
423 Address addressOfInterpreterPC() const {
424 return Address(FramePointer, BaselineFrame::reverseOffsetOfInterpreterPC());
425 }
426 Address addressOfInterpreterICEntry() const {
427 return Address(FramePointer,
428 BaselineFrame::reverseOffsetOfInterpreterICEntry());
429 }
430 };
431
432 } // namespace jit
433 } // namespace js
434
435 #endif /* jit_BaselineFrameInfo_h */