| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 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 | |
| 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> | 5 #include <functional> |
| 14 | 6 |
| 15 #include "src/compiler/node-properties-inl.h" | 7 #include "src/compiler/node-properties-inl.h" |
| 16 #include "src/compiler/typer.h" | 8 #include "src/compiler/typer.h" |
| 17 #include "test/cctest/cctest.h" | 9 #include "test/cctest/cctest.h" |
| 18 #include "test/cctest/compiler/graph-builder-tester.h" | 10 #include "test/cctest/compiler/graph-builder-tester.h" |
| 11 #include "test/cctest/test-types.h" |
| 19 | 12 |
| 20 using namespace v8::internal; | 13 using namespace v8::internal; |
| 21 using namespace v8::internal::compiler; | 14 using namespace v8::internal::compiler; |
| 22 | 15 |
| 23 | 16 |
| 24 | 17 |
| 25 class TyperTester : public HandleAndZoneScope, public GraphAndBuilders { | 18 class TyperTester : public HandleAndZoneScope, public GraphAndBuilders { |
| 26 public: | 19 public: |
| 27 TyperTester() | 20 TyperTester() |
| 28 : GraphAndBuilders(main_zone()), | 21 : GraphAndBuilders(main_zone()), |
| 22 types_(main_zone(), isolate()), |
| 29 typer_(main_zone()), | 23 typer_(main_zone()), |
| 30 javascript_(main_zone()) { | 24 javascript_(main_zone()) { |
| 31 Node* s = graph()->NewNode(common()->Start(3)); | 25 Node* s = graph()->NewNode(common()->Start(3)); |
| 32 graph()->SetStart(s); | 26 graph()->SetStart(s); |
| 33 context_node_ = graph()->NewNode(common()->Parameter(2), graph()->start()); | 27 context_node_ = graph()->NewNode(common()->Parameter(2), graph()->start()); |
| 34 rng_ = isolate()->random_number_generator(); | 28 rng_ = isolate()->random_number_generator(); |
| 35 | 29 |
| 36 integers.push_back(0); | 30 integers.push_back(0); |
| 37 integers.push_back(0); | 31 integers.push_back(0); |
| 38 integers.push_back(-1); | 32 integers.push_back(-1); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 50 int32s.push_back(0); | 44 int32s.push_back(0); |
| 51 int32s.push_back(-1); | 45 int32s.push_back(-1); |
| 52 int32s.push_back(+1); | 46 int32s.push_back(+1); |
| 53 int32s.push_back(kMinInt); | 47 int32s.push_back(kMinInt); |
| 54 int32s.push_back(kMaxInt); | 48 int32s.push_back(kMaxInt); |
| 55 for (int i = 0; i < 10; ++i) { | 49 for (int i = 0; i < 10; ++i) { |
| 56 int32s.push_back(rng_->NextInt()); | 50 int32s.push_back(rng_->NextInt()); |
| 57 } | 51 } |
| 58 } | 52 } |
| 59 | 53 |
| 54 Types<Type, Type*, Zone> types_; |
| 60 Typer typer_; | 55 Typer typer_; |
| 61 JSOperatorBuilder javascript_; | 56 JSOperatorBuilder javascript_; |
| 62 Node* context_node_; | 57 Node* context_node_; |
| 63 v8::base::RandomNumberGenerator* rng_; | 58 v8::base::RandomNumberGenerator* rng_; |
| 64 std::vector<double> integers; | 59 std::vector<double> integers; |
| 65 std::vector<double> int32s; | 60 std::vector<double> int32s; |
| 66 | 61 |
| 67 Isolate* isolate() { return main_isolate(); } | 62 Isolate* isolate() { return main_isolate(); } |
| 68 Graph* graph() { return main_graph_; } | 63 Graph* graph() { return main_graph_; } |
| 69 CommonOperatorBuilder* common() { return &main_common_; } | 64 CommonOperatorBuilder* common() { return &main_common_; } |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 Type::RangeType* r2 = RandomRange(true)->AsRange(); | 149 Type::RangeType* r2 = RandomRange(true)->AsRange(); |
| 155 Type* expected_type = TypeBinaryOp(op, r1, r2); | 150 Type* expected_type = TypeBinaryOp(op, r1, r2); |
| 156 int32_t x1 = static_cast<int32_t>(RandomInt(r1)); | 151 int32_t x1 = static_cast<int32_t>(RandomInt(r1)); |
| 157 int32_t x2 = static_cast<int32_t>(RandomInt(r2)); | 152 int32_t x2 = static_cast<int32_t>(RandomInt(r2)); |
| 158 double result_value = opfun(x1, x2); | 153 double result_value = opfun(x1, x2); |
| 159 Type* result_type = Type::Constant( | 154 Type* result_type = Type::Constant( |
| 160 isolate()->factory()->NewNumber(result_value), main_zone()); | 155 isolate()->factory()->NewNumber(result_value), main_zone()); |
| 161 CHECK(result_type->Is(expected_type)); | 156 CHECK(result_type->Is(expected_type)); |
| 162 } | 157 } |
| 163 } | 158 } |
| 159 |
| 160 Type* RandomSubtype(Type* type) { |
| 161 Type* subtype; |
| 162 do { |
| 163 subtype = types_.Fuzz(); |
| 164 } while (!subtype->Is(type)); |
| 165 return subtype; |
| 166 } |
| 167 |
| 168 void TestBinaryMonotonicity(const Operator* op) { |
| 169 for (int i = 0; i < 50; ++i) { |
| 170 Type* type1 = types_.Fuzz(); |
| 171 Type* type2 = types_.Fuzz(); |
| 172 Type* type = TypeBinaryOp(op, type1, type2); |
| 173 Type* subtype1 = RandomSubtype(type1);; |
| 174 Type* subtype2 = RandomSubtype(type2);; |
| 175 Type* subtype = TypeBinaryOp(op, subtype1, subtype2); |
| 176 CHECK(subtype->Is(type)); |
| 177 } |
| 178 } |
| 164 }; | 179 }; |
| 165 | 180 |
| 166 | 181 |
| 167 static int32_t shift_left(int32_t x, int32_t y) { return x << y; } | 182 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; } | 183 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; } | 184 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; } | 185 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; } | 186 static int32_t bit_xor(int32_t x, int32_t y) { return x ^ y; } |
| 172 | 187 |
| 173 | 188 |
| 189 //------------------------------------------------------------------------------ |
| 190 // Soundness |
| 191 // For simplicity, we currently only test soundness on expression operators |
| 192 // that have a direct equivalent in C++. Also, testing is currently limited |
| 193 // to ranges as input types. |
| 194 |
| 195 |
| 174 TEST(TypeJSAdd) { | 196 TEST(TypeJSAdd) { |
| 175 TyperTester t; | 197 TyperTester t; |
| 176 t.TestBinaryArithOp(t.javascript_.Subtract(), std::plus<double>()); | 198 t.TestBinaryArithOp(t.javascript_.Subtract(), std::plus<double>()); |
| 177 } | 199 } |
| 178 | 200 |
| 179 | 201 |
| 180 TEST(TypeJSSubtract) { | 202 TEST(TypeJSSubtract) { |
| 181 TyperTester t; | 203 TyperTester t; |
| 182 t.TestBinaryArithOp(t.javascript_.Subtract(), std::minus<double>()); | 204 t.TestBinaryArithOp(t.javascript_.Subtract(), std::minus<double>()); |
| 183 } | 205 } |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 TyperTester t; | 290 TyperTester t; |
| 269 t.TestBinaryCompareOp(t.javascript_.StrictEqual(), std::equal_to<double>()); | 291 t.TestBinaryCompareOp(t.javascript_.StrictEqual(), std::equal_to<double>()); |
| 270 } | 292 } |
| 271 | 293 |
| 272 | 294 |
| 273 TEST(TypeJSStrictNotEqual) { | 295 TEST(TypeJSStrictNotEqual) { |
| 274 TyperTester t; | 296 TyperTester t; |
| 275 t.TestBinaryCompareOp( | 297 t.TestBinaryCompareOp( |
| 276 t.javascript_.StrictNotEqual(), std::not_equal_to<double>()); | 298 t.javascript_.StrictNotEqual(), std::not_equal_to<double>()); |
| 277 } | 299 } |
| 300 |
| 301 |
| 302 //------------------------------------------------------------------------------ |
| 303 // Monotonicity |
| 304 |
| 305 |
| 306 TEST(Monotonicity) { |
| 307 TyperTester t; |
| 308 #define TEST_TYPE(name, x, y, z) \ |
| 309 t.TestBinaryMonotonicity(t.javascript_.name()); |
| 310 SHARED_SIMPLE_BINOP_LIST(TEST_TYPE) |
| 311 #undef TEST_TYPE |
| 312 } |
| OLD | NEW |