8 public class RepairGenerator {
10 java.io.PrintWriter outputrepair = null;
11 java.io.PrintWriter outputaux = null;
12 java.io.PrintWriter outputhead = null;
15 static VarDescriptor oldmodel=null;
16 static VarDescriptor newmodel=null;
17 static VarDescriptor worklist=null;
18 static VarDescriptor repairtable=null;
19 static VarDescriptor goodflag=null;
20 Rule currentrule=null;
21 Hashtable updatenames;
23 Termination termination;
26 static boolean DEBUG=false;
28 ModelRuleDependence mrd;
30 public RepairGenerator(State state, Termination t) {
32 updatenames=new Hashtable();
33 usedupdates=new HashSet();
36 togenerate=new HashSet();
37 togenerate.addAll(termination.conjunctions);
39 togenerate.removeAll(removed);
40 GraphNode.computeclosure(togenerate,removed);
42 mrd=ModelRuleDependence.doAnalysis(state);
43 Repair.repairgenerator=this;
46 private void generatetypechecks(boolean flag) {
48 DotExpr.DOTYPECHECKS=true;
49 VarExpr.DOTYPECHECKS=true;
53 VarExpr.DOTYPECHECKS=false;
54 DotExpr.DOTYPECHECKS=false;
61 private void name_updates() {
63 for(Iterator it=termination.updatenodes.iterator();it.hasNext();) {
64 GraphNode gn=(GraphNode) it.next();
65 TermNode tn=(TermNode) gn.getOwner();
66 MultUpdateNode mun=tn.getUpdate();
67 if (togenerate.contains(gn))
68 for (int i=0;i<mun.numUpdates();i++) {
69 UpdateNode un=mun.getUpdate(i);
70 String name="update"+String.valueOf(count++);
71 updatenames.put(un,name);
76 public void generate(OutputStream outputrepair, OutputStream outputaux,OutputStream outputhead, String st) {
77 this.outputrepair = new java.io.PrintWriter(outputrepair, true);
78 this.outputaux = new java.io.PrintWriter(outputaux, true);
79 this.outputhead = new java.io.PrintWriter(outputhead, true);
82 generatetypechecks(true);
83 generate_tokentable();
84 generate_hashtables();
85 generate_stateobject();
89 if (!Compiler.REPAIR||Compiler.GENERATEDEBUGPRINT) {
94 CodeWriter crhead = new StandardCodeWriter(this.outputhead);
95 CodeWriter craux = new StandardCodeWriter(this.outputaux);
96 crhead.outputline("};");
97 craux.outputline("}");
99 if (Compiler.GENERATEDEBUGHOOKS) {
100 crhead.outputline("void debughook();");
101 craux.outputline("void debughook() {}");
103 generatetypechecks(false);
104 generate_computesizes();
105 generatetypechecks(true);
106 generate_recomputesizes();
107 generatetypechecks(false);
109 StructureGenerator sg=new StructureGenerator(state,this);
111 crhead.outputline("#endif");
114 String ststate="state";
115 String stmodel="model";
116 String strepairtable="repairtable";
117 String stleft="left";
118 String stright="right";
119 String stnew="newvalue";
121 private void generate_updates() {
123 CodeWriter crhead = new StandardCodeWriter(outputhead);
124 CodeWriter craux = new StandardCodeWriter(outputaux);
125 RelationDescriptor.prefix = "model->";
126 SetDescriptor.prefix = "model->";
128 /* Rewrite globals */
130 for (Iterator it=this.state.stGlobals.descriptors();it.hasNext();) {
131 VarDescriptor vd=(VarDescriptor)it.next();
132 craux.outputline("#define "+vd.getSafeSymbol()+" "+ststate+"->"+vd.getSafeSymbol());
135 for(Iterator it=termination.updatenodes.iterator();it.hasNext();) {
136 GraphNode gn=(GraphNode) it.next();
137 TermNode tn=(TermNode) gn.getOwner();
138 MultUpdateNode mun=tn.getUpdate();
139 boolean isrelation=(mun.getDescriptor() instanceof RelationDescriptor);
140 if (togenerate.contains(gn))
141 for (int i=0;i<mun.numUpdates();i++) {
142 UpdateNode un=mun.getUpdate(i);
143 String methodname=(String)updatenames.get(un);
146 case MultUpdateNode.ADD:
148 crhead.outputline("void "+methodname+"("+name+"_state * " +ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable+", int "+stleft+", int "+stright+");");
149 craux.outputline("void "+methodname+"("+name+"_state * "+ ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable+", int "+stleft+", int "+stright+")");
151 crhead.outputline("void "+methodname+"("+name+"_state * "+ ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable+", int "+stleft+");");
152 craux.outputline("void "+methodname+"("+name+"_state * "+ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable+", int "+stleft+")");
155 craux.outputline("int maybe=0;");
156 final SymbolTable st = un.getRule().getSymbolTable();
157 CodeWriter cr = new StandardCodeWriter(outputaux) {
158 public SymbolTable getSymbolTable() { return st; }
160 un.generate(cr, false, false, stleft,stright, null,this);
161 craux.outputline("if (maybe) printf(\"REALLY BAD\");");
164 case MultUpdateNode.REMOVE: {
166 String methodcall="void "+methodname+"("+name+"_state * "+ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable;
167 for(int j=0;j<r.numQuantifiers();j++) {
168 Quantifier q=r.getQuantifier(j);
169 if (q instanceof SetQuantifier) {
170 SetQuantifier sq=(SetQuantifier) q;
171 methodcall+=","+sq.getVar().getType().getGenerateType().getSafeSymbol()+" "+sq.getVar().getSafeSymbol();
172 } else if (q instanceof RelationQuantifier) {
173 RelationQuantifier rq=(RelationQuantifier) q;
175 methodcall+=","+rq.x.getType().getGenerateType().getSafeSymbol()+" "+rq.x.getSafeSymbol();
176 methodcall+=","+rq.y.getType().getGenerateType().getSafeSymbol()+" "+rq.y.getSafeSymbol();
177 } else if (q instanceof ForQuantifier) {
178 ForQuantifier fq=(ForQuantifier) q;
179 methodcall+=",int "+fq.getVar().getSafeSymbol();
183 crhead.outputline(methodcall+";");
184 craux.outputline(methodcall);
186 craux.outputline("int maybe=0;");
187 final SymbolTable st2 = un.getRule().getSymbolTable();
188 CodeWriter cr2 = new StandardCodeWriter(outputaux) {
189 public SymbolTable getSymbolTable() { return st2; }
191 un.generate(cr2, true, false, null,null, null,this);
192 craux.outputline("if (maybe) printf(\"REALLY BAD\");");
196 case MultUpdateNode.MODIFY: {
198 String methodcall="void "+methodname+"("+name+"_state * "+ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable;
199 for(int j=0;j<r.numQuantifiers();j++) {
200 Quantifier q=r.getQuantifier(j);
201 if (q instanceof SetQuantifier) {
202 SetQuantifier sq=(SetQuantifier) q;
203 methodcall+=","+sq.getVar().getType().getGenerateType().getSafeSymbol()+" "+sq.getVar().getSafeSymbol();
204 } else if (q instanceof RelationQuantifier) {
205 RelationQuantifier rq=(RelationQuantifier) q;
207 methodcall+=", "+rq.x.getType().getGenerateType().getSafeSymbol()+" "+rq.x.getSafeSymbol();
208 methodcall+=", "+rq.y.getType().getGenerateType().getSafeSymbol()+" "+rq.y.getSafeSymbol();
209 } else if (q instanceof ForQuantifier) {
210 ForQuantifier fq=(ForQuantifier) q;
211 methodcall+=", int "+fq.getVar().getSafeSymbol();
214 methodcall+=", int "+stleft+", int "+stright+", int "+stnew;
216 crhead.outputline(methodcall+";");
217 craux.outputline(methodcall);
219 craux.outputline("int maybe=0;");
220 final SymbolTable st2 = un.getRule().getSymbolTable();
221 CodeWriter cr2 = new StandardCodeWriter(outputaux) {
222 public SymbolTable getSymbolTable() { return st2; }
224 un.generate(cr2, false, true, stleft, stright, stnew, this);
225 craux.outputline("if (maybe) printf(\"REALLY BAD\");");
231 throw new Error("Nonimplement Update");
237 private void generate_call() {
238 CodeWriter cr = new StandardCodeWriter(outputrepair);
239 VarDescriptor vdstate=VarDescriptor.makeNew("repairstate");
240 cr.outputline(name+"_state * "+vdstate.getSafeSymbol()+"=new "+name+"_state();");
241 Iterator globals=state.stGlobals.descriptors();
242 while (globals.hasNext()) {
243 VarDescriptor vd=(VarDescriptor) globals.next();
244 cr.outputline(vdstate.getSafeSymbol()+"->"+vd.getSafeSymbol()+"=("+vd.getType().getGenerateType().getSafeSymbol()+")"+vd.getSafeSymbol()+";");
246 /* Insert repair here */
247 cr.outputline(vdstate.getSafeSymbol()+"->doanalysis();");
248 globals=state.stGlobals.descriptors();
249 while (globals.hasNext()) {
250 VarDescriptor vd=(VarDescriptor) globals.next();
251 cr.outputline("*(("+vd.getType().getGenerateType().getSafeSymbol()+"*) &"+vd.getSafeSymbol()+")="+vdstate.getSafeSymbol()+"->"+vd.getSafeSymbol()+";");
253 cr.outputline("delete "+vdstate.getSafeSymbol()+";");
256 private void generate_tokentable() {
257 CodeWriter cr = new StandardCodeWriter(outputrepair);
258 Iterator tokens = TokenLiteralExpr.tokens.keySet().iterator();
261 cr.outputline("// Token values");
264 while (tokens.hasNext()) {
265 Object token = tokens.next();
266 cr.outputline("// " + token.toString() + " = " + TokenLiteralExpr.tokens.get(token).toString());
273 private void generate_stateobject() {
274 CodeWriter crhead = new StandardCodeWriter(outputhead);
275 crhead.outputline("class "+name+"_state {");
276 crhead.outputline("public:");
277 Iterator globals=state.stGlobals.descriptors();
278 while (globals.hasNext()) {
279 VarDescriptor vd=(VarDescriptor) globals.next();
280 crhead.outputline(vd.getType().getGenerateType().getSafeSymbol()+" "+vd.getSafeSymbol()+";");
282 crhead.outputline("void computesizes(int *,int **);");
283 crhead.outputline("void recomputesizes();");
286 private void generate_computesizes() {
287 int max=TypeDescriptor.counter;
288 TypeDescriptor[] tdarray=new TypeDescriptor[max];
289 for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
290 TypeDescriptor ttd=(TypeDescriptor)it.next();
291 tdarray[ttd.getId()]=ttd;
293 final SymbolTable st = state.stGlobals;
294 CodeWriter cr = new StandardCodeWriter(outputaux) {
295 public SymbolTable getSymbolTable() { return st; }
298 cr.outputline("void "+name+"_state::computesizes(int *sizearray,int **numele) {");
299 cr.outputline("int maybe=0;");
300 for(int i=0;i<max;i++) {
301 TypeDescriptor td=tdarray[i];
302 Expr size=td.getSizeExpr();
303 VarDescriptor vd=VarDescriptor.makeNew("size");
304 size.generate(cr,vd);
305 cr.outputline("sizearray["+i+"]="+vd.getSafeSymbol()+";");
307 for(int i=0;i<max;i++) {
308 TypeDescriptor td=tdarray[i];
309 if (td instanceof StructureTypeDescriptor) {
310 StructureTypeDescriptor std=(StructureTypeDescriptor) td;
311 for(int j=0;j<std.fieldlist.size();j++) {
312 FieldDescriptor fd=(FieldDescriptor)std.fieldlist.get(j);
313 if (fd instanceof ArrayDescriptor) {
314 ArrayDescriptor ad=(ArrayDescriptor)fd;
315 Expr index=ad.getIndexBound();
316 VarDescriptor vd=VarDescriptor.makeNew("index");
317 index.generate(cr,vd);
318 cr.outputline("numele["+i+"]["+j+"]="+vd.getSafeSymbol()+";");
323 cr.outputline("if (maybe) printf(\"BAD ERROR\");");
327 private void generate_recomputesizes() {
328 int max=TypeDescriptor.counter;
329 TypeDescriptor[] tdarray=new TypeDescriptor[max];
330 for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
331 TypeDescriptor ttd=(TypeDescriptor)it.next();
332 tdarray[ttd.getId()]=ttd;
334 final SymbolTable st = state.stGlobals;
335 CodeWriter cr = new StandardCodeWriter(outputaux) {
336 public SymbolTable getSymbolTable() { return st; }
338 cr.outputline("void "+name+"_state::recomputesizes() {");
339 cr.outputline("int maybe=0;");
340 for(int i=0;i<max;i++) {
341 TypeDescriptor td=tdarray[i];
342 Expr size=td.getSizeExpr();
343 VarDescriptor vd=VarDescriptor.makeNew("size");
344 size.generate(cr,vd);
346 for(int i=0;i<max;i++) {
347 TypeDescriptor td=tdarray[i];
348 if (td instanceof StructureTypeDescriptor) {
349 StructureTypeDescriptor std=(StructureTypeDescriptor) td;
350 for(int j=0;j<std.fieldlist.size();j++) {
351 FieldDescriptor fd=(FieldDescriptor)std.fieldlist.get(j);
352 if (fd instanceof ArrayDescriptor) {
353 ArrayDescriptor ad=(ArrayDescriptor)fd;
354 Expr index=ad.getIndexBound();
355 VarDescriptor vd=VarDescriptor.makeNew("index");
356 index.generate(cr,vd);
361 cr.outputline("if (maybe) printf(\"BAD ERROR\");");
366 private void generate_hashtables() {
367 CodeWriter craux = new StandardCodeWriter(outputaux);
368 CodeWriter crhead = new StandardCodeWriter(outputhead);
369 crhead.outputline("#ifndef "+name+"_h");
370 crhead.outputline("#define "+name+"_h");
371 crhead.outputline("#include \"SimpleHash.h\"");
372 crhead.outputline("extern \"C\" {");
373 crhead.outputline("#include \"instrument.h\"");
374 crhead.outputline("}");
375 crhead.outputline("#include <stdio.h>");
376 crhead.outputline("#include <stdlib.h>");
377 crhead.outputline("class "+name+" {");
378 crhead.outputline("public:");
379 crhead.outputline(name+"();");
380 crhead.outputline("~"+name+"();");
381 craux.outputline("#include \""+headername+"\"");
382 craux.outputline("#include \"size.h\"");
384 craux.outputline(name+"::"+name+"() {");
385 craux.outputline("// creating hashtables ");
388 Iterator sets = state.stSets.descriptors();
390 /* first pass create all the hash tables */
391 while (sets.hasNext()) {
392 SetDescriptor set = (SetDescriptor) sets.next();
393 crhead.outputline("SimpleHash* " + set.getSafeSymbol() + "_hash;");
394 craux.outputline(set.getSafeSymbol() + "_hash = new SimpleHash();");
397 /* second pass build relationships between hashtables */
398 sets = state.stSets.descriptors();
400 while (sets.hasNext()) {
401 SetDescriptor set = (SetDescriptor) sets.next();
402 Iterator subsets = set.subsets();
404 while (subsets.hasNext()) {
405 SetDescriptor subset = (SetDescriptor) subsets.next();
406 craux.outputline(subset.getSafeSymbol() + "_hash->addParent(" + set.getSafeSymbol() + "_hash);");
410 /* build relations */
411 Iterator relations = state.stRelations.descriptors();
413 /* first pass create all the hash tables */
414 while (relations.hasNext()) {
415 RelationDescriptor relation = (RelationDescriptor) relations.next();
417 if (relation.testUsage(RelationDescriptor.IMAGE)) {
418 crhead.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hash;");
419 craux.outputline(relation.getSafeSymbol() + "_hash = new SimpleHash();");
422 if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
423 crhead.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hashinv;");
424 craux.outputline(relation.getSafeSymbol() + "_hashinv = new SimpleHash();");
428 craux.outputline("}");
429 crhead.outputline("};");
430 craux.outputline(name+"::~"+name+"() {");
431 craux.outputline("// deleting hashtables");
433 /* build destructor */
434 sets = state.stSets.descriptors();
436 /* first pass create all the hash tables */
437 while (sets.hasNext()) {
438 SetDescriptor set = (SetDescriptor) sets.next();
439 craux.outputline("delete "+set.getSafeSymbol() + "_hash;");
442 /* destroy relations */
443 relations = state.stRelations.descriptors();
445 /* first pass create all the hash tables */
446 while (relations.hasNext()) {
447 RelationDescriptor relation = (RelationDescriptor) relations.next();
449 if (relation.testUsage(RelationDescriptor.IMAGE)) {
450 craux.outputline("delete "+relation.getSafeSymbol() + "_hash;");
453 if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
454 craux.outputline("delete " + relation.getSafeSymbol() + "_hashinv;");
457 craux.outputline("}");
460 private void generate_start() {
461 CodeWriter crhead = new StandardCodeWriter(outputhead);
462 CodeWriter craux = new StandardCodeWriter(outputaux);
463 oldmodel=VarDescriptor.makeNew("oldmodel");
464 newmodel=VarDescriptor.makeNew("newmodel");
465 worklist=VarDescriptor.makeNew("worklist");
466 goodflag=VarDescriptor.makeNew("goodflag");
467 repairtable=VarDescriptor.makeNew("repairtable");
468 crhead.outputline("void doanalysis();");
469 craux.outputline("void "+name +"_state::doanalysis()");
471 craux.outputline("int highmark;");
472 craux.outputline("initializestack(&highmark);");
473 craux.outputline("typeobject *typeobject1=gettypeobject();");
474 craux.outputline("typeobject1->computesizes(this);");
475 craux.outputline("recomputesizes();");
476 craux.outputline(name+ " * "+oldmodel.getSafeSymbol()+"=0;");
477 craux.outputline("WorkList * "+worklist.getSafeSymbol()+" = new WorkList();");
478 craux.outputline("RepairHash * "+repairtable.getSafeSymbol()+"=0;");
479 craux.outputline("while (1)");
481 craux.outputline(name+ " * "+newmodel.getSafeSymbol()+"=new "+name+"();");
484 private void generate_teardown() {
485 CodeWriter cr = new StandardCodeWriter(outputaux);
489 private void generate_print() {
491 final SymbolTable st = new SymbolTable();
493 CodeWriter cr = new StandardCodeWriter(outputaux) {
494 public SymbolTable getSymbolTable() { return st; }
497 cr.outputline("// printing sets!");
498 cr.outputline("printf(\"\\n\\nPRINTING SETS AND RELATIONS\\n\");");
500 Iterator setiterator = state.stSets.descriptors();
501 while (setiterator.hasNext()) {
502 SetDescriptor sd = (SetDescriptor) setiterator.next();
503 if (sd.getSymbol().equals("int") || sd.getSymbol().equals("token")) {
507 String setname = sd.getSafeSymbol();
510 cr.outputline("// printing set " + setname);
511 cr.outputline("printf(\"\\nPrinting set " + sd.getSymbol() + " - %d elements \\n\", " + setname + "_hash->count());");
512 cr.outputline("SimpleIterator __setiterator;");
513 cr.outputline("" + setname + "_hash->iterator(__setiterator);");
514 cr.outputline("while (__setiterator.hasNext())");
516 cr.outputline("int __setval = (int) __setiterator.next();");
518 TypeDescriptor td = sd.getType();
519 if (td instanceof StructureTypeDescriptor) {
520 StructureTypeDescriptor std = (StructureTypeDescriptor) td;
521 VarDescriptor vd = new VarDescriptor ("__setval", "__setval", td, false);
522 std.generate_printout(cr, vd);
523 } else { // Missing type descriptor or reserved type, just print int
524 cr.outputline("printf(\"<%d> \", __setval);");
532 cr.outputline("printf(\"\\n\\n------------------- END PRINTING\\n\");");
536 private void generate_rules() {
537 /* first we must sort the rules */
538 RelationDescriptor.prefix = newmodel.getSafeSymbol()+"->";
539 SetDescriptor.prefix = newmodel.getSafeSymbol()+"->";
540 System.out.println("SCC="+(mrd.numSCC()-1));
541 for(int sccindex=0;sccindex<mrd.numSCC();sccindex++) {
542 ruleset=mrd.getSCC(sccindex);
543 boolean needworklist=mrd.hasCycle(sccindex);
546 Iterator iterator_rs = ruleset.iterator();
547 while (iterator_rs.hasNext()) {
548 Rule rule = (Rule) iterator_rs.next();
550 final SymbolTable st = rule.getSymbolTable();
551 CodeWriter cr = new StandardCodeWriter(outputaux) {
552 public SymbolTable getSymbolTable() { return st; }
554 cr.outputline("// build " +escape(rule.toString()));
556 cr.outputline("int maybe=0;");
557 ListIterator quantifiers = rule.quantifiers();
558 while (quantifiers.hasNext()) {
559 Quantifier quantifier = (Quantifier) quantifiers.next();
560 quantifier.generate_open(cr);
565 rule.getGuardExpr().prettyPrint(cr);
568 /* now we have to generate the guard test */
569 VarDescriptor guardval = VarDescriptor.makeNew();
570 rule.getGuardExpr().generate(cr, guardval);
571 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
574 /* now we have to generate the inclusion code */
576 rule.getInclusion().generate(cr);
578 while (quantifiers.hasPrevious()) {
579 Quantifier quantifier = (Quantifier) quantifiers.previous();
588 CodeWriter cr2 = new StandardCodeWriter(outputaux);
590 for(Iterator initialworklist=ruleset.iterator();initialworklist.hasNext();) {
591 /** Construct initial worklist set */
592 Rule rule=(Rule)initialworklist.next();
593 cr2.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+",-1,0,0);");
596 cr2.outputline("while ("+worklist.getSafeSymbol()+"->hasMoreElements())");
598 VarDescriptor idvar=VarDescriptor.makeNew("id");
599 cr2.outputline("int "+idvar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->getid();");
601 String elseladder = "if";
603 Iterator iterator_rules = ruleset.iterator();
604 while (iterator_rules.hasNext()) {
606 Rule rule = (Rule) iterator_rules.next();
607 int dispatchid = rule.getNum();
610 final SymbolTable st = rule.getSymbolTable();
611 CodeWriter cr = new StandardCodeWriter(outputaux) {
612 public SymbolTable getSymbolTable() { return st; }
616 cr.outputline(elseladder + " ("+idvar.getSafeSymbol()+" == " + dispatchid + ")");
618 cr.outputline("int maybe=0;");
619 VarDescriptor typevar=VarDescriptor.makeNew("type");
620 VarDescriptor leftvar=VarDescriptor.makeNew("left");
621 VarDescriptor rightvar=VarDescriptor.makeNew("right");
622 cr.outputline("int "+typevar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->gettype();");
623 cr.outputline("int "+leftvar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->getlvalue();");
624 cr.outputline("int "+rightvar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->getrvalue();");
625 cr.outputline("// build " +escape(rule.toString()));
628 for (int j=0;j<rule.numQuantifiers();j++) {
629 Quantifier quantifier = rule.getQuantifier(j);
630 quantifier.generate_open(cr, typevar.getSafeSymbol(),j,leftvar.getSafeSymbol(),rightvar.getSafeSymbol());
636 rule.getGuardExpr().prettyPrint(cr);
639 /* now we have to generate the guard test */
641 VarDescriptor guardval = VarDescriptor.makeNew();
642 rule.getGuardExpr().generate(cr, guardval);
644 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
647 /* now we have to generate the inclusion code */
649 rule.getInclusion().generate(cr);
652 for (int j=0;j<rule.numQuantifiers();j++) {
656 // close startblocks generated by DotExpr memory checks
657 //DotExpr.generate_memory_endblocks(cr);
659 cr.endblock(); // end else-if WORKLIST ladder
661 elseladder = "else if";
664 cr2.outputline("else");
666 cr2.outputline("printf(\"VERY BAD !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\\n\\n\");");
667 cr2.outputline("exit(1);");
669 // end block created for worklist
670 cr2.outputline(worklist.getSafeSymbol()+"->pop();");
676 public static String escape(String s) {
678 for(int i=0;i<s.length();i++) {
688 private void generate_checks() {
690 /* do constraint checks */
691 // Vector constraints = state.vConstraints;
694 // for (int i = 0; i < constraints.size(); i++) {
695 // Constraint constraint = (Constraint) constraints.elementAt(i);
696 for (Iterator i = termination.constraintdependence.computeOrdering().iterator(); i.hasNext();) {
697 Constraint constraint = (Constraint) ((GraphNode)i.next()).getOwner();
700 final SymbolTable st = constraint.getSymbolTable();
701 CodeWriter cr = new StandardCodeWriter(outputaux);
702 cr.pushSymbolTable(constraint.getSymbolTable());
704 cr.outputline("// checking " + escape(constraint.toString()));
707 ListIterator quantifiers = constraint.quantifiers();
709 while (quantifiers.hasNext()) {
710 Quantifier quantifier = (Quantifier) quantifiers.next();
711 quantifier.generate_open(cr);
714 cr.outputline("int maybe = 0;");
716 /* now we have to generate the guard test */
718 VarDescriptor constraintboolean = VarDescriptor.makeNew("constraintboolean");
719 constraint.getLogicStatement().generate(cr, constraintboolean);
721 cr.outputline("if (maybe)");
723 cr.outputline("printf(\"maybe fail " + escape(constraint.toString()) + ". \\n\");");
724 cr.outputline("exit(1);");
727 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
729 if (!Compiler.REPAIR||Compiler.GENERATEDEBUGHOOKS)
730 cr.outputline("printf(\"fail " + escape(constraint.toString()) + ". \\n\");");
732 if (Compiler.REPAIR) {
734 /* Build new repair table */
735 cr.outputline("if ("+repairtable.getSafeSymbol()+")");
736 cr.outputline("delete "+repairtable.getSafeSymbol()+";");
737 cr.outputline(repairtable.getSafeSymbol()+"=new RepairHash();");
739 if (Compiler.GENERATEDEBUGHOOKS)
740 cr.outputline("debughook();");
741 /* Compute cost of each repair */
742 VarDescriptor mincost=VarDescriptor.makeNew("mincost");
743 VarDescriptor mincostindex=VarDescriptor.makeNew("mincostindex");
744 Vector dnfconst=new Vector();
745 dnfconst.addAll((Set)termination.conjunctionmap.get(constraint));
747 if (dnfconst.size()<=1) {
748 cr.outputline("int "+mincostindex.getSafeSymbol()+"=0;");
750 if (dnfconst.size()>1) {
751 cr.outputline("int "+mincostindex.getSafeSymbol()+";");
753 for(int j=0;j<dnfconst.size();j++) {
754 GraphNode gn=(GraphNode)dnfconst.get(j);
755 Conjunction conj=((TermNode)gn.getOwner()).getConjunction();
756 if (removed.contains(gn))
759 VarDescriptor costvar;
763 costvar=VarDescriptor.makeNew("cost");
764 for(int k=0;k<conj.size();k++) {
765 DNFPredicate dpred=conj.get(k);
766 Predicate p=dpred.getPredicate();
767 boolean negate=dpred.isNegated();
768 VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
769 p.generate(cr,predvalue);
771 cr.outputline("int "+costvar.getSafeSymbol()+"=0;");
774 cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
776 cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
777 cr.outputline(costvar.getSafeSymbol()+"+="+cost.getCost(dpred)+";");
781 cr.outputline("if ("+costvar.getSafeSymbol()+"<"+mincost.getSafeSymbol()+")");
783 cr.outputline(mincost.getSafeSymbol()+"="+costvar.getSafeSymbol()+";");
784 cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
787 cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
791 cr.outputline("switch("+mincostindex.getSafeSymbol()+") {");
792 for(int j=0;j<dnfconst.size();j++) {
793 GraphNode gn=(GraphNode)dnfconst.get(j);
794 Conjunction conj=((TermNode)gn.getOwner()).getConjunction();
796 if (removed.contains(gn))
798 cr.outputline("case "+j+":");
799 for(int k=0;k<conj.size();k++) {
800 DNFPredicate dpred=conj.get(k);
801 Predicate p=dpred.getPredicate();
802 boolean negate=dpred.isNegated();
803 VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
804 p.generate(cr,predvalue);
806 cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
808 cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
810 if (p instanceof InclusionPredicate)
811 generateinclusionrepair(conj,dpred, cr);
812 else if (p instanceof ExprPredicate) {
813 ExprPredicate ep=(ExprPredicate)p;
814 if (ep.getType()==ExprPredicate.SIZE)
815 generatesizerepair(conj,dpred,cr);
816 else if (ep.getType()==ExprPredicate.COMPARISON)
817 generatecomparisonrepair(conj,dpred,cr);
818 } else throw new Error("Unrecognized Predicate");
822 cr.outputline("break;");
826 cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
827 cr.outputline("delete "+oldmodel.getSafeSymbol()+";");
828 cr.outputline(oldmodel.getSafeSymbol()+"="+newmodel.getSafeSymbol()+";");
829 cr.outputline("goto rebuild;"); /* Rebuild model and all */
833 while (quantifiers.hasPrevious()) {
834 Quantifier quantifier = (Quantifier) quantifiers.previous();
842 CodeWriter cr = new StandardCodeWriter(outputaux);
844 cr.outputline("if ("+repairtable.getSafeSymbol()+")");
845 cr.outputline("delete "+repairtable.getSafeSymbol()+";");
846 cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
847 cr.outputline("delete "+oldmodel.getSafeSymbol()+";");
848 cr.outputline("delete "+newmodel.getSafeSymbol()+";");
849 cr.outputline("delete "+worklist.getSafeSymbol()+";");
850 cr.outputline("resettypemap();");
851 cr.outputline("break;");
853 cr.outputline("rebuild:");
858 private MultUpdateNode getmultupdatenode(Conjunction conj, DNFPredicate dpred, int repairtype) {
859 MultUpdateNode mun=null;
860 GraphNode gn=(GraphNode) termination.conjtonodemap.get(conj);
861 for(Iterator edgeit=gn.edges();(mun==null)&&edgeit.hasNext();) {
862 GraphNode gn2=((GraphNode.Edge) edgeit.next()).getTarget();
863 TermNode tn2=(TermNode)gn2.getOwner();
864 if (tn2.getType()==TermNode.ABSTRACT) {
865 AbstractRepair ar=tn2.getAbstract();
866 if (((repairtype==-1)||(ar.getType()==repairtype))&&
867 ar.getPredicate()==dpred) {
868 for(Iterator edgeit2=gn2.edges();edgeit2.hasNext();) {
869 GraphNode gn3=((GraphNode.Edge) edgeit2.next()).getTarget();
870 if (!removed.contains(gn3)) {
871 TermNode tn3=(TermNode)gn3.getOwner();
872 if (tn3.getType()==TermNode.UPDATE) {
884 /** Generates abstract (and concrete) repair for a comparison */
886 private void generatecomparisonrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
887 MultUpdateNode munmodify=getmultupdatenode(conj,dpred,AbstractRepair.MODIFYRELATION);
888 MultUpdateNode munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMRELATION);
889 MultUpdateNode munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTORELATION);
890 ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
891 RelationDescriptor rd=(RelationDescriptor)ep.getDescriptor();
892 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
893 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
894 boolean inverted=ep.inverted();
895 boolean negated=dpred.isNegated();
896 OpExpr expr=(OpExpr)ep.expr;
897 Opcode opcode=expr.getOpcode();
898 VarDescriptor leftside=VarDescriptor.makeNew("leftside");
899 VarDescriptor rightside=VarDescriptor.makeNew("rightside");
900 VarDescriptor newvalue=VarDescriptor.makeNew("newvalue");
902 ((RelationExpr)expr.getLeftExpr()).getExpr().generate(cr,leftside);
903 expr.getRightExpr().generate(cr,newvalue);
904 cr.outputline(rd.getRange().getType().getGenerateType().getSafeSymbol()+" "+rightside.getSafeSymbol()+";");
905 cr.outputline(rd.getSafeSymbol()+"_hash->get("+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
907 ((RelationExpr)expr.getLeftExpr()).getExpr().generate(cr,rightside);
908 expr.getRightExpr().generate(cr,newvalue);
909 cr.outputline(rd.getDomain().getType().getGenerateType().getSafeSymbol()+" "+leftside.getSafeSymbol()+";");
910 cr.outputline(rd.getSafeSymbol()+"_hashinv->get("+rightside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
913 if (opcode==Opcode.GT) {
915 } else if (opcode==Opcode.GE) {
917 } else if (opcode==Opcode.LT) {
919 } else if (opcode==Opcode.LE) {
921 } else if (opcode==Opcode.EQ) {
923 } else if (opcode==Opcode.NE) {
926 throw new Error("Unrecognized Opcode");
929 if (opcode==Opcode.GT) {
930 cr.outputline(newvalue.getSafeSymbol()+"++;");
931 } else if (opcode==Opcode.GE) {
933 } else if (opcode==Opcode.LT) {
934 cr.outputline(newvalue.getSafeSymbol()+"--;");
935 } else if (opcode==Opcode.LE) {
937 } else if (opcode==Opcode.EQ) {
939 } else if (opcode==Opcode.NE) { /* search for FLAGNE if this is changed*/
940 cr.outputline(newvalue.getSafeSymbol()+"++;");
942 throw new Error("Unrecognized Opcode");
944 /* Do abstract repairs */
946 cr.outputline(rd.getSafeSymbol()+"_hash->remove("+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
948 cr.outputline(rd.getSafeSymbol()+"_hash->add("+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
950 cr.outputline(rd.getSafeSymbol()+"_hash->add("+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
954 cr.outputline(rd.getSafeSymbol()+"_hashinv->remove("+rightside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
956 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+newvalue.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
958 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+rightside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
961 /* Do concrete repairs */
962 if (munmodify!=null) {
963 for(int i=0;i<state.vRules.size();i++) {
964 Rule r=(Rule)state.vRules.get(i);
965 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
966 for(int j=0;j<munmodify.numUpdates();j++) {
967 UpdateNode un=munmodify.getUpdate(j);
968 if (un.getRule()==r) {
969 /* Update for rule r */
970 String name=(String)updatenames.get(un);
971 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+","+newvalue.getSafeSymbol()+");");
978 /* Start with scheduling removal */
979 for(int i=0;i<state.vRules.size();i++) {
980 Rule r=(Rule)state.vRules.get(i);
981 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
982 for(int j=0;j<munremove.numUpdates();j++) {
983 UpdateNode un=munremove.getUpdate(i);
984 if (un.getRule()==r) {
985 /* Update for rule r */
986 String name=(String)updatenames.get(un);
987 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+");");
992 /* Now do addition */
993 UpdateNode un=munadd.getUpdate(0);
994 String name=(String)updatenames.get(un);
996 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
998 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1003 public void generatesizerepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr) {
1004 ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
1005 OpExpr expr=(OpExpr)ep.expr;
1006 Opcode opcode=expr.getOpcode();
1007 opcode=Opcode.translateOpcode(dpred.isNegated(),opcode);
1009 MultUpdateNode munremove;
1011 MultUpdateNode munadd;
1012 if (ep.getDescriptor() instanceof RelationDescriptor) {
1013 munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMRELATION);
1014 munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTORELATION);
1016 munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMSET);
1017 munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTOSET);
1019 int size=ep.rightSize();
1020 VarDescriptor sizevar=VarDescriptor.makeNew("size");
1021 ((OpExpr)expr).left.generate(cr, sizevar);
1022 VarDescriptor change=VarDescriptor.makeNew("change");
1023 cr.outputline("int "+change.getSafeSymbol()+";");
1024 boolean generateadd=false;
1025 boolean generateremove=false;
1026 if (opcode==Opcode.GT) {
1027 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
1029 generateremove=false;
1030 } else if (opcode==Opcode.GE) {
1031 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1033 generateremove=false;
1034 } else if (opcode==Opcode.LT) {
1035 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size-1)+"-"+sizevar.getSafeSymbol()+";");
1037 generateremove=true;
1038 } else if (opcode==Opcode.LE) {
1039 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1041 generateremove=true;
1042 } else if (opcode==Opcode.EQ) {
1043 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1048 generateremove=true;
1049 } else if (opcode==Opcode.NE) {
1050 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
1052 generateremove=false;
1054 throw new Error("Unrecognized Opcode");
1057 // In some cases the analysis has determined that generating removes
1059 if (generateremove&&munremove==null)
1060 generateremove=false;
1062 Descriptor d=ep.getDescriptor();
1063 if (generateremove) {
1064 cr.outputline("for(;"+change.getSafeSymbol()+"<0;"+change.getSafeSymbol()+"++)");
1066 /* Find element to remove */
1067 VarDescriptor leftvar=VarDescriptor.makeNew("leftvar");
1068 VarDescriptor rightvar=VarDescriptor.makeNew("rightvar");
1069 if (d instanceof RelationDescriptor) {
1070 if (ep.inverted()) {
1071 rightvar=((ImageSetExpr)((SizeofExpr)((OpExpr)expr).left).setexpr).getVar();
1072 cr.outputline("int "+leftvar.getSafeSymbol()+";");
1073 cr.outputline(d.getSafeSymbol()+"_hashinv->get((int)"+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1075 leftvar=((ImageSetExpr)((SizeofExpr)((OpExpr)expr).left).setexpr).getVar();
1076 cr.outputline("int "+rightvar.getSafeSymbol()+"=0;");
1077 cr.outputline(d.getSafeSymbol()+"_hash->get((int)"+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+");");
1080 cr.outputline("int "+leftvar.getSafeSymbol()+"="+d.getSafeSymbol()+"_hash->firstkey();");
1082 /* Generate abstract remove instruction */
1083 if (d instanceof RelationDescriptor) {
1084 RelationDescriptor rd=(RelationDescriptor) d;
1085 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1086 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1088 cr.outputline(rd.getSafeSymbol() + "_hash->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1090 cr.outputline(rd.getSafeSymbol() + "_hashinv->remove((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1092 cr.outputline(d.getSafeSymbol() + "_hash->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1094 /* Generate concrete remove instruction */
1095 for(int i=0;i<state.vRules.size();i++) {
1096 Rule r=(Rule)state.vRules.get(i);
1097 if (r.getInclusion().getTargetDescriptors().contains(d)) {
1098 for(int j=0;j<munremove.numUpdates();j++) {
1099 UpdateNode un=munremove.getUpdate(j);
1100 if (un.getRule()==r) {
1101 /* Update for rule rule r */
1102 String name=(String)updatenames.get(un);
1103 if (d instanceof RelationDescriptor) {
1104 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
1106 cr.outputline(repairtable.getSafeSymbol()+"->addset("+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1116 cr.outputline("for(;"+change.getSafeSymbol()+">0;"+change.getSafeSymbol()+"--)");
1118 VarDescriptor newobject=VarDescriptor.makeNew("newobject");
1119 if (d instanceof RelationDescriptor) {
1120 VarDescriptor otherside=((ImageSetExpr)((SizeofExpr)((OpExpr)ep.expr).left).setexpr).vd;
1121 RelationDescriptor rd=(RelationDescriptor)d;
1122 if (termination.sources.relsetSource(rd,!ep.inverted())) {
1124 SetDescriptor sd=termination.sources.relgetSourceSet(rd,!ep.inverted());
1125 VarDescriptor iterator=VarDescriptor.makeNew("iterator");
1126 cr.outputline(sd.getType().getGenerateType().getSafeSymbol() +" "+newobject.getSafeSymbol()+";");
1127 cr.outputline("SimpleIterator "+iterator.getSafeSymbol()+";");
1128 cr.outputline("for("+sd.getSafeSymbol()+"_hash->iterator("+ iterator.getSafeSymbol() +");"+iterator.getSafeSymbol()+".hasNext();)");
1130 if (ep.inverted()) {
1131 cr.outputline("if (!"+rd.getSafeSymbol()+"_hashinv->contains("+iterator.getSafeSymbol()+".key(),"+otherside.getSafeSymbol()+"))");
1133 cr.outputline("if (!"+rd.getSafeSymbol()+"_hash->contains("+otherside.getSafeSymbol()+","+iterator.getSafeSymbol()+".key()))");
1135 cr.outputline(newobject.getSafeSymbol()+"="+iterator.getSafeSymbol()+".key();");
1136 cr.outputline(iterator.getSafeSymbol()+".next();");
1138 } else if (termination.sources.relallocSource(rd,!ep.inverted())) {
1139 /* Allocation Source*/
1140 termination.sources.relgenerateSourceAlloc(cr,newobject,rd,!ep.inverted());
1141 } else throw new Error("No source for adding to Relation");
1142 if (ep.inverted()) {
1143 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1144 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1146 cr.outputline(rd.getSafeSymbol()+"_hash->add("+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1148 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1150 UpdateNode un=munadd.getUpdate(0);
1151 String name=(String)updatenames.get(un);
1152 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1154 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1155 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1157 cr.outputline(rd.getSafeSymbol()+"_hash->add("+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1159 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1160 UpdateNode un=munadd.getUpdate(0);
1161 String name=(String)updatenames.get(un);
1162 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1165 SetDescriptor sd=(SetDescriptor)d;
1166 if (termination.sources.setSource(sd)) {
1169 SetDescriptor sourcesd=termination.sources.getSourceSet(sd);
1170 VarDescriptor iterator=VarDescriptor.makeNew("iterator");
1171 cr.outputline(sourcesd.getType().getGenerateType().getSafeSymbol() +" "+newobject.getSafeSymbol()+";");
1172 cr.outputline("SimpleIterator "+iterator.getSafeSymbol()+";");
1173 cr.outputline("for("+sourcesd.getSafeSymbol()+"_hash->iterator("+iterator.getSafeSymbol()+");"+iterator.getSafeSymbol()+".hasNext();)");
1175 cr.outputline("if (!"+sd.getSafeSymbol()+"_hash->contains("+iterator.getSafeSymbol()+".key()))");
1176 cr.outputline(newobject.getSafeSymbol()+"="+iterator.getSafeSymbol()+".key();");
1177 cr.outputline(iterator.getSafeSymbol()+".next();");
1179 } else if (termination.sources.allocSource(sd)) {
1180 /* Allocation Source*/
1181 termination.sources.generateSourceAlloc(cr,newobject,sd);
1182 } else throw new Error("No source for adding to Set");
1183 cr.outputline(sd.getSafeSymbol()+"_hash->add("+newobject.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1184 UpdateNode un=munadd.getUpdate(0);
1185 String name=(String)updatenames.get(un);
1186 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1192 public void generateinclusionrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
1193 InclusionPredicate ip=(InclusionPredicate) dpred.getPredicate();
1194 boolean negated=dpred.isNegated();
1195 MultUpdateNode mun=getmultupdatenode(conj,dpred,-1);
1196 VarDescriptor leftvar=VarDescriptor.makeNew("left");
1197 ip.expr.generate(cr, leftvar);
1200 if (ip.setexpr instanceof ImageSetExpr) {
1201 ImageSetExpr ise=(ImageSetExpr) ip.setexpr;
1202 VarDescriptor rightvar=ise.getVar();
1203 boolean inverse=ise.inverted();
1204 RelationDescriptor rd=ise.getRelation();
1205 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1206 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1209 cr.outputline(rd.getSafeSymbol() + "_hash->remove((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1211 cr.outputline(rd.getSafeSymbol() + "_hashinv->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1214 cr.outputline(rd.getSafeSymbol() + "_hash->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1216 cr.outputline(rd.getSafeSymbol() + "_hashinv->remove((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1218 for(int i=0;i<state.vRules.size();i++) {
1219 Rule r=(Rule)state.vRules.get(i);
1220 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1221 for(int j=0;j<mun.numUpdates();j++) {
1222 UpdateNode un=mun.getUpdate(j);
1223 if (un.getRule()==r) {
1224 /* Update for rule rule r */
1225 String name=(String)updatenames.get(un);
1227 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1229 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
1236 SetDescriptor sd=ip.setexpr.sd;
1237 cr.outputline(sd.getSafeSymbol() + "_hash->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1239 for(int i=0;i<state.vRules.size();i++) {
1240 Rule r=(Rule)state.vRules.get(i);
1241 if (r.getInclusion().getTargetDescriptors().contains(sd)) {
1242 for(int j=0;j<mun.numUpdates();j++) {
1243 UpdateNode un=mun.getUpdate(j);
1244 if (un.getRule()==r) {
1245 /* Update for rule rule r */
1246 String name=(String)updatenames.get(un);
1247 cr.outputline(repairtable.getSafeSymbol()+"->addset("+sd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1254 /* Generate update */
1255 if (ip.setexpr instanceof ImageSetExpr) {
1256 ImageSetExpr ise=(ImageSetExpr) ip.setexpr;
1257 VarDescriptor rightvar=ise.getVar();
1258 boolean inverse=ise.inverted();
1259 RelationDescriptor rd=ise.getRelation();
1260 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1261 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1264 cr.outputline(rd.getSafeSymbol() + "_hash->add((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1266 cr.outputline(rd.getSafeSymbol() + "_hashinv->add((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1269 cr.outputline(rd.getSafeSymbol() + "_hash->add((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1271 cr.outputline(rd.getSafeSymbol() + "_hashinv->add((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1273 UpdateNode un=mun.getUpdate(0);
1274 String name=(String)updatenames.get(un);
1276 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1278 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+");");
1281 SetDescriptor sd=ip.setexpr.sd;
1282 cr.outputline(sd.getSafeSymbol() + "_hash->add((int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1284 UpdateNode un=mun.getUpdate(0);
1285 /* Update for rule rule r */
1286 String name=(String)updatenames.get(un);
1287 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1292 public static Vector getrulelist(Descriptor d) {
1293 Vector dispatchrules = new Vector();
1294 Vector rules = State.currentState.vRules;
1296 for (int i = 0; i < rules.size(); i++) {
1297 Rule rule = (Rule) rules.elementAt(i);
1298 Set requiredsymbols = rule.getRequiredDescriptors();
1300 // #TBD#: in general this is wrong because these descriptors may contain descriptors
1301 // bound in "in?" expressions which need to be dealt with in a topologically sorted
1304 if (rule.getRequiredDescriptors().contains(d)) {
1305 dispatchrules.addElement(rule);
1308 return dispatchrules;
1311 private boolean need_compensation(Rule r) {
1312 if (!Compiler.REPAIR)
1314 GraphNode gn=(GraphNode)termination.scopefalsify.get(r);
1315 for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1316 GraphNode.Edge edge=(GraphNode.Edge)edgeit.next();
1317 GraphNode gn2=edge.getTarget();
1318 if (!removed.contains(gn2)) {
1319 TermNode tn2=(TermNode)gn2.getOwner();
1320 if (tn2.getType()==TermNode.CONSEQUENCE)
1327 private UpdateNode find_compensation(Rule r) {
1328 GraphNode gn=(GraphNode)termination.scopefalsify.get(r);
1329 for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1330 GraphNode.Edge edge=(GraphNode.Edge)edgeit.next();
1331 GraphNode gn2=edge.getTarget();
1332 if (!removed.contains(gn2)) {
1333 TermNode tn2=(TermNode)gn2.getOwner();
1334 if (tn2.getType()==TermNode.UPDATE) {
1335 MultUpdateNode mun=tn2.getUpdate();
1336 for(int i=0;i<mun.numUpdates();i++) {
1337 UpdateNode un=mun.getUpdate(i);
1338 if (un.getRule()==r)
1344 throw new Error("No Compensation Update could be found");
1347 public void generate_dispatch(CodeWriter cr, RelationDescriptor rd, String leftvar, String rightvar) {
1348 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1349 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1351 if (!(usageinvimage||usageimage)) /* not used at all*/
1354 cr.outputline("// RELATION DISPATCH ");
1355 if (Compiler.REPAIR) {
1356 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1358 cr.outputline("!"+oldmodel.getSafeSymbol() +"->"+rd.getJustSafeSymbol()+"_hash->contains("+leftvar+","+rightvar+"))");
1360 cr.outputline("!"+oldmodel.getSafeSymbol() +"->"+rd.getJustSafeSymbol()+"_hashinv->contains("+rightvar+","+leftvar+"))");
1363 /* Adding new item */
1364 /* Perform safety checks */
1365 cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
1366 cr.outputline(repairtable.getSafeSymbol()+"->containsrelation("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+"))");
1368 /* Have update to call into */
1369 VarDescriptor mdfyptr=VarDescriptor.makeNew("modifyptr");
1370 VarDescriptor ismdfyptr=VarDescriptor.makeNew("ismodifyptr");
1371 cr.outputline("int "+ismdfyptr.getSafeSymbol()+"="+repairtable.getSafeSymbol()+"->ismodify("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1377 for(int i=0;i<currentrule.numQuantifiers();i++) {
1378 if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1379 parttype=parttype+", int, int";
1381 parttype=parttype+", int";
1383 VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1384 VarDescriptor tmpptr=VarDescriptor.makeNew("tempupdateptr");
1386 String methodcall="("+funptr.getSafeSymbol()+") (this,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
1387 for(int i=0;i<currentrule.numQuantifiers();i++) {
1388 Quantifier q=currentrule.getQuantifier(i);
1389 if (q instanceof SetQuantifier) {
1390 SetQuantifier sq=(SetQuantifier) q;
1391 methodcall+=","+sq.getVar().getSafeSymbol();
1392 } else if (q instanceof RelationQuantifier) {
1393 RelationQuantifier rq=(RelationQuantifier) q;
1394 methodcall+=","+rq.x.getSafeSymbol();
1395 methodcall+=","+rq.y.getSafeSymbol();
1396 } else if (q instanceof ForQuantifier) {
1397 ForQuantifier fq=(ForQuantifier) q;
1398 methodcall+=","+fq.getVar().getSafeSymbol();
1404 cr.outputline("void *"+tmpptr.getSafeSymbol()+"=");
1405 cr.outputline("(void *) "+repairtable.getSafeSymbol()+"->getrelation("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1406 cr.outputline("if ("+ismdfyptr.getSafeSymbol()+")");
1409 cr.outputline("int "+mdfyptr.getSafeSymbol()+"="+repairtable.getSafeSymbol()+"->getrelation2("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1410 cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+",int,int,int)="+"(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+",int,int,int)) "+tmpptr.getSafeSymbol()+";");
1411 cr.outputline(methodcall+","+leftvar+", "+rightvar+", "+mdfyptr.getSafeSymbol() +");");
1414 cr.outputline("else ");
1417 cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")="+"(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")) "+tmpptr.getSafeSymbol()+";");
1418 cr.outputline(methodcall+");");
1421 cr.outputline("delete "+newmodel.getSafeSymbol()+";");
1422 cr.outputline("goto rebuild;");
1426 /* Build standard compensation actions */
1427 if (need_compensation(currentrule)) {
1428 UpdateNode un=find_compensation(currentrule);
1429 String name=(String)updatenames.get(un);
1430 usedupdates.add(un); /* Mark as used */
1431 String methodcall=name+"(this,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
1432 for(int i=0;i<currentrule.numQuantifiers();i++) {
1433 Quantifier q=currentrule.getQuantifier(i);
1434 if (q instanceof SetQuantifier) {
1435 SetQuantifier sq=(SetQuantifier) q;
1436 methodcall+=","+sq.getVar().getSafeSymbol();
1437 } else if (q instanceof RelationQuantifier) {
1438 RelationQuantifier rq=(RelationQuantifier) q;
1439 methodcall+=","+rq.x.getSafeSymbol();
1440 methodcall+=","+rq.y.getSafeSymbol();
1441 } else if (q instanceof ForQuantifier) {
1442 ForQuantifier fq=(ForQuantifier) q;
1443 methodcall+=","+fq.getVar().getSafeSymbol();
1447 cr.outputline(methodcall);
1448 cr.outputline("delete "+newmodel.getSafeSymbol()+";");
1449 cr.outputline("goto rebuild;");
1455 String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1456 cr.outputline("int " + addeditem + "=0;");
1458 String ifstring="if (!maybe&&";
1459 boolean dogenerate=false;
1460 if (rd.getDomain().getType() instanceof StructureTypeDescriptor) {
1465 if (rd.getRange().getType() instanceof StructureTypeDescriptor) {
1467 ifstring+="&&"+rightvar;
1475 if (rd.testUsage(RelationDescriptor.IMAGE)) {
1476 cr.outputline(ifstring);
1477 cr.outputline(addeditem + " = " + rd.getSafeSymbol() + "_hash->add((int)" + leftvar + ", (int)" + rightvar+ ");");
1480 if (rd.testUsage(RelationDescriptor.INVIMAGE)) {
1481 cr.outputline(ifstring);
1482 cr.outputline(addeditem + " = " + rd.getSafeSymbol() + "_hashinv->add((int)" + rightvar + ", (int)" + leftvar + ");");
1486 Vector dispatchrules = getrulelist(rd);
1488 Set toremove=new HashSet();
1489 for(int i=0;i<dispatchrules.size();i++) {
1490 Rule r=(Rule)dispatchrules.get(i);
1491 if (!ruleset.contains(r))
1494 dispatchrules.removeAll(toremove);
1495 if (dispatchrules.size() == 0) {
1496 cr.outputline("// nothing to dispatch");
1500 cr.outputline("if (" + addeditem + ")");
1503 for(int i = 0; i < dispatchrules.size(); i++) {
1504 Rule rule = (Rule) dispatchrules.elementAt(i);
1505 if (rule.getGuardExpr().getRequiredDescriptors().contains(rd)) {
1506 /* Guard depends on this relation, so we recomput everything */
1507 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+",-1,0,0);");
1509 for (int j=0;j<rule.numQuantifiers();j++) {
1510 Quantifier q=rule.getQuantifier(j);
1511 if (q.getRequiredDescriptors().contains(rd)) {
1513 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+","+j+","+leftvar+","+rightvar+");");
1523 public void generate_dispatch(CodeWriter cr, SetDescriptor sd, String setvar) {
1525 cr.outputline("// SET DISPATCH ");
1526 if (Compiler.REPAIR) {
1527 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1528 cr.outputline("!"+oldmodel.getSafeSymbol() +"->"+sd.getJustSafeSymbol()+"_hash->contains("+setvar+"))");
1530 /* Adding new item */
1531 /* Perform safety checks */
1532 cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
1533 cr.outputline(repairtable.getSafeSymbol()+"->containsset("+sd.getNum()+","+currentrule.getNum()+","+setvar+"))");
1535 /* Have update to call into */
1536 VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1538 for(int i=0;i<currentrule.numQuantifiers();i++) {
1539 if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1540 parttype=parttype+", int, int";
1542 parttype=parttype+", int";
1544 cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")=");
1545 cr.outputline("(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")) "+repairtable.getSafeSymbol()+"->getset("+sd.getNum()+","+currentrule.getNum()+","+setvar+");");
1546 String methodcall="("+funptr.getSafeSymbol()+") (this,"+oldmodel.getSafeSymbol()+","+
1547 repairtable.getSafeSymbol();
1548 for(int i=0;i<currentrule.numQuantifiers();i++) {
1549 Quantifier q=currentrule.getQuantifier(i);
1550 if (q instanceof SetQuantifier) {
1551 SetQuantifier sq=(SetQuantifier) q;
1552 methodcall+=","+sq.getVar().getSafeSymbol();
1553 } else if (q instanceof RelationQuantifier) {
1554 RelationQuantifier rq=(RelationQuantifier) q;
1555 methodcall+=","+rq.x.getSafeSymbol();
1556 methodcall+=","+rq.y.getSafeSymbol();
1557 } else if (q instanceof ForQuantifier) {
1558 ForQuantifier fq=(ForQuantifier) q;
1559 methodcall+=","+fq.getVar().getSafeSymbol();
1563 cr.outputline(methodcall);
1564 cr.outputline("delete "+newmodel.getSafeSymbol()+";");
1565 cr.outputline("goto rebuild;");
1568 /* Build standard compensation actions */
1569 if (need_compensation(currentrule)) {
1570 UpdateNode un=find_compensation(currentrule);
1571 String name=(String)updatenames.get(un);
1572 usedupdates.add(un); /* Mark as used */
1574 String methodcall=name+"(this,"+oldmodel.getSafeSymbol()+","+
1575 repairtable.getSafeSymbol();
1576 for(int i=0;i<currentrule.numQuantifiers();i++) {
1577 Quantifier q=currentrule.getQuantifier(i);
1578 if (q instanceof SetQuantifier) {
1579 SetQuantifier sq=(SetQuantifier) q;
1580 methodcall+=","+sq.getVar().getSafeSymbol();
1581 } else if (q instanceof RelationQuantifier) {
1582 RelationQuantifier rq=(RelationQuantifier) q;
1583 methodcall+=","+rq.x.getSafeSymbol();
1584 methodcall+=","+rq.y.getSafeSymbol();
1585 } else if (q instanceof ForQuantifier) {
1586 ForQuantifier fq=(ForQuantifier) q;
1587 methodcall+=","+fq.getVar().getSafeSymbol();
1591 cr.outputline(methodcall);
1592 cr.outputline("delete "+newmodel.getSafeSymbol()+";");
1593 cr.outputline("goto rebuild;");
1599 String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1600 cr.outputline("int " + addeditem + " = 0;");
1601 if (sd.getType() instanceof StructureTypeDescriptor) {
1602 cr.outputline("if (!maybe&&"+setvar+")");
1604 cr.outputline("if (!maybe)");
1605 cr.outputline(addeditem + " = " + sd.getSafeSymbol() + "_hash->add((int)" + setvar + ", (int)" + setvar + ");");
1607 Vector dispatchrules = getrulelist(sd);
1609 Set toremove=new HashSet();
1610 for(int i=0;i<dispatchrules.size();i++) {
1611 Rule r=(Rule)dispatchrules.get(i);
1612 if (!ruleset.contains(r))
1615 dispatchrules.removeAll(toremove);
1617 if (dispatchrules.size() == 0) {
1618 cr.outputline("// nothing to dispatch");
1622 cr.outputline("if ("+addeditem+")");
1624 for(int i = 0; i < dispatchrules.size(); i++) {
1625 Rule rule = (Rule) dispatchrules.elementAt(i);
1626 if (SetDescriptor.expand(rule.getGuardExpr().getRequiredDescriptors()).contains(sd)) {
1627 /* Guard depends on this relation, so we recompute everything */
1628 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+",-1,0,0);");
1630 for (int j=0;j<rule.numQuantifiers();j++) {
1631 Quantifier q=rule.getQuantifier(j);
1632 if (SetDescriptor.expand(q.getRequiredDescriptors()).contains(sd)) {
1634 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+","+j+","+setvar+",0);");