0
|
1 /*
|
1385
|
2 * Copyright 2002-2010 Sun Microsystems, Inc. All Rights Reserved.
|
0
|
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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 * have any 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._jsr, BytecodeJsr.class);
|
|
76 addBytecodeClass(Bytecodes._jsr_w, BytecodeJsrW.class);
|
|
77 addBytecodeClass(Bytecodes._iload, BytecodeLoad.class);
|
|
78 addBytecodeClass(Bytecodes._lload, BytecodeLoad.class);
|
|
79 addBytecodeClass(Bytecodes._fload, BytecodeLoad.class);
|
|
80 addBytecodeClass(Bytecodes._dload, BytecodeLoad.class);
|
|
81 addBytecodeClass(Bytecodes._aload, BytecodeLoad.class);
|
|
82 addBytecodeClass(Bytecodes._ldc, BytecodeLoadConstant.class);
|
|
83 addBytecodeClass(Bytecodes._ldc_w, BytecodeLoadConstant.class);
|
|
84 addBytecodeClass(Bytecodes._ldc2_w, BytecodeLoadConstant.class);
|
|
85 addBytecodeClass(Bytecodes._lookupswitch, BytecodeLookupswitch.class);
|
|
86 addBytecodeClass(Bytecodes._multianewarray, BytecodeMultiANewArray.class);
|
|
87 addBytecodeClass(Bytecodes._new, BytecodeNew.class);
|
|
88 addBytecodeClass(Bytecodes._newarray, BytecodeNewArray.class);
|
|
89 addBytecodeClass(Bytecodes._putfield, BytecodePutField.class);
|
|
90 addBytecodeClass(Bytecodes._putstatic, BytecodePutStatic.class);
|
|
91 addBytecodeClass(Bytecodes._ret, BytecodeRet.class);
|
|
92 addBytecodeClass(Bytecodes._sipush, BytecodeSipush.class);
|
|
93 addBytecodeClass(Bytecodes._istore, BytecodeStore.class);
|
|
94 addBytecodeClass(Bytecodes._lstore, BytecodeStore.class);
|
|
95 addBytecodeClass(Bytecodes._fstore, BytecodeStore.class);
|
|
96 addBytecodeClass(Bytecodes._dstore, BytecodeStore.class);
|
|
97 addBytecodeClass(Bytecodes._astore, BytecodeStore.class);
|
|
98 addBytecodeClass(Bytecodes._tableswitch, BytecodeTableswitch.class);
|
|
99 }
|
|
100
|
|
101 public BytecodeDisassembler(Method method) {
|
|
102 this.method = method;
|
|
103 }
|
|
104
|
|
105 public Method getMethod() {
|
|
106 return method;
|
|
107 }
|
|
108
|
|
109 public void decode(BytecodeVisitor visitor) {
|
|
110 visitor.prologue(method);
|
|
111
|
|
112 BytecodeStream stream = new BytecodeStream(method);
|
|
113 int javacode = Bytecodes._illegal;
|
|
114 while ( (javacode = stream.next()) != Bytecodes._illegal) {
|
|
115 // look for special Bytecode class
|
|
116 int bci = stream.bci();
|
|
117 int hotspotcode = method.getBytecodeOrBPAt(bci);
|
|
118 Class clazz = getBytecodeClass(javacode);
|
|
119 if (clazz == null) {
|
|
120 // check for fast_(i|a)_access_0
|
|
121 clazz = getBytecodeClass(hotspotcode);
|
|
122 if (clazz == null) {
|
|
123 // use generic bytecode class
|
|
124 clazz = Bytecode.class;
|
|
125 }
|
|
126 }
|
|
127
|
|
128 // All bytecode classes must have a constructor with signature
|
|
129 // (Lsun/jvm/hotspot/oops/Method;I)V
|
|
130
|
|
131 Constructor cstr = null;
|
|
132 try {
|
|
133 cstr = clazz.getDeclaredConstructor(new Class[] { Method.class, Integer.TYPE });
|
|
134 } catch(NoSuchMethodException nomethod) {
|
|
135 if (Assert.ASSERTS_ENABLED) {
|
|
136 Assert.that(false, "Bytecode class without proper constructor!");
|
|
137 }
|
|
138 }
|
|
139
|
|
140 Object bytecodeObj = null;
|
|
141 try {
|
|
142 bytecodeObj = cstr.newInstance(new Object[] { method, new Integer(bci) });
|
|
143 } catch (Exception exp) {
|
|
144 if (Assert.ASSERTS_ENABLED) {
|
|
145 Assert.that(false, "Bytecode instance of class "
|
|
146 + clazz.getName() + " can not be created!");
|
|
147 }
|
|
148 }
|
|
149
|
|
150 if (stream.isWide()) {
|
|
151 visitor.visit(new Bytecode(method, bci - 1));
|
|
152 }
|
|
153
|
|
154 try {
|
|
155 visitor.visit((Bytecode) bytecodeObj);
|
|
156 } catch(ClassCastException castfail) {
|
|
157 if (Assert.ASSERTS_ENABLED) {
|
|
158 Assert.that(false, clazz.getName() + " is not derived from Bytecode!");
|
|
159 }
|
|
160 }
|
|
161 }
|
|
162
|
|
163 visitor.epilogue();
|
|
164 }
|
|
165 }
|