initial import
[vuplus_webkit] / Source / JavaScriptCore / jit / SpecializedThunkJIT.h
1 /*
2  * Copyright (C) 2010 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. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef SpecializedThunkJIT_h
27 #define SpecializedThunkJIT_h
28
29 #if ENABLE(JIT)
30
31 #include "Executable.h"
32 #include "JSInterfaceJIT.h"
33 #include "LinkBuffer.h"
34
35 namespace JSC {
36
37     class SpecializedThunkJIT : public JSInterfaceJIT {
38     public:
39         static const int ThisArgument = -1;
40         SpecializedThunkJIT(int expectedArgCount, JSGlobalData* globalData)
41             : m_expectedArgCount(expectedArgCount)
42             , m_globalData(globalData)
43         {
44             // Check that we have the expected number of arguments
45             m_failures.append(branch32(NotEqual, Address(callFrameRegister, RegisterFile::ArgumentCount * (int)sizeof(Register)), TrustedImm32(expectedArgCount + 1)));
46         }
47         
48         void loadDoubleArgument(int argument, FPRegisterID dst, RegisterID scratch)
49         {
50             unsigned src = argumentToVirtualRegister(argument);
51             m_failures.append(emitLoadDouble(src, dst, scratch));
52         }
53         
54         void loadCellArgument(int argument, RegisterID dst)
55         {
56             unsigned src = argumentToVirtualRegister(argument);
57             m_failures.append(emitLoadJSCell(src, dst));
58         }
59         
60         void loadJSStringArgument(int argument, RegisterID dst)
61         {
62             loadCellArgument(argument, dst);
63             m_failures.append(branchPtr(NotEqual, Address(dst, 0), TrustedImmPtr(m_globalData->jsStringVPtr)));
64             m_failures.append(branchTest32(NonZero, Address(dst, OBJECT_OFFSETOF(JSString, m_fiberCount))));
65         }
66         
67         void loadInt32Argument(int argument, RegisterID dst, Jump& failTarget)
68         {
69             unsigned src = argumentToVirtualRegister(argument);
70             failTarget = emitLoadInt32(src, dst);
71         }
72         
73         void loadInt32Argument(int argument, RegisterID dst)
74         {
75             Jump conversionFailed;
76             loadInt32Argument(argument, dst, conversionFailed);
77             m_failures.append(conversionFailed);
78         }
79         
80         void appendFailure(const Jump& failure)
81         {
82             m_failures.append(failure);
83         }
84
85         void returnJSValue(RegisterID src)
86         {
87             if (src != regT0)
88                 move(src, regT0);
89             loadPtr(payloadFor(RegisterFile::CallerFrame, callFrameRegister), callFrameRegister);
90             ret();
91         }
92         
93         void returnDouble(FPRegisterID src)
94         {
95 #if USE(JSVALUE64)
96             moveDoubleToPtr(src, regT0);
97             Jump zero = branchTestPtr(Zero, regT0);
98             subPtr(tagTypeNumberRegister, regT0);
99             Jump done = jump();
100             zero.link(this);
101             move(tagTypeNumberRegister, regT0);
102             done.link(this);
103 #else
104             storeDouble(src, Address(stackPointerRegister, -(int)sizeof(double)));
105             loadPtr(Address(stackPointerRegister, OBJECT_OFFSETOF(JSValue, u.asBits.tag) - sizeof(double)), regT1);
106             loadPtr(Address(stackPointerRegister, OBJECT_OFFSETOF(JSValue, u.asBits.payload) - sizeof(double)), regT0);
107             Jump lowNonZero = branchTestPtr(NonZero, regT1);
108             Jump highNonZero = branchTestPtr(NonZero, regT0);
109             move(TrustedImm32(0), regT0);
110             move(TrustedImm32(Int32Tag), regT1);
111             lowNonZero.link(this);
112             highNonZero.link(this);
113 #endif
114             loadPtr(payloadFor(RegisterFile::CallerFrame, callFrameRegister), callFrameRegister);
115             ret();
116         }
117
118         void returnInt32(RegisterID src)
119         {
120             if (src != regT0)
121                 move(src, regT0);
122             tagReturnAsInt32();
123             loadPtr(payloadFor(RegisterFile::CallerFrame, callFrameRegister), callFrameRegister);
124             ret();
125         }
126
127         void returnJSCell(RegisterID src)
128         {
129             if (src != regT0)
130                 move(src, regT0);
131             tagReturnAsJSCell();
132             loadPtr(payloadFor(RegisterFile::CallerFrame, callFrameRegister), callFrameRegister);
133             ret();
134         }
135         
136         MacroAssemblerCodeRef finalize(JSGlobalData& globalData, MacroAssemblerCodePtr fallback)
137         {
138             LinkBuffer patchBuffer(globalData, this);
139             patchBuffer.link(m_failures, CodeLocationLabel(fallback));
140             for (unsigned i = 0; i < m_calls.size(); i++)
141                 patchBuffer.link(m_calls[i].first, m_calls[i].second);
142             return patchBuffer.finalizeCode();
143         }
144
145         // Assumes that the target function uses fpRegister0 as the first argument
146         // and return value. Like any sensible architecture would.
147         void callDoubleToDouble(FunctionPtr function)
148         {
149             m_calls.append(std::make_pair(call(), function));
150         }
151
152     private:
153         int argumentToVirtualRegister(unsigned argument)
154         {
155             return -static_cast<int>(RegisterFile::CallFrameHeaderSize + (m_expectedArgCount - argument));
156         }
157
158         void tagReturnAsInt32()
159         {
160 #if USE(JSVALUE64)
161             orPtr(tagTypeNumberRegister, regT0);
162 #else
163             move(TrustedImm32(JSValue::Int32Tag), regT1);
164 #endif
165         }
166
167         void tagReturnAsJSCell()
168         {
169 #if USE(JSVALUE32_64)
170             move(TrustedImm32(JSValue::CellTag), regT1);
171 #endif
172         }
173         
174         int m_expectedArgCount;
175         JSGlobalData* m_globalData;
176         MacroAssembler::JumpList m_failures;
177         Vector<std::pair<Call, FunctionPtr> > m_calls;
178     };
179
180 }
181
182 #endif // ENABLE(JIT)
183
184 #endif // SpecializedThunkJIT_h