This commit was manufactured by cvs2svn to create tag 'buildscript'.
[IRC.git] /
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     public String printMethod() {
94         return printMethod(null);
95     }
96
97     /** This method returns a string that is a human readable
98      * representation of this method. */
99
100     public String printMethod(Hashtable map) {
101         String st=method+" {\n";
102         HashSet tovisit=new HashSet();
103         HashSet visited=new HashSet();
104         int labelindex=0;
105         Hashtable nodetolabel=new Hashtable();
106         tovisit.add(this);
107         FlatNode current_node=null;
108         //Assign labels 1st
109         //Node needs a label if it is
110         while(!tovisit.isEmpty()) {
111             FlatNode fn=(FlatNode)tovisit.iterator().next();
112             tovisit.remove(fn);
113             visited.add(fn);
114
115             for(int i=0;i<fn.numNext();i++) {
116                 FlatNode nn=fn.getNext(i);
117                 if(i>0) {
118                     //1) Edge >1 of node
119                     nodetolabel.put(nn,new Integer(labelindex++));
120                 }
121                 if (!visited.contains(nn)&&!tovisit.contains(nn)) {
122                     tovisit.add(nn);
123                 } else {
124                     //2) Join point
125                     nodetolabel.put(nn,new Integer(labelindex++));
126                 }
127             }
128         }
129
130         //Do the actual printing
131         tovisit=new HashSet();
132         visited=new HashSet();
133         tovisit.add(this);
134         while(current_node!=null||!tovisit.isEmpty()) {
135             if (current_node==null) {
136                 current_node=(FlatNode)tovisit.iterator().next();
137                 tovisit.remove(current_node);
138             }
139             visited.add(current_node);
140             if (nodetolabel.containsKey(current_node))
141                 st+="L"+nodetolabel.get(current_node)+":\n";
142             if (current_node.numNext()==0) {
143                 if (map==null)
144                     st+="   "+current_node.toString()+"\n";
145                 else
146                     st+="   "+current_node.toString()+"["+map.get(current_node)+"]\n";
147                 current_node=null;
148             } else if(current_node.numNext()==1) {
149                 if (map==null)
150                     st+="   "+current_node.toString()+"\n";
151                 else
152                     st+="   "+current_node.toString()+"["+map.get(current_node)+"]\n";
153                 FlatNode nextnode=current_node.getNext(0);
154                 if (visited.contains(nextnode)) {
155                     st+="goto L"+nodetolabel.get(nextnode)+"\n";
156                     current_node=null;
157                 } else
158                     current_node=nextnode;
159             } else if (current_node.numNext()==2) {
160                 /* Branch */
161                 st+="   "+((FlatCondBranch)current_node).toString("L"+nodetolabel.get(current_node.getNext(1)))+"\n";
162                 if (!visited.contains(current_node.getNext(1)))
163                     tovisit.add(current_node.getNext(1));
164                 if (visited.contains(current_node.getNext(0))) {
165                     st+="goto L"+nodetolabel.get(current_node.getNext(0))+"\n";
166                     current_node=null;
167                 } else
168                     current_node=current_node.getNext(0);
169             } else throw new Error();
170         }
171         return st+"}\n";
172     }
173     
174     public TempDescriptor [] writesTemps() {
175         return (TempDescriptor[]) parameterTemps.toArray(new TempDescriptor[ parameterTemps.size()]);
176     }
177 }