dd460118b2f8ae66f14e7f8b732f0736f242f20e
[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             {
121
122                 final SymbolTable st = rule.getSymbolTable();                
123                 CodeWriter cr = new StandardCodeWriter(output) {
124                         public SymbolTable getSymbolTable() { return st; }
125                     };
126                 
127                 cr.outputline("// build " + rule.getLabel());
128                 cr.startblock();
129
130                 ListIterator quantifiers = rule.quantifiers();
131
132                 while (quantifiers.hasNext()) {
133                     Quantifier quantifier = (Quantifier) quantifiers.next();                   
134                     quantifier.generate_open(cr);
135                 }            
136                         
137                 /* pretty print! */
138                 cr.output("//");
139                 rule.getGuardExpr().prettyPrint(cr);
140                 cr.outputline("");
141
142                 /* now we have to generate the guard test */
143         
144                 VarDescriptor guardval = VarDescriptor.makeNew();
145                 rule.getGuardExpr().generate(cr, guardval);
146                 
147                 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
148                 cr.startblock();
149
150                 /* now we have to generate the inclusion code */
151                 rule.getInclusion().generate(cr);
152                 cr.endblock();
153
154                 // close startblocks generated by DotExpr memory checks
155                 //DotExpr.generate_memory_endblocks(cr);
156
157                 while (quantifiers.hasPrevious()) {
158                     Quantifier quantifier = (Quantifier) quantifiers.previous();
159                     cr.endblock();
160                 }
161
162                 cr.endblock();
163                 cr.outputline("");
164                 cr.outputline("");
165             }
166         }
167     }
168
169     private void generate_implicit_checks() {
170
171         /* do post checks */
172          
173         CodeWriter cr = new StandardCodeWriter(output);
174            
175         // #TBD#: these should be implicit checks added to the set of constraints
176         //output.println("check multiplicity");
177     }
178
179     private void generate_checks() {
180
181         /* do constraint checks */
182         Vector constraints = state.vConstraints;
183
184         for (int i = 0; i < constraints.size(); i++) {
185
186             Constraint constraint = (Constraint) constraints.elementAt(i); 
187
188             {
189
190                 final SymbolTable st = constraint.getSymbolTable();
191                 
192                 CodeWriter cr = new StandardCodeWriter(output) {
193                         public SymbolTable getSymbolTable() { return st; }
194                     };
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 " + (i+1) + ". \");");
217                 cr.endblock();
218
219                 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
220                 cr.startblock();
221
222                 cr.outputline("__Success = 0;");
223                 cr.outputline("printf(\"fail " + (i+1) + ". \");");
224                 cr.endblock();
225
226                 while (quantifiers.hasPrevious()) {
227                     Quantifier quantifier = (Quantifier) quantifiers.previous();
228                     cr.endblock();
229                 }
230
231                 cr.endblock();
232                 cr.outputline("");
233                 cr.outputline("");
234             }
235             
236         }
237
238         output.println("if (__Success) { printf(\"all tests passed\"); }");
239     }    
240
241 }
242
243
244