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.
5 // TODO(mythria): Remove this define after this flag is turned on globally
6 #define V8_IMMINENT_DEPRECATION_WARNINGS
13 #include "src/ast-expression-visitor.h"
14 #include "src/parser.h"
15 #include "src/rewriter.h"
16 #include "src/scopes.h"
17 #include "src/typing-reset.h"
18 #include "test/cctest/cctest.h"
19 #include "test/cctest/compiler/function-tester.h"
20 #include "test/cctest/expression-type-collector.h"
21 #include "test/cctest/expression-type-collector-macros.h"
23 #define INT32_TYPE Bounds(Type::Signed32(), Type::Signed32())
25 using namespace v8::internal;
29 class TypeSetter : public AstExpressionVisitor {
31 TypeSetter(Isolate* isolate, Zone* zone, FunctionLiteral* root)
32 : AstExpressionVisitor(isolate, zone, root) {}
35 void VisitExpression(Expression* expression) {
36 expression->set_bounds(INT32_TYPE);
41 void CheckAllSame(ZoneVector<ExpressionTypeEntry>& types,
42 Bounds expected_type) {
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);
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);
61 CHECK_EXPR(Assignment, expected_type) {
62 CHECK_VAR(sum, expected_type);
63 CHECK_EXPR(Literal, expected_type);
65 CHECK_EXPR(Assignment, expected_type) {
66 CHECK_VAR(p, expected_type);
67 CHECK_EXPR(Literal, expected_type);
69 CHECK_EXPR(Assignment, expected_type) {
70 CHECK_VAR(q, expected_type);
71 CHECK_EXPR(Literal, expected_type);
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);
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);
91 CHECK_EXPR(CompareOperation, expected_type) {
92 CHECK_EXPR(BinaryOperation, expected_type) {
93 CHECK_VAR(p, expected_type);
94 CHECK_EXPR(Literal, expected_type);
96 CHECK_EXPR(BinaryOperation, expected_type) {
97 CHECK_VAR(q, expected_type);
98 CHECK_EXPR(Literal, expected_type);
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);
109 CHECK_EXPR(Literal, expected_type);
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_EXPR(Property, expected_type) {
121 CHECK_VAR(values, expected_type);
122 CHECK_EXPR(BinaryOperation, expected_type) {
123 CHECK_VAR(p, expected_type);
124 CHECK_EXPR(Literal, expected_type);
128 CHECK_EXPR(Literal, expected_type);
133 CHECK_EXPR(BinaryOperation, expected_type) {
134 CHECK_VAR(sum, expected_type);
135 CHECK_EXPR(Literal, expected_type);
138 // function geometricMean
139 CHECK_EXPR(FunctionLiteral, expected_type) {
140 CHECK_EXPR(Assignment, expected_type) {
141 CHECK_VAR(start, expected_type);
142 CHECK_EXPR(BinaryOperation, expected_type) {
143 CHECK_VAR(start, expected_type);
144 CHECK_EXPR(Literal, expected_type);
147 CHECK_EXPR(Assignment, expected_type) {
148 CHECK_VAR(end, expected_type);
149 CHECK_EXPR(BinaryOperation, expected_type) {
150 CHECK_VAR(end, expected_type);
151 CHECK_EXPR(Literal, expected_type);
154 // return +exp(+logSum(start, end) / +((end - start)|0));
155 CHECK_EXPR(BinaryOperation, expected_type) {
156 CHECK_EXPR(Call, expected_type) {
157 CHECK_VAR(exp, expected_type);
158 CHECK_EXPR(BinaryOperation, expected_type) {
159 CHECK_EXPR(BinaryOperation, expected_type) {
160 CHECK_EXPR(Call, expected_type) {
161 CHECK_VAR(logSum, expected_type);
162 CHECK_VAR(start, expected_type);
163 CHECK_VAR(end, expected_type);
165 CHECK_EXPR(Literal, expected_type);
167 CHECK_EXPR(BinaryOperation, expected_type) {
168 CHECK_EXPR(BinaryOperation, expected_type) {
169 CHECK_EXPR(BinaryOperation, expected_type) {
170 CHECK_VAR(end, expected_type);
171 CHECK_VAR(start, expected_type);
173 CHECK_EXPR(Literal, expected_type);
175 CHECK_EXPR(Literal, expected_type);
179 CHECK_EXPR(Literal, expected_type);
183 CHECK_EXPR(Literal, expected_type);
184 // var exp = stdlib.Math.exp;
185 CHECK_EXPR(Assignment, expected_type) {
186 CHECK_VAR(exp, expected_type);
187 CHECK_EXPR(Property, expected_type) {
188 CHECK_EXPR(Property, expected_type) {
189 CHECK_VAR(stdlib, expected_type);
190 CHECK_EXPR(Literal, expected_type);
192 CHECK_EXPR(Literal, expected_type);
195 // var log = stdlib.Math.log;
196 CHECK_EXPR(Assignment, expected_type) {
197 CHECK_VAR(log, expected_type);
198 CHECK_EXPR(Property, expected_type) {
199 CHECK_EXPR(Property, expected_type) {
200 CHECK_VAR(stdlib, expected_type);
201 CHECK_EXPR(Literal, expected_type);
203 CHECK_EXPR(Literal, expected_type);
206 // var values = new stdlib.Float64Array(buffer);
207 CHECK_EXPR(Assignment, expected_type) {
208 CHECK_VAR(values, expected_type);
209 CHECK_EXPR(CallNew, expected_type) {
210 CHECK_EXPR(Property, expected_type) {
211 CHECK_VAR(stdlib, expected_type);
212 CHECK_EXPR(Literal, expected_type);
214 CHECK_VAR(buffer, expected_type);
217 // return { geometricMean: geometricMean };
218 CHECK_EXPR(ObjectLiteral, expected_type) {
219 CHECK_VAR(geometricMean, expected_type);
228 TEST(ResetTypingInfo) {
229 const char test_function[] =
230 "function GeometricMean(stdlib, foreign, buffer) {\n"
233 " var exp = stdlib.Math.exp;\n"
234 " var log = stdlib.Math.log;\n"
235 " var values = new stdlib.Float64Array(buffer);\n"
237 " function logSum(start, end) {\n"
238 " start = start|0;\n"
241 " var sum = 0.0, p = 0, q = 0;\n"
243 " // asm.js forces byte addressing of the heap by requiring shifting "
245 " for (p = start << 3, q = end << 3; (p|0) < (q|0); p = (p + 8)|0) {\n"
246 " sum = sum + +log(values[p>>3]);\n"
252 " function geometricMean(start, end) {\n"
253 " start = start|0;\n"
256 " return +exp(+logSum(start, end) / +((end - start)|0));\n"
259 " return { geometricMean: geometricMean };\n"
262 v8::V8::Initialize();
263 HandleAndZoneScope handles;
265 i::Isolate* isolate = CcTest::i_isolate();
266 i::Factory* factory = isolate->factory();
268 i::Handle<i::String> source_code =
269 factory->NewStringFromUtf8(i::CStrVector(test_function))
272 i::Handle<i::Script> script = factory->NewScript(source_code);
274 i::ParseInfo info(handles.main_zone(), script);
275 i::Parser parser(&info);
276 parser.set_allow_harmony_arrow_functions(true);
277 parser.set_allow_harmony_sloppy(true);
279 info.set_lazy(false);
280 info.set_allow_lazy_parsing(false);
281 info.set_toplevel(true);
283 CHECK(i::Compiler::ParseAndAnalyze(&info));
284 FunctionLiteral* root =
285 info.scope()->declarations()->at(0)->AsFunctionDeclaration()->fun();
288 ZoneVector<ExpressionTypeEntry> types(handles.main_zone());
289 ExpressionTypeCollector(isolate, handles.main_zone(), root, &types).Run();
290 CheckAllSame(types, Bounds::Unbounded());
292 TypeSetter(isolate, handles.main_zone(), root).Run();
294 ExpressionTypeCollector(isolate, handles.main_zone(), root, &types).Run();
295 CheckAllSame(types, INT32_TYPE);
297 TypingReseter(isolate, handles.main_zone(), root).Run();
299 ExpressionTypeCollector(isolate, handles.main_zone(), root, &types).Run();
300 CheckAllSame(types, Bounds::Unbounded());