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