Rename Operations.h to JSCInlines.h
[WebKit-https.git] / Source / JavaScriptCore / jit / RegisterPreservationWrapperGenerator.cpp
1 /*
2  * Copyright (C) 2013 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 #include "config.h"
27 #include "RegisterPreservationWrapperGenerator.h"
28
29 #if ENABLE(JIT)
30
31 #include "AssemblyHelpers.h"
32 #include "LinkBuffer.h"
33 #include "JSCInlines.h"
34 #include "StackAlignment.h"
35
36 namespace JSC {
37
38 RegisterSet registersToPreserve()
39 {
40     RegisterSet calleeSaves = RegisterSet::calleeSaveRegisters();
41     
42     // No need to preserve FP since that always gets preserved anyway.
43     calleeSaves.clear(GPRInfo::callFrameRegister);
44     
45     return calleeSaves;
46 }
47
48 ptrdiff_t registerPreservationOffset()
49 {
50     unsigned numberOfCalleeSaves = registersToPreserve().numberOfSetRegisters();
51     
52     // Need to preserve the old return PC.
53     unsigned numberOfValuesToSave = numberOfCalleeSaves + 1;
54     
55     // Alignment. Preserve the same alignment invariants that the caller imposed.
56     unsigned numberOfNewStackSlots =
57         WTF::roundUpToMultipleOf(stackAlignmentRegisters(), numberOfValuesToSave);
58     
59     return sizeof(Register) * numberOfNewStackSlots;
60 }
61
62 MacroAssemblerCodeRef generateRegisterPreservationWrapper(VM& vm, ExecutableBase* executable, MacroAssemblerCodePtr target)
63 {
64 #if ENABLE(FTL_JIT)
65     // We shouldn't ever be generating wrappers for native functions.
66     RegisterSet toSave = registersToPreserve();
67     ptrdiff_t offset = registerPreservationOffset();
68     
69     AssemblyHelpers jit(&vm, 0);
70     
71     jit.preserveReturnAddressAfterCall(GPRInfo::regT1);
72     jit.load32(
73         AssemblyHelpers::Address(
74             AssemblyHelpers::stackPointerRegister,
75             (JSStack::ArgumentCount - JSStack::CallerFrameAndPCSize) * sizeof(Register) + PayloadOffset),
76         GPRInfo::regT2);
77     
78     // Place the stack pointer where we want it to be.
79     jit.subPtr(AssemblyHelpers::TrustedImm32(offset), AssemblyHelpers::stackPointerRegister);
80     
81     // Compute the number of things we will be copying.
82     jit.add32(
83         AssemblyHelpers::TrustedImm32(
84             JSStack::CallFrameHeaderSize - JSStack::CallerFrameAndPCSize),
85         GPRInfo::regT2);
86
87     ASSERT(!toSave.get(GPRInfo::regT4));
88     jit.move(AssemblyHelpers::stackPointerRegister, GPRInfo::regT4);
89     
90     AssemblyHelpers::Label loop = jit.label();
91     jit.sub32(AssemblyHelpers::TrustedImm32(1), GPRInfo::regT2);
92     jit.load64(AssemblyHelpers::Address(GPRInfo::regT4, offset), GPRInfo::regT0);
93     jit.store64(GPRInfo::regT0, GPRInfo::regT4);
94     jit.addPtr(AssemblyHelpers::TrustedImm32(sizeof(Register)), GPRInfo::regT4);
95     jit.branchTest32(AssemblyHelpers::NonZero, GPRInfo::regT2).linkTo(loop, &jit);
96
97     // At this point regT4 + offset points to where we save things.
98     ptrdiff_t currentOffset = 0;
99     jit.storePtr(GPRInfo::regT1, AssemblyHelpers::Address(GPRInfo::regT4, currentOffset));
100     
101     for (GPRReg gpr = AssemblyHelpers::firstRegister(); gpr <= AssemblyHelpers::lastRegister(); gpr = static_cast<GPRReg>(gpr + 1)) {
102         if (!toSave.get(gpr))
103             continue;
104         currentOffset += sizeof(Register);
105         jit.store64(gpr, AssemblyHelpers::Address(GPRInfo::regT4, currentOffset));
106     }
107     
108     // Assume that there aren't any saved FP registers.
109     
110     // Restore the tag registers.
111     jit.move(AssemblyHelpers::TrustedImm64(TagTypeNumber), GPRInfo::tagTypeNumberRegister);
112     jit.add64(AssemblyHelpers::TrustedImm32(TagMask - TagTypeNumber), GPRInfo::tagTypeNumberRegister, GPRInfo::tagMaskRegister);
113     
114     jit.move(
115         AssemblyHelpers::TrustedImmPtr(
116             vm.getCTIStub(registerRestorationThunkGenerator).code().executableAddress()),
117         GPRInfo::nonArgGPR0);
118     jit.restoreReturnAddressBeforeReturn(GPRInfo::nonArgGPR0);
119     AssemblyHelpers::Jump jump = jit.jump();
120     
121     LinkBuffer linkBuffer(vm, &jit, GLOBAL_THUNK_ID);
122     linkBuffer.link(jump, CodeLocationLabel(target));
123
124     if (Options::verboseFTLToJSThunk())
125         dataLog("Need a thunk for calls from FTL to non-FTL version of ", *executable, "\n");
126     
127     return FINALIZE_DFG_CODE(linkBuffer, ("Register preservation wrapper for %s/%s, %p", toCString(executable->hashFor(CodeForCall)).data(), toCString(executable->hashFor(CodeForConstruct)).data(), target.executableAddress()));
128 #else // ENABLE(FTL_JIT)
129     UNUSED_PARAM(vm);
130     UNUSED_PARAM(executable);
131     UNUSED_PARAM(target);
132     // We don't support non-FTL builds for two reasons:
133     // - It just so happens that currently only the FTL bottoms out in this code.
134     // - The code above uses 64-bit instructions. It doesn't necessarily have to; it would be
135     //   easy to change it so that it doesn't. But obviously making that change would be a
136     //   prerequisite to removing this #if.
137     UNREACHABLE_FOR_PLATFORM();
138     return MacroAssemblerCodeRef();
139 #endif // ENABLE(FTL_JIT)
140 }
141
142 static void generateRegisterRestoration(AssemblyHelpers& jit)
143 {
144 #if ENABLE(FTL_JIT)
145     RegisterSet toSave = registersToPreserve();
146     ptrdiff_t offset = registerPreservationOffset();
147     
148     ASSERT(!toSave.get(GPRInfo::regT4));
149
150     // We need to place the stack pointer back to where the caller thought they left it.
151     // But also, in order to recover the registers, we need to figure out how big the
152     // arguments area is.
153     
154     jit.load32(
155         AssemblyHelpers::Address(
156             AssemblyHelpers::stackPointerRegister,
157             (JSStack::ArgumentCount - JSStack::CallerFrameAndPCSize) * sizeof(Register) + PayloadOffset),
158         GPRInfo::regT4);
159     
160     jit.move(GPRInfo::regT4, GPRInfo::regT2);
161     jit.lshift32(AssemblyHelpers::TrustedImm32(3), GPRInfo::regT2);
162     
163     jit.addPtr(AssemblyHelpers::TrustedImm32(offset), AssemblyHelpers::stackPointerRegister);
164     jit.addPtr(AssemblyHelpers::stackPointerRegister, GPRInfo::regT2);
165     
166     // We saved things at:
167     //
168     //     adjSP + (JSStack::CallFrameHeaderSize - JSStack::CallerFrameAndPCSize + NumArgs) * 8
169     //
170     // Where:
171     //
172     //     adjSP = origSP - offset
173     //
174     // regT2 now points at:
175     //
176     //     origSP + NumArgs * 8
177     //   = adjSP + offset + NumArgs * 8
178     // 
179     // So if we subtract offset and then add JSStack::CallFrameHeaderSize and subtract
180     // JSStack::CallerFrameAndPCSize, we'll get the thing we want.
181     ptrdiff_t currentOffset = -offset + sizeof(Register) * (
182         JSStack::CallFrameHeaderSize - JSStack::CallerFrameAndPCSize);
183     jit.loadPtr(AssemblyHelpers::Address(GPRInfo::regT2, currentOffset), GPRInfo::regT1);
184     
185     for (GPRReg gpr = AssemblyHelpers::firstRegister(); gpr <= AssemblyHelpers::lastRegister(); gpr = static_cast<GPRReg>(gpr + 1)) {
186         if (!toSave.get(gpr))
187             continue;
188         currentOffset += sizeof(Register);
189         jit.load64(AssemblyHelpers::Address(GPRInfo::regT2, currentOffset), gpr);
190     }
191     
192     // Thunks like this rely on the ArgumentCount being intact. Pay it forward.
193     jit.store32(
194         GPRInfo::regT4,
195         AssemblyHelpers::Address(
196             AssemblyHelpers::stackPointerRegister,
197             (JSStack::ArgumentCount - JSStack::CallerFrameAndPCSize) * sizeof(Register) + PayloadOffset));
198     
199     if (!ASSERT_DISABLED) {
200         AssemblyHelpers::Jump ok = jit.branchPtr(
201             AssemblyHelpers::Above, GPRInfo::regT1, AssemblyHelpers::TrustedImmPtr(static_cast<size_t>(0x1000)));
202         jit.breakpoint();
203         ok.link(&jit);
204     }
205     
206     jit.jump(GPRInfo::regT1);
207 #else // ENABLE(FTL_JIT)
208     UNUSED_PARAM(jit);
209     UNREACHABLE_FOR_PLATFORM();
210 #endif // ENABLE(FTL_JIT)
211 }
212
213 MacroAssemblerCodeRef registerRestorationThunkGenerator(VM* vm)
214 {
215     AssemblyHelpers jit(vm, 0);
216     generateRegisterRestoration(jit);
217     LinkBuffer linkBuffer(*vm, &jit, GLOBAL_THUNK_ID);
218     return FINALIZE_CODE(linkBuffer, ("Register restoration thunk"));
219 }
220
221 } // namespace JSC
222
223 #endif // ENABLE(JIT)
224