[presubmit] Enable readability/namespace linter checking.
[platform/upstream/v8.git] / test / cctest / test-typing-reset.cc
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 // TODO(mythria): Remove this define after this flag is turned on globally
6 #define V8_IMMINENT_DEPRECATION_WARNINGS
7
8 #include <stdlib.h>
9
10 #include "src/v8.h"
11
12 #include "src/ast.h"
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"
22
23 #define INT32_TYPE Bounds(Type::Signed32(), Type::Signed32())
24
25 using namespace v8::internal;
26
27 namespace {
28
29 class TypeSetter : public AstExpressionVisitor {
30  public:
31   TypeSetter(Isolate* isolate, Zone* zone, FunctionLiteral* root)
32       : AstExpressionVisitor(isolate, zone, root) {}
33
34  protected:
35   void VisitExpression(Expression* expression) {
36     expression->set_bounds(INT32_TYPE);
37   }
38 };
39
40
41 void CheckAllSame(ZoneVector<ExpressionTypeEntry>& types,
42                   Bounds expected_type) {
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_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);
125                   }
126                 }
127               }
128               CHECK_EXPR(Literal, expected_type);
129             }
130           }
131         }
132         // return +sum;
133         CHECK_EXPR(BinaryOperation, expected_type) {
134           CHECK_VAR(sum, expected_type);
135           CHECK_EXPR(Literal, expected_type);
136         }
137       }
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);
145           }
146         }
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);
152           }
153         }
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);
164                 }
165                 CHECK_EXPR(Literal, expected_type);
166               }
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);
172                   }
173                   CHECK_EXPR(Literal, expected_type);
174                 }
175                 CHECK_EXPR(Literal, expected_type);
176               }
177             }
178           }
179           CHECK_EXPR(Literal, expected_type);
180         }
181       }
182       // "use asm";
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);
191           }
192           CHECK_EXPR(Literal, expected_type);
193         }
194       }
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);
202           }
203           CHECK_EXPR(Literal, expected_type);
204         }
205       }
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);
213           }
214           CHECK_VAR(buffer, expected_type);
215         }
216       }
217       // return { geometricMean: geometricMean };
218       CHECK_EXPR(ObjectLiteral, expected_type) {
219         CHECK_VAR(geometricMean, expected_type);
220       }
221     }
222   }
223   CHECK_TYPES_END
224 }
225
226 }  // namespace
227
228
229 TEST(ResetTypingInfo) {
230   const char test_function[] =
231       "function GeometricMean(stdlib, foreign, buffer) {\n"
232       "  \"use asm\";\n"
233       "\n"
234       "  var exp = stdlib.Math.exp;\n"
235       "  var log = stdlib.Math.log;\n"
236       "  var values = new stdlib.Float64Array(buffer);\n"
237       "\n"
238       "  function logSum(start, end) {\n"
239       "    start = start|0;\n"
240       "    end = end|0;\n"
241       "\n"
242       "    var sum = 0.0, p = 0, q = 0;\n"
243       "\n"
244       "    // asm.js forces byte addressing of the heap by requiring shifting "
245       "by 3\n"
246       "    for (p = start << 3, q = end << 3; (p|0) < (q|0); p = (p + 8)|0) {\n"
247       "      sum = sum + +log(values[p>>3]);\n"
248       "    }\n"
249       "\n"
250       "    return +sum;\n"
251       "  }\n"
252       "\n"
253       " function geometricMean(start, end) {\n"
254       "    start = start|0;\n"
255       "    end = end|0;\n"
256       "\n"
257       "    return +exp(+logSum(start, end) / +((end - start)|0));\n"
258       "  }\n"
259       "\n"
260       "  return { geometricMean: geometricMean };\n"
261       "}\n";
262
263   v8::V8::Initialize();
264   HandleAndZoneScope handles;
265
266   i::Isolate* isolate = CcTest::i_isolate();
267   i::Factory* factory = isolate->factory();
268
269   i::Handle<i::String> source_code =
270       factory->NewStringFromUtf8(i::CStrVector(test_function))
271           .ToHandleChecked();
272
273   i::Handle<i::Script> script = factory->NewScript(source_code);
274
275   i::ParseInfo info(handles.main_zone(), script);
276   i::Parser parser(&info);
277   parser.set_allow_harmony_arrow_functions(true);
278   parser.set_allow_harmony_sloppy(true);
279   info.set_global();
280   info.set_lazy(false);
281   info.set_allow_lazy_parsing(false);
282   info.set_toplevel(true);
283
284   CHECK(i::Compiler::ParseAndAnalyze(&info));
285   FunctionLiteral* root =
286       info.scope()->declarations()->at(0)->AsFunctionDeclaration()->fun();
287
288   // Core of the test.
289   ZoneVector<ExpressionTypeEntry> types(handles.main_zone());
290   ExpressionTypeCollector(isolate, handles.main_zone(), root, &types).Run();
291   CheckAllSame(types, Bounds::Unbounded());
292
293   TypeSetter(isolate, handles.main_zone(), root).Run();
294
295   ExpressionTypeCollector(isolate, handles.main_zone(), root, &types).Run();
296   CheckAllSame(types, INT32_TYPE);
297
298   TypingReseter(isolate, handles.main_zone(), root).Run();
299
300   ExpressionTypeCollector(isolate, handles.main_zone(), root, &types).Run();
301   CheckAllSame(types, Bounds::Unbounded());
302 }