]>
Commit | Line | Data |
---|---|---|
ba379fdc | 1 | /* |
93a37866 | 2 | * Copyright (C) 2009, 2010, 2012 Apple Inc. All rights reserved. |
ba379fdc A |
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 LinkBuffer_h | |
27 | #define LinkBuffer_h | |
28 | ||
ba379fdc A |
29 | #if ENABLE(ASSEMBLER) |
30 | ||
14957cd0 A |
31 | #define DUMP_LINK_STATISTICS 0 |
32 | #define DUMP_CODE 0 | |
33 | ||
6fe7ccc8 A |
34 | #define GLOBAL_THUNK_ID reinterpret_cast<void*>(static_cast<intptr_t>(-1)) |
35 | #define REGEXP_CODE_ID reinterpret_cast<void*>(static_cast<intptr_t>(-2)) | |
36 | ||
37 | #include "JITCompilationEffort.h" | |
38 | #include "MacroAssembler.h" | |
39 | #include <wtf/DataLog.h> | |
ba379fdc A |
40 | #include <wtf/Noncopyable.h> |
41 | ||
42 | namespace JSC { | |
43 | ||
93a37866 | 44 | class VM; |
14957cd0 | 45 | |
ba379fdc A |
46 | // LinkBuffer: |
47 | // | |
48 | // This class assists in linking code generated by the macro assembler, once code generation | |
49 | // has been completed, and the code has been copied to is final location in memory. At this | |
50 | // time pointers to labels within the code may be resolved, and relative offsets to external | |
51 | // addresses may be fixed. | |
52 | // | |
53 | // Specifically: | |
54 | // * Jump objects may be linked to external targets, | |
55 | // * The address of Jump objects may taken, such that it can later be relinked. | |
56 | // * The return address of a Call may be acquired. | |
57 | // * The address of a Label pointing into the code may be resolved. | |
58 | // * The value referenced by a DataLabel may be set. | |
59 | // | |
14957cd0 A |
60 | class LinkBuffer { |
61 | WTF_MAKE_NONCOPYABLE(LinkBuffer); | |
ba379fdc | 62 | typedef MacroAssemblerCodeRef CodeRef; |
b80e6193 | 63 | typedef MacroAssemblerCodePtr CodePtr; |
ba379fdc A |
64 | typedef MacroAssembler::Label Label; |
65 | typedef MacroAssembler::Jump Jump; | |
6fe7ccc8 | 66 | typedef MacroAssembler::PatchableJump PatchableJump; |
ba379fdc A |
67 | typedef MacroAssembler::JumpList JumpList; |
68 | typedef MacroAssembler::Call Call; | |
14957cd0 | 69 | typedef MacroAssembler::DataLabelCompact DataLabelCompact; |
ba379fdc A |
70 | typedef MacroAssembler::DataLabel32 DataLabel32; |
71 | typedef MacroAssembler::DataLabelPtr DataLabelPtr; | |
93a37866 | 72 | typedef MacroAssembler::ConvertibleLoadLabel ConvertibleLoadLabel; |
b80e6193 A |
73 | #if ENABLE(BRANCH_COMPACTION) |
74 | typedef MacroAssembler::LinkRecord LinkRecord; | |
75 | typedef MacroAssembler::JumpLinkType JumpLinkType; | |
76 | #endif | |
ba379fdc A |
77 | |
78 | public: | |
93a37866 | 79 | LinkBuffer(VM& vm, MacroAssembler* masm, void* ownerUID, JITCompilationEffort effort = JITCompilationMustSucceed) |
6fe7ccc8 A |
80 | : m_size(0) |
81 | #if ENABLE(BRANCH_COMPACTION) | |
82 | , m_initialSize(0) | |
14957cd0 | 83 | #endif |
14957cd0 A |
84 | , m_code(0) |
85 | , m_assembler(masm) | |
93a37866 | 86 | , m_vm(&vm) |
ba379fdc A |
87 | #ifndef NDEBUG |
88 | , m_completed(false) | |
6fe7ccc8 | 89 | , m_effort(effort) |
ba379fdc A |
90 | #endif |
91 | { | |
6fe7ccc8 | 92 | linkCode(ownerUID, effort); |
ba379fdc A |
93 | } |
94 | ||
95 | ~LinkBuffer() | |
96 | { | |
6fe7ccc8 A |
97 | ASSERT(m_completed || (!m_executableMemory && m_effort == JITCompilationCanFail)); |
98 | } | |
99 | ||
100 | bool didFailToAllocate() const | |
101 | { | |
102 | return !m_executableMemory; | |
ba379fdc A |
103 | } |
104 | ||
6fe7ccc8 A |
105 | bool isValid() const |
106 | { | |
107 | return !didFailToAllocate(); | |
108 | } | |
109 | ||
ba379fdc A |
110 | // These methods are used to link or set values at code generation time. |
111 | ||
112 | void link(Call call, FunctionPtr function) | |
113 | { | |
114 | ASSERT(call.isFlagSet(Call::Linkable)); | |
6fe7ccc8 | 115 | call.m_label = applyOffset(call.m_label); |
ba379fdc A |
116 | MacroAssembler::linkCall(code(), call, function); |
117 | } | |
118 | ||
119 | void link(Jump jump, CodeLocationLabel label) | |
120 | { | |
6fe7ccc8 | 121 | jump.m_label = applyOffset(jump.m_label); |
ba379fdc A |
122 | MacroAssembler::linkJump(code(), jump, label); |
123 | } | |
124 | ||
125 | void link(JumpList list, CodeLocationLabel label) | |
126 | { | |
127 | for (unsigned i = 0; i < list.m_jumps.size(); ++i) | |
b80e6193 | 128 | link(list.m_jumps[i], label); |
ba379fdc A |
129 | } |
130 | ||
131 | void patch(DataLabelPtr label, void* value) | |
132 | { | |
14957cd0 | 133 | AssemblerLabel target = applyOffset(label.m_label); |
b80e6193 | 134 | MacroAssembler::linkPointer(code(), target, value); |
ba379fdc A |
135 | } |
136 | ||
137 | void patch(DataLabelPtr label, CodeLocationLabel value) | |
138 | { | |
14957cd0 | 139 | AssemblerLabel target = applyOffset(label.m_label); |
b80e6193 | 140 | MacroAssembler::linkPointer(code(), target, value.executableAddress()); |
ba379fdc A |
141 | } |
142 | ||
143 | // These methods are used to obtain handles to allow the code to be relinked / repatched later. | |
144 | ||
145 | CodeLocationCall locationOf(Call call) | |
146 | { | |
147 | ASSERT(call.isFlagSet(Call::Linkable)); | |
148 | ASSERT(!call.isFlagSet(Call::Near)); | |
6fe7ccc8 | 149 | return CodeLocationCall(MacroAssembler::getLinkerAddress(code(), applyOffset(call.m_label))); |
ba379fdc A |
150 | } |
151 | ||
152 | CodeLocationNearCall locationOfNearCall(Call call) | |
153 | { | |
154 | ASSERT(call.isFlagSet(Call::Linkable)); | |
155 | ASSERT(call.isFlagSet(Call::Near)); | |
6fe7ccc8 A |
156 | return CodeLocationNearCall(MacroAssembler::getLinkerAddress(code(), applyOffset(call.m_label))); |
157 | } | |
158 | ||
159 | CodeLocationLabel locationOf(PatchableJump jump) | |
160 | { | |
161 | return CodeLocationLabel(MacroAssembler::getLinkerAddress(code(), applyOffset(jump.m_jump.m_label))); | |
ba379fdc A |
162 | } |
163 | ||
164 | CodeLocationLabel locationOf(Label label) | |
165 | { | |
b80e6193 | 166 | return CodeLocationLabel(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label))); |
ba379fdc A |
167 | } |
168 | ||
169 | CodeLocationDataLabelPtr locationOf(DataLabelPtr label) | |
170 | { | |
b80e6193 | 171 | return CodeLocationDataLabelPtr(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label))); |
ba379fdc A |
172 | } |
173 | ||
174 | CodeLocationDataLabel32 locationOf(DataLabel32 label) | |
175 | { | |
b80e6193 | 176 | return CodeLocationDataLabel32(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label))); |
ba379fdc | 177 | } |
14957cd0 A |
178 | |
179 | CodeLocationDataLabelCompact locationOf(DataLabelCompact label) | |
180 | { | |
181 | return CodeLocationDataLabelCompact(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label))); | |
182 | } | |
ba379fdc | 183 | |
93a37866 A |
184 | CodeLocationConvertibleLoad locationOf(ConvertibleLoadLabel label) |
185 | { | |
186 | return CodeLocationConvertibleLoad(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label))); | |
187 | } | |
188 | ||
ba379fdc A |
189 | // This method obtains the return address of the call, given as an offset from |
190 | // the start of the code. | |
191 | unsigned returnAddressOffset(Call call) | |
192 | { | |
6fe7ccc8 | 193 | call.m_label = applyOffset(call.m_label); |
ba379fdc A |
194 | return MacroAssembler::getLinkerCallReturnOffset(call); |
195 | } | |
196 | ||
6fe7ccc8 | 197 | uint32_t offsetOf(Label label) |
ba379fdc | 198 | { |
6fe7ccc8 | 199 | return applyOffset(label.m_label).m_offset; |
ba379fdc | 200 | } |
b80e6193 | 201 | |
93a37866 A |
202 | // Upon completion of all patching 'FINALIZE_CODE()' should be called once to |
203 | // complete generation of the code. Alternatively, call | |
204 | // finalizeCodeWithoutDisassembly() directly if you have your own way of | |
205 | // displaying disassembly. | |
206 | ||
207 | CodeRef finalizeCodeWithoutDisassembly(); | |
208 | CodeRef finalizeCodeWithDisassembly(const char* format, ...) WTF_ATTRIBUTE_PRINTF(2, 3); | |
ba379fdc | 209 | |
b80e6193 A |
210 | CodePtr trampolineAt(Label label) |
211 | { | |
212 | return CodePtr(MacroAssembler::AssemblerType_T::getRelocatedAddress(code(), applyOffset(label.m_label))); | |
213 | } | |
214 | ||
14957cd0 A |
215 | void* debugAddress() |
216 | { | |
217 | return m_code; | |
218 | } | |
6fe7ccc8 A |
219 | |
220 | size_t debugSize() | |
221 | { | |
222 | return m_size; | |
223 | } | |
14957cd0 | 224 | |
ba379fdc | 225 | private: |
b80e6193 A |
226 | template <typename T> T applyOffset(T src) |
227 | { | |
228 | #if ENABLE(BRANCH_COMPACTION) | |
229 | src.m_offset -= m_assembler->executableOffsetFor(src.m_offset); | |
230 | #endif | |
231 | return src; | |
232 | } | |
233 | ||
6fe7ccc8 | 234 | // Keep this private! - the underlying code should only be obtained externally via finalizeCode(). |
ba379fdc A |
235 | void* code() |
236 | { | |
237 | return m_code; | |
238 | } | |
239 | ||
93a37866 | 240 | void linkCode(void* ownerUID, JITCompilationEffort); |
6fe7ccc8 | 241 | #if ENABLE(BRANCH_COMPACTION) |
93a37866 A |
242 | template <typename InstructionType> |
243 | void copyCompactAndLinkCode(void* ownerUID, JITCompilationEffort); | |
6fe7ccc8 | 244 | #endif |
93a37866 A |
245 | |
246 | void performFinalization(); | |
ba379fdc | 247 | |
14957cd0 | 248 | #if DUMP_LINK_STATISTICS |
93a37866 | 249 | static void dumpLinkStatistics(void* code, size_t initialSize, size_t finalSize); |
14957cd0 A |
250 | #endif |
251 | ||
252 | #if DUMP_CODE | |
93a37866 | 253 | static void dumpCode(void* code, size_t); |
14957cd0 A |
254 | #endif |
255 | ||
6fe7ccc8 | 256 | RefPtr<ExecutableMemoryHandle> m_executableMemory; |
ba379fdc | 257 | size_t m_size; |
6fe7ccc8 A |
258 | #if ENABLE(BRANCH_COMPACTION) |
259 | size_t m_initialSize; | |
260 | #endif | |
b80e6193 A |
261 | void* m_code; |
262 | MacroAssembler* m_assembler; | |
93a37866 | 263 | VM* m_vm; |
ba379fdc A |
264 | #ifndef NDEBUG |
265 | bool m_completed; | |
6fe7ccc8 | 266 | JITCompilationEffort m_effort; |
ba379fdc A |
267 | #endif |
268 | }; | |
269 | ||
93a37866 A |
270 | #define FINALIZE_CODE_IF(condition, linkBufferReference, dataLogFArgumentsForHeading) \ |
271 | (UNLIKELY((condition)) \ | |
272 | ? ((linkBufferReference).finalizeCodeWithDisassembly dataLogFArgumentsForHeading) \ | |
273 | : (linkBufferReference).finalizeCodeWithoutDisassembly()) | |
274 | ||
275 | // Use this to finalize code, like so: | |
276 | // | |
277 | // CodeRef code = FINALIZE_CODE(linkBuffer, ("my super thingy number %d", number)); | |
278 | // | |
279 | // Which, in disassembly mode, will print: | |
280 | // | |
281 | // Generated JIT code for my super thingy number 42: | |
282 | // Code at [0x123456, 0x234567]: | |
283 | // 0x123456: mov $0, 0 | |
284 | // 0x12345a: ret | |
285 | // | |
286 | // ... and so on. | |
287 | // | |
288 | // Note that the dataLogFArgumentsForHeading are only evaluated when showDisassembly | |
289 | // is true, so you can hide expensive disassembly-only computations inside there. | |
290 | ||
291 | #define FINALIZE_CODE(linkBufferReference, dataLogFArgumentsForHeading) \ | |
292 | FINALIZE_CODE_IF(Options::showDisassembly(), linkBufferReference, dataLogFArgumentsForHeading) | |
293 | ||
294 | #define FINALIZE_DFG_CODE(linkBufferReference, dataLogFArgumentsForHeading) \ | |
295 | FINALIZE_CODE_IF((Options::showDisassembly() || Options::showDFGDisassembly()), linkBufferReference, dataLogFArgumentsForHeading) | |
296 | ||
ba379fdc A |
297 | } // namespace JSC |
298 | ||
299 | #endif // ENABLE(ASSEMBLER) | |
300 | ||
301 | #endif // LinkBuffer_h |