Fixed bug in toString() for FlatMethod of a task.
[IRC.git] / Robust / src / IR / Flat / FlatMethod.java
1 package IR.Flat;
2 import IR.MethodDescriptor;
3 import IR.TaskDescriptor;
4 import java.util.*;
5
6 public class FlatMethod extends FlatNode {
7     MethodDescriptor method;
8     TaskDescriptor task;
9     Vector parameterTemps;
10     Vector tagTemps;
11     Hashtable tagtointmap;
12
13     FlatMethod(MethodDescriptor md) {
14         method=md;
15         task=null;
16         parameterTemps=new Vector();
17         tagTemps=new Vector();
18         tagtointmap=new Hashtable();
19     }
20
21     FlatMethod(TaskDescriptor td) {
22         task=td;
23         method=null;
24         parameterTemps=new Vector();
25         tagTemps=new Vector();
26         tagtointmap=new Hashtable();
27     }
28
29     public String toString() {
30         if( method != null ) {
31             return method.toString();
32         }
33         return task.toString();
34     }
35
36     public MethodDescriptor getMethod() {
37         return method;
38     }
39
40     public TaskDescriptor getTask() {
41         return task;
42     }
43     
44     public int kind() {
45         return FKind.FlatMethod;
46     }
47
48     public void addParameterTemp(TempDescriptor t) {
49         parameterTemps.add(t);
50     }
51
52     public int numParameters() {
53         return parameterTemps.size();
54     }
55
56     public void addTagTemp(TempDescriptor t) {
57         tagtointmap.put(t, new Integer(tagTemps.size()));
58         tagTemps.add(t);
59     }
60
61     public int getTagInt(TempDescriptor t) {
62         return ((Integer)tagtointmap.get(t)).intValue();
63     }
64
65     public int numTags() {
66         return tagTemps.size();
67     }
68
69     public TempDescriptor getTag(int i) {
70         return (TempDescriptor) tagTemps.get(i);
71     }
72     
73     public TempDescriptor getParameter(int i) {
74         return (TempDescriptor) parameterTemps.get(i);
75     }
76
77     /** This method returns a set of the nodes in this flat representation */
78
79     public Set<FlatNode> getNodeSet() {
80         HashSet<FlatNode> tovisit=new HashSet<FlatNode>();
81         HashSet<FlatNode> visited=new HashSet<FlatNode>();
82         tovisit.add(this);
83         while(!tovisit.isEmpty()) {
84             FlatNode fn=tovisit.iterator().next();
85             tovisit.remove(fn);
86             visited.add(fn);
87             for(int i=0;i<fn.numNext();i++) {
88                 FlatNode nn=fn.getNext(i);
89                 if (!visited.contains(nn))
90                     tovisit.add(nn);
91             }
92         }
93         return visited;
94     }
95     
96     public String printMethod() {
97         return printMethod(null);
98     }
99
100     /** This method returns a string that is a human readable
101      * representation of this method. */
102
103     public String printMethod(Hashtable map) {
104         String st=method+" {\n";
105         HashSet tovisit=new HashSet();
106         HashSet visited=new HashSet();
107         int labelindex=0;
108         Hashtable nodetolabel=new Hashtable();
109         tovisit.add(this);
110         FlatNode current_node=null;
111         //Assign labels 1st
112         //Node needs a label if it is
113         while(!tovisit.isEmpty()) {
114             FlatNode fn=(FlatNode)tovisit.iterator().next();
115             tovisit.remove(fn);
116             visited.add(fn);
117
118             for(int i=0;i<fn.numNext();i++) {
119                 FlatNode nn=fn.getNext(i);
120                 if(i>0) {
121                     //1) Edge >1 of node
122                     nodetolabel.put(nn,new Integer(labelindex++));
123                 }
124                 if (!visited.contains(nn)&&!tovisit.contains(nn)) {
125                     tovisit.add(nn);
126                 } else {
127                     //2) Join point
128                     nodetolabel.put(nn,new Integer(labelindex++));
129                 }
130             }
131         }
132
133         //Do the actual printing
134         tovisit=new HashSet();
135         visited=new HashSet();
136         tovisit.add(this);
137         while(current_node!=null||!tovisit.isEmpty()) {
138             if (current_node==null) {
139                 current_node=(FlatNode)tovisit.iterator().next();
140                 tovisit.remove(current_node);
141             }
142             visited.add(current_node);
143             if (nodetolabel.containsKey(current_node))
144                 st+="L"+nodetolabel.get(current_node)+":\n";
145             if (current_node.numNext()==0) {
146                 if (map==null)
147                     st+="   "+current_node.toString()+"\n";
148                 else
149                     st+="   "+current_node.toString()+"["+map.get(current_node)+"]\n";
150                 current_node=null;
151             } else if(current_node.numNext()==1) {
152                 if (map==null)
153                     st+="   "+current_node.toString()+"\n";
154                 else
155                     st+="   "+current_node.toString()+"["+map.get(current_node)+"]\n";
156                 FlatNode nextnode=current_node.getNext(0);
157                 if (visited.contains(nextnode)) {
158                     st+="goto L"+nodetolabel.get(nextnode)+"\n";
159                     current_node=null;
160                 } else
161                     current_node=nextnode;
162             } else if (current_node.numNext()==2) {
163                 /* Branch */
164                 st+="   "+((FlatCondBranch)current_node).toString("L"+nodetolabel.get(current_node.getNext(1)))+"\n";
165                 if (!visited.contains(current_node.getNext(1)))
166                     tovisit.add(current_node.getNext(1));
167                 if (visited.contains(current_node.getNext(0))) {
168                     st+="goto L"+nodetolabel.get(current_node.getNext(0))+"\n";
169                     current_node=null;
170                 } else
171                     current_node=current_node.getNext(0);
172             } else throw new Error();
173         }
174         return st+"}\n";
175     }
176     
177     public TempDescriptor [] writesTemps() {
178         return (TempDescriptor[]) parameterTemps.toArray(new TempDescriptor[ parameterTemps.size()]);
179     }
180 }