1 cananian 1.1.2.1  // MOVE.java, created Wed Jan 13 21:14:57 1999 by cananian
  2 cananian 1.1.2.14 // Copyright (C) 1998 C. Scott Ananian <cananian@alumni.princeton.edu>
  3 cananian 1.1.2.14 // 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.4  import harpoon.ClassFile.HCodeElement;
  7 cananian 1.1.2.23 import harpoon.Temp.TempMap;
  8 cananian 1.1.2.4  import harpoon.Util.Util;
  9 cananian 1.1.2.4  
 10 duncan   1.1.2.10 import java.util.HashSet;
 11 duncan   1.1.2.10 import java.util.Iterator;
 12 duncan   1.1.2.10 import java.util.Set;
 13 duncan   1.1.2.7  
 14 duncan   1.1.2.7  
 15 cananian 1.1.2.1  /**
 16 cananian 1.1.2.1   * <code>MOVE</code> statements assign a value to a location.
 17 cananian 1.1.2.1   * <code>MOVE(TEMP <i>t</i>, <i>e</i>)</code> evaluates expression <i>e</i>
 18 cananian 1.1.2.1   * and assigns its value into temporary <i>t</i>.
 19 cananian 1.1.2.1   * <code>MOVE(MEM(<i>e1</i>, <i>k</i>), <i>e2</i>)</code> evaluates
 20 cananian 1.1.2.1   * expression <i>e1</i> yielding address <i>a</i>, then evaluates <i>e2</i>
 21 cananian 1.1.2.1   * and assigns its value into memory starting at address <i>a</i>.
 22 cananian 1.1.2.1   * 
 23 cananian 1.1.2.1   * @author  C. Scott Ananian <cananian@alumni.princeton.edu>, based on
 24 cananian 1.1.2.1   *          <i>Modern Compiler Implementation in Java</i> by Andrew Appel.
 25 cananian 1.4       * @version $Id: MOVE.java,v 1.4 2002/04/10 03:05:45 cananian Exp $
 26 cananian 1.1.2.1   */
 27 cananian 1.1.2.9  public class MOVE extends Stm implements Typed {
 28 cananian 1.1.2.9      /** Constructor. 
 29 cananian 1.1.2.9       * <p>The type of the <code>src</code> expression and of the
 30 cananian 1.1.2.9       * <code>dst</code> expression must be identical.  (Use
 31 cananian 1.1.2.9       * a <code>UNOP</code> to convert, if they are necessary.)
 32 cananian 1.1.2.9       */
 33 cananian 1.1.2.4      public MOVE(TreeFactory tf, HCodeElement source,
 34 cananian 1.1.2.4                  Exp dst, Exp src) {
 35 cananian 1.1.2.21         super(tf, source, 2);
 36 cananian 1.3.2.1          assert dst != src : "dst and src cannot be same";
 37 cananian 1.3.2.1          assert dst!=null && src!=null : "Dest and Source cannot be null";
 38 cananian 1.3.2.1          assert dst.type()==src.type() : "Dest (type:"+Type.toString(dst.type()) + 
 39 pnkfelix 1.1.2.11                     ") and Source (type:" + Type.toString(src.type()) + 
 40 cananian 1.3.2.1                      ") must have same type";
 41 cananian 1.3.2.1          assert dst.tf == src.tf : "Dest and Src must have same tree factory";
 42 cananian 1.3.2.1          assert tf == src.tf : "This and Src must have same tree factory";
 43 cananian 1.1.2.21         this.setDst(dst); this.setSrc(src); 
 44 pnkfelix 1.1.2.19 
 45 pnkfelix 1.1.2.20         // FSK: debugging hack
 46 pnkfelix 1.1.2.20         // this.accept(TreeVerifyingVisitor.norepeats());
 47 cananian 1.1.2.4      }
 48 duncan   1.1.2.7    
 49 cananian 1.1.2.21     /** Returns the expression giving the destination for the
 50 cananian 1.1.2.21      *  computed value. */
 51 cananian 1.1.2.21     public Exp getDst() { return (Exp) getChild(0); }
 52 cananian 1.1.2.21     /** Returns the expression for the computed value. */
 53 cananian 1.1.2.21     public Exp getSrc() { return (Exp) getChild(1); } 
 54 cananian 1.1.2.21 
 55 cananian 1.1.2.21     /** Sets the expression giving the destination for the
 56 cananian 1.1.2.21      *  computed value. */
 57 cananian 1.1.2.22     public void setDst(Exp dst) {
 58 cananian 1.3.2.1          assert dst.kind()==TreeKind.MEM || dst.kind()==TreeKind.TEMP;
 59 cananian 1.1.2.22         setChild(0, dst);
 60 cananian 1.1.2.22     }
 61 cananian 1.1.2.21     /** Sets the expression for the computed value. */
 62 cananian 1.1.2.21     public void setSrc(Exp src) { setChild(1, src); }
 63 duncan   1.1.2.8  
 64 duncan   1.1.2.8      public int kind() { return TreeKind.MOVE; }
 65 duncan   1.1.2.8  
 66 cananian 1.1.2.21     public ExpList kids() {
 67 cananian 1.1.2.21         ExpList commontail = new ExpList(getSrc(), null);
 68 cananian 1.1.2.21         if (getDst().kind()==TreeKind.MEM)
 69 cananian 1.1.2.21             return new ExpList(getDst().kids().head, commontail);
 70 cananian 1.1.2.21         else return commontail;
 71 cananian 1.1.2.21     }
 72 duncan   1.1.2.13     public Stm build(TreeFactory tf, ExpList kids) {
 73 cananian 1.3.2.1          assert kids!=null;
 74 cananian 1.3.2.1          assert tf == kids.head.tf;
 75 cananian 1.3.2.1          assert tf == this.tf : "cloning src not yet implemented";
 76 cananian 1.1.2.21         if (getDst().kind()==TreeKind.MEM)
 77 cananian 1.1.2.21             return new MOVE(tf, this, 
 78 cananian 1.1.2.21                             getDst().build(tf, new ExpList(kids.head, null)),
 79 cananian 1.1.2.21                             kids.tail.head);
 80 cananian 1.1.2.21         else return new MOVE(tf, this, getDst(), kids.head);
 81 cananian 1.1.2.1      }
 82 duncan   1.1.2.3      /** Accept a visitor */
 83 cananian 1.1.2.15     public void accept(TreeVisitor v) { v.visit(this); } 
 84 duncan   1.1.2.5  
 85 cananian 1.1.2.23     public Tree rename(TreeFactory tf, TempMap tm, CloneCallback cb) {
 86 cananian 1.1.2.23         return cb.callback(this, new MOVE(tf, this,
 87 cananian 1.1.2.23                                           (Exp)getDst().rename(tf, tm, cb),
 88 cananian 1.1.2.24                                           (Exp)getSrc().rename(tf, tm, cb)),
 89 cananian 1.1.2.24                            tm);
 90 duncan   1.1.2.5      }
 91 cananian 1.1.2.9  
 92 cananian 1.1.2.9      /** @return the type of <code>dst</code> expression. */
 93 cananian 1.1.2.21     public int type() { return getDst().type(); }
 94 cananian 1.1.2.21     public boolean isDoubleWord() { return getDst().isDoubleWord(); }
 95 cananian 1.1.2.21     public boolean isFloatingPoint() { return getDst().isFloatingPoint(); }
 96 andyb    1.1.2.6  
 97 andyb    1.1.2.6      public String toString() {
 98 cananian 1.1.2.21         return "MOVE(#"+getDst().getID()+", #"+getSrc().getID()+")";
 99 andyb    1.1.2.6      }
100 cananian 1.1.2.1  }
101 cananian 1.2