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

IINC.java

00001 package de.fub.bytecode.generic;
00002 
00003 import java.io.*;
00004 import de.fub.bytecode.util.ByteSequence;
00005 
00006 /**
00007  * IINC - Increment local variable by constant
00008  *
00009  * @version $Id: IINC.java,v 1.1.1.1 2002/01/24 03:44:06 pserver Exp $
00010  * @author  <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
00011  */
00012 public class IINC extends Instruction {
00013   private boolean wide;
00014   private int     n, c;
00015 
00016   /**
00017    * Empty constructor needed for the Class.newInstance() statement in
00018    * Instruction.readInstruction(). Not to be used otherwise.
00019    */
00020   IINC() {}  
00021   public IINC(int n, int c) {
00022     super(IINC, (short)3);
00023 
00024     setIndex(n);    // May set wide as side effect
00025     setIncrement(c);
00026 
00027     this.n = n;
00028     this.c = c;
00029   }  
00030   /**
00031    * Dump instruction as byte code to stream out.
00032    * @param out Output stream
00033    */
00034   public void dump(DataOutputStream out) throws IOException {
00035     if(wide) // Need WIDE prefix ?
00036       out.writeByte(WIDE);
00037 
00038     out.writeByte(tag);
00039 
00040     if(wide) {
00041       out.writeShort(n);
00042       out.writeShort(c);
00043     }
00044     else {
00045       out.writeByte(n);
00046       out.writeByte(c);
00047     }
00048   }  
00049   /**
00050    * @return increment factor
00051    */
00052   public final int getIncrement() { return c; }  
00053   /**
00054    * @return index of local variable referred by this instruction.
00055    */
00056   public final int getIndex() { return n; }  
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       length = 6;
00066       n = bytes.readUnsignedShort();
00067       c = bytes.readShort();
00068     }
00069     else {
00070       length = 3;
00071       n = bytes.readUnsignedByte();
00072       c = bytes.readByte();
00073     }
00074   }  
00075   /**
00076    * Set increment factor.
00077    */
00078   public final void setIncrement(int c) {
00079     this.c = c;
00080     setWide();
00081   }  
00082   /**
00083    * Set index of local variable.
00084    */
00085   public final void setIndex(int n) { 
00086     if(n < 0)
00087       throw new ClassGenException("Negative index value: " + n);
00088 
00089     this.n = n;
00090     setWide();
00091   }  
00092   private final void setWide() {
00093     if(wide = ((n > MAX_SHORT) || (Math.abs(c) > Byte.MAX_VALUE)))
00094       length = 6; // wide byte included  
00095     else
00096       length = 3;
00097   }  
00098   /**
00099    * @return mnemonic for instruction
00100    */
00101   public String toString(boolean verbose) {
00102     return super.toString(verbose) + " " + n + " " + c;
00103   }  
00104 }

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