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

Side by Side Diff: test/cctest/test-types.cc

Issue 564413004: Re-revert "Use unsigned type bitsets to limit undefined behaviour" (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 3 months 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/types-inl.h ('k') | no next file » | 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 // 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 #include <vector> 5 #include <vector>
6 6
7 #include "src/hydrogen-types.h" 7 #include "src/hydrogen-types.h"
8 #include "src/isolate-inl.h" 8 #include "src/isolate-inl.h"
9 #include "src/types.h" 9 #include "src/types.h"
10 #include "test/cctest/cctest.h" 10 #include "test/cctest/cctest.h"
11 11
12 using namespace v8::internal; 12 using namespace v8::internal;
13 13
14 // Testing auxiliaries (breaking the Type abstraction). 14 // Testing auxiliaries (breaking the Type abstraction).
15 typedef uintptr_t bitset;
16
17 struct ZoneRep { 15 struct ZoneRep {
18 typedef void* Struct; 16 typedef void* Struct;
19 17
20 static bool IsStruct(Type* t, int tag) { 18 static bool IsStruct(Type* t, int tag) {
21 return !IsBitset(t) && reinterpret_cast<intptr_t>(AsStruct(t)[0]) == tag; 19 return !IsBitset(t) && reinterpret_cast<intptr_t>(AsStruct(t)[0]) == tag;
22 } 20 }
23 static bool IsBitset(Type* t) { return reinterpret_cast<bitset>(t) & 1; } 21 static bool IsBitset(Type* t) { return reinterpret_cast<intptr_t>(t) & 1; }
24 static bool IsUnion(Type* t) { return IsStruct(t, 6); } 22 static bool IsUnion(Type* t) { return IsStruct(t, 6); }
25 23
26 static Struct* AsStruct(Type* t) { 24 static Struct* AsStruct(Type* t) {
27 return reinterpret_cast<Struct*>(t); 25 return reinterpret_cast<Struct*>(t);
28 } 26 }
29 static bitset AsBitset(Type* t) { 27 static int AsBitset(Type* t) {
30 return reinterpret_cast<bitset>(t) ^ 1u; 28 return static_cast<int>(reinterpret_cast<intptr_t>(t) >> 1);
31 } 29 }
32 static Struct* AsUnion(Type* t) { 30 static Struct* AsUnion(Type* t) {
33 return AsStruct(t); 31 return AsStruct(t);
34 } 32 }
35 static int Length(Struct* structured) { 33 static int Length(Struct* structured) {
36 return static_cast<int>(reinterpret_cast<intptr_t>(structured[1])); 34 return static_cast<int>(reinterpret_cast<intptr_t>(structured[1]));
37 } 35 }
38 36
39 static Zone* ToRegion(Zone* zone, Isolate* isolate) { return zone; } 37 static Zone* ToRegion(Zone* zone, Isolate* isolate) { return zone; }
40 38
41 struct BitsetType : Type::BitsetType { 39 struct BitsetType : Type::BitsetType {
42 using Type::BitsetType::New; 40 using Type::BitsetType::New;
43 using Type::BitsetType::Glb; 41 using Type::BitsetType::Glb;
44 using Type::BitsetType::Lub; 42 using Type::BitsetType::Lub;
45 using Type::BitsetType::InherentLub; 43 using Type::BitsetType::InherentLub;
46 }; 44 };
47 }; 45 };
48 46
49 47
50 struct HeapRep { 48 struct HeapRep {
51 typedef FixedArray Struct; 49 typedef FixedArray Struct;
52 50
53 static bool IsStruct(Handle<HeapType> t, int tag) { 51 static bool IsStruct(Handle<HeapType> t, int tag) {
54 return t->IsFixedArray() && Smi::cast(AsStruct(t)->get(0))->value() == tag; 52 return t->IsFixedArray() && Smi::cast(AsStruct(t)->get(0))->value() == tag;
55 } 53 }
56 static bool IsBitset(Handle<HeapType> t) { return t->IsSmi(); } 54 static bool IsBitset(Handle<HeapType> t) { return t->IsSmi(); }
57 static bool IsUnion(Handle<HeapType> t) { return IsStruct(t, 6); } 55 static bool IsUnion(Handle<HeapType> t) { return IsStruct(t, 6); }
58 56
59 static Struct* AsStruct(Handle<HeapType> t) { return FixedArray::cast(*t); } 57 static Struct* AsStruct(Handle<HeapType> t) { return FixedArray::cast(*t); }
60 static bitset AsBitset(Handle<HeapType> t) { 58 static int AsBitset(Handle<HeapType> t) { return Smi::cast(*t)->value(); }
61 return reinterpret_cast<bitset>(*t);
62 }
63 static Struct* AsUnion(Handle<HeapType> t) { return AsStruct(t); } 59 static Struct* AsUnion(Handle<HeapType> t) { return AsStruct(t); }
64 static int Length(Struct* structured) { return structured->length() - 1; } 60 static int Length(Struct* structured) { return structured->length() - 1; }
65 61
66 static Isolate* ToRegion(Zone* zone, Isolate* isolate) { return isolate; } 62 static Isolate* ToRegion(Zone* zone, Isolate* isolate) { return isolate; }
67 63
68 struct BitsetType : HeapType::BitsetType { 64 struct BitsetType : HeapType::BitsetType {
69 using HeapType::BitsetType::New; 65 using HeapType::BitsetType::New;
70 using HeapType::BitsetType::Glb; 66 using HeapType::BitsetType::Glb;
71 using HeapType::BitsetType::Lub; 67 using HeapType::BitsetType::Lub;
72 using HeapType::BitsetType::InherentLub; 68 using HeapType::BitsetType::InherentLub;
73 static bitset Glb(Handle<HeapType> type) { return Glb(*type); } 69 static int Glb(Handle<HeapType> type) { return Glb(*type); }
74 static bitset Lub(Handle<HeapType> type) { return Lub(*type); } 70 static int Lub(Handle<HeapType> type) { return Lub(*type); }
75 static bitset InherentLub(Handle<HeapType> type) { 71 static int InherentLub(Handle<HeapType> type) { return InherentLub(*type); }
76 return InherentLub(*type);
77 }
78 }; 72 };
79 }; 73 };
80 74
81 75
82 template<class Type, class TypeHandle, class Region> 76 template<class Type, class TypeHandle, class Region>
83 class Types { 77 class Types {
84 public: 78 public:
85 Types(Region* region, Isolate* isolate) 79 Types(Region* region, Isolate* isolate)
86 : region_(region), rng_(isolate->random_number_generator()) { 80 : region_(region), rng_(isolate->random_number_generator()) {
87 #define DECLARE_TYPE(name, value) \ 81 #define DECLARE_TYPE(name, value) \
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 } 358 }
365 359
366 void CheckEqual(TypeHandle type1, TypeHandle type2) { 360 void CheckEqual(TypeHandle type1, TypeHandle type2) {
367 CHECK(Equal(type1, type2)); 361 CHECK(Equal(type1, type2));
368 } 362 }
369 363
370 void CheckSub(TypeHandle type1, TypeHandle type2) { 364 void CheckSub(TypeHandle type1, TypeHandle type2) {
371 CHECK(type1->Is(type2)); 365 CHECK(type1->Is(type2));
372 CHECK(!type2->Is(type1)); 366 CHECK(!type2->Is(type1));
373 if (Rep::IsBitset(type1) && Rep::IsBitset(type2)) { 367 if (Rep::IsBitset(type1) && Rep::IsBitset(type2)) {
374 CHECK(Rep::AsBitset(type1) != Rep::AsBitset(type2)); 368 CHECK_NE(Rep::AsBitset(type1), Rep::AsBitset(type2));
375 } 369 }
376 } 370 }
377 371
378 void CheckUnordered(TypeHandle type1, TypeHandle type2) { 372 void CheckUnordered(TypeHandle type1, TypeHandle type2) {
379 CHECK(!type1->Is(type2)); 373 CHECK(!type1->Is(type2));
380 CHECK(!type2->Is(type1)); 374 CHECK(!type2->Is(type1));
381 if (Rep::IsBitset(type1) && Rep::IsBitset(type2)) { 375 if (Rep::IsBitset(type1) && Rep::IsBitset(type2)) {
382 CHECK(Rep::AsBitset(type1) != Rep::AsBitset(type2)); 376 CHECK_NE(Rep::AsBitset(type1), Rep::AsBitset(type2));
383 } 377 }
384 } 378 }
385 379
386 void CheckOverlap(TypeHandle type1, TypeHandle type2, TypeHandle mask) { 380 void CheckOverlap(TypeHandle type1, TypeHandle type2, TypeHandle mask) {
387 CHECK(type1->Maybe(type2)); 381 CHECK(type1->Maybe(type2));
388 CHECK(type2->Maybe(type1)); 382 CHECK(type2->Maybe(type1));
389 if (Rep::IsBitset(type1) && Rep::IsBitset(type2)) { 383 if (Rep::IsBitset(type1) && Rep::IsBitset(type2)) {
390 CHECK(0 != 384 CHECK_NE(0,
391 (Rep::AsBitset(type1) & Rep::AsBitset(type2) & Rep::AsBitset(mask))); 385 Rep::AsBitset(type1) & Rep::AsBitset(type2) & Rep::AsBitset(mask));
392 } 386 }
393 } 387 }
394 388
395 void CheckDisjoint(TypeHandle type1, TypeHandle type2, TypeHandle mask) { 389 void CheckDisjoint(TypeHandle type1, TypeHandle type2, TypeHandle mask) {
396 CHECK(!type1->Is(type2)); 390 CHECK(!type1->Is(type2));
397 CHECK(!type2->Is(type1)); 391 CHECK(!type2->Is(type1));
398 CHECK(!type1->Maybe(type2)); 392 CHECK(!type1->Maybe(type2));
399 CHECK(!type2->Maybe(type1)); 393 CHECK(!type2->Maybe(type1));
400 if (Rep::IsBitset(type1) && Rep::IsBitset(type2)) { 394 if (Rep::IsBitset(type1) && Rep::IsBitset(type2)) {
401 CHECK(0 == 395 CHECK_EQ(0,
402 (Rep::AsBitset(type1) & Rep::AsBitset(type2) & Rep::AsBitset(mask))); 396 Rep::AsBitset(type1) & Rep::AsBitset(type2) & Rep::AsBitset(mask));
403 } 397 }
404 } 398 }
405 399
406 void Bitset() { 400 void Bitset() {
407 // None and Any are bitsets. 401 // None and Any are bitsets.
408 CHECK(this->IsBitset(T.None)); 402 CHECK(this->IsBitset(T.None));
409 CHECK(this->IsBitset(T.Any)); 403 CHECK(this->IsBitset(T.Any));
410 404
411 CHECK(bitset(0) == this->AsBitset(T.None)); 405 CHECK_EQ(0, this->AsBitset(T.None));
412 printf("[BitSet] value=%p enum=%p bitset=%p any=%p this=%p any=%p\n", 406 CHECK_EQ(-1, this->AsBitset(T.Any));
413 reinterpret_cast<void*>(bitset(0xfffffffeu)),
414 reinterpret_cast<void*>(bitset(HeapType::BitsetType::kAny)),
415 reinterpret_cast<void*>(
416 HeapTypeConfig::from_bitset(HeapType::BitsetType::kAny)),
417 reinterpret_cast<void*>(HeapType::Any()),
418 reinterpret_cast<void*>(this->AsBitset(T.Any)),
419 reinterpret_cast<void*>(*T.Any));
420 CHECK(bitset(0xfffffffeu) == this->AsBitset(T.Any));
421 407
422 // Union(T1, T2) is bitset for bitsets T1,T2 408 // Union(T1, T2) is bitset for bitsets T1,T2
423 for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) { 409 for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
424 for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) { 410 for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
425 TypeHandle type1 = *it1; 411 TypeHandle type1 = *it1;
426 TypeHandle type2 = *it2; 412 TypeHandle type2 = *it2;
427 TypeHandle union12 = T.Union(type1, type2); 413 TypeHandle union12 = T.Union(type1, type2);
428 CHECK(!(this->IsBitset(type1) && this->IsBitset(type2)) || 414 CHECK(!(this->IsBitset(type1) && this->IsBitset(type2)) ||
429 this->IsBitset(union12)); 415 this->IsBitset(union12));
430 } 416 }
(...skipping 21 matching lines...) Expand all
452 } 438 }
453 } 439 }
454 440
455 // Union(T1, T2) is bitwise disjunction for bitsets T1,T2 441 // Union(T1, T2) is bitwise disjunction for bitsets T1,T2
456 for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) { 442 for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
457 for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) { 443 for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
458 TypeHandle type1 = *it1; 444 TypeHandle type1 = *it1;
459 TypeHandle type2 = *it2; 445 TypeHandle type2 = *it2;
460 TypeHandle union12 = T.Union(type1, type2); 446 TypeHandle union12 = T.Union(type1, type2);
461 if (this->IsBitset(type1) && this->IsBitset(type2)) { 447 if (this->IsBitset(type1) && this->IsBitset(type2)) {
462 CHECK( 448 CHECK_EQ(
463 (this->AsBitset(type1) | this->AsBitset(type2)) == 449 this->AsBitset(type1) | this->AsBitset(type2),
464 this->AsBitset(union12)); 450 this->AsBitset(union12));
465 } 451 }
466 } 452 }
467 } 453 }
468 454
469 // Intersect(T1, T2) is bitwise conjunction for bitsets T1,T2 455 // Intersect(T1, T2) is bitwise conjunction for bitsets T1,T2
470 for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) { 456 for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
471 for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) { 457 for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
472 TypeHandle type1 = *it1; 458 TypeHandle type1 = *it1;
473 TypeHandle type2 = *it2; 459 TypeHandle type2 = *it2;
474 TypeHandle intersect12 = T.Intersect(type1, type2); 460 TypeHandle intersect12 = T.Intersect(type1, type2);
475 if (this->IsBitset(type1) && this->IsBitset(type2)) { 461 if (this->IsBitset(type1) && this->IsBitset(type2)) {
476 CHECK( 462 CHECK_EQ(
477 (this->AsBitset(type1) & this->AsBitset(type2)) == 463 this->AsBitset(type1) & this->AsBitset(type2),
478 this->AsBitset(intersect12)); 464 this->AsBitset(intersect12));
479 } 465 }
480 } 466 }
481 } 467 }
482 } 468 }
483 469
484 void Class() { 470 void Class() {
485 // Constructor 471 // Constructor
486 for (MapIterator mt = T.maps.begin(); mt != T.maps.end(); ++mt) { 472 for (MapIterator mt = T.maps.begin(); mt != T.maps.end(); ++mt) {
487 Handle<i::Map> map = *mt; 473 Handle<i::Map> map = *mt;
(...skipping 1336 matching lines...) Expand 10 before | Expand all | Expand 10 after
1824 } 1810 }
1825 } 1811 }
1826 }; 1812 };
1827 1813
1828 typedef Tests<Type, Type*, Zone, ZoneRep> ZoneTests; 1814 typedef Tests<Type, Type*, Zone, ZoneRep> ZoneTests;
1829 typedef Tests<HeapType, Handle<HeapType>, Isolate, HeapRep> HeapTests; 1815 typedef Tests<HeapType, Handle<HeapType>, Isolate, HeapRep> HeapTests;
1830 1816
1831 1817
1832 TEST(BitsetType) { 1818 TEST(BitsetType) {
1833 CcTest::InitializeVM(); 1819 CcTest::InitializeVM();
1834 // ZoneTests().Bitset(); 1820 ZoneTests().Bitset();
1835 HeapTests().Bitset(); 1821 HeapTests().Bitset();
1836 } 1822 }
1837 1823
1838 1824
1839 TEST(ClassType) { 1825 TEST(ClassType) {
1840 CcTest::InitializeVM(); 1826 CcTest::InitializeVM();
1841 ZoneTests().Class(); 1827 ZoneTests().Class();
1842 HeapTests().Class(); 1828 HeapTests().Class();
1843 } 1829 }
1844 1830
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
1976 ZoneTests().Convert<HeapType, Handle<HeapType>, Isolate, HeapRep>(); 1962 ZoneTests().Convert<HeapType, Handle<HeapType>, Isolate, HeapRep>();
1977 HeapTests().Convert<Type, Type*, Zone, ZoneRep>(); 1963 HeapTests().Convert<Type, Type*, Zone, ZoneRep>();
1978 } 1964 }
1979 1965
1980 1966
1981 TEST(HTypeFromType) { 1967 TEST(HTypeFromType) {
1982 CcTest::InitializeVM(); 1968 CcTest::InitializeVM();
1983 ZoneTests().HTypeFromType(); 1969 ZoneTests().HTypeFromType();
1984 HeapTests().HTypeFromType(); 1970 HeapTests().HTypeFromType();
1985 } 1971 }
OLDNEW
« no previous file with comments | « src/types-inl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698