ArithmeticInstruction.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.  * Super class for the family of arithmetic instructions.
  21.  */
  22. public abstract class ArithmeticInstruction extends Instruction implements TypedInstruction, StackProducer, StackConsumer {

  23.     /**
  24.      * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
  25.      */
  26.     ArithmeticInstruction() {
  27.     }

  28.     /**
  29.      * @param opcode of instruction
  30.      */
  31.     protected ArithmeticInstruction(final short opcode) {
  32.         super(opcode, (short) 1);
  33.     }

  34.     /**
  35.      * @return type associated with the instruction
  36.      */
  37.     @Override
  38.     public Type getType(final ConstantPoolGen cp) {
  39.         final short opcode = super.getOpcode();
  40.         switch (opcode) {
  41.         case Const.DADD:
  42.         case Const.DDIV:
  43.         case Const.DMUL:
  44.         case Const.DNEG:
  45.         case Const.DREM:
  46.         case Const.DSUB:
  47.             return Type.DOUBLE;
  48.         case Const.FADD:
  49.         case Const.FDIV:
  50.         case Const.FMUL:
  51.         case Const.FNEG:
  52.         case Const.FREM:
  53.         case Const.FSUB:
  54.             return Type.FLOAT;
  55.         case Const.IADD:
  56.         case Const.IAND:
  57.         case Const.IDIV:
  58.         case Const.IMUL:
  59.         case Const.INEG:
  60.         case Const.IOR:
  61.         case Const.IREM:
  62.         case Const.ISHL:
  63.         case Const.ISHR:
  64.         case Const.ISUB:
  65.         case Const.IUSHR:
  66.         case Const.IXOR:
  67.             return Type.INT;
  68.         case Const.LADD:
  69.         case Const.LAND:
  70.         case Const.LDIV:
  71.         case Const.LMUL:
  72.         case Const.LNEG:
  73.         case Const.LOR:
  74.         case Const.LREM:
  75.         case Const.LSHL:
  76.         case Const.LSHR:
  77.         case Const.LSUB:
  78.         case Const.LUSHR:
  79.         case Const.LXOR:
  80.             return Type.LONG;
  81.         default: // Never reached
  82.             throw new ClassGenException("Unknown type " + opcode);
  83.         }
  84.     }
  85. }