View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.bcel.generic;
21  
22  import org.apache.bcel.Const;
23  
24  /**
25   * Contains shareable instruction objects.
26   * <p>
27   * In order to save memory you can use some instructions multiply, since they have an immutable state and are directly derived from Instruction. I.e. they have
28   * no instance fields that could be changed. Since some of these instructions like ICONST_0 occur very frequently this can save a lot of time and space. This
29   * feature is an adaptation of the FlyWeight design pattern, we just use an array instead of a factory.
30   * </p>
31   * <p>
32   * The Instructions can also accessed directly under their names, so it's possible to write il.append(Instruction.ICONST_0);
33   * </p>
34   */
35  public final class InstructionConst {
36  
37      /**
38       * Predefined instruction objects. NOTE these are not currently immutable, because Instruction has mutable protected fields opcode and length.
39       */
40  
41      /** NOP instruction. */
42      public static final Instruction NOP = new NOP();
43  
44      /** ACONST_NULL instruction. */
45      public static final Instruction ACONST_NULL = new ACONST_NULL();
46  
47      /** ICONST_M1 instruction. */
48      public static final Instruction ICONST_M1 = new ICONST(-1);
49  
50      /** ICONST_0 instruction. */
51      public static final Instruction ICONST_0 = new ICONST(0);
52  
53      /** ICONST_1 instruction. */
54      public static final Instruction ICONST_1 = new ICONST(1);
55  
56      /** ICONST_2 instruction. */
57      public static final Instruction ICONST_2 = new ICONST(2);
58  
59      /** ICONST_3 instruction. */
60      public static final Instruction ICONST_3 = new ICONST(3);
61  
62      /** ICONST_4 instruction. */
63      public static final Instruction ICONST_4 = new ICONST(4);
64  
65      /** ICONST_5 instruction. */
66      public static final Instruction ICONST_5 = new ICONST(5);
67  
68      /** LCONST_0 instruction. */
69      public static final Instruction LCONST_0 = new LCONST(0);
70  
71      /** LCONST_1 instruction. */
72      public static final Instruction LCONST_1 = new LCONST(1);
73  
74      /** FCONST_0 instruction. */
75      public static final Instruction FCONST_0 = new FCONST(0);
76  
77      /** FCONST_1 instruction. */
78      public static final Instruction FCONST_1 = new FCONST(1);
79  
80      /** FCONST_2 instruction. */
81      public static final Instruction FCONST_2 = new FCONST(2);
82  
83      /** DCONST_0 instruction. */
84      public static final Instruction DCONST_0 = new DCONST(0);
85  
86      /** DCONST_1 instruction. */
87      public static final Instruction DCONST_1 = new DCONST(1);
88  
89      /** IALOAD instruction. */
90      public static final ArrayInstruction IALOAD = new IALOAD();
91  
92      /** LALOAD instruction. */
93      public static final ArrayInstruction LALOAD = new LALOAD();
94  
95      /** FALOAD instruction. */
96      public static final ArrayInstruction FALOAD = new FALOAD();
97  
98      /** DALOAD instruction. */
99      public static final ArrayInstruction DALOAD = new DALOAD();
100 
101     /** AALOAD instruction. */
102     public static final ArrayInstruction AALOAD = new AALOAD();
103 
104     /** BALOAD instruction. */
105     public static final ArrayInstruction BALOAD = new BALOAD();
106 
107     /** CALOAD instruction. */
108     public static final ArrayInstruction CALOAD = new CALOAD();
109 
110     /** SALOAD instruction. */
111     public static final ArrayInstruction SALOAD = new SALOAD();
112 
113     /** IASTORE instruction. */
114     public static final ArrayInstruction IASTORE = new IASTORE();
115 
116     /** LASTORE instruction. */
117     public static final ArrayInstruction LASTORE = new LASTORE();
118 
119     /** FASTORE instruction. */
120     public static final ArrayInstruction FASTORE = new FASTORE();
121 
122     /** DASTORE instruction. */
123     public static final ArrayInstruction DASTORE = new DASTORE();
124 
125     /** AASTORE instruction. */
126     public static final ArrayInstruction AASTORE = new AASTORE();
127 
128     /** BASTORE instruction. */
129     public static final ArrayInstruction BASTORE = new BASTORE();
130 
131     /** CASTORE instruction. */
132     public static final ArrayInstruction CASTORE = new CASTORE();
133 
134     /** SASTORE instruction. */
135     public static final ArrayInstruction SASTORE = new SASTORE();
136 
137     /** POP instruction. */
138     public static final StackInstruction POP = new POP();
139 
140     /** POP2 instruction. */
141     public static final StackInstruction POP2 = new POP2();
142 
143     /** DUP instruction. */
144     public static final StackInstruction DUP = new DUP();
145 
146     /** DUP_X1 instruction. */
147     public static final StackInstruction DUP_X1 = new DUP_X1();
148 
149     /** DUP_X2 instruction. */
150     public static final StackInstruction DUP_X2 = new DUP_X2();
151 
152     /** DUP2 instruction. */
153     public static final StackInstruction DUP2 = new DUP2();
154 
155     /** DUP2_X1 instruction. */
156     public static final StackInstruction DUP2_X1 = new DUP2_X1();
157 
158     /** DUP2_X2 instruction. */
159     public static final StackInstruction DUP2_X2 = new DUP2_X2();
160 
161     /** SWAP instruction. */
162     public static final StackInstruction SWAP = new SWAP();
163 
164     /** IADD instruction. */
165     public static final ArithmeticInstruction IADD = new IADD();
166 
167     /** LADD instruction. */
168     public static final ArithmeticInstruction LADD = new LADD();
169 
170     /** FADD instruction. */
171     public static final ArithmeticInstruction FADD = new FADD();
172 
173     /** DADD instruction. */
174     public static final ArithmeticInstruction DADD = new DADD();
175 
176     /** ISUB instruction. */
177     public static final ArithmeticInstruction ISUB = new ISUB();
178 
179     /** LSUB instruction. */
180     public static final ArithmeticInstruction LSUB = new LSUB();
181 
182     /** FSUB instruction. */
183     public static final ArithmeticInstruction FSUB = new FSUB();
184 
185     /** DSUB instruction. */
186     public static final ArithmeticInstruction DSUB = new DSUB();
187 
188     /** IMUL instruction. */
189     public static final ArithmeticInstruction IMUL = new IMUL();
190 
191     /** LMUL instruction. */
192     public static final ArithmeticInstruction LMUL = new LMUL();
193 
194     /** FMUL instruction. */
195     public static final ArithmeticInstruction FMUL = new FMUL();
196 
197     /** DMUL instruction. */
198     public static final ArithmeticInstruction DMUL = new DMUL();
199 
200     /** IDIV instruction. */
201     public static final ArithmeticInstruction IDIV = new IDIV();
202 
203     /** LDIV instruction. */
204     public static final ArithmeticInstruction LDIV = new LDIV();
205 
206     /** FDIV instruction. */
207     public static final ArithmeticInstruction FDIV = new FDIV();
208 
209     /** DDIV instruction. */
210     public static final ArithmeticInstruction DDIV = new DDIV();
211 
212     /** IREM instruction. */
213     public static final ArithmeticInstruction IREM = new IREM();
214 
215     /** LREM instruction. */
216     public static final ArithmeticInstruction LREM = new LREM();
217 
218     /** FREM instruction. */
219     public static final ArithmeticInstruction FREM = new FREM();
220 
221     /** DREM instruction. */
222     public static final ArithmeticInstruction DREM = new DREM();
223 
224     /** INEG instruction. */
225     public static final ArithmeticInstruction INEG = new INEG();
226 
227     /** LNEG instruction. */
228     public static final ArithmeticInstruction LNEG = new LNEG();
229 
230     /** FNEG instruction. */
231     public static final ArithmeticInstruction FNEG = new FNEG();
232 
233     /** DNEG instruction. */
234     public static final ArithmeticInstruction DNEG = new DNEG();
235 
236     /** ISHL instruction. */
237     public static final ArithmeticInstruction ISHL = new ISHL();
238 
239     /** LSHL instruction. */
240     public static final ArithmeticInstruction LSHL = new LSHL();
241 
242     /** ISHR instruction. */
243     public static final ArithmeticInstruction ISHR = new ISHR();
244 
245     /** LSHR instruction. */
246     public static final ArithmeticInstruction LSHR = new LSHR();
247 
248     /** IUSHR instruction. */
249     public static final ArithmeticInstruction IUSHR = new IUSHR();
250 
251     /** LUSHR instruction. */
252     public static final ArithmeticInstruction LUSHR = new LUSHR();
253 
254     /** IAND instruction. */
255     public static final ArithmeticInstruction IAND = new IAND();
256 
257     /** LAND instruction. */
258     public static final ArithmeticInstruction LAND = new LAND();
259 
260     /** IOR instruction. */
261     public static final ArithmeticInstruction IOR = new IOR();
262 
263     /** LOR instruction. */
264     public static final ArithmeticInstruction LOR = new LOR();
265 
266     /** IXOR instruction. */
267     public static final ArithmeticInstruction IXOR = new IXOR();
268 
269     /** LXOR instruction. */
270     public static final ArithmeticInstruction LXOR = new LXOR();
271 
272     /** I2L instruction. */
273     public static final ConversionInstruction I2L = new I2L();
274 
275     /** I2F instruction. */
276     public static final ConversionInstruction I2F = new I2F();
277 
278     /** I2D instruction. */
279     public static final ConversionInstruction I2D = new I2D();
280 
281     /** L2I instruction. */
282     public static final ConversionInstruction L2I = new L2I();
283 
284     /** L2F instruction. */
285     public static final ConversionInstruction L2F = new L2F();
286 
287     /** L2D instruction. */
288     public static final ConversionInstruction L2D = new L2D();
289 
290     /** F2I instruction. */
291     public static final ConversionInstruction F2I = new F2I();
292 
293     /** F2L instruction. */
294     public static final ConversionInstruction F2L = new F2L();
295 
296     /** F2D instruction. */
297     public static final ConversionInstruction F2D = new F2D();
298 
299     /** D2I instruction. */
300     public static final ConversionInstruction D2I = new D2I();
301 
302     /** D2L instruction. */
303     public static final ConversionInstruction D2L = new D2L();
304 
305     /** D2F instruction. */
306     public static final ConversionInstruction D2F = new D2F();
307 
308     /** I2B instruction. */
309     public static final ConversionInstruction I2B = new I2B();
310 
311     /** I2C instruction. */
312     public static final ConversionInstruction I2C = new I2C();
313 
314     /** I2S instruction. */
315     public static final ConversionInstruction I2S = new I2S();
316 
317     /** LCMP instruction. */
318     public static final Instruction LCMP = new LCMP();
319 
320     /** FCMPL instruction. */
321     public static final Instruction FCMPL = new FCMPL();
322 
323     /** FCMPG instruction. */
324     public static final Instruction FCMPG = new FCMPG();
325 
326     /** DCMPL instruction. */
327     public static final Instruction DCMPL = new DCMPL();
328 
329     /** DCMPG instruction. */
330     public static final Instruction DCMPG = new DCMPG();
331 
332     /** IRETURN instruction. */
333     public static final ReturnInstruction IRETURN = new IRETURN();
334 
335     /** LRETURN instruction. */
336     public static final ReturnInstruction LRETURN = new LRETURN();
337 
338     /** FRETURN instruction. */
339     public static final ReturnInstruction FRETURN = new FRETURN();
340 
341     /** DRETURN instruction. */
342     public static final ReturnInstruction DRETURN = new DRETURN();
343 
344     /** ARETURN instruction. */
345     public static final ReturnInstruction ARETURN = new ARETURN();
346 
347     /** RETURN instruction. */
348     public static final ReturnInstruction RETURN = new RETURN();
349 
350     /** ARRAYLENGTH instruction. */
351     public static final Instruction ARRAYLENGTH = new ARRAYLENGTH();
352 
353     /** ATHROW instruction. */
354     public static final Instruction ATHROW = new ATHROW();
355 
356     /** MONITORENTER instruction. */
357     public static final Instruction MONITORENTER = new MONITORENTER();
358 
359     /** MONITOREXIT instruction. */
360     public static final Instruction MONITOREXIT = new MONITOREXIT();
361 
362     /**
363      * You can use these constants in multiple places safely, if you can guarantee that you will never alter their internal values, for example call setIndex().
364      * THIS instruction (ALOAD_0)
365      */
366     public static final LocalVariableInstruction THIS = new ALOAD(0);
367 
368     /** ALOAD_0 instruction (same as THIS). */
369     public static final LocalVariableInstruction ALOAD_0 = THIS;
370 
371     /** ALOAD_1 instruction. */
372     public static final LocalVariableInstruction ALOAD_1 = new ALOAD(1);
373 
374     /** ALOAD_2 instruction. */
375     public static final LocalVariableInstruction ALOAD_2 = new ALOAD(2);
376 
377     /** ILOAD_0 instruction. */
378     public static final LocalVariableInstruction ILOAD_0 = new ILOAD(0);
379 
380     /** ILOAD_1 instruction. */
381     public static final LocalVariableInstruction ILOAD_1 = new ILOAD(1);
382 
383     /** ILOAD_2 instruction. */
384     public static final LocalVariableInstruction ILOAD_2 = new ILOAD(2);
385 
386     /** ASTORE_0 instruction. */
387     public static final LocalVariableInstruction ASTORE_0 = new ASTORE(0);
388 
389     /** ASTORE_1 instruction. */
390     public static final LocalVariableInstruction ASTORE_1 = new ASTORE(1);
391 
392     /** ASTORE_2 instruction. */
393     public static final LocalVariableInstruction ASTORE_2 = new ASTORE(2);
394 
395     /** ISTORE_0 instruction. */
396     public static final LocalVariableInstruction ISTORE_0 = new ISTORE(0);
397 
398     /** ISTORE_1 instruction. */
399     public static final LocalVariableInstruction ISTORE_1 = new ISTORE(1);
400 
401     /** ISTORE_2 instruction. */
402     public static final LocalVariableInstruction ISTORE_2 = new ISTORE(2);
403 
404     /**
405      * Gets object via its opcode, for immutable instructions like branch instructions entries are set to null.
406      */
407     static final Instruction[] INSTRUCTIONS = new Instruction[256];
408 
409     static {
410         INSTRUCTIONS[Const.NOP] = NOP;
411         INSTRUCTIONS[Const.ACONST_NULL] = ACONST_NULL;
412         INSTRUCTIONS[Const.ICONST_M1] = ICONST_M1;
413         INSTRUCTIONS[Const.ICONST_0] = ICONST_0;
414         INSTRUCTIONS[Const.ICONST_1] = ICONST_1;
415         INSTRUCTIONS[Const.ICONST_2] = ICONST_2;
416         INSTRUCTIONS[Const.ICONST_3] = ICONST_3;
417         INSTRUCTIONS[Const.ICONST_4] = ICONST_4;
418         INSTRUCTIONS[Const.ICONST_5] = ICONST_5;
419         INSTRUCTIONS[Const.LCONST_0] = LCONST_0;
420         INSTRUCTIONS[Const.LCONST_1] = LCONST_1;
421         INSTRUCTIONS[Const.FCONST_0] = FCONST_0;
422         INSTRUCTIONS[Const.FCONST_1] = FCONST_1;
423         INSTRUCTIONS[Const.FCONST_2] = FCONST_2;
424         INSTRUCTIONS[Const.DCONST_0] = DCONST_0;
425         INSTRUCTIONS[Const.DCONST_1] = DCONST_1;
426         INSTRUCTIONS[Const.IALOAD] = IALOAD;
427         INSTRUCTIONS[Const.LALOAD] = LALOAD;
428         INSTRUCTIONS[Const.FALOAD] = FALOAD;
429         INSTRUCTIONS[Const.DALOAD] = DALOAD;
430         INSTRUCTIONS[Const.AALOAD] = AALOAD;
431         INSTRUCTIONS[Const.BALOAD] = BALOAD;
432         INSTRUCTIONS[Const.CALOAD] = CALOAD;
433         INSTRUCTIONS[Const.SALOAD] = SALOAD;
434         INSTRUCTIONS[Const.IASTORE] = IASTORE;
435         INSTRUCTIONS[Const.LASTORE] = LASTORE;
436         INSTRUCTIONS[Const.FASTORE] = FASTORE;
437         INSTRUCTIONS[Const.DASTORE] = DASTORE;
438         INSTRUCTIONS[Const.AASTORE] = AASTORE;
439         INSTRUCTIONS[Const.BASTORE] = BASTORE;
440         INSTRUCTIONS[Const.CASTORE] = CASTORE;
441         INSTRUCTIONS[Const.SASTORE] = SASTORE;
442         INSTRUCTIONS[Const.POP] = POP;
443         INSTRUCTIONS[Const.POP2] = POP2;
444         INSTRUCTIONS[Const.DUP] = DUP;
445         INSTRUCTIONS[Const.DUP_X1] = DUP_X1;
446         INSTRUCTIONS[Const.DUP_X2] = DUP_X2;
447         INSTRUCTIONS[Const.DUP2] = DUP2;
448         INSTRUCTIONS[Const.DUP2_X1] = DUP2_X1;
449         INSTRUCTIONS[Const.DUP2_X2] = DUP2_X2;
450         INSTRUCTIONS[Const.SWAP] = SWAP;
451         INSTRUCTIONS[Const.IADD] = IADD;
452         INSTRUCTIONS[Const.LADD] = LADD;
453         INSTRUCTIONS[Const.FADD] = FADD;
454         INSTRUCTIONS[Const.DADD] = DADD;
455         INSTRUCTIONS[Const.ISUB] = ISUB;
456         INSTRUCTIONS[Const.LSUB] = LSUB;
457         INSTRUCTIONS[Const.FSUB] = FSUB;
458         INSTRUCTIONS[Const.DSUB] = DSUB;
459         INSTRUCTIONS[Const.IMUL] = IMUL;
460         INSTRUCTIONS[Const.LMUL] = LMUL;
461         INSTRUCTIONS[Const.FMUL] = FMUL;
462         INSTRUCTIONS[Const.DMUL] = DMUL;
463         INSTRUCTIONS[Const.IDIV] = IDIV;
464         INSTRUCTIONS[Const.LDIV] = LDIV;
465         INSTRUCTIONS[Const.FDIV] = FDIV;
466         INSTRUCTIONS[Const.DDIV] = DDIV;
467         INSTRUCTIONS[Const.IREM] = IREM;
468         INSTRUCTIONS[Const.LREM] = LREM;
469         INSTRUCTIONS[Const.FREM] = FREM;
470         INSTRUCTIONS[Const.DREM] = DREM;
471         INSTRUCTIONS[Const.INEG] = INEG;
472         INSTRUCTIONS[Const.LNEG] = LNEG;
473         INSTRUCTIONS[Const.FNEG] = FNEG;
474         INSTRUCTIONS[Const.DNEG] = DNEG;
475         INSTRUCTIONS[Const.ISHL] = ISHL;
476         INSTRUCTIONS[Const.LSHL] = LSHL;
477         INSTRUCTIONS[Const.ISHR] = ISHR;
478         INSTRUCTIONS[Const.LSHR] = LSHR;
479         INSTRUCTIONS[Const.IUSHR] = IUSHR;
480         INSTRUCTIONS[Const.LUSHR] = LUSHR;
481         INSTRUCTIONS[Const.IAND] = IAND;
482         INSTRUCTIONS[Const.LAND] = LAND;
483         INSTRUCTIONS[Const.IOR] = IOR;
484         INSTRUCTIONS[Const.LOR] = LOR;
485         INSTRUCTIONS[Const.IXOR] = IXOR;
486         INSTRUCTIONS[Const.LXOR] = LXOR;
487         INSTRUCTIONS[Const.I2L] = I2L;
488         INSTRUCTIONS[Const.I2F] = I2F;
489         INSTRUCTIONS[Const.I2D] = I2D;
490         INSTRUCTIONS[Const.L2I] = L2I;
491         INSTRUCTIONS[Const.L2F] = L2F;
492         INSTRUCTIONS[Const.L2D] = L2D;
493         INSTRUCTIONS[Const.F2I] = F2I;
494         INSTRUCTIONS[Const.F2L] = F2L;
495         INSTRUCTIONS[Const.F2D] = F2D;
496         INSTRUCTIONS[Const.D2I] = D2I;
497         INSTRUCTIONS[Const.D2L] = D2L;
498         INSTRUCTIONS[Const.D2F] = D2F;
499         INSTRUCTIONS[Const.I2B] = I2B;
500         INSTRUCTIONS[Const.I2C] = I2C;
501         INSTRUCTIONS[Const.I2S] = I2S;
502         INSTRUCTIONS[Const.LCMP] = LCMP;
503         INSTRUCTIONS[Const.FCMPL] = FCMPL;
504         INSTRUCTIONS[Const.FCMPG] = FCMPG;
505         INSTRUCTIONS[Const.DCMPL] = DCMPL;
506         INSTRUCTIONS[Const.DCMPG] = DCMPG;
507         INSTRUCTIONS[Const.IRETURN] = IRETURN;
508         INSTRUCTIONS[Const.LRETURN] = LRETURN;
509         INSTRUCTIONS[Const.FRETURN] = FRETURN;
510         INSTRUCTIONS[Const.DRETURN] = DRETURN;
511         INSTRUCTIONS[Const.ARETURN] = ARETURN;
512         INSTRUCTIONS[Const.RETURN] = RETURN;
513         INSTRUCTIONS[Const.ARRAYLENGTH] = ARRAYLENGTH;
514         INSTRUCTIONS[Const.ATHROW] = ATHROW;
515         INSTRUCTIONS[Const.MONITORENTER] = MONITORENTER;
516         INSTRUCTIONS[Const.MONITOREXIT] = MONITOREXIT;
517     }
518 
519     /**
520      * Gets the Instruction.
521      *
522      * @param index the index, for example {@link Const#RETURN}.
523      * @return the entry from the private INSTRUCTIONS table.
524      */
525     public static Instruction getInstruction(final int index) {
526         return INSTRUCTIONS[index];
527     }
528 
529     private InstructionConst() {
530     } // non-instantiable
531 }