2 * Copyright (C) 2009, 2010, 2012 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
31 #define DUMP_LINK_STATISTICS 0
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))
37 #include "JITCompilationEffort.h"
38 #include "MacroAssembler.h"
39 #include <wtf/DataLog.h>
40 #include <wtf/Noncopyable.h>
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.
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.
61 WTF_MAKE_NONCOPYABLE(LinkBuffer);
62 typedef MacroAssemblerCodeRef CodeRef;
63 typedef MacroAssemblerCodePtr CodePtr;
64 typedef MacroAssembler::Label Label;
65 typedef MacroAssembler::Jump Jump;
66 typedef MacroAssembler::PatchableJump PatchableJump;
67 typedef MacroAssembler::JumpList JumpList;
68 typedef MacroAssembler::Call Call;
69 typedef MacroAssembler::DataLabelCompact DataLabelCompact;
70 typedef MacroAssembler::DataLabel32 DataLabel32;
71 typedef MacroAssembler::DataLabelPtr DataLabelPtr;
72 typedef MacroAssembler::ConvertibleLoadLabel ConvertibleLoadLabel;
73 #if ENABLE(BRANCH_COMPACTION)
74 typedef MacroAssembler::LinkRecord LinkRecord;
75 typedef MacroAssembler::JumpLinkType JumpLinkType;
79 LinkBuffer(JSGlobalData& globalData, MacroAssembler* masm, void* ownerUID, JITCompilationEffort effort = JITCompilationMustSucceed)
81 #if ENABLE(BRANCH_COMPACTION)
86 , m_globalData(&globalData)
92 linkCode(ownerUID, effort);
97 ASSERT(m_completed || (!m_executableMemory && m_effort == JITCompilationCanFail));
100 bool didFailToAllocate() const
102 return !m_executableMemory;
107 return !didFailToAllocate();
110 // These methods are used to link or set values at code generation time.
112 void link(Call call, FunctionPtr function)
114 ASSERT(call.isFlagSet(Call::Linkable));
115 call.m_label = applyOffset(call.m_label);
116 MacroAssembler::linkCall(code(), call, function);
119 void link(Jump jump, CodeLocationLabel label)
121 jump.m_label = applyOffset(jump.m_label);
122 MacroAssembler::linkJump(code(), jump, label);
125 void link(JumpList list, CodeLocationLabel label)
127 for (unsigned i = 0; i < list.m_jumps.size(); ++i)
128 link(list.m_jumps[i], label);
131 void patch(DataLabelPtr label, void* value)
133 AssemblerLabel target = applyOffset(label.m_label);
134 MacroAssembler::linkPointer(code(), target, value);
137 void patch(DataLabelPtr label, CodeLocationLabel value)
139 AssemblerLabel target = applyOffset(label.m_label);
140 MacroAssembler::linkPointer(code(), target, value.executableAddress());
143 // These methods are used to obtain handles to allow the code to be relinked / repatched later.
145 CodeLocationCall locationOf(Call call)
147 ASSERT(call.isFlagSet(Call::Linkable));
148 ASSERT(!call.isFlagSet(Call::Near));
149 return CodeLocationCall(MacroAssembler::getLinkerAddress(code(), applyOffset(call.m_label)));
152 CodeLocationNearCall locationOfNearCall(Call call)
154 ASSERT(call.isFlagSet(Call::Linkable));
155 ASSERT(call.isFlagSet(Call::Near));
156 return CodeLocationNearCall(MacroAssembler::getLinkerAddress(code(), applyOffset(call.m_label)));
159 CodeLocationLabel locationOf(PatchableJump jump)
161 return CodeLocationLabel(MacroAssembler::getLinkerAddress(code(), applyOffset(jump.m_jump.m_label)));
164 CodeLocationLabel locationOf(Label label)
166 return CodeLocationLabel(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label)));
169 CodeLocationDataLabelPtr locationOf(DataLabelPtr label)
171 return CodeLocationDataLabelPtr(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label)));
174 CodeLocationDataLabel32 locationOf(DataLabel32 label)
176 return CodeLocationDataLabel32(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label)));
179 CodeLocationDataLabelCompact locationOf(DataLabelCompact label)
181 return CodeLocationDataLabelCompact(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label)));
184 CodeLocationConvertibleLoad locationOf(ConvertibleLoadLabel label)
186 return CodeLocationConvertibleLoad(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label)));
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)
193 call.m_label = applyOffset(call.m_label);
194 return MacroAssembler::getLinkerCallReturnOffset(call);
197 uint32_t offsetOf(Label label)
199 return applyOffset(label.m_label).m_offset;
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.
207 CodeRef finalizeCodeWithoutDisassembly();
208 CodeRef finalizeCodeWithDisassembly(const char* format, ...) WTF_ATTRIBUTE_PRINTF(2, 3);
210 CodePtr trampolineAt(Label label)
212 return CodePtr(MacroAssembler::AssemblerType_T::getRelocatedAddress(code(), applyOffset(label.m_label)));
226 template <typename T> T applyOffset(T src)
228 #if ENABLE(BRANCH_COMPACTION)
229 src.m_offset -= m_assembler->executableOffsetFor(src.m_offset);
234 // Keep this private! - the underlying code should only be obtained externally via finalizeCode().
240 void linkCode(void* ownerUID, JITCompilationEffort);
242 void performFinalization();
244 #if DUMP_LINK_STATISTICS
245 static void dumpLinkStatistics(void* code, size_t initialSize, size_t finalSize);
249 static void dumpCode(void* code, size_t);
252 RefPtr<ExecutableMemoryHandle> m_executableMemory;
254 #if ENABLE(BRANCH_COMPACTION)
255 size_t m_initialSize;
258 MacroAssembler* m_assembler;
259 JSGlobalData* m_globalData;
262 JITCompilationEffort m_effort;
266 #define FINALIZE_CODE_IF(condition, linkBufferReference, dataLogArgumentsForHeading) \
267 (UNLIKELY((condition)) \
268 ? ((linkBufferReference).finalizeCodeWithDisassembly dataLogArgumentsForHeading) \
269 : (linkBufferReference).finalizeCodeWithoutDisassembly())
271 // Use this to finalize code, like so:
273 // CodeRef code = FINALIZE_CODE(linkBuffer, ("my super thingy number %d", number));
275 // Which, in disassembly mode, will print:
277 // Generated JIT code for my super thingy number 42:
278 // Code at [0x123456, 0x234567]:
279 // 0x123456: mov $0, 0
284 // Note that the dataLogArgumentsForHeading are only evaluated when showDisassembly
285 // is true, so you can hide expensive disassembly-only computations inside there.
287 #define FINALIZE_CODE(linkBufferReference, dataLogArgumentsForHeading) \
288 FINALIZE_CODE_IF(Options::showDisassembly(), linkBufferReference, dataLogArgumentsForHeading)
292 #endif // ENABLE(ASSEMBLER)
294 #endif // LinkBuffer_h