8 public class RepairGenerator {
10 java.io.PrintWriter outputrepair = null;
11 java.io.PrintWriter outputaux = null;
12 java.io.PrintWriter outputhead = null;
15 static VarDescriptor oldmodel=null;
16 static VarDescriptor newmodel=null;
17 static VarDescriptor worklist=null;
18 static VarDescriptor repairtable=null;
19 static VarDescriptor goodflag=null;
20 Rule currentrule=null;
21 Hashtable updatenames;
23 Termination termination;
26 static boolean DEBUG=false;
28 ModelRuleDependence mrd;
30 public RepairGenerator(State state, Termination t) {
32 updatenames=new Hashtable();
33 usedupdates=new HashSet();
36 togenerate=new HashSet();
37 togenerate.addAll(termination.conjunctions);
39 togenerate.removeAll(removed);
40 GraphNode.computeclosure(togenerate,removed);
42 mrd=ModelRuleDependence.doAnalysis(state);
43 Repair.repairgenerator=this;
46 private void generatetypechecks(boolean flag) {
48 DotExpr.DOTYPECHECKS=true;
49 VarExpr.DOTYPECHECKS=true;
53 VarExpr.DOTYPECHECKS=false;
54 DotExpr.DOTYPECHECKS=false;
61 private void name_updates() {
63 for(Iterator it=termination.updatenodes.iterator();it.hasNext();) {
64 GraphNode gn=(GraphNode) it.next();
65 TermNode tn=(TermNode) gn.getOwner();
66 MultUpdateNode mun=tn.getUpdate();
67 if (togenerate.contains(gn))
68 for (int i=0;i<mun.numUpdates();i++) {
69 UpdateNode un=mun.getUpdate(i);
70 String name="update"+String.valueOf(count++);
71 updatenames.put(un,name);
76 public void generate(OutputStream outputrepair, OutputStream outputaux,OutputStream outputhead, String st) {
77 this.outputrepair = new java.io.PrintWriter(outputrepair, true);
78 this.outputaux = new java.io.PrintWriter(outputaux, true);
79 this.outputhead = new java.io.PrintWriter(outputhead, true);
82 generatetypechecks(true);
83 generate_tokentable();
84 generate_hashtables();
85 generate_stateobject();
89 if (!Compiler.REPAIR||Compiler.GENERATEDEBUGPRINT) {
94 CodeWriter crhead = new StandardCodeWriter(this.outputhead);
95 CodeWriter craux = new StandardCodeWriter(this.outputaux);
96 crhead.outputline("};");
97 craux.outputline("}");
99 if (Compiler.GENERATEDEBUGHOOKS) {
100 crhead.outputline("void debughook();");
101 craux.outputline("void debughook() {}");
103 generatetypechecks(false);
104 generate_computesizes();
105 generatetypechecks(true);
106 generate_recomputesizes();
107 generatetypechecks(false);
109 StructureGenerator sg=new StructureGenerator(state,this);
111 crhead.outputline("#endif");
114 String ststate="state";
115 String stmodel="model";
116 String strepairtable="repairtable";
117 String stleft="left";
118 String stright="right";
119 String stnew="newvalue";
121 private void generate_updates() {
123 CodeWriter crhead = new StandardCodeWriter(outputhead);
124 CodeWriter craux = new StandardCodeWriter(outputaux);
125 RelationDescriptor.prefix = "model->";
126 SetDescriptor.prefix = "model->";
128 /* Rewrite globals */
130 for (Iterator it=this.state.stGlobals.descriptors();it.hasNext();) {
131 VarDescriptor vd=(VarDescriptor)it.next();
132 craux.outputline("#define "+vd.getSafeSymbol()+" "+ststate+"->"+vd.getSafeSymbol());
135 for(Iterator it=termination.updatenodes.iterator();it.hasNext();) {
136 GraphNode gn=(GraphNode) it.next();
137 TermNode tn=(TermNode) gn.getOwner();
138 MultUpdateNode mun=tn.getUpdate();
139 boolean isrelation=(mun.getDescriptor() instanceof RelationDescriptor);
140 if (togenerate.contains(gn))
141 for (int i=0;i<mun.numUpdates();i++) {
142 UpdateNode un=mun.getUpdate(i);
143 String methodname=(String)updatenames.get(un);
146 case MultUpdateNode.ADD:
148 crhead.outputline("void "+methodname+"("+name+"_state * " +ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable+", int "+stleft+", int "+stright+");");
149 craux.outputline("void "+methodname+"("+name+"_state * "+ ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable+", int "+stleft+", int "+stright+")");
151 crhead.outputline("void "+methodname+"("+name+"_state * "+ ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable+", int "+stleft+");");
152 craux.outputline("void "+methodname+"("+name+"_state * "+ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable+", int "+stleft+")");
155 craux.outputline("int maybe=0;");
156 if (Compiler.GENERATEINSTRUMENT)
157 craux.outputline("updatecount++;");
159 final SymbolTable st = un.getRule().getSymbolTable();
160 CodeWriter cr = new StandardCodeWriter(outputaux) {
161 public SymbolTable getSymbolTable() { return st; }
163 un.generate(cr, false, false, stleft,stright, null,this);
164 craux.outputline("if (maybe) printf(\"REALLY BAD\");");
167 case MultUpdateNode.REMOVE: {
169 String methodcall="void "+methodname+"("+name+"_state * "+ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable;
170 for(int j=0;j<r.numQuantifiers();j++) {
171 Quantifier q=r.getQuantifier(j);
172 if (q instanceof SetQuantifier) {
173 SetQuantifier sq=(SetQuantifier) q;
174 methodcall+=","+sq.getVar().getType().getGenerateType().getSafeSymbol()+" "+sq.getVar().getSafeSymbol();
175 } else if (q instanceof RelationQuantifier) {
176 RelationQuantifier rq=(RelationQuantifier) q;
178 methodcall+=","+rq.x.getType().getGenerateType().getSafeSymbol()+" "+rq.x.getSafeSymbol();
179 methodcall+=","+rq.y.getType().getGenerateType().getSafeSymbol()+" "+rq.y.getSafeSymbol();
180 } else if (q instanceof ForQuantifier) {
181 ForQuantifier fq=(ForQuantifier) q;
182 methodcall+=",int "+fq.getVar().getSafeSymbol();
186 crhead.outputline(methodcall+";");
187 craux.outputline(methodcall);
189 craux.outputline("int maybe=0;");
190 if (Compiler.GENERATEINSTRUMENT)
191 craux.outputline("updatecount++;");
192 final SymbolTable st2 = un.getRule().getSymbolTable();
193 CodeWriter cr2 = new StandardCodeWriter(outputaux) {
194 public SymbolTable getSymbolTable() { return st2; }
196 un.generate(cr2, true, false, null,null, null,this);
197 craux.outputline("if (maybe) printf(\"REALLY BAD\");");
201 case MultUpdateNode.MODIFY: {
203 String methodcall="void "+methodname+"("+name+"_state * "+ststate+","+name+" * "+stmodel+", RepairHash * "+strepairtable;
204 for(int j=0;j<r.numQuantifiers();j++) {
205 Quantifier q=r.getQuantifier(j);
206 if (q instanceof SetQuantifier) {
207 SetQuantifier sq=(SetQuantifier) q;
208 methodcall+=","+sq.getVar().getType().getGenerateType().getSafeSymbol()+" "+sq.getVar().getSafeSymbol();
209 } else if (q instanceof RelationQuantifier) {
210 RelationQuantifier rq=(RelationQuantifier) q;
212 methodcall+=", "+rq.x.getType().getGenerateType().getSafeSymbol()+" "+rq.x.getSafeSymbol();
213 methodcall+=", "+rq.y.getType().getGenerateType().getSafeSymbol()+" "+rq.y.getSafeSymbol();
214 } else if (q instanceof ForQuantifier) {
215 ForQuantifier fq=(ForQuantifier) q;
216 methodcall+=", int "+fq.getVar().getSafeSymbol();
219 methodcall+=", int "+stleft+", int "+stright+", int "+stnew;
221 crhead.outputline(methodcall+";");
222 craux.outputline(methodcall);
224 craux.outputline("int maybe=0;");
225 if (Compiler.GENERATEINSTRUMENT)
226 craux.outputline("updatecount++;");
227 final SymbolTable st2 = un.getRule().getSymbolTable();
228 CodeWriter cr2 = new StandardCodeWriter(outputaux) {
229 public SymbolTable getSymbolTable() { return st2; }
231 un.generate(cr2, false, true, stleft, stright, stnew, this);
232 craux.outputline("if (maybe) printf(\"REALLY BAD\");");
238 throw new Error("Nonimplement Update");
244 private void generate_call() {
245 CodeWriter cr = new StandardCodeWriter(outputrepair);
246 VarDescriptor vdstate=VarDescriptor.makeNew("repairstate");
247 cr.outputline(name+"_state * "+vdstate.getSafeSymbol()+"=new "+name+"_state();");
248 Iterator globals=state.stGlobals.descriptors();
249 while (globals.hasNext()) {
250 VarDescriptor vd=(VarDescriptor) globals.next();
251 cr.outputline(vdstate.getSafeSymbol()+"->"+vd.getSafeSymbol()+"=("+vd.getType().getGenerateType().getSafeSymbol()+")"+vd.getSafeSymbol()+";");
253 /* Insert repair here */
254 cr.outputline(vdstate.getSafeSymbol()+"->doanalysis();");
255 globals=state.stGlobals.descriptors();
256 while (globals.hasNext()) {
257 VarDescriptor vd=(VarDescriptor) globals.next();
258 cr.outputline("*(("+vd.getType().getGenerateType().getSafeSymbol()+"*) &"+vd.getSafeSymbol()+")="+vdstate.getSafeSymbol()+"->"+vd.getSafeSymbol()+";");
260 cr.outputline("delete "+vdstate.getSafeSymbol()+";");
263 private void generate_tokentable() {
264 CodeWriter cr = new StandardCodeWriter(outputrepair);
265 Iterator tokens = TokenLiteralExpr.tokens.keySet().iterator();
268 cr.outputline("// Token values");
271 while (tokens.hasNext()) {
272 Object token = tokens.next();
273 cr.outputline("// " + token.toString() + " = " + TokenLiteralExpr.tokens.get(token).toString());
280 private void generate_stateobject() {
281 CodeWriter crhead = new StandardCodeWriter(outputhead);
282 crhead.outputline("class "+name+"_state {");
283 crhead.outputline("public:");
284 Iterator globals=state.stGlobals.descriptors();
285 while (globals.hasNext()) {
286 VarDescriptor vd=(VarDescriptor) globals.next();
287 crhead.outputline(vd.getType().getGenerateType().getSafeSymbol()+" "+vd.getSafeSymbol()+";");
289 crhead.outputline("void computesizes(int *,int **);");
290 crhead.outputline("void recomputesizes();");
293 private void generate_computesizes() {
294 int max=TypeDescriptor.counter;
295 TypeDescriptor[] tdarray=new TypeDescriptor[max];
296 for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
297 TypeDescriptor ttd=(TypeDescriptor)it.next();
298 tdarray[ttd.getId()]=ttd;
300 final SymbolTable st = state.stGlobals;
301 CodeWriter cr = new StandardCodeWriter(outputaux) {
302 public SymbolTable getSymbolTable() { return st; }
305 cr.outputline("void "+name+"_state::computesizes(int *sizearray,int **numele) {");
306 cr.outputline("int maybe=0;");
307 for(int i=0;i<max;i++) {
308 TypeDescriptor td=tdarray[i];
309 Expr size=td.getSizeExpr();
310 VarDescriptor vd=VarDescriptor.makeNew("size");
311 size.generate(cr,vd);
312 cr.outputline("sizearray["+i+"]="+vd.getSafeSymbol()+";");
314 for(int i=0;i<max;i++) {
315 TypeDescriptor td=tdarray[i];
316 if (td instanceof StructureTypeDescriptor) {
317 StructureTypeDescriptor std=(StructureTypeDescriptor) td;
318 for(int j=0;j<std.fieldlist.size();j++) {
319 FieldDescriptor fd=(FieldDescriptor)std.fieldlist.get(j);
320 if (fd instanceof ArrayDescriptor) {
321 ArrayDescriptor ad=(ArrayDescriptor)fd;
322 Expr index=ad.getIndexBound();
323 VarDescriptor vd=VarDescriptor.makeNew("index");
324 index.generate(cr,vd);
325 cr.outputline("numele["+i+"]["+j+"]="+vd.getSafeSymbol()+";");
330 cr.outputline("if (maybe) printf(\"BAD ERROR\");");
334 private void generate_recomputesizes() {
335 int max=TypeDescriptor.counter;
336 TypeDescriptor[] tdarray=new TypeDescriptor[max];
337 for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
338 TypeDescriptor ttd=(TypeDescriptor)it.next();
339 tdarray[ttd.getId()]=ttd;
341 final SymbolTable st = state.stGlobals;
342 CodeWriter cr = new StandardCodeWriter(outputaux) {
343 public SymbolTable getSymbolTable() { return st; }
345 cr.outputline("void "+name+"_state::recomputesizes() {");
346 cr.outputline("int maybe=0;");
347 for(int i=0;i<max;i++) {
348 TypeDescriptor td=tdarray[i];
349 Expr size=td.getSizeExpr();
350 VarDescriptor vd=VarDescriptor.makeNew("size");
351 size.generate(cr,vd);
353 for(int i=0;i<max;i++) {
354 TypeDescriptor td=tdarray[i];
355 if (td instanceof StructureTypeDescriptor) {
356 StructureTypeDescriptor std=(StructureTypeDescriptor) td;
357 for(int j=0;j<std.fieldlist.size();j++) {
358 FieldDescriptor fd=(FieldDescriptor)std.fieldlist.get(j);
359 if (fd instanceof ArrayDescriptor) {
360 ArrayDescriptor ad=(ArrayDescriptor)fd;
361 Expr index=ad.getIndexBound();
362 VarDescriptor vd=VarDescriptor.makeNew("index");
363 index.generate(cr,vd);
368 cr.outputline("if (maybe) printf(\"BAD ERROR\");");
373 private void generate_hashtables() {
374 CodeWriter craux = new StandardCodeWriter(outputaux);
375 CodeWriter crhead = new StandardCodeWriter(outputhead);
376 crhead.outputline("#ifndef "+name+"_h");
377 crhead.outputline("#define "+name+"_h");
378 crhead.outputline("#include \"SimpleHash.h\"");
379 crhead.outputline("extern \"C\" {");
380 crhead.outputline("#include \"instrument.h\"");
381 crhead.outputline("}");
382 crhead.outputline("#include <stdio.h>");
383 crhead.outputline("#include <stdlib.h>");
384 crhead.outputline("class "+name+" {");
385 crhead.outputline("public:");
386 crhead.outputline(name+"();");
387 crhead.outputline("~"+name+"();");
388 craux.outputline("#include \""+headername+"\"");
389 craux.outputline("#include \"size.h\"");
391 craux.outputline(name+"::"+name+"() {");
392 craux.outputline("// creating hashtables ");
395 Iterator sets = state.stSets.descriptors();
397 /* first pass create all the hash tables */
398 while (sets.hasNext()) {
399 SetDescriptor set = (SetDescriptor) sets.next();
400 crhead.outputline("SimpleHash* " + set.getSafeSymbol() + "_hash;");
401 craux.outputline(set.getSafeSymbol() + "_hash = new SimpleHash();");
404 /* second pass build relationships between hashtables */
405 sets = state.stSets.descriptors();
407 while (sets.hasNext()) {
408 SetDescriptor set = (SetDescriptor) sets.next();
409 Iterator subsets = set.subsets();
411 while (subsets.hasNext()) {
412 SetDescriptor subset = (SetDescriptor) subsets.next();
413 craux.outputline(subset.getSafeSymbol() + "_hash->addParent(" + set.getSafeSymbol() + "_hash);");
417 /* build relations */
418 Iterator relations = state.stRelations.descriptors();
420 /* first pass create all the hash tables */
421 while (relations.hasNext()) {
422 RelationDescriptor relation = (RelationDescriptor) relations.next();
424 if (relation.testUsage(RelationDescriptor.IMAGE)) {
425 crhead.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hash;");
426 craux.outputline(relation.getSafeSymbol() + "_hash = new SimpleHash();");
429 if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
430 crhead.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hashinv;");
431 craux.outputline(relation.getSafeSymbol() + "_hashinv = new SimpleHash();");
435 craux.outputline("}");
436 crhead.outputline("};");
437 craux.outputline(name+"::~"+name+"() {");
438 craux.outputline("// deleting hashtables");
440 /* build destructor */
441 sets = state.stSets.descriptors();
443 /* first pass create all the hash tables */
444 while (sets.hasNext()) {
445 SetDescriptor set = (SetDescriptor) sets.next();
446 craux.outputline("delete "+set.getSafeSymbol() + "_hash;");
449 /* destroy relations */
450 relations = state.stRelations.descriptors();
452 /* first pass create all the hash tables */
453 while (relations.hasNext()) {
454 RelationDescriptor relation = (RelationDescriptor) relations.next();
456 if (relation.testUsage(RelationDescriptor.IMAGE)) {
457 craux.outputline("delete "+relation.getSafeSymbol() + "_hash;");
460 if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
461 craux.outputline("delete " + relation.getSafeSymbol() + "_hashinv;");
464 craux.outputline("}");
467 private void generate_start() {
468 CodeWriter crhead = new StandardCodeWriter(outputhead);
469 CodeWriter craux = new StandardCodeWriter(outputaux);
470 oldmodel=VarDescriptor.makeNew("oldmodel");
471 newmodel=VarDescriptor.makeNew("newmodel");
472 worklist=VarDescriptor.makeNew("worklist");
473 goodflag=VarDescriptor.makeNew("goodflag");
474 repairtable=VarDescriptor.makeNew("repairtable");
476 if (Compiler.GENERATEINSTRUMENT) {
477 craux.outputline("int updatecount;");
478 craux.outputline("int rebuildcount;");
479 craux.outputline("int abstractcount;");
482 crhead.outputline("void doanalysis();");
483 craux.outputline("void "+name +"_state::doanalysis()");
485 if (Compiler.GENERATEINSTRUMENT) {
486 craux.outputline("updatecount=0;");
487 craux.outputline("rebuildcount=0;");
488 craux.outputline("abstractcount=0;");
490 craux.outputline("int highmark;");
491 craux.outputline("initializestack(&highmark);");
492 craux.outputline("typeobject *typeobject1=gettypeobject();");
493 craux.outputline("typeobject1->computesizes(this);");
494 craux.outputline("recomputesizes();");
495 craux.outputline(name+ " * "+oldmodel.getSafeSymbol()+"=0;");
496 craux.outputline("WorkList * "+worklist.getSafeSymbol()+" = new WorkList();");
497 craux.outputline("RepairHash * "+repairtable.getSafeSymbol()+"=0;");
498 craux.outputline("while (1)");
500 craux.outputline(name+ " * "+newmodel.getSafeSymbol()+"=new "+name+"();");
501 craux.outputline(worklist.getSafeSymbol()+"->reset();");
502 if (Compiler.GENERATEINSTRUMENT)
503 craux.outputline("rebuildcount++;");
506 private void generate_teardown() {
507 CodeWriter cr = new StandardCodeWriter(outputaux);
509 if (Compiler.GENERATEINSTRUMENT) {
510 cr.outputline("printf(\"updatecount=%d\\n\",updatecount);");
511 cr.outputline("printf(\"rebuildcount=%d\\n\",rebuildcount);");
512 cr.outputline("printf(\"abstractcount=%d\\n\",abstractcount);");
517 private void generate_print() {
519 final SymbolTable st = new SymbolTable();
521 CodeWriter cr = new StandardCodeWriter(outputaux) {
522 public SymbolTable getSymbolTable() { return st; }
525 cr.outputline("// printing sets!");
526 cr.outputline("printf(\"\\n\\nPRINTING SETS AND RELATIONS\\n\");");
528 Iterator setiterator = state.stSets.descriptors();
529 while (setiterator.hasNext()) {
530 SetDescriptor sd = (SetDescriptor) setiterator.next();
531 if (sd.getSymbol().equals("int") || sd.getSymbol().equals("token")) {
535 String setname = sd.getSafeSymbol();
538 cr.outputline("// printing set " + setname);
539 cr.outputline("printf(\"\\nPrinting set " + sd.getSymbol() + " - %d elements \\n\", " + setname + "_hash->count());");
540 cr.outputline("SimpleIterator __setiterator;");
541 cr.outputline("" + setname + "_hash->iterator(__setiterator);");
542 cr.outputline("while (__setiterator.hasNext())");
544 cr.outputline("int __setval = (int) __setiterator.next();");
546 TypeDescriptor td = sd.getType();
547 if (td instanceof StructureTypeDescriptor) {
548 StructureTypeDescriptor std = (StructureTypeDescriptor) td;
549 VarDescriptor vd = new VarDescriptor ("__setval", "__setval", td, false);
550 std.generate_printout(cr, vd);
551 } else { // Missing type descriptor or reserved type, just print int
552 cr.outputline("printf(\"<%d> \", __setval);");
560 cr.outputline("printf(\"\\n\\n------------------- END PRINTING\\n\");");
564 private void generate_rules() {
565 /* first we must sort the rules */
566 RelationDescriptor.prefix = newmodel.getSafeSymbol()+"->";
567 SetDescriptor.prefix = newmodel.getSafeSymbol()+"->";
568 System.out.println("SCC="+(mrd.numSCC()-1));
569 for(int sccindex=0;sccindex<mrd.numSCC();sccindex++) {
570 ruleset=mrd.getSCC(sccindex);
571 boolean needworklist=mrd.hasCycle(sccindex);
574 Iterator iterator_rs = ruleset.iterator();
575 while (iterator_rs.hasNext()) {
576 Rule rule = (Rule) iterator_rs.next();
577 if (rule.getnogenerate())
580 final SymbolTable st = rule.getSymbolTable();
581 CodeWriter cr = new StandardCodeWriter(outputaux) {
582 public SymbolTable getSymbolTable() { return st; }
584 InvariantValue ivalue=new InvariantValue();
585 cr.setInvariantValue(ivalue);
587 cr.outputline("// build " +escape(rule.toString()));
589 cr.outputline("int maybe=0;");
591 Expr ruleexpr=rule.getGuardExpr();
592 HashSet invariantvars=new HashSet();
593 Set invariants=ruleexpr.findInvariants(invariantvars);
595 if ((ruleexpr instanceof BooleanLiteralExpr)&&
596 ((BooleanLiteralExpr)ruleexpr).getValue()) {
597 if (rule.getInclusion() instanceof SetInclusion) {
598 invariants.addAll(((SetInclusion)rule.getInclusion()).getExpr().findInvariants(invariantvars));
599 } else if (rule.getInclusion() instanceof RelationInclusion) {
600 invariants.addAll(((RelationInclusion)rule.getInclusion()).getLeftExpr().findInvariants(invariantvars));
601 invariants.addAll(((RelationInclusion)rule.getInclusion()).getRightExpr().findInvariants(invariantvars));
604 ListIterator quantifiers = rule.quantifiers();
605 while (quantifiers.hasNext()) {
606 Quantifier quantifier = (Quantifier) quantifiers.next();
607 if (quantifier instanceof ForQuantifier) {
608 ForQuantifier fq=(ForQuantifier)quantifier;
609 invariants.addAll(fq.lower.findInvariants(invariantvars));
610 invariants.addAll(fq.upper.findInvariants(invariantvars));
614 for(Iterator invit=invariants.iterator();invit.hasNext();) {
615 Expr invexpr=(Expr)invit.next();
616 VarDescriptor tmpvd=VarDescriptor.makeNew("tmpvar");
617 VarDescriptor maybevd=VarDescriptor.makeNew("maybevar");
618 invexpr.generate(cr,tmpvd);
619 cr.outputline("int "+maybevd.getSafeSymbol()+"=maybe;");
620 cr.outputline("maybe=0;");
621 ivalue.assignPair(invexpr,tmpvd,maybevd);
624 quantifiers = rule.quantifiers();
625 while (quantifiers.hasNext()) {
626 Quantifier quantifier = (Quantifier) quantifiers.next();
627 quantifier.generate_open(cr);
632 rule.getGuardExpr().prettyPrint(cr);
635 /* now we have to generate the guard test */
636 VarDescriptor guardval = VarDescriptor.makeNew();
637 rule.getGuardExpr().generate(cr, guardval);
638 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
641 /* now we have to generate the inclusion code */
643 rule.getInclusion().generate(cr);
645 while (quantifiers.hasPrevious()) {
646 Quantifier quantifier = (Quantifier) quantifiers.previous();
655 CodeWriter cr2 = new StandardCodeWriter(outputaux);
657 for(Iterator initialworklist=ruleset.iterator();initialworklist.hasNext();) {
658 /** Construct initial worklist set */
659 Rule rule=(Rule)initialworklist.next();
660 cr2.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+",-1,0,0);");
663 cr2.outputline("while ("+worklist.getSafeSymbol()+"->hasMoreElements())");
665 VarDescriptor idvar=VarDescriptor.makeNew("id");
666 cr2.outputline("int "+idvar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->getid();");
668 String elseladder = "if";
670 Iterator iterator_rules = ruleset.iterator();
671 while (iterator_rules.hasNext()) {
673 Rule rule = (Rule) iterator_rules.next();
674 int dispatchid = rule.getNum();
677 final SymbolTable st = rule.getSymbolTable();
678 CodeWriter cr = new StandardCodeWriter(outputaux) {
679 public SymbolTable getSymbolTable() { return st; }
683 cr.outputline(elseladder + " ("+idvar.getSafeSymbol()+" == " + dispatchid + ")");
685 cr.outputline("int maybe=0;");
686 VarDescriptor typevar=VarDescriptor.makeNew("type");
687 VarDescriptor leftvar=VarDescriptor.makeNew("left");
688 VarDescriptor rightvar=VarDescriptor.makeNew("right");
689 cr.outputline("int "+typevar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->gettype();");
690 cr.outputline("int "+leftvar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->getlvalue();");
691 cr.outputline("int "+rightvar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->getrvalue();");
692 cr.outputline("// build " +escape(rule.toString()));
695 for (int j=0;j<rule.numQuantifiers();j++) {
696 Quantifier quantifier = rule.getQuantifier(j);
697 quantifier.generate_open(cr, typevar.getSafeSymbol(),j,leftvar.getSafeSymbol(),rightvar.getSafeSymbol());
703 rule.getGuardExpr().prettyPrint(cr);
706 /* now we have to generate the guard test */
708 VarDescriptor guardval = VarDescriptor.makeNew();
709 rule.getGuardExpr().generate(cr, guardval);
711 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
714 /* now we have to generate the inclusion code */
716 rule.getInclusion().generate(cr);
719 for (int j=0;j<rule.numQuantifiers();j++) {
723 // close startblocks generated by DotExpr memory checks
724 //DotExpr.generate_memory_endblocks(cr);
726 cr.endblock(); // end else-if WORKLIST ladder
728 elseladder = "else if";
731 cr2.outputline("else");
733 cr2.outputline("printf(\"VERY BAD !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\\n\\n\");");
734 cr2.outputline("exit(1);");
736 // end block created for worklist
737 cr2.outputline(worklist.getSafeSymbol()+"->pop();");
743 public static String escape(String s) {
745 for(int i=0;i<s.length();i++) {
755 private void generate_checks() {
756 /* do constraint checks */
759 i=termination.constraintdependence.computeOrdering().iterator();
761 i=state.vConstraints.iterator();
762 for (; i.hasNext();) {
763 Constraint constraint;
765 constraint= (Constraint) ((GraphNode)i.next()).getOwner();
767 constraint=(Constraint)i.next();
770 final SymbolTable st = constraint.getSymbolTable();
771 CodeWriter cr = new StandardCodeWriter(outputaux);
772 cr.pushSymbolTable(constraint.getSymbolTable());
774 cr.outputline("// checking " + escape(constraint.toString()));
777 ListIterator quantifiers = constraint.quantifiers();
779 while (quantifiers.hasNext()) {
780 Quantifier quantifier = (Quantifier) quantifiers.next();
781 quantifier.generate_open(cr);
784 cr.outputline("int maybe = 0;");
786 /* now we have to generate the guard test */
788 VarDescriptor constraintboolean = VarDescriptor.makeNew("constraintboolean");
789 constraint.getLogicStatement().generate(cr, constraintboolean);
791 cr.outputline("if (maybe)");
793 cr.outputline("printf(\"maybe fail " + escape(constraint.toString()) + ". \\n\");");
794 //cr.outputline("exit(1);");
797 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
799 if (!Compiler.REPAIR||Compiler.GENERATEDEBUGHOOKS)
800 cr.outputline("printf(\"fail " + escape(constraint.toString()) + ". \\n\");");
802 if (Compiler.REPAIR) {
804 /* Build new repair table */
805 cr.outputline("if ("+repairtable.getSafeSymbol()+")");
806 cr.outputline("delete "+repairtable.getSafeSymbol()+";");
807 cr.outputline(repairtable.getSafeSymbol()+"=new RepairHash();");
809 if (Compiler.GENERATEDEBUGHOOKS)
810 cr.outputline("debughook();");
811 /* Compute cost of each repair */
812 VarDescriptor mincost=VarDescriptor.makeNew("mincost");
813 VarDescriptor mincostindex=VarDescriptor.makeNew("mincostindex");
814 Vector dnfconst=new Vector();
815 dnfconst.addAll((Set)termination.conjunctionmap.get(constraint));
817 if (dnfconst.size()<=1) {
818 cr.outputline("int "+mincostindex.getSafeSymbol()+"=0;");
820 if (dnfconst.size()>1) {
821 cr.outputline("int "+mincostindex.getSafeSymbol()+";");
823 for(int j=0;j<dnfconst.size();j++) {
824 GraphNode gn=(GraphNode)dnfconst.get(j);
825 Conjunction conj=((TermNode)gn.getOwner()).getConjunction();
826 if (removed.contains(gn))
829 VarDescriptor costvar;
833 costvar=VarDescriptor.makeNew("cost");
834 for(int k=0;k<conj.size();k++) {
835 DNFPredicate dpred=conj.get(k);
836 Predicate p=dpred.getPredicate();
837 boolean negate=dpred.isNegated();
838 VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
839 p.generate(cr,predvalue);
841 cr.outputline("int "+costvar.getSafeSymbol()+"=0;");
844 cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
846 cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
847 cr.outputline(costvar.getSafeSymbol()+"+="+cost.getCost(dpred)+";");
851 cr.outputline("if ("+costvar.getSafeSymbol()+"<"+mincost.getSafeSymbol()+")");
853 cr.outputline(mincost.getSafeSymbol()+"="+costvar.getSafeSymbol()+";");
854 cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
857 cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
861 cr.outputline("switch("+mincostindex.getSafeSymbol()+") {");
862 for(int j=0;j<dnfconst.size();j++) {
863 GraphNode gn=(GraphNode)dnfconst.get(j);
864 Conjunction conj=((TermNode)gn.getOwner()).getConjunction();
865 if (removed.contains(gn))
867 cr.outputline("case "+j+":");
869 for(int k=0;k<conj.size();k++) {
870 DNFPredicate dpred=conj.get(k);
871 Predicate p=dpred.getPredicate();
872 boolean negate=dpred.isNegated();
873 VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
874 p.generate(cr,predvalue);
876 cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
878 cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
880 if (Compiler.GENERATEINSTRUMENT)
881 cr.outputline("abstractcount++;");
882 if (p instanceof InclusionPredicate)
883 generateinclusionrepair(conj,dpred, cr);
884 else if (p instanceof ExprPredicate) {
885 ExprPredicate ep=(ExprPredicate)p;
886 if (ep.getType()==ExprPredicate.SIZE)
887 generatesizerepair(conj,dpred,cr);
888 else if (ep.getType()==ExprPredicate.COMPARISON)
889 generatecomparisonrepair(conj,dpred,cr);
890 } else throw new Error("Unrecognized Predicate");
895 cr.outputline("break;");
899 cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
900 cr.outputline("delete "+oldmodel.getSafeSymbol()+";");
901 cr.outputline(oldmodel.getSafeSymbol()+"="+newmodel.getSafeSymbol()+";");
902 cr.outputline("goto rebuild;"); /* Rebuild model and all */
906 while (quantifiers.hasPrevious()) {
907 Quantifier quantifier = (Quantifier) quantifiers.previous();
915 CodeWriter cr = new StandardCodeWriter(outputaux);
917 cr.outputline("if ("+repairtable.getSafeSymbol()+")");
918 cr.outputline("delete "+repairtable.getSafeSymbol()+";");
919 cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
920 cr.outputline("delete "+oldmodel.getSafeSymbol()+";");
921 cr.outputline("delete "+newmodel.getSafeSymbol()+";");
922 cr.outputline("delete "+worklist.getSafeSymbol()+";");
923 cr.outputline("resettypemap();");
924 cr.outputline("break;");
926 cr.outputline("rebuild:");
930 private MultUpdateNode getmultupdatenode(Conjunction conj, DNFPredicate dpred, int repairtype) {
931 Set nodes=getmultupdatenodeset(conj,dpred,repairtype);
932 Iterator it=nodes.iterator();
934 return (MultUpdateNode)it.next();
939 private Set getmultupdatenodeset(Conjunction conj, DNFPredicate dpred, int repairtype) {
940 HashSet hs=new HashSet();
941 MultUpdateNode mun=null;
942 GraphNode gn=(GraphNode) termination.conjtonodemap.get(conj);
943 for(Iterator edgeit=gn.edges();(mun==null)&&edgeit.hasNext();) {
944 GraphNode gn2=((GraphNode.Edge) edgeit.next()).getTarget();
945 TermNode tn2=(TermNode)gn2.getOwner();
946 if (tn2.getType()==TermNode.ABSTRACT) {
947 AbstractRepair ar=tn2.getAbstract();
948 if (((repairtype==-1)||(ar.getType()==repairtype))&&
949 ar.getPredicate()==dpred) {
950 for(Iterator edgeit2=gn2.edges();edgeit2.hasNext();) {
951 GraphNode gn3=((GraphNode.Edge) edgeit2.next()).getTarget();
952 if (!removed.contains(gn3)) {
953 TermNode tn3=(TermNode)gn3.getOwner();
954 if (tn3.getType()==TermNode.UPDATE) {
965 private AbstractRepair getabstractrepair(Conjunction conj, DNFPredicate dpred, int repairtype) {
966 HashSet hs=new HashSet();
967 MultUpdateNode mun=null;
968 GraphNode gn=(GraphNode) termination.conjtonodemap.get(conj);
969 for(Iterator edgeit=gn.edges();(mun==null)&&edgeit.hasNext();) {
970 GraphNode gn2=((GraphNode.Edge) edgeit.next()).getTarget();
971 TermNode tn2=(TermNode)gn2.getOwner();
972 if (tn2.getType()==TermNode.ABSTRACT) {
973 AbstractRepair ar=tn2.getAbstract();
974 if (((repairtype==-1)||(ar.getType()==repairtype))&&
975 ar.getPredicate()==dpred) {
984 /** Generates abstract (and concrete) repair for a comparison */
986 private void generatecomparisonrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
987 Set updates=getmultupdatenodeset(conj,dpred,AbstractRepair.MODIFYRELATION);
988 AbstractRepair ar=getabstractrepair(conj,dpred,AbstractRepair.MODIFYRELATION);
989 MultUpdateNode munmodify=null;
990 MultUpdateNode munadd=null;
991 MultUpdateNode munremove=null;
992 for(Iterator it=updates.iterator();it.hasNext();) {
993 MultUpdateNode mun=(MultUpdateNode)it.next();
994 if (mun.getType()==MultUpdateNode.ADD) {
996 } else if (mun.getType()==MultUpdateNode.REMOVE) {
998 } else if (mun.getType()==MultUpdateNode.MODIFY) {
1003 ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
1004 RelationDescriptor rd=(RelationDescriptor)ep.getDescriptor();
1005 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1006 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1007 boolean inverted=ep.inverted();
1008 boolean negated=dpred.isNegated();
1009 OpExpr expr=(OpExpr)ep.expr;
1010 Opcode opcode=expr.getOpcode();
1011 VarDescriptor leftside=VarDescriptor.makeNew("leftside");
1012 VarDescriptor rightside=VarDescriptor.makeNew("rightside");
1013 VarDescriptor newvalue=VarDescriptor.makeNew("newvalue");
1015 ((RelationExpr)expr.getLeftExpr()).getExpr().generate(cr,leftside);
1016 expr.getRightExpr().generate(cr,newvalue);
1017 cr.outputline(rd.getRange().getType().getGenerateType().getSafeSymbol()+" "+rightside.getSafeSymbol()+";");
1018 cr.outputline(rd.getSafeSymbol()+"_hash->get("+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1020 ((RelationExpr)expr.getLeftExpr()).getExpr().generate(cr,rightside);
1021 expr.getRightExpr().generate(cr,newvalue);
1022 cr.outputline(rd.getDomain().getType().getGenerateType().getSafeSymbol()+" "+leftside.getSafeSymbol()+";");
1023 cr.outputline(rd.getSafeSymbol()+"_hashinv->get("+rightside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
1026 if (opcode==Opcode.GT) {
1028 } else if (opcode==Opcode.GE) {
1030 } else if (opcode==Opcode.LT) {
1032 } else if (opcode==Opcode.LE) {
1034 } else if (opcode==Opcode.EQ) {
1036 } else if (opcode==Opcode.NE) {
1039 throw new Error("Unrecognized Opcode");
1042 if (opcode==Opcode.GT) {
1043 cr.outputline(newvalue.getSafeSymbol()+"++;");
1044 } else if (opcode==Opcode.GE) {
1046 } else if (opcode==Opcode.LT) {
1047 cr.outputline(newvalue.getSafeSymbol()+"--;");
1048 } else if (opcode==Opcode.LE) {
1050 } else if (opcode==Opcode.EQ) {
1052 } else if (opcode==Opcode.NE) { /* search for FLAGNE if this is changed*/
1053 cr.outputline(newvalue.getSafeSymbol()+"++;");
1055 throw new Error("Unrecognized Opcode");
1057 /* Do abstract repairs */
1059 cr.outputline(rd.getSafeSymbol()+"_hash->remove("+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1061 cr.outputline(rd.getSafeSymbol()+"_hash->add("+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1063 cr.outputline(rd.getSafeSymbol()+"_hash->add("+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1066 if (usageinvimage) {
1067 cr.outputline(rd.getSafeSymbol()+"_hashinv->remove("+rightside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
1069 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+newvalue.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
1071 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+rightside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1074 /* Do concrete repairs */
1075 if (munmodify!=null) {
1076 for(int i=0;i<state.vRules.size();i++) {
1077 Rule r=(Rule)state.vRules.get(i);
1078 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1079 for(int j=0;j<munmodify.numUpdates();j++) {
1080 UpdateNode un=munmodify.getUpdate(j);
1081 if (un.getRule()==r) {
1082 /* Update for rule r */
1083 String name=(String)updatenames.get(un);
1084 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+","+newvalue.getSafeSymbol()+");");
1090 /* Start with scheduling removal */
1091 if (ar.needsRemoves(state))
1092 for(int i=0;i<state.vRules.size();i++) {
1093 Rule r=(Rule)state.vRules.get(i);
1094 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1095 for(int j=0;j<munremove.numUpdates();j++) {
1096 UpdateNode un=munremove.getUpdate(i);
1097 if (un.getRule()==r) {
1098 /* Update for rule r */
1099 String name=(String)updatenames.get(un);
1100 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+");");
1105 /* Now do addition */
1106 UpdateNode un=munadd.getUpdate(0);
1107 String name=(String)updatenames.get(un);
1109 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1111 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1116 public void generatesizerepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr) {
1117 ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
1118 OpExpr expr=(OpExpr)ep.expr;
1119 Opcode opcode=expr.getOpcode();
1120 opcode=Opcode.translateOpcode(dpred.isNegated(),opcode);
1122 MultUpdateNode munremove;
1124 MultUpdateNode munadd;
1125 if (ep.getDescriptor() instanceof RelationDescriptor) {
1126 munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMRELATION);
1127 munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTORELATION);
1129 munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMSET);
1130 munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTOSET);
1132 int size=ep.rightSize();
1133 VarDescriptor sizevar=VarDescriptor.makeNew("size");
1134 ((OpExpr)expr).left.generate(cr, sizevar);
1135 VarDescriptor change=VarDescriptor.makeNew("change");
1136 cr.outputline("int "+change.getSafeSymbol()+";");
1137 boolean generateadd=false;
1138 boolean generateremove=false;
1139 if (opcode==Opcode.GT) {
1140 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
1142 generateremove=false;
1143 } else if (opcode==Opcode.GE) {
1144 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1146 generateremove=false;
1147 } else if (opcode==Opcode.LT) {
1148 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size-1)+"-"+sizevar.getSafeSymbol()+";");
1150 generateremove=true;
1151 } else if (opcode==Opcode.LE) {
1152 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1154 generateremove=true;
1155 } else if (opcode==Opcode.EQ) {
1156 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1161 generateremove=true;
1162 } else if (opcode==Opcode.NE) {
1163 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
1165 generateremove=false;
1167 throw new Error("Unrecognized Opcode");
1170 // In some cases the analysis has determined that generating removes
1172 if (generateremove&&munremove==null)
1173 generateremove=false;
1175 Descriptor d=ep.getDescriptor();
1176 if (generateremove) {
1177 cr.outputline("for(;"+change.getSafeSymbol()+"<0;"+change.getSafeSymbol()+"++)");
1179 /* Find element to remove */
1180 VarDescriptor leftvar=VarDescriptor.makeNew("leftvar");
1181 VarDescriptor rightvar=VarDescriptor.makeNew("rightvar");
1182 if (d instanceof RelationDescriptor) {
1183 if (ep.inverted()) {
1184 rightvar=((ImageSetExpr)((SizeofExpr)((OpExpr)expr).left).setexpr).getVar();
1185 cr.outputline("int "+leftvar.getSafeSymbol()+";");
1186 cr.outputline(d.getSafeSymbol()+"_hashinv->get((int)"+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1188 leftvar=((ImageSetExpr)((SizeofExpr)((OpExpr)expr).left).setexpr).getVar();
1189 cr.outputline("int "+rightvar.getSafeSymbol()+"=0;");
1190 cr.outputline(d.getSafeSymbol()+"_hash->get((int)"+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+");");
1193 cr.outputline("int "+leftvar.getSafeSymbol()+"="+d.getSafeSymbol()+"_hash->firstkey();");
1195 /* Generate abstract remove instruction */
1196 if (d instanceof RelationDescriptor) {
1197 RelationDescriptor rd=(RelationDescriptor) d;
1198 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1199 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1201 cr.outputline(rd.getSafeSymbol() + "_hash->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1203 cr.outputline(rd.getSafeSymbol() + "_hashinv->remove((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1205 cr.outputline(d.getSafeSymbol() + "_hash->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1207 /* Generate concrete remove instruction */
1208 for(int i=0;i<state.vRules.size();i++) {
1209 Rule r=(Rule)state.vRules.get(i);
1210 if (r.getInclusion().getTargetDescriptors().contains(d)) {
1211 for(int j=0;j<munremove.numUpdates();j++) {
1212 UpdateNode un=munremove.getUpdate(j);
1213 if (un.getRule()==r) {
1214 /* Update for rule rule r */
1215 String name=(String)updatenames.get(un);
1216 if (d instanceof RelationDescriptor) {
1217 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
1219 cr.outputline(repairtable.getSafeSymbol()+"->addset("+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1229 cr.outputline("for(;"+change.getSafeSymbol()+">0;"+change.getSafeSymbol()+"--)");
1231 VarDescriptor newobject=VarDescriptor.makeNew("newobject");
1232 if (d instanceof RelationDescriptor) {
1233 VarDescriptor otherside=((ImageSetExpr)((SizeofExpr)((OpExpr)ep.expr).left).setexpr).vd;
1234 RelationDescriptor rd=(RelationDescriptor)d;
1235 if (termination.sources.relsetSource(rd,!ep.inverted())) {
1237 SetDescriptor sd=termination.sources.relgetSourceSet(rd,!ep.inverted());
1238 VarDescriptor iterator=VarDescriptor.makeNew("iterator");
1239 cr.outputline(sd.getType().getGenerateType().getSafeSymbol() +" "+newobject.getSafeSymbol()+";");
1240 cr.outputline("SimpleIterator "+iterator.getSafeSymbol()+";");
1241 cr.outputline("for("+sd.getSafeSymbol()+"_hash->iterator("+ iterator.getSafeSymbol() +");"+iterator.getSafeSymbol()+".hasNext();)");
1243 if (ep.inverted()) {
1244 cr.outputline("if (!"+rd.getSafeSymbol()+"_hashinv->contains("+iterator.getSafeSymbol()+".key(),"+otherside.getSafeSymbol()+"))");
1246 cr.outputline("if (!"+rd.getSafeSymbol()+"_hash->contains("+otherside.getSafeSymbol()+","+iterator.getSafeSymbol()+".key()))");
1248 cr.outputline(newobject.getSafeSymbol()+"="+iterator.getSafeSymbol()+".key();");
1249 cr.outputline(iterator.getSafeSymbol()+".next();");
1251 } else if (termination.sources.relallocSource(rd,!ep.inverted())) {
1252 /* Allocation Source*/
1253 termination.sources.relgenerateSourceAlloc(cr,newobject,rd,!ep.inverted());
1254 } else throw new Error("No source for adding to Relation");
1255 if (ep.inverted()) {
1256 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1257 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1259 cr.outputline(rd.getSafeSymbol()+"_hash->add("+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1261 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1263 UpdateNode un=munadd.getUpdate(0);
1264 String name=(String)updatenames.get(un);
1265 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1267 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1268 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1270 cr.outputline(rd.getSafeSymbol()+"_hash->add("+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1272 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1273 UpdateNode un=munadd.getUpdate(0);
1274 String name=(String)updatenames.get(un);
1275 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1278 SetDescriptor sd=(SetDescriptor)d;
1279 if (termination.sources.setSource(sd)) {
1282 SetDescriptor sourcesd=termination.sources.getSourceSet(sd);
1283 VarDescriptor iterator=VarDescriptor.makeNew("iterator");
1284 cr.outputline(sourcesd.getType().getGenerateType().getSafeSymbol() +" "+newobject.getSafeSymbol()+";");
1285 cr.outputline("SimpleIterator "+iterator.getSafeSymbol()+";");
1286 cr.outputline("for("+sourcesd.getSafeSymbol()+"_hash->iterator("+iterator.getSafeSymbol()+");"+iterator.getSafeSymbol()+".hasNext();)");
1288 cr.outputline("if (!"+sd.getSafeSymbol()+"_hash->contains("+iterator.getSafeSymbol()+".key()))");
1289 cr.outputline(newobject.getSafeSymbol()+"="+iterator.getSafeSymbol()+".key();");
1290 cr.outputline(iterator.getSafeSymbol()+".next();");
1292 } else if (termination.sources.allocSource(sd)) {
1293 /* Allocation Source*/
1294 termination.sources.generateSourceAlloc(cr,newobject,sd);
1295 } else throw new Error("No source for adding to Set");
1296 cr.outputline(sd.getSafeSymbol()+"_hash->add("+newobject.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1297 UpdateNode un=munadd.getUpdate(0);
1298 String name=(String)updatenames.get(un);
1299 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1305 public void generateinclusionrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
1306 InclusionPredicate ip=(InclusionPredicate) dpred.getPredicate();
1307 boolean negated=dpred.isNegated();
1308 MultUpdateNode mun=getmultupdatenode(conj,dpred,-1);
1309 VarDescriptor leftvar=VarDescriptor.makeNew("left");
1310 ip.expr.generate(cr, leftvar);
1313 if (ip.setexpr instanceof ImageSetExpr) {
1314 ImageSetExpr ise=(ImageSetExpr) ip.setexpr;
1315 VarDescriptor rightvar=ise.getVar();
1316 boolean inverse=ise.inverted();
1317 RelationDescriptor rd=ise.getRelation();
1318 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1319 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1322 cr.outputline(rd.getSafeSymbol() + "_hash->remove((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1324 cr.outputline(rd.getSafeSymbol() + "_hashinv->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1327 cr.outputline(rd.getSafeSymbol() + "_hash->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1329 cr.outputline(rd.getSafeSymbol() + "_hashinv->remove((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1331 for(int i=0;i<state.vRules.size();i++) {
1332 Rule r=(Rule)state.vRules.get(i);
1333 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1334 for(int j=0;j<mun.numUpdates();j++) {
1335 UpdateNode un=mun.getUpdate(j);
1336 if (un.getRule()==r) {
1337 /* Update for rule rule r */
1338 String name=(String)updatenames.get(un);
1340 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1342 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
1349 SetDescriptor sd=ip.setexpr.sd;
1350 cr.outputline(sd.getSafeSymbol() + "_hash->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1352 for(int i=0;i<state.vRules.size();i++) {
1353 Rule r=(Rule)state.vRules.get(i);
1354 if (r.getInclusion().getTargetDescriptors().contains(sd)) {
1355 for(int j=0;j<mun.numUpdates();j++) {
1356 UpdateNode un=mun.getUpdate(j);
1357 if (un.getRule()==r) {
1358 /* Update for rule rule r */
1359 String name=(String)updatenames.get(un);
1360 cr.outputline(repairtable.getSafeSymbol()+"->addset("+sd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1367 /* Generate update */
1368 if (ip.setexpr instanceof ImageSetExpr) {
1369 ImageSetExpr ise=(ImageSetExpr) ip.setexpr;
1370 VarDescriptor rightvar=ise.getVar();
1371 boolean inverse=ise.inverted();
1372 RelationDescriptor rd=ise.getRelation();
1373 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1374 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1377 cr.outputline(rd.getSafeSymbol() + "_hash->add((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1379 cr.outputline(rd.getSafeSymbol() + "_hashinv->add((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1382 cr.outputline(rd.getSafeSymbol() + "_hash->add((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1384 cr.outputline(rd.getSafeSymbol() + "_hashinv->add((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1386 UpdateNode un=mun.getUpdate(0);
1387 String name=(String)updatenames.get(un);
1389 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1391 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+");");
1394 SetDescriptor sd=ip.setexpr.sd;
1395 cr.outputline(sd.getSafeSymbol() + "_hash->add((int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1397 UpdateNode un=mun.getUpdate(0);
1398 /* Update for rule rule r */
1399 String name=(String)updatenames.get(un);
1400 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1405 public static Vector getrulelist(Descriptor d) {
1406 Vector dispatchrules = new Vector();
1407 Vector rules = State.currentState.vRules;
1409 for (int i = 0; i < rules.size(); i++) {
1410 Rule rule = (Rule) rules.elementAt(i);
1411 Set requiredsymbols = rule.getRequiredDescriptors();
1413 // #TBD#: in general this is wrong because these descriptors may contain descriptors
1414 // bound in "in?" expressions which need to be dealt with in a topologically sorted
1417 if (rule.getRequiredDescriptors().contains(d)) {
1418 dispatchrules.addElement(rule);
1421 return dispatchrules;
1424 private boolean need_compensation(Rule r) {
1425 if (!Compiler.REPAIR)
1427 GraphNode gn=(GraphNode)termination.scopefalsify.get(r);
1428 for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1429 GraphNode.Edge edge=(GraphNode.Edge)edgeit.next();
1430 GraphNode gn2=edge.getTarget();
1431 if (!removed.contains(gn2)) {
1432 TermNode tn2=(TermNode)gn2.getOwner();
1433 if (tn2.getType()==TermNode.CONSEQUENCE)
1440 private UpdateNode find_compensation(Rule r) {
1441 GraphNode gn=(GraphNode)termination.scopefalsify.get(r);
1442 for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1443 GraphNode.Edge edge=(GraphNode.Edge)edgeit.next();
1444 GraphNode gn2=edge.getTarget();
1445 if (!removed.contains(gn2)) {
1446 TermNode tn2=(TermNode)gn2.getOwner();
1447 if (tn2.getType()==TermNode.UPDATE) {
1448 MultUpdateNode mun=tn2.getUpdate();
1449 for(int i=0;i<mun.numUpdates();i++) {
1450 UpdateNode un=mun.getUpdate(i);
1451 if (un.getRule()==r)
1457 throw new Error("No Compensation Update could be found");
1460 public void generate_dispatch(CodeWriter cr, RelationDescriptor rd, String leftvar, String rightvar) {
1461 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1462 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1464 if (!(usageinvimage||usageimage)) /* not used at all*/
1467 cr.outputline("// RELATION DISPATCH ");
1468 if (Compiler.REPAIR) {
1469 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1471 cr.outputline("!"+oldmodel.getSafeSymbol() +"->"+rd.getJustSafeSymbol()+"_hash->contains("+leftvar+","+rightvar+"))");
1473 cr.outputline("!"+oldmodel.getSafeSymbol() +"->"+rd.getJustSafeSymbol()+"_hashinv->contains("+rightvar+","+leftvar+"))");
1476 /* Adding new item */
1477 /* Perform safety checks */
1478 cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
1479 cr.outputline(repairtable.getSafeSymbol()+"->containsrelation("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+"))");
1481 /* Have update to call into */
1482 VarDescriptor mdfyptr=VarDescriptor.makeNew("modifyptr");
1483 VarDescriptor ismdfyptr=VarDescriptor.makeNew("ismodifyptr");
1484 cr.outputline("int "+ismdfyptr.getSafeSymbol()+"="+repairtable.getSafeSymbol()+"->ismodify("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1490 for(int i=0;i<currentrule.numQuantifiers();i++) {
1491 if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1492 parttype=parttype+", int, int";
1494 parttype=parttype+", int";
1496 VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1497 VarDescriptor tmpptr=VarDescriptor.makeNew("tempupdateptr");
1499 String methodcall="("+funptr.getSafeSymbol()+") (this,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
1500 for(int i=0;i<currentrule.numQuantifiers();i++) {
1501 Quantifier q=currentrule.getQuantifier(i);
1502 if (q instanceof SetQuantifier) {
1503 SetQuantifier sq=(SetQuantifier) q;
1504 methodcall+=","+sq.getVar().getSafeSymbol();
1505 } else if (q instanceof RelationQuantifier) {
1506 RelationQuantifier rq=(RelationQuantifier) q;
1507 methodcall+=","+rq.x.getSafeSymbol();
1508 methodcall+=","+rq.y.getSafeSymbol();
1509 } else if (q instanceof ForQuantifier) {
1510 ForQuantifier fq=(ForQuantifier) q;
1511 methodcall+=","+fq.getVar().getSafeSymbol();
1517 cr.outputline("void *"+tmpptr.getSafeSymbol()+"=");
1518 cr.outputline("(void *) "+repairtable.getSafeSymbol()+"->getrelation("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1519 cr.outputline("if ("+ismdfyptr.getSafeSymbol()+")");
1522 cr.outputline("int "+mdfyptr.getSafeSymbol()+"="+repairtable.getSafeSymbol()+"->getrelation2("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1523 cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+",int,int,int)="+"(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+",int,int,int)) "+tmpptr.getSafeSymbol()+";");
1524 cr.outputline(methodcall+","+leftvar+", "+rightvar+", "+mdfyptr.getSafeSymbol() +");");
1527 cr.outputline("else ");
1530 cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")="+"(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")) "+tmpptr.getSafeSymbol()+";");
1531 cr.outputline(methodcall+");");
1534 cr.outputline("delete "+newmodel.getSafeSymbol()+";");
1535 cr.outputline("goto rebuild;");
1539 /* Build standard compensation actions */
1540 if (need_compensation(currentrule)) {
1541 UpdateNode un=find_compensation(currentrule);
1542 String name=(String)updatenames.get(un);
1543 usedupdates.add(un); /* Mark as used */
1544 String methodcall=name+"(this,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
1545 for(int i=0;i<currentrule.numQuantifiers();i++) {
1546 Quantifier q=currentrule.getQuantifier(i);
1547 if (q instanceof SetQuantifier) {
1548 SetQuantifier sq=(SetQuantifier) q;
1549 methodcall+=","+sq.getVar().getSafeSymbol();
1550 } else if (q instanceof RelationQuantifier) {
1551 RelationQuantifier rq=(RelationQuantifier) q;
1552 methodcall+=","+rq.x.getSafeSymbol();
1553 methodcall+=","+rq.y.getSafeSymbol();
1554 } else if (q instanceof ForQuantifier) {
1555 ForQuantifier fq=(ForQuantifier) q;
1556 methodcall+=","+fq.getVar().getSafeSymbol();
1560 cr.outputline(methodcall);
1561 cr.outputline("delete "+newmodel.getSafeSymbol()+";");
1562 cr.outputline("goto rebuild;");
1568 String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1569 cr.outputline("int " + addeditem + "=0;");
1571 String ifstring="if (!maybe&&";
1572 boolean dogenerate=false;
1573 if (rd.getDomain().getType() instanceof StructureTypeDescriptor) {
1578 if (rd.getRange().getType() instanceof StructureTypeDescriptor) {
1580 ifstring+="&&"+rightvar;
1588 if (rd.testUsage(RelationDescriptor.IMAGE)) {
1589 cr.outputline(ifstring);
1590 cr.outputline(addeditem + " = " + rd.getSafeSymbol() + "_hash->add((int)" + leftvar + ", (int)" + rightvar+ ");");
1593 if (rd.testUsage(RelationDescriptor.INVIMAGE)) {
1594 cr.outputline(ifstring);
1595 cr.outputline(addeditem + " = " + rd.getSafeSymbol() + "_hashinv->add((int)" + rightvar + ", (int)" + leftvar + ");");
1599 Vector dispatchrules = getrulelist(rd);
1601 Set toremove=new HashSet();
1602 for(int i=0;i<dispatchrules.size();i++) {
1603 Rule r=(Rule)dispatchrules.get(i);
1604 if (!ruleset.contains(r))
1607 dispatchrules.removeAll(toremove);
1608 if (dispatchrules.size() == 0) {
1609 cr.outputline("// nothing to dispatch");
1613 cr.outputline("if (" + addeditem + ")");
1616 for(int i = 0; i < dispatchrules.size(); i++) {
1617 Rule rule = (Rule) dispatchrules.elementAt(i);
1618 if (rule.getGuardExpr().getRequiredDescriptors().contains(rd)) {
1619 /* Guard depends on this relation, so we recomput everything */
1620 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+",-1,0,0);");
1622 for (int j=0;j<rule.numQuantifiers();j++) {
1623 Quantifier q=rule.getQuantifier(j);
1624 if (q.getRequiredDescriptors().contains(rd)) {
1626 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+","+j+","+leftvar+","+rightvar+");");
1636 public void generate_dispatch(CodeWriter cr, SetDescriptor sd, String setvar) {
1637 cr.outputline("// SET DISPATCH ");
1638 if (Compiler.REPAIR) {
1639 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1640 cr.outputline("!"+oldmodel.getSafeSymbol() +"->"+sd.getJustSafeSymbol()+"_hash->contains("+setvar+"))");
1642 /* Adding new item */
1643 /* See if there is an outstanding update in the repairtable */
1644 cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
1645 cr.outputline(repairtable.getSafeSymbol()+"->containsset("+sd.getNum()+","+currentrule.getNum()+","+setvar+"))");
1647 /* Have update to call into */
1648 VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1650 for(int i=0;i<currentrule.numQuantifiers();i++) {
1651 if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1652 parttype=parttype+", int, int";
1654 parttype=parttype+", int";
1656 cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")=");
1657 cr.outputline("(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")) "+repairtable.getSafeSymbol()+"->getset("+sd.getNum()+","+currentrule.getNum()+","+setvar+");");
1658 String methodcall="("+funptr.getSafeSymbol()+") (this,"+oldmodel.getSafeSymbol()+","+
1659 repairtable.getSafeSymbol();
1660 for(int i=0;i<currentrule.numQuantifiers();i++) {
1661 Quantifier q=currentrule.getQuantifier(i);
1662 if (q instanceof SetQuantifier) {
1663 SetQuantifier sq=(SetQuantifier) q;
1664 methodcall+=","+sq.getVar().getSafeSymbol();
1665 } else if (q instanceof RelationQuantifier) {
1666 RelationQuantifier rq=(RelationQuantifier) q;
1667 methodcall+=","+rq.x.getSafeSymbol();
1668 methodcall+=","+rq.y.getSafeSymbol();
1669 } else if (q instanceof ForQuantifier) {
1670 ForQuantifier fq=(ForQuantifier) q;
1671 methodcall+=","+fq.getVar().getSafeSymbol();
1675 cr.outputline(methodcall);
1676 cr.outputline("delete "+newmodel.getSafeSymbol()+";");
1677 cr.outputline("goto rebuild;");
1680 /* Build standard compensation actions */
1681 Vector ruleset=new Vector();
1682 ruleset.add(currentrule);
1683 if (state.implicitruleinv.containsKey(currentrule))
1684 ruleset.addAll((Set)state.implicitruleinv.get(currentrule));
1685 for(int i=0;i<ruleset.size();i++) {
1686 Rule itrule=(Rule)ruleset.get(i);
1688 if (need_compensation(itrule)) {
1689 UpdateNode un=find_compensation(itrule);
1690 String name=(String)updatenames.get(un);
1691 usedupdates.add(un); /* Mark as used */
1693 String methodcall=name+"(this,"+oldmodel.getSafeSymbol()+","+
1694 repairtable.getSafeSymbol();
1695 for(int j=0;j<currentrule.numQuantifiers();j++) {
1696 Quantifier q=currentrule.getQuantifier(j);
1697 if (q instanceof SetQuantifier) {
1698 SetQuantifier sq=(SetQuantifier) q;
1699 methodcall+=","+sq.getVar().getSafeSymbol();
1700 } else if (q instanceof RelationQuantifier) {
1701 RelationQuantifier rq=(RelationQuantifier) q;
1702 methodcall+=","+rq.x.getSafeSymbol();
1703 methodcall+=","+rq.y.getSafeSymbol();
1704 } else if (q instanceof ForQuantifier) {
1705 ForQuantifier fq=(ForQuantifier) q;
1706 methodcall+=","+fq.getVar().getSafeSymbol();
1710 if (currentrule!=itrule) {
1711 SetDescriptor sdrule=((SetInclusion)itrule.getInclusion()).getSet();
1712 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1713 cr.outputline("!"+oldmodel.getSafeSymbol() +"->"+sdrule.getJustSafeSymbol()+"_hash->contains("+setvar+"))");
1716 cr.outputline(methodcall);
1717 cr.outputline("delete "+newmodel.getSafeSymbol()+";");
1718 cr.outputline("goto rebuild;");
1721 if (currentrule==itrule)
1727 String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1728 cr.outputline("int " + addeditem + " = 0;");
1729 if (sd.getType() instanceof StructureTypeDescriptor) {
1730 cr.outputline("if (!maybe&&"+setvar+")");
1732 cr.outputline("if (!maybe)");
1733 cr.outputline(addeditem + " = " + sd.getSafeSymbol() + "_hash->add((int)" + setvar + ", (int)" + setvar + ");");
1735 Vector dispatchrules = getrulelist(sd);
1737 Set toremove=new HashSet();
1738 for(int i=0;i<dispatchrules.size();i++) {
1739 Rule r=(Rule)dispatchrules.get(i);
1740 if (!ruleset.contains(r))
1743 dispatchrules.removeAll(toremove);
1745 if (dispatchrules.size() == 0) {
1746 cr.outputline("// nothing to dispatch");
1750 /* Add item to worklist if new */
1751 cr.outputline("if ("+addeditem+")");
1753 for(int i = 0; i < dispatchrules.size(); i++) {
1754 Rule rule = (Rule) dispatchrules.elementAt(i);
1755 if (SetDescriptor.expand(rule.getGuardExpr().getRequiredDescriptors()).contains(sd)) {
1756 /* Guard depends on this relation, so we recompute everything */
1757 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+",-1,0,0);");
1759 for (int j=0;j<rule.numQuantifiers();j++) {
1760 Quantifier q=rule.getQuantifier(j);
1761 if (SetDescriptor.expand(q.getRequiredDescriptors()).contains(sd)) {
1763 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+","+j+","+setvar+",0);");