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 = RandomInt(r1); |
| 157 int32_t x2 = 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 |
| 170 |
| 171 TEST(TypeJSAdd) { |
| 172 TyperTester t; |
| 173 t.TestBinaryArithOp(t.javascript_.Subtract(), std::plus<double>()); |
| 174 } |
| 175 |
| 176 |
| 177 TEST(TypeJSSubtract) { |
| 178 TyperTester t; |
| 179 t.TestBinaryArithOp(t.javascript_.Subtract(), std::minus<double>()); |
| 180 } |
| 181 |
| 182 |
| 183 TEST(TypeJSMultiply) { |
| 184 TyperTester t; |
| 185 t.TestBinaryArithOp(t.javascript_.Multiply(), std::multiplies<double>()); |
| 186 } |
| 187 |
| 188 |
| 189 TEST(TypeJSDivide) { |
| 190 TyperTester t; |
| 191 t.TestBinaryArithOp(t.javascript_.Divide(), std::divides<double>()); |
| 192 } |
| 193 |
| 194 |
| 195 TEST(TypeJSBitwiseOr) { |
| 196 TyperTester t; |
| 197 t.TestBinaryBitOp(t.javascript_.BitwiseOr(), std::bit_or<int32_t>()); |
| 198 } |
| 199 |
| 200 |
| 201 TEST(TypeJSBitwiseAnd) { |
| 202 TyperTester t; |
| 203 t.TestBinaryBitOp(t.javascript_.BitwiseAnd(), std::bit_and<int32_t>()); |
| 204 } |
| 205 |
| 206 |
| 207 TEST(TypeJSBitwiseXor) { |
| 208 TyperTester t; |
| 209 t.TestBinaryBitOp(t.javascript_.BitwiseXor(), std::bit_xor<int32_t>()); |
| 210 } |
| 211 |
| 212 |
| 213 TEST(TypeJSShiftLeft) { |
| 214 TyperTester t; |
| 215 t.TestBinaryBitOp(t.javascript_.ShiftLeft(), shift_left); |
| 216 } |
| 217 |
| 218 |
| 219 TEST(TypeJSShiftRight) { |
| 220 TyperTester t; |
| 221 t.TestBinaryBitOp(t.javascript_.ShiftRight(), shift_right); |
| 222 } |
| 223 |
| 224 |
| 225 TEST(TypeJSLessThan) { |
| 226 TyperTester t; |
| 227 t.TestBinaryCompareOp(t.javascript_.LessThan(), std::less<double>()); |
| 228 } |
| 229 |
| 230 |
| 231 TEST(TypeJSLessThanOrEqual) { |
| 232 TyperTester t; |
| 233 t.TestBinaryCompareOp( |
| 234 t.javascript_.LessThanOrEqual(), std::less_equal<double>()); |
| 235 } |
| 236 |
| 237 |
| 238 TEST(TypeJSGreaterThan) { |
| 239 TyperTester t; |
| 240 t.TestBinaryCompareOp(t.javascript_.GreaterThan(), std::greater<double>()); |
| 241 } |
| 242 |
| 243 |
| 244 TEST(TypeJSGreaterThanOrEqual) { |
| 245 TyperTester t; |
| 246 t.TestBinaryCompareOp( |
| 247 t.javascript_.GreaterThanOrEqual(), std::greater_equal<double>()); |
| 248 } |
| 249 |
| 250 |
| 251 TEST(TypeJSEqual) { |
| 252 TyperTester t; |
| 253 t.TestBinaryCompareOp(t.javascript_.Equal(), std::equal_to<double>()); |
| 254 } |
| 255 |
| 256 |
| 257 TEST(TypeJSNotEqual) { |
| 258 TyperTester t; |
| 259 t.TestBinaryCompareOp(t.javascript_.NotEqual(), std::not_equal_to<double>()); |
| 260 } |
| 261 |
| 262 |
| 263 // For numbers there's no difference between strict and non-strict equality. |
| 264 TEST(TypeJSStrictEqual) { |
| 265 TyperTester t; |
| 266 t.TestBinaryCompareOp(t.javascript_.StrictEqual(), std::equal_to<double>()); |
| 267 } |
| 268 |
| 269 |
| 270 TEST(TypeJSStrictNotEqual) { |
| 271 TyperTester t; |
| 272 t.TestBinaryCompareOp( |
| 273 t.javascript_.StrictNotEqual(), std::not_equal_to<double>()); |
| 274 } |
OLD | NEW |