OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 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 |
| 28 #ifndef V8_TEST_CCTEST_TYPES_H_ |
| 29 #define V8_TEST_CCTEST_TYPES_H_ |
| 30 |
| 31 #include "src/v8.h" |
| 32 |
| 33 namespace v8 { |
| 34 namespace internal { |
| 35 |
| 36 |
| 37 template<class Type, class TypeHandle, class Region> |
| 38 class Types { |
| 39 public: |
| 40 Types(Region* region, Isolate* isolate) |
| 41 : region_(region), rng_(isolate->random_number_generator()) { |
| 42 #define DECLARE_TYPE(name, value) \ |
| 43 name = Type::name(region); \ |
| 44 if (SmiValuesAre31Bits() || \ |
| 45 (!Type::name(region)->Equals(Type::OtherSigned32()) && \ |
| 46 !Type::name(region)->Equals(Type::OtherUnsigned31()))) { \ |
| 47 /* Hack: Avoid generating those empty bitset types. */ \ |
| 48 types.push_back(name); \ |
| 49 } |
| 50 PROPER_BITSET_TYPE_LIST(DECLARE_TYPE) |
| 51 #undef DECLARE_TYPE |
| 52 |
| 53 object_map = isolate->factory()->NewMap( |
| 54 JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 55 array_map = isolate->factory()->NewMap( |
| 56 JS_ARRAY_TYPE, JSArray::kSize); |
| 57 number_map = isolate->factory()->NewMap( |
| 58 HEAP_NUMBER_TYPE, HeapNumber::kSize); |
| 59 uninitialized_map = isolate->factory()->uninitialized_map(); |
| 60 ObjectClass = Type::Class(object_map, region); |
| 61 ArrayClass = Type::Class(array_map, region); |
| 62 NumberClass = Type::Class(number_map, region); |
| 63 UninitializedClass = Type::Class(uninitialized_map, region); |
| 64 |
| 65 maps.push_back(object_map); |
| 66 maps.push_back(array_map); |
| 67 maps.push_back(uninitialized_map); |
| 68 for (MapVector::iterator it = maps.begin(); it != maps.end(); ++it) { |
| 69 types.push_back(Type::Class(*it, region)); |
| 70 } |
| 71 |
| 72 smi = handle(Smi::FromInt(666), isolate); |
| 73 signed32 = isolate->factory()->NewHeapNumber(0x40000000); |
| 74 object1 = isolate->factory()->NewJSObjectFromMap(object_map); |
| 75 object2 = isolate->factory()->NewJSObjectFromMap(object_map); |
| 76 array = isolate->factory()->NewJSArray(20); |
| 77 uninitialized = isolate->factory()->uninitialized_value(); |
| 78 SmiConstant = Type::Constant(smi, region); |
| 79 Signed32Constant = Type::Constant(signed32, region); |
| 80 ObjectConstant1 = Type::Constant(object1, region); |
| 81 ObjectConstant2 = Type::Constant(object2, region); |
| 82 ArrayConstant = Type::Constant(array, region); |
| 83 UninitializedConstant = Type::Constant(uninitialized, region); |
| 84 |
| 85 values.push_back(smi); |
| 86 values.push_back(signed32); |
| 87 values.push_back(object1); |
| 88 values.push_back(object2); |
| 89 values.push_back(array); |
| 90 values.push_back(uninitialized); |
| 91 for (ValueVector::iterator it = values.begin(); it != values.end(); ++it) { |
| 92 types.push_back(Type::Constant(*it, region)); |
| 93 } |
| 94 |
| 95 integers.push_back(isolate->factory()->NewNumber(-V8_INFINITY)); |
| 96 integers.push_back(isolate->factory()->NewNumber(+V8_INFINITY)); |
| 97 integers.push_back(isolate->factory()->NewNumber(-rng_->NextInt(10))); |
| 98 integers.push_back(isolate->factory()->NewNumber(+rng_->NextInt(10))); |
| 99 for (int i = 0; i < 10; ++i) { |
| 100 double x = rng_->NextInt(); |
| 101 integers.push_back(isolate->factory()->NewNumber(x)); |
| 102 x *= rng_->NextInt(); |
| 103 if (!IsMinusZero(x)) integers.push_back(isolate->factory()->NewNumber(x)); |
| 104 } |
| 105 |
| 106 NumberArray = Type::Array(Number, region); |
| 107 StringArray = Type::Array(String, region); |
| 108 AnyArray = Type::Array(Any, region); |
| 109 |
| 110 SignedFunction1 = Type::Function(SignedSmall, SignedSmall, region); |
| 111 NumberFunction1 = Type::Function(Number, Number, region); |
| 112 NumberFunction2 = Type::Function(Number, Number, Number, region); |
| 113 MethodFunction = Type::Function(String, Object, 0, region); |
| 114 |
| 115 for (int i = 0; i < 30; ++i) { |
| 116 types.push_back(Fuzz()); |
| 117 } |
| 118 } |
| 119 |
| 120 Handle<i::Map> object_map; |
| 121 Handle<i::Map> array_map; |
| 122 Handle<i::Map> number_map; |
| 123 Handle<i::Map> uninitialized_map; |
| 124 |
| 125 Handle<i::Smi> smi; |
| 126 Handle<i::HeapNumber> signed32; |
| 127 Handle<i::JSObject> object1; |
| 128 Handle<i::JSObject> object2; |
| 129 Handle<i::JSArray> array; |
| 130 Handle<i::Oddball> uninitialized; |
| 131 |
| 132 #define DECLARE_TYPE(name, value) TypeHandle name; |
| 133 BITSET_TYPE_LIST(DECLARE_TYPE) |
| 134 #undef DECLARE_TYPE |
| 135 |
| 136 TypeHandle ObjectClass; |
| 137 TypeHandle ArrayClass; |
| 138 TypeHandle NumberClass; |
| 139 TypeHandle UninitializedClass; |
| 140 |
| 141 TypeHandle SmiConstant; |
| 142 TypeHandle Signed32Constant; |
| 143 TypeHandle ObjectConstant1; |
| 144 TypeHandle ObjectConstant2; |
| 145 TypeHandle ArrayConstant; |
| 146 TypeHandle UninitializedConstant; |
| 147 |
| 148 TypeHandle NumberArray; |
| 149 TypeHandle StringArray; |
| 150 TypeHandle AnyArray; |
| 151 |
| 152 TypeHandle SignedFunction1; |
| 153 TypeHandle NumberFunction1; |
| 154 TypeHandle NumberFunction2; |
| 155 TypeHandle MethodFunction; |
| 156 |
| 157 typedef std::vector<TypeHandle> TypeVector; |
| 158 typedef std::vector<Handle<i::Map> > MapVector; |
| 159 typedef std::vector<Handle<i::Object> > ValueVector; |
| 160 |
| 161 TypeVector types; |
| 162 MapVector maps; |
| 163 ValueVector values; |
| 164 ValueVector integers; // "Integer" values used for range limits. |
| 165 |
| 166 TypeHandle Of(Handle<i::Object> value) { |
| 167 return Type::Of(value, region_); |
| 168 } |
| 169 |
| 170 TypeHandle NowOf(Handle<i::Object> value) { |
| 171 return Type::NowOf(value, region_); |
| 172 } |
| 173 |
| 174 TypeHandle Class(Handle<i::Map> map) { |
| 175 return Type::Class(map, region_); |
| 176 } |
| 177 |
| 178 TypeHandle Constant(Handle<i::Object> value) { |
| 179 return Type::Constant(value, region_); |
| 180 } |
| 181 |
| 182 TypeHandle Range(Handle<i::Object> min, Handle<i::Object> max) { |
| 183 return Type::Range(min, max, region_); |
| 184 } |
| 185 |
| 186 TypeHandle Context(TypeHandle outer) { |
| 187 return Type::Context(outer, region_); |
| 188 } |
| 189 |
| 190 TypeHandle Array1(TypeHandle element) { |
| 191 return Type::Array(element, region_); |
| 192 } |
| 193 |
| 194 TypeHandle Function0(TypeHandle result, TypeHandle receiver) { |
| 195 return Type::Function(result, receiver, 0, region_); |
| 196 } |
| 197 |
| 198 TypeHandle Function1(TypeHandle result, TypeHandle receiver, TypeHandle arg) { |
| 199 TypeHandle type = Type::Function(result, receiver, 1, region_); |
| 200 type->AsFunction()->InitParameter(0, arg); |
| 201 return type; |
| 202 } |
| 203 |
| 204 TypeHandle Function2(TypeHandle result, TypeHandle arg1, TypeHandle arg2) { |
| 205 return Type::Function(result, arg1, arg2, region_); |
| 206 } |
| 207 |
| 208 TypeHandle Union(TypeHandle t1, TypeHandle t2) { |
| 209 return Type::Union(t1, t2, region_); |
| 210 } |
| 211 TypeHandle Intersect(TypeHandle t1, TypeHandle t2) { |
| 212 return Type::Intersect(t1, t2, region_); |
| 213 } |
| 214 |
| 215 template<class Type2, class TypeHandle2> |
| 216 TypeHandle Convert(TypeHandle2 t) { |
| 217 return Type::template Convert<Type2>(t, region_); |
| 218 } |
| 219 |
| 220 TypeHandle Random() { |
| 221 return types[rng_->NextInt(static_cast<int>(types.size()))]; |
| 222 } |
| 223 |
| 224 TypeHandle Fuzz(int depth = 4) { |
| 225 switch (rng_->NextInt(depth == 0 ? 3 : 20)) { |
| 226 case 0: { // bitset |
| 227 #define COUNT_BITSET_TYPES(type, value) + 1 |
| 228 int n = 0 PROPER_BITSET_TYPE_LIST(COUNT_BITSET_TYPES); |
| 229 #undef COUNT_BITSET_TYPES |
| 230 // Pick a bunch of named bitsets and return their intersection. |
| 231 TypeHandle result = Type::Any(region_); |
| 232 for (int i = 0, m = 1 + rng_->NextInt(3); i < m; ++i) { |
| 233 int j = rng_->NextInt(n); |
| 234 #define PICK_BITSET_TYPE(type, value) \ |
| 235 if (j-- == 0) { \ |
| 236 if (!SmiValuesAre31Bits() && \ |
| 237 (Type::type(region_)->Equals(Type::OtherSigned32()) || \ |
| 238 Type::type(region_)->Equals(Type::OtherUnsigned31()))) { \ |
| 239 /* Hack: Avoid generating those empty bitset types. */ \ |
| 240 continue; \ |
| 241 } \ |
| 242 TypeHandle tmp = Type::Intersect( \ |
| 243 result, Type::type(region_), region_); \ |
| 244 if (tmp->Is(Type::None()) && i != 0) { \ |
| 245 break; \ |
| 246 } else { \ |
| 247 result = tmp; \ |
| 248 continue; \ |
| 249 } \ |
| 250 } |
| 251 PROPER_BITSET_TYPE_LIST(PICK_BITSET_TYPE) |
| 252 #undef PICK_BITSET_TYPE |
| 253 } |
| 254 return result; |
| 255 } |
| 256 case 1: { // class |
| 257 int i = rng_->NextInt(static_cast<int>(maps.size())); |
| 258 return Type::Class(maps[i], region_); |
| 259 } |
| 260 case 2: { // constant |
| 261 int i = rng_->NextInt(static_cast<int>(values.size())); |
| 262 return Type::Constant(values[i], region_); |
| 263 } |
| 264 case 3: { // range |
| 265 int i = rng_->NextInt(static_cast<int>(integers.size())); |
| 266 int j = rng_->NextInt(static_cast<int>(integers.size())); |
| 267 i::Handle<i::Object> min = integers[i]; |
| 268 i::Handle<i::Object> max = integers[j]; |
| 269 if (min->Number() > max->Number()) std::swap(min, max); |
| 270 return Type::Range(min, max, region_); |
| 271 } |
| 272 case 4: { // context |
| 273 int depth = rng_->NextInt(3); |
| 274 TypeHandle type = Type::Internal(region_); |
| 275 for (int i = 0; i < depth; ++i) type = Type::Context(type, region_); |
| 276 return type; |
| 277 } |
| 278 case 5: { // array |
| 279 TypeHandle element = Fuzz(depth / 2); |
| 280 return Type::Array(element, region_); |
| 281 } |
| 282 case 6: |
| 283 case 7: { // function |
| 284 TypeHandle result = Fuzz(depth / 2); |
| 285 TypeHandle receiver = Fuzz(depth / 2); |
| 286 int arity = rng_->NextInt(3); |
| 287 TypeHandle type = Type::Function(result, receiver, arity, region_); |
| 288 for (int i = 0; i < type->AsFunction()->Arity(); ++i) { |
| 289 TypeHandle parameter = Fuzz(depth / 2); |
| 290 type->AsFunction()->InitParameter(i, parameter); |
| 291 } |
| 292 return type; |
| 293 } |
| 294 default: { // union |
| 295 int n = rng_->NextInt(10); |
| 296 TypeHandle type = None; |
| 297 for (int i = 0; i < n; ++i) { |
| 298 TypeHandle operand = Fuzz(depth - 1); |
| 299 type = Type::Union(type, operand, region_); |
| 300 } |
| 301 return type; |
| 302 } |
| 303 } |
| 304 UNREACHABLE(); |
| 305 } |
| 306 |
| 307 Region* region() { return region_; } |
| 308 |
| 309 private: |
| 310 Region* region_; |
| 311 v8::base::RandomNumberGenerator* rng_; |
| 312 }; |
| 313 |
| 314 |
| 315 } } // namespace v8::internal |
| 316 |
| 317 #endif |
OLD | NEW |