changeset 1809:29c6984a1673

4965689: class literal code wastes a byte Reviewed-by: jjg
author vromero
date Tue, 02 Apr 2013 10:51:16 +0100
parents d3648557391b
children 46d2f144ebbd
files src/share/classes/com/sun/tools/javac/jvm/Code.java src/share/classes/com/sun/tools/javac/jvm/Gen.java src/share/classes/com/sun/tools/javac/jvm/Items.java test/tools/javac/T4965689/ClassLiteralWastesByteTest.java
diffstat 4 files changed, 80 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/jvm/Code.java	Thu Mar 28 10:58:45 2013 -0700
+++ b/src/share/classes/com/sun/tools/javac/jvm/Code.java	Tue Apr 02 10:51:16 2013 +0100
@@ -373,6 +373,17 @@
         Assert.check(alive || state.stacksize == 0);
     }
 
+    /** Emit a ldc (or ldc_w) instruction, taking into account operand size
+    */
+    public void emitLdc(int od) {
+        if (od <= 255) {
+            emitop1(ldc1, od);
+        }
+        else {
+            emitop2(ldc2, od);
+        }
+    }
+
     /** Emit a multinewarray instruction.
      */
     public void emitMultianewarray(int ndims, int type, Type arrayType) {
--- a/src/share/classes/com/sun/tools/javac/jvm/Gen.java	Thu Mar 28 10:58:45 2013 -0700
+++ b/src/share/classes/com/sun/tools/javac/jvm/Gen.java	Tue Apr 02 10:51:16 2013 +0100
@@ -2227,7 +2227,7 @@
 
         if (tree.name == names._class) {
             Assert.check(target.hasClassLiterals());
-            code.emitop2(ldc2, makeRef(tree.pos(), tree.selected.type));
+            code.emitLdc(makeRef(tree.pos(), tree.selected.type));
             result = items.makeStackItem(pt);
             return;
        }
--- a/src/share/classes/com/sun/tools/javac/jvm/Items.java	Thu Mar 28 10:58:45 2013 -0700
+++ b/src/share/classes/com/sun/tools/javac/jvm/Items.java	Tue Apr 02 10:51:16 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 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
@@ -571,10 +571,8 @@
             int idx = pool.put(value);
             if (typecode == LONGcode || typecode == DOUBLEcode) {
                 code.emitop2(ldc2w, idx);
-            } else if (idx <= 255) {
-                code.emitop1(ldc1, idx);
             } else {
-                code.emitop2(ldc2, idx);
+                code.emitLdc(idx);
             }
         }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/T4965689/ClassLiteralWastesByteTest.java	Tue Apr 02 10:51:16 2013 +0100
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 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.
+ */
+
+/*
+ * @test
+ * @bug 4965689
+ * @summary class literal code wastes a byte
+ */
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.nio.file.Paths;
+
+public class ClassLiteralWastesByteTest {
+
+    private static final String assertionErrorMsg =
+            "Instead of ldc_w, ldc instruction should have been generated";
+
+    public static void main(String[] args) {
+        new ClassLiteralWastesByteTest().run();
+    }
+
+    void run() {
+        check("-c", Paths.get(System.getProperty("test.classes"),
+                "test.class").toString());
+    }
+
+    void check(String... params) {
+        StringWriter s;
+        String out;
+        try (PrintWriter pw = new PrintWriter(s = new StringWriter())) {
+            com.sun.tools.javap.Main.run(params, pw);
+            out = s.toString();
+        }
+        if (out.contains("ldc_w")) {
+            throw new AssertionError(assertionErrorMsg);
+        }
+    }
+
+}
+
+class test {
+    void m() {
+        Class<?> aClass = test.class;
+    }
+}