1/*
2 * Copyright (C) 2013 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#ifndef FTLSlowPathCallKey_h
27#define FTLSlowPathCallKey_h
28
29#if ENABLE(FTL_JIT)
30
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_callTarget(0)
49        , m_offset(0)
50    {
51    }
52
53    SlowPathCallKey(
54        const RegisterSet& set, void* callTarget, const RegisterSet& argumentRegisters,
55        ptrdiff_t offset)
56        : m_usedRegisters(set)
57        , m_callTarget(callTarget)
58        , m_argumentRegisters(argumentRegisters)
59        , m_offset(offset)
60    {
61    }
62
63    const RegisterSet& usedRegisters() const { return m_usedRegisters; }
64    void* callTarget() const { return m_callTarget; }
65    const RegisterSet& argumentRegisters() const { return m_argumentRegisters; }
66    ptrdiff_t offset() const { return m_offset; }
67
68    SlowPathCallKey withCallTarget(void* callTarget)
69    {
70        return SlowPathCallKey(usedRegisters(), callTarget, argumentRegisters(), offset());
71    }
72
73    void dump(PrintStream&) const;
74
75    enum EmptyValueTag { EmptyValue };
76    enum DeletedValueTag { DeletedValue };
77
78    SlowPathCallKey(EmptyValueTag)
79        : m_usedRegisters(RegisterSet::EmptyValue)
80        , m_callTarget(0)
81        , m_offset(0)
82    {
83    }
84
85    SlowPathCallKey(DeletedValueTag)
86        : m_usedRegisters(RegisterSet::DeletedValue)
87        , m_callTarget(0)
88        , m_offset(0)
89    {
90    }
91
92    bool isEmptyValue() const { return m_usedRegisters.isEmptyValue(); }
93    bool isDeletedValue() const { return m_usedRegisters.isDeletedValue(); }
94
95    bool operator==(const SlowPathCallKey& other) const
96    {
97        return m_usedRegisters == other.m_usedRegisters
98            && m_callTarget == other.m_callTarget
99            && m_offset == other.m_offset;
100    }
101    unsigned hash() const
102    {
103        return m_usedRegisters.hash() + PtrHash<void*>::hash(m_callTarget) + m_offset;
104    }
105
106private:
107    RegisterSet m_usedRegisters;
108    void* m_callTarget;
109    RegisterSet m_argumentRegisters;
110    ptrdiff_t m_offset;
111};
112
113struct SlowPathCallKeyHash {
114    static unsigned hash(const SlowPathCallKey& key) { return key.hash(); }
115    static bool equal(const SlowPathCallKey& a, const SlowPathCallKey& b) { return a == b; }
116    static const bool safeToCompareToEmptyOrDeleted = false;
117};
118
119} } // namespace JSC::FTL
120
121namespace WTF {
122
123template<typename T> struct DefaultHash;
124template<> struct DefaultHash<JSC::FTL::SlowPathCallKey> {
125    typedef JSC::FTL::SlowPathCallKeyHash Hash;
126};
127
128template<typename T> struct HashTraits;
129template<> struct HashTraits<JSC::FTL::SlowPathCallKey> : public CustomHashTraits<JSC::FTL::SlowPathCallKey> { };
130
131} // namespace WTF
132
133#endif // ENABLE(FTL_JIT)
134
135#endif // FTLSlowPathCallKey_h
136
137