]> git.saurik.com Git - apple/javascriptcore.git/blob - bytecode/Operands.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / bytecode / Operands.h
1 /*
2 * Copyright (C) 2011, 2012, 2013, 2015 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 Operands_h
27 #define Operands_h
28
29 #include "CallFrame.h"
30 #include "JSObject.h"
31 #include "VirtualRegister.h"
32
33 #include <wtf/PrintStream.h>
34 #include <wtf/Vector.h>
35
36 namespace JSC {
37
38 template<typename T> struct OperandValueTraits;
39
40 template<typename T>
41 struct OperandValueTraits {
42 static T defaultValue() { return T(); }
43 static bool isEmptyForDump(const T& value) { return !value; }
44 };
45
46 enum OperandKind { ArgumentOperand, LocalOperand };
47
48 enum OperandsLikeTag { OperandsLike };
49
50 template<typename T, typename Traits = OperandValueTraits<T>>
51 class Operands {
52 public:
53 Operands() { }
54
55 explicit Operands(size_t numArguments, size_t numLocals, const T& initialValue = Traits::defaultValue())
56 {
57 m_arguments.fill(initialValue, numArguments);
58 m_locals.fill(initialValue, numLocals);
59 }
60
61 template<typename U, typename OtherTraits>
62 explicit Operands(OperandsLikeTag, const Operands<U, OtherTraits>& other)
63 {
64 m_arguments.fill(Traits::defaultValue(), other.numberOfArguments());
65 m_locals.fill(Traits::defaultValue(), other.numberOfLocals());
66 }
67
68 size_t numberOfArguments() const { return m_arguments.size(); }
69 size_t numberOfLocals() const { return m_locals.size(); }
70
71 T& argument(size_t idx) { return m_arguments[idx]; }
72 const T& argument(size_t idx) const { return m_arguments[idx]; }
73
74 T& local(size_t idx) { return m_locals[idx]; }
75 const T& local(size_t idx) const { return m_locals[idx]; }
76
77 template<OperandKind operandKind>
78 size_t sizeFor() const
79 {
80 if (operandKind == ArgumentOperand)
81 return numberOfArguments();
82 return numberOfLocals();
83 }
84 template<OperandKind operandKind>
85 T& atFor(size_t idx)
86 {
87 if (operandKind == ArgumentOperand)
88 return argument(idx);
89 return local(idx);
90 }
91 template<OperandKind operandKind>
92 const T& atFor(size_t idx) const
93 {
94 if (operandKind == ArgumentOperand)
95 return argument(idx);
96 return local(idx);
97 }
98
99 void ensureLocals(size_t size, const T& ensuredValue = Traits::defaultValue())
100 {
101 if (size <= m_locals.size())
102 return;
103
104 size_t oldSize = m_locals.size();
105 m_locals.resize(size);
106 for (size_t i = oldSize; i < m_locals.size(); ++i)
107 m_locals[i] = ensuredValue;
108 }
109
110 void setLocal(size_t idx, const T& value)
111 {
112 ensureLocals(idx + 1);
113
114 m_locals[idx] = value;
115 }
116
117 T getLocal(size_t idx)
118 {
119 if (idx >= m_locals.size())
120 return Traits::defaultValue();
121 return m_locals[idx];
122 }
123
124 void setArgumentFirstTime(size_t idx, const T& value)
125 {
126 ASSERT(m_arguments[idx] == Traits::defaultValue());
127 argument(idx) = value;
128 }
129
130 void setLocalFirstTime(size_t idx, const T& value)
131 {
132 ASSERT(idx >= m_locals.size() || m_locals[idx] == Traits::defaultValue());
133 setLocal(idx, value);
134 }
135
136 T& operand(int operand)
137 {
138 if (operandIsArgument(operand)) {
139 int argument = VirtualRegister(operand).toArgument();
140 return m_arguments[argument];
141 }
142
143 return m_locals[VirtualRegister(operand).toLocal()];
144 }
145
146 T& operand(VirtualRegister virtualRegister)
147 {
148 return operand(virtualRegister.offset());
149 }
150
151 const T& operand(int operand) const { return const_cast<const T&>(const_cast<Operands*>(this)->operand(operand)); }
152 const T& operand(VirtualRegister operand) const { return const_cast<const T&>(const_cast<Operands*>(this)->operand(operand)); }
153
154 bool hasOperand(int operand) const
155 {
156 if (operandIsArgument(operand))
157 return true;
158 return static_cast<size_t>(VirtualRegister(operand).toLocal()) < numberOfLocals();
159 }
160 bool hasOperand(VirtualRegister reg) const
161 {
162 return hasOperand(reg.offset());
163 }
164
165 void setOperand(int operand, const T& value)
166 {
167 if (operandIsArgument(operand)) {
168 int argument = VirtualRegister(operand).toArgument();
169 m_arguments[argument] = value;
170 return;
171 }
172
173 setLocal(VirtualRegister(operand).toLocal(), value);
174 }
175
176 void setOperand(VirtualRegister virtualRegister, const T& value)
177 {
178 setOperand(virtualRegister.offset(), value);
179 }
180
181 size_t size() const { return numberOfArguments() + numberOfLocals(); }
182 const T& at(size_t index) const
183 {
184 if (index < numberOfArguments())
185 return m_arguments[index];
186 return m_locals[index - numberOfArguments()];
187 }
188 T& at(size_t index)
189 {
190 if (index < numberOfArguments())
191 return m_arguments[index];
192 return m_locals[index - numberOfArguments()];
193 }
194 const T& operator[](size_t index) const { return at(index); }
195 T& operator[](size_t index) { return at(index); }
196
197 bool isArgument(size_t index) const { return index < numberOfArguments(); }
198 bool isVariable(size_t index) const { return !isArgument(index); }
199 int argumentForIndex(size_t index) const
200 {
201 return index;
202 }
203 int variableForIndex(size_t index) const
204 {
205 return index - m_arguments.size();
206 }
207 int operandForIndex(size_t index) const
208 {
209 if (index < numberOfArguments())
210 return virtualRegisterForArgument(index).offset();
211 return virtualRegisterForLocal(index - numberOfArguments()).offset();
212 }
213 VirtualRegister virtualRegisterForIndex(size_t index) const
214 {
215 return VirtualRegister(operandForIndex(index));
216 }
217 size_t indexForOperand(int operand) const
218 {
219 if (operandIsArgument(operand))
220 return static_cast<size_t>(VirtualRegister(operand).toArgument());
221 return static_cast<size_t>(VirtualRegister(operand).toLocal()) + numberOfArguments();
222 }
223 size_t indexForOperand(VirtualRegister reg) const
224 {
225 return indexForOperand(reg.offset());
226 }
227
228 void setOperandFirstTime(int operand, const T& value)
229 {
230 if (operandIsArgument(operand)) {
231 setArgumentFirstTime(VirtualRegister(operand).toArgument(), value);
232 return;
233 }
234
235 setLocalFirstTime(VirtualRegister(operand).toLocal(), value);
236 }
237
238 void fill(T value)
239 {
240 for (size_t i = 0; i < m_arguments.size(); ++i)
241 m_arguments[i] = value;
242 for (size_t i = 0; i < m_locals.size(); ++i)
243 m_locals[i] = value;
244 }
245
246 void clear()
247 {
248 fill(Traits::defaultValue());
249 }
250
251 bool operator==(const Operands& other) const
252 {
253 ASSERT(numberOfArguments() == other.numberOfArguments());
254 ASSERT(numberOfLocals() == other.numberOfLocals());
255
256 return m_arguments == other.m_arguments && m_locals == other.m_locals;
257 }
258
259 void dumpInContext(PrintStream& out, DumpContext* context) const;
260 void dump(PrintStream& out) const;
261
262 private:
263 Vector<T, 8> m_arguments;
264 Vector<T, 16> m_locals;
265 };
266
267 } // namespace JSC
268
269 #endif // Operands_h
270