RepatchBuffer.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 RepatchBuffer_h
  26. #define RepatchBuffer_h
  27. #if ENABLE(JIT)
  28. #include "CodeBlock.h"
  29. #include <MacroAssembler.h>
  30. #include <wtf/Noncopyable.h>
  31. namespace JSC {
  32. // RepatchBuffer:
  33. //
  34. // This class is used to modify code after code generation has been completed,
  35. // and after the code has potentially already been executed. This mechanism is
  36. // used to apply optimizations to the code.
  37. //
  38. class RepatchBuffer {
  39. typedef MacroAssemblerCodePtr CodePtr;
  40. public:
  41. RepatchBuffer(CodeBlock* codeBlock)
  42. {
  43. JITCode& code = codeBlock->getJITCode();
  44. m_start = code.start();
  45. m_size = code.size();
  46. ExecutableAllocator::makeWritable(m_start, m_size);
  47. }
  48. ~RepatchBuffer()
  49. {
  50. ExecutableAllocator::makeExecutable(m_start, m_size);
  51. }
  52. void relink(CodeLocationJump jump, CodeLocationLabel destination)
  53. {
  54. MacroAssembler::repatchJump(jump, destination);
  55. }
  56. void relink(CodeLocationCall call, CodeLocationLabel destination)
  57. {
  58. MacroAssembler::repatchCall(call, destination);
  59. }
  60. void relink(CodeLocationCall call, FunctionPtr destination)
  61. {
  62. MacroAssembler::repatchCall(call, destination);
  63. }
  64. void relink(CodeLocationNearCall nearCall, CodePtr destination)
  65. {
  66. MacroAssembler::repatchNearCall(nearCall, CodeLocationLabel(destination));
  67. }
  68. void relink(CodeLocationNearCall nearCall, CodeLocationLabel destination)
  69. {
  70. MacroAssembler::repatchNearCall(nearCall, destination);
  71. }
  72. void repatch(CodeLocationDataLabel32 dataLabel32, int32_t value)
  73. {
  74. MacroAssembler::repatchInt32(dataLabel32, value);
  75. }
  76. void repatch(CodeLocationDataLabelCompact dataLabelCompact, int32_t value)
  77. {
  78. MacroAssembler::repatchCompact(dataLabelCompact, value);
  79. }
  80. void repatch(CodeLocationDataLabelPtr dataLabelPtr, void* value)
  81. {
  82. MacroAssembler::repatchPointer(dataLabelPtr, value);
  83. }
  84. void relinkCallerToTrampoline(ReturnAddressPtr returnAddress, CodeLocationLabel label)
  85. {
  86. relink(CodeLocationCall(CodePtr(returnAddress)), label);
  87. }
  88. void relinkCallerToTrampoline(ReturnAddressPtr returnAddress, CodePtr newCalleeFunction)
  89. {
  90. relinkCallerToTrampoline(returnAddress, CodeLocationLabel(newCalleeFunction));
  91. }
  92. void relinkCallerToFunction(ReturnAddressPtr returnAddress, FunctionPtr function)
  93. {
  94. relink(CodeLocationCall(CodePtr(returnAddress)), function);
  95. }
  96. void relinkNearCallerToTrampoline(ReturnAddressPtr returnAddress, CodeLocationLabel label)
  97. {
  98. relink(CodeLocationNearCall(CodePtr(returnAddress)), label);
  99. }
  100. void relinkNearCallerToTrampoline(ReturnAddressPtr returnAddress, CodePtr newCalleeFunction)
  101. {
  102. relinkNearCallerToTrampoline(returnAddress, CodeLocationLabel(newCalleeFunction));
  103. }
  104. void replaceWithLoad(CodeLocationConvertibleLoad label)
  105. {
  106. MacroAssembler::replaceWithLoad(label);
  107. }
  108. void replaceWithAddressComputation(CodeLocationConvertibleLoad label)
  109. {
  110. MacroAssembler::replaceWithAddressComputation(label);
  111. }
  112. void setLoadInstructionIsActive(CodeLocationConvertibleLoad label, bool isActive)
  113. {
  114. if (isActive)
  115. replaceWithLoad(label);
  116. else
  117. replaceWithAddressComputation(label);
  118. }
  119. static CodeLocationLabel startOfBranchPtrWithPatchOnRegister(CodeLocationDataLabelPtr label)
  120. {
  121. return MacroAssembler::startOfBranchPtrWithPatchOnRegister(label);
  122. }
  123. static CodeLocationLabel startOfPatchableBranchPtrWithPatchOnAddress(CodeLocationDataLabelPtr label)
  124. {
  125. return MacroAssembler::startOfPatchableBranchPtrWithPatchOnAddress(label);
  126. }
  127. void replaceWithJump(CodeLocationLabel instructionStart, CodeLocationLabel destination)
  128. {
  129. MacroAssembler::replaceWithJump(instructionStart, destination);
  130. }
  131. // This is a *bit* of a silly API, since we currently always also repatch the
  132. // immediate after calling this. But I'm fine with that, since this just feels
  133. // less yucky.
  134. void revertJumpReplacementToBranchPtrWithPatch(CodeLocationLabel instructionStart, MacroAssembler::RegisterID reg, void* value)
  135. {
  136. MacroAssembler::revertJumpReplacementToBranchPtrWithPatch(instructionStart, reg, value);
  137. }
  138. void revertJumpReplacementToPatchableBranchPtrWithPatch(CodeLocationLabel instructionStart, MacroAssembler::Address address, void* value)
  139. {
  140. MacroAssembler::revertJumpReplacementToPatchableBranchPtrWithPatch(instructionStart, address, value);
  141. }
  142. private:
  143. void* m_start;
  144. size_t m_size;
  145. };
  146. } // namespace JSC
  147. #endif // ENABLE(ASSEMBLER)
  148. #endif // RepatchBuffer_h