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
3 // found in the LICENSE file.
12 static bool Match(void* key1, void* key2) {
13 String* name1 = *static_cast<String**>(key1);
14 String* name2 = *static_cast<String**>(key2);
15 ASSERT(name1->IsInternalizedString());
16 ASSERT(name2->IsInternalizedString());
17 return name1 == name2;
21 Interface* Interface::Lookup(Handle<String> name, Zone* zone) {
23 ZoneHashMap* map = Chase()->exports_;
24 if (map == NULL) return NULL;
25 ZoneAllocationPolicy allocator(zone);
26 ZoneHashMap::Entry* p = map->Lookup(name.location(), name->Hash(), false,
28 if (p == NULL) return NULL;
29 ASSERT(*static_cast<String**>(p->key) == *name);
30 ASSERT(p->value != NULL);
31 return static_cast<Interface*>(p->value);
36 // Current nesting depth for debug output.
39 Nesting() { current_ += 2; }
40 ~Nesting() { current_ -= 2; }
41 static int current() { return current_; }
46 int Nesting::current_ = 0;
50 void Interface::DoAdd(
51 void* name, uint32_t hash, Interface* interface, Zone* zone, bool* ok) {
56 if (FLAG_print_interface_details) {
57 PrintF("%*s# Adding...\n", Nesting::current(), "");
58 PrintF("%*sthis = ", Nesting::current(), "");
59 this->Print(Nesting::current());
60 PrintF("%*s%s : ", Nesting::current(), "",
61 (*static_cast<String**>(name))->ToAsciiArray());
62 interface->Print(Nesting::current());
66 ZoneHashMap** map = &Chase()->exports_;
67 ZoneAllocationPolicy allocator(zone);
70 *map = new(zone->New(sizeof(ZoneHashMap)))
71 ZoneHashMap(Match, ZoneHashMap::kDefaultHashMapCapacity, allocator);
74 ZoneHashMap::Entry* p = (*map)->Lookup(name, hash, !IsFrozen(), allocator);
76 // This didn't have name but was frozen already, that's an error.
78 } else if (p->value == NULL) {
84 static_cast<Interface*>(p->value)->Unify(interface, zone, ok);
88 if (FLAG_print_interface_details) {
89 PrintF("%*sthis' = ", Nesting::current(), "");
90 this->Print(Nesting::current());
91 PrintF("%*s# Added.\n", Nesting::current(), "");
97 void Interface::Unify(Interface* that, Zone* zone, bool* ok) {
98 if (this->forward_) return this->Chase()->Unify(that, zone, ok);
99 if (that->forward_) return this->Unify(that->Chase(), zone, ok);
100 ASSERT(this->forward_ == NULL);
101 ASSERT(that->forward_ == NULL);
104 if (this == that) return;
105 if (this->IsValue()) {
107 if (*ok && this->IsConst()) that->MakeConst(ok);
110 if (that->IsValue()) {
112 if (*ok && that->IsConst()) this->MakeConst(ok);
117 if (FLAG_print_interface_details) {
118 PrintF("%*s# Unifying...\n", Nesting::current(), "");
119 PrintF("%*sthis = ", Nesting::current(), "");
120 this->Print(Nesting::current());
121 PrintF("%*sthat = ", Nesting::current(), "");
122 that->Print(Nesting::current());
126 // Merge the smaller interface into the larger, for performance.
127 if (this->exports_ != NULL && (that->exports_ == NULL ||
128 this->exports_->occupancy() >= that->exports_->occupancy())) {
129 this->DoUnify(that, ok, zone);
131 that->DoUnify(this, ok, zone);
135 if (FLAG_print_interface_details) {
136 PrintF("%*sthis' = ", Nesting::current(), "");
137 this->Print(Nesting::current());
138 PrintF("%*sthat' = ", Nesting::current(), "");
139 that->Print(Nesting::current());
140 PrintF("%*s# Unified.\n", Nesting::current(), "");
146 void Interface::DoUnify(Interface* that, bool* ok, Zone* zone) {
147 ASSERT(this->forward_ == NULL);
148 ASSERT(that->forward_ == NULL);
149 ASSERT(!this->IsValue());
150 ASSERT(!that->IsValue());
151 ASSERT(this->index_ == -1);
152 ASSERT(that->index_ == -1);
159 // Try to merge all members from that into this.
160 ZoneHashMap* map = that->exports_;
162 for (ZoneHashMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
163 this->DoAdd(p->key, p->hash, static_cast<Interface*>(p->value), zone, ok);
168 // If the new interface is larger than that's, then there were members in
169 // 'this' which 'that' didn't have. If 'that' was frozen that is an error.
170 int this_size = this->exports_ == NULL ? 0 : this->exports_->occupancy();
171 int that_size = map == NULL ? 0 : map->occupancy();
172 if (that->IsFrozen() && this_size > that_size) {
178 this->flags_ |= that->flags_;
179 that->forward_ = this;
184 void Interface::Print(int n) {
185 int n0 = n > 0 ? n : 0;
187 if (FLAG_print_interface_details) {
188 PrintF("%p", static_cast<void*>(this));
189 for (Interface* link = this->forward_; link != NULL; link = link->forward_)
190 PrintF("->%p", static_cast<void*>(link));
196 } else if (IsConst()) {
198 } else if (IsValue()) {
200 } else if (IsModule()) {
201 PrintF("module %d %s{", Index(), IsFrozen() ? "" : "(unresolved) ");
202 ZoneHashMap* map = Chase()->exports_;
203 if (map == NULL || map->occupancy() == 0) {
205 } else if (n < 0 || n0 >= 2 * FLAG_print_interface_depth) {
206 // Avoid infinite recursion on cyclic types.
210 for (ZoneHashMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
211 String* name = *static_cast<String**>(p->key);
212 Interface* interface = static_cast<Interface*>(p->value);
213 PrintF("%*s%s : ", n0 + 2, "", name->ToAsciiArray());
214 interface->Print(n0 + 2);
216 PrintF("%*s}\n", n0, "");
222 } } // namespace v8::internal