summaryrefslogtreecommitdiffstats
path: root/js/src/jit/BaselineFrameInfo.cpp
blob: d641ace2ab14f9d42bc0cdd1e7564a986d0a3a8c (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
/* -*- 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/. */

#include "jit/BaselineFrameInfo.h"

#include <algorithm>

#include "jit/BaselineIC.h"
#ifdef DEBUG
#  include "jit/BytecodeAnalysis.h"
#endif

#include "jit/BaselineFrameInfo-inl.h"
#include "jit/JitFrames.h"
#include "jit/MacroAssembler-inl.h"

using namespace js;
using namespace js::jit;

bool CompilerFrameInfo::init(TempAllocator& alloc) {
  // An extra slot is needed for global scopes because INITGLEXICAL (stack
  // depth 1) is compiled as a SETPROP (stack depth 2) on the global lexical
  // scope.
  size_t extra = script->isGlobalCode() ? 1 : 0;
  size_t nstack =
      std::max(script->nslots() - script->nfixed(), size_t(MinJITStackSize)) +
      extra;
  if (!stack.init(alloc, nstack)) {
    return false;
  }

  return true;
}

void CompilerFrameInfo::sync(StackValue* val) {
  switch (val->kind()) {
    case StackValue::Stack:
      break;
    case StackValue::LocalSlot:
      masm.pushValue(addressOfLocal(val->localSlot()));
      break;
    case StackValue::ArgSlot:
      masm.pushValue(addressOfArg(val->argSlot()));
      break;
    case StackValue::ThisSlot:
      masm.pushValue(addressOfThis());
      break;
    case StackValue::Register:
      masm.pushValue(val->reg());
      break;
    case StackValue::Constant:
      masm.pushValue(val->constant());
      break;
    default:
      MOZ_CRASH("Invalid kind");
  }

  val->setStack();
}

void CompilerFrameInfo::syncStack(uint32_t uses) {
  MOZ_ASSERT(uses <= stackDepth());

  uint32_t depth = stackDepth() - uses;

  for (uint32_t i = 0; i < depth; i++) {
    StackValue* current = &stack[i];
    sync(current);
  }
}

uint32_t CompilerFrameInfo::numUnsyncedSlots() {
  // Start at the bottom, find the first value that's not synced.
  uint32_t i = 0;
  for (; i < stackDepth(); i++) {
    if (peek(-int32_t(i + 1))->kind() == StackValue::Stack) {
      break;
    }
  }
  return i;
}

void CompilerFrameInfo::popValue(ValueOperand dest) {
  StackValue* val = peek(-1);

  switch (val->kind()) {
    case StackValue::Constant:
      masm.moveValue(val->constant(), dest);
      break;
    case StackValue::LocalSlot:
      masm.loadValue(addressOfLocal(val->localSlot()), dest);
      break;
    case StackValue::ArgSlot:
      masm.loadValue(addressOfArg(val->argSlot()), dest);
      break;
    case StackValue::ThisSlot:
      masm.loadValue(addressOfThis(), dest);
      break;
    case StackValue::Stack:
      masm.popValue(dest);
      break;
    case StackValue::Register:
      masm.moveValue(val->reg(), dest);
      break;
    default:
      MOZ_CRASH("Invalid kind");
  }

  // masm.popValue already adjusted the stack pointer, don't do it twice.
  pop(DontAdjustStack);
}

void CompilerFrameInfo::popRegsAndSync(uint32_t uses) {
  // x86 has only 3 Value registers. Only support 2 regs here for now,
  // so that there's always a scratch Value register for reg -> reg
  // moves.
  MOZ_ASSERT(uses > 0);
  MOZ_ASSERT(uses <= 2);
  MOZ_ASSERT(uses <= stackDepth());

  syncStack(uses);

  switch (uses) {
    case 1:
      popValue(R0);
      break;
    case 2: {
      // If the second value is in R1, move it to R2 so that it's not
      // clobbered by the first popValue.
      StackValue* val = peek(-2);
      if (val->kind() == StackValue::Register && val->reg() == R1) {
        masm.moveValue(R1, ValueOperand(R2));
        val->setRegister(R2);
      }
      popValue(R1);
      popValue(R0);
      break;
    }
    default:
      MOZ_CRASH("Invalid uses");
  }
  // On arm64, SP may be < PSP now (that's OK).
  // eg testcase: tests/bug1580246.js
}

void InterpreterFrameInfo::popRegsAndSync(uint32_t uses) {
  switch (uses) {
    case 1:
      popValue(R0);
      break;
    case 2: {
      popValue(R1);
      popValue(R0);
      break;
    }
    default:
      MOZ_CRASH("Invalid uses");
  }
  // On arm64, SP may be < PSP now (that's OK).
  // eg testcase: tests/backup-point-bug1315634.js
}

void InterpreterFrameInfo::bumpInterpreterICEntry() {
  masm.addPtr(Imm32(sizeof(ICEntry)), addressOfInterpreterICEntry());
}

void CompilerFrameInfo::storeStackValue(int32_t depth, const Address& dest,
                                        const ValueOperand& scratch) {
  const StackValue* source = peek(depth);
  switch (source->kind()) {
    case StackValue::Constant:
      masm.storeValue(source->constant(), dest);
      break;
    case StackValue::Register:
      masm.storeValue(source->reg(), dest);
      break;
    case StackValue::LocalSlot:
      masm.loadValue(addressOfLocal(source->localSlot()), scratch);
      masm.storeValue(scratch, dest);
      break;
    case StackValue::ArgSlot:
      masm.loadValue(addressOfArg(source->argSlot()), scratch);
      masm.storeValue(scratch, dest);
      break;
    case StackValue::ThisSlot:
      masm.loadValue(addressOfThis(), scratch);
      masm.storeValue(scratch, dest);
      break;
    case StackValue::Stack:
      masm.loadValue(addressOfStackValue(depth), scratch);
      masm.storeValue(scratch, dest);
      break;
    default:
      MOZ_CRASH("Invalid kind");
  }
}

#ifdef DEBUG
void CompilerFrameInfo::assertValidState(const BytecodeInfo& info) {
  // Check stack depth.
  MOZ_ASSERT(stackDepth() == info.stackDepth);

  // Start at the bottom, find the first value that's not synced.
  uint32_t i = 0;
  for (; i < stackDepth(); i++) {
    if (stack[i].kind() != StackValue::Stack) {
      break;
    }
  }

  // Assert all values on top of it are also not synced.
  for (; i < stackDepth(); i++) {
    MOZ_ASSERT(stack[i].kind() != StackValue::Stack);
  }

  // Assert every Value register is used by at most one StackValue.
  // R2 is used as scratch register by the compiler and FrameInfo,
  // so it shouldn't be used for StackValues.
  bool usedR0 = false, usedR1 = false;

  for (i = 0; i < stackDepth(); i++) {
    if (stack[i].kind() == StackValue::Register) {
      ValueOperand reg = stack[i].reg();
      if (reg == R0) {
        MOZ_ASSERT(!usedR0);
        usedR0 = true;
      } else if (reg == R1) {
        MOZ_ASSERT(!usedR1);
        usedR1 = true;
      } else {
        MOZ_CRASH("Invalid register");
      }
    }
  }
}
#endif