CodeLocation.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (C) 2009 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 CodeLocation_h
  26. #define CodeLocation_h
  27. #include "MacroAssemblerCodeRef.h"
  28. #if ENABLE(ASSEMBLER)
  29. namespace JSC {
  30. class CodeLocationInstruction;
  31. class CodeLocationLabel;
  32. class CodeLocationJump;
  33. class CodeLocationCall;
  34. class CodeLocationNearCall;
  35. class CodeLocationDataLabelCompact;
  36. class CodeLocationDataLabel32;
  37. class CodeLocationDataLabelPtr;
  38. class CodeLocationConvertibleLoad;
  39. // The CodeLocation* types are all pretty much do-nothing wrappers around
  40. // CodePtr (or MacroAssemblerCodePtr, to give it its full name). These
  41. // classes only exist to provide type-safety when linking and patching code.
  42. //
  43. // The one new piece of functionallity introduced by these classes is the
  44. // ability to create (or put another way, to re-discover) another CodeLocation
  45. // at an offset from one you already know. When patching code to optimize it
  46. // we often want to patch a number of instructions that are short, fixed
  47. // offsets apart. To reduce memory overhead we will only retain a pointer to
  48. // one of the instructions, and we will use the *AtOffset methods provided by
  49. // CodeLocationCommon to find the other points in the code to modify.
  50. class CodeLocationCommon : public MacroAssemblerCodePtr {
  51. public:
  52. CodeLocationInstruction instructionAtOffset(int offset);
  53. CodeLocationLabel labelAtOffset(int offset);
  54. CodeLocationJump jumpAtOffset(int offset);
  55. CodeLocationCall callAtOffset(int offset);
  56. CodeLocationNearCall nearCallAtOffset(int offset);
  57. CodeLocationDataLabelPtr dataLabelPtrAtOffset(int offset);
  58. CodeLocationDataLabel32 dataLabel32AtOffset(int offset);
  59. CodeLocationDataLabelCompact dataLabelCompactAtOffset(int offset);
  60. CodeLocationConvertibleLoad convertibleLoadAtOffset(int offset);
  61. protected:
  62. CodeLocationCommon()
  63. {
  64. }
  65. CodeLocationCommon(MacroAssemblerCodePtr location)
  66. : MacroAssemblerCodePtr(location)
  67. {
  68. }
  69. };
  70. class CodeLocationInstruction : public CodeLocationCommon {
  71. public:
  72. CodeLocationInstruction() {}
  73. explicit CodeLocationInstruction(MacroAssemblerCodePtr location)
  74. : CodeLocationCommon(location) {}
  75. explicit CodeLocationInstruction(void* location)
  76. : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
  77. };
  78. class CodeLocationLabel : public CodeLocationCommon {
  79. public:
  80. CodeLocationLabel() {}
  81. explicit CodeLocationLabel(MacroAssemblerCodePtr location)
  82. : CodeLocationCommon(location) {}
  83. explicit CodeLocationLabel(void* location)
  84. : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
  85. };
  86. class CodeLocationJump : public CodeLocationCommon {
  87. public:
  88. CodeLocationJump() {}
  89. explicit CodeLocationJump(MacroAssemblerCodePtr location)
  90. : CodeLocationCommon(location) {}
  91. explicit CodeLocationJump(void* location)
  92. : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
  93. };
  94. class CodeLocationCall : public CodeLocationCommon {
  95. public:
  96. CodeLocationCall() {}
  97. explicit CodeLocationCall(MacroAssemblerCodePtr location)
  98. : CodeLocationCommon(location) {}
  99. explicit CodeLocationCall(void* location)
  100. : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
  101. };
  102. class CodeLocationNearCall : public CodeLocationCommon {
  103. public:
  104. CodeLocationNearCall() {}
  105. explicit CodeLocationNearCall(MacroAssemblerCodePtr location)
  106. : CodeLocationCommon(location) {}
  107. explicit CodeLocationNearCall(void* location)
  108. : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
  109. };
  110. class CodeLocationDataLabel32 : public CodeLocationCommon {
  111. public:
  112. CodeLocationDataLabel32() {}
  113. explicit CodeLocationDataLabel32(MacroAssemblerCodePtr location)
  114. : CodeLocationCommon(location) {}
  115. explicit CodeLocationDataLabel32(void* location)
  116. : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
  117. };
  118. class CodeLocationDataLabelCompact : public CodeLocationCommon {
  119. public:
  120. CodeLocationDataLabelCompact() { }
  121. explicit CodeLocationDataLabelCompact(MacroAssemblerCodePtr location)
  122. : CodeLocationCommon(location) { }
  123. explicit CodeLocationDataLabelCompact(void* location)
  124. : CodeLocationCommon(MacroAssemblerCodePtr(location)) { }
  125. };
  126. class CodeLocationDataLabelPtr : public CodeLocationCommon {
  127. public:
  128. CodeLocationDataLabelPtr() {}
  129. explicit CodeLocationDataLabelPtr(MacroAssemblerCodePtr location)
  130. : CodeLocationCommon(location) {}
  131. explicit CodeLocationDataLabelPtr(void* location)
  132. : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
  133. };
  134. class CodeLocationConvertibleLoad : public CodeLocationCommon {
  135. public:
  136. CodeLocationConvertibleLoad() { }
  137. explicit CodeLocationConvertibleLoad(MacroAssemblerCodePtr location)
  138. : CodeLocationCommon(location) { }
  139. explicit CodeLocationConvertibleLoad(void* location)
  140. : CodeLocationCommon(MacroAssemblerCodePtr(location)) { }
  141. };
  142. inline CodeLocationInstruction CodeLocationCommon::instructionAtOffset(int offset)
  143. {
  144. ASSERT_VALID_CODE_OFFSET(offset);
  145. return CodeLocationInstruction(reinterpret_cast<char*>(dataLocation()) + offset);
  146. }
  147. inline CodeLocationLabel CodeLocationCommon::labelAtOffset(int offset)
  148. {
  149. ASSERT_VALID_CODE_OFFSET(offset);
  150. return CodeLocationLabel(reinterpret_cast<char*>(dataLocation()) + offset);
  151. }
  152. inline CodeLocationJump CodeLocationCommon::jumpAtOffset(int offset)
  153. {
  154. ASSERT_VALID_CODE_OFFSET(offset);
  155. return CodeLocationJump(reinterpret_cast<char*>(dataLocation()) + offset);
  156. }
  157. inline CodeLocationCall CodeLocationCommon::callAtOffset(int offset)
  158. {
  159. ASSERT_VALID_CODE_OFFSET(offset);
  160. return CodeLocationCall(reinterpret_cast<char*>(dataLocation()) + offset);
  161. }
  162. inline CodeLocationNearCall CodeLocationCommon::nearCallAtOffset(int offset)
  163. {
  164. ASSERT_VALID_CODE_OFFSET(offset);
  165. return CodeLocationNearCall(reinterpret_cast<char*>(dataLocation()) + offset);
  166. }
  167. inline CodeLocationDataLabelPtr CodeLocationCommon::dataLabelPtrAtOffset(int offset)
  168. {
  169. ASSERT_VALID_CODE_OFFSET(offset);
  170. return CodeLocationDataLabelPtr(reinterpret_cast<char*>(dataLocation()) + offset);
  171. }
  172. inline CodeLocationDataLabel32 CodeLocationCommon::dataLabel32AtOffset(int offset)
  173. {
  174. ASSERT_VALID_CODE_OFFSET(offset);
  175. return CodeLocationDataLabel32(reinterpret_cast<char*>(dataLocation()) + offset);
  176. }
  177. inline CodeLocationDataLabelCompact CodeLocationCommon::dataLabelCompactAtOffset(int offset)
  178. {
  179. ASSERT_VALID_CODE_OFFSET(offset);
  180. return CodeLocationDataLabelCompact(reinterpret_cast<char*>(dataLocation()) + offset);
  181. }
  182. inline CodeLocationConvertibleLoad CodeLocationCommon::convertibleLoadAtOffset(int offset)
  183. {
  184. ASSERT_VALID_CODE_OFFSET(offset);
  185. return CodeLocationConvertibleLoad(reinterpret_cast<char*>(dataLocation()) + offset);
  186. }
  187. } // namespace JSC
  188. #endif // ENABLE(ASSEMBLER)
  189. #endif // CodeLocation_h