8044533: Deoptimizing negation produces wrong result for zero
authorattila
Tue, 03 Jun 2014 12:04:36 +0200
changeset 24774 da5a0201be9d
parent 24773 34c21f8464c5
child 24775 a0ae9045563f
8044533: Deoptimizing negation produces wrong result for zero Reviewed-by: lagergren, sundar
nashorn/src/jdk/nashorn/internal/codegen/CodeGenerator.java
nashorn/test/script/basic/JDK-8044533.js
nashorn/test/script/basic/JDK-8044533.js.EXPECTED
--- a/nashorn/src/jdk/nashorn/internal/codegen/CodeGenerator.java	Tue Jun 03 11:31:06 2014 +0200
+++ b/nashorn/src/jdk/nashorn/internal/codegen/CodeGenerator.java	Tue Jun 03 12:04:36 2014 +0200
@@ -3579,7 +3579,8 @@
     }
 
     private void loadSUB(final UnaryNode unaryNode, final TypeBounds resultBounds) {
-        assert unaryNode.getType().isNumeric();
+        final Type type = unaryNode.getType();
+        assert type.isNumeric();
         final TypeBounds numericBounds = resultBounds.booleanToInt();
         new OptimisticOperation(unaryNode, numericBounds) {
             @Override
@@ -3589,6 +3590,12 @@
             }
             @Override
             void consumeStack() {
+                // Must do an explicit conversion to the operation's type when it's double so that we correctly handle
+                // negation of an int 0 to a double -0. With this, we get the correct negation of a local variable after
+                // it deoptimized, e.g. "iload_2; i2d; dneg". Without this, we get "iload_2; ineg; i2d".
+                if(type.isNumber()) {
+                    method.convert(type);
+                }
                 method.neg(getProgramPoint());
             }
         }.emit();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8044533.js	Tue Jun 03 12:04:36 2014 +0200
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2010, 2013, 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-8044533: Deoptimizing negation produces wrong result for zero
+ *
+ * @test
+ * @run
+ */
+
+print(1/(function() { var f = 0; return -f; })())
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8044533.js.EXPECTED	Tue Jun 03 12:04:36 2014 +0200
@@ -0,0 +1,1 @@
+-Infinity