| 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 // Use of this source code is governed by a BSD-style license that can be |
| 3 // modification, are permitted provided that the following conditions are | 3 // found in the LICENSE file. |
| 4 // met: | |
| 5 // | |
| 6 // * Redistributions of source code must retain the above copyright | |
| 7 // notice, this list of conditions and the following disclaimer. | |
| 8 // * Redistributions in binary form must reproduce the above | |
| 9 // copyright notice, this list of conditions and the following | |
| 10 // disclaimer in the documentation and/or other materials provided | |
| 11 // with the distribution. | |
| 12 // * Neither the name of Google Inc. nor the names of its | |
| 13 // contributors may be used to endorse or promote products derived | |
| 14 // from this software without specific prior written permission. | |
| 15 // | |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 27 | 4 |
| 28 #include <vector> | 5 #include <vector> |
| 29 | 6 |
| 30 #include "src/base/utils/random-number-generator.h" | |
| 31 #include "src/hydrogen-types.h" | 7 #include "src/hydrogen-types.h" |
| 8 #include "src/isolate-inl.h" |
| 32 #include "src/types.h" | 9 #include "src/types.h" |
| 33 #include "test/cctest/cctest.h" | 10 #include "test/cctest/cctest.h" |
| 34 | 11 |
| 35 using namespace v8::internal; | 12 using namespace v8::internal; |
| 36 | 13 |
| 37 // Testing auxiliaries (breaking the Type abstraction). | 14 // Testing auxiliaries (breaking the Type abstraction). |
| 38 struct ZoneRep { | 15 struct ZoneRep { |
| 39 typedef void* Struct; | 16 typedef void* Struct; |
| 40 | 17 |
| 41 static bool IsStruct(Type* t, int tag) { | 18 static bool IsStruct(Type* t, int tag) { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 static int Glb(Handle<HeapType> type) { return Glb(*type); } | 97 static int Glb(Handle<HeapType> type) { return Glb(*type); } |
| 121 static int Lub(Handle<HeapType> type) { return Lub(*type); } | 98 static int Lub(Handle<HeapType> type) { return Lub(*type); } |
| 122 static int InherentLub(Handle<HeapType> type) { return InherentLub(*type); } | 99 static int InherentLub(Handle<HeapType> type) { return InherentLub(*type); } |
| 123 }; | 100 }; |
| 124 }; | 101 }; |
| 125 | 102 |
| 126 | 103 |
| 127 template<class Type, class TypeHandle, class Region> | 104 template<class Type, class TypeHandle, class Region> |
| 128 class Types { | 105 class Types { |
| 129 public: | 106 public: |
| 130 Types(Region* region, Isolate* isolate) : region_(region) { | 107 Types(Region* region, Isolate* isolate) |
| 108 : region_(region), rng_(isolate->random_number_generator()) { |
| 131 #define DECLARE_TYPE(name, value) \ | 109 #define DECLARE_TYPE(name, value) \ |
| 132 name = Type::name(region); \ | 110 name = Type::name(region); \ |
| 133 types.push_back(name); | 111 types.push_back(name); |
| 134 BITSET_TYPE_LIST(DECLARE_TYPE) | 112 BITSET_TYPE_LIST(DECLARE_TYPE) |
| 135 #undef DECLARE_TYPE | 113 #undef DECLARE_TYPE |
| 136 | 114 |
| 137 object_map = isolate->factory()->NewMap(JS_OBJECT_TYPE, 3 * kPointerSize); | 115 object_map = isolate->factory()->NewMap(JS_OBJECT_TYPE, 3 * kPointerSize); |
| 138 array_map = isolate->factory()->NewMap(JS_ARRAY_TYPE, 4 * kPointerSize); | 116 array_map = isolate->factory()->NewMap(JS_ARRAY_TYPE, 4 * kPointerSize); |
| 139 uninitialized_map = isolate->factory()->uninitialized_map(); | 117 uninitialized_map = isolate->factory()->uninitialized_map(); |
| 140 ObjectClass = Type::Class(object_map, region); | 118 ObjectClass = Type::Class(object_map, region); |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 TypeHandle Intersect(TypeHandle t1, TypeHandle t2) { | 245 TypeHandle Intersect(TypeHandle t1, TypeHandle t2) { |
| 268 return Type::Intersect(t1, t2, region_); | 246 return Type::Intersect(t1, t2, region_); |
| 269 } | 247 } |
| 270 | 248 |
| 271 template<class Type2, class TypeHandle2> | 249 template<class Type2, class TypeHandle2> |
| 272 TypeHandle Convert(TypeHandle2 t) { | 250 TypeHandle Convert(TypeHandle2 t) { |
| 273 return Type::template Convert<Type2>(t, region_); | 251 return Type::template Convert<Type2>(t, region_); |
| 274 } | 252 } |
| 275 | 253 |
| 276 TypeHandle Random() { | 254 TypeHandle Random() { |
| 277 return types[rng_.NextInt(static_cast<int>(types.size()))]; | 255 return types[rng_->NextInt(static_cast<int>(types.size()))]; |
| 278 } | 256 } |
| 279 | 257 |
| 280 TypeHandle Fuzz(int depth = 5) { | 258 TypeHandle Fuzz(int depth = 5) { |
| 281 switch (rng_.NextInt(depth == 0 ? 3 : 20)) { | 259 switch (rng_->NextInt(depth == 0 ? 3 : 20)) { |
| 282 case 0: { // bitset | 260 case 0: { // bitset |
| 283 int n = 0 | 261 int n = 0 |
| 284 #define COUNT_BITSET_TYPES(type, value) + 1 | 262 #define COUNT_BITSET_TYPES(type, value) + 1 |
| 285 BITSET_TYPE_LIST(COUNT_BITSET_TYPES) | 263 BITSET_TYPE_LIST(COUNT_BITSET_TYPES) |
| 286 #undef COUNT_BITSET_TYPES | 264 #undef COUNT_BITSET_TYPES |
| 287 ; | 265 ; |
| 288 int i = rng_.NextInt(n); | 266 int i = rng_->NextInt(n); |
| 289 #define PICK_BITSET_TYPE(type, value) \ | 267 #define PICK_BITSET_TYPE(type, value) \ |
| 290 if (i-- == 0) return Type::type(region_); | 268 if (i-- == 0) return Type::type(region_); |
| 291 BITSET_TYPE_LIST(PICK_BITSET_TYPE) | 269 BITSET_TYPE_LIST(PICK_BITSET_TYPE) |
| 292 #undef PICK_BITSET_TYPE | 270 #undef PICK_BITSET_TYPE |
| 293 UNREACHABLE(); | 271 UNREACHABLE(); |
| 294 } | 272 } |
| 295 case 1: { // class | 273 case 1: { // class |
| 296 int i = rng_.NextInt(static_cast<int>(maps.size())); | 274 int i = rng_->NextInt(static_cast<int>(maps.size())); |
| 297 return Type::Class(maps[i], region_); | 275 return Type::Class(maps[i], region_); |
| 298 } | 276 } |
| 299 case 2: { // constant | 277 case 2: { // constant |
| 300 int i = rng_.NextInt(static_cast<int>(values.size())); | 278 int i = rng_->NextInt(static_cast<int>(values.size())); |
| 301 return Type::Constant(values[i], region_); | 279 return Type::Constant(values[i], region_); |
| 302 } | 280 } |
| 303 case 3: { // context | 281 case 3: { // context |
| 304 int depth = rng_.NextInt(3); | 282 int depth = rng_->NextInt(3); |
| 305 TypeHandle type = Type::Internal(region_); | 283 TypeHandle type = Type::Internal(region_); |
| 306 for (int i = 0; i < depth; ++i) type = Type::Context(type, region_); | 284 for (int i = 0; i < depth; ++i) type = Type::Context(type, region_); |
| 307 return type; | 285 return type; |
| 308 } | 286 } |
| 309 case 4: { // array | 287 case 4: { // array |
| 310 TypeHandle element = Fuzz(depth / 2); | 288 TypeHandle element = Fuzz(depth / 2); |
| 311 return Type::Array(element, region_); | 289 return Type::Array(element, region_); |
| 312 } | 290 } |
| 313 case 5: | 291 case 5: |
| 314 case 6: | 292 case 6: |
| 315 case 7: { // function | 293 case 7: { // function |
| 316 TypeHandle result = Fuzz(depth / 2); | 294 TypeHandle result = Fuzz(depth / 2); |
| 317 TypeHandle receiver = Fuzz(depth / 2); | 295 TypeHandle receiver = Fuzz(depth / 2); |
| 318 int arity = rng_.NextInt(3); | 296 int arity = rng_->NextInt(3); |
| 319 TypeHandle type = Type::Function(result, receiver, arity, region_); | 297 TypeHandle type = Type::Function(result, receiver, arity, region_); |
| 320 for (int i = 0; i < type->AsFunction()->Arity(); ++i) { | 298 for (int i = 0; i < type->AsFunction()->Arity(); ++i) { |
| 321 TypeHandle parameter = Fuzz(depth - 1); | 299 TypeHandle parameter = Fuzz(depth - 1); |
| 322 type->AsFunction()->InitParameter(i, parameter); | 300 type->AsFunction()->InitParameter(i, parameter); |
| 323 } | 301 } |
| 324 } | 302 } |
| 325 default: { // union | 303 default: { // union |
| 326 int n = rng_.NextInt(10); | 304 int n = rng_->NextInt(10); |
| 327 TypeHandle type = None; | 305 TypeHandle type = None; |
| 328 for (int i = 0; i < n; ++i) { | 306 for (int i = 0; i < n; ++i) { |
| 329 TypeHandle operand = Fuzz(depth - 1); | 307 TypeHandle operand = Fuzz(depth - 1); |
| 330 type = Type::Union(type, operand, region_); | 308 type = Type::Union(type, operand, region_); |
| 331 } | 309 } |
| 332 return type; | 310 return type; |
| 333 } | 311 } |
| 334 } | 312 } |
| 335 UNREACHABLE(); | 313 UNREACHABLE(); |
| 336 } | 314 } |
| 337 | 315 |
| 338 Region* region() { return region_; } | 316 Region* region() { return region_; } |
| 339 | 317 |
| 340 private: | 318 private: |
| 341 Region* region_; | 319 Region* region_; |
| 342 v8::base::RandomNumberGenerator rng_; | 320 v8::base::RandomNumberGenerator* rng_; |
| 343 }; | 321 }; |
| 344 | 322 |
| 345 | 323 |
| 346 template<class Type, class TypeHandle, class Region, class Rep> | 324 template<class Type, class TypeHandle, class Region, class Rep> |
| 347 struct Tests : Rep { | 325 struct Tests : Rep { |
| 348 typedef Types<Type, TypeHandle, Region> TypesInstance; | 326 typedef Types<Type, TypeHandle, Region> TypesInstance; |
| 349 typedef typename TypesInstance::TypeVector::iterator TypeIterator; | 327 typedef typename TypesInstance::TypeVector::iterator TypeIterator; |
| 350 typedef typename TypesInstance::MapVector::iterator MapIterator; | 328 typedef typename TypesInstance::MapVector::iterator MapIterator; |
| 351 typedef typename TypesInstance::ValueVector::iterator ValueIterator; | 329 typedef typename TypesInstance::ValueVector::iterator ValueIterator; |
| 352 | 330 |
| (...skipping 1580 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1933 ZoneTests().Convert<HeapType, Handle<HeapType>, Isolate, HeapRep>(); | 1911 ZoneTests().Convert<HeapType, Handle<HeapType>, Isolate, HeapRep>(); |
| 1934 HeapTests().Convert<Type, Type*, Zone, ZoneRep>(); | 1912 HeapTests().Convert<Type, Type*, Zone, ZoneRep>(); |
| 1935 } | 1913 } |
| 1936 | 1914 |
| 1937 | 1915 |
| 1938 TEST(HTypeFromType) { | 1916 TEST(HTypeFromType) { |
| 1939 CcTest::InitializeVM(); | 1917 CcTest::InitializeVM(); |
| 1940 ZoneTests().HTypeFromType(); | 1918 ZoneTests().HTypeFromType(); |
| 1941 HeapTests().HTypeFromType(); | 1919 HeapTests().HTypeFromType(); |
| 1942 } | 1920 } |
| OLD | NEW |