blob: 382efff4479f1e92eb972a1d638504e3d6b70f73 [file] [log] [blame]
barraclough@apple.comba92ede2009-07-22 00:37:03 +00001/*
mark.lam@apple.com03313a92018-02-27 07:50:54 +00002 * Copyright (C) 2009-2018 Apple Inc. All rights reserved.
barraclough@apple.comba92ede2009-07-22 00:37:03 +00003 *
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
ryanhaddad@apple.com22104f52016-09-28 17:08:17 +000026#pragma once
barraclough@apple.comba92ede2009-07-22 00:37:03 +000027
barraclough@apple.comba92ede2009-07-22 00:37:03 +000028#if ENABLE(ASSEMBLER)
29
ggaren@apple.comaa701ef2010-10-14 19:19:17 +000030#define DUMP_LINK_STATISTICS 0
31#define DUMP_CODE 0
32
barraclough@apple.comb6a00d32012-01-23 21:08:34 +000033#define GLOBAL_THUNK_ID reinterpret_cast<void*>(static_cast<intptr_t>(-1))
34#define REGEXP_CODE_ID reinterpret_cast<void*>(static_cast<intptr_t>(-2))
benjamin@webkit.org182c19a2013-12-23 00:45:25 +000035#define CSS_CODE_ID reinterpret_cast<void*>(static_cast<intptr_t>(-3))
barraclough@apple.comb6a00d32012-01-23 21:08:34 +000036
fpizlo@apple.com0f25ee82012-03-01 05:46:20 +000037#include "JITCompilationEffort.h"
fpizlo@apple.com7bbcaab2012-02-22 05:23:19 +000038#include "MacroAssembler.h"
mark.lam@apple.comde1e15f2018-04-30 22:29:21 +000039#include "MacroAssemblerCodeRef.h"
fpizlo@apple.comd095b242012-02-12 03:21:32 +000040#include <wtf/DataLog.h>
andersca@apple.com3d185a82013-09-07 18:04:57 +000041#include <wtf/FastMalloc.h>
barraclough@apple.comba92ede2009-07-22 00:37:03 +000042#include <wtf/Noncopyable.h>
43
44namespace JSC {
45
keith_miller@apple.com8e7bd482019-10-01 16:38:26 +000046namespace Wasm {
47enum class CompilationMode : uint8_t;
48}
49
msaboff@apple.com95894332014-01-29 19:18:54 +000050class CodeBlock;
oliver@apple.com7e98c922011-05-27 18:30:08 +000051
barraclough@apple.comba92ede2009-07-22 00:37:03 +000052// LinkBuffer:
53//
54// This class assists in linking code generated by the macro assembler, once code generation
55// has been completed, and the code has been copied to is final location in memory. At this
56// time pointers to labels within the code may be resolved, and relative offsets to external
57// addresses may be fixed.
58//
59// Specifically:
60// * Jump objects may be linked to external targets,
61// * The address of Jump objects may taken, such that it can later be relinked.
62// * The return address of a Call may be acquired.
63// * The address of a Label pointing into the code may be resolved.
64// * The value referenced by a DataLabel may be set.
65//
ossy@webkit.org95c1bc42011-01-20 16:30:54 +000066class LinkBuffer {
oliver@apple.com90fce822013-07-25 04:00:13 +000067 WTF_MAKE_NONCOPYABLE(LinkBuffer); WTF_MAKE_FAST_ALLOCATED;
68
mark.lam@apple.comde0dba72018-04-18 03:31:09 +000069 template<PtrTag tag> using CodePtr = MacroAssemblerCodePtr<tag>;
70 template<PtrTag tag> using CodeRef = MacroAssemblerCodeRef<tag>;
barraclough@apple.comba92ede2009-07-22 00:37:03 +000071 typedef MacroAssembler::Label Label;
72 typedef MacroAssembler::Jump Jump;
barraclough@apple.com959b92e2012-04-25 00:06:04 +000073 typedef MacroAssembler::PatchableJump PatchableJump;
barraclough@apple.comba92ede2009-07-22 00:37:03 +000074 typedef MacroAssembler::JumpList JumpList;
75 typedef MacroAssembler::Call Call;
oliver@apple.com2c012fa2011-05-17 20:02:41 +000076 typedef MacroAssembler::DataLabelCompact DataLabelCompact;
barraclough@apple.comba92ede2009-07-22 00:37:03 +000077 typedef MacroAssembler::DataLabel32 DataLabel32;
78 typedef MacroAssembler::DataLabelPtr DataLabelPtr;
fpizlo@apple.comd68b1f82012-07-05 22:55:51 +000079 typedef MacroAssembler::ConvertibleLoadLabel ConvertibleLoadLabel;
oliver@apple.com2d5480d2010-08-10 03:19:19 +000080#if ENABLE(BRANCH_COMPACTION)
81 typedef MacroAssembler::LinkRecord LinkRecord;
82 typedef MacroAssembler::JumpLinkType JumpLinkType;
83#endif
barraclough@apple.comba92ede2009-07-22 00:37:03 +000084
85public:
sbarati@apple.com893729f2017-03-29 22:55:53 +000086 LinkBuffer(MacroAssembler& macroAssembler, void* ownerUID, JITCompilationEffort effort = JITCompilationMustSucceed)
fpizlo@apple.come6a7f5f2011-09-11 05:49:36 +000087 : m_size(0)
fpizlo@apple.coma26c9042013-10-20 02:07:39 +000088 , m_didAllocate(false)
barraclough@apple.come00c8ce2011-04-30 23:59:17 +000089#ifndef NDEBUG
90 , m_completed(false)
91#endif
92 {
benjamin@webkit.orgf766fd92014-07-08 04:23:30 +000093 linkCode(macroAssembler, ownerUID, effort);
barraclough@apple.come00c8ce2011-04-30 23:59:17 +000094 }
95
mark.lam@apple.comde1e15f2018-04-30 22:29:21 +000096 template<PtrTag tag>
97 LinkBuffer(MacroAssembler& macroAssembler, MacroAssemblerCodePtr<tag> code, size_t size, JITCompilationEffort effort = JITCompilationMustSucceed, bool shouldPerformBranchCompaction = true)
fpizlo@apple.com2e7ada02013-10-23 18:22:09 +000098 : m_size(size)
fpizlo@apple.com2e7ada02013-10-23 18:22:09 +000099 , m_didAllocate(false)
fpizlo@apple.com2e7ada02013-10-23 18:22:09 +0000100#ifndef NDEBUG
101 , m_completed(false)
102#endif
rmorisset@apple.com4ccbcf12019-03-12 19:07:04 +0000103 , m_code(code.template retagged<LinkBufferPtrTag>())
fpizlo@apple.com2e7ada02013-10-23 18:22:09 +0000104 {
sbarati@apple.comb5bee812016-06-19 19:42:18 +0000105#if ENABLE(BRANCH_COMPACTION)
106 m_shouldPerformBranchCompaction = shouldPerformBranchCompaction;
107#else
108 UNUSED_PARAM(shouldPerformBranchCompaction);
109#endif
oliver@apple.com89b03a62016-03-09 00:08:53 +0000110 linkCode(macroAssembler, 0, effort);
fpizlo@apple.com2e7ada02013-10-23 18:22:09 +0000111 }
112
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000113 ~LinkBuffer()
114 {
fpizlo@apple.com0f25ee82012-03-01 05:46:20 +0000115 }
116
117 bool didFailToAllocate() const
118 {
fpizlo@apple.coma26c9042013-10-20 02:07:39 +0000119 return !m_didAllocate;
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000120 }
121
fpizlo@apple.com0f25ee82012-03-01 05:46:20 +0000122 bool isValid() const
123 {
124 return !didFailToAllocate();
125 }
126
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000127 // These methods are used to link or set values at code generation time.
128
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000129 template<PtrTag tag, typename Func, typename = std::enable_if_t<std::is_function<typename std::remove_pointer<Func>::type>::value>>
130 void link(Call call, Func funcName)
mark.lam@apple.come1fc7a02018-03-14 20:05:37 +0000131 {
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000132 FunctionPtr<tag> function(funcName);
mark.lam@apple.come1fc7a02018-03-14 20:05:37 +0000133 link(call, function);
134 }
135
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000136 template<PtrTag tag>
137 void link(Call call, FunctionPtr<tag> function)
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000138 {
139 ASSERT(call.isFlagSet(Call::Linkable));
barraclough@apple.com0d15b9e2011-05-26 04:12:41 +0000140 call.m_label = applyOffset(call.m_label);
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000141 MacroAssembler::linkCall(code(), call, function);
142 }
143
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000144 template<PtrTag tag>
145 void link(Call call, CodeLocationLabel<tag> label)
fpizlo@apple.com2e7ada02013-10-23 18:22:09 +0000146 {
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000147 link(call, FunctionPtr<tag>(label));
fpizlo@apple.com2e7ada02013-10-23 18:22:09 +0000148 }
149
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000150 template<PtrTag tag>
151 void link(Jump jump, CodeLocationLabel<tag> label)
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000152 {
barraclough@apple.com0d15b9e2011-05-26 04:12:41 +0000153 jump.m_label = applyOffset(jump.m_label);
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000154 MacroAssembler::linkJump(code(), jump, label);
155 }
156
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000157 template<PtrTag tag>
158 void link(const JumpList& list, CodeLocationLabel<tag> label)
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000159 {
commit-queue@webkit.org8f207872016-04-19 06:54:25 +0000160 for (const Jump& jump : list.jumps())
161 link(jump, label);
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000162 }
163
164 void patch(DataLabelPtr label, void* value)
165 {
barraclough@apple.com4836c7a2011-05-01 22:20:59 +0000166 AssemblerLabel target = applyOffset(label.m_label);
oliver@apple.com2d5480d2010-08-10 03:19:19 +0000167 MacroAssembler::linkPointer(code(), target, value);
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000168 }
169
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000170 template<PtrTag tag>
171 void patch(DataLabelPtr label, CodeLocationLabel<tag> value)
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000172 {
barraclough@apple.com4836c7a2011-05-01 22:20:59 +0000173 AssemblerLabel target = applyOffset(label.m_label);
mark.lam@apple.com2cd487f2017-11-30 23:47:35 +0000174 MacroAssembler::linkPointer(code(), target, value);
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000175 }
176
177 // These methods are used to obtain handles to allow the code to be relinked / repatched later.
msaboff@apple.com95894332014-01-29 19:18:54 +0000178
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000179 template<PtrTag tag>
180 CodeLocationLabel<tag> entrypoint()
msaboff@apple.com95894332014-01-29 19:18:54 +0000181 {
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000182 return CodeLocationLabel<tag>(tagCodePtr<tag>(code()));
msaboff@apple.com95894332014-01-29 19:18:54 +0000183 }
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000184
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000185 template<PtrTag tag>
186 CodeLocationCall<tag> locationOf(Call call)
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000187 {
188 ASSERT(call.isFlagSet(Call::Linkable));
189 ASSERT(!call.isFlagSet(Call::Near));
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000190 return CodeLocationCall<tag>(MacroAssembler::getLinkerAddress<tag>(code(), applyOffset(call.m_label)));
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000191 }
192
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000193 template<PtrTag tag>
194 CodeLocationNearCall<tag> locationOfNearCall(Call call)
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000195 {
196 ASSERT(call.isFlagSet(Call::Linkable));
197 ASSERT(call.isFlagSet(Call::Near));
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000198 return CodeLocationNearCall<tag>(MacroAssembler::getLinkerAddress<tag>(code(), applyOffset(call.m_label)),
msaboff@apple.comc15ae7e2015-09-16 23:40:35 +0000199 call.isFlagSet(Call::Tail) ? NearCallMode::Tail : NearCallMode::Regular);
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000200 }
201
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000202 template<PtrTag tag>
203 CodeLocationLabel<tag> locationOf(PatchableJump jump)
barraclough@apple.com03571d72011-11-08 23:45:05 +0000204 {
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000205 return CodeLocationLabel<tag>(MacroAssembler::getLinkerAddress<tag>(code(), applyOffset(jump.m_jump.m_label)));
barraclough@apple.com03571d72011-11-08 23:45:05 +0000206 }
207
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000208 template<PtrTag tag>
209 CodeLocationLabel<tag> locationOf(Label label)
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000210 {
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000211 return CodeLocationLabel<tag>(MacroAssembler::getLinkerAddress<tag>(code(), applyOffset(label.m_label)));
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000212 }
213
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000214 template<PtrTag tag>
215 CodeLocationDataLabelPtr<tag> locationOf(DataLabelPtr label)
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000216 {
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000217 return CodeLocationDataLabelPtr<tag>(MacroAssembler::getLinkerAddress<tag>(code(), applyOffset(label.m_label)));
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000218 }
219
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000220 template<PtrTag tag>
221 CodeLocationDataLabel32<tag> locationOf(DataLabel32 label)
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000222 {
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000223 return CodeLocationDataLabel32<tag>(MacroAssembler::getLinkerAddress<tag>(code(), applyOffset(label.m_label)));
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000224 }
oliver@apple.com2c012fa2011-05-17 20:02:41 +0000225
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000226 template<PtrTag tag>
227 CodeLocationDataLabelCompact<tag> locationOf(DataLabelCompact label)
oliver@apple.com2c012fa2011-05-17 20:02:41 +0000228 {
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000229 return CodeLocationDataLabelCompact<tag>(MacroAssembler::getLinkerAddress<tag>(code(), applyOffset(label.m_label)));
oliver@apple.com2c012fa2011-05-17 20:02:41 +0000230 }
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000231
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000232 template<PtrTag tag>
233 CodeLocationConvertibleLoad<tag> locationOf(ConvertibleLoadLabel label)
fpizlo@apple.comd68b1f82012-07-05 22:55:51 +0000234 {
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000235 return CodeLocationConvertibleLoad<tag>(MacroAssembler::getLinkerAddress<tag>(code(), applyOffset(label.m_label)));
fpizlo@apple.comd68b1f82012-07-05 22:55:51 +0000236 }
237
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000238 // This method obtains the return address of the call, given as an offset from
239 // the start of the code.
240 unsigned returnAddressOffset(Call call)
241 {
barraclough@apple.com0d15b9e2011-05-26 04:12:41 +0000242 call.m_label = applyOffset(call.m_label);
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000243 return MacroAssembler::getLinkerCallReturnOffset(call);
244 }
245
barraclough@apple.com13b38062011-11-09 00:43:15 +0000246 uint32_t offsetOf(Label label)
247 {
248 return applyOffset(label.m_label).m_offset;
249 }
250
oliver@apple.comea771492013-07-25 03:58:38 +0000251 unsigned offsetOf(PatchableJump jump)
252 {
253 return applyOffset(jump.m_jump.m_label).m_offset;
254 }
255
fpizlo@apple.com2adf5272012-06-20 01:33:30 +0000256 // Upon completion of all patching 'FINALIZE_CODE()' should be called once to
257 // complete generation of the code. Alternatively, call
258 // finalizeCodeWithoutDisassembly() directly if you have your own way of
259 // displaying disassembly.
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000260
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000261 template<PtrTag tag>
262 CodeRef<tag> finalizeCodeWithoutDisassembly()
oliver@apple.com2d5480d2010-08-10 03:19:19 +0000263 {
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000264 return finalizeCodeWithoutDisassemblyImpl().template retagged<tag>();
265 }
266
267 template<PtrTag tag, typename... Args>
yusukesuzuki@slowstart.org8735b842018-10-05 19:59:04 +0000268 CodeRef<tag> finalizeCodeWithDisassembly(bool dumpDisassembly, const char* format, Args... args)
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000269 {
guijemont@igalia.com65db7e42018-09-12 15:09:16 +0000270 ALLOW_NONLITERAL_FORMAT_BEGIN
271 IGNORE_WARNINGS_BEGIN("format-security")
yusukesuzuki@slowstart.org8735b842018-10-05 19:59:04 +0000272 return finalizeCodeWithDisassemblyImpl(dumpDisassembly, format, args...).template retagged<tag>();
guijemont@igalia.com65db7e42018-09-12 15:09:16 +0000273 IGNORE_WARNINGS_END
274 ALLOW_NONLITERAL_FORMAT_END
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000275 }
276
277 template<PtrTag tag>
278 CodePtr<tag> trampolineAt(Label label)
279 {
280 return CodePtr<tag>(MacroAssembler::AssemblerType_T::getRelocatedAddress(code(), applyOffset(label.m_label)));
oliver@apple.com2d5480d2010-08-10 03:19:19 +0000281 }
282
barraclough@apple.com9cb663d2011-04-15 00:25:59 +0000283 void* debugAddress()
284 {
mark.lam@apple.comde1e15f2018-04-30 22:29:21 +0000285 return m_code.dataLocation();
barraclough@apple.com9cb663d2011-04-15 00:25:59 +0000286 }
benjamin@webkit.orgf6de0772014-07-15 23:59:14 +0000287
sbarati@apple.comb5bee812016-06-19 19:42:18 +0000288 size_t size() const { return m_size; }
fpizlo@apple.com32622c92015-03-24 05:37:19 +0000289
290 bool wasAlreadyDisassembled() const { return m_alreadyDisassembled; }
291 void didAlreadyDisassemble() { m_alreadyDisassembled = true; }
barraclough@apple.com9cb663d2011-04-15 00:25:59 +0000292
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000293private:
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000294 JS_EXPORT_PRIVATE CodeRef<LinkBufferPtrTag> finalizeCodeWithoutDisassemblyImpl();
yusukesuzuki@slowstart.org8735b842018-10-05 19:59:04 +0000295 JS_EXPORT_PRIVATE CodeRef<LinkBufferPtrTag> finalizeCodeWithDisassemblyImpl(bool dumpDisassembly, const char* format, ...) WTF_ATTRIBUTE_PRINTF(3, 4);
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000296
fpizlo@apple.com00593b22014-02-22 19:55:12 +0000297#if ENABLE(BRANCH_COMPACTION)
298 int executableOffsetFor(int location)
299 {
guijemont@igalia.com99ff59a2018-06-20 17:16:21 +0000300 // Returning 0 in this case works because at location <
301 // sizeof(int32_t), no compaction could have happened before this
302 // point as the assembler could not have placed a branch instruction
303 // within this space that required compaction.
304 if (location < static_cast<int>(sizeof(int32_t)))
fpizlo@apple.com00593b22014-02-22 19:55:12 +0000305 return 0;
benjamin@webkit.orgf6de0772014-07-15 23:59:14 +0000306 return bitwise_cast<int32_t*>(m_assemblerStorage.buffer())[location / sizeof(int32_t) - 1];
fpizlo@apple.com00593b22014-02-22 19:55:12 +0000307 }
308#endif
309
oliver@apple.com2d5480d2010-08-10 03:19:19 +0000310 template <typename T> T applyOffset(T src)
311 {
312#if ENABLE(BRANCH_COMPACTION)
fpizlo@apple.com00593b22014-02-22 19:55:12 +0000313 src.m_offset -= executableOffsetFor(src.m_offset);
oliver@apple.com2d5480d2010-08-10 03:19:19 +0000314#endif
315 return src;
316 }
sbarati@apple.comb5bee812016-06-19 19:42:18 +0000317
barraclough@apple.comb6a00d32012-01-23 21:08:34 +0000318 // Keep this private! - the underlying code should only be obtained externally via finalizeCode().
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000319 void* code()
320 {
mark.lam@apple.comde1e15f2018-04-30 22:29:21 +0000321 return m_code.dataLocation();
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000322 }
fpizlo@apple.coma26c9042013-10-20 02:07:39 +0000323
sbarati@apple.comb5bee812016-06-19 19:42:18 +0000324 void allocate(MacroAssembler&, void* ownerUID, JITCompilationEffort);
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000325
benjamin@webkit.orgf766fd92014-07-08 04:23:30 +0000326 JS_EXPORT_PRIVATE void linkCode(MacroAssembler&, void* ownerUID, JITCompilationEffort);
dbates@webkit.org98f0de02013-10-15 22:16:39 +0000327#if ENABLE(BRANCH_COMPACTION)
328 template <typename InstructionType>
benjamin@webkit.orgf766fd92014-07-08 04:23:30 +0000329 void copyCompactAndLinkCode(MacroAssembler&, void* ownerUID, JITCompilationEffort);
dbates@webkit.org98f0de02013-10-15 22:16:39 +0000330#endif
oliver@apple.com2d5480d2010-08-10 03:19:19 +0000331
fpizlo@apple.com2adf5272012-06-20 01:33:30 +0000332 void performFinalization();
ggaren@apple.comaa701ef2010-10-14 19:19:17 +0000333
334#if DUMP_LINK_STATISTICS
fpizlo@apple.com2adf5272012-06-20 01:33:30 +0000335 static void dumpLinkStatistics(void* code, size_t initialSize, size_t finalSize);
ggaren@apple.comaa701ef2010-10-14 19:19:17 +0000336#endif
337
338#if DUMP_CODE
fpizlo@apple.com2adf5272012-06-20 01:33:30 +0000339 static void dumpCode(void* code, size_t);
ggaren@apple.comaa701ef2010-10-14 19:19:17 +0000340#endif
341
fpizlo@apple.come6a7f5f2011-09-11 05:49:36 +0000342 RefPtr<ExecutableMemoryHandle> m_executableMemory;
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000343 size_t m_size;
commit-queue@webkit.orgf2566dc2012-02-10 23:01:54 +0000344#if ENABLE(BRANCH_COMPACTION)
benjamin@webkit.orgf6de0772014-07-15 23:59:14 +0000345 AssemblerData m_assemblerStorage;
sbarati@apple.comb5bee812016-06-19 19:42:18 +0000346 bool m_shouldPerformBranchCompaction { true };
commit-queue@webkit.orgf2566dc2012-02-10 23:01:54 +0000347#endif
fpizlo@apple.coma26c9042013-10-20 02:07:39 +0000348 bool m_didAllocate;
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000349#ifndef NDEBUG
barraclough@apple.comce1490e2010-08-26 23:21:24 +0000350 bool m_completed;
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000351#endif
fpizlo@apple.com32622c92015-03-24 05:37:19 +0000352 bool m_alreadyDisassembled { false };
rmorisset@apple.com4ccbcf12019-03-12 19:07:04 +0000353 MacroAssemblerCodePtr<LinkBufferPtrTag> m_code;
fpizlo@apple.com6e697962015-10-12 17:56:26 +0000354 Vector<RefPtr<SharedTask<void(LinkBuffer&)>>> m_linkTasks;
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000355};
356
yusukesuzuki@slowstart.org8735b842018-10-05 19:59:04 +0000357#if OS(LINUX)
mark.lam@apple.come1fc7a02018-03-14 20:05:37 +0000358#define FINALIZE_CODE_IF(condition, linkBufferReference, resultPtrTag, ...) \
fpizlo@apple.com12c18392012-06-27 23:16:10 +0000359 (UNLIKELY((condition)) \
yusukesuzuki@slowstart.org8735b842018-10-05 19:59:04 +0000360 ? (linkBufferReference).finalizeCodeWithDisassembly<resultPtrTag>(true, __VA_ARGS__) \
361 : (UNLIKELY(JSC::Options::logJITCodeForPerf()) \
362 ? (linkBufferReference).finalizeCodeWithDisassembly<resultPtrTag>(false, __VA_ARGS__) \
363 : (linkBufferReference).finalizeCodeWithoutDisassembly<resultPtrTag>()))
364#else
365#define FINALIZE_CODE_IF(condition, linkBufferReference, resultPtrTag, ...) \
366 (UNLIKELY((condition)) \
367 ? (linkBufferReference).finalizeCodeWithDisassembly<resultPtrTag>(true, __VA_ARGS__) \
mark.lam@apple.comde0dba72018-04-18 03:31:09 +0000368 : (linkBufferReference).finalizeCodeWithoutDisassembly<resultPtrTag>())
yusukesuzuki@slowstart.org8735b842018-10-05 19:59:04 +0000369#endif
fpizlo@apple.com12c18392012-06-27 23:16:10 +0000370
mark.lam@apple.comee3c4102015-10-14 18:57:07 +0000371bool shouldDumpDisassemblyFor(CodeBlock*);
msaboff@apple.com95894332014-01-29 19:18:54 +0000372
mark.lam@apple.come1fc7a02018-03-14 20:05:37 +0000373#define FINALIZE_CODE_FOR(codeBlock, linkBufferReference, resultPtrTag, ...) \
374 FINALIZE_CODE_IF((shouldDumpDisassemblyFor(codeBlock) || Options::asyncDisassembly()), linkBufferReference, resultPtrTag, __VA_ARGS__)
msaboff@apple.com95894332014-01-29 19:18:54 +0000375
fpizlo@apple.com2adf5272012-06-20 01:33:30 +0000376// Use this to finalize code, like so:
377//
mark.lam@apple.come1fc7a02018-03-14 20:05:37 +0000378// CodeRef code = FINALIZE_CODE(linkBuffer, tag, "my super thingy number %d", number);
fpizlo@apple.com2adf5272012-06-20 01:33:30 +0000379//
380// Which, in disassembly mode, will print:
381//
382// Generated JIT code for my super thingy number 42:
383// Code at [0x123456, 0x234567]:
384// 0x123456: mov $0, 0
385// 0x12345a: ret
386//
387// ... and so on.
388//
mark.lam@apple.com03313a92018-02-27 07:50:54 +0000389// Note that the format string and print arguments are only evaluated when dumpDisassembly
fpizlo@apple.com2adf5272012-06-20 01:33:30 +0000390// is true, so you can hide expensive disassembly-only computations inside there.
391
mark.lam@apple.come1fc7a02018-03-14 20:05:37 +0000392#define FINALIZE_CODE(linkBufferReference, resultPtrTag, ...) \
393 FINALIZE_CODE_IF((JSC::Options::asyncDisassembly() || JSC::Options::dumpDisassembly()), linkBufferReference, resultPtrTag, __VA_ARGS__)
fpizlo@apple.com2adf5272012-06-20 01:33:30 +0000394
mark.lam@apple.come1fc7a02018-03-14 20:05:37 +0000395#define FINALIZE_DFG_CODE(linkBufferReference, resultPtrTag, ...) \
396 FINALIZE_CODE_IF((JSC::Options::asyncDisassembly() || JSC::Options::dumpDisassembly() || Options::dumpDFGDisassembly()), linkBufferReference, resultPtrTag, __VA_ARGS__)
fpizlo@apple.com57fb54d2012-09-21 23:29:30 +0000397
yusukesuzuki@slowstart.orge8dd3102018-08-21 03:29:32 +0000398#define FINALIZE_REGEXP_CODE(linkBufferReference, resultPtrTag, dataLogFArgumentsForHeading) \
399 FINALIZE_CODE_IF(JSC::Options::asyncDisassembly() || JSC::Options::dumpDisassembly() || Options::dumpRegExpDisassembly(), linkBufferReference, resultPtrTag, dataLogFArgumentsForHeading)
400
keith_miller@apple.com8e7bd482019-10-01 16:38:26 +0000401bool shouldDumpDisassemblyFor(Wasm::CompilationMode);
402
403#define FINALIZE_WASM_CODE(linkBufferReference, resultPtrTag, ...) \
404 FINALIZE_CODE_IF((JSC::Options::asyncDisassembly() || JSC::Options::dumpDisassembly() || Options::dumpWasmDisassembly()), linkBufferReference, resultPtrTag, __VA_ARGS__)
405
406#define FINALIZE_WASM_CODE_FOR_MODE(mode, linkBufferReference, resultPtrTag, ...) \
407 FINALIZE_CODE_IF(shouldDumpDisassemblyFor(mode), linkBufferReference, resultPtrTag, __VA_ARGS__)
408
409
410
barraclough@apple.comba92ede2009-07-22 00:37:03 +0000411} // namespace JSC
412
413#endif // ENABLE(ASSEMBLER)