OLD | NEW |
---|---|
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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_TYPES_H_ | 5 #ifndef V8_TYPES_H_ |
6 #define V8_TYPES_H_ | 6 #define V8_TYPES_H_ |
7 | 7 |
8 #include "handles.h" | 8 #include "handles.h" |
9 | 9 |
10 namespace v8 { | 10 namespace v8 { |
(...skipping 21 matching lines...) Expand all Loading... | |
32 // | 32 // |
33 // Receiver = Object \/ Proxy | 33 // Receiver = Object \/ Proxy |
34 // Array < Object | 34 // Array < Object |
35 // Function < Object | 35 // Function < Object |
36 // RegExp < Object | 36 // RegExp < Object |
37 // Undetectable < Object | 37 // Undetectable < Object |
38 // Detectable = Receiver \/ Number \/ Name - Undetectable | 38 // Detectable = Receiver \/ Number \/ Name - Undetectable |
39 // | 39 // |
40 // Class(map) < T iff instance_type(map) < T | 40 // Class(map) < T iff instance_type(map) < T |
41 // Constant(x) < T iff instance_type(map(x)) < T | 41 // Constant(x) < T iff instance_type(map(x)) < T |
42 // Array(T) < Array | |
43 // Function(R, S, T0, T1, ...) < Function | |
42 // | 44 // |
45 // Both structural Array and Function types are invariant in all parameters. | |
46 // Relaxing this would make Union and Intersect operations more involved. | |
43 // Note that Constant(x) < Class(map(x)) does _not_ hold, since x's map can | 47 // Note that Constant(x) < Class(map(x)) does _not_ hold, since x's map can |
44 // change! (Its instance type cannot, however.) | 48 // change! (Its instance type cannot, however.) |
45 // TODO(rossberg): the latter is not currently true for proxies, because of fix, | 49 // TODO(rossberg): the latter is not currently true for proxies, because of fix, |
46 // but will hold once we implement direct proxies. | 50 // but will hold once we implement direct proxies. |
47 // | 51 // |
48 // For the representation axis, the following holds: | 52 // For the representation axis, the following holds: |
49 // | 53 // |
50 // None <= R | 54 // None <= R |
51 // R <= Any | 55 // R <= Any |
52 // | 56 // |
(...skipping 22 matching lines...) Expand all Loading... | |
75 // T1->Maybe(T2) -- tests whether T1 and T2 overlap (i.e., T1 /\ T2 =/= 0) | 79 // T1->Maybe(T2) -- tests whether T1 and T2 overlap (i.e., T1 /\ T2 =/= 0) |
76 // | 80 // |
77 // Typically, the former is to be used to select representations (e.g., via | 81 // Typically, the former is to be used to select representations (e.g., via |
78 // T->Is(SignedSmall())), and the latter to check whether a specific case needs | 82 // T->Is(SignedSmall())), and the latter to check whether a specific case needs |
79 // handling (e.g., via T->Maybe(Number())). | 83 // handling (e.g., via T->Maybe(Number())). |
80 // | 84 // |
81 // There is no functionality to discover whether a type is a leaf in the | 85 // There is no functionality to discover whether a type is a leaf in the |
82 // lattice. That is intentional. It should always be possible to refine the | 86 // lattice. That is intentional. It should always be possible to refine the |
83 // lattice (e.g., splitting up number types further) without invalidating any | 87 // lattice (e.g., splitting up number types further) without invalidating any |
84 // existing assumptions or tests. | 88 // existing assumptions or tests. |
85 // Consequently, do not use pointer equality for type tests, always use Is! | 89 // Consequently, do not normally use Equals for type tests, always use Is! |
86 // | 90 // |
87 // Internally, all 'primitive' types, and their unions, are represented as | 91 // Internally, all 'primitive' types, and their unions, are represented as |
88 // bitsets. Class is a heap pointer to the respective map. Only Constant's, or | 92 // bitsets. Class is a heap pointer to the respective map. Only Constant's, or |
89 // unions containing Class'es or Constant's, currently require allocation. | 93 // unions containing Class'es or Constant's, currently require allocation. |
90 // Note that the bitset representation is closed under both Union and Intersect. | 94 // Note that the bitset representation is closed under both Union and Intersect. |
91 // | 95 // |
92 // There are two type representations, using different allocation: | 96 // There are two type representations, using different allocation: |
93 // | 97 // |
94 // - class Type (zone-allocated, for compiler and concurrent compilation) | 98 // - class Type (zone-allocated, for compiler and concurrent compilation) |
95 // - class HeapType (heap-allocated, for persistent types) | 99 // - class HeapType (heap-allocated, for persistent types) |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
156 kUndefined | kInternal) \ | 160 kUndefined | kInternal) \ |
157 V(Any, kNumber | kNonNumber) | 161 V(Any, kNumber | kNonNumber) |
158 | 162 |
159 #define BITSET_TYPE_LIST(V) \ | 163 #define BITSET_TYPE_LIST(V) \ |
160 MASK_BITSET_TYPE_LIST(V) \ | 164 MASK_BITSET_TYPE_LIST(V) \ |
161 REPRESENTATION_BITSET_TYPE_LIST(V) \ | 165 REPRESENTATION_BITSET_TYPE_LIST(V) \ |
162 SEMANTIC_BITSET_TYPE_LIST(V) | 166 SEMANTIC_BITSET_TYPE_LIST(V) |
163 | 167 |
164 | 168 |
165 // struct Config { | 169 // struct Config { |
170 // typedef TypeImpl<Config> Type; | |
166 // typedef Base; | 171 // typedef Base; |
167 // typedef Struct; | 172 // typedef Struct; |
168 // typedef Region; | 173 // typedef Region; |
169 // template<class> struct Handle { typedef type; } // No template typedefs... | 174 // template<class> struct Handle { typedef type; } // No template typedefs... |
170 // static Handle<Type>::type handle(Type* type); // !is_bitset(type) | 175 // template<class T> static Handle<T>::type handle(T* t); // !is_bitset(t) |
176 // template<class T> static Handle<T>::type cast(Handle<Type>::type); | |
171 // static bool is_bitset(Type*); | 177 // static bool is_bitset(Type*); |
172 // static bool is_class(Type*); | 178 // static bool is_class(Type*); |
173 // static bool is_constant(Type*); | 179 // static bool is_constant(Type*); |
174 // static bool is_struct(Type*); | 180 // static bool is_struct(Type*, int tag); |
175 // static int as_bitset(Type*); | 181 // static int as_bitset(Type*); |
176 // static i::Handle<i::Map> as_class(Type*); | 182 // static i::Handle<i::Map> as_class(Type*); |
177 // static i::Handle<i::Object> as_constant(Type*); | 183 // static i::Handle<i::Object> as_constant(Type*); |
178 // static Handle<Struct>::type as_struct(Type*); | 184 // static Handle<Struct>::type as_struct(Type*); |
179 // static Type* from_bitset(int bitset); | 185 // static Type* from_bitset(int bitset); |
180 // static Handle<Type>::type from_bitset(int bitset, Region*); | 186 // static Handle<Type>::type from_bitset(int bitset, Region*); |
181 // static Handle<Type>::type from_class(i::Handle<Map>, int lub, Region*); | 187 // static Handle<Type>::type from_class(i::Handle<Map>, int lub, Region*); |
182 // static Handle<Type>::type from_constant(i::Handle<Object>, int, Region*); | 188 // static Handle<Type>::type from_constant(i::Handle<Object>, int, Region*); |
183 // static Handle<Type>::type from_struct(Handle<Struct>::type); | 189 // static Handle<Type>::type from_struct(Handle<Struct>::type, int tag); |
184 // static Handle<Struct>::type struct_create(int tag, int length, Region*); | 190 // static Handle<Struct>::type struct_create(int tag, int length, Region*); |
185 // static void struct_shrink(Handle<Struct>::type, int length); | 191 // static void struct_shrink(Handle<Struct>::type, int length); |
186 // static int struct_tag(Handle<Struct>::type); | 192 // static int struct_tag(Handle<Struct>::type); |
187 // static int struct_length(Handle<Struct>::type); | 193 // static int struct_length(Handle<Struct>::type); |
188 // static Handle<Type>::type struct_get(Handle<Struct>::type, int); | 194 // static Handle<Type>::type struct_get(Handle<Struct>::type, int); |
189 // static void struct_set(Handle<Struct>::type, int, Handle<Type>::type); | 195 // static void struct_set(Handle<Struct>::type, int, Handle<Type>::type); |
190 // static int lub_bitset(Type*); | 196 // static int lub_bitset(Type*); |
191 // } | 197 // } |
192 template<class Config> | 198 template<class Config> |
193 class TypeImpl : public Config::Base { | 199 class TypeImpl : public Config::Base { |
194 public: | 200 public: |
201 class BitsetType; // Internal | |
202 class StructuralType; // Internal | |
203 class UnionType; // Internal | |
204 | |
205 class ClassType; | |
206 class ConstantType; | |
207 class ArrayType; | |
208 class FunctionType; | |
209 | |
195 typedef typename Config::template Handle<TypeImpl>::type TypeHandle; | 210 typedef typename Config::template Handle<TypeImpl>::type TypeHandle; |
211 typedef typename Config::template Handle<ClassType>::type ClassHandle; | |
212 typedef typename Config::template Handle<ConstantType>::type ConstantHandle; | |
213 typedef typename Config::template Handle<ArrayType>::type ArrayHandle; | |
214 typedef typename Config::template Handle<FunctionType>::type FunctionHandle; | |
215 typedef typename Config::template Handle<UnionType>::type UnionHandle; | |
196 typedef typename Config::Region Region; | 216 typedef typename Config::Region Region; |
197 | 217 |
198 #define DEFINE_TYPE_CONSTRUCTOR(type, value) \ | 218 #define DEFINE_TYPE_CONSTRUCTOR(type, value) \ |
199 static TypeImpl* type() { return Config::from_bitset(k##type); } \ | 219 static TypeImpl* type() { return BitsetType::New(BitsetType::k##type); } \ |
200 static TypeHandle type(Region* region) { \ | 220 static TypeHandle type(Region* region) { \ |
201 return Config::from_bitset(k##type, region); \ | 221 return BitsetType::New(BitsetType::k##type, region); \ |
202 } | 222 } |
203 BITSET_TYPE_LIST(DEFINE_TYPE_CONSTRUCTOR) | 223 BITSET_TYPE_LIST(DEFINE_TYPE_CONSTRUCTOR) |
204 #undef DEFINE_TYPE_CONSTRUCTOR | 224 #undef DEFINE_TYPE_CONSTRUCTOR |
205 | 225 |
206 static TypeHandle Class(i::Handle<i::Map> map, Region* region) { | 226 static TypeHandle Class(i::Handle<i::Map> map, Region* region) { |
207 return Config::from_class(map, LubBitset(*map), region); | 227 return ClassType::New(map, region); |
208 } | 228 } |
209 static TypeHandle Constant(i::Handle<i::Object> value, Region* region) { | 229 static TypeHandle Constant(i::Handle<i::Object> value, Region* region) { |
210 return Config::from_constant(value, LubBitset(*value), region); | 230 return ConstantType::New(value, region); |
231 } | |
232 static TypeHandle Array(TypeHandle element, Region* region) { | |
233 return ArrayType::New(element, region); | |
234 } | |
235 static FunctionHandle Function( | |
236 TypeHandle result, TypeHandle receiver, int arity, Region* region) { | |
237 return FunctionType::New(result, receiver, arity, region); | |
238 } | |
239 static TypeHandle Function(TypeHandle result, Region* region) { | |
240 return Function(result, Any(region), 0, region); | |
241 } | |
242 static TypeHandle Function( | |
243 TypeHandle result, TypeHandle param0, Region* region) { | |
244 FunctionHandle function = Function(result, Any(region), 1, region); | |
245 function->InitParameter(0, param0); | |
246 return function; | |
247 } | |
248 static TypeHandle Function( | |
249 TypeHandle result, TypeHandle param0, TypeHandle param1, Region* region) { | |
250 FunctionHandle function = Function(result, Any(region), 2, region); | |
251 function->InitParameter(0, param0); | |
252 function->InitParameter(1, param1); | |
253 return function; | |
211 } | 254 } |
212 | 255 |
213 static TypeHandle Union(TypeHandle type1, TypeHandle type2, Region* reg); | 256 static TypeHandle Union(TypeHandle type1, TypeHandle type2, Region* reg); |
214 static TypeHandle Intersect(TypeHandle type1, TypeHandle type2, Region* reg); | 257 static TypeHandle Intersect(TypeHandle type1, TypeHandle type2, Region* reg); |
215 | 258 |
216 static TypeHandle Of(i::Object* value, Region* region) { | 259 static TypeHandle Of(i::Object* value, Region* region) { |
217 return Config::from_bitset(LubBitset(value), region); | 260 return Config::from_bitset(BitsetType::Lub(value), region); |
218 } | 261 } |
219 static TypeHandle Of(i::Handle<i::Object> value, Region* region) { | 262 static TypeHandle Of(i::Handle<i::Object> value, Region* region) { |
220 return Of(*value, region); | 263 return Of(*value, region); |
221 } | 264 } |
222 | 265 |
223 bool Is(TypeImpl* that) { return this == that || this->SlowIs(that); } | 266 bool Is(TypeImpl* that) { return this == that || this->SlowIs(that); } |
224 template<class TypeHandle> | 267 template<class TypeHandle> |
225 bool Is(TypeHandle that) { return this->Is(*that); } | 268 bool Is(TypeHandle that) { return this->Is(*that); } |
226 | 269 |
227 bool Maybe(TypeImpl* that); | 270 bool Maybe(TypeImpl* that); |
228 template<class TypeHandle> | 271 template<class TypeHandle> |
229 bool Maybe(TypeHandle that) { return this->Maybe(*that); } | 272 bool Maybe(TypeHandle that) { return this->Maybe(*that); } |
230 | 273 |
274 bool Equals(TypeImpl* that) { return this->Is(that) && that->Is(this); } | |
275 template<class TypeHandle> | |
276 bool Equals(TypeHandle that) { return this->Equals(*that); } | |
277 | |
231 // Equivalent to Constant(value)->Is(this), but avoiding allocation. | 278 // Equivalent to Constant(value)->Is(this), but avoiding allocation. |
232 bool Contains(i::Object* val); | 279 bool Contains(i::Object* val); |
233 bool Contains(i::Handle<i::Object> val) { return this->Contains(*val); } | 280 bool Contains(i::Handle<i::Object> val) { return this->Contains(*val); } |
234 | 281 |
235 // State-dependent versions of Of and Is that consider subtyping between | 282 // State-dependent versions of Of and Is that consider subtyping between |
236 // a constant and its map class. | 283 // a constant and its map class. |
237 static TypeHandle NowOf(i::Object* value, Region* region); | 284 static TypeHandle NowOf(i::Object* value, Region* region); |
238 static TypeHandle NowOf(i::Handle<i::Object> value, Region* region) { | 285 static TypeHandle NowOf(i::Handle<i::Object> value, Region* region) { |
239 return NowOf(*value, region); | 286 return NowOf(*value, region); |
240 } | 287 } |
241 bool NowIs(TypeImpl* that); | 288 bool NowIs(TypeImpl* that); |
242 template<class TypeHandle> | 289 template<class TypeHandle> |
243 bool NowIs(TypeHandle that) { return this->NowIs(*that); } | 290 bool NowIs(TypeHandle that) { return this->NowIs(*that); } |
244 bool NowContains(i::Object* val); | 291 bool NowContains(i::Object* val); |
245 bool NowContains(i::Handle<i::Object> val) { return this->NowContains(*val); } | 292 bool NowContains(i::Handle<i::Object> val) { return this->NowContains(*val); } |
246 | 293 |
247 bool IsClass() { return Config::is_class(this); } | 294 bool IsClass() { return Config::is_class(this); } |
248 bool IsConstant() { return Config::is_constant(this); } | 295 bool IsConstant() { return Config::is_constant(this); } |
249 i::Handle<i::Map> AsClass() { return Config::as_class(this); } | 296 bool IsArray() { return Config::is_struct(this, StructuralType::kArrayTag); } |
250 i::Handle<i::Object> AsConstant() { return Config::as_constant(this); } | 297 bool IsFunction() { |
298 return Config::is_struct(this, StructuralType::kFunctionTag); | |
299 } | |
300 | |
301 ClassType* AsClass() { return ClassType::cast(this); } | |
Benedikt Meurer
2014/04/09 05:33:14
I really dislike this change. AsClass() and AsCons
rossberg
2014/04/09 10:59:25
As discussed offline, I prefer the consistency. It
| |
302 ConstantType* AsConstant() { return ConstantType::cast(this); } | |
303 ArrayType* AsArray() { return ArrayType::cast(this); } | |
304 FunctionType* AsFunction() { return FunctionType::cast(this); } | |
251 | 305 |
252 int NumClasses(); | 306 int NumClasses(); |
253 int NumConstants(); | 307 int NumConstants(); |
254 | 308 |
255 template<class T> | 309 template<class T> class Iterator; |
256 class Iterator { | 310 inline Iterator<i::Map> Classes(); |
257 public: | 311 inline Iterator<i::Object> Constants(); |
258 bool Done() const { return index_ < 0; } | 312 |
259 i::Handle<T> Current(); | 313 static inline TypeImpl* cast(typename Config::Base* object); |
260 void Advance(); | |
261 | |
262 private: | |
263 template<class> friend class TypeImpl; | |
264 | |
265 Iterator() : index_(-1) {} | |
266 explicit Iterator(TypeHandle type) : type_(type), index_(-1) { | |
267 Advance(); | |
268 } | |
269 | |
270 inline bool matches(TypeHandle type); | |
271 inline TypeHandle get_type(); | |
272 | |
273 TypeHandle type_; | |
274 int index_; | |
275 }; | |
276 | |
277 Iterator<i::Map> Classes() { | |
278 if (this->IsBitset()) return Iterator<i::Map>(); | |
279 return Iterator<i::Map>(Config::handle(this)); | |
280 } | |
281 Iterator<i::Object> Constants() { | |
282 if (this->IsBitset()) return Iterator<i::Object>(); | |
283 return Iterator<i::Object>(Config::handle(this)); | |
284 } | |
285 | |
286 static TypeImpl* cast(typename Config::Base* object) { | |
287 TypeImpl* t = static_cast<TypeImpl*>(object); | |
288 ASSERT(t->IsBitset() || t->IsClass() || t->IsConstant() || t->IsUnion()); | |
289 return t; | |
290 } | |
291 | 314 |
292 template<class OtherTypeImpl> | 315 template<class OtherTypeImpl> |
293 static TypeHandle Convert( | 316 static TypeHandle Convert( |
294 typename OtherTypeImpl::TypeHandle type, Region* region); | 317 typename OtherTypeImpl::TypeHandle type, Region* region); |
295 | 318 |
296 enum PrintDimension { BOTH_DIMS, SEMANTIC_DIM, REPRESENTATION_DIM }; | 319 enum PrintDimension { BOTH_DIMS, SEMANTIC_DIM, REPRESENTATION_DIM }; |
297 void TypePrint(PrintDimension = BOTH_DIMS); | 320 void TypePrint(PrintDimension = BOTH_DIMS); |
298 void TypePrint(FILE* out, PrintDimension = BOTH_DIMS); | 321 void TypePrint(FILE* out, PrintDimension = BOTH_DIMS); |
299 | 322 |
300 private: | 323 protected: |
301 template<class> friend class Iterator; | 324 template<class> friend class Iterator; |
302 template<class> friend class TypeImpl; | 325 template<class> friend class TypeImpl; |
303 friend struct ZoneTypeConfig; | 326 |
304 friend struct HeapTypeConfig; | 327 template<class T> |
305 | 328 static typename Config::template Handle<T>::type handle(T* type) { |
306 enum Tag { | 329 return Config::handle(type); |
307 kClassTag, | 330 } |
308 kConstantTag, | 331 |
309 kUnionTag | 332 bool IsNone() { return this == None(); } |
310 }; | 333 bool IsAny() { return this == Any(); } |
311 | 334 bool IsBitset() { return Config::is_bitset(this); } |
312 // A structured type contains a tag an a variable number of type fields. | 335 bool IsUnion() { return Config::is_struct(this, StructuralType::kUnionTag); } |
313 // A union is a structured type with the following invariants: | 336 |
314 // - its length is at least 2 | 337 int AsBitset() { |
315 // - at most one field is a bitset, and it must go into index 0 | 338 ASSERT(this->IsBitset()); |
316 // - no field is a union | 339 return static_cast<BitsetType*>(this)->Bitset(); |
317 typedef typename Config::Struct Struct; | 340 } |
318 typedef typename Config::template Handle<Struct>::type StructHandle; | 341 UnionType* AsUnion() { return UnionType::cast(this); } |
342 | |
343 bool SlowIs(TypeImpl* that); | |
344 | |
345 bool InUnion(UnionHandle unioned, int current_size); | |
346 static int ExtendUnion( | |
347 UnionHandle unioned, TypeHandle t, int current_size); | |
348 static int ExtendIntersection( | |
349 UnionHandle unioned, TypeHandle t, TypeHandle other, int current_size); | |
350 | |
351 int BitsetGlb() { return BitsetType::Glb(this); } | |
352 int BitsetLub() { return BitsetType::Lub(this); } | |
353 }; | |
354 | |
355 | |
356 template<class Config> | |
357 class TypeImpl<Config>::BitsetType : public TypeImpl<Config> { | |
358 private: | |
359 friend class TypeImpl<Config>; | |
319 | 360 |
320 enum { | 361 enum { |
321 #define DECLARE_TYPE(type, value) k##type = (value), | 362 #define DECLARE_TYPE(type, value) k##type = (value), |
322 BITSET_TYPE_LIST(DECLARE_TYPE) | 363 BITSET_TYPE_LIST(DECLARE_TYPE) |
323 #undef DECLARE_TYPE | 364 #undef DECLARE_TYPE |
324 kUnusedEOL = 0 | 365 kUnusedEOL = 0 |
325 }; | 366 }; |
326 | 367 |
327 bool IsNone() { return this == None(); } | 368 int Bitset() { return Config::as_bitset(this); } |
328 bool IsAny() { return this == Any(); } | 369 |
329 bool IsBitset() { return Config::is_bitset(this); } | 370 static BitsetType* New(int bitset) { |
330 bool IsStruct(Tag tag) { | 371 return static_cast<BitsetType*>(Config::from_bitset(bitset)); |
331 return Config::is_struct(this) | 372 } |
332 && Config::struct_tag(Config::as_struct(this)) == tag; | 373 static TypeHandle New(int bitset, Region* region) { |
333 } | 374 return Config::from_bitset(bitset, region); |
334 bool IsUnion() { return IsStruct(kUnionTag); } | 375 } |
335 | |
336 int AsBitset() { return Config::as_bitset(this); } | |
337 StructHandle AsStruct(Tag tag) { | |
338 ASSERT(IsStruct(tag)); | |
339 return Config::as_struct(this); | |
340 } | |
341 StructHandle AsUnion() { return AsStruct(kUnionTag); } | |
342 | |
343 static int StructLength(StructHandle structured) { | |
344 return Config::struct_length(structured); | |
345 } | |
346 static TypeHandle StructGet(StructHandle structured, int i) { | |
347 return Config::struct_get(structured, i); | |
348 } | |
349 | |
350 bool SlowIs(TypeImpl* that); | |
351 | 376 |
352 static bool IsInhabited(int bitset) { | 377 static bool IsInhabited(int bitset) { |
353 return (bitset & kRepresentation) && (bitset & kSemantic); | 378 return (bitset & kRepresentation) && (bitset & kSemantic); |
354 } | 379 } |
355 | 380 |
356 int LubBitset(); // least upper bound that's a bitset | 381 static int Glb(TypeImpl* type); // greatest lower bound that's a bitset |
357 int GlbBitset(); // greatest lower bound that's a bitset | 382 static int Lub(TypeImpl* type); // least upper bound that's a bitset |
358 | 383 static int Lub(i::Object* value); |
359 static int LubBitset(i::Object* value); | 384 static int Lub(i::Map* map); |
360 static int LubBitset(i::Map* map); | 385 |
361 | 386 static const char* Name(int bitset); |
362 bool InUnion(StructHandle unioned, int current_size); | |
363 static int ExtendUnion( | |
364 StructHandle unioned, TypeHandle t, int current_size); | |
365 static int ExtendIntersection( | |
366 StructHandle unioned, TypeHandle t, TypeHandle other, int current_size); | |
367 | |
368 static const char* bitset_name(int bitset); | |
369 static void BitsetTypePrint(FILE* out, int bitset); | 387 static void BitsetTypePrint(FILE* out, int bitset); |
370 }; | 388 }; |
371 | 389 |
372 | 390 |
391 // Internal | |
392 // A structured type contains a tag and a variable number of type fields. | |
393 template<class Config> | |
394 class TypeImpl<Config>::StructuralType : public TypeImpl<Config> { | |
395 protected: | |
396 template<class> friend class TypeImpl; | |
397 friend struct ZoneTypeConfig; // For tags. | |
398 friend struct HeapTypeConfig; | |
399 | |
400 enum Tag { | |
401 kClassTag, | |
402 kConstantTag, | |
403 kArrayTag, | |
404 kFunctionTag, | |
405 kUnionTag | |
406 }; | |
407 | |
408 int Length() { | |
409 return Config::struct_length(Config::as_struct(this)); | |
410 } | |
411 TypeHandle Get(int i) { | |
412 return Config::struct_get(Config::as_struct(this), i); | |
413 } | |
414 void Set(int i, TypeHandle type) { | |
415 Config::struct_set(Config::as_struct(this), i, type); | |
416 } | |
417 void Shrink(int length) { | |
418 Config::struct_shrink(Config::as_struct(this), length); | |
419 } | |
420 | |
421 static TypeHandle New(Tag tag, int length, Region* region) { | |
422 return Config::from_struct(Config::struct_create(tag, length, region)); | |
423 } | |
424 }; | |
425 | |
426 | |
427 template<class Config> | |
428 class TypeImpl<Config>::ClassType : public TypeImpl<Config> { | |
429 public: | |
430 i::Handle<i::Map> Map() { return Config::as_class(this); } | |
431 | |
432 static ClassHandle New(i::Handle<i::Map> map, Region* region) { | |
433 return Config::template cast<ClassType>( | |
434 Config::from_class(map, BitsetType::Lub(*map), region)); | |
435 } | |
436 | |
437 static ClassType* cast(TypeImpl* type) { | |
438 ASSERT(type->IsClass()); | |
439 return static_cast<ClassType*>(type); | |
440 } | |
441 }; | |
442 | |
443 | |
444 template<class Config> | |
445 class TypeImpl<Config>::ConstantType : public TypeImpl<Config> { | |
446 public: | |
447 i::Handle<i::Object> Value() { return Config::as_constant(this); } | |
448 | |
449 static ConstantHandle New(i::Handle<i::Object> value, Region* region) { | |
450 return Config::template cast<ConstantType>( | |
451 Config::from_constant(value, BitsetType::Lub(*value), region)); | |
452 } | |
453 | |
454 static ConstantType* cast(TypeImpl* type) { | |
455 ASSERT(type->IsConstant()); | |
456 return static_cast<ConstantType*>(type); | |
457 } | |
458 }; | |
459 | |
460 | |
461 // Internal | |
462 // A union is a structured type with the following invariants: | |
463 // - its length is at least 2 | |
464 // - at most one field is a bitset, and it must go into index 0 | |
465 // - no field is a union | |
466 template<class Config> | |
467 class TypeImpl<Config>::UnionType : public StructuralType { | |
468 public: | |
469 static UnionHandle New(int length, Region* region) { | |
470 return Config::template cast<UnionType>( | |
471 StructuralType::New(StructuralType::kUnionTag, length, region)); | |
472 } | |
473 | |
474 static UnionType* cast(TypeImpl* type) { | |
475 ASSERT(type->IsUnion()); | |
476 return static_cast<UnionType*>(type); | |
477 } | |
478 }; | |
479 | |
480 | |
481 template<class Config> | |
482 class TypeImpl<Config>::ArrayType : public StructuralType { | |
483 public: | |
484 TypeHandle Element() { return this->Get(0); } | |
485 | |
486 static ArrayHandle New(TypeHandle element, Region* region) { | |
487 ArrayHandle type = Config::template cast<ArrayType>( | |
488 StructuralType::New(StructuralType::kArrayTag, 1, region)); | |
489 type->Set(0, element); | |
490 return type; | |
491 } | |
492 | |
493 static ArrayType* cast(TypeImpl* type) { | |
494 ASSERT(type->IsArray()); | |
495 return static_cast<ArrayType*>(type); | |
496 } | |
497 }; | |
498 | |
499 | |
500 template<class Config> | |
501 class TypeImpl<Config>::FunctionType : public StructuralType { | |
502 public: | |
503 int Arity() { return this->Length() - 2; } | |
504 TypeHandle Result() { return this->Get(0); } | |
505 TypeHandle Receiver() { return this->Get(1); } | |
506 TypeHandle Parameter(int i) { return this->Get(2 + i); } | |
507 | |
508 void InitParameter(int i, TypeHandle type) { this->Set(2 + i, type); } | |
509 | |
510 static FunctionHandle New( | |
511 TypeHandle result, TypeHandle receiver, int arity, Region* region) { | |
512 FunctionHandle type = Config::template cast<FunctionType>( | |
513 StructuralType::New(StructuralType::kFunctionTag, 2 + arity, region)); | |
514 type->Set(0, result); | |
515 type->Set(1, receiver); | |
516 return type; | |
517 } | |
518 | |
519 static FunctionType* cast(TypeImpl* type) { | |
520 ASSERT(type->IsFunction()); | |
521 return static_cast<FunctionType*>(type); | |
522 } | |
523 }; | |
524 | |
525 | |
526 template<class Config> template<class T> | |
527 class TypeImpl<Config>::Iterator { | |
528 public: | |
529 bool Done() const { return index_ < 0; } | |
530 i::Handle<T> Current(); | |
531 void Advance(); | |
532 | |
533 private: | |
534 template<class> friend class TypeImpl; | |
535 | |
536 Iterator() : index_(-1) {} | |
537 explicit Iterator(TypeHandle type) : type_(type), index_(-1) { | |
538 Advance(); | |
539 } | |
540 | |
541 inline bool matches(TypeHandle type); | |
542 inline TypeHandle get_type(); | |
543 | |
544 TypeHandle type_; | |
545 int index_; | |
546 }; | |
547 | |
548 | |
373 // Zone-allocated types are either (odd) integers to represent bitsets, or | 549 // Zone-allocated types are either (odd) integers to represent bitsets, or |
374 // (even) pointers to structures for everything else. | 550 // (even) pointers to structures for everything else. |
375 struct ZoneTypeConfig { | 551 struct ZoneTypeConfig { |
376 typedef TypeImpl<ZoneTypeConfig> Type; | 552 typedef TypeImpl<ZoneTypeConfig> Type; |
377 class Base {}; | 553 class Base {}; |
378 typedef void* Struct; | 554 typedef void* Struct; |
379 typedef i::Zone Region; | 555 typedef i::Zone Region; |
380 template<class T> struct Handle { typedef T* type; }; | 556 template<class T> struct Handle { typedef T* type; }; |
381 | 557 |
382 static inline Type* handle(Type* type); | 558 template<class T> static inline T* handle(T* type); |
559 template<class T> static inline T* cast(Type* type); | |
560 | |
383 static inline bool is_bitset(Type* type); | 561 static inline bool is_bitset(Type* type); |
384 static inline bool is_class(Type* type); | 562 static inline bool is_class(Type* type); |
385 static inline bool is_constant(Type* type); | 563 static inline bool is_constant(Type* type); |
386 static inline bool is_struct(Type* type); | 564 static inline bool is_struct(Type* type, int tag); |
565 | |
387 static inline int as_bitset(Type* type); | 566 static inline int as_bitset(Type* type); |
388 static inline Struct* as_struct(Type* type); | 567 static inline Struct* as_struct(Type* type); |
389 static inline i::Handle<i::Map> as_class(Type* type); | 568 static inline i::Handle<i::Map> as_class(Type* type); |
390 static inline i::Handle<i::Object> as_constant(Type* type); | 569 static inline i::Handle<i::Object> as_constant(Type* type); |
570 | |
391 static inline Type* from_bitset(int bitset); | 571 static inline Type* from_bitset(int bitset); |
392 static inline Type* from_bitset(int bitset, Zone* zone); | 572 static inline Type* from_bitset(int bitset, Zone* zone); |
393 static inline Type* from_struct(Struct* structured); | 573 static inline Type* from_struct(Struct* structured); |
394 static inline Type* from_class(i::Handle<i::Map> map, int lub, Zone* zone); | 574 static inline Type* from_class(i::Handle<i::Map> map, int lub, Zone* zone); |
395 static inline Type* from_constant( | 575 static inline Type* from_constant( |
396 i::Handle<i::Object> value, int lub, Zone* zone); | 576 i::Handle<i::Object> value, int lub, Zone* zone); |
577 | |
397 static inline Struct* struct_create(int tag, int length, Zone* zone); | 578 static inline Struct* struct_create(int tag, int length, Zone* zone); |
398 static inline void struct_shrink(Struct* structured, int length); | 579 static inline void struct_shrink(Struct* structured, int length); |
399 static inline int struct_tag(Struct* structured); | 580 static inline int struct_tag(Struct* structured); |
400 static inline int struct_length(Struct* structured); | 581 static inline int struct_length(Struct* structured); |
401 static inline Type* struct_get(Struct* structured, int i); | 582 static inline Type* struct_get(Struct* structured, int i); |
402 static inline void struct_set(Struct* structured, int i, Type* type); | 583 static inline void struct_set(Struct* structured, int i, Type* type); |
584 | |
403 static inline int lub_bitset(Type* type); | 585 static inline int lub_bitset(Type* type); |
404 }; | 586 }; |
405 | 587 |
406 typedef TypeImpl<ZoneTypeConfig> Type; | 588 typedef TypeImpl<ZoneTypeConfig> Type; |
407 | 589 |
408 | 590 |
409 // Heap-allocated types are either smis for bitsets, maps for classes, boxes for | 591 // Heap-allocated types are either smis for bitsets, maps for classes, boxes for |
410 // constants, or fixed arrays for unions. | 592 // constants, or fixed arrays for unions. |
411 struct HeapTypeConfig { | 593 struct HeapTypeConfig { |
412 typedef TypeImpl<HeapTypeConfig> Type; | 594 typedef TypeImpl<HeapTypeConfig> Type; |
413 typedef i::Object Base; | 595 typedef i::Object Base; |
414 typedef i::FixedArray Struct; | 596 typedef i::FixedArray Struct; |
415 typedef i::Isolate Region; | 597 typedef i::Isolate Region; |
416 template<class T> struct Handle { typedef i::Handle<T> type; }; | 598 template<class T> struct Handle { typedef i::Handle<T> type; }; |
417 | 599 |
418 static inline i::Handle<Type> handle(Type* type); | 600 template<class T> static inline i::Handle<T> handle(T* type); |
601 template<class T> static inline i::Handle<T> cast(i::Handle<Type> type); | |
602 | |
419 static inline bool is_bitset(Type* type); | 603 static inline bool is_bitset(Type* type); |
420 static inline bool is_class(Type* type); | 604 static inline bool is_class(Type* type); |
421 static inline bool is_constant(Type* type); | 605 static inline bool is_constant(Type* type); |
422 static inline bool is_struct(Type* type); | 606 static inline bool is_struct(Type* type, int tag); |
607 | |
423 static inline int as_bitset(Type* type); | 608 static inline int as_bitset(Type* type); |
424 static inline i::Handle<i::Map> as_class(Type* type); | 609 static inline i::Handle<i::Map> as_class(Type* type); |
425 static inline i::Handle<i::Object> as_constant(Type* type); | 610 static inline i::Handle<i::Object> as_constant(Type* type); |
426 static inline i::Handle<Struct> as_struct(Type* type); | 611 static inline i::Handle<Struct> as_struct(Type* type); |
612 | |
427 static inline Type* from_bitset(int bitset); | 613 static inline Type* from_bitset(int bitset); |
428 static inline i::Handle<Type> from_bitset(int bitset, Isolate* isolate); | 614 static inline i::Handle<Type> from_bitset(int bitset, Isolate* isolate); |
429 static inline i::Handle<Type> from_class( | 615 static inline i::Handle<Type> from_class( |
430 i::Handle<i::Map> map, int lub, Isolate* isolate); | 616 i::Handle<i::Map> map, int lub, Isolate* isolate); |
431 static inline i::Handle<Type> from_constant( | 617 static inline i::Handle<Type> from_constant( |
432 i::Handle<i::Object> value, int lub, Isolate* isolate); | 618 i::Handle<i::Object> value, int lub, Isolate* isolate); |
433 static inline i::Handle<Type> from_struct(i::Handle<Struct> structured); | 619 static inline i::Handle<Type> from_struct(i::Handle<Struct> structured); |
620 | |
434 static inline i::Handle<Struct> struct_create( | 621 static inline i::Handle<Struct> struct_create( |
435 int tag, int length, Isolate* isolate); | 622 int tag, int length, Isolate* isolate); |
436 static inline void struct_shrink(i::Handle<Struct> structured, int length); | 623 static inline void struct_shrink(i::Handle<Struct> structured, int length); |
437 static inline int struct_tag(i::Handle<Struct> structured); | 624 static inline int struct_tag(i::Handle<Struct> structured); |
438 static inline int struct_length(i::Handle<Struct> structured); | 625 static inline int struct_length(i::Handle<Struct> structured); |
439 static inline i::Handle<Type> struct_get(i::Handle<Struct> structured, int i); | 626 static inline i::Handle<Type> struct_get(i::Handle<Struct> structured, int i); |
440 static inline void struct_set( | 627 static inline void struct_set( |
441 i::Handle<Struct> structured, int i, i::Handle<Type> type); | 628 i::Handle<Struct> structured, int i, i::Handle<Type> type); |
629 | |
442 static inline int lub_bitset(Type* type); | 630 static inline int lub_bitset(Type* type); |
443 }; | 631 }; |
444 | 632 |
445 typedef TypeImpl<HeapTypeConfig> HeapType; | 633 typedef TypeImpl<HeapTypeConfig> HeapType; |
446 | 634 |
447 | 635 |
448 // A simple struct to represent a pair of lower/upper type bounds. | 636 // A simple struct to represent a pair of lower/upper type bounds. |
449 template<class Config> | 637 template<class Config> |
450 struct BoundsImpl { | 638 struct BoundsImpl { |
451 typedef TypeImpl<Config> Type; | 639 typedef TypeImpl<Config> Type; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
497 bool Narrows(BoundsImpl that) { | 685 bool Narrows(BoundsImpl that) { |
498 return that.lower->Is(this->lower) && this->upper->Is(that.upper); | 686 return that.lower->Is(this->lower) && this->upper->Is(that.upper); |
499 } | 687 } |
500 }; | 688 }; |
501 | 689 |
502 typedef BoundsImpl<ZoneTypeConfig> Bounds; | 690 typedef BoundsImpl<ZoneTypeConfig> Bounds; |
503 | 691 |
504 } } // namespace v8::internal | 692 } } // namespace v8::internal |
505 | 693 |
506 #endif // V8_TYPES_H_ | 694 #endif // V8_TYPES_H_ |
OLD | NEW |