1 cananian 1.1.2.1  // MEM.java, created Wed Jan 13 21:14:57 1999 by cananian
  2 cananian 1.1.2.13 // Copyright (C) 1998 C. Scott Ananian <cananian@alumni.princeton.edu>
  3 cananian 1.1.2.13 // 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.5  import harpoon.ClassFile.HCodeElement;
  7 cananian 1.1.2.24 import harpoon.Temp.TempMap;
  8 cananian 1.1.2.4  import harpoon.Util.Util;
  9 cananian 1.1.2.4  
 10 cananian 1.1.2.1  /**
 11 cananian 1.1.2.1   * <code>MEM</code> objects are expressions which stand for the contents of
 12 cananian 1.1.2.2   * a value in memory starting at the address specified by the
 13 cananian 1.1.2.1   * subexpression.  Note that when <code>MEM</code> is used as the left child
 14 cananian 1.1.2.2   * of a <code>MOVE</code> or <code>CALL</code>, it means "store," but
 15 cananian 1.1.2.2   * anywhere else it means "fetch."
 16 cananian 1.1.2.1   * 
 17 cananian 1.1.2.1   * @author  C. Scott Ananian <cananian@alumni.princeton.edu>, based on
 18 cananian 1.1.2.1   *          <i>Modern Compiler Implementation in Java</i> by Andrew Appel.
 19 cananian 1.4       * @version $Id: MEM.java,v 1.4 2002/04/10 03:05:45 cananian Exp $
 20 cananian 1.1.2.1   */
 21 duncan   1.1.2.14 public class MEM extends Exp implements PreciselyTyped {
 22 cananian 1.1.2.4      /** The type of this memory reference expression. */
 23 cananian 1.1.2.22     public final int type;
 24 duncan   1.1.2.14 
 25 cananian 1.1.2.17     // PreciselyTyped interface
 26 cananian 1.1.2.17     public final boolean isSmall;
 27 duncan   1.1.2.14     private int bitwidth   = -1;
 28 cananian 1.1.2.16     private boolean signed = true;
 29 duncan   1.1.2.14 
 30 cananian 1.1.2.1      /** Constructor. */
 31 cananian 1.1.2.5      public MEM(TreeFactory tf, HCodeElement source,
 32 cananian 1.1.2.5                 int type, Exp exp) {
 33 cananian 1.1.2.23         super(tf, source, 1);
 34 duncan   1.1.2.20         this.type=type; this.setExp(exp); this.isSmall=false;
 35 cananian 1.3.2.1          assert Type.isValid(type) && exp!=null;
 36 cananian 1.3.2.1          assert tf == exp.tf : "This and Exp must have same tree factory";
 37 cananian 1.1.2.4      }
 38 duncan   1.1.2.14 
 39 duncan   1.1.2.14     /** Creates a MEM with a precisely defined type.  
 40 duncan   1.1.2.14      *  @param bitwidth    the width in bits of this <code>MEM</code>'s type.
 41 duncan   1.1.2.14      *                     Fails unless <code>0 <= bitwidth < 32</code>.
 42 duncan   1.1.2.14      *  @param signed      whether this <code>MEM</code> is signed
 43 duncan   1.1.2.14      *  @param exp         the location at which to load or store 
 44 duncan   1.1.2.14      */
 45 duncan   1.1.2.14     public MEM(TreeFactory tf, HCodeElement source, 
 46 duncan   1.1.2.14                int bitwidth, boolean signed, Exp exp) { 
 47 cananian 1.1.2.23         super(tf, source, 1);
 48 cananian 1.3.2.1          assert exp!=null;
 49 duncan   1.1.2.20         this.type=INT; this.setExp(exp); this.isSmall=true;
 50 duncan   1.1.2.14         this.bitwidth=bitwidth; this.signed=signed;
 51 cananian 1.3.2.1          assert tf == exp.tf : "This and Exp must have same tree factory";
 52 cananian 1.3.2.1          assert (0<=bitwidth)&&(bitwidth<32) : "Invalid bitwidth";
 53 duncan   1.1.2.14     }
 54 duncan   1.1.2.14     
 55 duncan   1.1.2.14     private MEM(TreeFactory tf, HCodeElement source, int type, Exp exp, 
 56 cananian 1.1.2.17                 boolean isSmall, int bitwidth, boolean signed) { 
 57 cananian 1.1.2.23         super(tf, source, 1);
 58 cananian 1.3.2.1          assert exp!=null;
 59 duncan   1.1.2.20         this.type=type; this.setExp(exp); this.isSmall=isSmall;
 60 duncan   1.1.2.14         this.bitwidth=bitwidth; this.signed=signed;
 61 cananian 1.3.2.1          assert tf == exp.tf : "This and Exp must have same tree factory";
 62 cananian 1.3.2.1          assert Type.isValid(type);
 63 cananian 1.3.2.1          assert !isSmall || type==INT;
 64 duncan   1.1.2.20     }
 65 duncan   1.1.2.20 
 66 cananian 1.1.2.23     /** Returns a subexpression evaluating to a memory reference location. */
 67 cananian 1.1.2.23     public Exp getExp() { return (Exp) getChild(0); } 
 68 duncan   1.1.2.20 
 69 cananian 1.1.2.23     /** Sets the memory reference subexpression. */
 70 cananian 1.1.2.23     public void setExp(Exp exp) { setChild(0, exp); }
 71 duncan   1.1.2.9  
 72 duncan   1.1.2.9      public int kind() { return TreeKind.MEM; }
 73 duncan   1.1.2.9  
 74 duncan   1.1.2.12     public Exp build(TreeFactory tf, ExpList kids) {
 75 cananian 1.3.2.1          assert tf == kids.head.tf;
 76 cananian 1.3.2.1          assert kids.tail==null;
 77 cananian 1.1.2.17         return new MEM(tf, this, type, kids.head, isSmall, bitwidth, signed);
 78 cananian 1.1.2.4      }
 79 cananian 1.1.2.2  
 80 cananian 1.1.2.2      // Typed interface:
 81 cananian 1.3.2.1      public int type() { assert Type.isValid(type); return type; }
 82 duncan   1.1.2.14     
 83 duncan   1.1.2.14     // PreciselyTyped interface:
 84 cananian 1.1.2.17     /** Returns true if this is a sub-integer expression. */
 85 cananian 1.1.2.17     public boolean isSmall() { return isSmall; }
 86 cananian 1.1.2.16     /** Returns the size of the expression, in bits.
 87 cananian 1.1.2.17      *  Only valid if the <code>isSmall()==true</code>. */
 88 cananian 1.3.2.1      public int bitwidth() { assert type==INT&&isSmall; return bitwidth; }
 89 cananian 1.1.2.16     /** Returns true if this is a signed expression, false otherwise.
 90 cananian 1.1.2.17      *  Only valid if the <code>isSmall()==true</code>. */
 91 cananian 1.3.2.1      public boolean signed() { assert type==INT&&isSmall; return signed; }
 92 cananian 1.1.2.4  
 93 duncan   1.1.2.3      /** Accept a visitor */
 94 cananian 1.1.2.18     public void accept(TreeVisitor v) { v.visit(this); }
 95 duncan   1.1.2.7  
 96 cananian 1.1.2.24     public Tree rename(TreeFactory tf, TempMap tm, CloneCallback cb) {
 97 cananian 1.1.2.24         return cb.callback(this, new MEM(tf, this, type,
 98 cananian 1.1.2.24                                          (Exp)getExp().rename(tf, tm, cb), 
 99 cananian 1.1.2.25                                          isSmall, bitwidth, signed),
100 cananian 1.1.2.25                            tm);
101 andyb    1.1.2.8      }
102 andyb    1.1.2.8  
103 andyb    1.1.2.8      public String toString() {
104 cananian 1.1.2.23         return "MEM<" + Type.toString(this) + ">(#" + getExp().getID() + ")";
105 duncan   1.1.2.7      }
106 cananian 1.1.2.1  }
107 cananian 1.2