summaryrefslogtreecommitdiffstats
path: root/js/src/jit/BaselineFrameInfo.cpp
blob: 5afab76aa8103da795d2d1eb8a537c35d402bd48 (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * 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"

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

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

using namespace js;
using namespace js::jit;

bool
FrameInfo::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 = Max(script->nslots() - script->nfixed(), size_t(MinJITStackSize)) + extra;
    if (!stack.init(alloc, nstack))
        return false;

    return true;
}

void
FrameInfo::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::EvalNewTargetSlot:
        MOZ_ASSERT(script->isForEval());
        masm.pushValue(addressOfEvalNewTarget());
        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
FrameInfo::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
FrameInfo::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
FrameInfo::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::EvalNewTargetSlot:
        masm.loadValue(addressOfEvalNewTarget(), 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
FrameInfo::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, R2);
            val->setRegister(R2);
        }
        popValue(R1);
        popValue(R0);
        break;
      }
      default:
        MOZ_CRASH("Invalid uses");
    }
}

#ifdef DEBUG
void
FrameInfo::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