OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 // Consequently, do not use pointer equality for type tests, always use Is! | 87 // Consequently, do not use pointer equality for type tests, always use Is! |
88 // | 88 // |
89 // Internally, all 'primitive' types, and their unions, are represented as | 89 // Internally, all 'primitive' types, and their unions, are represented as |
90 // bitsets via smis. Class is a heap pointer to the respective map. Only | 90 // bitsets via smis. Class is a heap pointer to the respective map. Only |
91 // Constant's, or unions containing Class'es or Constant's, require allocation. | 91 // Constant's, or unions containing Class'es or Constant's, require allocation. |
92 // Note that the bitset representation is closed under both Union and Intersect. | 92 // Note that the bitset representation is closed under both Union and Intersect. |
93 // | 93 // |
94 // The type representation is heap-allocated, so cannot (currently) be used in | 94 // The type representation is heap-allocated, so cannot (currently) be used in |
95 // a parallel compilation context. | 95 // a parallel compilation context. |
96 | 96 |
| 97 |
| 98 #define PRIMITIVE_TYPE_LIST(V) \ |
| 99 V(None, 0) \ |
| 100 V(Null, 1 << 0) \ |
| 101 V(Undefined, 1 << 1) \ |
| 102 V(Boolean, 1 << 2) \ |
| 103 V(Smi, 1 << 3) \ |
| 104 V(OtherSigned32, 1 << 4) \ |
| 105 V(Unsigned32, 1 << 5) \ |
| 106 V(Double, 1 << 6) \ |
| 107 V(Symbol, 1 << 7) \ |
| 108 V(InternalizedString, 1 << 8) \ |
| 109 V(OtherString, 1 << 9) \ |
| 110 V(Undetectable, 1 << 10) \ |
| 111 V(Array, 1 << 11) \ |
| 112 V(Function, 1 << 12) \ |
| 113 V(RegExp, 1 << 13) \ |
| 114 V(OtherObject, 1 << 14) \ |
| 115 V(Proxy, 1 << 15) \ |
| 116 V(Internal, 1 << 16) |
| 117 |
| 118 #define COMPOSED_TYPE_LIST(V) \ |
| 119 V(Oddball, kBoolean | kNull | kUndefined) \ |
| 120 V(Signed32, kSmi | kOtherSigned32) \ |
| 121 V(Number, kSigned32 | kUnsigned32 | kDouble) \ |
| 122 V(String, kInternalizedString | kOtherString) \ |
| 123 V(UniqueName, kSymbol | kInternalizedString) \ |
| 124 V(Name, kSymbol | kString) \ |
| 125 V(NumberOrString, kNumber | kString) \ |
| 126 V(Object, kUndetectable | kArray | kFunction | \ |
| 127 kRegExp | kOtherObject) \ |
| 128 V(Receiver, kObject | kProxy) \ |
| 129 V(Allocated, kDouble | kName | kReceiver) \ |
| 130 V(Any, kOddball | kNumber | kAllocated | kInternal) \ |
| 131 V(Detectable, kAllocated - kUndetectable) |
| 132 |
| 133 #define TYPE_LIST(V) \ |
| 134 PRIMITIVE_TYPE_LIST(V) \ |
| 135 COMPOSED_TYPE_LIST(V) |
| 136 |
| 137 |
| 138 |
97 class Type : public Object { | 139 class Type : public Object { |
98 public: | 140 public: |
99 static Type* None() { return from_bitset(kNone); } | 141 #define DEFINE_TYPE_CONSTRUCTOR(type, value) \ |
100 static Type* Any() { return from_bitset(kAny); } | 142 static Type* type() { return from_bitset(k##type); } |
101 static Type* Allocated() { return from_bitset(kAllocated); } | 143 TYPE_LIST(DEFINE_TYPE_CONSTRUCTOR) |
102 static Type* Detectable() { return from_bitset(kDetectable); } | 144 #undef DEFINE_TYPE_CONSTRUCTOR |
103 | |
104 static Type* Oddball() { return from_bitset(kOddball); } | |
105 static Type* Boolean() { return from_bitset(kBoolean); } | |
106 static Type* Null() { return from_bitset(kNull); } | |
107 static Type* Undefined() { return from_bitset(kUndefined); } | |
108 | |
109 static Type* Number() { return from_bitset(kNumber); } | |
110 static Type* Smi() { return from_bitset(kSmi); } | |
111 static Type* Signed32() { return from_bitset(kSigned32); } | |
112 static Type* Unsigned32() { return from_bitset(kUnsigned32); } | |
113 static Type* Double() { return from_bitset(kDouble); } | |
114 static Type* NumberOrString() { return from_bitset(kNumberOrString); } | |
115 | |
116 static Type* Name() { return from_bitset(kName); } | |
117 static Type* UniqueName() { return from_bitset(kUniqueName); } | |
118 static Type* String() { return from_bitset(kString); } | |
119 static Type* InternalizedString() { return from_bitset(kInternalizedString); } | |
120 static Type* Symbol() { return from_bitset(kSymbol); } | |
121 | |
122 static Type* Receiver() { return from_bitset(kReceiver); } | |
123 static Type* Object() { return from_bitset(kObject); } | |
124 static Type* Undetectable() { return from_bitset(kUndetectable); } | |
125 static Type* Array() { return from_bitset(kArray); } | |
126 static Type* Function() { return from_bitset(kFunction); } | |
127 static Type* RegExp() { return from_bitset(kRegExp); } | |
128 static Type* Proxy() { return from_bitset(kProxy); } | |
129 static Type* Internal() { return from_bitset(kInternal); } | |
130 | 145 |
131 static Type* Class(Handle<Map> map) { return from_handle(map); } | 146 static Type* Class(Handle<Map> map) { return from_handle(map); } |
132 static Type* Constant(Handle<HeapObject> value) { | 147 static Type* Constant(Handle<HeapObject> value) { |
133 return Constant(value, value->GetIsolate()); | 148 return Constant(value, value->GetIsolate()); |
134 } | 149 } |
135 static Type* Constant(Handle<v8::internal::Object> value, Isolate* isolate) { | 150 static Type* Constant(Handle<v8::internal::Object> value, Isolate* isolate) { |
136 return from_handle(isolate->factory()->NewBox(value)); | 151 return from_handle(isolate->factory()->NewBox(value)); |
137 } | 152 } |
138 | 153 |
139 static Type* Union(Handle<Type> type1, Handle<Type> type2); | 154 static Type* Union(Handle<Type> type1, Handle<Type> type2); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 | 192 |
178 Iterator<Map> Classes() { | 193 Iterator<Map> Classes() { |
179 if (this->is_bitset()) return Iterator<Map>(); | 194 if (this->is_bitset()) return Iterator<Map>(); |
180 return Iterator<Map>(this->handle()); | 195 return Iterator<Map>(this->handle()); |
181 } | 196 } |
182 Iterator<v8::internal::Object> Constants() { | 197 Iterator<v8::internal::Object> Constants() { |
183 if (this->is_bitset()) return Iterator<v8::internal::Object>(); | 198 if (this->is_bitset()) return Iterator<v8::internal::Object>(); |
184 return Iterator<v8::internal::Object>(this->handle()); | 199 return Iterator<v8::internal::Object>(this->handle()); |
185 } | 200 } |
186 | 201 |
| 202 #ifdef OBJECT_PRINT |
| 203 void TypePrint(); |
| 204 void TypePrint(FILE* out); |
| 205 #endif |
| 206 |
187 private: | 207 private: |
188 // A union is a fixed array containing types. Invariants: | 208 // A union is a fixed array containing types. Invariants: |
189 // - its length is at least 2 | 209 // - its length is at least 2 |
190 // - at most one field is a bitset, and it must go into index 0 | 210 // - at most one field is a bitset, and it must go into index 0 |
191 // - no field is a union | 211 // - no field is a union |
192 typedef FixedArray Unioned; | 212 typedef FixedArray Unioned; |
193 | 213 |
194 enum { | 214 enum { |
195 kNull = 1 << 0, | 215 #define DECLARE_TYPE(type, value) k##type = (value), |
196 kUndefined = 1 << 1, | 216 TYPE_LIST(DECLARE_TYPE) |
197 kBoolean = 1 << 2, | 217 #undef DECLARE_TYPE |
198 kSmi = 1 << 3, | 218 kUnusedEOL = 0 |
199 kOtherSigned32 = 1 << 4, | |
200 kUnsigned32 = 1 << 5, | |
201 kDouble = 1 << 6, | |
202 kSymbol = 1 << 7, | |
203 kInternalizedString = 1 << 8, | |
204 kOtherString = 1 << 9, | |
205 kUndetectable = 1 << 10, | |
206 kArray = 1 << 11, | |
207 kFunction = 1 << 12, | |
208 kRegExp = 1 << 13, | |
209 kOtherObject = 1 << 14, | |
210 kProxy = 1 << 15, | |
211 kInternal = 1 << 16, | |
212 | |
213 kOddball = kBoolean | kNull | kUndefined, | |
214 kSigned32 = kSmi | kOtherSigned32, | |
215 kNumber = kSigned32 | kUnsigned32 | kDouble, | |
216 kString = kInternalizedString | kOtherString, | |
217 kUniqueName = kSymbol | kInternalizedString, | |
218 kName = kSymbol | kString, | |
219 kNumberOrString = kNumber | kString, | |
220 kObject = kUndetectable | kArray | kFunction | kRegExp | kOtherObject, | |
221 kReceiver = kObject | kProxy, | |
222 kAllocated = kDouble | kName | kReceiver, | |
223 kAny = kOddball | kNumber | kAllocated | kInternal, | |
224 kDetectable = kAllocated - kUndetectable, | |
225 kNone = 0 | |
226 }; | 219 }; |
227 | 220 |
228 bool is_bitset() { return this->IsSmi(); } | 221 bool is_bitset() { return this->IsSmi(); } |
229 bool is_class() { return this->IsMap(); } | 222 bool is_class() { return this->IsMap(); } |
230 bool is_constant() { return this->IsBox(); } | 223 bool is_constant() { return this->IsBox(); } |
231 bool is_union() { return this->IsFixedArray(); } | 224 bool is_union() { return this->IsFixedArray(); } |
232 | 225 |
233 bool IsSlowCase(Type* that); | 226 bool IsSlowCase(Type* that); |
234 | 227 |
235 int as_bitset() { return Smi::cast(this)->value(); } | 228 int as_bitset() { return Smi::cast(this)->value(); } |
(...skipping 22 matching lines...) Expand all Loading... |
258 ASSERT(!type->is_union()); | 251 ASSERT(!type->is_union()); |
259 return type->handle_via_isolate_of(from_handle(unioned)); | 252 return type->handle_via_isolate_of(from_handle(unioned)); |
260 } | 253 } |
261 | 254 |
262 int LubBitset(); // least upper bound that's a bitset | 255 int LubBitset(); // least upper bound that's a bitset |
263 int GlbBitset(); // greatest lower bound that's a bitset | 256 int GlbBitset(); // greatest lower bound that's a bitset |
264 bool InUnion(Handle<Unioned> unioned, int current_size); | 257 bool InUnion(Handle<Unioned> unioned, int current_size); |
265 int ExtendUnion(Handle<Unioned> unioned, int current_size); | 258 int ExtendUnion(Handle<Unioned> unioned, int current_size); |
266 int ExtendIntersection( | 259 int ExtendIntersection( |
267 Handle<Unioned> unioned, Handle<Type> type, int current_size); | 260 Handle<Unioned> unioned, Handle<Type> type, int current_size); |
| 261 |
| 262 static const char* GetComposedName(int type) { |
| 263 switch (type) { |
| 264 #define PRINT_COMPOSED_TYPE(type, value) \ |
| 265 case k##type: \ |
| 266 return # type; |
| 267 COMPOSED_TYPE_LIST(PRINT_COMPOSED_TYPE) |
| 268 #undef PRINT_COMPOSED_TYPE |
| 269 } |
| 270 return NULL; |
| 271 } |
| 272 |
| 273 static const char* GetPrimitiveName(int type) { |
| 274 switch (type) { |
| 275 #define PRINT_PRIMITIVE_TYPE(type, value) \ |
| 276 case k##type: \ |
| 277 return # type; |
| 278 PRIMITIVE_TYPE_LIST(PRINT_PRIMITIVE_TYPE) |
| 279 #undef PRINT_PRIMITIVE_TYPE |
| 280 default: |
| 281 UNREACHABLE(); |
| 282 return "InvalidType"; |
| 283 } |
| 284 } |
268 }; | 285 }; |
269 | 286 |
270 } } // namespace v8::internal | 287 } } // namespace v8::internal |
271 | 288 |
272 #endif // V8_TYPES_H_ | 289 #endif // V8_TYPES_H_ |
OLD | NEW |