52ef2a1c27
Expressions of the form a_0 + a_1 + a_2 + a_3 + ... + a_n seem to be reasonably common for cases such as building templates. However, parsing these expressions results in a n-deep expression tree: ... / + / \ + a_2 / \ a_0 a_1 Traversing this tree during compilation can cause a stack overflow when n is large. Instead, for left-associate operations such as add, we now build up an n-ary node in the parse tree, of the form n-ary + / | \ / | ... \ a_0 a_1 a_n The bytecode compiler can now iterate through the child expressions rather than recursing. This patch only supports arithmetic operations -- subsequent patches will enable the same optimization for logical tests and comma expressions. Bug: v8:6964 Bug: chromium:724961 Bug: chromium:731861 Bug: chromium:752081 Bug: chromium:771653 Bug: chromium:777302 Change-Id: Ie97e4ce42506fe62a7bc4ffbdaa90a9f698352cb Reviewed-on: https://chromium-review.googlesource.com/733120 Commit-Queue: Leszek Swirski <leszeks@chromium.org> Reviewed-by: Marja Hölttä <marja@chromium.org> Cr-Commit-Position: refs/heads/master@{#48920}
87 lines
1.8 KiB
JavaScript
87 lines
1.8 KiB
JavaScript
// Copyright 2017 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.
|
|
|
|
// Test that n-ary chains of binary ops give an equal result to individual
|
|
// binary op calls.
|
|
|
|
// Generate a function of the form
|
|
//
|
|
// function(init,a0,...,aN) {
|
|
// return init + a0 + ... + aN;
|
|
// }
|
|
//
|
|
// where + can be any binary operation.
|
|
function generate_chained_op(op, num_ops) {
|
|
let str = "(function(init";
|
|
for (let i = 0; i < num_ops; i++) {
|
|
str += ",a"+i;
|
|
}
|
|
str += "){return (init";
|
|
for (let i = 0; i < num_ops; i++) {
|
|
str += op+"a"+i;
|
|
}
|
|
str += ");})";
|
|
return eval(str);
|
|
}
|
|
|
|
// Generate a function of the form
|
|
//
|
|
// function(init,a0,...,aN) {
|
|
// var tmp = init;
|
|
// tmp = tmp + a0;
|
|
// ...
|
|
// tmp = tmp + aN;
|
|
// return tmp;
|
|
// }
|
|
//
|
|
// where + can be any binary operation.
|
|
function generate_nonchained_op(op, num_ops) {
|
|
let str = "(function(init";
|
|
for (let i = 0; i < num_ops; i++) {
|
|
str += ",a"+i;
|
|
}
|
|
str += "){ var tmp=init; ";
|
|
for (let i = 0; i < num_ops; i++) {
|
|
str += "tmp=(tmp"+op+"a"+i+");";
|
|
}
|
|
str += "return tmp;})";
|
|
return eval(str);
|
|
}
|
|
|
|
const BINOPS = [
|
|
",",
|
|
"||",
|
|
"&&",
|
|
"|",
|
|
"^",
|
|
"&",
|
|
"<<",
|
|
">>",
|
|
">>>",
|
|
"+",
|
|
"-",
|
|
"*",
|
|
"/",
|
|
"%",
|
|
];
|
|
|
|
// Test each binop to see if the chained version is equivalent to the non-
|
|
// chained one.
|
|
for (let op of BINOPS) {
|
|
let chained = generate_chained_op(op, 5);
|
|
let nonchained = generate_nonchained_op(op, 5);
|
|
|
|
// With numbers.
|
|
assertEquals(
|
|
nonchained(1,2,3,4,5),
|
|
chained(1,2,3,4,5),
|
|
"numeric " + op);
|
|
|
|
// With numbers and strings.
|
|
assertEquals(
|
|
nonchained(1,"2",3,"4",5),
|
|
chained(1,"2",3,"4",5),
|
|
"numeric and string " + op);
|
|
}
|