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 /** This method returns a string that is a human readable
94 * representation of this method. */
96 public String printMethod() {
97 String st=method+" {\n";
98 HashSet tovisit=new HashSet();
99 HashSet visited=new HashSet();
101 Hashtable nodetolabel=new Hashtable();
103 FlatNode current_node=null;
105 //Node needs a label if it is
106 while(!tovisit.isEmpty()) {
107 FlatNode fn=(FlatNode)tovisit.iterator().next();
111 // System.out.println("Next : "+fn.numNext());
113 for(int i=0;i<fn.numNext();i++) {
114 FlatNode nn=fn.getNext(i);
117 nodetolabel.put(nn,new Integer(labelindex++));
119 if (!visited.contains(nn)&&!tovisit.contains(nn)) {
123 nodetolabel.put(nn,new Integer(labelindex++));
128 //Do the actual printing
129 tovisit=new HashSet();
130 visited=new HashSet();
132 while(current_node!=null||!tovisit.isEmpty()) {
133 if (current_node==null) {
134 current_node=(FlatNode)tovisit.iterator().next();
135 tovisit.remove(current_node);
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";
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";
150 current_node=nextnode;
151 } else if (current_node.numNext()==2) {
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";
160 current_node=current_node.getNext(0);
161 } else throw new Error();
166 public TempDescriptor [] writesTemps() {
167 return (TempDescriptor[]) parameterTemps.toArray(new TempDescriptor[ parameterTemps.size()]);