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\"");
390 if (Compiler.ALLOCATECPLUSPLUS) {
391 for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
392 TypeDescriptor td=(TypeDescriptor)it.next();
393 if (td instanceof StructureTypeDescriptor) {
394 if (((StructureTypeDescriptor)td).size()>0) {
395 FieldDescriptor fd=((StructureTypeDescriptor)td).get(0);
396 if (fd.getSymbol().startsWith("_vptr_")) {
397 String vtable="_ZTV";
398 vtable+=td.getSymbol().length();
399 vtable+=td.getSymbol();
400 craux.outputline("extern void * "+vtable+";");
406 craux.outputline(name+"::"+name+"() {");
407 craux.outputline("// creating hashtables ");
410 Iterator sets = state.stSets.descriptors();
412 /* first pass create all the hash tables */
413 while (sets.hasNext()) {
414 SetDescriptor set = (SetDescriptor) sets.next();
415 crhead.outputline("SimpleHash* " + set.getSafeSymbol() + "_hash;");
416 craux.outputline(set.getSafeSymbol() + "_hash = new SimpleHash();");
419 /* second pass build relationships between hashtables */
420 sets = state.stSets.descriptors();
422 while (sets.hasNext()) {
423 SetDescriptor set = (SetDescriptor) sets.next();
424 Iterator subsets = set.subsets();
426 while (subsets.hasNext()) {
427 SetDescriptor subset = (SetDescriptor) subsets.next();
428 craux.outputline(subset.getSafeSymbol() + "_hash->addParent(" + set.getSafeSymbol() + "_hash);");
432 /* build relations */
433 Iterator relations = state.stRelations.descriptors();
435 /* first pass create all the hash tables */
436 while (relations.hasNext()) {
437 RelationDescriptor relation = (RelationDescriptor) relations.next();
439 if (relation.testUsage(RelationDescriptor.IMAGE)) {
440 crhead.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hash;");
441 craux.outputline(relation.getSafeSymbol() + "_hash = new SimpleHash();");
444 if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
445 crhead.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hashinv;");
446 craux.outputline(relation.getSafeSymbol() + "_hashinv = new SimpleHash();");
450 craux.outputline("}");
451 crhead.outputline("};");
452 craux.outputline(name+"::~"+name+"() {");
453 craux.outputline("// deleting hashtables");
455 /* build destructor */
456 sets = state.stSets.descriptors();
458 /* first pass create all the hash tables */
459 while (sets.hasNext()) {
460 SetDescriptor set = (SetDescriptor) sets.next();
461 craux.outputline("delete "+set.getSafeSymbol() + "_hash;");
464 /* destroy relations */
465 relations = state.stRelations.descriptors();
467 /* first pass create all the hash tables */
468 while (relations.hasNext()) {
469 RelationDescriptor relation = (RelationDescriptor) relations.next();
471 if (relation.testUsage(RelationDescriptor.IMAGE)) {
472 craux.outputline("delete "+relation.getSafeSymbol() + "_hash;");
475 if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
476 craux.outputline("delete " + relation.getSafeSymbol() + "_hashinv;");
479 craux.outputline("}");
482 private void generate_start() {
483 CodeWriter crhead = new StandardCodeWriter(outputhead);
484 CodeWriter craux = new StandardCodeWriter(outputaux);
485 oldmodel=VarDescriptor.makeNew("oldmodel");
486 newmodel=VarDescriptor.makeNew("newmodel");
487 worklist=VarDescriptor.makeNew("worklist");
488 goodflag=VarDescriptor.makeNew("goodflag");
489 repairtable=VarDescriptor.makeNew("repairtable");
491 if (Compiler.GENERATEINSTRUMENT) {
492 craux.outputline("int updatecount;");
493 craux.outputline("int rebuildcount;");
494 craux.outputline("int abstractcount;");
497 crhead.outputline("void doanalysis();");
498 craux.outputline("void "+name +"_state::doanalysis()");
500 if (Compiler.GENERATEINSTRUMENT) {
501 craux.outputline("updatecount=0;");
502 craux.outputline("rebuildcount=0;");
503 craux.outputline("abstractcount=0;");
505 craux.outputline("int highmark;");
506 craux.outputline("initializestack(&highmark);");
507 craux.outputline("typeobject *typeobject1=gettypeobject();");
508 craux.outputline("typeobject1->computesizes(this);");
509 craux.outputline("recomputesizes();");
510 craux.outputline(name+ " * "+oldmodel.getSafeSymbol()+"=0;");
511 craux.outputline("WorkList * "+worklist.getSafeSymbol()+" = new WorkList();");
512 craux.outputline("RepairHash * "+repairtable.getSafeSymbol()+"=0;");
513 craux.outputline("while (1)");
515 craux.outputline(name+ " * "+newmodel.getSafeSymbol()+"=new "+name+"();");
516 craux.outputline(worklist.getSafeSymbol()+"->reset();");
517 if (Compiler.GENERATEINSTRUMENT)
518 craux.outputline("rebuildcount++;");
521 private void generate_teardown() {
522 CodeWriter cr = new StandardCodeWriter(outputaux);
524 if (Compiler.GENERATEINSTRUMENT) {
525 cr.outputline("printf(\"updatecount=%d\\n\",updatecount);");
526 cr.outputline("printf(\"rebuildcount=%d\\n\",rebuildcount);");
527 cr.outputline("printf(\"abstractcount=%d\\n\",abstractcount);");
532 private void generate_print() {
534 final SymbolTable st = new SymbolTable();
536 CodeWriter cr = new StandardCodeWriter(outputaux) {
537 public SymbolTable getSymbolTable() { return st; }
540 cr.outputline("// printing sets!");
541 cr.outputline("printf(\"\\n\\nPRINTING SETS AND RELATIONS\\n\");");
543 Iterator setiterator = state.stSets.descriptors();
544 while (setiterator.hasNext()) {
545 SetDescriptor sd = (SetDescriptor) setiterator.next();
546 if (sd.getSymbol().equals("int") || sd.getSymbol().equals("token")) {
550 String setname = sd.getSafeSymbol();
553 cr.outputline("// printing set " + setname);
554 cr.outputline("printf(\"\\nPrinting set " + sd.getSymbol() + " - %d elements \\n\", " + setname + "_hash->count());");
555 cr.outputline("SimpleIterator __setiterator;");
556 cr.outputline("" + setname + "_hash->iterator(__setiterator);");
557 cr.outputline("while (__setiterator.hasNext())");
559 cr.outputline("int __setval = (int) __setiterator.next();");
561 TypeDescriptor td = sd.getType();
562 if (td instanceof StructureTypeDescriptor) {
563 StructureTypeDescriptor std = (StructureTypeDescriptor) td;
564 VarDescriptor vd = new VarDescriptor ("__setval", "__setval", td, false);
565 std.generate_printout(cr, vd);
566 } else { // Missing type descriptor or reserved type, just print int
567 cr.outputline("printf(\"<%d> \", __setval);");
575 cr.outputline("printf(\"\\n\\n------------------- END PRINTING\\n\");");
579 private void generate_rules() {
580 /* first we must sort the rules */
581 RelationDescriptor.prefix = newmodel.getSafeSymbol()+"->";
582 SetDescriptor.prefix = newmodel.getSafeSymbol()+"->";
583 System.out.println("SCC="+(mrd.numSCC()-1));
584 for(int sccindex=0;sccindex<mrd.numSCC();sccindex++) {
585 ruleset=mrd.getSCC(sccindex);
586 boolean needworklist=mrd.hasCycle(sccindex);
589 Iterator iterator_rs = ruleset.iterator();
590 while (iterator_rs.hasNext()) {
591 Rule rule = (Rule) iterator_rs.next();
592 if (rule.getnogenerate())
595 final SymbolTable st = rule.getSymbolTable();
596 CodeWriter cr = new StandardCodeWriter(outputaux) {
597 public SymbolTable getSymbolTable() { return st; }
599 InvariantValue ivalue=new InvariantValue();
600 cr.setInvariantValue(ivalue);
602 cr.outputline("// build " +escape(rule.toString()));
604 cr.outputline("int maybe=0;");
606 Expr ruleexpr=rule.getGuardExpr();
607 HashSet invariantvars=new HashSet();
608 Set invariants=ruleexpr.findInvariants(invariantvars);
610 if ((ruleexpr instanceof BooleanLiteralExpr)&&
611 ((BooleanLiteralExpr)ruleexpr).getValue()) {
612 if (rule.getInclusion() instanceof SetInclusion) {
613 invariants.addAll(((SetInclusion)rule.getInclusion()).getExpr().findInvariants(invariantvars));
614 } else if (rule.getInclusion() instanceof RelationInclusion) {
615 invariants.addAll(((RelationInclusion)rule.getInclusion()).getLeftExpr().findInvariants(invariantvars));
616 invariants.addAll(((RelationInclusion)rule.getInclusion()).getRightExpr().findInvariants(invariantvars));
619 ListIterator quantifiers = rule.quantifiers();
620 while (quantifiers.hasNext()) {
621 Quantifier quantifier = (Quantifier) quantifiers.next();
622 if (quantifier instanceof ForQuantifier) {
623 ForQuantifier fq=(ForQuantifier)quantifier;
624 invariants.addAll(fq.lower.findInvariants(invariantvars));
625 invariants.addAll(fq.upper.findInvariants(invariantvars));
629 for(Iterator invit=invariants.iterator();invit.hasNext();) {
630 Expr invexpr=(Expr)invit.next();
631 VarDescriptor tmpvd=VarDescriptor.makeNew("tmpvar");
632 VarDescriptor maybevd=VarDescriptor.makeNew("maybevar");
633 invexpr.generate(cr,tmpvd);
634 cr.outputline("int "+maybevd.getSafeSymbol()+"=maybe;");
635 cr.outputline("maybe=0;");
636 ivalue.assignPair(invexpr,tmpvd,maybevd);
639 quantifiers = rule.quantifiers();
640 while (quantifiers.hasNext()) {
641 Quantifier quantifier = (Quantifier) quantifiers.next();
642 quantifier.generate_open(cr);
647 rule.getGuardExpr().prettyPrint(cr);
650 /* now we have to generate the guard test */
651 VarDescriptor guardval = VarDescriptor.makeNew();
652 rule.getGuardExpr().generate(cr, guardval);
653 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
656 /* now we have to generate the inclusion code */
658 rule.getInclusion().generate(cr);
660 while (quantifiers.hasPrevious()) {
661 Quantifier quantifier = (Quantifier) quantifiers.previous();
670 CodeWriter cr2 = new StandardCodeWriter(outputaux);
672 for(Iterator initialworklist=ruleset.iterator();initialworklist.hasNext();) {
673 /** Construct initial worklist set */
674 Rule rule=(Rule)initialworklist.next();
675 cr2.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+",-1,0,0);");
678 cr2.outputline("while ("+worklist.getSafeSymbol()+"->hasMoreElements())");
680 VarDescriptor idvar=VarDescriptor.makeNew("id");
681 cr2.outputline("int "+idvar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->getid();");
683 String elseladder = "if";
685 Iterator iterator_rules = ruleset.iterator();
686 while (iterator_rules.hasNext()) {
688 Rule rule = (Rule) iterator_rules.next();
689 int dispatchid = rule.getNum();
692 final SymbolTable st = rule.getSymbolTable();
693 CodeWriter cr = new StandardCodeWriter(outputaux) {
694 public SymbolTable getSymbolTable() { return st; }
698 cr.outputline(elseladder + " ("+idvar.getSafeSymbol()+" == " + dispatchid + ")");
700 cr.outputline("int maybe=0;");
701 VarDescriptor typevar=VarDescriptor.makeNew("type");
702 VarDescriptor leftvar=VarDescriptor.makeNew("left");
703 VarDescriptor rightvar=VarDescriptor.makeNew("right");
704 cr.outputline("int "+typevar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->gettype();");
705 cr.outputline("int "+leftvar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->getlvalue();");
706 cr.outputline("int "+rightvar.getSafeSymbol()+"="+worklist.getSafeSymbol()+"->getrvalue();");
707 cr.outputline("// build " +escape(rule.toString()));
710 for (int j=0;j<rule.numQuantifiers();j++) {
711 Quantifier quantifier = rule.getQuantifier(j);
712 quantifier.generate_open(cr, typevar.getSafeSymbol(),j,leftvar.getSafeSymbol(),rightvar.getSafeSymbol());
718 rule.getGuardExpr().prettyPrint(cr);
721 /* now we have to generate the guard test */
723 VarDescriptor guardval = VarDescriptor.makeNew();
724 rule.getGuardExpr().generate(cr, guardval);
726 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
729 /* now we have to generate the inclusion code */
731 rule.getInclusion().generate(cr);
734 for (int j=0;j<rule.numQuantifiers();j++) {
738 // close startblocks generated by DotExpr memory checks
739 //DotExpr.generate_memory_endblocks(cr);
741 cr.endblock(); // end else-if WORKLIST ladder
743 elseladder = "else if";
746 cr2.outputline("else");
748 cr2.outputline("printf(\"VERY BAD !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\\n\\n\");");
749 cr2.outputline("exit(1);");
751 // end block created for worklist
752 cr2.outputline(worklist.getSafeSymbol()+"->pop();");
758 public static String escape(String s) {
760 for(int i=0;i<s.length();i++) {
770 private void generate_checks() {
771 /* do constraint checks */
774 i=termination.constraintdependence.computeOrdering().iterator();
776 i=state.vConstraints.iterator();
777 for (; i.hasNext();) {
778 Constraint constraint;
780 constraint= (Constraint) ((GraphNode)i.next()).getOwner();
782 constraint=(Constraint)i.next();
785 final SymbolTable st = constraint.getSymbolTable();
786 CodeWriter cr = new StandardCodeWriter(outputaux);
787 cr.pushSymbolTable(constraint.getSymbolTable());
789 cr.outputline("// checking " + escape(constraint.toString()));
792 ListIterator quantifiers = constraint.quantifiers();
794 while (quantifiers.hasNext()) {
795 Quantifier quantifier = (Quantifier) quantifiers.next();
796 quantifier.generate_open(cr);
799 cr.outputline("int maybe = 0;");
801 /* now we have to generate the guard test */
803 VarDescriptor constraintboolean = VarDescriptor.makeNew("constraintboolean");
804 constraint.getLogicStatement().generate(cr, constraintboolean);
806 cr.outputline("if (maybe)");
808 cr.outputline("printf(\"maybe fail " + escape(constraint.toString()) + ". \\n\");");
809 //cr.outputline("exit(1);");
812 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
814 if (!Compiler.REPAIR||Compiler.GENERATEDEBUGHOOKS)
815 cr.outputline("printf(\"fail " + escape(constraint.toString()) + ". \\n\");");
817 if (Compiler.REPAIR) {
819 /* Build new repair table */
820 cr.outputline("if ("+repairtable.getSafeSymbol()+")");
821 cr.outputline("delete "+repairtable.getSafeSymbol()+";");
822 cr.outputline(repairtable.getSafeSymbol()+"=new RepairHash();");
824 if (Compiler.GENERATEDEBUGHOOKS)
825 cr.outputline("debughook();");
826 /* Compute cost of each repair */
827 VarDescriptor mincost=VarDescriptor.makeNew("mincost");
828 VarDescriptor mincostindex=VarDescriptor.makeNew("mincostindex");
829 Vector dnfconst=new Vector();
830 dnfconst.addAll((Set)termination.conjunctionmap.get(constraint));
832 if (dnfconst.size()<=1) {
833 cr.outputline("int "+mincostindex.getSafeSymbol()+"=0;");
835 if (dnfconst.size()>1) {
836 cr.outputline("int "+mincostindex.getSafeSymbol()+";");
838 for(int j=0;j<dnfconst.size();j++) {
839 GraphNode gn=(GraphNode)dnfconst.get(j);
840 Conjunction conj=((TermNode)gn.getOwner()).getConjunction();
841 if (removed.contains(gn))
844 VarDescriptor costvar;
848 costvar=VarDescriptor.makeNew("cost");
849 for(int k=0;k<conj.size();k++) {
850 DNFPredicate dpred=conj.get(k);
851 Predicate p=dpred.getPredicate();
852 boolean negate=dpred.isNegated();
853 VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
854 p.generate(cr,predvalue);
856 cr.outputline("int "+costvar.getSafeSymbol()+"=0;");
859 cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
861 cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
862 cr.outputline(costvar.getSafeSymbol()+"+="+cost.getCost(dpred)+";");
866 cr.outputline("if ("+costvar.getSafeSymbol()+"<"+mincost.getSafeSymbol()+")");
868 cr.outputline(mincost.getSafeSymbol()+"="+costvar.getSafeSymbol()+";");
869 cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
872 cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
876 cr.outputline("switch("+mincostindex.getSafeSymbol()+") {");
877 for(int j=0;j<dnfconst.size();j++) {
878 GraphNode gn=(GraphNode)dnfconst.get(j);
879 Conjunction conj=((TermNode)gn.getOwner()).getConjunction();
880 if (removed.contains(gn))
882 cr.outputline("case "+j+":");
884 for(int k=0;k<conj.size();k++) {
885 DNFPredicate dpred=conj.get(k);
886 Predicate p=dpred.getPredicate();
887 boolean negate=dpred.isNegated();
888 VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
889 p.generate(cr,predvalue);
891 cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
893 cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
895 if (Compiler.GENERATEINSTRUMENT)
896 cr.outputline("abstractcount++;");
897 if (p instanceof InclusionPredicate)
898 generateinclusionrepair(conj,dpred, cr);
899 else if (p instanceof ExprPredicate) {
900 ExprPredicate ep=(ExprPredicate)p;
901 if (ep.getType()==ExprPredicate.SIZE)
902 generatesizerepair(conj,dpred,cr);
903 else if (ep.getType()==ExprPredicate.COMPARISON)
904 generatecomparisonrepair(conj,dpred,cr);
905 } else throw new Error("Unrecognized Predicate");
910 cr.outputline("break;");
914 cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
915 cr.outputline("delete "+oldmodel.getSafeSymbol()+";");
916 cr.outputline(oldmodel.getSafeSymbol()+"="+newmodel.getSafeSymbol()+";");
917 cr.outputline("goto rebuild;"); /* Rebuild model and all */
921 while (quantifiers.hasPrevious()) {
922 Quantifier quantifier = (Quantifier) quantifiers.previous();
930 CodeWriter cr = new StandardCodeWriter(outputaux);
932 cr.outputline("if ("+repairtable.getSafeSymbol()+")");
933 cr.outputline("delete "+repairtable.getSafeSymbol()+";");
934 cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
935 cr.outputline("delete "+oldmodel.getSafeSymbol()+";");
936 cr.outputline("delete "+newmodel.getSafeSymbol()+";");
937 cr.outputline("delete "+worklist.getSafeSymbol()+";");
938 cr.outputline("resettypemap();");
939 cr.outputline("break;");
941 cr.outputline("rebuild:");
945 private MultUpdateNode getmultupdatenode(Conjunction conj, DNFPredicate dpred, int repairtype) {
946 Set nodes=getmultupdatenodeset(conj,dpred,repairtype);
947 Iterator it=nodes.iterator();
949 return (MultUpdateNode)it.next();
954 private Set getmultupdatenodeset(Conjunction conj, DNFPredicate dpred, int repairtype) {
955 HashSet hs=new HashSet();
956 GraphNode gn=(GraphNode) termination.conjtonodemap.get(conj);
957 for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
958 GraphNode gn2=((GraphNode.Edge) edgeit.next()).getTarget();
959 TermNode tn2=(TermNode)gn2.getOwner();
960 if (tn2.getType()==TermNode.ABSTRACT) {
961 AbstractRepair ar=tn2.getAbstract();
962 if (((repairtype==-1)||(ar.getType()==repairtype))&&
963 ar.getPredicate()==dpred) {
964 for(Iterator edgeit2=gn2.edges();edgeit2.hasNext();) {
965 GraphNode gn3=((GraphNode.Edge) edgeit2.next()).getTarget();
966 if (!removed.contains(gn3)) {
967 TermNode tn3=(TermNode)gn3.getOwner();
968 if (tn3.getType()==TermNode.UPDATE) {
969 hs.add(tn3.getUpdate());
979 private AbstractRepair getabstractrepair(Conjunction conj, DNFPredicate dpred, int repairtype) {
980 HashSet hs=new HashSet();
981 MultUpdateNode mun=null;
982 GraphNode gn=(GraphNode) termination.conjtonodemap.get(conj);
983 for(Iterator edgeit=gn.edges();(mun==null)&&edgeit.hasNext();) {
984 GraphNode gn2=((GraphNode.Edge) edgeit.next()).getTarget();
985 TermNode tn2=(TermNode)gn2.getOwner();
986 if (tn2.getType()==TermNode.ABSTRACT) {
987 AbstractRepair ar=tn2.getAbstract();
988 if (((repairtype==-1)||(ar.getType()==repairtype))&&
989 ar.getPredicate()==dpred) {
998 /** Generates abstract (and concrete) repair for a comparison */
1000 private void generatecomparisonrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
1001 Set updates=getmultupdatenodeset(conj,dpred,AbstractRepair.MODIFYRELATION);
1002 AbstractRepair ar=getabstractrepair(conj,dpred,AbstractRepair.MODIFYRELATION);
1003 MultUpdateNode munmodify=null;
1004 MultUpdateNode munadd=null;
1005 MultUpdateNode munremove=null;
1006 for(Iterator it=updates.iterator();it.hasNext();) {
1007 MultUpdateNode mun=(MultUpdateNode)it.next();
1008 if (mun.getType()==MultUpdateNode.ADD) {
1010 } else if (mun.getType()==MultUpdateNode.REMOVE) {
1012 } else if (mun.getType()==MultUpdateNode.MODIFY) {
1017 ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
1018 RelationDescriptor rd=(RelationDescriptor)ep.getDescriptor();
1019 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1020 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1021 boolean inverted=ep.inverted();
1022 boolean negated=dpred.isNegated();
1023 OpExpr expr=(OpExpr)ep.expr;
1024 Opcode opcode=expr.getOpcode();
1025 VarDescriptor leftside=VarDescriptor.makeNew("leftside");
1026 VarDescriptor rightside=VarDescriptor.makeNew("rightside");
1027 VarDescriptor newvalue=VarDescriptor.makeNew("newvalue");
1028 boolean needremoveloop=ar.mayNeedFunctionEnforcement(state)&&ar.needsRemoves(state);
1030 if (needremoveloop&&((munadd==null)||(munremove==null))) {
1031 System.out.println("Warning: need to have individual remove operations for"+dpred.name());
1032 needremoveloop=false;
1035 cr.outputline("while (1) {");
1038 ((RelationExpr)expr.getLeftExpr()).getExpr().generate(cr,leftside);
1039 expr.getRightExpr().generate(cr,newvalue);
1040 cr.outputline(rd.getRange().getType().getGenerateType().getSafeSymbol()+" "+rightside.getSafeSymbol()+";");
1041 cr.outputline(rd.getSafeSymbol()+"_hash->get("+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1043 ((RelationExpr)expr.getLeftExpr()).getExpr().generate(cr,rightside);
1044 expr.getRightExpr().generate(cr,newvalue);
1045 cr.outputline(rd.getDomain().getType().getGenerateType().getSafeSymbol()+" "+leftside.getSafeSymbol()+";");
1046 cr.outputline(rd.getSafeSymbol()+"_hashinv->get("+rightside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
1049 opcode=Opcode.translateOpcode(negated,opcode);
1051 if (opcode==Opcode.GT) {
1052 cr.outputline(newvalue.getSafeSymbol()+"++;");
1053 } else if (opcode==Opcode.GE) {
1055 } else if (opcode==Opcode.LT) {
1056 cr.outputline(newvalue.getSafeSymbol()+"--;");
1057 } else if (opcode==Opcode.LE) {
1059 } else if (opcode==Opcode.EQ) {
1061 } else if (opcode==Opcode.NE) { /* search for FLAGNE if this is changed*/
1062 cr.outputline(newvalue.getSafeSymbol()+"++;");
1064 throw new Error("Unrecognized Opcode");
1066 /* Do abstract repairs */
1068 cr.outputline(rd.getSafeSymbol()+"_hash->remove("+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1070 if (usageinvimage) {
1071 cr.outputline(rd.getSafeSymbol()+"_hashinv->remove("+rightside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
1074 if (needremoveloop) {
1076 cr.outputline("if ("+rd.getSafeSymbol()+"_hash->contains("+leftside.getSafeSymbol()+")) {");
1078 cr.outputline("if ("+rd.getSafeSymbol()+"_hashinv->contains("+rightside.getSafeSymbol()+")) {");
1080 for(int i=0;i<state.vRules.size();i++) {
1081 Rule r=(Rule)state.vRules.get(i);
1082 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1083 for(int j=0;j<munremove.numUpdates();j++) {
1084 UpdateNode un=munremove.getUpdate(i);
1085 if (un.getRule()==r) {
1086 /* Update for rule r */
1087 String name=(String)updatenames.get(un);
1088 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+");");
1093 cr.outputline("continue;");
1099 cr.outputline(rd.getSafeSymbol()+"_hash->add("+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1101 cr.outputline(rd.getSafeSymbol()+"_hash->add("+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1104 if (usageinvimage) {
1106 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+newvalue.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
1108 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+rightside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1111 /* Do concrete repairs */
1112 if (munmodify!=null&&(!ar.mayNeedFunctionEnforcement(state))||(munadd==null)||(ar.needsRemoves(state)&&(munremove==null))) {
1113 for(int i=0;i<state.vRules.size();i++) {
1114 Rule r=(Rule)state.vRules.get(i);
1115 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1116 for(int j=0;j<munmodify.numUpdates();j++) {
1117 UpdateNode un=munmodify.getUpdate(j);
1118 if (un.getRule()==r) {
1119 /* Update for rule r */
1120 String name=(String)updatenames.get(un);
1121 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+","+newvalue.getSafeSymbol()+");");
1127 /* Start with scheduling removal */
1128 if (ar.needsRemoves(state))
1129 for(int i=0;i<state.vRules.size();i++) {
1130 Rule r=(Rule)state.vRules.get(i);
1131 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1132 for(int j=0;j<munremove.numUpdates();j++) {
1133 UpdateNode un=munremove.getUpdate(i);
1134 if (un.getRule()==r) {
1135 /* Update for rule r */
1136 String name=(String)updatenames.get(un);
1137 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+");");
1142 /* Now do addition */
1143 UpdateNode un=munadd.getUpdate(0);
1144 String name=(String)updatenames.get(un);
1146 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1148 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1151 if (needremoveloop) {
1152 cr.outputline("break;");
1157 public void generatesizerepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr) {
1158 ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
1159 OpExpr expr=(OpExpr)ep.expr;
1160 Opcode opcode=expr.getOpcode();
1161 opcode=Opcode.translateOpcode(dpred.isNegated(),opcode);
1163 MultUpdateNode munremove;
1165 MultUpdateNode munadd;
1166 if (ep.getDescriptor() instanceof RelationDescriptor) {
1167 munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMRELATION);
1168 munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTORELATION);
1170 munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMSET);
1171 munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTOSET);
1173 int size=ep.rightSize();
1174 VarDescriptor sizevar=VarDescriptor.makeNew("size");
1175 ((OpExpr)expr).left.generate(cr, sizevar);
1176 VarDescriptor change=VarDescriptor.makeNew("change");
1177 cr.outputline("int "+change.getSafeSymbol()+";");
1178 boolean generateadd=false;
1179 boolean generateremove=false;
1180 if (opcode==Opcode.GT) {
1181 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
1183 generateremove=false;
1184 } else if (opcode==Opcode.GE) {
1185 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1187 generateremove=false;
1188 } else if (opcode==Opcode.LT) {
1189 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size-1)+"-"+sizevar.getSafeSymbol()+";");
1191 generateremove=true;
1192 } else if (opcode==Opcode.LE) {
1193 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1195 generateremove=true;
1196 } else if (opcode==Opcode.EQ) {
1197 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1202 generateremove=true;
1203 } else if (opcode==Opcode.NE) {
1204 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
1206 generateremove=false;
1208 throw new Error("Unrecognized Opcode");
1211 // In some cases the analysis has determined that generating removes
1213 if (generateremove&&munremove==null)
1214 generateremove=false;
1216 Descriptor d=ep.getDescriptor();
1217 if (generateremove) {
1218 cr.outputline("for(;"+change.getSafeSymbol()+"<0;"+change.getSafeSymbol()+"++)");
1220 /* Find element to remove */
1221 VarDescriptor leftvar=VarDescriptor.makeNew("leftvar");
1222 VarDescriptor rightvar=VarDescriptor.makeNew("rightvar");
1223 if (d instanceof RelationDescriptor) {
1224 if (ep.inverted()) {
1225 rightvar=((ImageSetExpr)((SizeofExpr)((OpExpr)expr).left).setexpr).getVar();
1226 cr.outputline("int "+leftvar.getSafeSymbol()+";");
1227 cr.outputline(d.getSafeSymbol()+"_hashinv->get((int)"+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1229 leftvar=((ImageSetExpr)((SizeofExpr)((OpExpr)expr).left).setexpr).getVar();
1230 cr.outputline("int "+rightvar.getSafeSymbol()+"=0;");
1231 cr.outputline(d.getSafeSymbol()+"_hash->get((int)"+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+");");
1234 cr.outputline("int "+leftvar.getSafeSymbol()+"="+d.getSafeSymbol()+"_hash->firstkey();");
1236 /* Generate abstract remove instruction */
1237 if (d instanceof RelationDescriptor) {
1238 RelationDescriptor rd=(RelationDescriptor) d;
1239 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1240 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1242 cr.outputline(rd.getSafeSymbol() + "_hash->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1244 cr.outputline(rd.getSafeSymbol() + "_hashinv->remove((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1246 cr.outputline(d.getSafeSymbol() + "_hash->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1248 /* Generate concrete remove instruction */
1249 for(int i=0;i<state.vRules.size();i++) {
1250 Rule r=(Rule)state.vRules.get(i);
1251 if (r.getInclusion().getTargetDescriptors().contains(d)) {
1252 for(int j=0;j<munremove.numUpdates();j++) {
1253 UpdateNode un=munremove.getUpdate(j);
1254 if (un.getRule()==r) {
1255 /* Update for rule rule r */
1256 String name=(String)updatenames.get(un);
1257 if (d instanceof RelationDescriptor) {
1258 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
1260 cr.outputline(repairtable.getSafeSymbol()+"->addset("+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1270 cr.outputline("for(;"+change.getSafeSymbol()+">0;"+change.getSafeSymbol()+"--)");
1272 VarDescriptor newobject=VarDescriptor.makeNew("newobject");
1273 if (d instanceof RelationDescriptor) {
1274 VarDescriptor otherside=((ImageSetExpr)((SizeofExpr)((OpExpr)ep.expr).left).setexpr).vd;
1275 RelationDescriptor rd=(RelationDescriptor)d;
1276 if (termination.sources.relsetSource(rd,!ep.inverted())) {
1278 SetDescriptor sd=termination.sources.relgetSourceSet(rd,!ep.inverted());
1279 VarDescriptor iterator=VarDescriptor.makeNew("iterator");
1280 cr.outputline(sd.getType().getGenerateType().getSafeSymbol() +" "+newobject.getSafeSymbol()+";");
1281 cr.outputline("SimpleIterator "+iterator.getSafeSymbol()+";");
1282 cr.outputline("for("+sd.getSafeSymbol()+"_hash->iterator("+ iterator.getSafeSymbol() +");"+iterator.getSafeSymbol()+".hasNext();)");
1284 if (ep.inverted()) {
1285 cr.outputline("if (!"+rd.getSafeSymbol()+"_hashinv->contains("+iterator.getSafeSymbol()+".key(),"+otherside.getSafeSymbol()+"))");
1287 cr.outputline("if (!"+rd.getSafeSymbol()+"_hash->contains("+otherside.getSafeSymbol()+","+iterator.getSafeSymbol()+".key()))");
1289 cr.outputline(newobject.getSafeSymbol()+"="+iterator.getSafeSymbol()+".key();");
1290 cr.outputline(iterator.getSafeSymbol()+".next();");
1292 } else if (termination.sources.relallocSource(rd,!ep.inverted())) {
1293 /* Allocation Source*/
1294 termination.sources.relgenerateSourceAlloc(cr,newobject,rd,!ep.inverted());
1295 } else throw new Error("No source for adding to Relation");
1296 if (ep.inverted()) {
1297 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1298 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1300 cr.outputline(rd.getSafeSymbol()+"_hash->add("+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1302 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1304 UpdateNode un=munadd.getUpdate(0);
1305 String name=(String)updatenames.get(un);
1306 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1308 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1309 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1311 cr.outputline(rd.getSafeSymbol()+"_hash->add("+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1313 cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1314 UpdateNode un=munadd.getUpdate(0);
1315 String name=(String)updatenames.get(un);
1316 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1319 SetDescriptor sd=(SetDescriptor)d;
1320 if (termination.sources.setSource(sd)) {
1323 SetDescriptor sourcesd=termination.sources.getSourceSet(sd);
1324 VarDescriptor iterator=VarDescriptor.makeNew("iterator");
1325 cr.outputline(sourcesd.getType().getGenerateType().getSafeSymbol() +" "+newobject.getSafeSymbol()+";");
1326 cr.outputline("SimpleIterator "+iterator.getSafeSymbol()+";");
1327 cr.outputline("for("+sourcesd.getSafeSymbol()+"_hash->iterator("+iterator.getSafeSymbol()+");"+iterator.getSafeSymbol()+".hasNext();)");
1329 cr.outputline("if (!"+sd.getSafeSymbol()+"_hash->contains("+iterator.getSafeSymbol()+".key()))");
1330 cr.outputline(newobject.getSafeSymbol()+"="+iterator.getSafeSymbol()+".key();");
1331 cr.outputline(iterator.getSafeSymbol()+".next();");
1333 } else if (termination.sources.allocSource(sd)) {
1334 /* Allocation Source*/
1335 termination.sources.generateSourceAlloc(cr,newobject,sd);
1336 } else throw new Error("No source for adding to Set");
1337 cr.outputline(sd.getSafeSymbol()+"_hash->add("+newobject.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1338 UpdateNode un=munadd.getUpdate(0);
1339 String name=(String)updatenames.get(un);
1340 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1346 public void generateinclusionrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
1347 InclusionPredicate ip=(InclusionPredicate) dpred.getPredicate();
1348 boolean negated=dpred.isNegated();
1349 MultUpdateNode mun=getmultupdatenode(conj,dpred,-1);
1350 VarDescriptor leftvar=VarDescriptor.makeNew("left");
1351 ip.expr.generate(cr, leftvar);
1354 if (ip.setexpr instanceof ImageSetExpr) {
1355 ImageSetExpr ise=(ImageSetExpr) ip.setexpr;
1356 VarDescriptor rightvar=ise.getVar();
1357 boolean inverse=ise.inverted();
1358 RelationDescriptor rd=ise.getRelation();
1359 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1360 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1363 cr.outputline(rd.getSafeSymbol() + "_hash->remove((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1365 cr.outputline(rd.getSafeSymbol() + "_hashinv->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1368 cr.outputline(rd.getSafeSymbol() + "_hash->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1370 cr.outputline(rd.getSafeSymbol() + "_hashinv->remove((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1372 for(int i=0;i<state.vRules.size();i++) {
1373 Rule r=(Rule)state.vRules.get(i);
1374 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1375 for(int j=0;j<mun.numUpdates();j++) {
1376 UpdateNode un=mun.getUpdate(j);
1377 if (un.getRule()==r) {
1378 /* Update for rule rule r */
1379 String name=(String)updatenames.get(un);
1381 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1383 cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
1390 SetDescriptor sd=ip.setexpr.sd;
1391 cr.outputline(sd.getSafeSymbol() + "_hash->remove((int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1393 for(int i=0;i<state.vRules.size();i++) {
1394 Rule r=(Rule)state.vRules.get(i);
1395 if (r.getInclusion().getTargetDescriptors().contains(sd)) {
1396 for(int j=0;j<mun.numUpdates();j++) {
1397 UpdateNode un=mun.getUpdate(j);
1398 if (un.getRule()==r) {
1399 /* Update for rule rule r */
1400 String name=(String)updatenames.get(un);
1401 cr.outputline(repairtable.getSafeSymbol()+"->addset("+sd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1408 /* Generate update */
1409 if (ip.setexpr instanceof ImageSetExpr) {
1410 ImageSetExpr ise=(ImageSetExpr) ip.setexpr;
1411 VarDescriptor rightvar=ise.getVar();
1412 boolean inverse=ise.inverted();
1413 RelationDescriptor rd=ise.getRelation();
1414 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1415 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1418 cr.outputline(rd.getSafeSymbol() + "_hash->add((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1420 cr.outputline(rd.getSafeSymbol() + "_hashinv->add((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1423 cr.outputline(rd.getSafeSymbol() + "_hash->add((int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1425 cr.outputline(rd.getSafeSymbol() + "_hashinv->add((int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1427 UpdateNode un=mun.getUpdate(0);
1428 String name=(String)updatenames.get(un);
1430 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1432 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+");");
1435 SetDescriptor sd=ip.setexpr.sd;
1436 cr.outputline(sd.getSafeSymbol() + "_hash->add((int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1438 UpdateNode un=mun.getUpdate(0);
1439 /* Update for rule rule r */
1440 String name=(String)updatenames.get(un);
1441 cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1446 public static Vector getrulelist(Descriptor d) {
1447 Vector dispatchrules = new Vector();
1448 Vector rules = State.currentState.vRules;
1450 for (int i = 0; i < rules.size(); i++) {
1451 Rule rule = (Rule) rules.elementAt(i);
1452 Set requiredsymbols = rule.getRequiredDescriptors();
1454 // #TBD#: in general this is wrong because these descriptors may contain descriptors
1455 // bound in "in?" expressions which need to be dealt with in a topologically sorted
1458 if (rule.getRequiredDescriptors().contains(d)) {
1459 dispatchrules.addElement(rule);
1462 return dispatchrules;
1465 private boolean need_compensation(Rule r) {
1466 if (!Compiler.REPAIR)
1468 GraphNode gn=(GraphNode)termination.scopefalsify.get(r);
1469 for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1470 GraphNode.Edge edge=(GraphNode.Edge)edgeit.next();
1471 GraphNode gn2=edge.getTarget();
1472 if (!removed.contains(gn2)) {
1473 TermNode tn2=(TermNode)gn2.getOwner();
1474 if (tn2.getType()==TermNode.CONSEQUENCE)
1481 private UpdateNode find_compensation(Rule r) {
1482 GraphNode gn=(GraphNode)termination.scopefalsify.get(r);
1483 for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1484 GraphNode.Edge edge=(GraphNode.Edge)edgeit.next();
1485 GraphNode gn2=edge.getTarget();
1486 if (!removed.contains(gn2)) {
1487 TermNode tn2=(TermNode)gn2.getOwner();
1488 if (tn2.getType()==TermNode.UPDATE) {
1489 MultUpdateNode mun=tn2.getUpdate();
1490 for(int i=0;i<mun.numUpdates();i++) {
1491 UpdateNode un=mun.getUpdate(i);
1492 if (un.getRule()==r)
1498 throw new Error("No Compensation Update could be found");
1501 public void generate_dispatch(CodeWriter cr, RelationDescriptor rd, String leftvar, String rightvar) {
1502 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1503 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1505 if (!(usageinvimage||usageimage)) /* not used at all*/
1508 cr.outputline("// RELATION DISPATCH ");
1509 if (Compiler.REPAIR) {
1510 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1512 cr.outputline("!"+oldmodel.getSafeSymbol() +"->"+rd.getJustSafeSymbol()+"_hash->contains("+leftvar+","+rightvar+"))");
1514 cr.outputline("!"+oldmodel.getSafeSymbol() +"->"+rd.getJustSafeSymbol()+"_hashinv->contains("+rightvar+","+leftvar+"))");
1517 /* Adding new item */
1518 /* Perform safety checks */
1519 cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
1520 cr.outputline(repairtable.getSafeSymbol()+"->containsrelation("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+"))");
1522 /* Have update to call into */
1523 VarDescriptor mdfyptr=VarDescriptor.makeNew("modifyptr");
1524 VarDescriptor ismdfyptr=VarDescriptor.makeNew("ismodifyptr");
1525 cr.outputline("int "+ismdfyptr.getSafeSymbol()+"="+repairtable.getSafeSymbol()+"->ismodify("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1531 for(int i=0;i<currentrule.numQuantifiers();i++) {
1532 if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1533 parttype=parttype+", int, int";
1535 parttype=parttype+", int";
1537 VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1538 VarDescriptor tmpptr=VarDescriptor.makeNew("tempupdateptr");
1540 String methodcall="("+funptr.getSafeSymbol()+") (this,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
1541 for(int i=0;i<currentrule.numQuantifiers();i++) {
1542 Quantifier q=currentrule.getQuantifier(i);
1543 if (q instanceof SetQuantifier) {
1544 SetQuantifier sq=(SetQuantifier) q;
1545 methodcall+=","+sq.getVar().getSafeSymbol();
1546 } else if (q instanceof RelationQuantifier) {
1547 RelationQuantifier rq=(RelationQuantifier) q;
1548 methodcall+=","+rq.x.getSafeSymbol();
1549 methodcall+=","+rq.y.getSafeSymbol();
1550 } else if (q instanceof ForQuantifier) {
1551 ForQuantifier fq=(ForQuantifier) q;
1552 methodcall+=","+fq.getVar().getSafeSymbol();
1558 cr.outputline("void *"+tmpptr.getSafeSymbol()+"=");
1559 cr.outputline("(void *) "+repairtable.getSafeSymbol()+"->getrelation("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1560 cr.outputline("if ("+ismdfyptr.getSafeSymbol()+")");
1563 cr.outputline("int "+mdfyptr.getSafeSymbol()+"="+repairtable.getSafeSymbol()+"->getrelation2("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1564 cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+",int,int,int)="+"(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+",int,int,int)) "+tmpptr.getSafeSymbol()+";");
1565 cr.outputline(methodcall+","+leftvar+", "+rightvar+", "+mdfyptr.getSafeSymbol() +");");
1568 cr.outputline("else ");
1571 cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")="+"(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")) "+tmpptr.getSafeSymbol()+";");
1572 cr.outputline(methodcall+");");
1575 cr.outputline("delete "+newmodel.getSafeSymbol()+";");
1576 cr.outputline("goto rebuild;");
1580 /* Build standard compensation actions */
1581 if (need_compensation(currentrule)) {
1582 UpdateNode un=find_compensation(currentrule);
1583 String name=(String)updatenames.get(un);
1584 usedupdates.add(un); /* Mark as used */
1585 String methodcall=name+"(this,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
1586 for(int i=0;i<currentrule.numQuantifiers();i++) {
1587 Quantifier q=currentrule.getQuantifier(i);
1588 if (q instanceof SetQuantifier) {
1589 SetQuantifier sq=(SetQuantifier) q;
1590 methodcall+=","+sq.getVar().getSafeSymbol();
1591 } else if (q instanceof RelationQuantifier) {
1592 RelationQuantifier rq=(RelationQuantifier) q;
1593 methodcall+=","+rq.x.getSafeSymbol();
1594 methodcall+=","+rq.y.getSafeSymbol();
1595 } else if (q instanceof ForQuantifier) {
1596 ForQuantifier fq=(ForQuantifier) q;
1597 methodcall+=","+fq.getVar().getSafeSymbol();
1601 cr.outputline(methodcall);
1602 cr.outputline("delete "+newmodel.getSafeSymbol()+";");
1603 cr.outputline("goto rebuild;");
1609 String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1610 cr.outputline("int " + addeditem + "=0;");
1612 String ifstring="if (!maybe&&";
1613 boolean dogenerate=false;
1614 if (rd.getDomain().getType() instanceof StructureTypeDescriptor) {
1619 if (rd.getRange().getType() instanceof StructureTypeDescriptor) {
1621 ifstring+="&&"+rightvar;
1629 if (rd.testUsage(RelationDescriptor.IMAGE)) {
1630 cr.outputline(ifstring);
1631 cr.outputline(addeditem + " = " + rd.getSafeSymbol() + "_hash->add((int)" + leftvar + ", (int)" + rightvar+ ");");
1634 if (rd.testUsage(RelationDescriptor.INVIMAGE)) {
1635 cr.outputline(ifstring);
1636 cr.outputline(addeditem + " = " + rd.getSafeSymbol() + "_hashinv->add((int)" + rightvar + ", (int)" + leftvar + ");");
1640 Vector dispatchrules = getrulelist(rd);
1642 Set toremove=new HashSet();
1643 for(int i=0;i<dispatchrules.size();i++) {
1644 Rule r=(Rule)dispatchrules.get(i);
1645 if (!ruleset.contains(r))
1648 dispatchrules.removeAll(toremove);
1649 if (dispatchrules.size() == 0) {
1650 cr.outputline("// nothing to dispatch");
1654 cr.outputline("if (" + addeditem + ")");
1657 for(int i = 0; i < dispatchrules.size(); i++) {
1658 Rule rule = (Rule) dispatchrules.elementAt(i);
1659 if (rule.getGuardExpr().getRequiredDescriptors().contains(rd)) {
1660 /* Guard depends on this relation, so we recomput everything */
1661 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+",-1,0,0);");
1663 for (int j=0;j<rule.numQuantifiers();j++) {
1664 Quantifier q=rule.getQuantifier(j);
1665 if (q.getRequiredDescriptors().contains(rd)) {
1667 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+","+j+","+leftvar+","+rightvar+");");
1677 public void generate_dispatch(CodeWriter cr, SetDescriptor sd, String setvar) {
1678 cr.outputline("// SET DISPATCH ");
1679 if (Compiler.REPAIR) {
1680 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1681 cr.outputline("!"+oldmodel.getSafeSymbol() +"->"+sd.getJustSafeSymbol()+"_hash->contains("+setvar+"))");
1683 /* Adding new item */
1684 /* See if there is an outstanding update in the repairtable */
1685 cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
1686 cr.outputline(repairtable.getSafeSymbol()+"->containsset("+sd.getNum()+","+currentrule.getNum()+","+setvar+"))");
1688 /* Have update to call into */
1689 VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1691 for(int i=0;i<currentrule.numQuantifiers();i++) {
1692 if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1693 parttype=parttype+", int, int";
1695 parttype=parttype+", int";
1697 cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")=");
1698 cr.outputline("(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")) "+repairtable.getSafeSymbol()+"->getset("+sd.getNum()+","+currentrule.getNum()+","+setvar+");");
1699 String methodcall="("+funptr.getSafeSymbol()+") (this,"+oldmodel.getSafeSymbol()+","+
1700 repairtable.getSafeSymbol();
1701 for(int i=0;i<currentrule.numQuantifiers();i++) {
1702 Quantifier q=currentrule.getQuantifier(i);
1703 if (q instanceof SetQuantifier) {
1704 SetQuantifier sq=(SetQuantifier) q;
1705 methodcall+=","+sq.getVar().getSafeSymbol();
1706 } else if (q instanceof RelationQuantifier) {
1707 RelationQuantifier rq=(RelationQuantifier) q;
1708 methodcall+=","+rq.x.getSafeSymbol();
1709 methodcall+=","+rq.y.getSafeSymbol();
1710 } else if (q instanceof ForQuantifier) {
1711 ForQuantifier fq=(ForQuantifier) q;
1712 methodcall+=","+fq.getVar().getSafeSymbol();
1716 cr.outputline(methodcall);
1717 cr.outputline("delete "+newmodel.getSafeSymbol()+";");
1718 cr.outputline("goto rebuild;");
1721 /* Build standard compensation actions */
1722 Vector ruleset=new Vector();
1723 ruleset.add(currentrule);
1724 if (state.implicitruleinv.containsKey(currentrule))
1725 ruleset.addAll((Set)state.implicitruleinv.get(currentrule));
1726 for(int i=0;i<ruleset.size();i++) {
1727 Rule itrule=(Rule)ruleset.get(i);
1729 if (need_compensation(itrule)) {
1730 UpdateNode un=find_compensation(itrule);
1731 String name=(String)updatenames.get(un);
1732 usedupdates.add(un); /* Mark as used */
1734 String methodcall=name+"(this,"+oldmodel.getSafeSymbol()+","+
1735 repairtable.getSafeSymbol();
1736 for(int j=0;j<currentrule.numQuantifiers();j++) {
1737 Quantifier q=currentrule.getQuantifier(j);
1738 if (q instanceof SetQuantifier) {
1739 SetQuantifier sq=(SetQuantifier) q;
1740 methodcall+=","+sq.getVar().getSafeSymbol();
1741 } else if (q instanceof RelationQuantifier) {
1742 RelationQuantifier rq=(RelationQuantifier) q;
1743 methodcall+=","+rq.x.getSafeSymbol();
1744 methodcall+=","+rq.y.getSafeSymbol();
1745 } else if (q instanceof ForQuantifier) {
1746 ForQuantifier fq=(ForQuantifier) q;
1747 methodcall+=","+fq.getVar().getSafeSymbol();
1751 if (currentrule!=itrule) {
1752 SetDescriptor sdrule=((SetInclusion)itrule.getInclusion()).getSet();
1753 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1754 cr.outputline("!"+oldmodel.getSafeSymbol() +"->"+sdrule.getJustSafeSymbol()+"_hash->contains("+setvar+"))");
1757 cr.outputline(methodcall);
1758 cr.outputline("delete "+newmodel.getSafeSymbol()+";");
1759 cr.outputline("goto rebuild;");
1762 if (currentrule==itrule)
1768 String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1769 cr.outputline("int " + addeditem + " = 0;");
1770 if (sd.getType() instanceof StructureTypeDescriptor) {
1771 cr.outputline("if (!maybe&&"+setvar+")");
1773 cr.outputline("if (!maybe)");
1774 cr.outputline(addeditem + " = " + sd.getSafeSymbol() + "_hash->add((int)" + setvar + ", (int)" + setvar + ");");
1776 Vector dispatchrules = getrulelist(sd);
1778 Set toremove=new HashSet();
1779 for(int i=0;i<dispatchrules.size();i++) {
1780 Rule r=(Rule)dispatchrules.get(i);
1781 if (!ruleset.contains(r))
1784 dispatchrules.removeAll(toremove);
1786 if (dispatchrules.size() == 0) {
1787 cr.outputline("// nothing to dispatch");
1791 /* Add item to worklist if new */
1792 cr.outputline("if ("+addeditem+")");
1794 for(int i = 0; i < dispatchrules.size(); i++) {
1795 Rule rule = (Rule) dispatchrules.elementAt(i);
1796 if (SetDescriptor.expand(rule.getGuardExpr().getRequiredDescriptors()).contains(sd)) {
1797 /* Guard depends on this relation, so we recompute everything */
1798 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+",-1,0,0);");
1800 for (int j=0;j<rule.numQuantifiers();j++) {
1801 Quantifier q=rule.getQuantifier(j);
1802 if (SetDescriptor.expand(q.getRequiredDescriptors()).contains(sd)) {
1804 cr.outputline(worklist.getSafeSymbol()+"->add("+rule.getNum()+","+j+","+setvar+",0);");