1 cananian 1.1.2.1 // TreeUseDefer.java, created Wed Feb 16 16:04:18 2000 by cananian 2 cananian 1.1.2.1 // Copyright (C) 2000 C. Scott Ananian <cananian@alumni.princeton.edu> 3 cananian 1.1.2.1 // Licensed under the terms of the GNU GPL; see COPYING for details. 4 cananian 1.1.2.1 package harpoon.IR.Tree; 5 cananian 1.1.2.1 6 cananian 1.1.2.1 import harpoon.ClassFile.HCodeElement; 7 cananian 1.1.2.1 import harpoon.Temp.Temp; 8 cananian 1.5 import net.cscott.jutil.Default; 9 cananian 1.1.2.1 import harpoon.Util.Util; 10 cananian 1.1.2.1 11 cananian 1.1.2.1 import java.util.ArrayList; 12 cananian 1.1.2.1 import java.util.Arrays; 13 cananian 1.1.2.1 import java.util.Collection; 14 cananian 1.1.2.1 import java.util.Collections; 15 cananian 1.1.2.1 import java.util.HashSet; 16 cananian 1.1.2.1 import java.util.Set; 17 cananian 1.1.2.1 /** 18 cananian 1.1.2.1 * <code>TreeUseDefer</code> implements the <code>Properties.UseDefer</code> 19 cananian 1.1.2.1 * interface for non-<code>SEQ</code> <code>Stm</code>s of a tree. 20 cananian 1.1.2.1 * 21 cananian 1.1.2.1 * @author C. Scott Ananian <cananian@alumni.princeton.edu> 22 cananian 1.5 * @version $Id: TreeUseDefer.java,v 1.5 2004/02/08 01:55:51 cananian Exp $ 23 cananian 1.1.2.1 */ 24 cananian 1.3.2.2 public class TreeUseDefer extends harpoon.IR.Properties.UseDefer<Tree> { 25 cananian 1.1.2.1 private final Code code; 26 cananian 1.1.2.1 27 cananian 1.1.2.1 /** Creates a <code>UseDefer</code>. */ 28 cananian 1.1.2.1 public TreeUseDefer(Code code) { this.code = code; } 29 cananian 1.1.2.1 30 cananian 1.1.2.1 /** Returns a collection of <code>Temp</code>s defined by <code>hce</code>. 31 cananian 1.1.2.1 * <p> 32 cananian 1.1.2.1 * The only <code>Tree.Tree</code>s which define <code>Temp</code>s 33 cananian 1.1.2.1 * are <code>CALL</code>, <code>NATIVECALL</code>, <code>METHOD</code>, 34 cananian 1.1.2.1 * and <code>MOVE</code> when the destination expression is a 35 cananian 1.1.2.1 * <code>TEMP</code>. For all other elements, this method returns 36 cananian 1.1.2.1 * a zero-element collection. */ 37 cananian 1.3.2.2 public Collection<Temp> defC(Tree tree) { 38 cananian 1.3.2.2 assert (tree instanceof Stm) && ! (tree instanceof SEQ); 39 cananian 1.1.2.1 if (tree instanceof INVOCATION) { 40 cananian 1.1.2.1 INVOCATION invok = (INVOCATION) tree; 41 cananian 1.3.2.2 Collection<Temp> c = new ArrayList<Temp>(2); 42 cananian 1.1.2.1 if (invok.getRetval()!=null) c.add(invok.getRetval().temp); 43 cananian 1.1.2.1 if (invok instanceof CALL) c.add(((CALL)invok).getRetex().temp); 44 cananian 1.1.2.1 return c; 45 cananian 1.1.2.1 } 46 cananian 1.1.2.1 if (tree instanceof METHOD) { 47 cananian 1.1.2.1 TEMP[] rT = ((METHOD) tree).getParams(); 48 cananian 1.1.2.1 Temp[] rt = new Temp[rT.length]; 49 cananian 1.1.2.1 for (int i=0; i<rt.length; i++) 50 cananian 1.1.2.1 rt[i] = rT[i].temp; 51 cananian 1.1.2.1 return Arrays.asList(rt); 52 cananian 1.1.2.1 } 53 cananian 1.1.2.1 if (tree instanceof MOVE && ((MOVE)tree).getDst() instanceof TEMP) 54 cananian 1.1.2.1 return Collections.singleton(((TEMP) ((MOVE)tree).getDst()).temp); 55 cananian 1.1.2.1 // okay, no definitions then. 56 cananian 1.1.2.1 return Collections.EMPTY_SET; 57 cananian 1.1.2.1 } 58 cananian 1.1.2.1 /** Returns a collection of <code>Temp</code>s which are used by the 59 cananian 1.1.2.1 * statement/expression subtree rooted at <code>hce</code>. */ 60 cananian 1.3.2.2 public Collection<Temp> useC(Tree hce) { 61 cananian 1.3.2.1 assert (hce instanceof Stm) && ! (hce instanceof SEQ); 62 cananian 1.3.2.2 Set<Temp> s = new HashSet<Temp>(); 63 cananian 1.3.2.2 addUses(s, hce.kids()); 64 cananian 1.1.2.1 return s; 65 cananian 1.1.2.1 } 66 cananian 1.3.2.2 private static void addUses(Set<Temp> uses, ExpList el) { 67 cananian 1.1.2.1 for (ExpList elp = el; elp!=null; elp=elp.tail) 68 cananian 1.1.2.1 addUses(uses, elp.head); 69 cananian 1.1.2.1 } 70 cananian 1.3.2.2 private static void addUses(Set<Temp> uses, Exp e) { 71 cananian 1.1.2.1 if (e instanceof TEMP) 72 cananian 1.1.2.1 uses.add(((TEMP)e).temp); 73 cananian 1.1.2.1 addUses(uses, e.kids()); 74 cananian 1.1.2.1 } 75 cananian 1.2 }