OLD | NEW |
| (Empty) |
1 // Copyright 2014 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 | |
6 // This tests the correctness of the typer. | |
7 // | |
8 // For simplicity, it currently only tests it on expression operators that have | |
9 // a direct equivalent in C++. Also, testing is currently limited to ranges as | |
10 // input types. | |
11 | |
12 | |
13 #include <functional> | |
14 | |
15 #include "src/compiler/node-properties-inl.h" | |
16 #include "src/compiler/typer.h" | |
17 #include "test/cctest/cctest.h" | |
18 #include "test/cctest/compiler/graph-builder-tester.h" | |
19 | |
20 using namespace v8::internal; | |
21 using namespace v8::internal::compiler; | |
22 | |
23 | |
24 | |
25 class TyperTester : public HandleAndZoneScope, public GraphAndBuilders { | |
26 public: | |
27 TyperTester() | |
28 : GraphAndBuilders(main_zone()), | |
29 typer_(main_zone()), | |
30 javascript_(main_zone()) { | |
31 Node* s = graph()->NewNode(common()->Start(3)); | |
32 graph()->SetStart(s); | |
33 context_node_ = graph()->NewNode(common()->Parameter(2), graph()->start()); | |
34 rng_ = isolate()->random_number_generator(); | |
35 | |
36 integers.push_back(0); | |
37 integers.push_back(0); | |
38 integers.push_back(-1); | |
39 integers.push_back(+1); | |
40 integers.push_back(-V8_INFINITY); | |
41 integers.push_back(+V8_INFINITY); | |
42 for (int i = 0; i < 5; ++i) { | |
43 double x = rng_->NextInt(); | |
44 integers.push_back(x); | |
45 x *= rng_->NextInt(); | |
46 if (!IsMinusZero(x)) integers.push_back(x); | |
47 } | |
48 | |
49 int32s.push_back(0); | |
50 int32s.push_back(0); | |
51 int32s.push_back(-1); | |
52 int32s.push_back(+1); | |
53 int32s.push_back(kMinInt); | |
54 int32s.push_back(kMaxInt); | |
55 for (int i = 0; i < 10; ++i) { | |
56 int32s.push_back(rng_->NextInt()); | |
57 } | |
58 } | |
59 | |
60 Typer typer_; | |
61 JSOperatorBuilder javascript_; | |
62 Node* context_node_; | |
63 v8::base::RandomNumberGenerator* rng_; | |
64 std::vector<double> integers; | |
65 std::vector<double> int32s; | |
66 | |
67 Isolate* isolate() { return main_isolate(); } | |
68 Graph* graph() { return main_graph_; } | |
69 CommonOperatorBuilder* common() { return &main_common_; } | |
70 | |
71 Node* Parameter(int index = 0) { | |
72 return graph()->NewNode(common()->Parameter(index), graph()->start()); | |
73 } | |
74 | |
75 Type* TypeBinaryOp(const Operator* op, Type* lhs, Type* rhs) { | |
76 Node* p0 = Parameter(0); | |
77 Node* p1 = Parameter(1); | |
78 NodeProperties::SetBounds(p0, Bounds(lhs)); | |
79 NodeProperties::SetBounds(p1, Bounds(rhs)); | |
80 Node* n = graph()->NewNode( | |
81 op, p0, p1, context_node_, graph()->start(), graph()->start()); | |
82 typer_.Init(n); | |
83 return NodeProperties::GetBounds(n).upper; | |
84 } | |
85 | |
86 Type* RandomRange(bool int32 = false) { | |
87 std::vector<double>& numbers = int32 ? int32s : integers; | |
88 Factory* f = isolate()->factory(); | |
89 int i = rng_->NextInt(static_cast<int>(numbers.size())); | |
90 int j = rng_->NextInt(static_cast<int>(numbers.size())); | |
91 i::Handle<i::Object> min = f->NewNumber(numbers[i]); | |
92 i::Handle<i::Object> max = f->NewNumber(numbers[j]); | |
93 if (min->Number() > max->Number()) std::swap(min, max); | |
94 return Type::Range(min, max, main_zone()); | |
95 } | |
96 | |
97 double RandomInt(double min, double max) { | |
98 switch (rng_->NextInt(4)) { | |
99 case 0: return min; | |
100 case 1: return max; | |
101 default: break; | |
102 } | |
103 if (min == +V8_INFINITY) return +V8_INFINITY; | |
104 if (max == -V8_INFINITY) return -V8_INFINITY; | |
105 if (min == -V8_INFINITY && max == +V8_INFINITY) { | |
106 return rng_->NextInt() * static_cast<double>(rng_->NextInt()); | |
107 } | |
108 double result = nearbyint(min + (max - min) * rng_->NextDouble()); | |
109 if (IsMinusZero(result)) return 0; | |
110 if (std::isnan(result)) return rng_->NextInt(2) ? min : max; | |
111 DCHECK(min <= result && result <= max); | |
112 return result; | |
113 } | |
114 | |
115 double RandomInt(Type::RangeType* range) { | |
116 return RandomInt(range->Min()->Number(), range->Max()->Number()); | |
117 } | |
118 | |
119 template <class BinaryFunction> | |
120 void TestBinaryArithOp(const Operator* op, BinaryFunction opfun) { | |
121 for (int i = 0; i < 100; ++i) { | |
122 Type::RangeType* r1 = RandomRange()->AsRange(); | |
123 Type::RangeType* r2 = RandomRange()->AsRange(); | |
124 Type* expected_type = TypeBinaryOp(op, r1, r2); | |
125 double x1 = RandomInt(r1); | |
126 double x2 = RandomInt(r2); | |
127 double result_value = opfun(x1, x2); | |
128 Type* result_type = Type::Constant( | |
129 isolate()->factory()->NewNumber(result_value), main_zone()); | |
130 CHECK(result_type->Is(expected_type)); | |
131 } | |
132 } | |
133 | |
134 template <class BinaryFunction> | |
135 void TestBinaryCompareOp(const Operator* op, BinaryFunction opfun) { | |
136 for (int i = 0; i < 100; ++i) { | |
137 Type::RangeType* r1 = RandomRange()->AsRange(); | |
138 Type::RangeType* r2 = RandomRange()->AsRange(); | |
139 Type* expected_type = TypeBinaryOp(op, r1, r2); | |
140 double x1 = RandomInt(r1); | |
141 double x2 = RandomInt(r2); | |
142 bool result_value = opfun(x1, x2); | |
143 Type* result_type = Type::Constant(result_value ? | |
144 isolate()->factory()->true_value() : | |
145 isolate()->factory()->false_value(), main_zone()); | |
146 CHECK(result_type->Is(expected_type)); | |
147 } | |
148 } | |
149 | |
150 template <class BinaryFunction> | |
151 void TestBinaryBitOp(const Operator* op, BinaryFunction opfun) { | |
152 for (int i = 0; i < 100; ++i) { | |
153 Type::RangeType* r1 = RandomRange(true)->AsRange(); | |
154 Type::RangeType* r2 = RandomRange(true)->AsRange(); | |
155 Type* expected_type = TypeBinaryOp(op, r1, r2); | |
156 int32_t x1 = static_cast<int32_t>(RandomInt(r1)); | |
157 int32_t x2 = static_cast<int32_t>(RandomInt(r2)); | |
158 double result_value = opfun(x1, x2); | |
159 Type* result_type = Type::Constant( | |
160 isolate()->factory()->NewNumber(result_value), main_zone()); | |
161 CHECK(result_type->Is(expected_type)); | |
162 } | |
163 } | |
164 }; | |
165 | |
166 | |
167 static int32_t shift_left(int32_t x, int32_t y) { return x << y; } | |
168 static int32_t shift_right(int32_t x, int32_t y) { return x >> y; } | |
169 static int32_t bit_or(int32_t x, int32_t y) { return x | y; } | |
170 static int32_t bit_and(int32_t x, int32_t y) { return x & y; } | |
171 static int32_t bit_xor(int32_t x, int32_t y) { return x ^ y; } | |
172 | |
173 | |
174 TEST(TypeJSAdd) { | |
175 TyperTester t; | |
176 t.TestBinaryArithOp(t.javascript_.Subtract(), std::plus<double>()); | |
177 } | |
178 | |
179 | |
180 TEST(TypeJSSubtract) { | |
181 TyperTester t; | |
182 t.TestBinaryArithOp(t.javascript_.Subtract(), std::minus<double>()); | |
183 } | |
184 | |
185 | |
186 TEST(TypeJSMultiply) { | |
187 TyperTester t; | |
188 t.TestBinaryArithOp(t.javascript_.Multiply(), std::multiplies<double>()); | |
189 } | |
190 | |
191 | |
192 TEST(TypeJSDivide) { | |
193 TyperTester t; | |
194 t.TestBinaryArithOp(t.javascript_.Divide(), std::divides<double>()); | |
195 } | |
196 | |
197 | |
198 TEST(TypeJSBitwiseOr) { | |
199 TyperTester t; | |
200 t.TestBinaryBitOp(t.javascript_.BitwiseOr(), bit_or); | |
201 } | |
202 | |
203 | |
204 TEST(TypeJSBitwiseAnd) { | |
205 TyperTester t; | |
206 t.TestBinaryBitOp(t.javascript_.BitwiseAnd(), bit_and); | |
207 } | |
208 | |
209 | |
210 TEST(TypeJSBitwiseXor) { | |
211 TyperTester t; | |
212 t.TestBinaryBitOp(t.javascript_.BitwiseXor(), bit_xor); | |
213 } | |
214 | |
215 | |
216 TEST(TypeJSShiftLeft) { | |
217 TyperTester t; | |
218 t.TestBinaryBitOp(t.javascript_.ShiftLeft(), shift_left); | |
219 } | |
220 | |
221 | |
222 TEST(TypeJSShiftRight) { | |
223 TyperTester t; | |
224 t.TestBinaryBitOp(t.javascript_.ShiftRight(), shift_right); | |
225 } | |
226 | |
227 | |
228 TEST(TypeJSLessThan) { | |
229 TyperTester t; | |
230 t.TestBinaryCompareOp(t.javascript_.LessThan(), std::less<double>()); | |
231 } | |
232 | |
233 | |
234 TEST(TypeJSLessThanOrEqual) { | |
235 TyperTester t; | |
236 t.TestBinaryCompareOp( | |
237 t.javascript_.LessThanOrEqual(), std::less_equal<double>()); | |
238 } | |
239 | |
240 | |
241 TEST(TypeJSGreaterThan) { | |
242 TyperTester t; | |
243 t.TestBinaryCompareOp(t.javascript_.GreaterThan(), std::greater<double>()); | |
244 } | |
245 | |
246 | |
247 TEST(TypeJSGreaterThanOrEqual) { | |
248 TyperTester t; | |
249 t.TestBinaryCompareOp( | |
250 t.javascript_.GreaterThanOrEqual(), std::greater_equal<double>()); | |
251 } | |
252 | |
253 | |
254 TEST(TypeJSEqual) { | |
255 TyperTester t; | |
256 t.TestBinaryCompareOp(t.javascript_.Equal(), std::equal_to<double>()); | |
257 } | |
258 | |
259 | |
260 TEST(TypeJSNotEqual) { | |
261 TyperTester t; | |
262 t.TestBinaryCompareOp(t.javascript_.NotEqual(), std::not_equal_to<double>()); | |
263 } | |
264 | |
265 | |
266 // For numbers there's no difference between strict and non-strict equality. | |
267 TEST(TypeJSStrictEqual) { | |
268 TyperTester t; | |
269 t.TestBinaryCompareOp(t.javascript_.StrictEqual(), std::equal_to<double>()); | |
270 } | |
271 | |
272 | |
273 TEST(TypeJSStrictNotEqual) { | |
274 TyperTester t; | |
275 t.TestBinaryCompareOp( | |
276 t.javascript_.StrictNotEqual(), std::not_equal_to<double>()); | |
277 } | |
OLD | NEW |