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;
61 private void name_updates() {
63 for(Iterator it=termination.updatenodes.iterator();it.hasNext();) {
64 GraphNode gn=(GraphNode) it.next();
65 TermNode tn=(TermNode) gn.getOwner();
66 MultUpdateNode mun=tn.getUpdate();
67 if (togenerate.contains(gn))
68 for (int i=0;i<mun.numUpdates();i++) {
69 UpdateNode un=mun.getUpdate(i);
70 String name="update"+String.valueOf(count++);
71 updatenames.put(un,name);
76 public void generate(OutputStream outputrepair, OutputStream outputaux,OutputStream outputhead, String st) {
77 this.outputrepair = new PrintWrapper(new java.io.PrintWriter(outputrepair, true));
78 this.outputaux = new PrintWrapper(new java.io.PrintWriter(outputaux, true));
79 this.outputhead = new PrintWrapper(new java.io.PrintWriter(outputhead, true));
83 generatetypechecks(true);
84 generate_tokentable();
85 RelationDescriptor.prefix = "thisvar->";
86 SetDescriptor.prefix = "thisvar->";
88 generate_hashtables();
89 generate_stateobject();
93 CodeWriter craux = new StandardCodeWriter(this.outputaux);
94 for (Iterator it=this.state.stGlobals.descriptors();it.hasNext();) {
95 VarDescriptor vd=(VarDescriptor)it.next();
96 craux.outputline("#define "+vd.getSafeSymbol()+" thisvar->"+vd.getSafeSymbol());
103 if (!Compiler.REPAIR||Compiler.GENERATEDEBUGPRINT) {
108 CodeWriter crhead = new StandardCodeWriter(this.outputhead);
109 craux = new StandardCodeWriter(this.outputaux);
113 if (Compiler.GENERATEDEBUGHOOKS) {
114 crhead.outputline("void debughook();");
115 craux.outputline("void debughook() {}");
117 generatetypechecks(false);
118 generate_computesizes();
119 generatetypechecks(true);
120 generate_recomputesizes();
121 generatetypechecks(false);
123 StructureGenerator sg=new StructureGenerator(state,this);
125 crhead.outputline("#endif");
128 String ststate="state";
129 String stmodel="model";
130 String strepairtable="repairtable";
131 String stleft="left";
132 String stright="right";
133 String stnew="newvalue";
135 private void generate_updates() {
137 CodeWriter crhead = new StandardCodeWriter(outputhead);
138 CodeWriter craux = new StandardCodeWriter(outputaux);
139 RelationDescriptor.prefix = "model->";
140 SetDescriptor.prefix = "model->";
142 /* Rewrite globals */
144 for (Iterator it=this.state.stGlobals.descriptors();it.hasNext();) {
145 VarDescriptor vd=(VarDescriptor)it.next();
146 craux.outputline("#undef "+vd.getSafeSymbol());
147 craux.outputline("#define "+vd.getSafeSymbol()+" "+ststate+"->"+vd.getSafeSymbol());
150 for(Iterator it=termination.updatenodes.iterator();it.hasNext();) {
151 GraphNode gn=(GraphNode) it.next();
152 TermNode tn=(TermNode) gn.getOwner();
153 MultUpdateNode mun=tn.getUpdate();
154 boolean isrelation=(mun.getDescriptor() instanceof RelationDescriptor);
155 if (togenerate.contains(gn))
156 for (int i=0;i<mun.numUpdates();i++) {
157 UpdateNode un=mun.getUpdate(i);
158 String methodname=(String)updatenames.get(un);
161 case MultUpdateNode.ADD:
163 crhead.outputline("void "+methodname+"(struct "+name+"_state * " +ststate+",struct "+name+" * "+stmodel+", struct RepairHash * "+strepairtable+", int "+stleft+", int "+stright+");");
164 craux.outputline("void "+methodname+"(struct "+name+"_state * "+ ststate+", struct "+name+" * "+stmodel+", struct RepairHash * "+strepairtable+", int "+stleft+", int "+stright+")");
166 crhead.outputline("void "+methodname+"(struct "+name+"_state * "+ ststate+", struct "+name+" * "+stmodel+", struct RepairHash * "+strepairtable+", int "+stleft+");");
167 craux.outputline("void "+methodname+"(struct "+name+"_state * "+ststate+", struct "+name+" * "+stmodel+", struct RepairHash * "+strepairtable+", int "+stleft+")");
171 craux.addDeclaration("int","maybe");
172 craux.outputline("maybe=0;");
173 if (Compiler.GENERATEINSTRUMENT)
174 craux.outputline("updatecount++;");
176 final SymbolTable st = un.getRule().getSymbolTable();
177 CodeWriter cr = new StandardCodeWriter(outputaux) {
178 public SymbolTable getSymbolTable() { return st; }
180 un.generate(cr, false, false, stleft,stright, null,this);
181 craux.outputline("if (maybe) printf(\"REALLY BAD\");");
185 case MultUpdateNode.REMOVE: {
187 String methodcall="void "+methodname+"(struct "+name+"_state * "+ststate+", struct "+name+" * "+stmodel+", struct RepairHash * "+strepairtable;
188 for(int j=0;j<r.numQuantifiers();j++) {
189 Quantifier q=r.getQuantifier(j);
190 if (q instanceof SetQuantifier) {
191 SetQuantifier sq=(SetQuantifier) q;
192 methodcall+=","+sq.getVar().getType().getGenerateType().getSafeSymbol()+" "+sq.getVar().getSafeSymbol();
193 } else if (q instanceof RelationQuantifier) {
194 RelationQuantifier rq=(RelationQuantifier) q;
196 methodcall+=","+rq.x.getType().getGenerateType().getSafeSymbol()+" "+rq.x.getSafeSymbol();
197 methodcall+=","+rq.y.getType().getGenerateType().getSafeSymbol()+" "+rq.y.getSafeSymbol();
198 } else if (q instanceof ForQuantifier) {
199 ForQuantifier fq=(ForQuantifier) q;
200 methodcall+=",int "+fq.getVar().getSafeSymbol();
204 crhead.outputline(methodcall+";");
205 craux.outputline(methodcall);
208 craux.addDeclaration("int","maybe");
209 craux.outputline("maybe=0;");
210 if (Compiler.GENERATEINSTRUMENT)
211 craux.outputline("updatecount++;");
212 final SymbolTable st2 = un.getRule().getSymbolTable();
213 CodeWriter cr2 = new StandardCodeWriter(outputaux) {
214 public SymbolTable getSymbolTable() { return st2; }
216 un.generate(cr2, true, false, null,null, null,this);
217 craux.outputline("if (maybe) printf(\"REALLY BAD\");");
222 case MultUpdateNode.MODIFY: {
224 String methodcall="void "+methodname+"(struct "+name+"_state * "+ststate+", struct "+name+" * "+stmodel+", struct RepairHash * "+strepairtable;
225 for(int j=0;j<r.numQuantifiers();j++) {
226 Quantifier q=r.getQuantifier(j);
227 if (q instanceof SetQuantifier) {
228 SetQuantifier sq=(SetQuantifier) q;
229 methodcall+=","+sq.getVar().getType().getGenerateType().getSafeSymbol()+" "+sq.getVar().getSafeSymbol();
230 } else if (q instanceof RelationQuantifier) {
231 RelationQuantifier rq=(RelationQuantifier) q;
233 methodcall+=", "+rq.x.getType().getGenerateType().getSafeSymbol()+" "+rq.x.getSafeSymbol();
234 methodcall+=", "+rq.y.getType().getGenerateType().getSafeSymbol()+" "+rq.y.getSafeSymbol();
235 } else if (q instanceof ForQuantifier) {
236 ForQuantifier fq=(ForQuantifier) q;
237 methodcall+=", int "+fq.getVar().getSafeSymbol();
240 methodcall+=", int "+stleft+", int "+stright+", int "+stnew;
242 crhead.outputline(methodcall+";");
243 craux.outputline(methodcall);
246 craux.outputline("int maybe=0;");
247 if (Compiler.GENERATEINSTRUMENT)
248 craux.outputline("updatecount++;");
249 final SymbolTable st2 = un.getRule().getSymbolTable();
250 CodeWriter cr2 = new StandardCodeWriter(outputaux) {
251 public SymbolTable getSymbolTable() { return st2; }
253 un.generate(cr2, false, true, stleft, stright, stnew, this);
254 craux.outputline("if (maybe) printf(\"REALLY BAD\");");
261 throw new Error("Nonimplement Update");
267 private void generate_call() {
268 CodeWriter cr = new StandardCodeWriter(outputrepair);
269 VarDescriptor vdstate=VarDescriptor.makeNew("repairstate");
270 cr.addDeclaration("struct "+ name+"_state *", vdstate.getSafeSymbol());
271 cr.outputline(vdstate.getSafeSymbol()+"=allocate"+name+"_state();");
272 Iterator globals=state.stGlobals.descriptors();
273 while (globals.hasNext()) {
274 VarDescriptor vd=(VarDescriptor) globals.next();
275 cr.outputline(vdstate.getSafeSymbol()+"->"+vd.getSafeSymbol()+"=("+vd.getType().getGenerateType().getSafeSymbol()+")"+vd.getSafeSymbol()+";");
277 /* Insert repair here */
278 cr.outputline("doanalysis("+vdstate.getSafeSymbol()+");");
279 globals=state.stGlobals.descriptors();
280 while (globals.hasNext()) {
281 VarDescriptor vd=(VarDescriptor) globals.next();
282 cr.outputline("*(("+vd.getType().getGenerateType().getSafeSymbol()+"*) &"+vd.getSafeSymbol()+")="+vdstate.getSafeSymbol()+"->"+vd.getSafeSymbol()+";");
284 cr.outputline("free"+name+"_state("+vdstate.getSafeSymbol()+");");
287 private void generate_tokentable() {
288 CodeWriter cr = new StandardCodeWriter(outputrepair);
289 Iterator tokens = TokenLiteralExpr.tokens.keySet().iterator();
292 cr.outputline("/* Token values*/");
295 while (tokens.hasNext()) {
296 Object token = tokens.next();
297 cr.outputline("/* " + token.toString() + " = " + TokenLiteralExpr.tokens.get(token).toString()+"*/");
304 private void generate_stateobject() {
305 CodeWriter crhead = new StandardCodeWriter(outputhead);
306 CodeWriter craux = new StandardCodeWriter(outputaux);
307 crhead.outputline("struct "+name+"_state {");
308 Iterator globals=state.stGlobals.descriptors();
309 while (globals.hasNext()) {
310 VarDescriptor vd=(VarDescriptor) globals.next();
311 crhead.outputline(vd.getType().getGenerateType().getSafeSymbol()+" "+vd.getSafeSymbol()+";");
313 crhead.outputline("};");
314 crhead.outputline("struct "+name+"_state * allocate"+name+"_state();");
315 craux.outputline("struct "+name+"_state * allocate"+name+"_state()");
317 craux.outputline("return (struct "+name+"_state *) malloc(sizeof(struct "+name+"_state));");
320 crhead.outputline("void free"+name+"_state(struct "+name+"_state *);");
321 craux.outputline("void free"+name+"_state(struct "+name+"_state * thisvar)");
323 craux.outputline("free(thisvar);");
326 crhead.outputline("void "+name+"_statecomputesizes(struct "+name+"_state * ,int *,int **);");
327 crhead.outputline("void "+name+"_staterecomputesizes(struct "+name+"_state *);");
330 private void generate_computesizes() {
331 int max=TypeDescriptor.counter;
332 TypeDescriptor[] tdarray=new TypeDescriptor[max];
333 for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
334 TypeDescriptor ttd=(TypeDescriptor)it.next();
335 tdarray[ttd.getId()]=ttd;
337 final SymbolTable st = state.stGlobals;
338 CodeWriter cr = new StandardCodeWriter(outputaux) {
339 public SymbolTable getSymbolTable() { return st; }
342 cr.outputline("void "+name+"_statecomputesizes(struct "+name+"_state * thisvar,int *sizearray,int **numele)");
345 cr.addDeclaration("int","maybe");
346 cr.outputline("maybe=0;");
347 for(int i=0;i<max;i++) {
348 TypeDescriptor td=tdarray[i];
349 Expr size=td.getSizeExpr();
350 VarDescriptor vd=VarDescriptor.makeNew("size");
351 size.generate(cr,vd);
352 cr.outputline("sizearray["+i+"]="+vd.getSafeSymbol()+";");
354 for(int i=0;i<max;i++) {
355 TypeDescriptor td=tdarray[i];
356 if (td instanceof StructureTypeDescriptor) {
357 StructureTypeDescriptor std=(StructureTypeDescriptor) td;
358 for(int j=0;j<std.fieldlist.size();j++) {
359 FieldDescriptor fd=(FieldDescriptor)std.fieldlist.get(j);
360 if (fd instanceof ArrayDescriptor) {
361 ArrayDescriptor ad=(ArrayDescriptor)fd;
362 Expr index=ad.getIndexBound();
363 VarDescriptor vd=VarDescriptor.makeNew("index");
364 index.generate(cr,vd);
365 cr.outputline("numele["+i+"]["+j+"]="+vd.getSafeSymbol()+";");
370 cr.outputline("if (maybe) printf(\"BAD ERROR\");");
375 private void generate_recomputesizes() {
376 int max=TypeDescriptor.counter;
377 TypeDescriptor[] tdarray=new TypeDescriptor[max];
378 for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
379 TypeDescriptor ttd=(TypeDescriptor)it.next();
380 tdarray[ttd.getId()]=ttd;
382 final SymbolTable st = state.stGlobals;
383 CodeWriter cr = new StandardCodeWriter(outputaux) {
384 public SymbolTable getSymbolTable() { return st; }
386 cr.outputline("void "+name+"_staterecomputesizes(struct "+name+"_state * thisvar)");
389 cr.addDeclaration("int","maybe");
390 cr.outputline("maybe=0;");
391 for(int i=0;i<max;i++) {
392 TypeDescriptor td=tdarray[i];
393 Expr size=td.getSizeExpr();
394 VarDescriptor vd=VarDescriptor.makeNew("size");
395 size.generate(cr,vd);
397 for(int i=0;i<max;i++) {
398 TypeDescriptor td=tdarray[i];
399 if (td instanceof StructureTypeDescriptor) {
400 StructureTypeDescriptor std=(StructureTypeDescriptor) td;
401 for(int j=0;j<std.fieldlist.size();j++) {
402 FieldDescriptor fd=(FieldDescriptor)std.fieldlist.get(j);
403 if (fd instanceof ArrayDescriptor) {
404 ArrayDescriptor ad=(ArrayDescriptor)fd;
405 Expr index=ad.getIndexBound();
406 VarDescriptor vd=VarDescriptor.makeNew("index");
407 index.generate(cr,vd);
412 cr.outputline("if (maybe) printf(\"BAD ERROR\");");
418 private void generate_hashtables() {
419 CodeWriter craux = new StandardCodeWriter(outputaux);
420 CodeWriter crhead = new StandardCodeWriter(outputhead);
421 crhead.outputline("#ifndef "+name+"_h");
422 crhead.outputline("#define "+name+"_h");
423 crhead.outputline("#include \"SimpleHash.h\"");
424 crhead.outputline("#include \"instrument.h\"");
425 crhead.outputline("#include <stdio.h>");
426 crhead.outputline("#include <stdlib.h>");
427 crhead.outputline("struct "+name+" * allocate"+name+"();");
428 crhead.outputline("void free"+name+"(struct "+name+" *);");
429 crhead.outputline("struct "+name+" {");
430 craux.outputline("#include \""+headername+"\"");
431 craux.outputline("#include \"size.h\"");
433 craux.outputline("#include <sys/time.h>");
435 if (Compiler.ALLOCATECPLUSPLUS) {
436 for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
437 TypeDescriptor td=(TypeDescriptor)it.next();
438 if (td instanceof StructureTypeDescriptor) {
439 if (((StructureTypeDescriptor)td).size()>0) {
440 FieldDescriptor fd=((StructureTypeDescriptor)td).get(0);
441 if (fd.getSymbol().startsWith("_vptr_")) {
442 String vtable="_ZTV";
443 vtable+=td.getSymbol().length();
444 vtable+=td.getSymbol();
445 craux.outputline("extern void * "+vtable+";");
451 craux.outputline("struct "+ name+"* allocate"+name+"()");
453 craux.outputline("/* creating hashtables */");
456 Iterator sets = state.stSets.descriptors();
457 craux.addDeclaration("struct "+name+"*", "thisvar");
458 craux.outputline("thisvar=(struct "+name+"*) malloc(sizeof(struct "+name+"));");
460 /* first pass create all the hash tables */
461 while (sets.hasNext()) {
462 SetDescriptor set = (SetDescriptor) sets.next();
463 crhead.outputline("struct SimpleHash* " + set.getJustSafeSymbol() + "_hash;");
464 craux.outputline(set.getSafeSymbol() + "_hash = noargallocateSimpleHash();");
467 /* second pass build relationships between hashtables */
468 sets = state.stSets.descriptors();
470 while (sets.hasNext()) {
471 SetDescriptor set = (SetDescriptor) sets.next();
472 Iterator subsets = set.subsets();
474 while (subsets.hasNext()) {
475 SetDescriptor subset = (SetDescriptor) subsets.next();
476 craux.outputline("SimpleHashaddParent("+subset.getSafeSymbol() +"_hash ,"+ set.getSafeSymbol() + "_hash);");
480 /* build relations */
481 Iterator relations = state.stRelations.descriptors();
483 /* first pass create all the hash tables */
484 while (relations.hasNext()) {
485 RelationDescriptor relation = (RelationDescriptor) relations.next();
487 if (relation.testUsage(RelationDescriptor.IMAGE)) {
488 crhead.outputline("struct SimpleHash* " + relation.getJustSafeSymbol() + "_hash;");
489 craux.outputline(relation.getSafeSymbol() + "_hash = noargallocateSimpleHash();");
492 if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
493 crhead.outputline("struct SimpleHash* " + relation.getJustSafeSymbol() + "_hashinv;");
494 craux.outputline(relation.getSafeSymbol() + "_hashinv = noargallocateSimpleHash();");
499 crhead.outputline("};");
500 craux.outputline("void free"+name+"(struct "+ name +"* thisvar)");
502 craux.outputline("/* deleting hashtables */");
504 /* build destructor */
505 sets = state.stSets.descriptors();
507 /* first pass create all the hash tables */
508 while (sets.hasNext()) {
509 SetDescriptor set = (SetDescriptor) sets.next();
510 craux.outputline("freeSimpleHash("+set.getSafeSymbol() + "_hash);");
513 /* destroy relations */
514 relations = state.stRelations.descriptors();
516 /* first pass create all the hash tables */
517 while (relations.hasNext()) {
518 RelationDescriptor relation = (RelationDescriptor) relations.next();
520 if (relation.testUsage(RelationDescriptor.IMAGE)) {
521 craux.outputline("freeSimpleHash("+relation.getSafeSymbol() + "_hash);");
524 if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
525 craux.outputline("freeSimpleHash(" + relation.getSafeSymbol() + "_hashinv);");
528 craux.outputline("free(thisvar);");
532 private void generate_start() {
533 CodeWriter crhead = new StandardCodeWriter(outputhead);
534 CodeWriter craux = new StandardCodeWriter(outputaux);
535 oldmodel=VarDescriptor.makeNew("oldmodel");
536 newmodel=VarDescriptor.makeNew("newmodel");
537 worklist=VarDescriptor.makeNew("worklist");
538 goodflag=VarDescriptor.makeNew("goodflag");
539 repairtable=VarDescriptor.makeNew("repairtable");
541 if (Compiler.GENERATEINSTRUMENT) {
542 craux.outputline("int updatecount;");
543 craux.outputline("int rebuildcount;");
544 craux.outputline("int abstractcount;");
547 crhead.outputline("void doanalysis(struct "+name+"_state *);");
548 craux.outputline("void doanalysis(struct "+name+"_state * thisvar)");
552 craux.outputline("struct timeval _begin_time,_end_time;");
553 craux.outputline("gettimeofday(&_begin_time,NULL);");
555 if (Compiler.GENERATEINSTRUMENT) {
556 craux.outputline("updatecount=0;");
557 craux.outputline("rebuildcount=0;");
558 craux.outputline("abstractcount=0;");
560 craux.addDeclaration("int","highmark");
562 craux.addDeclaration("struct "+name+ " * ",oldmodel.getSafeSymbol());
563 craux.outputline(oldmodel.getSafeSymbol()+"=0;");
564 craux.addDeclaration("struct WorkList * ",worklist.getSafeSymbol());
565 craux.outputline(worklist.getSafeSymbol()+" = allocateWorkList();");
566 craux.addDeclaration("struct RepairHash * ",repairtable.getSafeSymbol());
567 craux.outputline(repairtable.getSafeSymbol()+"=0;");
568 craux.outputline("initializestack(&highmark);");
569 craux.outputline("computesizes(thisvar);");
570 craux.outputline(name+"_staterecomputesizes(thisvar);");
571 craux.outputline("while (1)");
573 craux.addDeclaration("struct "+name+ " * ",newmodel.getSafeSymbol());
574 craux.outputline(newmodel.getSafeSymbol()+"=allocate"+name+"();");
575 craux.outputline("WorkListreset("+worklist.getSafeSymbol()+");");
576 if (Compiler.GENERATEINSTRUMENT)
577 craux.outputline("rebuildcount++;");
580 private void generate_teardown() {
581 CodeWriter cr = new StandardCodeWriter(outputaux);
584 cr.outputline("gettimeofday(&_end_time,NULL);");
585 cr.outputline("printf(\"time=%ld uS\\n\",(_end_time.tv_sec-_begin_time.tv_sec)*1000000+_end_time.tv_usec-_begin_time.tv_usec);");
588 if (Compiler.GENERATEINSTRUMENT) {
589 cr.outputline("printf(\"updatecount=%d\\n\",updatecount);");
590 cr.outputline("printf(\"rebuildcount=%d\\n\",rebuildcount);");
591 cr.outputline("printf(\"abstractcount=%d\\n\",abstractcount);");
596 private void generate_print() {
598 final SymbolTable st = new SymbolTable();
600 CodeWriter cr = new StandardCodeWriter(outputaux) {
601 public SymbolTable getSymbolTable() { return st; }
604 cr.outputline("/* printing sets!*/");
605 cr.outputline("printf(\"\\n\\nPRINTING SETS AND RELATIONS\\n\");");
607 Iterator setiterator = state.stSets.descriptors();
608 while (setiterator.hasNext()) {
609 SetDescriptor sd = (SetDescriptor) setiterator.next();
610 if (sd.getSymbol().equals("int") || sd.getSymbol().equals("token")) {
614 String setname = sd.getSafeSymbol();
617 cr.outputline("/* printing set " + setname+"*/");
618 cr.outputline("printf(\"\\nPrinting set " + sd.getSymbol() + " - %d elements \\n\", SimpleHashcountset("+setname+"_hash));");
619 cr.addDeclaration("struct SimpleIterator","__setiterator");
620 cr.outputline("SimpleHashiterator("+setname+"_hash,&__setiterator);");
621 cr.outputline("while (hasNext(&__setiterator))");
623 cr.addDeclaration("int","__setval");
624 cr.outputline("__setval = (int) next(&__setiterator);");
626 TypeDescriptor td = sd.getType();
627 if (td instanceof StructureTypeDescriptor) {
628 StructureTypeDescriptor std = (StructureTypeDescriptor) td;
629 VarDescriptor vd = new VarDescriptor ("__setval", "__setval", td, false);
630 std.generate_printout(cr, vd);
631 } else { // Missing type descriptor or reserved type, just print int
632 cr.outputline("printf(\"<%d> \", __setval);");
640 cr.outputline("printf(\"\\n\\n------------------- END PRINTING\\n\");");
644 private void generate_rules() {
645 /* first we must sort the rules */
646 RelationDescriptor.prefix = newmodel.getSafeSymbol()+"->";
647 SetDescriptor.prefix = newmodel.getSafeSymbol()+"->";
648 System.out.println("SCC="+(mrd.numSCC()-1));
649 for(int sccindex=0;sccindex<mrd.numSCC();sccindex++) {
650 ruleset=mrd.getSCC(sccindex);
651 boolean needworklist=mrd.hasCycle(sccindex);
654 Iterator iterator_rs = ruleset.iterator();
655 while (iterator_rs.hasNext()) {
656 Rule rule = (Rule) iterator_rs.next();
657 if (rule.getnogenerate())
660 final SymbolTable st = rule.getSymbolTable();
661 CodeWriter cr = new StandardCodeWriter(outputaux) {
662 public SymbolTable getSymbolTable() { return st; }
664 InvariantValue ivalue=new InvariantValue();
665 cr.setInvariantValue(ivalue);
667 cr.outputline("/* build " +escape(rule.toString())+"*/");
669 cr.addDeclaration("int","maybe");
670 cr.outputline("maybe=0;");
672 Expr ruleexpr=rule.getGuardExpr();
673 HashSet invariantvars=new HashSet();
674 Set invariants=ruleexpr.findInvariants(invariantvars);
676 if ((ruleexpr instanceof BooleanLiteralExpr)&&
677 ((BooleanLiteralExpr)ruleexpr).getValue()) {
678 if (rule.getInclusion() instanceof SetInclusion) {
679 invariants.addAll(((SetInclusion)rule.getInclusion()).getExpr().findInvariants(invariantvars));
680 } else if (rule.getInclusion() instanceof RelationInclusion) {
681 invariants.addAll(((RelationInclusion)rule.getInclusion()).getLeftExpr().findInvariants(invariantvars));
682 invariants.addAll(((RelationInclusion)rule.getInclusion()).getRightExpr().findInvariants(invariantvars));
685 ListIterator quantifiers = rule.quantifiers();
686 while (quantifiers.hasNext()) {
687 Quantifier quantifier = (Quantifier) quantifiers.next();
688 if (quantifier instanceof ForQuantifier) {
689 ForQuantifier fq=(ForQuantifier)quantifier;
690 invariants.addAll(fq.lower.findInvariants(invariantvars));
691 invariants.addAll(fq.upper.findInvariants(invariantvars));
695 int openparencount=0;
696 for(Iterator invit=invariants.iterator();invit.hasNext();) {
697 Expr invexpr=(Expr)invit.next();
698 VarDescriptor tmpvd=VarDescriptor.makeNew("tmpvar");
699 VarDescriptor maybevd=VarDescriptor.makeNew("maybevar");
700 invexpr.generate(cr,tmpvd);
701 cr.addDeclaration("int ",maybevd.getSafeSymbol());
702 cr.outputline(maybevd.getSafeSymbol()+"=maybe;");
703 cr.outputline("maybe=0;");
704 ivalue.assignPair(invexpr,tmpvd,maybevd);
708 quantifiers = rule.quantifiers();
709 while (quantifiers.hasNext()) {
710 Quantifier quantifier = (Quantifier) quantifiers.next();
711 quantifier.generate_open(cr);
716 rule.getGuardExpr().prettyPrint(cr);
719 /* now we have to generate the guard test */
720 VarDescriptor guardval = VarDescriptor.makeNew();
721 rule.getGuardExpr().generate(cr, guardval);
722 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
725 /* now we have to generate the inclusion code */
727 rule.getInclusion().generate(cr);
729 while (quantifiers.hasPrevious()) {
730 Quantifier quantifier = (Quantifier) quantifiers.previous();
734 while((openparencount--)>0)
741 CodeWriter cr2 = new StandardCodeWriter(outputaux);
743 for(Iterator initialworklist=ruleset.iterator();initialworklist.hasNext();) {
744 /** Construct initial worklist set */
745 Rule rule=(Rule)initialworklist.next();
746 cr2.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+",-1,0,0);");
749 cr2.outputline("while (WorkListhasMoreElements("+worklist.getSafeSymbol()+"))");
751 VarDescriptor idvar=VarDescriptor.makeNew("id");
752 cr2.addDeclaration("int ",idvar.getSafeSymbol());
753 cr2.outputline(idvar.getSafeSymbol()+"=WorkListgetid("+worklist.getSafeSymbol()+");");
755 String elseladder = "if";
757 Iterator iterator_rules = ruleset.iterator();
758 while (iterator_rules.hasNext()) {
760 Rule rule = (Rule) iterator_rules.next();
761 int dispatchid = rule.getNum();
764 final SymbolTable st = rule.getSymbolTable();
765 CodeWriter cr = new StandardCodeWriter(outputaux) {
766 public SymbolTable getSymbolTable() { return st; }
770 cr.outputline(elseladder + " ("+idvar.getSafeSymbol()+" == " + dispatchid + ")");
772 cr.addDeclaration("int","maybe");
773 cr.outputline("maybe=0;");
774 VarDescriptor typevar=VarDescriptor.makeNew("type");
775 VarDescriptor leftvar=VarDescriptor.makeNew("left");
776 VarDescriptor rightvar=VarDescriptor.makeNew("right");
777 cr.addDeclaration("int",typevar.getSafeSymbol());
778 cr.outputline(typevar.getSafeSymbol()+"= WorkListgettype("+worklist.getSafeSymbol()+");");
779 cr.addDeclaration("int",leftvar.getSafeSymbol());
780 cr.outputline(leftvar.getSafeSymbol()+"= WorkListgetlvalue("+worklist.getSafeSymbol()+");");
781 cr.addDeclaration("int",rightvar.getSafeSymbol());
782 cr.outputline(rightvar.getSafeSymbol()+"= WorkListgetrvalue("+worklist.getSafeSymbol()+");");
783 cr.outputline("/* build " +escape(rule.toString())+"*/");
786 for (int j=0;j<rule.numQuantifiers();j++) {
787 Quantifier quantifier = rule.getQuantifier(j);
788 quantifier.generate_open(cr, typevar.getSafeSymbol(),j,leftvar.getSafeSymbol(),rightvar.getSafeSymbol());
794 rule.getGuardExpr().prettyPrint(cr);
797 /* now we have to generate the guard test */
799 VarDescriptor guardval = VarDescriptor.makeNew();
800 rule.getGuardExpr().generate(cr, guardval);
802 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
805 /* now we have to generate the inclusion code */
807 rule.getInclusion().generate(cr);
810 for (int j=0;j<rule.numQuantifiers();j++) {
814 // close startblocks generated by DotExpr memory checks
815 //DotExpr.generate_memory_endblocks(cr);
817 cr.endblock(); // end else-if WORKLIST ladder
819 elseladder = "else if";
822 cr2.outputline("else");
824 cr2.outputline("printf(\"VERY BAD !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\\n\\n\");");
825 cr2.outputline("exit(1);");
827 // end block created for worklist
828 cr2.outputline("WorkListpop("+worklist.getSafeSymbol()+");");
834 public static String escape(String s) {
836 for(int i=0;i<s.length();i++) {
846 private void generate_checks() {
847 /* do constraint checks */
850 i=termination.constraintdependence.computeOrdering().iterator();
852 i=state.vConstraints.iterator();
853 for (; i.hasNext();) {
854 Constraint constraint;
856 constraint= (Constraint) ((GraphNode)i.next()).getOwner();
858 constraint=(Constraint)i.next();
861 final SymbolTable st = constraint.getSymbolTable();
862 CodeWriter cr = new StandardCodeWriter(outputaux);
863 cr.pushSymbolTable(constraint.getSymbolTable());
865 cr.outputline("/* checking " + escape(constraint.toString())+"*/");
868 ListIterator quantifiers = constraint.quantifiers();
870 while (quantifiers.hasNext()) {
871 Quantifier quantifier = (Quantifier) quantifiers.next();
872 quantifier.generate_open(cr);
875 cr.addDeclaration("int","maybe");
876 cr.outputline("maybe = 0;");
878 /* now we have to generate the guard test */
880 VarDescriptor constraintboolean = VarDescriptor.makeNew("constraintboolean");
881 constraint.getLogicStatement().generate(cr, constraintboolean);
883 cr.outputline("if (maybe)");
885 cr.outputline("printf(\"maybe fail " + escape(constraint.toString()) + ". \\n\");");
886 //cr.outputline("exit(1);");
889 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
891 if (!Compiler.REPAIR||Compiler.GENERATEDEBUGHOOKS)
892 cr.outputline("printf(\"fail " + escape(constraint.toString()) + ". \\n\");");
894 if (Compiler.REPAIR) {
896 /* Build new repair table */
898 cr.outputline("if ("+repairtable.getSafeSymbol()+")");
899 cr.outputline("freeRepairHash("+repairtable.getSafeSymbol()+");");
900 cr.outputline(repairtable.getSafeSymbol()+"=noargallocateRepairHash();");
902 if (Compiler.GENERATEDEBUGHOOKS)
903 cr.outputline("debughook();");
904 /* Compute cost of each repair */
905 VarDescriptor mincost=VarDescriptor.makeNew("mincost");
906 VarDescriptor mincostindex=VarDescriptor.makeNew("mincostindex");
907 Vector dnfconst=new Vector();
908 dnfconst.addAll((Set)termination.conjunctionmap.get(constraint));
910 if (dnfconst.size()<=1) {
911 cr.addDeclaration("int",mincostindex.getSafeSymbol());
912 cr.outputline(mincostindex.getSafeSymbol()+"=0;");
914 if (dnfconst.size()>1) {
915 cr.addDeclaration("int",mincostindex.getSafeSymbol());
917 for(int j=0;j<dnfconst.size();j++) {
918 GraphNode gn=(GraphNode)dnfconst.get(j);
919 Conjunction conj=((TermNode)gn.getOwner()).getConjunction();
920 if (removed.contains(gn))
923 VarDescriptor costvar;
927 costvar=VarDescriptor.makeNew("cost");
928 for(int k=0;k<conj.size();k++) {
929 DNFPredicate dpred=conj.get(k);
930 Predicate p=dpred.getPredicate();
931 boolean negate=dpred.isNegated();
932 VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
933 p.generate(cr,predvalue);
935 cr.addDeclaration("int",costvar.getSafeSymbol());
936 cr.outputline(costvar.getSafeSymbol()+"=0;");
939 cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
941 cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
942 cr.outputline(costvar.getSafeSymbol()+"+="+cost.getCost(dpred)+";");
946 cr.outputline("if ("+costvar.getSafeSymbol()+"<"+mincost.getSafeSymbol()+")");
948 cr.outputline(mincost.getSafeSymbol()+"="+costvar.getSafeSymbol()+";");
949 cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
952 cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
956 cr.outputline("switch("+mincostindex.getSafeSymbol()+")");
958 for(int j=0;j<dnfconst.size();j++) {
959 GraphNode gn=(GraphNode)dnfconst.get(j);
960 Conjunction conj=((TermNode)gn.getOwner()).getConjunction();
961 if (removed.contains(gn))
963 cr.outputline("case "+j+":");
965 for(int k=0;k<conj.size();k++) {
966 DNFPredicate dpred=conj.get(k);
967 Predicate p=dpred.getPredicate();
968 boolean negate=dpred.isNegated();
969 VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
970 p.generate(cr,predvalue);
972 cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
974 cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
976 if (Compiler.GENERATEINSTRUMENT)
977 cr.outputline("abstractcount++;");
978 if (p instanceof InclusionPredicate)
979 generateinclusionrepair(conj,dpred, cr);
980 else if (p instanceof ExprPredicate) {
981 ExprPredicate ep=(ExprPredicate)p;
982 if (ep.getType()==ExprPredicate.SIZE)
983 generatesizerepair(conj,dpred,cr);
984 else if (ep.getType()==ExprPredicate.COMPARISON)
985 generatecomparisonrepair(conj,dpred,cr);
986 } else throw new Error("Unrecognized Predicate");
991 cr.outputline("break;");
995 cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
996 cr.outputline("free"+name+"("+oldmodel.getSafeSymbol()+");");
997 cr.outputline(oldmodel.getSafeSymbol()+"="+newmodel.getSafeSymbol()+";");
998 cr.outputline("goto rebuild;"); /* Rebuild model and all */
1002 while (quantifiers.hasPrevious()) {
1003 Quantifier quantifier = (Quantifier) quantifiers.previous();
1011 CodeWriter cr = new StandardCodeWriter(outputaux);
1013 cr.outputline("if ("+repairtable.getSafeSymbol()+")");
1014 cr.outputline("freeRepairHash("+repairtable.getSafeSymbol()+");");
1015 cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
1016 cr.outputline("free"+name+"("+oldmodel.getSafeSymbol()+");");
1017 cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1018 cr.outputline("freeWorkList("+worklist.getSafeSymbol()+");");
1019 cr.outputline("resettypemap();");
1020 cr.outputline("break;");
1022 cr.outputline("rebuild:");
1026 private MultUpdateNode getmultupdatenode(Conjunction conj, DNFPredicate dpred, int repairtype) {
1027 Set nodes=getmultupdatenodeset(conj,dpred,repairtype);
1028 Iterator it=nodes.iterator();
1030 return (MultUpdateNode)it.next();
1035 private Set getmultupdatenodeset(Conjunction conj, DNFPredicate dpred, int repairtype) {
1036 HashSet hs=new HashSet();
1037 GraphNode gn=(GraphNode) termination.conjtonodemap.get(conj);
1038 for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1039 GraphNode gn2=((GraphNode.Edge) edgeit.next()).getTarget();
1040 TermNode tn2=(TermNode)gn2.getOwner();
1041 if (tn2.getType()==TermNode.ABSTRACT) {
1042 AbstractRepair ar=tn2.getAbstract();
1043 if (((repairtype==-1)||(ar.getType()==repairtype))&&
1044 ar.getPredicate()==dpred) {
1045 for(Iterator edgeit2=gn2.edges();edgeit2.hasNext();) {
1046 GraphNode gn3=((GraphNode.Edge) edgeit2.next()).getTarget();
1047 if (!removed.contains(gn3)) {
1048 TermNode tn3=(TermNode)gn3.getOwner();
1049 if (tn3.getType()==TermNode.UPDATE) {
1050 hs.add(tn3.getUpdate());
1060 private AbstractRepair getabstractrepair(Conjunction conj, DNFPredicate dpred, int repairtype) {
1061 HashSet hs=new HashSet();
1062 MultUpdateNode mun=null;
1063 GraphNode gn=(GraphNode) termination.conjtonodemap.get(conj);
1064 for(Iterator edgeit=gn.edges();(mun==null)&&edgeit.hasNext();) {
1065 GraphNode gn2=((GraphNode.Edge) edgeit.next()).getTarget();
1066 TermNode tn2=(TermNode)gn2.getOwner();
1067 if (tn2.getType()==TermNode.ABSTRACT) {
1068 AbstractRepair ar=tn2.getAbstract();
1069 if (((repairtype==-1)||(ar.getType()==repairtype))&&
1070 ar.getPredicate()==dpred) {
1079 /** Generates abstract (and concrete) repair for a comparison */
1081 private void generatecomparisonrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
1082 Set updates=getmultupdatenodeset(conj,dpred,AbstractRepair.MODIFYRELATION);
1083 AbstractRepair ar=getabstractrepair(conj,dpred,AbstractRepair.MODIFYRELATION);
1084 MultUpdateNode munmodify=null;
1085 MultUpdateNode munadd=null;
1086 MultUpdateNode munremove=null;
1087 for(Iterator it=updates.iterator();it.hasNext();) {
1088 MultUpdateNode mun=(MultUpdateNode)it.next();
1089 if (mun.getType()==MultUpdateNode.ADD) {
1091 } else if (mun.getType()==MultUpdateNode.REMOVE) {
1093 } else if (mun.getType()==MultUpdateNode.MODIFY) {
1098 ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
1099 RelationDescriptor rd=(RelationDescriptor)ep.getDescriptor();
1100 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1101 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1102 boolean inverted=ep.inverted();
1103 boolean negated=dpred.isNegated();
1104 OpExpr expr=(OpExpr)ep.expr;
1105 Opcode opcode=expr.getOpcode();
1106 VarDescriptor leftside=VarDescriptor.makeNew("leftside");
1107 VarDescriptor rightside=VarDescriptor.makeNew("rightside");
1108 VarDescriptor newvalue=VarDescriptor.makeNew("newvalue");
1109 boolean needremoveloop=ar.mayNeedFunctionEnforcement(state)&&ar.needsRemoves(state);
1111 if (needremoveloop&&((munadd==null)||(munremove==null))) {
1112 System.out.println("Warning: need to have individual remove operations for"+dpred.name());
1113 needremoveloop=false;
1115 if (needremoveloop) {
1116 cr.outputline("while (1)");
1120 ((RelationExpr)expr.getLeftExpr()).getExpr().generate(cr,leftside);
1121 expr.getRightExpr().generate(cr,newvalue);
1122 cr.addDeclaration(rd.getRange().getType().getGenerateType().getSafeSymbol(),rightside.getSafeSymbol());
1123 cr.outputline("SimpleHashget("+rd.getSafeSymbol()+"_hash,"+leftside.getSafeSymbol()+", &"+rightside.getSafeSymbol()+");");
1125 ((RelationExpr)expr.getLeftExpr()).getExpr().generate(cr,rightside);
1126 expr.getRightExpr().generate(cr,newvalue);
1127 cr.outputline(rd.getDomain().getType().getGenerateType().getSafeSymbol()+" "+leftside.getSafeSymbol()+";");
1128 cr.outputline("SimpleHashget("+rd.getSafeSymbol()+"_hashinv,"+rightside.getSafeSymbol()+", &"+leftside.getSafeSymbol()+");");
1131 opcode=Opcode.translateOpcode(negated,opcode);
1133 if (opcode==Opcode.GT) {
1134 cr.outputline(newvalue.getSafeSymbol()+"++;");
1135 } else if (opcode==Opcode.GE) {
1137 } else if (opcode==Opcode.LT) {
1138 cr.outputline(newvalue.getSafeSymbol()+"--;");
1139 } else if (opcode==Opcode.LE) {
1141 } else if (opcode==Opcode.EQ) {
1143 } else if (opcode==Opcode.NE) { /* search for FLAGNE if this is changed*/
1144 cr.outputline(newvalue.getSafeSymbol()+"++;");
1146 throw new Error("Unrecognized Opcode");
1148 /* Do abstract repairs */
1150 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash, "+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1152 if (usageinvimage) {
1153 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv, "+rightside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
1156 if (needremoveloop) {
1158 cr.outputline("if (SimpleHashcontainskey("+rd.getSafeSymbol()+"_hash, "+leftside.getSafeSymbol()+"))");
1161 cr.outputline("if (SimpleHashcontainskey("+rd.getSafeSymbol()+"_hashinv, "+rightside.getSafeSymbol()+"))");
1164 for(int i=0;i<state.vRules.size();i++) {
1165 Rule r=(Rule)state.vRules.get(i);
1166 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1167 for(int j=0;j<munremove.numUpdates();j++) {
1168 UpdateNode un=munremove.getUpdate(i);
1169 if (un.getRule()==r) {
1170 /* Update for rule r */
1171 String name=(String)updatenames.get(un);
1172 cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+");");
1177 cr.outputline("continue;");
1183 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash,"+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1185 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, "+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1188 if (usageinvimage) {
1190 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, "+newvalue.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
1192 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv,"+rightside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1195 /* Do concrete repairs */
1196 if (munmodify!=null&&(!ar.mayNeedFunctionEnforcement(state))||(munadd==null)||(ar.needsRemoves(state)&&(munremove==null))) {
1197 for(int i=0;i<state.vRules.size();i++) {
1198 Rule r=(Rule)state.vRules.get(i);
1199 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1200 for(int j=0;j<munmodify.numUpdates();j++) {
1201 UpdateNode un=munmodify.getUpdate(j);
1202 if (un.getRule()==r) {
1203 /* Update for rule r */
1204 String name=(String)updatenames.get(un);
1205 cr.outputline("RepairHashaddrelation2("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+","+newvalue.getSafeSymbol()+");");
1211 /* Start with scheduling removal */
1212 if (ar.needsRemoves(state))
1213 for(int i=0;i<state.vRules.size();i++) {
1214 Rule r=(Rule)state.vRules.get(i);
1215 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1216 for(int j=0;j<munremove.numUpdates();j++) {
1217 UpdateNode un=munremove.getUpdate(i);
1218 if (un.getRule()==r) {
1219 /* Update for rule r */
1220 String name=(String)updatenames.get(un);
1221 cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+");");
1226 /* Now do addition */
1227 UpdateNode un=munadd.getUpdate(0);
1228 String name=(String)updatenames.get(un);
1230 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1232 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1235 if (needremoveloop) {
1236 cr.outputline("break;");
1241 public void generatesizerepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr) {
1242 ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
1243 OpExpr expr=(OpExpr)ep.expr;
1244 Opcode opcode=expr.getOpcode();
1245 opcode=Opcode.translateOpcode(dpred.isNegated(),opcode);
1247 MultUpdateNode munremove;
1249 MultUpdateNode munadd;
1250 if (ep.getDescriptor() instanceof RelationDescriptor) {
1251 munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMRELATION);
1252 munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTORELATION);
1254 munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMSET);
1255 munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTOSET);
1257 int size=ep.rightSize();
1258 VarDescriptor sizevar=VarDescriptor.makeNew("size");
1259 ((OpExpr)expr).left.generate(cr, sizevar);
1260 VarDescriptor change=VarDescriptor.makeNew("change");
1261 cr.addDeclaration("int",change.getSafeSymbol());
1262 boolean generateadd=false;
1263 boolean generateremove=false;
1264 if (opcode==Opcode.GT) {
1265 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
1267 generateremove=false;
1268 } else if (opcode==Opcode.GE) {
1269 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1271 generateremove=false;
1272 } else if (opcode==Opcode.LT) {
1273 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size-1)+"-"+sizevar.getSafeSymbol()+";");
1275 generateremove=true;
1276 } else if (opcode==Opcode.LE) {
1277 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1279 generateremove=true;
1280 } else if (opcode==Opcode.EQ) {
1281 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1286 generateremove=true;
1287 } else if (opcode==Opcode.NE) {
1288 cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
1290 generateremove=false;
1292 throw new Error("Unrecognized Opcode");
1295 // In some cases the analysis has determined that generating removes
1297 if (generateremove&&munremove==null)
1298 generateremove=false;
1300 Descriptor d=ep.getDescriptor();
1301 if (generateremove) {
1302 cr.outputline("for(;"+change.getSafeSymbol()+"<0;"+change.getSafeSymbol()+"++)");
1304 /* Find element to remove */
1305 VarDescriptor leftvar=VarDescriptor.makeNew("leftvar");
1306 VarDescriptor rightvar=VarDescriptor.makeNew("rightvar");
1307 if (d instanceof RelationDescriptor) {
1308 if (ep.inverted()) {
1309 ((ImageSetExpr)((SizeofExpr)expr.left).setexpr).generate_leftside(cr,rightvar);
1310 cr.addDeclaration("int",leftvar.getSafeSymbol());
1311 cr.outputline("SimpleHashget("+d.getSafeSymbol()+"_hashinv,(int)"+rightvar.getSafeSymbol()+", &"+leftvar.getSafeSymbol()+");");
1313 ((ImageSetExpr)((SizeofExpr)expr.left).setexpr).generate_leftside(cr,leftvar);
1314 cr.addDeclaration("int",rightvar.getSafeSymbol());
1315 cr.outputline("SimpleHashget("+d.getSafeSymbol()+"_hash ,(int)"+leftvar.getSafeSymbol()+", &"+rightvar.getSafeSymbol()+");");
1318 cr.addDeclaration("int",leftvar.getSafeSymbol());
1319 cr.outputline(leftvar.getSafeSymbol()+"= SimpleHashfirstkey("+d.getSafeSymbol()+"_hash);");
1321 /* Generate abstract remove instruction */
1322 if (d instanceof RelationDescriptor) {
1323 RelationDescriptor rd=(RelationDescriptor) d;
1324 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1325 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1327 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1329 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv ,(int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1331 cr.outputline("SimpleHashremove("+d.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1333 /* Generate concrete remove instruction */
1334 for(int i=0;i<state.vRules.size();i++) {
1335 Rule r=(Rule)state.vRules.get(i);
1336 if (r.getInclusion().getTargetDescriptors().contains(d)) {
1337 for(int j=0;j<munremove.numUpdates();j++) {
1338 UpdateNode un=munremove.getUpdate(j);
1339 if (un.getRule()==r) {
1340 /* Update for rule rule r */
1341 String name=(String)updatenames.get(un);
1342 if (d instanceof RelationDescriptor) {
1343 cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
1345 cr.outputline("RepairHashaddset("+repairtable.getSafeSymbol()+","+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1354 // In some cases the analysis has determined that generating removes
1356 if (generateadd&&munadd==null)
1361 cr.outputline("for(;"+change.getSafeSymbol()+">0;"+change.getSafeSymbol()+"--)");
1363 VarDescriptor newobject=VarDescriptor.makeNew("newobject");
1364 if (d instanceof RelationDescriptor) {
1365 VarDescriptor otherside=VarDescriptor.makeNew("otherside");
1366 ((ImageSetExpr)((SizeofExpr)expr.left).setexpr).generate_leftside(cr,otherside);
1368 RelationDescriptor rd=(RelationDescriptor)d;
1369 if (termination.sources.relsetSource(rd,!ep.inverted())) {
1371 SetDescriptor sd=termination.sources.relgetSourceSet(rd,!ep.inverted());
1372 VarDescriptor iterator=VarDescriptor.makeNew("iterator");
1373 cr.addDeclaration(sd.getType().getGenerateType().getSafeSymbol(),newobject.getSafeSymbol());
1374 cr.addDeclaration("struct SimpleIterator",iterator.getSafeSymbol());
1375 cr.outputline("for(SimpleHashiterator("+sd.getSafeSymbol()+"_hash , &"+ iterator.getSafeSymbol() +"); hasNext(&"+iterator.getSafeSymbol()+");)");
1377 if (ep.inverted()) {
1378 cr.outputline("if (!SimpleHashcontainskeydata("+rd.getSafeSymbol()+"_hashinv,"+iterator.getSafeSymbol()+".key(),"+otherside.getSafeSymbol()+"))");
1380 cr.outputline("if (!SimpleHashcontainskeydata("+rd.getSafeSymbol()+"_hash, "+otherside.getSafeSymbol()+","+iterator.getSafeSymbol()+".key()))");
1382 cr.outputline(newobject.getSafeSymbol()+"=key(&"+iterator.getSafeSymbol()+");");
1383 cr.outputline("next(&"+iterator.getSafeSymbol()+");");
1385 } else if (termination.sources.relallocSource(rd,!ep.inverted())) {
1386 /* Allocation Source*/
1387 termination.sources.relgenerateSourceAlloc(cr,newobject,rd,!ep.inverted());
1388 } else throw new Error("No source for adding to Relation");
1389 if (ep.inverted()) {
1390 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1391 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1393 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, "+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1395 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, "+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1397 UpdateNode un=munadd.getUpdate(0);
1398 String name=(String)updatenames.get(un);
1399 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1401 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1402 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1404 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, "+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1406 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, "+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1407 UpdateNode un=munadd.getUpdate(0);
1408 String name=(String)updatenames.get(un);
1409 cr.outputline(name+"(thisvar, "+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1412 SetDescriptor sd=(SetDescriptor)d;
1413 if (termination.sources.setSource(sd)) {
1416 SetDescriptor sourcesd=termination.sources.getSourceSet(sd);
1417 VarDescriptor iterator=VarDescriptor.makeNew("iterator");
1418 cr.addDeclaration(sourcesd.getType().getGenerateType().getSafeSymbol(), newobject.getSafeSymbol());
1419 cr.addDeclaration("struct SimpleIterator",iterator.getSafeSymbol());
1420 cr.outputline("for(SimpleHashiterator("+sourcesd.getSafeSymbol()+"_hash, &"+iterator.getSafeSymbol()+"); hasNext(&"+iterator.getSafeSymbol()+");)");
1422 cr.outputline("if (!SimpleHashcontainskey("+sd.getSafeSymbol()+"_hash, key(&"+iterator.getSafeSymbol()+")))");
1423 cr.outputline(newobject.getSafeSymbol()+"=key(&"+iterator.getSafeSymbol()+");");
1424 cr.outputline("next(&"+iterator.getSafeSymbol()+");");
1426 } else if (termination.sources.allocSource(sd)) {
1427 /* Allocation Source*/
1428 termination.sources.generateSourceAlloc(cr,newobject,sd);
1429 } else throw new Error("No source for adding to Set");
1430 cr.outputline("SimpleHashadd("+sd.getSafeSymbol()+"_hash, "+newobject.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1431 UpdateNode un=munadd.getUpdate(0);
1432 String name=(String)updatenames.get(un);
1433 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1439 public void generateinclusionrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
1440 InclusionPredicate ip=(InclusionPredicate) dpred.getPredicate();
1441 boolean negated=dpred.isNegated();
1442 MultUpdateNode mun=getmultupdatenode(conj,dpred,-1);
1443 VarDescriptor leftvar=VarDescriptor.makeNew("left");
1444 ip.expr.generate(cr, leftvar);
1447 if (ip.setexpr instanceof ImageSetExpr) {
1448 ImageSetExpr ise=(ImageSetExpr) ip.setexpr;
1449 VarDescriptor rightvar=ise.getVar();
1450 boolean inverse=ise.inverted();
1451 RelationDescriptor rd=ise.getRelation();
1452 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1453 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1456 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1458 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1461 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash ,(int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1463 cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1465 for(int i=0;i<state.vRules.size();i++) {
1466 Rule r=(Rule)state.vRules.get(i);
1467 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1468 for(int j=0;j<mun.numUpdates();j++) {
1469 UpdateNode un=mun.getUpdate(j);
1470 if (un.getRule()==r) {
1471 /* Update for rule rule r */
1472 String name=(String)updatenames.get(un);
1474 cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1476 cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
1483 SetDescriptor sd=ip.setexpr.sd;
1484 cr.outputline("SimpleHashremove("+sd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1486 for(int i=0;i<state.vRules.size();i++) {
1487 Rule r=(Rule)state.vRules.get(i);
1488 if (r.getInclusion().getTargetDescriptors().contains(sd)) {
1489 for(int j=0;j<mun.numUpdates();j++) {
1490 UpdateNode un=mun.getUpdate(j);
1491 if (un.getRule()==r) {
1492 /* Update for rule rule r */
1493 String name=(String)updatenames.get(un);
1494 cr.outputline("RepairHashaddset("+repairtable.getSafeSymbol()+","+sd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1501 /* Generate update */
1502 if (ip.setexpr instanceof ImageSetExpr) {
1503 ImageSetExpr ise=(ImageSetExpr) ip.setexpr;
1504 VarDescriptor rightvar=ise.getVar();
1505 boolean inverse=ise.inverted();
1506 RelationDescriptor rd=ise.getRelation();
1507 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1508 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1511 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1513 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1516 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1518 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1520 UpdateNode un=mun.getUpdate(0);
1521 String name=(String)updatenames.get(un);
1523 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1525 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+");");
1528 SetDescriptor sd=ip.setexpr.sd;
1529 cr.outputline("SimpleHashadd("+sd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1531 UpdateNode un=mun.getUpdate(0);
1532 /* Update for rule rule r */
1533 String name=(String)updatenames.get(un);
1534 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1539 public static Vector getrulelist(Descriptor d) {
1540 Vector dispatchrules = new Vector();
1541 Vector rules = State.currentState.vRules;
1543 for (int i = 0; i < rules.size(); i++) {
1544 Rule rule = (Rule) rules.elementAt(i);
1545 Set requiredsymbols = rule.getRequiredDescriptors();
1547 // #TBD#: in general this is wrong because these descriptors may contain descriptors
1548 // bound in "in?" expressions which need to be dealt with in a topologically sorted
1551 if (rule.getRequiredDescriptors().contains(d)) {
1552 dispatchrules.addElement(rule);
1555 return dispatchrules;
1558 private boolean need_compensation(Rule r) {
1559 if (!Compiler.REPAIR)
1561 GraphNode gn=(GraphNode)termination.scopefalsify.get(r);
1562 for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1563 GraphNode.Edge edge=(GraphNode.Edge)edgeit.next();
1564 GraphNode gn2=edge.getTarget();
1565 if (!removed.contains(gn2)) {
1566 TermNode tn2=(TermNode)gn2.getOwner();
1567 if (tn2.getType()==TermNode.CONSEQUENCE)
1574 private UpdateNode find_compensation(Rule r) {
1575 GraphNode gn=(GraphNode)termination.scopefalsify.get(r);
1576 for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1577 GraphNode.Edge edge=(GraphNode.Edge)edgeit.next();
1578 GraphNode gn2=edge.getTarget();
1579 if (!removed.contains(gn2)) {
1580 TermNode tn2=(TermNode)gn2.getOwner();
1581 if (tn2.getType()==TermNode.UPDATE) {
1582 MultUpdateNode mun=tn2.getUpdate();
1583 for(int i=0;i<mun.numUpdates();i++) {
1584 UpdateNode un=mun.getUpdate(i);
1585 if (un.getRule()==r)
1591 throw new Error("No Compensation Update could be found");
1594 public void generate_dispatch(CodeWriter cr, RelationDescriptor rd, String leftvar, String rightvar) {
1595 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1596 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1598 if (!(usageinvimage||usageimage)) /* not used at all*/
1601 cr.outputline("/* RELATION DISPATCH */");
1602 if (Compiler.REPAIR) {
1603 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1605 cr.outputline("!SimpleHashcontainskeydata("+oldmodel.getSafeSymbol()+"->"+rd.getJustSafeSymbol() + "_hash, "+leftvar+","+rightvar+"))");
1607 cr.outputline("!SimpleHashcontainskeydata("+oldmodel.getSafeSymbol() +"->"+rd.getJustSafeSymbol()+"_hashinv, "+rightvar+","+leftvar+"))");
1610 /* Adding new item */
1611 /* Perform safety checks */
1612 cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
1613 cr.outputline("RepairHashcontainsrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+"))");
1615 /* Have update to call into */
1616 VarDescriptor mdfyptr=VarDescriptor.makeNew("modifyptr");
1617 VarDescriptor ismdfyptr=VarDescriptor.makeNew("ismodifyptr");
1618 cr.addDeclaration("int ",ismdfyptr.getSafeSymbol());
1619 cr.outputline(ismdfyptr.getSafeSymbol()+"=RepairHashismodify("+repairtable.getSafeSymbol()+","+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1625 for(int i=0;i<currentrule.numQuantifiers();i++) {
1626 if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1627 parttype=parttype+", int, int";
1629 parttype=parttype+", int";
1632 VarDescriptor tmpptr=VarDescriptor.makeNew("tempupdateptr");
1634 String methodcall="(thisvar,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
1635 for(int i=0;i<currentrule.numQuantifiers();i++) {
1636 Quantifier q=currentrule.getQuantifier(i);
1637 if (q instanceof SetQuantifier) {
1638 SetQuantifier sq=(SetQuantifier) q;
1639 methodcall+=","+sq.getVar().getSafeSymbol();
1640 } else if (q instanceof RelationQuantifier) {
1641 RelationQuantifier rq=(RelationQuantifier) q;
1642 methodcall+=","+rq.x.getSafeSymbol();
1643 methodcall+=","+rq.y.getSafeSymbol();
1644 } else if (q instanceof ForQuantifier) {
1645 ForQuantifier fq=(ForQuantifier) q;
1646 methodcall+=","+fq.getVar().getSafeSymbol();
1652 cr.addDeclaration("void *",tmpptr.getSafeSymbol());
1653 cr.outputline(tmpptr.getSafeSymbol()+"=");
1654 cr.outputline("(void *) RepairHashgetrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1655 cr.outputline("if ("+ismdfyptr.getSafeSymbol()+")");
1657 VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1658 String methodcallprefix="("+funptr.getSafeSymbol()+") ";
1660 cr.addDeclaration("int",mdfyptr.getSafeSymbol());
1661 cr.outputline(mdfyptr.getSafeSymbol()+"=RepairHashgetrelation2("+repairtable.getSafeSymbol()+","+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1662 cr.addDeclaration("void (*"+funptr.getSafeSymbol()+") (struct "+name+"_state *, struct "+name+"*, struct RepairHash *"+parttype+",int,int,int);");
1663 cr.outputline(funptr.getSafeSymbol()+"="+"(void (*) (struct "+name+"_state *, struct "+name+"*, struct RepairHash *"+parttype+",int,int,int)) "+tmpptr.getSafeSymbol()+";");
1664 cr.outputline(methodcallprefix+methodcall+","+leftvar+", "+rightvar+", "+mdfyptr.getSafeSymbol() +");");
1667 cr.outputline("else ");
1669 VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1670 String methodcallprefix="("+funptr.getSafeSymbol()+") ";
1672 cr.addDeclaration("void (*"+funptr.getSafeSymbol()+") (struct "+name+"_state *, struct "+name+"*,struct RepairHash *"+parttype+");");
1673 cr.outputline(funptr.getSafeSymbol()+"="+"(void (*) (struct "+name+"_state *,struct "+name+"*,struct RepairHash *"+parttype+")) "+tmpptr.getSafeSymbol()+";");
1674 cr.outputline(methodcallprefix+methodcall+");");
1677 cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1678 cr.outputline("goto rebuild;");
1682 /* Build standard compensation actions */
1683 if (need_compensation(currentrule)) {
1684 UpdateNode un=find_compensation(currentrule);
1685 String name=(String)updatenames.get(un);
1686 usedupdates.add(un); /* Mark as used */
1687 String methodcall=name+"(thisvar,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
1688 for(int i=0;i<currentrule.numQuantifiers();i++) {
1689 Quantifier q=currentrule.getQuantifier(i);
1690 if (q instanceof SetQuantifier) {
1691 SetQuantifier sq=(SetQuantifier) q;
1692 methodcall+=","+sq.getVar().getSafeSymbol();
1693 } else if (q instanceof RelationQuantifier) {
1694 RelationQuantifier rq=(RelationQuantifier) q;
1695 methodcall+=","+rq.x.getSafeSymbol();
1696 methodcall+=","+rq.y.getSafeSymbol();
1697 } else if (q instanceof ForQuantifier) {
1698 ForQuantifier fq=(ForQuantifier) q;
1699 methodcall+=","+fq.getVar().getSafeSymbol();
1703 cr.outputline(methodcall);
1704 cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1705 cr.outputline("goto rebuild;");
1711 String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1713 cr.addDeclaration("int" , addeditem);
1714 cr.outputline(addeditem + "=0;");
1716 String ifstring="if (!maybe";
1717 if (rd.getDomain().getType() instanceof StructureTypeDescriptor) {
1718 ifstring+="&&"+leftvar;
1721 if (rd.getRange().getType() instanceof StructureTypeDescriptor) {
1722 ifstring+="&&"+rightvar;
1727 if (rd.testUsage(RelationDescriptor.IMAGE)) {
1728 cr.outputline(ifstring);
1729 cr.outputline(addeditem + " = SimpleHashadd("+rd.getSafeSymbol()+"_hash, (int)" + leftvar + ", (int)" + rightvar+ ");");
1732 if (rd.testUsage(RelationDescriptor.INVIMAGE)) {
1733 cr.outputline(ifstring);
1734 cr.outputline(addeditem + " = SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, (int)" + rightvar + ", (int)" + leftvar + ");");
1738 Vector dispatchrules = getrulelist(rd);
1740 Set toremove=new HashSet();
1741 for(int i=0;i<dispatchrules.size();i++) {
1742 Rule r=(Rule)dispatchrules.get(i);
1743 if (!ruleset.contains(r))
1746 dispatchrules.removeAll(toremove);
1747 if (dispatchrules.size() == 0) {
1748 cr.outputline("/* nothing to dispatch */");
1753 cr.outputline("if (" + addeditem + ")");
1756 for(int i = 0; i < dispatchrules.size(); i++) {
1757 Rule rule = (Rule) dispatchrules.elementAt(i);
1758 if (rule.getGuardExpr().getRequiredDescriptors().contains(rd)) {
1759 /* Guard depends on this relation, so we recomput everything */
1760 cr.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+",-1,0,0);");
1762 for (int j=0;j<rule.numQuantifiers();j++) {
1763 Quantifier q=rule.getQuantifier(j);
1764 if (q.getRequiredDescriptors().contains(rd)) {
1766 cr.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+","+j+","+leftvar+","+rightvar+");");
1776 public void generate_dispatch(CodeWriter cr, SetDescriptor sd, String setvar) {
1777 cr.outputline("/* SET DISPATCH */");
1778 if (Compiler.REPAIR) {
1779 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1780 cr.outputline("!SimpleHashcontainskey("+oldmodel.getSafeSymbol() +"->"+sd.getJustSafeSymbol()+"_hash, "+setvar+"))");
1782 /* Adding new item */
1783 /* See if there is an outstanding update in the repairtable */
1784 cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
1785 cr.outputline("RepairHashcontainsset("+repairtable.getSafeSymbol()+","+sd.getNum()+","+currentrule.getNum()+","+setvar+"))");
1787 /* Have update to call into */
1788 VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1790 for(int i=0;i<currentrule.numQuantifiers();i++) {
1791 if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1792 parttype=parttype+", int, int";
1794 parttype=parttype+", int";
1796 cr.addDeclaration("void (*"+funptr.getSafeSymbol()+") (struct "+name+"_state *,struct "+name+"*,struct RepairHash *"+parttype+");");
1797 cr.outputline(funptr.getSafeSymbol()+"=");
1798 cr.outputline("(void (*) (struct "+name+"_state *,struct "+name+"*,struct RepairHash *"+parttype+")) RepairHashgetset("+repairtable.getSafeSymbol()+","+sd.getNum()+","+currentrule.getNum()+","+setvar+");");
1799 String methodcall="("+funptr.getSafeSymbol()+") (thisvar,"+oldmodel.getSafeSymbol()+","+
1800 repairtable.getSafeSymbol();
1801 for(int i=0;i<currentrule.numQuantifiers();i++) {
1802 Quantifier q=currentrule.getQuantifier(i);
1803 if (q instanceof SetQuantifier) {
1804 SetQuantifier sq=(SetQuantifier) q;
1805 methodcall+=","+sq.getVar().getSafeSymbol();
1806 } else if (q instanceof RelationQuantifier) {
1807 RelationQuantifier rq=(RelationQuantifier) q;
1808 methodcall+=","+rq.x.getSafeSymbol();
1809 methodcall+=","+rq.y.getSafeSymbol();
1810 } else if (q instanceof ForQuantifier) {
1811 ForQuantifier fq=(ForQuantifier) q;
1812 methodcall+=","+fq.getVar().getSafeSymbol();
1816 cr.outputline(methodcall);
1817 cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1818 cr.outputline("goto rebuild;");
1821 /* Build standard compensation actions */
1822 Vector ruleset=new Vector();
1823 ruleset.add(currentrule);
1824 if (state.implicitruleinv.containsKey(currentrule))
1825 ruleset.addAll((Set)state.implicitruleinv.get(currentrule));
1826 for(int i=0;i<ruleset.size();i++) {
1827 Rule itrule=(Rule)ruleset.get(i);
1829 if (need_compensation(itrule)) {
1830 UpdateNode un=find_compensation(itrule);
1831 String name=(String)updatenames.get(un);
1832 usedupdates.add(un); /* Mark as used */
1834 String methodcall=name+"(thisvar,"+oldmodel.getSafeSymbol()+","+
1835 repairtable.getSafeSymbol();
1836 for(int j=0;j<currentrule.numQuantifiers();j++) {
1837 Quantifier q=currentrule.getQuantifier(j);
1838 if (q instanceof SetQuantifier) {
1839 SetQuantifier sq=(SetQuantifier) q;
1840 methodcall+=","+sq.getVar().getSafeSymbol();
1841 } else if (q instanceof RelationQuantifier) {
1842 RelationQuantifier rq=(RelationQuantifier) q;
1843 methodcall+=","+rq.x.getSafeSymbol();
1844 methodcall+=","+rq.y.getSafeSymbol();
1845 } else if (q instanceof ForQuantifier) {
1846 ForQuantifier fq=(ForQuantifier) q;
1847 methodcall+=","+fq.getVar().getSafeSymbol();
1851 if (currentrule!=itrule) {
1852 SetDescriptor sdrule=((SetInclusion)itrule.getInclusion()).getSet();
1853 cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1854 cr.outputline("!SimpleHashcontainskey("+ oldmodel.getSafeSymbol() +"->"+sdrule.getJustSafeSymbol() +"_hash,"+setvar+"))");
1857 cr.outputline(methodcall);
1858 cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1859 cr.outputline("goto rebuild;");
1862 if (currentrule==itrule)
1869 String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1870 cr.addDeclaration("int", addeditem);
1871 cr.outputline(addeditem + " = 0;");
1872 if (sd.getType() instanceof StructureTypeDescriptor) {
1873 cr.outputline("if (!maybe&&"+setvar+")");
1875 cr.outputline("if (!maybe)");
1876 cr.outputline(addeditem + " = SimpleHashadd("+sd.getSafeSymbol()+"_hash, (int)" + setvar + ", (int)" + setvar + ");");
1878 Vector dispatchrules = getrulelist(sd);
1880 Set toremove=new HashSet();
1881 for(int i=0;i<dispatchrules.size();i++) {
1882 Rule r=(Rule)dispatchrules.get(i);
1883 if (!ruleset.contains(r))
1886 dispatchrules.removeAll(toremove);
1888 if (dispatchrules.size() == 0) {
1889 cr.outputline("/* nothing to dispatch */");
1894 /* Add item to worklist if new */
1895 cr.outputline("if ("+addeditem+")");
1897 for(int i = 0; i < dispatchrules.size(); i++) {
1898 Rule rule = (Rule) dispatchrules.elementAt(i);
1899 if (SetDescriptor.expand(rule.getGuardExpr().getRequiredDescriptors()).contains(sd)) {
1900 /* Guard depends on this relation, so we recompute everything */
1901 cr.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+",-1,0,0);");
1903 for (int j=0;j<rule.numQuantifiers();j++) {
1904 Quantifier q=rule.getQuantifier(j);
1905 if (SetDescriptor.expand(q.getRequiredDescriptors()).contains(sd)) {
1907 cr.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+","+j+","+setvar+",0);");