Don't overwrite transition array map while iterating over the transition tree.
[platform/upstream/v8.git] / src / transitions-inl.h
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 //       copyright notice, this list of conditions and the following
10 //       disclaimer in the documentation and/or other materials provided
11 //       with the distribution.
12 //     * Neither the name of Google Inc. nor the names of its
13 //       contributors may be used to endorse or promote products derived
14 //       from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #ifndef V8_TRANSITIONS_INL_H_
29 #define V8_TRANSITIONS_INL_H_
30
31 #include "transitions.h"
32
33 namespace v8 {
34 namespace internal {
35
36
37 #define FIELD_ADDR(p, offset) \
38   (reinterpret_cast<byte*>(p) + offset - kHeapObjectTag)
39
40 #define WRITE_FIELD(p, offset, value) \
41   (*reinterpret_cast<Object**>(FIELD_ADDR(p, offset)) = value)
42
43 #define CONDITIONAL_WRITE_BARRIER(heap, object, offset, value, mode)    \
44   if (mode == UPDATE_WRITE_BARRIER) {                                   \
45     heap->incremental_marking()->RecordWrite(                           \
46       object, HeapObject::RawField(object, offset), value);             \
47     if (heap->InNewSpace(value)) {                                      \
48       heap->RecordWrite(object->address(), offset);                     \
49     }                                                                   \
50   }
51
52
53 TransitionArray* TransitionArray::cast(Object* object) {
54   ASSERT(object->IsTransitionArray());
55   return reinterpret_cast<TransitionArray*>(object);
56 }
57
58
59 bool TransitionArray::HasElementsTransition() {
60   return Search(GetHeap()->elements_transition_symbol()) != kNotFound;
61 }
62
63
64 Object* TransitionArray::back_pointer_storage() {
65   return get(kBackPointerStorageIndex);
66 }
67
68
69 void TransitionArray::set_back_pointer_storage(Object* back_pointer,
70                                                WriteBarrierMode mode) {
71   Heap* heap = GetHeap();
72   WRITE_FIELD(this, kBackPointerStorageOffset, back_pointer);
73   CONDITIONAL_WRITE_BARRIER(
74       heap, this, kBackPointerStorageOffset, back_pointer, mode);
75 }
76
77
78 bool TransitionArray::HasPrototypeTransitions() {
79   return IsFullTransitionArray() &&
80       get(kPrototypeTransitionsIndex) != Smi::FromInt(0);
81 }
82
83
84 FixedArray* TransitionArray::GetPrototypeTransitions() {
85   ASSERT(IsFullTransitionArray());
86   Object* prototype_transitions = get(kPrototypeTransitionsIndex);
87   return FixedArray::cast(prototype_transitions);
88 }
89
90
91 void TransitionArray::SetPrototypeTransitions(FixedArray* transitions,
92                                               WriteBarrierMode mode) {
93   ASSERT(IsFullTransitionArray());
94   ASSERT(transitions->IsFixedArray());
95   Heap* heap = GetHeap();
96   WRITE_FIELD(this, kPrototypeTransitionsOffset, transitions);
97   CONDITIONAL_WRITE_BARRIER(
98       heap, this, kPrototypeTransitionsOffset, transitions, mode);
99 }
100
101
102 Object** TransitionArray::GetPrototypeTransitionsSlot() {
103   return HeapObject::RawField(reinterpret_cast<HeapObject*>(this),
104                               kPrototypeTransitionsOffset);
105 }
106
107
108 Object** TransitionArray::GetKeySlot(int transition_number) {
109   ASSERT(!IsSimpleTransition());
110   ASSERT(transition_number < number_of_transitions());
111   return RawFieldOfElementAt(ToKeyIndex(transition_number));
112 }
113
114
115 Name* TransitionArray::GetKey(int transition_number) {
116   if (IsSimpleTransition()) {
117     Map* target = GetTarget(kSimpleTransitionIndex);
118     int descriptor = target->LastAdded();
119     Name* key = target->instance_descriptors()->GetKey(descriptor);
120     return key;
121   }
122   ASSERT(transition_number < number_of_transitions());
123   return Name::cast(get(ToKeyIndex(transition_number)));
124 }
125
126
127 void TransitionArray::SetKey(int transition_number, Name* key) {
128   ASSERT(!IsSimpleTransition());
129   ASSERT(transition_number < number_of_transitions());
130   set(ToKeyIndex(transition_number), key);
131 }
132
133
134 Map* TransitionArray::GetTarget(int transition_number) {
135   if (IsSimpleTransition()) {
136     ASSERT(transition_number == kSimpleTransitionIndex);
137     return Map::cast(get(kSimpleTransitionTarget));
138   }
139   ASSERT(transition_number < number_of_transitions());
140   return Map::cast(get(ToTargetIndex(transition_number)));
141 }
142
143
144 void TransitionArray::SetTarget(int transition_number, Map* value) {
145   if (IsSimpleTransition()) {
146     ASSERT(transition_number == kSimpleTransitionIndex);
147     return set(kSimpleTransitionTarget, value);
148   }
149   ASSERT(transition_number < number_of_transitions());
150   set(ToTargetIndex(transition_number), value);
151 }
152
153
154 PropertyDetails TransitionArray::GetTargetDetails(int transition_number) {
155   Map* map = GetTarget(transition_number);
156   return map->GetLastDescriptorDetails();
157 }
158
159
160 int TransitionArray::Search(Name* name) {
161   if (IsSimpleTransition()) {
162     Name* key = GetKey(kSimpleTransitionIndex);
163     if (key->Equals(name)) return kSimpleTransitionIndex;
164     return kNotFound;
165   }
166   return internal::Search<ALL_ENTRIES>(this, name);
167 }
168
169
170 void TransitionArray::NoIncrementalWriteBarrierSet(int transition_number,
171                                                    Name* key,
172                                                    Map* target) {
173   FixedArray::NoIncrementalWriteBarrierSet(
174       this, ToKeyIndex(transition_number), key);
175   FixedArray::NoIncrementalWriteBarrierSet(
176       this, ToTargetIndex(transition_number), target);
177 }
178
179
180 #undef FIELD_ADDR
181 #undef WRITE_FIELD
182 #undef CONDITIONAL_WRITE_BARRIER
183
184
185 } }  // namespace v8::internal
186
187 #endif  // V8_TRANSITIONS_INL_H_