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