7 public class RepairGenerator {
9 java.io.PrintWriter outputrepair = null;
10 java.io.PrintWriter outputaux = null;
11 java.io.PrintWriter outputhead = null;
14 static VarDescriptor oldmodel=null;
15 static VarDescriptor newmodel=null;
16 static VarDescriptor worklist=null;
17 static VarDescriptor repairtable=null;
18 static VarDescriptor goodflag=null;
19 Rule currentrule=null;
20 Hashtable updatenames;
22 Termination termination;
25 static boolean DEBUG=false;
29 public RepairGenerator(State state, Termination t) {
31 updatenames=new Hashtable();
32 usedupdates=new HashSet();
35 togenerate=new HashSet();
36 togenerate.addAll(termination.conjunctions);
37 togenerate.removeAll(removed);
38 GraphNode.computeclosure(togenerate,removed);
40 sources=new Sources(state);
41 Repair.repairgenerator=this;
44 private void generatetypechecks(boolean flag) {
46 DotExpr.DOTYPECHECKS=true;
47 VarExpr.DOTYPECHECKS=true;
51 VarExpr.DOTYPECHECKS=false;
52 DotExpr.DOTYPECHECKS=false;
59 private void name_updates() {
61 for(Iterator it=termination.updatenodes.iterator();it.hasNext();) {
62 GraphNode gn=(GraphNode) it.next();
63 TermNode tn=(TermNode) gn.getOwner();
64 MultUpdateNode mun=tn.getUpdate();
65 if (togenerate.contains(gn))
66 for (int i=0;i<mun.numUpdates();i++) {
67 UpdateNode un=mun.getUpdate(i);
68 String name="update"+String.valueOf(count++);
69 updatenames.put(un,name);
74 public void generate(OutputStream outputrepair, OutputStream outputaux,OutputStream outputhead, String st) {
75 this.outputrepair = new java.io.PrintWriter(outputrepair, true);
76 this.outputaux = new java.io.PrintWriter(outputaux, true);
77 this.outputhead = new java.io.PrintWriter(outputhead, true);
80 generatetypechecks(true);
81 generate_tokentable();
82 generate_hashtables();
83 generate_stateobject();
89 CodeWriter crhead = new StandardCodeWriter(this.outputhead);
90 CodeWriter craux = new StandardCodeWriter(this.outputaux);
91 crhead.outputline("};");
92 craux.outputline("}");
93 generatetypechecks(false);
94 generate_computesizes();
95 generatetypechecks(true);
96 generate_recomputesizes();
97 generatetypechecks(false);
99 StructureGenerator sg=new StructureGenerator(state,this);
101 crhead.outputline("#endif");
104 String ststate="state";
105 String stmodel="model";
106 String strepairtable="repairtable";
107 String stleft="left";
108 String stright="right";
110 private void generate_updates() {
112 CodeWriter crhead = new StandardCodeWriter(outputhead);
113 CodeWriter craux = new StandardCodeWriter(outputaux);
115 /* Rewrite globals */
117 for (Iterator it=this.state.stGlobals.descriptors();it.hasNext();) {
118 VarDescriptor vd=(VarDescriptor)it.next();
119 craux.outputline("#define "+vd.getSafeSymbol()+" "+ststate+"->"+vd.getSafeSymbol());
122 for(Iterator it=termination.updatenodes.iterator();it.hasNext();) {
123 GraphNode gn=(GraphNode) it.next();
124 TermNode tn=(TermNode) gn.getOwner();
125 MultUpdateNode mun=tn.getUpdate();
126 boolean isrelation=(mun.getDescriptor() instanceof RelationDescriptor);
127 if (togenerate.contains(gn))
128 for (int i=0;i<mun.numUpdates();i++) {
129 UpdateNode un=mun.getUpdate(i);
130 String methodname=(String)updatenames.get(un);
133 case MultUpdateNode.ADD:
135 crhead.outputline("void "+methodname+"("+name+"_state * " +ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable+", int "+stleft+", int "+stright+");");
136 craux.outputline("void "+methodname+"("+name+"_state * "+ ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable+", int "+stleft+", int "+stright+")");
138 crhead.outputline("void "+methodname+"("+name+"_state * "+ ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable+", int "+stleft+");");
139 craux.outputline("void "+methodname+"("+name+"_state * "+ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable+", int "+stleft+")");
142 craux.outputline("int maybe=0;");
143 final SymbolTable st = un.getRule().getSymbolTable();
144 CodeWriter cr = new StandardCodeWriter(outputaux) {
145 public SymbolTable getSymbolTable() { return st; }
147 un.generate(cr, false, stleft,stright,this);
148 craux.outputline("if (maybe) printf(\"REALLY BAD\");");
151 case MultUpdateNode.REMOVE:
153 String methodcall="void "+methodname+"("+name+"_state * "+ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable;
154 for(int j=0;j<r.numQuantifiers();j++) {
155 Quantifier q=r.getQuantifier(j);
156 if (q instanceof SetQuantifier) {
157 SetQuantifier sq=(SetQuantifier) q;
158 methodcall+=","+sq.getVar().getType().getGenerateType().getSafeSymbol()+" "+sq.getVar().getSafeSymbol();
159 } else if (q instanceof RelationQuantifier) {
160 RelationQuantifier rq=(RelationQuantifier) q;
162 methodcall+=","+rq.x.getType().getGenerateType().getSafeSymbol()+" "+rq.x.getSafeSymbol();
163 methodcall+=","+rq.y.getType().getGenerateType().getSafeSymbol()+" "+rq.y.getSafeSymbol();
164 } else if (q instanceof ForQuantifier) {
165 ForQuantifier fq=(ForQuantifier) q;
166 methodcall+=",int "+fq.getVar().getSafeSymbol();
170 crhead.outputline(methodcall+";");
171 craux.outputline(methodcall);
173 craux.outputline("int maybe=0;");
174 final SymbolTable st2 = un.getRule().getSymbolTable();
175 CodeWriter cr2 = new StandardCodeWriter(outputaux) {
176 public SymbolTable getSymbolTable() { return st2; }
178 un.generate(cr2, true, null,null,this);
179 craux.outputline("if (maybe) printf(\"REALLY BAD\");");
182 case MultUpdateNode.MODIFY:
184 throw new Error("Nonimplement Update");
190 private void generate_call() {
191 CodeWriter cr = new StandardCodeWriter(outputrepair);
192 VarDescriptor vdstate=VarDescriptor.makeNew("repairstate");
193 cr.outputline(name+"_state * "+vdstate.getSafeSymbol()+"=new "+name+"_state();");
194 Iterator globals=state.stGlobals.descriptors();
195 while (globals.hasNext()) {
196 VarDescriptor vd=(VarDescriptor) globals.next();
197 cr.outputline(vdstate.getSafeSymbol()+"->"+vd.getSafeSymbol()+"=("+vd.getType().getGenerateType().getSafeSymbol()+")"+vd.getSafeSymbol()+";");
199 /* Insert repair here */
200 cr.outputline(vdstate.getSafeSymbol()+"->doanalysis();");
201 globals=state.stGlobals.descriptors();
202 while (globals.hasNext()) {
203 VarDescriptor vd=(VarDescriptor) globals.next();
204 cr.outputline("*(("+vd.getType().getGenerateType().getSafeSymbol()+"*) &"+vd.getSafeSymbol()+")="+vdstate.getSafeSymbol()+"->"+vd.getSafeSymbol()+";");
206 cr.outputline("delete "+vdstate.getSafeSymbol()+";");
209 private void generate_tokentable() {
210 CodeWriter cr = new StandardCodeWriter(outputrepair);
211 Iterator tokens = TokenLiteralExpr.tokens.keySet().iterator();
214 cr.outputline("// Token values");
217 while (tokens.hasNext()) {
218 Object token = tokens.next();
219 cr.outputline("// " + token.toString() + " = " + TokenLiteralExpr.tokens.get(token).toString());
226 private void generate_stateobject() {
227 CodeWriter crhead = new StandardCodeWriter(outputhead);
228 crhead.outputline("class "+name+"_state {");
229 crhead.outputline("public:");
230 Iterator globals=state.stGlobals.descriptors();
231 while (globals.hasNext()) {
232 VarDescriptor vd=(VarDescriptor) globals.next();
233 crhead.outputline(vd.getType().getGenerateType().getSafeSymbol()+" "+vd.getSafeSymbol()+";");
235 crhead.outputline("void computesizes(int *,int **);");
236 crhead.outputline("void recomputesizes();");
239 private void generate_computesizes() {
240 int max=TypeDescriptor.counter;
241 TypeDescriptor[] tdarray=new TypeDescriptor[max];
242 for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
243 TypeDescriptor ttd=(TypeDescriptor)it.next();
244 tdarray[ttd.getId()]=ttd;
246 CodeWriter cr=new StandardCodeWriter(outputaux);
247 cr.outputline("void "+name+"_state::computesizes(int *sizearray,int **numele) {");
248 for(int i=0;i<max;i++) {
249 TypeDescriptor td=tdarray[i];
250 Expr size=td.getSizeExpr();
251 VarDescriptor vd=VarDescriptor.makeNew("size");
252 size.generate(cr,vd);
253 cr.outputline("sizearray["+i+"]="+vd.getSafeSymbol()+";");
255 for(int i=0;i<max;i++) {
256 TypeDescriptor td=tdarray[i];
257 if (td instanceof StructureTypeDescriptor) {
258 StructureTypeDescriptor std=(StructureTypeDescriptor) td;
259 for(int j=0;j<std.fieldlist.size();j++) {
260 FieldDescriptor fd=(FieldDescriptor)std.fieldlist.get(j);
261 if (fd instanceof ArrayDescriptor) {
262 ArrayDescriptor ad=(ArrayDescriptor)fd;
263 Expr index=ad.getIndexBound();
264 VarDescriptor vd=VarDescriptor.makeNew("index");
265 index.generate(cr,vd);
266 cr.outputline("numele["+i+"]["+j+"]="+vd.getSafeSymbol()+";");
274 private void generate_recomputesizes() {
275 int max=TypeDescriptor.counter;
276 TypeDescriptor[] tdarray=new TypeDescriptor[max];
277 for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
278 TypeDescriptor ttd=(TypeDescriptor)it.next();
279 tdarray[ttd.getId()]=ttd;
281 CodeWriter cr=new StandardCodeWriter(outputaux);
282 cr.outputline("void "+name+"_state::recomputesizes() {");
283 for(int i=0;i<max;i++) {
284 TypeDescriptor td=tdarray[i];
285 Expr size=td.getSizeExpr();
286 VarDescriptor vd=VarDescriptor.makeNew("size");
287 size.generate(cr,vd);
289 for(int i=0;i<max;i++) {
290 TypeDescriptor td=tdarray[i];
291 if (td instanceof StructureTypeDescriptor) {
292 StructureTypeDescriptor std=(StructureTypeDescriptor) td;
293 for(int j=0;j<std.fieldlist.size();j++) {
294 FieldDescriptor fd=(FieldDescriptor)std.fieldlist.get(j);
295 if (fd instanceof ArrayDescriptor) {
296 ArrayDescriptor ad=(ArrayDescriptor)fd;
297 Expr index=ad.getIndexBound();
298 VarDescriptor vd=VarDescriptor.makeNew("index");
299 index.generate(cr,vd);
308 private void generate_hashtables() {
309 CodeWriter craux = new StandardCodeWriter(outputaux);
310 CodeWriter crhead = new StandardCodeWriter(outputhead);
311 crhead.outputline("#ifndef "+name+"_h");
312 crhead.outputline("#define "+name+"_h");
313 crhead.outputline("#include \"SimpleHash.h\"");
314 crhead.outputline("extern \"C\" {");
315 crhead.outputline("#include \"instrument.h\"");
316 crhead.outputline("}");
317 crhead.outputline("#include <stdio.h>");
318 crhead.outputline("#include <stdlib.h>");
319 crhead.outputline("class "+name+" {");
320 crhead.outputline("public:");
321 crhead.outputline(name+"();");
322 crhead.outputline("~"+name+"();");
323 craux.outputline("#include \""+headername+"\"");
324 craux.outputline("#include \"size.h\"");
326 craux.outputline(name+"::"+name+"() {");
327 craux.outputline("// creating hashtables ");
330 Iterator sets = state.stSets.descriptors();
332 /* first pass create all the hash tables */
333 while (sets.hasNext()) {
334 SetDescriptor set = (SetDescriptor) sets.next();
335 crhead.outputline("SimpleHash* " + set.getSafeSymbol() + "_hash;");
336 craux.outputline(set.getSafeSymbol() + "_hash = new SimpleHash();");
339 /* second pass build relationships between hashtables */
340 sets = state.stSets.descriptors();
342 while (sets.hasNext()) {
343 SetDescriptor set = (SetDescriptor) sets.next();
344 Iterator subsets = set.subsets();
346 while (subsets.hasNext()) {
347 SetDescriptor subset = (SetDescriptor) subsets.next();
348 craux.outputline(subset.getSafeSymbol() + "_hash->addParent(" + set.getSafeSymbol() + "_hash);");
352 /* build relations */
353 Iterator relations = state.stRelations.descriptors();
355 /* first pass create all the hash tables */
356 while (relations.hasNext()) {
357 RelationDescriptor relation = (RelationDescriptor) relations.next();
359 if (relation.testUsage(RelationDescriptor.IMAGE)) {
360 crhead.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hash;");
361 craux.outputline(relation.getSafeSymbol() + "_hash = new SimpleHash();");
364 if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
365 crhead.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hashinv;");
366 craux.outputline(relation.getSafeSymbol() + "_hashinv = new SimpleHash();");
370 craux.outputline("}");
371 crhead.outputline("};");
372 craux.outputline(name+"::~"+name+"() {");
373 craux.outputline("// deleting hashtables");
375 /* build destructor */
376 sets = state.stSets.descriptors();
378 /* first pass create all the hash tables */
379 while (sets.hasNext()) {
380 SetDescriptor set = (SetDescriptor) sets.next();
381 craux.outputline("delete "+set.getSafeSymbol() + "_hash;");
384 /* destroy relations */
385 relations = state.stRelations.descriptors();
387 /* first pass create all the hash tables */
388 while (relations.hasNext()) {
389 RelationDescriptor relation = (RelationDescriptor) relations.next();
391 if (relation.testUsage(RelationDescriptor.IMAGE)) {
392 craux.outputline("delete "+relation.getSafeSymbol() + "_hash;");
395 if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
396 craux.outputline("delete " + relation.getSafeSymbol() + ";");
399 craux.outputline("}");
402 private void generate_worklist() {
403 CodeWriter crhead = new StandardCodeWriter(outputhead);
404 CodeWriter craux = new StandardCodeWriter(outputaux);
405 oldmodel=VarDescriptor.makeNew("oldmodel");
406 newmodel=VarDescriptor.makeNew("newmodel");
407 worklist=VarDescriptor.makeNew("worklist");
408 goodflag=VarDescriptor.makeNew("goodflag");
409 repairtable=VarDescriptor.makeNew("repairtable");
410 crhead.outputline("void doanalysis();");
411 craux.outputline("void "+name +"_state::doanalysis()");
413 craux.outputline("int highmark;");
414 craux.outputline("initializestack(&highmark);");
415 craux.outputline("typeobject *typeobject1=gettypeobject();");
416 craux.outputline("typeobject1->computesizes(this);");
417 craux.outputline("recomputesizes();");
418 craux.outputline(name+ " * "+oldmodel.getSafeSymbol()+"=0;");
419 craux.outputline("WorkList * "+worklist.getSafeSymbol()+" = new WorkList();");
420 craux.outputline("RepairHash * "+repairtable.getSafeSymbol()+"=0;");
421 craux.outputline("while (1)");
423 craux.outputline("int "+goodflag.getSafeSymbol()+"=1;");
424 craux.outputline(name+ " * "+newmodel.getSafeSymbol()+"=new "+name+"();");
427 private void generate_teardown() {
428 CodeWriter cr = new StandardCodeWriter(outputaux);
432 private void generate_rules() {
433 /* first we must sort the rules */
434 Iterator allrules = state.vRules.iterator();
435 Vector emptyrules = new Vector(); // rules with no quantifiers
436 Vector worklistrules = new Vector(); // the rest of the rules
437 RelationDescriptor.prefix = newmodel.getSafeSymbol()+"->";
438 SetDescriptor.prefix = newmodel.getSafeSymbol()+"->";
440 while (allrules.hasNext()) {
441 Rule rule = (Rule) allrules.next();
442 ListIterator quantifiers = rule.quantifiers();
443 boolean noquantifiers = true;
444 while (quantifiers.hasNext()) {
445 Quantifier quantifier = (Quantifier) quantifiers.next();
446 if (quantifier instanceof ForQuantifier) {
447 // ok, because integers exist already!
450 noquantifiers = false;
455 emptyrules.add(rule);
457 worklistrules.add(rule);
461 Iterator iterator_er = emptyrules.iterator();
462 while (iterator_er.hasNext()) {
463 Rule rule = (Rule) iterator_er.next();
465 final SymbolTable st = rule.getSymbolTable();
466 CodeWriter cr = new StandardCodeWriter(outputaux) {
467 public SymbolTable getSymbolTable() { return st; }
469 cr.outputline("// build " +escape(rule.toString()));
471 cr.outputline("int maybe=0;");
472 ListIterator quantifiers = rule.quantifiers();
473 while (quantifiers.hasNext()) {
474 Quantifier quantifier = (Quantifier) quantifiers.next();
475 quantifier.generate_open(cr);
480 rule.getGuardExpr().prettyPrint(cr);
483 /* now we have to generate the guard test */
484 VarDescriptor guardval = VarDescriptor.makeNew();
485 rule.getGuardExpr().generate(cr, guardval);
486 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
489 /* now we have to generate the inclusion code */
491 rule.getInclusion().generate(cr);
493 while (quantifiers.hasPrevious()) {
494 Quantifier quantifier = (Quantifier) quantifiers.previous();
503 CodeWriter cr2 = new StandardCodeWriter(outputaux);
505 cr2.outputline("while ("+goodflag.getSafeSymbol()+"&&"+worklist.getSafeSymbol()+"->hasMoreElements())");
507 VarDescriptor idvar=VarDescriptor.makeNew("id");
508 cr2.outputline("int "+idvar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->getid();");
510 String elseladder = "if";
512 Iterator iterator_rules = worklistrules.iterator();
513 while (iterator_rules.hasNext()) {
515 Rule rule = (Rule) iterator_rules.next();
516 int dispatchid = rule.getNum();
519 final SymbolTable st = rule.getSymbolTable();
520 CodeWriter cr = new StandardCodeWriter(outputaux) {
521 public SymbolTable getSymbolTable() { return st; }
525 cr.outputline(elseladder + " ("+idvar.getSafeSymbol()+" == " + dispatchid + ")");
527 cr.outputline("int maybe=0;");
528 VarDescriptor typevar=VarDescriptor.makeNew("type");
529 VarDescriptor leftvar=VarDescriptor.makeNew("left");
530 VarDescriptor rightvar=VarDescriptor.makeNew("right");
531 cr.outputline("int "+typevar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->gettype();");
532 cr.outputline("int "+leftvar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->getlvalue();");
533 cr.outputline("int "+rightvar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->getrvalue();");
534 cr.outputline("// build " +escape(rule.toString()));
537 for (int j=0;j<rule.numQuantifiers();j++) {
538 Quantifier quantifier = rule.getQuantifier(j);
539 quantifier.generate_open(cr, typevar.getSafeSymbol(),j,leftvar.getSafeSymbol(),rightvar.getSafeSymbol());
545 rule.getGuardExpr().prettyPrint(cr);
548 /* now we have to generate the guard test */
550 VarDescriptor guardval = VarDescriptor.makeNew();
551 rule.getGuardExpr().generate(cr, guardval);
553 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
556 /* now we have to generate the inclusion code */
558 rule.getInclusion().generate(cr);
561 for (int j=0;j<rule.numQuantifiers();j++) {
565 // close startblocks generated by DotExpr memory checks
566 //DotExpr.generate_memory_endblocks(cr);
568 cr.endblock(); // end else-if WORKLIST ladder
570 elseladder = "else if";
574 cr2.outputline("else");
576 cr2.outputline("printf(\"VERY BAD !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\\n\\n\");");
577 cr2.outputline("exit(1);");
579 // end block created for worklist
580 cr2.outputline(worklist.getSafeSymbol()+"->pop();");
584 public static String escape(String s) {
586 for(int i=0;i<s.length();i++) {
596 private void generate_checks() {
598 /* do constraint checks */
599 Vector constraints = state.vConstraints;
601 for (int i = 0; i < constraints.size(); i++) {
603 Constraint constraint = (Constraint) constraints.elementAt(i);
607 final SymbolTable st = constraint.getSymbolTable();
609 CodeWriter cr = new StandardCodeWriter(outputaux) {
610 public SymbolTable getSymbolTable() { return st; }
613 cr.outputline("// checking " + escape(constraint.toString()));
616 ListIterator quantifiers = constraint.quantifiers();
618 while (quantifiers.hasNext()) {
619 Quantifier quantifier = (Quantifier) quantifiers.next();
620 quantifier.generate_open(cr);
623 cr.outputline("int maybe = 0;");
625 /* now we have to generate the guard test */
627 VarDescriptor constraintboolean = VarDescriptor.makeNew("constraintboolean");
628 constraint.getLogicStatement().generate(cr, constraintboolean);
630 cr.outputline("if (maybe)");
632 cr.outputline("printf(\"maybe fail " + escape(constraint.toString()) + ". \");");
633 cr.outputline("exit(1);");
636 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
639 cr.outputline("printf(\"fail " + escape(constraint.toString()) + ". \");");
642 /* Build new repair table */
643 cr.outputline("if ("+repairtable.getSafeSymbol()+")");
644 cr.outputline("delete "+repairtable.getSafeSymbol()+";");
645 cr.outputline(repairtable.getSafeSymbol()+"=new RepairHash();");
648 /* Compute cost of each repair */
649 VarDescriptor mincost=VarDescriptor.makeNew("mincost");
650 VarDescriptor mincostindex=VarDescriptor.makeNew("mincostindex");
651 DNFConstraint dnfconst=constraint.dnfconstraint;
652 if (dnfconst.size()<=1) {
653 cr.outputline("int "+mincostindex.getSafeSymbol()+"=0;");
655 if (dnfconst.size()>1) {
656 cr.outputline("int "+mincostindex.getSafeSymbol()+";");
658 for(int j=0;j<dnfconst.size();j++) {
659 Conjunction conj=dnfconst.get(j);
660 GraphNode gn=(GraphNode)termination.conjtonodemap.get(conj);
661 if (removed.contains(gn))
664 VarDescriptor costvar;
668 costvar=VarDescriptor.makeNew("cost");
669 for(int k=0;k<conj.size();k++) {
670 DNFPredicate dpred=conj.get(k);
671 Predicate p=dpred.getPredicate();
672 boolean negate=dpred.isNegated();
673 VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
674 p.generate(cr,predvalue);
676 cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
678 cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
680 cr.outputline("int "+costvar.getSafeSymbol()+"="+cost.getCost(dpred)+";");
682 cr.outputline(costvar.getSafeSymbol()+"+="+cost.getCost(dpred)+";");
686 cr.outputline("if ("+costvar.getSafeSymbol()+"<"+mincost.getSafeSymbol()+")");
688 cr.outputline(mincost.getSafeSymbol()+"="+costvar.getSafeSymbol()+";");
689 cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
692 cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
696 cr.outputline("switch("+mincostindex.getSafeSymbol()+") {");
697 for(int j=0;j<dnfconst.size();j++) {
698 Conjunction conj=dnfconst.get(j);
699 GraphNode gn=(GraphNode)termination.conjtonodemap.get(conj);
700 if (removed.contains(gn))
702 cr.outputline("case "+j+":");
703 for(int k=0;k<conj.size();k++) {
704 DNFPredicate dpred=conj.get(k);
705 Predicate p=dpred.getPredicate();
706 boolean negate=dpred.isNegated();
707 VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
708 p.generate(cr,predvalue);
710 cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
712 cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
714 if (p instanceof InclusionPredicate)
715 generateinclusionrepair(conj,dpred, cr);
716 else if (p instanceof ExprPredicate) {
717 ExprPredicate ep=(ExprPredicate)p;
718 if (ep.getType()==ExprPredicate.SIZE)
719 generatesizerepair(conj,dpred,cr);
720 else if (ep.getType()==ExprPredicate.COMPARISON)
721 generatecomparisonrepair(conj,dpred,cr);
722 } else throw new Error("Unrecognized Predicate");
726 cr.outputline("break;");
730 cr.outputline(goodflag.getSafeSymbol()+"=0;");
731 cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
732 cr.outputline("delete "+oldmodel.getSafeSymbol()+";");
733 cr.outputline(oldmodel.getSafeSymbol()+"="+newmodel.getSafeSymbol()+";");
734 cr.outputline("goto rebuild;"); /* Rebuild model and all */
738 while (quantifiers.hasPrevious()) {
739 Quantifier quantifier = (Quantifier) quantifiers.previous();
742 cr.outputline("if ("+goodflag.getSafeSymbol()+")");
744 cr.outputline("if ("+repairtable.getSafeSymbol()+")");
745 cr.outputline("delete "+repairtable.getSafeSymbol()+";");
746 cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
747 cr.outputline("delete "+oldmodel.getSafeSymbol()+";");
748 cr.outputline("delete "+newmodel.getSafeSymbol()+";");
749 cr.outputline("delete "+worklist.getSafeSymbol()+";");
750 cr.outputline("resettypemap();");
751 cr.outputline("break;");
753 cr.outputline("rebuild:");
762 private MultUpdateNode getmultupdatenode(Conjunction conj, DNFPredicate dpred, int repairtype) {
763 MultUpdateNode mun=null;
764 GraphNode gn=(GraphNode) termination.conjtonodemap.get(conj);
765 for(Iterator edgeit=gn.edges();(mun==null)&&edgeit.hasNext();) {
766 GraphNode gn2=((GraphNode.Edge) edgeit.next()).getTarget();
767 TermNode tn2=(TermNode)gn2.getOwner();
768 if (tn2.getType()==TermNode.ABSTRACT) {
769 AbstractRepair ar=tn2.getAbstract();
770 if (((repairtype==-1)||(ar.getType()==repairtype))&&
771 ar.getPredicate()==dpred) {
772 for(Iterator edgeit2=gn2.edges();edgeit2.hasNext();) {
773 GraphNode gn3=((GraphNode.Edge) edgeit2.next()).getTarget();
774 if (!removed.contains(gn3)) {
775 TermNode tn3=(TermNode)gn3.getOwner();
776 if (tn3.getType()==TermNode.UPDATE) {
788 public void generatecomparisonrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
789 MultUpdateNode munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMRELATION);
790 MultUpdateNode munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTORELATION);
791 ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
792 RelationDescriptor rd=(RelationDescriptor)ep.getDescriptor();
793 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
794 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
795 boolean inverted=ep.inverted();
796 boolean negated=dpred.isNegated();
797 OpExpr expr=(OpExpr)ep.expr;
798 Opcode opcode=expr.getOpcode();
799 VarDescriptor leftside=VarDescriptor.makeNew("leftside");
800 VarDescriptor rightside=VarDescriptor.makeNew("rightside");
801 VarDescriptor newvalue=VarDescriptor.makeNew("newvalue");
803 expr.getLeftExpr().generate(cr,leftside);
804 expr.getRightExpr().generate(cr,newvalue);
805 cr.outputline(rd.getRange().getType().getGenerateType().getSafeSymbol()+" "+rightside.getSafeSymbol()+";");
806 cr.outputline(rd.getSafeSymbol()+"_hash->get("+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
808 expr.getLeftExpr().generate(cr,rightside);
809 expr.getRightExpr().generate(cr,newvalue);
810 cr.outputline(rd.getDomain().getType().getGenerateType().getSafeSymbol()+" "+leftside.getSafeSymbol()+";");
811 cr.outputline(rd.getSafeSymbol()+"_hashinv->get("+leftside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
814 if (opcode==Opcode.GT) {
816 } else if (opcode==Opcode.GE) {
818 } else if (opcode==Opcode.LT) {
820 } else if (opcode==Opcode.LE) {
822 } else if (opcode==Opcode.EQ) {
824 } else if (opcode==Opcode.NE) {
827 throw new Error("Unrecognized Opcode");
830 if (opcode==Opcode.GT) {
831 cr.outputline(newvalue.getSafeSymbol()+"++;");
832 } else if (opcode==Opcode.GE) {
834 } else if (opcode==Opcode.LT) {
835 cr.outputline(newvalue.getSafeSymbol()+"--;");
836 } else if (opcode==Opcode.LE) {
838 } else if (opcode==Opcode.EQ) {
840 } else if (opcode==Opcode.NE) {
841 cr.outputline(newvalue.getSafeSymbol()+"++;");
843 throw new Error("Unrecognized Opcode");
845 /* Do abstract repairs */
847 cr.outputline(rd.getSafeSymbol()+"_hash->remove("+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
849 cr.outputline(rd.getSafeSymbol()+"_hash->add("+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
851 cr.outputline(rd.getSafeSymbol()+"_hash->add("+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
855 cr.outputline(rd.getSafeSymbol()+"_hashinv->remove("+rightside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
857 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+newvalue.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
859 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+rightside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
862 /* Do concrete repairs */
863 /* Start with scheduling removal */
864 for(int i=0;i<state.vRules.size();i++) {
865 Rule r=(Rule)state.vRules.get(i);
866 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
867 for(int j=0;j<munremove.numUpdates();j++) {
868 UpdateNode un=munremove.getUpdate(i);
869 if (un.getRule()==r) {
870 /* Update for rule r */
871 String name=(String)updatenames.get(un);
872 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+");");
877 /* Now do addition */
878 UpdateNode un=munadd.getUpdate(0);
879 String name=(String)updatenames.get(un);
881 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
883 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
887 public void generatesizerepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr) {
888 ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
889 OpExpr expr=(OpExpr)ep.expr;
890 Opcode opcode=expr.getOpcode();
892 boolean negated=dpred.isNegated();
894 if (opcode==Opcode.GT) {
896 } else if (opcode==Opcode.GE) {
898 } else if (opcode==Opcode.LT) {
900 } else if (opcode==Opcode.LE) {
902 } else if (opcode==Opcode.EQ) {
904 } else if (opcode==Opcode.NE) {
907 throw new Error("Unrecognized Opcode");
910 MultUpdateNode munremove;
912 MultUpdateNode munadd;
913 if (ep.getDescriptor() instanceof RelationDescriptor) {
914 munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMRELATION);
915 munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTORELATION);
917 munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMSET);
918 munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTOSET);
920 int size=ep.leftsize();
921 VarDescriptor sizevar=VarDescriptor.makeNew("size");
922 ((OpExpr)expr).left.generate(cr, sizevar);
923 VarDescriptor change=VarDescriptor.makeNew("change");
924 cr.outputline("int "+change.getSafeSymbol()+";");
925 boolean generateadd=false;
926 boolean generateremove=false;
927 if (opcode==Opcode.GT) {
928 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
930 generateremove=false;
931 } else if (opcode==Opcode.GE) {
932 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
934 generateremove=false;
935 } else if (opcode==Opcode.LT) {
936 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size-1)+"-"+sizevar.getSafeSymbol()+";");
939 } else if (opcode==Opcode.LE) {
940 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
943 } else if (opcode==Opcode.EQ) {
944 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
947 } else if (opcode==Opcode.NE) {
948 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
950 generateremove=false;
952 throw new Error("Unrecognized Opcode");
954 Descriptor d=ep.getDescriptor();
955 if (generateremove) {
956 cr.outputline("for(;"+change.getSafeSymbol()+"<0;"+change.getSafeSymbol()+"++)");
958 /* Find element to remove */
959 VarDescriptor leftvar=VarDescriptor.makeNew("leftvar");
960 VarDescriptor rightvar=VarDescriptor.makeNew("rightvar");
961 if (d instanceof RelationDescriptor) {
963 rightvar=((ImageSetExpr)((SizeofExpr)((OpExpr)expr).left).setexpr).getVar();
964 cr.outputline("int "+leftvar.getSafeSymbol()+";");
965 cr.outputline(d.getSafeSymbol()+"_hashinv->get((int)"+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
967 leftvar=((ImageSetExpr)((SizeofExpr)((OpExpr)expr).left).setexpr).getVar();
968 cr.outputline("int "+rightvar.getSafeSymbol()+";");
969 cr.outputline(d.getSafeSymbol()+"_hash->get((int)"+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+");");
972 cr.outputline("int "+leftvar.getSafeSymbol()+"="+d.getSafeSymbol()+"_hash->firstkey();");
974 /* Generate abstract remove instruction */
975 if (d instanceof RelationDescriptor) {
976 RelationDescriptor rd=(RelationDescriptor) d;
977 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
978 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
980 cr.outputline(rd.getSafeSymbol() + "_hash->remove((int)" + leftvar + ", (int)" + rightvar + ");");
982 cr.outputline(rd.getSafeSymbol() + "_hashinv->remove((int)" + rightvar + ", (int)" + leftvar + ");");
984 cr.outputline(d.getSafeSymbol() + "_hash->remove((int)" + leftvar + ", (int)" + leftvar + ");");
986 /* Generate concrete remove instruction */
987 for(int i=0;i<state.vRules.size();i++) {
988 Rule r=(Rule)state.vRules.get(i);
989 if (r.getInclusion().getTargetDescriptors().contains(d)) {
990 for(int j=0;j<munremove.numUpdates();j++) {
991 UpdateNode un=munremove.getUpdate(i);
992 if (un.getRule()==r) {
993 /* Update for rule rule r */
994 String name=(String)updatenames.get(un);
995 if (d instanceof RelationDescriptor) {
996 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
998 cr.outputline(repairtable.getSafeSymbol()+"->addset("+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1008 cr.outputline("for(;"+change.getSafeSymbol()+">0;"+change.getSafeSymbol()+"--)");
1010 VarDescriptor newobject=VarDescriptor.makeNew("newobject");
1011 if (d instanceof RelationDescriptor) {
1012 VarDescriptor otherside=((ImageSetExpr)((SizeofExpr)ep.expr).setexpr).vd;
1013 RelationDescriptor rd=(RelationDescriptor)d;
1014 if (sources.relsetSource(rd,!ep.inverted())) {
1016 SetDescriptor sd=sources.relgetSourceSet(rd,!ep.inverted());
1017 VarDescriptor iterator=VarDescriptor.makeNew("iterator");
1018 cr.outputline(sd.getType().getGenerateType().getSafeSymbol() +" "+newobject.getSafeSymbol()+";");
1019 cr.outputline("for("+iterator.getSafeSymbol()+"="+sd.getSafeSymbol()+"_hash->iterator();"+iterator.getSafeSymbol()+".hasNext();)");
1021 if (ep.inverted()) {
1022 cr.outputline("if !"+rd.getSafeSymbol()+"_hashinv->contains("+iterator.getSafeSymbol()+"->key(),"+otherside.getSafeSymbol()+")");
1024 cr.outputline("if !"+rd.getSafeSymbol()+"_hash->contains("+otherside.getSafeSymbol()+","+iterator.getSafeSymbol()+"->key())");
1026 cr.outputline(newobject.getSafeSymbol()+"="+iterator.getSafeSymbol()+".key();");
1027 cr.outputline(iterator.getSafeSymbol()+"->next();");
1029 } else if (sources.relallocSource(rd,!ep.inverted())) {
1030 /* Allocation Source*/
1031 sources.relgenerateSourceAlloc(cr,newobject,rd,!ep.inverted());
1032 } else throw new Error("No source for adding to Relation");
1033 if (ep.inverted()) {
1034 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1035 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1037 cr.outputline(rd.getSafeSymbol()+"_hash->add("+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1039 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1041 UpdateNode un=munadd.getUpdate(0);
1042 String name=(String)updatenames.get(un);
1043 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1045 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1046 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1048 cr.outputline(rd.getSafeSymbol()+"_hash->add("+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1050 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1051 UpdateNode un=munadd.getUpdate(0);
1052 String name=(String)updatenames.get(un);
1053 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1056 SetDescriptor sd=(SetDescriptor)d;
1057 if (sources.setSource(sd)) {
1060 SetDescriptor sourcesd=sources.getSourceSet(sd);
1061 VarDescriptor iterator=VarDescriptor.makeNew("iterator");
1062 cr.outputline(sourcesd.getType().getGenerateType().getSafeSymbol() +" "+newobject.getSafeSymbol()+";");
1063 cr.outputline("for("+iterator.getSafeSymbol()+"="+sourcesd.getSafeSymbol()+"_hash->iterator();"+iterator.getSafeSymbol()+".hasNext();)");
1065 cr.outputline("if !"+sd.getSafeSymbol()+"_hash->contains("+iterator.getSafeSymbol()+"->key())");
1066 cr.outputline(newobject.getSafeSymbol()+"="+iterator.getSafeSymbol()+".key();");
1067 cr.outputline(iterator.getSafeSymbol()+"->next();");
1069 } else if (sources.allocSource(sd)) {
1070 /* Allocation Source*/
1071 sources.generateSourceAlloc(cr,newobject,sd);
1072 } else throw new Error("No source for adding to Set");
1073 cr.outputline(sd.getSafeSymbol()+"_hash->add("+newobject.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1074 UpdateNode un=munadd.getUpdate(0);
1075 String name=(String)updatenames.get(un);
1076 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1082 public void generateinclusionrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
1083 InclusionPredicate ip=(InclusionPredicate) dpred.getPredicate();
1084 boolean negated=dpred.isNegated();
1085 MultUpdateNode mun=getmultupdatenode(conj,dpred,-1);
1086 VarDescriptor leftvar=VarDescriptor.makeNew("left");
1087 ip.expr.generate(cr, leftvar);
1090 if (ip.setexpr instanceof ImageSetExpr) {
1091 ImageSetExpr ise=(ImageSetExpr) ip.setexpr;
1092 VarDescriptor rightvar=ise.getVar();
1093 boolean inverse=ise.inverted();
1094 RelationDescriptor rd=ise.getRelation();
1095 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1096 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1099 cr.outputline(rd.getSafeSymbol() + "_hash->remove((int)" + rightvar + ", (int)" + leftvar + ");");
1101 cr.outputline(rd.getSafeSymbol() + "_hashinv->remove((int)" + leftvar + ", (int)" + rightvar + ");");
1104 cr.outputline(rd.getSafeSymbol() + "_hash->remove((int)" + leftvar + ", (int)" + rightvar + ");");
1106 cr.outputline(rd.getSafeSymbol() + "_hashinv->remove((int)" + rightvar + ", (int)" + leftvar + ");");
1108 for(int i=0;i<state.vRules.size();i++) {
1109 Rule r=(Rule)state.vRules.get(i);
1110 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1111 for(int j=0;j<mun.numUpdates();j++) {
1112 UpdateNode un=mun.getUpdate(i);
1113 if (un.getRule()==r) {
1114 /* Update for rule rule r */
1115 String name=(String)updatenames.get(un);
1117 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1119 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
1126 SetDescriptor sd=ip.setexpr.sd;
1127 cr.outputline(sd.getSafeSymbol() + "_hash->remove((int)" + leftvar + ", (int)" + leftvar + ");");
1129 for(int i=0;i<state.vRules.size();i++) {
1130 Rule r=(Rule)state.vRules.get(i);
1131 if (r.getInclusion().getTargetDescriptors().contains(sd)) {
1132 for(int j=0;j<mun.numUpdates();j++) {
1133 UpdateNode un=mun.getUpdate(i);
1134 if (un.getRule()==r) {
1135 /* Update for rule rule r */
1136 String name=(String)updatenames.get(un);
1137 cr.outputline(repairtable.getSafeSymbol()+"->addset("+sd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1144 /* Generate update */
1145 if (ip.setexpr instanceof ImageSetExpr) {
1146 ImageSetExpr ise=(ImageSetExpr) ip.setexpr;
1147 VarDescriptor rightvar=ise.getVar();
1148 boolean inverse=ise.inverted();
1149 RelationDescriptor rd=ise.getRelation();
1150 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1151 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1154 cr.outputline(rd.getSafeSymbol() + "_hash->add((int)" + rightvar + ", (int)" + leftvar + ");");
1156 cr.outputline(rd.getSafeSymbol() + "_hashinv->add((int)" + leftvar + ", (int)" + rightvar + ");");
1159 cr.outputline(rd.getSafeSymbol() + "_hash->add((int)" + leftvar + ", (int)" + rightvar + ");");
1161 cr.outputline(rd.getSafeSymbol() + "_hashinv->add((int)" + rightvar + ", (int)" + leftvar + ");");
1163 UpdateNode un=mun.getUpdate(0);
1164 String name=(String)updatenames.get(un);
1166 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1168 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+");");
1171 SetDescriptor sd=ip.setexpr.sd;
1172 cr.outputline(sd.getSafeSymbol() + "_hash->add((int)" + leftvar + ", (int)" + leftvar + ");");
1174 UpdateNode un=mun.getUpdate(0);
1175 /* Update for rule rule r */
1176 String name=(String)updatenames.get(un);
1177 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1184 public static Vector getrulelist(Descriptor d) {
1185 Vector dispatchrules = new Vector();
1186 Vector rules = State.currentState.vRules;
1188 for (int i = 0; i < rules.size(); i++) {
1189 Rule rule = (Rule) rules.elementAt(i);
1190 Set requiredsymbols = rule.getRequiredDescriptors();
1192 // #TBD#: in general this is wrong because these descriptors may contain descriptors
1193 // bound in "in?" expressions which need to be dealt with in a topologically sorted
1196 if (rule.getRequiredDescriptors().contains(d)) {
1197 dispatchrules.addElement(rule);
1200 return dispatchrules;
1203 private boolean need_compensation(Rule r) {
1204 GraphNode gn=(GraphNode)termination.scopefalsify.get(r);
1205 for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1206 GraphNode.Edge edge=(GraphNode.Edge)edgeit.next();
1207 GraphNode gn2=edge.getTarget();
1208 if (!removed.contains(gn2)) {
1209 TermNode tn2=(TermNode)gn2.getOwner();
1210 if (tn2.getType()==TermNode.CONSEQUENCE)
1217 private UpdateNode find_compensation(Rule r) {
1218 GraphNode gn=(GraphNode)termination.scopefalsify.get(r);
1219 for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1220 GraphNode.Edge edge=(GraphNode.Edge)edgeit.next();
1221 GraphNode gn2=edge.getTarget();
1222 if (!removed.contains(gn2)) {
1223 TermNode tn2=(TermNode)gn2.getOwner();
1224 if (tn2.getType()==TermNode.UPDATE) {
1225 MultUpdateNode mun=tn2.getUpdate();
1226 for(int i=0;i<mun.numUpdates();i++) {
1227 UpdateNode un=mun.getUpdate(i);
1228 if (un.getRule()==r)
1234 throw new Error("No Compensation Update could be found");
1237 public void generate_dispatch(CodeWriter cr, RelationDescriptor rd, String leftvar, String rightvar) {
1238 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1239 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1241 if (!(usageinvimage||usageimage)) /* not used at all*/
1244 cr.outputline("// RELATION DISPATCH ");
1245 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1247 cr.outputline("!"+oldmodel.getSafeSymbol() +"->"+rd.getJustSafeSymbol()+"_hash->contains("+leftvar+","+rightvar+"))");
1249 cr.outputline("!"+oldmodel.getSafeSymbol() +"->"+rd.getJustSafeSymbol()+"_hashinv->contains("+rightvar+","+leftvar+"))");
1251 /* Adding new item */
1252 /* Perform safety checks */
1253 cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
1254 cr.outputline(repairtable.getSafeSymbol()+"->containsrelation("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+"))");
1256 /* Have update to call into */
1257 VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1259 for(int i=0;i<currentrule.numQuantifiers();i++) {
1260 if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1261 parttype=parttype+", int, int";
1263 parttype=parttype+", int";
1265 cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")=");
1266 cr.outputline("(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")) "+repairtable.getSafeSymbol()+"->getrelation("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1267 String methodcall="("+funptr.getSafeSymbol()+") (this,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
1268 for(int i=0;i<currentrule.numQuantifiers();i++) {
1269 Quantifier q=currentrule.getQuantifier(i);
1270 if (q instanceof SetQuantifier) {
1271 SetQuantifier sq=(SetQuantifier) q;
1272 methodcall+=","+sq.getVar().getSafeSymbol();
1273 } else if (q instanceof RelationQuantifier) {
1274 RelationQuantifier rq=(RelationQuantifier) q;
1275 methodcall+=","+rq.x.getSafeSymbol();
1276 methodcall+=","+rq.y.getSafeSymbol();
1277 } else if (q instanceof ForQuantifier) {
1278 ForQuantifier fq=(ForQuantifier) q;
1279 methodcall+=","+fq.getVar().getSafeSymbol();
1283 cr.outputline(methodcall);
1284 cr.outputline(goodflag.getSafeSymbol()+"=0;");
1285 cr.outputline("continue;");
1288 /* Build standard compensation actions */
1289 if (need_compensation(currentrule)) {
1290 UpdateNode un=find_compensation(currentrule);
1291 String name=(String)updatenames.get(un);
1292 usedupdates.add(un); /* Mark as used */
1293 String methodcall=name+"(this,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
1294 for(int i=0;i<currentrule.numQuantifiers();i++) {
1295 Quantifier q=currentrule.getQuantifier(i);
1296 if (q instanceof SetQuantifier) {
1297 SetQuantifier sq=(SetQuantifier) q;
1298 methodcall+=","+sq.getVar().getSafeSymbol();
1299 } else if (q instanceof RelationQuantifier) {
1300 RelationQuantifier rq=(RelationQuantifier) q;
1301 methodcall+=","+rq.x.getSafeSymbol();
1302 methodcall+=","+rq.y.getSafeSymbol();
1303 } else if (q instanceof ForQuantifier) {
1304 ForQuantifier fq=(ForQuantifier) q;
1305 methodcall+=","+fq.getVar().getSafeSymbol();
1309 cr.outputline(methodcall);
1310 cr.outputline(goodflag.getSafeSymbol()+"=0;");
1311 cr.outputline("continue;");
1316 String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1317 cr.outputline("int " + addeditem + ";");
1318 if (rd.testUsage(RelationDescriptor.IMAGE)) {
1319 cr.outputline(addeditem + " = " + rd.getSafeSymbol() + "_hash->add((int)" + leftvar + ", (int)" + rightvar + ");");
1322 if (rd.testUsage(RelationDescriptor.INVIMAGE)) {
1323 cr.outputline(addeditem + " = " + rd.getSafeSymbol() + "_hashinv->add((int)" + rightvar + ", (int)" + leftvar + ");");
1326 cr.outputline("if (" + addeditem + ")");
1329 Vector dispatchrules = getrulelist(rd);
1331 if (dispatchrules.size() == 0) {
1332 cr.outputline("// nothing to dispatch");
1337 for(int i = 0; i < dispatchrules.size(); i++) {
1338 Rule rule = (Rule) dispatchrules.elementAt(i);
1339 if (rule.getGuardExpr().getRequiredDescriptors().contains(rd)) {
1340 /* Guard depends on this relation, so we recomput everything */
1341 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+",-1,0,0);");
1343 for (int j=0;j<rule.numQuantifiers();j++) {
1344 Quantifier q=rule.getQuantifier(j);
1345 if (q.getRequiredDescriptors().contains(rd)) {
1347 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+","+j+","+leftvar+","+rightvar+");");
1356 public void generate_dispatch(CodeWriter cr, SetDescriptor sd, String setvar) {
1358 cr.outputline("// SET DISPATCH ");
1360 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1361 cr.outputline("!"+oldmodel.getSafeSymbol() +"->"+sd.getJustSafeSymbol()+"_hash->contains("+setvar+"))");
1363 /* Adding new item */
1364 /* Perform safety checks */
1365 cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
1366 cr.outputline(repairtable.getSafeSymbol()+"->containsset("+sd.getNum()+","+currentrule.getNum()+","+setvar+"))");
1368 /* Have update to call into */
1369 VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1371 for(int i=0;i<currentrule.numQuantifiers();i++) {
1372 if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1373 parttype=parttype+", int, int";
1375 parttype=parttype+", int";
1377 cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")=");
1378 cr.outputline("(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")) "+repairtable.getSafeSymbol()+"->getset("+sd.getNum()+","+currentrule.getNum()+","+setvar+");");
1379 String methodcall="("+funptr.getSafeSymbol()+") (this,"+oldmodel.getSafeSymbol()+","+
1380 repairtable.getSafeSymbol();
1381 for(int i=0;i<currentrule.numQuantifiers();i++) {
1382 Quantifier q=currentrule.getQuantifier(i);
1383 if (q instanceof SetQuantifier) {
1384 SetQuantifier sq=(SetQuantifier) q;
1385 methodcall+=","+sq.getVar().getSafeSymbol();
1386 } else if (q instanceof RelationQuantifier) {
1387 RelationQuantifier rq=(RelationQuantifier) q;
1388 methodcall+=","+rq.x.getSafeSymbol();
1389 methodcall+=","+rq.y.getSafeSymbol();
1390 } else if (q instanceof ForQuantifier) {
1391 ForQuantifier fq=(ForQuantifier) q;
1392 methodcall+=","+fq.getVar().getSafeSymbol();
1396 cr.outputline(methodcall);
1397 cr.outputline(goodflag.getSafeSymbol()+"=0;");
1398 cr.outputline("continue;");
1401 /* Build standard compensation actions */
1402 if (need_compensation(currentrule)) {
1403 UpdateNode un=find_compensation(currentrule);
1404 String name=(String)updatenames.get(un);
1405 usedupdates.add(un); /* Mark as used */
1407 String methodcall=name+"(this,"+oldmodel.getSafeSymbol()+","+
1408 repairtable.getSafeSymbol();
1409 for(int i=0;i<currentrule.numQuantifiers();i++) {
1410 Quantifier q=currentrule.getQuantifier(i);
1411 if (q instanceof SetQuantifier) {
1412 SetQuantifier sq=(SetQuantifier) q;
1413 methodcall+=","+sq.getVar().getSafeSymbol();
1414 } else if (q instanceof RelationQuantifier) {
1415 RelationQuantifier rq=(RelationQuantifier) q;
1416 methodcall+=","+rq.x.getSafeSymbol();
1417 methodcall+=","+rq.y.getSafeSymbol();
1418 } else if (q instanceof ForQuantifier) {
1419 ForQuantifier fq=(ForQuantifier) q;
1420 methodcall+=","+fq.getVar().getSafeSymbol();
1424 cr.outputline(methodcall);
1425 cr.outputline(goodflag.getSafeSymbol()+"=0;");
1426 cr.outputline("continue;");
1431 String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1432 cr.outputline("int " + addeditem + " = 1;");
1433 cr.outputline(addeditem + " = " + sd.getSafeSymbol() + "_hash->add((int)" + setvar + ", (int)" + setvar + ");");
1435 Vector dispatchrules = getrulelist(sd);
1437 if (dispatchrules.size() == 0) {
1438 cr.outputline("// nothing to dispatch");
1443 for(int i = 0; i < dispatchrules.size(); i++) {
1444 Rule rule = (Rule) dispatchrules.elementAt(i);
1445 if (SetDescriptor.expand(rule.getGuardExpr().getRequiredDescriptors()).contains(sd)) {
1446 /* Guard depends on this relation, so we recompute everything */
1447 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+",-1,0,0);");
1449 for (int j=0;j<rule.numQuantifiers();j++) {
1450 Quantifier q=rule.getQuantifier(j);
1451 if (SetDescriptor.expand(q.getRequiredDescriptors()).contains(sd)) {
1453 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+","+j+","+setvar+",0);");