StructureTransitionTable.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (C) 2008, 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 COMPUTER, 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 COMPUTER, 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 StructureTransitionTable_h
  26. #define StructureTransitionTable_h
  27. #include "IndexingType.h"
  28. #include "WeakGCMap.h"
  29. #include <wtf/HashFunctions.h>
  30. #include <wtf/OwnPtr.h>
  31. #include <wtf/RefPtr.h>
  32. #include <wtf/text/StringImpl.h>
  33. namespace JSC {
  34. class JSCell;
  35. class Structure;
  36. static const unsigned FirstInternalAttribute = 1 << 6; // Use for transitions that don't have to do with property additions.
  37. // Support for attributes used to indicate transitions not related to properties.
  38. // If any of these are used, the string portion of the key should be 0.
  39. enum NonPropertyTransition {
  40. AllocateUndecided,
  41. AllocateInt32,
  42. AllocateDouble,
  43. AllocateContiguous,
  44. AllocateArrayStorage,
  45. AllocateSlowPutArrayStorage,
  46. SwitchToSlowPutArrayStorage,
  47. AddIndexedAccessors
  48. };
  49. inline unsigned toAttributes(NonPropertyTransition transition)
  50. {
  51. return transition + FirstInternalAttribute;
  52. }
  53. inline IndexingType newIndexingType(IndexingType oldType, NonPropertyTransition transition)
  54. {
  55. switch (transition) {
  56. case AllocateUndecided:
  57. ASSERT(!hasIndexedProperties(oldType));
  58. return oldType | UndecidedShape;
  59. case AllocateInt32:
  60. ASSERT(!hasIndexedProperties(oldType) || hasUndecided(oldType));
  61. return (oldType & ~IndexingShapeMask) | Int32Shape;
  62. case AllocateDouble:
  63. ASSERT(!hasIndexedProperties(oldType) || hasUndecided(oldType) || hasInt32(oldType));
  64. return (oldType & ~IndexingShapeMask) | DoubleShape;
  65. case AllocateContiguous:
  66. ASSERT(!hasIndexedProperties(oldType) || hasUndecided(oldType) || hasInt32(oldType) || hasDouble(oldType));
  67. return (oldType & ~IndexingShapeMask) | ContiguousShape;
  68. case AllocateArrayStorage:
  69. ASSERT(!hasIndexedProperties(oldType) || hasUndecided(oldType) || hasInt32(oldType) || hasDouble(oldType) || hasContiguous(oldType));
  70. return (oldType & ~IndexingShapeMask) | ArrayStorageShape;
  71. case AllocateSlowPutArrayStorage:
  72. ASSERT(!hasIndexedProperties(oldType) || hasUndecided(oldType) || hasInt32(oldType) || hasDouble(oldType) || hasContiguous(oldType) || hasContiguous(oldType));
  73. return (oldType & ~IndexingShapeMask) | SlowPutArrayStorageShape;
  74. case SwitchToSlowPutArrayStorage:
  75. ASSERT(hasFastArrayStorage(oldType));
  76. return (oldType & ~IndexingShapeMask) | SlowPutArrayStorageShape;
  77. case AddIndexedAccessors:
  78. return oldType | MayHaveIndexedAccessors;
  79. default:
  80. RELEASE_ASSERT_NOT_REACHED();
  81. return oldType;
  82. }
  83. }
  84. class StructureTransitionTable {
  85. static const intptr_t UsingSingleSlotFlag = 1;
  86. struct Hash {
  87. typedef std::pair<RefPtr<StringImpl>, unsigned> Key;
  88. static unsigned hash(const Key& p)
  89. {
  90. unsigned result = p.second;
  91. if (p.first)
  92. result += p.first->existingHash();
  93. return result;
  94. }
  95. static bool equal(const Key& a, const Key& b)
  96. {
  97. return a == b;
  98. }
  99. static const bool safeToCompareToEmptyOrDeleted = true;
  100. };
  101. typedef WeakGCMap<Hash::Key, Structure, Hash> TransitionMap;
  102. public:
  103. StructureTransitionTable()
  104. : m_data(UsingSingleSlotFlag)
  105. {
  106. }
  107. ~StructureTransitionTable()
  108. {
  109. if (!isUsingSingleSlot()) {
  110. delete map();
  111. return;
  112. }
  113. WeakImpl* impl = this->weakImpl();
  114. if (!impl)
  115. return;
  116. WeakSet::deallocate(impl);
  117. }
  118. inline void add(VM&, Structure*);
  119. inline bool contains(StringImpl* rep, unsigned attributes) const;
  120. inline Structure* get(StringImpl* rep, unsigned attributes) const;
  121. private:
  122. bool isUsingSingleSlot() const
  123. {
  124. return m_data & UsingSingleSlotFlag;
  125. }
  126. TransitionMap* map() const
  127. {
  128. ASSERT(!isUsingSingleSlot());
  129. return reinterpret_cast<TransitionMap*>(m_data);
  130. }
  131. WeakImpl* weakImpl() const
  132. {
  133. ASSERT(isUsingSingleSlot());
  134. return reinterpret_cast<WeakImpl*>(m_data & ~UsingSingleSlotFlag);
  135. }
  136. void setMap(TransitionMap* map)
  137. {
  138. ASSERT(isUsingSingleSlot());
  139. if (WeakImpl* impl = this->weakImpl())
  140. WeakSet::deallocate(impl);
  141. // This implicitly clears the flag that indicates we're using a single transition
  142. m_data = reinterpret_cast<intptr_t>(map);
  143. ASSERT(!isUsingSingleSlot());
  144. }
  145. Structure* singleTransition() const
  146. {
  147. ASSERT(isUsingSingleSlot());
  148. if (WeakImpl* impl = this->weakImpl()) {
  149. if (impl->state() == WeakImpl::Live)
  150. return reinterpret_cast<Structure*>(impl->jsValue().asCell());
  151. }
  152. return 0;
  153. }
  154. void setSingleTransition(VM&, Structure* structure)
  155. {
  156. ASSERT(isUsingSingleSlot());
  157. if (WeakImpl* impl = this->weakImpl())
  158. WeakSet::deallocate(impl);
  159. WeakImpl* impl = WeakSet::allocate(reinterpret_cast<JSCell*>(structure));
  160. m_data = reinterpret_cast<intptr_t>(impl) | UsingSingleSlotFlag;
  161. }
  162. intptr_t m_data;
  163. };
  164. } // namespace JSC
  165. #endif // StructureTransitionTable_h