2 * Copyright (C) 2011, 2012, 2013, 2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #include "CallFrame.h"
31 #include "VirtualRegister.h"
33 #include <wtf/PrintStream.h>
34 #include <wtf/Vector.h>
38 template<typename T
> struct OperandValueTraits
;
41 struct OperandValueTraits
{
42 static T
defaultValue() { return T(); }
43 static bool isEmptyForDump(const T
& value
) { return !value
; }
46 enum OperandKind
{ ArgumentOperand
, LocalOperand
};
48 enum OperandsLikeTag
{ OperandsLike
};
50 template<typename T
, typename Traits
= OperandValueTraits
<T
>>
55 explicit Operands(size_t numArguments
, size_t numLocals
, const T
& initialValue
= Traits::defaultValue())
57 m_arguments
.fill(initialValue
, numArguments
);
58 m_locals
.fill(initialValue
, numLocals
);
61 template<typename U
, typename OtherTraits
>
62 explicit Operands(OperandsLikeTag
, const Operands
<U
, OtherTraits
>& other
)
64 m_arguments
.fill(Traits::defaultValue(), other
.numberOfArguments());
65 m_locals
.fill(Traits::defaultValue(), other
.numberOfLocals());
68 size_t numberOfArguments() const { return m_arguments
.size(); }
69 size_t numberOfLocals() const { return m_locals
.size(); }
71 T
& argument(size_t idx
) { return m_arguments
[idx
]; }
72 const T
& argument(size_t idx
) const { return m_arguments
[idx
]; }
74 T
& local(size_t idx
) { return m_locals
[idx
]; }
75 const T
& local(size_t idx
) const { return m_locals
[idx
]; }
77 template<OperandKind operandKind
>
78 size_t sizeFor() const
80 if (operandKind
== ArgumentOperand
)
81 return numberOfArguments();
82 return numberOfLocals();
84 template<OperandKind operandKind
>
87 if (operandKind
== ArgumentOperand
)
91 template<OperandKind operandKind
>
92 const T
& atFor(size_t idx
) const
94 if (operandKind
== ArgumentOperand
)
99 void ensureLocals(size_t size
, const T
& ensuredValue
= Traits::defaultValue())
101 if (size
<= m_locals
.size())
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
;
110 void setLocal(size_t idx
, const T
& value
)
112 ensureLocals(idx
+ 1);
114 m_locals
[idx
] = value
;
117 T
getLocal(size_t idx
)
119 if (idx
>= m_locals
.size())
120 return Traits::defaultValue();
121 return m_locals
[idx
];
124 void setArgumentFirstTime(size_t idx
, const T
& value
)
126 ASSERT(m_arguments
[idx
] == Traits::defaultValue());
127 argument(idx
) = value
;
130 void setLocalFirstTime(size_t idx
, const T
& value
)
132 ASSERT(idx
>= m_locals
.size() || m_locals
[idx
] == Traits::defaultValue());
133 setLocal(idx
, value
);
136 T
& operand(int operand
)
138 if (operandIsArgument(operand
)) {
139 int argument
= VirtualRegister(operand
).toArgument();
140 return m_arguments
[argument
];
143 return m_locals
[VirtualRegister(operand
).toLocal()];
146 T
& operand(VirtualRegister virtualRegister
)
148 return operand(virtualRegister
.offset());
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
)); }
154 bool hasOperand(int operand
) const
156 if (operandIsArgument(operand
))
158 return static_cast<size_t>(VirtualRegister(operand
).toLocal()) < numberOfLocals();
160 bool hasOperand(VirtualRegister reg
) const
162 return hasOperand(reg
.offset());
165 void setOperand(int operand
, const T
& value
)
167 if (operandIsArgument(operand
)) {
168 int argument
= VirtualRegister(operand
).toArgument();
169 m_arguments
[argument
] = value
;
173 setLocal(VirtualRegister(operand
).toLocal(), value
);
176 void setOperand(VirtualRegister virtualRegister
, const T
& value
)
178 setOperand(virtualRegister
.offset(), value
);
181 size_t size() const { return numberOfArguments() + numberOfLocals(); }
182 const T
& at(size_t index
) const
184 if (index
< numberOfArguments())
185 return m_arguments
[index
];
186 return m_locals
[index
- numberOfArguments()];
190 if (index
< numberOfArguments())
191 return m_arguments
[index
];
192 return m_locals
[index
- numberOfArguments()];
194 const T
& operator[](size_t index
) const { return at(index
); }
195 T
& operator[](size_t index
) { return at(index
); }
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
203 int variableForIndex(size_t index
) const
205 return index
- m_arguments
.size();
207 int operandForIndex(size_t index
) const
209 if (index
< numberOfArguments())
210 return virtualRegisterForArgument(index
).offset();
211 return virtualRegisterForLocal(index
- numberOfArguments()).offset();
213 VirtualRegister
virtualRegisterForIndex(size_t index
) const
215 return VirtualRegister(operandForIndex(index
));
217 size_t indexForOperand(int operand
) const
219 if (operandIsArgument(operand
))
220 return static_cast<size_t>(VirtualRegister(operand
).toArgument());
221 return static_cast<size_t>(VirtualRegister(operand
).toLocal()) + numberOfArguments();
223 size_t indexForOperand(VirtualRegister reg
) const
225 return indexForOperand(reg
.offset());
228 void setOperandFirstTime(int operand
, const T
& value
)
230 if (operandIsArgument(operand
)) {
231 setArgumentFirstTime(VirtualRegister(operand
).toArgument(), value
);
235 setLocalFirstTime(VirtualRegister(operand
).toLocal(), value
);
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
)
248 fill(Traits::defaultValue());
251 bool operator==(const Operands
& other
) const
253 ASSERT(numberOfArguments() == other
.numberOfArguments());
254 ASSERT(numberOfLocals() == other
.numberOfLocals());
256 return m_arguments
== other
.m_arguments
&& m_locals
== other
.m_locals
;
259 void dumpInContext(PrintStream
& out
, DumpContext
* context
) const;
260 void dump(PrintStream
& out
) const;
263 Vector
<T
, 8> m_arguments
;
264 Vector
<T
, 16> m_locals
;