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

LocalVariableInstruction.java

00001 package de.fub.bytecode.generic;
00002 
00003 import java.io.*;
00004 import de.fub.bytecode.util.ByteSequence;
00005 import de.fub.bytecode.classfile.Utility;
00006 
00007 /** 
00008  * Abstract super class for instructions dealing with local variables.
00009  *
00010  * @version $Id: LocalVariableInstruction.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 abstract class LocalVariableInstruction extends Instruction {
00014   private int     n;     // index of referenced variable
00015   private short   c_tag; // compact version, such as ILOAD_0
00016   private short   canon_tag; // canonical tag such as ILOAD
00017 
00018   /**
00019    * Empty constructor needed for the Class.newInstance() statement in
00020    * Instruction.readInstruction(). Not to be used otherwise.
00021    * tag and length are defined in readInstruction and initFromFile, respectively.
00022    */
00023   LocalVariableInstruction(short canon_tag, short c_tag) {
00024     super();
00025     this.canon_tag = canon_tag;
00026     this.c_tag     = c_tag;
00027   }  
00028   /**
00029    * @param tag Instruction number
00030    * @param c_tag Instruction number for compact version, ALOAD_0, e.g.
00031    * @param n local variable index (unsigned short)
00032    */
00033   protected LocalVariableInstruction(short tag, short c_tag, int n) {
00034     super(tag, (short)2);
00035 
00036     this.c_tag = c_tag;
00037     canon_tag  = tag;
00038 
00039     setIndex(n);
00040   }  
00041   /**
00042    * Dump instruction as byte code to stream out.
00043    * @param out Output stream
00044    */
00045   public void dump(DataOutputStream out) throws IOException {
00046     if(wide()) // Need WIDE prefix ?
00047       out.writeByte(WIDE);
00048 
00049     out.writeByte(tag);
00050 
00051     if(length > 1) { // Otherwise ILOAD_n, instruction, e.g.
00052       if(wide())
00053     out.writeShort(n);
00054       else
00055     out.writeByte(n);
00056     }
00057   }  
00058   /**
00059    * @return local variable index  referred by this instruction.
00060    */
00061   public final int getIndex() { return n; }  
00062   /**
00063    * Read needed data (e.g. index) from file.
00064    * PRE: (ILOAD <= tag <= ALOAD_3) || (ISTORE <= tag <= ASTORE_3)
00065    */
00066   protected void initFromFile(ByteSequence bytes, boolean wide)
00067     throws IOException
00068   {
00069     if(wide) {
00070       n         = bytes.readUnsignedShort();
00071       length    = 4;
00072     } else if(((tag >= ILOAD)  && (tag <= ALOAD)) ||
00073         ((tag >= ISTORE) && (tag <= ASTORE))) {
00074       n      = bytes.readUnsignedByte();
00075       length = 2;
00076     } else if(tag <= ALOAD_3) { // compact load instruction such as ILOAD_2
00077       n      = (tag - ILOAD_0) % 4;
00078       length = 1;
00079     } else { // Assert ISTORE_0 <= tag <= ASTORE_3
00080       n      = (tag - ISTORE_0) % 4;
00081       length = 1;
00082     }
00083  } 
00084   /**
00085    * Set the local variable index
00086    */
00087   public final void setIndex(int n) { 
00088     if((n < 0) || (n > MAX_SHORT))
00089       throw new ClassGenException("Illegal value: " + n);
00090 
00091     this.n = n;
00092 
00093     if(n >= 0 && n <= 3) { // Use more compact instruction xLOAD_n
00094       tag    = (short)(c_tag + n);
00095       length = 1;
00096     } else {
00097       tag = canon_tag;
00098       
00099       if(wide()) // Need WIDE prefix ?
00100     length = 4;
00101       else
00102     length = 2;
00103     }
00104   }  
00105   /**
00106    * Long output format:
00107    *
00108    * &lt;name of opcode&gt; "["&lt;opcode number&gt;"]" 
00109    * "("&lt;length of instruction&gt;")" "&lt;"&lt; local variable index&gt;"&gt;"
00110    *
00111    * @param verbose long/short format switch
00112    * @return mnemonic for instruction
00113    */
00114   public String toString(boolean verbose) {
00115     if(((tag >= ILOAD_0) && (tag <= ALOAD_3)) ||
00116        ((tag >= ISTORE_0) && (tag <= ASTORE_3)))
00117       return super.toString(verbose);
00118     else
00119       return super.toString(verbose) + " " + n;
00120   }  
00121   private final boolean wide() { return n > MAX_BYTE; }  
00122 }

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