1/*
2 * Copyright (C) 2013, 2014 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "FTLSaveRestore.h"
28
29#if ENABLE(FTL_JIT)
30
31#include "FPRInfo.h"
32#include "GPRInfo.h"
33#include "MacroAssembler.h"
34#include "RegisterSet.h"
35
36namespace JSC { namespace FTL {
37
38static size_t bytesForGPRs()
39{
40    return MacroAssembler::numberOfRegisters() * sizeof(int64_t);
41}
42
43static size_t bytesForFPRs()
44{
45    // FIXME: It might be worthwhile saving the full state of the FP registers, at some point.
46    // Right now we don't need this since we only do the save/restore just prior to OSR exit, and
47    // OSR exit will be guaranteed to only need the double portion of the FP registers.
48    return MacroAssembler::numberOfFPRegisters() * sizeof(double);
49}
50
51size_t requiredScratchMemorySizeInBytes()
52{
53    return bytesForGPRs() + bytesForFPRs();
54}
55
56size_t offsetOfGPR(GPRReg reg)
57{
58    return MacroAssembler::registerIndex(reg) * sizeof(int64_t);
59}
60
61size_t offsetOfFPR(FPRReg reg)
62{
63    return bytesForGPRs() + MacroAssembler::fpRegisterIndex(reg) * sizeof(double);
64}
65
66size_t offsetOfReg(Reg reg)
67{
68    if (reg.isGPR())
69        return offsetOfGPR(reg.gpr());
70    return offsetOfFPR(reg.fpr());
71}
72
73namespace {
74
75struct Regs {
76    Regs()
77    {
78        special = RegisterSet::stackRegisters();
79        special.merge(RegisterSet::reservedHardwareRegisters());
80
81        first = MacroAssembler::firstRegister();
82        while (special.get(first))
83            first = MacroAssembler::nextRegister(first);
84        second = MacroAssembler::nextRegister(first);
85        while (special.get(second))
86            second = MacroAssembler::nextRegister(second);
87    }
88
89    RegisterSet special;
90    GPRReg first;
91    GPRReg second;
92};
93
94} // anonymous namespace
95
96void saveAllRegisters(MacroAssembler& jit, char* scratchMemory)
97{
98    Regs regs;
99
100    // Get the first register out of the way, so that we can use it as a pointer.
101    jit.poke64(regs.first, 0);
102    jit.move(MacroAssembler::TrustedImmPtr(scratchMemory), regs.first);
103
104    // Get all of the other GPRs out of the way.
105    for (MacroAssembler::RegisterID reg = regs.second; reg <= MacroAssembler::lastRegister(); reg = MacroAssembler::nextRegister(reg)) {
106        if (regs.special.get(reg))
107            continue;
108        jit.store64(reg, MacroAssembler::Address(regs.first, offsetOfGPR(reg)));
109    }
110
111    // Restore the first register into the second one and save it.
112    jit.peek64(regs.second, 0);
113    jit.store64(regs.second, MacroAssembler::Address(regs.first, offsetOfGPR(regs.first)));
114
115    // Finally save all FPR's.
116    for (MacroAssembler::FPRegisterID reg = MacroAssembler::firstFPRegister(); reg <= MacroAssembler::lastFPRegister(); reg = MacroAssembler::nextFPRegister(reg)) {
117        if (regs.special.get(reg))
118            continue;
119        jit.storeDouble(reg, MacroAssembler::Address(regs.first, offsetOfFPR(reg)));
120    }
121}
122
123void restoreAllRegisters(MacroAssembler& jit, char* scratchMemory)
124{
125    Regs regs;
126
127    // Give ourselves a pointer to the scratch memory.
128    jit.move(MacroAssembler::TrustedImmPtr(scratchMemory), regs.first);
129
130    // Restore all FPR's.
131    for (MacroAssembler::FPRegisterID reg = MacroAssembler::firstFPRegister(); reg <= MacroAssembler::lastFPRegister(); reg = MacroAssembler::nextFPRegister(reg)) {
132        if (regs.special.get(reg))
133            continue;
134        jit.loadDouble(MacroAssembler::Address(regs.first, offsetOfFPR(reg)), reg);
135    }
136
137    for (MacroAssembler::RegisterID reg = regs.second; reg <= MacroAssembler::lastRegister(); reg = MacroAssembler::nextRegister(reg)) {
138        if (regs.special.get(reg))
139            continue;
140        jit.load64(MacroAssembler::Address(regs.first, offsetOfGPR(reg)), reg);
141    }
142
143    jit.load64(MacroAssembler::Address(regs.first, offsetOfGPR(regs.first)), regs.first);
144}
145
146} } // namespace JSC::FTL
147
148#endif // ENABLE(FTL_JIT)
149
150