deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / test / cctest / compiler / test-loop-assignment-analysis.cc
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 #include "src/compiler/ast-loop-assignment-analyzer.h"
6 #include "src/parser.h"
7 #include "src/rewriter.h"
8 #include "src/scopes.h"
9 #include "test/cctest/cctest.h"
10
11 using namespace v8::internal;
12 using namespace v8::internal::compiler;
13
14 namespace {
15 const int kBufferSize = 1024;
16
17 struct TestHelper : public HandleAndZoneScope {
18   Handle<JSFunction> function;
19   LoopAssignmentAnalysis* result;
20
21   explicit TestHelper(const char* body)
22       : function(Handle<JSFunction>::null()), result(NULL) {
23     ScopedVector<char> program(kBufferSize);
24     SNPrintF(program, "function f(a,b,c) { %s; } f;", body);
25     v8::Local<v8::Value> v = CompileRun(program.start());
26     Handle<Object> obj = v8::Utils::OpenHandle(*v);
27     function = Handle<JSFunction>::cast(obj);
28   }
29
30   void CheckLoopAssignedCount(int expected, const char* var_name) {
31     // TODO(titzer): don't scope analyze every single time.
32     ParseInfo parse_info(main_zone(), function);
33     CompilationInfo info(&parse_info);
34
35     CHECK(Parser::ParseStatic(&parse_info));
36     CHECK(Rewriter::Rewrite(&parse_info));
37     CHECK(Scope::Analyze(&parse_info));
38
39     Scope* scope = info.function()->scope();
40     AstValueFactory* factory = parse_info.ast_value_factory();
41     CHECK(scope);
42
43     if (result == NULL) {
44       AstLoopAssignmentAnalyzer analyzer(main_zone(), &info);
45       result = analyzer.Analyze();
46       CHECK(result);
47     }
48
49     const i::AstRawString* name = factory->GetOneByteString(var_name);
50
51     i::Variable* var = scope->Lookup(name);
52     CHECK(var);
53
54     if (var->location() == Variable::UNALLOCATED) {
55       CHECK_EQ(0, expected);
56     } else {
57       CHECK(var->IsStackAllocated());
58       CHECK_EQ(expected, result->GetAssignmentCountForTesting(scope, var));
59     }
60   }
61 };
62 }
63
64
65 TEST(SimpleLoop1) {
66   TestHelper f("var x = 0; while (x) ;");
67
68   f.CheckLoopAssignedCount(0, "x");
69 }
70
71
72 TEST(SimpleLoop2) {
73   const char* loops[] = {
74       "while (x) { var x = 0; }",            "for(;;) { var x = 0; }",
75       "for(;x;) { var x = 0; }",             "for(;x;x) { var x = 0; }",
76       "for(var i = x; x; x) { var x = 0; }", "for(y in 0) { var x = 0; }",
77       "for(y of 0) { var x = 0; }",          "for(var x = 0; x; x++) { }",
78       "for(var x = 0; x++;) { }",            "var x; for(;x;x++) { }",
79       "var x; do { x = 1; } while (0);",     "do { var x = 1; } while (0);"};
80
81   for (size_t i = 0; i < arraysize(loops); i++) {
82     TestHelper f(loops[i]);
83     f.CheckLoopAssignedCount(1, "x");
84   }
85 }
86
87
88 TEST(ForInOf1) {
89   const char* loops[] = {
90       "for(x in 0) { }", "for(x of 0) { }",
91   };
92
93   for (size_t i = 0; i < arraysize(loops); i++) {
94     TestHelper f(loops[i]);
95     f.CheckLoopAssignedCount(0, "x");
96   }
97 }
98
99
100 TEST(Param1) {
101   TestHelper f("while (1) a = 0;");
102
103   f.CheckLoopAssignedCount(1, "a");
104   f.CheckLoopAssignedCount(0, "b");
105   f.CheckLoopAssignedCount(0, "c");
106 }
107
108
109 TEST(Param2) {
110   TestHelper f("for (;;) b = 0;");
111
112   f.CheckLoopAssignedCount(0, "a");
113   f.CheckLoopAssignedCount(1, "b");
114   f.CheckLoopAssignedCount(0, "c");
115 }
116
117
118 TEST(Param2b) {
119   TestHelper f("a; b; c; for (;;) b = 0;");
120
121   f.CheckLoopAssignedCount(0, "a");
122   f.CheckLoopAssignedCount(1, "b");
123   f.CheckLoopAssignedCount(0, "c");
124 }
125
126
127 TEST(Param3) {
128   TestHelper f("for(x in 0) c = 0;");
129
130   f.CheckLoopAssignedCount(0, "a");
131   f.CheckLoopAssignedCount(0, "b");
132   f.CheckLoopAssignedCount(1, "c");
133 }
134
135
136 TEST(Param3b) {
137   TestHelper f("a; b; c; for(x in 0) c = 0;");
138
139   f.CheckLoopAssignedCount(0, "a");
140   f.CheckLoopAssignedCount(0, "b");
141   f.CheckLoopAssignedCount(1, "c");
142 }
143
144
145 TEST(NestedLoop1) {
146   TestHelper f("while (x) { while (x) { var x = 0; } }");
147
148   f.CheckLoopAssignedCount(2, "x");
149 }
150
151
152 TEST(NestedLoop2) {
153   TestHelper f("while (0) { while (0) { var x = 0; } }");
154
155   f.CheckLoopAssignedCount(2, "x");
156 }
157
158
159 TEST(NestedLoop3) {
160   TestHelper f("while (0) { var y = 1; while (0) { var x = 0; } }");
161
162   f.CheckLoopAssignedCount(2, "x");
163   f.CheckLoopAssignedCount(1, "y");
164 }
165
166
167 TEST(NestedInc1) {
168   const char* loops[] = {
169       "while (1) a(b++);",
170       "while (1) a(0, b++);",
171       "while (1) a(0, 0, b++);",
172       "while (1) a(b++, 1, 1);",
173       "while (1) a(++b);",
174       "while (1) a + (b++);",
175       "while (1) (b++) + a;",
176       "while (1) a + c(b++);",
177       "while (1) throw b++;",
178       "while (1) switch (b++) {} ;",
179       "while (1) switch (a) {case (b++): 0; } ;",
180       "while (1) switch (a) {case b: b++; } ;",
181       "while (1) a == (b++);",
182       "while (1) a === (b++);",
183       "while (1) +(b++);",
184       "while (1) ~(b++);",
185       "while (1) new a(b++);",
186       "while (1) (b++).f;",
187       "while (1) a[b++];",
188       "while (1) (b++)();",
189       "while (1) [b++];",
190       "while (1) [0,b++];",
191       "while (1) var y = [11,b++,12];",
192       "while (1) var y = {f:11,g:(b++),h:12};",
193       "while (1) try {b++;} finally {};",
194       "while (1) try {} finally {b++};",
195       "while (1) try {b++;} catch (e) {};",
196       "while (1) try {} catch (e) {b++};",
197       "while (1) return b++;",
198       "while (1) (b++) ? b : b;",
199       "while (1) b ? (b++) : b;",
200       "while (1) b ? b : (b++);",
201   };
202
203   for (size_t i = 0; i < arraysize(loops); i++) {
204     TestHelper f(loops[i]);
205     f.CheckLoopAssignedCount(1, "b");
206   }
207 }
208
209
210 TEST(NestedAssign1) {
211   const char* loops[] = {
212       "while (1) a(b=1);",
213       "while (1) a(0, b=1);",
214       "while (1) a(0, 0, b=1);",
215       "while (1) a(b=1, 1, 1);",
216       "while (1) a + (b=1);",
217       "while (1) (b=1) + a;",
218       "while (1) a + c(b=1);",
219       "while (1) throw b=1;",
220       "while (1) switch (b=1) {} ;",
221       "while (1) switch (a) {case b=1: 0; } ;",
222       "while (1) switch (a) {case b: b=1; } ;",
223       "while (1) a == (b=1);",
224       "while (1) a === (b=1);",
225       "while (1) +(b=1);",
226       "while (1) ~(b=1);",
227       "while (1) new a(b=1);",
228       "while (1) (b=1).f;",
229       "while (1) a[b=1];",
230       "while (1) (b=1)();",
231       "while (1) [b=1];",
232       "while (1) [0,b=1];",
233       "while (1) var z = [11,b=1,12];",
234       "while (1) var y = {f:11,g:(b=1),h:12};",
235       "while (1) try {b=1;} finally {};",
236       "while (1) try {} finally {b=1};",
237       "while (1) try {b=1;} catch (e) {};",
238       "while (1) try {} catch (e) {b=1};",
239       "while (1) return b=1;",
240       "while (1) (b=1) ? b : b;",
241       "while (1) b ? (b=1) : b;",
242       "while (1) b ? b : (b=1);",
243   };
244
245   for (size_t i = 0; i < arraysize(loops); i++) {
246     TestHelper f(loops[i]);
247     f.CheckLoopAssignedCount(1, "b");
248   }
249 }
250
251
252 TEST(NestedLoops3) {
253   TestHelper f("var x, y, z, w; while (x++) while (y++) while (z++) ; w;");
254
255   f.CheckLoopAssignedCount(1, "x");
256   f.CheckLoopAssignedCount(2, "y");
257   f.CheckLoopAssignedCount(3, "z");
258   f.CheckLoopAssignedCount(0, "w");
259 }
260
261
262 TEST(NestedLoops3b) {
263   TestHelper f(
264       "var x, y, z, w;"
265       "while (1) { x=1; while (1) { y=1; while (1) z=1; } }"
266       "w;");
267
268   f.CheckLoopAssignedCount(1, "x");
269   f.CheckLoopAssignedCount(2, "y");
270   f.CheckLoopAssignedCount(3, "z");
271   f.CheckLoopAssignedCount(0, "w");
272 }
273
274
275 TEST(NestedLoops3c) {
276   TestHelper f(
277       "var x, y, z, w;"
278       "while (1) {"
279       "  x++;"
280       "  while (1) {"
281       "    y++;"
282       "    while (1) z++;"
283       "  }"
284       "  while (1) {"
285       "    y++;"
286       "    while (1) z++;"
287       "  }"
288       "}"
289       "w;");
290
291   f.CheckLoopAssignedCount(1, "x");
292   f.CheckLoopAssignedCount(3, "y");
293   f.CheckLoopAssignedCount(5, "z");
294   f.CheckLoopAssignedCount(0, "w");
295 }