Merge "Save and re-bind previously bounded texture when using cairo_gl_surface_set_bi...
[framework/web/webkit-efl.git] / Source / JavaScriptCore / assembler / MacroAssemblerCodeRef.h
1 /*
2  * Copyright (C) 2009, 2012 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 #ifndef MacroAssemblerCodeRef_h
27 #define MacroAssemblerCodeRef_h
28
29 #include "Disassembler.h"
30 #include "ExecutableAllocator.h"
31 #include "LLIntData.h"
32 #include <wtf/DataLog.h>
33 #include <wtf/PassRefPtr.h>
34 #include <wtf/RefPtr.h>
35 #include <wtf/UnusedParam.h>
36
37 // ASSERT_VALID_CODE_POINTER checks that ptr is a non-null pointer, and that it is a valid
38 // instruction address on the platform (for example, check any alignment requirements).
39 #if CPU(ARM_THUMB2)
40 // ARM/thumb instructions must be 16-bit aligned, but all code pointers to be loaded
41 // into the processor are decorated with the bottom bit set, indicating that this is
42 // thumb code (as oposed to 32-bit traditional ARM).  The first test checks for both
43 // decorated and undectorated null, and the second test ensures that the pointer is
44 // decorated.
45 #define ASSERT_VALID_CODE_POINTER(ptr) \
46     ASSERT(reinterpret_cast<intptr_t>(ptr) & ~1); \
47     ASSERT(reinterpret_cast<intptr_t>(ptr) & 1)
48 #define ASSERT_VALID_CODE_OFFSET(offset) \
49     ASSERT(!(offset & 1)) // Must be multiple of 2.
50 #else
51 #define ASSERT_VALID_CODE_POINTER(ptr) \
52     ASSERT(ptr)
53 #define ASSERT_VALID_CODE_OFFSET(offset) // Anything goes!
54 #endif
55
56 #if CPU(X86) && OS(WINDOWS)
57 #define CALLING_CONVENTION_IS_STDCALL 1
58 #ifndef CDECL
59 #if COMPILER(MSVC)
60 #define CDECL __cdecl
61 #else
62 #define CDECL __attribute__ ((__cdecl))
63 #endif // COMPILER(MSVC)
64 #endif // CDECL
65 #else
66 #define CALLING_CONVENTION_IS_STDCALL 0
67 #endif
68
69 #if CPU(X86)
70 #define HAS_FASTCALL_CALLING_CONVENTION 1
71 #ifndef FASTCALL
72 #if COMPILER(MSVC)
73 #define FASTCALL __fastcall
74 #else
75 #define FASTCALL  __attribute__ ((fastcall))
76 #endif // COMPILER(MSVC)
77 #endif // FASTCALL
78 #else
79 #define HAS_FASTCALL_CALLING_CONVENTION 0
80 #endif // CPU(X86)
81
82 namespace JSC {
83
84 // FunctionPtr:
85 //
86 // FunctionPtr should be used to wrap pointers to C/C++ functions in JSC
87 // (particularly, the stub functions).
88 class FunctionPtr {
89 public:
90     FunctionPtr()
91         : m_value(0)
92     {
93     }
94
95     template<typename returnType>
96     FunctionPtr(returnType(*value)())
97         : m_value((void*)value)
98     {
99         ASSERT_VALID_CODE_POINTER(m_value);
100     }
101
102     template<typename returnType, typename argType1>
103     FunctionPtr(returnType(*value)(argType1))
104         : m_value((void*)value)
105     {
106         ASSERT_VALID_CODE_POINTER(m_value);
107     }
108
109     template<typename returnType, typename argType1, typename argType2>
110     FunctionPtr(returnType(*value)(argType1, argType2))
111         : m_value((void*)value)
112     {
113         ASSERT_VALID_CODE_POINTER(m_value);
114     }
115
116     template<typename returnType, typename argType1, typename argType2, typename argType3>
117     FunctionPtr(returnType(*value)(argType1, argType2, argType3))
118         : m_value((void*)value)
119     {
120         ASSERT_VALID_CODE_POINTER(m_value);
121     }
122
123     template<typename returnType, typename argType1, typename argType2, typename argType3, typename argType4>
124     FunctionPtr(returnType(*value)(argType1, argType2, argType3, argType4))
125         : m_value((void*)value)
126     {
127         ASSERT_VALID_CODE_POINTER(m_value);
128     }
129
130     template<typename returnType, typename argType1, typename argType2, typename argType3, typename argType4, typename argType5>
131     FunctionPtr(returnType(*value)(argType1, argType2, argType3, argType4, argType5))
132         : m_value((void*)value)
133     {
134         ASSERT_VALID_CODE_POINTER(m_value);
135     }
136
137 // MSVC doesn't seem to treat functions with different calling conventions as
138 // different types; these methods already defined for fastcall, below.
139 #if CALLING_CONVENTION_IS_STDCALL && !OS(WINDOWS)
140
141     template<typename returnType>
142     FunctionPtr(returnType (CDECL *value)())
143         : m_value((void*)value)
144     {
145         ASSERT_VALID_CODE_POINTER(m_value);
146     }
147
148     template<typename returnType, typename argType1>
149     FunctionPtr(returnType (CDECL *value)(argType1))
150         : m_value((void*)value)
151     {
152         ASSERT_VALID_CODE_POINTER(m_value);
153     }
154
155     template<typename returnType, typename argType1, typename argType2>
156     FunctionPtr(returnType (CDECL *value)(argType1, argType2))
157         : m_value((void*)value)
158     {
159         ASSERT_VALID_CODE_POINTER(m_value);
160     }
161
162     template<typename returnType, typename argType1, typename argType2, typename argType3>
163     FunctionPtr(returnType (CDECL *value)(argType1, argType2, argType3))
164         : m_value((void*)value)
165     {
166         ASSERT_VALID_CODE_POINTER(m_value);
167     }
168
169     template<typename returnType, typename argType1, typename argType2, typename argType3, typename argType4>
170     FunctionPtr(returnType (CDECL *value)(argType1, argType2, argType3, argType4))
171         : m_value((void*)value)
172     {
173         ASSERT_VALID_CODE_POINTER(m_value);
174     }
175 #endif
176
177 #if HAS_FASTCALL_CALLING_CONVENTION
178
179     template<typename returnType>
180     FunctionPtr(returnType (FASTCALL *value)())
181         : m_value((void*)value)
182     {
183         ASSERT_VALID_CODE_POINTER(m_value);
184     }
185
186     template<typename returnType, typename argType1>
187     FunctionPtr(returnType (FASTCALL *value)(argType1))
188         : m_value((void*)value)
189     {
190         ASSERT_VALID_CODE_POINTER(m_value);
191     }
192
193     template<typename returnType, typename argType1, typename argType2>
194     FunctionPtr(returnType (FASTCALL *value)(argType1, argType2))
195         : m_value((void*)value)
196     {
197         ASSERT_VALID_CODE_POINTER(m_value);
198     }
199
200     template<typename returnType, typename argType1, typename argType2, typename argType3>
201     FunctionPtr(returnType (FASTCALL *value)(argType1, argType2, argType3))
202         : m_value((void*)value)
203     {
204         ASSERT_VALID_CODE_POINTER(m_value);
205     }
206
207     template<typename returnType, typename argType1, typename argType2, typename argType3, typename argType4>
208     FunctionPtr(returnType (FASTCALL *value)(argType1, argType2, argType3, argType4))
209         : m_value((void*)value)
210     {
211         ASSERT_VALID_CODE_POINTER(m_value);
212     }
213 #endif
214
215     template<typename FunctionType>
216     explicit FunctionPtr(FunctionType* value)
217         // Using a C-ctyle cast here to avoid compiler error on RVTC:
218         // Error:  #694: reinterpret_cast cannot cast away const or other type qualifiers
219         // (I guess on RVTC function pointers have a different constness to GCC/MSVC?)
220         : m_value((void*)value)
221     {
222         ASSERT_VALID_CODE_POINTER(m_value);
223     }
224
225     void* value() const { return m_value; }
226     void* executableAddress() const { return m_value; }
227
228
229 private:
230     void* m_value;
231 };
232
233 // ReturnAddressPtr:
234 //
235 // ReturnAddressPtr should be used to wrap return addresses generated by processor
236 // 'call' instructions exectued in JIT code.  We use return addresses to look up
237 // exception and optimization information, and to repatch the call instruction
238 // that is the source of the return address.
239 class ReturnAddressPtr {
240 public:
241     ReturnAddressPtr()
242         : m_value(0)
243     {
244     }
245
246     explicit ReturnAddressPtr(void* value)
247         : m_value(value)
248     {
249         ASSERT_VALID_CODE_POINTER(m_value);
250     }
251
252     explicit ReturnAddressPtr(FunctionPtr function)
253         : m_value(function.value())
254     {
255         ASSERT_VALID_CODE_POINTER(m_value);
256     }
257
258     void* value() const { return m_value; }
259
260 private:
261     void* m_value;
262 };
263
264 // MacroAssemblerCodePtr:
265 //
266 // MacroAssemblerCodePtr should be used to wrap pointers to JIT generated code.
267 class MacroAssemblerCodePtr {
268 public:
269     MacroAssemblerCodePtr()
270         : m_value(0)
271     {
272     }
273
274     explicit MacroAssemblerCodePtr(void* value)
275 #if CPU(ARM_THUMB2)
276         // Decorate the pointer as a thumb code pointer.
277         : m_value(reinterpret_cast<char*>(value) + 1)
278 #else
279         : m_value(value)
280 #endif
281     {
282         ASSERT_VALID_CODE_POINTER(m_value);
283     }
284     
285     static MacroAssemblerCodePtr createFromExecutableAddress(void* value)
286     {
287         ASSERT_VALID_CODE_POINTER(value);
288         MacroAssemblerCodePtr result;
289         result.m_value = value;
290         return result;
291     }
292
293 #if ENABLE(LLINT)
294     static MacroAssemblerCodePtr createLLIntCodePtr(LLIntCode codeId)
295     {
296         return createFromExecutableAddress(LLInt::getCodePtr(codeId));
297     }
298 #endif
299
300     explicit MacroAssemblerCodePtr(ReturnAddressPtr ra)
301         : m_value(ra.value())
302     {
303         ASSERT_VALID_CODE_POINTER(m_value);
304     }
305
306     void* executableAddress() const { return m_value; }
307 #if CPU(ARM_THUMB2)
308     // To use this pointer as a data address remove the decoration.
309     void* dataLocation() const { ASSERT_VALID_CODE_POINTER(m_value); return reinterpret_cast<char*>(m_value) - 1; }
310 #else
311     void* dataLocation() const { ASSERT_VALID_CODE_POINTER(m_value); return m_value; }
312 #endif
313
314     bool operator!() const
315     {
316         return !m_value;
317     }
318
319 private:
320     void* m_value;
321 };
322
323 // MacroAssemblerCodeRef:
324 //
325 // A reference to a section of JIT generated code.  A CodeRef consists of a
326 // pointer to the code, and a ref pointer to the pool from within which it
327 // was allocated.
328 class MacroAssemblerCodeRef {
329 private:
330     // This is private because it's dangerous enough that we want uses of it
331     // to be easy to find - hence the static create method below.
332     explicit MacroAssemblerCodeRef(MacroAssemblerCodePtr codePtr)
333         : m_codePtr(codePtr)
334     {
335         ASSERT(m_codePtr);
336     }
337
338 public:
339     MacroAssemblerCodeRef()
340     {
341     }
342
343     MacroAssemblerCodeRef(PassRefPtr<ExecutableMemoryHandle> executableMemory)
344         : m_codePtr(executableMemory->start())
345         , m_executableMemory(executableMemory)
346     {
347         ASSERT(m_executableMemory->isManaged());
348         ASSERT(m_executableMemory->start());
349         ASSERT(m_codePtr);
350     }
351     
352     // Use this only when you know that the codePtr refers to code that is
353     // already being kept alive through some other means. Typically this means
354     // that codePtr is immortal.
355     static MacroAssemblerCodeRef createSelfManagedCodeRef(MacroAssemblerCodePtr codePtr)
356     {
357         return MacroAssemblerCodeRef(codePtr);
358     }
359     
360 #if ENABLE(LLINT)
361     // Helper for creating self-managed code refs from LLInt.
362     static MacroAssemblerCodeRef createLLIntCodeRef(LLIntCode codeId)
363     {
364         return createSelfManagedCodeRef(MacroAssemblerCodePtr::createFromExecutableAddress(LLInt::getCodePtr(codeId)));
365     }
366 #endif
367
368     ExecutableMemoryHandle* executableMemory() const
369     {
370         return m_executableMemory.get();
371     }
372     
373     MacroAssemblerCodePtr code() const
374     {
375         return m_codePtr;
376     }
377     
378     size_t size() const
379     {
380         if (!m_executableMemory)
381             return 0;
382         return m_executableMemory->sizeInBytes();
383     }
384     
385     bool tryToDisassemble(const char* prefix) const
386     {
387         return JSC::tryToDisassemble(m_codePtr, size(), prefix, WTF::dataFile());
388     }
389     
390     bool operator!() const { return !m_codePtr; }
391
392 private:
393     MacroAssemblerCodePtr m_codePtr;
394     RefPtr<ExecutableMemoryHandle> m_executableMemory;
395 };
396
397 } // namespace JSC
398
399 #endif // MacroAssemblerCodeRef_h