View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.bcel.generic;
18  
19  import org.apache.bcel.Const;
20  
21  /**
22   * Contains shareable instruction objects.
23   * <p>
24   * In order to save memory you can use some instructions multiply, since they have an immutable state and are directly
25   * derived from Instruction. I.e. they have no instance fields that could be changed. Since some of these instructions
26   * like ICONST_0 occur very frequently this can save a lot of time and space. This feature is an adaptation of the
27   * FlyWeight design pattern, we just use an array instead of a factory.
28   * </p>
29   * <p>
30   * The Instructions can also accessed directly under their names, so it's possible to write
31   * il.append(Instruction.ICONST_0);
32   * </p>
33   */
34  public final class InstructionConst {
35  
36      /**
37       * Predefined instruction objects.
38       *
39       * NOTE these are not currently immutable, because Instruction has mutable protected fields opcode and length.
40       */
41      public static final Instruction NOP = new NOP();
42      public static final Instruction ACONST_NULL = new ACONST_NULL();
43      public static final Instruction ICONST_M1 = new ICONST(-1);
44      public static final Instruction ICONST_0 = new ICONST(0);
45      public static final Instruction ICONST_1 = new ICONST(1);
46      public static final Instruction ICONST_2 = new ICONST(2);
47      public static final Instruction ICONST_3 = new ICONST(3);
48      public static final Instruction ICONST_4 = new ICONST(4);
49      public static final Instruction ICONST_5 = new ICONST(5);
50      public static final Instruction LCONST_0 = new LCONST(0);
51      public static final Instruction LCONST_1 = new LCONST(1);
52      public static final Instruction FCONST_0 = new FCONST(0);
53      public static final Instruction FCONST_1 = new FCONST(1);
54      public static final Instruction FCONST_2 = new FCONST(2);
55      public static final Instruction DCONST_0 = new DCONST(0);
56      public static final Instruction DCONST_1 = new DCONST(1);
57      public static final ArrayInstruction IALOAD = new IALOAD();
58      public static final ArrayInstruction LALOAD = new LALOAD();
59      public static final ArrayInstruction FALOAD = new FALOAD();
60      public static final ArrayInstruction DALOAD = new DALOAD();
61      public static final ArrayInstruction AALOAD = new AALOAD();
62      public static final ArrayInstruction BALOAD = new BALOAD();
63      public static final ArrayInstruction CALOAD = new CALOAD();
64      public static final ArrayInstruction SALOAD = new SALOAD();
65      public static final ArrayInstruction IASTORE = new IASTORE();
66      public static final ArrayInstruction LASTORE = new LASTORE();
67      public static final ArrayInstruction FASTORE = new FASTORE();
68      public static final ArrayInstruction DASTORE = new DASTORE();
69      public static final ArrayInstruction AASTORE = new AASTORE();
70      public static final ArrayInstruction BASTORE = new BASTORE();
71      public static final ArrayInstruction CASTORE = new CASTORE();
72      public static final ArrayInstruction SASTORE = new SASTORE();
73      public static final StackInstruction POP = new POP();
74      public static final StackInstruction POP2 = new POP2();
75      public static final StackInstruction DUP = new DUP();
76      public static final StackInstruction DUP_X1 = new DUP_X1();
77      public static final StackInstruction DUP_X2 = new DUP_X2();
78      public static final StackInstruction DUP2 = new DUP2();
79      public static final StackInstruction DUP2_X1 = new DUP2_X1();
80      public static final StackInstruction DUP2_X2 = new DUP2_X2();
81      public static final StackInstruction SWAP = new SWAP();
82      public static final ArithmeticInstruction IADD = new IADD();
83      public static final ArithmeticInstruction LADD = new LADD();
84      public static final ArithmeticInstruction FADD = new FADD();
85      public static final ArithmeticInstruction DADD = new DADD();
86      public static final ArithmeticInstruction ISUB = new ISUB();
87      public static final ArithmeticInstruction LSUB = new LSUB();
88      public static final ArithmeticInstruction FSUB = new FSUB();
89      public static final ArithmeticInstruction DSUB = new DSUB();
90      public static final ArithmeticInstruction IMUL = new IMUL();
91      public static final ArithmeticInstruction LMUL = new LMUL();
92      public static final ArithmeticInstruction FMUL = new FMUL();
93      public static final ArithmeticInstruction DMUL = new DMUL();
94      public static final ArithmeticInstruction IDIV = new IDIV();
95      public static final ArithmeticInstruction LDIV = new LDIV();
96      public static final ArithmeticInstruction FDIV = new FDIV();
97      public static final ArithmeticInstruction DDIV = new DDIV();
98      public static final ArithmeticInstruction IREM = new IREM();
99      public static final ArithmeticInstruction LREM = new LREM();
100     public static final ArithmeticInstruction FREM = new FREM();
101     public static final ArithmeticInstruction DREM = new DREM();
102     public static final ArithmeticInstruction INEG = new INEG();
103     public static final ArithmeticInstruction LNEG = new LNEG();
104     public static final ArithmeticInstruction FNEG = new FNEG();
105     public static final ArithmeticInstruction DNEG = new DNEG();
106     public static final ArithmeticInstruction ISHL = new ISHL();
107     public static final ArithmeticInstruction LSHL = new LSHL();
108     public static final ArithmeticInstruction ISHR = new ISHR();
109     public static final ArithmeticInstruction LSHR = new LSHR();
110     public static final ArithmeticInstruction IUSHR = new IUSHR();
111     public static final ArithmeticInstruction LUSHR = new LUSHR();
112     public static final ArithmeticInstruction IAND = new IAND();
113     public static final ArithmeticInstruction LAND = new LAND();
114     public static final ArithmeticInstruction IOR = new IOR();
115     public static final ArithmeticInstruction LOR = new LOR();
116     public static final ArithmeticInstruction IXOR = new IXOR();
117     public static final ArithmeticInstruction LXOR = new LXOR();
118     public static final ConversionInstruction I2L = new I2L();
119     public static final ConversionInstruction I2F = new I2F();
120     public static final ConversionInstruction I2D = new I2D();
121     public static final ConversionInstruction L2I = new L2I();
122     public static final ConversionInstruction L2F = new L2F();
123     public static final ConversionInstruction L2D = new L2D();
124     public static final ConversionInstruction F2I = new F2I();
125     public static final ConversionInstruction F2L = new F2L();
126     public static final ConversionInstruction F2D = new F2D();
127     public static final ConversionInstruction D2I = new D2I();
128     public static final ConversionInstruction D2L = new D2L();
129     public static final ConversionInstruction D2F = new D2F();
130     public static final ConversionInstruction I2B = new I2B();
131     public static final ConversionInstruction I2C = new I2C();
132     public static final ConversionInstruction I2S = new I2S();
133     public static final Instruction LCMP = new LCMP();
134     public static final Instruction FCMPL = new FCMPL();
135     public static final Instruction FCMPG = new FCMPG();
136     public static final Instruction DCMPL = new DCMPL();
137     public static final Instruction DCMPG = new DCMPG();
138     public static final ReturnInstruction IRETURN = new IRETURN();
139     public static final ReturnInstruction LRETURN = new LRETURN();
140     public static final ReturnInstruction FRETURN = new FRETURN();
141     public static final ReturnInstruction DRETURN = new DRETURN();
142     public static final ReturnInstruction ARETURN = new ARETURN();
143     public static final ReturnInstruction RETURN = new RETURN();
144     public static final Instruction ARRAYLENGTH = new ARRAYLENGTH();
145     public static final Instruction ATHROW = new ATHROW();
146     public static final Instruction MONITORENTER = new MONITORENTER();
147     public static final Instruction MONITOREXIT = new MONITOREXIT();
148 
149     /**
150      * You can use these constants in multiple places safely, if you can guarantee that you will never alter their internal
151      * values, e.g. call setIndex().
152      */
153     public static final LocalVariableInstruction THIS = new ALOAD(0);
154     public static final LocalVariableInstruction ALOAD_0 = THIS;
155     public static final LocalVariableInstruction ALOAD_1 = new ALOAD(1);
156     public static final LocalVariableInstruction ALOAD_2 = new ALOAD(2);
157     public static final LocalVariableInstruction ILOAD_0 = new ILOAD(0);
158     public static final LocalVariableInstruction ILOAD_1 = new ILOAD(1);
159     public static final LocalVariableInstruction ILOAD_2 = new ILOAD(2);
160     public static final LocalVariableInstruction ASTORE_0 = new ASTORE(0);
161     public static final LocalVariableInstruction ASTORE_1 = new ASTORE(1);
162     public static final LocalVariableInstruction ASTORE_2 = new ASTORE(2);
163     public static final LocalVariableInstruction ISTORE_0 = new ISTORE(0);
164     public static final LocalVariableInstruction ISTORE_1 = new ISTORE(1);
165     public static final LocalVariableInstruction ISTORE_2 = new ISTORE(2);
166 
167     /**
168      * Gets object via its opcode, for immutable instructions like branch instructions entries are set to null.
169      */
170     static final Instruction[] INSTRUCTIONS = new Instruction[256];
171 
172     static {
173         INSTRUCTIONS[Const.NOP] = NOP;
174         INSTRUCTIONS[Const.ACONST_NULL] = ACONST_NULL;
175         INSTRUCTIONS[Const.ICONST_M1] = ICONST_M1;
176         INSTRUCTIONS[Const.ICONST_0] = ICONST_0;
177         INSTRUCTIONS[Const.ICONST_1] = ICONST_1;
178         INSTRUCTIONS[Const.ICONST_2] = ICONST_2;
179         INSTRUCTIONS[Const.ICONST_3] = ICONST_3;
180         INSTRUCTIONS[Const.ICONST_4] = ICONST_4;
181         INSTRUCTIONS[Const.ICONST_5] = ICONST_5;
182         INSTRUCTIONS[Const.LCONST_0] = LCONST_0;
183         INSTRUCTIONS[Const.LCONST_1] = LCONST_1;
184         INSTRUCTIONS[Const.FCONST_0] = FCONST_0;
185         INSTRUCTIONS[Const.FCONST_1] = FCONST_1;
186         INSTRUCTIONS[Const.FCONST_2] = FCONST_2;
187         INSTRUCTIONS[Const.DCONST_0] = DCONST_0;
188         INSTRUCTIONS[Const.DCONST_1] = DCONST_1;
189         INSTRUCTIONS[Const.IALOAD] = IALOAD;
190         INSTRUCTIONS[Const.LALOAD] = LALOAD;
191         INSTRUCTIONS[Const.FALOAD] = FALOAD;
192         INSTRUCTIONS[Const.DALOAD] = DALOAD;
193         INSTRUCTIONS[Const.AALOAD] = AALOAD;
194         INSTRUCTIONS[Const.BALOAD] = BALOAD;
195         INSTRUCTIONS[Const.CALOAD] = CALOAD;
196         INSTRUCTIONS[Const.SALOAD] = SALOAD;
197         INSTRUCTIONS[Const.IASTORE] = IASTORE;
198         INSTRUCTIONS[Const.LASTORE] = LASTORE;
199         INSTRUCTIONS[Const.FASTORE] = FASTORE;
200         INSTRUCTIONS[Const.DASTORE] = DASTORE;
201         INSTRUCTIONS[Const.AASTORE] = AASTORE;
202         INSTRUCTIONS[Const.BASTORE] = BASTORE;
203         INSTRUCTIONS[Const.CASTORE] = CASTORE;
204         INSTRUCTIONS[Const.SASTORE] = SASTORE;
205         INSTRUCTIONS[Const.POP] = POP;
206         INSTRUCTIONS[Const.POP2] = POP2;
207         INSTRUCTIONS[Const.DUP] = DUP;
208         INSTRUCTIONS[Const.DUP_X1] = DUP_X1;
209         INSTRUCTIONS[Const.DUP_X2] = DUP_X2;
210         INSTRUCTIONS[Const.DUP2] = DUP2;
211         INSTRUCTIONS[Const.DUP2_X1] = DUP2_X1;
212         INSTRUCTIONS[Const.DUP2_X2] = DUP2_X2;
213         INSTRUCTIONS[Const.SWAP] = SWAP;
214         INSTRUCTIONS[Const.IADD] = IADD;
215         INSTRUCTIONS[Const.LADD] = LADD;
216         INSTRUCTIONS[Const.FADD] = FADD;
217         INSTRUCTIONS[Const.DADD] = DADD;
218         INSTRUCTIONS[Const.ISUB] = ISUB;
219         INSTRUCTIONS[Const.LSUB] = LSUB;
220         INSTRUCTIONS[Const.FSUB] = FSUB;
221         INSTRUCTIONS[Const.DSUB] = DSUB;
222         INSTRUCTIONS[Const.IMUL] = IMUL;
223         INSTRUCTIONS[Const.LMUL] = LMUL;
224         INSTRUCTIONS[Const.FMUL] = FMUL;
225         INSTRUCTIONS[Const.DMUL] = DMUL;
226         INSTRUCTIONS[Const.IDIV] = IDIV;
227         INSTRUCTIONS[Const.LDIV] = LDIV;
228         INSTRUCTIONS[Const.FDIV] = FDIV;
229         INSTRUCTIONS[Const.DDIV] = DDIV;
230         INSTRUCTIONS[Const.IREM] = IREM;
231         INSTRUCTIONS[Const.LREM] = LREM;
232         INSTRUCTIONS[Const.FREM] = FREM;
233         INSTRUCTIONS[Const.DREM] = DREM;
234         INSTRUCTIONS[Const.INEG] = INEG;
235         INSTRUCTIONS[Const.LNEG] = LNEG;
236         INSTRUCTIONS[Const.FNEG] = FNEG;
237         INSTRUCTIONS[Const.DNEG] = DNEG;
238         INSTRUCTIONS[Const.ISHL] = ISHL;
239         INSTRUCTIONS[Const.LSHL] = LSHL;
240         INSTRUCTIONS[Const.ISHR] = ISHR;
241         INSTRUCTIONS[Const.LSHR] = LSHR;
242         INSTRUCTIONS[Const.IUSHR] = IUSHR;
243         INSTRUCTIONS[Const.LUSHR] = LUSHR;
244         INSTRUCTIONS[Const.IAND] = IAND;
245         INSTRUCTIONS[Const.LAND] = LAND;
246         INSTRUCTIONS[Const.IOR] = IOR;
247         INSTRUCTIONS[Const.LOR] = LOR;
248         INSTRUCTIONS[Const.IXOR] = IXOR;
249         INSTRUCTIONS[Const.LXOR] = LXOR;
250         INSTRUCTIONS[Const.I2L] = I2L;
251         INSTRUCTIONS[Const.I2F] = I2F;
252         INSTRUCTIONS[Const.I2D] = I2D;
253         INSTRUCTIONS[Const.L2I] = L2I;
254         INSTRUCTIONS[Const.L2F] = L2F;
255         INSTRUCTIONS[Const.L2D] = L2D;
256         INSTRUCTIONS[Const.F2I] = F2I;
257         INSTRUCTIONS[Const.F2L] = F2L;
258         INSTRUCTIONS[Const.F2D] = F2D;
259         INSTRUCTIONS[Const.D2I] = D2I;
260         INSTRUCTIONS[Const.D2L] = D2L;
261         INSTRUCTIONS[Const.D2F] = D2F;
262         INSTRUCTIONS[Const.I2B] = I2B;
263         INSTRUCTIONS[Const.I2C] = I2C;
264         INSTRUCTIONS[Const.I2S] = I2S;
265         INSTRUCTIONS[Const.LCMP] = LCMP;
266         INSTRUCTIONS[Const.FCMPL] = FCMPL;
267         INSTRUCTIONS[Const.FCMPG] = FCMPG;
268         INSTRUCTIONS[Const.DCMPL] = DCMPL;
269         INSTRUCTIONS[Const.DCMPG] = DCMPG;
270         INSTRUCTIONS[Const.IRETURN] = IRETURN;
271         INSTRUCTIONS[Const.LRETURN] = LRETURN;
272         INSTRUCTIONS[Const.FRETURN] = FRETURN;
273         INSTRUCTIONS[Const.DRETURN] = DRETURN;
274         INSTRUCTIONS[Const.ARETURN] = ARETURN;
275         INSTRUCTIONS[Const.RETURN] = RETURN;
276         INSTRUCTIONS[Const.ARRAYLENGTH] = ARRAYLENGTH;
277         INSTRUCTIONS[Const.ATHROW] = ATHROW;
278         INSTRUCTIONS[Const.MONITORENTER] = MONITORENTER;
279         INSTRUCTIONS[Const.MONITOREXIT] = MONITOREXIT;
280     }
281 
282     /**
283      * Gets the Instruction.
284      *
285      * @param index the index, e.g. {@link Const#RETURN}
286      * @return the entry from the private INSTRUCTIONS table
287      */
288     public static Instruction getInstruction(final int index) {
289         return INSTRUCTIONS[index];
290     }
291 
292     private InstructionConst() {
293     } // non-instantiable
294 }