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 "DFGCommon.h"
31#include "FPRInfo.h"
32#include "GPRInfo.h"
33#include "Reg.h"
34#include <wtf/HashMap.h>
35
36namespace JSC {
37
38namespace B3 {
39class ValueRep;
40} // namespace B3
41
42namespace FTL {
43
44class Location {
45public:
46 enum Kind {
47 Unprocessed,
48 Register,
49 Indirect,
50 Constant
51 };
52
53 Location()
54 : m_kind(Unprocessed)
55 {
56 u.constant = 0;
57 }
58
59 Location(WTF::HashTableDeletedValueType)
60 : m_kind(Unprocessed)
61 {
62 u.constant = 1;
63 }
64
65 static Location forRegister(Reg reg, int32_t addend)
66 {
67 Location result;
68 result.m_kind = Register;
69 result.u.variable.regIndex = reg.index();
70 result.u.variable.offset = addend;
71 return result;
72 }
73
74 static Location forIndirect(Reg reg, int32_t offset)
75 {
76 Location result;
77 result.m_kind = Indirect;
78 result.u.variable.regIndex = reg.index();
79 result.u.variable.offset = offset;
80 return result;
81 }
82
83 static Location forConstant(int64_t constant)
84 {
85 Location result;
86 result.m_kind = Constant;
87 result.u.constant = constant;
88 return result;
89 }
90
91 static Location forValueRep(const B3::ValueRep&);
92
93 Kind kind() const { return m_kind; }
94
95 bool hasReg() const { return kind() == Register || kind() == Indirect; }
96 Reg reg() const
97 {
98 ASSERT(hasReg());
99 return Reg::fromIndex(u.variable.regIndex);
100 }
101
102 bool hasOffset() const { return kind() == Indirect; }
103 int32_t offset() const
104 {
105 ASSERT(hasOffset());
106 return u.variable.offset;
107 }
108
109 bool hasAddend() const { return kind() == Register; }
110 int32_t addend() const
111 {
112 ASSERT(hasAddend());
113 return u.variable.offset;
114 }
115
116 bool hasConstant() const { return kind() == Constant; }
117 int64_t constant() const
118 {
119 ASSERT(hasConstant());
120 return u.constant;
121 }
122
123 explicit operator bool() const { return kind() != Unprocessed || u.variable.offset; }
124
125 bool operator!() const { return !static_cast<bool>(*this); }
126
127 bool isHashTableDeletedValue() const { return kind() == Unprocessed && u.variable.offset; }
128
129 bool operator==(const Location& other) const
130 {
131 return m_kind == other.m_kind
132 && u.constant == other.u.constant;
133 }
134
135 unsigned hash() const
136 {
137 unsigned result = m_kind;
138
139 switch (kind()) {
140 case Unprocessed:
141 result ^= u.variable.offset;
142 break;
143
144 case Register:
145 result ^= u.variable.regIndex;
146 break;
147
148 case Indirect:
149 result ^= u.variable.regIndex;
150 result ^= u.variable.offset;
151 break;
152
153 case Constant:
154 result ^= WTF::IntHash<int64_t>::hash(u.constant);
155 break;
156 }
157
158 return WTF::IntHash<unsigned>::hash(result);
159 }
160
161 void dump(PrintStream&) const;
162
163 bool isGPR() const;
164 bool involvesGPR() const;
165 GPRReg gpr() const;
166 GPRReg directGPR() const; // Get the GPR and assert that there is no addend.
167
168 bool isFPR() const;
169 FPRReg fpr() const;
170
171 // Assuming that all registers are saved to the savedRegisters buffer according
172 // to FTLSaveRestore convention, this loads the value into the given register.
173 // The code that this generates isn't exactly super fast. This assumes that FP
174 // and SP contain the same values that they would have contained in the original
175 // frame, or that you've done one or more canonically formed calls (i.e. can
176 // restore the FP by following the call frame linked list numFramesToPop times,
177 // and SP can be recovered by popping FP numFramesToPop-1 times and adding 16).
178 void restoreInto(MacroAssembler&, char* savedRegisters, GPRReg result, unsigned numFramesToPop = 0) const;
179
180private:
181 Kind m_kind;
182 union {
183 int64_t constant;
184 struct {
185 unsigned regIndex;
186 int32_t offset;
187 } variable;
188 } u;
189};
190
191struct LocationHash {
192 static unsigned hash(const Location& key) { return key.hash(); }
193 static bool equal(const Location& a, const Location& b) { return a == b; }
194 static constexpr bool safeToCompareToEmptyOrDeleted = true;
195};
196
197} } // namespace JSC::FTL
198
199namespace WTF {
200
201void printInternal(PrintStream&, JSC::FTL::Location::Kind);
202
203template<typename T> struct DefaultHash;
204template<> struct DefaultHash<JSC::FTL::Location> {
205 typedef JSC::FTL::LocationHash Hash;
206};
207
208template<typename T> struct HashTraits;
209template<> struct HashTraits<JSC::FTL::Location> : SimpleClassHashTraits<JSC::FTL::Location> { };
210
211} // namespace WTF
212
213#endif // ENABLE(FTL_JIT)
214