1 cananian 1.1.2.7 // ConstPointer.java, created Sat Mar 27 17:05:07 1999 by duncan 2 cananian 1.1.2.6 // Copyright (C) 1998 Duncan Bryce <duncan@lcs.mit.edu> 3 cananian 1.1.2.6 // Licensed under the terms of the GNU GPL; see COPYING for details. 4 duncan 1.1.2.1 package harpoon.Interpret.Tree; 5 duncan 1.1.2.1 6 duncan 1.1.2.1 import harpoon.ClassFile.HClass; 7 duncan 1.1.2.1 import harpoon.Util.Tuple; 8 duncan 1.1.2.1 import harpoon.Util.Util; 9 duncan 1.1.2.1 import harpoon.Temp.Label; 10 duncan 1.1.2.1 11 duncan 1.1.2.1 /** 12 duncan 1.1.2.1 * The <code>ConstPointer</code> class is used to represent 13 duncan 1.1.2.1 * pointers that can be resolved statically. Unlike the other pointer 14 duncan 1.1.2.1 * classes, <code>ConstPointer</code>s have no offsets. Their base 15 duncan 1.1.2.1 * is simply a <code>Label</code> object. 16 duncan 1.1.2.1 * 17 duncan 1.1.2.1 * @author Duncan Bryce <duncan@lcs.mit.edu> 18 cananian 1.2 * @version $Id: ConstPointer.java,v 1.2 2002/02/25 21:05:50 cananian Exp $ 19 duncan 1.1.2.1 */ 20 duncan 1.1.2.1 class ConstPointer extends Pointer { 21 duncan 1.1.2.1 private final StaticState ss; 22 duncan 1.1.2.4 23 duncan 1.1.2.4 public static final ConstPointer NULL_POINTER = 24 duncan 1.1.2.4 new ConstPointer(null, null); 25 duncan 1.1.2.1 26 duncan 1.1.2.1 /** Class constructor. */ 27 duncan 1.1.2.1 ConstPointer(Label label, StaticState ss) { 28 duncan 1.1.2.1 super(new Object[] { label } ); 29 duncan 1.1.2.1 this.ss = ss; 30 duncan 1.1.2.1 } 31 duncan 1.1.2.1 32 duncan 1.1.2.1 /** Throws an <code>Error</code>. <code>ConstPointer</code>s must 33 duncan 1.1.2.1 * remain constant */ 34 duncan 1.1.2.1 public Pointer add(long offset) { 35 duncan 1.1.2.1 throw new Error("Can't add to a ConstPointer!"); 36 duncan 1.1.2.1 } 37 duncan 1.1.2.1 38 duncan 1.1.2.1 /** Returns true if <code>obj</code> is a <code>ConstPointer</code> which 39 duncan 1.1.2.1 * points to the same location as this <code>ConstPointer</code>. 40 duncan 1.1.2.1 */ 41 duncan 1.1.2.1 public boolean equals(Object obj) { 42 cananian 1.1.2.3 ConstPointer ptr; 43 cananian 1.1.2.3 if (this==obj) return true; 44 cananian 1.1.2.3 if (null==obj) return false; 45 duncan 1.1.2.4 if (this==NULL_POINTER || obj==NULL_POINTER) return false; 46 cananian 1.1.2.3 try { ptr = (ConstPointer)obj; } 47 cananian 1.1.2.3 catch (ClassCastException e) { return false; } 48 cananian 1.1.2.3 return ((Label)getBase()).toString(). 49 cananian 1.1.2.3 equals(((Label)ptr.getBase()).toString()); 50 duncan 1.1.2.1 } 51 duncan 1.1.2.1 52 duncan 1.1.2.1 /** Returns a <code>Label</code> representing the base of this 53 duncan 1.1.2.1 * <code>ConstPointer</code>. 54 duncan 1.1.2.1 */ 55 duncan 1.1.2.1 public Object getBase() { return (Label)proj(0); } 56 duncan 1.1.2.1 57 duncan 1.1.2.1 /** Throws an <code>Error</code>. Since <code>ConstPointer</code>s 58 duncan 1.1.2.1 * have no offsets, it is incorrect to try to access their offsets. 59 duncan 1.1.2.1 */ 60 duncan 1.1.2.1 public long getOffset() { 61 duncan 1.1.2.1 throw new Error("ConstPointers have no offsets"); 62 duncan 1.1.2.1 } 63 duncan 1.1.2.1 64 duncan 1.1.2.1 /** Returns the value obtained by dereferencing this 65 duncan 1.1.2.1 * <code>ConstPointer</code>. 66 duncan 1.1.2.1 */ 67 duncan 1.1.2.1 public Object getValue() { 68 duncan 1.1.2.5 return (this==NULL_POINTER) ? 69 duncan 1.1.2.5 Method.toNonNativeFormat(null) : 70 duncan 1.1.2.5 Method.toNonNativeFormat(ss.getValue(this)); 71 duncan 1.1.2.1 } 72 duncan 1.1.2.1 73 duncan 1.1.2.1 /** Always returns true. */ 74 duncan 1.1.2.1 public boolean isConst() { return true; } 75 duncan 1.1.2.1 76 duncan 1.1.2.1 /** Always returns false. */ 77 duncan 1.1.2.1 public boolean isDerived() { return false; } 78 duncan 1.1.2.4 79 duncan 1.1.2.4 /** Returns an integer enumeration of the kind of this Pointer. The 80 duncan 1.1.2.4 enumerated values are public fields of the Pointer class. 81 duncan 1.1.2.4 */ 82 duncan 1.1.2.4 public int kind() { return Pointer.CONST_PTR; } 83 duncan 1.1.2.1 84 duncan 1.1.2.1 /** If this <code>ConstPointer</code> points to a static field, then 85 duncan 1.1.2.1 * returns the type of this static field. Otherwise throws an 86 duncan 1.1.2.1 * <code>Error</code>. 87 duncan 1.1.2.1 */ 88 duncan 1.1.2.1 public HClass getType() { 89 duncan 1.1.2.1 return ss.getField(this).getType(); 90 duncan 1.1.2.1 } 91 duncan 1.1.2.1 92 duncan 1.1.2.1 /** If this <code>ConstPointer</code> points to a static field, then 93 duncan 1.1.2.1 * updates the value of this static field. Otherwise throws an 94 duncan 1.1.2.1 * <code>Error</code>. 95 duncan 1.1.2.1 */ 96 duncan 1.1.2.1 public void updateValue(Object value) { 97 duncan 1.1.2.1 ss.updateFieldValue(this, Method.toNativeFormat(value, getType())); 98 duncan 1.1.2.1 } 99 duncan 1.1.2.1 100 duncan 1.1.2.1 /** Returns a human-readable representation of this 101 duncan 1.1.2.1 * <code>ConstPointer</code>. 102 duncan 1.1.2.1 */ 103 duncan 1.1.2.1 public String toString() { 104 duncan 1.1.2.1 StringBuffer sb = new StringBuffer("ConstPtr: < "); 105 duncan 1.1.2.4 if (this==NULL_POINTER) 106 duncan 1.1.2.4 sb.append("null"); 107 duncan 1.1.2.4 else 108 duncan 1.1.2.4 sb.append(getBase().toString()); 109 duncan 1.1.2.1 sb.append(" >"); 110 duncan 1.1.2.1 return sb.toString(); 111 duncan 1.1.2.1 } 112 duncan 1.1.2.1 113 duncan 1.1.2.1 } 114 duncan 1.1.2.2 115 duncan 1.1.2.2 116 cananian 1.2