View Javadoc
1   /*
2    * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.
8    *
9    * This code is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12   * version 2 for more details (a copy is included in the LICENSE file that
13   * accompanied this code).
14   *
15   * You should have received a copy of the GNU General Public License version
16   * 2 along with this work; if not, write to the Free Software Foundation,
17   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18   *
19   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20   * or visit www.oracle.com if you need additional information or have any
21   * questions.
22   *
23   */
24  
25  package sun.jvm.hotspot.interpreter;
26  
27  import java.util.*;
28  import java.lang.reflect.Constructor;
29  import sun.jvm.hotspot.oops.*;
30  import sun.jvm.hotspot.utilities.*;
31  
32  public class BytecodeDisassembler {
33     private Method method;
34  
35     private static Map bytecode2Class = new HashMap(); // Map<int, Class>
36  
37     private static void addBytecodeClass(int bytecode, Class clazz) {
38        bytecode2Class.put(new Integer(bytecode), clazz);
39     }
40  
41     private static Class getBytecodeClass(int bytecode) {
42        return (Class) bytecode2Class.get(new Integer(bytecode));
43     }
44  
45     static {
46        addBytecodeClass(Bytecodes._anewarray, BytecodeANewArray.class);
47        addBytecodeClass(Bytecodes._bipush, BytecodeBipush.class);
48        addBytecodeClass(Bytecodes._checkcast, BytecodeCheckCast.class);
49        addBytecodeClass(Bytecodes._getfield, BytecodeGetField.class);
50        addBytecodeClass(Bytecodes._getstatic, BytecodeGetStatic.class);
51        addBytecodeClass(Bytecodes._goto, BytecodeGoto.class);
52        addBytecodeClass(Bytecodes._goto_w, BytecodeGotoW.class);
53        addBytecodeClass(Bytecodes._ifeq, BytecodeIf.class);
54        addBytecodeClass(Bytecodes._ifne, BytecodeIf.class);
55        addBytecodeClass(Bytecodes._iflt, BytecodeIf.class);
56        addBytecodeClass(Bytecodes._ifge, BytecodeIf.class);
57        addBytecodeClass(Bytecodes._ifgt, BytecodeIf.class);
58        addBytecodeClass(Bytecodes._ifle, BytecodeIf.class);
59        addBytecodeClass(Bytecodes._if_icmpeq, BytecodeIf.class);
60        addBytecodeClass(Bytecodes._if_icmpne, BytecodeIf.class);
61        addBytecodeClass(Bytecodes._if_icmplt, BytecodeIf.class);
62        addBytecodeClass(Bytecodes._if_icmpge, BytecodeIf.class);
63        addBytecodeClass(Bytecodes._if_icmpgt, BytecodeIf.class);
64        addBytecodeClass(Bytecodes._if_icmple, BytecodeIf.class);
65        addBytecodeClass(Bytecodes._if_acmpeq, BytecodeIf.class);
66        addBytecodeClass(Bytecodes._if_acmpne, BytecodeIf.class);
67        addBytecodeClass(Bytecodes._ifnull, BytecodeIf.class);
68        addBytecodeClass(Bytecodes._ifnonnull, BytecodeIf.class);
69        addBytecodeClass(Bytecodes._iinc, BytecodeIinc.class);
70        addBytecodeClass(Bytecodes._instanceof, BytecodeInstanceOf.class);
71        addBytecodeClass(Bytecodes._invokevirtual, BytecodeInvoke.class);
72        addBytecodeClass(Bytecodes._invokestatic, BytecodeInvoke.class);
73        addBytecodeClass(Bytecodes._invokespecial, BytecodeInvoke.class);
74        addBytecodeClass(Bytecodes._invokeinterface, BytecodeInvoke.class);
75        addBytecodeClass(Bytecodes._invokedynamic, BytecodeInvoke.class);
76        addBytecodeClass(Bytecodes._jsr, BytecodeJsr.class);
77        addBytecodeClass(Bytecodes._jsr_w, BytecodeJsrW.class);
78        addBytecodeClass(Bytecodes._iload, BytecodeLoad.class);
79        addBytecodeClass(Bytecodes._lload, BytecodeLoad.class);
80        addBytecodeClass(Bytecodes._fload, BytecodeLoad.class);
81        addBytecodeClass(Bytecodes._dload, BytecodeLoad.class);
82        addBytecodeClass(Bytecodes._aload, BytecodeLoad.class);
83        addBytecodeClass(Bytecodes._ldc,   BytecodeLoadConstant.class);
84        addBytecodeClass(Bytecodes._ldc_w, BytecodeLoadConstant.class);
85        addBytecodeClass(Bytecodes._ldc2_w, BytecodeLoadConstant.class);
86        addBytecodeClass(Bytecodes._lookupswitch, BytecodeLookupswitch.class);
87        addBytecodeClass(Bytecodes._multianewarray, BytecodeMultiANewArray.class);
88        addBytecodeClass(Bytecodes._new, BytecodeNew.class);
89        addBytecodeClass(Bytecodes._newarray, BytecodeNewArray.class);
90        addBytecodeClass(Bytecodes._putfield, BytecodePutField.class);
91        addBytecodeClass(Bytecodes._putstatic, BytecodePutStatic.class);
92        addBytecodeClass(Bytecodes._ret, BytecodeRet.class);
93        addBytecodeClass(Bytecodes._sipush, BytecodeSipush.class);
94        addBytecodeClass(Bytecodes._istore, BytecodeStore.class);
95        addBytecodeClass(Bytecodes._lstore, BytecodeStore.class);
96        addBytecodeClass(Bytecodes._fstore, BytecodeStore.class);
97        addBytecodeClass(Bytecodes._dstore, BytecodeStore.class);
98        addBytecodeClass(Bytecodes._astore, BytecodeStore.class);
99        addBytecodeClass(Bytecodes._tableswitch, BytecodeTableswitch.class);
100    }
101 
102    public BytecodeDisassembler(Method method) {
103       this.method = method;
104    }
105 
106    public Method getMethod() {
107       return method;
108    }
109 
110    public void decode(BytecodeVisitor visitor) {
111       visitor.prologue(method);
112 
113       BytecodeStream stream = new BytecodeStream(method);
114       int javacode = Bytecodes._illegal;
115       while ( (javacode = stream.next()) != Bytecodes._illegal) {
116          // look for special Bytecode class
117          int bci = stream.bci();
118          int hotspotcode = method.getBytecodeOrBPAt(bci);
119          Class clazz = getBytecodeClass(javacode);
120          if (clazz == null) {
121             // check for fast_(i|a)_access_0
122             clazz = getBytecodeClass(hotspotcode);
123             if (clazz == null) {
124                // use generic bytecode class
125                clazz = Bytecode.class;
126             }
127          }
128 
129          // All bytecode classes must have a constructor with signature
130          // (Lsun/jvm/hotspot/oops/Method;I)V
131 
132          Constructor cstr = null;
133          try {
134             cstr = clazz.getDeclaredConstructor(new Class[] { Method.class, Integer.TYPE });
135          } catch(NoSuchMethodException nomethod) {
136             if (Assert.ASSERTS_ENABLED) {
137                Assert.that(false, "Bytecode class without proper constructor!");
138             }
139          }
140 
141          Bytecode bytecodeObj = null;
142          try {
143             bytecodeObj = (Bytecode)cstr.newInstance(new Object[] { method, new Integer(bci) });
144          } catch (Exception exp) {
145             if (Assert.ASSERTS_ENABLED) {
146                Assert.that(false, "Bytecode instance of class "
147                            + clazz.getName() + " can not be created!");
148             }
149          }
150 
151          if (stream.isWide()) {
152             visitor.visit(new Bytecode(method, bci - 1));
153          }
154 
155          try {
156             visitor.visit(bytecodeObj);
157          } catch(ClassCastException castfail) {
158              castfail.printStackTrace();
159              System.err.println(method.getAddress() + " " + bci);
160          }
161       }
162 
163       visitor.epilogue();
164    }
165 }