1 cananian 1.1.2.9  // INVOCATION.java, created Thu Feb 18 17:43:19 1999 by duncan
 2 cananian 1.1.2.8  // Copyright (C) 1998 Duncan Bryce <duncan@lcs.mit.edu>
 3 cananian 1.1.2.8  // 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 duncan   1.1.2.1  import harpoon.Util.Util;
 8 duncan   1.1.2.1  
 9 duncan   1.1.2.5  import java.util.HashSet;
10 duncan   1.1.2.5  import java.util.Set;
11 duncan   1.1.2.5  
12 duncan   1.1.2.2  
13 duncan   1.1.2.1  /**
14 duncan   1.1.2.1   * <code>INVOCATION</code> objects are statements which stand for 
15 duncan   1.1.2.1   * procedure calls.  The return value of the <code>CALL</code> is 
16 duncan   1.1.2.1   * stored in <code>retval</code>.  If the call throws an exception, 
17 duncan   1.1.2.1   * the exception object will be placed in <code>retex</code>.
18 cananian 1.1.2.13  * The <code>retval</code> field will be <code>null</code> if the
19 cananian 1.1.2.13  * called procedure has void return type.
20 duncan   1.1.2.1   * 
21 cananian 1.1.2.8   * @author  Duncan Bryce <duncan@lcs.mit.edu>, based on
22 duncan   1.1.2.1   *          <i>Modern Compiler Implementation in Java</i> by Andrew Appel.
23 cananian 1.4       * @version $Id: INVOCATION.java,v 1.4 2002/04/10 03:05:45 cananian Exp $
24 pnkfelix 1.1.2.3   * @see harpoon.IR.Quads.CALL
25 pnkfelix 1.1.2.3   * @see CALL
26 pnkfelix 1.1.2.3   * @see NATIVECALL
27 duncan   1.1.2.1   */
28 duncan   1.1.2.1  public abstract class INVOCATION extends Stm {
29 cananian 1.1.2.16     private final int argsLength;
30 cananian 1.1.2.16     private final int kidsStart;
31 cananian 1.1.2.16     private final boolean isVoid;
32 duncan   1.1.2.1  
33 cananian 1.1.2.16     /** Constructor.
34 cananian 1.1.2.16      * @param retval Expression indicating the destination of the return
35 cananian 1.1.2.16      *  value. The <code>retval</code> is <code>null</code> for 
36 cananian 1.1.2.16      *  <code>void</code> functions.
37 cananian 1.1.2.16      * @param func A subexpression which evaluates to the function
38 cananian 1.1.2.16      *  reference to invoke.
39 cananian 1.1.2.16      * @param args Subexpressions for the arguments to the function.
40 cananian 1.1.2.16      */
41 duncan   1.1.2.1      protected INVOCATION(TreeFactory tf, HCodeElement source,
42 cananian 1.1.2.16                          TEMP retval, Exp func, ExpList args, int addlArgs) {
43 cananian 1.1.2.16         super(tf, source, 1+ExpList.size(args)+(retval==null?0:1)+addlArgs); 
44 cananian 1.3.2.1          assert func!=null;
45 cananian 1.1.2.16         this.isVoid = (retval==null);
46 cananian 1.1.2.16         this.kidsStart = (isVoid?0:1)+addlArgs;
47 cananian 1.1.2.16         this.argsLength = ExpList.size(args);
48 cananian 1.1.2.16         setRetval(retval); setFunc(func); setArgs(args);
49 cananian 1.3.2.1          assert tf==func.tf : "This and Func must have same tree factory";
50 cananian 1.3.2.1          assert retval==null || tf == retval.tf : "This and Retval must have same tree factory";
51 duncan   1.1.2.1      }
52 duncan   1.1.2.2  
53 cananian 1.1.2.16     /** Returns the expression indicating the destination of the return value.
54 cananian 1.1.2.16      *  Returns <code>null</code> for <code>void</code>
55 cananian 1.1.2.16      *  functions. */
56 cananian 1.1.2.16     public TEMP getRetval() {
57 cananian 1.1.2.16         return (isVoid) ? null : (TEMP) getChild(kidsStart-1);
58 cananian 1.1.2.16     }
59 cananian 1.1.2.16     /** Returns an expression for the function reference to invoke.*/
60 cananian 1.1.2.16     public Exp getFunc() { return (Exp) getChild(kidsStart); }
61 cananian 1.1.2.16     /** Returns a list of subexpressions for the function arguments. */
62 cananian 1.1.2.16     public ExpList getArgs() { return kids().tail; } 
63 cananian 1.1.2.16 
64 cananian 1.1.2.16     // kids start with func expression and go from there.
65 cananian 1.1.2.16     public ExpList kids() { return _kids(getFunc()); }
66 cananian 1.1.2.16     private ExpList _kids(Exp e) {
67 cananian 1.1.2.16         if (e==null) return null;
68 cananian 1.1.2.16         else return new ExpList(e, _kids((Exp)e.getSibling()));
69 cananian 1.1.2.16     }
70 cananian 1.1.2.16 
71 cananian 1.1.2.16     /** Sets the expression indicating the destination of the return value.
72 cananian 1.1.2.16      * @param retval <code>null</code> for <code>void</code> functions,
73 cananian 1.1.2.16      *        non-<code>null</code> otherwise.
74 cananian 1.1.2.16      */
75 cananian 1.1.2.16     public void setRetval(TEMP retval) {
76 cananian 1.1.2.16         if (isVoid) {
77 cananian 1.3.2.1              assert retval==null : "Can't make a void function non-void";
78 cananian 1.1.2.16         } else {
79 cananian 1.3.2.1              assert retval!=null : "Can't make a non-void function void";
80 cananian 1.1.2.16             setChild(kidsStart-1, retval);
81 cananian 1.1.2.16         }
82 cananian 1.1.2.16     }
83 cananian 1.1.2.16     /** Sets the function reference expression. */
84 cananian 1.1.2.16     public void setFunc(Exp func) { setChild(kidsStart, func); }
85 cananian 1.1.2.16     /** Sets the function argument list. */
86 cananian 1.1.2.16     public void setArgs(ExpList args) {
87 cananian 1.3.2.1          assert argsLength==ExpList.size(args) : "Can't change the number of arguments to the function";
88 cananian 1.1.2.16         int i=kidsStart+1;
89 cananian 1.1.2.16         for (ExpList ep = args; ep!=null; ep=ep.tail)
90 cananian 1.1.2.16             setChild(i++, ep.head);
91 cananian 1.1.2.16     }
92 duncan   1.1.2.15 
93 duncan   1.1.2.1      abstract public boolean isNative();
94 cananian 1.1.2.11     abstract public void accept(TreeVisitor v);
95 duncan   1.1.2.1  }
96 cananian 1.2