在深入分析ClassReader.readCode()方法之前,我们需要大概了解下ASM把jvm的指令分的类型,这在readCode会用到。先看ClassReader的代码:
static {
int i;
byte[] b = new byte[220];
String s = "AAAAAAAAAAAAAAAABCLMMDDDDDEEEEEEEEEEEEEEEEEEEEAAAAAAAADD"
+ "DDDEEEEEEEEEEEEEEEEEEEEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
+ "AAAAAAAAAAAAAAAAANAAAAAAAAAAAAAAAAAAAAJJJJJJJJJJJJJJJJDOPAA"
+ "AAAAGGGGGGGHIFBFAAFFAARQJJKKJJJJJJJJJJJJJJJJJJ";
for (i = 0; i < b.length; ++i) {
b[i] = (byte) (s.charAt(i) - 'A');
}
TYPE = b;
}
这其中的TYPE是一个字节数组,索引是jvm指令的byte值,值从0开始,分了16类,简化后是:
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 2,11,12,
12,3, 3, 3, 3, 3, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 0, 0, 0, 0,
0, 0, 0, 0, 3, 3, 3, 3, 3, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0,13, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 3,
14,15,0, 0, 0, 0, 0, 0, 6, 6,
6, 6, 6, 6, 6, 7, 8, 5, 1, 5,
0, 0, 5, 5, 0, 0, 17,16,9, 9,
10,10, 9, 9, 9, 9, 9,9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9
补全下,指令跟指令类型的对照表:
指令操作码 16进制 指令类型 指令
##类型为0的指令没有操作数
0 = 0x0 = 0 ...............nop
1 = 0x1 = 0 ...............aconst_null
2 = 0x2 = 0 ...............iconst_m1
3 = 0x3 = 0 ...............iconst_0
4 = 0x4 = 0 ...............iconst_1
5 = 0x5 = 0 ...............iconst_2
6 = 0x6 = 0 ...............iconst_3
7 = 0x7 = 0 ...............iconst_4
8 = 0x8 = 0 ...............iconst_5
9 = 0x9 = 0 ...............lconst_0
10 = 0xa = 0 ...............lconst_1
11 = 0xb = 0 ...............fconst_0
12 = 0xc = 0 ...............fconst_1
13 = 0xd = 0 ...............fconst_2
14 = 0xe = 0 ...............dconst_0
15 = 0xf = 0 ...............dconst_1
##类型为1的指令操作数长度为1个字节
16 = 0x10 = 1 ...............bipush
##类型为2的指令操作数长度为2个字节
17 = 0x11 = 2 ...............sipush
18 = 0x12 = 11 ...............ldc
19 = 0x13 = 12 ...............ldc_w
20 = 0x14 = 12 ...............ldc2_w
##类型为3的指令操作数长度为1个字节
21 = 0x15 = 3 ...............iload
22 = 0x16 = 3 ...............lload
23 = 0x17 = 3 ...............fload
24 = 0x18 = 3 ...............dload
25 = 0x19 = 3 ...............aload
##类型为4的指令操作数长度为1个字节
26 = 0x1a = 4 ...............iload_0
27 = 0x1b = 4 ...............iload_1
28 = 0x1c = 4 ...............iload_2
29 = 0x1d = 4 ...............iload_3
30 = 0x1e = 4 ...............lload_0
31 = 0x1f = 4 ...............lload_1
32 = 0x20 = 4 ...............lload_2
33 = 0x21 = 4 ...............lload_3
34 = 0x22 = 4 ...............fload_0
35 = 0x23 = 4 ...............fload_1
36 = 0x24 = 4 ...............fload_2
37 = 0x25 = 4 ...............fload_3
38 = 0x26 = 4 ...............dload_0
39 = 0x27 = 4 ...............dload_1
40 = 0x28 = 4 ...............dload_2
41 = 0x29 = 4 ...............dload_3
42 = 0x2a = 4 ...............aload_0
43 = 0x2b = 4 ...............aload_1
44 = 0x2c = 4 ...............aload_2
45 = 0x2d = 4 ...............aload_3
46 = 0x2e = 0 ...............iaload
47 = 0x2f = 0 ...............laload
48 = 0x30 = 0 ...............faload
49 = 0x31 = 0 ...............daload
50 = 0x32 = 0 ...............aaload
51 = 0x33 = 0 ...............baload
52 = 0x34 = 0 ...............caload
53 = 0x35 = 0 ...............saload
54 = 0x36 = 3 ...............istore
55 = 0x37 = 3 ...............lstore
56 = 0x38 = 3 ...............fstore
57 = 0x39 = 3 ...............dstore
58 = 0x3a = 3 ...............astore
59 = 0x3b = 4 ...............istore_0
60 = 0x3c = 4 ...............istore_1
61 = 0x3d = 4 ...............istore_2
62 = 0x3e = 4 ...............istore_3
63 = 0x3f = 4 ...............lstore_0
64 = 0x40 = 4 ...............lstore_1
65 = 0x41 = 4 ...............lstore_2
66 = 0x42 = 4 ...............lstore_3
67 = 0x43 = 4 ...............fstore_0
68 = 0x44 = 4 ...............fstore_1
69 = 0x45 = 4 ...............fstore_2
70 = 0x46 = 4 ...............fstore_3
71 = 0x47 = 4 ...............dstore_0
72 = 0x48 = 4 ...............dstore_1
73 = 0x49 = 4 ...............dstore_2
74 = 0x4a = 4 ...............dstore_3
75 = 0x4b = 4 ...............astore_0
76 = 0x4c = 4 ...............astore_1
77 = 0x4d = 4 ...............astore_2
78 = 0x4e = 4 ...............astore_3
79 = 0x4f = 0 ...............iastore
80 = 0x50 = 0 ...............lastore
81 = 0x51 = 0 ...............fastore
82 = 0x52 = 0 ...............dastore
83 = 0x53 = 0 ...............aastore
84 = 0x54 = 0 ...............bastore
85 = 0x55 = 0 ...............castore
86 = 0x56 = 0 ...............sastore
87 = 0x57 = 0 ...............pop
88 = 0x58 = 0 ...............pop2
89 = 0x59 = 0 ...............dup
90 = 0x5a = 0 ...............dup_x1
91 = 0x5b = 0 ...............dup_x2
92 = 0x5c = 0 ...............dup2
93 = 0x5d = 0 ...............dup2_x1
94 = 0x5e = 0 ...............dup2_x2
95 = 0x5f = 0 ...............swap
96 = 0x60 = 0 ...............iadd
97 = 0x61 = 0 ...............ladd
98 = 0x62 = 0 ...............fadd
99 = 0x63 = 0 ...............dadd
100 = 0x64 = 0 ...............isub
101 = 0x65 = 0 ...............lsub
102 = 0x66 = 0 ...............fsub
103 = 0x67 = 0 ...............dsub
104 = 0x68 = 0 ...............imul
105 = 0x69 = 0 ...............lmul
106 = 0x6a = 0 ...............fmul
107 = 0x6b = 0 ...............dmul
108 = 0x6c = 0 ...............idiv
109 = 0x6d = 0 ...............ldiv
110 = 0x6e = 0 ...............fdiv
111 = 0x6f = 0 ...............ddiv
112 = 0x70 = 0 ...............irem
113 = 0x71 = 0 ...............lrem
114 = 0x72 = 0 ...............frem
115 = 0x73 = 0 ...............drem
116 = 0x74 = 0 ...............ineg
117 = 0x75 = 0 ...............lneg
118 = 0x76 = 0 ...............fneg
119 = 0x77 = 0 ...............dneg
120 = 0x78 = 0 ...............ishl
121 = 0x79 = 0 ...............lshl
122 = 0x7a = 0 ...............ishr
123 = 0x7b = 0 ...............lshr
124 = 0x7c = 0 ...............iushr
125 = 0x7d = 0 ...............lushr
126 = 0x7e = 0 ...............iand
127 = 0x7f = 0 ...............land
128 = 0x80 = 0 ...............ior
129 = 0x81 = 0 ...............lor
130 = 0x82 = 0 ...............ixor
131 = 0x83 = 0 ...............lxor
132 = 0x84 = 13 ...............iinc
133 = 0x85 = 0 ...............i2l
134 = 0x86 = 0 ...............i2f
135 = 0x87 = 0 ...............i2d
136 = 0x88 = 0 ...............l2i
137 = 0x89 = 0 ...............l2f
138 = 0x8a = 0 ...............l2d
139 = 0x8b = 0 ...............f2i
140 = 0x8c = 0 ...............f2l
141 = 0x8d = 0 ...............f2d
142 = 0x8e = 0 ...............d2i
143 = 0x8f = 0 ...............d2l
144 = 0x90 = 0 ...............d2f
145 = 0x91 = 0 ...............i2b
146 = 0x92 = 0 ...............i2c
147 = 0x93 = 0 ...............i2s
148 = 0x94 = 0 ...............lcmp
149 = 0x95 = 0 ...............fcmpl
150 = 0x96 = 0 ...............fcmpg
151 = 0x97 = 0 ...............dcmpl
152 = 0x98 = 0 ...............dcmpg
#类型为9的指令,主要是if、goto、jsr三大类指令
153 = 0x99 = 9 ...............ifeq
154 = 0x9a = 9 ...............ifne
155 = 0x9b = 9 ...............iflt
156 = 0x9c = 9 ...............ifge
157 = 0x9d = 9 ...............ifgt
158 = 0x9e = 9 ...............ifle
159 = 0x9f = 9 ...............if_icmpeq
160 = 0xa0 = 9 ...............if_icmpne
161 = 0xa1 = 9 ...............if_icmplt
162 = 0xa2 = 9 ...............if_icmpge
163 = 0xa3 = 9 ...............if_icmpgt
164 = 0xa4 = 9 ...............if_icmple
165 = 0xa5 = 9 ...............if_acmpeq
166 = 0xa6 = 9 ...............if_acmpne
167 = 0xa7 = 9 ...............goto
168 = 0xa8 = 9 ...............jsr
169 = 0xa9 = 3 ...............ret
#14 tableswitch 对齐变长,high(4) / low(4) / offsets[(high-low+1)*4]
170 = 0xaa = 14 ...............tableswitch
#15 lookupswitch 对齐变长 default(4) / npairs(4) / match(4) - offset(4) pairs
171 = 0xab = 15 ...............lookupswitch
172 = 0xac = 0 ...............ireturn
173 = 0xad = 0 ...............lreturn
174 = 0xae = 0 ...............freturn
175 = 0xaf = 0 ...............dreturn
176 = 0xb0 = 0 ...............areturn
177 = 0xb1 = 0 ...............return
178 = 0xb2 = 6 ...............getstatic
179 = 0xb3 = 6 ...............putstatic
180 = 0xb4 = 6 ...............getfield
181 = 0xb5 = 6 ...............putfield
182 = 0xb6 = 6 ...............invokevirtual
183 = 0xb7 = 6 ...............invokespecial
184 = 0xb8 = 6 ...............invokestatic
185 = 0xb9 = 7 ...............invokeinterface
186 = 0xba = 8 ...............invokedynamic
187 = 0xbb = 5 ...............new
188 = 0xbc = 1 ...............newarray
189 = 0xbd = 5 ...............anewarray
190 = 0xbe = 0 ...............arraylength
191 = 0xbf = 0 ...............athrow
192 = 0xc0 = 5 ...............checkcast
193 = 0xc1 = 5 ...............instanceof
194 = 0xc2 = 0 ...............monitorenter
195 = 0xc3 = 0 ...............monitorexit
196 = 0xc4 = 17 ...............wide
197 = 0xc5 = 16 ...............multianewarray
198 = 0xc6 = 9 ...............ifnull
199 = 0xc7 = 9 ...............ifnonnull
#goto、jsr扩展指令
200 = 0xc8 = 10 ...............goto_w
201 = 0xc9 = 10 ...............jsr_w
202 = 0xca = 9 Reserved opcodes:
203 = 0xcb = 9 ...............breakpoint
204 = 0xcc = 9 ...............impdep1
205 = 0xcd = 9 ...............impdep2
206 = 0xce = 9
207 = 0xcf = 9
208 = 0xd0 = 9
209 = 0xd1 = 9
210 = 0xd2 = 9
211 = 0xd3 = 9
212 = 0xd4 = 9
213 = 0xd5 = 9
214 = 0xd6 = 9
215 = 0xd7 = 9
216 = 0xd8 = 9
217 = 0xd9 = 9
218 = 0xda = 9
219 = 0xdb = 9