1 cananian 1.1.2.1 // DATUM.java, created Fri Jul 23 13:39:22 1999 by duncan 2 cananian 1.1.2.1 // Copyright (C) 1998 Duncan Bryce <duncan@lcs.mit.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.3 import harpoon.Temp.TempMap; 8 cananian 1.1.2.1 import harpoon.Util.Util; 9 cananian 1.1.2.1 10 cananian 1.1.2.1 /** 11 cananian 1.1.2.1 * <code>DATUM</code> objects are statements which write a value to memory 12 cananian 1.1.2.1 * at the time when a program is loaded. The location written is 13 cananian 1.1.2.1 * calculated using this formula: 14 cananian 1.1.2.1 * 15 cananian 1.1.2.1 * <PRE> 16 cananian 1.1.2.1 * location = base + offset 17 cananian 1.1.2.1 * 18 cananian 1.1.2.1 * where 19 cananian 1.1.2.1 * base = location of nearest LABEL, l, which precedes this DATUM 20 cananian 1.1.2.1 * offset = the total size of all instructions between l and this DATUM 21 cananian 1.1.2.1 * 22 cananian 1.1.2.1 * </PRE> 23 cananian 1.1.2.1 * 24 cananian 1.1.2.1 * @author Duncan Bryce <duncan@lcs.mit.edu> 25 cananian 1.4 * @version $Id: DATUM.java,v 1.4 2002/04/10 03:05:44 cananian Exp $ 26 cananian 1.1.2.1 */ 27 cananian 1.1.2.1 public class DATUM extends Stm implements harpoon.ClassFile.HDataElement { 28 cananian 1.1.2.1 /** If false, the memory is not initialized; instead it is reserved 29 cananian 1.1.2.1 * with an unspecified value. */ 30 cananian 1.1.2.1 public final boolean initialized; 31 cananian 1.1.2.1 32 cananian 1.1.2.1 /** Class constructor. 33 cananian 1.1.2.1 * The parameter <br><code>data</code> must be an instance of either 34 cananian 1.1.2.1 * <code>harpoon.IR.Tree.CONST</code> or 35 cananian 1.1.2.1 * <code>harpoon.IR.Tree.NAME</code>. Passing <code>null</code> for 36 cananian 1.1.2.1 * the parameter <code>data</code> reserves a word of memory at the 37 cananian 1.1.2.1 * location of this <code>DATUM</code> without assigning it a value. 38 cananian 1.1.2.1 */ 39 cananian 1.1.2.1 public DATUM(TreeFactory tf, HCodeElement source, Exp data) { 40 cananian 1.1.2.2 super(tf, source, 1); 41 cananian 1.3.2.1 assert data!=null; 42 cananian 1.1.2.1 this.setData(data); 43 cananian 1.1.2.1 this.initialized = true; 44 cananian 1.3.2.1 assert data.kind()==TreeKind.CONST || 45 cananian 1.3.2.1 data.kind()==TreeKind.NAME; 46 cananian 1.3.2.1 assert tf==data.tf : "Dest and Src must have same tree factory"; 47 cananian 1.1.2.1 } 48 cananian 1.1.2.1 49 cananian 1.1.2.1 /** Class constructor. 50 cananian 1.1.2.1 * Reserves memory at the location of this <code>DATUM</code> 51 cananian 1.1.2.1 * of the size of the specified type without assigning it a value. 52 cananian 1.1.2.1 */ 53 cananian 1.1.2.1 public DATUM(TreeFactory tf, HCodeElement source, int type) { 54 cananian 1.1.2.2 super(tf, source, 1); 55 cananian 1.3.2.1 assert Type.isValid(type); 56 cananian 1.1.2.1 if (type==Type.INT) 57 cananian 1.1.2.1 this.setData(new CONST(tf, source, (int)0)); 58 cananian 1.1.2.1 else if (type==Type.LONG) 59 cananian 1.1.2.1 this.setData(new CONST(tf, source, (long)0)); 60 cananian 1.1.2.1 else if (type==Type.FLOAT) 61 cananian 1.1.2.1 this.setData(new CONST(tf, source, (float)0)); 62 cananian 1.1.2.1 else if (type==Type.DOUBLE) 63 cananian 1.1.2.1 this.setData(new CONST(tf, source, (double)0)); 64 cananian 1.1.2.1 else if (type==Type.POINTER) 65 cananian 1.1.2.1 this.setData(new CONST(tf, source)); // null 66 cananian 1.1.2.1 else throw new Error("Impossible!"); 67 cananian 1.1.2.1 this.initialized = false; 68 cananian 1.1.2.1 } 69 cananian 1.1.2.1 70 cananian 1.1.2.1 /** Class constructor. 71 cananian 1.1.2.1 * Reserves memory at the location of this <code>DATUM</code> 72 cananian 1.1.2.1 * of the specified small without assigning it a value. 73 cananian 1.1.2.1 */ 74 cananian 1.1.2.1 public DATUM(TreeFactory tf, HCodeElement source, 75 cananian 1.1.2.1 int bitwidth, boolean signed) { 76 cananian 1.1.2.2 super(tf, source, 1); 77 cananian 1.1.2.1 this.setData(new CONST(tf, source, bitwidth, signed, 0)); 78 cananian 1.1.2.1 this.initialized = false; 79 cananian 1.1.2.1 } 80 cananian 1.1.2.1 81 cananian 1.1.2.1 private DATUM(TreeFactory tf, HCodeElement source, 82 cananian 1.1.2.1 Exp data, boolean initialized) { 83 cananian 1.1.2.2 super(tf, source, 1); 84 cananian 1.1.2.1 this.setData(data); 85 cananian 1.1.2.1 this.initialized = initialized; 86 cananian 1.3.2.1 assert data.kind()==TreeKind.CONST || 87 cananian 1.3.2.1 data.kind()==TreeKind.NAME; 88 cananian 1.3.2.1 assert tf==data.tf : "Dest and Src must have same tree factory"; 89 cananian 1.1.2.1 } 90 cananian 1.1.2.1 91 cananian 1.1.2.2 /** Returns the expression to write to memory. Never null. */ 92 cananian 1.1.2.2 public Exp getData() { return (Exp) getChild(0); } 93 cananian 1.1.2.1 94 cananian 1.1.2.2 /** Sets the expression to write to memory. Never null. */ 95 cananian 1.1.2.2 public void setData(Exp data) { setChild(0, data); } 96 cananian 1.1.2.1 97 cananian 1.1.2.1 public int kind() { return TreeKind.DATUM; } 98 cananian 1.1.2.1 99 cananian 1.1.2.1 public Stm build(TreeFactory tf, ExpList kids) { 100 cananian 1.3.2.1 assert kids!=null && kids.tail==null; 101 cananian 1.3.2.1 assert kids.head == null || tf == kids.head.tf; 102 cananian 1.1.2.1 return new DATUM(tf, this, kids.head, initialized); 103 cananian 1.1.2.1 } 104 cananian 1.1.2.1 105 cananian 1.1.2.1 /** Accept a visitor */ 106 cananian 1.1.2.1 public void accept(TreeVisitor v) { v.visit(this); } 107 cananian 1.1.2.1 108 cananian 1.1.2.3 public Tree rename(TreeFactory tf, TempMap tm, CloneCallback cb) { 109 cananian 1.1.2.4 return cb.callback(this, new DATUM(tf, this, (Exp)getData().rename(tf, tm, cb),initialized), tm); 110 cananian 1.1.2.1 } 111 cananian 1.1.2.1 112 cananian 1.1.2.1 public String toString() { 113 cananian 1.1.2.1 StringBuffer sb = new StringBuffer("DATUM<"); 114 cananian 1.1.2.2 sb.append(getData() instanceof PreciselyTyped ? 115 cananian 1.1.2.2 Type.toString((PreciselyTyped)getData()) : 116 cananian 1.1.2.2 Type.toString(getData().type())); 117 cananian 1.1.2.1 sb.append(">(#"); sb.append(getID()); sb.append(")"); 118 cananian 1.1.2.1 return sb.toString(); 119 cananian 1.1.2.1 } 120 cananian 1.2 }