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 Iterator reliterator = state.stRelations.descriptors();
642 while (reliterator.hasNext()) {
643 RelationDescriptor rd = (RelationDescriptor) reliterator.next();
646 String relname = rd.getSafeSymbol();
647 if (rd.testUsage(RelationDescriptor.IMAGE)) {
649 cr.outputline("/* printing relation " + relname+"*/");
650 cr.outputline("printf(\"\\nPrinting relation " + rd.getSymbol() + " - %d elements \\n\", SimpleHashcountset("+relname+"_hash));");
651 cr.addDeclaration("struct SimpleIterator","__reliterator");
652 cr.outputline("SimpleHashiterator("+relname+"_hash,&__reliterator);");
653 cr.outputline("while (hasNext(&__reliterator))");
655 cr.addDeclaration("int","__relval");
656 cr.addDeclaration("int","__relval2");
657 cr.outputline("__relval2 = (int) key(&__reliterator);");
658 cr.outputline("__relval = (int) next(&__reliterator);");
660 cr.outputline("printf(\"<%ld,%ld> \", __relval2,__relval);");
664 } else if (rd.testUsage(RelationDescriptor.INVIMAGE)) {
666 cr.outputline("/* printing inv relation " + relname+"*/");
667 cr.outputline("printf(\"\\nPrinting relation using inv" + rd.getSymbol() + " - %d elements \\n\", SimpleHashcountset("+relname+"_hash));");
668 cr.addDeclaration("struct SimpleIterator","__reliterator");
669 cr.outputline("SimpleHashiterator("+relname+"_hashinv,&__reliterator);");
670 cr.outputline("while (hasNext(&__reliterator))");
672 cr.addDeclaration("int","__relval");
673 cr.addDeclaration("int","__relval2");
674 cr.outputline("__relval2 = (int) key(&__reliterator);");
675 cr.outputline("__relval = (int) next(&__reliterator);");
678 cr.outputline("printf(\"<%ld,%ld> \", __relval,__relval2);");
686 cr.outputline("printf(\"\\n\\n------------------- END PRINTING\\n\");");
690 private void generate_rules() {
691 /* first we must sort the rules */
692 RelationDescriptor.prefix = newmodel.getSafeSymbol()+"->";
693 SetDescriptor.prefix = newmodel.getSafeSymbol()+"->";
694 System.out.println("SCC="+(mrd.numSCC()-1));
695 for(int sccindex=0;sccindex<mrd.numSCC();sccindex++) {
696 ruleset=mrd.getSCC(sccindex);
697 boolean needworklist=mrd.hasCycle(sccindex);
700 Iterator iterator_rs = ruleset.iterator();
701 while (iterator_rs.hasNext()) {
702 Rule rule = (Rule) iterator_rs.next();
703 if (rule.getnogenerate())
706 final SymbolTable st = rule.getSymbolTable();
707 CodeWriter cr = new StandardCodeWriter(outputaux) {
708 public SymbolTable getSymbolTable() { return st; }
710 InvariantValue ivalue=new InvariantValue();
711 cr.setInvariantValue(ivalue);
713 cr.outputline("/* build " +escape(rule.toString())+"*/");
715 cr.addDeclaration("int","maybe");
716 cr.outputline("maybe=0;");
718 Expr ruleexpr=rule.getGuardExpr();
719 HashSet invariantvars=new HashSet();
720 Set invariants=ruleexpr.findInvariants(invariantvars);
722 if ((ruleexpr instanceof BooleanLiteralExpr)&&
723 ((BooleanLiteralExpr)ruleexpr).getValue()) {
724 if (rule.getInclusion() instanceof SetInclusion) {
725 invariants.addAll(((SetInclusion)rule.getInclusion()).getExpr().findInvariants(invariantvars));
726 } else if (rule.getInclusion() instanceof RelationInclusion) {
727 invariants.addAll(((RelationInclusion)rule.getInclusion()).getLeftExpr().findInvariants(invariantvars));
728 invariants.addAll(((RelationInclusion)rule.getInclusion()).getRightExpr().findInvariants(invariantvars));
731 ListIterator quantifiers = rule.quantifiers();
732 while (quantifiers.hasNext()) {
733 Quantifier quantifier = (Quantifier) quantifiers.next();
734 if (quantifier instanceof ForQuantifier) {
735 ForQuantifier fq=(ForQuantifier)quantifier;
736 invariants.addAll(fq.lower.findInvariants(invariantvars));
737 invariants.addAll(fq.upper.findInvariants(invariantvars));
741 int openparencount=0;
742 for(Iterator invit=invariants.iterator();invit.hasNext();) {
743 Expr invexpr=(Expr)invit.next();
744 VarDescriptor tmpvd=VarDescriptor.makeNew("tmpvar");
745 VarDescriptor maybevd=VarDescriptor.makeNew("maybevar");
746 invexpr.generate(cr,tmpvd);
747 cr.addDeclaration("int ",maybevd.getSafeSymbol());
748 cr.outputline(maybevd.getSafeSymbol()+"=maybe;");
749 cr.outputline("maybe=0;");
750 ivalue.assignPair(invexpr,tmpvd,maybevd);
754 quantifiers = rule.quantifiers();
755 while (quantifiers.hasNext()) {
756 Quantifier quantifier = (Quantifier) quantifiers.next();
757 quantifier.generate_open(cr);
762 rule.getGuardExpr().prettyPrint(cr);
765 /* now we have to generate the guard test */
766 VarDescriptor guardval = VarDescriptor.makeNew();
767 rule.getGuardExpr().generate(cr, guardval);
768 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
771 /* now we have to generate the inclusion code */
773 rule.getInclusion().generate(cr);
775 while (quantifiers.hasPrevious()) {
776 Quantifier quantifier = (Quantifier) quantifiers.previous();
780 while((openparencount--)>0)
787 CodeWriter cr2 = new StandardCodeWriter(outputaux);
789 for(Iterator initialworklist=ruleset.iterator();initialworklist.hasNext();) {
790 /** Construct initial worklist set */
791 Rule rule=(Rule)initialworklist.next();
792 cr2.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+",-1,0,0);");
795 cr2.outputline("while (WorkListhasMoreElements("+worklist.getSafeSymbol()+"))");
797 VarDescriptor idvar=VarDescriptor.makeNew("id");
798 cr2.addDeclaration("int ",idvar.getSafeSymbol());
799 cr2.outputline(idvar.getSafeSymbol()+"=WorkListgetid("+worklist.getSafeSymbol()+");");
801 String elseladder = "if";
803 Iterator iterator_rules = ruleset.iterator();
804 while (iterator_rules.hasNext()) {
806 Rule rule = (Rule) iterator_rules.next();
807 int dispatchid = rule.getNum();
810 final SymbolTable st = rule.getSymbolTable();
811 CodeWriter cr = new StandardCodeWriter(outputaux) {
812 public SymbolTable getSymbolTable() { return st; }
816 cr.outputline(elseladder + " ("+idvar.getSafeSymbol()+" == " + dispatchid + ")");
818 cr.addDeclaration("int","maybe");
819 cr.outputline("maybe=0;");
820 VarDescriptor typevar=VarDescriptor.makeNew("type");
821 VarDescriptor leftvar=VarDescriptor.makeNew("left");
822 VarDescriptor rightvar=VarDescriptor.makeNew("right");
823 cr.addDeclaration("int",typevar.getSafeSymbol());
824 cr.outputline(typevar.getSafeSymbol()+"= WorkListgettype("+worklist.getSafeSymbol()+");");
825 cr.addDeclaration("int",leftvar.getSafeSymbol());
826 cr.outputline(leftvar.getSafeSymbol()+"= WorkListgetlvalue("+worklist.getSafeSymbol()+");");
827 cr.addDeclaration("int",rightvar.getSafeSymbol());
828 cr.outputline(rightvar.getSafeSymbol()+"= WorkListgetrvalue("+worklist.getSafeSymbol()+");");
829 cr.outputline("/* build " +escape(rule.toString())+"*/");
832 for (int j=0;j<rule.numQuantifiers();j++) {
833 Quantifier quantifier = rule.getQuantifier(j);
834 quantifier.generate_open(cr, typevar.getSafeSymbol(),j,leftvar.getSafeSymbol(),rightvar.getSafeSymbol());
840 rule.getGuardExpr().prettyPrint(cr);
843 /* now we have to generate the guard test */
845 VarDescriptor guardval = VarDescriptor.makeNew();
846 rule.getGuardExpr().generate(cr, guardval);
848 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
851 /* now we have to generate the inclusion code */
853 rule.getInclusion().generate(cr);
856 for (int j=0;j<rule.numQuantifiers();j++) {
860 // close startblocks generated by DotExpr memory checks
861 //DotExpr.generate_memory_endblocks(cr);
863 cr.endblock(); // end else-if WORKLIST ladder
865 elseladder = "else if";
868 cr2.outputline("else");
870 cr2.outputline("printf(\"VERY BAD !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\\n\\n\");");
871 cr2.outputline("exit(1);");
873 // end block created for worklist
874 cr2.outputline("WorkListpop("+worklist.getSafeSymbol()+");");
880 public static String escape(String s) {
882 for(int i=0;i<s.length();i++) {
892 private void generate_checks() {
893 /* do constraint checks */
896 i=termination.constraintdependence.computeOrdering().iterator();
898 i=state.vConstraints.iterator();
899 for (; i.hasNext();) {
900 Constraint constraint;
902 constraint= (Constraint) ((GraphNode)i.next()).getOwner();
904 constraint=(Constraint)i.next();
907 final SymbolTable st = constraint.getSymbolTable();
908 CodeWriter cr = new StandardCodeWriter(outputaux);
909 cr.pushSymbolTable(constraint.getSymbolTable());
911 cr.outputline("/* checking " + escape(constraint.toString())+"*/");
914 ListIterator quantifiers = constraint.quantifiers();
916 while (quantifiers.hasNext()) {
917 Quantifier quantifier = (Quantifier) quantifiers.next();
918 quantifier.generate_open(cr);
921 cr.addDeclaration("int","maybe");
922 cr.outputline("maybe = 0;");
924 /* now we have to generate the guard test */
926 VarDescriptor constraintboolean = VarDescriptor.makeNew("constraintboolean");
927 constraint.getLogicStatement().generate(cr, constraintboolean);
929 cr.outputline("if (maybe)");
931 cr.outputline("printf(\"maybe fail " + escape(constraint.toString()) + ". \\n\");");
932 //cr.outputline("exit(1);");
935 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
937 if (!Compiler.REPAIR||Compiler.GENERATEDEBUGHOOKS)
938 cr.outputline("printf(\"fail " + escape(constraint.toString()) + ". \\n\");");
940 if (Compiler.REPAIR) {
942 /* Build new repair table */
944 cr.outputline("if ("+repairtable.getSafeSymbol()+")");
945 cr.outputline("freeRepairHash("+repairtable.getSafeSymbol()+");");
946 cr.outputline(repairtable.getSafeSymbol()+"=noargallocateRepairHash();");
948 if (Compiler.GENERATEDEBUGHOOKS)
949 cr.outputline("debughook();");
950 /* Compute cost of each repair */
951 VarDescriptor mincost=VarDescriptor.makeNew("mincost");
952 VarDescriptor mincostindex=VarDescriptor.makeNew("mincostindex");
953 Vector dnfconst=new Vector();
954 dnfconst.addAll((Set)termination.conjunctionmap.get(constraint));
956 if (dnfconst.size()<=1) {
957 cr.addDeclaration("int",mincostindex.getSafeSymbol());
958 cr.outputline(mincostindex.getSafeSymbol()+"=0;");
960 if (dnfconst.size()>1) {
961 cr.addDeclaration("int",mincostindex.getSafeSymbol());
963 for(int j=0;j<dnfconst.size();j++) {
964 GraphNode gn=(GraphNode)dnfconst.get(j);
965 Conjunction conj=((TermNode)gn.getOwner()).getConjunction();
966 if (removed.contains(gn))
969 VarDescriptor costvar;
973 costvar=VarDescriptor.makeNew("cost");
974 for(int k=0;k<conj.size();k++) {
975 DNFPredicate dpred=conj.get(k);
976 Predicate p=dpred.getPredicate();
977 boolean negate=dpred.isNegated();
978 VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
979 p.generate(cr,predvalue);
981 cr.addDeclaration("int",costvar.getSafeSymbol());
982 cr.outputline(costvar.getSafeSymbol()+"=0;");
985 cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
987 cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
988 cr.outputline(costvar.getSafeSymbol()+"+="+cost.getCost(dpred)+";");
992 cr.outputline("if ("+costvar.getSafeSymbol()+"<"+mincost.getSafeSymbol()+")");
994 cr.outputline(mincost.getSafeSymbol()+"="+costvar.getSafeSymbol()+";");
995 cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
998 cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
1002 cr.outputline("switch("+mincostindex.getSafeSymbol()+")");
1004 for(int j=0;j<dnfconst.size();j++) {
1005 GraphNode gn=(GraphNode)dnfconst.get(j);
1006 Conjunction conj=((TermNode)gn.getOwner()).getConjunction();
1007 if (removed.contains(gn))
1009 cr.outputline("case "+j+":");
1011 for(int k=0;k<conj.size();k++) {
1012 DNFPredicate dpred=conj.get(k);
1013 Predicate p=dpred.getPredicate();
1014 boolean negate=dpred.isNegated();
1015 VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
1016 p.generate(cr,predvalue);
1018 cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
1020 cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
1022 if (Compiler.GENERATEINSTRUMENT)
1023 cr.outputline("abstractcount++;");
1024 if (p instanceof InclusionPredicate)
1025 generateinclusionrepair(conj,dpred, cr);
1026 else if (p instanceof ExprPredicate) {
1027 ExprPredicate ep=(ExprPredicate)p;
1028 if (ep.getType()==ExprPredicate.SIZE)
1029 generatesizerepair(conj,dpred,cr);
1030 else if (ep.getType()==ExprPredicate.COMPARISON)
1031 generatecomparisonrepair(conj,dpred,cr);
1032 } else throw new Error("Unrecognized Predicate");
1037 cr.outputline("break;");
1041 cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
1042 cr.outputline("free"+name+"("+oldmodel.getSafeSymbol()+");");
1043 cr.outputline(oldmodel.getSafeSymbol()+"="+newmodel.getSafeSymbol()+";");
1044 cr.outputline("goto rebuild;"); /* Rebuild model and all */
1048 while (quantifiers.hasPrevious()) {
1049 Quantifier quantifier = (Quantifier) quantifiers.previous();
1057 CodeWriter cr = new StandardCodeWriter(outputaux);
1059 cr.outputline("if ("+repairtable.getSafeSymbol()+")");
1060 cr.outputline("freeRepairHash("+repairtable.getSafeSymbol()+");");
1061 cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
1062 cr.outputline("free"+name+"("+oldmodel.getSafeSymbol()+");");
1063 cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1064 cr.outputline("freeWorkList("+worklist.getSafeSymbol()+");");
1065 cr.outputline("resettypemap();");
1066 cr.outputline("break;");
1068 cr.outputline("rebuild:");
1072 private MultUpdateNode getmultupdatenode(Conjunction conj, DNFPredicate dpred, int repairtype) {
1073 Set nodes=getmultupdatenodeset(conj,dpred,repairtype);
1074 Iterator it=nodes.iterator();
1076 return (MultUpdateNode)it.next();
1081 private Set getmultupdatenodeset(Conjunction conj, DNFPredicate dpred, int repairtype) {
1082 HashSet hs=new HashSet();
1083 GraphNode gn=(GraphNode) termination.conjtonodemap.get(conj);
1084 for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1085 GraphNode gn2=((GraphNode.Edge) edgeit.next()).getTarget();
1086 TermNode tn2=(TermNode)gn2.getOwner();
1087 if (tn2.getType()==TermNode.ABSTRACT) {
1088 AbstractRepair ar=tn2.getAbstract();
1089 if (((repairtype==-1)||(ar.getType()==repairtype))&&
1090 ar.getPredicate()==dpred) {
1091 for(Iterator edgeit2=gn2.edges();edgeit2.hasNext();) {
1092 GraphNode gn3=((GraphNode.Edge) edgeit2.next()).getTarget();
1093 if (!removed.contains(gn3)) {
1094 TermNode tn3=(TermNode)gn3.getOwner();
1095 if (tn3.getType()==TermNode.UPDATE) {
1096 hs.add(tn3.getUpdate());
1106 private AbstractRepair getabstractrepair(Conjunction conj, DNFPredicate dpred, int repairtype) {
1107 HashSet hs=new HashSet();
1108 MultUpdateNode mun=null;
1109 GraphNode gn=(GraphNode) termination.conjtonodemap.get(conj);
1110 for(Iterator edgeit=gn.edges();(mun==null)&&edgeit.hasNext();) {
1111 GraphNode gn2=((GraphNode.Edge) edgeit.next()).getTarget();
1112 TermNode tn2=(TermNode)gn2.getOwner();
1113 if (tn2.getType()==TermNode.ABSTRACT) {
1114 AbstractRepair ar=tn2.getAbstract();
1115 if (((repairtype==-1)||(ar.getType()==repairtype))&&
1116 ar.getPredicate()==dpred) {
1125 /** Generates abstract (and concrete) repair for a comparison */
1127 private void generatecomparisonrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
1128 Set updates=getmultupdatenodeset(conj,dpred,AbstractRepair.MODIFYRELATION);
1129 AbstractRepair ar=getabstractrepair(conj,dpred,AbstractRepair.MODIFYRELATION);
1130 MultUpdateNode munmodify=null;
1131 MultUpdateNode munadd=null;
1132 MultUpdateNode munremove=null;
1133 for(Iterator it=updates.iterator();it.hasNext();) {
1134 MultUpdateNode mun=(MultUpdateNode)it.next();
1135 if (mun.getType()==MultUpdateNode.ADD) {
1137 } else if (mun.getType()==MultUpdateNode.REMOVE) {
1139 } else if (mun.getType()==MultUpdateNode.MODIFY) {
1144 ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
1145 RelationDescriptor rd=(RelationDescriptor)ep.getDescriptor();
1146 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1147 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1148 boolean inverted=ep.inverted();
1149 boolean negated=dpred.isNegated();
1150 OpExpr expr=(OpExpr)ep.expr;
1151 Opcode opcode=expr.getOpcode();
1152 VarDescriptor leftside=VarDescriptor.makeNew("leftside");
1153 VarDescriptor rightside=VarDescriptor.makeNew("rightside");
1154 VarDescriptor newvalue=VarDescriptor.makeNew("newvalue");
1155 boolean needremoveloop=ar.mayNeedFunctionEnforcement(state)&&ar.needsRemoves(state);
1157 if (needremoveloop&&((munadd==null)||(munremove==null))) {
1158 System.out.println("Warning: need to have individual remove operations for"+dpred.name());
1159 needremoveloop=false;
1161 if (needremoveloop) {
1162 cr.outputline("while (1)");
1166 ((RelationExpr)expr.getLeftExpr()).getExpr().generate(cr,leftside);
1167 expr.getRightExpr().generate(cr,newvalue);
1168 cr.addDeclaration(rd.getRange().getType().getGenerateType().getSafeSymbol(),rightside.getSafeSymbol());
1169 cr.outputline("SimpleHashget("+rd.getSafeSymbol()+"_hash,"+leftside.getSafeSymbol()+", &"+rightside.getSafeSymbol()+");");
1171 ((RelationExpr)expr.getLeftExpr()).getExpr().generate(cr,rightside);
1172 expr.getRightExpr().generate(cr,newvalue);
1173 cr.outputline(rd.getDomain().getType().getGenerateType().getSafeSymbol()+" "+leftside.getSafeSymbol()+";");
1174 cr.outputline("SimpleHashget("+rd.getSafeSymbol()+"_hashinv,"+rightside.getSafeSymbol()+", &"+leftside.getSafeSymbol()+");");
1177 opcode=Opcode.translateOpcode(negated,opcode);
1179 if (opcode==Opcode.GT) {
1180 cr.outputline(newvalue.getSafeSymbol()+"++;");
1181 } else if (opcode==Opcode.GE) {
1183 } else if (opcode==Opcode.LT) {
1184 cr.outputline(newvalue.getSafeSymbol()+"--;");
1185 } else if (opcode==Opcode.LE) {
1187 } else if (opcode==Opcode.EQ) {
1189 } else if (opcode==Opcode.NE) { /* search for FLAGNE if this is changed*/
1190 cr.outputline(newvalue.getSafeSymbol()+"++;");
1192 throw new Error("Unrecognized Opcode");
1194 /* Do abstract repairs */
1196 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash, "+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1198 if (usageinvimage) {
1199 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv, "+rightside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
1202 if (needremoveloop) {
1204 cr.outputline("if (SimpleHashcontainskey("+rd.getSafeSymbol()+"_hash, "+leftside.getSafeSymbol()+"))");
1207 cr.outputline("if (SimpleHashcontainskey("+rd.getSafeSymbol()+"_hashinv, "+rightside.getSafeSymbol()+"))");
1210 for(int i=0;i<state.vRules.size();i++) {
1211 Rule r=(Rule)state.vRules.get(i);
1212 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1213 for(int j=0;j<munremove.numUpdates();j++) {
1214 UpdateNode un=munremove.getUpdate(i);
1215 if (un.getRule()==r) {
1216 /* Update for rule r */
1217 String name=(String)updatenames.get(un);
1218 cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+");");
1223 cr.outputline("continue;");
1229 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash,"+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1231 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, "+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1234 if (usageinvimage) {
1236 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, "+newvalue.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
1238 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv,"+rightside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1241 /* Do concrete repairs */
1242 if (munmodify!=null&&(!ar.mayNeedFunctionEnforcement(state))||(munadd==null)||(ar.needsRemoves(state)&&(munremove==null))) {
1243 for(int i=0;i<state.vRules.size();i++) {
1244 Rule r=(Rule)state.vRules.get(i);
1245 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1246 for(int j=0;j<munmodify.numUpdates();j++) {
1247 UpdateNode un=munmodify.getUpdate(j);
1248 if (un.getRule()==r) {
1249 /* Update for rule r */
1250 String name=(String)updatenames.get(un);
1251 cr.outputline("RepairHashaddrelation2("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+","+newvalue.getSafeSymbol()+");");
1257 /* Start with scheduling removal */
1258 if (ar.needsRemoves(state))
1259 for(int i=0;i<state.vRules.size();i++) {
1260 Rule r=(Rule)state.vRules.get(i);
1261 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1262 for(int j=0;j<munremove.numUpdates();j++) {
1263 UpdateNode un=munremove.getUpdate(i);
1264 if (un.getRule()==r) {
1265 /* Update for rule r */
1266 String name=(String)updatenames.get(un);
1267 cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+");");
1272 /* Now do addition */
1273 UpdateNode un=munadd.getUpdate(0);
1274 String name=(String)updatenames.get(un);
1276 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1278 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1281 if (needremoveloop) {
1282 cr.outputline("break;");
1287 public void generatesizerepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr) {
1288 ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
1289 OpExpr expr=(OpExpr)ep.expr;
1290 Opcode opcode=expr.getOpcode();
1291 opcode=Opcode.translateOpcode(dpred.isNegated(),opcode);
1293 MultUpdateNode munremove;
1295 MultUpdateNode munadd;
1296 if (ep.getDescriptor() instanceof RelationDescriptor) {
1297 munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMRELATION);
1298 munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTORELATION);
1300 munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMSET);
1301 munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTOSET);
1303 int size=ep.rightSize();
1304 VarDescriptor sizevar=VarDescriptor.makeNew("size");
1305 ((OpExpr)expr).left.generate(cr, sizevar);
1306 VarDescriptor change=VarDescriptor.makeNew("change");
1307 cr.addDeclaration("int",change.getSafeSymbol());
1308 boolean generateadd=false;
1309 boolean generateremove=false;
1310 if (opcode==Opcode.GT) {
1311 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
1313 generateremove=false;
1314 } else if (opcode==Opcode.GE) {
1315 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1317 generateremove=false;
1318 } else if (opcode==Opcode.LT) {
1319 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size-1)+"-"+sizevar.getSafeSymbol()+";");
1321 generateremove=true;
1322 } else if (opcode==Opcode.LE) {
1323 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1325 generateremove=true;
1326 } else if (opcode==Opcode.EQ) {
1327 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1332 generateremove=true;
1333 } else if (opcode==Opcode.NE) {
1334 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
1336 generateremove=false;
1338 throw new Error("Unrecognized Opcode");
1341 // In some cases the analysis has determined that generating removes
1343 if (generateremove&&munremove==null)
1344 generateremove=false;
1346 Descriptor d=ep.getDescriptor();
1347 if (generateremove) {
1348 cr.outputline("for(;"+change.getSafeSymbol()+"<0;"+change.getSafeSymbol()+"++)");
1350 /* Find element to remove */
1351 VarDescriptor leftvar=VarDescriptor.makeNew("leftvar");
1352 VarDescriptor rightvar=VarDescriptor.makeNew("rightvar");
1353 if (d instanceof RelationDescriptor) {
1354 if (ep.inverted()) {
1355 ((ImageSetExpr)((SizeofExpr)expr.left).setexpr).generate_leftside(cr,rightvar);
1356 cr.addDeclaration("int",leftvar.getSafeSymbol());
1357 cr.outputline("SimpleHashget("+d.getSafeSymbol()+"_hashinv,(int)"+rightvar.getSafeSymbol()+", &"+leftvar.getSafeSymbol()+");");
1359 ((ImageSetExpr)((SizeofExpr)expr.left).setexpr).generate_leftside(cr,leftvar);
1360 cr.addDeclaration("int",rightvar.getSafeSymbol());
1361 cr.outputline("SimpleHashget("+d.getSafeSymbol()+"_hash ,(int)"+leftvar.getSafeSymbol()+", &"+rightvar.getSafeSymbol()+");");
1364 cr.addDeclaration("int",leftvar.getSafeSymbol());
1365 cr.outputline(leftvar.getSafeSymbol()+"= SimpleHashfirstkey("+d.getSafeSymbol()+"_hash);");
1367 /* Generate abstract remove instruction */
1368 if (d instanceof RelationDescriptor) {
1369 RelationDescriptor rd=(RelationDescriptor) d;
1370 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1371 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1373 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1375 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv ,(int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1377 cr.outputline("SimpleHashremove("+d.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1379 /* Generate concrete remove instruction */
1380 for(int i=0;i<state.vRules.size();i++) {
1381 Rule r=(Rule)state.vRules.get(i);
1382 if (r.getInclusion().getTargetDescriptors().contains(d)) {
1383 for(int j=0;j<munremove.numUpdates();j++) {
1384 UpdateNode un=munremove.getUpdate(j);
1385 if (un.getRule()==r) {
1386 /* Update for rule rule r */
1387 String name=(String)updatenames.get(un);
1388 if (d instanceof RelationDescriptor) {
1389 cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
1391 cr.outputline("RepairHashaddset("+repairtable.getSafeSymbol()+","+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1400 // In some cases the analysis has determined that generating removes
1402 if (generateadd&&munadd==null)
1407 cr.outputline("for(;"+change.getSafeSymbol()+">0;"+change.getSafeSymbol()+"--)");
1409 VarDescriptor newobject=VarDescriptor.makeNew("newobject");
1410 if (d instanceof RelationDescriptor) {
1411 VarDescriptor otherside=VarDescriptor.makeNew("otherside");
1412 ((ImageSetExpr)((SizeofExpr)expr.left).setexpr).generate_leftside(cr,otherside);
1414 RelationDescriptor rd=(RelationDescriptor)d;
1415 if (termination.sources.relsetSource(rd,!ep.inverted())) {
1417 SetDescriptor sd=termination.sources.relgetSourceSet(rd,!ep.inverted());
1418 VarDescriptor iterator=VarDescriptor.makeNew("iterator");
1419 cr.addDeclaration(sd.getType().getGenerateType().getSafeSymbol(),newobject.getSafeSymbol());
1420 cr.addDeclaration("struct SimpleIterator",iterator.getSafeSymbol());
1421 cr.outputline("for(SimpleHashiterator("+sd.getSafeSymbol()+"_hash , &"+ iterator.getSafeSymbol() +"); hasNext(&"+iterator.getSafeSymbol()+");)");
1423 if (ep.inverted()) {
1424 cr.outputline("if (!SimpleHashcontainskeydata("+rd.getSafeSymbol()+"_hashinv,"+iterator.getSafeSymbol()+".key(),"+otherside.getSafeSymbol()+"))");
1426 cr.outputline("if (!SimpleHashcontainskeydata("+rd.getSafeSymbol()+"_hash, "+otherside.getSafeSymbol()+","+iterator.getSafeSymbol()+".key()))");
1428 cr.outputline(newobject.getSafeSymbol()+"=key(&"+iterator.getSafeSymbol()+");");
1429 cr.outputline("next(&"+iterator.getSafeSymbol()+");");
1431 } else if (termination.sources.relallocSource(rd,!ep.inverted())) {
1432 /* Allocation Source*/
1433 termination.sources.relgenerateSourceAlloc(cr,newobject,rd,!ep.inverted());
1434 } else throw new Error("No source for adding to Relation");
1435 if (ep.inverted()) {
1436 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1437 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1439 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, "+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1441 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, "+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1443 UpdateNode un=munadd.getUpdate(0);
1444 String name=(String)updatenames.get(un);
1445 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1447 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1448 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1450 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, "+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1452 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, "+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1453 UpdateNode un=munadd.getUpdate(0);
1454 String name=(String)updatenames.get(un);
1455 cr.outputline(name+"(thisvar, "+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1458 SetDescriptor sd=(SetDescriptor)d;
1459 if (termination.sources.setSource(sd)) {
1462 SetDescriptor sourcesd=termination.sources.getSourceSet(sd);
1463 VarDescriptor iterator=VarDescriptor.makeNew("iterator");
1464 cr.addDeclaration(sourcesd.getType().getGenerateType().getSafeSymbol(), newobject.getSafeSymbol());
1465 cr.addDeclaration("struct SimpleIterator",iterator.getSafeSymbol());
1466 cr.outputline("for(SimpleHashiterator("+sourcesd.getSafeSymbol()+"_hash, &"+iterator.getSafeSymbol()+"); hasNext(&"+iterator.getSafeSymbol()+");)");
1468 cr.outputline("if (!SimpleHashcontainskey("+sd.getSafeSymbol()+"_hash, key(&"+iterator.getSafeSymbol()+")))");
1469 cr.outputline(newobject.getSafeSymbol()+"=key(&"+iterator.getSafeSymbol()+");");
1470 cr.outputline("next(&"+iterator.getSafeSymbol()+");");
1472 } else if (termination.sources.allocSource(sd)) {
1473 /* Allocation Source*/
1474 termination.sources.generateSourceAlloc(cr,newobject,sd);
1475 } else throw new Error("No source for adding to Set");
1476 cr.outputline("SimpleHashadd("+sd.getSafeSymbol()+"_hash, "+newobject.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1477 UpdateNode un=munadd.getUpdate(0);
1478 String name=(String)updatenames.get(un);
1479 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1485 public void generateinclusionrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
1486 InclusionPredicate ip=(InclusionPredicate) dpred.getPredicate();
1487 boolean negated=dpred.isNegated();
1488 MultUpdateNode mun=getmultupdatenode(conj,dpred,-1);
1489 VarDescriptor leftvar=VarDescriptor.makeNew("left");
1490 ip.expr.generate(cr, leftvar);
1493 if (ip.setexpr instanceof ImageSetExpr) {
1494 ImageSetExpr ise=(ImageSetExpr) ip.setexpr;
1495 VarDescriptor rightvar=ise.getVar();
1496 boolean inverse=ise.inverted();
1497 RelationDescriptor rd=ise.getRelation();
1498 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1499 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1502 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1504 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1507 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash ,(int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1509 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1511 for(int i=0;i<state.vRules.size();i++) {
1512 Rule r=(Rule)state.vRules.get(i);
1513 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1514 for(int j=0;j<mun.numUpdates();j++) {
1515 UpdateNode un=mun.getUpdate(j);
1516 if (un.getRule()==r) {
1517 /* Update for rule rule r */
1518 String name=(String)updatenames.get(un);
1520 cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1522 cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
1529 SetDescriptor sd=ip.setexpr.sd;
1530 cr.outputline("SimpleHashremove("+sd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1532 for(int i=0;i<state.vRules.size();i++) {
1533 Rule r=(Rule)state.vRules.get(i);
1534 if (r.getInclusion().getTargetDescriptors().contains(sd)) {
1535 for(int j=0;j<mun.numUpdates();j++) {
1536 UpdateNode un=mun.getUpdate(j);
1537 if (un.getRule()==r) {
1538 /* Update for rule rule r */
1539 String name=(String)updatenames.get(un);
1540 cr.outputline("RepairHashaddset("+repairtable.getSafeSymbol()+","+sd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1547 /* Generate update */
1548 if (ip.setexpr instanceof ImageSetExpr) {
1549 ImageSetExpr ise=(ImageSetExpr) ip.setexpr;
1550 VarDescriptor rightvar=ise.getVar();
1551 boolean inverse=ise.inverted();
1552 RelationDescriptor rd=ise.getRelation();
1553 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1554 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1557 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1559 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1562 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1564 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1566 UpdateNode un=mun.getUpdate(0);
1567 String name=(String)updatenames.get(un);
1569 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1571 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+");");
1574 SetDescriptor sd=ip.setexpr.sd;
1575 cr.outputline("SimpleHashadd("+sd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1577 UpdateNode un=mun.getUpdate(0);
1578 /* Update for rule rule r */
1579 String name=(String)updatenames.get(un);
1580 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1585 public static Vector getrulelist(Descriptor d) {
1586 Vector dispatchrules = new Vector();
1587 Vector rules = State.currentState.vRules;
1589 for (int i = 0; i < rules.size(); i++) {
1590 Rule rule = (Rule) rules.elementAt(i);
1591 Set requiredsymbols = rule.getRequiredDescriptors();
1593 // #TBD#: in general this is wrong because these descriptors may contain descriptors
1594 // bound in "in?" expressions which need to be dealt with in a topologically sorted
1597 if (rule.getRequiredDescriptors().contains(d)) {
1598 dispatchrules.addElement(rule);
1601 return dispatchrules;
1604 private boolean need_compensation(Rule r) {
1605 if (!Compiler.REPAIR)
1607 GraphNode gn=(GraphNode)termination.scopefalsify.get(r);
1608 for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1609 GraphNode.Edge edge=(GraphNode.Edge)edgeit.next();
1610 GraphNode gn2=edge.getTarget();
1611 if (!removed.contains(gn2)) {
1612 TermNode tn2=(TermNode)gn2.getOwner();
1613 if (tn2.getType()==TermNode.CONSEQUENCE)
1620 private UpdateNode find_compensation(Rule r) {
1621 GraphNode gn=(GraphNode)termination.scopefalsify.get(r);
1622 for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1623 GraphNode.Edge edge=(GraphNode.Edge)edgeit.next();
1624 GraphNode gn2=edge.getTarget();
1625 if (!removed.contains(gn2)) {
1626 TermNode tn2=(TermNode)gn2.getOwner();
1627 if (tn2.getType()==TermNode.UPDATE) {
1628 MultUpdateNode mun=tn2.getUpdate();
1629 for(int i=0;i<mun.numUpdates();i++) {
1630 UpdateNode un=mun.getUpdate(i);
1631 if (un.getRule()==r)
1637 throw new Error("No Compensation Update could be found");
1640 public void generate_dispatch(CodeWriter cr, RelationDescriptor rd, String leftvar, String rightvar) {
1641 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1642 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1644 if (!(usageinvimage||usageimage)) /* not used at all*/
1647 cr.outputline("/* RELATION DISPATCH */");
1648 if (Compiler.REPAIR) {
1649 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1651 cr.outputline("!SimpleHashcontainskeydata("+oldmodel.getSafeSymbol()+"->"+rd.getJustSafeSymbol() + "_hash, "+leftvar+","+rightvar+"))");
1653 cr.outputline("!SimpleHashcontainskeydata("+oldmodel.getSafeSymbol() +"->"+rd.getJustSafeSymbol()+"_hashinv, "+rightvar+","+leftvar+"))");
1656 /* Adding new item */
1657 /* Perform safety checks */
1658 cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
1659 cr.outputline("RepairHashcontainsrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+"))");
1661 /* Have update to call into */
1662 VarDescriptor mdfyptr=VarDescriptor.makeNew("modifyptr");
1663 VarDescriptor ismdfyptr=VarDescriptor.makeNew("ismodifyptr");
1664 cr.addDeclaration("int ",ismdfyptr.getSafeSymbol());
1665 cr.outputline(ismdfyptr.getSafeSymbol()+"=RepairHashismodify("+repairtable.getSafeSymbol()+","+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1671 for(int i=0;i<currentrule.numQuantifiers();i++) {
1672 if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1673 parttype=parttype+", int, int";
1675 parttype=parttype+", int";
1678 VarDescriptor tmpptr=VarDescriptor.makeNew("tempupdateptr");
1680 String methodcall="(thisvar,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
1681 for(int i=0;i<currentrule.numQuantifiers();i++) {
1682 Quantifier q=currentrule.getQuantifier(i);
1683 if (q instanceof SetQuantifier) {
1684 SetQuantifier sq=(SetQuantifier) q;
1685 methodcall+=","+sq.getVar().getSafeSymbol();
1686 } else if (q instanceof RelationQuantifier) {
1687 RelationQuantifier rq=(RelationQuantifier) q;
1688 methodcall+=","+rq.x.getSafeSymbol();
1689 methodcall+=","+rq.y.getSafeSymbol();
1690 } else if (q instanceof ForQuantifier) {
1691 ForQuantifier fq=(ForQuantifier) q;
1692 methodcall+=","+fq.getVar().getSafeSymbol();
1698 cr.addDeclaration("void *",tmpptr.getSafeSymbol());
1699 cr.outputline(tmpptr.getSafeSymbol()+"=");
1700 cr.outputline("(void *) RepairHashgetrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1701 cr.outputline("if ("+ismdfyptr.getSafeSymbol()+")");
1703 VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1704 String methodcallprefix="("+funptr.getSafeSymbol()+") ";
1706 cr.addDeclaration("int",mdfyptr.getSafeSymbol());
1707 cr.outputline(mdfyptr.getSafeSymbol()+"=RepairHashgetrelation2("+repairtable.getSafeSymbol()+","+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1708 cr.addDeclaration("void (*"+funptr.getSafeSymbol()+") (struct "+name+"_state *, struct "+name+"*, struct RepairHash *"+parttype+",int,int,int);");
1709 cr.outputline(funptr.getSafeSymbol()+"="+"(void (*) (struct "+name+"_state *, struct "+name+"*, struct RepairHash *"+parttype+",int,int,int)) "+tmpptr.getSafeSymbol()+";");
1710 cr.outputline(methodcallprefix+methodcall+","+leftvar+", "+rightvar+", "+mdfyptr.getSafeSymbol() +");");
1713 cr.outputline("else ");
1715 VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1716 String methodcallprefix="("+funptr.getSafeSymbol()+") ";
1718 cr.addDeclaration("void (*"+funptr.getSafeSymbol()+") (struct "+name+"_state *, struct "+name+"*,struct RepairHash *"+parttype+");");
1719 cr.outputline(funptr.getSafeSymbol()+"="+"(void (*) (struct "+name+"_state *,struct "+name+"*,struct RepairHash *"+parttype+")) "+tmpptr.getSafeSymbol()+";");
1720 cr.outputline(methodcallprefix+methodcall+");");
1723 cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1724 cr.outputline("goto rebuild;");
1728 /* Build standard compensation actions */
1729 if (need_compensation(currentrule)) {
1730 UpdateNode un=find_compensation(currentrule);
1731 String name=(String)updatenames.get(un);
1732 usedupdates.add(un); /* Mark as used */
1733 String methodcall=name+"(thisvar,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
1734 for(int i=0;i<currentrule.numQuantifiers();i++) {
1735 Quantifier q=currentrule.getQuantifier(i);
1736 if (q instanceof SetQuantifier) {
1737 SetQuantifier sq=(SetQuantifier) q;
1738 methodcall+=","+sq.getVar().getSafeSymbol();
1739 } else if (q instanceof RelationQuantifier) {
1740 RelationQuantifier rq=(RelationQuantifier) q;
1741 methodcall+=","+rq.x.getSafeSymbol();
1742 methodcall+=","+rq.y.getSafeSymbol();
1743 } else if (q instanceof ForQuantifier) {
1744 ForQuantifier fq=(ForQuantifier) q;
1745 methodcall+=","+fq.getVar().getSafeSymbol();
1749 cr.outputline(methodcall);
1750 cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1751 cr.outputline("goto rebuild;");
1757 String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1759 cr.addDeclaration("int" , addeditem);
1760 cr.outputline(addeditem + "=0;");
1762 String ifstring="if (!maybe";
1763 if (rd.getDomain().getType() instanceof StructureTypeDescriptor) {
1764 ifstring+="&&"+leftvar;
1767 if (rd.getRange().getType() instanceof StructureTypeDescriptor) {
1768 ifstring+="&&"+rightvar;
1773 if (rd.testUsage(RelationDescriptor.IMAGE)) {
1774 cr.outputline(ifstring);
1775 cr.outputline(addeditem + " = SimpleHashadd("+rd.getSafeSymbol()+"_hash, (int)" + leftvar + ", (int)" + rightvar+ ");");
1778 if (rd.testUsage(RelationDescriptor.INVIMAGE)) {
1779 cr.outputline(ifstring);
1780 cr.outputline(addeditem + " = SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, (int)" + rightvar + ", (int)" + leftvar + ");");
1784 Vector dispatchrules = getrulelist(rd);
1786 Set toremove=new HashSet();
1787 for(int i=0;i<dispatchrules.size();i++) {
1788 Rule r=(Rule)dispatchrules.get(i);
1789 if (!ruleset.contains(r))
1792 dispatchrules.removeAll(toremove);
1793 if (dispatchrules.size() == 0) {
1794 cr.outputline("/* nothing to dispatch */");
1799 cr.outputline("if (" + addeditem + ")");
1802 for(int i = 0; i < dispatchrules.size(); i++) {
1803 Rule rule = (Rule) dispatchrules.elementAt(i);
1804 if (rule.getGuardExpr().getRequiredDescriptors().contains(rd)) {
1805 /* Guard depends on this relation, so we recomput everything */
1806 cr.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+",-1,0,0);");
1808 for (int j=0;j<rule.numQuantifiers();j++) {
1809 Quantifier q=rule.getQuantifier(j);
1810 if (q.getRequiredDescriptors().contains(rd)) {
1812 cr.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+","+j+","+leftvar+","+rightvar+");");
1822 public void generate_dispatch(CodeWriter cr, SetDescriptor sd, String setvar) {
1823 cr.outputline("/* SET DISPATCH */");
1824 if (Compiler.REPAIR) {
1825 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1826 cr.outputline("!SimpleHashcontainskey("+oldmodel.getSafeSymbol() +"->"+sd.getJustSafeSymbol()+"_hash, "+setvar+"))");
1828 /* Adding new item */
1829 /* See if there is an outstanding update in the repairtable */
1830 cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
1831 cr.outputline("RepairHashcontainsset("+repairtable.getSafeSymbol()+","+sd.getNum()+","+currentrule.getNum()+","+setvar+"))");
1833 /* Have update to call into */
1834 VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1836 for(int i=0;i<currentrule.numQuantifiers();i++) {
1837 if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1838 parttype=parttype+", int, int";
1840 parttype=parttype+", int";
1842 cr.addDeclaration("void (*"+funptr.getSafeSymbol()+") (struct "+name+"_state *,struct "+name+"*,struct RepairHash *"+parttype+");");
1843 cr.outputline(funptr.getSafeSymbol()+"=");
1844 cr.outputline("(void (*) (struct "+name+"_state *,struct "+name+"*,struct RepairHash *"+parttype+")) RepairHashgetset("+repairtable.getSafeSymbol()+","+sd.getNum()+","+currentrule.getNum()+","+setvar+");");
1845 String methodcall="("+funptr.getSafeSymbol()+") (thisvar,"+oldmodel.getSafeSymbol()+","+
1846 repairtable.getSafeSymbol();
1847 for(int i=0;i<currentrule.numQuantifiers();i++) {
1848 Quantifier q=currentrule.getQuantifier(i);
1849 if (q instanceof SetQuantifier) {
1850 SetQuantifier sq=(SetQuantifier) q;
1851 methodcall+=","+sq.getVar().getSafeSymbol();
1852 } else if (q instanceof RelationQuantifier) {
1853 RelationQuantifier rq=(RelationQuantifier) q;
1854 methodcall+=","+rq.x.getSafeSymbol();
1855 methodcall+=","+rq.y.getSafeSymbol();
1856 } else if (q instanceof ForQuantifier) {
1857 ForQuantifier fq=(ForQuantifier) q;
1858 methodcall+=","+fq.getVar().getSafeSymbol();
1862 cr.outputline(methodcall);
1863 cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1864 cr.outputline("goto rebuild;");
1867 /* Build standard compensation actions */
1868 Vector ruleset=new Vector();
1869 ruleset.add(currentrule);
1870 if (state.implicitruleinv.containsKey(currentrule))
1871 ruleset.addAll((Set)state.implicitruleinv.get(currentrule));
1872 for(int i=0;i<ruleset.size();i++) {
1873 Rule itrule=(Rule)ruleset.get(i);
1875 if (need_compensation(itrule)) {
1876 UpdateNode un=find_compensation(itrule);
1877 String name=(String)updatenames.get(un);
1878 usedupdates.add(un); /* Mark as used */
1880 String methodcall=name+"(thisvar,"+oldmodel.getSafeSymbol()+","+
1881 repairtable.getSafeSymbol();
1882 for(int j=0;j<currentrule.numQuantifiers();j++) {
1883 Quantifier q=currentrule.getQuantifier(j);
1884 if (q instanceof SetQuantifier) {
1885 SetQuantifier sq=(SetQuantifier) q;
1886 methodcall+=","+sq.getVar().getSafeSymbol();
1887 } else if (q instanceof RelationQuantifier) {
1888 RelationQuantifier rq=(RelationQuantifier) q;
1889 methodcall+=","+rq.x.getSafeSymbol();
1890 methodcall+=","+rq.y.getSafeSymbol();
1891 } else if (q instanceof ForQuantifier) {
1892 ForQuantifier fq=(ForQuantifier) q;
1893 methodcall+=","+fq.getVar().getSafeSymbol();
1897 if (currentrule!=itrule) {
1898 SetDescriptor sdrule=((SetInclusion)itrule.getInclusion()).getSet();
1899 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1900 cr.outputline("!SimpleHashcontainskey("+ oldmodel.getSafeSymbol() +"->"+sdrule.getJustSafeSymbol() +"_hash,"+setvar+"))");
1903 cr.outputline(methodcall);
1904 cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1905 cr.outputline("goto rebuild;");
1908 if (currentrule==itrule)
1915 String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1916 cr.addDeclaration("int", addeditem);
1917 cr.outputline(addeditem + " = 0;");
1918 if (sd.getType() instanceof StructureTypeDescriptor) {
1919 cr.outputline("if (!maybe&&"+setvar+")");
1921 cr.outputline("if (!maybe)");
1922 cr.outputline(addeditem + " = SimpleHashadd("+sd.getSafeSymbol()+"_hash, (int)" + setvar + ", (int)" + setvar + ");");
1924 Vector dispatchrules = getrulelist(sd);
1926 Set toremove=new HashSet();
1927 for(int i=0;i<dispatchrules.size();i++) {
1928 Rule r=(Rule)dispatchrules.get(i);
1929 if (!ruleset.contains(r))
1932 dispatchrules.removeAll(toremove);
1934 if (dispatchrules.size() == 0) {
1935 cr.outputline("/* nothing to dispatch */");
1940 /* Add item to worklist if new */
1941 cr.outputline("if ("+addeditem+")");
1943 for(int i = 0; i < dispatchrules.size(); i++) {
1944 Rule rule = (Rule) dispatchrules.elementAt(i);
1945 if (SetDescriptor.expand(rule.getGuardExpr().getRequiredDescriptors()).contains(sd)) {
1946 /* Guard depends on this relation, so we recompute everything */
1947 cr.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+",-1,0,0);");
1949 for (int j=0;j<rule.numQuantifiers();j++) {
1950 Quantifier q=rule.getQuantifier(j);
1951 if (SetDescriptor.expand(q.getRequiredDescriptors()).contains(sd)) {
1953 cr.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+","+j+","+setvar+",0);");