023b06491b41337947bb682bc6b2414ecb94fe9b
[repair.git] / Repair / RepairCompiler / MCC / IR / WorkList.java
1 package MCC.IR;
2
3 import MCC.State;
4 import java.util.*;
5
6 public class WorkList {
7
8
9     public static Vector getrulelist(Descriptor d) {
10
11         Vector dispatchrules = new Vector();
12         Vector rules = State.currentState.vRules;        
13         
14         for (int i = 0; i < rules.size(); i++) {
15             Rule rule = (Rule) rules.elementAt(i);
16             Set requiredsymbols = rule.getRequiredDescriptors();
17             
18             // #TBD#: in general this is wrong because these descriptors may contain descriptors
19             // bound in "in?" expressions which need to be dealt with in a topologically sorted
20             // fashion...
21
22             if (rule.getRequiredDescriptors().contains(d)) {
23                 dispatchrules.addElement(rule);
24             }
25         }
26
27         return dispatchrules;
28     }
29     
30
31     public static void generate_dispatch(CodeWriter cr, RelationDescriptor rd, String leftvar, String rightvar) {
32
33         cr.outputline("// RELATION DISPATCH ");        
34
35         Vector dispatchrules = getrulelist(rd);
36         
37         if (dispatchrules.size() == 0) {
38             cr.outputline("// nothing to dispatch");
39             return;
40         }
41        
42         for(int i = 0; i < dispatchrules.size(); i++) {
43             Rule rule = (Rule) dispatchrules.elementAt(i);
44             cr.outputline("need to dispatch for " + rule.getLabel());
45         }
46
47         assert false; // unsupported
48
49     }
50
51
52     public static void generate_dispatch(CodeWriter cr, SetDescriptor sd, String setvar) {
53                
54         cr.outputline("// SET DISPATCH ");        
55  
56         Vector dispatchrules = getrulelist(sd);
57
58         if (dispatchrules.size() == 0) {
59             cr.outputline("// nothing to dispatch");
60             return;
61         }
62
63         for(int i = 0; i < dispatchrules.size(); i++) {
64             Rule rule = (Rule) dispatchrules.elementAt(i);
65             
66             ListIterator quantifiers = rule.quantifiers();
67             Vector otherq = new Vector(); // quantifiers that we need to iterate over to add workitems
68
69             cr.outputline("// " + rule.getLabel());
70             cr.startblock();
71                           
72
73             // #ATTN#: this may/does not handle multiple instances of the same quantifier being bound
74             // solution is probabyl to iterate over all bindings
75
76             // find quantifier that we have bound
77             String boundname = null;
78             int size = 4; // starts at 4 because we have to store the ID
79             while (quantifiers.hasNext()) {
80                 Quantifier quantifier = (Quantifier) quantifiers.next();
81                 if (quantifier instanceof SetQuantifier) {
82                     size += 4;
83                     SetQuantifier sq = (SetQuantifier) quantifier;
84                     if (sq.getSet() == sd) {
85                         // we have found our quantifier
86                         boundname = sq.getVar().getSafeSymbol();
87
88                         break;
89                     }
90                 } else if (quantifier instanceof RelationQuantifier) {
91                     size += 8;
92                 } else { // ForQuantifier
93                     size += 4;
94                 }                    
95
96                 otherq.addElement(quantifier);        
97             }
98
99             assert boundname != null;
100
101             // bind bound variable
102             cr.outputline("int " + boundname + " = " + setvar + ";");
103             
104             // add the rest of the quantifiers and continue to calculate size
105             while (quantifiers.hasNext()) {
106                 Quantifier quantifier = (Quantifier) quantifiers.next();
107                 if (quantifier instanceof RelationQuantifier) {
108                     size += 8;
109                 } else {
110                     size += 4;
111                 }
112             }
113             
114             ListIterator otheriterator = otherq.listIterator();
115             while (otheriterator.hasNext()) {
116                 Quantifier quantifier = (Quantifier) otheriterator.next();                   
117                 quantifier.generate_open(cr);
118                 // implicitly opens bracket
119             }            
120                         
121             cr.outputline("// dispatching to " + rule.getLabel());
122             // #TODO#:  add code to do worklist addition
123
124             cr.outputline("WORKITEM *wi = (WORKITEM *) malloc(" + size + ");");
125             cr.outputline("wi->id = " + rule.getNum() + ";");
126
127             // reset quantifiers
128             quantifiers = rule.quantifiers();
129
130             // list quantifier so the order's match!
131             int offset = 0;
132             while (quantifiers.hasNext()) {
133                 Quantifier quantifier = (Quantifier) quantifiers.next();
134                 offset = quantifier.generate_workliststore(cr, offset);
135             }            
136              
137             // now store in worklist!
138             cr.outputline("WORKLIST->add((int) wi);");            
139            
140             // close all those brackets
141             while (otheriterator.hasPrevious()) {
142                 otheriterator.previous(); // throw away
143                 cr.endblock();
144             }           
145
146             // end rule
147             cr.endblock();
148
149         }    
150     }
151
152 }