1 cananian 1.1.2.1 // Edge.java, created Wed Sep  9 20:53:22 1998 by cananian
 2 cananian 1.1.2.1 // Copyright (C) 1998 C. Scott Ananian <cananian@alumni.princeton.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.Quads;
 5 cananian 1.1.2.1 
 6 cananian 1.1.2.2 import harpoon.ClassFile.HCodeEdge;
 7 cananian 1.1.2.2 import harpoon.ClassFile.HCodeElement;
 8 duncan   1.1.2.8 import harpoon.IR.Properties.CFGEdge; 
 9 duncan   1.1.2.8 import harpoon.IR.Properties.CFGraphable; 
10 cananian 1.1.2.1 import harpoon.Util.ArrayFactory;
11 cananian 1.1.2.3 import harpoon.Util.Util;
12 cananian 1.1.2.1 /**
13 cananian 1.1.2.1  * <code>Edge</code> objects connect <code>Quad</code> nodes in the
14 cananian 1.1.2.1  * control-flow graph.  The <code>hashCode</code> and <code>equals</code>
15 cananian 1.1.2.1  * methods of <code>Edge</code> have been implemented so that 
16 andyb    1.1.2.5  * <code>Edge</code>s can be used as hash table keys to associate analysis
17 cananian 1.1.2.1  * data with control-flow edges.
18 cananian 1.1.2.1  * 
19 cananian 1.1.2.1  * @author  C. Scott Ananian <cananian@alumni.princeton.edu>
20 cananian 1.6      * @version $Id: Edge.java,v 1.6 2003/05/09 16:35:32 cananian Exp $
21 cananian 1.1.2.1  */
22 cananian 1.1.2.1 
23 cananian 1.5     public class Edge extends CFGEdge<Quad,Edge> implements java.io.Serializable {
24 cananian 1.1.2.1     Quad from, to;
25 cananian 1.1.2.1     int from_index, to_index;
26 cananian 1.1.2.1     
27 cananian 1.1.2.1     /** Creates a <code>Edge</code>. */
28 cananian 1.1.2.1     Edge(Quad from, int from_index, Quad to, int to_index) {
29 cananian 1.3.2.1         assert from!=null : "'from' is null";
30 cananian 1.3.2.1         assert to!=null : "'to' is null";
31 cananian 1.3.2.1         assert 0 <= from_index && from_index < from.next.length : "'from' index out of range";
32 cananian 1.3.2.1         assert 0 <= to_index   && to_index   <   to.prev.length : "'to' index out of range";
33 cananian 1.1.2.1         this.from = from;
34 cananian 1.1.2.1         this.from_index = from_index;
35 cananian 1.1.2.1         this.to = to;
36 cananian 1.1.2.1         this.to_index = to_index;
37 cananian 1.1.2.1     }
38 cananian 1.1.2.1 
39 cananian 1.1.2.1     /** Returns the source vertex of this Edge. */
40 cananian 1.6         public Quad from() { return from; }
41 cananian 1.1.2.1     /** Returns the destination vertex of this Edge. */
42 cananian 1.6         public Quad to() { return to; }
43 cananian 1.1.2.1     /** Returns the predecessor index of this Edge in <code>to</code>.
44 cananian 1.1.2.1      *  <code>this.to().prevEdge(this.which_pred()) == this</code>. */
45 cananian 1.1.2.1     public int which_pred() { return to_index; }
46 cananian 1.1.2.1     /** Returns the successor index of this Edge in <code>from</code>.
47 cananian 1.1.2.1      *  <code>this.from().nextEdge(this.which_succ()) == this</code>. */
48 cananian 1.1.2.1     public int which_succ() { return from_index; }
49 cananian 1.1.2.1 
50 cananian 1.1.2.1     /** Array factory: returns new <code>Edge[]</code>. */
51 cananian 1.3.2.2     public static final ArrayFactory<Edge> arrayFactory =
52 cananian 1.3.2.2         new ArrayFactory<Edge>() {
53 cananian 1.3.2.2             public Edge[] newArray(int len) { return new Edge[len]; }
54 cananian 1.1.2.1         };
55 cananian 1.1.2.1     
56 cananian 1.1.2.1     /** Compares two Edges for equality. */
57 cananian 1.1.2.1     public boolean equals(Object obj) {
58 cananian 1.1.2.6         Edge e;
59 cananian 1.1.2.6         if (this==obj) return true;
60 cananian 1.1.2.6         if (null==obj) return false;
61 cananian 1.1.2.6         try { e = (Edge) obj; }
62 cananian 1.1.2.6         catch (ClassCastException ignore) { return false; }
63 cananian 1.1.2.6         if (e.from.equals(from) && e.to.equals(to) &&
64 cananian 1.1.2.6             e.from_index == from_index /*&& e.to_index == to_index*/)
65 cananian 1.1.2.6             return true;
66 cananian 1.1.2.1         return false;
67 cananian 1.1.2.1     }
68 cananian 1.1.2.1     /** Returns a hash code value for this object. */
69 cananian 1.1.2.1     public int hashCode() {
70 cananian 1.1.2.1         // hashcode is independent of to_index so we
71 cananian 1.1.2.1         // can remove inputs to phis without screwing up an edge mapping.
72 cananian 1.1.2.1         // exit branches usually carry meaning, thus from_index *is*
73 cananian 1.1.2.1         // included in the hashcode.
74 cananian 1.1.2.1         return (from.hashCode() ^ to.hashCode()) + from_index;
75 cananian 1.1.2.1     }
76 cananian 1.1.2.1 
77 cananian 1.1.2.1     /** Returns a human-readable representation of the Edge. */
78 cananian 1.1.2.1     public String toString() {
79 cananian 1.1.2.1         return "Edge " +
80 cananian 1.1.2.1             "from (#"+from().getID()+","+which_succ()+") " +
81 cananian 1.1.2.1             "to (#"+to().getID()+","+which_pred()+")";
82 cananian 1.1.2.1     }
83 cananian 1.2     }