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 *
017 */
018package org.apache.bcel.generic;
019
020import org.apache.bcel.Const;
021
022/**
023 * This interface contains shareable instruction objects.
024 *
025 * In order to save memory you can use some instructions multiply,
026 * since they have an immutable state and are directly derived from
027 * Instruction.  I.e. they have no instance fields that could be
028 * changed. Since some of these instructions like ICONST_0 occur
029 * very frequently this can save a lot of time and space. This
030 * feature is an adaptation of the FlyWeight design pattern, we
031 * just use an array instead of a factory.
032 *
033 * The Instructions can also accessed directly under their names, so
034 * it's possible to write il.append(Instruction.ICONST_0);
035 *
036 */
037public final class InstructionConst {
038
039    /**
040     * Predefined instruction objects
041     */
042    /*
043     * NOTE these are not currently immutable, because Instruction
044     * has mutable protected fields opcode and length.
045     */
046    public static final Instruction NOP = new NOP();
047    public static final Instruction ACONST_NULL = new ACONST_NULL();
048    public static final Instruction ICONST_M1 = new ICONST(-1);
049    public static final Instruction ICONST_0 = new ICONST(0);
050    public static final Instruction ICONST_1 = new ICONST(1);
051    public static final Instruction ICONST_2 = new ICONST(2);
052    public static final Instruction ICONST_3 = new ICONST(3);
053    public static final Instruction ICONST_4 = new ICONST(4);
054    public static final Instruction ICONST_5 = new ICONST(5);
055    public static final Instruction LCONST_0 = new LCONST(0);
056    public static final Instruction LCONST_1 = new LCONST(1);
057    public static final Instruction FCONST_0 = new FCONST(0);
058    public static final Instruction FCONST_1 = new FCONST(1);
059    public static final Instruction FCONST_2 = new FCONST(2);
060    public static final Instruction DCONST_0 = new DCONST(0);
061    public static final Instruction DCONST_1 = new DCONST(1);
062    public static final ArrayInstruction IALOAD = new IALOAD();
063    public static final ArrayInstruction LALOAD = new LALOAD();
064    public static final ArrayInstruction FALOAD = new FALOAD();
065    public static final ArrayInstruction DALOAD = new DALOAD();
066    public static final ArrayInstruction AALOAD = new AALOAD();
067    public static final ArrayInstruction BALOAD = new BALOAD();
068    public static final ArrayInstruction CALOAD = new CALOAD();
069    public static final ArrayInstruction SALOAD = new SALOAD();
070    public static final ArrayInstruction IASTORE = new IASTORE();
071    public static final ArrayInstruction LASTORE = new LASTORE();
072    public static final ArrayInstruction FASTORE = new FASTORE();
073    public static final ArrayInstruction DASTORE = new DASTORE();
074    public static final ArrayInstruction AASTORE = new AASTORE();
075    public static final ArrayInstruction BASTORE = new BASTORE();
076    public static final ArrayInstruction CASTORE = new CASTORE();
077    public static final ArrayInstruction SASTORE = new SASTORE();
078    public static final StackInstruction POP = new POP();
079    public static final StackInstruction POP2 = new POP2();
080    public static final StackInstruction DUP = new DUP();
081    public static final StackInstruction DUP_X1 = new DUP_X1();
082    public static final StackInstruction DUP_X2 = new DUP_X2();
083    public static final StackInstruction DUP2 = new DUP2();
084    public static final StackInstruction DUP2_X1 = new DUP2_X1();
085    public static final StackInstruction DUP2_X2 = new DUP2_X2();
086    public static final StackInstruction SWAP = new SWAP();
087    public static final ArithmeticInstruction IADD = new IADD();
088    public static final ArithmeticInstruction LADD = new LADD();
089    public static final ArithmeticInstruction FADD = new FADD();
090    public static final ArithmeticInstruction DADD = new DADD();
091    public static final ArithmeticInstruction ISUB = new ISUB();
092    public static final ArithmeticInstruction LSUB = new LSUB();
093    public static final ArithmeticInstruction FSUB = new FSUB();
094    public static final ArithmeticInstruction DSUB = new DSUB();
095    public static final ArithmeticInstruction IMUL = new IMUL();
096    public static final ArithmeticInstruction LMUL = new LMUL();
097    public static final ArithmeticInstruction FMUL = new FMUL();
098    public static final ArithmeticInstruction DMUL = new DMUL();
099    public static final ArithmeticInstruction IDIV = new IDIV();
100    public static final ArithmeticInstruction LDIV = new LDIV();
101    public static final ArithmeticInstruction FDIV = new FDIV();
102    public static final ArithmeticInstruction DDIV = new DDIV();
103    public static final ArithmeticInstruction IREM = new IREM();
104    public static final ArithmeticInstruction LREM = new LREM();
105    public static final ArithmeticInstruction FREM = new FREM();
106    public static final ArithmeticInstruction DREM = new DREM();
107    public static final ArithmeticInstruction INEG = new INEG();
108    public static final ArithmeticInstruction LNEG = new LNEG();
109    public static final ArithmeticInstruction FNEG = new FNEG();
110    public static final ArithmeticInstruction DNEG = new DNEG();
111    public static final ArithmeticInstruction ISHL = new ISHL();
112    public static final ArithmeticInstruction LSHL = new LSHL();
113    public static final ArithmeticInstruction ISHR = new ISHR();
114    public static final ArithmeticInstruction LSHR = new LSHR();
115    public static final ArithmeticInstruction IUSHR = new IUSHR();
116    public static final ArithmeticInstruction LUSHR = new LUSHR();
117    public static final ArithmeticInstruction IAND = new IAND();
118    public static final ArithmeticInstruction LAND = new LAND();
119    public static final ArithmeticInstruction IOR = new IOR();
120    public static final ArithmeticInstruction LOR = new LOR();
121    public static final ArithmeticInstruction IXOR = new IXOR();
122    public static final ArithmeticInstruction LXOR = new LXOR();
123    public static final ConversionInstruction I2L = new I2L();
124    public static final ConversionInstruction I2F = new I2F();
125    public static final ConversionInstruction I2D = new I2D();
126    public static final ConversionInstruction L2I = new L2I();
127    public static final ConversionInstruction L2F = new L2F();
128    public static final ConversionInstruction L2D = new L2D();
129    public static final ConversionInstruction F2I = new F2I();
130    public static final ConversionInstruction F2L = new F2L();
131    public static final ConversionInstruction F2D = new F2D();
132    public static final ConversionInstruction D2I = new D2I();
133    public static final ConversionInstruction D2L = new D2L();
134    public static final ConversionInstruction D2F = new D2F();
135    public static final ConversionInstruction I2B = new I2B();
136    public static final ConversionInstruction I2C = new I2C();
137    public static final ConversionInstruction I2S = new I2S();
138    public static final Instruction LCMP = new LCMP();
139    public static final Instruction FCMPL = new FCMPL();
140    public static final Instruction FCMPG = new FCMPG();
141    public static final Instruction DCMPL = new DCMPL();
142    public static final Instruction DCMPG = new DCMPG();
143    public static final ReturnInstruction IRETURN = new IRETURN();
144    public static final ReturnInstruction LRETURN = new LRETURN();
145    public static final ReturnInstruction FRETURN = new FRETURN();
146    public static final ReturnInstruction DRETURN = new DRETURN();
147    public static final ReturnInstruction ARETURN = new ARETURN();
148    public static final ReturnInstruction RETURN = new RETURN();
149    public static final Instruction ARRAYLENGTH = new ARRAYLENGTH();
150    public static final Instruction ATHROW = new ATHROW();
151    public static final Instruction MONITORENTER = new MONITORENTER();
152    public static final Instruction MONITOREXIT = new MONITOREXIT();
153
154    /** You can use these constants in multiple places safely, if you can guarantee
155     * that you will never alter their internal values, e.g. call setIndex().
156     */
157    public static final LocalVariableInstruction THIS = new ALOAD(0);
158    public static final LocalVariableInstruction ALOAD_0 = THIS;
159    public static final LocalVariableInstruction ALOAD_1 = new ALOAD(1);
160    public static final LocalVariableInstruction ALOAD_2 = new ALOAD(2);
161    public static final LocalVariableInstruction ILOAD_0 = new ILOAD(0);
162    public static final LocalVariableInstruction ILOAD_1 = new ILOAD(1);
163    public static final LocalVariableInstruction ILOAD_2 = new ILOAD(2);
164    public static final LocalVariableInstruction ASTORE_0 = new ASTORE(0);
165    public static final LocalVariableInstruction ASTORE_1 = new ASTORE(1);
166    public static final LocalVariableInstruction ASTORE_2 = new ASTORE(2);
167    public static final LocalVariableInstruction ISTORE_0 = new ISTORE(0);
168    public static final LocalVariableInstruction ISTORE_1 = new ISTORE(1);
169    public static final LocalVariableInstruction ISTORE_2 = new ISTORE(2);
170
171    /** Get object via its opcode, for immutable instructions like
172     * branch instructions entries are set to null.
173     */
174    private static final Instruction[] INSTRUCTIONS = new Instruction[256];
175
176    static {
177        INSTRUCTIONS[Const.NOP] = NOP;
178        INSTRUCTIONS[Const.ACONST_NULL] = ACONST_NULL;
179        INSTRUCTIONS[Const.ICONST_M1] = ICONST_M1;
180        INSTRUCTIONS[Const.ICONST_0] = ICONST_0;
181        INSTRUCTIONS[Const.ICONST_1] = ICONST_1;
182        INSTRUCTIONS[Const.ICONST_2] = ICONST_2;
183        INSTRUCTIONS[Const.ICONST_3] = ICONST_3;
184        INSTRUCTIONS[Const.ICONST_4] = ICONST_4;
185        INSTRUCTIONS[Const.ICONST_5] = ICONST_5;
186        INSTRUCTIONS[Const.LCONST_0] = LCONST_0;
187        INSTRUCTIONS[Const.LCONST_1] = LCONST_1;
188        INSTRUCTIONS[Const.FCONST_0] = FCONST_0;
189        INSTRUCTIONS[Const.FCONST_1] = FCONST_1;
190        INSTRUCTIONS[Const.FCONST_2] = FCONST_2;
191        INSTRUCTIONS[Const.DCONST_0] = DCONST_0;
192        INSTRUCTIONS[Const.DCONST_1] = DCONST_1;
193        INSTRUCTIONS[Const.IALOAD] = IALOAD;
194        INSTRUCTIONS[Const.LALOAD] = LALOAD;
195        INSTRUCTIONS[Const.FALOAD] = FALOAD;
196        INSTRUCTIONS[Const.DALOAD] = DALOAD;
197        INSTRUCTIONS[Const.AALOAD] = AALOAD;
198        INSTRUCTIONS[Const.BALOAD] = BALOAD;
199        INSTRUCTIONS[Const.CALOAD] = CALOAD;
200        INSTRUCTIONS[Const.SALOAD] = SALOAD;
201        INSTRUCTIONS[Const.IASTORE] = IASTORE;
202        INSTRUCTIONS[Const.LASTORE] = LASTORE;
203        INSTRUCTIONS[Const.FASTORE] = FASTORE;
204        INSTRUCTIONS[Const.DASTORE] = DASTORE;
205        INSTRUCTIONS[Const.AASTORE] = AASTORE;
206        INSTRUCTIONS[Const.BASTORE] = BASTORE;
207        INSTRUCTIONS[Const.CASTORE] = CASTORE;
208        INSTRUCTIONS[Const.SASTORE] = SASTORE;
209        INSTRUCTIONS[Const.POP] = POP;
210        INSTRUCTIONS[Const.POP2] = POP2;
211        INSTRUCTIONS[Const.DUP] = DUP;
212        INSTRUCTIONS[Const.DUP_X1] = DUP_X1;
213        INSTRUCTIONS[Const.DUP_X2] = DUP_X2;
214        INSTRUCTIONS[Const.DUP2] = DUP2;
215        INSTRUCTIONS[Const.DUP2_X1] = DUP2_X1;
216        INSTRUCTIONS[Const.DUP2_X2] = DUP2_X2;
217        INSTRUCTIONS[Const.SWAP] = SWAP;
218        INSTRUCTIONS[Const.IADD] = IADD;
219        INSTRUCTIONS[Const.LADD] = LADD;
220        INSTRUCTIONS[Const.FADD] = FADD;
221        INSTRUCTIONS[Const.DADD] = DADD;
222        INSTRUCTIONS[Const.ISUB] = ISUB;
223        INSTRUCTIONS[Const.LSUB] = LSUB;
224        INSTRUCTIONS[Const.FSUB] = FSUB;
225        INSTRUCTIONS[Const.DSUB] = DSUB;
226        INSTRUCTIONS[Const.IMUL] = IMUL;
227        INSTRUCTIONS[Const.LMUL] = LMUL;
228        INSTRUCTIONS[Const.FMUL] = FMUL;
229        INSTRUCTIONS[Const.DMUL] = DMUL;
230        INSTRUCTIONS[Const.IDIV] = IDIV;
231        INSTRUCTIONS[Const.LDIV] = LDIV;
232        INSTRUCTIONS[Const.FDIV] = FDIV;
233        INSTRUCTIONS[Const.DDIV] = DDIV;
234        INSTRUCTIONS[Const.IREM] = IREM;
235        INSTRUCTIONS[Const.LREM] = LREM;
236        INSTRUCTIONS[Const.FREM] = FREM;
237        INSTRUCTIONS[Const.DREM] = DREM;
238        INSTRUCTIONS[Const.INEG] = INEG;
239        INSTRUCTIONS[Const.LNEG] = LNEG;
240        INSTRUCTIONS[Const.FNEG] = FNEG;
241        INSTRUCTIONS[Const.DNEG] = DNEG;
242        INSTRUCTIONS[Const.ISHL] = ISHL;
243        INSTRUCTIONS[Const.LSHL] = LSHL;
244        INSTRUCTIONS[Const.ISHR] = ISHR;
245        INSTRUCTIONS[Const.LSHR] = LSHR;
246        INSTRUCTIONS[Const.IUSHR] = IUSHR;
247        INSTRUCTIONS[Const.LUSHR] = LUSHR;
248        INSTRUCTIONS[Const.IAND] = IAND;
249        INSTRUCTIONS[Const.LAND] = LAND;
250        INSTRUCTIONS[Const.IOR] = IOR;
251        INSTRUCTIONS[Const.LOR] = LOR;
252        INSTRUCTIONS[Const.IXOR] = IXOR;
253        INSTRUCTIONS[Const.LXOR] = LXOR;
254        INSTRUCTIONS[Const.I2L] = I2L;
255        INSTRUCTIONS[Const.I2F] = I2F;
256        INSTRUCTIONS[Const.I2D] = I2D;
257        INSTRUCTIONS[Const.L2I] = L2I;
258        INSTRUCTIONS[Const.L2F] = L2F;
259        INSTRUCTIONS[Const.L2D] = L2D;
260        INSTRUCTIONS[Const.F2I] = F2I;
261        INSTRUCTIONS[Const.F2L] = F2L;
262        INSTRUCTIONS[Const.F2D] = F2D;
263        INSTRUCTIONS[Const.D2I] = D2I;
264        INSTRUCTIONS[Const.D2L] = D2L;
265        INSTRUCTIONS[Const.D2F] = D2F;
266        INSTRUCTIONS[Const.I2B] = I2B;
267        INSTRUCTIONS[Const.I2C] = I2C;
268        INSTRUCTIONS[Const.I2S] = I2S;
269        INSTRUCTIONS[Const.LCMP] = LCMP;
270        INSTRUCTIONS[Const.FCMPL] = FCMPL;
271        INSTRUCTIONS[Const.FCMPG] = FCMPG;
272        INSTRUCTIONS[Const.DCMPL] = DCMPL;
273        INSTRUCTIONS[Const.DCMPG] = DCMPG;
274        INSTRUCTIONS[Const.IRETURN] = IRETURN;
275        INSTRUCTIONS[Const.LRETURN] = LRETURN;
276        INSTRUCTIONS[Const.FRETURN] = FRETURN;
277        INSTRUCTIONS[Const.DRETURN] = DRETURN;
278        INSTRUCTIONS[Const.ARETURN] = ARETURN;
279        INSTRUCTIONS[Const.RETURN] = RETURN;
280        INSTRUCTIONS[Const.ARRAYLENGTH] = ARRAYLENGTH;
281        INSTRUCTIONS[Const.ATHROW] = ATHROW;
282        INSTRUCTIONS[Const.MONITORENTER] = MONITORENTER;
283        INSTRUCTIONS[Const.MONITOREXIT] = MONITOREXIT;
284    }
285
286    private InstructionConst() { } // non-instantiable
287
288    /**
289     * Gets the Instruction.
290     * @param index the index, e.g. {@link Const#RETURN}
291     * @return the entry from the private INSTRUCTIONS table
292     */
293    public static Instruction getInstruction(final int index) {
294        return INSTRUCTIONS[index];
295    }
296}