2014-10-28 17:29:41 +00:00
|
|
|
// Copyright 2014 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2015-11-26 16:22:34 +00:00
|
|
|
#include "src/ast/scopes.h"
|
2016-08-31 08:49:14 +00:00
|
|
|
#include "src/compilation-info.h"
|
2014-10-28 17:29:41 +00:00
|
|
|
#include "src/compiler/ast-loop-assignment-analyzer.h"
|
2017-01-09 13:43:28 +00:00
|
|
|
#include "src/objects-inl.h"
|
2016-08-22 11:33:30 +00:00
|
|
|
#include "src/parsing/parse-info.h"
|
2016-11-30 13:21:11 +00:00
|
|
|
#include "src/parsing/parsing.h"
|
2015-11-26 16:22:34 +00:00
|
|
|
#include "src/parsing/rewriter.h"
|
2014-10-28 17:29:41 +00:00
|
|
|
#include "test/cctest/cctest.h"
|
|
|
|
|
2015-10-30 09:16:26 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace compiler {
|
2014-10-28 17:29:41 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
const int kBufferSize = 1024;
|
|
|
|
|
|
|
|
struct TestHelper : public HandleAndZoneScope {
|
|
|
|
Handle<JSFunction> function;
|
|
|
|
LoopAssignmentAnalysis* result;
|
|
|
|
|
|
|
|
explicit TestHelper(const char* body)
|
|
|
|
: function(Handle<JSFunction>::null()), result(NULL) {
|
2014-10-28 17:54:41 +00:00
|
|
|
ScopedVector<char> program(kBufferSize);
|
|
|
|
SNPrintF(program, "function f(a,b,c) { %s; } f;", body);
|
|
|
|
v8::Local<v8::Value> v = CompileRun(program.start());
|
2014-10-28 17:29:41 +00:00
|
|
|
Handle<Object> obj = v8::Utils::OpenHandle(*v);
|
|
|
|
function = Handle<JSFunction>::cast(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckLoopAssignedCount(int expected, const char* var_name) {
|
|
|
|
// TODO(titzer): don't scope analyze every single time.
|
2017-07-21 09:32:32 +00:00
|
|
|
Handle<SharedFunctionInfo> shared(function->shared());
|
|
|
|
ParseInfo parse_info(shared);
|
2017-03-22 12:46:46 +00:00
|
|
|
CompilationInfo info(parse_info.zone(), &parse_info, function->GetIsolate(),
|
2017-07-21 09:32:32 +00:00
|
|
|
shared, function);
|
2014-10-28 17:29:41 +00:00
|
|
|
|
2017-07-21 09:32:32 +00:00
|
|
|
CHECK(parsing::ParseFunction(&parse_info, info.shared_info(),
|
|
|
|
info.isolate()));
|
2017-03-22 13:00:00 +00:00
|
|
|
CHECK(Rewriter::Rewrite(&parse_info, function->GetIsolate()));
|
2017-03-22 14:47:21 +00:00
|
|
|
DeclarationScope::Analyze(&parse_info, info.isolate(),
|
|
|
|
AnalyzeMode::kRegular);
|
2014-10-28 17:29:41 +00:00
|
|
|
|
2016-08-05 14:30:54 +00:00
|
|
|
DeclarationScope* scope = info.literal()->scope();
|
2015-03-09 14:51:13 +00:00
|
|
|
AstValueFactory* factory = parse_info.ast_value_factory();
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(scope);
|
2014-10-28 17:29:41 +00:00
|
|
|
|
|
|
|
if (result == NULL) {
|
|
|
|
AstLoopAssignmentAnalyzer analyzer(main_zone(), &info);
|
|
|
|
result = analyzer.Analyze();
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(result);
|
2014-10-28 17:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const i::AstRawString* name = factory->GetOneByteString(var_name);
|
|
|
|
|
|
|
|
i::Variable* var = scope->Lookup(name);
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(var);
|
2014-10-28 17:29:41 +00:00
|
|
|
|
2015-07-06 16:36:28 +00:00
|
|
|
if (var->location() == VariableLocation::UNALLOCATED) {
|
2014-10-28 17:29:41 +00:00
|
|
|
CHECK_EQ(0, expected);
|
|
|
|
} else {
|
|
|
|
CHECK(var->IsStackAllocated());
|
|
|
|
CHECK_EQ(expected, result->GetAssignmentCountForTesting(scope, var));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-09-30 13:46:56 +00:00
|
|
|
} // namespace
|
2014-10-28 17:29:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(SimpleLoop1) {
|
|
|
|
TestHelper f("var x = 0; while (x) ;");
|
|
|
|
|
|
|
|
f.CheckLoopAssignedCount(0, "x");
|
|
|
|
}
|
|
|
|
|
[ignition] desugar GetIterator() via bytecode rather than via AST
Introduces:
- a new AST node representing the GetIterator() algorithm in the specification, to be used by ForOfStatement, YieldExpression (in the case of delegating yield*), and the future `for-await-of` loop proposed in http://tc39.github.io/proposal-async-iteration/#sec-async-iterator-value-unwrap-functions.
- a new opcode (JumpIfJSReceiver), which is useful for `if Type(object) is not Object` checks which are common throughout the specification. This node is easily eliminated by TurboFan.
The AST node is desugared specially in bytecode, rather than manually when building the AST. The benefit of this is that desugaring in the BytecodeGenerator is much simpler and easier to understand than desugaring the AST.
This also reduces parse time very slightly, and allows us to use LoadIC rather than KeyedLoadIC, which seems to have better baseline performance. This results in a ~20% improvement in test/js-perf-test/Iterators micro-benchmarks, which I believe owes to the use of the slightly faster LoadIC as opposed to the KeyedLoadIC in the baseline case. Both produce identical optimized code via TurboFan when the type check can be eliminated, and the load can be replaced with a constant value.
BUG=v8:4280
R=bmeurer@chromium.org, rmcilroy@chromium.org, adamk@chromium.org, neis@chromium.org, jarin@chromium.org
TBR=rossberg@chromium.org
Review-Url: https://codereview.chromium.org/2557593004
Cr-Commit-Position: refs/heads/master@{#41555}
2016-12-07 15:19:52 +00:00
|
|
|
TEST(ForIn1) {
|
|
|
|
const char* loops[] = {"for(x in 0) { }"};
|
2014-10-28 17:29:41 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < arraysize(loops); i++) {
|
|
|
|
TestHelper f(loops[i]);
|
|
|
|
f.CheckLoopAssignedCount(0, "x");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Param1) {
|
|
|
|
TestHelper f("while (1) a = 0;");
|
|
|
|
|
|
|
|
f.CheckLoopAssignedCount(1, "a");
|
|
|
|
f.CheckLoopAssignedCount(0, "b");
|
|
|
|
f.CheckLoopAssignedCount(0, "c");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Param2) {
|
|
|
|
TestHelper f("for (;;) b = 0;");
|
|
|
|
|
|
|
|
f.CheckLoopAssignedCount(0, "a");
|
|
|
|
f.CheckLoopAssignedCount(1, "b");
|
|
|
|
f.CheckLoopAssignedCount(0, "c");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Param2b) {
|
|
|
|
TestHelper f("a; b; c; for (;;) b = 0;");
|
|
|
|
|
|
|
|
f.CheckLoopAssignedCount(0, "a");
|
|
|
|
f.CheckLoopAssignedCount(1, "b");
|
|
|
|
f.CheckLoopAssignedCount(0, "c");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Param3) {
|
|
|
|
TestHelper f("for(x in 0) c = 0;");
|
|
|
|
|
|
|
|
f.CheckLoopAssignedCount(0, "a");
|
|
|
|
f.CheckLoopAssignedCount(0, "b");
|
|
|
|
f.CheckLoopAssignedCount(1, "c");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Param3b) {
|
|
|
|
TestHelper f("a; b; c; for(x in 0) c = 0;");
|
|
|
|
|
|
|
|
f.CheckLoopAssignedCount(0, "a");
|
|
|
|
f.CheckLoopAssignedCount(0, "b");
|
|
|
|
f.CheckLoopAssignedCount(1, "c");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(NestedLoop1) {
|
|
|
|
TestHelper f("while (x) { while (x) { var x = 0; } }");
|
|
|
|
|
|
|
|
f.CheckLoopAssignedCount(2, "x");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(NestedLoop2) {
|
|
|
|
TestHelper f("while (0) { while (0) { var x = 0; } }");
|
|
|
|
|
|
|
|
f.CheckLoopAssignedCount(2, "x");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(NestedLoop3) {
|
|
|
|
TestHelper f("while (0) { var y = 1; while (0) { var x = 0; } }");
|
|
|
|
|
|
|
|
f.CheckLoopAssignedCount(2, "x");
|
|
|
|
f.CheckLoopAssignedCount(1, "y");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(NestedInc1) {
|
|
|
|
const char* loops[] = {
|
|
|
|
"while (1) a(b++);",
|
|
|
|
"while (1) a(0, b++);",
|
|
|
|
"while (1) a(0, 0, b++);",
|
|
|
|
"while (1) a(b++, 1, 1);",
|
|
|
|
"while (1) a(++b);",
|
|
|
|
"while (1) a + (b++);",
|
|
|
|
"while (1) (b++) + a;",
|
|
|
|
"while (1) a + c(b++);",
|
|
|
|
"while (1) throw b++;",
|
|
|
|
"while (1) switch (b++) {} ;",
|
|
|
|
"while (1) switch (a) {case (b++): 0; } ;",
|
|
|
|
"while (1) switch (a) {case b: b++; } ;",
|
|
|
|
"while (1) a == (b++);",
|
|
|
|
"while (1) a === (b++);",
|
|
|
|
"while (1) +(b++);",
|
|
|
|
"while (1) ~(b++);",
|
|
|
|
"while (1) new a(b++);",
|
|
|
|
"while (1) (b++).f;",
|
|
|
|
"while (1) a[b++];",
|
|
|
|
"while (1) (b++)();",
|
|
|
|
"while (1) [b++];",
|
|
|
|
"while (1) [0,b++];",
|
|
|
|
"while (1) var y = [11,b++,12];",
|
|
|
|
"while (1) var y = {f:11,g:(b++),h:12};",
|
|
|
|
"while (1) try {b++;} finally {};",
|
|
|
|
"while (1) try {} finally {b++};",
|
|
|
|
"while (1) try {b++;} catch (e) {};",
|
|
|
|
"while (1) try {} catch (e) {b++};",
|
|
|
|
"while (1) return b++;",
|
|
|
|
"while (1) (b++) ? b : b;",
|
|
|
|
"while (1) b ? (b++) : b;",
|
|
|
|
"while (1) b ? b : (b++);",
|
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < arraysize(loops); i++) {
|
|
|
|
TestHelper f(loops[i]);
|
|
|
|
f.CheckLoopAssignedCount(1, "b");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(NestedAssign1) {
|
|
|
|
const char* loops[] = {
|
|
|
|
"while (1) a(b=1);",
|
|
|
|
"while (1) a(0, b=1);",
|
|
|
|
"while (1) a(0, 0, b=1);",
|
|
|
|
"while (1) a(b=1, 1, 1);",
|
|
|
|
"while (1) a + (b=1);",
|
|
|
|
"while (1) (b=1) + a;",
|
|
|
|
"while (1) a + c(b=1);",
|
|
|
|
"while (1) throw b=1;",
|
|
|
|
"while (1) switch (b=1) {} ;",
|
|
|
|
"while (1) switch (a) {case b=1: 0; } ;",
|
|
|
|
"while (1) switch (a) {case b: b=1; } ;",
|
|
|
|
"while (1) a == (b=1);",
|
|
|
|
"while (1) a === (b=1);",
|
|
|
|
"while (1) +(b=1);",
|
|
|
|
"while (1) ~(b=1);",
|
|
|
|
"while (1) new a(b=1);",
|
|
|
|
"while (1) (b=1).f;",
|
|
|
|
"while (1) a[b=1];",
|
|
|
|
"while (1) (b=1)();",
|
|
|
|
"while (1) [b=1];",
|
|
|
|
"while (1) [0,b=1];",
|
|
|
|
"while (1) var z = [11,b=1,12];",
|
|
|
|
"while (1) var y = {f:11,g:(b=1),h:12};",
|
|
|
|
"while (1) try {b=1;} finally {};",
|
|
|
|
"while (1) try {} finally {b=1};",
|
|
|
|
"while (1) try {b=1;} catch (e) {};",
|
|
|
|
"while (1) try {} catch (e) {b=1};",
|
|
|
|
"while (1) return b=1;",
|
|
|
|
"while (1) (b=1) ? b : b;",
|
|
|
|
"while (1) b ? (b=1) : b;",
|
|
|
|
"while (1) b ? b : (b=1);",
|
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < arraysize(loops); i++) {
|
|
|
|
TestHelper f(loops[i]);
|
|
|
|
f.CheckLoopAssignedCount(1, "b");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(NestedLoops3) {
|
|
|
|
TestHelper f("var x, y, z, w; while (x++) while (y++) while (z++) ; w;");
|
|
|
|
|
|
|
|
f.CheckLoopAssignedCount(1, "x");
|
|
|
|
f.CheckLoopAssignedCount(2, "y");
|
|
|
|
f.CheckLoopAssignedCount(3, "z");
|
|
|
|
f.CheckLoopAssignedCount(0, "w");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(NestedLoops3b) {
|
|
|
|
TestHelper f(
|
|
|
|
"var x, y, z, w;"
|
|
|
|
"while (1) { x=1; while (1) { y=1; while (1) z=1; } }"
|
|
|
|
"w;");
|
|
|
|
|
|
|
|
f.CheckLoopAssignedCount(1, "x");
|
|
|
|
f.CheckLoopAssignedCount(2, "y");
|
|
|
|
f.CheckLoopAssignedCount(3, "z");
|
|
|
|
f.CheckLoopAssignedCount(0, "w");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(NestedLoops3c) {
|
|
|
|
TestHelper f(
|
|
|
|
"var x, y, z, w;"
|
|
|
|
"while (1) {"
|
|
|
|
" x++;"
|
|
|
|
" while (1) {"
|
|
|
|
" y++;"
|
|
|
|
" while (1) z++;"
|
|
|
|
" }"
|
|
|
|
" while (1) {"
|
|
|
|
" y++;"
|
|
|
|
" while (1) z++;"
|
|
|
|
" }"
|
|
|
|
"}"
|
|
|
|
"w;");
|
|
|
|
|
|
|
|
f.CheckLoopAssignedCount(1, "x");
|
|
|
|
f.CheckLoopAssignedCount(3, "y");
|
|
|
|
f.CheckLoopAssignedCount(5, "z");
|
|
|
|
f.CheckLoopAssignedCount(0, "w");
|
|
|
|
}
|
2015-10-30 09:16:26 +00:00
|
|
|
|
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|