ValueRecovery.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 ValueRecovery_h
  26. #define ValueRecovery_h
  27. #include "DataFormat.h"
  28. #include "JSCJSValue.h"
  29. #include "MacroAssembler.h"
  30. #include "VirtualRegister.h"
  31. #include <stdio.h>
  32. #include <wtf/Platform.h>
  33. namespace JSC {
  34. // Describes how to recover a given bytecode virtual register at a given
  35. // code point.
  36. enum ValueRecoveryTechnique {
  37. // It's already in the stack at the right location.
  38. AlreadyInJSStack,
  39. // It's already in the stack but unboxed.
  40. AlreadyInJSStackAsUnboxedInt32,
  41. AlreadyInJSStackAsUnboxedCell,
  42. AlreadyInJSStackAsUnboxedBoolean,
  43. AlreadyInJSStackAsUnboxedDouble,
  44. // It's in a register.
  45. InGPR,
  46. UnboxedInt32InGPR,
  47. UnboxedBooleanInGPR,
  48. #if USE(JSVALUE32_64)
  49. InPair,
  50. #endif
  51. InFPR,
  52. UInt32InGPR,
  53. // It's in the stack, but at a different location.
  54. DisplacedInJSStack,
  55. // It's in the stack, at a different location, and it's unboxed.
  56. Int32DisplacedInJSStack,
  57. DoubleDisplacedInJSStack,
  58. CellDisplacedInJSStack,
  59. BooleanDisplacedInJSStack,
  60. // It's an Arguments object.
  61. ArgumentsThatWereNotCreated,
  62. // It's a constant.
  63. Constant,
  64. // Don't know how to recover it.
  65. DontKnow
  66. };
  67. class ValueRecovery {
  68. public:
  69. ValueRecovery()
  70. : m_technique(DontKnow)
  71. {
  72. }
  73. bool isSet() const { return m_technique != DontKnow; }
  74. bool operator!() const { return !isSet(); }
  75. static ValueRecovery alreadyInJSStack()
  76. {
  77. ValueRecovery result;
  78. result.m_technique = AlreadyInJSStack;
  79. return result;
  80. }
  81. static ValueRecovery alreadyInJSStackAsUnboxedInt32()
  82. {
  83. ValueRecovery result;
  84. result.m_technique = AlreadyInJSStackAsUnboxedInt32;
  85. return result;
  86. }
  87. static ValueRecovery alreadyInJSStackAsUnboxedCell()
  88. {
  89. ValueRecovery result;
  90. result.m_technique = AlreadyInJSStackAsUnboxedCell;
  91. return result;
  92. }
  93. static ValueRecovery alreadyInJSStackAsUnboxedBoolean()
  94. {
  95. ValueRecovery result;
  96. result.m_technique = AlreadyInJSStackAsUnboxedBoolean;
  97. return result;
  98. }
  99. static ValueRecovery alreadyInJSStackAsUnboxedDouble()
  100. {
  101. ValueRecovery result;
  102. result.m_technique = AlreadyInJSStackAsUnboxedDouble;
  103. return result;
  104. }
  105. static ValueRecovery inGPR(MacroAssembler::RegisterID gpr, DataFormat dataFormat)
  106. {
  107. ASSERT(dataFormat != DataFormatNone);
  108. #if USE(JSVALUE32_64)
  109. ASSERT(dataFormat == DataFormatInteger || dataFormat == DataFormatCell || dataFormat == DataFormatBoolean);
  110. #endif
  111. ValueRecovery result;
  112. if (dataFormat == DataFormatInteger)
  113. result.m_technique = UnboxedInt32InGPR;
  114. else if (dataFormat == DataFormatBoolean)
  115. result.m_technique = UnboxedBooleanInGPR;
  116. else
  117. result.m_technique = InGPR;
  118. result.m_source.gpr = gpr;
  119. return result;
  120. }
  121. static ValueRecovery uint32InGPR(MacroAssembler::RegisterID gpr)
  122. {
  123. ValueRecovery result;
  124. result.m_technique = UInt32InGPR;
  125. result.m_source.gpr = gpr;
  126. return result;
  127. }
  128. #if USE(JSVALUE32_64)
  129. static ValueRecovery inPair(MacroAssembler::RegisterID tagGPR, MacroAssembler::RegisterID payloadGPR)
  130. {
  131. ValueRecovery result;
  132. result.m_technique = InPair;
  133. result.m_source.pair.tagGPR = tagGPR;
  134. result.m_source.pair.payloadGPR = payloadGPR;
  135. return result;
  136. }
  137. #endif
  138. static ValueRecovery inFPR(MacroAssembler::FPRegisterID fpr)
  139. {
  140. ValueRecovery result;
  141. result.m_technique = InFPR;
  142. result.m_source.fpr = fpr;
  143. return result;
  144. }
  145. static ValueRecovery displacedInJSStack(VirtualRegister virtualReg, DataFormat dataFormat)
  146. {
  147. ValueRecovery result;
  148. switch (dataFormat) {
  149. case DataFormatInteger:
  150. result.m_technique = Int32DisplacedInJSStack;
  151. break;
  152. case DataFormatDouble:
  153. result.m_technique = DoubleDisplacedInJSStack;
  154. break;
  155. case DataFormatCell:
  156. result.m_technique = CellDisplacedInJSStack;
  157. break;
  158. case DataFormatBoolean:
  159. result.m_technique = BooleanDisplacedInJSStack;
  160. break;
  161. default:
  162. ASSERT(dataFormat != DataFormatNone && dataFormat != DataFormatStorage);
  163. result.m_technique = DisplacedInJSStack;
  164. break;
  165. }
  166. result.m_source.virtualReg = virtualReg;
  167. return result;
  168. }
  169. static ValueRecovery constant(JSValue value)
  170. {
  171. ValueRecovery result;
  172. result.m_technique = Constant;
  173. result.m_source.constant = JSValue::encode(value);
  174. return result;
  175. }
  176. static ValueRecovery argumentsThatWereNotCreated()
  177. {
  178. ValueRecovery result;
  179. result.m_technique = ArgumentsThatWereNotCreated;
  180. return result;
  181. }
  182. ValueRecoveryTechnique technique() const { return m_technique; }
  183. bool isConstant() const { return m_technique == Constant; }
  184. bool isInRegisters() const
  185. {
  186. switch (m_technique) {
  187. case InGPR:
  188. case UnboxedInt32InGPR:
  189. case UnboxedBooleanInGPR:
  190. #if USE(JSVALUE32_64)
  191. case InPair:
  192. #endif
  193. case InFPR:
  194. return true;
  195. default:
  196. return false;
  197. }
  198. }
  199. bool isAlreadyInJSStack() const
  200. {
  201. switch (technique()) {
  202. case AlreadyInJSStack:
  203. case AlreadyInJSStackAsUnboxedInt32:
  204. case AlreadyInJSStackAsUnboxedCell:
  205. case AlreadyInJSStackAsUnboxedBoolean:
  206. case AlreadyInJSStackAsUnboxedDouble:
  207. return true;
  208. default:
  209. return false;
  210. }
  211. }
  212. MacroAssembler::RegisterID gpr() const
  213. {
  214. ASSERT(m_technique == InGPR || m_technique == UnboxedInt32InGPR || m_technique == UnboxedBooleanInGPR || m_technique == UInt32InGPR);
  215. return m_source.gpr;
  216. }
  217. #if USE(JSVALUE32_64)
  218. MacroAssembler::RegisterID tagGPR() const
  219. {
  220. ASSERT(m_technique == InPair);
  221. return m_source.pair.tagGPR;
  222. }
  223. MacroAssembler::RegisterID payloadGPR() const
  224. {
  225. ASSERT(m_technique == InPair);
  226. return m_source.pair.payloadGPR;
  227. }
  228. #endif
  229. MacroAssembler::FPRegisterID fpr() const
  230. {
  231. ASSERT(m_technique == InFPR);
  232. return m_source.fpr;
  233. }
  234. VirtualRegister virtualRegister() const
  235. {
  236. ASSERT(m_technique == DisplacedInJSStack || m_technique == Int32DisplacedInJSStack || m_technique == DoubleDisplacedInJSStack || m_technique == CellDisplacedInJSStack || m_technique == BooleanDisplacedInJSStack);
  237. return m_source.virtualReg;
  238. }
  239. JSValue constant() const
  240. {
  241. ASSERT(m_technique == Constant);
  242. return JSValue::decode(m_source.constant);
  243. }
  244. void dump(PrintStream& out) const
  245. {
  246. switch (technique()) {
  247. case AlreadyInJSStack:
  248. out.printf("-");
  249. break;
  250. case AlreadyInJSStackAsUnboxedInt32:
  251. out.printf("(int32)");
  252. break;
  253. case AlreadyInJSStackAsUnboxedCell:
  254. out.printf("(cell)");
  255. break;
  256. case AlreadyInJSStackAsUnboxedBoolean:
  257. out.printf("(bool)");
  258. break;
  259. case AlreadyInJSStackAsUnboxedDouble:
  260. out.printf("(double)");
  261. break;
  262. case InGPR:
  263. out.printf("%%r%d", gpr());
  264. break;
  265. case UnboxedInt32InGPR:
  266. out.printf("int32(%%r%d)", gpr());
  267. break;
  268. case UnboxedBooleanInGPR:
  269. out.printf("bool(%%r%d)", gpr());
  270. break;
  271. case UInt32InGPR:
  272. out.printf("uint32(%%r%d)", gpr());
  273. break;
  274. case InFPR:
  275. out.printf("%%fr%d", fpr());
  276. break;
  277. #if USE(JSVALUE32_64)
  278. case InPair:
  279. out.printf("pair(%%r%d, %%r%d)", tagGPR(), payloadGPR());
  280. break;
  281. #endif
  282. case DisplacedInJSStack:
  283. out.printf("*%d", virtualRegister());
  284. break;
  285. case Int32DisplacedInJSStack:
  286. out.printf("*int32(%d)", virtualRegister());
  287. break;
  288. case DoubleDisplacedInJSStack:
  289. out.printf("*double(%d)", virtualRegister());
  290. break;
  291. case CellDisplacedInJSStack:
  292. out.printf("*cell(%d)", virtualRegister());
  293. break;
  294. case BooleanDisplacedInJSStack:
  295. out.printf("*bool(%d)", virtualRegister());
  296. break;
  297. case ArgumentsThatWereNotCreated:
  298. out.printf("arguments");
  299. break;
  300. case Constant:
  301. out.print("[", constant(), "]");
  302. break;
  303. case DontKnow:
  304. out.printf("!");
  305. break;
  306. default:
  307. out.printf("?%d", technique());
  308. break;
  309. }
  310. }
  311. private:
  312. ValueRecoveryTechnique m_technique;
  313. union {
  314. MacroAssembler::RegisterID gpr;
  315. MacroAssembler::FPRegisterID fpr;
  316. #if USE(JSVALUE32_64)
  317. struct {
  318. MacroAssembler::RegisterID tagGPR;
  319. MacroAssembler::RegisterID payloadGPR;
  320. } pair;
  321. #endif
  322. VirtualRegister virtualReg;
  323. EncodedJSValue constant;
  324. } m_source;
  325. };
  326. } // namespace JSC
  327. #endif // ValueRecovery_h