Hack to fix naming issues.
[repair.git] / Repair / RepairCompiler / MCC / IR / RepairGenerator.java
1 package MCC.IR;
2
3 import java.io.*;
4 import java.util.*;
5 import MCC.State;
6 import MCC.Compiler;
7
8 public class RepairGenerator {
9     State state;
10     PrintWrapper outputrepair = null;
11     PrintWrapper outputaux = null;
12     PrintWrapper outputhead = null;
13     String name="foo";
14     String headername;
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;
22     HashSet usedupdates;
23     Termination termination;
24     Set removed;
25     HashSet togenerate;
26     static boolean DEBUG=false;
27     Cost cost;
28     ModelRuleDependence mrd;
29
30     public RepairGenerator(State state, Termination t) {
31         this.state = state;
32         updatenames=new Hashtable();
33         usedupdates=new HashSet();
34         termination=t;
35         removed=t.removedset;
36         togenerate=new HashSet();
37         togenerate.addAll(termination.conjunctions);
38         if (Compiler.REPAIR)
39             togenerate.removeAll(removed);
40         GraphNode.computeclosure(togenerate,removed);
41         cost=new Cost();
42         mrd=ModelRuleDependence.doAnalysis(state);
43         Repair.repairgenerator=this;
44     }
45
46     private void generatetypechecks(boolean flag) {
47         if (flag) {
48             DotExpr.DOTYPECHECKS=true;
49             VarExpr.DOTYPECHECKS=true;
50             DotExpr.DONULL=true;
51             VarExpr.DONULL=true;
52         } else {
53             VarExpr.DOTYPECHECKS=false;
54             DotExpr.DOTYPECHECKS=false;
55             VarExpr.DONULL=true;
56             DotExpr.DONULL=true;
57         }
58     }
59
60     private void name_updates() {
61         int count=0;
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);
71             }
72         }
73     }
74
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));
79
80         headername=st;
81         name_updates();
82         generatetypechecks(true);
83         generate_tokentable();
84         RelationDescriptor.prefix = "thisvar->";
85         SetDescriptor.prefix = "thisvar->";
86
87         generate_hashtables();
88         generate_stateobject();
89
90
91         /* Rewrite globals */
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());
96         }
97
98
99         generate_call();
100         generate_start();
101         generate_rules();
102         if (!Compiler.REPAIR||Compiler.GENERATEDEBUGPRINT) {
103             generate_print();
104         }
105         generate_checks();
106         generate_teardown();
107         CodeWriter crhead = new StandardCodeWriter(this.outputhead);
108         craux = new StandardCodeWriter(this.outputaux);
109         craux.emptyBuffer();
110         craux.endblock();
111
112         if (Compiler.GENERATEDEBUGHOOKS) {
113             crhead.outputline("void debughook();");
114             craux.outputline("void debughook() {}");
115         }
116         generatetypechecks(false);
117         generate_computesizes();
118         generatetypechecks(true);
119         generate_recomputesizes();
120         generatetypechecks(false);
121         generate_updates();
122         StructureGenerator sg=new StructureGenerator(state,this);
123         sg.buildall();
124         crhead.outputline("#endif");
125     }
126
127     String ststate="state";
128     String stmodel="model";
129     String strepairtable="repairtable";
130     String stleft="left";
131     String stright="right";
132     String stnew="newvalue";
133
134     private void generate_updates() {
135         int count=0;
136         CodeWriter crhead = new StandardCodeWriter(outputhead);
137         CodeWriter craux = new StandardCodeWriter(outputaux);
138         RelationDescriptor.prefix = "model->";
139         SetDescriptor.prefix = "model->";
140
141         /* Rewrite globals */
142
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());
147         }
148
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);
158
159                 switch(mun.op) {
160                 case MultUpdateNode.ADD:
161                     if (isrelation) {
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+")");
164                     } else {
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+")");
167                     }
168                     craux.startblock();
169                     craux.startBuffer();
170                     craux.addDeclaration("int","maybe");
171                     craux.outputline("maybe=0;");
172                     if (Compiler.GENERATEINSTRUMENT)
173                         craux.outputline("updatecount++;");
174
175                     final SymbolTable st = un.getRule().getSymbolTable();
176                     CodeWriter cr = new StandardCodeWriter(outputaux) {
177                         public SymbolTable getSymbolTable() { return st; }
178                     };
179                     un.generate(cr, false, false, stleft,stright, null,this);
180                     craux.outputline("if (maybe) printf(\"REALLY BAD\");");
181                     craux.emptyBuffer();
182                     craux.endblock();
183                     break;
184                 case MultUpdateNode.REMOVE: {
185                     Rule r=un.getRule();
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;
194
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();
200                         }
201                     }
202                     methodcall+=")";
203                     crhead.outputline(methodcall+";");
204                     craux.outputline(methodcall);
205                     craux.startblock();
206                     craux.startBuffer();
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; }
214                     };
215                     un.generate(cr2, true, false, null,null, null,this);
216                     craux.outputline("if (maybe) printf(\"REALLY BAD\");");
217                     craux.emptyBuffer();
218                     craux.endblock();
219                 }
220                     break;
221                 case MultUpdateNode.MODIFY: {
222                     Rule r=un.getRule();
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;
231
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();
237                         }
238                     }
239                     methodcall+=", int "+stleft+", int "+stright+", int "+stnew;
240                     methodcall+=")";
241                     crhead.outputline(methodcall+";");
242                     craux.outputline(methodcall);
243                     craux.startblock();
244                     craux.startBuffer();
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; }
251                     };
252                     un.generate(cr2, false, true, stleft, stright, stnew, this);
253                     craux.outputline("if (maybe) printf(\"REALLY BAD\");");
254                     craux.emptyBuffer();
255                     craux.endblock();
256                 }
257                     break;
258
259                 default:
260                     throw new Error("Nonimplement Update");
261                 }
262             }
263         }
264     }
265
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()+";");
275         }
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()+";");
282         }
283         cr.outputline("free"+name+"_state("+vdstate.getSafeSymbol()+");");
284     }
285
286     private void generate_tokentable() {
287         CodeWriter cr = new StandardCodeWriter(outputrepair);
288         Iterator tokens = TokenLiteralExpr.tokens.keySet().iterator();
289
290         cr.outputline("");
291         cr.outputline("/* Token values*/");
292         cr.outputline("");
293
294         while (tokens.hasNext()) {
295             Object token = tokens.next();
296             cr.outputline("/* " + token.toString() + " = " + TokenLiteralExpr.tokens.get(token).toString()+"*/");
297         }
298
299         cr.outputline("");
300         cr.outputline("");
301     }
302
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()+";");
311         }
312         crhead.outputline("};");
313         crhead.outputline("struct "+name+"_state * allocate"+name+"_state();");
314         craux.outputline("struct "+name+"_state * allocate"+name+"_state()");
315         craux.startblock();
316         craux.outputline("return (struct "+name+"_state *) malloc(sizeof(struct "+name+"_state));");
317         craux.endblock();
318
319         crhead.outputline("void free"+name+"_state(struct "+name+"_state *);");
320         craux.outputline("void free"+name+"_state(struct "+name+"_state * thisvar)");
321         craux.startblock();
322         craux.outputline("free(thisvar);");
323         craux.endblock();
324
325         crhead.outputline("void "+name+"_statecomputesizes(struct "+name+"_state * ,int *,int **);");
326         crhead.outputline("void "+name+"_staterecomputesizes(struct "+name+"_state *);");
327     }
328
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;
335         }
336         final SymbolTable st = state.stGlobals;
337         CodeWriter cr = new StandardCodeWriter(outputaux) {
338                 public SymbolTable getSymbolTable() { return st; }
339             };
340
341         cr.outputline("void "+name+"_statecomputesizes(struct "+name+"_state * thisvar,int *sizearray,int **numele)");
342         cr.startblock();
343         cr.startBuffer();
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()+";");
352         }
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()+";");
365                     }
366                 }
367             }
368         }
369         cr.outputline("if (maybe) printf(\"BAD ERROR\");");
370         cr.emptyBuffer();
371         cr.endblock();
372     }
373
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;
380         }
381         final SymbolTable st = state.stGlobals;
382         CodeWriter cr = new StandardCodeWriter(outputaux) {
383                 public SymbolTable getSymbolTable() { return st; }
384             };
385         cr.outputline("void "+name+"_staterecomputesizes(struct "+name+"_state * thisvar)");
386         cr.startblock();
387         cr.startBuffer();
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);
395         }
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);
407                     }
408                 }
409             }
410         }
411         cr.outputline("if (maybe) printf(\"BAD ERROR\");");
412         cr.emptyBuffer();
413         cr.endblock();
414     }
415
416
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\"");
431         if (Compiler.TIME) {
432             craux.outputline("#include <sys/time.h>");
433         }
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+";");
445                         }
446                     }
447                 }
448             }
449         }
450         craux.outputline("struct "+ name+"* allocate"+name+"()");
451         craux.startblock();
452         craux.outputline("/* creating hashtables */");
453
454         /* build sets */
455         Iterator sets = state.stSets.descriptors();
456         craux.addDeclaration("struct "+name+"*", "thisvar");
457         craux.outputline("thisvar=(struct "+name+"*) malloc(sizeof(struct "+name+"));");
458
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();");
464         }
465
466         /* second pass build relationships between hashtables */
467         sets = state.stSets.descriptors();
468
469         while (sets.hasNext()) {
470             SetDescriptor set = (SetDescriptor) sets.next();
471             Iterator subsets = set.subsets();
472
473             while (subsets.hasNext()) {
474                 SetDescriptor subset = (SetDescriptor) subsets.next();
475                 craux.outputline("SimpleHashaddParent("+subset.getSafeSymbol() +"_hash ,"+ set.getSafeSymbol() + "_hash);");
476             }
477         }
478
479         /* build relations */
480         Iterator relations = state.stRelations.descriptors();
481
482         /* first pass create all the hash tables */
483         while (relations.hasNext()) {
484             RelationDescriptor relation = (RelationDescriptor) relations.next();
485
486             if (relation.testUsage(RelationDescriptor.IMAGE)) {
487                 crhead.outputline("struct SimpleHash* " + relation.getJustSafeSymbol() + "_hash;");
488                 craux.outputline(relation.getSafeSymbol() + "_hash = noargallocateSimpleHash();");
489             }
490
491             if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
492                 crhead.outputline("struct SimpleHash* " + relation.getJustSafeSymbol() + "_hashinv;");
493                 craux.outputline(relation.getSafeSymbol() + "_hashinv = noargallocateSimpleHash();");
494             }
495         }
496         craux.outputline("return thisvar;");
497         craux.endblock();
498         crhead.outputline("};");
499         craux.outputline("void free"+name+"(struct "+ name +"* thisvar)");
500         craux.startblock();
501         craux.outputline("/* deleting hashtables */");
502
503         /* build destructor */
504         sets = state.stSets.descriptors();
505
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);");
510         }
511
512         /* destroy relations */
513         relations = state.stRelations.descriptors();
514
515         /* first pass create all the hash tables */
516         while (relations.hasNext()) {
517             RelationDescriptor relation = (RelationDescriptor) relations.next();
518
519             if (relation.testUsage(RelationDescriptor.IMAGE)) {
520                 craux.outputline("freeSimpleHash("+relation.getSafeSymbol() + "_hash);");
521             }
522
523             if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
524                 craux.outputline("freeSimpleHash(" + relation.getSafeSymbol() + "_hashinv);");
525             }
526         }
527         craux.outputline("free(thisvar);");
528         craux.endblock();
529     }
530
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");
539
540         if (Compiler.GENERATEINSTRUMENT) {
541             craux.outputline("int updatecount;");
542             craux.outputline("int rebuildcount;");
543             craux.outputline("int abstractcount;");
544         }
545
546         crhead.outputline("void doanalysis(struct "+name+"_state *);");
547         craux.outputline("void doanalysis(struct "+name+"_state * thisvar)");
548         craux.startblock();
549         craux.outputline("int highmark;"); /* This declaration is special...need it to be first */
550         craux.startBuffer();
551
552         if (Compiler.TIME) {
553             craux.outputline("struct timeval _begin_time,_end_time;");
554             craux.outputline("gettimeofday(&_begin_time,NULL);");
555         }
556         if (Compiler.GENERATEINSTRUMENT) {
557             craux.outputline("updatecount=0;");
558             craux.outputline("rebuildcount=0;");
559             craux.outputline("abstractcount=0;");
560         }
561
562
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)");
573         craux.startblock();
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++;");
579     }
580
581     private void generate_teardown() {
582         CodeWriter cr = new StandardCodeWriter(outputaux);
583         cr.endblock();
584         if (Compiler.TIME) {
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);");
587         }
588
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);");
593         }
594
595     }
596
597     private void generate_print() {
598
599         final SymbolTable st = new SymbolTable();
600
601         CodeWriter cr = new StandardCodeWriter(outputaux) {
602                 public SymbolTable getSymbolTable() { return st; }
603             };
604
605         cr.outputline("/* printing sets!*/");
606         cr.outputline("printf(\"\\n\\nPRINTING SETS AND RELATIONS\\n\");");
607
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")) {
612                 continue;
613             }
614
615             String setname = sd.getSafeSymbol();
616
617             cr.startblock();
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))");
623             cr.startblock();
624             cr.addDeclaration("int","__setval");
625             cr.outputline("__setval = (int) next(&__setiterator);");
626
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);");
634             }
635
636
637             cr.endblock();
638             cr.endblock();
639         }
640
641         Iterator reliterator = state.stRelations.descriptors();
642         while (reliterator.hasNext()) {
643             RelationDescriptor rd = (RelationDescriptor) reliterator.next();
644
645
646             String relname = rd.getSafeSymbol();
647             if (rd.testUsage(RelationDescriptor.IMAGE)) {
648                 cr.startblock();
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))");
654                 cr.startblock();
655                 cr.addDeclaration("int","__relval");
656                 cr.addDeclaration("int","__relval2");
657                 cr.outputline("__relval2 = (int) key(&__reliterator);");
658                 cr.outputline("__relval = (int) next(&__reliterator);");
659
660                 cr.outputline("printf(\"<%ld,%ld> \", __relval2,__relval);");
661
662                 cr.endblock();
663                 cr.endblock();
664             } else if (rd.testUsage(RelationDescriptor.INVIMAGE)) {
665                 cr.startblock();
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))");
671                 cr.startblock();
672                 cr.addDeclaration("int","__relval");
673                 cr.addDeclaration("int","__relval2");
674                 cr.outputline("__relval2 = (int) key(&__reliterator);");
675                 cr.outputline("__relval = (int) next(&__reliterator);");
676
677
678                 cr.outputline("printf(\"<%ld,%ld> \", __relval,__relval2);");
679
680                 cr.endblock();
681                 cr.endblock();
682             }
683
684         }
685
686         cr.outputline("printf(\"\\n\\n------------------- END PRINTING\\n\");");
687     }
688
689     Set ruleset=null;
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);
698
699             if (!needworklist) {
700                 Iterator iterator_rs = ruleset.iterator();
701                 while (iterator_rs.hasNext()) {
702                     Rule rule = (Rule) iterator_rs.next();
703                     if (rule.getnogenerate())
704                         continue;
705                     {
706                         final SymbolTable st = rule.getSymbolTable();
707                         CodeWriter cr = new StandardCodeWriter(outputaux) {
708                                 public SymbolTable getSymbolTable() { return st; }
709                             };
710                         InvariantValue ivalue=new InvariantValue();
711                         cr.setInvariantValue(ivalue);
712
713                         cr.outputline("/* build " +escape(rule.toString())+"*/");
714                         cr.startblock();
715                         cr.addDeclaration("int","maybe");
716                         cr.outputline("maybe=0;");
717
718                         Expr ruleexpr=rule.getGuardExpr();
719                         HashSet invariantvars=new HashSet();
720                         Set invariants=ruleexpr.findInvariants(invariantvars);
721
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));
729                             }
730                         }
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));
738                             }
739                         }
740
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);
751                             openparencount++;
752                             cr.startblock();
753                         }
754                         quantifiers = rule.quantifiers();
755                         while (quantifiers.hasNext()) {
756                             Quantifier quantifier = (Quantifier) quantifiers.next();
757                             quantifier.generate_open(cr);
758                         }
759
760                         /* pretty print! */
761                         cr.output("/*");
762                         rule.getGuardExpr().prettyPrint(cr);
763                         cr.outputline("*/");
764
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() + ")");
769                         cr.startblock();
770
771                         /* now we have to generate the inclusion code */
772                         currentrule=rule;
773                         rule.getInclusion().generate(cr);
774                         cr.endblock();
775                         while (quantifiers.hasPrevious()) {
776                             Quantifier quantifier = (Quantifier) quantifiers.previous();
777                             cr.endblock();
778                         }
779                         cr.endblock();
780                         while((openparencount--)>0)
781                             cr.endblock();
782                         cr.outputline("");
783                         cr.outputline("");
784                     }
785                 }
786             } else {
787                 CodeWriter cr2 = new StandardCodeWriter(outputaux);
788
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);");
793                 }
794
795                 cr2.outputline("while (WorkListhasMoreElements("+worklist.getSafeSymbol()+"))");
796                 cr2.startblock();
797                 VarDescriptor idvar=VarDescriptor.makeNew("id");
798                 cr2.addDeclaration("int ",idvar.getSafeSymbol());
799                 cr2.outputline(idvar.getSafeSymbol()+"=WorkListgetid("+worklist.getSafeSymbol()+");");
800
801                 String elseladder = "if";
802
803                 Iterator iterator_rules = ruleset.iterator();
804                 while (iterator_rules.hasNext()) {
805
806                     Rule rule = (Rule) iterator_rules.next();
807                     int dispatchid = rule.getNum();
808
809                     {
810                         final SymbolTable st = rule.getSymbolTable();
811                         CodeWriter cr = new StandardCodeWriter(outputaux) {
812                                 public SymbolTable getSymbolTable() { return st; }
813                             };
814
815                         cr.indent();
816                         cr.outputline(elseladder + " ("+idvar.getSafeSymbol()+" == " + dispatchid + ")");
817                         cr.startblock();
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())+"*/");
830
831
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());
835                         }
836
837                         /* pretty print! */
838                         cr.output("/*");
839
840                         rule.getGuardExpr().prettyPrint(cr);
841                         cr.outputline("*/");
842
843                         /* now we have to generate the guard test */
844
845                         VarDescriptor guardval = VarDescriptor.makeNew();
846                         rule.getGuardExpr().generate(cr, guardval);
847
848                         cr.outputline("if (" + guardval.getSafeSymbol() + ")");
849                         cr.startblock();
850
851                         /* now we have to generate the inclusion code */
852                         currentrule=rule;
853                         rule.getInclusion().generate(cr);
854                         cr.endblock();
855
856                         for (int j=0;j<rule.numQuantifiers();j++) {
857                             cr.endblock();
858                         }
859
860                         // close startblocks generated by DotExpr memory checks
861                         //DotExpr.generate_memory_endblocks(cr);
862
863                         cr.endblock(); // end else-if WORKLIST ladder
864
865                         elseladder = "else if";
866                     }
867                 }
868                 cr2.outputline("else");
869                 cr2.startblock();
870                 cr2.outputline("printf(\"VERY BAD !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\\n\\n\");");
871                 cr2.outputline("exit(1);");
872                 cr2.endblock();
873                 // end block created for worklist
874                 cr2.outputline("WorkListpop("+worklist.getSafeSymbol()+");");
875                 cr2.endblock();
876             }
877         }
878     }
879
880     public static String escape(String s) {
881         String newstring="";
882         for(int i=0;i<s.length();i++) {
883             char c=s.charAt(i);
884             if (c=='"')
885                 newstring+="\"";
886             else
887                 newstring+=c;
888         }
889         return newstring;
890     }
891
892     private void generate_checks() {
893         /* do constraint checks */
894         Iterator i;
895         if (Compiler.REPAIR)
896             i=termination.constraintdependence.computeOrdering().iterator();
897         else
898             i=state.vConstraints.iterator();
899         for (; i.hasNext();) {
900             Constraint constraint;
901             if (Compiler.REPAIR)
902                 constraint= (Constraint) ((GraphNode)i.next()).getOwner();
903             else
904                 constraint=(Constraint)i.next();
905
906             {
907                 final SymbolTable st = constraint.getSymbolTable();
908                 CodeWriter cr = new StandardCodeWriter(outputaux);
909                 cr.pushSymbolTable(constraint.getSymbolTable());
910
911                 cr.outputline("/* checking " + escape(constraint.toString())+"*/");
912                 cr.startblock();
913
914                 ListIterator quantifiers = constraint.quantifiers();
915
916                 while (quantifiers.hasNext()) {
917                     Quantifier quantifier = (Quantifier) quantifiers.next();
918                     quantifier.generate_open(cr);
919                 }
920
921                 cr.addDeclaration("int","maybe");
922                 cr.outputline("maybe = 0;");
923
924                 /* now we have to generate the guard test */
925
926                 VarDescriptor constraintboolean = VarDescriptor.makeNew("constraintboolean");
927                 constraint.getLogicStatement().generate(cr, constraintboolean);
928
929                 cr.outputline("if (maybe)");
930                 cr.startblock();
931                 cr.outputline("printf(\"maybe fail " +  escape(constraint.toString()) + ". \\n\");");
932                 //cr.outputline("exit(1);");
933                 cr.endblock();
934
935                 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
936                 cr.startblock();
937                 if (!Compiler.REPAIR||Compiler.GENERATEDEBUGHOOKS)
938                     cr.outputline("printf(\"fail " + escape(constraint.toString()) + ". \\n\");");
939
940                 if (Compiler.REPAIR) {
941                 /* Do repairs */
942                 /* Build new repair table */
943
944                 cr.outputline("if ("+repairtable.getSafeSymbol()+")");
945                 cr.outputline("freeRepairHash("+repairtable.getSafeSymbol()+");");
946                 cr.outputline(repairtable.getSafeSymbol()+"=noargallocateRepairHash();");
947
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));
955
956                 if (dnfconst.size()<=1) {
957                     cr.addDeclaration("int",mincostindex.getSafeSymbol());
958                     cr.outputline(mincostindex.getSafeSymbol()+"=0;");
959                 }
960                 if (dnfconst.size()>1) {
961                     cr.addDeclaration("int",mincostindex.getSafeSymbol());
962                     boolean first=true;
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))
967                             continue;
968
969                         VarDescriptor costvar;
970                         if (first) {
971                             costvar=mincost;
972                         } else
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);
980                             if (k==0) {
981                                 cr.addDeclaration("int",costvar.getSafeSymbol());
982                                 cr.outputline(costvar.getSafeSymbol()+"=0;");
983                             }
984                             if (negate)
985                                 cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
986                             else
987                                 cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
988                             cr.outputline(costvar.getSafeSymbol()+"+="+cost.getCost(dpred)+";");
989                         }
990
991                         if(!first) {
992                             cr.outputline("if ("+costvar.getSafeSymbol()+"<"+mincost.getSafeSymbol()+")");
993                             cr.startblock();
994                             cr.outputline(mincost.getSafeSymbol()+"="+costvar.getSafeSymbol()+";");
995                             cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
996                             cr.endblock();
997                         } else
998                             cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
999                         first=false;
1000                     }
1001                 }
1002                 cr.outputline("switch("+mincostindex.getSafeSymbol()+")");
1003                 cr.startblock();
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))
1008                         continue;
1009                     cr.outputline("case "+j+":");
1010                     cr.startblock();
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);
1017                         if (negate)
1018                             cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
1019                         else
1020                             cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
1021                         cr.startblock();
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");
1033                         cr.endblock();
1034                     }
1035                     /* Update model */
1036                     cr.endblock();
1037                     cr.outputline("break;");
1038                 }
1039                 cr.endblock();
1040
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 */
1045                 }
1046                 cr.endblock();
1047
1048                 while (quantifiers.hasPrevious()) {
1049                     Quantifier quantifier = (Quantifier) quantifiers.previous();
1050                     cr.endblock();
1051                 }
1052                 cr.endblock();
1053                 cr.outputline("");
1054                 cr.outputline("");
1055             }
1056         }
1057         CodeWriter cr = new StandardCodeWriter(outputaux);
1058         cr.startblock();
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;");
1067         cr.endblock();
1068         cr.outputline("rebuild:");
1069         cr.outputline(";");
1070     }
1071
1072     private MultUpdateNode getmultupdatenode(Conjunction conj, DNFPredicate dpred, int repairtype) {
1073         Set nodes=getmultupdatenodeset(conj,dpred,repairtype);
1074         Iterator it=nodes.iterator();
1075         if (it.hasNext())
1076             return (MultUpdateNode)it.next();
1077         else
1078             return null;
1079     }
1080
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());
1097                             }
1098                         }
1099                     }
1100                 }
1101             }
1102         }
1103         return hs;
1104     }
1105
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) {
1117                     return ar;
1118                 }
1119             }
1120         }
1121         return null;
1122     }
1123
1124
1125     /** Generates abstract (and concrete) repair for a comparison */
1126
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) {
1136                 munadd=mun;
1137             } else if (mun.getType()==MultUpdateNode.REMOVE) {
1138                 munremove=mun;
1139             } else if (mun.getType()==MultUpdateNode.MODIFY) {
1140                 munmodify=mun;
1141             }
1142         }
1143
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);
1156
1157         if (needremoveloop&&((munadd==null)||(munremove==null))) {
1158             System.out.println("Warning:  need to have individual remove operations for"+dpred.name());
1159             needremoveloop=false;
1160         }
1161         if (needremoveloop) {
1162             cr.outputline("while (1)");
1163             cr.startblock();
1164         }
1165         if (!inverted) {
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()+");");
1170         } else {
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()+");");
1175         }
1176
1177         opcode=Opcode.translateOpcode(negated,opcode);
1178
1179         if (opcode==Opcode.GT) {
1180             cr.outputline(newvalue.getSafeSymbol()+"++;");
1181         } else if (opcode==Opcode.GE) {
1182             /* Equal */
1183         } else if (opcode==Opcode.LT) {
1184             cr.outputline(newvalue.getSafeSymbol()+"--;");
1185         } else if (opcode==Opcode.LE) {
1186             /* Equal */
1187         } else if (opcode==Opcode.EQ) {
1188             /* Equal */
1189         } else if (opcode==Opcode.NE) { /* search for FLAGNE if this is changed*/
1190             cr.outputline(newvalue.getSafeSymbol()+"++;");
1191         } else {
1192             throw new Error("Unrecognized Opcode");
1193         }
1194         /* Do abstract repairs */
1195         if (usageimage) {
1196             cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash, "+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1197         }
1198         if (usageinvimage) {
1199             cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv, "+rightside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
1200         }
1201
1202         if (needremoveloop) {
1203             if (!inverted) {
1204                 cr.outputline("if (SimpleHashcontainskey("+rd.getSafeSymbol()+"_hash, "+leftside.getSafeSymbol()+"))");
1205                 cr.startblock();
1206             } else {
1207                 cr.outputline("if (SimpleHashcontainskey("+rd.getSafeSymbol()+"_hashinv, "+rightside.getSafeSymbol()+"))");
1208                 cr.startblock();
1209             }
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+");");
1219                         }
1220                     }
1221                 }
1222             }
1223             cr.outputline("continue;");
1224             cr.endblock();
1225         }
1226
1227         if (usageimage) {
1228             if (!inverted) {
1229                 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash,"+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1230             } else {
1231                 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, "+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1232             }
1233         }
1234         if (usageinvimage) {
1235             if (!inverted) {
1236                 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, "+newvalue.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
1237             } else {
1238                 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv,"+rightside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1239             }
1240         }
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()+");");
1252                         }
1253                     }
1254                 }
1255             }
1256         } else {
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+");");
1268                             }
1269                         }
1270                     }
1271                 }
1272             /* Now do addition */
1273             UpdateNode un=munadd.getUpdate(0);
1274             String name=(String)updatenames.get(un);
1275             if (!inverted) {
1276                 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1277             } else {
1278                 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1279             }
1280         }
1281         if (needremoveloop) {
1282             cr.outputline("break;");
1283             cr.endblock();
1284         }
1285     }
1286
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);
1292
1293         MultUpdateNode munremove;
1294
1295         MultUpdateNode munadd;
1296         if (ep.getDescriptor() instanceof RelationDescriptor) {
1297             munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMRELATION);
1298             munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTORELATION);
1299         } else {
1300             munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMSET);
1301             munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTOSET);
1302         }
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()+";");
1312             generateadd=true;
1313             generateremove=false;
1314         } else if (opcode==Opcode.GE) {
1315             cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1316             generateadd=true;
1317             generateremove=false;
1318         } else if (opcode==Opcode.LT) {
1319             cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size-1)+"-"+sizevar.getSafeSymbol()+";");
1320             generateadd=false;
1321             generateremove=true;
1322         } else if (opcode==Opcode.LE) {
1323             cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1324             generateadd=false;
1325             generateremove=true;
1326         } else if (opcode==Opcode.EQ) {
1327             cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1328             if (size==0)
1329                 generateadd=false;
1330             else
1331                 generateadd=true;
1332             generateremove=true;
1333         } else if (opcode==Opcode.NE) {
1334             cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
1335             generateadd=true;
1336             generateremove=false;
1337         } else {
1338             throw new Error("Unrecognized Opcode");
1339         }
1340
1341 // In some cases the analysis has determined that generating removes
1342 // is unnecessary
1343         if (generateremove&&munremove==null)
1344             generateremove=false;
1345
1346         Descriptor d=ep.getDescriptor();
1347         if (generateremove) {
1348             cr.outputline("for(;"+change.getSafeSymbol()+"<0;"+change.getSafeSymbol()+"++)");
1349             cr.startblock();
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()+");");
1358                 } else {
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()+");");
1362                 }
1363             } else {
1364                 cr.addDeclaration("int",leftvar.getSafeSymbol());
1365                 cr.outputline(leftvar.getSafeSymbol()+"= SimpleHashfirstkey("+d.getSafeSymbol()+"_hash);");
1366             }
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);
1372                 if (usageimage)
1373                     cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1374                 if (usageinvimage)
1375                     cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv ,(int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1376             } else {
1377                 cr.outputline("SimpleHashremove("+d.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1378             }
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+");");
1390                             } else {
1391                                 cr.outputline("RepairHashaddset("+repairtable.getSafeSymbol()+","+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1392                             }
1393                         }
1394                     }
1395                 }
1396             }
1397             cr.endblock();
1398         }
1399
1400 // In some cases the analysis has determined that generating removes
1401 // is unnecessary
1402         if (generateadd&&munadd==null)
1403             generateadd=false;
1404
1405         if (generateadd) {
1406
1407             cr.outputline("for(;"+change.getSafeSymbol()+">0;"+change.getSafeSymbol()+"--)");
1408             cr.startblock();
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);
1413
1414                 RelationDescriptor rd=(RelationDescriptor)d;
1415                 if (termination.sources.relsetSource(rd,!ep.inverted())) {
1416                     /* Set Source */
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()+");)");
1422                     cr.startblock();
1423                     if (ep.inverted()) {
1424                         cr.outputline("if (!SimpleHashcontainskeydata("+rd.getSafeSymbol()+"_hashinv,"+iterator.getSafeSymbol()+".key(),"+otherside.getSafeSymbol()+"))");
1425                     } else {
1426                         cr.outputline("if (!SimpleHashcontainskeydata("+rd.getSafeSymbol()+"_hash, "+otherside.getSafeSymbol()+","+iterator.getSafeSymbol()+".key()))");
1427                     }
1428                     cr.outputline(newobject.getSafeSymbol()+"=key(&"+iterator.getSafeSymbol()+");");
1429                     cr.outputline("next(&"+iterator.getSafeSymbol()+");");
1430                     cr.endblock();
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);
1438                     if (usageimage)
1439                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, "+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1440                     if (usageinvimage)
1441                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, "+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1442
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()+");");
1446                 } else {
1447                     boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1448                     boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1449                     if (usageimage)
1450                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, "+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1451                     if (usageinvimage)
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()+");");
1456                 }
1457             } else {
1458                 SetDescriptor sd=(SetDescriptor)d;
1459                 if (termination.sources.setSource(sd)) {
1460                     /* Set Source */
1461                     /* Set Source */
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()+");)");
1467                     cr.startblock();
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()+");");
1471                     cr.endblock();
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()+");");
1480             }
1481             cr.endblock();
1482         }
1483     }
1484
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);
1491
1492         if (negated) {
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);
1500                 if (inverse) {
1501                     if (usageimage)
1502                         cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1503                     if (usageinvimage)
1504                         cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1505                 } else {
1506                     if (usageimage)
1507                         cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash ,(int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1508                     if (usageinvimage)
1509                         cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1510                 }
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);
1519                                 if (inverse) {
1520                                     cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1521                                 } else {
1522                                     cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
1523                                 }
1524                             }
1525                         }
1526                     }
1527                 }
1528             } else {
1529                 SetDescriptor sd=ip.setexpr.sd;
1530                 cr.outputline("SimpleHashremove("+sd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1531
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+");");
1541                             }
1542                         }
1543                     }
1544                 }
1545             }
1546         } else {
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);
1555                 if (inverse) {
1556                     if (usageimage)
1557                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1558                     if (usageinvimage)
1559                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1560                 } else {
1561                     if (usageimage)
1562                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1563                     if (usageinvimage)
1564                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1565                 }
1566                 UpdateNode un=mun.getUpdate(0);
1567                 String name=(String)updatenames.get(un);
1568                 if (inverse) {
1569                     cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1570                 } else {
1571                     cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+");");
1572                 }
1573             } else {
1574                 SetDescriptor sd=ip.setexpr.sd;
1575                 cr.outputline("SimpleHashadd("+sd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1576
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()+");");
1581             }
1582         }
1583     }
1584
1585     public static Vector getrulelist(Descriptor d) {
1586         Vector dispatchrules = new Vector();
1587         Vector rules = State.currentState.vRules;
1588
1589         for (int i = 0; i < rules.size(); i++) {
1590             Rule rule = (Rule) rules.elementAt(i);
1591             Set requiredsymbols = rule.getRequiredDescriptors();
1592
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
1595             // fashion...
1596
1597             if (rule.getRequiredDescriptors().contains(d)) {
1598                 dispatchrules.addElement(rule);
1599             }
1600         }
1601         return dispatchrules;
1602     }
1603
1604     private boolean need_compensation(Rule r) {
1605         if (!Compiler.REPAIR)
1606             return false;
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)
1614                     return false;
1615             }
1616         }
1617         return true;
1618     }
1619
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)
1632                             return un;
1633                     }
1634                 }
1635             }
1636         }
1637         throw new Error("No Compensation Update could be found");
1638     }
1639
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);
1643
1644         if (!(usageinvimage||usageimage)) /* not used at all*/
1645             return;
1646
1647         cr.outputline("/* RELATION DISPATCH */");
1648         if (Compiler.REPAIR) {
1649             cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1650             if (usageimage)
1651                 cr.outputline("!SimpleHashcontainskeydata("+oldmodel.getSafeSymbol()+"->"+rd.getJustSafeSymbol() + "_hash, "+leftvar+","+rightvar+"))");
1652             else
1653                 cr.outputline("!SimpleHashcontainskeydata("+oldmodel.getSafeSymbol() +"->"+rd.getJustSafeSymbol()+"_hashinv, "+rightvar+","+leftvar+"))");
1654
1655             cr.startblock(); {
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+"))");
1660                 cr.startblock(); {
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+");");
1666
1667
1668
1669
1670                     String parttype="";
1671                     for(int i=0;i<currentrule.numQuantifiers();i++) {
1672                         if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1673                             parttype=parttype+", int, int";
1674                         else
1675                             parttype=parttype+", int";
1676                     }
1677
1678                     VarDescriptor tmpptr=VarDescriptor.makeNew("tempupdateptr");
1679
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();
1693                         }
1694                     }
1695
1696
1697
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()+")");
1702                     {
1703                         VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1704                         String methodcallprefix="("+funptr.getSafeSymbol()+") ";
1705                         cr.startblock();
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() +");");
1711                         cr.endblock();
1712                     }
1713                     cr.outputline("else ");
1714                     {
1715                         VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1716                         String methodcallprefix="("+funptr.getSafeSymbol()+") ";
1717                         cr.startblock();
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+");");
1721                         cr.endblock();
1722                     }
1723                     cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1724                     cr.outputline("goto rebuild;");
1725                 }
1726                 cr.endblock();
1727
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();
1746                         }
1747                     }
1748                     methodcall+=");";
1749                     cr.outputline(methodcall);
1750                     cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1751                     cr.outputline("goto rebuild;");
1752                 }
1753             }
1754             cr.endblock();
1755         }
1756
1757         String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1758         cr.startblock();
1759         cr.addDeclaration("int" , addeditem);
1760         cr.outputline(addeditem + "=0;");
1761
1762         String ifstring="if (!maybe";
1763         if (rd.getDomain().getType() instanceof StructureTypeDescriptor)  {
1764             ifstring+="&&"+leftvar;
1765         }
1766
1767         if (rd.getRange().getType() instanceof StructureTypeDescriptor)  {
1768             ifstring+="&&"+rightvar;
1769         }
1770
1771         ifstring+=")";
1772
1773         if (rd.testUsage(RelationDescriptor.IMAGE)) {
1774             cr.outputline(ifstring);
1775             cr.outputline(addeditem + " = SimpleHashadd("+rd.getSafeSymbol()+"_hash, (int)" + leftvar + ", (int)" + rightvar+ ");");
1776         }
1777
1778         if (rd.testUsage(RelationDescriptor.INVIMAGE)) {
1779             cr.outputline(ifstring);
1780             cr.outputline(addeditem + " = SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, (int)" + rightvar + ", (int)" + leftvar + ");");
1781         }
1782
1783
1784         Vector dispatchrules = getrulelist(rd);
1785
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))
1790                 toremove.add(r);
1791         }
1792         dispatchrules.removeAll(toremove);
1793         if (dispatchrules.size() == 0) {
1794             cr.outputline("/* nothing to dispatch */");
1795             cr.endblock();
1796             return;
1797         }
1798
1799         cr.outputline("if (" + addeditem + ")");
1800         cr.startblock();
1801
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);");
1807             } else {
1808                 for (int j=0;j<rule.numQuantifiers();j++) {
1809                     Quantifier q=rule.getQuantifier(j);
1810                     if (q.getRequiredDescriptors().contains(rd)) {
1811                         /* Generate add */
1812                         cr.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+","+j+","+leftvar+","+rightvar+");");
1813                     }
1814                 }
1815             }
1816         }
1817         cr.endblock();
1818         cr.endblock();
1819     }
1820
1821
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+"))");
1827             cr.startblock(); {
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+"))");
1832                 cr.startblock(); {
1833                     /* Have update to call into */
1834                     VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1835                     String parttype="";
1836                     for(int i=0;i<currentrule.numQuantifiers();i++) {
1837                         if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1838                             parttype=parttype+", int, int";
1839                         else
1840                             parttype=parttype+", int";
1841                     }
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();
1859                         }
1860                     }
1861                     methodcall+=");";
1862                     cr.outputline(methodcall);
1863                     cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1864                     cr.outputline("goto rebuild;");
1865                 }
1866                 cr.endblock();
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);
1874
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 */
1879
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();
1894                             }
1895                         }
1896                         methodcall+=");";
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+"))");
1901                             cr.startblock();
1902                         }
1903                         cr.outputline(methodcall);
1904                         cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1905                         cr.outputline("goto rebuild;");
1906                         cr.endblock();
1907                     }
1908                     if (currentrule==itrule)
1909                         cr.endblock();
1910                 }
1911             }
1912         }
1913
1914         cr.startblock();
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+")");
1920         } else
1921             cr.outputline("if (!maybe)");
1922         cr.outputline(addeditem + " = SimpleHashadd("+sd.getSafeSymbol()+"_hash, (int)" + setvar +  ", (int)" + setvar + ");");
1923         cr.startblock();
1924         Vector dispatchrules = getrulelist(sd);
1925
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))
1930                 toremove.add(r);
1931         }
1932         dispatchrules.removeAll(toremove);
1933
1934         if (dispatchrules.size() == 0) {
1935             cr.outputline("/* nothing to dispatch */");
1936             cr.endblock();
1937             cr.endblock();
1938             return;
1939         }
1940         /* Add item to worklist if new */
1941         cr.outputline("if ("+addeditem+")");
1942         cr.startblock();
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);");
1948             } else {
1949                 for (int j=0;j<rule.numQuantifiers();j++) {
1950                     Quantifier q=rule.getQuantifier(j);
1951                     if (SetDescriptor.expand(q.getRequiredDescriptors()).contains(sd)) {
1952                         /* Generate add */
1953                         cr.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+","+j+","+setvar+",0);");
1954                     }
1955                 }
1956             }
1957         }
1958         cr.endblock();
1959         cr.endblock();
1960         cr.endblock();
1961     }
1962 }