OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/interface.h" | 7 #include "src/interface.h" |
8 | 8 |
| 9 #include "src/base/lazy-instance.h" |
| 10 |
9 namespace v8 { | 11 namespace v8 { |
10 namespace internal { | 12 namespace internal { |
11 | 13 |
| 14 // --------------------------------------------------------------------------- |
| 15 // Initialization. |
| 16 |
| 17 struct Interface::ValueCreate { |
| 18 static void Construct(Interface* ptr) { |
| 19 ::new (ptr) Interface(VALUE + FROZEN); |
| 20 } |
| 21 }; |
| 22 |
| 23 |
| 24 struct Interface::ConstCreate { |
| 25 static void Construct(Interface* ptr) { |
| 26 ::new (ptr) Interface(VALUE + CONST + FROZEN); |
| 27 } |
| 28 }; |
| 29 |
| 30 |
| 31 namespace { |
| 32 base::LazyInstance<Interface, Interface::ValueCreate>::type value_interface = |
| 33 LAZY_INSTANCE_INITIALIZER; |
| 34 |
| 35 base::LazyInstance<Interface, Interface::ConstCreate>::type const_interface = |
| 36 LAZY_INSTANCE_INITIALIZER; |
| 37 } |
| 38 |
| 39 |
| 40 Interface* Interface::NewValue() { |
| 41 return value_interface.Pointer(); // Cached. |
| 42 } |
| 43 |
| 44 |
| 45 Interface* Interface::NewConst() { |
| 46 return const_interface.Pointer(); // Cached. |
| 47 } |
| 48 |
| 49 |
| 50 // --------------------------------------------------------------------------- |
| 51 // Lookup. |
| 52 |
12 Interface* Interface::Lookup(Handle<String> name, Zone* zone) { | 53 Interface* Interface::Lookup(Handle<String> name, Zone* zone) { |
13 DCHECK(IsModule()); | 54 DCHECK(IsModule()); |
14 ZoneHashMap* map = Chase()->exports_; | 55 ZoneHashMap* map = Chase()->exports_; |
15 if (map == NULL) return NULL; | 56 if (map == nullptr) return nullptr; |
16 ZoneAllocationPolicy allocator(zone); | 57 ZoneAllocationPolicy allocator(zone); |
17 ZoneHashMap::Entry* p = map->Lookup(name.location(), name->Hash(), false, | 58 ZoneHashMap::Entry* p = |
18 allocator); | 59 map->Lookup(name.location(), name->Hash(), false, allocator); |
19 if (p == NULL) return NULL; | 60 if (p == nullptr) return nullptr; |
20 DCHECK(*static_cast<String**>(p->key) == *name); | 61 DCHECK(*static_cast<String**>(p->key) == *name); |
21 DCHECK(p->value != NULL); | 62 DCHECK(p->value != nullptr); |
22 return static_cast<Interface*>(p->value); | 63 return static_cast<Interface*>(p->value); |
23 } | 64 } |
24 | 65 |
25 | 66 |
| 67 // --------------------------------------------------------------------------- |
| 68 // Addition. |
| 69 |
26 #ifdef DEBUG | 70 #ifdef DEBUG |
27 // Current nesting depth for debug output. | 71 // Current nesting depth for debug output. |
28 class Nesting { | 72 class Nesting { |
29 public: | 73 public: |
30 Nesting() { current_ += 2; } | 74 Nesting() { current_ += 2; } |
31 ~Nesting() { current_ -= 2; } | 75 ~Nesting() { current_ -= 2; } |
32 static int current() { return current_; } | 76 static int current() { return current_; } |
33 private: | 77 private: |
34 static int current_; | 78 static int current_; |
35 }; | 79 }; |
36 | 80 |
37 int Nesting::current_ = 0; | 81 int Nesting::current_ = 0; |
38 #endif | 82 #endif |
39 | 83 |
40 | 84 |
41 void Interface::DoAdd(const void* name, uint32_t hash, Interface* interface, | 85 void Interface::DoAdd(const void* name, uint32_t hash, Interface* interface, |
42 Zone* zone, bool* ok) { | 86 Zone* zone, bool* ok) { |
43 MakeModule(ok); | 87 MakeModule(ok); |
44 if (!*ok) return; | 88 if (!*ok) return; |
45 | 89 |
46 #ifdef DEBUG | 90 #ifdef DEBUG |
47 if (FLAG_print_interface_details) { | 91 if (FLAG_print_interface_details) { |
48 PrintF("%*s# Adding...\n", Nesting::current(), ""); | 92 PrintF("%*s# Adding...\n", Nesting::current(), ""); |
49 PrintF("%*sthis = ", Nesting::current(), ""); | 93 PrintF("%*sthis = ", Nesting::current(), ""); |
50 this->Print(Nesting::current()); | 94 this->Print(Nesting::current()); |
51 const AstRawString* symbol = static_cast<const AstRawString*>(name); | 95 const AstRawString* raw = static_cast<const AstRawString*>(name); |
52 PrintF("%*s%.*s : ", Nesting::current(), "", symbol->length(), | 96 PrintF("%*s%.*s : ", Nesting::current(), "", |
53 symbol->raw_data()); | 97 raw->length(), raw->raw_data()); |
54 interface->Print(Nesting::current()); | 98 interface->Print(Nesting::current()); |
55 } | 99 } |
56 #endif | 100 #endif |
57 | 101 |
58 ZoneHashMap** map = &Chase()->exports_; | 102 ZoneHashMap** map = &Chase()->exports_; |
59 ZoneAllocationPolicy allocator(zone); | 103 ZoneAllocationPolicy allocator(zone); |
60 | 104 |
61 if (*map == NULL) { | 105 if (*map == nullptr) { |
62 *map = new(zone->New(sizeof(ZoneHashMap))) | 106 *map = new(zone->New(sizeof(ZoneHashMap))) |
63 ZoneHashMap(ZoneHashMap::PointersMatch, | 107 ZoneHashMap(ZoneHashMap::PointersMatch, |
64 ZoneHashMap::kDefaultHashMapCapacity, allocator); | 108 ZoneHashMap::kDefaultHashMapCapacity, allocator); |
65 } | 109 } |
66 | 110 |
67 ZoneHashMap::Entry* p = | 111 ZoneHashMap::Entry* p = |
68 (*map)->Lookup(const_cast<void*>(name), hash, !IsFrozen(), allocator); | 112 (*map)->Lookup(const_cast<void*>(name), hash, !IsFrozen(), allocator); |
69 if (p == NULL) { | 113 if (p == nullptr) { |
70 // This didn't have name but was frozen already, that's an error. | 114 // This didn't have name but was frozen already, that's an error. |
71 *ok = false; | 115 *ok = false; |
72 } else if (p->value == NULL) { | 116 } else if (p->value == nullptr) { |
73 p->value = interface; | 117 p->value = interface; |
74 } else { | 118 } else { |
75 #ifdef DEBUG | 119 #ifdef DEBUG |
76 Nesting nested; | 120 Nesting nested; |
77 #endif | 121 #endif |
78 static_cast<Interface*>(p->value)->Unify(interface, zone, ok); | 122 static_cast<Interface*>(p->value)->Unify(interface, zone, ok); |
79 } | 123 } |
80 | 124 |
81 #ifdef DEBUG | 125 #ifdef DEBUG |
82 if (FLAG_print_interface_details) { | 126 if (FLAG_print_interface_details) { |
83 PrintF("%*sthis' = ", Nesting::current(), ""); | 127 PrintF("%*sthis' = ", Nesting::current(), ""); |
84 this->Print(Nesting::current()); | 128 this->Print(Nesting::current()); |
85 PrintF("%*s# Added.\n", Nesting::current(), ""); | 129 PrintF("%*s# Added.\n", Nesting::current(), ""); |
86 } | 130 } |
87 #endif | 131 #endif |
88 } | 132 } |
89 | 133 |
90 | 134 |
| 135 // --------------------------------------------------------------------------- |
| 136 // Unification. |
| 137 |
91 void Interface::Unify(Interface* that, Zone* zone, bool* ok) { | 138 void Interface::Unify(Interface* that, Zone* zone, bool* ok) { |
92 if (this->forward_) return this->Chase()->Unify(that, zone, ok); | 139 if (this->forward_) return this->Chase()->Unify(that, zone, ok); |
93 if (that->forward_) return this->Unify(that->Chase(), zone, ok); | 140 if (that->forward_) return this->Unify(that->Chase(), zone, ok); |
94 DCHECK(this->forward_ == NULL); | 141 DCHECK(this->forward_ == nullptr); |
95 DCHECK(that->forward_ == NULL); | 142 DCHECK(that->forward_ == nullptr); |
96 | 143 |
97 *ok = true; | 144 *ok = true; |
98 if (this == that) return; | 145 if (this == that) return; |
99 if (this->IsValue()) { | 146 if (this->IsValue()) { |
100 that->MakeValue(ok); | 147 that->MakeValue(ok); |
101 if (*ok && this->IsConst()) that->MakeConst(ok); | 148 if (*ok && this->IsConst()) that->MakeConst(ok); |
102 return; | 149 return; |
103 } | 150 } |
104 if (that->IsValue()) { | 151 if (that->IsValue()) { |
105 this->MakeValue(ok); | 152 this->MakeValue(ok); |
106 if (*ok && that->IsConst()) this->MakeConst(ok); | 153 if (*ok && that->IsConst()) this->MakeConst(ok); |
107 return; | 154 return; |
108 } | 155 } |
109 | 156 |
110 #ifdef DEBUG | 157 #ifdef DEBUG |
111 if (FLAG_print_interface_details) { | 158 if (FLAG_print_interface_details) { |
112 PrintF("%*s# Unifying...\n", Nesting::current(), ""); | 159 PrintF("%*s# Unifying...\n", Nesting::current(), ""); |
113 PrintF("%*sthis = ", Nesting::current(), ""); | 160 PrintF("%*sthis = ", Nesting::current(), ""); |
114 this->Print(Nesting::current()); | 161 this->Print(Nesting::current()); |
115 PrintF("%*sthat = ", Nesting::current(), ""); | 162 PrintF("%*sthat = ", Nesting::current(), ""); |
116 that->Print(Nesting::current()); | 163 that->Print(Nesting::current()); |
117 } | 164 } |
118 #endif | 165 #endif |
119 | 166 |
120 // Merge the smaller interface into the larger, for performance. | 167 // Merge the smaller interface into the larger, for performance. |
121 if (this->exports_ != NULL && (that->exports_ == NULL || | 168 if (this->exports_ != nullptr && (that->exports_ == nullptr || |
122 this->exports_->occupancy() >= that->exports_->occupancy())) { | 169 this->exports_->occupancy() >= that->exports_->occupancy())) { |
123 this->DoUnify(that, ok, zone); | 170 this->DoUnify(that, ok, zone); |
124 } else { | 171 } else { |
125 that->DoUnify(this, ok, zone); | 172 that->DoUnify(this, ok, zone); |
126 } | 173 } |
127 | 174 |
128 #ifdef DEBUG | 175 #ifdef DEBUG |
129 if (FLAG_print_interface_details) { | 176 if (FLAG_print_interface_details) { |
130 PrintF("%*sthis' = ", Nesting::current(), ""); | 177 PrintF("%*sthis' = ", Nesting::current(), ""); |
131 this->Print(Nesting::current()); | 178 this->Print(Nesting::current()); |
132 PrintF("%*sthat' = ", Nesting::current(), ""); | 179 PrintF("%*sthat' = ", Nesting::current(), ""); |
133 that->Print(Nesting::current()); | 180 that->Print(Nesting::current()); |
134 PrintF("%*s# Unified.\n", Nesting::current(), ""); | 181 PrintF("%*s# Unified.\n", Nesting::current(), ""); |
135 } | 182 } |
136 #endif | 183 #endif |
137 } | 184 } |
138 | 185 |
139 | 186 |
140 void Interface::DoUnify(Interface* that, bool* ok, Zone* zone) { | 187 void Interface::DoUnify(Interface* that, bool* ok, Zone* zone) { |
141 DCHECK(this->forward_ == NULL); | 188 DCHECK(this->forward_ == nullptr); |
142 DCHECK(that->forward_ == NULL); | 189 DCHECK(that->forward_ == nullptr); |
143 DCHECK(!this->IsValue()); | 190 DCHECK(!this->IsValue()); |
144 DCHECK(!that->IsValue()); | 191 DCHECK(!that->IsValue()); |
145 DCHECK(this->index_ == -1); | 192 DCHECK(this->index_ == -1); |
146 DCHECK(that->index_ == -1); | 193 DCHECK(that->index_ == -1); |
147 DCHECK(*ok); | 194 DCHECK(*ok); |
148 | 195 |
149 #ifdef DEBUG | 196 #ifdef DEBUG |
150 Nesting nested; | 197 Nesting nested; |
151 #endif | 198 #endif |
152 | 199 |
153 // Try to merge all members from that into this. | 200 // Try to merge all members from that into this. |
154 ZoneHashMap* map = that->exports_; | 201 ZoneHashMap* map = that->exports_; |
155 if (map != NULL) { | 202 if (map != nullptr) { |
156 for (ZoneHashMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) { | 203 for (ZoneHashMap::Entry* p = map->Start(); p != nullptr; p = map->Next(p)) { |
157 this->DoAdd(p->key, p->hash, static_cast<Interface*>(p->value), zone, ok); | 204 this->DoAdd(p->key, p->hash, static_cast<Interface*>(p->value), zone, ok); |
158 if (!*ok) return; | 205 if (!*ok) return; |
159 } | 206 } |
160 } | 207 } |
161 | 208 |
162 // If the new interface is larger than that's, then there were members in | 209 // If the new interface is larger than that's, then there were members in |
163 // 'this' which 'that' didn't have. If 'that' was frozen that is an error. | 210 // 'this' which 'that' didn't have. If 'that' was frozen that is an error. |
164 int this_size = this->exports_ == NULL ? 0 : this->exports_->occupancy(); | 211 int this_size = this->exports_ == nullptr ? 0 : this->exports_->occupancy(); |
165 int that_size = map == NULL ? 0 : map->occupancy(); | 212 int that_size = map == nullptr ? 0 : map->occupancy(); |
166 if (that->IsFrozen() && this_size > that_size) { | 213 if (that->IsFrozen() && this_size > that_size) { |
167 *ok = false; | 214 *ok = false; |
168 return; | 215 return; |
169 } | 216 } |
170 | 217 |
171 // Merge interfaces. | 218 // Merge interfaces. |
172 this->flags_ |= that->flags_; | 219 this->flags_ |= that->flags_; |
173 that->forward_ = this; | 220 that->forward_ = this; |
174 } | 221 } |
175 | 222 |
176 | 223 |
| 224 // --------------------------------------------------------------------------- |
| 225 // Printing. |
| 226 |
177 #ifdef DEBUG | 227 #ifdef DEBUG |
178 void Interface::Print(int n) { | 228 void Interface::Print(int n) { |
179 int n0 = n > 0 ? n : 0; | 229 int n0 = n > 0 ? n : 0; |
180 | 230 |
181 if (FLAG_print_interface_details) { | 231 if (FLAG_print_interface_details) { |
182 PrintF("%p", static_cast<void*>(this)); | 232 PrintF("%p", static_cast<void*>(this)); |
183 for (Interface* link = this->forward_; link != NULL; link = link->forward_) | 233 for (Interface* link = this->forward_; link != nullptr; |
| 234 link = link->forward_) { |
184 PrintF("->%p", static_cast<void*>(link)); | 235 PrintF("->%p", static_cast<void*>(link)); |
| 236 } |
185 PrintF(" "); | 237 PrintF(" "); |
186 } | 238 } |
187 | 239 |
188 if (IsUnknown()) { | 240 if (IsUnknown()) { |
189 PrintF("unknown\n"); | 241 PrintF("unknown\n"); |
190 } else if (IsConst()) { | 242 } else if (IsConst()) { |
191 PrintF("const\n"); | 243 PrintF("const\n"); |
192 } else if (IsValue()) { | 244 } else if (IsValue()) { |
193 PrintF("value\n"); | 245 PrintF("value\n"); |
194 } else if (IsModule()) { | 246 } else if (IsModule()) { |
195 PrintF("module %d %s{", Index(), IsFrozen() ? "" : "(unresolved) "); | 247 PrintF("module %d %s{", Index(), IsFrozen() ? "" : "(unresolved) "); |
196 ZoneHashMap* map = Chase()->exports_; | 248 ZoneHashMap* map = Chase()->exports_; |
197 if (map == NULL || map->occupancy() == 0) { | 249 if (map == nullptr || map->occupancy() == 0) { |
198 PrintF("}\n"); | 250 PrintF("}\n"); |
199 } else if (n < 0 || n0 >= 2 * FLAG_print_interface_depth) { | 251 } else if (n < 0 || n0 >= 2 * FLAG_print_interface_depth) { |
200 // Avoid infinite recursion on cyclic types. | 252 // Avoid infinite recursion on cyclic types. |
201 PrintF("...}\n"); | 253 PrintF("...}\n"); |
202 } else { | 254 } else { |
203 PrintF("\n"); | 255 PrintF("\n"); |
204 for (ZoneHashMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) { | 256 for (ZoneHashMap::Entry* p = map->Start(); |
| 257 p != nullptr; p = map->Next(p)) { |
205 String* name = *static_cast<String**>(p->key); | 258 String* name = *static_cast<String**>(p->key); |
206 Interface* interface = static_cast<Interface*>(p->value); | 259 Interface* interface = static_cast<Interface*>(p->value); |
207 PrintF("%*s%s : ", n0 + 2, "", name->ToAsciiArray()); | 260 PrintF("%*s%s : ", n0 + 2, "", name->ToAsciiArray()); |
208 interface->Print(n0 + 2); | 261 interface->Print(n0 + 2); |
209 } | 262 } |
210 PrintF("%*s}\n", n0, ""); | 263 PrintF("%*s}\n", n0, ""); |
211 } | 264 } |
212 } | 265 } |
213 } | 266 } |
214 #endif | 267 #endif |
215 | 268 |
216 } } // namespace v8::internal | 269 } } // namespace v8::internal |
OLD | NEW |