Home | History | Annotate | Download | only in generic
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  */
     18 package org.apache.bcel.generic;
     19 
     20 import org.apache.bcel.Const;
     21 
     22 /**
     23  * This interface contains shareable instruction objects.
     24  *
     25  * In order to save memory you can use some instructions multiply,
     26  * since they have an immutable state and are directly derived from
     27  * Instruction.  I.e. they have no instance fields that could be
     28  * changed. Since some of these instructions like ICONST_0 occur
     29  * very frequently this can save a lot of time and space. This
     30  * feature is an adaptation of the FlyWeight design pattern, we
     31  * just use an array instead of a factory.
     32  *
     33  * The Instructions can also accessed directly under their names, so
     34  * it's possible to write il.append(Instruction.ICONST_0);
     35  *
     36  * @version $Id: InstructionConstants.java 1695415 2015-08-12 01:02:39Z chas $
     37  */
     38 public final class InstructionConst {
     39 
     40     /**
     41      * Predefined instruction objects
     42      */
     43     /*
     44      * NOTE these are not currently immutable, because Instruction
     45      * has mutable protected fields opcode and length.
     46      */
     47     public static final Instruction NOP = new NOP();
     48     public static final Instruction ACONST_NULL = new ACONST_NULL();
     49     public static final Instruction ICONST_M1 = new ICONST(-1);
     50     public static final Instruction ICONST_0 = new ICONST(0);
     51     public static final Instruction ICONST_1 = new ICONST(1);
     52     public static final Instruction ICONST_2 = new ICONST(2);
     53     public static final Instruction ICONST_3 = new ICONST(3);
     54     public static final Instruction ICONST_4 = new ICONST(4);
     55     public static final Instruction ICONST_5 = new ICONST(5);
     56     public static final Instruction LCONST_0 = new LCONST(0);
     57     public static final Instruction LCONST_1 = new LCONST(1);
     58     public static final Instruction FCONST_0 = new FCONST(0);
     59     public static final Instruction FCONST_1 = new FCONST(1);
     60     public static final Instruction FCONST_2 = new FCONST(2);
     61     public static final Instruction DCONST_0 = new DCONST(0);
     62     public static final Instruction DCONST_1 = new DCONST(1);
     63     public static final ArrayInstruction IALOAD = new IALOAD();
     64     public static final ArrayInstruction LALOAD = new LALOAD();
     65     public static final ArrayInstruction FALOAD = new FALOAD();
     66     public static final ArrayInstruction DALOAD = new DALOAD();
     67     public static final ArrayInstruction AALOAD = new AALOAD();
     68     public static final ArrayInstruction BALOAD = new BALOAD();
     69     public static final ArrayInstruction CALOAD = new CALOAD();
     70     public static final ArrayInstruction SALOAD = new SALOAD();
     71     public static final ArrayInstruction IASTORE = new IASTORE();
     72     public static final ArrayInstruction LASTORE = new LASTORE();
     73     public static final ArrayInstruction FASTORE = new FASTORE();
     74     public static final ArrayInstruction DASTORE = new DASTORE();
     75     public static final ArrayInstruction AASTORE = new AASTORE();
     76     public static final ArrayInstruction BASTORE = new BASTORE();
     77     public static final ArrayInstruction CASTORE = new CASTORE();
     78     public static final ArrayInstruction SASTORE = new SASTORE();
     79     public static final StackInstruction POP = new POP();
     80     public static final StackInstruction POP2 = new POP2();
     81     public static final StackInstruction DUP = new DUP();
     82     public static final StackInstruction DUP_X1 = new DUP_X1();
     83     public static final StackInstruction DUP_X2 = new DUP_X2();
     84     public static final StackInstruction DUP2 = new DUP2();
     85     public static final StackInstruction DUP2_X1 = new DUP2_X1();
     86     public static final StackInstruction DUP2_X2 = new DUP2_X2();
     87     public static final StackInstruction SWAP = new SWAP();
     88     public static final ArithmeticInstruction IADD = new IADD();
     89     public static final ArithmeticInstruction LADD = new LADD();
     90     public static final ArithmeticInstruction FADD = new FADD();
     91     public static final ArithmeticInstruction DADD = new DADD();
     92     public static final ArithmeticInstruction ISUB = new ISUB();
     93     public static final ArithmeticInstruction LSUB = new LSUB();
     94     public static final ArithmeticInstruction FSUB = new FSUB();
     95     public static final ArithmeticInstruction DSUB = new DSUB();
     96     public static final ArithmeticInstruction IMUL = new IMUL();
     97     public static final ArithmeticInstruction LMUL = new LMUL();
     98     public static final ArithmeticInstruction FMUL = new FMUL();
     99     public static final ArithmeticInstruction DMUL = new DMUL();
    100     public static final ArithmeticInstruction IDIV = new IDIV();
    101     public static final ArithmeticInstruction LDIV = new LDIV();
    102     public static final ArithmeticInstruction FDIV = new FDIV();
    103     public static final ArithmeticInstruction DDIV = new DDIV();
    104     public static final ArithmeticInstruction IREM = new IREM();
    105     public static final ArithmeticInstruction LREM = new LREM();
    106     public static final ArithmeticInstruction FREM = new FREM();
    107     public static final ArithmeticInstruction DREM = new DREM();
    108     public static final ArithmeticInstruction INEG = new INEG();
    109     public static final ArithmeticInstruction LNEG = new LNEG();
    110     public static final ArithmeticInstruction FNEG = new FNEG();
    111     public static final ArithmeticInstruction DNEG = new DNEG();
    112     public static final ArithmeticInstruction ISHL = new ISHL();
    113     public static final ArithmeticInstruction LSHL = new LSHL();
    114     public static final ArithmeticInstruction ISHR = new ISHR();
    115     public static final ArithmeticInstruction LSHR = new LSHR();
    116     public static final ArithmeticInstruction IUSHR = new IUSHR();
    117     public static final ArithmeticInstruction LUSHR = new LUSHR();
    118     public static final ArithmeticInstruction IAND = new IAND();
    119     public static final ArithmeticInstruction LAND = new LAND();
    120     public static final ArithmeticInstruction IOR = new IOR();
    121     public static final ArithmeticInstruction LOR = new LOR();
    122     public static final ArithmeticInstruction IXOR = new IXOR();
    123     public static final ArithmeticInstruction LXOR = new LXOR();
    124     public static final ConversionInstruction I2L = new I2L();
    125     public static final ConversionInstruction I2F = new I2F();
    126     public static final ConversionInstruction I2D = new I2D();
    127     public static final ConversionInstruction L2I = new L2I();
    128     public static final ConversionInstruction L2F = new L2F();
    129     public static final ConversionInstruction L2D = new L2D();
    130     public static final ConversionInstruction F2I = new F2I();
    131     public static final ConversionInstruction F2L = new F2L();
    132     public static final ConversionInstruction F2D = new F2D();
    133     public static final ConversionInstruction D2I = new D2I();
    134     public static final ConversionInstruction D2L = new D2L();
    135     public static final ConversionInstruction D2F = new D2F();
    136     public static final ConversionInstruction I2B = new I2B();
    137     public static final ConversionInstruction I2C = new I2C();
    138     public static final ConversionInstruction I2S = new I2S();
    139     public static final Instruction LCMP = new LCMP();
    140     public static final Instruction FCMPL = new FCMPL();
    141     public static final Instruction FCMPG = new FCMPG();
    142     public static final Instruction DCMPL = new DCMPL();
    143     public static final Instruction DCMPG = new DCMPG();
    144     public static final ReturnInstruction IRETURN = new IRETURN();
    145     public static final ReturnInstruction LRETURN = new LRETURN();
    146     public static final ReturnInstruction FRETURN = new FRETURN();
    147     public static final ReturnInstruction DRETURN = new DRETURN();
    148     public static final ReturnInstruction ARETURN = new ARETURN();
    149     public static final ReturnInstruction RETURN = new RETURN();
    150     public static final Instruction ARRAYLENGTH = new ARRAYLENGTH();
    151     public static final Instruction ATHROW = new ATHROW();
    152     public static final Instruction MONITORENTER = new MONITORENTER();
    153     public static final Instruction MONITOREXIT = new MONITOREXIT();
    154 
    155     /** You can use these constants in multiple places safely, if you can guarantee
    156      * that you will never alter their internal values, e.g. call setIndex().
    157      */
    158     public static final LocalVariableInstruction THIS = new ALOAD(0);
    159     public static final LocalVariableInstruction ALOAD_0 = THIS;
    160     public static final LocalVariableInstruction ALOAD_1 = new ALOAD(1);
    161     public static final LocalVariableInstruction ALOAD_2 = new ALOAD(2);
    162     public static final LocalVariableInstruction ILOAD_0 = new ILOAD(0);
    163     public static final LocalVariableInstruction ILOAD_1 = new ILOAD(1);
    164     public static final LocalVariableInstruction ILOAD_2 = new ILOAD(2);
    165     public static final LocalVariableInstruction ASTORE_0 = new ASTORE(0);
    166     public static final LocalVariableInstruction ASTORE_1 = new ASTORE(1);
    167     public static final LocalVariableInstruction ASTORE_2 = new ASTORE(2);
    168     public static final LocalVariableInstruction ISTORE_0 = new ISTORE(0);
    169     public static final LocalVariableInstruction ISTORE_1 = new ISTORE(1);
    170     public static final LocalVariableInstruction ISTORE_2 = new ISTORE(2);
    171 
    172     /** Get object via its opcode, for immutable instructions like
    173      * branch instructions entries are set to null.
    174      */
    175     private static final Instruction[] INSTRUCTIONS = new Instruction[256];
    176 
    177     static {
    178         INSTRUCTIONS[Const.NOP] = NOP;
    179         INSTRUCTIONS[Const.ACONST_NULL] = ACONST_NULL;
    180         INSTRUCTIONS[Const.ICONST_M1] = ICONST_M1;
    181         INSTRUCTIONS[Const.ICONST_0] = ICONST_0;
    182         INSTRUCTIONS[Const.ICONST_1] = ICONST_1;
    183         INSTRUCTIONS[Const.ICONST_2] = ICONST_2;
    184         INSTRUCTIONS[Const.ICONST_3] = ICONST_3;
    185         INSTRUCTIONS[Const.ICONST_4] = ICONST_4;
    186         INSTRUCTIONS[Const.ICONST_5] = ICONST_5;
    187         INSTRUCTIONS[Const.LCONST_0] = LCONST_0;
    188         INSTRUCTIONS[Const.LCONST_1] = LCONST_1;
    189         INSTRUCTIONS[Const.FCONST_0] = FCONST_0;
    190         INSTRUCTIONS[Const.FCONST_1] = FCONST_1;
    191         INSTRUCTIONS[Const.FCONST_2] = FCONST_2;
    192         INSTRUCTIONS[Const.DCONST_0] = DCONST_0;
    193         INSTRUCTIONS[Const.DCONST_1] = DCONST_1;
    194         INSTRUCTIONS[Const.IALOAD] = IALOAD;
    195         INSTRUCTIONS[Const.LALOAD] = LALOAD;
    196         INSTRUCTIONS[Const.FALOAD] = FALOAD;
    197         INSTRUCTIONS[Const.DALOAD] = DALOAD;
    198         INSTRUCTIONS[Const.AALOAD] = AALOAD;
    199         INSTRUCTIONS[Const.BALOAD] = BALOAD;
    200         INSTRUCTIONS[Const.CALOAD] = CALOAD;
    201         INSTRUCTIONS[Const.SALOAD] = SALOAD;
    202         INSTRUCTIONS[Const.IASTORE] = IASTORE;
    203         INSTRUCTIONS[Const.LASTORE] = LASTORE;
    204         INSTRUCTIONS[Const.FASTORE] = FASTORE;
    205         INSTRUCTIONS[Const.DASTORE] = DASTORE;
    206         INSTRUCTIONS[Const.AASTORE] = AASTORE;
    207         INSTRUCTIONS[Const.BASTORE] = BASTORE;
    208         INSTRUCTIONS[Const.CASTORE] = CASTORE;
    209         INSTRUCTIONS[Const.SASTORE] = SASTORE;
    210         INSTRUCTIONS[Const.POP] = POP;
    211         INSTRUCTIONS[Const.POP2] = POP2;
    212         INSTRUCTIONS[Const.DUP] = DUP;
    213         INSTRUCTIONS[Const.DUP_X1] = DUP_X1;
    214         INSTRUCTIONS[Const.DUP_X2] = DUP_X2;
    215         INSTRUCTIONS[Const.DUP2] = DUP2;
    216         INSTRUCTIONS[Const.DUP2_X1] = DUP2_X1;
    217         INSTRUCTIONS[Const.DUP2_X2] = DUP2_X2;
    218         INSTRUCTIONS[Const.SWAP] = SWAP;
    219         INSTRUCTIONS[Const.IADD] = IADD;
    220         INSTRUCTIONS[Const.LADD] = LADD;
    221         INSTRUCTIONS[Const.FADD] = FADD;
    222         INSTRUCTIONS[Const.DADD] = DADD;
    223         INSTRUCTIONS[Const.ISUB] = ISUB;
    224         INSTRUCTIONS[Const.LSUB] = LSUB;
    225         INSTRUCTIONS[Const.FSUB] = FSUB;
    226         INSTRUCTIONS[Const.DSUB] = DSUB;
    227         INSTRUCTIONS[Const.IMUL] = IMUL;
    228         INSTRUCTIONS[Const.LMUL] = LMUL;
    229         INSTRUCTIONS[Const.FMUL] = FMUL;
    230         INSTRUCTIONS[Const.DMUL] = DMUL;
    231         INSTRUCTIONS[Const.IDIV] = IDIV;
    232         INSTRUCTIONS[Const.LDIV] = LDIV;
    233         INSTRUCTIONS[Const.FDIV] = FDIV;
    234         INSTRUCTIONS[Const.DDIV] = DDIV;
    235         INSTRUCTIONS[Const.IREM] = IREM;
    236         INSTRUCTIONS[Const.LREM] = LREM;
    237         INSTRUCTIONS[Const.FREM] = FREM;
    238         INSTRUCTIONS[Const.DREM] = DREM;
    239         INSTRUCTIONS[Const.INEG] = INEG;
    240         INSTRUCTIONS[Const.LNEG] = LNEG;
    241         INSTRUCTIONS[Const.FNEG] = FNEG;
    242         INSTRUCTIONS[Const.DNEG] = DNEG;
    243         INSTRUCTIONS[Const.ISHL] = ISHL;
    244         INSTRUCTIONS[Const.LSHL] = LSHL;
    245         INSTRUCTIONS[Const.ISHR] = ISHR;
    246         INSTRUCTIONS[Const.LSHR] = LSHR;
    247         INSTRUCTIONS[Const.IUSHR] = IUSHR;
    248         INSTRUCTIONS[Const.LUSHR] = LUSHR;
    249         INSTRUCTIONS[Const.IAND] = IAND;
    250         INSTRUCTIONS[Const.LAND] = LAND;
    251         INSTRUCTIONS[Const.IOR] = IOR;
    252         INSTRUCTIONS[Const.LOR] = LOR;
    253         INSTRUCTIONS[Const.IXOR] = IXOR;
    254         INSTRUCTIONS[Const.LXOR] = LXOR;
    255         INSTRUCTIONS[Const.I2L] = I2L;
    256         INSTRUCTIONS[Const.I2F] = I2F;
    257         INSTRUCTIONS[Const.I2D] = I2D;
    258         INSTRUCTIONS[Const.L2I] = L2I;
    259         INSTRUCTIONS[Const.L2F] = L2F;
    260         INSTRUCTIONS[Const.L2D] = L2D;
    261         INSTRUCTIONS[Const.F2I] = F2I;
    262         INSTRUCTIONS[Const.F2L] = F2L;
    263         INSTRUCTIONS[Const.F2D] = F2D;
    264         INSTRUCTIONS[Const.D2I] = D2I;
    265         INSTRUCTIONS[Const.D2L] = D2L;
    266         INSTRUCTIONS[Const.D2F] = D2F;
    267         INSTRUCTIONS[Const.I2B] = I2B;
    268         INSTRUCTIONS[Const.I2C] = I2C;
    269         INSTRUCTIONS[Const.I2S] = I2S;
    270         INSTRUCTIONS[Const.LCMP] = LCMP;
    271         INSTRUCTIONS[Const.FCMPL] = FCMPL;
    272         INSTRUCTIONS[Const.FCMPG] = FCMPG;
    273         INSTRUCTIONS[Const.DCMPL] = DCMPL;
    274         INSTRUCTIONS[Const.DCMPG] = DCMPG;
    275         INSTRUCTIONS[Const.IRETURN] = IRETURN;
    276         INSTRUCTIONS[Const.LRETURN] = LRETURN;
    277         INSTRUCTIONS[Const.FRETURN] = FRETURN;
    278         INSTRUCTIONS[Const.DRETURN] = DRETURN;
    279         INSTRUCTIONS[Const.ARETURN] = ARETURN;
    280         INSTRUCTIONS[Const.RETURN] = RETURN;
    281         INSTRUCTIONS[Const.ARRAYLENGTH] = ARRAYLENGTH;
    282         INSTRUCTIONS[Const.ATHROW] = ATHROW;
    283         INSTRUCTIONS[Const.MONITORENTER] = MONITORENTER;
    284         INSTRUCTIONS[Const.MONITOREXIT] = MONITOREXIT;
    285     }
    286 
    287     private InstructionConst() { } // non-instantiable
    288 
    289     /**
    290      * Gets the Instruction.
    291      * @param index the index, e.g. {@link Const#RETURN}
    292      * @return the entry from the private INSTRUCTIONS table
    293      */
    294     public static Instruction getInstruction(final int index) {
    295         return INSTRUCTIONS[index];
    296     }
    297 }
    298