]> git.saurik.com Git - apple/javascriptcore.git/blob - assembler/MacroAssemblerCodeRef.h
341a7ff338e51011c0dc8afd0ae1e96ef55ffc23
[apple/javascriptcore.git] / assembler / MacroAssemblerCodeRef.h
1 /*
2 * Copyright (C) 2009 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 <wtf/Platform.h>
30
31 #include "ExecutableAllocator.h"
32 #include "PassRefPtr.h"
33 #include "RefPtr.h"
34 #include "UnusedParam.h"
35
36 #if ENABLE(ASSEMBLER)
37
38 // ASSERT_VALID_CODE_POINTER checks that ptr is a non-null pointer, and that it is a valid
39 // instruction address on the platform (for example, check any alignment requirements).
40 #if PLATFORM_ARM_ARCH(7)
41 // ARM/thumb instructions must be 16-bit aligned, but all code pointers to be loaded
42 // into the processor are decorated with the bottom bit set, indicating that this is
43 // thumb code (as oposed to 32-bit traditional ARM). The first test checks for both
44 // decorated and undectorated null, and the second test ensures that the pointer is
45 // decorated.
46 #define ASSERT_VALID_CODE_POINTER(ptr) \
47 ASSERT(reinterpret_cast<intptr_t>(ptr) & ~1); \
48 ASSERT(reinterpret_cast<intptr_t>(ptr) & 1)
49 #define ASSERT_VALID_CODE_OFFSET(offset) \
50 ASSERT(!(offset & 1)) // Must be multiple of 2.
51 #else
52 #define ASSERT_VALID_CODE_POINTER(ptr) \
53 ASSERT(ptr)
54 #define ASSERT_VALID_CODE_OFFSET(offset) // Anything goes!
55 #endif
56
57 namespace JSC {
58
59 // FunctionPtr:
60 //
61 // FunctionPtr should be used to wrap pointers to C/C++ functions in JSC
62 // (particularly, the stub functions).
63 class FunctionPtr {
64 public:
65 FunctionPtr()
66 : m_value(0)
67 {
68 }
69
70 template<typename FunctionType>
71 explicit FunctionPtr(FunctionType* value)
72 : m_value(reinterpret_cast<void*>(value))
73 {
74 ASSERT_VALID_CODE_POINTER(m_value);
75 }
76
77 void* value() const { return m_value; }
78 void* executableAddress() const { return m_value; }
79
80
81 private:
82 void* m_value;
83 };
84
85 // ReturnAddressPtr:
86 //
87 // ReturnAddressPtr should be used to wrap return addresses generated by processor
88 // 'call' instructions exectued in JIT code. We use return addresses to look up
89 // exception and optimization information, and to repatch the call instruction
90 // that is the source of the return address.
91 class ReturnAddressPtr {
92 public:
93 ReturnAddressPtr()
94 : m_value(0)
95 {
96 }
97
98 explicit ReturnAddressPtr(void* value)
99 : m_value(value)
100 {
101 ASSERT_VALID_CODE_POINTER(m_value);
102 }
103
104 explicit ReturnAddressPtr(FunctionPtr function)
105 : m_value(function.value())
106 {
107 ASSERT_VALID_CODE_POINTER(m_value);
108 }
109
110 void* value() const { return m_value; }
111
112 private:
113 void* m_value;
114 };
115
116 // MacroAssemblerCodePtr:
117 //
118 // MacroAssemblerCodePtr should be used to wrap pointers to JIT generated code.
119 class MacroAssemblerCodePtr {
120 public:
121 MacroAssemblerCodePtr()
122 : m_value(0)
123 {
124 }
125
126 explicit MacroAssemblerCodePtr(void* value)
127 #if PLATFORM_ARM_ARCH(7)
128 // Decorate the pointer as a thumb code pointer.
129 : m_value(reinterpret_cast<char*>(value) + 1)
130 #else
131 : m_value(value)
132 #endif
133 {
134 ASSERT_VALID_CODE_POINTER(m_value);
135 }
136
137 explicit MacroAssemblerCodePtr(ReturnAddressPtr ra)
138 : m_value(ra.value())
139 {
140 ASSERT_VALID_CODE_POINTER(m_value);
141 }
142
143 void* executableAddress() const { return m_value; }
144 #if PLATFORM_ARM_ARCH(7)
145 // To use this pointer as a data address remove the decoration.
146 void* dataLocation() const { ASSERT_VALID_CODE_POINTER(m_value); return reinterpret_cast<char*>(m_value) - 1; }
147 #else
148 void* dataLocation() const { ASSERT_VALID_CODE_POINTER(m_value); return m_value; }
149 #endif
150
151 bool operator!()
152 {
153 return !m_value;
154 }
155
156 private:
157 void* m_value;
158 };
159
160 // MacroAssemblerCodeRef:
161 //
162 // A reference to a section of JIT generated code. A CodeRef consists of a
163 // pointer to the code, and a ref pointer to the pool from within which it
164 // was allocated.
165 class MacroAssemblerCodeRef {
166 public:
167 MacroAssemblerCodeRef()
168 : m_size(0)
169 {
170 }
171
172 MacroAssemblerCodeRef(void* code, PassRefPtr<ExecutablePool> executablePool, size_t size)
173 : m_code(code)
174 , m_executablePool(executablePool)
175 , m_size(size)
176 {
177 }
178
179 MacroAssemblerCodePtr m_code;
180 RefPtr<ExecutablePool> m_executablePool;
181 size_t m_size;
182 };
183
184 } // namespace JSC
185
186 #endif // ENABLE(ASSEMBLER)
187
188 #endif // MacroAssemblerCodeRef_h