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 return method.toString();
33 public MethodDescriptor getMethod() {
37 public TaskDescriptor getTask() {
42 return FKind.FlatMethod;
45 public void addParameterTemp(TempDescriptor t) {
46 parameterTemps.add(t);
49 public int numParameters() {
50 return parameterTemps.size();
53 public void addTagTemp(TempDescriptor t) {
54 tagtointmap.put(t, new Integer(tagTemps.size()));
58 public int getTagInt(TempDescriptor t) {
59 return ((Integer)tagtointmap.get(t)).intValue();
62 public int numTags() {
63 return tagTemps.size();
66 public TempDescriptor getTag(int i) {
67 return (TempDescriptor) tagTemps.get(i);
70 public TempDescriptor getParameter(int i) {
71 return (TempDescriptor) parameterTemps.get(i);
74 /** This method returns a set of the nodes in this flat representation */
76 public Set<FlatNode> getNodeSet() {
77 HashSet<FlatNode> tovisit=new HashSet<FlatNode>();
78 HashSet<FlatNode> visited=new HashSet<FlatNode>();
80 while(!tovisit.isEmpty()) {
81 FlatNode fn=tovisit.iterator().next();
84 for(int i=0;i<fn.numNext();i++) {
85 FlatNode nn=fn.getNext(i);
86 if (!visited.contains(nn))
93 public String printMethod() {
94 return printMethod(null);
97 /** This method returns a string that is a human readable
98 * representation of this method. */
100 public String printMethod(Hashtable map) {
101 String st=method+" {\n";
102 HashSet tovisit=new HashSet();
103 HashSet visited=new HashSet();
105 Hashtable nodetolabel=new Hashtable();
107 FlatNode current_node=null;
109 //Node needs a label if it is
110 while(!tovisit.isEmpty()) {
111 FlatNode fn=(FlatNode)tovisit.iterator().next();
115 for(int i=0;i<fn.numNext();i++) {
116 FlatNode nn=fn.getNext(i);
119 nodetolabel.put(nn,new Integer(labelindex++));
121 if (!visited.contains(nn)&&!tovisit.contains(nn)) {
125 nodetolabel.put(nn,new Integer(labelindex++));
130 //Do the actual printing
131 tovisit=new HashSet();
132 visited=new HashSet();
134 while(current_node!=null||!tovisit.isEmpty()) {
135 if (current_node==null) {
136 current_node=(FlatNode)tovisit.iterator().next();
137 tovisit.remove(current_node);
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) {
144 st+=" "+current_node.toString()+"\n";
146 st+=" "+current_node.toString()+"["+map.get(current_node)+"]\n";
148 } else if(current_node.numNext()==1) {
150 st+=" "+current_node.toString()+"\n";
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";
158 current_node=nextnode;
159 } else if (current_node.numNext()==2) {
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";
168 current_node=current_node.getNext(0);
169 } else throw new Error();
174 public TempDescriptor [] writesTemps() {
175 return (TempDescriptor[]) parameterTemps.toArray(new TempDescriptor[ parameterTemps.size()]);