| 1 | /* |
| 2 | * Copyright (C) 2012-2018 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 | #pragma once |
| 27 | |
| 28 | #if ENABLE(JIT) |
| 29 | |
| 30 | #include "CallData.h" |
| 31 | #include "Intrinsic.h" |
| 32 | #include "MacroAssemblerCodeRef.h" |
| 33 | #include "ThunkGenerator.h" |
| 34 | #include "Weak.h" |
| 35 | #include "WeakHandleOwner.h" |
| 36 | #include <tuple> |
| 37 | #include <wtf/HashMap.h> |
| 38 | #include <wtf/text/StringHash.h> |
| 39 | |
| 40 | namespace JSC { |
| 41 | namespace DOMJIT { |
| 42 | class Signature; |
| 43 | } |
| 44 | |
| 45 | class VM; |
| 46 | class NativeExecutable; |
| 47 | |
| 48 | class JITThunks final : private WeakHandleOwner { |
| 49 | WTF_MAKE_FAST_ALLOCATED; |
| 50 | public: |
| 51 | JITThunks(); |
| 52 | virtual ~JITThunks(); |
| 53 | |
| 54 | MacroAssemblerCodePtr<JITThunkPtrTag> ctiNativeCall(VM*); |
| 55 | MacroAssemblerCodePtr<JITThunkPtrTag> ctiNativeConstruct(VM*); |
| 56 | MacroAssemblerCodePtr<JITThunkPtrTag> ctiNativeTailCall(VM*); |
| 57 | MacroAssemblerCodePtr<JITThunkPtrTag> ctiNativeTailCallWithoutSavedTags(VM*); |
| 58 | MacroAssemblerCodePtr<JITThunkPtrTag> ctiInternalFunctionCall(VM*); |
| 59 | MacroAssemblerCodePtr<JITThunkPtrTag> ctiInternalFunctionConstruct(VM*); |
| 60 | |
| 61 | MacroAssemblerCodeRef<JITThunkPtrTag> ctiStub(VM*, ThunkGenerator); |
| 62 | MacroAssemblerCodeRef<JITThunkPtrTag> existingCTIStub(ThunkGenerator); |
| 63 | |
| 64 | NativeExecutable* hostFunctionStub(VM*, TaggedNativeFunction, TaggedNativeFunction constructor, const String& name); |
| 65 | NativeExecutable* hostFunctionStub(VM*, TaggedNativeFunction, TaggedNativeFunction constructor, ThunkGenerator, Intrinsic, const DOMJIT::Signature*, const String& name); |
| 66 | NativeExecutable* hostFunctionStub(VM*, TaggedNativeFunction, ThunkGenerator, Intrinsic, const String& name); |
| 67 | |
| 68 | void clearHostFunctionStubs(); |
| 69 | |
| 70 | private: |
| 71 | void finalize(Handle<Unknown>, void* context) override; |
| 72 | |
| 73 | typedef HashMap<ThunkGenerator, MacroAssemblerCodeRef<JITThunkPtrTag>> CTIStubMap; |
| 74 | CTIStubMap m_ctiStubMap; |
| 75 | |
| 76 | typedef std::tuple<TaggedNativeFunction, TaggedNativeFunction, String> HostFunctionKey; |
| 77 | |
| 78 | struct HostFunctionHash { |
| 79 | static unsigned hash(const HostFunctionKey& key) |
| 80 | { |
| 81 | unsigned hash = WTF::pairIntHash(hashPointer(std::get<0>(key)), hashPointer(std::get<1>(key))); |
| 82 | if (!std::get<2>(key).isNull()) |
| 83 | hash = WTF::pairIntHash(hash, DefaultHash<String>::Hash::hash(std::get<2>(key))); |
| 84 | return hash; |
| 85 | } |
| 86 | static bool equal(const HostFunctionKey& a, const HostFunctionKey& b) |
| 87 | { |
| 88 | return (std::get<0>(a) == std::get<0>(b)) && (std::get<1>(a) == std::get<1>(b)) && (std::get<2>(a) == std::get<2>(b)); |
| 89 | } |
| 90 | static const bool safeToCompareToEmptyOrDeleted = true; |
| 91 | |
| 92 | private: |
| 93 | static inline unsigned hashPointer(TaggedNativeFunction p) |
| 94 | { |
| 95 | return DefaultHash<TaggedNativeFunction>::Hash::hash(p); |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | struct HostFunctionHashTrait : WTF::GenericHashTraits<HostFunctionKey> { |
| 100 | static const bool emptyValueIsZero = true; |
| 101 | static EmptyValueType emptyValue() { return std::make_tuple(nullptr, nullptr, String()); } |
| 102 | |
| 103 | static void constructDeletedValue(HostFunctionKey& slot) { std::get<0>(slot) = TaggedNativeFunction(-1); } |
| 104 | static bool isDeletedValue(const HostFunctionKey& value) { return std::get<0>(value) == TaggedNativeFunction(-1); } |
| 105 | }; |
| 106 | |
| 107 | typedef HashMap<HostFunctionKey, Weak<NativeExecutable>, HostFunctionHash, HostFunctionHashTrait> HostFunctionStubMap; |
| 108 | std::unique_ptr<HostFunctionStubMap> m_hostFunctionStubMap; |
| 109 | Lock m_lock; |
| 110 | }; |
| 111 | |
| 112 | } // namespace JSC |
| 113 | |
| 114 | #endif // ENABLE(JIT) |
| 115 | |