JITWriteBarrier.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef JITWriteBarrier_h
  26. #define JITWriteBarrier_h
  27. #if ENABLE(JIT)
  28. #include "MacroAssembler.h"
  29. #include "SlotVisitor.h"
  30. #include "UnusedPointer.h"
  31. #include "WriteBarrier.h"
  32. namespace JSC {
  33. class JSCell;
  34. class VM;
  35. // Needs to be even to appease some of the backends.
  36. #define JITWriteBarrierFlag ((void*)2)
  37. class JITWriteBarrierBase {
  38. public:
  39. typedef void* (JITWriteBarrierBase::*UnspecifiedBoolType);
  40. operator UnspecifiedBoolType*() const { return get() ? reinterpret_cast<UnspecifiedBoolType*>(1) : 0; }
  41. bool operator!() const { return !get(); }
  42. void setFlagOnBarrier()
  43. {
  44. ASSERT(!m_location);
  45. m_location = CodeLocationDataLabelPtr(JITWriteBarrierFlag);
  46. }
  47. bool isFlagged() const
  48. {
  49. return !!m_location;
  50. }
  51. void setLocation(CodeLocationDataLabelPtr location)
  52. {
  53. ASSERT(!m_location);
  54. m_location = location;
  55. }
  56. CodeLocationDataLabelPtr location() const
  57. {
  58. ASSERT((!!m_location) && m_location.executableAddress() != JITWriteBarrierFlag);
  59. return m_location;
  60. }
  61. void clear() { clear(0); }
  62. void clearToUnusedPointer() { clear(reinterpret_cast<void*>(unusedPointer)); }
  63. protected:
  64. JITWriteBarrierBase()
  65. {
  66. }
  67. void set(VM&, CodeLocationDataLabelPtr location, JSCell* owner, JSCell* value)
  68. {
  69. Heap::writeBarrier(owner, value);
  70. m_location = location;
  71. ASSERT(((!!m_location) && m_location.executableAddress() != JITWriteBarrierFlag) || (location.executableAddress() == m_location.executableAddress()));
  72. MacroAssembler::repatchPointer(m_location, value);
  73. ASSERT(get() == value);
  74. }
  75. JSCell* get() const
  76. {
  77. if (!m_location || m_location.executableAddress() == JITWriteBarrierFlag)
  78. return 0;
  79. void* result = static_cast<JSCell*>(MacroAssembler::readPointer(m_location));
  80. if (result == reinterpret_cast<void*>(unusedPointer))
  81. return 0;
  82. return static_cast<JSCell*>(result);
  83. }
  84. private:
  85. void clear(void* clearedValue)
  86. {
  87. if (!m_location)
  88. return;
  89. if (m_location.executableAddress() != JITWriteBarrierFlag)
  90. MacroAssembler::repatchPointer(m_location, clearedValue);
  91. }
  92. CodeLocationDataLabelPtr m_location;
  93. };
  94. #undef JITWriteBarrierFlag
  95. template <typename T> class JITWriteBarrier : public JITWriteBarrierBase {
  96. public:
  97. JITWriteBarrier()
  98. {
  99. }
  100. void set(VM& vm, CodeLocationDataLabelPtr location, JSCell* owner, T* value)
  101. {
  102. validateCell(owner);
  103. validateCell(value);
  104. JITWriteBarrierBase::set(vm, location, owner, value);
  105. }
  106. void set(VM& vm, JSCell* owner, T* value)
  107. {
  108. set(vm, location(), owner, value);
  109. }
  110. T* get() const
  111. {
  112. T* result = static_cast<T*>(JITWriteBarrierBase::get());
  113. if (result)
  114. validateCell(result);
  115. return result;
  116. }
  117. };
  118. template<typename T> inline void SlotVisitor::append(JITWriteBarrier<T>* slot)
  119. {
  120. internalAppend(slot->get());
  121. }
  122. }
  123. #endif // ENABLE(JIT)
  124. #endif