1/*
2 * Copyright (C) 2012-2019 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#include "config.h"
27#include "JITThunks.h"
28
29#if ENABLE(JIT)
30
31#include "JIT.h"
32#include "JSCInlines.h"
33#include "LLIntData.h"
34#include "ThunkGenerators.h"
35#include "VM.h"
36
37namespace JSC {
38
39JITThunks::JITThunks()
40 : m_hostFunctionStubMap(makeUnique<HostFunctionStubMap>())
41{
42}
43
44JITThunks::~JITThunks()
45{
46}
47
48MacroAssemblerCodePtr<JITThunkPtrTag> JITThunks::ctiNativeCall(VM& vm)
49{
50 ASSERT(VM::canUseJIT());
51 return ctiStub(vm, nativeCallGenerator).code();
52}
53
54MacroAssemblerCodePtr<JITThunkPtrTag> JITThunks::ctiNativeConstruct(VM& vm)
55{
56 ASSERT(VM::canUseJIT());
57 return ctiStub(vm, nativeConstructGenerator).code();
58}
59
60MacroAssemblerCodePtr<JITThunkPtrTag> JITThunks::ctiNativeTailCall(VM& vm)
61{
62 ASSERT(VM::canUseJIT());
63 return ctiStub(vm, nativeTailCallGenerator).code();
64}
65
66MacroAssemblerCodePtr<JITThunkPtrTag> JITThunks::ctiNativeTailCallWithoutSavedTags(VM& vm)
67{
68 ASSERT(VM::canUseJIT());
69 return ctiStub(vm, nativeTailCallWithoutSavedTagsGenerator).code();
70}
71
72MacroAssemblerCodePtr<JITThunkPtrTag> JITThunks::ctiInternalFunctionCall(VM& vm)
73{
74 ASSERT(VM::canUseJIT());
75 return ctiStub(vm, internalFunctionCallGenerator).code();
76}
77
78MacroAssemblerCodePtr<JITThunkPtrTag> JITThunks::ctiInternalFunctionConstruct(VM& vm)
79{
80 ASSERT(VM::canUseJIT());
81 return ctiStub(vm, internalFunctionConstructGenerator).code();
82}
83
84MacroAssemblerCodeRef<JITThunkPtrTag> JITThunks::ctiStub(VM& vm, ThunkGenerator generator)
85{
86 LockHolder locker(m_lock);
87 CTIStubMap::AddResult entry = m_ctiStubMap.add(generator, MacroAssemblerCodeRef<JITThunkPtrTag>());
88 if (entry.isNewEntry) {
89 // Compilation thread can only retrieve existing entries.
90 ASSERT(!isCompilationThread());
91 entry.iterator->value = generator(vm);
92 }
93 return entry.iterator->value;
94}
95
96MacroAssemblerCodeRef<JITThunkPtrTag> JITThunks::existingCTIStub(ThunkGenerator generator)
97{
98 LockHolder locker(m_lock);
99 CTIStubMap::iterator entry = m_ctiStubMap.find(generator);
100 if (entry == m_ctiStubMap.end())
101 return MacroAssemblerCodeRef<JITThunkPtrTag>();
102 return entry->value;
103}
104
105void JITThunks::finalize(Handle<Unknown> handle, void*)
106{
107 auto* nativeExecutable = static_cast<NativeExecutable*>(handle.get().asCell());
108 weakRemove(*m_hostFunctionStubMap, std::make_tuple(nativeExecutable->function(), nativeExecutable->constructor(), nativeExecutable->name()), nativeExecutable);
109}
110
111NativeExecutable* JITThunks::hostFunctionStub(VM& vm, TaggedNativeFunction function, TaggedNativeFunction constructor, const String& name)
112{
113 return hostFunctionStub(vm, function, constructor, nullptr, NoIntrinsic, nullptr, name);
114}
115
116NativeExecutable* JITThunks::hostFunctionStub(VM& vm, TaggedNativeFunction function, TaggedNativeFunction constructor, ThunkGenerator generator, Intrinsic intrinsic, const DOMJIT::Signature* signature, const String& name)
117{
118 ASSERT(!isCompilationThread());
119 ASSERT(VM::canUseJIT());
120
121 if (NativeExecutable* nativeExecutable = m_hostFunctionStubMap->get(std::make_tuple(function, constructor, name)))
122 return nativeExecutable;
123
124 RefPtr<JITCode> forCall;
125 if (generator) {
126 MacroAssemblerCodeRef<JSEntryPtrTag> entry = generator(vm).retagged<JSEntryPtrTag>();
127 forCall = adoptRef(new DirectJITCode(entry, entry.code(), JITType::HostCallThunk, intrinsic));
128 } else if (signature)
129 forCall = adoptRef(new NativeDOMJITCode(MacroAssemblerCodeRef<JSEntryPtrTag>::createSelfManagedCodeRef(ctiNativeCall(vm).retagged<JSEntryPtrTag>()), JITType::HostCallThunk, intrinsic, signature));
130 else
131 forCall = adoptRef(new NativeJITCode(MacroAssemblerCodeRef<JSEntryPtrTag>::createSelfManagedCodeRef(ctiNativeCall(vm).retagged<JSEntryPtrTag>()), JITType::HostCallThunk, intrinsic));
132
133 Ref<JITCode> forConstruct = adoptRef(*new NativeJITCode(MacroAssemblerCodeRef<JSEntryPtrTag>::createSelfManagedCodeRef(ctiNativeConstruct(vm).retagged<JSEntryPtrTag>()), JITType::HostCallThunk, NoIntrinsic));
134
135 NativeExecutable* nativeExecutable = NativeExecutable::create(vm, forCall.releaseNonNull(), function, WTFMove(forConstruct), constructor, name);
136 weakAdd(*m_hostFunctionStubMap, std::make_tuple(function, constructor, name), Weak<NativeExecutable>(nativeExecutable, this));
137 return nativeExecutable;
138}
139
140NativeExecutable* JITThunks::hostFunctionStub(VM& vm, TaggedNativeFunction function, ThunkGenerator generator, Intrinsic intrinsic, const String& name)
141{
142 return hostFunctionStub(vm, function, callHostFunctionAsConstructor, generator, intrinsic, nullptr, name);
143}
144
145void JITThunks::clearHostFunctionStubs()
146{
147 m_hostFunctionStubMap = nullptr;
148}
149
150} // namespace JSC
151
152#endif // ENABLE(JIT)
153