| OLD | NEW |
| 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 #include "test/cctest/types.h" |
| 11 | 12 |
| 12 using namespace v8::internal; | 13 using namespace v8::internal; |
| 13 | 14 |
| 14 | 15 |
| 15 // Testing auxiliaries (breaking the Type abstraction). | 16 // Testing auxiliaries (breaking the Type abstraction). |
| 16 | 17 |
| 17 | 18 |
| 18 static bool IsInteger(double x) { | 19 static bool IsInteger(double x) { |
| 19 return nearbyint(x) == x && !i::IsMinusZero(x); // Allows for infinities. | 20 return nearbyint(x) == x && !i::IsMinusZero(x); // Allows for infinities. |
| 20 } | 21 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 using HeapType::BitsetType::New; | 84 using HeapType::BitsetType::New; |
| 84 using HeapType::BitsetType::Glb; | 85 using HeapType::BitsetType::Glb; |
| 85 using HeapType::BitsetType::Lub; | 86 using HeapType::BitsetType::Lub; |
| 86 using HeapType::BitsetType::IsInhabited; | 87 using HeapType::BitsetType::IsInhabited; |
| 87 static bitset Glb(Handle<HeapType> type) { return Glb(*type); } | 88 static bitset Glb(Handle<HeapType> type) { return Glb(*type); } |
| 88 static bitset Lub(Handle<HeapType> type) { return Lub(*type); } | 89 static bitset Lub(Handle<HeapType> type) { return Lub(*type); } |
| 89 }; | 90 }; |
| 90 }; | 91 }; |
| 91 | 92 |
| 92 | 93 |
| 93 template<class Type, class TypeHandle, class Region> | |
| 94 class Types { | |
| 95 public: | |
| 96 Types(Region* region, Isolate* isolate) | |
| 97 : region_(region), rng_(isolate->random_number_generator()) { | |
| 98 #define DECLARE_TYPE(name, value) \ | |
| 99 name = Type::name(region); \ | |
| 100 types.push_back(name); | |
| 101 PROPER_BITSET_TYPE_LIST(DECLARE_TYPE) | |
| 102 #undef DECLARE_TYPE | |
| 103 | |
| 104 object_map = isolate->factory()->NewMap( | |
| 105 JS_OBJECT_TYPE, JSObject::kHeaderSize); | |
| 106 array_map = isolate->factory()->NewMap( | |
| 107 JS_ARRAY_TYPE, JSArray::kSize); | |
| 108 number_map = isolate->factory()->NewMap( | |
| 109 HEAP_NUMBER_TYPE, HeapNumber::kSize); | |
| 110 uninitialized_map = isolate->factory()->uninitialized_map(); | |
| 111 ObjectClass = Type::Class(object_map, region); | |
| 112 ArrayClass = Type::Class(array_map, region); | |
| 113 NumberClass = Type::Class(number_map, region); | |
| 114 UninitializedClass = Type::Class(uninitialized_map, region); | |
| 115 | |
| 116 maps.push_back(object_map); | |
| 117 maps.push_back(array_map); | |
| 118 maps.push_back(uninitialized_map); | |
| 119 for (MapVector::iterator it = maps.begin(); it != maps.end(); ++it) { | |
| 120 types.push_back(Type::Class(*it, region)); | |
| 121 } | |
| 122 | |
| 123 smi = handle(Smi::FromInt(666), isolate); | |
| 124 signed32 = isolate->factory()->NewHeapNumber(0x40000000); | |
| 125 object1 = isolate->factory()->NewJSObjectFromMap(object_map); | |
| 126 object2 = isolate->factory()->NewJSObjectFromMap(object_map); | |
| 127 array = isolate->factory()->NewJSArray(20); | |
| 128 uninitialized = isolate->factory()->uninitialized_value(); | |
| 129 SmiConstant = Type::Constant(smi, region); | |
| 130 Signed32Constant = Type::Constant(signed32, region); | |
| 131 ObjectConstant1 = Type::Constant(object1, region); | |
| 132 ObjectConstant2 = Type::Constant(object2, region); | |
| 133 ArrayConstant = Type::Constant(array, region); | |
| 134 UninitializedConstant = Type::Constant(uninitialized, region); | |
| 135 | |
| 136 values.push_back(smi); | |
| 137 values.push_back(signed32); | |
| 138 values.push_back(object1); | |
| 139 values.push_back(object2); | |
| 140 values.push_back(array); | |
| 141 values.push_back(uninitialized); | |
| 142 for (ValueVector::iterator it = values.begin(); it != values.end(); ++it) { | |
| 143 types.push_back(Type::Constant(*it, region)); | |
| 144 } | |
| 145 | |
| 146 integers.push_back(isolate->factory()->NewNumber(-V8_INFINITY)); | |
| 147 integers.push_back(isolate->factory()->NewNumber(+V8_INFINITY)); | |
| 148 integers.push_back(isolate->factory()->NewNumber(-rng_->NextInt(10))); | |
| 149 integers.push_back(isolate->factory()->NewNumber(+rng_->NextInt(10))); | |
| 150 for (int i = 0; i < 10; ++i) { | |
| 151 double x = rng_->NextInt(); | |
| 152 integers.push_back(isolate->factory()->NewNumber(x)); | |
| 153 x *= rng_->NextInt(); | |
| 154 if (!IsMinusZero(x)) integers.push_back(isolate->factory()->NewNumber(x)); | |
| 155 } | |
| 156 | |
| 157 NumberArray = Type::Array(Number, region); | |
| 158 StringArray = Type::Array(String, region); | |
| 159 AnyArray = Type::Array(Any, region); | |
| 160 | |
| 161 SignedFunction1 = Type::Function(SignedSmall, SignedSmall, region); | |
| 162 NumberFunction1 = Type::Function(Number, Number, region); | |
| 163 NumberFunction2 = Type::Function(Number, Number, Number, region); | |
| 164 MethodFunction = Type::Function(String, Object, 0, region); | |
| 165 | |
| 166 for (int i = 0; i < 30; ++i) { | |
| 167 types.push_back(Fuzz()); | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 Handle<i::Map> object_map; | |
| 172 Handle<i::Map> array_map; | |
| 173 Handle<i::Map> number_map; | |
| 174 Handle<i::Map> uninitialized_map; | |
| 175 | |
| 176 Handle<i::Smi> smi; | |
| 177 Handle<i::HeapNumber> signed32; | |
| 178 Handle<i::JSObject> object1; | |
| 179 Handle<i::JSObject> object2; | |
| 180 Handle<i::JSArray> array; | |
| 181 Handle<i::Oddball> uninitialized; | |
| 182 | |
| 183 #define DECLARE_TYPE(name, value) TypeHandle name; | |
| 184 BITSET_TYPE_LIST(DECLARE_TYPE) | |
| 185 #undef DECLARE_TYPE | |
| 186 | |
| 187 TypeHandle ObjectClass; | |
| 188 TypeHandle ArrayClass; | |
| 189 TypeHandle NumberClass; | |
| 190 TypeHandle UninitializedClass; | |
| 191 | |
| 192 TypeHandle SmiConstant; | |
| 193 TypeHandle Signed32Constant; | |
| 194 TypeHandle ObjectConstant1; | |
| 195 TypeHandle ObjectConstant2; | |
| 196 TypeHandle ArrayConstant; | |
| 197 TypeHandle UninitializedConstant; | |
| 198 | |
| 199 TypeHandle NumberArray; | |
| 200 TypeHandle StringArray; | |
| 201 TypeHandle AnyArray; | |
| 202 | |
| 203 TypeHandle SignedFunction1; | |
| 204 TypeHandle NumberFunction1; | |
| 205 TypeHandle NumberFunction2; | |
| 206 TypeHandle MethodFunction; | |
| 207 | |
| 208 typedef std::vector<TypeHandle> TypeVector; | |
| 209 typedef std::vector<Handle<i::Map> > MapVector; | |
| 210 typedef std::vector<Handle<i::Object> > ValueVector; | |
| 211 | |
| 212 TypeVector types; | |
| 213 MapVector maps; | |
| 214 ValueVector values; | |
| 215 ValueVector integers; // "Integer" values used for range limits. | |
| 216 | |
| 217 TypeHandle Of(Handle<i::Object> value) { | |
| 218 return Type::Of(value, region_); | |
| 219 } | |
| 220 | |
| 221 TypeHandle NowOf(Handle<i::Object> value) { | |
| 222 return Type::NowOf(value, region_); | |
| 223 } | |
| 224 | |
| 225 TypeHandle Class(Handle<i::Map> map) { | |
| 226 return Type::Class(map, region_); | |
| 227 } | |
| 228 | |
| 229 TypeHandle Constant(Handle<i::Object> value) { | |
| 230 return Type::Constant(value, region_); | |
| 231 } | |
| 232 | |
| 233 TypeHandle Range(Handle<i::Object> min, Handle<i::Object> max) { | |
| 234 return Type::Range(min, max, region_); | |
| 235 } | |
| 236 | |
| 237 TypeHandle Context(TypeHandle outer) { | |
| 238 return Type::Context(outer, region_); | |
| 239 } | |
| 240 | |
| 241 TypeHandle Array1(TypeHandle element) { | |
| 242 return Type::Array(element, region_); | |
| 243 } | |
| 244 | |
| 245 TypeHandle Function0(TypeHandle result, TypeHandle receiver) { | |
| 246 return Type::Function(result, receiver, 0, region_); | |
| 247 } | |
| 248 | |
| 249 TypeHandle Function1(TypeHandle result, TypeHandle receiver, TypeHandle arg) { | |
| 250 TypeHandle type = Type::Function(result, receiver, 1, region_); | |
| 251 type->AsFunction()->InitParameter(0, arg); | |
| 252 return type; | |
| 253 } | |
| 254 | |
| 255 TypeHandle Function2(TypeHandle result, TypeHandle arg1, TypeHandle arg2) { | |
| 256 return Type::Function(result, arg1, arg2, region_); | |
| 257 } | |
| 258 | |
| 259 TypeHandle Union(TypeHandle t1, TypeHandle t2) { | |
| 260 return Type::Union(t1, t2, region_); | |
| 261 } | |
| 262 TypeHandle Intersect(TypeHandle t1, TypeHandle t2) { | |
| 263 return Type::Intersect(t1, t2, region_); | |
| 264 } | |
| 265 | |
| 266 template<class Type2, class TypeHandle2> | |
| 267 TypeHandle Convert(TypeHandle2 t) { | |
| 268 return Type::template Convert<Type2>(t, region_); | |
| 269 } | |
| 270 | |
| 271 TypeHandle Random() { | |
| 272 return types[rng_->NextInt(static_cast<int>(types.size()))]; | |
| 273 } | |
| 274 | |
| 275 TypeHandle Fuzz(int depth = 4) { | |
| 276 switch (rng_->NextInt(depth == 0 ? 3 : 20)) { | |
| 277 case 0: { // bitset | |
| 278 #define COUNT_BITSET_TYPES(type, value) + 1 | |
| 279 int n = 0 PROPER_BITSET_TYPE_LIST(COUNT_BITSET_TYPES); | |
| 280 #undef COUNT_BITSET_TYPES | |
| 281 // Pick a bunch of named bitsets and return their intersection. | |
| 282 TypeHandle result = Type::Any(region_); | |
| 283 for (int i = 0, m = 1 + rng_->NextInt(3); i < m; ++i) { | |
| 284 int j = rng_->NextInt(n); | |
| 285 #define PICK_BITSET_TYPE(type, value) \ | |
| 286 if (j-- == 0) { \ | |
| 287 TypeHandle tmp = Type::Intersect( \ | |
| 288 result, Type::type(region_), region_); \ | |
| 289 if (tmp->Is(Type::None()) && i != 0) { \ | |
| 290 break; \ | |
| 291 } { \ | |
| 292 result = tmp; \ | |
| 293 continue; \ | |
| 294 } \ | |
| 295 } | |
| 296 PROPER_BITSET_TYPE_LIST(PICK_BITSET_TYPE) | |
| 297 #undef PICK_BITSET_TYPE | |
| 298 } | |
| 299 return result; | |
| 300 } | |
| 301 case 1: { // class | |
| 302 int i = rng_->NextInt(static_cast<int>(maps.size())); | |
| 303 return Type::Class(maps[i], region_); | |
| 304 } | |
| 305 case 2: { // constant | |
| 306 int i = rng_->NextInt(static_cast<int>(values.size())); | |
| 307 return Type::Constant(values[i], region_); | |
| 308 } | |
| 309 case 3: { // range | |
| 310 int i = rng_->NextInt(static_cast<int>(integers.size())); | |
| 311 int j = rng_->NextInt(static_cast<int>(integers.size())); | |
| 312 i::Handle<i::Object> min = integers[i]; | |
| 313 i::Handle<i::Object> max = integers[j]; | |
| 314 if (min->Number() > max->Number()) std::swap(min, max); | |
| 315 return Type::Range(min, max, region_); | |
| 316 } | |
| 317 case 4: { // context | |
| 318 int depth = rng_->NextInt(3); | |
| 319 TypeHandle type = Type::Internal(region_); | |
| 320 for (int i = 0; i < depth; ++i) type = Type::Context(type, region_); | |
| 321 return type; | |
| 322 } | |
| 323 case 5: { // array | |
| 324 TypeHandle element = Fuzz(depth / 2); | |
| 325 return Type::Array(element, region_); | |
| 326 } | |
| 327 case 6: | |
| 328 case 7: { // function | |
| 329 TypeHandle result = Fuzz(depth / 2); | |
| 330 TypeHandle receiver = Fuzz(depth / 2); | |
| 331 int arity = rng_->NextInt(3); | |
| 332 TypeHandle type = Type::Function(result, receiver, arity, region_); | |
| 333 for (int i = 0; i < type->AsFunction()->Arity(); ++i) { | |
| 334 TypeHandle parameter = Fuzz(depth / 2); | |
| 335 type->AsFunction()->InitParameter(i, parameter); | |
| 336 } | |
| 337 return type; | |
| 338 } | |
| 339 default: { // union | |
| 340 int n = rng_->NextInt(10); | |
| 341 TypeHandle type = None; | |
| 342 for (int i = 0; i < n; ++i) { | |
| 343 TypeHandle operand = Fuzz(depth - 1); | |
| 344 type = Type::Union(type, operand, region_); | |
| 345 } | |
| 346 return type; | |
| 347 } | |
| 348 } | |
| 349 UNREACHABLE(); | |
| 350 } | |
| 351 | |
| 352 Region* region() { return region_; } | |
| 353 | |
| 354 private: | |
| 355 Region* region_; | |
| 356 v8::base::RandomNumberGenerator* rng_; | |
| 357 }; | |
| 358 | |
| 359 | |
| 360 template<class Type, class TypeHandle, class Region, class Rep> | 94 template<class Type, class TypeHandle, class Region, class Rep> |
| 361 struct Tests : Rep { | 95 struct Tests : Rep { |
| 362 typedef Types<Type, TypeHandle, Region> TypesInstance; | 96 typedef Types<Type, TypeHandle, Region> TypesInstance; |
| 363 typedef typename TypesInstance::TypeVector::iterator TypeIterator; | 97 typedef typename TypesInstance::TypeVector::iterator TypeIterator; |
| 364 typedef typename TypesInstance::MapVector::iterator MapIterator; | 98 typedef typename TypesInstance::MapVector::iterator MapIterator; |
| 365 typedef typename TypesInstance::ValueVector::iterator ValueIterator; | 99 typedef typename TypesInstance::ValueVector::iterator ValueIterator; |
| 366 | 100 |
| 367 Isolate* isolate; | 101 Isolate* isolate; |
| 368 HandleScope scope; | 102 HandleScope scope; |
| 369 Zone zone; | 103 Zone zone; |
| (...skipping 1802 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2172 } | 1906 } |
| 2173 | 1907 |
| 2174 | 1908 |
| 2175 TEST(NowOf) { | 1909 TEST(NowOf) { |
| 2176 CcTest::InitializeVM(); | 1910 CcTest::InitializeVM(); |
| 2177 ZoneTests().NowOf(); | 1911 ZoneTests().NowOf(); |
| 2178 HeapTests().NowOf(); | 1912 HeapTests().NowOf(); |
| 2179 } | 1913 } |
| 2180 | 1914 |
| 2181 | 1915 |
| 1916 TEST(MinMax) { |
| 1917 CcTest::InitializeVM(); |
| 1918 ZoneTests().MinMax(); |
| 1919 HeapTests().MinMax(); |
| 1920 } |
| 1921 |
| 1922 |
| 2182 TEST(BitsetGlb) { | 1923 TEST(BitsetGlb) { |
| 2183 CcTest::InitializeVM(); | 1924 CcTest::InitializeVM(); |
| 2184 ZoneTests().BitsetGlb(); | 1925 ZoneTests().BitsetGlb(); |
| 2185 HeapTests().BitsetGlb(); | 1926 HeapTests().BitsetGlb(); |
| 2186 } | 1927 } |
| 2187 | 1928 |
| 2188 | 1929 |
| 2189 TEST(BitsetLub) { | 1930 TEST(BitsetLub) { |
| 2190 CcTest::InitializeVM(); | 1931 CcTest::InitializeVM(); |
| 2191 ZoneTests().BitsetLub(); | 1932 ZoneTests().BitsetLub(); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2284 ZoneTests().Convert<HeapType, Handle<HeapType>, Isolate, HeapRep>(); | 2025 ZoneTests().Convert<HeapType, Handle<HeapType>, Isolate, HeapRep>(); |
| 2285 HeapTests().Convert<Type, Type*, Zone, ZoneRep>(); | 2026 HeapTests().Convert<Type, Type*, Zone, ZoneRep>(); |
| 2286 } | 2027 } |
| 2287 | 2028 |
| 2288 | 2029 |
| 2289 TEST(HTypeFromType) { | 2030 TEST(HTypeFromType) { |
| 2290 CcTest::InitializeVM(); | 2031 CcTest::InitializeVM(); |
| 2291 ZoneTests().HTypeFromType(); | 2032 ZoneTests().HTypeFromType(); |
| 2292 HeapTests().HTypeFromType(); | 2033 HeapTests().HTypeFromType(); |
| 2293 } | 2034 } |
| OLD | NEW |