fpizlo@apple.com | ea92c20 | 2013-10-10 04:24:57 +0000 | [diff] [blame] | 1 | /* |
msaboff@apple.com | 6c6dffe | 2014-03-07 22:22:46 +0000 | [diff] [blame] | 2 | * Copyright (C) 2013, 2014 Apple Inc. All rights reserved. |
fpizlo@apple.com | ea92c20 | 2013-10-10 04:24:57 +0000 | [diff] [blame] | 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" |
msaboff@apple.com | 6c6dffe | 2014-03-07 22:22:46 +0000 | [diff] [blame] | 34 | #include "RegisterSet.h" |
fpizlo@apple.com | ea92c20 | 2013-10-10 04:24:57 +0000 | [diff] [blame] | 35 | |
| 36 | namespace JSC { namespace FTL { |
| 37 | |
| 38 | static size_t bytesForGPRs() |
| 39 | { |
fpizlo@apple.com | 9dbc4b4 | 2013-10-21 04:48:29 +0000 | [diff] [blame] | 40 | return MacroAssembler::numberOfRegisters() * sizeof(int64_t); |
fpizlo@apple.com | ea92c20 | 2013-10-10 04:24:57 +0000 | [diff] [blame] | 41 | } |
| 42 | |
fpizlo@apple.com | 50cd41c | 2013-10-10 22:57:10 +0000 | [diff] [blame] | 43 | static 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.com | 9dbc4b4 | 2013-10-21 04:48:29 +0000 | [diff] [blame] | 48 | return MacroAssembler::numberOfFPRegisters() * sizeof(double); |
fpizlo@apple.com | 50cd41c | 2013-10-10 22:57:10 +0000 | [diff] [blame] | 49 | } |
| 50 | |
fpizlo@apple.com | ea92c20 | 2013-10-10 04:24:57 +0000 | [diff] [blame] | 51 | size_t requiredScratchMemorySizeInBytes() |
| 52 | { |
fpizlo@apple.com | 50cd41c | 2013-10-10 22:57:10 +0000 | [diff] [blame] | 53 | return bytesForGPRs() + bytesForFPRs(); |
fpizlo@apple.com | ea92c20 | 2013-10-10 04:24:57 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | size_t offsetOfGPR(GPRReg reg) |
| 57 | { |
fpizlo@apple.com | 9dbc4b4 | 2013-10-21 04:48:29 +0000 | [diff] [blame] | 58 | return MacroAssembler::registerIndex(reg) * sizeof(int64_t); |
fpizlo@apple.com | ea92c20 | 2013-10-10 04:24:57 +0000 | [diff] [blame] | 59 | } |
| 60 | |
fpizlo@apple.com | 50cd41c | 2013-10-10 22:57:10 +0000 | [diff] [blame] | 61 | size_t offsetOfFPR(FPRReg reg) |
| 62 | { |
fpizlo@apple.com | 9dbc4b4 | 2013-10-21 04:48:29 +0000 | [diff] [blame] | 63 | return bytesForGPRs() + MacroAssembler::fpRegisterIndex(reg) * sizeof(double); |
fpizlo@apple.com | 50cd41c | 2013-10-10 22:57:10 +0000 | [diff] [blame] | 64 | } |
| 65 | |
fpizlo@apple.com | 4118d35 | 2014-02-18 23:04:53 +0000 | [diff] [blame] | 66 | size_t offsetOfReg(Reg reg) |
| 67 | { |
| 68 | if (reg.isGPR()) |
| 69 | return offsetOfGPR(reg.gpr()); |
| 70 | return offsetOfFPR(reg.fpr()); |
| 71 | } |
| 72 | |
msaboff@apple.com | 6c6dffe | 2014-03-07 22:22:46 +0000 | [diff] [blame] | 73 | namespace { |
| 74 | |
| 75 | struct 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.com | ea92c20 | 2013-10-10 04:24:57 +0000 | [diff] [blame] | 96 | void saveAllRegisters(MacroAssembler& jit, char* scratchMemory) |
| 97 | { |
msaboff@apple.com | 6c6dffe | 2014-03-07 22:22:46 +0000 | [diff] [blame] | 98 | Regs regs; |
| 99 | |
fpizlo@apple.com | ea92c20 | 2013-10-10 04:24:57 +0000 | [diff] [blame] | 100 | // Get the first register out of the way, so that we can use it as a pointer. |
msaboff@apple.com | 6c6dffe | 2014-03-07 22:22:46 +0000 | [diff] [blame] | 101 | jit.poke64(regs.first, 0); |
| 102 | jit.move(MacroAssembler::TrustedImmPtr(scratchMemory), regs.first); |
fpizlo@apple.com | ea92c20 | 2013-10-10 04:24:57 +0000 | [diff] [blame] | 103 | |
| 104 | // Get all of the other GPRs out of the way. |
msaboff@apple.com | 6c6dffe | 2014-03-07 22:22:46 +0000 | [diff] [blame] | 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 | } |
fpizlo@apple.com | ea92c20 | 2013-10-10 04:24:57 +0000 | [diff] [blame] | 110 | |
| 111 | // Restore the first register into the second one and save it. |
msaboff@apple.com | 6c6dffe | 2014-03-07 22:22:46 +0000 | [diff] [blame] | 112 | jit.peek64(regs.second, 0); |
| 113 | jit.store64(regs.second, MacroAssembler::Address(regs.first, offsetOfGPR(regs.first))); |
fpizlo@apple.com | ea92c20 | 2013-10-10 04:24:57 +0000 | [diff] [blame] | 114 | |
fpizlo@apple.com | 50cd41c | 2013-10-10 22:57:10 +0000 | [diff] [blame] | 115 | // Finally save all FPR's. |
msaboff@apple.com | 6c6dffe | 2014-03-07 22:22:46 +0000 | [diff] [blame] | 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 | } |
fpizlo@apple.com | ea92c20 | 2013-10-10 04:24:57 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void restoreAllRegisters(MacroAssembler& jit, char* scratchMemory) |
| 124 | { |
msaboff@apple.com | 6c6dffe | 2014-03-07 22:22:46 +0000 | [diff] [blame] | 125 | Regs regs; |
| 126 | |
fpizlo@apple.com | 50cd41c | 2013-10-10 22:57:10 +0000 | [diff] [blame] | 127 | // Give ourselves a pointer to the scratch memory. |
msaboff@apple.com | 6c6dffe | 2014-03-07 22:22:46 +0000 | [diff] [blame] | 128 | jit.move(MacroAssembler::TrustedImmPtr(scratchMemory), regs.first); |
fpizlo@apple.com | ea92c20 | 2013-10-10 04:24:57 +0000 | [diff] [blame] | 129 | |
fpizlo@apple.com | 50cd41c | 2013-10-10 22:57:10 +0000 | [diff] [blame] | 130 | // Restore all FPR's. |
msaboff@apple.com | 6c6dffe | 2014-03-07 22:22:46 +0000 | [diff] [blame] | 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 | } |
fpizlo@apple.com | 50cd41c | 2013-10-10 22:57:10 +0000 | [diff] [blame] | 136 | |
msaboff@apple.com | 6c6dffe | 2014-03-07 22:22:46 +0000 | [diff] [blame] | 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 | } |
fpizlo@apple.com | ea92c20 | 2013-10-10 04:24:57 +0000 | [diff] [blame] | 142 | |
msaboff@apple.com | 6c6dffe | 2014-03-07 22:22:46 +0000 | [diff] [blame] | 143 | jit.load64(MacroAssembler::Address(regs.first, offsetOfGPR(regs.first)), regs.first); |
fpizlo@apple.com | ea92c20 | 2013-10-10 04:24:57 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | } } // namespace JSC::FTL |
| 147 | |
| 148 | #endif // ENABLE(FTL_JIT) |
| 149 | |