001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 */
017package org.apache.bcel.generic;
018
019import org.apache.bcel.Const;
020
021/**
022 * Contains shareable instruction objects.
023 * <p>
024 * In order to save memory you can use some instructions multiply, since they have an immutable state and are directly
025 * derived from Instruction. I.e. they have no instance fields that could be changed. Since some of these instructions
026 * like ICONST_0 occur very frequently this can save a lot of time and space. This feature is an adaptation of the
027 * FlyWeight design pattern, we just use an array instead of a factory.
028 * </p>
029 * <p>
030 * The Instructions can also accessed directly under their names, so it's possible to write
031 * il.append(Instruction.ICONST_0);
032 * </p>
033 */
034public final class InstructionConst {
035
036    /**
037     * Predefined instruction objects.
038     *
039     * NOTE these are not currently immutable, because Instruction has mutable protected fields opcode and length.
040     */
041    public static final Instruction NOP = new NOP();
042    public static final Instruction ACONST_NULL = new ACONST_NULL();
043    public static final Instruction ICONST_M1 = new ICONST(-1);
044    public static final Instruction ICONST_0 = new ICONST(0);
045    public static final Instruction ICONST_1 = new ICONST(1);
046    public static final Instruction ICONST_2 = new ICONST(2);
047    public static final Instruction ICONST_3 = new ICONST(3);
048    public static final Instruction ICONST_4 = new ICONST(4);
049    public static final Instruction ICONST_5 = new ICONST(5);
050    public static final Instruction LCONST_0 = new LCONST(0);
051    public static final Instruction LCONST_1 = new LCONST(1);
052    public static final Instruction FCONST_0 = new FCONST(0);
053    public static final Instruction FCONST_1 = new FCONST(1);
054    public static final Instruction FCONST_2 = new FCONST(2);
055    public static final Instruction DCONST_0 = new DCONST(0);
056    public static final Instruction DCONST_1 = new DCONST(1);
057    public static final ArrayInstruction IALOAD = new IALOAD();
058    public static final ArrayInstruction LALOAD = new LALOAD();
059    public static final ArrayInstruction FALOAD = new FALOAD();
060    public static final ArrayInstruction DALOAD = new DALOAD();
061    public static final ArrayInstruction AALOAD = new AALOAD();
062    public static final ArrayInstruction BALOAD = new BALOAD();
063    public static final ArrayInstruction CALOAD = new CALOAD();
064    public static final ArrayInstruction SALOAD = new SALOAD();
065    public static final ArrayInstruction IASTORE = new IASTORE();
066    public static final ArrayInstruction LASTORE = new LASTORE();
067    public static final ArrayInstruction FASTORE = new FASTORE();
068    public static final ArrayInstruction DASTORE = new DASTORE();
069    public static final ArrayInstruction AASTORE = new AASTORE();
070    public static final ArrayInstruction BASTORE = new BASTORE();
071    public static final ArrayInstruction CASTORE = new CASTORE();
072    public static final ArrayInstruction SASTORE = new SASTORE();
073    public static final StackInstruction POP = new POP();
074    public static final StackInstruction POP2 = new POP2();
075    public static final StackInstruction DUP = new DUP();
076    public static final StackInstruction DUP_X1 = new DUP_X1();
077    public static final StackInstruction DUP_X2 = new DUP_X2();
078    public static final StackInstruction DUP2 = new DUP2();
079    public static final StackInstruction DUP2_X1 = new DUP2_X1();
080    public static final StackInstruction DUP2_X2 = new DUP2_X2();
081    public static final StackInstruction SWAP = new SWAP();
082    public static final ArithmeticInstruction IADD = new IADD();
083    public static final ArithmeticInstruction LADD = new LADD();
084    public static final ArithmeticInstruction FADD = new FADD();
085    public static final ArithmeticInstruction DADD = new DADD();
086    public static final ArithmeticInstruction ISUB = new ISUB();
087    public static final ArithmeticInstruction LSUB = new LSUB();
088    public static final ArithmeticInstruction FSUB = new FSUB();
089    public static final ArithmeticInstruction DSUB = new DSUB();
090    public static final ArithmeticInstruction IMUL = new IMUL();
091    public static final ArithmeticInstruction LMUL = new LMUL();
092    public static final ArithmeticInstruction FMUL = new FMUL();
093    public static final ArithmeticInstruction DMUL = new DMUL();
094    public static final ArithmeticInstruction IDIV = new IDIV();
095    public static final ArithmeticInstruction LDIV = new LDIV();
096    public static final ArithmeticInstruction FDIV = new FDIV();
097    public static final ArithmeticInstruction DDIV = new DDIV();
098    public static final ArithmeticInstruction IREM = new IREM();
099    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}