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