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();
93 CodeWriter crhead = new StandardCodeWriter(this.outputhead);
94 CodeWriter craux = new StandardCodeWriter(this.outputaux);
95 crhead.outputline("};");
96 craux.outputline("}");
97 generatetypechecks(false);
98 generate_computesizes();
99 generatetypechecks(true);
100 generate_recomputesizes();
101 generatetypechecks(false);
103 StructureGenerator sg=new StructureGenerator(state,this);
105 crhead.outputline("#endif");
108 String ststate="state";
109 String stmodel="model";
110 String strepairtable="repairtable";
111 String stleft="left";
112 String stright="right";
113 String stnew="newvalue";
115 private void generate_updates() {
117 CodeWriter crhead = new StandardCodeWriter(outputhead);
118 CodeWriter craux = new StandardCodeWriter(outputaux);
120 /* Rewrite globals */
122 for (Iterator it=this.state.stGlobals.descriptors();it.hasNext();) {
123 VarDescriptor vd=(VarDescriptor)it.next();
124 craux.outputline("#define "+vd.getSafeSymbol()+" "+ststate+"->"+vd.getSafeSymbol());
127 for(Iterator it=termination.updatenodes.iterator();it.hasNext();) {
128 GraphNode gn=(GraphNode) it.next();
129 TermNode tn=(TermNode) gn.getOwner();
130 MultUpdateNode mun=tn.getUpdate();
131 boolean isrelation=(mun.getDescriptor() instanceof RelationDescriptor);
132 if (togenerate.contains(gn))
133 for (int i=0;i<mun.numUpdates();i++) {
134 UpdateNode un=mun.getUpdate(i);
135 String methodname=(String)updatenames.get(un);
138 case MultUpdateNode.ADD:
140 crhead.outputline("void "+methodname+"("+name+"_state * " +ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable+", int "+stleft+", int "+stright+");");
141 craux.outputline("void "+methodname+"("+name+"_state * "+ ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable+", int "+stleft+", int "+stright+")");
143 crhead.outputline("void "+methodname+"("+name+"_state * "+ ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable+", int "+stleft+");");
144 craux.outputline("void "+methodname+"("+name+"_state * "+ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable+", int "+stleft+")");
147 craux.outputline("int maybe=0;");
148 final SymbolTable st = un.getRule().getSymbolTable();
149 CodeWriter cr = new StandardCodeWriter(outputaux) {
150 public SymbolTable getSymbolTable() { return st; }
152 un.generate(cr, false, false, stleft,stright, null,this);
153 craux.outputline("if (maybe) printf(\"REALLY BAD\");");
156 case MultUpdateNode.REMOVE: {
158 String methodcall="void "+methodname+"("+name+"_state * "+ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable;
159 for(int j=0;j<r.numQuantifiers();j++) {
160 Quantifier q=r.getQuantifier(j);
161 if (q instanceof SetQuantifier) {
162 SetQuantifier sq=(SetQuantifier) q;
163 methodcall+=","+sq.getVar().getType().getGenerateType().getSafeSymbol()+" "+sq.getVar().getSafeSymbol();
164 } else if (q instanceof RelationQuantifier) {
165 RelationQuantifier rq=(RelationQuantifier) q;
167 methodcall+=","+rq.x.getType().getGenerateType().getSafeSymbol()+" "+rq.x.getSafeSymbol();
168 methodcall+=","+rq.y.getType().getGenerateType().getSafeSymbol()+" "+rq.y.getSafeSymbol();
169 } else if (q instanceof ForQuantifier) {
170 ForQuantifier fq=(ForQuantifier) q;
171 methodcall+=",int "+fq.getVar().getSafeSymbol();
175 crhead.outputline(methodcall+";");
176 craux.outputline(methodcall);
178 craux.outputline("int maybe=0;");
179 final SymbolTable st2 = un.getRule().getSymbolTable();
180 CodeWriter cr2 = new StandardCodeWriter(outputaux) {
181 public SymbolTable getSymbolTable() { return st2; }
183 un.generate(cr2, true, false, null,null, null,this);
184 craux.outputline("if (maybe) printf(\"REALLY BAD\");");
188 case MultUpdateNode.MODIFY: {
190 String methodcall="void "+methodname+"("+name+"_state * "+ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable;
191 for(int j=0;j<r.numQuantifiers();j++) {
192 Quantifier q=r.getQuantifier(j);
193 if (q instanceof SetQuantifier) {
194 SetQuantifier sq=(SetQuantifier) q;
195 methodcall+=","+sq.getVar().getType().getGenerateType().getSafeSymbol()+" "+sq.getVar().getSafeSymbol();
196 } else if (q instanceof RelationQuantifier) {
197 RelationQuantifier rq=(RelationQuantifier) q;
199 methodcall+=", "+rq.x.getType().getGenerateType().getSafeSymbol()+" "+rq.x.getSafeSymbol();
200 methodcall+=", "+rq.y.getType().getGenerateType().getSafeSymbol()+" "+rq.y.getSafeSymbol();
201 } else if (q instanceof ForQuantifier) {
202 ForQuantifier fq=(ForQuantifier) q;
203 methodcall+=", int "+fq.getVar().getSafeSymbol();
206 methodcall+=", "+stleft+", "+stright+", "+stnew;
208 crhead.outputline(methodcall+";");
209 craux.outputline(methodcall);
211 craux.outputline("int maybe=0;");
212 final SymbolTable st2 = un.getRule().getSymbolTable();
213 CodeWriter cr2 = new StandardCodeWriter(outputaux) {
214 public SymbolTable getSymbolTable() { return st2; }
216 un.generate(cr2, false, true, stleft, stright, stnew, this);
217 craux.outputline("if (maybe) printf(\"REALLY BAD\");");
223 throw new Error("Nonimplement Update");
229 private void generate_call() {
230 CodeWriter cr = new StandardCodeWriter(outputrepair);
231 VarDescriptor vdstate=VarDescriptor.makeNew("repairstate");
232 cr.outputline(name+"_state * "+vdstate.getSafeSymbol()+"=new "+name+"_state();");
233 Iterator globals=state.stGlobals.descriptors();
234 while (globals.hasNext()) {
235 VarDescriptor vd=(VarDescriptor) globals.next();
236 cr.outputline(vdstate.getSafeSymbol()+"->"+vd.getSafeSymbol()+"=("+vd.getType().getGenerateType().getSafeSymbol()+")"+vd.getSafeSymbol()+";");
238 /* Insert repair here */
239 cr.outputline(vdstate.getSafeSymbol()+"->doanalysis();");
240 globals=state.stGlobals.descriptors();
241 while (globals.hasNext()) {
242 VarDescriptor vd=(VarDescriptor) globals.next();
243 cr.outputline("*(("+vd.getType().getGenerateType().getSafeSymbol()+"*) &"+vd.getSafeSymbol()+")="+vdstate.getSafeSymbol()+"->"+vd.getSafeSymbol()+";");
245 cr.outputline("delete "+vdstate.getSafeSymbol()+";");
248 private void generate_tokentable() {
249 CodeWriter cr = new StandardCodeWriter(outputrepair);
250 Iterator tokens = TokenLiteralExpr.tokens.keySet().iterator();
253 cr.outputline("// Token values");
256 while (tokens.hasNext()) {
257 Object token = tokens.next();
258 cr.outputline("// " + token.toString() + " = " + TokenLiteralExpr.tokens.get(token).toString());
265 private void generate_stateobject() {
266 CodeWriter crhead = new StandardCodeWriter(outputhead);
267 crhead.outputline("class "+name+"_state {");
268 crhead.outputline("public:");
269 Iterator globals=state.stGlobals.descriptors();
270 while (globals.hasNext()) {
271 VarDescriptor vd=(VarDescriptor) globals.next();
272 crhead.outputline(vd.getType().getGenerateType().getSafeSymbol()+" "+vd.getSafeSymbol()+";");
274 crhead.outputline("void computesizes(int *,int **);");
275 crhead.outputline("void recomputesizes();");
278 private void generate_computesizes() {
279 int max=TypeDescriptor.counter;
280 TypeDescriptor[] tdarray=new TypeDescriptor[max];
281 for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
282 TypeDescriptor ttd=(TypeDescriptor)it.next();
283 tdarray[ttd.getId()]=ttd;
285 final SymbolTable st = state.stGlobals;
286 CodeWriter cr = new StandardCodeWriter(outputaux) {
287 public SymbolTable getSymbolTable() { return st; }
290 cr.outputline("void "+name+"_state::computesizes(int *sizearray,int **numele) {");
291 for(int i=0;i<max;i++) {
292 TypeDescriptor td=tdarray[i];
293 Expr size=td.getSizeExpr();
294 VarDescriptor vd=VarDescriptor.makeNew("size");
295 size.generate(cr,vd);
296 cr.outputline("sizearray["+i+"]="+vd.getSafeSymbol()+";");
298 for(int i=0;i<max;i++) {
299 TypeDescriptor td=tdarray[i];
300 if (td instanceof StructureTypeDescriptor) {
301 StructureTypeDescriptor std=(StructureTypeDescriptor) td;
302 for(int j=0;j<std.fieldlist.size();j++) {
303 FieldDescriptor fd=(FieldDescriptor)std.fieldlist.get(j);
304 if (fd instanceof ArrayDescriptor) {
305 ArrayDescriptor ad=(ArrayDescriptor)fd;
306 Expr index=ad.getIndexBound();
307 VarDescriptor vd=VarDescriptor.makeNew("index");
308 index.generate(cr,vd);
309 cr.outputline("numele["+i+"]["+j+"]="+vd.getSafeSymbol()+";");
317 private void generate_recomputesizes() {
318 int max=TypeDescriptor.counter;
319 TypeDescriptor[] tdarray=new TypeDescriptor[max];
320 for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
321 TypeDescriptor ttd=(TypeDescriptor)it.next();
322 tdarray[ttd.getId()]=ttd;
324 final SymbolTable st = state.stGlobals;
325 CodeWriter cr = new StandardCodeWriter(outputaux) {
326 public SymbolTable getSymbolTable() { return st; }
328 cr.outputline("void "+name+"_state::recomputesizes() {");
329 for(int i=0;i<max;i++) {
330 TypeDescriptor td=tdarray[i];
331 Expr size=td.getSizeExpr();
332 VarDescriptor vd=VarDescriptor.makeNew("size");
333 size.generate(cr,vd);
335 for(int i=0;i<max;i++) {
336 TypeDescriptor td=tdarray[i];
337 if (td instanceof StructureTypeDescriptor) {
338 StructureTypeDescriptor std=(StructureTypeDescriptor) td;
339 for(int j=0;j<std.fieldlist.size();j++) {
340 FieldDescriptor fd=(FieldDescriptor)std.fieldlist.get(j);
341 if (fd instanceof ArrayDescriptor) {
342 ArrayDescriptor ad=(ArrayDescriptor)fd;
343 Expr index=ad.getIndexBound();
344 VarDescriptor vd=VarDescriptor.makeNew("index");
345 index.generate(cr,vd);
354 private void generate_hashtables() {
355 CodeWriter craux = new StandardCodeWriter(outputaux);
356 CodeWriter crhead = new StandardCodeWriter(outputhead);
357 crhead.outputline("#ifndef "+name+"_h");
358 crhead.outputline("#define "+name+"_h");
359 crhead.outputline("#include \"SimpleHash.h\"");
360 crhead.outputline("extern \"C\" {");
361 crhead.outputline("#include \"instrument.h\"");
362 crhead.outputline("}");
363 crhead.outputline("#include <stdio.h>");
364 crhead.outputline("#include <stdlib.h>");
365 crhead.outputline("class "+name+" {");
366 crhead.outputline("public:");
367 crhead.outputline(name+"();");
368 crhead.outputline("~"+name+"();");
369 craux.outputline("#include \""+headername+"\"");
370 craux.outputline("#include \"size.h\"");
372 craux.outputline(name+"::"+name+"() {");
373 craux.outputline("// creating hashtables ");
376 Iterator sets = state.stSets.descriptors();
378 /* first pass create all the hash tables */
379 while (sets.hasNext()) {
380 SetDescriptor set = (SetDescriptor) sets.next();
381 crhead.outputline("SimpleHash* " + set.getSafeSymbol() + "_hash;");
382 craux.outputline(set.getSafeSymbol() + "_hash = new SimpleHash();");
385 /* second pass build relationships between hashtables */
386 sets = state.stSets.descriptors();
388 while (sets.hasNext()) {
389 SetDescriptor set = (SetDescriptor) sets.next();
390 Iterator subsets = set.subsets();
392 while (subsets.hasNext()) {
393 SetDescriptor subset = (SetDescriptor) subsets.next();
394 craux.outputline(subset.getSafeSymbol() + "_hash->addParent(" + set.getSafeSymbol() + "_hash);");
398 /* build relations */
399 Iterator relations = state.stRelations.descriptors();
401 /* first pass create all the hash tables */
402 while (relations.hasNext()) {
403 RelationDescriptor relation = (RelationDescriptor) relations.next();
405 if (relation.testUsage(RelationDescriptor.IMAGE)) {
406 crhead.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hash;");
407 craux.outputline(relation.getSafeSymbol() + "_hash = new SimpleHash();");
410 if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
411 crhead.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hashinv;");
412 craux.outputline(relation.getSafeSymbol() + "_hashinv = new SimpleHash();");
416 craux.outputline("}");
417 crhead.outputline("};");
418 craux.outputline(name+"::~"+name+"() {");
419 craux.outputline("// deleting hashtables");
421 /* build destructor */
422 sets = state.stSets.descriptors();
424 /* first pass create all the hash tables */
425 while (sets.hasNext()) {
426 SetDescriptor set = (SetDescriptor) sets.next();
427 craux.outputline("delete "+set.getSafeSymbol() + "_hash;");
430 /* destroy relations */
431 relations = state.stRelations.descriptors();
433 /* first pass create all the hash tables */
434 while (relations.hasNext()) {
435 RelationDescriptor relation = (RelationDescriptor) relations.next();
437 if (relation.testUsage(RelationDescriptor.IMAGE)) {
438 craux.outputline("delete "+relation.getSafeSymbol() + "_hash;");
441 if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
442 craux.outputline("delete " + relation.getSafeSymbol() + "_hashinv;");
445 craux.outputline("}");
448 private void generate_start() {
449 CodeWriter crhead = new StandardCodeWriter(outputhead);
450 CodeWriter craux = new StandardCodeWriter(outputaux);
451 oldmodel=VarDescriptor.makeNew("oldmodel");
452 newmodel=VarDescriptor.makeNew("newmodel");
453 worklist=VarDescriptor.makeNew("worklist");
454 goodflag=VarDescriptor.makeNew("goodflag");
455 repairtable=VarDescriptor.makeNew("repairtable");
456 crhead.outputline("void doanalysis();");
457 craux.outputline("void "+name +"_state::doanalysis()");
459 craux.outputline("int highmark;");
460 craux.outputline("initializestack(&highmark);");
461 craux.outputline("typeobject *typeobject1=gettypeobject();");
462 craux.outputline("typeobject1->computesizes(this);");
463 craux.outputline("recomputesizes();");
464 craux.outputline(name+ " * "+oldmodel.getSafeSymbol()+"=0;");
465 craux.outputline("WorkList * "+worklist.getSafeSymbol()+" = new WorkList();");
466 craux.outputline("RepairHash * "+repairtable.getSafeSymbol()+"=0;");
467 craux.outputline("while (1)");
469 craux.outputline(name+ " * "+newmodel.getSafeSymbol()+"=new "+name+"();");
472 private void generate_teardown() {
473 CodeWriter cr = new StandardCodeWriter(outputaux);
478 private void generate_rules() {
479 /* first we must sort the rules */
480 RelationDescriptor.prefix = newmodel.getSafeSymbol()+"->";
481 SetDescriptor.prefix = newmodel.getSafeSymbol()+"->";
482 System.out.println("SCC="+(mrd.numSCC()-1));
483 for(int sccindex=0;sccindex<mrd.numSCC();sccindex++) {
484 ruleset=mrd.getSCC(sccindex);
485 boolean needworklist=mrd.hasCycle(sccindex);
488 Iterator iterator_rs = ruleset.iterator();
489 while (iterator_rs.hasNext()) {
490 Rule rule = (Rule) iterator_rs.next();
492 final SymbolTable st = rule.getSymbolTable();
493 CodeWriter cr = new StandardCodeWriter(outputaux) {
494 public SymbolTable getSymbolTable() { return st; }
496 cr.outputline("// build " +escape(rule.toString()));
498 cr.outputline("int maybe=0;");
499 ListIterator quantifiers = rule.quantifiers();
500 while (quantifiers.hasNext()) {
501 Quantifier quantifier = (Quantifier) quantifiers.next();
502 quantifier.generate_open(cr);
507 rule.getGuardExpr().prettyPrint(cr);
510 /* now we have to generate the guard test */
511 VarDescriptor guardval = VarDescriptor.makeNew();
512 rule.getGuardExpr().generate(cr, guardval);
513 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
516 /* now we have to generate the inclusion code */
518 rule.getInclusion().generate(cr);
520 while (quantifiers.hasPrevious()) {
521 Quantifier quantifier = (Quantifier) quantifiers.previous();
530 CodeWriter cr2 = new StandardCodeWriter(outputaux);
532 for(Iterator initialworklist=ruleset.iterator();initialworklist.hasNext();) {
533 /** Construct initial worklist set */
534 Rule rule=(Rule)initialworklist.next();
535 cr2.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+",-1,0,0);");
538 cr2.outputline("while ("+worklist.getSafeSymbol()+"->hasMoreElements())");
540 VarDescriptor idvar=VarDescriptor.makeNew("id");
541 cr2.outputline("int "+idvar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->getid();");
543 String elseladder = "if";
545 Iterator iterator_rules = ruleset.iterator();
546 while (iterator_rules.hasNext()) {
548 Rule rule = (Rule) iterator_rules.next();
549 int dispatchid = rule.getNum();
552 final SymbolTable st = rule.getSymbolTable();
553 CodeWriter cr = new StandardCodeWriter(outputaux) {
554 public SymbolTable getSymbolTable() { return st; }
558 cr.outputline(elseladder + " ("+idvar.getSafeSymbol()+" == " + dispatchid + ")");
560 cr.outputline("int maybe=0;");
561 VarDescriptor typevar=VarDescriptor.makeNew("type");
562 VarDescriptor leftvar=VarDescriptor.makeNew("left");
563 VarDescriptor rightvar=VarDescriptor.makeNew("right");
564 cr.outputline("int "+typevar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->gettype();");
565 cr.outputline("int "+leftvar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->getlvalue();");
566 cr.outputline("int "+rightvar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->getrvalue();");
567 cr.outputline("// build " +escape(rule.toString()));
570 for (int j=0;j<rule.numQuantifiers();j++) {
571 Quantifier quantifier = rule.getQuantifier(j);
572 quantifier.generate_open(cr, typevar.getSafeSymbol(),j,leftvar.getSafeSymbol(),rightvar.getSafeSymbol());
578 rule.getGuardExpr().prettyPrint(cr);
581 /* now we have to generate the guard test */
583 VarDescriptor guardval = VarDescriptor.makeNew();
584 rule.getGuardExpr().generate(cr, guardval);
586 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
589 /* now we have to generate the inclusion code */
591 rule.getInclusion().generate(cr);
594 for (int j=0;j<rule.numQuantifiers();j++) {
598 // close startblocks generated by DotExpr memory checks
599 //DotExpr.generate_memory_endblocks(cr);
601 cr.endblock(); // end else-if WORKLIST ladder
603 elseladder = "else if";
606 cr2.outputline("else");
608 cr2.outputline("printf(\"VERY BAD !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\\n\\n\");");
609 cr2.outputline("exit(1);");
611 // end block created for worklist
612 cr2.outputline(worklist.getSafeSymbol()+"->pop();");
618 public static String escape(String s) {
620 for(int i=0;i<s.length();i++) {
630 private void generate_checks() {
632 /* do constraint checks */
633 // Vector constraints = state.vConstraints;
636 // for (int i = 0; i < constraints.size(); i++) {
637 // Constraint constraint = (Constraint) constraints.elementAt(i);
638 for (Iterator i = termination.constraintdependence.computeOrdering().iterator(); i.hasNext();) {
639 Constraint constraint = (Constraint) ((GraphNode)i.next()).getOwner();
642 final SymbolTable st = constraint.getSymbolTable();
643 CodeWriter cr = new StandardCodeWriter(outputaux);
644 cr.pushSymbolTable(constraint.getSymbolTable());
646 cr.outputline("// checking " + escape(constraint.toString()));
649 ListIterator quantifiers = constraint.quantifiers();
651 while (quantifiers.hasNext()) {
652 Quantifier quantifier = (Quantifier) quantifiers.next();
653 quantifier.generate_open(cr);
656 cr.outputline("int maybe = 0;");
658 /* now we have to generate the guard test */
660 VarDescriptor constraintboolean = VarDescriptor.makeNew("constraintboolean");
661 constraint.getLogicStatement().generate(cr, constraintboolean);
663 cr.outputline("if (maybe)");
665 cr.outputline("printf(\"maybe fail " + escape(constraint.toString()) + ". \\n\");");
666 cr.outputline("exit(1);");
669 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
671 if (!Compiler.REPAIR)
672 cr.outputline("printf(\"fail " + escape(constraint.toString()) + ". \\n\");");
675 /* Build new repair table */
676 cr.outputline("if ("+repairtable.getSafeSymbol()+")");
677 cr.outputline("delete "+repairtable.getSafeSymbol()+";");
678 cr.outputline(repairtable.getSafeSymbol()+"=new RepairHash();");
681 /* Compute cost of each repair */
682 VarDescriptor mincost=VarDescriptor.makeNew("mincost");
683 VarDescriptor mincostindex=VarDescriptor.makeNew("mincostindex");
684 DNFConstraint dnfconst=constraint.dnfconstraint;
685 if (dnfconst.size()<=1) {
686 cr.outputline("int "+mincostindex.getSafeSymbol()+"=0;");
688 if (dnfconst.size()>1) {
689 cr.outputline("int "+mincostindex.getSafeSymbol()+";");
691 for(int j=0;j<dnfconst.size();j++) {
692 Conjunction conj=dnfconst.get(j);
693 GraphNode gn=(GraphNode)termination.conjtonodemap.get(conj);
694 if (removed.contains(gn))
697 VarDescriptor costvar;
701 costvar=VarDescriptor.makeNew("cost");
702 for(int k=0;k<conj.size();k++) {
703 DNFPredicate dpred=conj.get(k);
704 Predicate p=dpred.getPredicate();
705 boolean negate=dpred.isNegated();
706 VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
707 p.generate(cr,predvalue);
709 cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
711 cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
713 cr.outputline("int "+costvar.getSafeSymbol()+"="+cost.getCost(dpred)+";");
715 cr.outputline(costvar.getSafeSymbol()+"+="+cost.getCost(dpred)+";");
719 cr.outputline("if ("+costvar.getSafeSymbol()+"<"+mincost.getSafeSymbol()+")");
721 cr.outputline(mincost.getSafeSymbol()+"="+costvar.getSafeSymbol()+";");
722 cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
725 cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
729 cr.outputline("switch("+mincostindex.getSafeSymbol()+") {");
730 for(int j=0;j<dnfconst.size();j++) {
731 Conjunction conj=dnfconst.get(j);
732 GraphNode gn=(GraphNode)termination.conjtonodemap.get(conj);
733 if (removed.contains(gn))
735 cr.outputline("case "+j+":");
736 for(int k=0;k<conj.size();k++) {
737 DNFPredicate dpred=conj.get(k);
738 Predicate p=dpred.getPredicate();
739 boolean negate=dpred.isNegated();
740 VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
741 p.generate(cr,predvalue);
743 cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
745 cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
747 if (p instanceof InclusionPredicate)
748 generateinclusionrepair(conj,dpred, cr);
749 else if (p instanceof ExprPredicate) {
750 ExprPredicate ep=(ExprPredicate)p;
751 if (ep.getType()==ExprPredicate.SIZE)
752 generatesizerepair(conj,dpred,cr);
753 else if (ep.getType()==ExprPredicate.COMPARISON)
754 generatecomparisonrepair(conj,dpred,cr);
755 } else throw new Error("Unrecognized Predicate");
759 cr.outputline("break;");
763 cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
764 cr.outputline("delete "+oldmodel.getSafeSymbol()+";");
765 cr.outputline(oldmodel.getSafeSymbol()+"="+newmodel.getSafeSymbol()+";");
766 cr.outputline("goto rebuild;"); /* Rebuild model and all */
770 while (quantifiers.hasPrevious()) {
771 Quantifier quantifier = (Quantifier) quantifiers.previous();
779 CodeWriter cr = new StandardCodeWriter(outputaux);
781 cr.outputline("if ("+repairtable.getSafeSymbol()+")");
782 cr.outputline("delete "+repairtable.getSafeSymbol()+";");
783 cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
784 cr.outputline("delete "+oldmodel.getSafeSymbol()+";");
785 cr.outputline("delete "+newmodel.getSafeSymbol()+";");
786 cr.outputline("delete "+worklist.getSafeSymbol()+";");
787 cr.outputline("resettypemap();");
788 cr.outputline("break;");
790 cr.outputline("rebuild:");
795 private MultUpdateNode getmultupdatenode(Conjunction conj, DNFPredicate dpred, int repairtype) {
796 MultUpdateNode mun=null;
797 GraphNode gn=(GraphNode) termination.conjtonodemap.get(conj);
798 for(Iterator edgeit=gn.edges();(mun==null)&&edgeit.hasNext();) {
799 GraphNode gn2=((GraphNode.Edge) edgeit.next()).getTarget();
800 TermNode tn2=(TermNode)gn2.getOwner();
801 if (tn2.getType()==TermNode.ABSTRACT) {
802 AbstractRepair ar=tn2.getAbstract();
803 if (((repairtype==-1)||(ar.getType()==repairtype))&&
804 ar.getPredicate()==dpred) {
805 for(Iterator edgeit2=gn2.edges();edgeit2.hasNext();) {
806 GraphNode gn3=((GraphNode.Edge) edgeit2.next()).getTarget();
807 if (!removed.contains(gn3)) {
808 TermNode tn3=(TermNode)gn3.getOwner();
809 if (tn3.getType()==TermNode.UPDATE) {
821 /** Generates abstract (and concrete) repair for a comparison */
823 private void generatecomparisonrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
824 MultUpdateNode munmodify=getmultupdatenode(conj,dpred,AbstractRepair.MODIFYRELATION);
825 MultUpdateNode munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMRELATION);
826 MultUpdateNode munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTORELATION);
827 ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
828 RelationDescriptor rd=(RelationDescriptor)ep.getDescriptor();
829 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
830 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
831 boolean inverted=ep.inverted();
832 boolean negated=dpred.isNegated();
833 OpExpr expr=(OpExpr)ep.expr;
834 Opcode opcode=expr.getOpcode();
835 VarDescriptor leftside=VarDescriptor.makeNew("leftside");
836 VarDescriptor rightside=VarDescriptor.makeNew("rightside");
837 VarDescriptor newvalue=VarDescriptor.makeNew("newvalue");
839 expr.getLeftExpr().generate(cr,leftside);
840 expr.getRightExpr().generate(cr,newvalue);
841 cr.outputline(rd.getRange().getType().getGenerateType().getSafeSymbol()+" "+rightside.getSafeSymbol()+";");
842 cr.outputline(rd.getSafeSymbol()+"_hash->get("+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
844 expr.getLeftExpr().generate(cr,rightside);
845 expr.getRightExpr().generate(cr,newvalue);
846 cr.outputline(rd.getDomain().getType().getGenerateType().getSafeSymbol()+" "+leftside.getSafeSymbol()+";");
847 cr.outputline(rd.getSafeSymbol()+"_hashinv->get("+leftside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
850 if (opcode==Opcode.GT) {
852 } else if (opcode==Opcode.GE) {
854 } else if (opcode==Opcode.LT) {
856 } else if (opcode==Opcode.LE) {
858 } else if (opcode==Opcode.EQ) {
860 } else if (opcode==Opcode.NE) {
863 throw new Error("Unrecognized Opcode");
866 if (opcode==Opcode.GT) {
867 cr.outputline(newvalue.getSafeSymbol()+"++;");
868 } else if (opcode==Opcode.GE) {
870 } else if (opcode==Opcode.LT) {
871 cr.outputline(newvalue.getSafeSymbol()+"--;");
872 } else if (opcode==Opcode.LE) {
874 } else if (opcode==Opcode.EQ) {
876 } else if (opcode==Opcode.NE) { /* search for FLAGNE if this is changed*/
877 cr.outputline(newvalue.getSafeSymbol()+"++;");
879 throw new Error("Unrecognized Opcode");
881 /* Do abstract repairs */
883 cr.outputline(rd.getSafeSymbol()+"_hash->remove("+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
885 cr.outputline(rd.getSafeSymbol()+"_hash->add("+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
887 cr.outputline(rd.getSafeSymbol()+"_hash->add("+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
891 cr.outputline(rd.getSafeSymbol()+"_hashinv->remove("+rightside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
893 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+newvalue.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
895 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+rightside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
898 /* Do concrete repairs */
899 if (munmodify!=null) {
900 for(int i=0;i<state.vRules.size();i++) {
901 Rule r=(Rule)state.vRules.get(i);
902 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
903 for(int j=0;j<munmodify.numUpdates();j++) {
904 UpdateNode un=munmodify.getUpdate(j);
905 if (un.getRule()==r) {
906 /* Update for rule r */
907 String name=(String)updatenames.get(un);
908 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+","+newvalue.getSafeSymbol()+");");
915 /* Start with scheduling removal */
916 for(int i=0;i<state.vRules.size();i++) {
917 Rule r=(Rule)state.vRules.get(i);
918 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
919 for(int j=0;j<munremove.numUpdates();j++) {
920 UpdateNode un=munremove.getUpdate(i);
921 if (un.getRule()==r) {
922 /* Update for rule r */
923 String name=(String)updatenames.get(un);
924 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+");");
929 /* Now do addition */
930 UpdateNode un=munadd.getUpdate(0);
931 String name=(String)updatenames.get(un);
933 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
935 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
940 public void generatesizerepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr) {
941 ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
942 OpExpr expr=(OpExpr)ep.expr;
943 Opcode opcode=expr.getOpcode();
944 opcode=Opcode.translateOpcode(dpred.isNegated(),opcode);
946 MultUpdateNode munremove;
948 MultUpdateNode munadd;
949 if (ep.getDescriptor() instanceof RelationDescriptor) {
950 munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMRELATION);
951 munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTORELATION);
953 munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMSET);
954 munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTOSET);
956 int size=ep.rightSize();
957 VarDescriptor sizevar=VarDescriptor.makeNew("size");
958 ((OpExpr)expr).left.generate(cr, sizevar);
959 VarDescriptor change=VarDescriptor.makeNew("change");
960 cr.outputline("int "+change.getSafeSymbol()+";");
961 boolean generateadd=false;
962 boolean generateremove=false;
963 if (opcode==Opcode.GT) {
964 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
966 generateremove=false;
967 } else if (opcode==Opcode.GE) {
968 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
970 generateremove=false;
971 } else if (opcode==Opcode.LT) {
972 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size-1)+"-"+sizevar.getSafeSymbol()+";");
975 } else if (opcode==Opcode.LE) {
976 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
979 } else if (opcode==Opcode.EQ) {
980 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
986 } else if (opcode==Opcode.NE) {
987 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
989 generateremove=false;
991 throw new Error("Unrecognized Opcode");
994 // In some cases the analysis has determined that generating removes
996 if (generateremove&&munremove==null)
997 generateremove=false;
999 Descriptor d=ep.getDescriptor();
1000 if (generateremove) {
1001 cr.outputline("for(;"+change.getSafeSymbol()+"<0;"+change.getSafeSymbol()+"++)");
1003 /* Find element to remove */
1004 VarDescriptor leftvar=VarDescriptor.makeNew("leftvar");
1005 VarDescriptor rightvar=VarDescriptor.makeNew("rightvar");
1006 if (d instanceof RelationDescriptor) {
1007 if (ep.inverted()) {
1008 rightvar=((ImageSetExpr)((SizeofExpr)((OpExpr)expr).left).setexpr).getVar();
1009 cr.outputline("int "+leftvar.getSafeSymbol()+";");
1010 cr.outputline(d.getSafeSymbol()+"_hashinv->get((int)"+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1012 leftvar=((ImageSetExpr)((SizeofExpr)((OpExpr)expr).left).setexpr).getVar();
1013 cr.outputline("int "+rightvar.getSafeSymbol()+"=0;");
1014 cr.outputline(d.getSafeSymbol()+"_hash->get((int)"+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+");");
1017 cr.outputline("int "+leftvar.getSafeSymbol()+"="+d.getSafeSymbol()+"_hash->firstkey();");
1019 /* Generate abstract remove instruction */
1020 if (d instanceof RelationDescriptor) {
1021 RelationDescriptor rd=(RelationDescriptor) d;
1022 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1023 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1025 cr.outputline(rd.getSafeSymbol() + "_hash->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1027 cr.outputline(rd.getSafeSymbol() + "_hashinv->remove((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1029 cr.outputline(d.getSafeSymbol() + "_hash->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1031 /* Generate concrete remove instruction */
1032 for(int i=0;i<state.vRules.size();i++) {
1033 Rule r=(Rule)state.vRules.get(i);
1034 if (r.getInclusion().getTargetDescriptors().contains(d)) {
1035 for(int j=0;j<munremove.numUpdates();j++) {
1036 UpdateNode un=munremove.getUpdate(j);
1037 if (un.getRule()==r) {
1038 /* Update for rule rule r */
1039 String name=(String)updatenames.get(un);
1040 if (d instanceof RelationDescriptor) {
1041 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
1043 cr.outputline(repairtable.getSafeSymbol()+"->addset("+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1053 cr.outputline("for(;"+change.getSafeSymbol()+">0;"+change.getSafeSymbol()+"--)");
1055 VarDescriptor newobject=VarDescriptor.makeNew("newobject");
1056 if (d instanceof RelationDescriptor) {
1057 VarDescriptor otherside=((ImageSetExpr)((SizeofExpr)((OpExpr)ep.expr).left).setexpr).vd;
1058 RelationDescriptor rd=(RelationDescriptor)d;
1059 if (sources.relsetSource(rd,!ep.inverted())) {
1061 SetDescriptor sd=sources.relgetSourceSet(rd,!ep.inverted());
1062 VarDescriptor iterator=VarDescriptor.makeNew("iterator");
1063 cr.outputline(sd.getType().getGenerateType().getSafeSymbol() +" "+newobject.getSafeSymbol()+";");
1064 cr.outputline("for("+iterator.getSafeSymbol()+"="+sd.getSafeSymbol()+"_hash->iterator();"+iterator.getSafeSymbol()+".hasNext();)");
1066 if (ep.inverted()) {
1067 cr.outputline("if !"+rd.getSafeSymbol()+"_hashinv->contains("+iterator.getSafeSymbol()+"->key(),"+otherside.getSafeSymbol()+")");
1069 cr.outputline("if !"+rd.getSafeSymbol()+"_hash->contains("+otherside.getSafeSymbol()+","+iterator.getSafeSymbol()+"->key())");
1071 cr.outputline(newobject.getSafeSymbol()+"="+iterator.getSafeSymbol()+".key();");
1072 cr.outputline(iterator.getSafeSymbol()+"->next();");
1074 } else if (sources.relallocSource(rd,!ep.inverted())) {
1075 /* Allocation Source*/
1076 sources.relgenerateSourceAlloc(cr,newobject,rd,!ep.inverted());
1077 } else throw new Error("No source for adding to Relation");
1078 if (ep.inverted()) {
1079 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1080 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1082 cr.outputline(rd.getSafeSymbol()+"_hash->add("+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1084 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1086 UpdateNode un=munadd.getUpdate(0);
1087 String name=(String)updatenames.get(un);
1088 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1090 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1091 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1093 cr.outputline(rd.getSafeSymbol()+"_hash->add("+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1095 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1096 UpdateNode un=munadd.getUpdate(0);
1097 String name=(String)updatenames.get(un);
1098 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1101 SetDescriptor sd=(SetDescriptor)d;
1102 if (sources.setSource(sd)) {
1105 SetDescriptor sourcesd=sources.getSourceSet(sd);
1106 VarDescriptor iterator=VarDescriptor.makeNew("iterator");
1107 cr.outputline(sourcesd.getType().getGenerateType().getSafeSymbol() +" "+newobject.getSafeSymbol()+";");
1108 cr.outputline("for("+iterator.getSafeSymbol()+"="+sourcesd.getSafeSymbol()+"_hash->iterator();"+iterator.getSafeSymbol()+".hasNext();)");
1110 cr.outputline("if !"+sd.getSafeSymbol()+"_hash->contains("+iterator.getSafeSymbol()+"->key())");
1111 cr.outputline(newobject.getSafeSymbol()+"="+iterator.getSafeSymbol()+".key();");
1112 cr.outputline(iterator.getSafeSymbol()+"->next();");
1114 } else if (sources.allocSource(sd)) {
1115 /* Allocation Source*/
1116 sources.generateSourceAlloc(cr,newobject,sd);
1117 } else throw new Error("No source for adding to Set");
1118 cr.outputline(sd.getSafeSymbol()+"_hash->add("+newobject.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1119 UpdateNode un=munadd.getUpdate(0);
1120 String name=(String)updatenames.get(un);
1121 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1127 public void generateinclusionrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
1128 InclusionPredicate ip=(InclusionPredicate) dpred.getPredicate();
1129 boolean negated=dpred.isNegated();
1130 MultUpdateNode mun=getmultupdatenode(conj,dpred,-1);
1131 VarDescriptor leftvar=VarDescriptor.makeNew("left");
1132 ip.expr.generate(cr, leftvar);
1135 if (ip.setexpr instanceof ImageSetExpr) {
1136 ImageSetExpr ise=(ImageSetExpr) ip.setexpr;
1137 VarDescriptor rightvar=ise.getVar();
1138 boolean inverse=ise.inverted();
1139 RelationDescriptor rd=ise.getRelation();
1140 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1141 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1144 cr.outputline(rd.getSafeSymbol() + "_hash->remove((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1146 cr.outputline(rd.getSafeSymbol() + "_hashinv->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1149 cr.outputline(rd.getSafeSymbol() + "_hash->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1151 cr.outputline(rd.getSafeSymbol() + "_hashinv->remove((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1153 for(int i=0;i<state.vRules.size();i++) {
1154 Rule r=(Rule)state.vRules.get(i);
1155 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1156 for(int j=0;j<mun.numUpdates();j++) {
1157 UpdateNode un=mun.getUpdate(i);
1158 if (un.getRule()==r) {
1159 /* Update for rule rule r */
1160 String name=(String)updatenames.get(un);
1162 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1164 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
1171 SetDescriptor sd=ip.setexpr.sd;
1172 cr.outputline(sd.getSafeSymbol() + "_hash->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1174 for(int i=0;i<state.vRules.size();i++) {
1175 Rule r=(Rule)state.vRules.get(i);
1176 if (r.getInclusion().getTargetDescriptors().contains(sd)) {
1177 for(int j=0;j<mun.numUpdates();j++) {
1178 UpdateNode un=mun.getUpdate(i);
1179 if (un.getRule()==r) {
1180 /* Update for rule rule r */
1181 String name=(String)updatenames.get(un);
1182 cr.outputline(repairtable.getSafeSymbol()+"->addset("+sd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1189 /* Generate update */
1190 if (ip.setexpr instanceof ImageSetExpr) {
1191 ImageSetExpr ise=(ImageSetExpr) ip.setexpr;
1192 VarDescriptor rightvar=ise.getVar();
1193 boolean inverse=ise.inverted();
1194 RelationDescriptor rd=ise.getRelation();
1195 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1196 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1199 cr.outputline(rd.getSafeSymbol() + "_hash->add((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1201 cr.outputline(rd.getSafeSymbol() + "_hashinv->add((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1204 cr.outputline(rd.getSafeSymbol() + "_hash->add((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1206 cr.outputline(rd.getSafeSymbol() + "_hashinv->add((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1208 UpdateNode un=mun.getUpdate(0);
1209 String name=(String)updatenames.get(un);
1211 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1213 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+");");
1216 SetDescriptor sd=ip.setexpr.sd;
1217 cr.outputline(sd.getSafeSymbol() + "_hash->add((int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1219 UpdateNode un=mun.getUpdate(0);
1220 /* Update for rule rule r */
1221 String name=(String)updatenames.get(un);
1222 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1227 public static Vector getrulelist(Descriptor d) {
1228 Vector dispatchrules = new Vector();
1229 Vector rules = State.currentState.vRules;
1231 for (int i = 0; i < rules.size(); i++) {
1232 Rule rule = (Rule) rules.elementAt(i);
1233 Set requiredsymbols = rule.getRequiredDescriptors();
1235 // #TBD#: in general this is wrong because these descriptors may contain descriptors
1236 // bound in "in?" expressions which need to be dealt with in a topologically sorted
1239 if (rule.getRequiredDescriptors().contains(d)) {
1240 dispatchrules.addElement(rule);
1243 return dispatchrules;
1246 private boolean need_compensation(Rule r) {
1247 if (!Compiler.REPAIR)
1249 GraphNode gn=(GraphNode)termination.scopefalsify.get(r);
1250 for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1251 GraphNode.Edge edge=(GraphNode.Edge)edgeit.next();
1252 GraphNode gn2=edge.getTarget();
1253 if (!removed.contains(gn2)) {
1254 TermNode tn2=(TermNode)gn2.getOwner();
1255 if (tn2.getType()==TermNode.CONSEQUENCE)
1262 private UpdateNode find_compensation(Rule r) {
1263 GraphNode gn=(GraphNode)termination.scopefalsify.get(r);
1264 for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1265 GraphNode.Edge edge=(GraphNode.Edge)edgeit.next();
1266 GraphNode gn2=edge.getTarget();
1267 if (!removed.contains(gn2)) {
1268 TermNode tn2=(TermNode)gn2.getOwner();
1269 if (tn2.getType()==TermNode.UPDATE) {
1270 MultUpdateNode mun=tn2.getUpdate();
1271 for(int i=0;i<mun.numUpdates();i++) {
1272 UpdateNode un=mun.getUpdate(i);
1273 if (un.getRule()==r)
1279 throw new Error("No Compensation Update could be found");
1282 public void generate_dispatch(CodeWriter cr, RelationDescriptor rd, String leftvar, String rightvar) {
1283 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1284 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1286 if (!(usageinvimage||usageimage)) /* not used at all*/
1289 cr.outputline("// RELATION DISPATCH ");
1290 if (Compiler.REPAIR) {
1291 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1293 cr.outputline("!"+oldmodel.getSafeSymbol() +"->"+rd.getJustSafeSymbol()+"_hash->contains("+leftvar+","+rightvar+"))");
1295 cr.outputline("!"+oldmodel.getSafeSymbol() +"->"+rd.getJustSafeSymbol()+"_hashinv->contains("+rightvar+","+leftvar+"))");
1298 /* Adding new item */
1299 /* Perform safety checks */
1300 cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
1301 cr.outputline(repairtable.getSafeSymbol()+"->containsrelation("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+"))");
1303 /* Have update to call into */
1304 VarDescriptor mdfyptr=VarDescriptor.makeNew("modifyptr");
1305 cr.outputline("int "+mdfyptr.getSafeSymbol()+"="+repairtable.getSafeSymbol()+"->getrelation2("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1308 for(int i=0;i<currentrule.numQuantifiers();i++) {
1309 if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1310 parttype=parttype+", int, int";
1312 parttype=parttype+", int";
1314 VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1315 VarDescriptor tmpptr=VarDescriptor.makeNew("tempupdateptr");
1317 String methodcall="("+funptr.getSafeSymbol()+") (this,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
1318 for(int i=0;i<currentrule.numQuantifiers();i++) {
1319 Quantifier q=currentrule.getQuantifier(i);
1320 if (q instanceof SetQuantifier) {
1321 SetQuantifier sq=(SetQuantifier) q;
1322 methodcall+=","+sq.getVar().getSafeSymbol();
1323 } else if (q instanceof RelationQuantifier) {
1324 RelationQuantifier rq=(RelationQuantifier) q;
1325 methodcall+=","+rq.x.getSafeSymbol();
1326 methodcall+=","+rq.y.getSafeSymbol();
1327 } else if (q instanceof ForQuantifier) {
1328 ForQuantifier fq=(ForQuantifier) q;
1329 methodcall+=","+fq.getVar().getSafeSymbol();
1335 cr.outputline("void *"+tmpptr.getSafeSymbol()+"=");
1336 cr.outputline("(void *) "+repairtable.getSafeSymbol()+"->getrelation("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1337 cr.outputline("if ("+mdfyptr.getSafeSymbol()+")");
1340 cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+",int,int,int)="+"(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+",int,int,int)) "+tmpptr.getSafeSymbol());
1341 cr.outputline(methodcall+leftvar+", "+rightvar+", "+mdfyptr.getSafeSymbol() +");");
1344 cr.outputline("else ");
1347 cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")="+"(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")) "+tmpptr.getSafeSymbol());
1348 cr.outputline(methodcall+");");
1351 cr.outputline("goto rebuild;");
1355 /* Build standard compensation actions */
1356 if (need_compensation(currentrule)) {
1357 UpdateNode un=find_compensation(currentrule);
1358 String name=(String)updatenames.get(un);
1359 usedupdates.add(un); /* Mark as used */
1360 String methodcall=name+"(this,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
1361 for(int i=0;i<currentrule.numQuantifiers();i++) {
1362 Quantifier q=currentrule.getQuantifier(i);
1363 if (q instanceof SetQuantifier) {
1364 SetQuantifier sq=(SetQuantifier) q;
1365 methodcall+=","+sq.getVar().getSafeSymbol();
1366 } else if (q instanceof RelationQuantifier) {
1367 RelationQuantifier rq=(RelationQuantifier) q;
1368 methodcall+=","+rq.x.getSafeSymbol();
1369 methodcall+=","+rq.y.getSafeSymbol();
1370 } else if (q instanceof ForQuantifier) {
1371 ForQuantifier fq=(ForQuantifier) q;
1372 methodcall+=","+fq.getVar().getSafeSymbol();
1376 cr.outputline(methodcall);
1377 cr.outputline("goto rebuild;");
1383 String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1384 cr.outputline("int " + addeditem + ";");
1385 if (rd.testUsage(RelationDescriptor.IMAGE)) {
1386 cr.outputline(addeditem + " = " + rd.getSafeSymbol() + "_hash->add((int)" + leftvar + ", (int)" + rightvar+ ");");
1389 if (rd.testUsage(RelationDescriptor.INVIMAGE)) {
1390 cr.outputline(addeditem + " = " + rd.getSafeSymbol() + "_hashinv->add((int)" + rightvar + ", (int)" + leftvar + ");");
1395 Vector dispatchrules = getrulelist(rd);
1397 Set toremove=new HashSet();
1398 for(int i=0;i<dispatchrules.size();i++) {
1399 Rule r=(Rule)dispatchrules.get(i);
1400 if (!ruleset.contains(r))
1403 dispatchrules.removeAll(toremove);
1404 if (dispatchrules.size() == 0) {
1405 cr.outputline("// nothing to dispatch");
1409 cr.outputline("if (" + addeditem + ")");
1412 for(int i = 0; i < dispatchrules.size(); i++) {
1413 Rule rule = (Rule) dispatchrules.elementAt(i);
1414 if (rule.getGuardExpr().getRequiredDescriptors().contains(rd)) {
1415 /* Guard depends on this relation, so we recomput everything */
1416 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+",-1,0,0);");
1418 for (int j=0;j<rule.numQuantifiers();j++) {
1419 Quantifier q=rule.getQuantifier(j);
1420 if (q.getRequiredDescriptors().contains(rd)) {
1422 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+","+j+","+leftvar+","+rightvar+");");
1432 public void generate_dispatch(CodeWriter cr, SetDescriptor sd, String setvar) {
1434 cr.outputline("// SET DISPATCH ");
1435 if (Compiler.REPAIR) {
1436 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1437 cr.outputline("!"+oldmodel.getSafeSymbol() +"->"+sd.getJustSafeSymbol()+"_hash->contains("+setvar+"))");
1439 /* Adding new item */
1440 /* Perform safety checks */
1441 cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
1442 cr.outputline(repairtable.getSafeSymbol()+"->containsset("+sd.getNum()+","+currentrule.getNum()+","+setvar+"))");
1444 /* Have update to call into */
1445 VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1447 for(int i=0;i<currentrule.numQuantifiers();i++) {
1448 if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1449 parttype=parttype+", int, int";
1451 parttype=parttype+", int";
1453 cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")=");
1454 cr.outputline("(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")) "+repairtable.getSafeSymbol()+"->getset("+sd.getNum()+","+currentrule.getNum()+","+setvar+");");
1455 String methodcall="("+funptr.getSafeSymbol()+") (this,"+oldmodel.getSafeSymbol()+","+
1456 repairtable.getSafeSymbol();
1457 for(int i=0;i<currentrule.numQuantifiers();i++) {
1458 Quantifier q=currentrule.getQuantifier(i);
1459 if (q instanceof SetQuantifier) {
1460 SetQuantifier sq=(SetQuantifier) q;
1461 methodcall+=","+sq.getVar().getSafeSymbol();
1462 } else if (q instanceof RelationQuantifier) {
1463 RelationQuantifier rq=(RelationQuantifier) q;
1464 methodcall+=","+rq.x.getSafeSymbol();
1465 methodcall+=","+rq.y.getSafeSymbol();
1466 } else if (q instanceof ForQuantifier) {
1467 ForQuantifier fq=(ForQuantifier) q;
1468 methodcall+=","+fq.getVar().getSafeSymbol();
1472 cr.outputline(methodcall);
1473 cr.outputline("goto rebuild;");
1476 /* Build standard compensation actions */
1477 if (need_compensation(currentrule)) {
1478 UpdateNode un=find_compensation(currentrule);
1479 String name=(String)updatenames.get(un);
1480 usedupdates.add(un); /* Mark as used */
1482 String methodcall=name+"(this,"+oldmodel.getSafeSymbol()+","+
1483 repairtable.getSafeSymbol();
1484 for(int i=0;i<currentrule.numQuantifiers();i++) {
1485 Quantifier q=currentrule.getQuantifier(i);
1486 if (q instanceof SetQuantifier) {
1487 SetQuantifier sq=(SetQuantifier) q;
1488 methodcall+=","+sq.getVar().getSafeSymbol();
1489 } else if (q instanceof RelationQuantifier) {
1490 RelationQuantifier rq=(RelationQuantifier) q;
1491 methodcall+=","+rq.x.getSafeSymbol();
1492 methodcall+=","+rq.y.getSafeSymbol();
1493 } else if (q instanceof ForQuantifier) {
1494 ForQuantifier fq=(ForQuantifier) q;
1495 methodcall+=","+fq.getVar().getSafeSymbol();
1499 cr.outputline(methodcall);
1500 cr.outputline("goto rebuild;");
1506 String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1507 cr.outputline("int " + addeditem + " = 1;");
1508 cr.outputline(addeditem + " = " + sd.getSafeSymbol() + "_hash->add((int)" + setvar + ", (int)" + setvar + ");");
1510 Vector dispatchrules = getrulelist(sd);
1512 Set toremove=new HashSet();
1513 for(int i=0;i<dispatchrules.size();i++) {
1514 Rule r=(Rule)dispatchrules.get(i);
1515 if (!ruleset.contains(r))
1518 dispatchrules.removeAll(toremove);
1520 if (dispatchrules.size() == 0) {
1521 cr.outputline("// nothing to dispatch");
1525 cr.outputline("if ("+addeditem+")");
1527 for(int i = 0; i < dispatchrules.size(); i++) {
1528 Rule rule = (Rule) dispatchrules.elementAt(i);
1529 if (SetDescriptor.expand(rule.getGuardExpr().getRequiredDescriptors()).contains(sd)) {
1530 /* Guard depends on this relation, so we recompute everything */
1531 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+",-1,0,0);");
1533 for (int j=0;j<rule.numQuantifiers();j++) {
1534 Quantifier q=rule.getQuantifier(j);
1535 if (SetDescriptor.expand(q.getRequiredDescriptors()).contains(sd)) {
1537 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+","+j+","+setvar+",0);");