]>
Commit | Line | Data |
---|---|---|
6fe7ccc8 | 1 | /* |
81345200 | 2 | * Copyright (C) 2011, 2012, 2013 Apple Inc. All rights reserved. |
6fe7ccc8 A |
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" | |
81345200 A |
31 | #include "VirtualRegister.h" |
32 | ||
93a37866 | 33 | #include <wtf/PrintStream.h> |
6fe7ccc8 A |
34 | #include <wtf/Vector.h> |
35 | ||
36 | namespace JSC { | |
37 | ||
6fe7ccc8 A |
38 | template<typename T> struct OperandValueTraits; |
39 | ||
40 | template<typename T> | |
41 | struct OperandValueTraits { | |
42 | static T defaultValue() { return T(); } | |
81345200 | 43 | static bool isEmptyForDump(const T& value) { return !value; } |
6fe7ccc8 A |
44 | }; |
45 | ||
93a37866 A |
46 | enum OperandKind { ArgumentOperand, LocalOperand }; |
47 | ||
81345200 A |
48 | enum OperandsLikeTag { OperandsLike }; |
49 | ||
50 | template<typename T, typename Traits = OperandValueTraits<T>> | |
6fe7ccc8 A |
51 | class Operands { |
52 | public: | |
53 | Operands() { } | |
54 | ||
55 | explicit Operands(size_t numArguments, size_t numLocals) | |
56 | { | |
57 | m_arguments.fill(Traits::defaultValue(), numArguments); | |
58 | m_locals.fill(Traits::defaultValue(), numLocals); | |
59 | } | |
60 | ||
81345200 A |
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 | ||
6fe7ccc8 A |
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 | ||
93a37866 A |
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 | ||
6fe7ccc8 A |
99 | void ensureLocals(size_t size) |
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] = Traits::defaultValue(); | |
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)) { | |
81345200 | 139 | int argument = VirtualRegister(operand).toArgument(); |
6fe7ccc8 A |
140 | return m_arguments[argument]; |
141 | } | |
81345200 A |
142 | |
143 | return m_locals[VirtualRegister(operand).toLocal()]; | |
6fe7ccc8 | 144 | } |
81345200 A |
145 | |
146 | T& operand(VirtualRegister virtualRegister) | |
147 | { | |
148 | return operand(virtualRegister.offset()); | |
149 | } | |
150 | ||
6fe7ccc8 A |
151 | const T& operand(int operand) const { return const_cast<const T&>(const_cast<Operands*>(this)->operand(operand)); } |
152 | ||
93a37866 A |
153 | bool hasOperand(int operand) const |
154 | { | |
155 | if (operandIsArgument(operand)) | |
156 | return true; | |
81345200 A |
157 | return static_cast<size_t>(VirtualRegister(operand).toLocal()) < numberOfLocals(); |
158 | } | |
159 | bool hasOperand(VirtualRegister reg) const | |
160 | { | |
161 | return hasOperand(reg.offset()); | |
93a37866 A |
162 | } |
163 | ||
6fe7ccc8 A |
164 | void setOperand(int operand, const T& value) |
165 | { | |
166 | if (operandIsArgument(operand)) { | |
81345200 | 167 | int argument = VirtualRegister(operand).toArgument(); |
6fe7ccc8 A |
168 | m_arguments[argument] = value; |
169 | return; | |
170 | } | |
171 | ||
81345200 | 172 | setLocal(VirtualRegister(operand).toLocal(), value); |
6fe7ccc8 A |
173 | } |
174 | ||
81345200 A |
175 | void setOperand(VirtualRegister virtualRegister, const T& value) |
176 | { | |
177 | setOperand(virtualRegister.offset(), value); | |
178 | } | |
179 | ||
93a37866 A |
180 | size_t size() const { return numberOfArguments() + numberOfLocals(); } |
181 | const T& at(size_t index) const | |
182 | { | |
183 | if (index < numberOfArguments()) | |
184 | return m_arguments[index]; | |
185 | return m_locals[index - numberOfArguments()]; | |
186 | } | |
187 | T& at(size_t index) | |
188 | { | |
189 | if (index < numberOfArguments()) | |
190 | return m_arguments[index]; | |
191 | return m_locals[index - numberOfArguments()]; | |
192 | } | |
193 | const T& operator[](size_t index) const { return at(index); } | |
194 | T& operator[](size_t index) { return at(index); } | |
195 | ||
196 | bool isArgument(size_t index) const { return index < numberOfArguments(); } | |
197 | bool isVariable(size_t index) const { return !isArgument(index); } | |
198 | int argumentForIndex(size_t index) const | |
199 | { | |
200 | return index; | |
201 | } | |
202 | int variableForIndex(size_t index) const | |
203 | { | |
204 | return index - m_arguments.size(); | |
205 | } | |
206 | int operandForIndex(size_t index) const | |
207 | { | |
208 | if (index < numberOfArguments()) | |
81345200 A |
209 | return virtualRegisterForArgument(index).offset(); |
210 | return virtualRegisterForLocal(index - numberOfArguments()).offset(); | |
211 | } | |
212 | size_t indexForOperand(int operand) const | |
213 | { | |
214 | if (operandIsArgument(operand)) | |
215 | return static_cast<size_t>(VirtualRegister(operand).toArgument()); | |
216 | return static_cast<size_t>(VirtualRegister(operand).toLocal()) + numberOfArguments(); | |
217 | } | |
218 | size_t indexForOperand(VirtualRegister reg) const | |
219 | { | |
220 | return indexForOperand(reg.offset()); | |
93a37866 A |
221 | } |
222 | ||
223 | void setOperandFirstTime(int operand, const T& value) | |
224 | { | |
225 | if (operandIsArgument(operand)) { | |
81345200 | 226 | setArgumentFirstTime(VirtualRegister(operand).toArgument(), value); |
93a37866 A |
227 | return; |
228 | } | |
229 | ||
81345200 | 230 | setLocalFirstTime(VirtualRegister(operand).toLocal(), value); |
93a37866 A |
231 | } |
232 | ||
81345200 | 233 | void fill(T value) |
6fe7ccc8 A |
234 | { |
235 | for (size_t i = 0; i < m_arguments.size(); ++i) | |
81345200 | 236 | m_arguments[i] = value; |
6fe7ccc8 | 237 | for (size_t i = 0; i < m_locals.size(); ++i) |
81345200 A |
238 | m_locals[i] = value; |
239 | } | |
240 | ||
241 | void clear() | |
242 | { | |
243 | fill(Traits::defaultValue()); | |
244 | } | |
245 | ||
246 | bool operator==(const Operands& other) const | |
247 | { | |
248 | ASSERT(numberOfArguments() == other.numberOfArguments()); | |
249 | ASSERT(numberOfLocals() == other.numberOfLocals()); | |
250 | ||
251 | return m_arguments == other.m_arguments && m_locals == other.m_locals; | |
252 | } | |
253 | ||
254 | void dumpInContext(PrintStream& out, DumpContext* context) const; | |
255 | ||
256 | void dump(PrintStream& out) const | |
257 | { | |
258 | dumpInContext(out, 0); | |
6fe7ccc8 A |
259 | } |
260 | ||
261 | private: | |
262 | Vector<T, 8> m_arguments; | |
263 | Vector<T, 16> m_locals; | |
264 | }; | |
265 | ||
6fe7ccc8 A |
266 | } // namespace JSC |
267 | ||
268 | #endif // Operands_h | |
269 |