summaryrefslogtreecommitdiffstats
path: root/js/src/jit/BaselineFrameInfo.h
blob: b777aad809c3362c133da5ebe7e8d018a618c8f2 (plain)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef jit_BaselineFrameInfo_h
#define jit_BaselineFrameInfo_h

#include "mozilla/Attributes.h"
#include "mozilla/Maybe.h"

#include <new>

#include "jit/BaselineFrame.h"
#include "jit/BaselineJIT.h"
#include "jit/FixedList.h"
#include "jit/MacroAssembler.h"
#include "jit/SharedICRegisters.h"

namespace js {
namespace jit {

struct BytecodeInfo;
class MacroAssembler;

// [SMDOC] Baseline FrameInfo overview.
//
// FrameInfo is used by BaselineCodeGen to track values stored in the frame.
// There are two implementations:
//
// InterpreterFrameInfo
// --------------------
// The InterpreterFrameInfo class is used by the interpreter generator and is
// a very simple interface on top of the MacroAssembler, because the stack is
// always synced.
//
// CompilerFrameInfo
// -----------------
// The CompilerFrameInfo class is more complicated because it maintains a
// virtual stack to optimize some common stack operations. Locals and arguments
// are always fully synced. Stack values can either be synced, stored as
// constant, stored in a Value register or refer to a local slot. Syncing a
// StackValue ensures it's stored on the stack, e.g. kind == Stack.
//
// To see how this works, consider the following statement:
//
//    var y = x + 9;
//
// Here two values are pushed: StackValue(LocalSlot(0)) and
// StackValue(Int32Value(9)). Only when we reach the ADD op, code is generated
// to load the operands directly into the right operand registers and sync all
// other stack values.
//
// For stack values, the following invariants hold (and are checked between
// ops):
//
// (1) If a value is synced (kind == Stack), all values below it must also be
//     synced. In other words, values with kind other than Stack can only appear
//     on top of the abstract stack.
//
// (2) When we call a stub or IC, all values still on the stack must be synced.

// Represents a value pushed on the stack. Note that StackValue is not used for
// locals or arguments since these are always fully synced.
class StackValue {
 public:
  enum Kind {
    Constant,
    Register,
    Stack,
    LocalSlot,
    ArgSlot,
    ThisSlot,
#ifdef DEBUG
    // In debug builds, assert Kind is initialized.
    Uninitialized,
#endif
  };

 private:
  MOZ_INIT_OUTSIDE_CTOR Kind kind_;

  MOZ_INIT_OUTSIDE_CTOR union Data {
    JS::Value constant;
    ValueOperand reg;
    uint32_t localSlot;
    uint32_t argSlot;

    // |constant| has a non-trivial constructor and therefore MUST be
    // placement-new'd into existence.
    MOZ_PUSH_DISABLE_NONTRIVIAL_UNION_WARNINGS
    Data() {}
    MOZ_POP_DISABLE_NONTRIVIAL_UNION_WARNINGS
  } data;

  MOZ_INIT_OUTSIDE_CTOR JSValueType knownType_;

 public:
  StackValue() { reset(); }

  Kind kind() const { return kind_; }
  bool hasKnownType() const { return knownType_ != JSVAL_TYPE_UNKNOWN; }
  bool hasKnownType(JSValueType type) const {
    MOZ_ASSERT(type != JSVAL_TYPE_UNKNOWN);
    return knownType_ == type;
  }
  JSValueType knownType() const {
    MOZ_ASSERT(hasKnownType());
    return knownType_;
  }
  void reset() {
#ifdef DEBUG
    kind_ = Uninitialized;
    knownType_ = JSVAL_TYPE_UNKNOWN;
#endif
  }
  Value constant() const {
    MOZ_ASSERT(kind_ == Constant);
    return data.constant;
  }
  ValueOperand reg() const {
    MOZ_ASSERT(kind_ == Register);
    return data.reg;
  }
  uint32_t localSlot() const {
    MOZ_ASSERT(kind_ == LocalSlot);
    return data.localSlot;
  }
  uint32_t argSlot() const {
    MOZ_ASSERT(kind_ == ArgSlot);
    return data.argSlot;
  }

  void setConstant(const Value& v) {
    kind_ = Constant;
    new (&data.constant) Value(v);
    knownType_ = v.isDouble() ? JSVAL_TYPE_DOUBLE : v.extractNonDoubleType();
  }
  void setRegister(const ValueOperand& val,
                   JSValueType knownType = JSVAL_TYPE_UNKNOWN) {
    kind_ = Register;
    new (&data.reg) ValueOperand(val);
    knownType_ = knownType;
  }
  void setLocalSlot(uint32_t slot) {
    kind_ = LocalSlot;
    new (&data.localSlot) uint32_t(slot);
    knownType_ = JSVAL_TYPE_UNKNOWN;
  }
  void setArgSlot(uint32_t slot) {
    kind_ = ArgSlot;
    new (&data.argSlot) uint32_t(slot);
    knownType_ = JSVAL_TYPE_UNKNOWN;
  }
  void setThis() {
    kind_ = ThisSlot;
    knownType_ = JSVAL_TYPE_UNKNOWN;
  }
  void setStack() {
    kind_ = Stack;
    knownType_ = JSVAL_TYPE_UNKNOWN;
  }
};

enum StackAdjustment { AdjustStack, DontAdjustStack };

class FrameInfo {
 protected:
  MacroAssembler& masm;

 public:
  explicit FrameInfo(MacroAssembler& masm) : masm(masm) {}

  Address addressOfLocal(size_t local) const {
    return Address(FramePointer, BaselineFrame::reverseOffsetOfLocal(local));
  }
  Address addressOfArg(size_t arg) const {
    return Address(FramePointer, JitFrameLayout::offsetOfActualArg(arg));
  }
  Address addressOfThis() const {
    return Address(FramePointer, JitFrameLayout::offsetOfThis());
  }
  Address addressOfCalleeToken() const {
    return Address(FramePointer, JitFrameLayout::offsetOfCalleeToken());
  }
  Address addressOfEnvironmentChain() const {
    return Address(FramePointer,
                   BaselineFrame::reverseOffsetOfEnvironmentChain());
  }
  Address addressOfICScript() const {
    return Address(FramePointer, BaselineFrame::reverseOffsetOfICScript());
  }
  Address addressOfFlags() const {
    return Address(FramePointer, BaselineFrame::reverseOffsetOfFlags());
  }
  Address addressOfReturnValue() const {
    return Address(FramePointer, BaselineFrame::reverseOffsetOfReturnValue());
  }
  Address addressOfArgsObj() const {
    return Address(FramePointer, BaselineFrame::reverseOffsetOfArgsObj());
  }
  Address addressOfScratchValue() const {
    return Address(FramePointer, BaselineFrame::reverseOffsetOfScratchValue());
  }
  Address addressOfScratchValueLow32() const {
    return Address(FramePointer,
                   BaselineFrame::reverseOffsetOfScratchValueLow32());
  }
  Address addressOfScratchValueHigh32() const {
    return Address(FramePointer,
                   BaselineFrame::reverseOffsetOfScratchValueHigh32());
  }
#ifdef DEBUG
  Address addressOfDebugFrameSize() const {
    return Address(FramePointer,
                   BaselineFrame::reverseOffsetOfDebugFrameSize());
  }
#endif
};

class CompilerFrameInfo : public FrameInfo {
  JSScript* script;
  FixedList<StackValue> stack;
  size_t spIndex;

 public:
  CompilerFrameInfo(JSScript* script, MacroAssembler& masm)
      : FrameInfo(masm), script(script), stack(), spIndex(0) {}
  [[nodiscard]] bool init(TempAllocator& alloc);

  size_t nlocals() const { return script->nfixed(); }
  size_t nargs() const { return script->function()->nargs(); }

 private:
  inline StackValue* rawPush() {
    StackValue* val = &stack[spIndex++];
    val->reset();
    return val;
  }

  inline StackValue* peek(int32_t index) const {
    MOZ_ASSERT(index < 0);
    return const_cast<StackValue*>(&stack[spIndex + index]);
  }

 public:
  inline size_t stackDepth() const { return spIndex; }
  inline void setStackDepth(uint32_t newDepth) {
    if (newDepth <= stackDepth()) {
      spIndex = newDepth;
    } else {
      uint32_t diff = newDepth - stackDepth();
      for (uint32_t i = 0; i < diff; i++) {
        StackValue* val = rawPush();
        val->setStack();
      }

      MOZ_ASSERT(spIndex == newDepth);
    }
  }

  void assertStackDepth(uint32_t depth) { MOZ_ASSERT(stackDepth() == depth); }
  void incStackDepth(int32_t diff) { setStackDepth(stackDepth() + diff); }
  bool hasKnownStackDepth(uint32_t depth) { return stackDepth() == depth; }

  inline void pop(StackAdjustment adjust = AdjustStack);
  inline void popn(uint32_t n, StackAdjustment adjust = AdjustStack);
  inline void push(const Value& val) {
    StackValue* sv = rawPush();
    sv->setConstant(val);
  }
  inline void push(const ValueOperand& val,
                   JSValueType knownType = JSVAL_TYPE_UNKNOWN) {
    StackValue* sv = rawPush();
    sv->setRegister(val, knownType);
  }
  inline void pushLocal(uint32_t local) {
    MOZ_ASSERT(local < nlocals());
    StackValue* sv = rawPush();
    sv->setLocalSlot(local);
  }
  inline void pushArg(uint32_t arg) {
    StackValue* sv = rawPush();
    sv->setArgSlot(arg);
  }
  inline void pushThis() {
    StackValue* sv = rawPush();
    sv->setThis();
  }

  inline void pushScratchValue() {
    masm.pushValue(addressOfScratchValue());
    StackValue* sv = rawPush();
    sv->setStack();
  }

  Address addressOfLocal(size_t local) const {
    MOZ_ASSERT(local < nlocals());
    return FrameInfo::addressOfLocal(local);
  }
  Address addressOfArg(size_t arg) const {
    MOZ_ASSERT(arg < nargs());
    return FrameInfo::addressOfArg(arg);
  }

  Address addressOfStackValue(int32_t depth) const {
    const StackValue* value = peek(depth);
    MOZ_ASSERT(value->kind() == StackValue::Stack);
    size_t slot = value - &stack[0];
    MOZ_ASSERT(slot < stackDepth());
    return Address(FramePointer,
                   BaselineFrame::reverseOffsetOfLocal(nlocals() + slot));
  }

  void popValue(ValueOperand dest);

  void sync(StackValue* val);
  void syncStack(uint32_t uses);
  uint32_t numUnsyncedSlots();
  void popRegsAndSync(uint32_t uses);

  void assertSyncedStack() const {
    MOZ_ASSERT_IF(stackDepth() > 0, peek(-1)->kind() == StackValue::Stack);
  }

  bool stackValueHasKnownType(int32_t depth, JSValueType type) const {
    return peek(depth)->hasKnownType(type);
  }

  mozilla::Maybe<Value> knownStackValue(int32_t depth) const {
    StackValue* val = peek(depth);
    if (val->kind() == StackValue::Constant) {
      return mozilla::Some(val->constant());
    }
    return mozilla::Nothing();
  }

  void storeStackValue(int32_t depth, const Address& dest,
                       const ValueOperand& scratch);

  uint32_t frameSize() const {
    return BaselineFrame::frameSizeForNumValueSlots(nlocals() + stackDepth());
  }

#ifdef DEBUG
  // Assert the state is valid before excuting "pc".
  void assertValidState(const BytecodeInfo& info);
#else
  inline void assertValidState(const BytecodeInfo& info) {}
#endif
};

class InterpreterFrameInfo : public FrameInfo {
 public:
  explicit InterpreterFrameInfo(MacroAssembler& masm) : FrameInfo(masm) {}

  // These methods are no-ops in the interpreter, because we don't have a
  // virtual stack there.
  void syncStack(uint32_t uses) {}
  void assertSyncedStack() const {}
  void assertStackDepth(uint32_t depth) {}
  void incStackDepth(int32_t diff) {}
  bool hasKnownStackDepth(uint32_t depth) { return false; }
  uint32_t numUnsyncedSlots() { return 0; }

  bool stackValueHasKnownType(int32_t depth, JSValueType type) const {
    return false;
  }

  mozilla::Maybe<Value> knownStackValue(int32_t depth) const {
    return mozilla::Nothing();
  }

  Address addressOfStackValue(int depth) const {
    MOZ_ASSERT(depth < 0);
    return Address(masm.getStackPointer(),
                   masm.framePushed() + size_t(-(depth + 1)) * sizeof(Value));
  }

  BaseIndex addressOfStackValue(Register index, int32_t offset = 0) const {
    return BaseIndex(masm.getStackPointer(), index, ValueScale, offset);
  }

  void popRegsAndSync(uint32_t uses);

  inline void pop();

  inline void popn(uint32_t n);

  void popn(Register reg) {
    // sp := sp + reg * sizeof(Value)
    Register spReg = AsRegister(masm.getStackPointer());
    masm.computeEffectiveAddress(BaseValueIndex(spReg, reg), spReg);
    // On arm64, SP may be < PSP now (that's OK).
    // eg testcase: tests/arguments/strict-args-generator-flushstack.js
  }

  void popValue(ValueOperand dest) { masm.popValue(dest); }

  void push(const ValueOperand& val,
            JSValueType knownType = JSVAL_TYPE_UNKNOWN) {
    masm.pushValue(val);
  }
  void push(const Value& val) { masm.pushValue(val); }

  void pushThis() { masm.pushValue(addressOfThis()); }
  void pushScratchValue() { masm.pushValue(addressOfScratchValue()); }

  void storeStackValue(int32_t depth, const Address& dest,
                       const ValueOperand& scratch) {
    masm.loadValue(addressOfStackValue(depth), scratch);
    masm.storeValue(scratch, dest);
  }

  void bumpInterpreterICEntry();

  Address addressOfInterpreterScript() const {
    return Address(FramePointer,
                   BaselineFrame::reverseOffsetOfInterpreterScript());
  }
  Address addressOfInterpreterPC() const {
    return Address(FramePointer, BaselineFrame::reverseOffsetOfInterpreterPC());
  }
  Address addressOfInterpreterICEntry() const {
    return Address(FramePointer,
                   BaselineFrame::reverseOffsetOfInterpreterICEntry());
  }
};

}  // namespace jit
}  // namespace js

#endif /* jit_BaselineFrameInfo_h */