1 cananian 1.1.2.1 // CONST.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.5 import harpoon.ClassFile.HCodeElement; 7 cananian 1.1.2.19 import harpoon.ClassFile.HDataElement; 8 cananian 1.1.2.25 import harpoon.Temp.TempMap; 9 cananian 1.1.2.4 import harpoon.Util.Util; 10 cananian 1.1.2.4 11 cananian 1.1.2.1 /** 12 cananian 1.1.2.1 * <code>CONST</code> objects are expressions which stand for a constant 13 duncan 1.1.2.15 * value. <code>CONST</code>s should be used to represent numeric types 14 duncan 1.1.2.15 * only. The one notable exception is the constant <code>null</code>, 15 duncan 1.1.2.15 * which is represented by a <code>CONST</code> with <code>value == null</code> 16 duncan 1.1.2.15 * and <code>type == POINTER</code>. 17 cananian 1.1.2.1 * 18 cananian 1.1.2.1 * @author C. Scott Ananian <cananian@alumni.princeton.edu>, based on 19 cananian 1.1.2.1 * <i>Modern Compiler Implementation in Java</i> by Andrew Appel. 20 cananian 1.4 * @version $Id: CONST.java,v 1.4 2002/04/10 03:05:40 cananian Exp $ 21 cananian 1.1.2.1 */ 22 cananian 1.1.2.19 public class CONST extends Exp implements PreciselyTyped, HDataElement { 23 cananian 1.1.2.4 /** The constant value of this <code>CONST</code> expression. */ 24 cananian 1.1.2.4 public final Number value; 25 cananian 1.1.2.4 /** The type of this <code>CONST</code> expression. */ 26 cananian 1.1.2.4 public final int type; 27 cananian 1.1.2.4 28 cananian 1.1.2.20 // PreciselyTyped interface 29 cananian 1.1.2.20 public final boolean isSmall; 30 duncan 1.1.2.16 private int bitwidth = -1; 31 cananian 1.1.2.18 private boolean signed = true; 32 duncan 1.1.2.16 33 cananian 1.1.2.5 public CONST(TreeFactory tf, HCodeElement source, int ival) { 34 cananian 1.1.2.24 super(tf, source, 0); 35 cananian 1.1.2.20 this.type = INT; this.value = new Integer(ival); this.isSmall=false; 36 cananian 1.1.2.4 } 37 cananian 1.1.2.5 public CONST(TreeFactory tf, HCodeElement source, long lval) { 38 cananian 1.1.2.24 super(tf, source, 0); 39 cananian 1.1.2.20 this.type = LONG; this.value = new Long(lval); this.isSmall=false; 40 cananian 1.1.2.4 } 41 cananian 1.1.2.5 public CONST(TreeFactory tf, HCodeElement source, float fval) { 42 cananian 1.1.2.24 super(tf, source, 0); 43 cananian 1.1.2.20 this.type = FLOAT; this.value = new Float(fval); this.isSmall=false; 44 cananian 1.1.2.4 } 45 cananian 1.1.2.5 public CONST(TreeFactory tf, HCodeElement source, double dval) { 46 cananian 1.1.2.24 super(tf, source, 0); 47 cananian 1.1.2.20 this.type = DOUBLE; this.value = new Double(dval); this.isSmall=false; 48 cananian 1.1.2.4 } 49 duncan 1.1.2.15 /** Creates a <code>CONST</code> representing the constant 50 duncan 1.1.2.15 * <code>null</code>. */ 51 duncan 1.1.2.15 public CONST(TreeFactory tf, HCodeElement source) { 52 cananian 1.1.2.24 super(tf, source, 0); 53 cananian 1.1.2.20 this.type = POINTER; this.value = null; this.isSmall=false; 54 duncan 1.1.2.12 } 55 duncan 1.1.2.15 56 duncan 1.1.2.16 /** Creates a CONST with a precisely defined type. 57 cananian 1.1.2.18 * For example, <code>CONST(tf, src, 8, true, 0xFF)</code> corresponds 58 cananian 1.1.2.18 * to the value <code>-1</code>, as a byte. 59 cananian 1.1.2.18 * <code>CONST(tf, src, 8, true, -1)</code> also corresponds 60 cananian 1.1.2.18 * to the value <code>-1</code> as a byte. 61 duncan 1.1.2.16 * @param bitwidth the width in bits of this <code>CONST</code>'s type. 62 duncan 1.1.2.16 * Fails unless <code>0 <= bitwidth < 32</code>. 63 duncan 1.1.2.16 * @param signed whether this <code>CONST</code> is signed 64 duncan 1.1.2.16 * @param val the value of this <code>CONST</code>. 65 duncan 1.1.2.16 * Note that only the lower <code>bitwidth</code> bits 66 duncan 1.1.2.16 * of this parameter will actually be used. 67 duncan 1.1.2.16 */ 68 duncan 1.1.2.16 public CONST(TreeFactory tf, HCodeElement source, 69 duncan 1.1.2.16 int bitwidth, boolean signed, int val) { 70 cananian 1.1.2.24 super(tf, source, 0); 71 cananian 1.3.2.1 assert (0<=bitwidth)&&(bitwidth<32) : "Invalid bitwidth"; 72 cananian 1.1.2.20 this.type = INT; 73 duncan 1.1.2.16 this.bitwidth = bitwidth; 74 duncan 1.1.2.16 this.signed = signed; 75 duncan 1.1.2.16 this.value = new Integer(val & ((~0) >> (32-bitwidth))); 76 cananian 1.1.2.20 this.isSmall = true; 77 duncan 1.1.2.16 } 78 duncan 1.1.2.16 79 duncan 1.1.2.8 private CONST(TreeFactory tf, HCodeElement source, 80 cananian 1.1.2.20 int type, Number value, 81 cananian 1.1.2.20 boolean isSmall, int bitwidth, boolean signed) { 82 cananian 1.1.2.24 super(tf, source, 0); 83 cananian 1.1.2.20 this.type = type; this.value = value; this.isSmall = isSmall; 84 duncan 1.1.2.17 this.bitwidth = bitwidth; this.signed = signed; 85 cananian 1.3.2.1 assert Type.isValid(type); 86 cananian 1.3.2.1 assert !isSmall || type==INT; 87 duncan 1.1.2.8 } 88 duncan 1.1.2.8 89 cananian 1.1.2.1 /** Return the constant value of this <code>CONST</code> expression. */ 90 cananian 1.1.2.4 public Number value() { return value; } 91 duncan 1.1.2.10 92 duncan 1.1.2.10 public int kind() { return TreeKind.CONST; } 93 duncan 1.1.2.10 94 duncan 1.1.2.13 public Exp build(TreeFactory tf, ExpList kids) { 95 cananian 1.3.2.1 assert kids==null; 96 cananian 1.1.2.20 return new CONST(tf, this, type, value, isSmall, bitwidth, signed); 97 duncan 1.1.2.13 } 98 cananian 1.1.2.2 99 cananian 1.1.2.2 // Typed interface. 100 cananian 1.3.2.1 public int type() { assert Type.isValid(type); return type; } 101 cananian 1.1.2.4 102 duncan 1.1.2.16 // PreciselyTyped interface. 103 cananian 1.1.2.20 /** Returns true if this is a sub-integer expression. */ 104 cananian 1.1.2.20 public boolean isSmall() { return isSmall; } 105 cananian 1.1.2.18 /** Returns the size of the expression, in bits. 106 cananian 1.1.2.20 * Only valid if the <code>isSmall()==true</code>. */ 107 cananian 1.3.2.1 public int bitwidth() { assert type==INT&&isSmall; return bitwidth; } 108 cananian 1.1.2.18 /** Returns true if this is a signed expression, false otherwise. 109 cananian 1.1.2.20 * Only valid if the <code>isSmall()==true</code>. */ 110 cananian 1.3.2.1 public boolean signed() { assert type==INT&&isSmall; return signed; } 111 duncan 1.1.2.16 112 duncan 1.1.2.3 /** Accept a visitor */ 113 cananian 1.1.2.21 public void accept(TreeVisitor v) { v.visit(this); } 114 duncan 1.1.2.7 115 cananian 1.1.2.25 public Tree rename(TreeFactory tf, TempMap tm, CloneCallback cb) { 116 cananian 1.1.2.26 return cb.callback(this, new CONST(tf, this, type, value, isSmall, bitwidth, signed), tm); 117 duncan 1.1.2.7 } 118 andyb 1.1.2.9 119 andyb 1.1.2.9 public String toString() { 120 cananian 1.1.2.20 return "CONST<"+Type.toString(this)+">("+value+")"; 121 andyb 1.1.2.9 } 122 cananian 1.2 }