[JSC] Remove per-host-function CTI stub in 32bit environment
[WebKit-https.git] / Source / JavaScriptCore / jit / JITThunks.cpp
1 /*
2  * Copyright (C) 2012, 2013, 2015-2016 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
37 namespace JSC {
38
39 JITThunks::JITThunks()
40     : m_hostFunctionStubMap(std::make_unique<HostFunctionStubMap>())
41 {
42 }
43
44 JITThunks::~JITThunks()
45 {
46 }
47
48 MacroAssemblerCodePtr JITThunks::ctiNativeCall(VM* vm)
49 {
50     if (!vm->canUseJIT())
51         return MacroAssemblerCodePtr::createLLIntCodePtr(llint_native_call_trampoline);
52     return ctiStub(vm, nativeCallGenerator).code();
53 }
54
55 MacroAssemblerCodePtr JITThunks::ctiNativeConstruct(VM* vm)
56 {
57     if (!vm->canUseJIT())
58         return MacroAssemblerCodePtr::createLLIntCodePtr(llint_native_construct_trampoline);
59     return ctiStub(vm, nativeConstructGenerator).code();
60 }
61
62 MacroAssemblerCodePtr JITThunks::ctiNativeTailCall(VM* vm)
63 {
64     ASSERT(vm->canUseJIT());
65     return ctiStub(vm, nativeTailCallGenerator).code();
66 }
67
68 MacroAssemblerCodePtr JITThunks::ctiNativeTailCallWithoutSavedTags(VM* vm)
69 {
70     ASSERT(vm->canUseJIT());
71     return ctiStub(vm, nativeTailCallWithoutSavedTagsGenerator).code();
72 }
73
74 MacroAssemblerCodeRef JITThunks::ctiStub(VM* vm, ThunkGenerator generator)
75 {
76     LockHolder locker(m_lock);
77     CTIStubMap::AddResult entry = m_ctiStubMap.add(generator, MacroAssemblerCodeRef());
78     if (entry.isNewEntry) {
79         // Compilation thread can only retrieve existing entries.
80         ASSERT(!isCompilationThread());
81         entry.iterator->value = generator(vm);
82     }
83     return entry.iterator->value;
84 }
85
86 MacroAssemblerCodeRef JITThunks::existingCTIStub(ThunkGenerator generator)
87 {
88     LockHolder locker(m_lock);
89     CTIStubMap::iterator entry = m_ctiStubMap.find(generator);
90     if (entry == m_ctiStubMap.end())
91         return MacroAssemblerCodeRef();
92     return entry->value;
93 }
94
95 void JITThunks::finalize(Handle<Unknown> handle, void*)
96 {
97     auto* nativeExecutable = static_cast<NativeExecutable*>(handle.get().asCell());
98     weakRemove(*m_hostFunctionStubMap, std::make_tuple(nativeExecutable->function(), nativeExecutable->constructor(), nativeExecutable->name()), nativeExecutable);
99 }
100
101 NativeExecutable* JITThunks::hostFunctionStub(VM* vm, NativeFunction function, NativeFunction constructor, const String& name)
102 {
103     return hostFunctionStub(vm, function, constructor, nullptr, NoIntrinsic, nullptr, name);
104 }
105
106 NativeExecutable* JITThunks::hostFunctionStub(VM* vm, NativeFunction function, NativeFunction constructor, ThunkGenerator generator, Intrinsic intrinsic, const DOMJIT::Signature* signature, const String& name)
107 {
108     ASSERT(!isCompilationThread());    
109     ASSERT(vm->canUseJIT());
110
111     if (NativeExecutable* nativeExecutable = m_hostFunctionStubMap->get(std::make_tuple(function, constructor, name)))
112         return nativeExecutable;
113
114     RefPtr<JITCode> forCall;
115     if (generator) {
116         MacroAssemblerCodeRef entry = generator(vm);
117         forCall = adoptRef(new DirectJITCode(entry, entry.code(), JITCode::HostCallThunk));
118     } else
119         forCall = adoptRef(new NativeJITCode(MacroAssemblerCodeRef::createSelfManagedCodeRef(ctiNativeCall(vm)), JITCode::HostCallThunk));
120     
121     Ref<JITCode> forConstruct = adoptRef(*new NativeJITCode(MacroAssemblerCodeRef::createSelfManagedCodeRef(ctiNativeConstruct(vm)), JITCode::HostCallThunk));
122     
123     NativeExecutable* nativeExecutable = NativeExecutable::create(*vm, forCall.releaseNonNull(), function, WTFMove(forConstruct), constructor, intrinsic, signature, name);
124     weakAdd(*m_hostFunctionStubMap, std::make_tuple(function, constructor, name), Weak<NativeExecutable>(nativeExecutable, this));
125     return nativeExecutable;
126 }
127
128 NativeExecutable* JITThunks::hostFunctionStub(VM* vm, NativeFunction function, ThunkGenerator generator, Intrinsic intrinsic, const String& name)
129 {
130     return hostFunctionStub(vm, function, callHostFunctionAsConstructor, generator, intrinsic, nullptr, name);
131 }
132
133 void JITThunks::clearHostFunctionStubs()
134 {
135     m_hostFunctionStubMap = nullptr;
136 }
137
138 } // namespace JSC
139
140 #endif // ENABLE(JIT)