blob: c752fd22fb766312cb3c4a12df1426be197b26b4 [file] [log] [blame]
fpizlo@apple.comea92c202013-10-10 04:24:57 +00001/*
msaboff@apple.com6c6dffe2014-03-07 22:22:46 +00002 * Copyright (C) 2013, 2014 Apple Inc. All rights reserved.
fpizlo@apple.comea92c202013-10-10 04:24:57 +00003 *
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"
msaboff@apple.com6c6dffe2014-03-07 22:22:46 +000034#include "RegisterSet.h"
fpizlo@apple.comea92c202013-10-10 04:24:57 +000035
36namespace JSC { namespace FTL {
37
38static size_t bytesForGPRs()
39{
fpizlo@apple.com9dbc4b42013-10-21 04:48:29 +000040 return MacroAssembler::numberOfRegisters() * sizeof(int64_t);
fpizlo@apple.comea92c202013-10-10 04:24:57 +000041}
42
fpizlo@apple.com50cd41c2013-10-10 22:57:10 +000043static 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.
fpizlo@apple.com9dbc4b42013-10-21 04:48:29 +000048 return MacroAssembler::numberOfFPRegisters() * sizeof(double);
fpizlo@apple.com50cd41c2013-10-10 22:57:10 +000049}
50
fpizlo@apple.comea92c202013-10-10 04:24:57 +000051size_t requiredScratchMemorySizeInBytes()
52{
fpizlo@apple.com50cd41c2013-10-10 22:57:10 +000053 return bytesForGPRs() + bytesForFPRs();
fpizlo@apple.comea92c202013-10-10 04:24:57 +000054}
55
56size_t offsetOfGPR(GPRReg reg)
57{
fpizlo@apple.com9dbc4b42013-10-21 04:48:29 +000058 return MacroAssembler::registerIndex(reg) * sizeof(int64_t);
fpizlo@apple.comea92c202013-10-10 04:24:57 +000059}
60
fpizlo@apple.com50cd41c2013-10-10 22:57:10 +000061size_t offsetOfFPR(FPRReg reg)
62{
fpizlo@apple.com9dbc4b42013-10-21 04:48:29 +000063 return bytesForGPRs() + MacroAssembler::fpRegisterIndex(reg) * sizeof(double);
fpizlo@apple.com50cd41c2013-10-10 22:57:10 +000064}
65
fpizlo@apple.com4118d352014-02-18 23:04:53 +000066size_t offsetOfReg(Reg reg)
67{
68 if (reg.isGPR())
69 return offsetOfGPR(reg.gpr());
70 return offsetOfFPR(reg.fpr());
71}
72
msaboff@apple.com6c6dffe2014-03-07 22:22:46 +000073namespace {
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
fpizlo@apple.comea92c202013-10-10 04:24:57 +000096void saveAllRegisters(MacroAssembler& jit, char* scratchMemory)
97{
msaboff@apple.com6c6dffe2014-03-07 22:22:46 +000098 Regs regs;
99
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000100 // Get the first register out of the way, so that we can use it as a pointer.
msaboff@apple.com6c6dffe2014-03-07 22:22:46 +0000101 jit.poke64(regs.first, 0);
102 jit.move(MacroAssembler::TrustedImmPtr(scratchMemory), regs.first);
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000103
104 // Get all of the other GPRs out of the way.
msaboff@apple.com6c6dffe2014-03-07 22:22:46 +0000105 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 }
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000110
111 // Restore the first register into the second one and save it.
msaboff@apple.com6c6dffe2014-03-07 22:22:46 +0000112 jit.peek64(regs.second, 0);
113 jit.store64(regs.second, MacroAssembler::Address(regs.first, offsetOfGPR(regs.first)));
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000114
fpizlo@apple.com50cd41c2013-10-10 22:57:10 +0000115 // Finally save all FPR's.
msaboff@apple.com6c6dffe2014-03-07 22:22:46 +0000116 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 }
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000121}
122
123void restoreAllRegisters(MacroAssembler& jit, char* scratchMemory)
124{
msaboff@apple.com6c6dffe2014-03-07 22:22:46 +0000125 Regs regs;
126
fpizlo@apple.com50cd41c2013-10-10 22:57:10 +0000127 // Give ourselves a pointer to the scratch memory.
msaboff@apple.com6c6dffe2014-03-07 22:22:46 +0000128 jit.move(MacroAssembler::TrustedImmPtr(scratchMemory), regs.first);
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000129
fpizlo@apple.com50cd41c2013-10-10 22:57:10 +0000130 // Restore all FPR's.
msaboff@apple.com6c6dffe2014-03-07 22:22:46 +0000131 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 }
fpizlo@apple.com50cd41c2013-10-10 22:57:10 +0000136
msaboff@apple.com6c6dffe2014-03-07 22:22:46 +0000137 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 }
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000142
msaboff@apple.com6c6dffe2014-03-07 22:22:46 +0000143 jit.load64(MacroAssembler::Address(regs.first, offsetOfGPR(regs.first)), regs.first);
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000144}
145
146} } // namespace JSC::FTL
147
148#endif // ENABLE(FTL_JIT)
149