OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 #include <stdlib.h> |
| 6 |
| 7 #include "src/v8.h" |
| 8 |
| 9 #include "src/ast.h" |
| 10 #include "src/ast-expression-visitor.h" |
| 11 #include "src/parser.h" |
| 12 #include "src/rewriter.h" |
| 13 #include "src/scopes.h" |
| 14 #include "src/typing-reset.h" |
| 15 #include "test/cctest/cctest.h" |
| 16 #include "test/cctest/compiler/function-tester.h" |
| 17 #include "test/cctest/expression-type-collector.h" |
| 18 |
| 19 using namespace v8::internal; |
| 20 |
| 21 namespace { |
| 22 |
| 23 class TypeSetter : public AstExpressionVisitor { |
| 24 public: |
| 25 static void Run(CompilationInfo* info) { |
| 26 TypeSetter* visitor = new (info->zone()) TypeSetter(info); |
| 27 visitor->VisitAll(); |
| 28 } |
| 29 |
| 30 protected: |
| 31 void VisitExpression(Expression* expression) { |
| 32 expression->set_bounds(Bounds(Type::Integral32())); |
| 33 } |
| 34 |
| 35 private: |
| 36 explicit TypeSetter(CompilationInfo* info) : AstExpressionVisitor(info) {} |
| 37 }; |
| 38 |
| 39 |
| 40 void CheckAllSame(ZoneVector<ExpressionTypeEntry>& types, |
| 41 Bounds expected_type) { |
| 42 HandleAndZoneScope handles; |
| 43 CHECK_TYPES_BEGIN { |
| 44 // function logSum |
| 45 CHECK_EXPR(FunctionLiteral, expected_type) { |
| 46 CHECK_EXPR(FunctionLiteral, expected_type) { |
| 47 CHECK_EXPR(Assignment, expected_type) { |
| 48 CHECK_VAR(start, expected_type); |
| 49 CHECK_EXPR(BinaryOperation, expected_type) { |
| 50 CHECK_VAR(start, expected_type); |
| 51 CHECK_EXPR(Literal, expected_type); |
| 52 } |
| 53 } |
| 54 CHECK_EXPR(Assignment, expected_type) { |
| 55 CHECK_VAR(end, expected_type); |
| 56 CHECK_EXPR(BinaryOperation, expected_type) { |
| 57 CHECK_VAR(end, expected_type); |
| 58 CHECK_EXPR(Literal, expected_type); |
| 59 } |
| 60 } |
| 61 CHECK_EXPR(Assignment, expected_type) { |
| 62 CHECK_VAR(sum, expected_type); |
| 63 CHECK_EXPR(Literal, expected_type); |
| 64 } |
| 65 CHECK_EXPR(Assignment, expected_type) { |
| 66 CHECK_VAR(p, expected_type); |
| 67 CHECK_EXPR(Literal, expected_type); |
| 68 } |
| 69 CHECK_EXPR(Assignment, expected_type) { |
| 70 CHECK_VAR(q, expected_type); |
| 71 CHECK_EXPR(Literal, expected_type); |
| 72 } |
| 73 // for (p = start << 3, q = end << 3; |
| 74 CHECK_EXPR(BinaryOperation, expected_type) { |
| 75 CHECK_EXPR(Assignment, expected_type) { |
| 76 CHECK_VAR(p, expected_type); |
| 77 CHECK_EXPR(BinaryOperation, expected_type) { |
| 78 CHECK_VAR(start, expected_type); |
| 79 CHECK_EXPR(Literal, expected_type); |
| 80 } |
| 81 } |
| 82 CHECK_EXPR(Assignment, expected_type) { |
| 83 CHECK_VAR(q, expected_type); |
| 84 CHECK_EXPR(BinaryOperation, expected_type) { |
| 85 CHECK_VAR(end, expected_type); |
| 86 CHECK_EXPR(Literal, expected_type); |
| 87 } |
| 88 } |
| 89 } |
| 90 // (p|0) < (q|0); |
| 91 CHECK_EXPR(CompareOperation, expected_type) { |
| 92 CHECK_EXPR(BinaryOperation, expected_type) { |
| 93 CHECK_VAR(p, expected_type); |
| 94 CHECK_EXPR(Literal, expected_type); |
| 95 } |
| 96 CHECK_EXPR(BinaryOperation, expected_type) { |
| 97 CHECK_VAR(q, expected_type); |
| 98 CHECK_EXPR(Literal, expected_type); |
| 99 } |
| 100 } |
| 101 // p = (p + 8)|0) {\n" |
| 102 CHECK_EXPR(Assignment, expected_type) { |
| 103 CHECK_VAR(p, expected_type); |
| 104 CHECK_EXPR(BinaryOperation, expected_type) { |
| 105 CHECK_EXPR(BinaryOperation, expected_type) { |
| 106 CHECK_VAR(p, expected_type); |
| 107 CHECK_EXPR(Literal, expected_type); |
| 108 } |
| 109 CHECK_EXPR(Literal, expected_type); |
| 110 } |
| 111 } |
| 112 // sum = sum + +log(values[p>>3]); |
| 113 CHECK_EXPR(Assignment, expected_type) { |
| 114 CHECK_VAR(sum, expected_type); |
| 115 CHECK_EXPR(BinaryOperation, expected_type) { |
| 116 CHECK_VAR(sum, expected_type); |
| 117 CHECK_EXPR(BinaryOperation, expected_type) { |
| 118 CHECK_EXPR(Call, expected_type) { |
| 119 CHECK_VAR(log, expected_type); |
| 120 CHECK_VAR(values, expected_type); |
| 121 CHECK_EXPR(BinaryOperation, expected_type) { |
| 122 CHECK_VAR(p, expected_type); |
| 123 CHECK_EXPR(Literal, expected_type); |
| 124 } |
| 125 } |
| 126 CHECK_EXPR(Literal, expected_type); |
| 127 } |
| 128 } |
| 129 } |
| 130 // return +sum; |
| 131 CHECK_EXPR(BinaryOperation, expected_type) { |
| 132 CHECK_VAR(sum, expected_type); |
| 133 CHECK_EXPR(Literal, expected_type); |
| 134 } |
| 135 } |
| 136 // function geometricMean |
| 137 CHECK_EXPR(FunctionLiteral, expected_type) { |
| 138 CHECK_EXPR(Assignment, expected_type) { |
| 139 CHECK_VAR(start, expected_type); |
| 140 CHECK_EXPR(BinaryOperation, expected_type) { |
| 141 CHECK_VAR(start, expected_type); |
| 142 CHECK_EXPR(Literal, expected_type); |
| 143 } |
| 144 } |
| 145 CHECK_EXPR(Assignment, expected_type) { |
| 146 CHECK_VAR(end, expected_type); |
| 147 CHECK_EXPR(BinaryOperation, expected_type) { |
| 148 CHECK_VAR(end, expected_type); |
| 149 CHECK_EXPR(Literal, expected_type); |
| 150 } |
| 151 } |
| 152 // return +exp(+logSum(start, end) / +((end - start)|0)); |
| 153 CHECK_EXPR(BinaryOperation, expected_type) { |
| 154 CHECK_EXPR(Call, expected_type) { |
| 155 CHECK_VAR(exp, expected_type); |
| 156 CHECK_EXPR(BinaryOperation, expected_type) { |
| 157 CHECK_EXPR(BinaryOperation, expected_type) { |
| 158 CHECK_EXPR(Call, expected_type) { |
| 159 CHECK_VAR(logSum, expected_type); |
| 160 CHECK_VAR(start, expected_type); |
| 161 CHECK_VAR(end, expected_type); |
| 162 } |
| 163 CHECK_EXPR(Literal, expected_type); |
| 164 } |
| 165 CHECK_EXPR(BinaryOperation, expected_type) { |
| 166 CHECK_EXPR(BinaryOperation, expected_type) { |
| 167 CHECK_EXPR(BinaryOperation, expected_type) { |
| 168 CHECK_VAR(end, expected_type); |
| 169 CHECK_VAR(start, expected_type); |
| 170 } |
| 171 CHECK_EXPR(Literal, expected_type); |
| 172 } |
| 173 CHECK_EXPR(Literal, expected_type); |
| 174 } |
| 175 } |
| 176 } |
| 177 CHECK_EXPR(Literal, expected_type); |
| 178 } |
| 179 } |
| 180 // "use asm"; |
| 181 CHECK_EXPR(Literal, expected_type); |
| 182 // var exp = stdlib.Math.exp; |
| 183 CHECK_EXPR(Assignment, expected_type) { |
| 184 CHECK_VAR(exp, expected_type); |
| 185 CHECK_VAR(stdlib, expected_type); |
| 186 CHECK_EXPR(Literal, expected_type); |
| 187 CHECK_EXPR(Literal, expected_type); |
| 188 } |
| 189 // var log = stdlib.Math.log; |
| 190 CHECK_EXPR(Assignment, expected_type) { |
| 191 CHECK_VAR(log, expected_type); |
| 192 CHECK_VAR(stdlib, expected_type); |
| 193 CHECK_EXPR(Literal, expected_type); |
| 194 CHECK_EXPR(Literal, expected_type); |
| 195 } |
| 196 // var values = new stdlib.Float64Array(buffer); |
| 197 CHECK_EXPR(Assignment, expected_type) { |
| 198 CHECK_VAR(values, expected_type); |
| 199 CHECK_EXPR(CallNew, expected_type) { |
| 200 CHECK_VAR(stdlib, expected_type); |
| 201 CHECK_EXPR(Literal, expected_type); |
| 202 CHECK_VAR(buffer, expected_type); |
| 203 } |
| 204 } |
| 205 // return { geometricMean: geometricMean }; |
| 206 CHECK_EXPR(ObjectLiteral, expected_type) { |
| 207 CHECK_VAR(geometricMean, expected_type); |
| 208 } |
| 209 } |
| 210 } |
| 211 CHECK_TYPES_END |
| 212 } |
| 213 } |
| 214 |
| 215 |
| 216 TEST(ResetTypingInfo) { |
| 217 const char test_function[] = |
| 218 "function GeometricMean(stdlib, foreign, buffer) {\n" |
| 219 " \"use asm\";\n" |
| 220 "\n" |
| 221 " var exp = stdlib.Math.exp;\n" |
| 222 " var log = stdlib.Math.log;\n" |
| 223 " var values = new stdlib.Float64Array(buffer);\n" |
| 224 "\n" |
| 225 " function logSum(start, end) {\n" |
| 226 " start = start|0;\n" |
| 227 " end = end|0;\n" |
| 228 "\n" |
| 229 " var sum = 0.0, p = 0, q = 0;\n" |
| 230 "\n" |
| 231 " // asm.js forces byte addressing of the heap by requiring shifting " |
| 232 "by 3\n" |
| 233 " for (p = start << 3, q = end << 3; (p|0) < (q|0); p = (p + 8)|0) {\n" |
| 234 " sum = sum + +log(values[p>>3]);\n" |
| 235 " }\n" |
| 236 "\n" |
| 237 " return +sum;\n" |
| 238 " }\n" |
| 239 "\n" |
| 240 " function geometricMean(start, end) {\n" |
| 241 " start = start|0;\n" |
| 242 " end = end|0;\n" |
| 243 "\n" |
| 244 " return +exp(+logSum(start, end) / +((end - start)|0));\n" |
| 245 " }\n" |
| 246 "\n" |
| 247 " return { geometricMean: geometricMean };\n" |
| 248 "}\n"; |
| 249 |
| 250 v8::V8::Initialize(); |
| 251 HandleAndZoneScope handles; |
| 252 |
| 253 i::Isolate* isolate = CcTest::i_isolate(); |
| 254 i::Factory* factory = isolate->factory(); |
| 255 |
| 256 i::Handle<i::String> source_code = |
| 257 factory->NewStringFromUtf8(i::CStrVector(test_function)) |
| 258 .ToHandleChecked(); |
| 259 |
| 260 i::Handle<i::Script> script = factory->NewScript(source_code); |
| 261 |
| 262 i::ParseInfo info(handles.main_zone(), script); |
| 263 i::Parser parser(&info); |
| 264 parser.set_allow_harmony_arrow_functions(true); |
| 265 parser.set_allow_harmony_sloppy(true); |
| 266 info.set_global(); |
| 267 info.set_lazy(false); |
| 268 info.set_allow_lazy_parsing(false); |
| 269 info.set_toplevel(true); |
| 270 |
| 271 i::CompilationInfo compilation_info(&info); |
| 272 CHECK(i::Compiler::ParseAndAnalyze(&info)); |
| 273 info.set_literal( |
| 274 info.scope()->declarations()->at(0)->AsFunctionDeclaration()->fun()); |
| 275 |
| 276 // Core of the test. |
| 277 ZoneVector<ExpressionTypeEntry> types(handles.main_zone()); |
| 278 ExpressionTypeCollector::Run(&compilation_info, &types); |
| 279 CheckAllSame(types, DEFAULT_TYPE); |
| 280 |
| 281 TypeSetter::Run(&compilation_info); |
| 282 |
| 283 ExpressionTypeCollector::Run(&compilation_info, &types); |
| 284 CheckAllSame(types, INT32_TYPE); |
| 285 |
| 286 TypingReseter::Run(&compilation_info); |
| 287 |
| 288 ExpressionTypeCollector::Run(&compilation_info, &types); |
| 289 CheckAllSame(types, DEFAULT_TYPE); |
| 290 } |
OLD | NEW |