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:41:40 pserver Exp $
00010  * @author  <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
00011  */
00012 public class IINC extends Instruction implements IndexedInstruction {
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(de.fub.bytecode.Constants.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    * Call corresponding visitor method(s). The order is:
00032    * Call visitor methods of implemented interfaces first, then
00033    * call methods according to the class hierarchy in descending order,
00034    * i.e., the most specific visitXXX() call comes last.
00035    *
00036    * @param v Visitor object
00037    */
00038   public void accept(Visitor v) {
00039     v.visitIINC(this);
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(de.fub.bytecode.Constants.WIDE);
00048 
00049     out.writeByte(tag);
00050 
00051     if(wide) {
00052       out.writeShort(n);
00053       out.writeShort(c);
00054     }
00055     else {
00056       out.writeByte(n);
00057       out.writeByte(c);
00058     }
00059   }  
00060   /**
00061    * @return increment factor
00062    */
00063   public final int getIncrement() { return c; }  
00064   /**
00065    * @return index of local variable referred by this instruction.
00066    */
00067   public final int getIndex() { return n; }  
00068   /**
00069    * Read needed data (e.g. index) from file.
00070    */
00071   protected void initFromFile(ByteSequence bytes, boolean wide) throws IOException
00072   {
00073     this.wide = wide;
00074 
00075     if(wide) {
00076       length = 6;
00077       n = bytes.readUnsignedShort();
00078       c = bytes.readShort();
00079     }
00080     else {
00081       length = 3;
00082       n = bytes.readUnsignedByte();
00083       c = bytes.readByte();
00084     }
00085   }  
00086   /**
00087    * Set increment factor.
00088    */
00089   public final void setIncrement(int c) {
00090     this.c = c;
00091     setWide();
00092   }  
00093   /**
00094    * Set index of local variable.
00095    */
00096   public final void setIndex(int n) { 
00097     if(n < 0)
00098       throw new ClassGenException("Negative index value: " + n);
00099 
00100     this.n = n;
00101     setWide();
00102   }  
00103   private final void setWide() {
00104     if(wide = ((n > de.fub.bytecode.Constants.MAX_SHORT) || (Math.abs(c) > Byte.MAX_VALUE)))
00105       length = 6; // wide byte included  
00106     else
00107       length = 3;
00108   }  
00109   /**
00110    * @return mnemonic for instruction
00111    */
00112   public String toString(boolean verbose) {
00113     return super.toString(verbose) + " " + n + " " + c;
00114   }  
00115 }

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