286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/Apackage com.sun.org.apache.bcel.internal.generic;
286N/A
286N/A/* ====================================================================
286N/A * The Apache Software License, Version 1.1
286N/A *
286N/A * Copyright (c) 2001 The Apache Software Foundation. All rights
286N/A * reserved.
286N/A *
286N/A * Redistribution and use in source and binary forms, with or without
286N/A * modification, are permitted provided that the following conditions
286N/A * are met:
286N/A *
286N/A * 1. Redistributions of source code must retain the above copyright
286N/A * notice, this list of conditions and the following disclaimer.
286N/A *
286N/A * 2. Redistributions in binary form must reproduce the above copyright
286N/A * notice, this list of conditions and the following disclaimer in
286N/A * the documentation and/or other materials provided with the
286N/A * distribution.
286N/A *
286N/A * 3. The end-user documentation included with the redistribution,
286N/A * if any, must include the following acknowledgment:
286N/A * "This product includes software developed by the
286N/A * Apache Software Foundation (http://www.apache.org/)."
286N/A * Alternately, this acknowledgment may appear in the software itself,
286N/A * if and wherever such third-party acknowledgments normally appear.
286N/A *
286N/A * 4. The names "Apache" and "Apache Software Foundation" and
286N/A * "Apache BCEL" must not be used to endorse or promote products
286N/A * derived from this software without prior written permission. For
286N/A * written permission, please contact apache@apache.org.
286N/A *
286N/A * 5. Products derived from this software may not be called "Apache",
286N/A * "Apache BCEL", nor may "Apache" appear in their name, without
286N/A * prior written permission of the Apache Software Foundation.
286N/A *
286N/A * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
286N/A * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
286N/A * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
286N/A * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
286N/A * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
286N/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
286N/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
286N/A * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
286N/A * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
286N/A * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
286N/A * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
286N/A * SUCH DAMAGE.
286N/A * ====================================================================
286N/A *
286N/A * This software consists of voluntary contributions made by many
286N/A * individuals on behalf of the Apache Software Foundation. For more
286N/A * information on the Apache Software Foundation, please see
286N/A * <http://www.apache.org/>.
286N/A */
286N/Aimport com.sun.org.apache.bcel.internal.Constants;
286N/Aimport java.io.*;
286N/A
286N/A/**
286N/A * Wrapper class for push operations, which are implemented either as BIPUSH,
286N/A * LDC or xCONST_n instructions.
286N/A *
286N/A * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
286N/A */
286N/Apublic final class PUSH
286N/A implements CompoundInstruction, VariableLengthInstruction, InstructionConstants
286N/A{
286N/A private Instruction instruction;
286N/A
286N/A /**
286N/A * This constructor also applies for values of type short, char, byte
286N/A *
286N/A * @param cp Constant pool
286N/A * @param value to be pushed
286N/A */
286N/A public PUSH(ConstantPoolGen cp, int value) {
286N/A if((value >= -1) && (value <= 5)) // Use ICONST_n
286N/A instruction = INSTRUCTIONS[Constants.ICONST_0 + value];
286N/A else if((value >= -128) && (value <= 127)) // Use BIPUSH
286N/A instruction = new BIPUSH((byte)value);
286N/A else if((value >= -32768) && (value <= 32767)) // Use SIPUSH
286N/A instruction = new SIPUSH((short)value);
286N/A else // If everything fails create a Constant pool entry
286N/A instruction = new LDC(cp.addInteger(value));
286N/A }
286N/A
286N/A /**
286N/A * @param cp Constant pool
286N/A * @param value to be pushed
286N/A */
286N/A public PUSH(ConstantPoolGen cp, boolean value) {
286N/A instruction = INSTRUCTIONS[Constants.ICONST_0 + (value? 1 : 0)];
286N/A }
286N/A
286N/A /**
286N/A * @param cp Constant pool
286N/A * @param value to be pushed
286N/A */
286N/A public PUSH(ConstantPoolGen cp, float value) {
286N/A if(value == 0.0)
286N/A instruction = FCONST_0;
286N/A else if(value == 1.0)
286N/A instruction = FCONST_1;
286N/A else if(value == 2.0)
286N/A instruction = FCONST_2;
286N/A else // Create a Constant pool entry
286N/A instruction = new LDC(cp.addFloat(value));
286N/A }
286N/A
286N/A /**
286N/A * @param cp Constant pool
286N/A * @param value to be pushed
286N/A */
286N/A public PUSH(ConstantPoolGen cp, long value) {
286N/A if(value == 0)
286N/A instruction = LCONST_0;
286N/A else if(value == 1)
286N/A instruction = LCONST_1;
286N/A else // Create a Constant pool entry
286N/A instruction = new LDC2_W(cp.addLong(value));
286N/A }
286N/A
286N/A /**
286N/A * @param cp Constant pool
286N/A * @param value to be pushed
286N/A */
286N/A public PUSH(ConstantPoolGen cp, double value) {
286N/A if(value == 0.0)
286N/A instruction = DCONST_0;
286N/A else if(value == 1.0)
286N/A instruction = DCONST_1;
286N/A else // Create a Constant pool entry
286N/A instruction = new LDC2_W(cp.addDouble(value));
286N/A }
286N/A
286N/A /**
286N/A * @param cp Constant pool
286N/A * @param value to be pushed
286N/A */
286N/A public PUSH(ConstantPoolGen cp, String value) {
286N/A if(value == null)
286N/A instruction = ACONST_NULL;
286N/A else // Create a Constant pool entry
286N/A instruction = new LDC(cp.addString(value));
286N/A }
286N/A
286N/A /**
286N/A * @param cp Constant pool
286N/A * @param value to be pushed
286N/A */
286N/A public PUSH(ConstantPoolGen cp, Number value) {
286N/A if((value instanceof Integer) || (value instanceof Short) || (value instanceof Byte))
286N/A instruction = new PUSH(cp, value.intValue()).instruction;
286N/A else if(value instanceof Double)
286N/A instruction = new PUSH(cp, value.doubleValue()).instruction;
286N/A else if(value instanceof Float)
286N/A instruction = new PUSH(cp, value.floatValue()).instruction;
286N/A else if(value instanceof Long)
286N/A instruction = new PUSH(cp, value.longValue()).instruction;
286N/A else
286N/A throw new ClassGenException("What's this: " + value);
286N/A }
286N/A
286N/A /**
286N/A * @param cp Constant pool
286N/A * @param value to be pushed
286N/A */
286N/A public PUSH(ConstantPoolGen cp, Character value) {
286N/A this(cp, (int)value.charValue());
286N/A }
286N/A
286N/A /**
286N/A * @param cp Constant pool
286N/A * @param value to be pushed
286N/A */
286N/A public PUSH(ConstantPoolGen cp, Boolean value) {
286N/A this(cp, value.booleanValue());
286N/A }
286N/A
286N/A public final InstructionList getInstructionList() {
286N/A return new InstructionList(instruction);
286N/A }
286N/A
286N/A public final Instruction getInstruction() {
286N/A return instruction;
286N/A }
286N/A
286N/A /**
286N/A * @return mnemonic for instruction
286N/A */
286N/A public String toString() {
286N/A return instruction.toString() + " (PUSH)";
286N/A }
286N/A}