1/*
2 * Copyright (C) 2013-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#pragma once
27
28#if ENABLE(FTL_JIT)
29
30#include "MacroAssemblerCodeRef.h"
31#include "RegisterSet.h"
32
33namespace JSC { namespace FTL {
34
35// This is used for creating some sanity in slow-path calls out of the FTL's inline
36// caches. The idea is that we don't want all of the register save/restore stuff to
37// be generated at each IC site. Instead, the IC slow path call site will just save
38// the registers needed for the arguments. It will arrange for there to be enough
39// space on top of stack to save the remaining registers and the return PC. Then it
40// will call a shared thunk that will save the remaining registers. That thunk needs
41// to know the stack offset at which things get saved along with the call target.
42
43// Note that the offset is *not including* the return PC that would be pushed on X86.
44
45class SlowPathCallKey {
46public:
47 SlowPathCallKey()
48 : m_offset(0)
49 {
50 }
51
52 SlowPathCallKey(
53 const RegisterSet& set, FunctionPtr<CFunctionPtrTag> callTarget, const RegisterSet& argumentRegisters,
54 ptrdiff_t offset)
55 : m_usedRegisters(set)
56 , m_callTarget(callTarget.retagged<OperationPtrTag>())
57 , m_argumentRegisters(argumentRegisters)
58 , m_offset(offset)
59 {
60 }
61
62 const RegisterSet& usedRegisters() const { return m_usedRegisters; }
63 FunctionPtr<OperationPtrTag> callTarget() const { return m_callTarget; }
64 const RegisterSet& argumentRegisters() const { return m_argumentRegisters; }
65 ptrdiff_t offset() const { return m_offset; }
66
67 SlowPathCallKey withCallTarget(FunctionPtr<CFunctionPtrTag> callTarget)
68 {
69 return SlowPathCallKey(usedRegisters(), callTarget, argumentRegisters(), offset());
70 }
71
72 void dump(PrintStream&) const;
73
74 enum EmptyValueTag { EmptyValue };
75 enum DeletedValueTag { DeletedValue };
76
77 SlowPathCallKey(EmptyValueTag)
78 : m_usedRegisters(RegisterSet::EmptyValue)
79 , m_offset(0)
80 {
81 }
82
83 SlowPathCallKey(DeletedValueTag)
84 : m_usedRegisters(RegisterSet::DeletedValue)
85 , m_offset(0)
86 {
87 }
88
89 bool isEmptyValue() const { return m_usedRegisters.isEmptyValue(); }
90 bool isDeletedValue() const { return m_usedRegisters.isDeletedValue(); }
91
92 bool operator==(const SlowPathCallKey& other) const
93 {
94 return m_usedRegisters == other.m_usedRegisters
95 && m_callTarget == other.m_callTarget
96 && m_offset == other.m_offset;
97 }
98 unsigned hash() const
99 {
100 return m_usedRegisters.hash() + PtrHash<void*>::hash(m_callTarget.executableAddress()) + m_offset;
101 }
102
103private:
104 RegisterSet m_usedRegisters;
105 FunctionPtr<OperationPtrTag> m_callTarget;
106 RegisterSet m_argumentRegisters;
107 ptrdiff_t m_offset;
108};
109
110struct SlowPathCallKeyHash {
111 static unsigned hash(const SlowPathCallKey& key) { return key.hash(); }
112 static bool equal(const SlowPathCallKey& a, const SlowPathCallKey& b) { return a == b; }
113 static constexpr bool safeToCompareToEmptyOrDeleted = false;
114};
115
116} } // namespace JSC::FTL
117
118namespace WTF {
119
120template<typename T> struct DefaultHash;
121template<> struct DefaultHash<JSC::FTL::SlowPathCallKey> {
122 typedef JSC::FTL::SlowPathCallKeyHash Hash;
123};
124
125template<typename T> struct HashTraits;
126template<> struct HashTraits<JSC::FTL::SlowPathCallKey> : public CustomHashTraits<JSC::FTL::SlowPathCallKey> { };
127
128} // namespace WTF
129
130#endif // ENABLE(FTL_JIT)
131