Operands.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (C) 2011, 2012 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. #ifndef Operands_h
  26. #define Operands_h
  27. #include "CallFrame.h"
  28. #include "JSObject.h"
  29. #include <wtf/PrintStream.h>
  30. #include <wtf/Vector.h>
  31. namespace JSC {
  32. // argument 0 is 'this'.
  33. inline bool operandIsArgument(int operand) { return operand < 0; }
  34. inline int operandToArgument(int operand) { return -operand + CallFrame::thisArgumentOffset(); }
  35. inline int argumentToOperand(int argument) { return -argument + CallFrame::thisArgumentOffset(); }
  36. template<typename T> struct OperandValueTraits;
  37. template<typename T>
  38. struct OperandValueTraits {
  39. static T defaultValue() { return T(); }
  40. static void dump(const T& value, PrintStream& out) { value.dump(out); }
  41. };
  42. enum OperandKind { ArgumentOperand, LocalOperand };
  43. template<typename T, typename Traits = OperandValueTraits<T>, bool shared = false>
  44. class Operands {
  45. public:
  46. Operands() { }
  47. explicit Operands(size_t numArguments, size_t numLocals)
  48. {
  49. m_arguments.fill(Traits::defaultValue(), numArguments);
  50. m_locals.fill(Traits::defaultValue(), numLocals);
  51. }
  52. size_t numberOfArguments() const { return m_arguments.size(); }
  53. size_t numberOfLocals() const { return m_locals.size(); }
  54. T& argument(size_t idx) { return m_arguments[idx]; }
  55. const T& argument(size_t idx) const { return m_arguments[idx]; }
  56. T& local(size_t idx) { return m_locals[idx]; }
  57. const T& local(size_t idx) const { return m_locals[idx]; }
  58. template<OperandKind operandKind>
  59. size_t sizeFor() const
  60. {
  61. if (operandKind == ArgumentOperand)
  62. return numberOfArguments();
  63. return numberOfLocals();
  64. }
  65. template<OperandKind operandKind>
  66. T& atFor(size_t idx)
  67. {
  68. if (operandKind == ArgumentOperand)
  69. return argument(idx);
  70. return local(idx);
  71. }
  72. template<OperandKind operandKind>
  73. const T& atFor(size_t idx) const
  74. {
  75. if (operandKind == ArgumentOperand)
  76. return argument(idx);
  77. return local(idx);
  78. }
  79. void ensureLocals(size_t size)
  80. {
  81. if (size <= m_locals.size())
  82. return;
  83. size_t oldSize = m_locals.size();
  84. m_locals.resize(size);
  85. for (size_t i = oldSize; i < m_locals.size(); ++i)
  86. m_locals[i] = Traits::defaultValue();
  87. }
  88. void setLocal(size_t idx, const T& value)
  89. {
  90. ensureLocals(idx + 1);
  91. m_locals[idx] = value;
  92. }
  93. T getLocal(size_t idx)
  94. {
  95. if (idx >= m_locals.size())
  96. return Traits::defaultValue();
  97. return m_locals[idx];
  98. }
  99. void setArgumentFirstTime(size_t idx, const T& value)
  100. {
  101. ASSERT(m_arguments[idx] == Traits::defaultValue());
  102. argument(idx) = value;
  103. }
  104. void setLocalFirstTime(size_t idx, const T& value)
  105. {
  106. ASSERT(idx >= m_locals.size() || m_locals[idx] == Traits::defaultValue());
  107. setLocal(idx, value);
  108. }
  109. T& operand(int operand)
  110. {
  111. if (operandIsArgument(operand)) {
  112. int argument = operandToArgument(operand);
  113. return m_arguments[argument];
  114. }
  115. return m_locals[operand];
  116. }
  117. const T& operand(int operand) const { return const_cast<const T&>(const_cast<Operands*>(this)->operand(operand)); }
  118. bool hasOperand(int operand) const
  119. {
  120. if (operandIsArgument(operand))
  121. return true;
  122. return static_cast<size_t>(operand) < numberOfLocals();
  123. }
  124. void setOperand(int operand, const T& value)
  125. {
  126. if (operandIsArgument(operand)) {
  127. int argument = operandToArgument(operand);
  128. m_arguments[argument] = value;
  129. return;
  130. }
  131. setLocal(operand, value);
  132. }
  133. size_t size() const { return numberOfArguments() + numberOfLocals(); }
  134. const T& at(size_t index) const
  135. {
  136. if (index < numberOfArguments())
  137. return m_arguments[index];
  138. return m_locals[index - numberOfArguments()];
  139. }
  140. T& at(size_t index)
  141. {
  142. if (index < numberOfArguments())
  143. return m_arguments[index];
  144. return m_locals[index - numberOfArguments()];
  145. }
  146. const T& operator[](size_t index) const { return at(index); }
  147. T& operator[](size_t index) { return at(index); }
  148. bool isArgument(size_t index) const { return index < numberOfArguments(); }
  149. bool isVariable(size_t index) const { return !isArgument(index); }
  150. int argumentForIndex(size_t index) const
  151. {
  152. return index;
  153. }
  154. int variableForIndex(size_t index) const
  155. {
  156. return index - m_arguments.size();
  157. }
  158. int operandForIndex(size_t index) const
  159. {
  160. if (index < numberOfArguments())
  161. return argumentToOperand(index);
  162. return index - numberOfArguments();
  163. }
  164. void setOperandFirstTime(int operand, const T& value)
  165. {
  166. if (operandIsArgument(operand)) {
  167. setArgumentFirstTime(operandToArgument(operand), value);
  168. return;
  169. }
  170. setLocalFirstTime(operand, value);
  171. }
  172. void clear()
  173. {
  174. for (size_t i = 0; i < m_arguments.size(); ++i)
  175. m_arguments[i] = Traits::defaultValue();
  176. for (size_t i = 0; i < m_locals.size(); ++i)
  177. m_locals[i] = Traits::defaultValue();
  178. }
  179. private:
  180. Vector<T, 8, WTF::CrashOnOverflow, shared> m_arguments;
  181. Vector<T, 16, WTF::CrashOnOverflow, shared> m_locals;
  182. };
  183. #if ENABLE(DETACHED_JIT)
  184. template<typename T, typename Traits = OperandValueTraits<T> > using Operands_shared = Operands<T, Traits, true>;
  185. #else
  186. template<typename T, typename Traits = OperandValueTraits<T> > using Operands_shared = Operands<T, Traits, false>;
  187. #endif
  188. template<typename T, typename Traits>
  189. void dumpOperands(const Operands_shared<T, Traits>& operands, PrintStream& out)
  190. {
  191. for (size_t argument = operands.numberOfArguments(); argument--;) {
  192. if (argument != operands.numberOfArguments() - 1)
  193. out.printf(" ");
  194. out.print("arg", argument, ":");
  195. Traits::dump(operands.argument(argument), out);
  196. }
  197. out.printf(" : ");
  198. for (size_t local = 0; local < operands.numberOfLocals(); ++local) {
  199. if (local)
  200. out.printf(" ");
  201. out.print("r", local, ":");
  202. Traits::dump(operands.local(local), out);
  203. }
  204. }
  205. } // namespace JSC
  206. #endif // Operands_h