1/*
2 * Copyright (C) 2015-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(B3_JIT)
29
30#include "FPRInfo.h"
31#include "GPRInfo.h"
32#include "JSCJSValue.h"
33#include "Reg.h"
34#include "RegisterSet.h"
35#include "ValueRecovery.h"
36#include <wtf/PrintStream.h>
37
38namespace JSC {
39
40class AssemblyHelpers;
41
42namespace B3 {
43
44// We use this class to describe value representations at stackmaps. It's used both to force a
45// representation and to get the representation. When the B3 client forces a representation, we say
46// that it's an input. When B3 tells the client what representation it picked, we say that it's an
47// output.
48
49class ValueRep {
50public:
51 enum Kind {
52 // As an input representation, this means that B3 can pick any representation. As an output
53 // representation, this means that we don't know. This will only arise as an output
54 // representation for the active arguments of Check/CheckAdd/CheckSub/CheckMul.
55 WarmAny,
56
57 // Same as WarmAny, but implies that the use is cold. A cold use is not counted as a use for
58 // computing the priority of the used temporary.
59 ColdAny,
60
61 // Same as ColdAny, but also implies that the use occurs after all other effects of the stackmap
62 // value.
63 LateColdAny,
64
65 // As an input representation, this means that B3 should pick some register. It could be a
66 // register that this claims to clobber!
67 SomeRegister,
68
69 // As an input representation, this means that B3 should pick some register but that this
70 // register is then cobbered with garbage. This only works for patchpoints.
71 SomeRegisterWithClobber,
72
73 // As an input representation, this tells us that B3 should pick some register, but implies
74 // that the def happens before any of the effects of the stackmap. This is only valid for
75 // the result constraint of a Patchpoint.
76 SomeEarlyRegister,
77
78 // As an input representation, this forces a particular register. As an output
79 // representation, this tells us what register B3 picked.
80 Register,
81
82 // As an input representation, this forces a particular register and states that
83 // the register is used late. This means that the register is used after the result
84 // is defined (i.e, the result will interfere with this as an input).
85 // It's not a valid output representation.
86 LateRegister,
87
88 // As an output representation, this tells us what stack slot B3 picked. It's not a valid
89 // input representation.
90 Stack,
91
92 // As an input representation, this forces the value to end up in the argument area at some
93 // offset.
94 StackArgument,
95
96 // As an output representation, this tells us that B3 constant-folded the value.
97 Constant
98 };
99
100 ValueRep()
101 : m_kind(WarmAny)
102 {
103 }
104
105 explicit ValueRep(Reg reg)
106 : m_kind(Register)
107 {
108 u.reg = reg;
109 }
110
111 ValueRep(Kind kind)
112 : m_kind(kind)
113 {
114 ASSERT(kind == WarmAny || kind == ColdAny || kind == LateColdAny || kind == SomeRegister || kind == SomeRegisterWithClobber || kind == SomeEarlyRegister);
115 }
116
117 static ValueRep reg(Reg reg)
118 {
119 return ValueRep(reg);
120 }
121
122 static ValueRep lateReg(Reg reg)
123 {
124 ValueRep result(reg);
125 result.m_kind = LateRegister;
126 return result;
127 }
128
129 static ValueRep stack(intptr_t offsetFromFP)
130 {
131 ValueRep result;
132 result.m_kind = Stack;
133 result.u.offsetFromFP = offsetFromFP;
134 return result;
135 }
136
137 static ValueRep stackArgument(intptr_t offsetFromSP)
138 {
139 ValueRep result;
140 result.m_kind = StackArgument;
141 result.u.offsetFromSP = offsetFromSP;
142 return result;
143 }
144
145 static ValueRep constant(int64_t value)
146 {
147 ValueRep result;
148 result.m_kind = Constant;
149 result.u.value = value;
150 return result;
151 }
152
153 static ValueRep constantDouble(double value)
154 {
155 return ValueRep::constant(bitwise_cast<int64_t>(value));
156 }
157
158 Kind kind() const { return m_kind; }
159
160 bool operator==(const ValueRep& other) const
161 {
162 if (kind() != other.kind())
163 return false;
164 switch (kind()) {
165 case LateRegister:
166 case Register:
167 return u.reg == other.u.reg;
168 case Stack:
169 return u.offsetFromFP == other.u.offsetFromFP;
170 case StackArgument:
171 return u.offsetFromSP == other.u.offsetFromSP;
172 case Constant:
173 return u.value == other.u.value;
174 default:
175 return true;
176 }
177 }
178
179 bool operator!=(const ValueRep& other) const
180 {
181 return !(*this == other);
182 }
183
184 explicit operator bool() const { return kind() != WarmAny; }
185
186 bool isAny() const { return kind() == WarmAny || kind() == ColdAny || kind() == LateColdAny; }
187
188 bool isReg() const { return kind() == Register || kind() == LateRegister; }
189
190 Reg reg() const
191 {
192 ASSERT(isReg());
193 return u.reg;
194 }
195
196 bool isGPR() const { return isReg() && reg().isGPR(); }
197 bool isFPR() const { return isReg() && reg().isFPR(); }
198
199 GPRReg gpr() const { return reg().gpr(); }
200 FPRReg fpr() const { return reg().fpr(); }
201
202 bool isStack() const { return kind() == Stack; }
203
204 intptr_t offsetFromFP() const
205 {
206 ASSERT(isStack());
207 return u.offsetFromFP;
208 }
209
210 bool isStackArgument() const { return kind() == StackArgument; }
211
212 intptr_t offsetFromSP() const
213 {
214 ASSERT(isStackArgument());
215 return u.offsetFromSP;
216 }
217
218 bool isConstant() const { return kind() == Constant; }
219
220 int64_t value() const
221 {
222 ASSERT(isConstant());
223 return u.value;
224 }
225
226 double doubleValue() const
227 {
228 return bitwise_cast<double>(value());
229 }
230
231 ValueRep withOffset(intptr_t offset) const
232 {
233 switch (kind()) {
234 case Stack:
235 return stack(offsetFromFP() + offset);
236 case StackArgument:
237 return stackArgument(offsetFromSP() + offset);
238 default:
239 return *this;
240 }
241 }
242
243 void addUsedRegistersTo(RegisterSet&) const;
244
245 RegisterSet usedRegisters() const;
246
247 // Get the used registers for a vector of ValueReps.
248 template<typename VectorType>
249 static RegisterSet usedRegisters(const VectorType& vector)
250 {
251 RegisterSet result;
252 for (const ValueRep& value : vector)
253 value.addUsedRegistersTo(result);
254 return result;
255 }
256
257 JS_EXPORT_PRIVATE void dump(PrintStream&) const;
258
259 // This has a simple contract: it emits code to restore the value into the given register. This
260 // will work even if it requires moving between bits a GPR and a FPR.
261 void emitRestore(AssemblyHelpers&, Reg) const;
262
263 // Computes the ValueRecovery assuming that the Value* was for a JSValue (i.e. Int64).
264 // NOTE: We should avoid putting JSValue-related methods in B3, but this was hard to avoid
265 // because some parts of JSC use ValueRecovery like a general "where my bits at" object, almost
266 // exactly like ValueRep.
267 ValueRecovery recoveryForJSValue() const;
268
269private:
270 Kind m_kind;
271 union U {
272 Reg reg;
273 intptr_t offsetFromFP;
274 intptr_t offsetFromSP;
275 int64_t value;
276
277 U()
278 {
279 memset(static_cast<void*>(this), 0, sizeof(*this));
280 }
281 } u;
282};
283
284} } // namespace JSC::B3
285
286namespace WTF {
287
288void printInternal(PrintStream&, JSC::B3::ValueRep::Kind);
289
290} // namespace WTF
291
292#endif // ENABLE(B3_JIT)
293