001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.bcel.generic;
020
021import org.apache.bcel.Const;
022
023/**
024 * Super class for the family of arithmetic instructions.
025 */
026public abstract class ArithmeticInstruction extends Instruction implements TypedInstruction, StackProducer, StackConsumer {
027
028    /**
029     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
030     */
031    ArithmeticInstruction() {
032    }
033
034    /**
035     * Constructs an ArithmeticInstruction.
036     *
037     * @param opcode of instruction.
038     */
039    protected ArithmeticInstruction(final short opcode) {
040        super(opcode, (short) 1);
041    }
042
043    /**
044     * @return type associated with the instruction.
045     */
046    @Override
047    public Type getType(final ConstantPoolGen cp) {
048        final short opcode = super.getOpcode();
049        switch (opcode) {
050        case Const.DADD:
051        case Const.DDIV:
052        case Const.DMUL:
053        case Const.DNEG:
054        case Const.DREM:
055        case Const.DSUB:
056            return Type.DOUBLE;
057        case Const.FADD:
058        case Const.FDIV:
059        case Const.FMUL:
060        case Const.FNEG:
061        case Const.FREM:
062        case Const.FSUB:
063            return Type.FLOAT;
064        case Const.IADD:
065        case Const.IAND:
066        case Const.IDIV:
067        case Const.IMUL:
068        case Const.INEG:
069        case Const.IOR:
070        case Const.IREM:
071        case Const.ISHL:
072        case Const.ISHR:
073        case Const.ISUB:
074        case Const.IUSHR:
075        case Const.IXOR:
076            return Type.INT;
077        case Const.LADD:
078        case Const.LAND:
079        case Const.LDIV:
080        case Const.LMUL:
081        case Const.LNEG:
082        case Const.LOR:
083        case Const.LREM:
084        case Const.LSHL:
085        case Const.LSHR:
086        case Const.LSUB:
087        case Const.LUSHR:
088        case Const.LXOR:
089            return Type.LONG;
090        default: // Never reached
091            throw new ClassGenException("Unknown type " + opcode);
092        }
093    }
094}