1/*
2 * Copyright (C) 2013-2019 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#pragma once
27
28#if ENABLE(DFG_JIT)
29
30#include "CCallHelpers.h"
31#include "DFGOSRExit.h"
32#include "DFGCommonData.h"
33#include "DFGJITCode.h"
34#include "FTLJITCode.h"
35#include "RegisterSet.h"
36
37namespace JSC { namespace DFG {
38
39void handleExitCounts(VM&, CCallHelpers&, const OSRExitBase&);
40void reifyInlinedCallFrames(CCallHelpers&, const OSRExitBase&);
41void adjustAndJumpToTarget(VM&, CCallHelpers&, const OSRExitBase&);
42void* callerReturnPC(CodeBlock* baselineCodeBlockForCaller, BytecodeIndex callBytecodeIndex, InlineCallFrame::Kind callerKind, bool& callerIsLLInt);
43CCallHelpers::Address calleeSaveSlot(InlineCallFrame*, CodeBlock* baselineCodeBlock, GPRReg calleeSave);
44
45template <typename JITCodeType>
46void adjustFrameAndStackInOSRExitCompilerThunk(MacroAssembler& jit, VM& vm, JITType jitType)
47{
48 ASSERT(jitType == JITType::DFGJIT || jitType == JITType::FTLJIT);
49
50 bool isFTLOSRExit = jitType == JITType::FTLJIT;
51 RegisterSet registersToPreserve;
52 registersToPreserve.set(GPRInfo::regT0);
53 if (isFTLOSRExit) {
54 // FTL can use the scratch registers for values. The code below uses
55 // the scratch registers. We need to preserve them before doing anything.
56 registersToPreserve.merge(RegisterSet::macroScratchRegisters());
57 }
58
59 size_t scratchSize = sizeof(void*) * registersToPreserve.numberOfSetGPRs();
60 if (isFTLOSRExit)
61 scratchSize += sizeof(void*);
62
63 ScratchBuffer* scratchBuffer = vm.scratchBufferForSize(scratchSize);
64 char* buffer = static_cast<char*>(scratchBuffer->dataBuffer());
65
66 jit.pushToSave(GPRInfo::regT1);
67 jit.move(MacroAssembler::TrustedImmPtr(buffer), GPRInfo::regT1);
68
69 unsigned storeOffset = 0;
70 registersToPreserve.forEach([&](Reg reg) {
71 jit.storePtr(reg.gpr(), MacroAssembler::Address(GPRInfo::regT1, storeOffset));
72 storeOffset += sizeof(void*);
73 });
74
75 if (isFTLOSRExit) {
76 // FTL OSRExits are entered via the code FTLExitThunkGenerator emits which does
77 // pushToSaveImmediateWithoutTouchRegisters with the OSR exit index. We need to load
78 // that top value and then push it back when we reset our SP.
79 jit.loadPtr(MacroAssembler::Address(MacroAssembler::stackPointerRegister, MacroAssembler::pushToSaveByteOffset()), GPRInfo::regT0);
80 jit.storePtr(GPRInfo::regT0, MacroAssembler::Address(GPRInfo::regT1, registersToPreserve.numberOfSetGPRs() * sizeof(void*)));
81 }
82 jit.popToRestore(GPRInfo::regT1);
83
84 // We need to reset FP in the case of an exception.
85 jit.loadPtr(vm.addressOfCallFrameForCatch(), GPRInfo::regT0);
86 MacroAssembler::Jump didNotHaveException = jit.branchTestPtr(MacroAssembler::Zero, GPRInfo::regT0);
87 jit.move(GPRInfo::regT0, GPRInfo::callFrameRegister);
88 didNotHaveException.link(&jit);
89 // We need to make sure SP is correct in case of an exception.
90 jit.loadPtr(MacroAssembler::Address(GPRInfo::callFrameRegister, CallFrameSlot::codeBlock * static_cast<int>(sizeof(Register))), GPRInfo::regT0);
91 jit.loadPtr(MacroAssembler::Address(GPRInfo::regT0, CodeBlock::jitCodeOffset()), GPRInfo::regT0);
92 jit.addPtr(MacroAssembler::TrustedImm32(JITCodeType::commonDataOffset()), GPRInfo::regT0);
93 jit.load32(MacroAssembler::Address(GPRInfo::regT0, CommonData::frameRegisterCountOffset()), GPRInfo::regT0);
94 // This does virtualRegisterForLocal(frameRegisterCount - 1)*sizeof(Register) where:
95 // virtualRegisterForLocal(frameRegisterCount - 1)
96 // = VirtualRegister::localToOperand(frameRegisterCount - 1)
97 // = -1 - (frameRegisterCount - 1)
98 // = -frameRegisterCount
99 jit.neg32(GPRInfo::regT0);
100 jit.mul32(MacroAssembler::TrustedImm32(sizeof(Register)), GPRInfo::regT0, GPRInfo::regT0);
101#if USE(JSVALUE64)
102 jit.signExtend32ToPtr(GPRInfo::regT0, GPRInfo::regT0);
103#endif
104 jit.addPtr(GPRInfo::callFrameRegister, GPRInfo::regT0);
105 jit.move(GPRInfo::regT0, MacroAssembler::stackPointerRegister);
106
107 if (isFTLOSRExit) {
108 // Leave space for saving the OSR Exit Index.
109 jit.subPtr(MacroAssembler::TrustedImm32(MacroAssembler::pushToSaveByteOffset()), MacroAssembler::stackPointerRegister);
110 }
111 jit.pushToSave(GPRInfo::regT1);
112
113 jit.move(MacroAssembler::TrustedImmPtr(buffer), GPRInfo::regT1);
114 if (isFTLOSRExit) {
115 // FTL OSRExits are entered via FTLExitThunkGenerator code with does
116 // pushToSaveImmediateWithoutTouchRegisters. We need to load that top
117 // register and then store it back when we have our SP back to a safe value.
118 jit.loadPtr(MacroAssembler::Address(GPRInfo::regT1, registersToPreserve.numberOfSetGPRs() * sizeof(void*)), GPRInfo::regT0);
119 jit.storePtr(GPRInfo::regT0, MacroAssembler::Address(MacroAssembler::stackPointerRegister, MacroAssembler::pushToSaveByteOffset()));
120 }
121
122 unsigned loadOffset = 0;
123 registersToPreserve.forEach([&](Reg reg) {
124 jit.loadPtr(MacroAssembler::Address(GPRInfo::regT1, loadOffset), reg.gpr());
125 loadOffset += sizeof(void*);
126 });
127 jit.popToRestore(GPRInfo::regT1);
128}
129
130
131} } // namespace JSC::DFG
132
133#endif // ENABLE(DFG_JIT)
134