1 cananian 1.1.2.10 // METHOD.java, created Thu Aug  5  3:54:47 1999 by duncan
  2 cananian 1.1.2.10 // Copyright (C) 1998 Duncan Bryce <duncan@lcs.mit.edu>
  3 cananian 1.1.2.10 // Licensed under the terms of the GNU GPL; see COPYING for details.
  4 duncan   1.1.2.1  package harpoon.IR.Tree;
  5 duncan   1.1.2.1  
  6 duncan   1.1.2.1  import harpoon.ClassFile.HCodeElement;
  7 cananian 1.1.2.12 import harpoon.Temp.TempMap;
  8 cananian 1.1.2.15 import harpoon.Temp.Label;
  9 duncan   1.1.2.1  import harpoon.Util.Util;
 10 duncan   1.1.2.1  
 11 duncan   1.1.2.1  import java.util.ArrayList;
 12 cananian 1.1.2.11 import java.util.Arrays;
 13 cananian 1.1.2.5  import java.util.Collections;
 14 duncan   1.1.2.1  import java.util.HashSet;
 15 duncan   1.1.2.1  import java.util.List;
 16 duncan   1.1.2.1  import java.util.Set;
 17 duncan   1.1.2.1  
 18 duncan   1.1.2.1  
 19 duncan   1.1.2.1  /**
 20 cananian 1.1.2.6   * <code>Tree.METHOD</code> objects encode method-specific information:
 21 duncan   1.1.2.1   * the mapping of method formals to temporary variables, and
 22 duncan   1.1.2.1   * links to the exception handlers for the method. 
 23 duncan   1.1.2.1   * 
 24 duncan   1.1.2.1   * @author  Duncan Bryce <duncan@lcs.mit.edu>
 25 cananian 1.4       * @version $Id: METHOD.java,v 1.4 2002/04/10 03:05:45 cananian Exp $
 26 duncan   1.1.2.1   */
 27 duncan   1.1.2.1  public class METHOD extends Stm {
 28 cananian 1.1.2.11     private final int paramsLength;
 29 cananian 1.1.2.15     private final int retType;
 30 cananian 1.1.2.15     private final Label method;
 31 cananian 1.1.2.6      /** Creates a <code>Tree.METHOD</code> object. 
 32 cananian 1.1.2.15      * @param method   label to mark the top of the method
 33 cananian 1.1.2.15      * @param retType  integer enumeration (see <code>Typed</code>) of the
 34 cananian 1.1.2.15      *                 return type of this method, or -1 if the method
 35 cananian 1.1.2.15      *                 returns no value.
 36 duncan   1.1.2.1       * @param params   temporaries which map directly to the 
 37 cananian 1.1.2.6       *                 parameters of this <code>Tree.METHOD</code>.
 38 duncan   1.1.2.2       *                 The first element should be a pointer to the 
 39 duncan   1.1.2.2       *                 exception-handling code for this method.  For 
 40 duncan   1.1.2.2       *                 non-static methods, the second parameter should be the 
 41 duncan   1.1.2.2       *                 <code>this</code> pointer of the caller.  Subsequent 
 42 duncan   1.1.2.2       *                 elements of the array should be the formal parameters
 43 duncan   1.1.2.2       *                 of the method, in the order which they are declared. 
 44 duncan   1.1.2.2       */
 45 cananian 1.1.2.15     public METHOD(TreeFactory tf, HCodeElement source,
 46 cananian 1.1.2.15                   Label method, int retType, TEMP[] params) { 
 47 cananian 1.1.2.11         super(tf, source, params.length);
 48 cananian 1.3.2.1          assert method!=null;
 49 cananian 1.3.2.1          assert retType==-1 || Type.isValid(retType);
 50 cananian 1.3.2.1          assert params!=null; assert(params.length>0);
 51 cananian 1.3.2.1          for (int i=0; i<params.length; i++) assert params[i].tf == tf;
 52 cananian 1.1.2.15         this.method = method;
 53 cananian 1.1.2.15         this.retType = retType;
 54 cananian 1.1.2.11         this.paramsLength = params.length;
 55 duncan   1.1.2.9          this.setParams(params);
 56 duncan   1.1.2.7      }
 57 cananian 1.1.2.15     /** Return the label which should mark the top of the method. */
 58 cananian 1.1.2.15     public Label getMethod() { return method; }
 59 cananian 1.1.2.15     /** Return an integer enumeration (see <code>Typed</code>) of the
 60 cananian 1.1.2.15      *  return type of the method.  Returns -1 if the method returns no
 61 cananian 1.1.2.15      *  value. */
 62 cananian 1.1.2.15     public int getReturnType() { return retType; }
 63 duncan   1.1.2.7  
 64 cananian 1.1.2.11     /** Return the temporary variables used for method formals. */
 65 cananian 1.1.2.11     public TEMP[] getParams() {
 66 cananian 1.1.2.11         TEMP[] result = new TEMP[paramsLength];
 67 cananian 1.1.2.11         int i=0;
 68 cananian 1.1.2.11         for (Tree t=getFirstChild(); t!=null; t=t.getSibling())
 69 cananian 1.1.2.11             result[i++] = (TEMP) t;
 70 cananian 1.1.2.11         return result;
 71 duncan   1.1.2.7      }
 72 cananian 1.1.2.11     /** Set the temporary variables used for method formals. */
 73 duncan   1.1.2.7      public void setParams(TEMP[] params) { 
 74 cananian 1.3.2.1          assert paramsLength == params.length : "Can't change number of parameters to METHOD";
 75 cananian 1.1.2.11         for (int i=0; i<paramsLength; i++)
 76 cananian 1.1.2.11             setChild(i, params[i]);
 77 duncan   1.1.2.1      }
 78 duncan   1.1.2.1  
 79 cananian 1.1.2.11     // convenience/efficiency methods.
 80 cananian 1.1.2.11     public int getParamsLength() { return paramsLength; }
 81 cananian 1.1.2.11     public TEMP getParams(int i) { return (TEMP) getChild(i); }
 82 duncan   1.1.2.1  
 83 cananian 1.1.2.11     public int kind() { return TreeKind.METHOD; }
 84 duncan   1.1.2.1  
 85 cananian 1.1.2.11     public ExpList kids() { return null; /* definitions not considered kids */}
 86 duncan   1.1.2.1      public Stm build(TreeFactory tf, ExpList kids) { 
 87 cananian 1.3.2.1          assert kids==null;
 88 cananian 1.3.2.1          assert tf==this.tf : "cloning Params not yet implemented";
 89 cananian 1.1.2.15         return new METHOD(tf, this, method, retType, getParams());
 90 duncan   1.1.2.1      }
 91 duncan   1.1.2.1  
 92 cananian 1.1.2.12     public Tree rename(TreeFactory tf, TempMap tm, CloneCallback cb) {
 93 cananian 1.1.2.11         TEMP[] params  = getParams();
 94 duncan   1.1.2.1          TEMP[] newTmps = new TEMP[params.length];
 95 duncan   1.1.2.1          for (int i=0; i<params.length; i++) 
 96 cananian 1.1.2.12             newTmps[i] = (TEMP)params[i].rename(tf, tm, cb);
 97 cananian 1.1.2.15         return cb.callback(this,
 98 cananian 1.1.2.15                            new METHOD(tf, this, method, retType, newTmps),
 99 cananian 1.1.2.15                            tm);
100 duncan   1.1.2.1      }
101 duncan   1.1.2.1  
102 duncan   1.1.2.1      /** Accept a visitor. */
103 cananian 1.1.2.5      public void accept(TreeVisitor v) { v.visit(this); }
104 duncan   1.1.2.1  
105 cananian 1.1.2.6      /** Returns human-readable representation of this <code>Tree</code>. */
106 duncan   1.1.2.1      public String toString() {
107 duncan   1.1.2.1          StringBuffer sb = new StringBuffer("METHOD(");
108 cananian 1.1.2.11         TEMP[] params = getParams();
109 duncan   1.1.2.1          for (int i=0; i<params.length-1; i++) {
110 duncan   1.1.2.1              sb.append(params[i].toString());
111 duncan   1.1.2.1              sb.append(", ");
112 duncan   1.1.2.1          }
113 pnkfelix 1.1.2.4          sb.append(params[params.length-1].toString());
114 duncan   1.1.2.1          sb.append(")");
115 duncan   1.1.2.1          return sb.toString();
116 duncan   1.1.2.1      }
117 cananian 1.2      }