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;
29 ModelRuleDependence mrd;
31 public RepairGenerator(State state, Termination t) {
33 updatenames=new Hashtable();
34 usedupdates=new HashSet();
37 togenerate=new HashSet();
38 togenerate.addAll(termination.conjunctions);
40 togenerate.removeAll(removed);
41 GraphNode.computeclosure(togenerate,removed);
43 sources=new Sources(state);
44 mrd=ModelRuleDependence.doAnalysis(state);
45 Repair.repairgenerator=this;
48 private void generatetypechecks(boolean flag) {
50 DotExpr.DOTYPECHECKS=true;
51 VarExpr.DOTYPECHECKS=true;
55 VarExpr.DOTYPECHECKS=false;
56 DotExpr.DOTYPECHECKS=false;
63 private void name_updates() {
65 for(Iterator it=termination.updatenodes.iterator();it.hasNext();) {
66 GraphNode gn=(GraphNode) it.next();
67 TermNode tn=(TermNode) gn.getOwner();
68 MultUpdateNode mun=tn.getUpdate();
69 if (togenerate.contains(gn))
70 for (int i=0;i<mun.numUpdates();i++) {
71 UpdateNode un=mun.getUpdate(i);
72 String name="update"+String.valueOf(count++);
73 updatenames.put(un,name);
78 public void generate(OutputStream outputrepair, OutputStream outputaux,OutputStream outputhead, String st) {
79 this.outputrepair = new java.io.PrintWriter(outputrepair, true);
80 this.outputaux = new java.io.PrintWriter(outputaux, true);
81 this.outputhead = new java.io.PrintWriter(outputhead, true);
84 generatetypechecks(true);
85 generate_tokentable();
86 generate_hashtables();
87 generate_stateobject();
94 CodeWriter crhead = new StandardCodeWriter(this.outputhead);
95 CodeWriter craux = new StandardCodeWriter(this.outputaux);
96 crhead.outputline("};");
97 craux.outputline("}");
98 generatetypechecks(false);
99 generate_computesizes();
100 generatetypechecks(true);
101 generate_recomputesizes();
102 generatetypechecks(false);
104 StructureGenerator sg=new StructureGenerator(state,this);
106 crhead.outputline("#endif");
109 String ststate="state";
110 String stmodel="model";
111 String strepairtable="repairtable";
112 String stleft="left";
113 String stright="right";
114 String stnew="newvalue";
116 private void generate_updates() {
118 CodeWriter crhead = new StandardCodeWriter(outputhead);
119 CodeWriter craux = new StandardCodeWriter(outputaux);
121 /* Rewrite globals */
123 for (Iterator it=this.state.stGlobals.descriptors();it.hasNext();) {
124 VarDescriptor vd=(VarDescriptor)it.next();
125 craux.outputline("#define "+vd.getSafeSymbol()+" "+ststate+"->"+vd.getSafeSymbol());
128 for(Iterator it=termination.updatenodes.iterator();it.hasNext();) {
129 GraphNode gn=(GraphNode) it.next();
130 TermNode tn=(TermNode) gn.getOwner();
131 MultUpdateNode mun=tn.getUpdate();
132 boolean isrelation=(mun.getDescriptor() instanceof RelationDescriptor);
133 if (togenerate.contains(gn))
134 for (int i=0;i<mun.numUpdates();i++) {
135 UpdateNode un=mun.getUpdate(i);
136 String methodname=(String)updatenames.get(un);
139 case MultUpdateNode.ADD:
141 crhead.outputline("void "+methodname+"("+name+"_state * " +ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable+", int "+stleft+", int "+stright+");");
142 craux.outputline("void "+methodname+"("+name+"_state * "+ ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable+", int "+stleft+", int "+stright+")");
144 crhead.outputline("void "+methodname+"("+name+"_state * "+ ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable+", int "+stleft+");");
145 craux.outputline("void "+methodname+"("+name+"_state * "+ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable+", int "+stleft+")");
148 craux.outputline("int maybe=0;");
149 final SymbolTable st = un.getRule().getSymbolTable();
150 CodeWriter cr = new StandardCodeWriter(outputaux) {
151 public SymbolTable getSymbolTable() { return st; }
153 un.generate(cr, false, false, stleft,stright, null,this);
154 craux.outputline("if (maybe) printf(\"REALLY BAD\");");
157 case MultUpdateNode.REMOVE: {
159 String methodcall="void "+methodname+"("+name+"_state * "+ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable;
160 for(int j=0;j<r.numQuantifiers();j++) {
161 Quantifier q=r.getQuantifier(j);
162 if (q instanceof SetQuantifier) {
163 SetQuantifier sq=(SetQuantifier) q;
164 methodcall+=","+sq.getVar().getType().getGenerateType().getSafeSymbol()+" "+sq.getVar().getSafeSymbol();
165 } else if (q instanceof RelationQuantifier) {
166 RelationQuantifier rq=(RelationQuantifier) q;
168 methodcall+=","+rq.x.getType().getGenerateType().getSafeSymbol()+" "+rq.x.getSafeSymbol();
169 methodcall+=","+rq.y.getType().getGenerateType().getSafeSymbol()+" "+rq.y.getSafeSymbol();
170 } else if (q instanceof ForQuantifier) {
171 ForQuantifier fq=(ForQuantifier) q;
172 methodcall+=",int "+fq.getVar().getSafeSymbol();
176 crhead.outputline(methodcall+";");
177 craux.outputline(methodcall);
179 craux.outputline("int maybe=0;");
180 final SymbolTable st2 = un.getRule().getSymbolTable();
181 CodeWriter cr2 = new StandardCodeWriter(outputaux) {
182 public SymbolTable getSymbolTable() { return st2; }
184 un.generate(cr2, true, false, null,null, null,this);
185 craux.outputline("if (maybe) printf(\"REALLY BAD\");");
189 case MultUpdateNode.MODIFY: {
191 String methodcall="void "+methodname+"("+name+"_state * "+ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable;
192 for(int j=0;j<r.numQuantifiers();j++) {
193 Quantifier q=r.getQuantifier(j);
194 if (q instanceof SetQuantifier) {
195 SetQuantifier sq=(SetQuantifier) q;
196 methodcall+=","+sq.getVar().getType().getGenerateType().getSafeSymbol()+" "+sq.getVar().getSafeSymbol();
197 } else if (q instanceof RelationQuantifier) {
198 RelationQuantifier rq=(RelationQuantifier) q;
200 methodcall+=", "+rq.x.getType().getGenerateType().getSafeSymbol()+" "+rq.x.getSafeSymbol();
201 methodcall+=", "+rq.y.getType().getGenerateType().getSafeSymbol()+" "+rq.y.getSafeSymbol();
202 } else if (q instanceof ForQuantifier) {
203 ForQuantifier fq=(ForQuantifier) q;
204 methodcall+=", int "+fq.getVar().getSafeSymbol();
207 methodcall+=", "+stleft+", "+stright+", "+stnew;
209 crhead.outputline(methodcall+";");
210 craux.outputline(methodcall);
212 craux.outputline("int maybe=0;");
213 final SymbolTable st2 = un.getRule().getSymbolTable();
214 CodeWriter cr2 = new StandardCodeWriter(outputaux) {
215 public SymbolTable getSymbolTable() { return st2; }
217 un.generate(cr2, false, true, stleft, stright, stnew, this);
218 craux.outputline("if (maybe) printf(\"REALLY BAD\");");
224 throw new Error("Nonimplement Update");
230 private void generate_call() {
231 CodeWriter cr = new StandardCodeWriter(outputrepair);
232 VarDescriptor vdstate=VarDescriptor.makeNew("repairstate");
233 cr.outputline(name+"_state * "+vdstate.getSafeSymbol()+"=new "+name+"_state();");
234 Iterator globals=state.stGlobals.descriptors();
235 while (globals.hasNext()) {
236 VarDescriptor vd=(VarDescriptor) globals.next();
237 cr.outputline(vdstate.getSafeSymbol()+"->"+vd.getSafeSymbol()+"=("+vd.getType().getGenerateType().getSafeSymbol()+")"+vd.getSafeSymbol()+";");
239 /* Insert repair here */
240 cr.outputline(vdstate.getSafeSymbol()+"->doanalysis();");
241 globals=state.stGlobals.descriptors();
242 while (globals.hasNext()) {
243 VarDescriptor vd=(VarDescriptor) globals.next();
244 cr.outputline("*(("+vd.getType().getGenerateType().getSafeSymbol()+"*) &"+vd.getSafeSymbol()+")="+vdstate.getSafeSymbol()+"->"+vd.getSafeSymbol()+";");
246 cr.outputline("delete "+vdstate.getSafeSymbol()+";");
249 private void generate_tokentable() {
250 CodeWriter cr = new StandardCodeWriter(outputrepair);
251 Iterator tokens = TokenLiteralExpr.tokens.keySet().iterator();
254 cr.outputline("// Token values");
257 while (tokens.hasNext()) {
258 Object token = tokens.next();
259 cr.outputline("// " + token.toString() + " = " + TokenLiteralExpr.tokens.get(token).toString());
266 private void generate_stateobject() {
267 CodeWriter crhead = new StandardCodeWriter(outputhead);
268 crhead.outputline("class "+name+"_state {");
269 crhead.outputline("public:");
270 Iterator globals=state.stGlobals.descriptors();
271 while (globals.hasNext()) {
272 VarDescriptor vd=(VarDescriptor) globals.next();
273 crhead.outputline(vd.getType().getGenerateType().getSafeSymbol()+" "+vd.getSafeSymbol()+";");
275 crhead.outputline("void computesizes(int *,int **);");
276 crhead.outputline("void recomputesizes();");
279 private void generate_computesizes() {
280 int max=TypeDescriptor.counter;
281 TypeDescriptor[] tdarray=new TypeDescriptor[max];
282 for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
283 TypeDescriptor ttd=(TypeDescriptor)it.next();
284 tdarray[ttd.getId()]=ttd;
286 final SymbolTable st = state.stGlobals;
287 CodeWriter cr = new StandardCodeWriter(outputaux) {
288 public SymbolTable getSymbolTable() { return st; }
291 cr.outputline("void "+name+"_state::computesizes(int *sizearray,int **numele) {");
292 for(int i=0;i<max;i++) {
293 TypeDescriptor td=tdarray[i];
294 Expr size=td.getSizeExpr();
295 VarDescriptor vd=VarDescriptor.makeNew("size");
296 size.generate(cr,vd);
297 cr.outputline("sizearray["+i+"]="+vd.getSafeSymbol()+";");
299 for(int i=0;i<max;i++) {
300 TypeDescriptor td=tdarray[i];
301 if (td instanceof StructureTypeDescriptor) {
302 StructureTypeDescriptor std=(StructureTypeDescriptor) td;
303 for(int j=0;j<std.fieldlist.size();j++) {
304 FieldDescriptor fd=(FieldDescriptor)std.fieldlist.get(j);
305 if (fd instanceof ArrayDescriptor) {
306 ArrayDescriptor ad=(ArrayDescriptor)fd;
307 Expr index=ad.getIndexBound();
308 VarDescriptor vd=VarDescriptor.makeNew("index");
309 index.generate(cr,vd);
310 cr.outputline("numele["+i+"]["+j+"]="+vd.getSafeSymbol()+";");
318 private void generate_recomputesizes() {
319 int max=TypeDescriptor.counter;
320 TypeDescriptor[] tdarray=new TypeDescriptor[max];
321 for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
322 TypeDescriptor ttd=(TypeDescriptor)it.next();
323 tdarray[ttd.getId()]=ttd;
325 final SymbolTable st = state.stGlobals;
326 CodeWriter cr = new StandardCodeWriter(outputaux) {
327 public SymbolTable getSymbolTable() { return st; }
329 cr.outputline("void "+name+"_state::recomputesizes() {");
330 for(int i=0;i<max;i++) {
331 TypeDescriptor td=tdarray[i];
332 Expr size=td.getSizeExpr();
333 VarDescriptor vd=VarDescriptor.makeNew("size");
334 size.generate(cr,vd);
336 for(int i=0;i<max;i++) {
337 TypeDescriptor td=tdarray[i];
338 if (td instanceof StructureTypeDescriptor) {
339 StructureTypeDescriptor std=(StructureTypeDescriptor) td;
340 for(int j=0;j<std.fieldlist.size();j++) {
341 FieldDescriptor fd=(FieldDescriptor)std.fieldlist.get(j);
342 if (fd instanceof ArrayDescriptor) {
343 ArrayDescriptor ad=(ArrayDescriptor)fd;
344 Expr index=ad.getIndexBound();
345 VarDescriptor vd=VarDescriptor.makeNew("index");
346 index.generate(cr,vd);
355 private void generate_hashtables() {
356 CodeWriter craux = new StandardCodeWriter(outputaux);
357 CodeWriter crhead = new StandardCodeWriter(outputhead);
358 crhead.outputline("#ifndef "+name+"_h");
359 crhead.outputline("#define "+name+"_h");
360 crhead.outputline("#include \"SimpleHash.h\"");
361 crhead.outputline("extern \"C\" {");
362 crhead.outputline("#include \"instrument.h\"");
363 crhead.outputline("}");
364 crhead.outputline("#include <stdio.h>");
365 crhead.outputline("#include <stdlib.h>");
366 crhead.outputline("class "+name+" {");
367 crhead.outputline("public:");
368 crhead.outputline(name+"();");
369 crhead.outputline("~"+name+"();");
370 craux.outputline("#include \""+headername+"\"");
371 craux.outputline("#include \"size.h\"");
373 craux.outputline(name+"::"+name+"() {");
374 craux.outputline("// creating hashtables ");
377 Iterator sets = state.stSets.descriptors();
379 /* first pass create all the hash tables */
380 while (sets.hasNext()) {
381 SetDescriptor set = (SetDescriptor) sets.next();
382 crhead.outputline("SimpleHash* " + set.getSafeSymbol() + "_hash;");
383 craux.outputline(set.getSafeSymbol() + "_hash = new SimpleHash();");
386 /* second pass build relationships between hashtables */
387 sets = state.stSets.descriptors();
389 while (sets.hasNext()) {
390 SetDescriptor set = (SetDescriptor) sets.next();
391 Iterator subsets = set.subsets();
393 while (subsets.hasNext()) {
394 SetDescriptor subset = (SetDescriptor) subsets.next();
395 craux.outputline(subset.getSafeSymbol() + "_hash->addParent(" + set.getSafeSymbol() + "_hash);");
399 /* build relations */
400 Iterator relations = state.stRelations.descriptors();
402 /* first pass create all the hash tables */
403 while (relations.hasNext()) {
404 RelationDescriptor relation = (RelationDescriptor) relations.next();
406 if (relation.testUsage(RelationDescriptor.IMAGE)) {
407 crhead.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hash;");
408 craux.outputline(relation.getSafeSymbol() + "_hash = new SimpleHash();");
411 if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
412 crhead.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hashinv;");
413 craux.outputline(relation.getSafeSymbol() + "_hashinv = new SimpleHash();");
417 craux.outputline("}");
418 crhead.outputline("};");
419 craux.outputline(name+"::~"+name+"() {");
420 craux.outputline("// deleting hashtables");
422 /* build destructor */
423 sets = state.stSets.descriptors();
425 /* first pass create all the hash tables */
426 while (sets.hasNext()) {
427 SetDescriptor set = (SetDescriptor) sets.next();
428 craux.outputline("delete "+set.getSafeSymbol() + "_hash;");
431 /* destroy relations */
432 relations = state.stRelations.descriptors();
434 /* first pass create all the hash tables */
435 while (relations.hasNext()) {
436 RelationDescriptor relation = (RelationDescriptor) relations.next();
438 if (relation.testUsage(RelationDescriptor.IMAGE)) {
439 craux.outputline("delete "+relation.getSafeSymbol() + "_hash;");
442 if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
443 craux.outputline("delete " + relation.getSafeSymbol() + "_hashinv;");
446 craux.outputline("}");
449 private void generate_start() {
450 CodeWriter crhead = new StandardCodeWriter(outputhead);
451 CodeWriter craux = new StandardCodeWriter(outputaux);
452 oldmodel=VarDescriptor.makeNew("oldmodel");
453 newmodel=VarDescriptor.makeNew("newmodel");
454 worklist=VarDescriptor.makeNew("worklist");
455 goodflag=VarDescriptor.makeNew("goodflag");
456 repairtable=VarDescriptor.makeNew("repairtable");
457 crhead.outputline("void doanalysis();");
458 craux.outputline("void "+name +"_state::doanalysis()");
460 craux.outputline("int highmark;");
461 craux.outputline("initializestack(&highmark);");
462 craux.outputline("typeobject *typeobject1=gettypeobject();");
463 craux.outputline("typeobject1->computesizes(this);");
464 craux.outputline("recomputesizes();");
465 craux.outputline(name+ " * "+oldmodel.getSafeSymbol()+"=0;");
466 craux.outputline("WorkList * "+worklist.getSafeSymbol()+" = new WorkList();");
467 craux.outputline("RepairHash * "+repairtable.getSafeSymbol()+"=0;");
468 craux.outputline("while (1)");
470 craux.outputline(name+ " * "+newmodel.getSafeSymbol()+"=new "+name+"();");
473 private void generate_teardown() {
474 CodeWriter cr = new StandardCodeWriter(outputaux);
478 private void generate_print() {
480 final SymbolTable st = new SymbolTable();
482 CodeWriter cr = new StandardCodeWriter(outputaux) {
483 public SymbolTable getSymbolTable() { return st; }
486 cr.outputline("// printing sets!");
487 cr.outputline("printf(\"\\n\\nPRINTING SETS AND RELATIONS\\n\");");
489 Iterator setiterator = state.stSets.descriptors();
490 while (setiterator.hasNext()) {
491 SetDescriptor sd = (SetDescriptor) setiterator.next();
492 if (sd.getSymbol().equals("int") || sd.getSymbol().equals("token")) {
496 String setname = sd.getSafeSymbol();
499 cr.outputline("// printing set " + setname);
500 cr.outputline("printf(\"\\nPrinting set " + sd.getSymbol() + " - %d elements \\n\", " + setname + "_hash->count());");
501 cr.outputline("SimpleIterator __setiterator;");
502 cr.outputline("" + setname + "_hash->iterator(__setiterator);");
503 cr.outputline("while (__setiterator.hasNext())");
505 cr.outputline("int __setval = (int) __setiterator.next();");
507 TypeDescriptor td = sd.getType();
508 if (td instanceof StructureTypeDescriptor) {
509 StructureTypeDescriptor std = (StructureTypeDescriptor) td;
510 VarDescriptor vd = new VarDescriptor ("__setval", "__setval", td, false);
511 std.generate_printout(cr, vd);
512 } else { // Missing type descriptor or reserved type, just print int
513 cr.outputline("printf(\"<%d> \", __setval);");
521 cr.outputline("printf(\"\\n\\n------------------- END PRINTING\\n\");");
525 private void generate_rules() {
526 /* first we must sort the rules */
527 RelationDescriptor.prefix = newmodel.getSafeSymbol()+"->";
528 SetDescriptor.prefix = newmodel.getSafeSymbol()+"->";
529 System.out.println("SCC="+(mrd.numSCC()-1));
530 for(int sccindex=0;sccindex<mrd.numSCC();sccindex++) {
531 ruleset=mrd.getSCC(sccindex);
532 boolean needworklist=mrd.hasCycle(sccindex);
535 Iterator iterator_rs = ruleset.iterator();
536 while (iterator_rs.hasNext()) {
537 Rule rule = (Rule) iterator_rs.next();
539 final SymbolTable st = rule.getSymbolTable();
540 CodeWriter cr = new StandardCodeWriter(outputaux) {
541 public SymbolTable getSymbolTable() { return st; }
543 cr.outputline("// build " +escape(rule.toString()));
545 cr.outputline("int maybe=0;");
546 ListIterator quantifiers = rule.quantifiers();
547 while (quantifiers.hasNext()) {
548 Quantifier quantifier = (Quantifier) quantifiers.next();
549 quantifier.generate_open(cr);
554 rule.getGuardExpr().prettyPrint(cr);
557 /* now we have to generate the guard test */
558 VarDescriptor guardval = VarDescriptor.makeNew();
559 rule.getGuardExpr().generate(cr, guardval);
560 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
563 /* now we have to generate the inclusion code */
565 rule.getInclusion().generate(cr);
567 while (quantifiers.hasPrevious()) {
568 Quantifier quantifier = (Quantifier) quantifiers.previous();
577 CodeWriter cr2 = new StandardCodeWriter(outputaux);
579 for(Iterator initialworklist=ruleset.iterator();initialworklist.hasNext();) {
580 /** Construct initial worklist set */
581 Rule rule=(Rule)initialworklist.next();
582 cr2.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+",-1,0,0);");
585 cr2.outputline("while ("+worklist.getSafeSymbol()+"->hasMoreElements())");
587 VarDescriptor idvar=VarDescriptor.makeNew("id");
588 cr2.outputline("int "+idvar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->getid();");
590 String elseladder = "if";
592 Iterator iterator_rules = ruleset.iterator();
593 while (iterator_rules.hasNext()) {
595 Rule rule = (Rule) iterator_rules.next();
596 int dispatchid = rule.getNum();
599 final SymbolTable st = rule.getSymbolTable();
600 CodeWriter cr = new StandardCodeWriter(outputaux) {
601 public SymbolTable getSymbolTable() { return st; }
605 cr.outputline(elseladder + " ("+idvar.getSafeSymbol()+" == " + dispatchid + ")");
607 cr.outputline("int maybe=0;");
608 VarDescriptor typevar=VarDescriptor.makeNew("type");
609 VarDescriptor leftvar=VarDescriptor.makeNew("left");
610 VarDescriptor rightvar=VarDescriptor.makeNew("right");
611 cr.outputline("int "+typevar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->gettype();");
612 cr.outputline("int "+leftvar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->getlvalue();");
613 cr.outputline("int "+rightvar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->getrvalue();");
614 cr.outputline("// build " +escape(rule.toString()));
617 for (int j=0;j<rule.numQuantifiers();j++) {
618 Quantifier quantifier = rule.getQuantifier(j);
619 quantifier.generate_open(cr, typevar.getSafeSymbol(),j,leftvar.getSafeSymbol(),rightvar.getSafeSymbol());
625 rule.getGuardExpr().prettyPrint(cr);
628 /* now we have to generate the guard test */
630 VarDescriptor guardval = VarDescriptor.makeNew();
631 rule.getGuardExpr().generate(cr, guardval);
633 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
636 /* now we have to generate the inclusion code */
638 rule.getInclusion().generate(cr);
641 for (int j=0;j<rule.numQuantifiers();j++) {
645 // close startblocks generated by DotExpr memory checks
646 //DotExpr.generate_memory_endblocks(cr);
648 cr.endblock(); // end else-if WORKLIST ladder
650 elseladder = "else if";
653 cr2.outputline("else");
655 cr2.outputline("printf(\"VERY BAD !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\\n\\n\");");
656 cr2.outputline("exit(1);");
658 // end block created for worklist
659 cr2.outputline(worklist.getSafeSymbol()+"->pop();");
665 public static String escape(String s) {
667 for(int i=0;i<s.length();i++) {
677 private void generate_checks() {
679 /* do constraint checks */
680 // Vector constraints = state.vConstraints;
683 // for (int i = 0; i < constraints.size(); i++) {
684 // Constraint constraint = (Constraint) constraints.elementAt(i);
685 for (Iterator i = termination.constraintdependence.computeOrdering().iterator(); i.hasNext();) {
686 Constraint constraint = (Constraint) ((GraphNode)i.next()).getOwner();
689 final SymbolTable st = constraint.getSymbolTable();
690 CodeWriter cr = new StandardCodeWriter(outputaux);
691 cr.pushSymbolTable(constraint.getSymbolTable());
693 cr.outputline("// checking " + escape(constraint.toString()));
696 ListIterator quantifiers = constraint.quantifiers();
698 while (quantifiers.hasNext()) {
699 Quantifier quantifier = (Quantifier) quantifiers.next();
700 quantifier.generate_open(cr);
703 cr.outputline("int maybe = 0;");
705 /* now we have to generate the guard test */
707 VarDescriptor constraintboolean = VarDescriptor.makeNew("constraintboolean");
708 constraint.getLogicStatement().generate(cr, constraintboolean);
710 cr.outputline("if (maybe)");
712 cr.outputline("printf(\"maybe fail " + escape(constraint.toString()) + ". \\n\");");
713 cr.outputline("exit(1);");
716 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
718 if (!Compiler.REPAIR)
719 cr.outputline("printf(\"fail " + escape(constraint.toString()) + ". \\n\");");
722 /* Build new repair table */
723 cr.outputline("if ("+repairtable.getSafeSymbol()+")");
724 cr.outputline("delete "+repairtable.getSafeSymbol()+";");
725 cr.outputline(repairtable.getSafeSymbol()+"=new RepairHash();");
728 /* Compute cost of each repair */
729 VarDescriptor mincost=VarDescriptor.makeNew("mincost");
730 VarDescriptor mincostindex=VarDescriptor.makeNew("mincostindex");
731 DNFConstraint dnfconst=constraint.dnfconstraint;
732 if (dnfconst.size()<=1) {
733 cr.outputline("int "+mincostindex.getSafeSymbol()+"=0;");
735 if (dnfconst.size()>1) {
736 cr.outputline("int "+mincostindex.getSafeSymbol()+";");
738 for(int j=0;j<dnfconst.size();j++) {
739 Conjunction conj=dnfconst.get(j);
740 GraphNode gn=(GraphNode)termination.conjtonodemap.get(conj);
741 if (removed.contains(gn))
744 VarDescriptor costvar;
748 costvar=VarDescriptor.makeNew("cost");
749 for(int k=0;k<conj.size();k++) {
750 DNFPredicate dpred=conj.get(k);
751 Predicate p=dpred.getPredicate();
752 boolean negate=dpred.isNegated();
753 VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
754 p.generate(cr,predvalue);
756 cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
758 cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
760 cr.outputline("int "+costvar.getSafeSymbol()+"="+cost.getCost(dpred)+";");
762 cr.outputline(costvar.getSafeSymbol()+"+="+cost.getCost(dpred)+";");
766 cr.outputline("if ("+costvar.getSafeSymbol()+"<"+mincost.getSafeSymbol()+")");
768 cr.outputline(mincost.getSafeSymbol()+"="+costvar.getSafeSymbol()+";");
769 cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
772 cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
776 cr.outputline("switch("+mincostindex.getSafeSymbol()+") {");
777 for(int j=0;j<dnfconst.size();j++) {
778 Conjunction conj=dnfconst.get(j);
779 GraphNode gn=(GraphNode)termination.conjtonodemap.get(conj);
780 if (removed.contains(gn))
782 cr.outputline("case "+j+":");
783 for(int k=0;k<conj.size();k++) {
784 DNFPredicate dpred=conj.get(k);
785 Predicate p=dpred.getPredicate();
786 boolean negate=dpred.isNegated();
787 VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
788 p.generate(cr,predvalue);
790 cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
792 cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
794 if (p instanceof InclusionPredicate)
795 generateinclusionrepair(conj,dpred, cr);
796 else if (p instanceof ExprPredicate) {
797 ExprPredicate ep=(ExprPredicate)p;
798 if (ep.getType()==ExprPredicate.SIZE)
799 generatesizerepair(conj,dpred,cr);
800 else if (ep.getType()==ExprPredicate.COMPARISON)
801 generatecomparisonrepair(conj,dpred,cr);
802 } else throw new Error("Unrecognized Predicate");
806 cr.outputline("break;");
810 cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
811 cr.outputline("delete "+oldmodel.getSafeSymbol()+";");
812 cr.outputline(oldmodel.getSafeSymbol()+"="+newmodel.getSafeSymbol()+";");
813 cr.outputline("goto rebuild;"); /* Rebuild model and all */
817 while (quantifiers.hasPrevious()) {
818 Quantifier quantifier = (Quantifier) quantifiers.previous();
826 CodeWriter cr = new StandardCodeWriter(outputaux);
828 cr.outputline("if ("+repairtable.getSafeSymbol()+")");
829 cr.outputline("delete "+repairtable.getSafeSymbol()+";");
830 cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
831 cr.outputline("delete "+oldmodel.getSafeSymbol()+";");
832 cr.outputline("delete "+newmodel.getSafeSymbol()+";");
833 cr.outputline("delete "+worklist.getSafeSymbol()+";");
834 cr.outputline("resettypemap();");
835 cr.outputline("break;");
837 cr.outputline("rebuild:");
842 private MultUpdateNode getmultupdatenode(Conjunction conj, DNFPredicate dpred, int repairtype) {
843 MultUpdateNode mun=null;
844 GraphNode gn=(GraphNode) termination.conjtonodemap.get(conj);
845 for(Iterator edgeit=gn.edges();(mun==null)&&edgeit.hasNext();) {
846 GraphNode gn2=((GraphNode.Edge) edgeit.next()).getTarget();
847 TermNode tn2=(TermNode)gn2.getOwner();
848 if (tn2.getType()==TermNode.ABSTRACT) {
849 AbstractRepair ar=tn2.getAbstract();
850 if (((repairtype==-1)||(ar.getType()==repairtype))&&
851 ar.getPredicate()==dpred) {
852 for(Iterator edgeit2=gn2.edges();edgeit2.hasNext();) {
853 GraphNode gn3=((GraphNode.Edge) edgeit2.next()).getTarget();
854 if (!removed.contains(gn3)) {
855 TermNode tn3=(TermNode)gn3.getOwner();
856 if (tn3.getType()==TermNode.UPDATE) {
868 /** Generates abstract (and concrete) repair for a comparison */
870 private void generatecomparisonrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
871 MultUpdateNode munmodify=getmultupdatenode(conj,dpred,AbstractRepair.MODIFYRELATION);
872 MultUpdateNode munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMRELATION);
873 MultUpdateNode munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTORELATION);
874 ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
875 RelationDescriptor rd=(RelationDescriptor)ep.getDescriptor();
876 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
877 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
878 boolean inverted=ep.inverted();
879 boolean negated=dpred.isNegated();
880 OpExpr expr=(OpExpr)ep.expr;
881 Opcode opcode=expr.getOpcode();
882 VarDescriptor leftside=VarDescriptor.makeNew("leftside");
883 VarDescriptor rightside=VarDescriptor.makeNew("rightside");
884 VarDescriptor newvalue=VarDescriptor.makeNew("newvalue");
886 expr.getLeftExpr().generate(cr,leftside);
887 expr.getRightExpr().generate(cr,newvalue);
888 cr.outputline(rd.getRange().getType().getGenerateType().getSafeSymbol()+" "+rightside.getSafeSymbol()+";");
889 cr.outputline(rd.getSafeSymbol()+"_hash->get("+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
891 expr.getLeftExpr().generate(cr,rightside);
892 expr.getRightExpr().generate(cr,newvalue);
893 cr.outputline(rd.getDomain().getType().getGenerateType().getSafeSymbol()+" "+leftside.getSafeSymbol()+";");
894 cr.outputline(rd.getSafeSymbol()+"_hashinv->get("+leftside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
897 if (opcode==Opcode.GT) {
899 } else if (opcode==Opcode.GE) {
901 } else if (opcode==Opcode.LT) {
903 } else if (opcode==Opcode.LE) {
905 } else if (opcode==Opcode.EQ) {
907 } else if (opcode==Opcode.NE) {
910 throw new Error("Unrecognized Opcode");
913 if (opcode==Opcode.GT) {
914 cr.outputline(newvalue.getSafeSymbol()+"++;");
915 } else if (opcode==Opcode.GE) {
917 } else if (opcode==Opcode.LT) {
918 cr.outputline(newvalue.getSafeSymbol()+"--;");
919 } else if (opcode==Opcode.LE) {
921 } else if (opcode==Opcode.EQ) {
923 } else if (opcode==Opcode.NE) { /* search for FLAGNE if this is changed*/
924 cr.outputline(newvalue.getSafeSymbol()+"++;");
926 throw new Error("Unrecognized Opcode");
928 /* Do abstract repairs */
930 cr.outputline(rd.getSafeSymbol()+"_hash->remove("+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
932 cr.outputline(rd.getSafeSymbol()+"_hash->add("+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
934 cr.outputline(rd.getSafeSymbol()+"_hash->add("+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
938 cr.outputline(rd.getSafeSymbol()+"_hashinv->remove("+rightside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
940 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+newvalue.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
942 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+rightside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
945 /* Do concrete repairs */
946 if (munmodify!=null) {
947 for(int i=0;i<state.vRules.size();i++) {
948 Rule r=(Rule)state.vRules.get(i);
949 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
950 for(int j=0;j<munmodify.numUpdates();j++) {
951 UpdateNode un=munmodify.getUpdate(j);
952 if (un.getRule()==r) {
953 /* Update for rule r */
954 String name=(String)updatenames.get(un);
955 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+","+newvalue.getSafeSymbol()+");");
962 /* Start with scheduling removal */
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<munremove.numUpdates();j++) {
967 UpdateNode un=munremove.getUpdate(i);
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+");");
976 /* Now do addition */
977 UpdateNode un=munadd.getUpdate(0);
978 String name=(String)updatenames.get(un);
980 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
982 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
987 public void generatesizerepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr) {
988 ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
989 OpExpr expr=(OpExpr)ep.expr;
990 Opcode opcode=expr.getOpcode();
991 opcode=Opcode.translateOpcode(dpred.isNegated(),opcode);
993 MultUpdateNode munremove;
995 MultUpdateNode munadd;
996 if (ep.getDescriptor() instanceof RelationDescriptor) {
997 munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMRELATION);
998 munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTORELATION);
1000 munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMSET);
1001 munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTOSET);
1003 int size=ep.rightSize();
1004 VarDescriptor sizevar=VarDescriptor.makeNew("size");
1005 ((OpExpr)expr).left.generate(cr, sizevar);
1006 VarDescriptor change=VarDescriptor.makeNew("change");
1007 cr.outputline("int "+change.getSafeSymbol()+";");
1008 boolean generateadd=false;
1009 boolean generateremove=false;
1010 if (opcode==Opcode.GT) {
1011 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
1013 generateremove=false;
1014 } else if (opcode==Opcode.GE) {
1015 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1017 generateremove=false;
1018 } else if (opcode==Opcode.LT) {
1019 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size-1)+"-"+sizevar.getSafeSymbol()+";");
1021 generateremove=true;
1022 } else if (opcode==Opcode.LE) {
1023 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1025 generateremove=true;
1026 } else if (opcode==Opcode.EQ) {
1027 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1032 generateremove=true;
1033 } else if (opcode==Opcode.NE) {
1034 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
1036 generateremove=false;
1038 throw new Error("Unrecognized Opcode");
1041 // In some cases the analysis has determined that generating removes
1043 if (generateremove&&munremove==null)
1044 generateremove=false;
1046 Descriptor d=ep.getDescriptor();
1047 if (generateremove) {
1048 cr.outputline("for(;"+change.getSafeSymbol()+"<0;"+change.getSafeSymbol()+"++)");
1050 /* Find element to remove */
1051 VarDescriptor leftvar=VarDescriptor.makeNew("leftvar");
1052 VarDescriptor rightvar=VarDescriptor.makeNew("rightvar");
1053 if (d instanceof RelationDescriptor) {
1054 if (ep.inverted()) {
1055 rightvar=((ImageSetExpr)((SizeofExpr)((OpExpr)expr).left).setexpr).getVar();
1056 cr.outputline("int "+leftvar.getSafeSymbol()+";");
1057 cr.outputline(d.getSafeSymbol()+"_hashinv->get((int)"+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1059 leftvar=((ImageSetExpr)((SizeofExpr)((OpExpr)expr).left).setexpr).getVar();
1060 cr.outputline("int "+rightvar.getSafeSymbol()+"=0;");
1061 cr.outputline(d.getSafeSymbol()+"_hash->get((int)"+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+");");
1064 cr.outputline("int "+leftvar.getSafeSymbol()+"="+d.getSafeSymbol()+"_hash->firstkey();");
1066 /* Generate abstract remove instruction */
1067 if (d instanceof RelationDescriptor) {
1068 RelationDescriptor rd=(RelationDescriptor) d;
1069 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1070 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1072 cr.outputline(rd.getSafeSymbol() + "_hash->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1074 cr.outputline(rd.getSafeSymbol() + "_hashinv->remove((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1076 cr.outputline(d.getSafeSymbol() + "_hash->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1078 /* Generate concrete remove instruction */
1079 for(int i=0;i<state.vRules.size();i++) {
1080 Rule r=(Rule)state.vRules.get(i);
1081 if (r.getInclusion().getTargetDescriptors().contains(d)) {
1082 for(int j=0;j<munremove.numUpdates();j++) {
1083 UpdateNode un=munremove.getUpdate(j);
1084 if (un.getRule()==r) {
1085 /* Update for rule rule r */
1086 String name=(String)updatenames.get(un);
1087 if (d instanceof RelationDescriptor) {
1088 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
1090 cr.outputline(repairtable.getSafeSymbol()+"->addset("+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1100 cr.outputline("for(;"+change.getSafeSymbol()+">0;"+change.getSafeSymbol()+"--)");
1102 VarDescriptor newobject=VarDescriptor.makeNew("newobject");
1103 if (d instanceof RelationDescriptor) {
1104 VarDescriptor otherside=((ImageSetExpr)((SizeofExpr)((OpExpr)ep.expr).left).setexpr).vd;
1105 RelationDescriptor rd=(RelationDescriptor)d;
1106 if (sources.relsetSource(rd,!ep.inverted())) {
1108 SetDescriptor sd=sources.relgetSourceSet(rd,!ep.inverted());
1109 VarDescriptor iterator=VarDescriptor.makeNew("iterator");
1110 cr.outputline(sd.getType().getGenerateType().getSafeSymbol() +" "+newobject.getSafeSymbol()+";");
1111 cr.outputline("for("+iterator.getSafeSymbol()+"="+sd.getSafeSymbol()+"_hash->iterator();"+iterator.getSafeSymbol()+".hasNext();)");
1113 if (ep.inverted()) {
1114 cr.outputline("if !"+rd.getSafeSymbol()+"_hashinv->contains("+iterator.getSafeSymbol()+"->key(),"+otherside.getSafeSymbol()+")");
1116 cr.outputline("if !"+rd.getSafeSymbol()+"_hash->contains("+otherside.getSafeSymbol()+","+iterator.getSafeSymbol()+"->key())");
1118 cr.outputline(newobject.getSafeSymbol()+"="+iterator.getSafeSymbol()+".key();");
1119 cr.outputline(iterator.getSafeSymbol()+"->next();");
1121 } else if (sources.relallocSource(rd,!ep.inverted())) {
1122 /* Allocation Source*/
1123 sources.relgenerateSourceAlloc(cr,newobject,rd,!ep.inverted());
1124 } else throw new Error("No source for adding to Relation");
1125 if (ep.inverted()) {
1126 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1127 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1129 cr.outputline(rd.getSafeSymbol()+"_hash->add("+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1131 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1133 UpdateNode un=munadd.getUpdate(0);
1134 String name=(String)updatenames.get(un);
1135 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1137 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1138 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1140 cr.outputline(rd.getSafeSymbol()+"_hash->add("+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1142 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1143 UpdateNode un=munadd.getUpdate(0);
1144 String name=(String)updatenames.get(un);
1145 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1148 SetDescriptor sd=(SetDescriptor)d;
1149 if (sources.setSource(sd)) {
1152 SetDescriptor sourcesd=sources.getSourceSet(sd);
1153 VarDescriptor iterator=VarDescriptor.makeNew("iterator");
1154 cr.outputline(sourcesd.getType().getGenerateType().getSafeSymbol() +" "+newobject.getSafeSymbol()+";");
1155 cr.outputline("for("+iterator.getSafeSymbol()+"="+sourcesd.getSafeSymbol()+"_hash->iterator();"+iterator.getSafeSymbol()+".hasNext();)");
1157 cr.outputline("if !"+sd.getSafeSymbol()+"_hash->contains("+iterator.getSafeSymbol()+"->key())");
1158 cr.outputline(newobject.getSafeSymbol()+"="+iterator.getSafeSymbol()+".key();");
1159 cr.outputline(iterator.getSafeSymbol()+"->next();");
1161 } else if (sources.allocSource(sd)) {
1162 /* Allocation Source*/
1163 sources.generateSourceAlloc(cr,newobject,sd);
1164 } else throw new Error("No source for adding to Set");
1165 cr.outputline(sd.getSafeSymbol()+"_hash->add("+newobject.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1166 UpdateNode un=munadd.getUpdate(0);
1167 String name=(String)updatenames.get(un);
1168 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1174 public void generateinclusionrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
1175 InclusionPredicate ip=(InclusionPredicate) dpred.getPredicate();
1176 boolean negated=dpred.isNegated();
1177 MultUpdateNode mun=getmultupdatenode(conj,dpred,-1);
1178 VarDescriptor leftvar=VarDescriptor.makeNew("left");
1179 ip.expr.generate(cr, leftvar);
1182 if (ip.setexpr instanceof ImageSetExpr) {
1183 ImageSetExpr ise=(ImageSetExpr) ip.setexpr;
1184 VarDescriptor rightvar=ise.getVar();
1185 boolean inverse=ise.inverted();
1186 RelationDescriptor rd=ise.getRelation();
1187 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1188 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1191 cr.outputline(rd.getSafeSymbol() + "_hash->remove((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1193 cr.outputline(rd.getSafeSymbol() + "_hashinv->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1196 cr.outputline(rd.getSafeSymbol() + "_hash->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1198 cr.outputline(rd.getSafeSymbol() + "_hashinv->remove((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1200 for(int i=0;i<state.vRules.size();i++) {
1201 Rule r=(Rule)state.vRules.get(i);
1202 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1203 for(int j=0;j<mun.numUpdates();j++) {
1204 UpdateNode un=mun.getUpdate(i);
1205 if (un.getRule()==r) {
1206 /* Update for rule rule r */
1207 String name=(String)updatenames.get(un);
1209 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1211 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
1218 SetDescriptor sd=ip.setexpr.sd;
1219 cr.outputline(sd.getSafeSymbol() + "_hash->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1221 for(int i=0;i<state.vRules.size();i++) {
1222 Rule r=(Rule)state.vRules.get(i);
1223 if (r.getInclusion().getTargetDescriptors().contains(sd)) {
1224 for(int j=0;j<mun.numUpdates();j++) {
1225 UpdateNode un=mun.getUpdate(i);
1226 if (un.getRule()==r) {
1227 /* Update for rule rule r */
1228 String name=(String)updatenames.get(un);
1229 cr.outputline(repairtable.getSafeSymbol()+"->addset("+sd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1236 /* Generate update */
1237 if (ip.setexpr instanceof ImageSetExpr) {
1238 ImageSetExpr ise=(ImageSetExpr) ip.setexpr;
1239 VarDescriptor rightvar=ise.getVar();
1240 boolean inverse=ise.inverted();
1241 RelationDescriptor rd=ise.getRelation();
1242 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1243 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1246 cr.outputline(rd.getSafeSymbol() + "_hash->add((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1248 cr.outputline(rd.getSafeSymbol() + "_hashinv->add((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1251 cr.outputline(rd.getSafeSymbol() + "_hash->add((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1253 cr.outputline(rd.getSafeSymbol() + "_hashinv->add((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1255 UpdateNode un=mun.getUpdate(0);
1256 String name=(String)updatenames.get(un);
1258 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1260 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+");");
1263 SetDescriptor sd=ip.setexpr.sd;
1264 cr.outputline(sd.getSafeSymbol() + "_hash->add((int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1266 UpdateNode un=mun.getUpdate(0);
1267 /* Update for rule rule r */
1268 String name=(String)updatenames.get(un);
1269 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1274 public static Vector getrulelist(Descriptor d) {
1275 Vector dispatchrules = new Vector();
1276 Vector rules = State.currentState.vRules;
1278 for (int i = 0; i < rules.size(); i++) {
1279 Rule rule = (Rule) rules.elementAt(i);
1280 Set requiredsymbols = rule.getRequiredDescriptors();
1282 // #TBD#: in general this is wrong because these descriptors may contain descriptors
1283 // bound in "in?" expressions which need to be dealt with in a topologically sorted
1286 if (rule.getRequiredDescriptors().contains(d)) {
1287 dispatchrules.addElement(rule);
1290 return dispatchrules;
1293 private boolean need_compensation(Rule r) {
1294 if (!Compiler.REPAIR)
1296 GraphNode gn=(GraphNode)termination.scopefalsify.get(r);
1297 for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1298 GraphNode.Edge edge=(GraphNode.Edge)edgeit.next();
1299 GraphNode gn2=edge.getTarget();
1300 if (!removed.contains(gn2)) {
1301 TermNode tn2=(TermNode)gn2.getOwner();
1302 if (tn2.getType()==TermNode.CONSEQUENCE)
1309 private UpdateNode find_compensation(Rule r) {
1310 GraphNode gn=(GraphNode)termination.scopefalsify.get(r);
1311 for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1312 GraphNode.Edge edge=(GraphNode.Edge)edgeit.next();
1313 GraphNode gn2=edge.getTarget();
1314 if (!removed.contains(gn2)) {
1315 TermNode tn2=(TermNode)gn2.getOwner();
1316 if (tn2.getType()==TermNode.UPDATE) {
1317 MultUpdateNode mun=tn2.getUpdate();
1318 for(int i=0;i<mun.numUpdates();i++) {
1319 UpdateNode un=mun.getUpdate(i);
1320 if (un.getRule()==r)
1326 throw new Error("No Compensation Update could be found");
1329 public void generate_dispatch(CodeWriter cr, RelationDescriptor rd, String leftvar, String rightvar) {
1330 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1331 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1333 if (!(usageinvimage||usageimage)) /* not used at all*/
1336 cr.outputline("// RELATION DISPATCH ");
1337 if (Compiler.REPAIR) {
1338 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1340 cr.outputline("!"+oldmodel.getSafeSymbol() +"->"+rd.getJustSafeSymbol()+"_hash->contains("+leftvar+","+rightvar+"))");
1342 cr.outputline("!"+oldmodel.getSafeSymbol() +"->"+rd.getJustSafeSymbol()+"_hashinv->contains("+rightvar+","+leftvar+"))");
1345 /* Adding new item */
1346 /* Perform safety checks */
1347 cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
1348 cr.outputline(repairtable.getSafeSymbol()+"->containsrelation("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+"))");
1350 /* Have update to call into */
1351 VarDescriptor mdfyptr=VarDescriptor.makeNew("modifyptr");
1352 cr.outputline("int "+mdfyptr.getSafeSymbol()+"="+repairtable.getSafeSymbol()+"->getrelation2("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1355 for(int i=0;i<currentrule.numQuantifiers();i++) {
1356 if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1357 parttype=parttype+", int, int";
1359 parttype=parttype+", int";
1361 VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1362 VarDescriptor tmpptr=VarDescriptor.makeNew("tempupdateptr");
1364 String methodcall="("+funptr.getSafeSymbol()+") (this,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
1365 for(int i=0;i<currentrule.numQuantifiers();i++) {
1366 Quantifier q=currentrule.getQuantifier(i);
1367 if (q instanceof SetQuantifier) {
1368 SetQuantifier sq=(SetQuantifier) q;
1369 methodcall+=","+sq.getVar().getSafeSymbol();
1370 } else if (q instanceof RelationQuantifier) {
1371 RelationQuantifier rq=(RelationQuantifier) q;
1372 methodcall+=","+rq.x.getSafeSymbol();
1373 methodcall+=","+rq.y.getSafeSymbol();
1374 } else if (q instanceof ForQuantifier) {
1375 ForQuantifier fq=(ForQuantifier) q;
1376 methodcall+=","+fq.getVar().getSafeSymbol();
1382 cr.outputline("void *"+tmpptr.getSafeSymbol()+"=");
1383 cr.outputline("(void *) "+repairtable.getSafeSymbol()+"->getrelation("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1384 cr.outputline("if ("+mdfyptr.getSafeSymbol()+")");
1387 cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+",int,int,int)="+"(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+",int,int,int)) "+tmpptr.getSafeSymbol());
1388 cr.outputline(methodcall+leftvar+", "+rightvar+", "+mdfyptr.getSafeSymbol() +");");
1391 cr.outputline("else ");
1394 cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")="+"(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")) "+tmpptr.getSafeSymbol());
1395 cr.outputline(methodcall+");");
1398 cr.outputline("goto rebuild;");
1402 /* Build standard compensation actions */
1403 if (need_compensation(currentrule)) {
1404 UpdateNode un=find_compensation(currentrule);
1405 String name=(String)updatenames.get(un);
1406 usedupdates.add(un); /* Mark as used */
1407 String methodcall=name+"(this,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
1408 for(int i=0;i<currentrule.numQuantifiers();i++) {
1409 Quantifier q=currentrule.getQuantifier(i);
1410 if (q instanceof SetQuantifier) {
1411 SetQuantifier sq=(SetQuantifier) q;
1412 methodcall+=","+sq.getVar().getSafeSymbol();
1413 } else if (q instanceof RelationQuantifier) {
1414 RelationQuantifier rq=(RelationQuantifier) q;
1415 methodcall+=","+rq.x.getSafeSymbol();
1416 methodcall+=","+rq.y.getSafeSymbol();
1417 } else if (q instanceof ForQuantifier) {
1418 ForQuantifier fq=(ForQuantifier) q;
1419 methodcall+=","+fq.getVar().getSafeSymbol();
1423 cr.outputline(methodcall);
1424 cr.outputline("goto rebuild;");
1430 String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1431 cr.outputline("int " + addeditem + ";");
1432 if (rd.testUsage(RelationDescriptor.IMAGE)) {
1433 cr.outputline(addeditem + " = " + rd.getSafeSymbol() + "_hash->add((int)" + leftvar + ", (int)" + rightvar+ ");");
1436 if (rd.testUsage(RelationDescriptor.INVIMAGE)) {
1437 cr.outputline(addeditem + " = " + rd.getSafeSymbol() + "_hashinv->add((int)" + rightvar + ", (int)" + leftvar + ");");
1442 Vector dispatchrules = getrulelist(rd);
1444 Set toremove=new HashSet();
1445 for(int i=0;i<dispatchrules.size();i++) {
1446 Rule r=(Rule)dispatchrules.get(i);
1447 if (!ruleset.contains(r))
1450 dispatchrules.removeAll(toremove);
1451 if (dispatchrules.size() == 0) {
1452 cr.outputline("// nothing to dispatch");
1456 cr.outputline("if (" + addeditem + ")");
1459 for(int i = 0; i < dispatchrules.size(); i++) {
1460 Rule rule = (Rule) dispatchrules.elementAt(i);
1461 if (rule.getGuardExpr().getRequiredDescriptors().contains(rd)) {
1462 /* Guard depends on this relation, so we recomput everything */
1463 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+",-1,0,0);");
1465 for (int j=0;j<rule.numQuantifiers();j++) {
1466 Quantifier q=rule.getQuantifier(j);
1467 if (q.getRequiredDescriptors().contains(rd)) {
1469 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+","+j+","+leftvar+","+rightvar+");");
1479 public void generate_dispatch(CodeWriter cr, SetDescriptor sd, String setvar) {
1481 cr.outputline("// SET DISPATCH ");
1482 if (Compiler.REPAIR) {
1483 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1484 cr.outputline("!"+oldmodel.getSafeSymbol() +"->"+sd.getJustSafeSymbol()+"_hash->contains("+setvar+"))");
1486 /* Adding new item */
1487 /* Perform safety checks */
1488 cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
1489 cr.outputline(repairtable.getSafeSymbol()+"->containsset("+sd.getNum()+","+currentrule.getNum()+","+setvar+"))");
1491 /* Have update to call into */
1492 VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1494 for(int i=0;i<currentrule.numQuantifiers();i++) {
1495 if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1496 parttype=parttype+", int, int";
1498 parttype=parttype+", int";
1500 cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")=");
1501 cr.outputline("(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")) "+repairtable.getSafeSymbol()+"->getset("+sd.getNum()+","+currentrule.getNum()+","+setvar+");");
1502 String methodcall="("+funptr.getSafeSymbol()+") (this,"+oldmodel.getSafeSymbol()+","+
1503 repairtable.getSafeSymbol();
1504 for(int i=0;i<currentrule.numQuantifiers();i++) {
1505 Quantifier q=currentrule.getQuantifier(i);
1506 if (q instanceof SetQuantifier) {
1507 SetQuantifier sq=(SetQuantifier) q;
1508 methodcall+=","+sq.getVar().getSafeSymbol();
1509 } else if (q instanceof RelationQuantifier) {
1510 RelationQuantifier rq=(RelationQuantifier) q;
1511 methodcall+=","+rq.x.getSafeSymbol();
1512 methodcall+=","+rq.y.getSafeSymbol();
1513 } else if (q instanceof ForQuantifier) {
1514 ForQuantifier fq=(ForQuantifier) q;
1515 methodcall+=","+fq.getVar().getSafeSymbol();
1519 cr.outputline(methodcall);
1520 cr.outputline("goto rebuild;");
1523 /* Build standard compensation actions */
1524 if (need_compensation(currentrule)) {
1525 UpdateNode un=find_compensation(currentrule);
1526 String name=(String)updatenames.get(un);
1527 usedupdates.add(un); /* Mark as used */
1529 String methodcall=name+"(this,"+oldmodel.getSafeSymbol()+","+
1530 repairtable.getSafeSymbol();
1531 for(int i=0;i<currentrule.numQuantifiers();i++) {
1532 Quantifier q=currentrule.getQuantifier(i);
1533 if (q instanceof SetQuantifier) {
1534 SetQuantifier sq=(SetQuantifier) q;
1535 methodcall+=","+sq.getVar().getSafeSymbol();
1536 } else if (q instanceof RelationQuantifier) {
1537 RelationQuantifier rq=(RelationQuantifier) q;
1538 methodcall+=","+rq.x.getSafeSymbol();
1539 methodcall+=","+rq.y.getSafeSymbol();
1540 } else if (q instanceof ForQuantifier) {
1541 ForQuantifier fq=(ForQuantifier) q;
1542 methodcall+=","+fq.getVar().getSafeSymbol();
1546 cr.outputline(methodcall);
1547 cr.outputline("goto rebuild;");
1553 String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1554 cr.outputline("int " + addeditem + " = 1;");
1555 cr.outputline(addeditem + " = " + sd.getSafeSymbol() + "_hash->add((int)" + setvar + ", (int)" + setvar + ");");
1557 Vector dispatchrules = getrulelist(sd);
1559 Set toremove=new HashSet();
1560 for(int i=0;i<dispatchrules.size();i++) {
1561 Rule r=(Rule)dispatchrules.get(i);
1562 if (!ruleset.contains(r))
1565 dispatchrules.removeAll(toremove);
1567 if (dispatchrules.size() == 0) {
1568 cr.outputline("// nothing to dispatch");
1572 cr.outputline("if ("+addeditem+")");
1574 for(int i = 0; i < dispatchrules.size(); i++) {
1575 Rule rule = (Rule) dispatchrules.elementAt(i);
1576 if (SetDescriptor.expand(rule.getGuardExpr().getRequiredDescriptors()).contains(sd)) {
1577 /* Guard depends on this relation, so we recompute everything */
1578 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+",-1,0,0);");
1580 for (int j=0;j<rule.numQuantifiers();j++) {
1581 Quantifier q=rule.getQuantifier(j);
1582 if (SetDescriptor.expand(q.getRequiredDescriptors()).contains(sd)) {
1584 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+","+j+","+setvar+",0);");