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