Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Side by Side Diff: src/types.h

Issue 103743004: Implement zone-allocated types (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Addressed comments; silenced x64 warnings Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/objects.cc ('k') | src/types.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 V(NonNumber, kAny - kNumber) \ 130 V(NonNumber, kAny - kNumber) \
131 V(Detectable, kAllocated - kUndetectable) 131 V(Detectable, kAllocated - kUndetectable)
132 132
133 133
134 // struct Config { 134 // struct Config {
135 // typedef Base; 135 // typedef Base;
136 // typedef Unioned; 136 // typedef Unioned;
137 // typedef Region; 137 // typedef Region;
138 // template<class> struct Handle { typedef type; } // No template typedefs... 138 // template<class> struct Handle { typedef type; } // No template typedefs...
139 // static Handle<Type>::type handle(Type* type); // !is_bitset(type) 139 // static Handle<Type>::type handle(Type* type); // !is_bitset(type)
140 // static bool is_bitset(Type* type); 140 // static bool is_bitset(Type*);
141 // static bool is_class(Type* type); 141 // static bool is_class(Type*);
142 // static bool is_constant(Type* type); 142 // static bool is_constant(Type*);
143 // static bool is_union(Type* type); 143 // static bool is_union(Type*);
144 // static int as_bitset(Type* type); 144 // static int as_bitset(Type*);
145 // static i::Handle<i::Map> as_class(Type* type); 145 // static i::Handle<i::Map> as_class(Type*);
146 // static i::Handle<i::Object> as_constant(Type* type); 146 // static i::Handle<i::Object> as_constant(Type*);
147 // static Handle<Unioned>::type as_union(Type* type); 147 // static Handle<Unioned>::type as_union(Type*);
148 // static Type* from_bitset(int bitset); 148 // static Type* from_bitset(int bitset);
149 // static Handle<Type>::type from_bitset(int bitset, Region* region); 149 // static Handle<Type>::type from_bitset(int bitset, Region*);
150 // static Handle<Type>::type from_class(i::Handle<i::Map> map, Region* region) 150 // static Handle<Type>::type from_class(i::Handle<i::Map>, Region*)
151 // static Handle<Type>::type from_constant( 151 // static Handle<Type>::type from_constant(i::Handle<i::Object>, Region*);
152 // i::Handle<i::Object> value, Region* region); 152 // static Handle<Type>::type from_union(Handle<Unioned>::type);
153 // static Handle<Type>::type from_union(Handle<Unioned>::T unioned); 153 // static Handle<Unioned>::type union_create(int size, Region*);
154 // static Handle<Unioned>::type union_create(int size, Region* region); 154 // static void union_shrink(Handle<Unioned>::type, int size);
155 // static Handle<Type>::type union_get(Handle<Unioned>::T unioned, int i); 155 // static Handle<Type>::type union_get(Handle<Unioned>::type, int);
156 // static void union_set(Handle<Unioned>::type, int, Handle<Type>::type);
157 // static int union_length(Handle<Unioned>::type);
156 // } 158 // }
157 template<class Config> 159 template<class Config>
158 class TypeImpl : public Config::Base { 160 class TypeImpl : public Config::Base {
159 public: 161 public:
160 typedef typename Config::template Handle<TypeImpl>::type TypeHandle; 162 typedef typename Config::template Handle<TypeImpl>::type TypeHandle;
161 typedef typename Config::Region Region; 163 typedef typename Config::Region Region;
162 164
163 #define DEFINE_TYPE_CONSTRUCTOR(type, value) \ 165 #define DEFINE_TYPE_CONSTRUCTOR(type, value) \
164 static TypeImpl* type() { return Config::from_bitset(k##type); } \ 166 static TypeImpl* type() { return Config::from_bitset(k##type); } \
165 static TypeHandle type(Region* region) { \ 167 static TypeHandle type(Region* region) { \
(...skipping 10 matching lines...) Expand all
176 } 178 }
177 179
178 static TypeHandle Union(TypeHandle type1, TypeHandle type2, Region* reg); 180 static TypeHandle Union(TypeHandle type1, TypeHandle type2, Region* reg);
179 static TypeHandle Intersect(TypeHandle type1, TypeHandle type2, Region* reg); 181 static TypeHandle Intersect(TypeHandle type1, TypeHandle type2, Region* reg);
180 182
181 static TypeHandle Of(i::Handle<i::Object> value, Region* region) { 183 static TypeHandle Of(i::Handle<i::Object> value, Region* region) {
182 return Config::from_bitset(LubBitset(*value), region); 184 return Config::from_bitset(LubBitset(*value), region);
183 } 185 }
184 186
185 bool Is(TypeImpl* that) { return this == that || this->SlowIs(that); } 187 bool Is(TypeImpl* that) { return this == that || this->SlowIs(that); }
188 template<class TypeHandle>
186 bool Is(TypeHandle that) { return this->Is(*that); } 189 bool Is(TypeHandle that) { return this->Is(*that); }
187 bool Maybe(TypeImpl* that); 190 bool Maybe(TypeImpl* that);
191 template<class TypeHandle>
188 bool Maybe(TypeHandle that) { return this->Maybe(*that); } 192 bool Maybe(TypeHandle that) { return this->Maybe(*that); }
189 193
190 // State-dependent versions of Of and Is that consider subtyping between 194 // State-dependent versions of Of and Is that consider subtyping between
191 // a constant and its map class. 195 // a constant and its map class.
192 static TypeHandle OfCurrently(i::Handle<i::Object> value, Region* region); 196 static TypeHandle OfCurrently(i::Handle<i::Object> value, Region* region);
193 bool IsCurrently(TypeImpl* that); 197 bool IsCurrently(TypeImpl* that);
198 template<class TypeHandle>
194 bool IsCurrently(TypeHandle that) { return this->IsCurrently(*that); } 199 bool IsCurrently(TypeHandle that) { return this->IsCurrently(*that); }
195 200
196 bool IsClass() { return Config::is_class(this); } 201 bool IsClass() { return Config::is_class(this); }
197 bool IsConstant() { return Config::is_constant(this); } 202 bool IsConstant() { return Config::is_constant(this); }
198 i::Handle<i::Map> AsClass() { return Config::as_class(this); } 203 i::Handle<i::Map> AsClass() { return Config::as_class(this); }
199 i::Handle<i::Object> AsConstant() { return Config::as_constant(this); } 204 i::Handle<i::Object> AsConstant() { return Config::as_constant(this); }
200 205
201 int NumClasses(); 206 int NumClasses();
202 int NumConstants(); 207 int NumConstants();
203 208
(...skipping 21 matching lines...) Expand all
225 230
226 Iterator<i::Map> Classes() { 231 Iterator<i::Map> Classes() {
227 if (this->IsBitset()) return Iterator<i::Map>(); 232 if (this->IsBitset()) return Iterator<i::Map>();
228 return Iterator<i::Map>(Config::handle(this)); 233 return Iterator<i::Map>(Config::handle(this));
229 } 234 }
230 Iterator<i::Object> Constants() { 235 Iterator<i::Object> Constants() {
231 if (this->IsBitset()) return Iterator<i::Object>(); 236 if (this->IsBitset()) return Iterator<i::Object>();
232 return Iterator<i::Object>(Config::handle(this)); 237 return Iterator<i::Object>(Config::handle(this));
233 } 238 }
234 239
235 static TypeImpl* cast(i::Object* object) { 240 static TypeImpl* cast(typename Config::Base* object) {
236 TypeImpl* t = static_cast<Type*>(object); 241 TypeImpl* t = static_cast<TypeImpl*>(object);
237 ASSERT(t->IsBitset() || t->IsClass() || t->IsConstant() || t->IsUnion()); 242 ASSERT(t->IsBitset() || t->IsClass() || t->IsConstant() || t->IsUnion());
238 return t; 243 return t;
239 } 244 }
240 245
241 #ifdef OBJECT_PRINT 246 #ifdef OBJECT_PRINT
242 void TypePrint(); 247 void TypePrint();
243 void TypePrint(FILE* out); 248 void TypePrint(FILE* out);
244 #endif 249 #endif
245 250
246 private: 251 private:
(...skipping 22 matching lines...) Expand all
269 274
270 bool SlowIs(TypeImpl* that); 275 bool SlowIs(TypeImpl* that);
271 276
272 int LubBitset(); // least upper bound that's a bitset 277 int LubBitset(); // least upper bound that's a bitset
273 int GlbBitset(); // greatest lower bound that's a bitset 278 int GlbBitset(); // greatest lower bound that's a bitset
274 279
275 static int LubBitset(i::Object* value); 280 static int LubBitset(i::Object* value);
276 static int LubBitset(i::Map* map); 281 static int LubBitset(i::Map* map);
277 282
278 bool InUnion(UnionedHandle unioned, int current_size); 283 bool InUnion(UnionedHandle unioned, int current_size);
279 int ExtendUnion(UnionedHandle unioned, int current_size); 284 static int ExtendUnion(
280 int ExtendIntersection( 285 UnionedHandle unioned, TypeHandle t, int current_size);
281 UnionedHandle unioned, TypeHandle type, int current_size); 286 static int ExtendIntersection(
287 UnionedHandle unioned, TypeHandle t, TypeHandle other, int current_size);
282 288
283 static const char* bitset_name(int bitset); 289 static const char* bitset_name(int bitset);
284 }; 290 };
285 291
286 292
293 // Zone-allocated types are either (odd) integers to represent bitsets, or
294 // (even) pointers to zone lists for everything else. The first slot of every
295 // list is an explicit tag value to distinguish representation.
296 struct ZoneTypeConfig {
297 private:
298 typedef i::ZoneList<void*> Tagged;
299
300 enum Tag {
301 kClassTag,
302 kConstantTag,
303 kUnionTag
304 };
305
306 static Tagged* tagged_create(Tag tag, int size, Zone* zone) {
307 Tagged* tagged = new(zone) Tagged(size + 1, zone);
308 tagged->Add(reinterpret_cast<void*>(tag), zone);
309 tagged->AddBlock(NULL, size, zone);
310 return tagged;
311 }
312 static void tagged_shrink(Tagged* tagged, int size) {
313 tagged->Rewind(size + 1);
314 }
315 static Tag tagged_tag(Tagged* tagged) {
316 return static_cast<Tag>(reinterpret_cast<intptr_t>(tagged->at(0)));
317 }
318 template<class T>
319 static T tagged_get(Tagged* tagged, int i) {
320 return reinterpret_cast<T>(tagged->at(i + 1));
321 }
322 template<class T>
323 static void tagged_set(Tagged* tagged, int i, T value) {
324 tagged->at(i + 1) = reinterpret_cast<T>(value);
325 }
326 static int tagged_length(Tagged* tagged) {
327 return tagged->length() - 1;
328 }
329
330 public:
331 typedef TypeImpl<ZoneTypeConfig> Type;
332 class Base {};
333 typedef i::ZoneList<Type*> Unioned;
334 typedef i::Zone Region;
335 template<class T> struct Handle { typedef T* type; };
336
337 static Type* handle(Type* type) { return type; }
338
339 static bool is(Type* type, Tag tag) {
340 return is_tagged(type) && tagged_tag(as_tagged(type)) == tag;
341 }
342
343 static bool is_bitset(Type* type) {
344 return reinterpret_cast<intptr_t>(type) & 1;
345 }
346 static bool is_tagged(Type* type) { return !is_bitset(type); }
347 static bool is_class(Type* type) { return is(type, kClassTag); }
348 static bool is_constant(Type* type) { return is(type, kConstantTag); }
349 static bool is_union(Type* type) { return is(type, kUnionTag); }
350 static bool tagged_is_union(Tagged* tagged) {
351 return is(from_tagged(tagged), kUnionTag);
352 }
353
354 static int as_bitset(Type* type) {
355 ASSERT(is_bitset(type));
356 return reinterpret_cast<intptr_t>(type) >> 1;
357 }
358 static Tagged* as_tagged(Type* type) {
359 ASSERT(is_tagged(type));
360 return reinterpret_cast<Tagged*>(type);
361 }
362 static i::Handle<i::Map> as_class(Type* type) {
363 ASSERT(is_class(type));
364 return i::Handle<i::Map>(tagged_get<i::Map**>(as_tagged(type), 0));
365 }
366 static i::Handle<i::Object> as_constant(Type* type) {
367 ASSERT(is_constant(type));
368 return i::Handle<i::Object>(tagged_get<i::Object**>(as_tagged(type), 0));
369 }
370 static Unioned* as_union(Type* type) {
371 ASSERT(is_union(type));
372 return tagged_as_union(as_tagged(type));
373 }
374 static Unioned* tagged_as_union(Tagged* tagged) {
375 ASSERT(tagged_is_union(tagged));
376 return reinterpret_cast<Unioned*>(tagged);
377 }
378
379 static Type* from_bitset(int bitset) {
380 return reinterpret_cast<Type*>((bitset << 1) | 1);
381 }
382 static Type* from_bitset(int bitset, Zone* Zone) {
383 return from_bitset(bitset);
384 }
385 static Type* from_tagged(Tagged* tagged) {
386 return reinterpret_cast<Type*>(tagged);
387 }
388 static Type* from_class(i::Handle<i::Map> map, Zone* zone) {
389 Tagged* tagged = tagged_create(kClassTag, 1, zone);
390 tagged_set(tagged, 0, map.location());
391 return from_tagged(tagged);
392 }
393 static Type* from_constant(i::Handle<i::Object> value, Zone* zone) {
394 Tagged* tagged = tagged_create(kConstantTag, 1, zone);
395 tagged_set(tagged, 0, value.location());
396 return from_tagged(tagged);
397 }
398 static Type* from_union(Unioned* unioned) {
399 return from_tagged(tagged_from_union(unioned));
400 }
401 static Tagged* tagged_from_union(Unioned* unioned) {
402 return reinterpret_cast<Tagged*>(unioned);
403 }
404
405 static Unioned* union_create(int size, Zone* zone) {
406 return tagged_as_union(tagged_create(kUnionTag, size, zone));
407 }
408 static void union_shrink(Unioned* unioned, int size) {
409 tagged_shrink(tagged_from_union(unioned), size);
410 }
411 static Type* union_get(Unioned* unioned, int i) {
412 Type* type = tagged_get<Type*>(tagged_from_union(unioned), i);
413 ASSERT(!is_union(type));
414 return type;
415 }
416 static void union_set(Unioned* unioned, int i, Type* type) {
417 ASSERT(!is_union(type));
418 tagged_set(tagged_from_union(unioned), i, type);
419 }
420 static int union_length(Unioned* unioned) {
421 return tagged_length(tagged_from_union(unioned));
422 }
423 };
424
425
426 // Heap-allocated types are either smis for bitsets, maps for classes, boxes for
427 // constants, or fixed arrays for unions.
287 struct HeapTypeConfig { 428 struct HeapTypeConfig {
288 typedef TypeImpl<HeapTypeConfig> Type; 429 typedef TypeImpl<HeapTypeConfig> Type;
289 typedef i::Object Base; 430 typedef i::Object Base;
290 typedef i::FixedArray Unioned; 431 typedef i::FixedArray Unioned;
291 typedef i::Isolate Region; 432 typedef i::Isolate Region;
292 template<class T> struct Handle { typedef i::Handle<T> type; }; 433 template<class T> struct Handle { typedef i::Handle<T> type; };
293 434
294 static i::Handle<Type> handle(Type* type) { 435 static i::Handle<Type> handle(Type* type) {
295 return i::handle(type, i::HeapObject::cast(type)->GetIsolate()); 436 return i::handle(type, i::HeapObject::cast(type)->GetIsolate());
296 } 437 }
(...skipping 21 matching lines...) Expand all
318 return Type::cast(i::Smi::FromInt(bitset)); 459 return Type::cast(i::Smi::FromInt(bitset));
319 } 460 }
320 static i::Handle<Type> from_bitset(int bitset, Isolate* isolate) { 461 static i::Handle<Type> from_bitset(int bitset, Isolate* isolate) {
321 return i::handle(from_bitset(bitset), isolate); 462 return i::handle(from_bitset(bitset), isolate);
322 } 463 }
323 static i::Handle<Type> from_class(i::Handle<i::Map> map, Isolate* isolate) { 464 static i::Handle<Type> from_class(i::Handle<i::Map> map, Isolate* isolate) {
324 return i::Handle<Type>::cast(i::Handle<Object>::cast(map)); 465 return i::Handle<Type>::cast(i::Handle<Object>::cast(map));
325 } 466 }
326 static i::Handle<Type> from_constant( 467 static i::Handle<Type> from_constant(
327 i::Handle<i::Object> value, Isolate* isolate) { 468 i::Handle<i::Object> value, Isolate* isolate) {
328 ASSERT(isolate || value->IsHeapObject());
329 if (!isolate) isolate = i::HeapObject::cast(*value)->GetIsolate();
330 i::Handle<Box> box = isolate->factory()->NewBox(value); 469 i::Handle<Box> box = isolate->factory()->NewBox(value);
331 return i::Handle<Type>::cast(i::Handle<Object>::cast(box)); 470 return i::Handle<Type>::cast(i::Handle<Object>::cast(box));
332 } 471 }
333 static i::Handle<Type> from_union(i::Handle<Unioned> unioned) { 472 static i::Handle<Type> from_union(i::Handle<Unioned> unioned) {
334 return i::Handle<Type>::cast(i::Handle<Object>::cast(unioned)); 473 return i::Handle<Type>::cast(i::Handle<Object>::cast(unioned));
335 } 474 }
336 475
337 static i::Handle<Unioned> union_create(int size, Isolate* isolate) { 476 static i::Handle<Unioned> union_create(int size, Isolate* isolate) {
338 return isolate->factory()->NewFixedArray(size); 477 return isolate->factory()->NewFixedArray(size);
339 } 478 }
479 static void union_shrink(i::Handle<Unioned> unioned, int size) {
480 unioned->Shrink(size);
481 }
340 static i::Handle<Type> union_get(i::Handle<Unioned> unioned, int i) { 482 static i::Handle<Type> union_get(i::Handle<Unioned> unioned, int i) {
341 Type* type = static_cast<Type*>(unioned->get(i)); 483 Type* type = static_cast<Type*>(unioned->get(i));
342 ASSERT(!is_union(type)); 484 ASSERT(!is_union(type));
343 return i::handle(type, unioned->GetIsolate()); 485 return i::handle(type, unioned->GetIsolate());
344 } 486 }
487 static void union_set(
488 i::Handle<Unioned> unioned, int i, i::Handle<Type> type) {
489 ASSERT(!is_union(*type));
490 unioned->set(i, *type);
491 }
492 static int union_length(i::Handle<Unioned> unioned) {
493 return unioned->length();
494 }
345 }; 495 };
346 496
497 typedef TypeImpl<ZoneTypeConfig> ZoneType;
347 typedef TypeImpl<HeapTypeConfig> Type; 498 typedef TypeImpl<HeapTypeConfig> Type;
348 499
349 500
350 // A simple struct to represent a pair of lower/upper type bounds. 501 // A simple struct to represent a pair of lower/upper type bounds.
351 template<class Config> 502 template<class Config>
352 struct BoundsImpl { 503 struct BoundsImpl {
353 typedef TypeImpl<Config> Type; 504 typedef TypeImpl<Config> Type;
354 typedef typename Type::TypeHandle TypeHandle; 505 typedef typename Type::TypeHandle TypeHandle;
355 typedef typename Type::Region Region; 506 typedef typename Type::Region Region;
356 507
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 TypeHandle lower = Type::Union(b.lower, t, region); 541 TypeHandle lower = Type::Union(b.lower, t, region);
391 return BoundsImpl(lower, b.upper); 542 return BoundsImpl(lower, b.upper);
392 } 543 }
393 static BoundsImpl NarrowUpper(BoundsImpl b, TypeHandle t, Region* region) { 544 static BoundsImpl NarrowUpper(BoundsImpl b, TypeHandle t, Region* region) {
394 TypeHandle lower = Type::Intersect(b.lower, t, region); 545 TypeHandle lower = Type::Intersect(b.lower, t, region);
395 TypeHandle upper = Type::Intersect(b.upper, t, region); 546 TypeHandle upper = Type::Intersect(b.upper, t, region);
396 return BoundsImpl(lower, upper); 547 return BoundsImpl(lower, upper);
397 } 548 }
398 }; 549 };
399 550
551 typedef BoundsImpl<ZoneTypeConfig> ZoneBounds;
400 typedef BoundsImpl<HeapTypeConfig> Bounds; 552 typedef BoundsImpl<HeapTypeConfig> Bounds;
401 553
402 554
403 } } // namespace v8::internal 555 } } // namespace v8::internal
404 556
405 #endif // V8_TYPES_H_ 557 #endif // V8_TYPES_H_
OLDNEW
« no previous file with comments | « src/objects.cc ('k') | src/types.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698