2 * Copyright (C) 2011, 2012, 2013 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
)
57 m_arguments
.fill(Traits::defaultValue(), numArguments
);
58 m_locals
.fill(Traits::defaultValue(), 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
)
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
] = Traits::defaultValue();
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
)); }
153 bool hasOperand(int operand
) const
155 if (operandIsArgument(operand
))
157 return static_cast<size_t>(VirtualRegister(operand
).toLocal()) < numberOfLocals();
159 bool hasOperand(VirtualRegister reg
) const
161 return hasOperand(reg
.offset());
164 void setOperand(int operand
, const T
& value
)
166 if (operandIsArgument(operand
)) {
167 int argument
= VirtualRegister(operand
).toArgument();
168 m_arguments
[argument
] = value
;
172 setLocal(VirtualRegister(operand
).toLocal(), value
);
175 void setOperand(VirtualRegister virtualRegister
, const T
& value
)
177 setOperand(virtualRegister
.offset(), value
);
180 size_t size() const { return numberOfArguments() + numberOfLocals(); }
181 const T
& at(size_t index
) const
183 if (index
< numberOfArguments())
184 return m_arguments
[index
];
185 return m_locals
[index
- numberOfArguments()];
189 if (index
< numberOfArguments())
190 return m_arguments
[index
];
191 return m_locals
[index
- numberOfArguments()];
193 const T
& operator[](size_t index
) const { return at(index
); }
194 T
& operator[](size_t index
) { return at(index
); }
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
202 int variableForIndex(size_t index
) const
204 return index
- m_arguments
.size();
206 int operandForIndex(size_t index
) const
208 if (index
< numberOfArguments())
209 return virtualRegisterForArgument(index
).offset();
210 return virtualRegisterForLocal(index
- numberOfArguments()).offset();
212 size_t indexForOperand(int operand
) const
214 if (operandIsArgument(operand
))
215 return static_cast<size_t>(VirtualRegister(operand
).toArgument());
216 return static_cast<size_t>(VirtualRegister(operand
).toLocal()) + numberOfArguments();
218 size_t indexForOperand(VirtualRegister reg
) const
220 return indexForOperand(reg
.offset());
223 void setOperandFirstTime(int operand
, const T
& value
)
225 if (operandIsArgument(operand
)) {
226 setArgumentFirstTime(VirtualRegister(operand
).toArgument(), value
);
230 setLocalFirstTime(VirtualRegister(operand
).toLocal(), value
);
235 for (size_t i
= 0; i
< m_arguments
.size(); ++i
)
236 m_arguments
[i
] = value
;
237 for (size_t i
= 0; i
< m_locals
.size(); ++i
)
243 fill(Traits::defaultValue());
246 bool operator==(const Operands
& other
) const
248 ASSERT(numberOfArguments() == other
.numberOfArguments());
249 ASSERT(numberOfLocals() == other
.numberOfLocals());
251 return m_arguments
== other
.m_arguments
&& m_locals
== other
.m_locals
;
254 void dumpInContext(PrintStream
& out
, DumpContext
* context
) const;
256 void dump(PrintStream
& out
) const
258 dumpInContext(out
, 0);
262 Vector
<T
, 8> m_arguments
;
263 Vector
<T
, 16> m_locals
;