blob: bbb22e013e1db931ac334b241d71be745c7ea728 [file] [log] [blame]
oliver@apple.comb9009142013-07-25 03:58:26 +00001/*
fpizlo@apple.comda834ae2015-03-26 04:28:43 +00002 * Copyright (C) 2013, 2015 Apple Inc. All rights reserved.
oliver@apple.comb9009142013-07-25 03:58:26 +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#ifndef DFGOSRExitCompilerCommon_h
27#define DFGOSRExitCompilerCommon_h
28
oliver@apple.comb9009142013-07-25 03:58:26 +000029#if ENABLE(DFG_JIT)
30
fpizlo@apple.comcd8eb2c2013-09-20 18:42:41 +000031#include "CCallHelpers.h"
oliver@apple.comb9009142013-07-25 03:58:26 +000032#include "DFGOSRExit.h"
sbarati@apple.coma582c182015-12-05 00:04:01 +000033#include "DFGCommonData.h"
34#include "DFGJITCode.h"
35#include "FTLJITCode.h"
commit-queue@webkit.orgdad321b2016-01-28 00:29:09 +000036#include "RegisterSet.h"
oliver@apple.comb9009142013-07-25 03:58:26 +000037
38namespace JSC { namespace DFG {
39
oliver@apple.comea771492013-07-25 03:58:38 +000040void handleExitCounts(CCallHelpers&, const OSRExitBase&);
41void reifyInlinedCallFrames(CCallHelpers&, const OSRExitBase&);
sbarati@apple.comfb6a8e22016-01-29 19:46:01 +000042void adjustAndJumpToTarget(CCallHelpers&, const OSRExitBase&);
oliver@apple.comb9009142013-07-25 03:58:26 +000043
sbarati@apple.coma582c182015-12-05 00:04:01 +000044template <typename JITCodeType>
45void adjustFrameAndStackInOSRExitCompilerThunk(MacroAssembler& jit, VM* vm, JITCode::JITType jitType)
46{
47 ASSERT(jitType == JITCode::DFGJIT || jitType == JITCode::FTLJIT);
commit-queue@webkit.orgdad321b2016-01-28 00:29:09 +000048
sbarati@apple.coma582c182015-12-05 00:04:01 +000049 bool isFTLOSRExit = jitType == JITCode::FTLJIT;
commit-queue@webkit.orgdad321b2016-01-28 00:29:09 +000050 RegisterSet registersToPreserve;
51 registersToPreserve.set(GPRInfo::regT0);
52 if (isFTLOSRExit) {
53 // FTL can use the scratch registers for values. The code below uses
54 // the scratch registers. We need to preserve them before doing anything.
55 registersToPreserve.merge(RegisterSet::macroScratchRegisters());
56 }
57
58 size_t scratchSize = sizeof(void*) * registersToPreserve.numberOfSetGPRs();
sbarati@apple.coma582c182015-12-05 00:04:01 +000059 if (isFTLOSRExit)
60 scratchSize += sizeof(void*);
61
62 ScratchBuffer* scratchBuffer = vm->scratchBufferForSize(scratchSize);
63 char* buffer = static_cast<char*>(scratchBuffer->dataBuffer());
commit-queue@webkit.orgdad321b2016-01-28 00:29:09 +000064
65 jit.pushToSave(GPRInfo::regT1);
66 jit.move(MacroAssembler::TrustedImmPtr(buffer), GPRInfo::regT1);
67
68 unsigned storeOffset = 0;
69 registersToPreserve.forEach([&](Reg reg) {
70 jit.storePtr(reg.gpr(), MacroAssembler::Address(GPRInfo::regT1, storeOffset));
71 storeOffset += sizeof(void*);
72 });
sbarati@apple.coma582c182015-12-05 00:04:01 +000073
74 if (isFTLOSRExit) {
75 // FTL OSRExits are entered via the code FTLExitThunkGenerator emits which does
76 // pushToSaveImmediateWithoutTouchRegisters with the OSR exit index. We need to load
77 // that top value and then push it back when we reset our SP.
commit-queue@webkit.orgdad321b2016-01-28 00:29:09 +000078 jit.loadPtr(MacroAssembler::Address(MacroAssembler::stackPointerRegister, MacroAssembler::pushToSaveByteOffset()), GPRInfo::regT0);
79 jit.storePtr(GPRInfo::regT0, MacroAssembler::Address(GPRInfo::regT1, registersToPreserve.numberOfSetGPRs() * sizeof(void*)));
sbarati@apple.coma582c182015-12-05 00:04:01 +000080 }
commit-queue@webkit.orgdad321b2016-01-28 00:29:09 +000081 jit.popToRestore(GPRInfo::regT1);
sbarati@apple.coma582c182015-12-05 00:04:01 +000082
83 // We need to reset FP in the case of an exception.
84 jit.loadPtr(vm->addressOfCallFrameForCatch(), GPRInfo::regT0);
85 MacroAssembler::Jump didNotHaveException = jit.branchTestPtr(MacroAssembler::Zero, GPRInfo::regT0);
86 jit.move(GPRInfo::regT0, GPRInfo::callFrameRegister);
87 didNotHaveException.link(&jit);
88 // We need to make sure SP is correct in case of an exception.
89 jit.loadPtr(MacroAssembler::Address(GPRInfo::callFrameRegister, JSStack::CodeBlock * static_cast<int>(sizeof(Register))), GPRInfo::regT0);
90 jit.loadPtr(MacroAssembler::Address(GPRInfo::regT0, CodeBlock::jitCodeOffset()), GPRInfo::regT0);
91 jit.addPtr(MacroAssembler::TrustedImm32(JITCodeType::commonDataOffset()), GPRInfo::regT0);
92 jit.load32(MacroAssembler::Address(GPRInfo::regT0, CommonData::frameRegisterCountOffset()), GPRInfo::regT0);
93 // This does virtualRegisterForLocal(frameRegisterCount - 1)*sizeof(Register) where:
94 // virtualRegisterForLocal(frameRegisterCount - 1)
95 // = VirtualRegister::localToOperand(frameRegisterCount - 1)
96 // = -1 - (frameRegisterCount - 1)
97 // = -frameRegisterCount
98 jit.neg32(GPRInfo::regT0);
99 jit.mul32(MacroAssembler::TrustedImm32(sizeof(Register)), GPRInfo::regT0, GPRInfo::regT0);
100#if USE(JSVALUE64)
101 jit.signExtend32ToPtr(GPRInfo::regT0, GPRInfo::regT0);
102#endif
103 jit.addPtr(GPRInfo::callFrameRegister, GPRInfo::regT0);
104 jit.move(GPRInfo::regT0, MacroAssembler::stackPointerRegister);
105
106 if (isFTLOSRExit) {
commit-queue@webkit.orgdad321b2016-01-28 00:29:09 +0000107 // Leave space for saving the OSR Exit Index.
108 jit.subPtr(MacroAssembler::TrustedImm32(MacroAssembler::pushToSaveByteOffset()), MacroAssembler::stackPointerRegister);
109 }
110 jit.pushToSave(GPRInfo::regT1);
111
112 jit.move(MacroAssembler::TrustedImmPtr(buffer), GPRInfo::regT1);
113 if (isFTLOSRExit) {
sbarati@apple.coma582c182015-12-05 00:04:01 +0000114 // FTL OSRExits are entered via FTLExitThunkGenerator code with does
115 // pushToSaveImmediateWithoutTouchRegisters. We need to load that top
116 // register and then store it back when we have our SP back to a safe value.
commit-queue@webkit.orgdad321b2016-01-28 00:29:09 +0000117 jit.loadPtr(MacroAssembler::Address(GPRInfo::regT1, registersToPreserve.numberOfSetGPRs() * sizeof(void*)), GPRInfo::regT0);
118 jit.storePtr(GPRInfo::regT0, MacroAssembler::Address(MacroAssembler::stackPointerRegister, MacroAssembler::pushToSaveByteOffset()));
sbarati@apple.coma582c182015-12-05 00:04:01 +0000119 }
120
commit-queue@webkit.orgdad321b2016-01-28 00:29:09 +0000121 unsigned loadOffset = 0;
122 registersToPreserve.forEach([&](Reg reg) {
123 jit.loadPtr(MacroAssembler::Address(GPRInfo::regT1, loadOffset), reg.gpr());
124 loadOffset += sizeof(void*);
125 });
126 jit.popToRestore(GPRInfo::regT1);
sbarati@apple.coma582c182015-12-05 00:04:01 +0000127}
128
129
oliver@apple.comb9009142013-07-25 03:58:26 +0000130} } // namespace JSC::DFG
131
132#endif // ENABLE(DFG_JIT)
133
134#endif // DFGOSRExitCompilerCommon_h
135