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

BranchHandle.java

00001 package de.fub.bytecode.generic;
00002 
00003 /**
00004  * BranchHandle is returned by specialized InstructionList.append() whenever a
00005  * BranchInstruction is appended. This is useful when the target of this
00006  * instruction is not known at time of creation and must be set later
00007  * via setTarget().
00008  *
00009  * @see InstructionHandle
00010  * @see Instruction
00011  * @see InstructionList
00012  * @version $Id: BranchHandle.java,v 1.1.1.1 2002/01/24 03:41:38 pserver Exp $
00013  * @author  <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
00014  */
00015 public final class BranchHandle extends InstructionHandle {
00016   private BranchInstruction bi; // An alias in fact, but saves lots of casts
00017 
00018   /** Factory methods.
00019    */
00020   private static BranchHandle bh_list = null; // List of reusable handles
00021 
00022   private BranchHandle(BranchInstruction i) {
00023     super(i);
00024     bi = i;
00025   }  
00026   /** Handle adds itself to the list of resuable handles.
00027    */
00028   protected void addHandle() {
00029     next    = bh_list;
00030     bh_list = this;
00031   }  
00032   static final BranchHandle getBranchHandle(BranchInstruction i) {
00033     if(bh_list == null)
00034       return new BranchHandle(i);
00035     else {
00036       BranchHandle bh = bh_list;
00037       bh_list = (BranchHandle)bh.next;
00038 
00039       bh.setInstruction(i);
00040 
00041       return bh;
00042     }
00043   }  
00044   /* Override InstructionHandle methods: delegate to branch instruction.
00045    * Through this overriding all access to the private i_position field should
00046    * be prevented.
00047    */
00048   public int getPosition() { return bi.position; }  
00049   /**
00050    * @return target of instruction.
00051    */
00052   public InstructionHandle getTarget() {
00053     return bi.getTarget();
00054   }  
00055   /** 
00056    * Set new contents. Old instruction is disposed and may not be used anymore.
00057    */
00058   public void setInstruction(Instruction i) {
00059     super.setInstruction(i);
00060 
00061     if(!(i instanceof BranchInstruction))
00062       throw new ClassGenException("Assigning " + i +
00063                   " to branch handle which is not a branch instruction");
00064 
00065     bi = (BranchInstruction)i;
00066   }  
00067   void setPosition(int pos) {
00068     i_position = bi.position = pos;
00069   }  
00070   /**
00071    * Pass new target to instruction.
00072    */
00073   public void setTarget(InstructionHandle ih) {
00074     bi.setTarget(ih);
00075   }  
00076   protected int updatePosition(int offset, int max_offset) {
00077     int x = bi.updatePosition(offset, max_offset);
00078     i_position = bi.position;
00079     return x;
00080   }  
00081   /**
00082    * Update target of instruction.
00083    */
00084   public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
00085     bi.updateTarget(old_ih, new_ih);
00086   }  
00087 }

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