Source code: com/chaoswg/xtc4y/classdesc/code/instructions/InstructionDisassembler.java
1 //$Header: /cvsroot/xtc4y/xtc4y/src/com/chaoswg/xtc4y/classdesc/code/instructions/InstructionDisassembler.java,v 1.2 2003/08/26 12:35:25 toggm Exp $
2 /******************************************************************************
3 * XTC4y - eXtreme Testing Collection 4 you *
4 * -------------------------------------------------------------------------- *
5 * URL: http://www.chaoswg.com/xtc4y *
6 * Author: Mike Toggweiler (2.dog@gmx.ch) *
7 * *
8 * Last Updated: $Date: 2003/08/26 12:35:25 $, by $Author: toggm $ *
9 * Version: $Revision: 1.2 $ *
10 * -------------------------------------------------------------------------- *
11 * COPYRIGHT: (c) 2003 by Mike Toggweiler *
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 *****************************************************************************/
18 package com.chaoswg.xtc4y.classdesc.code.instructions;
19
20 import java.io.DataInputStream;
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23
24 import com.chaoswg.xtc4y.classdesc.ConstantPool;
25
26 /**
27 * This class just disassembles opcodes to instructions
28 * @author Mike Toggweiler
29 **/
30 public final class InstructionDisassembler {
31 public static final byte UNUSED = (byte)186;
32
33 /**
34 * Creates a new Instruction object depending on the opcode.
35 * @param dis DataInputStream to initialize the instruction
36 * @param cp the ConstantPool to resolve indices
37 **/
38 public static Instruction fetchInstruction(DataInputStream dis,
39 ConstantPool cp)
40 throws IOException {
41 byte opcode = (byte)dis.readUnsignedByte();
42 switch (opcode) {
43 //unary instructions
44 case UnaryInstruction.NOP:
45 case UnaryInstruction.ACONST_NULL:
46 case UnaryInstruction.ICONST_M1:
47 case UnaryInstruction.ICONST_0:
48 case UnaryInstruction.ICONST_1:
49 case UnaryInstruction.ICONST_2:
50 case UnaryInstruction.ICONST_3:
51 case UnaryInstruction.ICONST_4:
52 case UnaryInstruction.ICONST_5:
53 case UnaryInstruction.LCONST_0:
54 case UnaryInstruction.LCONST_1:
55 case UnaryInstruction.FCONST_0:
56 case UnaryInstruction.FCONST_1:
57 case UnaryInstruction.FCONST_2:
58 case UnaryInstruction.DCONST_0:
59 case UnaryInstruction.DCONST_1:
60 case UnaryInstruction.ILOAD_0:
61 case UnaryInstruction.ILOAD_1:
62 case UnaryInstruction.ILOAD_2:
63 case UnaryInstruction.ILOAD_3:
64 case UnaryInstruction.LLOAD_0:
65 case UnaryInstruction.LLOAD_1:
66 case UnaryInstruction.LLOAD_2:
67 case UnaryInstruction.LLOAD_3:
68 case UnaryInstruction.FLOAD_0:
69 case UnaryInstruction.FLOAD_1:
70 case UnaryInstruction.FLOAD_2:
71 case UnaryInstruction.FLOAD_3:
72 case UnaryInstruction.DLOAD_0:
73 case UnaryInstruction.DLOAD_1:
74 case UnaryInstruction.DLOAD_2:
75 case UnaryInstruction.DLOAD_3:
76 case UnaryInstruction.ALOAD_0:
77 case UnaryInstruction.ALOAD_1:
78 case UnaryInstruction.ALOAD_2:
79 case UnaryInstruction.ALOAD_3:
80 case UnaryInstruction.IALOAD:
81 case UnaryInstruction.LALOAD:
82 case UnaryInstruction.FALOAD:
83 case UnaryInstruction.DALOAD:
84 case UnaryInstruction.AALOAD:
85 case UnaryInstruction.BALOAD:
86 case UnaryInstruction.CALOAD:
87 case UnaryInstruction.SALOAD:
88 case UnaryInstruction.ISTORE_0:
89 case UnaryInstruction.ISTORE_1:
90 case UnaryInstruction.ISTORE_2:
91 case UnaryInstruction.ISTORE_3:
92 case UnaryInstruction.LSTORE_0:
93 case UnaryInstruction.LSTORE_1:
94 case UnaryInstruction.LSTORE_2:
95 case UnaryInstruction.LSTORE_3:
96 case UnaryInstruction.FSTORE_0:
97 case UnaryInstruction.FSTORE_1:
98 case UnaryInstruction.FSTORE_2:
99 case UnaryInstruction.FSTORE_3:
100 case UnaryInstruction.DSTORE_0:
101 case UnaryInstruction.DSTORE_1:
102 case UnaryInstruction.DSTORE_2:
103 case UnaryInstruction.DSTORE_3:
104 case UnaryInstruction.ASTORE_0:
105 case UnaryInstruction.ASTORE_1:
106 case UnaryInstruction.ASTORE_2:
107 case UnaryInstruction.ASTORE_3:
108 case UnaryInstruction.IASTORE:
109 case UnaryInstruction.LASTORE:
110 case UnaryInstruction.FASTORE:
111 case UnaryInstruction.DASTORE:
112 case UnaryInstruction.AASTORE:
113 case UnaryInstruction.BASTORE:
114 case UnaryInstruction.CASTORE:
115 case UnaryInstruction.SASTORE:
116 case UnaryInstruction.POP:
117 case UnaryInstruction.POP2:
118 case UnaryInstruction.DUP:
119 case UnaryInstruction.DUP_X1:
120 case UnaryInstruction.DUP_X2:
121 case UnaryInstruction.DUP2:
122 case UnaryInstruction.DUP2_X1:
123 case UnaryInstruction.DUP2_X2:
124 case UnaryInstruction.SWAP:
125 case UnaryInstruction.IADD:
126 case UnaryInstruction.LADD:
127 case UnaryInstruction.FADD:
128 case UnaryInstruction.DADD:
129 case UnaryInstruction.ISUB:
130 case UnaryInstruction.LSUB:
131 case UnaryInstruction.FSUB:
132 case UnaryInstruction.DSUB:
133 case UnaryInstruction.IMUL:
134 case UnaryInstruction.LMUL:
135 case UnaryInstruction.FMUL:
136 case UnaryInstruction.DMUL:
137 case UnaryInstruction.IREM:
138 case UnaryInstruction.LREM:
139 case UnaryInstruction.FREM:
140 case UnaryInstruction.DREM:
141 case UnaryInstruction.INEG:
142 case UnaryInstruction.LNEG:
143 case UnaryInstruction.FNEG:
144 case UnaryInstruction.DNEG:
145 case UnaryInstruction.ISHL:
146 case UnaryInstruction.LSHL:
147 case UnaryInstruction.ISHR:
148 case UnaryInstruction.LSHR:
149 case UnaryInstruction.IUSHR:
150 case UnaryInstruction.LUSHR:
151 case UnaryInstruction.IAND:
152 case UnaryInstruction.LAND:
153 case UnaryInstruction.IOR:
154 case UnaryInstruction.LOR:
155 case UnaryInstruction.IXOR:
156 case UnaryInstruction.LXOR:
157 case UnaryInstruction.I2L:
158 case UnaryInstruction.I2F:
159 case UnaryInstruction.I2D:
160 case UnaryInstruction.L2I:
161 case UnaryInstruction.L2F:
162 case UnaryInstruction.L2D:
163 case UnaryInstruction.F2I:
164 case UnaryInstruction.F2L:
165 case UnaryInstruction.F2D:
166 case UnaryInstruction.D2I:
167 case UnaryInstruction.D2L:
168 case UnaryInstruction.D2F:
169 case UnaryInstruction.I2B:
170 case UnaryInstruction.I2C:
171 case UnaryInstruction.I2S:
172 case UnaryInstruction.LCMP:
173 case UnaryInstruction.FCMPL:
174 case UnaryInstruction.FCMPG:
175 case UnaryInstruction.DCMPL:
176 case UnaryInstruction.DCMPG:
177 case UnaryInstruction.IRETURN:
178 case UnaryInstruction.LRETURN:
179 case UnaryInstruction.FRETURN:
180 case UnaryInstruction.DRETURN:
181 case UnaryInstruction.ARETURN:
182 case UnaryInstruction.RETURN:
183 case UnaryInstruction.ARRAYLENGTH:
184 case UnaryInstruction.ATHROW:
185 case UnaryInstruction.MONITORENTER:
186 case UnaryInstruction.MONITOREXIT:
187 case UnaryInstruction.BREAKPOINT:
188 case UnaryInstruction.IMPDEP1:
189 case UnaryInstruction.IMPDEP2:
190 return new UnaryInstruction(opcode);
191
192 //default byte binary instructions
193 case BinaryInstruction.BIPUSH:
194 case BinaryInstruction.ILOAD:
195 case BinaryInstruction.LLOAD:
196 case BinaryInstruction.FLOAD:
197 case BinaryInstruction.DLOAD:
198 case BinaryInstruction.ALOAD:
199 case BinaryInstruction.ISTORE:
200 case BinaryInstruction.LSTORE:
201 case BinaryInstruction.FSTORE:
202 case BinaryInstruction.DSTORE:
203 case BinaryInstruction.ASTORE:
204 case BinaryInstruction.RET:
205 return new BinaryInstruction(opcode, dis, cp);
206 //default short binary instructions
207 case WideBinaryInstruction.SIPUSH:
208 case WideBinaryInstruction.NEWARRAY:
209 return new WideBinaryInstruction(opcode, dis, cp);
210 //short cpref instructions
211 case ShortCPRefInstruction.LDC:
212 return new ShortCPRefInstruction(opcode, dis, cp);
213 //constant pool reference short binary instructions
214 case CPRefInstruction.LDC_W:
215 case CPRefInstruction.LDC2_W:
216 case CPRefInstruction.GETSTATIC:
217 case CPRefInstruction.PUTSTATIC:
218 case CPRefInstruction.GETFIELD:
219 case CPRefInstruction.PUTFIELD:
220 case CPRefInstruction.INVOKEVIRTUAL:
221 case CPRefInstruction.INVOKESPECIAL:
222 case CPRefInstruction.INVOKESTATIC:
223 case CPRefInstruction.NEW:
224 case CPRefInstruction.ANEWARRAY:
225 case CPRefInstruction.CHECKCAST:
226 case CPRefInstruction.INSTANCEOF:
227 return new CPRefInstruction(opcode, dis, cp);
228
229 //default trinary byte instructions
230 case TrinaryInstruction.IINC:
231 return new TrinaryInstruction(opcode, dis, cp);
232 //jump instructions
233 case JumpInstruction.IFEQ:
234 case JumpInstruction.IFNE:
235 case JumpInstruction.IFLT:
236 case JumpInstruction.IFGE:
237 case JumpInstruction.IFGT:
238 case JumpInstruction.IFLE:
239 case JumpInstruction.IF_ICMPEQ:
240 case JumpInstruction.IF_ICMPNE:
241 case JumpInstruction.IF_ICMPLT:
242 case JumpInstruction.IF_ICMPGE:
243 case JumpInstruction.IF_ICMPGT:
244 case JumpInstruction.IF_ICMPLE:
245 case JumpInstruction.IF_ACMPEQ:
246 case JumpInstruction.IF_ACMPNE:
247 case JumpInstruction.GOTO:
248 case JumpInstruction.JSR:
249 case JumpInstruction.IFNULL:
250 case JumpInstruction.IFNONNULL:
251 case JumpInstruction.GOTO_W:
252 case JumpInstruction.JSR_W:
253 return new JumpInstruction(opcode, dis, cp);
254
255 //tableswitch
256 case TableSwitchInstruction.TABLESWITCH:
257 return new TableSwitchInstruction(dis, cp);
258 //lookupswitch
259 case LookupSwitchInstruction.LOOKUPSWITCH:
260 return new LookupSwitchInstruction(dis, cp);
261 //InvokeInterface
262 case InvokeInterface.INVOKEINTERFACE:
263 return new InvokeInterface(dis, cp);
264 //MultiANewArray
265 case MultiANewArray.MULTIANEWARRAY:
266 return new MultiANewArray(dis, cp);
267 case UNUSED:
268 //not used for historical reasons
269 throw new ClassFormatError("Instruction shouldn't be used");
270 default:
271 //throw new MethodNotSupportedException("Instruction not yet"+
272 // " supported");
273 throw new RuntimeException("Instruction not yet supported:"+
274 ((opcode<0)?256+opcode:opcode));
275 }
276 //return null;
277 }
278 }