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