    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     * @param opcode of instruction
    036     */
    037    protected ArithmeticInstruction(final short opcode) {
    038        super(opcode, (short) 1);
    039    }
    040
    041    /**
    042     * @return type associated with the instruction
    043     */
    044    @Override
    045    public Type getType(final ConstantPoolGen cp) {
    046        final short opcode = super.getOpcode();
    047        switch (opcode) {
    048        case Const.DADD:
    049        case Const.DDIV:
    050        case Const.DMUL:
    051        case Const.DNEG:
    052        case Const.DREM:
    053        case Const.DSUB:
    054            return Type.DOUBLE;
    055        case Const.FADD:
    056        case Const.FDIV:
    057        case Const.FMUL:
    058        case Const.FNEG:
    059        case Const.FREM:
    060        case Const.FSUB:
    061            return Type.FLOAT;
    062        case Const.IADD:
    063        case Const.IAND:
    064        case Const.IDIV:
    065        case Const.IMUL:
    066        case Const.INEG:
    067        case Const.IOR:
    068        case Const.IREM:
    069        case Const.ISHL:
    070        case Const.ISHR:
    071        case Const.ISUB:
    072        case Const.IUSHR:
    073        case Const.IXOR:
    074            return Type.INT;
    075        case Const.LADD:
    076        case Const.LAND:
    077        case Const.LDIV:
    078        case Const.LMUL:
    079        case Const.LNEG:
    080        case Const.LOR:
    081        case Const.LREM:
    082        case Const.LSHL:
    083        case Const.LSHR:
    084        case Const.LSUB:
    085        case Const.LUSHR:
    086        case Const.LXOR:
    087            return Type.LONG;
    088        default: // Never reached
    089            throw new ClassGenException("Unknown type " + opcode);
    090        }
    091    }
    092}