start of new file
[IRC.git] / Robust / src / Analysis / TaskStateAnalysis / TaskQueueIterator.java
1 package Analysis.TaskStateAnalysis;
2 import IR.*;
3 import IR.Tree.*;
4 import IR.Flat.*;
5 import java.util.*;
6
7 public class TaskQueueIterator {
8     TaskQueue tq;
9     int index;
10     FlagTagState fts;
11     FlagTagState ftsarray[];
12     Hashtable<TempDescriptor, TagState> tsarray;
13     Hashtable<TempDescriptor, Integer> tsindexarray;
14     Iterator<FlagTagState> itarray[];
15     boolean needit;
16     boolean needinit;
17
18     public TaskQueueIterator(TaskQueue tq, int index, FlagTagState fts) {
19         this.tq=tq;
20         this.index=index;
21         this.fts=fts;
22         this.ftsarray=new FlagTagState[tq.numParameters()];
23         this.ftsarray[index]=fts;
24         this.tsarray=new Hashtable<TempDescriptor, TagState>();
25         this.tsindexarray=new Hashtable<TempDescriptor, Integer>();
26         this.itarray=(Iterator<FlagTagState>[]) new Iterator[tq.numParameters()];
27         needit=false;
28         needinit=true;
29         init();
30     }
31     
32     private void init() {
33         for(int i=ftsarray.length-1;i>=0;i--) {
34             if (i!=index)
35                 itarray[i]=tq.parameterset[i].iterator();
36             VarDescriptor vd=tq.task.getParameter(i);
37             TagExpressionList tel=tq.task.getTag(vd);
38             if (tel!=null)
39                 for(int j=0;j<tel.numTags();j++) {
40                     TempDescriptor tmp=tel.getTemp(j);
41                     if (!tsindexarray.containsKey(tmp)) {
42                         tsindexarray.put(tmp, new Integer(i));
43                     }
44                 }
45         }
46     }
47
48     public boolean hasNext() {
49         TaskDescriptor td=tq.task;
50         int i;
51         if (needinit) {
52             i=ftsarray.length-1;
53             if (i!=index)
54                 ftsarray[i]=itarray[i].next();
55         } else {
56             i=0;
57         }
58
59         if (i==0&&index==0&&ftsarray[0]!=null&&!needit) {
60             needinit=false;
61             return true;
62         }
63
64         objloop:
65         for(;i<ftsarray.length;i++) {
66             FlagState currfs=ftsarray[i].fs;
67             VarDescriptor vd=td.getParameter(i);
68             TagExpressionList tel=td.getTag(vd);
69             int j;
70             if (needinit) {
71                 j=(tel!=null)&&tel.numTags()>0?tel.numTags()-1:0;
72                 needinit=false;
73             } else
74                 j=0;
75             tagloop:
76             for(;tel!=null&&j<tel.numTags();j++) {
77                 TempDescriptor tmp=tel.getTemp(j);
78                 TagState currtag=tsarray.get(tmp);
79                 String type=tel.getType(j);
80                 
81                 if (tsindexarray.get(tmp).intValue()==i) {
82                     //doing the assignment right here!!!
83                     Vector<FlagTagState> possts=tq.map.get(currfs);
84                     int index=0;
85                     if (currtag!=null)
86                         index=possts.indexOf(new FlagTagState(currtag,currfs));
87                     if (needit) {
88                         index++;
89                         needit=false;
90                     }
91                     for(int k=index;k<possts.size();k++) {
92                         FlagTagState posstag=possts.get(k);
93                         if (posstag.ts.getTag().getSymbol().equals(type)) {
94                             tsarray.put(tmp, posstag.ts);
95                             if (j==0) {
96                                 if (i==0) {
97                                     //We are done!
98                                     return true;
99                                 } else {
100                                     //Backtrack on objects
101                                     i-=2;
102                                     continue objloop;
103                                 }
104                             } else {
105                                 //Backtrack on tags
106                                 j-=2;
107                                 continue tagloop;
108                             }
109                         }
110                     }
111                     //couldn't find a tag
112                     tsarray.put(tmp, null);
113                     needit=true;
114                 } else {
115                     //check tag compatibility
116                     if (!currtag.containsFS(currfs)) {
117                         //incompatible tag set by previous level
118                         //need to increment object state
119                         needit=true;
120                         break;
121                     }
122                 }
123             }
124             if (index==i) {
125                 continue;
126             }
127             if (needit) {
128                 if (itarray[i].hasNext()) {
129                     ftsarray[i]=itarray[i].next();
130                     needit=false;
131                     i-=1;
132                     continue objloop; //backtrack and fix up everything
133                 } else {
134                     itarray[i]=tq.parameterset[i].iterator();//keep going backwards
135                 }
136             } else {
137                 throw new Error();
138             }
139         }
140         return false;
141     }
142
143     public FlagTagState getFTS(int index) {
144         return ftsarray[index];
145     }
146
147     public TagState getTS(TempDescriptor tmp) {
148         return tsarray.get(tmp);
149     }
150     
151     public void next() {
152         needit=true;
153     }
154 }