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 */
017package org.apache.bcel.generic;
018
019import org.apache.bcel.Const;
020
021/**
022 * Super class for the family of arithmetic instructions.
023 */
024public abstract class ArithmeticInstruction extends Instruction implements TypedInstruction, StackProducer, StackConsumer {
025
026    /**
027     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
028     */
029    ArithmeticInstruction() {
030    }
031
032    /**
033     * @param opcode of instruction
034     */
035    protected ArithmeticInstruction(final short opcode) {
036        super(opcode, (short) 1);
037    }
038
039    /**
040     * @return type associated with the instruction
041     */
042    @Override
043    public Type getType(final ConstantPoolGen cp) {
044        final short opcode = super.getOpcode();
045        switch (opcode) {
046        case Const.DADD:
047        case Const.DDIV:
048        case Const.DMUL:
049        case Const.DNEG:
050        case Const.DREM:
051        case Const.DSUB:
052            return Type.DOUBLE;
053        case Const.FADD:
054        case Const.FDIV:
055        case Const.FMUL:
056        case Const.FNEG:
057        case Const.FREM:
058        case Const.FSUB:
059            return Type.FLOAT;
060        case Const.IADD:
061        case Const.IAND:
062        case Const.IDIV:
063        case Const.IMUL:
064        case Const.INEG:
065        case Const.IOR:
066        case Const.IREM:
067        case Const.ISHL:
068        case Const.ISHR:
069        case Const.ISUB:
070        case Const.IUSHR:
071        case Const.IXOR:
072            return Type.INT;
073        case Const.LADD:
074        case Const.LAND:
075        case Const.LDIV:
076        case Const.LMUL:
077        case Const.LNEG:
078        case Const.LOR:
079        case Const.LREM:
080        case Const.LSHL:
081        case Const.LSHR:
082        case Const.LSUB:
083        case Const.LUSHR:
084        case Const.LXOR:
085            return Type.LONG;
086        default: // Never reached
087            throw new ClassGenException("Unknown type " + opcode);
088        }
089    }
090}