comparison agent/src/share/classes/sun/jvm/hotspot/interpreter/BytecodeDisassembler.java @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children bc32f286fae0
comparison
equal deleted inserted replaced
-1:000000000000 0:a61af66fc99e
1 /*
2 * Copyright 2002 Sun Microsystems, Inc. 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 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 // only special fast_xxx cases. others are handled differently.
101 addBytecodeClass(Bytecodes._fast_iaccess_0, BytecodeFastAAccess0.class);
102 addBytecodeClass(Bytecodes._fast_aaccess_0, BytecodeFastIAccess0.class);
103 }
104
105 public BytecodeDisassembler(Method method) {
106 this.method = method;
107 }
108
109 public Method getMethod() {
110 return method;
111 }
112
113 public void decode(BytecodeVisitor visitor) {
114 visitor.prologue(method);
115
116 BytecodeStream stream = new BytecodeStream(method);
117 int javacode = Bytecodes._illegal;
118 while ( (javacode = stream.next()) != Bytecodes._illegal) {
119 // look for special Bytecode class
120 int bci = stream.bci();
121 int hotspotcode = method.getBytecodeOrBPAt(bci);
122 Class clazz = getBytecodeClass(javacode);
123 if (clazz == null) {
124 // check for fast_(i|a)_access_0
125 clazz = getBytecodeClass(hotspotcode);
126 if (clazz == null) {
127 // use generic bytecode class
128 clazz = Bytecode.class;
129 }
130 }
131
132 // All bytecode classes must have a constructor with signature
133 // (Lsun/jvm/hotspot/oops/Method;I)V
134
135 Constructor cstr = null;
136 try {
137 cstr = clazz.getDeclaredConstructor(new Class[] { Method.class, Integer.TYPE });
138 } catch(NoSuchMethodException nomethod) {
139 if (Assert.ASSERTS_ENABLED) {
140 Assert.that(false, "Bytecode class without proper constructor!");
141 }
142 }
143
144 Object bytecodeObj = null;
145 try {
146 bytecodeObj = cstr.newInstance(new Object[] { method, new Integer(bci) });
147 } catch (Exception exp) {
148 if (Assert.ASSERTS_ENABLED) {
149 Assert.that(false, "Bytecode instance of class "
150 + clazz.getName() + " can not be created!");
151 }
152 }
153
154 if (stream.isWide()) {
155 visitor.visit(new Bytecode(method, bci - 1));
156 }
157
158 try {
159 visitor.visit((Bytecode) bytecodeObj);
160 } catch(ClassCastException castfail) {
161 if (Assert.ASSERTS_ENABLED) {
162 Assert.that(false, clazz.getName() + " is not derived from Bytecode!");
163 }
164 }
165 }
166
167 visitor.epilogue();
168 }
169 }