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 static void TypeCheckAndReset(const char* source, const char* expected_typed, |
| 40 const char* expected_reset) { |
| 41 v8::V8::Initialize(); |
| 42 HandleAndZoneScope handles; |
| 43 |
| 44 i::Isolate* isolate = CcTest::i_isolate(); |
| 45 i::Factory* factory = isolate->factory(); |
| 46 |
| 47 i::Handle<i::String> source_code = |
| 48 factory->NewStringFromUtf8(i::CStrVector(source)).ToHandleChecked(); |
| 49 |
| 50 i::Handle<i::Script> script = factory->NewScript(source_code); |
| 51 |
| 52 i::ParseInfo info(handles.main_zone(), script); |
| 53 i::Parser parser(&info); |
| 54 parser.set_allow_harmony_arrow_functions(true); |
| 55 parser.set_allow_harmony_sloppy(true); |
| 56 info.set_global(); |
| 57 info.set_lazy(false); |
| 58 info.set_allow_lazy_parsing(false); |
| 59 info.set_toplevel(true); |
| 60 |
| 61 i::CompilationInfo compilation_info(&info); |
| 62 CHECK(i::Compiler::ParseAndAnalyze(&info)); |
| 63 info.set_literal( |
| 64 info.scope()->declarations()->at(0)->AsFunctionDeclaration()->fun()); |
| 65 |
| 66 // Core of the test. |
| 67 CHECK_EQ(expected_reset, ExpressionTypeCollector::Run(&compilation_info)); |
| 68 TypeSetter::Run(&compilation_info); |
| 69 CHECK_EQ(expected_typed, ExpressionTypeCollector::Run(&compilation_info)); |
| 70 TypingReseter::Run(&compilation_info); |
| 71 CHECK_EQ(expected_reset, ExpressionTypeCollector::Run(&compilation_info)); |
| 72 } |
| 73 |
| 74 std::string ReplaceAll(const std::string& s, const std::string& from, |
| 75 const std::string& to) { |
| 76 std::string ret = s; |
| 77 size_t i = 0; |
| 78 for (;;) { |
| 79 i = ret.find(from, i); |
| 80 if (i == std::string::npos) break; |
| 81 ret.replace(i, from.length(), to); |
| 82 i += to.length(); |
| 83 } |
| 84 return ret; |
| 85 } |
| 86 } |
| 87 |
| 88 |
| 89 TEST(ResetTypingInfo) { |
| 90 const char test_function[] = |
| 91 "function GeometricMean(stdlib, foreign, buffer) {\n" |
| 92 " \"use asm\";\n" |
| 93 "\n" |
| 94 " var exp = stdlib.Math.exp;\n" |
| 95 " var log = stdlib.Math.log;\n" |
| 96 " var values = new stdlib.Float64Array(buffer);\n" |
| 97 "\n" |
| 98 " function logSum(start, end) {\n" |
| 99 " start = start|0;\n" |
| 100 " end = end|0;\n" |
| 101 "\n" |
| 102 " var sum = 0.0, p = 0, q = 0;\n" |
| 103 "\n" |
| 104 " // asm.js forces byte addressing of the heap by requiring shifting " |
| 105 "by 3\n" |
| 106 " for (p = start << 3, q = end << 3; (p|0) < (q|0); p = (p + 8)|0) {\n" |
| 107 " sum = sum + +log(values[p>>3]);\n" |
| 108 " }\n" |
| 109 "\n" |
| 110 " return +sum;\n" |
| 111 " }\n" |
| 112 "\n" |
| 113 " function geometricMean(start, end) {\n" |
| 114 " start = start|0;\n" |
| 115 " end = end|0;\n" |
| 116 "\n" |
| 117 " return +exp(+logSum(start, end) / +((end - start)|0));\n" |
| 118 " }\n" |
| 119 "\n" |
| 120 " return { geometricMean: geometricMean };\n" |
| 121 "}\n"; |
| 122 const char reset_expected[] = |
| 123 "FunctionLiteral: Other\n" |
| 124 " FunctionLiteral: Other\n" |
| 125 " Assignment: Other\n" |
| 126 " start: VariableProxy: Other\n" |
| 127 " BinaryOperation: Other\n" |
| 128 " start: VariableProxy: Other\n" |
| 129 " Literal: Other\n" |
| 130 " Assignment: Other\n" |
| 131 " end: VariableProxy: Other\n" |
| 132 " BinaryOperation: Other\n" |
| 133 " end: VariableProxy: Other\n" |
| 134 " Literal: Other\n" |
| 135 " Assignment: Other\n" |
| 136 " sum: VariableProxy: Other\n" |
| 137 " Literal: Other\n" |
| 138 " Assignment: Other\n" |
| 139 " p: VariableProxy: Other\n" |
| 140 " Literal: Other\n" |
| 141 " Assignment: Other\n" |
| 142 " q: VariableProxy: Other\n" |
| 143 " Literal: Other\n" |
| 144 " BinaryOperation: Other\n" |
| 145 " Assignment: Other\n" |
| 146 " p: VariableProxy: Other\n" |
| 147 " BinaryOperation: Other\n" |
| 148 " start: VariableProxy: Other\n" |
| 149 " Literal: Other\n" |
| 150 " Assignment: Other\n" |
| 151 " q: VariableProxy: Other\n" |
| 152 " BinaryOperation: Other\n" |
| 153 " end: VariableProxy: Other\n" |
| 154 " Literal: Other\n" |
| 155 " CompareOperation: Other\n" |
| 156 " BinaryOperation: Other\n" |
| 157 " p: VariableProxy: Other\n" |
| 158 " Literal: Other\n" |
| 159 " BinaryOperation: Other\n" |
| 160 " q: VariableProxy: Other\n" |
| 161 " Literal: Other\n" |
| 162 " Assignment: Other\n" |
| 163 " p: VariableProxy: Other\n" |
| 164 " BinaryOperation: Other\n" |
| 165 " BinaryOperation: Other\n" |
| 166 " p: VariableProxy: Other\n" |
| 167 " Literal: Other\n" |
| 168 " Literal: Other\n" |
| 169 " Assignment: Other\n" |
| 170 " sum: VariableProxy: Other\n" |
| 171 " BinaryOperation: Other\n" |
| 172 " sum: VariableProxy: Other\n" |
| 173 " BinaryOperation: Other\n" |
| 174 " Call: Other\n" |
| 175 " log: VariableProxy: Other\n" |
| 176 " values: VariableProxy: Other\n" |
| 177 " BinaryOperation: Other\n" |
| 178 " p: VariableProxy: Other\n" |
| 179 " Literal: Other\n" |
| 180 " Literal: Other\n" |
| 181 " BinaryOperation: Other\n" |
| 182 " sum: VariableProxy: Other\n" |
| 183 " Literal: Other\n" |
| 184 " FunctionLiteral: Other\n" |
| 185 " Assignment: Other\n" |
| 186 " start: VariableProxy: Other\n" |
| 187 " BinaryOperation: Other\n" |
| 188 " start: VariableProxy: Other\n" |
| 189 " Literal: Other\n" |
| 190 " Assignment: Other\n" |
| 191 " end: VariableProxy: Other\n" |
| 192 " BinaryOperation: Other\n" |
| 193 " end: VariableProxy: Other\n" |
| 194 " Literal: Other\n" |
| 195 " BinaryOperation: Other\n" |
| 196 " Call: Other\n" |
| 197 " exp: VariableProxy: Other\n" |
| 198 " BinaryOperation: Other\n" |
| 199 " BinaryOperation: Other\n" |
| 200 " Call: Other\n" |
| 201 " logSum: VariableProxy: Other\n" |
| 202 " start: VariableProxy: Other\n" |
| 203 " end: VariableProxy: Other\n" |
| 204 " Literal: Other\n" |
| 205 " BinaryOperation: Other\n" |
| 206 " BinaryOperation: Other\n" |
| 207 " BinaryOperation: Other\n" |
| 208 " end: VariableProxy: Other\n" |
| 209 " start: VariableProxy: Other\n" |
| 210 " Literal: Other\n" |
| 211 " Literal: Other\n" |
| 212 " Literal: Other\n" |
| 213 " Literal: Other\n" |
| 214 " Assignment: Other\n" |
| 215 " exp: VariableProxy: Other\n" |
| 216 " stdlib: VariableProxy: Other\n" |
| 217 " Literal: Other\n" |
| 218 " Literal: Other\n" |
| 219 " Assignment: Other\n" |
| 220 " log: VariableProxy: Other\n" |
| 221 " stdlib: VariableProxy: Other\n" |
| 222 " Literal: Other\n" |
| 223 " Literal: Other\n" |
| 224 " Assignment: Other\n" |
| 225 " values: VariableProxy: Other\n" |
| 226 " CallNew: Other\n" |
| 227 " stdlib: VariableProxy: Other\n" |
| 228 " Literal: Other\n" |
| 229 " buffer: VariableProxy: Other\n" |
| 230 " ObjectLiteral: Other\n" |
| 231 " geometricMean: VariableProxy: Other\n"; |
| 232 std::string typed_expected = |
| 233 ReplaceAll(reset_expected, "Other", "Integral32"); |
| 234 TypeCheckAndReset(test_function, typed_expected.c_str(), reset_expected); |
| 235 } |
OLD | NEW |