1use crate::structs::LookupSwitchPair;
2
3#[derive(Debug, Clone)]
4pub enum Instruction {
5 AALoad,
6 AAStore,
7 ALoad(u8),
8 ALoadW(u16),
9 ANewArray(u16),
10 ANull,
11 AReturn,
12 ArrayLength,
13 AStore(u8),
14 AStoreW(u16),
15 AThrow,
16 BALoad,
17 BAStore,
18 Bipush(u8),
19 CALoad,
20 CAStore,
21 CheckCast(u16),
22 D2F,
23 D2I,
24 D2L,
25 DAdd,
26 DALoad,
27 DAStore,
28 DCmpg,
29 DCmpl,
30 DConst(f64),
31 DDiv,
32 DLoad(u8),
33 DLoadW(u16),
34 DMul,
35 DNeg,
36 DRem,
37 DReturn,
38 DStore(u8),
39 DStoreW(u16),
40 DSub,
41 Dup,
42 Dup2,
43 Dup2X1,
44 Dup2X2,
45 DupX1,
46 DupX2,
47 F2D,
48 F2I,
49 F2L,
50 FAdd,
51 FALoad,
52 FAStore,
53 FCmpg,
54 FCmpl,
55 FConst(f32),
56 FDiv,
57 FLoad(u8),
58 FLoadW(u16),
59 FMul,
60 FNeg,
61 FRem,
62 FReturn,
63 FStore(u8),
64 FStoreW(u16),
65 FSub,
66 GetField(u16),
67 GetStatic(u16),
68 Goto(i16),
69 GotoW(u32),
70 I2B,
71 I2C,
72 I2D,
73 I2F,
74 I2L,
75 I2S,
76 IAdd,
77 IALoad,
78 IAnd,
79 IAStore,
80 IConst(i32),
81 IDiv,
82 IfAcmpeq(i16),
83 IfAcmpne(i16),
84 Ifeq(i16),
85 Ifge(i16),
86 Ifgt(i16),
87 IfIcmpeq(i16),
88 IfIcmpge(i16),
89 IfIcmpgt(i16),
90 IfIcmple(i16),
91 IfIcmplt(i16),
92 IfIcmpne(i16),
93 Ifle(i16),
94 Iflt(i16),
95 Ifne(i16),
96 IfNonNull(u16),
97 IfNull(u16),
98 IInc(u8, i8),
99 IIncW(u16, u16),
100 ILoad(u8),
101 ILoadW(u16),
102 IMul,
103 INeg,
104 InstanceOf(u16),
105 InvokeDynamic(u16),
106 InvokeInterface {
107 index: u16,
108 count: u8,
109 },
110 InvokeSpecial(u16),
111 InvokeStatic(u16),
112 InvokeVirtual(u16),
113 IOr,
114 IRem,
115 IReturn,
116 IShl,
117 IShr,
118 IStore(u8),
119 IStoreW(u16),
120 ISub,
121 IUShr,
122 IXor,
123 Jsr(i16),
124 JsrW(u32),
125 L2D,
126 L2F,
127 L2I,
128 LAdd,
129 LALoad,
130 LAnd,
131 LAStore,
132 LCmp,
133 LConst(i64),
134 Ldc(u8),
135 Ldc2W(u16),
136 LdcW(u16),
137 LDiv,
138 LLoad(u8),
139 LLoadW(u16),
140 LMul,
141 LNeg,
142 LookupSwitch {
143 padding: u32,
144 default: u32,
145 pairs: Vec<LookupSwitchPair>,
146 },
147 LOr,
148 LRem,
149 LReturn,
150 LShl,
151 LShr,
152 LStore(u8),
153 LStoreW(u16),
154 LSub,
155 LUShr,
156 LXor,
157 MonitorEnter,
158 MonitorExit,
159 MultiANewArray(u16, u8),
160 New(u16),
161 NewArray(u8),
162 Nop,
163 Pop,
164 Pop2,
165 PutField(u16),
166 PutStatic(u16),
167 Ret(u8),
168 RetW(u16),
169 Return,
170 SALoad,
171 SAStore,
172 Sipush(i16),
173 Swap,
174 TableSwitch {
175 padding: u32,
176 minimum: u32,
177 maximum: u32,
178 jump_targets: Vec<u32>,
179 default: u32,
180 },
181}
182
183impl Instruction {
184 pub fn size(&self) -> u32 {
185 match self {
186 Instruction::AALoad
187 | Instruction::AAStore
188 | Instruction::ANull
189 | Instruction::AReturn
190 | Instruction::ArrayLength
191 | Instruction::AThrow
192 | Instruction::BALoad
193 | Instruction::BAStore
194 | Instruction::CALoad
195 | Instruction::CAStore
196 | Instruction::D2F
197 | Instruction::D2I
198 | Instruction::D2L
199 | Instruction::DAdd
200 | Instruction::DALoad
201 | Instruction::DAStore
202 | Instruction::DCmpg
203 | Instruction::DCmpl
204 | Instruction::DConst(..)
205 | Instruction::DDiv
206 | Instruction::DMul
207 | Instruction::DNeg
208 | Instruction::DRem
209 | Instruction::DReturn
210 | Instruction::DSub
211 | Instruction::Dup
212 | Instruction::Dup2
213 | Instruction::Dup2X1
214 | Instruction::Dup2X2
215 | Instruction::DupX1
216 | Instruction::DupX2
217 | Instruction::F2D
218 | Instruction::F2I
219 | Instruction::F2L
220 | Instruction::FAdd
221 | Instruction::FALoad
222 | Instruction::FAStore
223 | Instruction::FCmpg
224 | Instruction::FCmpl
225 | Instruction::FConst(..)
226 | Instruction::FDiv
227 | Instruction::FMul
228 | Instruction::FNeg
229 | Instruction::FRem
230 | Instruction::FReturn
231 | Instruction::FSub
232 | Instruction::I2B
233 | Instruction::I2C
234 | Instruction::I2D
235 | Instruction::I2F
236 | Instruction::I2L
237 | Instruction::I2S
238 | Instruction::IAdd
239 | Instruction::IALoad
240 | Instruction::IAnd
241 | Instruction::IAStore
242 | Instruction::IConst(..)
243 | Instruction::IDiv
244 | Instruction::IMul
245 | Instruction::INeg
246 | Instruction::IOr
247 | Instruction::IRem
248 | Instruction::IReturn
249 | Instruction::IShl
250 | Instruction::IShr
251 | Instruction::ISub
252 | Instruction::IUShr
253 | Instruction::IXor
254 | Instruction::L2D
255 | Instruction::L2F
256 | Instruction::L2I
257 | Instruction::LAdd
258 | Instruction::LALoad
259 | Instruction::LAnd
260 | Instruction::LAStore
261 | Instruction::LCmp
262 | Instruction::LConst(..)
263 | Instruction::LDiv
264 | Instruction::LMul
265 | Instruction::LNeg
266 | Instruction::LOr
267 | Instruction::LRem
268 | Instruction::LReturn
269 | Instruction::LShl
270 | Instruction::LShr
271 | Instruction::LSub
272 | Instruction::LUShr
273 | Instruction::LXor
274 | Instruction::MonitorEnter
275 | Instruction::MonitorExit
276 | Instruction::Nop
277 | Instruction::Pop
278 | Instruction::Pop2
279 | Instruction::Return
280 | Instruction::SALoad
281 | Instruction::SAStore
282 | Instruction::Swap => 1,
283 Instruction::ALoad(index)
284 | Instruction::DLoad(index)
285 | Instruction::FLoad(index)
286 | Instruction::ILoad(index)
287 | Instruction::LLoad(index) => {
288 if *index < 4 {
289 1
290 } else {
291 2
292 }
293 }
294 Instruction::AStore(index)
295 | Instruction::DStore(index)
296 | Instruction::FStore(index)
297 | Instruction::IStore(index)
298 | Instruction::LStore(index) => {
299 if *index < 4 {
300 1
301 } else {
302 2
303 }
304 }
305 Instruction::Bipush(..)
306 | Instruction::Ldc(..)
307 | Instruction::NewArray(..)
308 | Instruction::Ret(..) => 2,
309 Instruction::ANewArray(..)
310 | Instruction::CheckCast(..)
311 | Instruction::GetField(..)
312 | Instruction::GetStatic(..)
313 | Instruction::Goto(..)
314 | Instruction::IfAcmpeq(..)
315 | Instruction::IfAcmpne(..)
316 | Instruction::Ifeq(..)
317 | Instruction::Ifge(..)
318 | Instruction::Ifgt(..)
319 | Instruction::IfIcmpeq(..)
320 | Instruction::IfIcmpge(..)
321 | Instruction::IfIcmpgt(..)
322 | Instruction::IfIcmple(..)
323 | Instruction::IfIcmplt(..)
324 | Instruction::IfIcmpne(..)
325 | Instruction::Ifle(..)
326 | Instruction::Iflt(..)
327 | Instruction::Ifne(..)
328 | Instruction::IfNonNull(..)
329 | Instruction::IfNull(..)
330 | Instruction::IInc(..)
331 | Instruction::InstanceOf(..)
332 | Instruction::InvokeSpecial(..)
333 | Instruction::InvokeStatic(..)
334 | Instruction::InvokeVirtual(..)
335 | Instruction::Jsr(..)
336 | Instruction::Ldc2W(..)
337 | Instruction::LdcW(..)
338 | Instruction::New(..)
339 | Instruction::PutField(..)
340 | Instruction::PutStatic(..)
341 | Instruction::Sipush(..) => 3,
342 Instruction::ALoadW(..)
343 | Instruction::AStoreW(..)
344 | Instruction::DLoadW(..)
345 | Instruction::DStoreW(..)
346 | Instruction::FLoadW(..)
347 | Instruction::FStoreW(..)
348 | Instruction::ILoadW(..)
349 | Instruction::InvokeInterface { .. }
350 | Instruction::IStoreW(..)
351 | Instruction::LLoadW(..)
352 | Instruction::LStoreW(..)
353 | Instruction::MultiANewArray(..)
354 | Instruction::RetW(..) => 4,
355 Instruction::GotoW(..) | Instruction::InvokeDynamic(..) | Instruction::JsrW(..) => 5,
356 Instruction::IIncW(..) => 6,
357 Instruction::LookupSwitch {
358 padding,
359 default: _,
360 pairs,
361 } => 1 + padding + 8 + pairs.len() as u32 * 8,
362 Instruction::TableSwitch {
363 padding,
364 minimum: _,
365 maximum: _,
366 jump_targets,
367 default: _,
368 } => 1 + padding + 12 + jump_targets.len() as u32,
369 }
370 }
371}