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 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #include <list> | 28 #include <vector> |
29 | 29 |
30 #include "cctest.h" | 30 #include "cctest.h" |
31 #include "types.h" | 31 #include "types.h" |
32 | 32 |
33 using namespace v8::internal; | 33 using namespace v8::internal; |
34 | 34 |
35 template<class Type, class TypeHandle, class Region> | 35 template<class Type, class TypeHandle, class Region> |
36 class Types { | 36 class Types { |
37 public: | 37 public: |
38 Types(Region* region, Isolate* isolate) : | 38 Types(Region* region, Isolate* isolate) : |
(...skipping 28 matching lines...) Expand all Loading... | |
67 array = isolate->factory()->NewJSArray(20); | 67 array = isolate->factory()->NewJSArray(20); |
68 ObjectClass = Type::Class(object_map, region); | 68 ObjectClass = Type::Class(object_map, region); |
69 ArrayClass = Type::Class(array_map, region); | 69 ArrayClass = Type::Class(array_map, region); |
70 SmiConstant = Type::Constant(smi, region); | 70 SmiConstant = Type::Constant(smi, region); |
71 Signed32Constant = Type::Constant(signed32, region); | 71 Signed32Constant = Type::Constant(signed32, region); |
72 ObjectConstant1 = Type::Constant(object1, region); | 72 ObjectConstant1 = Type::Constant(object1, region); |
73 ObjectConstant2 = Type::Constant(object2, region); | 73 ObjectConstant2 = Type::Constant(object2, region); |
74 ArrayConstant1 = Type::Constant(array, region); | 74 ArrayConstant1 = Type::Constant(array, region); |
75 ArrayConstant2 = Type::Constant(array, region); | 75 ArrayConstant2 = Type::Constant(array, region); |
76 | 76 |
77 types.push_back(None); | |
78 types.push_back(Any); | |
79 types.push_back(Boolean); | |
80 types.push_back(Null); | |
81 types.push_back(Undefined); | |
82 types.push_back(Number); | |
83 types.push_back(SignedSmall); | |
84 types.push_back(Signed32); | |
85 types.push_back(Float); | |
86 types.push_back(Name); | |
87 types.push_back(UniqueName); | |
88 types.push_back(String); | |
89 types.push_back(InternalizedString); | |
90 types.push_back(Symbol); | |
91 types.push_back(Receiver); | |
92 types.push_back(Object); | |
93 types.push_back(Array); | |
94 types.push_back(Function); | |
95 types.push_back(Proxy); | |
96 types.push_back(ObjectClass); | |
97 types.push_back(ArrayClass); | |
98 types.push_back(SmiConstant); | |
99 types.push_back(Signed32Constant); | |
100 types.push_back(ObjectConstant1); | |
101 types.push_back(ObjectConstant2); | |
102 types.push_back(ArrayConstant1); | |
103 types.push_back(ArrayConstant2); | |
104 for (int i = 0; i < 300; ++i) { | |
105 types.push_back(Fuzz()); | |
106 } | |
107 | |
108 objects.push_back(smi); | 77 objects.push_back(smi); |
109 objects.push_back(signed32); | 78 objects.push_back(signed32); |
110 objects.push_back(object1); | 79 objects.push_back(object1); |
111 objects.push_back(object2); | 80 objects.push_back(object2); |
112 objects.push_back(array); | 81 objects.push_back(array); |
82 | |
83 static const size_t kMaxTypes = 300; | |
84 types.reserve(kMaxTypes); | |
85 #define PUSH_BACK_BITSET_TYPE(type, value) \ | |
86 types.push_back(Type::type(region)); | |
87 BITSET_TYPE_LIST(PUSH_BACK_BITSET_TYPE) | |
88 #undef PUSH_BACK_BITSET_TYPE | |
89 types.push_back(ObjectClass); | |
90 types.push_back(ArrayClass); | |
91 for (ObjectVector::iterator it = objects.begin(); it != objects.end(); ++it) | |
92 types.push_back(Type::Constant(*it, region)); | |
93 while (types.size() < kMaxTypes) { | |
94 size_t i = rand() % types.size(); | |
rossberg
2014/04/09 13:59:10
I thought we shouldn't use rand... :)
Benedikt Meurer
2014/04/10 05:19:16
Another CL...
| |
95 size_t j = rand() % types.size(); | |
96 if (i != j) types.push_back(Type::Union(types[i], types[j], region)); | |
97 } | |
113 } | 98 } |
114 | 99 |
115 TypeHandle Representation; | 100 TypeHandle Representation; |
116 TypeHandle Semantic; | 101 TypeHandle Semantic; |
117 TypeHandle None; | 102 TypeHandle None; |
118 TypeHandle Any; | 103 TypeHandle Any; |
119 TypeHandle Boolean; | 104 TypeHandle Boolean; |
120 TypeHandle Null; | 105 TypeHandle Null; |
121 TypeHandle Undefined; | 106 TypeHandle Undefined; |
122 TypeHandle Number; | 107 TypeHandle Number; |
(...skipping 23 matching lines...) Expand all Loading... | |
146 | 131 |
147 Handle<i::Map> object_map; | 132 Handle<i::Map> object_map; |
148 Handle<i::Map> array_map; | 133 Handle<i::Map> array_map; |
149 | 134 |
150 Handle<i::Smi> smi; | 135 Handle<i::Smi> smi; |
151 Handle<i::HeapNumber> signed32; | 136 Handle<i::HeapNumber> signed32; |
152 Handle<i::JSObject> object1; | 137 Handle<i::JSObject> object1; |
153 Handle<i::JSObject> object2; | 138 Handle<i::JSObject> object2; |
154 Handle<i::JSArray> array; | 139 Handle<i::JSArray> array; |
155 | 140 |
156 typedef std::list<TypeHandle> TypeList; | 141 typedef std::vector<TypeHandle> TypeVector; |
157 TypeList types; | 142 TypeVector types; |
158 | 143 |
159 typedef std::list<Handle<i::Object> > ObjectList; | 144 typedef std::vector<Handle<i::Object> > ObjectVector; |
160 ObjectList objects; | 145 ObjectVector objects; |
161 | 146 |
162 TypeHandle Of(Handle<i::Object> obj) { | 147 TypeHandle Of(Handle<i::Object> obj) { |
163 return Type::Of(obj, region_); | 148 return Type::Of(obj, region_); |
164 } | 149 } |
165 | 150 |
166 TypeHandle Constant(Handle<i::Object> obj) { | 151 TypeHandle Constant(Handle<i::Object> obj) { |
167 return Type::Constant(obj, region_); | 152 return Type::Constant(obj, region_); |
168 } | 153 } |
169 | 154 |
170 TypeHandle Union(TypeHandle t1, TypeHandle t2) { | 155 TypeHandle Union(TypeHandle t1, TypeHandle t2) { |
171 return Type::Union(t1, t2, region_); | 156 return Type::Union(t1, t2, region_); |
172 } | 157 } |
173 TypeHandle Intersect(TypeHandle t1, TypeHandle t2) { | 158 TypeHandle Intersect(TypeHandle t1, TypeHandle t2) { |
174 return Type::Intersect(t1, t2, region_); | 159 return Type::Intersect(t1, t2, region_); |
175 } | 160 } |
176 | 161 |
177 template<class Type2, class TypeHandle2> | 162 template<class Type2, class TypeHandle2> |
178 TypeHandle Convert(TypeHandle2 t) { | 163 TypeHandle Convert(TypeHandle2 t) { |
179 return Type::template Convert<Type2>(t, region_); | 164 return Type::template Convert<Type2>(t, region_); |
180 } | 165 } |
181 | 166 |
182 TypeHandle Fuzz(int depth = 5) { | |
183 switch (rand() % (depth == 0 ? 3 : 20)) { | |
184 case 0: { // bitset | |
185 int n = 0 | |
186 #define COUNT_BITSET_TYPES(type, value) + 1 | |
187 BITSET_TYPE_LIST(COUNT_BITSET_TYPES) | |
188 #undef COUNT_BITSET_TYPES | |
189 ; | |
190 int i = rand() % n; | |
191 #define PICK_BITSET_TYPE(type, value) \ | |
192 if (i-- == 0) return Type::type(region_); | |
193 BITSET_TYPE_LIST(PICK_BITSET_TYPE) | |
194 #undef PICK_BITSET_TYPE | |
195 UNREACHABLE(); | |
196 } | |
197 case 1: // class | |
198 switch (rand() % 2) { | |
199 case 0: return ObjectClass; | |
200 case 1: return ArrayClass; | |
201 } | |
202 UNREACHABLE(); | |
203 case 2: // constant | |
204 switch (rand() % 6) { | |
205 case 0: return SmiConstant; | |
206 case 1: return Signed32Constant; | |
207 case 2: return ObjectConstant1; | |
208 case 3: return ObjectConstant2; | |
209 case 4: return ArrayConstant1; | |
210 case 5: return ArrayConstant2; | |
211 } | |
212 UNREACHABLE(); | |
213 default: { // union | |
214 int n = rand() % 10; | |
215 TypeHandle type = None; | |
216 for (int i = 0; i < n; ++i) { | |
217 type = Type::Union(type, Fuzz(depth - 1), region_); | |
218 } | |
219 return type; | |
220 } | |
221 } | |
222 UNREACHABLE(); | |
223 } | |
224 | |
225 private: | 167 private: |
226 Region* region_; | 168 Region* region_; |
227 }; | 169 }; |
228 | 170 |
229 | 171 |
230 // Testing auxiliaries (breaking the Type abstraction). | 172 // Testing auxiliaries (breaking the Type abstraction). |
231 struct ZoneRep { | 173 struct ZoneRep { |
232 typedef void* Struct; | 174 typedef void* Struct; |
233 | 175 |
234 static bool IsStruct(Type* t, int tag) { | 176 static bool IsStruct(Type* t, int tag) { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
285 static Isolate* ToRegion(Zone* zone, Isolate* isolate) { return isolate; } | 227 static Isolate* ToRegion(Zone* zone, Isolate* isolate) { return isolate; } |
286 }; | 228 }; |
287 | 229 |
288 | 230 |
289 template<class Type, class TypeHandle, class Region, class Rep> | 231 template<class Type, class TypeHandle, class Region, class Rep> |
290 struct Tests : Rep { | 232 struct Tests : Rep { |
291 Isolate* isolate; | 233 Isolate* isolate; |
292 HandleScope scope; | 234 HandleScope scope; |
293 Zone zone; | 235 Zone zone; |
294 Types<Type, TypeHandle, Region> T; | 236 Types<Type, TypeHandle, Region> T; |
295 typedef typename Types<Type, TypeHandle, Region>::TypeList::iterator | 237 typedef typename Types<Type, TypeHandle, Region>::TypeVector::iterator |
296 TypeIterator; | 238 TypeIterator; |
297 typedef typename Types<Type, TypeHandle, Region>::ObjectList::iterator | 239 typedef typename Types<Type, TypeHandle, Region>::ObjectVector::iterator |
298 ObjectIterator; | 240 ObjectIterator; |
299 | 241 |
300 Tests() : | 242 Tests() : |
301 isolate(CcTest::i_isolate()), | 243 isolate(CcTest::i_isolate()), |
302 scope(isolate), | 244 scope(isolate), |
303 zone(isolate), | 245 zone(isolate), |
304 T(Rep::ToRegion(&zone, isolate), isolate) { | 246 T(Rep::ToRegion(&zone, isolate), isolate) { |
305 } | 247 } |
306 | 248 |
307 void CheckEqual(TypeHandle type1, TypeHandle type2) { | 249 void CheckEqual(TypeHandle type1, TypeHandle type2) { |
(...skipping 709 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1017 T.Intersect( | 959 T.Intersect( |
1018 T.Union(T.ObjectConstant2, T.ArrayConstant1), | 960 T.Union(T.ObjectConstant2, T.ArrayConstant1), |
1019 T.Union(T.ObjectConstant1, T.ArrayConstant2)), | 961 T.Union(T.ObjectConstant1, T.ArrayConstant2)), |
1020 T.ArrayConstant1); | 962 T.ArrayConstant1); |
1021 } | 963 } |
1022 | 964 |
1023 template<class Type2, class TypeHandle2, class Region2, class Rep2> | 965 template<class Type2, class TypeHandle2, class Region2, class Rep2> |
1024 void Convert() { | 966 void Convert() { |
1025 Types<Type2, TypeHandle2, Region2> T2( | 967 Types<Type2, TypeHandle2, Region2> T2( |
1026 Rep2::ToRegion(&zone, isolate), isolate); | 968 Rep2::ToRegion(&zone, isolate), isolate); |
1027 for (int i = 0; i < 100; ++i) { | 969 for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) { |
1028 TypeHandle type = T.Fuzz(); | 970 TypeHandle type = *it; |
1029 CheckEqual(type, | 971 CheckEqual(type, |
1030 T.template Convert<Type2>(T2.template Convert<Type>(type))); | 972 T.template Convert<Type2>(T2.template Convert<Type>(type))); |
1031 } | 973 } |
1032 } | 974 } |
1033 }; | 975 }; |
1034 | 976 |
1035 typedef Tests<Type, Type*, Zone, ZoneRep> ZoneTests; | 977 typedef Tests<Type, Type*, Zone, ZoneRep> ZoneTests; |
1036 typedef Tests<HeapType, Handle<HeapType>, Isolate, HeapRep> HeapTests; | 978 typedef Tests<HeapType, Handle<HeapType>, Isolate, HeapRep> HeapTests; |
1037 | 979 |
1038 | 980 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1097 ZoneTests().Intersect(); | 1039 ZoneTests().Intersect(); |
1098 HeapTests().Intersect(); | 1040 HeapTests().Intersect(); |
1099 } | 1041 } |
1100 | 1042 |
1101 | 1043 |
1102 TEST(Convert) { | 1044 TEST(Convert) { |
1103 CcTest::InitializeVM(); | 1045 CcTest::InitializeVM(); |
1104 ZoneTests().Convert<HeapType, Handle<HeapType>, Isolate, HeapRep>(); | 1046 ZoneTests().Convert<HeapType, Handle<HeapType>, Isolate, HeapRep>(); |
1105 HeapTests().Convert<Type, Type*, Zone, ZoneRep>(); | 1047 HeapTests().Convert<Type, Type*, Zone, ZoneRep>(); |
1106 } | 1048 } |
OLD | NEW |