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/test-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 if (SmiValuesAre31Bits() || \ | |
101 (!Type::name(region)->Equals(Type::OtherSigned32()) && \ | |
102 !Type::name(region)->Equals(Type::OtherUnsigned31()))) { \ | |
103 /* Hack: Avoid generating those empty bitset types. */ \ | |
104 types.push_back(name); \ | |
105 } | |
106 PROPER_BITSET_TYPE_LIST(DECLARE_TYPE) | |
107 #undef DECLARE_TYPE | |
108 | |
109 object_map = isolate->factory()->NewMap( | |
110 JS_OBJECT_TYPE, JSObject::kHeaderSize); | |
111 array_map = isolate->factory()->NewMap( | |
112 JS_ARRAY_TYPE, JSArray::kSize); | |
113 number_map = isolate->factory()->NewMap( | |
114 HEAP_NUMBER_TYPE, HeapNumber::kSize); | |
115 uninitialized_map = isolate->factory()->uninitialized_map(); | |
116 ObjectClass = Type::Class(object_map, region); | |
117 ArrayClass = Type::Class(array_map, region); | |
118 NumberClass = Type::Class(number_map, region); | |
119 UninitializedClass = Type::Class(uninitialized_map, region); | |
120 | |
121 maps.push_back(object_map); | |
122 maps.push_back(array_map); | |
123 maps.push_back(uninitialized_map); | |
124 for (MapVector::iterator it = maps.begin(); it != maps.end(); ++it) { | |
125 types.push_back(Type::Class(*it, region)); | |
126 } | |
127 | |
128 smi = handle(Smi::FromInt(666), isolate); | |
129 signed32 = isolate->factory()->NewHeapNumber(0x40000000); | |
130 object1 = isolate->factory()->NewJSObjectFromMap(object_map); | |
131 object2 = isolate->factory()->NewJSObjectFromMap(object_map); | |
132 array = isolate->factory()->NewJSArray(20); | |
133 uninitialized = isolate->factory()->uninitialized_value(); | |
134 SmiConstant = Type::Constant(smi, region); | |
135 Signed32Constant = Type::Constant(signed32, region); | |
136 ObjectConstant1 = Type::Constant(object1, region); | |
137 ObjectConstant2 = Type::Constant(object2, region); | |
138 ArrayConstant = Type::Constant(array, region); | |
139 UninitializedConstant = Type::Constant(uninitialized, region); | |
140 | |
141 values.push_back(smi); | |
142 values.push_back(signed32); | |
143 values.push_back(object1); | |
144 values.push_back(object2); | |
145 values.push_back(array); | |
146 values.push_back(uninitialized); | |
147 for (ValueVector::iterator it = values.begin(); it != values.end(); ++it) { | |
148 types.push_back(Type::Constant(*it, region)); | |
149 } | |
150 | |
151 integers.push_back(isolate->factory()->NewNumber(-V8_INFINITY)); | |
152 integers.push_back(isolate->factory()->NewNumber(+V8_INFINITY)); | |
153 integers.push_back(isolate->factory()->NewNumber(-rng_->NextInt(10))); | |
154 integers.push_back(isolate->factory()->NewNumber(+rng_->NextInt(10))); | |
155 for (int i = 0; i < 10; ++i) { | |
156 double x = rng_->NextInt(); | |
157 integers.push_back(isolate->factory()->NewNumber(x)); | |
158 x *= rng_->NextInt(); | |
159 if (!IsMinusZero(x)) integers.push_back(isolate->factory()->NewNumber(x)); | |
160 } | |
161 | |
162 NumberArray = Type::Array(Number, region); | |
163 StringArray = Type::Array(String, region); | |
164 AnyArray = Type::Array(Any, region); | |
165 | |
166 SignedFunction1 = Type::Function(SignedSmall, SignedSmall, region); | |
167 NumberFunction1 = Type::Function(Number, Number, region); | |
168 NumberFunction2 = Type::Function(Number, Number, Number, region); | |
169 MethodFunction = Type::Function(String, Object, 0, region); | |
170 | |
171 for (int i = 0; i < 30; ++i) { | |
172 types.push_back(Fuzz()); | |
173 } | |
174 } | |
175 | |
176 Handle<i::Map> object_map; | |
177 Handle<i::Map> array_map; | |
178 Handle<i::Map> number_map; | |
179 Handle<i::Map> uninitialized_map; | |
180 | |
181 Handle<i::Smi> smi; | |
182 Handle<i::HeapNumber> signed32; | |
183 Handle<i::JSObject> object1; | |
184 Handle<i::JSObject> object2; | |
185 Handle<i::JSArray> array; | |
186 Handle<i::Oddball> uninitialized; | |
187 | |
188 #define DECLARE_TYPE(name, value) TypeHandle name; | |
189 BITSET_TYPE_LIST(DECLARE_TYPE) | |
190 #undef DECLARE_TYPE | |
191 | |
192 TypeHandle ObjectClass; | |
193 TypeHandle ArrayClass; | |
194 TypeHandle NumberClass; | |
195 TypeHandle UninitializedClass; | |
196 | |
197 TypeHandle SmiConstant; | |
198 TypeHandle Signed32Constant; | |
199 TypeHandle ObjectConstant1; | |
200 TypeHandle ObjectConstant2; | |
201 TypeHandle ArrayConstant; | |
202 TypeHandle UninitializedConstant; | |
203 | |
204 TypeHandle NumberArray; | |
205 TypeHandle StringArray; | |
206 TypeHandle AnyArray; | |
207 | |
208 TypeHandle SignedFunction1; | |
209 TypeHandle NumberFunction1; | |
210 TypeHandle NumberFunction2; | |
211 TypeHandle MethodFunction; | |
212 | |
213 typedef std::vector<TypeHandle> TypeVector; | |
214 typedef std::vector<Handle<i::Map> > MapVector; | |
215 typedef std::vector<Handle<i::Object> > ValueVector; | |
216 | |
217 TypeVector types; | |
218 MapVector maps; | |
219 ValueVector values; | |
220 ValueVector integers; // "Integer" values used for range limits. | |
221 | |
222 TypeHandle Of(Handle<i::Object> value) { | |
223 return Type::Of(value, region_); | |
224 } | |
225 | |
226 TypeHandle NowOf(Handle<i::Object> value) { | |
227 return Type::NowOf(value, region_); | |
228 } | |
229 | |
230 TypeHandle Class(Handle<i::Map> map) { | |
231 return Type::Class(map, region_); | |
232 } | |
233 | |
234 TypeHandle Constant(Handle<i::Object> value) { | |
235 return Type::Constant(value, region_); | |
236 } | |
237 | |
238 TypeHandle Range(Handle<i::Object> min, Handle<i::Object> max) { | |
239 return Type::Range(min, max, region_); | |
240 } | |
241 | |
242 TypeHandle Context(TypeHandle outer) { | |
243 return Type::Context(outer, region_); | |
244 } | |
245 | |
246 TypeHandle Array1(TypeHandle element) { | |
247 return Type::Array(element, region_); | |
248 } | |
249 | |
250 TypeHandle Function0(TypeHandle result, TypeHandle receiver) { | |
251 return Type::Function(result, receiver, 0, region_); | |
252 } | |
253 | |
254 TypeHandle Function1(TypeHandle result, TypeHandle receiver, TypeHandle arg) { | |
255 TypeHandle type = Type::Function(result, receiver, 1, region_); | |
256 type->AsFunction()->InitParameter(0, arg); | |
257 return type; | |
258 } | |
259 | |
260 TypeHandle Function2(TypeHandle result, TypeHandle arg1, TypeHandle arg2) { | |
261 return Type::Function(result, arg1, arg2, region_); | |
262 } | |
263 | |
264 TypeHandle Union(TypeHandle t1, TypeHandle t2) { | |
265 return Type::Union(t1, t2, region_); | |
266 } | |
267 TypeHandle Intersect(TypeHandle t1, TypeHandle t2) { | |
268 return Type::Intersect(t1, t2, region_); | |
269 } | |
270 | |
271 template<class Type2, class TypeHandle2> | |
272 TypeHandle Convert(TypeHandle2 t) { | |
273 return Type::template Convert<Type2>(t, region_); | |
274 } | |
275 | |
276 TypeHandle Random() { | |
277 return types[rng_->NextInt(static_cast<int>(types.size()))]; | |
278 } | |
279 | |
280 TypeHandle Fuzz(int depth = 4) { | |
281 switch (rng_->NextInt(depth == 0 ? 3 : 20)) { | |
282 case 0: { // bitset | |
283 #define COUNT_BITSET_TYPES(type, value) + 1 | |
284 int n = 0 PROPER_BITSET_TYPE_LIST(COUNT_BITSET_TYPES); | |
285 #undef COUNT_BITSET_TYPES | |
286 // Pick a bunch of named bitsets and return their intersection. | |
287 TypeHandle result = Type::Any(region_); | |
288 for (int i = 0, m = 1 + rng_->NextInt(3); i < m; ++i) { | |
289 int j = rng_->NextInt(n); | |
290 #define PICK_BITSET_TYPE(type, value) \ | |
291 if (j-- == 0) { \ | |
292 if (!SmiValuesAre31Bits() && \ | |
293 (Type::type(region_)->Equals(Type::OtherSigned32()) || \ | |
294 Type::type(region_)->Equals(Type::OtherUnsigned31()))) { \ | |
295 /* Hack: Avoid generating those empty bitset types. */ \ | |
296 continue; \ | |
297 } \ | |
298 TypeHandle tmp = Type::Intersect( \ | |
299 result, Type::type(region_), region_); \ | |
300 if (tmp->Is(Type::None()) && i != 0) { \ | |
301 break; \ | |
302 } else { \ | |
303 result = tmp; \ | |
304 continue; \ | |
305 } \ | |
306 } | |
307 PROPER_BITSET_TYPE_LIST(PICK_BITSET_TYPE) | |
308 #undef PICK_BITSET_TYPE | |
309 } | |
310 return result; | |
311 } | |
312 case 1: { // class | |
313 int i = rng_->NextInt(static_cast<int>(maps.size())); | |
314 return Type::Class(maps[i], region_); | |
315 } | |
316 case 2: { // constant | |
317 int i = rng_->NextInt(static_cast<int>(values.size())); | |
318 return Type::Constant(values[i], region_); | |
319 } | |
320 case 3: { // range | |
321 int i = rng_->NextInt(static_cast<int>(integers.size())); | |
322 int j = rng_->NextInt(static_cast<int>(integers.size())); | |
323 i::Handle<i::Object> min = integers[i]; | |
324 i::Handle<i::Object> max = integers[j]; | |
325 if (min->Number() > max->Number()) std::swap(min, max); | |
326 return Type::Range(min, max, region_); | |
327 } | |
328 case 4: { // context | |
329 int depth = rng_->NextInt(3); | |
330 TypeHandle type = Type::Internal(region_); | |
331 for (int i = 0; i < depth; ++i) type = Type::Context(type, region_); | |
332 return type; | |
333 } | |
334 case 5: { // array | |
335 TypeHandle element = Fuzz(depth / 2); | |
336 return Type::Array(element, region_); | |
337 } | |
338 case 6: | |
339 case 7: { // function | |
340 TypeHandle result = Fuzz(depth / 2); | |
341 TypeHandle receiver = Fuzz(depth / 2); | |
342 int arity = rng_->NextInt(3); | |
343 TypeHandle type = Type::Function(result, receiver, arity, region_); | |
344 for (int i = 0; i < type->AsFunction()->Arity(); ++i) { | |
345 TypeHandle parameter = Fuzz(depth / 2); | |
346 type->AsFunction()->InitParameter(i, parameter); | |
347 } | |
348 return type; | |
349 } | |
350 default: { // union | |
351 int n = rng_->NextInt(10); | |
352 TypeHandle type = None; | |
353 for (int i = 0; i < n; ++i) { | |
354 TypeHandle operand = Fuzz(depth - 1); | |
355 type = Type::Union(type, operand, region_); | |
356 } | |
357 return type; | |
358 } | |
359 } | |
360 UNREACHABLE(); | |
361 } | |
362 | |
363 Region* region() { return region_; } | |
364 | |
365 private: | |
366 Region* region_; | |
367 v8::base::RandomNumberGenerator* rng_; | |
368 }; | |
369 | |
370 | |
371 template<class Type, class TypeHandle, class Region, class Rep> | 94 template<class Type, class TypeHandle, class Region, class Rep> |
372 struct Tests : Rep { | 95 struct Tests : Rep { |
373 typedef Types<Type, TypeHandle, Region> TypesInstance; | 96 typedef Types<Type, TypeHandle, Region> TypesInstance; |
374 typedef typename TypesInstance::TypeVector::iterator TypeIterator; | 97 typedef typename TypesInstance::TypeVector::iterator TypeIterator; |
375 typedef typename TypesInstance::MapVector::iterator MapIterator; | 98 typedef typename TypesInstance::MapVector::iterator MapIterator; |
376 typedef typename TypesInstance::ValueVector::iterator ValueIterator; | 99 typedef typename TypesInstance::ValueVector::iterator ValueIterator; |
377 | 100 |
378 Isolate* isolate; | 101 Isolate* isolate; |
379 HandleScope scope; | 102 HandleScope scope; |
380 Zone zone; | 103 Zone zone; |
(...skipping 1921 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2302 ZoneTests().Convert<HeapType, Handle<HeapType>, Isolate, HeapRep>(); | 2025 ZoneTests().Convert<HeapType, Handle<HeapType>, Isolate, HeapRep>(); |
2303 HeapTests().Convert<Type, Type*, Zone, ZoneRep>(); | 2026 HeapTests().Convert<Type, Type*, Zone, ZoneRep>(); |
2304 } | 2027 } |
2305 | 2028 |
2306 | 2029 |
2307 TEST(HTypeFromType) { | 2030 TEST(HTypeFromType) { |
2308 CcTest::InitializeVM(); | 2031 CcTest::InitializeVM(); |
2309 ZoneTests().HTypeFromType(); | 2032 ZoneTests().HTypeFromType(); |
2310 HeapTests().HTypeFromType(); | 2033 HeapTests().HTypeFromType(); |
2311 } | 2034 } |
OLD | NEW |