2 import IR.MethodDescriptor;
3 import IR.TaskDescriptor;
6 public class FlatMethod extends FlatNode {
7 MethodDescriptor method;
11 Hashtable tagtointmap;
13 FlatMethod(MethodDescriptor md) {
16 parameterTemps=new Vector();
17 tagTemps=new Vector();
18 tagtointmap=new Hashtable();
21 FlatMethod(TaskDescriptor td) {
24 parameterTemps=new Vector();
25 tagTemps=new Vector();
26 tagtointmap=new Hashtable();
29 public String toString() {
30 String ret = "FlatMethod_";
31 if( method != null ) {
32 ret += method.toString();
34 ret += task.toString();
39 public MethodDescriptor getMethod() {
43 public TaskDescriptor getTask() {
48 return FKind.FlatMethod;
51 public void addParameterTemp(TempDescriptor t) {
52 parameterTemps.add(t);
55 public int numParameters() {
56 return parameterTemps.size();
59 public void addTagTemp(TempDescriptor t) {
60 tagtointmap.put(t, new Integer(tagTemps.size()));
64 public int getTagInt(TempDescriptor t) {
65 return ((Integer)tagtointmap.get(t)).intValue();
68 public int numTags() {
69 return tagTemps.size();
72 public TempDescriptor getTag(int i) {
73 return (TempDescriptor) tagTemps.get(i);
76 public TempDescriptor getParameter(int i) {
77 return (TempDescriptor) parameterTemps.get(i);
80 /** This method returns a set of the nodes in this flat representation */
82 public Set<FlatNode> getNodeSet() {
83 HashSet<FlatNode> tovisit=new HashSet<FlatNode>();
84 HashSet<FlatNode> visited=new HashSet<FlatNode>();
86 while(!tovisit.isEmpty()) {
87 FlatNode fn=tovisit.iterator().next();
90 for(int i=0; i<fn.numNext(); i++) {
91 FlatNode nn=fn.getNext(i);
92 if (!visited.contains(nn))
99 public String printMethod() {
100 return printMethod(null);
103 /** This method returns a string that is a human readable
104 * representation of this method. */
106 public String printMethod(Hashtable map) {
107 String st=method+" {\n";
108 HashSet tovisit=new HashSet();
109 HashSet visited=new HashSet();
111 Hashtable nodetolabel=new Hashtable();
113 FlatNode current_node=null;
115 //Node needs a label if it is
116 while(!tovisit.isEmpty()) {
117 FlatNode fn=(FlatNode)tovisit.iterator().next();
121 for(int i=0; i<fn.numNext(); i++) {
122 FlatNode nn=fn.getNext(i);
125 nodetolabel.put(nn,new Integer(labelindex++));
127 if (!visited.contains(nn)&&!tovisit.contains(nn)) {
131 nodetolabel.put(nn,new Integer(labelindex++));
136 //Do the actual printing
137 tovisit=new HashSet();
138 visited=new HashSet();
140 while(current_node!=null||!tovisit.isEmpty()) {
141 if (current_node==null) {
142 current_node=(FlatNode)tovisit.iterator().next();
143 tovisit.remove(current_node);
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) {
150 st+=" "+current_node.toString()+"\n";
152 st+=" "+current_node.toString()+"["+map.get(current_node)+"]\n";
154 } else if(current_node.numNext()==1) {
156 st+=" "+current_node.toString()+"\n";
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";
164 current_node=nextnode;
165 } else if (current_node.numNext()==2) {
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";
174 current_node=current_node.getNext(0);
175 } else throw new Error();
180 public TempDescriptor [] writesTemps() {
181 return (TempDescriptor[])parameterTemps.toArray(new TempDescriptor[ parameterTemps.size()]);