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.
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"
11 using namespace v8::internal;
12 using namespace v8::internal::compiler;
15 const int kBufferSize = 1024;
17 struct TestHelper : public HandleAndZoneScope {
18 Handle<JSFunction> function;
19 LoopAssignmentAnalysis* result;
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);
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);
35 CHECK(Parser::ParseStatic(&parse_info));
36 CHECK(Rewriter::Rewrite(&parse_info));
37 CHECK(Scope::Analyze(&parse_info));
39 Scope* scope = info.function()->scope();
40 AstValueFactory* factory = parse_info.ast_value_factory();
44 AstLoopAssignmentAnalyzer analyzer(main_zone(), &info);
45 result = analyzer.Analyze();
49 const i::AstRawString* name = factory->GetOneByteString(var_name);
51 i::Variable* var = scope->Lookup(name);
54 if (var->location() == Variable::UNALLOCATED) {
55 CHECK_EQ(0, expected);
57 CHECK(var->IsStackAllocated());
58 CHECK_EQ(expected, result->GetAssignmentCountForTesting(scope, var));
66 TestHelper f("var x = 0; while (x) ;");
68 f.CheckLoopAssignedCount(0, "x");
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);"};
81 for (size_t i = 0; i < arraysize(loops); i++) {
82 TestHelper f(loops[i]);
83 f.CheckLoopAssignedCount(1, "x");
89 const char* loops[] = {
90 "for(x in 0) { }", "for(x of 0) { }",
93 for (size_t i = 0; i < arraysize(loops); i++) {
94 TestHelper f(loops[i]);
95 f.CheckLoopAssignedCount(0, "x");
101 TestHelper f("while (1) a = 0;");
103 f.CheckLoopAssignedCount(1, "a");
104 f.CheckLoopAssignedCount(0, "b");
105 f.CheckLoopAssignedCount(0, "c");
110 TestHelper f("for (;;) b = 0;");
112 f.CheckLoopAssignedCount(0, "a");
113 f.CheckLoopAssignedCount(1, "b");
114 f.CheckLoopAssignedCount(0, "c");
119 TestHelper f("a; b; c; for (;;) b = 0;");
121 f.CheckLoopAssignedCount(0, "a");
122 f.CheckLoopAssignedCount(1, "b");
123 f.CheckLoopAssignedCount(0, "c");
128 TestHelper f("for(x in 0) c = 0;");
130 f.CheckLoopAssignedCount(0, "a");
131 f.CheckLoopAssignedCount(0, "b");
132 f.CheckLoopAssignedCount(1, "c");
137 TestHelper f("a; b; c; for(x in 0) c = 0;");
139 f.CheckLoopAssignedCount(0, "a");
140 f.CheckLoopAssignedCount(0, "b");
141 f.CheckLoopAssignedCount(1, "c");
146 TestHelper f("while (x) { while (x) { var x = 0; } }");
148 f.CheckLoopAssignedCount(2, "x");
153 TestHelper f("while (0) { while (0) { var x = 0; } }");
155 f.CheckLoopAssignedCount(2, "x");
160 TestHelper f("while (0) { var y = 1; while (0) { var x = 0; } }");
162 f.CheckLoopAssignedCount(2, "x");
163 f.CheckLoopAssignedCount(1, "y");
168 const char* loops[] = {
170 "while (1) a(0, b++);",
171 "while (1) a(0, 0, b++);",
172 "while (1) a(b++, 1, 1);",
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++);",
185 "while (1) new a(b++);",
186 "while (1) (b++).f;",
188 "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++);",
203 for (size_t i = 0; i < arraysize(loops); i++) {
204 TestHelper f(loops[i]);
205 f.CheckLoopAssignedCount(1, "b");
210 TEST(NestedAssign1) {
211 const char* loops[] = {
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);",
227 "while (1) new a(b=1);",
228 "while (1) (b=1).f;",
230 "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);",
245 for (size_t i = 0; i < arraysize(loops); i++) {
246 TestHelper f(loops[i]);
247 f.CheckLoopAssignedCount(1, "b");
253 TestHelper f("var x, y, z, w; while (x++) while (y++) while (z++) ; w;");
255 f.CheckLoopAssignedCount(1, "x");
256 f.CheckLoopAssignedCount(2, "y");
257 f.CheckLoopAssignedCount(3, "z");
258 f.CheckLoopAssignedCount(0, "w");
262 TEST(NestedLoops3b) {
265 "while (1) { x=1; while (1) { y=1; while (1) z=1; } }"
268 f.CheckLoopAssignedCount(1, "x");
269 f.CheckLoopAssignedCount(2, "y");
270 f.CheckLoopAssignedCount(3, "z");
271 f.CheckLoopAssignedCount(0, "w");
275 TEST(NestedLoops3c) {
291 f.CheckLoopAssignedCount(1, "x");
292 f.CheckLoopAssignedCount(3, "y");
293 f.CheckLoopAssignedCount(5, "z");
294 f.CheckLoopAssignedCount(0, "w");