DataFormat.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (C) 2011 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 DataFormat_h
  26. #define DataFormat_h
  27. #include <wtf/Assertions.h>
  28. namespace JSC {
  29. // === DataFormat ===
  30. //
  31. // This enum tracks the current representation in which a value is being held.
  32. // Values may be unboxed primitives (int32, double, or cell), or boxed as a JSValue.
  33. // For boxed values, we may know the type of boxing that has taken place.
  34. // (May also need bool, array, object, string types!)
  35. enum DataFormat {
  36. DataFormatNone = 0,
  37. DataFormatInteger = 1,
  38. DataFormatDouble = 2,
  39. DataFormatBoolean = 3,
  40. DataFormatCell = 4,
  41. DataFormatStorage = 5,
  42. DataFormatJS = 8,
  43. DataFormatJSInteger = DataFormatJS | DataFormatInteger,
  44. DataFormatJSDouble = DataFormatJS | DataFormatDouble,
  45. DataFormatJSCell = DataFormatJS | DataFormatCell,
  46. DataFormatJSBoolean = DataFormatJS | DataFormatBoolean,
  47. // Marker deliminating ordinary data formats and OSR-only data formats.
  48. DataFormatOSRMarker = 32,
  49. // Special data formats used only for OSR.
  50. DataFormatDead = 33, // Implies jsUndefined().
  51. DataFormatArguments = 34 // Implies that the arguments object must be reified.
  52. };
  53. inline const char* dataFormatToString(DataFormat dataFormat)
  54. {
  55. switch (dataFormat) {
  56. case DataFormatNone:
  57. return "None";
  58. case DataFormatInteger:
  59. return "Integer";
  60. case DataFormatDouble:
  61. return "Double";
  62. case DataFormatCell:
  63. return "Cell";
  64. case DataFormatBoolean:
  65. return "Boolean";
  66. case DataFormatStorage:
  67. return "Storage";
  68. case DataFormatJS:
  69. return "JS";
  70. case DataFormatJSInteger:
  71. return "JSInteger";
  72. case DataFormatJSDouble:
  73. return "JSDouble";
  74. case DataFormatJSCell:
  75. return "JSCell";
  76. case DataFormatJSBoolean:
  77. return "JSBoolean";
  78. case DataFormatDead:
  79. return "Dead";
  80. case DataFormatArguments:
  81. return "Arguments";
  82. default:
  83. RELEASE_ASSERT_NOT_REACHED();
  84. return "Unknown";
  85. }
  86. }
  87. #if USE(JSVALUE64)
  88. inline bool needDataFormatConversion(DataFormat from, DataFormat to)
  89. {
  90. ASSERT(from != DataFormatNone);
  91. ASSERT(to != DataFormatNone);
  92. switch (from) {
  93. case DataFormatInteger:
  94. case DataFormatDouble:
  95. return to != from;
  96. case DataFormatCell:
  97. case DataFormatJS:
  98. case DataFormatJSInteger:
  99. case DataFormatJSDouble:
  100. case DataFormatJSCell:
  101. case DataFormatJSBoolean:
  102. switch (to) {
  103. case DataFormatInteger:
  104. case DataFormatDouble:
  105. return true;
  106. case DataFormatCell:
  107. case DataFormatJS:
  108. case DataFormatJSInteger:
  109. case DataFormatJSDouble:
  110. case DataFormatJSCell:
  111. case DataFormatJSBoolean:
  112. return false;
  113. default:
  114. // This captures DataFormatBoolean, which is currently unused.
  115. RELEASE_ASSERT_NOT_REACHED();
  116. }
  117. case DataFormatStorage:
  118. ASSERT(to == DataFormatStorage);
  119. return false;
  120. default:
  121. // This captures DataFormatBoolean, which is currently unused.
  122. RELEASE_ASSERT_NOT_REACHED();
  123. }
  124. return true;
  125. }
  126. #elif USE(JSVALUE32_64)
  127. inline bool needDataFormatConversion(DataFormat from, DataFormat to)
  128. {
  129. ASSERT(from != DataFormatNone);
  130. ASSERT(to != DataFormatNone);
  131. switch (from) {
  132. case DataFormatInteger:
  133. case DataFormatCell:
  134. case DataFormatBoolean:
  135. return ((to & DataFormatJS) || to == DataFormatDouble);
  136. case DataFormatDouble:
  137. case DataFormatJSDouble:
  138. return (to != DataFormatDouble && to != DataFormatJSDouble);
  139. case DataFormatJS:
  140. case DataFormatJSInteger:
  141. case DataFormatJSCell:
  142. case DataFormatJSBoolean:
  143. return (!(to & DataFormatJS) || to == DataFormatJSDouble);
  144. case DataFormatStorage:
  145. ASSERT(to == DataFormatStorage);
  146. return false;
  147. default:
  148. RELEASE_ASSERT_NOT_REACHED();
  149. }
  150. return true;
  151. }
  152. #endif
  153. inline bool isJSFormat(DataFormat format, DataFormat expectedFormat)
  154. {
  155. ASSERT(expectedFormat & DataFormatJS);
  156. return (format | DataFormatJS) == expectedFormat;
  157. }
  158. inline bool isJSInteger(DataFormat format)
  159. {
  160. return isJSFormat(format, DataFormatJSInteger);
  161. }
  162. inline bool isJSDouble(DataFormat format)
  163. {
  164. return isJSFormat(format, DataFormatJSDouble);
  165. }
  166. inline bool isJSCell(DataFormat format)
  167. {
  168. return isJSFormat(format, DataFormatJSCell);
  169. }
  170. inline bool isJSBoolean(DataFormat format)
  171. {
  172. return isJSFormat(format, DataFormatJSBoolean);
  173. }
  174. }
  175. #endif // DataFormat_h