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