8066224: fixes for folding a constant-test ternary operator

Reviewed-by: hannesw, lagergren
This commit is contained in:
Attila Szegedi 2014-12-10 11:55:25 +01:00
parent 47e744920e
commit 22573e0db4
4 changed files with 54 additions and 2 deletions

View File

@ -2021,6 +2021,19 @@ final class CodeGenerator extends NodeOperatorVisitor<CodeGeneratorLexicalContex
final Expression test = ifNode.getTest();
final Block pass = ifNode.getPass();
final Block fail = ifNode.getFail();
if (Expression.isAlwaysTrue(test)) {
loadAndDiscard(test);
pass.accept(this);
return false;
} else if (Expression.isAlwaysFalse(test)) {
loadAndDiscard(test);
if (fail != null) {
fail.accept(this);
}
return false;
}
final boolean hasFailConversion = LocalVariableConversion.hasLiveConversion(ifNode);
final Label failLabel = new Label("if_fail");
@ -2040,7 +2053,7 @@ final class CodeGenerator extends NodeOperatorVisitor<CodeGeneratorLexicalContex
method.beforeJoinPoint(ifNode);
}
if(afterLabel != null) {
if(afterLabel != null && afterLabel.isReachable()) {
method.label(afterLabel);
}

View File

@ -131,7 +131,7 @@ final class FoldConstants extends NodeVisitor<LexicalContext> implements Loggabl
public Node leaveTernaryNode(final TernaryNode ternaryNode) {
final Node test = ternaryNode.getTest();
if (test instanceof LiteralNode.PrimitiveLiteralNode) {
return ((LiteralNode.PrimitiveLiteralNode<?>)test).isTrue() ? ternaryNode.getTrueExpression() : ternaryNode.getFalseExpression();
return (((LiteralNode.PrimitiveLiteralNode<?>)test).isTrue() ? ternaryNode.getTrueExpression() : ternaryNode.getFalseExpression()).getExpression();
}
return ternaryNode;
}

View File

@ -0,0 +1,38 @@
/*
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* JDK-8066224: fixes for folding a constant-test ternary operator
*
* @test
* @run
*/
print((function(){
if(false ? 0 : '') {
throw false;
} else if (x = this) {
var x = x;
}
return x === this;
})())

View File

@ -0,0 +1 @@
true