7190862: javap shows an incorrect type for operands if the 'wide' prefix is used
authorvromero
Wed, 21 Nov 2012 18:40:45 +0000
changeset 14717 641fdce05089
parent 14554 4e29b285c723
child 14718 fd562276250e
7190862: javap shows an incorrect type for operands if the 'wide' prefix is used 7109747: (javap) classfile not treating iinc_w correctly. Reviewed-by: jjg, mcimadamore
langtools/src/share/classes/com/sun/tools/classfile/Instruction.java
langtools/src/share/classes/com/sun/tools/classfile/Opcode.java
langtools/test/tools/javap/T7190862.java
--- a/langtools/src/share/classes/com/sun/tools/classfile/Instruction.java	Tue Nov 20 09:58:55 2012 -0800
+++ b/langtools/src/share/classes/com/sun/tools/classfile/Instruction.java	Wed Nov 21 18:40:45 2012 +0000
@@ -71,11 +71,16 @@
         SHORT(3),
         /** Wide opcode is not followed by any operands. */
         WIDE_NO_OPERANDS(2),
+        /** Wide opcode is followed by a 2-byte index into the local variables array. */
+        WIDE_LOCAL(4),
         /** Wide opcode is followed by a 2-byte index into the constant pool. */
         WIDE_CPREF_W(4),
         /** Wide opcode is followed by a 2-byte index into the constant pool,
          *  and a signed short value. */
         WIDE_CPREF_W_SHORT(6),
+        /** Wide opcode is followed by a 2-byte reference to a local variable,
+         *  and a signed short value. */
+        WIDE_LOCAL_SHORT(6),
         /** Opcode was not recognized. */
         UNKNOWN(1);
 
@@ -101,7 +106,7 @@
         R visitConstantPoolRef(Instruction instr, int index, P p);
         /** See {@link Kind#CPREF_W_UBYTE}, {@link Kind#CPREF_W_UBYTE_ZERO}, {@link Kind#WIDE_CPREF_W_SHORT}. */
         R visitConstantPoolRefAndValue(Instruction instr, int index, int value, P p);
-        /** See {@link Kind#LOCAL}. */
+        /** See {@link Kind#LOCAL}, {@link Kind#WIDE_LOCAL}. */
         R visitLocal(Instruction instr, int index, P p);
         /** See {@link Kind#LOCAL_BYTE}. */
         R visitLocalAndValue(Instruction instr, int index, int value, P p);
@@ -315,6 +320,9 @@
             case WIDE_NO_OPERANDS:
                 return visitor.visitNoOperands(this, p);
 
+            case WIDE_LOCAL:
+                return visitor.visitLocal(this, getUnsignedShort(2), p);
+
             case WIDE_CPREF_W:
                 return visitor.visitConstantPoolRef(this, getUnsignedShort(2), p);
 
@@ -322,6 +330,10 @@
                 return visitor.visitConstantPoolRefAndValue(
                         this, getUnsignedShort(2), getUnsignedByte(4), p);
 
+            case WIDE_LOCAL_SHORT:
+                return visitor.visitLocalAndValue(
+                        this, getUnsignedShort(2), getShort(4), p);
+
             case UNKNOWN:
                 return visitor.visitUnknown(this, p);
 
--- a/langtools/src/share/classes/com/sun/tools/classfile/Opcode.java	Tue Nov 20 09:58:55 2012 -0800
+++ b/langtools/src/share/classes/com/sun/tools/classfile/Opcode.java	Wed Nov 21 18:40:45 2012 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009, 2012, 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
@@ -246,18 +246,18 @@
     // impdep 0xff: Picojava priv
 
     // wide opcodes
-    ILOAD_W(0xc415, WIDE_CPREF_W),
-    LLOAD_W(0xc416, WIDE_CPREF_W),
-    FLOAD_W(0xc417, WIDE_CPREF_W),
-    DLOAD_W(0xc418, WIDE_CPREF_W),
-    ALOAD_W(0xc419, WIDE_CPREF_W),
-    ISTORE_W(0xc436, WIDE_CPREF_W),
-    LSTORE_W(0xc437, WIDE_CPREF_W),
-    FSTORE_W(0xc438, WIDE_CPREF_W),
-    DSTORE_W(0xc439, WIDE_CPREF_W),
-    ASTORE_W(0xc43a, WIDE_CPREF_W),
-    IINC_W(0xc484, WIDE_CPREF_W_SHORT),
-    RET_W(0xc4a9, WIDE_CPREF_W),
+    ILOAD_W(0xc415, WIDE_LOCAL),
+    LLOAD_W(0xc416, WIDE_LOCAL),
+    FLOAD_W(0xc417, WIDE_LOCAL),
+    DLOAD_W(0xc418, WIDE_LOCAL),
+    ALOAD_W(0xc419, WIDE_LOCAL),
+    ISTORE_W(0xc436, WIDE_LOCAL),
+    LSTORE_W(0xc437, WIDE_LOCAL),
+    FSTORE_W(0xc438, WIDE_LOCAL),
+    DSTORE_W(0xc439, WIDE_LOCAL),
+    ASTORE_W(0xc43a, WIDE_LOCAL),
+    IINC_W(0xc484, WIDE_LOCAL_SHORT),
+    RET_W(0xc4a9, WIDE_LOCAL),
 
     // PicoJava nonpriv instructions
     LOAD_UBYTE(PICOJAVA, 0xfe00),
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javap/T7190862.java	Wed Nov 21 18:40:45 2012 +0000
@@ -0,0 +1,157 @@
+
+/*
+ * @test /nodynamiccopyright/
+ * @bug 7190862 7109747
+ * @summary javap shows an incorrect type for operands if the 'wide' prefix is used
+ */
+
+import com.sun.source.util.JavacTask;
+import com.sun.tools.javap.JavapFileManager;
+import com.sun.tools.javap.JavapTask;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Locale;
+import javax.tools.Diagnostic;
+import javax.tools.DiagnosticCollector;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileManager;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.ToolProvider;
+
+public class T7190862 {
+
+    enum TypeWideInstructionMap {
+        INT("int", new String[]{"istore_w", "iload_w"}),
+        LONG("long", new String[]{"lstore_w", "lload_w"}),
+        FLOAT("float", new String[]{"fstore_w", "fload_w"}),
+        DOUBLE("double", new String[]{"dstore_w", "dload_w"}),
+        OBJECT("Object", new String[]{"astore_w", "aload_w"});
+
+        String type;
+        String[] instructions;
+
+        TypeWideInstructionMap(String type, String[] instructions) {
+            this.type = type;
+            this.instructions = instructions;
+        }
+    }
+
+    JavaSource source;
+
+    public static void main(String[] args) {
+        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
+        new T7190862().run(comp);
+    }
+
+    private void run(JavaCompiler comp) {
+        String code;
+        for (TypeWideInstructionMap typeInstructionMap: TypeWideInstructionMap.values()) {
+            if (typeInstructionMap != TypeWideInstructionMap.OBJECT) {
+                code = createWideLocalSource(typeInstructionMap.type, 300);
+            } else {
+                code = createWideLocalSourceForObject(300);
+            }
+            source = new JavaSource(code);
+            compile(comp);
+            check(typeInstructionMap.instructions);
+        }
+
+        //an extra test for the iinc instruction
+        code = createIincSource();
+        source = new JavaSource(code);
+        compile(comp);
+        check(new String[]{"iinc_w"});
+    }
+
+    private void compile(JavaCompiler comp) {
+        JavacTask ct = (JavacTask)comp.getTask(null, null, null, null, null, Arrays.asList(source));
+        try {
+            if (!ct.call()) {
+                throw new AssertionError("Error thrown when compiling the following source:\n" + source.getCharContent(true));
+            }
+        } catch (Throwable ex) {
+            throw new AssertionError("Error thrown when compiling the following source:\n" + source.getCharContent(true));
+        }
+    }
+
+    private void check(String[] instructions) {
+        String out = javap(Arrays.asList("-c"), Arrays.asList("Test.class"));
+        for (String line: out.split(System.getProperty("line.separator"))) {
+            line = line.trim();
+            for (String instruction: instructions) {
+                if (line.contains(instruction) && line.contains("#")) {
+                    throw new Error("incorrect type for operands for instruction " + instruction);
+                }
+            }
+        }
+    }
+
+    private String javap(List<String> args, List<String> classes) {
+        DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>();
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter(sw);
+        JavaFileManager fm = JavapFileManager.create(dc, pw);
+        JavapTask t = new JavapTask(pw, fm, dc, args, classes);
+        boolean ok = t.run();
+        if (!ok)
+            throw new Error("javap failed unexpectedly");
+
+        List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();
+        for (Diagnostic<? extends JavaFileObject> d: diags) {
+            if (d.getKind() == Diagnostic.Kind.ERROR)
+                throw new Error(d.getMessage(Locale.ENGLISH));
+        }
+        return sw.toString();
+
+    }
+
+    private String createWideLocalSource(String type, int numberOfVars) {
+        String result = "    " + type + " x0 = 0;\n";
+        for (int i = 1; i < numberOfVars; i++) {
+            result += "        " + type + " x" + i + " = x" + (i - 1) + " + 1;\n";
+        }
+        return result;
+    }
+
+    private String createWideLocalSourceForObject(int numberOfVars) {
+        String result = "    Object x0 = new Object();\n";
+        for (int i = 1; i < numberOfVars; i++) {
+            result += "        Object x" + i + " = x0;\n";
+        }
+        return result;
+    }
+
+    private String createIincSource() {
+        return "    int i = 0;\n"
+                + "        i += 1;\n"
+                + "        i += 51;\n"
+                + "        i += 101;\n"
+                + "        i += 151;\n";
+    }
+
+    class JavaSource extends SimpleJavaFileObject {
+
+        String template = "class Test {\n" +
+                          "    public static void main(String[] args)\n" +
+                          "    {\n" +
+                          "        #C" +
+                          "    }\n" +
+                          "}";
+
+        String source;
+
+        public JavaSource(String code) {
+            super(URI.create("Test.java"), JavaFileObject.Kind.SOURCE);
+            source = template.replaceAll("#C", code);
+        }
+
+        @Override
+        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
+            return source;
+        }
+    }
+}