Added:
[repair.git] / Repair / RepairCompiler / MCC / IR / NaiveGenerator.java
1 package MCC.IR;
2
3 import java.io.*;
4 import java.util.*;
5 import MCC.State;
6
7 public class NaiveGenerator {
8
9     State state;
10     java.io.PrintWriter output = null;
11             
12     public NaiveGenerator(State state) {
13         this.state = state;
14     }
15
16     public void generate(java.io.OutputStream output) {
17         this.output = new java.io.PrintWriter(output, true); 
18         
19         generate_tokentable();
20         generate_hashtables();
21         generate_rules();
22         generate_implicit_checks();
23         generate_checks();
24
25     }
26
27     private void generate_tokentable() {
28
29         CodeWriter cr = new StandardCodeWriter(output);        
30         Iterator tokens = TokenLiteralExpr.tokens.keySet().iterator();        
31
32         cr.outputline("");
33         cr.outputline("// Token values");
34         cr.outputline("");
35
36         while (tokens.hasNext()) {
37             Object token = tokens.next();
38             cr.outputline("// " + token.toString() + " = " + TokenLiteralExpr.tokens.get(token).toString());            
39         }
40
41         cr.outputline("");
42         cr.outputline("");
43     }
44
45     private void generate_hashtables() {
46
47         CodeWriter cr = new StandardCodeWriter(output);
48         cr.outputline("int __Success = 1;\n");       
49         cr.outputline("// creating hashtables ");
50         
51         /* build all the hashtables */
52         Hashtable hashtables = new Hashtable();
53
54         /* build sets */
55         Iterator sets = state.stSets.descriptors();
56         
57         /* first pass create all the hash tables */
58         while (sets.hasNext()) {
59             SetDescriptor set = (SetDescriptor) sets.next();
60             cr.outputline("SimpleHash* " + set.getSafeSymbol() + "_hash = new SimpleHash();");
61         } 
62         
63         /* second pass build relationships between hashtables */
64         sets = state.stSets.descriptors();
65         
66         while (sets.hasNext()) {
67             SetDescriptor set = (SetDescriptor) sets.next();
68             Iterator subsets = set.subsets();
69             
70             while (subsets.hasNext()) {
71                 SetDescriptor subset = (SetDescriptor) subsets.next();                
72                 cr.outputline(subset.getSafeSymbol() + "_hash->addParent(" + set.getSafeSymbol() + "_hash);");
73             }
74         } 
75
76         /* build relations */
77         Iterator relations = state.stRelations.descriptors();
78         
79         /* first pass create all the hash tables */
80         while (relations.hasNext()) {
81             RelationDescriptor relation = (RelationDescriptor) relations.next();
82             
83             if (relation.testUsage(RelationDescriptor.IMAGE)) {
84                 cr.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hash = new SimpleHash();");
85             }
86
87             if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
88                 cr.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hashinv = new SimpleHash();");
89             } 
90         }
91
92         cr.outputline("");
93         cr.outputline("");
94     }
95
96     private void generate_rules() {
97
98         /* first we must sort the rules */
99         GraphNode.DFS.depthFirstSearch(state.rulenodes.values());
100
101         TreeSet topologicalsort = new TreeSet(new Comparator() {
102                 public boolean equals(Object obj) { return false; }
103                 public int compare(Object o1, Object o2) {
104                     GraphNode g1 = (GraphNode) o1;
105                     GraphNode g2 = (GraphNode) o2;
106                     return g2.getFinishingTime() - g1.getFinishingTime();
107                 }
108             });
109         
110         topologicalsort.addAll(state.rulenodes.values());        
111
112         /* build all the rules */
113         Iterator rules = topologicalsort.iterator();
114                 
115         while (rules.hasNext()) {
116
117             GraphNode rulenode = (GraphNode) rules.next();
118             Rule rule = (Rule) rulenode.getOwner();            
119
120             if (!state.vRules.contains(rule)) {
121                 // this is no longer a top-level rule
122                 continue;
123             }
124
125             {
126
127                 CodeWriter cr = new StandardCodeWriter(output);
128                 cr.pushSymbolTable(rule.getSymbolTable()); 
129                 
130                 cr.outputline("// build " + rule.getLabel());
131                 cr.startblock();
132
133                 ListIterator quantifiers = rule.quantifiers();
134
135                 while (quantifiers.hasNext()) {
136                     Quantifier quantifier = (Quantifier) quantifiers.next();                   
137                     quantifier.generate_open(cr);
138                 }            
139                         
140                 /* pretty print! */
141                 cr.output("//");
142                 rule.getGuardExpr().prettyPrint(cr);
143                 cr.outputline("");
144
145                 /* now we have to generate the guard test */
146         
147                 VarDescriptor guardval = VarDescriptor.makeNew();
148                 rule.getGuardExpr().generate(cr, guardval);
149                 
150                 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
151                 cr.startblock();
152
153                 /* now we have to generate the inclusion code */
154                 rule.getInclusion().generate(cr);
155                 cr.endblock();
156
157                 // close startblocks generated by DotExpr memory checks
158                 //DotExpr.generate_memory_endblocks(cr);
159
160                 while (quantifiers.hasPrevious()) {
161                     Quantifier quantifier = (Quantifier) quantifiers.previous();
162                     cr.endblock();
163                 }
164
165                 cr.endblock();
166                 cr.outputline("");
167                 cr.outputline("");
168             }
169         }
170     }
171
172     private void generate_implicit_checks() {
173
174         /* do post checks */
175          
176         CodeWriter cr = new StandardCodeWriter(output);
177            
178         // #TBD#: these should be implicit checks added to the set of constraints
179         //output.println("check multiplicity");
180     }
181
182     private void generate_checks() {
183
184         /* do constraint checks */
185         Vector constraints = state.vConstraints;
186
187         for (int i = 0; i < constraints.size(); i++) {
188
189             Constraint constraint = (Constraint) constraints.elementAt(i); 
190
191             {
192
193                 CodeWriter cr = new StandardCodeWriter(output);
194                 cr.pushSymbolTable(constraint.getSymbolTable());
195                 
196                 cr.outputline("// checking " + constraint.getLabel());
197                 cr.startblock();
198
199                 ListIterator quantifiers = constraint.quantifiers();
200
201                 while (quantifiers.hasNext()) {
202                     Quantifier quantifier = (Quantifier) quantifiers.next();                   
203                     quantifier.generate_open(cr);
204                 }            
205
206                 cr.outputline("int maybe = 0;");
207                         
208                 /* now we have to generate the guard test */
209         
210                 VarDescriptor constraintboolean = VarDescriptor.makeNew("constraintboolean");
211                 constraint.getLogicStatement().generate(cr, constraintboolean);
212                 
213                 cr.outputline("if (maybe)");
214                 cr.startblock();
215                 cr.outputline("__Success = 0;");
216                 cr.outputline("printf(\"maybe fail " + constraint.getNum() + ". \");");
217                 cr.outputline("exit(1);");
218                 cr.endblock();
219
220                 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
221                 cr.startblock();
222
223                 cr.outputline("__Success = 0;");
224                 cr.outputline("printf(\"fail " + constraint.getNum() + ". \");");
225                 cr.outputline("exit(1);");
226                 cr.endblock();
227
228                 while (quantifiers.hasPrevious()) {
229                     Quantifier quantifier = (Quantifier) quantifiers.previous();
230                     cr.endblock();
231                 }
232
233                 cr.endblock();
234                 cr.outputline("");
235                 cr.outputline("");
236             }
237             
238         }
239
240         output.println("//if (__Success) { printf(\"all tests passed\"); }");
241     }    
242
243 }
244
245
246