InstructionConst.java

  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. package org.apache.bcel.generic;

  18. import org.apache.bcel.Const;

  19. /**
  20.  * Contains shareable instruction objects.
  21.  * <p>
  22.  * In order to save memory you can use some instructions multiply, since they have an immutable state and are directly
  23.  * derived from Instruction. I.e. they have no instance fields that could be changed. Since some of these instructions
  24.  * like ICONST_0 occur very frequently this can save a lot of time and space. This feature is an adaptation of the
  25.  * FlyWeight design pattern, we just use an array instead of a factory.
  26.  * </p>
  27.  * <p>
  28.  * The Instructions can also accessed directly under their names, so it's possible to write
  29.  * il.append(Instruction.ICONST_0);
  30.  * </p>
  31.  */
  32. public final class InstructionConst {

  33.     /**
  34.      * Predefined instruction objects.
  35.      *
  36.      * NOTE these are not currently immutable, because Instruction has mutable protected fields opcode and length.
  37.      */
  38.     public static final Instruction NOP = new NOP();
  39.     public static final Instruction ACONST_NULL = new ACONST_NULL();
  40.     public static final Instruction ICONST_M1 = new ICONST(-1);
  41.     public static final Instruction ICONST_0 = new ICONST(0);
  42.     public static final Instruction ICONST_1 = new ICONST(1);
  43.     public static final Instruction ICONST_2 = new ICONST(2);
  44.     public static final Instruction ICONST_3 = new ICONST(3);
  45.     public static final Instruction ICONST_4 = new ICONST(4);
  46.     public static final Instruction ICONST_5 = new ICONST(5);
  47.     public static final Instruction LCONST_0 = new LCONST(0);
  48.     public static final Instruction LCONST_1 = new LCONST(1);
  49.     public static final Instruction FCONST_0 = new FCONST(0);
  50.     public static final Instruction FCONST_1 = new FCONST(1);
  51.     public static final Instruction FCONST_2 = new FCONST(2);
  52.     public static final Instruction DCONST_0 = new DCONST(0);
  53.     public static final Instruction DCONST_1 = new DCONST(1);
  54.     public static final ArrayInstruction IALOAD = new IALOAD();
  55.     public static final ArrayInstruction LALOAD = new LALOAD();
  56.     public static final ArrayInstruction FALOAD = new FALOAD();
  57.     public static final ArrayInstruction DALOAD = new DALOAD();
  58.     public static final ArrayInstruction AALOAD = new AALOAD();
  59.     public static final ArrayInstruction BALOAD = new BALOAD();
  60.     public static final ArrayInstruction CALOAD = new CALOAD();
  61.     public static final ArrayInstruction SALOAD = new SALOAD();
  62.     public static final ArrayInstruction IASTORE = new IASTORE();
  63.     public static final ArrayInstruction LASTORE = new LASTORE();
  64.     public static final ArrayInstruction FASTORE = new FASTORE();
  65.     public static final ArrayInstruction DASTORE = new DASTORE();
  66.     public static final ArrayInstruction AASTORE = new AASTORE();
  67.     public static final ArrayInstruction BASTORE = new BASTORE();
  68.     public static final ArrayInstruction CASTORE = new CASTORE();
  69.     public static final ArrayInstruction SASTORE = new SASTORE();
  70.     public static final StackInstruction POP = new POP();
  71.     public static final StackInstruction POP2 = new POP2();
  72.     public static final StackInstruction DUP = new DUP();
  73.     public static final StackInstruction DUP_X1 = new DUP_X1();
  74.     public static final StackInstruction DUP_X2 = new DUP_X2();
  75.     public static final StackInstruction DUP2 = new DUP2();
  76.     public static final StackInstruction DUP2_X1 = new DUP2_X1();
  77.     public static final StackInstruction DUP2_X2 = new DUP2_X2();
  78.     public static final StackInstruction SWAP = new SWAP();
  79.     public static final ArithmeticInstruction IADD = new IADD();
  80.     public static final ArithmeticInstruction LADD = new LADD();
  81.     public static final ArithmeticInstruction FADD = new FADD();
  82.     public static final ArithmeticInstruction DADD = new DADD();
  83.     public static final ArithmeticInstruction ISUB = new ISUB();
  84.     public static final ArithmeticInstruction LSUB = new LSUB();
  85.     public static final ArithmeticInstruction FSUB = new FSUB();
  86.     public static final ArithmeticInstruction DSUB = new DSUB();
  87.     public static final ArithmeticInstruction IMUL = new IMUL();
  88.     public static final ArithmeticInstruction LMUL = new LMUL();
  89.     public static final ArithmeticInstruction FMUL = new FMUL();
  90.     public static final ArithmeticInstruction DMUL = new DMUL();
  91.     public static final ArithmeticInstruction IDIV = new IDIV();
  92.     public static final ArithmeticInstruction LDIV = new LDIV();
  93.     public static final ArithmeticInstruction FDIV = new FDIV();
  94.     public static final ArithmeticInstruction DDIV = new DDIV();
  95.     public static final ArithmeticInstruction IREM = new IREM();
  96.     public static final ArithmeticInstruction LREM = new LREM();
  97.     public static final ArithmeticInstruction FREM = new FREM();
  98.     public static final ArithmeticInstruction DREM = new DREM();
  99.     public static final ArithmeticInstruction INEG = new INEG();
  100.     public static final ArithmeticInstruction LNEG = new LNEG();
  101.     public static final ArithmeticInstruction FNEG = new FNEG();
  102.     public static final ArithmeticInstruction DNEG = new DNEG();
  103.     public static final ArithmeticInstruction ISHL = new ISHL();
  104.     public static final ArithmeticInstruction LSHL = new LSHL();
  105.     public static final ArithmeticInstruction ISHR = new ISHR();
  106.     public static final ArithmeticInstruction LSHR = new LSHR();
  107.     public static final ArithmeticInstruction IUSHR = new IUSHR();
  108.     public static final ArithmeticInstruction LUSHR = new LUSHR();
  109.     public static final ArithmeticInstruction IAND = new IAND();
  110.     public static final ArithmeticInstruction LAND = new LAND();
  111.     public static final ArithmeticInstruction IOR = new IOR();
  112.     public static final ArithmeticInstruction LOR = new LOR();
  113.     public static final ArithmeticInstruction IXOR = new IXOR();
  114.     public static final ArithmeticInstruction LXOR = new LXOR();
  115.     public static final ConversionInstruction I2L = new I2L();
  116.     public static final ConversionInstruction I2F = new I2F();
  117.     public static final ConversionInstruction I2D = new I2D();
  118.     public static final ConversionInstruction L2I = new L2I();
  119.     public static final ConversionInstruction L2F = new L2F();
  120.     public static final ConversionInstruction L2D = new L2D();
  121.     public static final ConversionInstruction F2I = new F2I();
  122.     public static final ConversionInstruction F2L = new F2L();
  123.     public static final ConversionInstruction F2D = new F2D();
  124.     public static final ConversionInstruction D2I = new D2I();
  125.     public static final ConversionInstruction D2L = new D2L();
  126.     public static final ConversionInstruction D2F = new D2F();
  127.     public static final ConversionInstruction I2B = new I2B();
  128.     public static final ConversionInstruction I2C = new I2C();
  129.     public static final ConversionInstruction I2S = new I2S();
  130.     public static final Instruction LCMP = new LCMP();
  131.     public static final Instruction FCMPL = new FCMPL();
  132.     public static final Instruction FCMPG = new FCMPG();
  133.     public static final Instruction DCMPL = new DCMPL();
  134.     public static final Instruction DCMPG = new DCMPG();
  135.     public static final ReturnInstruction IRETURN = new IRETURN();
  136.     public static final ReturnInstruction LRETURN = new LRETURN();
  137.     public static final ReturnInstruction FRETURN = new FRETURN();
  138.     public static final ReturnInstruction DRETURN = new DRETURN();
  139.     public static final ReturnInstruction ARETURN = new ARETURN();
  140.     public static final ReturnInstruction RETURN = new RETURN();
  141.     public static final Instruction ARRAYLENGTH = new ARRAYLENGTH();
  142.     public static final Instruction ATHROW = new ATHROW();
  143.     public static final Instruction MONITORENTER = new MONITORENTER();
  144.     public static final Instruction MONITOREXIT = new MONITOREXIT();

  145.     /**
  146.      * You can use these constants in multiple places safely, if you can guarantee that you will never alter their internal
  147.      * values, e.g. call setIndex().
  148.      */
  149.     public static final LocalVariableInstruction THIS = new ALOAD(0);
  150.     public static final LocalVariableInstruction ALOAD_0 = THIS;
  151.     public static final LocalVariableInstruction ALOAD_1 = new ALOAD(1);
  152.     public static final LocalVariableInstruction ALOAD_2 = new ALOAD(2);
  153.     public static final LocalVariableInstruction ILOAD_0 = new ILOAD(0);
  154.     public static final LocalVariableInstruction ILOAD_1 = new ILOAD(1);
  155.     public static final LocalVariableInstruction ILOAD_2 = new ILOAD(2);
  156.     public static final LocalVariableInstruction ASTORE_0 = new ASTORE(0);
  157.     public static final LocalVariableInstruction ASTORE_1 = new ASTORE(1);
  158.     public static final LocalVariableInstruction ASTORE_2 = new ASTORE(2);
  159.     public static final LocalVariableInstruction ISTORE_0 = new ISTORE(0);
  160.     public static final LocalVariableInstruction ISTORE_1 = new ISTORE(1);
  161.     public static final LocalVariableInstruction ISTORE_2 = new ISTORE(2);

  162.     /**
  163.      * Gets object via its opcode, for immutable instructions like branch instructions entries are set to null.
  164.      */
  165.     static final Instruction[] INSTRUCTIONS = new Instruction[256];

  166.     static {
  167.         INSTRUCTIONS[Const.NOP] = NOP;
  168.         INSTRUCTIONS[Const.ACONST_NULL] = ACONST_NULL;
  169.         INSTRUCTIONS[Const.ICONST_M1] = ICONST_M1;
  170.         INSTRUCTIONS[Const.ICONST_0] = ICONST_0;
  171.         INSTRUCTIONS[Const.ICONST_1] = ICONST_1;
  172.         INSTRUCTIONS[Const.ICONST_2] = ICONST_2;
  173.         INSTRUCTIONS[Const.ICONST_3] = ICONST_3;
  174.         INSTRUCTIONS[Const.ICONST_4] = ICONST_4;
  175.         INSTRUCTIONS[Const.ICONST_5] = ICONST_5;
  176.         INSTRUCTIONS[Const.LCONST_0] = LCONST_0;
  177.         INSTRUCTIONS[Const.LCONST_1] = LCONST_1;
  178.         INSTRUCTIONS[Const.FCONST_0] = FCONST_0;
  179.         INSTRUCTIONS[Const.FCONST_1] = FCONST_1;
  180.         INSTRUCTIONS[Const.FCONST_2] = FCONST_2;
  181.         INSTRUCTIONS[Const.DCONST_0] = DCONST_0;
  182.         INSTRUCTIONS[Const.DCONST_1] = DCONST_1;
  183.         INSTRUCTIONS[Const.IALOAD] = IALOAD;
  184.         INSTRUCTIONS[Const.LALOAD] = LALOAD;
  185.         INSTRUCTIONS[Const.FALOAD] = FALOAD;
  186.         INSTRUCTIONS[Const.DALOAD] = DALOAD;
  187.         INSTRUCTIONS[Const.AALOAD] = AALOAD;
  188.         INSTRUCTIONS[Const.BALOAD] = BALOAD;
  189.         INSTRUCTIONS[Const.CALOAD] = CALOAD;
  190.         INSTRUCTIONS[Const.SALOAD] = SALOAD;
  191.         INSTRUCTIONS[Const.IASTORE] = IASTORE;
  192.         INSTRUCTIONS[Const.LASTORE] = LASTORE;
  193.         INSTRUCTIONS[Const.FASTORE] = FASTORE;
  194.         INSTRUCTIONS[Const.DASTORE] = DASTORE;
  195.         INSTRUCTIONS[Const.AASTORE] = AASTORE;
  196.         INSTRUCTIONS[Const.BASTORE] = BASTORE;
  197.         INSTRUCTIONS[Const.CASTORE] = CASTORE;
  198.         INSTRUCTIONS[Const.SASTORE] = SASTORE;
  199.         INSTRUCTIONS[Const.POP] = POP;
  200.         INSTRUCTIONS[Const.POP2] = POP2;
  201.         INSTRUCTIONS[Const.DUP] = DUP;
  202.         INSTRUCTIONS[Const.DUP_X1] = DUP_X1;
  203.         INSTRUCTIONS[Const.DUP_X2] = DUP_X2;
  204.         INSTRUCTIONS[Const.DUP2] = DUP2;
  205.         INSTRUCTIONS[Const.DUP2_X1] = DUP2_X1;
  206.         INSTRUCTIONS[Const.DUP2_X2] = DUP2_X2;
  207.         INSTRUCTIONS[Const.SWAP] = SWAP;
  208.         INSTRUCTIONS[Const.IADD] = IADD;
  209.         INSTRUCTIONS[Const.LADD] = LADD;
  210.         INSTRUCTIONS[Const.FADD] = FADD;
  211.         INSTRUCTIONS[Const.DADD] = DADD;
  212.         INSTRUCTIONS[Const.ISUB] = ISUB;
  213.         INSTRUCTIONS[Const.LSUB] = LSUB;
  214.         INSTRUCTIONS[Const.FSUB] = FSUB;
  215.         INSTRUCTIONS[Const.DSUB] = DSUB;
  216.         INSTRUCTIONS[Const.IMUL] = IMUL;
  217.         INSTRUCTIONS[Const.LMUL] = LMUL;
  218.         INSTRUCTIONS[Const.FMUL] = FMUL;
  219.         INSTRUCTIONS[Const.DMUL] = DMUL;
  220.         INSTRUCTIONS[Const.IDIV] = IDIV;
  221.         INSTRUCTIONS[Const.LDIV] = LDIV;
  222.         INSTRUCTIONS[Const.FDIV] = FDIV;
  223.         INSTRUCTIONS[Const.DDIV] = DDIV;
  224.         INSTRUCTIONS[Const.IREM] = IREM;
  225.         INSTRUCTIONS[Const.LREM] = LREM;
  226.         INSTRUCTIONS[Const.FREM] = FREM;
  227.         INSTRUCTIONS[Const.DREM] = DREM;
  228.         INSTRUCTIONS[Const.INEG] = INEG;
  229.         INSTRUCTIONS[Const.LNEG] = LNEG;
  230.         INSTRUCTIONS[Const.FNEG] = FNEG;
  231.         INSTRUCTIONS[Const.DNEG] = DNEG;
  232.         INSTRUCTIONS[Const.ISHL] = ISHL;
  233.         INSTRUCTIONS[Const.LSHL] = LSHL;
  234.         INSTRUCTIONS[Const.ISHR] = ISHR;
  235.         INSTRUCTIONS[Const.LSHR] = LSHR;
  236.         INSTRUCTIONS[Const.IUSHR] = IUSHR;
  237.         INSTRUCTIONS[Const.LUSHR] = LUSHR;
  238.         INSTRUCTIONS[Const.IAND] = IAND;
  239.         INSTRUCTIONS[Const.LAND] = LAND;
  240.         INSTRUCTIONS[Const.IOR] = IOR;
  241.         INSTRUCTIONS[Const.LOR] = LOR;
  242.         INSTRUCTIONS[Const.IXOR] = IXOR;
  243.         INSTRUCTIONS[Const.LXOR] = LXOR;
  244.         INSTRUCTIONS[Const.I2L] = I2L;
  245.         INSTRUCTIONS[Const.I2F] = I2F;
  246.         INSTRUCTIONS[Const.I2D] = I2D;
  247.         INSTRUCTIONS[Const.L2I] = L2I;
  248.         INSTRUCTIONS[Const.L2F] = L2F;
  249.         INSTRUCTIONS[Const.L2D] = L2D;
  250.         INSTRUCTIONS[Const.F2I] = F2I;
  251.         INSTRUCTIONS[Const.F2L] = F2L;
  252.         INSTRUCTIONS[Const.F2D] = F2D;
  253.         INSTRUCTIONS[Const.D2I] = D2I;
  254.         INSTRUCTIONS[Const.D2L] = D2L;
  255.         INSTRUCTIONS[Const.D2F] = D2F;
  256.         INSTRUCTIONS[Const.I2B] = I2B;
  257.         INSTRUCTIONS[Const.I2C] = I2C;
  258.         INSTRUCTIONS[Const.I2S] = I2S;
  259.         INSTRUCTIONS[Const.LCMP] = LCMP;
  260.         INSTRUCTIONS[Const.FCMPL] = FCMPL;
  261.         INSTRUCTIONS[Const.FCMPG] = FCMPG;
  262.         INSTRUCTIONS[Const.DCMPL] = DCMPL;
  263.         INSTRUCTIONS[Const.DCMPG] = DCMPG;
  264.         INSTRUCTIONS[Const.IRETURN] = IRETURN;
  265.         INSTRUCTIONS[Const.LRETURN] = LRETURN;
  266.         INSTRUCTIONS[Const.FRETURN] = FRETURN;
  267.         INSTRUCTIONS[Const.DRETURN] = DRETURN;
  268.         INSTRUCTIONS[Const.ARETURN] = ARETURN;
  269.         INSTRUCTIONS[Const.RETURN] = RETURN;
  270.         INSTRUCTIONS[Const.ARRAYLENGTH] = ARRAYLENGTH;
  271.         INSTRUCTIONS[Const.ATHROW] = ATHROW;
  272.         INSTRUCTIONS[Const.MONITORENTER] = MONITORENTER;
  273.         INSTRUCTIONS[Const.MONITOREXIT] = MONITOREXIT;
  274.     }

  275.     /**
  276.      * Gets the Instruction.
  277.      *
  278.      * @param index the index, e.g. {@link Const#RETURN}
  279.      * @return the entry from the private INSTRUCTIONS table
  280.      */
  281.     public static Instruction getInstruction(final int index) {
  282.         return INSTRUCTIONS[index];
  283.     }

  284.     private InstructionConst() {
  285.     } // non-instantiable
  286. }