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

RET.java

00001 package de.fub.bytecode.generic;
00002 
00003 import java.io.*;
00004 import de.fub.bytecode.util.ByteSequence;
00005 
00006 /** 
00007  * RET - Return from subroutine
00008  *
00009  * <PRE>Stack: ..., -&gt; ..., address</PRE>
00010  *
00011  * @version $Id: RET.java,v 1.1.1.1 2002/01/24 03:41:41 pserver Exp $
00012  * @author  <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
00013  */
00014 public class RET extends Instruction implements IndexedInstruction {
00015   private boolean wide;
00016   private int     index; // index to local variable containg the return address
00017 
00018   /**
00019    * Empty constructor needed for the Class.newInstance() statement in
00020    * Instruction.readInstruction(). Not to be used otherwise.
00021    */
00022   RET() {}  
00023   public RET(int index) {
00024     super(de.fub.bytecode.Constants.RET, (short)2);
00025     setIndex(index);   // May set wide as side effect
00026   }  
00027   /**
00028    * Call corresponding visitor method(s). The order is:
00029    * Call visitor methods of implemented interfaces first, then
00030    * call methods according to the class hierarchy in descending order,
00031    * i.e., the most specific visitXXX() call comes last.
00032    *
00033    * @param v Visitor object
00034    */
00035   public void accept(Visitor v) {
00036     v.visitRET(this);
00037   }  
00038   /**
00039    * Dump instruction as byte code to stream out.
00040    * @param out Output stream
00041    */
00042   public void dump(DataOutputStream out) throws IOException {
00043     if(wide)
00044       out.writeByte(de.fub.bytecode.Constants.WIDE);
00045 
00046     out.writeByte(tag);
00047 
00048     if(wide)
00049       out.writeShort(index);
00050     else
00051       out.writeByte(index);
00052   }  
00053   /**
00054    * @return index of local variable containg the return address
00055    */
00056   public final int getIndex() { return index; }  
00057   /**
00058    * Read needed data (e.g. index) from file.
00059    */
00060   protected void initFromFile(ByteSequence bytes, boolean wide) throws IOException
00061   {
00062     this.wide = wide;
00063 
00064     if(wide) {
00065       index  = bytes.readUnsignedShort();
00066       length = 4;
00067     } else {
00068       index = bytes.readUnsignedByte();
00069       length = 2;
00070     }
00071   }  
00072   /**
00073    * Set index of local variable containg the return address
00074    */
00075   public final void setIndex(int n) { 
00076     if(n < 0)
00077       throw new ClassGenException("Negative index value: " + n);
00078 
00079     index = n;
00080     setWide();
00081   }  
00082   private final void setWide() {
00083     if(wide = index > de.fub.bytecode.Constants.MAX_BYTE)
00084       length = 4; // Including the wide byte  
00085     else
00086       length = 2;
00087   }  
00088   /**
00089    * @return mnemonic for instruction
00090    */
00091   public String toString(boolean verbose) {
00092     return super.toString(verbose) + " " + index;
00093   }  
00094 }

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