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 #ifndef V8_INTERFACE_H_ | 5 #ifndef V8_INTERFACE_H_ |
6 #define V8_INTERFACE_H_ | 6 #define V8_INTERFACE_H_ |
7 | 7 |
8 #include "src/ast-value-factory.h" | 8 #include "src/zone.h" |
9 | 9 |
10 namespace v8 { | 10 namespace v8 { |
11 namespace internal { | 11 namespace internal { |
12 | 12 |
13 | 13 |
14 // This class implements the following abstract grammar of interfaces | 14 class AstRawString; |
15 // (i.e. module types): | 15 |
16 // interface ::= UNDETERMINED | VALUE | CONST | MODULE(exports) | 16 |
17 // exports ::= {name : interface, ...} | 17 // This class represents the interface of a module: a set of exported names. |
18 // A frozen type is one that is fully determined. Unification does not | |
19 // allow to turn non-const values into const, or adding additional exports to | |
20 // frozen interfaces. Otherwise, unifying modules merges their exports. | |
21 // Undetermined types are unification variables that can be unified freely. | |
22 // There is a natural subsort lattice that reflects the increase of knowledge: | |
23 // | 18 // |
24 // undetermined | 19 // TODO(adamk): Rename this to ModuleRecord, ModuleDescriptor, or similar. |
25 // // | \\ . | |
26 // value (frozen) module | |
27 // // \\ / \ // | |
28 // const fr.value fr.module | |
29 // \\ / | |
30 // fr.const | |
31 // | |
32 // where the bold lines are the only transitions allowed. | |
33 | |
34 class Interface : public ZoneObject { | 20 class Interface : public ZoneObject { |
35 public: | 21 public: |
36 // --------------------------------------------------------------------------- | 22 // --------------------------------------------------------------------------- |
37 // Factory methods. | 23 // Factory methods. |
38 | 24 |
39 static Interface* NewUnknown(Zone* zone) { | 25 static Interface* New(Zone* zone) { return new (zone) Interface(); } |
40 return new(zone) Interface(NONE); | |
41 } | |
42 | |
43 static Interface* NewValue(); | |
44 | |
45 static Interface* NewConst(); | |
46 | |
47 static Interface* NewModule(Zone* zone) { | |
48 return new(zone) Interface(MODULE); | |
49 } | |
50 | 26 |
51 // --------------------------------------------------------------------------- | 27 // --------------------------------------------------------------------------- |
52 // Mutators. | 28 // Mutators. |
53 | 29 |
54 // Add a name to the list of exports. If it already exists, unify with | 30 // Add a name to the list of exports. If it already exists, or this interface |
55 // interface, otherwise insert unless this is closed. | 31 // is frozen, that's an error. |
56 void Add(const AstRawString* name, Interface* interface, Zone* zone, | 32 void Add(const AstRawString* name, Zone* zone, bool* ok); |
57 bool* ok) { | |
58 DoAdd(name, name->hash(), interface, zone, ok); | |
59 } | |
60 | |
61 // Unify with another interface. If successful, both interface objects will | |
62 // represent the same type, and changes to one are reflected in the other. | |
63 void Unify(Interface* that, Zone* zone, bool* ok); | |
64 | |
65 // Determine this interface to be a value interface. | |
66 void MakeValue(bool* ok) { | |
67 *ok = !IsModule(); | |
68 if (*ok) Chase()->flags_ |= VALUE; | |
69 } | |
70 | |
71 // Determine this interface to be an immutable interface. | |
72 void MakeConst(bool* ok) { | |
73 *ok = !IsModule() && (IsConst() || !IsFrozen()); | |
74 if (*ok) Chase()->flags_ |= VALUE + CONST; | |
75 } | |
76 | |
77 // Determine this interface to be a module interface. | |
78 void MakeModule(bool* ok) { | |
79 *ok = !IsValue(); | |
80 if (*ok) Chase()->flags_ |= MODULE; | |
81 } | |
82 | 33 |
83 // Do not allow any further refinements, directly or through unification. | 34 // Do not allow any further refinements, directly or through unification. |
84 void Freeze(bool* ok) { | 35 void Freeze() { frozen_ = true; } |
85 *ok = IsValue() || IsModule(); | |
86 if (*ok) Chase()->flags_ |= FROZEN; | |
87 } | |
88 | 36 |
89 // Assign an index. | 37 // Assign an index. |
90 void Allocate(int index) { | 38 void Allocate(int index) { |
91 DCHECK(IsModule() && IsFrozen() && Chase()->index_ == -1); | 39 DCHECK(IsFrozen() && index_ == -1); |
92 Chase()->index_ = index; | 40 index_ = index; |
93 } | 41 } |
94 | 42 |
95 // --------------------------------------------------------------------------- | 43 // --------------------------------------------------------------------------- |
96 // Accessors. | 44 // Accessors. |
97 | 45 |
98 // Check whether this is still a fully undetermined type. | |
99 bool IsUnknown() { return Chase()->flags_ == NONE; } | |
100 | |
101 // Check whether this is a value type. | |
102 bool IsValue() { return Chase()->flags_ & VALUE; } | |
103 | |
104 // Check whether this is a constant type. | |
105 bool IsConst() { return Chase()->flags_ & CONST; } | |
106 | |
107 // Check whether this is a module type. | |
108 bool IsModule() { return Chase()->flags_ & MODULE; } | |
109 | |
110 // Check whether this is closed (i.e. fully determined). | 46 // Check whether this is closed (i.e. fully determined). |
111 bool IsFrozen() { return Chase()->flags_ & FROZEN; } | 47 bool IsFrozen() { return frozen_; } |
112 | |
113 bool IsUnified(Interface* that) { | |
114 return Chase() == that->Chase() | |
115 || (this->IsValue() == that->IsValue() && | |
116 this->IsConst() == that->IsConst()); | |
117 } | |
118 | 48 |
119 int Length() { | 49 int Length() { |
120 DCHECK(IsModule() && IsFrozen()); | 50 DCHECK(IsFrozen()); |
121 ZoneHashMap* exports = Chase()->exports_; | 51 ZoneHashMap* exports = exports_; |
122 return exports ? exports->occupancy() : 0; | 52 return exports ? exports->occupancy() : 0; |
123 } | 53 } |
124 | 54 |
125 // The context slot in the hosting script context pointing to this module. | 55 // The context slot in the hosting script context pointing to this module. |
126 int Index() { | 56 int Index() { |
127 DCHECK(IsModule() && IsFrozen()); | 57 DCHECK(IsFrozen()); |
128 return Chase()->index_; | 58 return index_; |
129 } | 59 } |
130 | 60 |
131 // Look up an exported name. Returns NULL if not (yet) defined. | |
132 Interface* Lookup(Handle<String> name, Zone* zone); | |
133 | |
134 // --------------------------------------------------------------------------- | 61 // --------------------------------------------------------------------------- |
135 // Iterators. | 62 // Iterators. |
136 | 63 |
137 // Use like: | 64 // Use like: |
138 // for (auto it = interface->iterator(); !it.done(); it.Advance()) { | 65 // for (auto it = interface->iterator(); !it.done(); it.Advance()) { |
139 // ... it.name() ... it.interface() ... | 66 // ... it.name() ... it.interface() ... |
140 // } | 67 // } |
141 class Iterator { | 68 class Iterator { |
142 public: | 69 public: |
143 bool done() const { return entry_ == NULL; } | 70 bool done() const { return entry_ == NULL; } |
144 const AstRawString* name() const { | 71 const AstRawString* name() const { |
145 DCHECK(!done()); | 72 DCHECK(!done()); |
146 return static_cast<const AstRawString*>(entry_->key); | 73 return static_cast<const AstRawString*>(entry_->key); |
147 } | 74 } |
148 Interface* interface() const { | |
149 DCHECK(!done()); | |
150 return static_cast<Interface*>(entry_->value); | |
151 } | |
152 void Advance() { entry_ = exports_->Next(entry_); } | 75 void Advance() { entry_ = exports_->Next(entry_); } |
153 | 76 |
154 private: | 77 private: |
155 friend class Interface; | 78 friend class Interface; |
156 explicit Iterator(const ZoneHashMap* exports) | 79 explicit Iterator(const ZoneHashMap* exports) |
157 : exports_(exports), entry_(exports ? exports->Start() : NULL) {} | 80 : exports_(exports), entry_(exports ? exports->Start() : NULL) {} |
158 | 81 |
159 const ZoneHashMap* exports_; | 82 const ZoneHashMap* exports_; |
160 ZoneHashMap::Entry* entry_; | 83 ZoneHashMap::Entry* entry_; |
161 }; | 84 }; |
162 | 85 |
163 Iterator iterator() const { return Iterator(this->exports_); } | 86 Iterator iterator() const { return Iterator(this->exports_); } |
164 | 87 |
165 // --------------------------------------------------------------------------- | 88 // --------------------------------------------------------------------------- |
166 // Debugging. | 89 // Debugging. |
167 #ifdef DEBUG | 90 #ifdef DEBUG |
168 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively | 91 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively |
169 #endif | 92 #endif |
170 | 93 |
171 // --------------------------------------------------------------------------- | 94 // --------------------------------------------------------------------------- |
172 // Implementation. | 95 // Implementation. |
173 private: | 96 private: |
174 struct Cache; | 97 bool frozen_; |
175 | |
176 enum Flags { // All flags are monotonic | |
177 NONE = 0, | |
178 VALUE = 1, // This type describes a value | |
179 CONST = 2, // This type describes a constant | |
180 MODULE = 4, // This type describes a module | |
181 FROZEN = 8 // This type is fully determined | |
182 }; | |
183 | |
184 int flags_; | |
185 Interface* forward_; // Unification link | |
186 ZoneHashMap* exports_; // Module exports and their types (allocated lazily) | 98 ZoneHashMap* exports_; // Module exports and their types (allocated lazily) |
187 int index_; | 99 int index_; |
188 | 100 |
189 explicit Interface(int flags) | 101 Interface() : frozen_(false), exports_(NULL), index_(-1) { |
190 : flags_(flags), | |
191 forward_(NULL), | |
192 exports_(NULL), | |
193 index_(-1) { | |
194 #ifdef DEBUG | 102 #ifdef DEBUG |
195 if (FLAG_print_interface_details) | 103 if (FLAG_print_interface_details) |
196 PrintF("# Creating %p\n", static_cast<void*>(this)); | 104 PrintF("# Creating %p\n", static_cast<void*>(this)); |
197 #endif | 105 #endif |
198 } | 106 } |
199 | |
200 Interface* Chase() { | |
201 Interface* result = this; | |
202 while (result->forward_ != NULL) result = result->forward_; | |
203 if (result != this) forward_ = result; // On-the-fly path compression. | |
204 return result; | |
205 } | |
206 | |
207 void DoAdd(const void* name, uint32_t hash, Interface* interface, Zone* zone, | |
208 bool* ok); | |
209 void DoUnify(Interface* that, bool* ok, Zone* zone); | |
210 }; | 107 }; |
211 | 108 |
212 } } // namespace v8::internal | 109 } } // namespace v8::internal |
213 | 110 |
214 #endif // V8_INTERFACE_H_ | 111 #endif // V8_INTERFACE_H_ |
OLD | NEW |