8 public class RepairGenerator {
10 PrintWrapper outputrepair = null;
11 PrintWrapper outputaux = null;
12 PrintWrapper 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;
60 private void name_updates() {
62 for(Iterator it=termination.updatenodes.iterator();it.hasNext();) {
63 GraphNode gn=(GraphNode) it.next();
64 TermNode tn=(TermNode) gn.getOwner();
65 MultUpdateNode mun=tn.getUpdate();
66 if (togenerate.contains(gn))
67 for (int i=0;i<mun.numUpdates();i++) {
68 UpdateNode un=mun.getUpdate(i);
69 String name="update"+String.valueOf(count++);
70 updatenames.put(un,name);
75 public void generate(OutputStream outputrepair, OutputStream outputaux,OutputStream outputhead, String st) {
76 this.outputrepair = new PrintWrapper(new java.io.PrintWriter(outputrepair, true));
77 this.outputaux = new PrintWrapper(new java.io.PrintWriter(outputaux, true));
78 this.outputhead = new PrintWrapper(new java.io.PrintWriter(outputhead, true));
82 generatetypechecks(true);
83 generate_tokentable();
84 RelationDescriptor.prefix = "thisvar->";
85 SetDescriptor.prefix = "thisvar->";
87 generate_hashtables();
88 generate_stateobject();
92 CodeWriter craux = new StandardCodeWriter(this.outputaux);
93 for (Iterator it=this.state.stGlobals.descriptors();it.hasNext();) {
94 VarDescriptor vd=(VarDescriptor)it.next();
95 craux.outputline("#define "+vd.getSafeSymbol()+" thisvar->"+vd.getSafeSymbol());
102 if (!Compiler.REPAIR||Compiler.GENERATEDEBUGPRINT) {
107 CodeWriter crhead = new StandardCodeWriter(this.outputhead);
108 craux = new StandardCodeWriter(this.outputaux);
112 if (Compiler.GENERATEDEBUGHOOKS) {
113 crhead.outputline("void debughook();");
114 craux.outputline("void debughook() {}");
116 generatetypechecks(false);
117 generate_computesizes();
118 generatetypechecks(true);
119 generate_recomputesizes();
120 generatetypechecks(false);
122 StructureGenerator sg=new StructureGenerator(state,this);
124 crhead.outputline("#endif");
127 String ststate="state";
128 String stmodel="model";
129 String strepairtable="repairtable";
130 String stleft="left";
131 String stright="right";
132 String stnew="newvalue";
134 private void generate_updates() {
136 CodeWriter crhead = new StandardCodeWriter(outputhead);
137 CodeWriter craux = new StandardCodeWriter(outputaux);
138 RelationDescriptor.prefix = "model->";
139 SetDescriptor.prefix = "model->";
141 /* Rewrite globals */
143 for (Iterator it=this.state.stGlobals.descriptors();it.hasNext();) {
144 VarDescriptor vd=(VarDescriptor)it.next();
145 craux.outputline("#undef "+vd.getSafeSymbol());
146 craux.outputline("#define "+vd.getSafeSymbol()+" "+ststate+"->"+vd.getSafeSymbol());
149 for(Iterator it=termination.updatenodes.iterator();it.hasNext();) {
150 GraphNode gn=(GraphNode) it.next();
151 TermNode tn=(TermNode) gn.getOwner();
152 MultUpdateNode mun=tn.getUpdate();
153 boolean isrelation=(mun.getDescriptor() instanceof RelationDescriptor);
154 if (togenerate.contains(gn))
155 for (int i=0;i<mun.numUpdates();i++) {
156 UpdateNode un=mun.getUpdate(i);
157 String methodname=(String)updatenames.get(un);
160 case MultUpdateNode.ADD:
162 crhead.outputline("void "+methodname+"(struct "+name+"_state * " +ststate+",struct "+name+" * "+stmodel+", struct RepairHash * "+strepairtable+", int "+stleft+", int "+stright+");");
163 craux.outputline("void "+methodname+"(struct "+name+"_state * "+ ststate+", struct "+name+" * "+stmodel+", struct RepairHash * "+strepairtable+", int "+stleft+", int "+stright+")");
165 crhead.outputline("void "+methodname+"(struct "+name+"_state * "+ ststate+", struct "+name+" * "+stmodel+", struct RepairHash * "+strepairtable+", int "+stleft+");");
166 craux.outputline("void "+methodname+"(struct "+name+"_state * "+ststate+", struct "+name+" * "+stmodel+", struct RepairHash * "+strepairtable+", int "+stleft+")");
170 craux.addDeclaration("int","maybe");
171 craux.outputline("maybe=0;");
172 if (Compiler.GENERATEINSTRUMENT)
173 craux.outputline("updatecount++;");
175 final SymbolTable st = un.getRule().getSymbolTable();
176 CodeWriter cr = new StandardCodeWriter(outputaux) {
177 public SymbolTable getSymbolTable() { return st; }
179 un.generate(cr, false, false, stleft,stright, null,this);
180 craux.outputline("if (maybe) printf(\"REALLY BAD\");");
184 case MultUpdateNode.REMOVE: {
186 String methodcall="void "+methodname+"(struct "+name+"_state * "+ststate+", struct "+name+" * "+stmodel+", struct RepairHash * "+strepairtable;
187 for(int j=0;j<r.numQuantifiers();j++) {
188 Quantifier q=r.getQuantifier(j);
189 if (q instanceof SetQuantifier) {
190 SetQuantifier sq=(SetQuantifier) q;
191 methodcall+=","+sq.getVar().getType().getGenerateType().getSafeSymbol()+" "+sq.getVar().getSafeSymbol();
192 } else if (q instanceof RelationQuantifier) {
193 RelationQuantifier rq=(RelationQuantifier) q;
195 methodcall+=","+rq.x.getType().getGenerateType().getSafeSymbol()+" "+rq.x.getSafeSymbol();
196 methodcall+=","+rq.y.getType().getGenerateType().getSafeSymbol()+" "+rq.y.getSafeSymbol();
197 } else if (q instanceof ForQuantifier) {
198 ForQuantifier fq=(ForQuantifier) q;
199 methodcall+=",int "+fq.getVar().getSafeSymbol();
203 crhead.outputline(methodcall+";");
204 craux.outputline(methodcall);
207 craux.addDeclaration("int","maybe");
208 craux.outputline("maybe=0;");
209 if (Compiler.GENERATEINSTRUMENT)
210 craux.outputline("updatecount++;");
211 final SymbolTable st2 = un.getRule().getSymbolTable();
212 CodeWriter cr2 = new StandardCodeWriter(outputaux) {
213 public SymbolTable getSymbolTable() { return st2; }
215 un.generate(cr2, true, false, null,null, null,this);
216 craux.outputline("if (maybe) printf(\"REALLY BAD\");");
221 case MultUpdateNode.MODIFY: {
223 String methodcall="void "+methodname+"(struct "+name+"_state * "+ststate+", struct "+name+" * "+stmodel+", struct RepairHash * "+strepairtable;
224 for(int j=0;j<r.numQuantifiers();j++) {
225 Quantifier q=r.getQuantifier(j);
226 if (q instanceof SetQuantifier) {
227 SetQuantifier sq=(SetQuantifier) q;
228 methodcall+=","+sq.getVar().getType().getGenerateType().getSafeSymbol()+" "+sq.getVar().getSafeSymbol();
229 } else if (q instanceof RelationQuantifier) {
230 RelationQuantifier rq=(RelationQuantifier) q;
232 methodcall+=", "+rq.x.getType().getGenerateType().getSafeSymbol()+" "+rq.x.getSafeSymbol();
233 methodcall+=", "+rq.y.getType().getGenerateType().getSafeSymbol()+" "+rq.y.getSafeSymbol();
234 } else if (q instanceof ForQuantifier) {
235 ForQuantifier fq=(ForQuantifier) q;
236 methodcall+=", int "+fq.getVar().getSafeSymbol();
239 methodcall+=", int "+stleft+", int "+stright+", int "+stnew;
241 crhead.outputline(methodcall+";");
242 craux.outputline(methodcall);
245 craux.outputline("int maybe=0;");
246 if (Compiler.GENERATEINSTRUMENT)
247 craux.outputline("updatecount++;");
248 final SymbolTable st2 = un.getRule().getSymbolTable();
249 CodeWriter cr2 = new StandardCodeWriter(outputaux) {
250 public SymbolTable getSymbolTable() { return st2; }
252 un.generate(cr2, false, true, stleft, stright, stnew, this);
253 craux.outputline("if (maybe) printf(\"REALLY BAD\");");
260 throw new Error("Nonimplement Update");
266 private void generate_call() {
267 CodeWriter cr = new StandardCodeWriter(outputrepair);
268 VarDescriptor vdstate=VarDescriptor.makeNew("repairstate");
269 cr.addDeclaration("struct "+ name+"_state *", vdstate.getSafeSymbol());
270 cr.outputline(vdstate.getSafeSymbol()+"=allocate"+name+"_state();");
271 Iterator globals=state.stGlobals.descriptors();
272 while (globals.hasNext()) {
273 VarDescriptor vd=(VarDescriptor) globals.next();
274 cr.outputline(vdstate.getSafeSymbol()+"->"+vd.getSafeSymbol()+"=("+vd.getType().getGenerateType().getSafeSymbol()+")"+vd.getSafeSymbol()+";");
276 /* Insert repair here */
277 cr.outputline("doanalysis("+vdstate.getSafeSymbol()+");");
278 globals=state.stGlobals.descriptors();
279 while (globals.hasNext()) {
280 VarDescriptor vd=(VarDescriptor) globals.next();
281 cr.outputline("*(("+vd.getType().getGenerateType().getSafeSymbol()+"*) &"+vd.getSafeSymbol()+")="+vdstate.getSafeSymbol()+"->"+vd.getSafeSymbol()+";");
283 cr.outputline("free"+name+"_state("+vdstate.getSafeSymbol()+");");
286 private void generate_tokentable() {
287 CodeWriter cr = new StandardCodeWriter(outputrepair);
288 Iterator tokens = TokenLiteralExpr.tokens.keySet().iterator();
291 cr.outputline("/* Token values*/");
294 while (tokens.hasNext()) {
295 Object token = tokens.next();
296 cr.outputline("/* " + token.toString() + " = " + TokenLiteralExpr.tokens.get(token).toString()+"*/");
303 private void generate_stateobject() {
304 CodeWriter crhead = new StandardCodeWriter(outputhead);
305 CodeWriter craux = new StandardCodeWriter(outputaux);
306 crhead.outputline("struct "+name+"_state {");
307 Iterator globals=state.stGlobals.descriptors();
308 while (globals.hasNext()) {
309 VarDescriptor vd=(VarDescriptor) globals.next();
310 crhead.outputline(vd.getType().getGenerateType().getSafeSymbol()+" "+vd.getSafeSymbol()+";");
312 crhead.outputline("};");
313 crhead.outputline("struct "+name+"_state * allocate"+name+"_state();");
314 craux.outputline("struct "+name+"_state * allocate"+name+"_state()");
316 craux.outputline("return (struct "+name+"_state *) malloc(sizeof(struct "+name+"_state));");
319 crhead.outputline("void free"+name+"_state(struct "+name+"_state *);");
320 craux.outputline("void free"+name+"_state(struct "+name+"_state * thisvar)");
322 craux.outputline("free(thisvar);");
325 crhead.outputline("void "+name+"_statecomputesizes(struct "+name+"_state * ,int *,int **);");
326 crhead.outputline("void "+name+"_staterecomputesizes(struct "+name+"_state *);");
329 private void generate_computesizes() {
330 int max=TypeDescriptor.counter;
331 TypeDescriptor[] tdarray=new TypeDescriptor[max];
332 for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
333 TypeDescriptor ttd=(TypeDescriptor)it.next();
334 tdarray[ttd.getId()]=ttd;
336 final SymbolTable st = state.stGlobals;
337 CodeWriter cr = new StandardCodeWriter(outputaux) {
338 public SymbolTable getSymbolTable() { return st; }
341 cr.outputline("void "+name+"_statecomputesizes(struct "+name+"_state * thisvar,int *sizearray,int **numele)");
344 cr.addDeclaration("int","maybe");
345 cr.outputline("maybe=0;");
346 for(int i=0;i<max;i++) {
347 TypeDescriptor td=tdarray[i];
348 Expr size=td.getSizeExpr();
349 VarDescriptor vd=VarDescriptor.makeNew("size");
350 size.generate(cr,vd);
351 cr.outputline("sizearray["+i+"]="+vd.getSafeSymbol()+";");
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);
364 cr.outputline("numele["+i+"]["+j+"]="+vd.getSafeSymbol()+";");
369 cr.outputline("if (maybe) printf(\"BAD ERROR\");");
374 private void generate_recomputesizes() {
375 int max=TypeDescriptor.counter;
376 TypeDescriptor[] tdarray=new TypeDescriptor[max];
377 for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
378 TypeDescriptor ttd=(TypeDescriptor)it.next();
379 tdarray[ttd.getId()]=ttd;
381 final SymbolTable st = state.stGlobals;
382 CodeWriter cr = new StandardCodeWriter(outputaux) {
383 public SymbolTable getSymbolTable() { return st; }
385 cr.outputline("void "+name+"_staterecomputesizes(struct "+name+"_state * thisvar)");
388 cr.addDeclaration("int","maybe");
389 cr.outputline("maybe=0;");
390 for(int i=0;i<max;i++) {
391 TypeDescriptor td=tdarray[i];
392 Expr size=td.getSizeExpr();
393 VarDescriptor vd=VarDescriptor.makeNew("size");
394 size.generate(cr,vd);
396 for(int i=0;i<max;i++) {
397 TypeDescriptor td=tdarray[i];
398 if (td instanceof StructureTypeDescriptor) {
399 StructureTypeDescriptor std=(StructureTypeDescriptor) td;
400 for(int j=0;j<std.fieldlist.size();j++) {
401 FieldDescriptor fd=(FieldDescriptor)std.fieldlist.get(j);
402 if (fd instanceof ArrayDescriptor) {
403 ArrayDescriptor ad=(ArrayDescriptor)fd;
404 Expr index=ad.getIndexBound();
405 VarDescriptor vd=VarDescriptor.makeNew("index");
406 index.generate(cr,vd);
411 cr.outputline("if (maybe) printf(\"BAD ERROR\");");
417 private void generate_hashtables() {
418 CodeWriter craux = new StandardCodeWriter(outputaux);
419 CodeWriter crhead = new StandardCodeWriter(outputhead);
420 crhead.outputline("#ifndef "+name+"_h");
421 crhead.outputline("#define "+name+"_h");
422 crhead.outputline("#include \"SimpleHash.h\"");
423 crhead.outputline("#include \"instrument.h\"");
424 crhead.outputline("#include <stdio.h>");
425 crhead.outputline("#include <stdlib.h>");
426 crhead.outputline("struct "+name+" * allocate"+name+"();");
427 crhead.outputline("void free"+name+"(struct "+name+" *);");
428 crhead.outputline("struct "+name+" {");
429 craux.outputline("#include \""+headername+"\"");
430 craux.outputline("#include \"size.h\"");
432 craux.outputline("#include <sys/time.h>");
434 if (Compiler.ALLOCATECPLUSPLUS) {
435 for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
436 TypeDescriptor td=(TypeDescriptor)it.next();
437 if (td instanceof StructureTypeDescriptor) {
438 if (((StructureTypeDescriptor)td).size()>0) {
439 FieldDescriptor fd=((StructureTypeDescriptor)td).get(0);
440 if (fd.getSymbol().startsWith("_vptr_")) {
441 String vtable="_ZTV";
442 vtable+=td.getSymbol().length();
443 vtable+=td.getSymbol();
444 craux.outputline("extern void * "+vtable+";");
450 craux.outputline("struct "+ name+"* allocate"+name+"()");
452 craux.outputline("/* creating hashtables */");
455 Iterator sets = state.stSets.descriptors();
456 craux.addDeclaration("struct "+name+"*", "thisvar");
457 craux.outputline("thisvar=(struct "+name+"*) malloc(sizeof(struct "+name+"));");
459 /* first pass create all the hash tables */
460 while (sets.hasNext()) {
461 SetDescriptor set = (SetDescriptor) sets.next();
462 crhead.outputline("struct SimpleHash* " + set.getJustSafeSymbol() + "_hash;");
463 craux.outputline(set.getSafeSymbol() + "_hash = noargallocateSimpleHash();");
466 /* second pass build relationships between hashtables */
467 sets = state.stSets.descriptors();
469 while (sets.hasNext()) {
470 SetDescriptor set = (SetDescriptor) sets.next();
471 Iterator subsets = set.subsets();
473 while (subsets.hasNext()) {
474 SetDescriptor subset = (SetDescriptor) subsets.next();
475 craux.outputline("SimpleHashaddParent("+subset.getSafeSymbol() +"_hash ,"+ set.getSafeSymbol() + "_hash);");
479 /* build relations */
480 Iterator relations = state.stRelations.descriptors();
482 /* first pass create all the hash tables */
483 while (relations.hasNext()) {
484 RelationDescriptor relation = (RelationDescriptor) relations.next();
486 if (relation.testUsage(RelationDescriptor.IMAGE)) {
487 crhead.outputline("struct SimpleHash* " + relation.getJustSafeSymbol() + "_hash;");
488 craux.outputline(relation.getSafeSymbol() + "_hash = noargallocateSimpleHash();");
491 if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
492 crhead.outputline("struct SimpleHash* " + relation.getJustSafeSymbol() + "_hashinv;");
493 craux.outputline(relation.getSafeSymbol() + "_hashinv = noargallocateSimpleHash();");
496 craux.outputline("return thisvar;");
498 crhead.outputline("};");
499 craux.outputline("void free"+name+"(struct "+ name +"* thisvar)");
501 craux.outputline("/* deleting hashtables */");
503 /* build destructor */
504 sets = state.stSets.descriptors();
506 /* first pass create all the hash tables */
507 while (sets.hasNext()) {
508 SetDescriptor set = (SetDescriptor) sets.next();
509 craux.outputline("freeSimpleHash("+set.getSafeSymbol() + "_hash);");
512 /* destroy relations */
513 relations = state.stRelations.descriptors();
515 /* first pass create all the hash tables */
516 while (relations.hasNext()) {
517 RelationDescriptor relation = (RelationDescriptor) relations.next();
519 if (relation.testUsage(RelationDescriptor.IMAGE)) {
520 craux.outputline("freeSimpleHash("+relation.getSafeSymbol() + "_hash);");
523 if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
524 craux.outputline("freeSimpleHash(" + relation.getSafeSymbol() + "_hashinv);");
527 craux.outputline("free(thisvar);");
531 private void generate_start() {
532 CodeWriter crhead = new StandardCodeWriter(outputhead);
533 CodeWriter craux = new StandardCodeWriter(outputaux);
534 oldmodel=VarDescriptor.makeNew("oldmodel");
535 newmodel=VarDescriptor.makeNew("newmodel");
536 worklist=VarDescriptor.makeNew("worklist");
537 goodflag=VarDescriptor.makeNew("goodflag");
538 repairtable=VarDescriptor.makeNew("repairtable");
540 if (Compiler.GENERATEINSTRUMENT) {
541 craux.outputline("int updatecount;");
542 craux.outputline("int rebuildcount;");
543 craux.outputline("int abstractcount;");
546 crhead.outputline("void doanalysis(struct "+name+"_state *);");
547 craux.outputline("void doanalysis(struct "+name+"_state * thisvar)");
549 craux.outputline("int highmark;"); /* This declaration is special...need it to be first */
553 craux.outputline("struct timeval _begin_time,_end_time;");
554 craux.outputline("gettimeofday(&_begin_time,NULL);");
556 if (Compiler.GENERATEINSTRUMENT) {
557 craux.outputline("updatecount=0;");
558 craux.outputline("rebuildcount=0;");
559 craux.outputline("abstractcount=0;");
563 craux.addDeclaration("struct "+name+ " * ",oldmodel.getSafeSymbol());
564 craux.outputline(oldmodel.getSafeSymbol()+"=0;");
565 craux.addDeclaration("struct WorkList * ",worklist.getSafeSymbol());
566 craux.outputline(worklist.getSafeSymbol()+" = allocateWorkList();");
567 craux.addDeclaration("struct RepairHash * ",repairtable.getSafeSymbol());
568 craux.outputline(repairtable.getSafeSymbol()+"=0;");
569 craux.outputline("initializestack(&highmark);");
570 craux.outputline("computesizes(thisvar);");
571 craux.outputline(name+"_staterecomputesizes(thisvar);");
572 craux.outputline("while (1)");
574 craux.addDeclaration("struct "+name+ " * ",newmodel.getSafeSymbol());
575 craux.outputline(newmodel.getSafeSymbol()+"=allocate"+name+"();");
576 craux.outputline("WorkListreset("+worklist.getSafeSymbol()+");");
577 if (Compiler.GENERATEINSTRUMENT)
578 craux.outputline("rebuildcount++;");
581 private void generate_teardown() {
582 CodeWriter cr = new StandardCodeWriter(outputaux);
585 cr.outputline("gettimeofday(&_end_time,NULL);");
586 cr.outputline("printf(\"time=%ld uS\\n\",(_end_time.tv_sec-_begin_time.tv_sec)*1000000+_end_time.tv_usec-_begin_time.tv_usec);");
589 if (Compiler.GENERATEINSTRUMENT) {
590 cr.outputline("printf(\"updatecount=%d\\n\",updatecount);");
591 cr.outputline("printf(\"rebuildcount=%d\\n\",rebuildcount);");
592 cr.outputline("printf(\"abstractcount=%d\\n\",abstractcount);");
597 private void generate_print() {
599 final SymbolTable st = new SymbolTable();
601 CodeWriter cr = new StandardCodeWriter(outputaux) {
602 public SymbolTable getSymbolTable() { return st; }
605 cr.outputline("/* printing sets!*/");
606 cr.outputline("printf(\"\\n\\nPRINTING SETS AND RELATIONS\\n\");");
608 Iterator setiterator = state.stSets.descriptors();
609 while (setiterator.hasNext()) {
610 SetDescriptor sd = (SetDescriptor) setiterator.next();
611 if (sd.getSymbol().equals("int") || sd.getSymbol().equals("token")) {
615 String setname = sd.getSafeSymbol();
618 cr.outputline("/* printing set " + setname+"*/");
619 cr.outputline("printf(\"\\nPrinting set " + sd.getSymbol() + " - %d elements \\n\", SimpleHashcountset("+setname+"_hash));");
620 cr.addDeclaration("struct SimpleIterator","__setiterator");
621 cr.outputline("SimpleHashiterator("+setname+"_hash,&__setiterator);");
622 cr.outputline("while (hasNext(&__setiterator))");
624 cr.addDeclaration("int","__setval");
625 cr.outputline("__setval = (int) next(&__setiterator);");
627 TypeDescriptor td = sd.getType();
628 if (td instanceof StructureTypeDescriptor) {
629 StructureTypeDescriptor std = (StructureTypeDescriptor) td;
630 VarDescriptor vd = new VarDescriptor ("__setval", "__setval", td, false);
631 std.generate_printout(cr, vd);
632 } else { // Missing type descriptor or reserved type, just print int
633 cr.outputline("printf(\"<%d> \", __setval);");
641 cr.outputline("printf(\"\\n\\n------------------- END PRINTING\\n\");");
645 private void generate_rules() {
646 /* first we must sort the rules */
647 RelationDescriptor.prefix = newmodel.getSafeSymbol()+"->";
648 SetDescriptor.prefix = newmodel.getSafeSymbol()+"->";
649 System.out.println("SCC="+(mrd.numSCC()-1));
650 for(int sccindex=0;sccindex<mrd.numSCC();sccindex++) {
651 ruleset=mrd.getSCC(sccindex);
652 boolean needworklist=mrd.hasCycle(sccindex);
655 Iterator iterator_rs = ruleset.iterator();
656 while (iterator_rs.hasNext()) {
657 Rule rule = (Rule) iterator_rs.next();
658 if (rule.getnogenerate())
661 final SymbolTable st = rule.getSymbolTable();
662 CodeWriter cr = new StandardCodeWriter(outputaux) {
663 public SymbolTable getSymbolTable() { return st; }
665 InvariantValue ivalue=new InvariantValue();
666 cr.setInvariantValue(ivalue);
668 cr.outputline("/* build " +escape(rule.toString())+"*/");
670 cr.addDeclaration("int","maybe");
671 cr.outputline("maybe=0;");
673 Expr ruleexpr=rule.getGuardExpr();
674 HashSet invariantvars=new HashSet();
675 Set invariants=ruleexpr.findInvariants(invariantvars);
677 if ((ruleexpr instanceof BooleanLiteralExpr)&&
678 ((BooleanLiteralExpr)ruleexpr).getValue()) {
679 if (rule.getInclusion() instanceof SetInclusion) {
680 invariants.addAll(((SetInclusion)rule.getInclusion()).getExpr().findInvariants(invariantvars));
681 } else if (rule.getInclusion() instanceof RelationInclusion) {
682 invariants.addAll(((RelationInclusion)rule.getInclusion()).getLeftExpr().findInvariants(invariantvars));
683 invariants.addAll(((RelationInclusion)rule.getInclusion()).getRightExpr().findInvariants(invariantvars));
686 ListIterator quantifiers = rule.quantifiers();
687 while (quantifiers.hasNext()) {
688 Quantifier quantifier = (Quantifier) quantifiers.next();
689 if (quantifier instanceof ForQuantifier) {
690 ForQuantifier fq=(ForQuantifier)quantifier;
691 invariants.addAll(fq.lower.findInvariants(invariantvars));
692 invariants.addAll(fq.upper.findInvariants(invariantvars));
696 int openparencount=0;
697 for(Iterator invit=invariants.iterator();invit.hasNext();) {
698 Expr invexpr=(Expr)invit.next();
699 VarDescriptor tmpvd=VarDescriptor.makeNew("tmpvar");
700 VarDescriptor maybevd=VarDescriptor.makeNew("maybevar");
701 invexpr.generate(cr,tmpvd);
702 cr.addDeclaration("int ",maybevd.getSafeSymbol());
703 cr.outputline(maybevd.getSafeSymbol()+"=maybe;");
704 cr.outputline("maybe=0;");
705 ivalue.assignPair(invexpr,tmpvd,maybevd);
709 quantifiers = rule.quantifiers();
710 while (quantifiers.hasNext()) {
711 Quantifier quantifier = (Quantifier) quantifiers.next();
712 quantifier.generate_open(cr);
717 rule.getGuardExpr().prettyPrint(cr);
720 /* now we have to generate the guard test */
721 VarDescriptor guardval = VarDescriptor.makeNew();
722 rule.getGuardExpr().generate(cr, guardval);
723 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
726 /* now we have to generate the inclusion code */
728 rule.getInclusion().generate(cr);
730 while (quantifiers.hasPrevious()) {
731 Quantifier quantifier = (Quantifier) quantifiers.previous();
735 while((openparencount--)>0)
742 CodeWriter cr2 = new StandardCodeWriter(outputaux);
744 for(Iterator initialworklist=ruleset.iterator();initialworklist.hasNext();) {
745 /** Construct initial worklist set */
746 Rule rule=(Rule)initialworklist.next();
747 cr2.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+",-1,0,0);");
750 cr2.outputline("while (WorkListhasMoreElements("+worklist.getSafeSymbol()+"))");
752 VarDescriptor idvar=VarDescriptor.makeNew("id");
753 cr2.addDeclaration("int ",idvar.getSafeSymbol());
754 cr2.outputline(idvar.getSafeSymbol()+"=WorkListgetid("+worklist.getSafeSymbol()+");");
756 String elseladder = "if";
758 Iterator iterator_rules = ruleset.iterator();
759 while (iterator_rules.hasNext()) {
761 Rule rule = (Rule) iterator_rules.next();
762 int dispatchid = rule.getNum();
765 final SymbolTable st = rule.getSymbolTable();
766 CodeWriter cr = new StandardCodeWriter(outputaux) {
767 public SymbolTable getSymbolTable() { return st; }
771 cr.outputline(elseladder + " ("+idvar.getSafeSymbol()+" == " + dispatchid + ")");
773 cr.addDeclaration("int","maybe");
774 cr.outputline("maybe=0;");
775 VarDescriptor typevar=VarDescriptor.makeNew("type");
776 VarDescriptor leftvar=VarDescriptor.makeNew("left");
777 VarDescriptor rightvar=VarDescriptor.makeNew("right");
778 cr.addDeclaration("int",typevar.getSafeSymbol());
779 cr.outputline(typevar.getSafeSymbol()+"= WorkListgettype("+worklist.getSafeSymbol()+");");
780 cr.addDeclaration("int",leftvar.getSafeSymbol());
781 cr.outputline(leftvar.getSafeSymbol()+"= WorkListgetlvalue("+worklist.getSafeSymbol()+");");
782 cr.addDeclaration("int",rightvar.getSafeSymbol());
783 cr.outputline(rightvar.getSafeSymbol()+"= WorkListgetrvalue("+worklist.getSafeSymbol()+");");
784 cr.outputline("/* build " +escape(rule.toString())+"*/");
787 for (int j=0;j<rule.numQuantifiers();j++) {
788 Quantifier quantifier = rule.getQuantifier(j);
789 quantifier.generate_open(cr, typevar.getSafeSymbol(),j,leftvar.getSafeSymbol(),rightvar.getSafeSymbol());
795 rule.getGuardExpr().prettyPrint(cr);
798 /* now we have to generate the guard test */
800 VarDescriptor guardval = VarDescriptor.makeNew();
801 rule.getGuardExpr().generate(cr, guardval);
803 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
806 /* now we have to generate the inclusion code */
808 rule.getInclusion().generate(cr);
811 for (int j=0;j<rule.numQuantifiers();j++) {
815 // close startblocks generated by DotExpr memory checks
816 //DotExpr.generate_memory_endblocks(cr);
818 cr.endblock(); // end else-if WORKLIST ladder
820 elseladder = "else if";
823 cr2.outputline("else");
825 cr2.outputline("printf(\"VERY BAD !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\\n\\n\");");
826 cr2.outputline("exit(1);");
828 // end block created for worklist
829 cr2.outputline("WorkListpop("+worklist.getSafeSymbol()+");");
835 public static String escape(String s) {
837 for(int i=0;i<s.length();i++) {
847 private void generate_checks() {
848 /* do constraint checks */
851 i=termination.constraintdependence.computeOrdering().iterator();
853 i=state.vConstraints.iterator();
854 for (; i.hasNext();) {
855 Constraint constraint;
857 constraint= (Constraint) ((GraphNode)i.next()).getOwner();
859 constraint=(Constraint)i.next();
862 final SymbolTable st = constraint.getSymbolTable();
863 CodeWriter cr = new StandardCodeWriter(outputaux);
864 cr.pushSymbolTable(constraint.getSymbolTable());
866 cr.outputline("/* checking " + escape(constraint.toString())+"*/");
869 ListIterator quantifiers = constraint.quantifiers();
871 while (quantifiers.hasNext()) {
872 Quantifier quantifier = (Quantifier) quantifiers.next();
873 quantifier.generate_open(cr);
876 cr.addDeclaration("int","maybe");
877 cr.outputline("maybe = 0;");
879 /* now we have to generate the guard test */
881 VarDescriptor constraintboolean = VarDescriptor.makeNew("constraintboolean");
882 constraint.getLogicStatement().generate(cr, constraintboolean);
884 cr.outputline("if (maybe)");
886 cr.outputline("printf(\"maybe fail " + escape(constraint.toString()) + ". \\n\");");
887 //cr.outputline("exit(1);");
890 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
892 if (!Compiler.REPAIR||Compiler.GENERATEDEBUGHOOKS)
893 cr.outputline("printf(\"fail " + escape(constraint.toString()) + ". \\n\");");
895 if (Compiler.REPAIR) {
897 /* Build new repair table */
899 cr.outputline("if ("+repairtable.getSafeSymbol()+")");
900 cr.outputline("freeRepairHash("+repairtable.getSafeSymbol()+");");
901 cr.outputline(repairtable.getSafeSymbol()+"=noargallocateRepairHash();");
903 if (Compiler.GENERATEDEBUGHOOKS)
904 cr.outputline("debughook();");
905 /* Compute cost of each repair */
906 VarDescriptor mincost=VarDescriptor.makeNew("mincost");
907 VarDescriptor mincostindex=VarDescriptor.makeNew("mincostindex");
908 Vector dnfconst=new Vector();
909 dnfconst.addAll((Set)termination.conjunctionmap.get(constraint));
911 if (dnfconst.size()<=1) {
912 cr.addDeclaration("int",mincostindex.getSafeSymbol());
913 cr.outputline(mincostindex.getSafeSymbol()+"=0;");
915 if (dnfconst.size()>1) {
916 cr.addDeclaration("int",mincostindex.getSafeSymbol());
918 for(int j=0;j<dnfconst.size();j++) {
919 GraphNode gn=(GraphNode)dnfconst.get(j);
920 Conjunction conj=((TermNode)gn.getOwner()).getConjunction();
921 if (removed.contains(gn))
924 VarDescriptor costvar;
928 costvar=VarDescriptor.makeNew("cost");
929 for(int k=0;k<conj.size();k++) {
930 DNFPredicate dpred=conj.get(k);
931 Predicate p=dpred.getPredicate();
932 boolean negate=dpred.isNegated();
933 VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
934 p.generate(cr,predvalue);
936 cr.addDeclaration("int",costvar.getSafeSymbol());
937 cr.outputline(costvar.getSafeSymbol()+"=0;");
940 cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
942 cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
943 cr.outputline(costvar.getSafeSymbol()+"+="+cost.getCost(dpred)+";");
947 cr.outputline("if ("+costvar.getSafeSymbol()+"<"+mincost.getSafeSymbol()+")");
949 cr.outputline(mincost.getSafeSymbol()+"="+costvar.getSafeSymbol()+";");
950 cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
953 cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
957 cr.outputline("switch("+mincostindex.getSafeSymbol()+")");
959 for(int j=0;j<dnfconst.size();j++) {
960 GraphNode gn=(GraphNode)dnfconst.get(j);
961 Conjunction conj=((TermNode)gn.getOwner()).getConjunction();
962 if (removed.contains(gn))
964 cr.outputline("case "+j+":");
966 for(int k=0;k<conj.size();k++) {
967 DNFPredicate dpred=conj.get(k);
968 Predicate p=dpred.getPredicate();
969 boolean negate=dpred.isNegated();
970 VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
971 p.generate(cr,predvalue);
973 cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
975 cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
977 if (Compiler.GENERATEINSTRUMENT)
978 cr.outputline("abstractcount++;");
979 if (p instanceof InclusionPredicate)
980 generateinclusionrepair(conj,dpred, cr);
981 else if (p instanceof ExprPredicate) {
982 ExprPredicate ep=(ExprPredicate)p;
983 if (ep.getType()==ExprPredicate.SIZE)
984 generatesizerepair(conj,dpred,cr);
985 else if (ep.getType()==ExprPredicate.COMPARISON)
986 generatecomparisonrepair(conj,dpred,cr);
987 } else throw new Error("Unrecognized Predicate");
992 cr.outputline("break;");
996 cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
997 cr.outputline("free"+name+"("+oldmodel.getSafeSymbol()+");");
998 cr.outputline(oldmodel.getSafeSymbol()+"="+newmodel.getSafeSymbol()+";");
999 cr.outputline("goto rebuild;"); /* Rebuild model and all */
1003 while (quantifiers.hasPrevious()) {
1004 Quantifier quantifier = (Quantifier) quantifiers.previous();
1012 CodeWriter cr = new StandardCodeWriter(outputaux);
1014 cr.outputline("if ("+repairtable.getSafeSymbol()+")");
1015 cr.outputline("freeRepairHash("+repairtable.getSafeSymbol()+");");
1016 cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
1017 cr.outputline("free"+name+"("+oldmodel.getSafeSymbol()+");");
1018 cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1019 cr.outputline("freeWorkList("+worklist.getSafeSymbol()+");");
1020 cr.outputline("resettypemap();");
1021 cr.outputline("break;");
1023 cr.outputline("rebuild:");
1027 private MultUpdateNode getmultupdatenode(Conjunction conj, DNFPredicate dpred, int repairtype) {
1028 Set nodes=getmultupdatenodeset(conj,dpred,repairtype);
1029 Iterator it=nodes.iterator();
1031 return (MultUpdateNode)it.next();
1036 private Set getmultupdatenodeset(Conjunction conj, DNFPredicate dpred, int repairtype) {
1037 HashSet hs=new HashSet();
1038 GraphNode gn=(GraphNode) termination.conjtonodemap.get(conj);
1039 for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1040 GraphNode gn2=((GraphNode.Edge) edgeit.next()).getTarget();
1041 TermNode tn2=(TermNode)gn2.getOwner();
1042 if (tn2.getType()==TermNode.ABSTRACT) {
1043 AbstractRepair ar=tn2.getAbstract();
1044 if (((repairtype==-1)||(ar.getType()==repairtype))&&
1045 ar.getPredicate()==dpred) {
1046 for(Iterator edgeit2=gn2.edges();edgeit2.hasNext();) {
1047 GraphNode gn3=((GraphNode.Edge) edgeit2.next()).getTarget();
1048 if (!removed.contains(gn3)) {
1049 TermNode tn3=(TermNode)gn3.getOwner();
1050 if (tn3.getType()==TermNode.UPDATE) {
1051 hs.add(tn3.getUpdate());
1061 private AbstractRepair getabstractrepair(Conjunction conj, DNFPredicate dpred, int repairtype) {
1062 HashSet hs=new HashSet();
1063 MultUpdateNode mun=null;
1064 GraphNode gn=(GraphNode) termination.conjtonodemap.get(conj);
1065 for(Iterator edgeit=gn.edges();(mun==null)&&edgeit.hasNext();) {
1066 GraphNode gn2=((GraphNode.Edge) edgeit.next()).getTarget();
1067 TermNode tn2=(TermNode)gn2.getOwner();
1068 if (tn2.getType()==TermNode.ABSTRACT) {
1069 AbstractRepair ar=tn2.getAbstract();
1070 if (((repairtype==-1)||(ar.getType()==repairtype))&&
1071 ar.getPredicate()==dpred) {
1080 /** Generates abstract (and concrete) repair for a comparison */
1082 private void generatecomparisonrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
1083 Set updates=getmultupdatenodeset(conj,dpred,AbstractRepair.MODIFYRELATION);
1084 AbstractRepair ar=getabstractrepair(conj,dpred,AbstractRepair.MODIFYRELATION);
1085 MultUpdateNode munmodify=null;
1086 MultUpdateNode munadd=null;
1087 MultUpdateNode munremove=null;
1088 for(Iterator it=updates.iterator();it.hasNext();) {
1089 MultUpdateNode mun=(MultUpdateNode)it.next();
1090 if (mun.getType()==MultUpdateNode.ADD) {
1092 } else if (mun.getType()==MultUpdateNode.REMOVE) {
1094 } else if (mun.getType()==MultUpdateNode.MODIFY) {
1099 ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
1100 RelationDescriptor rd=(RelationDescriptor)ep.getDescriptor();
1101 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1102 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1103 boolean inverted=ep.inverted();
1104 boolean negated=dpred.isNegated();
1105 OpExpr expr=(OpExpr)ep.expr;
1106 Opcode opcode=expr.getOpcode();
1107 VarDescriptor leftside=VarDescriptor.makeNew("leftside");
1108 VarDescriptor rightside=VarDescriptor.makeNew("rightside");
1109 VarDescriptor newvalue=VarDescriptor.makeNew("newvalue");
1110 boolean needremoveloop=ar.mayNeedFunctionEnforcement(state)&&ar.needsRemoves(state);
1112 if (needremoveloop&&((munadd==null)||(munremove==null))) {
1113 System.out.println("Warning: need to have individual remove operations for"+dpred.name());
1114 needremoveloop=false;
1116 if (needremoveloop) {
1117 cr.outputline("while (1)");
1121 ((RelationExpr)expr.getLeftExpr()).getExpr().generate(cr,leftside);
1122 expr.getRightExpr().generate(cr,newvalue);
1123 cr.addDeclaration(rd.getRange().getType().getGenerateType().getSafeSymbol(),rightside.getSafeSymbol());
1124 cr.outputline("SimpleHashget("+rd.getSafeSymbol()+"_hash,"+leftside.getSafeSymbol()+", &"+rightside.getSafeSymbol()+");");
1126 ((RelationExpr)expr.getLeftExpr()).getExpr().generate(cr,rightside);
1127 expr.getRightExpr().generate(cr,newvalue);
1128 cr.outputline(rd.getDomain().getType().getGenerateType().getSafeSymbol()+" "+leftside.getSafeSymbol()+";");
1129 cr.outputline("SimpleHashget("+rd.getSafeSymbol()+"_hashinv,"+rightside.getSafeSymbol()+", &"+leftside.getSafeSymbol()+");");
1132 opcode=Opcode.translateOpcode(negated,opcode);
1134 if (opcode==Opcode.GT) {
1135 cr.outputline(newvalue.getSafeSymbol()+"++;");
1136 } else if (opcode==Opcode.GE) {
1138 } else if (opcode==Opcode.LT) {
1139 cr.outputline(newvalue.getSafeSymbol()+"--;");
1140 } else if (opcode==Opcode.LE) {
1142 } else if (opcode==Opcode.EQ) {
1144 } else if (opcode==Opcode.NE) { /* search for FLAGNE if this is changed*/
1145 cr.outputline(newvalue.getSafeSymbol()+"++;");
1147 throw new Error("Unrecognized Opcode");
1149 /* Do abstract repairs */
1151 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash, "+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1153 if (usageinvimage) {
1154 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv, "+rightside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
1157 if (needremoveloop) {
1159 cr.outputline("if (SimpleHashcontainskey("+rd.getSafeSymbol()+"_hash, "+leftside.getSafeSymbol()+"))");
1162 cr.outputline("if (SimpleHashcontainskey("+rd.getSafeSymbol()+"_hashinv, "+rightside.getSafeSymbol()+"))");
1165 for(int i=0;i<state.vRules.size();i++) {
1166 Rule r=(Rule)state.vRules.get(i);
1167 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1168 for(int j=0;j<munremove.numUpdates();j++) {
1169 UpdateNode un=munremove.getUpdate(i);
1170 if (un.getRule()==r) {
1171 /* Update for rule r */
1172 String name=(String)updatenames.get(un);
1173 cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+");");
1178 cr.outputline("continue;");
1184 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash,"+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1186 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, "+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1189 if (usageinvimage) {
1191 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, "+newvalue.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
1193 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv,"+rightside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1196 /* Do concrete repairs */
1197 if (munmodify!=null&&(!ar.mayNeedFunctionEnforcement(state))||(munadd==null)||(ar.needsRemoves(state)&&(munremove==null))) {
1198 for(int i=0;i<state.vRules.size();i++) {
1199 Rule r=(Rule)state.vRules.get(i);
1200 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1201 for(int j=0;j<munmodify.numUpdates();j++) {
1202 UpdateNode un=munmodify.getUpdate(j);
1203 if (un.getRule()==r) {
1204 /* Update for rule r */
1205 String name=(String)updatenames.get(un);
1206 cr.outputline("RepairHashaddrelation2("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+","+newvalue.getSafeSymbol()+");");
1212 /* Start with scheduling removal */
1213 if (ar.needsRemoves(state))
1214 for(int i=0;i<state.vRules.size();i++) {
1215 Rule r=(Rule)state.vRules.get(i);
1216 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1217 for(int j=0;j<munremove.numUpdates();j++) {
1218 UpdateNode un=munremove.getUpdate(i);
1219 if (un.getRule()==r) {
1220 /* Update for rule r */
1221 String name=(String)updatenames.get(un);
1222 cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+");");
1227 /* Now do addition */
1228 UpdateNode un=munadd.getUpdate(0);
1229 String name=(String)updatenames.get(un);
1231 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1233 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1236 if (needremoveloop) {
1237 cr.outputline("break;");
1242 public void generatesizerepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr) {
1243 ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
1244 OpExpr expr=(OpExpr)ep.expr;
1245 Opcode opcode=expr.getOpcode();
1246 opcode=Opcode.translateOpcode(dpred.isNegated(),opcode);
1248 MultUpdateNode munremove;
1250 MultUpdateNode munadd;
1251 if (ep.getDescriptor() instanceof RelationDescriptor) {
1252 munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMRELATION);
1253 munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTORELATION);
1255 munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMSET);
1256 munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTOSET);
1258 int size=ep.rightSize();
1259 VarDescriptor sizevar=VarDescriptor.makeNew("size");
1260 ((OpExpr)expr).left.generate(cr, sizevar);
1261 VarDescriptor change=VarDescriptor.makeNew("change");
1262 cr.addDeclaration("int",change.getSafeSymbol());
1263 boolean generateadd=false;
1264 boolean generateremove=false;
1265 if (opcode==Opcode.GT) {
1266 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
1268 generateremove=false;
1269 } else if (opcode==Opcode.GE) {
1270 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1272 generateremove=false;
1273 } else if (opcode==Opcode.LT) {
1274 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size-1)+"-"+sizevar.getSafeSymbol()+";");
1276 generateremove=true;
1277 } else if (opcode==Opcode.LE) {
1278 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1280 generateremove=true;
1281 } else if (opcode==Opcode.EQ) {
1282 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1287 generateremove=true;
1288 } else if (opcode==Opcode.NE) {
1289 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
1291 generateremove=false;
1293 throw new Error("Unrecognized Opcode");
1296 // In some cases the analysis has determined that generating removes
1298 if (generateremove&&munremove==null)
1299 generateremove=false;
1301 Descriptor d=ep.getDescriptor();
1302 if (generateremove) {
1303 cr.outputline("for(;"+change.getSafeSymbol()+"<0;"+change.getSafeSymbol()+"++)");
1305 /* Find element to remove */
1306 VarDescriptor leftvar=VarDescriptor.makeNew("leftvar");
1307 VarDescriptor rightvar=VarDescriptor.makeNew("rightvar");
1308 if (d instanceof RelationDescriptor) {
1309 if (ep.inverted()) {
1310 ((ImageSetExpr)((SizeofExpr)expr.left).setexpr).generate_leftside(cr,rightvar);
1311 cr.addDeclaration("int",leftvar.getSafeSymbol());
1312 cr.outputline("SimpleHashget("+d.getSafeSymbol()+"_hashinv,(int)"+rightvar.getSafeSymbol()+", &"+leftvar.getSafeSymbol()+");");
1314 ((ImageSetExpr)((SizeofExpr)expr.left).setexpr).generate_leftside(cr,leftvar);
1315 cr.addDeclaration("int",rightvar.getSafeSymbol());
1316 cr.outputline("SimpleHashget("+d.getSafeSymbol()+"_hash ,(int)"+leftvar.getSafeSymbol()+", &"+rightvar.getSafeSymbol()+");");
1319 cr.addDeclaration("int",leftvar.getSafeSymbol());
1320 cr.outputline(leftvar.getSafeSymbol()+"= SimpleHashfirstkey("+d.getSafeSymbol()+"_hash);");
1322 /* Generate abstract remove instruction */
1323 if (d instanceof RelationDescriptor) {
1324 RelationDescriptor rd=(RelationDescriptor) d;
1325 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1326 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1328 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1330 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv ,(int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1332 cr.outputline("SimpleHashremove("+d.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1334 /* Generate concrete remove instruction */
1335 for(int i=0;i<state.vRules.size();i++) {
1336 Rule r=(Rule)state.vRules.get(i);
1337 if (r.getInclusion().getTargetDescriptors().contains(d)) {
1338 for(int j=0;j<munremove.numUpdates();j++) {
1339 UpdateNode un=munremove.getUpdate(j);
1340 if (un.getRule()==r) {
1341 /* Update for rule rule r */
1342 String name=(String)updatenames.get(un);
1343 if (d instanceof RelationDescriptor) {
1344 cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
1346 cr.outputline("RepairHashaddset("+repairtable.getSafeSymbol()+","+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1355 // In some cases the analysis has determined that generating removes
1357 if (generateadd&&munadd==null)
1362 cr.outputline("for(;"+change.getSafeSymbol()+">0;"+change.getSafeSymbol()+"--)");
1364 VarDescriptor newobject=VarDescriptor.makeNew("newobject");
1365 if (d instanceof RelationDescriptor) {
1366 VarDescriptor otherside=VarDescriptor.makeNew("otherside");
1367 ((ImageSetExpr)((SizeofExpr)expr.left).setexpr).generate_leftside(cr,otherside);
1369 RelationDescriptor rd=(RelationDescriptor)d;
1370 if (termination.sources.relsetSource(rd,!ep.inverted())) {
1372 SetDescriptor sd=termination.sources.relgetSourceSet(rd,!ep.inverted());
1373 VarDescriptor iterator=VarDescriptor.makeNew("iterator");
1374 cr.addDeclaration(sd.getType().getGenerateType().getSafeSymbol(),newobject.getSafeSymbol());
1375 cr.addDeclaration("struct SimpleIterator",iterator.getSafeSymbol());
1376 cr.outputline("for(SimpleHashiterator("+sd.getSafeSymbol()+"_hash , &"+ iterator.getSafeSymbol() +"); hasNext(&"+iterator.getSafeSymbol()+");)");
1378 if (ep.inverted()) {
1379 cr.outputline("if (!SimpleHashcontainskeydata("+rd.getSafeSymbol()+"_hashinv,"+iterator.getSafeSymbol()+".key(),"+otherside.getSafeSymbol()+"))");
1381 cr.outputline("if (!SimpleHashcontainskeydata("+rd.getSafeSymbol()+"_hash, "+otherside.getSafeSymbol()+","+iterator.getSafeSymbol()+".key()))");
1383 cr.outputline(newobject.getSafeSymbol()+"=key(&"+iterator.getSafeSymbol()+");");
1384 cr.outputline("next(&"+iterator.getSafeSymbol()+");");
1386 } else if (termination.sources.relallocSource(rd,!ep.inverted())) {
1387 /* Allocation Source*/
1388 termination.sources.relgenerateSourceAlloc(cr,newobject,rd,!ep.inverted());
1389 } else throw new Error("No source for adding to Relation");
1390 if (ep.inverted()) {
1391 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1392 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1394 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, "+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1396 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, "+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1398 UpdateNode un=munadd.getUpdate(0);
1399 String name=(String)updatenames.get(un);
1400 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1402 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1403 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1405 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, "+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1407 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, "+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1408 UpdateNode un=munadd.getUpdate(0);
1409 String name=(String)updatenames.get(un);
1410 cr.outputline(name+"(thisvar, "+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1413 SetDescriptor sd=(SetDescriptor)d;
1414 if (termination.sources.setSource(sd)) {
1417 SetDescriptor sourcesd=termination.sources.getSourceSet(sd);
1418 VarDescriptor iterator=VarDescriptor.makeNew("iterator");
1419 cr.addDeclaration(sourcesd.getType().getGenerateType().getSafeSymbol(), newobject.getSafeSymbol());
1420 cr.addDeclaration("struct SimpleIterator",iterator.getSafeSymbol());
1421 cr.outputline("for(SimpleHashiterator("+sourcesd.getSafeSymbol()+"_hash, &"+iterator.getSafeSymbol()+"); hasNext(&"+iterator.getSafeSymbol()+");)");
1423 cr.outputline("if (!SimpleHashcontainskey("+sd.getSafeSymbol()+"_hash, key(&"+iterator.getSafeSymbol()+")))");
1424 cr.outputline(newobject.getSafeSymbol()+"=key(&"+iterator.getSafeSymbol()+");");
1425 cr.outputline("next(&"+iterator.getSafeSymbol()+");");
1427 } else if (termination.sources.allocSource(sd)) {
1428 /* Allocation Source*/
1429 termination.sources.generateSourceAlloc(cr,newobject,sd);
1430 } else throw new Error("No source for adding to Set");
1431 cr.outputline("SimpleHashadd("+sd.getSafeSymbol()+"_hash, "+newobject.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1432 UpdateNode un=munadd.getUpdate(0);
1433 String name=(String)updatenames.get(un);
1434 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1440 public void generateinclusionrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
1441 InclusionPredicate ip=(InclusionPredicate) dpred.getPredicate();
1442 boolean negated=dpred.isNegated();
1443 MultUpdateNode mun=getmultupdatenode(conj,dpred,-1);
1444 VarDescriptor leftvar=VarDescriptor.makeNew("left");
1445 ip.expr.generate(cr, leftvar);
1448 if (ip.setexpr instanceof ImageSetExpr) {
1449 ImageSetExpr ise=(ImageSetExpr) ip.setexpr;
1450 VarDescriptor rightvar=ise.getVar();
1451 boolean inverse=ise.inverted();
1452 RelationDescriptor rd=ise.getRelation();
1453 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1454 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1457 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1459 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1462 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash ,(int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1464 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1466 for(int i=0;i<state.vRules.size();i++) {
1467 Rule r=(Rule)state.vRules.get(i);
1468 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1469 for(int j=0;j<mun.numUpdates();j++) {
1470 UpdateNode un=mun.getUpdate(j);
1471 if (un.getRule()==r) {
1472 /* Update for rule rule r */
1473 String name=(String)updatenames.get(un);
1475 cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1477 cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
1484 SetDescriptor sd=ip.setexpr.sd;
1485 cr.outputline("SimpleHashremove("+sd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1487 for(int i=0;i<state.vRules.size();i++) {
1488 Rule r=(Rule)state.vRules.get(i);
1489 if (r.getInclusion().getTargetDescriptors().contains(sd)) {
1490 for(int j=0;j<mun.numUpdates();j++) {
1491 UpdateNode un=mun.getUpdate(j);
1492 if (un.getRule()==r) {
1493 /* Update for rule rule r */
1494 String name=(String)updatenames.get(un);
1495 cr.outputline("RepairHashaddset("+repairtable.getSafeSymbol()+","+sd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1502 /* Generate update */
1503 if (ip.setexpr instanceof ImageSetExpr) {
1504 ImageSetExpr ise=(ImageSetExpr) ip.setexpr;
1505 VarDescriptor rightvar=ise.getVar();
1506 boolean inverse=ise.inverted();
1507 RelationDescriptor rd=ise.getRelation();
1508 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1509 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1512 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1514 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1517 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1519 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1521 UpdateNode un=mun.getUpdate(0);
1522 String name=(String)updatenames.get(un);
1524 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1526 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+");");
1529 SetDescriptor sd=ip.setexpr.sd;
1530 cr.outputline("SimpleHashadd("+sd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1532 UpdateNode un=mun.getUpdate(0);
1533 /* Update for rule rule r */
1534 String name=(String)updatenames.get(un);
1535 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1540 public static Vector getrulelist(Descriptor d) {
1541 Vector dispatchrules = new Vector();
1542 Vector rules = State.currentState.vRules;
1544 for (int i = 0; i < rules.size(); i++) {
1545 Rule rule = (Rule) rules.elementAt(i);
1546 Set requiredsymbols = rule.getRequiredDescriptors();
1548 // #TBD#: in general this is wrong because these descriptors may contain descriptors
1549 // bound in "in?" expressions which need to be dealt with in a topologically sorted
1552 if (rule.getRequiredDescriptors().contains(d)) {
1553 dispatchrules.addElement(rule);
1556 return dispatchrules;
1559 private boolean need_compensation(Rule r) {
1560 if (!Compiler.REPAIR)
1562 GraphNode gn=(GraphNode)termination.scopefalsify.get(r);
1563 for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1564 GraphNode.Edge edge=(GraphNode.Edge)edgeit.next();
1565 GraphNode gn2=edge.getTarget();
1566 if (!removed.contains(gn2)) {
1567 TermNode tn2=(TermNode)gn2.getOwner();
1568 if (tn2.getType()==TermNode.CONSEQUENCE)
1575 private UpdateNode find_compensation(Rule r) {
1576 GraphNode gn=(GraphNode)termination.scopefalsify.get(r);
1577 for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1578 GraphNode.Edge edge=(GraphNode.Edge)edgeit.next();
1579 GraphNode gn2=edge.getTarget();
1580 if (!removed.contains(gn2)) {
1581 TermNode tn2=(TermNode)gn2.getOwner();
1582 if (tn2.getType()==TermNode.UPDATE) {
1583 MultUpdateNode mun=tn2.getUpdate();
1584 for(int i=0;i<mun.numUpdates();i++) {
1585 UpdateNode un=mun.getUpdate(i);
1586 if (un.getRule()==r)
1592 throw new Error("No Compensation Update could be found");
1595 public void generate_dispatch(CodeWriter cr, RelationDescriptor rd, String leftvar, String rightvar) {
1596 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1597 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1599 if (!(usageinvimage||usageimage)) /* not used at all*/
1602 cr.outputline("/* RELATION DISPATCH */");
1603 if (Compiler.REPAIR) {
1604 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1606 cr.outputline("!SimpleHashcontainskeydata("+oldmodel.getSafeSymbol()+"->"+rd.getJustSafeSymbol() + "_hash, "+leftvar+","+rightvar+"))");
1608 cr.outputline("!SimpleHashcontainskeydata("+oldmodel.getSafeSymbol() +"->"+rd.getJustSafeSymbol()+"_hashinv, "+rightvar+","+leftvar+"))");
1611 /* Adding new item */
1612 /* Perform safety checks */
1613 cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
1614 cr.outputline("RepairHashcontainsrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+"))");
1616 /* Have update to call into */
1617 VarDescriptor mdfyptr=VarDescriptor.makeNew("modifyptr");
1618 VarDescriptor ismdfyptr=VarDescriptor.makeNew("ismodifyptr");
1619 cr.addDeclaration("int ",ismdfyptr.getSafeSymbol());
1620 cr.outputline(ismdfyptr.getSafeSymbol()+"=RepairHashismodify("+repairtable.getSafeSymbol()+","+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1626 for(int i=0;i<currentrule.numQuantifiers();i++) {
1627 if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1628 parttype=parttype+", int, int";
1630 parttype=parttype+", int";
1633 VarDescriptor tmpptr=VarDescriptor.makeNew("tempupdateptr");
1635 String methodcall="(thisvar,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
1636 for(int i=0;i<currentrule.numQuantifiers();i++) {
1637 Quantifier q=currentrule.getQuantifier(i);
1638 if (q instanceof SetQuantifier) {
1639 SetQuantifier sq=(SetQuantifier) q;
1640 methodcall+=","+sq.getVar().getSafeSymbol();
1641 } else if (q instanceof RelationQuantifier) {
1642 RelationQuantifier rq=(RelationQuantifier) q;
1643 methodcall+=","+rq.x.getSafeSymbol();
1644 methodcall+=","+rq.y.getSafeSymbol();
1645 } else if (q instanceof ForQuantifier) {
1646 ForQuantifier fq=(ForQuantifier) q;
1647 methodcall+=","+fq.getVar().getSafeSymbol();
1653 cr.addDeclaration("void *",tmpptr.getSafeSymbol());
1654 cr.outputline(tmpptr.getSafeSymbol()+"=");
1655 cr.outputline("(void *) RepairHashgetrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1656 cr.outputline("if ("+ismdfyptr.getSafeSymbol()+")");
1658 VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1659 String methodcallprefix="("+funptr.getSafeSymbol()+") ";
1661 cr.addDeclaration("int",mdfyptr.getSafeSymbol());
1662 cr.outputline(mdfyptr.getSafeSymbol()+"=RepairHashgetrelation2("+repairtable.getSafeSymbol()+","+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1663 cr.addDeclaration("void (*"+funptr.getSafeSymbol()+") (struct "+name+"_state *, struct "+name+"*, struct RepairHash *"+parttype+",int,int,int);");
1664 cr.outputline(funptr.getSafeSymbol()+"="+"(void (*) (struct "+name+"_state *, struct "+name+"*, struct RepairHash *"+parttype+",int,int,int)) "+tmpptr.getSafeSymbol()+";");
1665 cr.outputline(methodcallprefix+methodcall+","+leftvar+", "+rightvar+", "+mdfyptr.getSafeSymbol() +");");
1668 cr.outputline("else ");
1670 VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1671 String methodcallprefix="("+funptr.getSafeSymbol()+") ";
1673 cr.addDeclaration("void (*"+funptr.getSafeSymbol()+") (struct "+name+"_state *, struct "+name+"*,struct RepairHash *"+parttype+");");
1674 cr.outputline(funptr.getSafeSymbol()+"="+"(void (*) (struct "+name+"_state *,struct "+name+"*,struct RepairHash *"+parttype+")) "+tmpptr.getSafeSymbol()+";");
1675 cr.outputline(methodcallprefix+methodcall+");");
1678 cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1679 cr.outputline("goto rebuild;");
1683 /* Build standard compensation actions */
1684 if (need_compensation(currentrule)) {
1685 UpdateNode un=find_compensation(currentrule);
1686 String name=(String)updatenames.get(un);
1687 usedupdates.add(un); /* Mark as used */
1688 String methodcall=name+"(thisvar,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
1689 for(int i=0;i<currentrule.numQuantifiers();i++) {
1690 Quantifier q=currentrule.getQuantifier(i);
1691 if (q instanceof SetQuantifier) {
1692 SetQuantifier sq=(SetQuantifier) q;
1693 methodcall+=","+sq.getVar().getSafeSymbol();
1694 } else if (q instanceof RelationQuantifier) {
1695 RelationQuantifier rq=(RelationQuantifier) q;
1696 methodcall+=","+rq.x.getSafeSymbol();
1697 methodcall+=","+rq.y.getSafeSymbol();
1698 } else if (q instanceof ForQuantifier) {
1699 ForQuantifier fq=(ForQuantifier) q;
1700 methodcall+=","+fq.getVar().getSafeSymbol();
1704 cr.outputline(methodcall);
1705 cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1706 cr.outputline("goto rebuild;");
1712 String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1714 cr.addDeclaration("int" , addeditem);
1715 cr.outputline(addeditem + "=0;");
1717 String ifstring="if (!maybe";
1718 if (rd.getDomain().getType() instanceof StructureTypeDescriptor) {
1719 ifstring+="&&"+leftvar;
1722 if (rd.getRange().getType() instanceof StructureTypeDescriptor) {
1723 ifstring+="&&"+rightvar;
1728 if (rd.testUsage(RelationDescriptor.IMAGE)) {
1729 cr.outputline(ifstring);
1730 cr.outputline(addeditem + " = SimpleHashadd("+rd.getSafeSymbol()+"_hash, (int)" + leftvar + ", (int)" + rightvar+ ");");
1733 if (rd.testUsage(RelationDescriptor.INVIMAGE)) {
1734 cr.outputline(ifstring);
1735 cr.outputline(addeditem + " = SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, (int)" + rightvar + ", (int)" + leftvar + ");");
1739 Vector dispatchrules = getrulelist(rd);
1741 Set toremove=new HashSet();
1742 for(int i=0;i<dispatchrules.size();i++) {
1743 Rule r=(Rule)dispatchrules.get(i);
1744 if (!ruleset.contains(r))
1747 dispatchrules.removeAll(toremove);
1748 if (dispatchrules.size() == 0) {
1749 cr.outputline("/* nothing to dispatch */");
1754 cr.outputline("if (" + addeditem + ")");
1757 for(int i = 0; i < dispatchrules.size(); i++) {
1758 Rule rule = (Rule) dispatchrules.elementAt(i);
1759 if (rule.getGuardExpr().getRequiredDescriptors().contains(rd)) {
1760 /* Guard depends on this relation, so we recomput everything */
1761 cr.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+",-1,0,0);");
1763 for (int j=0;j<rule.numQuantifiers();j++) {
1764 Quantifier q=rule.getQuantifier(j);
1765 if (q.getRequiredDescriptors().contains(rd)) {
1767 cr.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+","+j+","+leftvar+","+rightvar+");");
1777 public void generate_dispatch(CodeWriter cr, SetDescriptor sd, String setvar) {
1778 cr.outputline("/* SET DISPATCH */");
1779 if (Compiler.REPAIR) {
1780 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1781 cr.outputline("!SimpleHashcontainskey("+oldmodel.getSafeSymbol() +"->"+sd.getJustSafeSymbol()+"_hash, "+setvar+"))");
1783 /* Adding new item */
1784 /* See if there is an outstanding update in the repairtable */
1785 cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
1786 cr.outputline("RepairHashcontainsset("+repairtable.getSafeSymbol()+","+sd.getNum()+","+currentrule.getNum()+","+setvar+"))");
1788 /* Have update to call into */
1789 VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1791 for(int i=0;i<currentrule.numQuantifiers();i++) {
1792 if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1793 parttype=parttype+", int, int";
1795 parttype=parttype+", int";
1797 cr.addDeclaration("void (*"+funptr.getSafeSymbol()+") (struct "+name+"_state *,struct "+name+"*,struct RepairHash *"+parttype+");");
1798 cr.outputline(funptr.getSafeSymbol()+"=");
1799 cr.outputline("(void (*) (struct "+name+"_state *,struct "+name+"*,struct RepairHash *"+parttype+")) RepairHashgetset("+repairtable.getSafeSymbol()+","+sd.getNum()+","+currentrule.getNum()+","+setvar+");");
1800 String methodcall="("+funptr.getSafeSymbol()+") (thisvar,"+oldmodel.getSafeSymbol()+","+
1801 repairtable.getSafeSymbol();
1802 for(int i=0;i<currentrule.numQuantifiers();i++) {
1803 Quantifier q=currentrule.getQuantifier(i);
1804 if (q instanceof SetQuantifier) {
1805 SetQuantifier sq=(SetQuantifier) q;
1806 methodcall+=","+sq.getVar().getSafeSymbol();
1807 } else if (q instanceof RelationQuantifier) {
1808 RelationQuantifier rq=(RelationQuantifier) q;
1809 methodcall+=","+rq.x.getSafeSymbol();
1810 methodcall+=","+rq.y.getSafeSymbol();
1811 } else if (q instanceof ForQuantifier) {
1812 ForQuantifier fq=(ForQuantifier) q;
1813 methodcall+=","+fq.getVar().getSafeSymbol();
1817 cr.outputline(methodcall);
1818 cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1819 cr.outputline("goto rebuild;");
1822 /* Build standard compensation actions */
1823 Vector ruleset=new Vector();
1824 ruleset.add(currentrule);
1825 if (state.implicitruleinv.containsKey(currentrule))
1826 ruleset.addAll((Set)state.implicitruleinv.get(currentrule));
1827 for(int i=0;i<ruleset.size();i++) {
1828 Rule itrule=(Rule)ruleset.get(i);
1830 if (need_compensation(itrule)) {
1831 UpdateNode un=find_compensation(itrule);
1832 String name=(String)updatenames.get(un);
1833 usedupdates.add(un); /* Mark as used */
1835 String methodcall=name+"(thisvar,"+oldmodel.getSafeSymbol()+","+
1836 repairtable.getSafeSymbol();
1837 for(int j=0;j<currentrule.numQuantifiers();j++) {
1838 Quantifier q=currentrule.getQuantifier(j);
1839 if (q instanceof SetQuantifier) {
1840 SetQuantifier sq=(SetQuantifier) q;
1841 methodcall+=","+sq.getVar().getSafeSymbol();
1842 } else if (q instanceof RelationQuantifier) {
1843 RelationQuantifier rq=(RelationQuantifier) q;
1844 methodcall+=","+rq.x.getSafeSymbol();
1845 methodcall+=","+rq.y.getSafeSymbol();
1846 } else if (q instanceof ForQuantifier) {
1847 ForQuantifier fq=(ForQuantifier) q;
1848 methodcall+=","+fq.getVar().getSafeSymbol();
1852 if (currentrule!=itrule) {
1853 SetDescriptor sdrule=((SetInclusion)itrule.getInclusion()).getSet();
1854 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1855 cr.outputline("!SimpleHashcontainskey("+ oldmodel.getSafeSymbol() +"->"+sdrule.getJustSafeSymbol() +"_hash,"+setvar+"))");
1858 cr.outputline(methodcall);
1859 cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1860 cr.outputline("goto rebuild;");
1863 if (currentrule==itrule)
1870 String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1871 cr.addDeclaration("int", addeditem);
1872 cr.outputline(addeditem + " = 0;");
1873 if (sd.getType() instanceof StructureTypeDescriptor) {
1874 cr.outputline("if (!maybe&&"+setvar+")");
1876 cr.outputline("if (!maybe)");
1877 cr.outputline(addeditem + " = SimpleHashadd("+sd.getSafeSymbol()+"_hash, (int)" + setvar + ", (int)" + setvar + ");");
1879 Vector dispatchrules = getrulelist(sd);
1881 Set toremove=new HashSet();
1882 for(int i=0;i<dispatchrules.size();i++) {
1883 Rule r=(Rule)dispatchrules.get(i);
1884 if (!ruleset.contains(r))
1887 dispatchrules.removeAll(toremove);
1889 if (dispatchrules.size() == 0) {
1890 cr.outputline("/* nothing to dispatch */");
1895 /* Add item to worklist if new */
1896 cr.outputline("if ("+addeditem+")");
1898 for(int i = 0; i < dispatchrules.size(); i++) {
1899 Rule rule = (Rule) dispatchrules.elementAt(i);
1900 if (SetDescriptor.expand(rule.getGuardExpr().getRequiredDescriptors()).contains(sd)) {
1901 /* Guard depends on this relation, so we recompute everything */
1902 cr.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+",-1,0,0);");
1904 for (int j=0;j<rule.numQuantifiers();j++) {
1905 Quantifier q=rule.getQuantifier(j);
1906 if (SetDescriptor.expand(q.getRequiredDescriptors()).contains(sd)) {
1908 cr.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+","+j+","+setvar+",0);");