7 public class RepairGenerator {
10 java.io.PrintWriter outputrepair = null;
11 java.io.PrintWriter outputaux = null;
12 java.io.PrintWriter outputhead = null;
15 public RepairGenerator(State state) {
19 public void generate(OutputStream outputrepair, OutputStream outputaux,OutputStream outputhead, String st) {
20 this.outputrepair = new java.io.PrintWriter(outputrepair, true);
21 this.outputaux = new java.io.PrintWriter(outputaux, true);
22 this.outputhead = new java.io.PrintWriter(outputhead, true);
25 generate_tokentable();
26 generate_hashtables();
27 generate_stateobject();
29 /* generate_worklist();
32 generate_teardown();*/
36 private void generate_call() {
37 CodeWriter cr = new StandardCodeWriter(outputrepair);
38 VarDescriptor vdstate=VarDescriptor.makeNew("repairstate");
39 cr.outputline(name+"_state * "+vdstate.getSafeSymbol()+"=new "+name+"_state();");
42 Iterator globals=state.stGlobals.descriptors();
43 while (globals.hasNext()) {
44 VarDescriptor vd=(VarDescriptor) globals.next();
45 cr.outputline(vdstate.getSafeSymbol()+"->"+vd.getSafeSymbol()+"=("+vd.getType().getGenerateType().getSafeSymbol()+")"+vd.getSafeSymbol()+";");
47 /* Insert repair here */
49 globals=state.stGlobals.descriptors();
50 while (globals.hasNext()) {
51 VarDescriptor vd=(VarDescriptor) globals.next();
52 cr.outputline("*(("+vd.getType().getGenerateType().getSafeSymbol()+"*) &"+vd.getSafeSymbol()+")="+vdstate.getSafeSymbol()+"->"+vd.getSafeSymbol()+";");
55 cr.outputline("delete ("+vdstate.getSafeSymbol()+");");
57 private void generate_tokentable() {
59 CodeWriter cr = new StandardCodeWriter(outputrepair);
60 Iterator tokens = TokenLiteralExpr.tokens.keySet().iterator();
63 cr.outputline("// Token values");
66 while (tokens.hasNext()) {
67 Object token = tokens.next();
68 cr.outputline("// " + token.toString() + " = " + TokenLiteralExpr.tokens.get(token).toString());
75 private void generate_stateobject() {
76 CodeWriter crhead = new StandardCodeWriter(outputhead);
77 crhead.outputline("class "+name+"_state {");
78 crhead.outputline("public:");
79 Iterator globals=state.stGlobals.descriptors();
80 while (globals.hasNext()) {
81 VarDescriptor vd=(VarDescriptor) globals.next();
82 crhead.outputline(vd.getType().getGenerateType().getSafeSymbol()+" "+vd.getSafeSymbol()+";");
86 private void generate_hashtables() {
87 CodeWriter craux = new StandardCodeWriter(outputaux);
88 CodeWriter crhead = new StandardCodeWriter(outputhead);
89 crhead.outputline("#include \"SimpleHash.h\"");
90 crhead.outputline("class "+name+" {");
91 crhead.outputline("public:");
92 crhead.outputline(name+"();");
93 crhead.outputline("~"+name+"();");
94 craux.outputline("#include \""+headername+"\"");
96 craux.outputline(name+"::"+name+"() {");
97 craux.outputline("// creating hashtables ");
100 Iterator sets = state.stSets.descriptors();
102 /* first pass create all the hash tables */
103 while (sets.hasNext()) {
104 SetDescriptor set = (SetDescriptor) sets.next();
105 crhead.outputline("SimpleHash* " + set.getSafeSymbol() + "_hash;");
106 craux.outputline(set.getSafeSymbol() + "_hash = new SimpleHash();");
109 /* second pass build relationships between hashtables */
110 sets = state.stSets.descriptors();
112 while (sets.hasNext()) {
113 SetDescriptor set = (SetDescriptor) sets.next();
114 Iterator subsets = set.subsets();
116 while (subsets.hasNext()) {
117 SetDescriptor subset = (SetDescriptor) subsets.next();
118 craux.outputline(subset.getSafeSymbol() + "_hash->addParent(" + set.getSafeSymbol() + "_hash);");
122 /* build relations */
123 Iterator relations = state.stRelations.descriptors();
125 /* first pass create all the hash tables */
126 while (relations.hasNext()) {
127 RelationDescriptor relation = (RelationDescriptor) relations.next();
129 if (relation.testUsage(RelationDescriptor.IMAGE)) {
130 crhead.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hash;");
131 craux.outputline(relation.getSafeSymbol() + "_hash = new SimpleHash();");
134 if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
135 crhead.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hashinv;");
136 craux.outputline(relation.getSafeSymbol() + "_hashinv = new SimpleHash();");
140 craux.outputline("}");
141 crhead.outputline("};");
142 craux.outputline(name+"::~"+name+"() {");
143 craux.outputline("// deleting hashtables");
145 /* build destructor */
146 sets = state.stSets.descriptors();
148 /* first pass create all the hash tables */
149 while (sets.hasNext()) {
150 SetDescriptor set = (SetDescriptor) sets.next();
151 craux.outputline("delete("+set.getSafeSymbol() + "_hash);");
154 /* destroy relations */
155 relations = state.stRelations.descriptors();
157 /* first pass create all the hash tables */
158 while (relations.hasNext()) {
159 RelationDescriptor relation = (RelationDescriptor) relations.next();
161 if (relation.testUsage(RelationDescriptor.IMAGE)) {
162 craux.outputline("delete("+relation.getSafeSymbol() + "_hash);");
165 if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
166 craux.outputline("delete(" + relation.getSafeSymbol() + ");");
169 craux.outputline("}");
172 private void generate_worklist() {
173 CodeWriter cr = new StandardCodeWriter(outputrepair);
174 cr.outputline("WORKLIST = new WorkList();");
177 private void generate_teardown() {
178 CodeWriter cr = new StandardCodeWriter(outputrepair);
179 cr.outputline("delete WORKLIST;");
182 private void generate_rules() {
184 /* first we must sort the rules */
185 Iterator allrules = state.vRules.iterator();
187 Vector emptyrules = new Vector(); // rules with no quantifiers
188 Vector worklistrules = new Vector(); // the rest of the rules
190 while (allrules.hasNext()) {
191 Rule rule = (Rule) allrules.next();
192 ListIterator quantifiers = rule.quantifiers();
194 boolean noquantifiers = true;
195 while (quantifiers.hasNext()) {
196 Quantifier quantifier = (Quantifier) quantifiers.next();
197 if (quantifier instanceof ForQuantifier) {
198 // ok, because integers exist already!
201 noquantifiers = false;
207 emptyrules.add(rule);
209 worklistrules.add(rule);
213 Iterator iterator_er = emptyrules.iterator();
214 while (iterator_er.hasNext()) {
216 Rule rule = (Rule) iterator_er.next();
219 final SymbolTable st = rule.getSymbolTable();
220 CodeWriter cr = new StandardCodeWriter(outputrepair) {
221 public SymbolTable getSymbolTable() { return st; }
224 cr.outputline("// build " + rule.getLabel());
227 ListIterator quantifiers = rule.quantifiers();
229 while (quantifiers.hasNext()) {
230 Quantifier quantifier = (Quantifier) quantifiers.next();
231 quantifier.generate_open(cr);
236 rule.getGuardExpr().prettyPrint(cr);
239 /* now we have to generate the guard test */
241 VarDescriptor guardval = VarDescriptor.makeNew();
242 rule.getGuardExpr().generate(cr, guardval);
244 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
247 /* now we have to generate the inclusion code */
248 rule.getInclusion().generate(cr);
251 while (quantifiers.hasPrevious()) {
252 Quantifier quantifier = (Quantifier) quantifiers.previous();
262 CodeWriter cr2 = new StandardCodeWriter(outputrepair);
264 cr2.outputline("WORKLIST->reset();");
265 cr2.outputline("while (WORKLIST->hasMoreElements())");
267 cr2.outputline("WORKITEM *wi = (WORKITEM *) WORKLIST->nextElement();");
269 String elseladder = "if";
271 Iterator iterator_rules = worklistrules.iterator();
272 while (iterator_rules.hasNext()) {
274 Rule rule = (Rule) iterator_rules.next();
275 int dispatchid = rule.getNum();
278 final SymbolTable st = rule.getSymbolTable();
279 CodeWriter cr = new StandardCodeWriter(outputrepair) {
280 public SymbolTable getSymbolTable() { return st; }
284 cr.outputline(elseladder + " (wi->id == " + dispatchid + ")");
287 cr.outputline("// build " + rule.getLabel());
289 ListIterator quantifiers = rule.quantifiers();
292 while (quantifiers.hasNext()) {
293 Quantifier quantifier = (Quantifier) quantifiers.next();
294 count = quantifier.generate_worklistload(cr, count );
299 rule.getGuardExpr().prettyPrint(cr);
302 /* now we have to generate the guard test */
304 VarDescriptor guardval = VarDescriptor.makeNew();
305 rule.getGuardExpr().generate(cr, guardval);
307 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
310 /* now we have to generate the inclusion code */
311 rule.getInclusion().generate(cr);
314 // close startblocks generated by DotExpr memory checks
315 //DotExpr.generate_memory_endblocks(cr);
317 cr.endblock(); // end else-if WORKLIST ladder
319 elseladder = "else if";
323 cr2.outputline("else");
325 cr2.outputline("printf(\"VERY BAD !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\\n\\n\");");
326 cr2.outputline("exit(1);");
329 // end block created for worklist
334 private void generate_checks() {
336 /* do constraint checks */
337 Vector constraints = state.vConstraints;
339 for (int i = 0; i < constraints.size(); i++) {
341 Constraint constraint = (Constraint) constraints.elementAt(i);
345 final SymbolTable st = constraint.getSymbolTable();
347 CodeWriter cr = new StandardCodeWriter(outputrepair) {
348 public SymbolTable getSymbolTable() { return st; }
351 cr.outputline("// checking " + constraint.getLabel());
354 ListIterator quantifiers = constraint.quantifiers();
356 while (quantifiers.hasNext()) {
357 Quantifier quantifier = (Quantifier) quantifiers.next();
358 quantifier.generate_open(cr);
361 cr.outputline("int maybe = 0;");
363 /* now we have to generate the guard test */
365 VarDescriptor constraintboolean = VarDescriptor.makeNew("constraintboolean");
366 constraint.getLogicStatement().generate(cr, constraintboolean);
368 cr.outputline("if (maybe)");
370 cr.outputline("__Success = 0;");
371 cr.outputline("printf(\"maybe fail " + (i+1) + ". \");");
372 cr.outputline("exit(1);");
375 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
378 cr.outputline("__Success = 0;");
379 cr.outputline("printf(\"fail " + (i+1) + ". \");");
380 cr.outputline("exit(1);");
383 while (quantifiers.hasPrevious()) {
384 Quantifier quantifier = (Quantifier) quantifiers.previous();
395 outputrepair.println("// if (__Success) { printf(\"all tests passed\"); }");