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