From: kmillikin@chromium.org Date: Wed, 10 Feb 2010 09:18:55 +0000 (+0000) Subject: Restrict the syntax that we aggressively optimize. X-Git-Tag: upstream/4.7.83~22513 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=30cf88af6b405698dc6eee65201439bfe908361d;p=platform%2Fupstream%2Fv8.git Restrict the syntax that we aggressively optimize. Do not use the speculative compiler for functions with other than one statement in the body, and do not use it if subexpressions can have side effects. Bailing out to the beginning of the full code is not sound if side effects have already occurred. Add tests that would fail without the restrictions. Review URL: http://codereview.chromium.org/598016 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@3826 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- diff --git a/src/fast-codegen.cc b/src/fast-codegen.cc index 587cda806..19091dafa 100644 --- a/src/fast-codegen.cc +++ b/src/fast-codegen.cc @@ -89,10 +89,10 @@ void FastCodeGenSyntaxChecker::VisitDeclarations( void FastCodeGenSyntaxChecker::VisitStatements(ZoneList* stmts) { - for (int i = 0, len = stmts->length(); i < len; i++) { - Visit(stmts->at(i)); - CHECK_BAILOUT; + if (stmts->length() != 1) { + BAILOUT("Function body is not a singleton statement."); } + Visit(stmts->at(0)); } @@ -276,6 +276,9 @@ void FastCodeGenSyntaxChecker::VisitAssignment(Assignment* expr) { Handle name = Handle::cast(key->handle()); LookupResult lookup; receiver->Lookup(*name, &lookup); + if (!lookup.IsValid()) { + BAILOUT("Assigned property not found at compile time"); + } if (lookup.holder() != *receiver) BAILOUT("Non-own property assignment"); if (!lookup.type() == FIELD) BAILOUT("Non-field property assignment"); } else { @@ -311,6 +314,9 @@ void FastCodeGenSyntaxChecker::VisitProperty(Property* expr) { Handle name = Handle::cast(key->handle()); LookupResult lookup; receiver->Lookup(*name, &lookup); + if (!lookup.IsValid()) { + BAILOUT("Referenced property not found at compile time"); + } if (lookup.holder() != *receiver) BAILOUT("Non-own property reference"); if (!lookup.type() == FIELD) BAILOUT("Non-field property reference"); } else { @@ -360,6 +366,16 @@ void FastCodeGenSyntaxChecker::VisitBinaryOperation(BinaryOperation* expr) { // a pair of registers to keep all intermediate values in registers // (i.e., the expression stack has height no more than two). if (!expr->right()->IsLeaf()) BAILOUT("expression nested on right"); + + // We do not allow subexpressions with side effects because we + // (currently) bail out to the beginning of the full function. The + // only expressions with side effects that we would otherwise handle + // are assignments. + if (expr->left()->AsAssignment() != NULL || + expr->right()->AsAssignment() != NULL) { + BAILOUT("subexpression of binary operation has side effects"); + } + Visit(expr->left()); CHECK_BAILOUT; Visit(expr->right()); diff --git a/test/mjsunit/compiler/simple-bailouts.js b/test/mjsunit/compiler/simple-bailouts.js new file mode 100644 index 000000000..af80b7f05 --- /dev/null +++ b/test/mjsunit/compiler/simple-bailouts.js @@ -0,0 +1,127 @@ +// Copyright 2010 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Flags: --fast-compiler + +function Test() { + this.result = 0; + this.x = 0; + this.y = 0; + this.z = 0; +} +var a = 1; +var b = 2; +var c = 4; +var d = 8; + +// Test operations expected to stay on the fast path. Enumerate all binary +// trees with <= 4 leaves. +Test.prototype.test0 = function () { + this.result = a | b; +}; + +Test.prototype.test1 = function() { + this.result = (a | b) | c; +}; + +Test.prototype.test2 = function() { + this.result = a | (b | c); +}; + +Test.prototype.test3 = function() { + this.result = ((a | b) | c) | d; +}; + +Test.prototype.test4 = function() { + this.result = (a | (b | c)) | d; +}; + +Test.prototype.test5 = function() { + this.result = (a | b) | (c | d); +}; + +Test.prototype.test6 = function() { + this.result = a | ((b | c) | d); +}; + +Test.prototype.test7 = function() { + this.result = a | (b | (c | d)); +}; + +// These tests should fail if we bailed out to the beginning of the full +// code. +Test.prototype.test8 = function () { + // If this.x = 1 and a = 1.1: + this.y = this.x | b; // Should be (1 | 2) == 3. + this.x = c; // Should be 4. + this.z = this.x | a; // Should be (4 | 1.1) == 5. +}; + +Test.prototype.test9 = function() { + // If this.x = 2 and a = 1.1: + this.z = // (14 | 1.1) == 15 + (this.x = // (6 | 8) == 14 + (this.y = // (2 | 4) == 6 + this.x // 2 + | c) // 4 + | d) // 8 + | a; // 1.1 +} + +var t = new Test(); + +t.test0(); +assertEquals(3, t.result); + +t.test1(); +assertEquals(7, t.result); +t.test2(); +assertEquals(7, t.result); + +t.test3(); +assertEquals(15, t.result); +t.test4(); +assertEquals(15, t.result); +t.test5(); +assertEquals(15, t.result); +t.test6(); +assertEquals(15, t.result); +t.test7(); +assertEquals(15, t.result); + +a = 1.1; +t.x = 1; +t.test8(); +assertEquals(4, t.x); +assertEquals(3, t.y); +assertEquals(5, t.z); + +t.x = 2; +t.test9(); +assertEquals(14, t.x); +assertEquals(6, t.y); +assertEquals(15, t.z);