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