blob: 4eac64c838b409ce0c96b1383d28dc8867c09a33 [file] [log] [blame]
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001// 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
33namespace v8 {
34namespace internal {
35
36
37template<class Type, class TypeHandle, class Region>
38class 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 types.push_back(name);
45 PROPER_BITSET_TYPE_LIST(DECLARE_TYPE)
46 #undef DECLARE_TYPE
47
48 object_map = isolate->factory()->NewMap(
49 JS_OBJECT_TYPE, JSObject::kHeaderSize);
50 array_map = isolate->factory()->NewMap(
51 JS_ARRAY_TYPE, JSArray::kSize);
52 number_map = isolate->factory()->NewMap(
53 HEAP_NUMBER_TYPE, HeapNumber::kSize);
54 uninitialized_map = isolate->factory()->uninitialized_map();
55 ObjectClass = Type::Class(object_map, region);
56 ArrayClass = Type::Class(array_map, region);
57 NumberClass = Type::Class(number_map, region);
58 UninitializedClass = Type::Class(uninitialized_map, region);
59
60 maps.push_back(object_map);
61 maps.push_back(array_map);
62 maps.push_back(uninitialized_map);
63 for (MapVector::iterator it = maps.begin(); it != maps.end(); ++it) {
64 types.push_back(Type::Class(*it, region));
65 }
66
67 smi = handle(Smi::FromInt(666), isolate);
68 signed32 = isolate->factory()->NewHeapNumber(0x40000000);
69 object1 = isolate->factory()->NewJSObjectFromMap(object_map);
70 object2 = isolate->factory()->NewJSObjectFromMap(object_map);
71 array = isolate->factory()->NewJSArray(20);
72 uninitialized = isolate->factory()->uninitialized_value();
73 SmiConstant = Type::Constant(smi, region);
74 Signed32Constant = Type::Constant(signed32, region);
75 ObjectConstant1 = Type::Constant(object1, region);
76 ObjectConstant2 = Type::Constant(object2, region);
77 ArrayConstant = Type::Constant(array, region);
78 UninitializedConstant = Type::Constant(uninitialized, region);
79
80 values.push_back(smi);
81 values.push_back(signed32);
82 values.push_back(object1);
83 values.push_back(object2);
84 values.push_back(array);
85 values.push_back(uninitialized);
86 for (ValueVector::iterator it = values.begin(); it != values.end(); ++it) {
87 types.push_back(Type::Constant(*it, region));
88 }
89
90 integers.push_back(isolate->factory()->NewNumber(-V8_INFINITY));
91 integers.push_back(isolate->factory()->NewNumber(+V8_INFINITY));
92 integers.push_back(isolate->factory()->NewNumber(-rng_->NextInt(10)));
93 integers.push_back(isolate->factory()->NewNumber(+rng_->NextInt(10)));
94 for (int i = 0; i < 10; ++i) {
95 double x = rng_->NextInt();
96 integers.push_back(isolate->factory()->NewNumber(x));
97 x *= rng_->NextInt();
98 if (!IsMinusZero(x)) integers.push_back(isolate->factory()->NewNumber(x));
99 }
100
101 Integer = Type::Range(isolate->factory()->NewNumber(-V8_INFINITY),
102 isolate->factory()->NewNumber(+V8_INFINITY), region);
103
104 NumberArray = Type::Array(Number, region);
105 StringArray = Type::Array(String, region);
106 AnyArray = Type::Array(Any, region);
107
108 SignedFunction1 = Type::Function(SignedSmall, SignedSmall, region);
109 NumberFunction1 = Type::Function(Number, Number, region);
110 NumberFunction2 = Type::Function(Number, Number, Number, region);
111 MethodFunction = Type::Function(String, Object, 0, region);
112
113 for (int i = 0; i < 30; ++i) {
114 types.push_back(Fuzz());
115 }
116 }
117
118 Handle<i::Map> object_map;
119 Handle<i::Map> array_map;
120 Handle<i::Map> number_map;
121 Handle<i::Map> uninitialized_map;
122
123 Handle<i::Smi> smi;
124 Handle<i::HeapNumber> signed32;
125 Handle<i::JSObject> object1;
126 Handle<i::JSObject> object2;
127 Handle<i::JSArray> array;
128 Handle<i::Oddball> uninitialized;
129
130 #define DECLARE_TYPE(name, value) TypeHandle name;
131 PROPER_BITSET_TYPE_LIST(DECLARE_TYPE)
132 #undef DECLARE_TYPE
133
134 TypeHandle ObjectClass;
135 TypeHandle ArrayClass;
136 TypeHandle NumberClass;
137 TypeHandle UninitializedClass;
138
139 TypeHandle SmiConstant;
140 TypeHandle Signed32Constant;
141 TypeHandle ObjectConstant1;
142 TypeHandle ObjectConstant2;
143 TypeHandle ArrayConstant;
144 TypeHandle UninitializedConstant;
145
146 TypeHandle Integer;
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 TypeHandle tmp = Type::Intersect( \
237 result, Type::type(region_), region_); \
238 if (tmp->Is(Type::None()) && i != 0) { \
239 break; \
240 } else { \
241 result = tmp; \
242 continue; \
243 } \
244 }
245 PROPER_BITSET_TYPE_LIST(PICK_BITSET_TYPE)
246 #undef PICK_BITSET_TYPE
247 }
248 return result;
249 }
250 case 1: { // class
251 int i = rng_->NextInt(static_cast<int>(maps.size()));
252 return Type::Class(maps[i], region_);
253 }
254 case 2: { // constant
255 int i = rng_->NextInt(static_cast<int>(values.size()));
256 return Type::Constant(values[i], region_);
257 }
258 case 3: { // range
259 int i = rng_->NextInt(static_cast<int>(integers.size()));
260 int j = rng_->NextInt(static_cast<int>(integers.size()));
261 i::Handle<i::Object> min = integers[i];
262 i::Handle<i::Object> max = integers[j];
263 if (min->Number() > max->Number()) std::swap(min, max);
264 return Type::Range(min, max, region_);
265 }
266 case 4: { // context
267 int depth = rng_->NextInt(3);
268 TypeHandle type = Type::Internal(region_);
269 for (int i = 0; i < depth; ++i) type = Type::Context(type, region_);
270 return type;
271 }
272 case 5: { // array
273 TypeHandle element = Fuzz(depth / 2);
274 return Type::Array(element, region_);
275 }
276 case 6:
277 case 7: { // function
278 TypeHandle result = Fuzz(depth / 2);
279 TypeHandle receiver = Fuzz(depth / 2);
280 int arity = rng_->NextInt(3);
281 TypeHandle type = Type::Function(result, receiver, arity, region_);
282 for (int i = 0; i < type->AsFunction()->Arity(); ++i) {
283 TypeHandle parameter = Fuzz(depth / 2);
284 type->AsFunction()->InitParameter(i, parameter);
285 }
286 return type;
287 }
288 default: { // union
289 int n = rng_->NextInt(10);
290 TypeHandle type = None;
291 for (int i = 0; i < n; ++i) {
292 TypeHandle operand = Fuzz(depth - 1);
293 type = Type::Union(type, operand, region_);
294 }
295 return type;
296 }
297 }
298 UNREACHABLE();
299 }
300
301 Region* region() { return region_; }
302
303 private:
304 Region* region_;
305 v8::base::RandomNumberGenerator* rng_;
306};
307
308
309} } // namespace v8::internal
310
311#endif