Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

PUSH.java

00001 package de.fub.bytecode.generic;
00002 
00003 import de.fub.bytecode.Constants;
00004 import java.io.*;
00005 
00006 /** 
00007  * Wrapper class for push operations, which are implemented either as BIPUSH,
00008  * LDC or xCONST_n instructions.
00009  *
00010  * @version $Id: PUSH.java,v 1.1.1.1 2002/01/24 03:44:03 pserver Exp $
00011  * @author  <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
00012  */
00013 public final class PUSH
00014   implements CompoundInstruction, VariableLengthInstruction, InstructionConstants
00015 {
00016   private Instruction instruction;
00017 
00018   /**
00019    * @param cp Constant pool
00020    * @param value to be pushed 
00021    */
00022   public PUSH(ConstantPoolGen cp, double value) {
00023     if(value == 0.0)
00024       instruction = DCONST_0;
00025     else if(value == 1.0)
00026       instruction = DCONST_1;
00027     else // Create a Constant pool entry
00028       instruction = new LDC2_W(cp.addDouble(value));
00029   }  
00030   /**
00031    * @param cp Constant pool
00032    * @param value to be pushed 
00033    */
00034   public PUSH(ConstantPoolGen cp, float value) {
00035     if(value == 0.0)
00036       instruction = FCONST_0;
00037     else if(value == 1.0)
00038       instruction = FCONST_1;
00039     else if(value == 2.0)
00040       instruction = FCONST_2;
00041     else // Create a Constant pool entry
00042       instruction = new LDC(cp.addFloat(value));
00043   }  
00044   /**
00045    * This constructor also applies for values of type short, char, byte 
00046    *
00047    * @param cp Constant pool
00048    * @param value to be pushed 
00049    */
00050   public PUSH(ConstantPoolGen cp, int value) {
00051     if((value >= -1) && (value <= 5)) // Use ICONST_n
00052       instruction = INSTRUCTIONS[Constants.ICONST_0 + value];
00053     else if((value >= -128) && (value <= 127)) // Use BIPUSH
00054       instruction = new BIPUSH((byte)value);
00055     else if((value >= -32768) && (value <= 32767)) // Use SIPUSH
00056       instruction = new SIPUSH((short)value);
00057     else // If everything fails create a Constant pool entry
00058       instruction = new LDC(cp.addInteger(value));
00059   }  
00060   /**
00061    * @param cp Constant pool
00062    * @param value to be pushed 
00063    */
00064   public PUSH(ConstantPoolGen cp, long value) {
00065     if(value == 0)
00066       instruction = LCONST_0;
00067     else if(value == 1)
00068       instruction = LCONST_1;
00069     else // Create a Constant pool entry
00070       instruction = new LDC2_W(cp.addLong(value));
00071   }  
00072   /**
00073    * @param cp Constant pool
00074    * @param value to be pushed 
00075    */
00076   public PUSH(ConstantPoolGen cp, Boolean value) {
00077     this(cp, value.booleanValue());
00078   }  
00079   /**
00080    * @param cp Constant pool
00081    * @param value to be pushed 
00082    */
00083   public PUSH(ConstantPoolGen cp, Character value) {
00084     this(cp, (int)value.charValue());
00085   }  
00086   /**
00087    * @param cp Constant pool
00088    * @param value to be pushed 
00089    */
00090   public PUSH(ConstantPoolGen cp, Number value) {
00091     if((value instanceof Integer) || (value instanceof Short) || (value instanceof Byte))
00092       instruction = new PUSH(cp, value.intValue()).instruction;
00093     else if(value instanceof Double)
00094       instruction = new PUSH(cp, value.doubleValue()).instruction;
00095     else if(value instanceof Float)
00096       instruction = new PUSH(cp, value.floatValue()).instruction;
00097     else if(value instanceof Long)
00098       instruction = new PUSH(cp, value.longValue()).instruction;
00099     else
00100       throw new ClassGenException("What's this: " + value);
00101   }  
00102   /**
00103    * @param cp Constant pool
00104    * @param value to be pushed 
00105    */
00106   public PUSH(ConstantPoolGen cp, String value) {
00107     if(value == null)
00108       instruction = ACONST_NULL;
00109     else // Create a Constant pool entry
00110       instruction = new LDC(cp.addString(value));
00111   }  
00112   /**
00113    * @param cp Constant pool
00114    * @param value to be pushed 
00115    */
00116   public PUSH(ConstantPoolGen cp, boolean value) {
00117     instruction = INSTRUCTIONS[Constants.ICONST_0 + (value? 1 : 0)];
00118   }  
00119   public final Instruction getInstruction() {
00120     return instruction;
00121   }  
00122   public final InstructionList getInstructionList() {
00123     return new InstructionList(instruction);
00124   }  
00125   /**
00126    * @return mnemonic for instruction
00127    */
00128   public String toString() {
00129     return instruction.toString() + " (PUSH)";
00130   }  
00131 }

Generated at Thu Feb 7 06:53:08 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001