Modify Generators to allow multiple specs
[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     public static String name="foo";
14     public static String postfix="";
15     String headername;
16     static VarDescriptor oldmodel=null;
17     static VarDescriptor newmodel=null;
18     static VarDescriptor worklist=null;
19     static VarDescriptor repairtable=null;
20     static VarDescriptor goodflag=null;
21     Rule currentrule=null;
22     Hashtable updatenames;
23     HashSet usedupdates;
24     Termination termination;
25     Set removed;
26     HashSet togenerate;
27     static boolean DEBUG=false;
28     Cost cost;
29     ModelRuleDependence mrd;
30
31     public RepairGenerator(State state, Termination t) {
32         this.state = state;
33         updatenames=new Hashtable();
34         usedupdates=new HashSet();
35         termination=t;
36         removed=t.removedset;
37         togenerate=new HashSet();
38         togenerate.addAll(termination.conjunctions);
39         if (Compiler.REPAIR)
40             togenerate.removeAll(removed);
41         GraphNode.computeclosure(togenerate,removed);
42         cost=new Cost();
43         mrd=ModelRuleDependence.doAnalysis(state);
44         Repair.repairgenerator=this;
45     }
46
47     private void generatetypechecks(boolean flag) {
48         if (flag) {
49             DotExpr.DOTYPECHECKS=true;
50             VarExpr.DOTYPECHECKS=true;
51             DotExpr.DONULL=true;
52             VarExpr.DONULL=true;
53         } else {
54             VarExpr.DOTYPECHECKS=false;
55             DotExpr.DOTYPECHECKS=false;
56             VarExpr.DONULL=true;
57             DotExpr.DONULL=true;
58         }
59     }
60
61     private void name_updates() {
62         int count=0;
63         for(Iterator it=termination.updatenodes.iterator();it.hasNext();) {
64             GraphNode gn=(GraphNode) it.next();
65             TermNode tn=(TermNode) gn.getOwner();
66             MultUpdateNode mun=tn.getUpdate();
67             if (togenerate.contains(gn))
68             for (int i=0;i<mun.numUpdates();i++) {
69                 UpdateNode un=mun.getUpdate(i);
70                 String name="update"+RepairGenerator.name+String.valueOf(count++);
71                 updatenames.put(un,name);
72             }
73         }
74     }
75
76     public void generate(OutputStream outputrepair, OutputStream outputaux,OutputStream outputhead, String st) {
77         this.outputrepair = new PrintWrapper(new java.io.PrintWriter(outputrepair, true));
78         this.outputaux = new PrintWrapper(new java.io.PrintWriter(outputaux, true));
79         this.outputhead = new PrintWrapper(new java.io.PrintWriter(outputhead, true));
80
81         headername=st;
82         name_updates();
83         generatetypechecks(true);
84         generate_tokentable();
85         RelationDescriptor.prefix = "thisvar->";
86         SetDescriptor.prefix = "thisvar->";
87
88         generate_hashtables();
89         generate_stateobject();
90
91
92         /* Rewrite globals */
93         CodeWriter craux = new StandardCodeWriter(this.outputaux);
94         for (Iterator it=this.state.stGlobals.descriptors();it.hasNext();) {
95             VarDescriptor vd=(VarDescriptor)it.next();
96             craux.outputline("#define "+vd.getSafeSymbol()+" thisvar->"+vd.getSafeSymbol());
97         }
98
99
100         generate_call();
101         generate_start();
102         generate_rules();
103         if (!Compiler.REPAIR||Compiler.GENERATEDEBUGPRINT) {
104             generate_print();
105         }
106         generate_checks();
107         generate_teardown();
108         CodeWriter crhead = new StandardCodeWriter(this.outputhead);
109         craux = new StandardCodeWriter(this.outputaux);
110         craux.emptyBuffer();
111         craux.endblock();
112
113         if (Compiler.GENERATEDEBUGHOOKS) {
114             crhead.outputline("void debughook();");
115             craux.outputline("void debughook() {}");
116         }
117         generatetypechecks(false);
118         generate_computesizes();
119         generatetypechecks(true);
120         generate_recomputesizes();
121         generatetypechecks(false);
122         generate_updates();
123         StructureGenerator sg=new StructureGenerator(state,this);
124         sg.buildall();
125         crhead.outputline("#endif");
126     }
127
128     String ststate="state";
129     String stmodel="model";
130     String strepairtable="repairtable";
131     String stleft="left";
132     String stright="right";
133     String stnew="newvalue";
134
135     private void generate_updates() {
136         int count=0;
137         CodeWriter crhead = new StandardCodeWriter(outputhead);
138         CodeWriter craux = new StandardCodeWriter(outputaux);
139         RelationDescriptor.prefix = "model->";
140         SetDescriptor.prefix = "model->";
141
142         /* Rewrite globals */
143
144         for (Iterator it=this.state.stGlobals.descriptors();it.hasNext();) {
145             VarDescriptor vd=(VarDescriptor)it.next();
146             craux.outputline("#undef "+vd.getSafeSymbol());
147             craux.outputline("#define "+vd.getSafeSymbol()+" "+ststate+"->"+vd.getSafeSymbol());
148         }
149
150         for(Iterator it=termination.updatenodes.iterator();it.hasNext();) {
151             GraphNode gn=(GraphNode) it.next();
152             TermNode tn=(TermNode) gn.getOwner();
153             MultUpdateNode mun=tn.getUpdate();
154             boolean isrelation=(mun.getDescriptor() instanceof RelationDescriptor);
155             if (togenerate.contains(gn))
156             for (int i=0;i<mun.numUpdates();i++) {
157                 UpdateNode un=mun.getUpdate(i);
158                 String methodname=(String)updatenames.get(un);
159
160                 switch(mun.op) {
161                 case MultUpdateNode.ADD:
162                     if (isrelation) {
163                         crhead.outputline("void "+methodname+"(struct "+name+"_state * " +ststate+",struct "+name+" * "+stmodel+", struct RepairHash * "+strepairtable+", int "+stleft+", int "+stright+");");
164                         craux.outputline("void "+methodname+"(struct "+name+"_state * "+ ststate+", struct "+name+" * "+stmodel+", struct RepairHash * "+strepairtable+", int "+stleft+", int "+stright+")");
165                     } else {
166                         crhead.outputline("void "+methodname+"(struct "+name+"_state * "+ ststate+", struct "+name+" * "+stmodel+", struct RepairHash * "+strepairtable+", int "+stleft+");");
167                         craux.outputline("void "+methodname+"(struct "+name+"_state * "+ststate+", struct "+name+" * "+stmodel+", struct RepairHash * "+strepairtable+", int "+stleft+")");
168                     }
169                     craux.startblock();
170                     craux.startBuffer();
171                     craux.addDeclaration("int","maybe");
172                     craux.outputline("maybe=0;");
173                     if (Compiler.GENERATEINSTRUMENT)
174                         craux.outputline("updatecount++;");
175
176                     final SymbolTable st = un.getRule().getSymbolTable();
177                     CodeWriter cr = new StandardCodeWriter(outputaux) {
178                         public SymbolTable getSymbolTable() { return st; }
179                     };
180                     un.generate(cr, false, false, stleft,stright, null,this);
181                     craux.outputline("if (maybe) printf(\"REALLY BAD\");");
182                     craux.emptyBuffer();
183                     craux.endblock();
184                     break;
185                 case MultUpdateNode.REMOVE: {
186                     Rule r=un.getRule();
187                     String methodcall="void "+methodname+"(struct "+name+"_state * "+ststate+", struct "+name+" * "+stmodel+", struct RepairHash * "+strepairtable;
188                     for(int j=0;j<r.numQuantifiers();j++) {
189                         Quantifier q=r.getQuantifier(j);
190                         if (q instanceof SetQuantifier) {
191                             SetQuantifier sq=(SetQuantifier) q;
192                             methodcall+=","+sq.getVar().getType().getGenerateType().getSafeSymbol()+" "+sq.getVar().getSafeSymbol();
193                         } else if (q instanceof RelationQuantifier) {
194                             RelationQuantifier rq=(RelationQuantifier) q;
195
196                             methodcall+=","+rq.x.getType().getGenerateType().getSafeSymbol()+" "+rq.x.getSafeSymbol();
197                             methodcall+=","+rq.y.getType().getGenerateType().getSafeSymbol()+" "+rq.y.getSafeSymbol();
198                         } else if (q instanceof ForQuantifier) {
199                             ForQuantifier fq=(ForQuantifier) q;
200                             methodcall+=",int "+fq.getVar().getSafeSymbol();
201                         }
202                     }
203                     methodcall+=")";
204                     crhead.outputline(methodcall+";");
205                     craux.outputline(methodcall);
206                     craux.startblock();
207                     craux.startBuffer();
208                     craux.addDeclaration("int","maybe");
209                     craux.outputline("maybe=0;");
210                     if (Compiler.GENERATEINSTRUMENT)
211                         craux.outputline("updatecount++;");
212                     final SymbolTable st2 = un.getRule().getSymbolTable();
213                     CodeWriter cr2 = new StandardCodeWriter(outputaux) {
214                         public SymbolTable getSymbolTable() { return st2; }
215                     };
216                     un.generate(cr2, true, false, null,null, null,this);
217                     craux.outputline("if (maybe) printf(\"REALLY BAD\");");
218                     craux.emptyBuffer();
219                     craux.endblock();
220                 }
221                     break;
222                 case MultUpdateNode.MODIFY: {
223                     Rule r=un.getRule();
224                     String methodcall="void "+methodname+"(struct "+name+"_state * "+ststate+", struct "+name+" * "+stmodel+", struct RepairHash * "+strepairtable;
225                     for(int j=0;j<r.numQuantifiers();j++) {
226                         Quantifier q=r.getQuantifier(j);
227                         if (q instanceof SetQuantifier) {
228                             SetQuantifier sq=(SetQuantifier) q;
229                             methodcall+=","+sq.getVar().getType().getGenerateType().getSafeSymbol()+" "+sq.getVar().getSafeSymbol();
230                         } else if (q instanceof RelationQuantifier) {
231                             RelationQuantifier rq=(RelationQuantifier) q;
232
233                             methodcall+=", "+rq.x.getType().getGenerateType().getSafeSymbol()+" "+rq.x.getSafeSymbol();
234                             methodcall+=", "+rq.y.getType().getGenerateType().getSafeSymbol()+" "+rq.y.getSafeSymbol();
235                         } else if (q instanceof ForQuantifier) {
236                             ForQuantifier fq=(ForQuantifier) q;
237                             methodcall+=", int "+fq.getVar().getSafeSymbol();
238                         }
239                     }
240                     methodcall+=", int "+stleft+", int "+stright+", int "+stnew;
241                     methodcall+=")";
242                     crhead.outputline(methodcall+";");
243                     craux.outputline(methodcall);
244                     craux.startblock();
245                     craux.startBuffer();
246                     craux.outputline("int maybe=0;");
247                     if (Compiler.GENERATEINSTRUMENT)
248                         craux.outputline("updatecount++;");
249                     final SymbolTable st2 = un.getRule().getSymbolTable();
250                     CodeWriter cr2 = new StandardCodeWriter(outputaux) {
251                         public SymbolTable getSymbolTable() { return st2; }
252                     };
253                     un.generate(cr2, false, true, stleft, stright, stnew, this);
254                     craux.outputline("if (maybe) printf(\"REALLY BAD\");");
255                     craux.emptyBuffer();
256                     craux.endblock();
257                 }
258                     break;
259
260                 default:
261                     throw new Error("Nonimplement Update");
262                 }
263             }
264         }
265     }
266
267     private void generate_call() {
268         CodeWriter cr = new StandardCodeWriter(outputrepair);
269         VarDescriptor vdstate=VarDescriptor.makeNew("repairstate");
270         cr.addDeclaration("struct "+ name+"_state *", vdstate.getSafeSymbol());
271         cr.outputline(vdstate.getSafeSymbol()+"=allocate"+name+"_state();");
272         Iterator globals=state.stGlobals.descriptors();
273         while (globals.hasNext()) {
274             VarDescriptor vd=(VarDescriptor) globals.next();
275             cr.outputline(vdstate.getSafeSymbol()+"->"+vd.getSafeSymbol()+"=("+vd.getType().getGenerateType().getSafeSymbol()+")"+vd.getSafeSymbol()+";");
276         }
277         /* Insert repair here */
278         cr.outputline("doanalysis"+postfix+"("+vdstate.getSafeSymbol()+");");
279         globals=state.stGlobals.descriptors();
280         while (globals.hasNext()) {
281             VarDescriptor vd=(VarDescriptor) globals.next();
282             cr.outputline("*(("+vd.getType().getGenerateType().getSafeSymbol()+"*) &"+vd.getSafeSymbol()+")="+vdstate.getSafeSymbol()+"->"+vd.getSafeSymbol()+";");
283         }
284         cr.outputline("free"+name+"_state("+vdstate.getSafeSymbol()+");");
285     }
286
287     private void generate_tokentable() {
288         CodeWriter cr = new StandardCodeWriter(outputrepair);
289         Iterator tokens = TokenLiteralExpr.tokens.keySet().iterator();
290
291         cr.outputline("");
292         cr.outputline("/* Token values*/");
293         cr.outputline("");
294
295         while (tokens.hasNext()) {
296             Object token = tokens.next();
297             cr.outputline("/* " + token.toString() + " = " + TokenLiteralExpr.tokens.get(token).toString()+"*/");
298         }
299
300         cr.outputline("");
301         cr.outputline("");
302     }
303
304     private void generate_stateobject() {
305         CodeWriter crhead = new StandardCodeWriter(outputhead);
306         CodeWriter craux = new StandardCodeWriter(outputaux);
307         crhead.outputline("struct "+name+"_state {");
308         Iterator globals=state.stGlobals.descriptors();
309         while (globals.hasNext()) {
310             VarDescriptor vd=(VarDescriptor) globals.next();
311             crhead.outputline(vd.getType().getGenerateType().getSafeSymbol()+" "+vd.getSafeSymbol()+";");
312         }
313         crhead.outputline("};");
314         crhead.outputline("struct "+name+"_state * allocate"+name+"_state();");
315         craux.outputline("struct "+name+"_state * allocate"+name+"_state()");
316         craux.startblock();
317         craux.outputline("return (struct "+name+"_state *) malloc(sizeof(struct "+name+"_state));");
318         craux.endblock();
319
320         crhead.outputline("void free"+name+"_state(struct "+name+"_state *);");
321         craux.outputline("void free"+name+"_state(struct "+name+"_state * thisvar)");
322         craux.startblock();
323         craux.outputline("free(thisvar);");
324         craux.endblock();
325
326         crhead.outputline("void "+name+"_statecomputesizes(struct "+name+"_state * ,int *,int **);");
327         crhead.outputline("void "+name+"_staterecomputesizes(struct "+name+"_state *);");
328     }
329
330     private void generate_computesizes() {
331         int max=TypeDescriptor.counter;
332         TypeDescriptor[] tdarray=new TypeDescriptor[max];
333         for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
334             TypeDescriptor ttd=(TypeDescriptor)it.next();
335             tdarray[ttd.getId()]=ttd;
336         }
337         final SymbolTable st = state.stGlobals;
338         CodeWriter cr = new StandardCodeWriter(outputaux) {
339                 public SymbolTable getSymbolTable() { return st; }
340             };
341
342         cr.outputline("void "+name+"_statecomputesizes(struct "+name+"_state * thisvar,int *sizearray,int **numele)");
343         cr.startblock();
344         cr.startBuffer();
345         cr.addDeclaration("int","maybe");
346         cr.outputline("maybe=0;");
347         for(int i=0;i<max;i++) {
348             TypeDescriptor td=tdarray[i];
349             Expr size=td.getSizeExpr();
350             VarDescriptor vd=VarDescriptor.makeNew("size");
351             size.generate(cr,vd);
352             cr.outputline("sizearray["+i+"]="+vd.getSafeSymbol()+";");
353         }
354         for(int i=0;i<max;i++) {
355             TypeDescriptor td=tdarray[i];
356             if (td instanceof StructureTypeDescriptor) {
357                 StructureTypeDescriptor std=(StructureTypeDescriptor) td;
358                 for(int j=0;j<std.fieldlist.size();j++) {
359                     FieldDescriptor fd=(FieldDescriptor)std.fieldlist.get(j);
360                     if (fd instanceof ArrayDescriptor) {
361                         ArrayDescriptor ad=(ArrayDescriptor)fd;
362                         Expr index=ad.getIndexBound();
363                         VarDescriptor vd=VarDescriptor.makeNew("index");
364                         index.generate(cr,vd);
365                         cr.outputline("numele["+i+"]["+j+"]="+vd.getSafeSymbol()+";");
366                     }
367                 }
368             }
369         }
370         cr.outputline("if (maybe) printf(\"BAD ERROR\");");
371         cr.emptyBuffer();
372         cr.endblock();
373     }
374
375     private void generate_recomputesizes() {
376         int max=TypeDescriptor.counter;
377         TypeDescriptor[] tdarray=new TypeDescriptor[max];
378         for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
379             TypeDescriptor ttd=(TypeDescriptor)it.next();
380             tdarray[ttd.getId()]=ttd;
381         }
382         final SymbolTable st = state.stGlobals;
383         CodeWriter cr = new StandardCodeWriter(outputaux) {
384                 public SymbolTable getSymbolTable() { return st; }
385             };
386         cr.outputline("void "+name+"_staterecomputesizes(struct "+name+"_state * thisvar)");
387         cr.startblock();
388         cr.startBuffer();
389         cr.addDeclaration("int","maybe");
390         cr.outputline("maybe=0;");
391         for(int i=0;i<max;i++) {
392             TypeDescriptor td=tdarray[i];
393             Expr size=td.getSizeExpr();
394             VarDescriptor vd=VarDescriptor.makeNew("size");
395             size.generate(cr,vd);
396         }
397         for(int i=0;i<max;i++) {
398             TypeDescriptor td=tdarray[i];
399             if (td instanceof StructureTypeDescriptor) {
400                 StructureTypeDescriptor std=(StructureTypeDescriptor) td;
401                 for(int j=0;j<std.fieldlist.size();j++) {
402                     FieldDescriptor fd=(FieldDescriptor)std.fieldlist.get(j);
403                     if (fd instanceof ArrayDescriptor) {
404                         ArrayDescriptor ad=(ArrayDescriptor)fd;
405                         Expr index=ad.getIndexBound();
406                         VarDescriptor vd=VarDescriptor.makeNew("index");
407                         index.generate(cr,vd);
408                     }
409                 }
410             }
411         }
412         cr.outputline("if (maybe) printf(\"BAD ERROR\");");
413         cr.emptyBuffer();
414         cr.endblock();
415     }
416
417
418     private void generate_hashtables() {
419         CodeWriter craux = new StandardCodeWriter(outputaux);
420         CodeWriter crhead = new StandardCodeWriter(outputhead);
421         crhead.outputline("#ifndef "+name+"_h");
422         crhead.outputline("#define "+name+"_h");
423         crhead.outputline("#include \"SimpleHash.h\"");
424         crhead.outputline("#include \"instrument.h\"");
425         crhead.outputline("#include <stdio.h>");
426         crhead.outputline("#include <stdlib.h>");
427         crhead.outputline("struct "+name+" * allocate"+name+"();");
428         crhead.outputline("void free"+name+"(struct "+name+" *);");
429         crhead.outputline("struct "+name+" {");
430         craux.outputline("#include \""+headername+"\"");
431         craux.outputline("#include \"size.h\"");
432         if (Compiler.TIME) {
433             craux.outputline("#include <sys/time.h>");
434         }
435         if (Compiler.ALLOCATECPLUSPLUS) {
436             for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
437                 TypeDescriptor td=(TypeDescriptor)it.next();
438                 if (td instanceof StructureTypeDescriptor) {
439                     if (((StructureTypeDescriptor)td).size()>0) {
440                         FieldDescriptor fd=((StructureTypeDescriptor)td).get(0);
441                         if (fd.getSymbol().startsWith("_vptr_")) {
442                             String vtable="_ZTV";
443                             vtable+=td.getSymbol().length();
444                             vtable+=td.getSymbol();
445                             craux.outputline("extern void * "+vtable+";");
446                         }
447                     }
448                 }
449             }
450         }
451         craux.outputline("struct "+ name+"* allocate"+name+"()");
452         craux.startblock();
453         craux.outputline("/* creating hashtables */");
454
455         /* build sets */
456         Iterator sets = state.stSets.descriptors();
457         craux.addDeclaration("struct "+name+"*", "thisvar");
458         craux.outputline("thisvar=(struct "+name+"*) malloc(sizeof(struct "+name+"));");
459
460         /* first pass create all the hash tables */
461         while (sets.hasNext()) {
462             SetDescriptor set = (SetDescriptor) sets.next();
463             crhead.outputline("struct SimpleHash* " + set.getJustSafeSymbol() + "_hash;");
464             craux.outputline(set.getSafeSymbol() + "_hash = noargallocateSimpleHash();");
465         }
466
467         /* second pass build relationships between hashtables */
468         sets = state.stSets.descriptors();
469
470         while (sets.hasNext()) {
471             SetDescriptor set = (SetDescriptor) sets.next();
472             Iterator subsets = set.subsets();
473
474             while (subsets.hasNext()) {
475                 SetDescriptor subset = (SetDescriptor) subsets.next();
476                 craux.outputline("SimpleHashaddParent("+subset.getSafeSymbol() +"_hash ,"+ set.getSafeSymbol() + "_hash);");
477             }
478         }
479
480         /* build relations */
481         Iterator relations = state.stRelations.descriptors();
482
483         /* first pass create all the hash tables */
484         while (relations.hasNext()) {
485             RelationDescriptor relation = (RelationDescriptor) relations.next();
486
487             if (relation.testUsage(RelationDescriptor.IMAGE)) {
488                 crhead.outputline("struct SimpleHash* " + relation.getJustSafeSymbol() + "_hash;");
489                 craux.outputline(relation.getSafeSymbol() + "_hash = noargallocateSimpleHash();");
490             }
491
492             if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
493                 crhead.outputline("struct SimpleHash* " + relation.getJustSafeSymbol() + "_hashinv;");
494                 craux.outputline(relation.getSafeSymbol() + "_hashinv = noargallocateSimpleHash();");
495             }
496         }
497         craux.outputline("return thisvar;");
498         craux.endblock();
499         crhead.outputline("};");
500         craux.outputline("void free"+name+"(struct "+ name +"* thisvar)");
501         craux.startblock();
502         craux.outputline("/* deleting hashtables */");
503
504         /* build destructor */
505         sets = state.stSets.descriptors();
506
507         /* first pass create all the hash tables */
508         while (sets.hasNext()) {
509             SetDescriptor set = (SetDescriptor) sets.next();
510             craux.outputline("freeSimpleHash("+set.getSafeSymbol() + "_hash);");
511         }
512
513         /* destroy relations */
514         relations = state.stRelations.descriptors();
515
516         /* first pass create all the hash tables */
517         while (relations.hasNext()) {
518             RelationDescriptor relation = (RelationDescriptor) relations.next();
519
520             if (relation.testUsage(RelationDescriptor.IMAGE)) {
521                 craux.outputline("freeSimpleHash("+relation.getSafeSymbol() + "_hash);");
522             }
523
524             if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
525                 craux.outputline("freeSimpleHash(" + relation.getSafeSymbol() + "_hashinv);");
526             }
527         }
528         craux.outputline("free(thisvar);");
529         craux.endblock();
530     }
531
532     private void generate_start() {
533         CodeWriter crhead = new StandardCodeWriter(outputhead);
534         CodeWriter craux = new StandardCodeWriter(outputaux);
535         oldmodel=VarDescriptor.makeNew("oldmodel");
536         newmodel=VarDescriptor.makeNew("newmodel");
537         worklist=VarDescriptor.makeNew("worklist");
538         goodflag=VarDescriptor.makeNew("goodflag");
539         repairtable=VarDescriptor.makeNew("repairtable");
540
541         if (Compiler.GENERATEINSTRUMENT) {
542             craux.outputline("int updatecount;");
543             craux.outputline("int rebuildcount;");
544             craux.outputline("int abstractcount;");
545         }
546
547         crhead.outputline("void doanalysis"+postfix+"(struct "+name+"_state *);");
548         craux.outputline("void doanalysis"+postfix+"(struct "+name+"_state * thisvar)");
549         craux.startblock();
550         craux.outputline("int highmark;"); /* This declaration is special...need it to be first */
551         craux.startBuffer();
552
553         if (Compiler.TIME) {
554             craux.outputline("struct timeval _begin_time,_end_time;");
555             craux.outputline("gettimeofday(&_begin_time,NULL);");
556         }
557         if (Compiler.GENERATEINSTRUMENT) {
558             craux.outputline("updatecount=0;");
559             craux.outputline("rebuildcount=0;");
560             craux.outputline("abstractcount=0;");
561         }
562
563
564         craux.addDeclaration("struct "+name+ " * ",oldmodel.getSafeSymbol());
565         craux.outputline(oldmodel.getSafeSymbol()+"=0;");
566         craux.addDeclaration("struct WorkList * ",worklist.getSafeSymbol());
567         craux.outputline(worklist.getSafeSymbol()+" = allocateWorkList();");
568         craux.addDeclaration("struct RepairHash * ",repairtable.getSafeSymbol());
569         craux.outputline(repairtable.getSafeSymbol()+"=0;");
570         craux.outputline("initializestack(&highmark);");
571         craux.outputline(name+"_statecomputesizes(thisvar, arsize, arnumelements);");
572         craux.outputline("computesizes();");
573         craux.outputline(name+"_staterecomputesizes(thisvar);");
574         craux.outputline("while (1)");
575         craux.startblock();
576         craux.addDeclaration("struct "+name+ " * ",newmodel.getSafeSymbol());
577         craux.outputline(newmodel.getSafeSymbol()+"=allocate"+name+"();");
578         craux.outputline("WorkListreset("+worklist.getSafeSymbol()+");");
579         if (Compiler.GENERATEINSTRUMENT)
580             craux.outputline("rebuildcount++;");
581     }
582
583     private void generate_teardown() {
584         CodeWriter cr = new StandardCodeWriter(outputaux);
585         cr.endblock();
586         if (Compiler.TIME) {
587             cr.outputline("gettimeofday(&_end_time,NULL);");
588             cr.outputline("printf(\"time=%ld uS\\n\",(_end_time.tv_sec-_begin_time.tv_sec)*1000000+_end_time.tv_usec-_begin_time.tv_usec);");
589         }
590
591         if (Compiler.GENERATEINSTRUMENT) {
592             cr.outputline("printf(\"updatecount=%d\\n\",updatecount);");
593             cr.outputline("printf(\"rebuildcount=%d\\n\",rebuildcount);");
594             cr.outputline("printf(\"abstractcount=%d\\n\",abstractcount);");
595         }
596
597     }
598
599     private void generate_print() {
600
601         final SymbolTable st = new SymbolTable();
602
603         CodeWriter cr = new StandardCodeWriter(outputaux) {
604                 public SymbolTable getSymbolTable() { return st; }
605             };
606
607         cr.outputline("/* printing sets!*/");
608         cr.outputline("printf(\"\\n\\nPRINTING SETS AND RELATIONS\\n\");");
609
610         Iterator setiterator = state.stSets.descriptors();
611         while (setiterator.hasNext()) {
612             SetDescriptor sd = (SetDescriptor) setiterator.next();
613             if (sd.getSymbol().equals("int") || sd.getSymbol().equals("token")) {
614                 continue;
615             }
616
617             String setname = sd.getSafeSymbol();
618
619             cr.startblock();
620             cr.outputline("/* printing set " + setname+"*/");
621             cr.outputline("printf(\"\\nPrinting set " + sd.getSymbol() + " - %d elements \\n\", SimpleHashcountset("+setname+"_hash));");
622             cr.addDeclaration("struct SimpleIterator","__setiterator");
623             cr.outputline("SimpleHashiterator("+setname+"_hash,&__setiterator);");
624             cr.outputline("while (hasNext(&__setiterator))");
625             cr.startblock();
626             cr.addDeclaration("int","__setval");
627             cr.outputline("__setval = (int) next(&__setiterator);");
628
629             TypeDescriptor td = sd.getType();
630             if (td instanceof StructureTypeDescriptor) {
631                 StructureTypeDescriptor std = (StructureTypeDescriptor) td;
632                 VarDescriptor vd = new VarDescriptor ("__setval", "__setval", td, false);
633                 std.generate_printout(cr, vd);
634             } else { // Missing type descriptor or reserved type, just print int
635                 cr.outputline("printf(\"<%d> \", __setval);");
636             }
637
638
639             cr.endblock();
640             cr.endblock();
641         }
642
643         Iterator reliterator = state.stRelations.descriptors();
644         while (reliterator.hasNext()) {
645             RelationDescriptor rd = (RelationDescriptor) reliterator.next();
646
647
648             String relname = rd.getSafeSymbol();
649             if (rd.testUsage(RelationDescriptor.IMAGE)) {
650                 cr.startblock();
651                 cr.outputline("/* printing relation " + relname+"*/");
652                 cr.outputline("printf(\"\\nPrinting relation " + rd.getSymbol() + " - %d elements \\n\", SimpleHashcountset("+relname+"_hash));");
653                 cr.addDeclaration("struct SimpleIterator","__reliterator");
654                 cr.outputline("SimpleHashiterator("+relname+"_hash,&__reliterator);");
655                 cr.outputline("while (hasNext(&__reliterator))");
656                 cr.startblock();
657                 cr.addDeclaration("int","__relval");
658                 cr.addDeclaration("int","__relval2");
659                 cr.outputline("__relval2 = (int) key(&__reliterator);");
660                 cr.outputline("__relval = (int) next(&__reliterator);");
661
662                 cr.outputline("printf(\"<%ld,%ld> \", __relval2,__relval);");
663
664                 cr.endblock();
665                 cr.endblock();
666             } else if (rd.testUsage(RelationDescriptor.INVIMAGE)) {
667                 cr.startblock();
668                 cr.outputline("/* printing inv relation " + relname+"*/");
669                 cr.outputline("printf(\"\\nPrinting relation using inv" + rd.getSymbol() + " - %d elements \\n\", SimpleHashcountset("+relname+"_hash));");
670                 cr.addDeclaration("struct SimpleIterator","__reliterator");
671                 cr.outputline("SimpleHashiterator("+relname+"_hashinv,&__reliterator);");
672                 cr.outputline("while (hasNext(&__reliterator))");
673                 cr.startblock();
674                 cr.addDeclaration("int","__relval");
675                 cr.addDeclaration("int","__relval2");
676                 cr.outputline("__relval2 = (int) key(&__reliterator);");
677                 cr.outputline("__relval = (int) next(&__reliterator);");
678
679
680                 cr.outputline("printf(\"<%ld,%ld> \", __relval,__relval2);");
681
682                 cr.endblock();
683                 cr.endblock();
684             }
685
686         }
687
688         cr.outputline("printf(\"\\n\\n------------------- END PRINTING\\n\");");
689     }
690
691     Set ruleset=null;
692     private void generate_rules() {
693         /* first we must sort the rules */
694         RelationDescriptor.prefix = newmodel.getSafeSymbol()+"->";
695         SetDescriptor.prefix = newmodel.getSafeSymbol()+"->";
696         System.out.println("SCC="+(mrd.numSCC()-1));
697         for(int sccindex=0;sccindex<mrd.numSCC();sccindex++) {
698             ruleset=mrd.getSCC(sccindex);
699             boolean needworklist=mrd.hasCycle(sccindex);
700
701             if (!needworklist) {
702                 Iterator iterator_rs = ruleset.iterator();
703                 while (iterator_rs.hasNext()) {
704                     Rule rule = (Rule) iterator_rs.next();
705                     if (rule.getnogenerate())
706                         continue;
707                     {
708                         final SymbolTable st = rule.getSymbolTable();
709                         CodeWriter cr = new StandardCodeWriter(outputaux) {
710                                 public SymbolTable getSymbolTable() { return st; }
711                             };
712                         InvariantValue ivalue=new InvariantValue();
713                         cr.setInvariantValue(ivalue);
714
715                         cr.outputline("/* build " +escape(rule.toString())+"*/");
716                         cr.startblock();
717                         cr.addDeclaration("int","maybe");
718                         cr.outputline("maybe=0;");
719
720                         Expr ruleexpr=rule.getGuardExpr();
721                         HashSet invariantvars=new HashSet();
722                         Set invariants=ruleexpr.findInvariants(invariantvars);
723
724                         if ((ruleexpr instanceof BooleanLiteralExpr)&&
725                             ((BooleanLiteralExpr)ruleexpr).getValue()) {
726                             if (rule.getInclusion() instanceof SetInclusion) {
727                                 invariants.addAll(((SetInclusion)rule.getInclusion()).getExpr().findInvariants(invariantvars));
728                             } else if (rule.getInclusion() instanceof RelationInclusion) {
729                                 invariants.addAll(((RelationInclusion)rule.getInclusion()).getLeftExpr().findInvariants(invariantvars));
730                                 invariants.addAll(((RelationInclusion)rule.getInclusion()).getRightExpr().findInvariants(invariantvars));
731                             }
732                         }
733                         ListIterator quantifiers = rule.quantifiers();
734                         while (quantifiers.hasNext()) {
735                             Quantifier quantifier = (Quantifier) quantifiers.next();
736                             if (quantifier instanceof ForQuantifier) {
737                                 ForQuantifier fq=(ForQuantifier)quantifier;
738                                 invariants.addAll(fq.lower.findInvariants(invariantvars));
739                                 invariants.addAll(fq.upper.findInvariants(invariantvars));
740                             }
741                         }
742
743                         int openparencount=0;
744                         for(Iterator invit=invariants.iterator();invit.hasNext();) {
745                             Expr invexpr=(Expr)invit.next();
746                             VarDescriptor tmpvd=VarDescriptor.makeNew("tmpvar");
747                             VarDescriptor maybevd=VarDescriptor.makeNew("maybevar");
748                             invexpr.generate(cr,tmpvd);
749                             cr.addDeclaration("int ",maybevd.getSafeSymbol());
750                             cr.outputline(maybevd.getSafeSymbol()+"=maybe;");
751                             cr.outputline("maybe=0;");
752                             ivalue.assignPair(invexpr,tmpvd,maybevd);
753                             openparencount++;
754                             cr.startblock();
755                         }
756                         quantifiers = rule.quantifiers();
757                         while (quantifiers.hasNext()) {
758                             Quantifier quantifier = (Quantifier) quantifiers.next();
759                             quantifier.generate_open(cr);
760                         }
761
762                         /* pretty print! */
763                         cr.output("/*");
764                         rule.getGuardExpr().prettyPrint(cr);
765                         cr.outputline("*/");
766
767                         /* now we have to generate the guard test */
768                         VarDescriptor guardval = VarDescriptor.makeNew();
769                         rule.getGuardExpr().generate(cr, guardval);
770                         cr.outputline("if (" + guardval.getSafeSymbol() + ")");
771                         cr.startblock();
772
773                         /* now we have to generate the inclusion code */
774                         currentrule=rule;
775                         rule.getInclusion().generate(cr);
776                         cr.endblock();
777                         while (quantifiers.hasPrevious()) {
778                             Quantifier quantifier = (Quantifier) quantifiers.previous();
779                             cr.endblock();
780                         }
781                         cr.endblock();
782                         while((openparencount--)>0)
783                             cr.endblock();
784                         cr.outputline("");
785                         cr.outputline("");
786                     }
787                 }
788             } else {
789                 CodeWriter cr2 = new StandardCodeWriter(outputaux);
790
791                 for(Iterator initialworklist=ruleset.iterator();initialworklist.hasNext();) {
792                     /** Construct initial worklist set */
793                     Rule rule=(Rule)initialworklist.next();
794                     cr2.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+",-1,0,0);");
795                 }
796
797                 cr2.outputline("while (WorkListhasMoreElements("+worklist.getSafeSymbol()+"))");
798                 cr2.startblock();
799                 VarDescriptor idvar=VarDescriptor.makeNew("id");
800                 cr2.addDeclaration("int ",idvar.getSafeSymbol());
801                 cr2.outputline(idvar.getSafeSymbol()+"=WorkListgetid("+worklist.getSafeSymbol()+");");
802
803                 String elseladder = "if";
804
805                 Iterator iterator_rules = ruleset.iterator();
806                 while (iterator_rules.hasNext()) {
807
808                     Rule rule = (Rule) iterator_rules.next();
809                     int dispatchid = rule.getNum();
810
811                     {
812                         final SymbolTable st = rule.getSymbolTable();
813                         CodeWriter cr = new StandardCodeWriter(outputaux) {
814                                 public SymbolTable getSymbolTable() { return st; }
815                             };
816
817                         cr.indent();
818                         cr.outputline(elseladder + " ("+idvar.getSafeSymbol()+" == " + dispatchid + ")");
819                         cr.startblock();
820                         cr.addDeclaration("int","maybe");
821                         cr.outputline("maybe=0;");
822                         VarDescriptor typevar=VarDescriptor.makeNew("type");
823                         VarDescriptor leftvar=VarDescriptor.makeNew("left");
824                         VarDescriptor rightvar=VarDescriptor.makeNew("right");
825                         cr.addDeclaration("int",typevar.getSafeSymbol());
826                         cr.outputline(typevar.getSafeSymbol()+"= WorkListgettype("+worklist.getSafeSymbol()+");");
827                         cr.addDeclaration("int",leftvar.getSafeSymbol());
828                         cr.outputline(leftvar.getSafeSymbol()+"= WorkListgetlvalue("+worklist.getSafeSymbol()+");");
829                         cr.addDeclaration("int",rightvar.getSafeSymbol());
830                         cr.outputline(rightvar.getSafeSymbol()+"= WorkListgetrvalue("+worklist.getSafeSymbol()+");");
831                         cr.outputline("/* build " +escape(rule.toString())+"*/");
832
833
834                         for (int j=0;j<rule.numQuantifiers();j++) {
835                             Quantifier quantifier = rule.getQuantifier(j);
836                             quantifier.generate_open(cr, typevar.getSafeSymbol(),j,leftvar.getSafeSymbol(),rightvar.getSafeSymbol());
837                         }
838
839                         /* pretty print! */
840                         cr.output("/*");
841
842                         rule.getGuardExpr().prettyPrint(cr);
843                         cr.outputline("*/");
844
845                         /* now we have to generate the guard test */
846
847                         VarDescriptor guardval = VarDescriptor.makeNew();
848                         rule.getGuardExpr().generate(cr, guardval);
849
850                         cr.outputline("if (" + guardval.getSafeSymbol() + ")");
851                         cr.startblock();
852
853                         /* now we have to generate the inclusion code */
854                         currentrule=rule;
855                         rule.getInclusion().generate(cr);
856                         cr.endblock();
857
858                         for (int j=0;j<rule.numQuantifiers();j++) {
859                             cr.endblock();
860                         }
861
862                         // close startblocks generated by DotExpr memory checks
863                         //DotExpr.generate_memory_endblocks(cr);
864
865                         cr.endblock(); // end else-if WORKLIST ladder
866
867                         elseladder = "else if";
868                     }
869                 }
870                 cr2.outputline("else");
871                 cr2.startblock();
872                 cr2.outputline("printf(\"VERY BAD !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\\n\\n\");");
873                 cr2.outputline("exit(1);");
874                 cr2.endblock();
875                 // end block created for worklist
876                 cr2.outputline("WorkListpop("+worklist.getSafeSymbol()+");");
877                 cr2.endblock();
878             }
879         }
880     }
881
882     public static String escape(String s) {
883         String newstring="";
884         for(int i=0;i<s.length();i++) {
885             char c=s.charAt(i);
886             if (c=='"')
887                 newstring+="\"";
888             else
889                 newstring+=c;
890         }
891         return newstring;
892     }
893
894     private void generate_checks() {
895         /* do constraint checks */
896         Iterator i;
897         if (Compiler.REPAIR)
898             i=termination.constraintdependence.computeOrdering().iterator();
899         else
900             i=state.vConstraints.iterator();
901         for (; i.hasNext();) {
902             Constraint constraint;
903             if (Compiler.REPAIR)
904                 constraint= (Constraint) ((GraphNode)i.next()).getOwner();
905             else
906                 constraint=(Constraint)i.next();
907
908             {
909                 final SymbolTable st = constraint.getSymbolTable();
910                 CodeWriter cr = new StandardCodeWriter(outputaux);
911                 cr.pushSymbolTable(constraint.getSymbolTable());
912
913                 cr.outputline("/* checking " + escape(constraint.toString())+"*/");
914                 cr.startblock();
915
916                 ListIterator quantifiers = constraint.quantifiers();
917
918                 while (quantifiers.hasNext()) {
919                     Quantifier quantifier = (Quantifier) quantifiers.next();
920                     quantifier.generate_open(cr);
921                 }
922
923                 cr.addDeclaration("int","maybe");
924                 cr.outputline("maybe = 0;");
925
926                 /* now we have to generate the guard test */
927
928                 VarDescriptor constraintboolean = VarDescriptor.makeNew("constraintboolean");
929                 constraint.getLogicStatement().generate(cr, constraintboolean);
930
931                 cr.outputline("if (maybe)");
932                 cr.startblock();
933                 cr.outputline("printf(\"maybe fail " +  escape(constraint.toString()) + ". \\n\");");
934                 //cr.outputline("exit(1);");
935                 cr.endblock();
936
937                 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
938                 cr.startblock();
939                 if (!Compiler.REPAIR||Compiler.GENERATEDEBUGHOOKS)
940                     cr.outputline("printf(\"fail " + escape(constraint.toString()) + ". \\n\");");
941
942                 if (Compiler.REPAIR) {
943                 /* Do repairs */
944                 /* Build new repair table */
945
946                 cr.outputline("if ("+repairtable.getSafeSymbol()+")");
947                 cr.outputline("freeRepairHash("+repairtable.getSafeSymbol()+");");
948                 cr.outputline(repairtable.getSafeSymbol()+"=noargallocateRepairHash();");
949
950                 if (Compiler.GENERATEDEBUGHOOKS)
951                     cr.outputline("debughook();");
952                 /* Compute cost of each repair */
953                 VarDescriptor mincost=VarDescriptor.makeNew("mincost");
954                 VarDescriptor mincostindex=VarDescriptor.makeNew("mincostindex");
955                 Vector dnfconst=new Vector();
956                 dnfconst.addAll((Set)termination.conjunctionmap.get(constraint));
957
958                 if (dnfconst.size()<=1) {
959                     cr.addDeclaration("int",mincostindex.getSafeSymbol());
960                     cr.outputline(mincostindex.getSafeSymbol()+"=0;");
961                 }
962                 if (dnfconst.size()>1) {
963                     cr.addDeclaration("int",mincostindex.getSafeSymbol());
964                     boolean first=true;
965                     for(int j=0;j<dnfconst.size();j++) {
966                         GraphNode gn=(GraphNode)dnfconst.get(j);
967                         Conjunction conj=((TermNode)gn.getOwner()).getConjunction();
968                         if (removed.contains(gn))
969                             continue;
970
971                         VarDescriptor costvar;
972                         if (first) {
973                             costvar=mincost;
974                         } else
975                             costvar=VarDescriptor.makeNew("cost");
976                         for(int k=0;k<conj.size();k++) {
977                             DNFPredicate dpred=conj.get(k);
978                             Predicate p=dpred.getPredicate();
979                             boolean negate=dpred.isNegated();
980                             VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
981                             p.generate(cr,predvalue);
982                             if (k==0) {
983                                 cr.addDeclaration("int",costvar.getSafeSymbol());
984                                 cr.outputline(costvar.getSafeSymbol()+"=0;");
985                             }
986                             if (negate)
987                                 cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
988                             else
989                                 cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
990                             cr.outputline(costvar.getSafeSymbol()+"+="+cost.getCost(dpred)+";");
991                         }
992
993                         if(!first) {
994                             cr.outputline("if ("+costvar.getSafeSymbol()+"<"+mincost.getSafeSymbol()+")");
995                             cr.startblock();
996                             cr.outputline(mincost.getSafeSymbol()+"="+costvar.getSafeSymbol()+";");
997                             cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
998                             cr.endblock();
999                         } else
1000                             cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
1001                         first=false;
1002                     }
1003                 }
1004                 cr.outputline("switch("+mincostindex.getSafeSymbol()+")");
1005                 cr.startblock();
1006                 for(int j=0;j<dnfconst.size();j++) {
1007                     GraphNode gn=(GraphNode)dnfconst.get(j);
1008                     Conjunction conj=((TermNode)gn.getOwner()).getConjunction();
1009                     if (removed.contains(gn))
1010                         continue;
1011                     cr.outputline("case "+j+":");
1012                     cr.startblock();
1013                     for(int k=0;k<conj.size();k++) {
1014                         DNFPredicate dpred=conj.get(k);
1015                         Predicate p=dpred.getPredicate();
1016                         boolean negate=dpred.isNegated();
1017                         VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
1018                         p.generate(cr,predvalue);
1019                         if (negate)
1020                             cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
1021                         else
1022                             cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
1023                         cr.startblock();
1024                         if (Compiler.GENERATEINSTRUMENT)
1025                             cr.outputline("abstractcount++;");
1026                         if (p instanceof InclusionPredicate)
1027                             generateinclusionrepair(conj,dpred, cr);
1028                         else if (p instanceof ExprPredicate) {
1029                             ExprPredicate ep=(ExprPredicate)p;
1030                             if (ep.getType()==ExprPredicate.SIZE)
1031                                 generatesizerepair(conj,dpred,cr);
1032                             else if (ep.getType()==ExprPredicate.COMPARISON)
1033                                 generatecomparisonrepair(conj,dpred,cr);
1034                         } else throw new Error("Unrecognized Predicate");
1035                         cr.endblock();
1036                     }
1037                     /* Update model */
1038                     cr.endblock();
1039                     cr.outputline("break;");
1040                 }
1041                 cr.endblock();
1042
1043                 cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
1044                 cr.outputline("free"+name+"("+oldmodel.getSafeSymbol()+");");
1045                 cr.outputline(oldmodel.getSafeSymbol()+"="+newmodel.getSafeSymbol()+";");
1046                 cr.outputline("goto rebuild;");  /* Rebuild model and all */
1047                 }
1048                 cr.endblock();
1049
1050                 while (quantifiers.hasPrevious()) {
1051                     Quantifier quantifier = (Quantifier) quantifiers.previous();
1052                     cr.endblock();
1053                 }
1054                 cr.endblock();
1055                 cr.outputline("");
1056                 cr.outputline("");
1057             }
1058         }
1059         CodeWriter cr = new StandardCodeWriter(outputaux);
1060         cr.startblock();
1061         cr.outputline("if ("+repairtable.getSafeSymbol()+")");
1062         cr.outputline("freeRepairHash("+repairtable.getSafeSymbol()+");");
1063         cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
1064         cr.outputline("free"+name+"("+oldmodel.getSafeSymbol()+");");
1065         cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1066         cr.outputline("freeWorkList("+worklist.getSafeSymbol()+");");
1067         cr.outputline("resettypemap();");
1068         cr.outputline("break;");
1069         cr.endblock();
1070         cr.outputline("rebuild:");
1071         cr.outputline(";");
1072     }
1073
1074     private MultUpdateNode getmultupdatenode(Conjunction conj, DNFPredicate dpred, int repairtype) {
1075         Set nodes=getmultupdatenodeset(conj,dpred,repairtype);
1076         Iterator it=nodes.iterator();
1077         if (it.hasNext())
1078             return (MultUpdateNode)it.next();
1079         else
1080             return null;
1081     }
1082
1083     private Set getmultupdatenodeset(Conjunction conj, DNFPredicate dpred, int repairtype) {
1084         HashSet hs=new HashSet();
1085         GraphNode gn=(GraphNode) termination.conjtonodemap.get(conj);
1086         for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1087             GraphNode gn2=((GraphNode.Edge) edgeit.next()).getTarget();
1088             TermNode tn2=(TermNode)gn2.getOwner();
1089             if (tn2.getType()==TermNode.ABSTRACT) {
1090                 AbstractRepair ar=tn2.getAbstract();
1091                 if (((repairtype==-1)||(ar.getType()==repairtype))&&
1092                     ar.getPredicate()==dpred) {
1093                     for(Iterator edgeit2=gn2.edges();edgeit2.hasNext();) {
1094                         GraphNode gn3=((GraphNode.Edge) edgeit2.next()).getTarget();
1095                         if (!removed.contains(gn3)) {
1096                             TermNode tn3=(TermNode)gn3.getOwner();
1097                             if (tn3.getType()==TermNode.UPDATE) {
1098                                 hs.add(tn3.getUpdate());
1099                             }
1100                         }
1101                     }
1102                 }
1103             }
1104         }
1105         return hs;
1106     }
1107
1108     private AbstractRepair getabstractrepair(Conjunction conj, DNFPredicate dpred, int repairtype) {
1109         HashSet hs=new HashSet();
1110         MultUpdateNode mun=null;
1111         GraphNode gn=(GraphNode) termination.conjtonodemap.get(conj);
1112         for(Iterator edgeit=gn.edges();(mun==null)&&edgeit.hasNext();) {
1113             GraphNode gn2=((GraphNode.Edge) edgeit.next()).getTarget();
1114             TermNode tn2=(TermNode)gn2.getOwner();
1115             if (tn2.getType()==TermNode.ABSTRACT) {
1116                 AbstractRepair ar=tn2.getAbstract();
1117                 if (((repairtype==-1)||(ar.getType()==repairtype))&&
1118                     ar.getPredicate()==dpred) {
1119                     return ar;
1120                 }
1121             }
1122         }
1123         return null;
1124     }
1125
1126
1127     /** Generates abstract (and concrete) repair for a comparison */
1128
1129     private void generatecomparisonrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
1130         Set updates=getmultupdatenodeset(conj,dpred,AbstractRepair.MODIFYRELATION);
1131         AbstractRepair ar=getabstractrepair(conj,dpred,AbstractRepair.MODIFYRELATION);
1132         MultUpdateNode munmodify=null;
1133         MultUpdateNode munadd=null;
1134         MultUpdateNode munremove=null;
1135         for(Iterator it=updates.iterator();it.hasNext();) {
1136             MultUpdateNode mun=(MultUpdateNode)it.next();
1137             if (mun.getType()==MultUpdateNode.ADD) {
1138                 munadd=mun;
1139             } else if (mun.getType()==MultUpdateNode.REMOVE) {
1140                 munremove=mun;
1141             } else if (mun.getType()==MultUpdateNode.MODIFY) {
1142                 munmodify=mun;
1143             }
1144         }
1145
1146         ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
1147         RelationDescriptor rd=(RelationDescriptor)ep.getDescriptor();
1148         boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1149         boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1150         boolean inverted=ep.inverted();
1151         boolean negated=dpred.isNegated();
1152         OpExpr expr=(OpExpr)ep.expr;
1153         Opcode opcode=expr.getOpcode();
1154         VarDescriptor leftside=VarDescriptor.makeNew("leftside");
1155         VarDescriptor rightside=VarDescriptor.makeNew("rightside");
1156         VarDescriptor newvalue=VarDescriptor.makeNew("newvalue");
1157         boolean needremoveloop=ar.mayNeedFunctionEnforcement(state)&&ar.needsRemoves(state);
1158
1159         if (needremoveloop&&((munadd==null)||(munremove==null))) {
1160             System.out.println("Warning:  need to have individual remove operations for"+dpred.name());
1161             needremoveloop=false;
1162         }
1163         if (needremoveloop) {
1164             cr.outputline("while (1)");
1165             cr.startblock();
1166         }
1167         if (!inverted) {
1168             ((RelationExpr)expr.getLeftExpr()).getExpr().generate(cr,leftside);
1169             expr.getRightExpr().generate(cr,newvalue);
1170             cr.addDeclaration(rd.getRange().getType().getGenerateType().getSafeSymbol(),rightside.getSafeSymbol());
1171             cr.outputline("SimpleHashget("+rd.getSafeSymbol()+"_hash,"+leftside.getSafeSymbol()+", &"+rightside.getSafeSymbol()+");");
1172         } else {
1173             ((RelationExpr)expr.getLeftExpr()).getExpr().generate(cr,rightside);
1174             expr.getRightExpr().generate(cr,newvalue);
1175             cr.outputline(rd.getDomain().getType().getGenerateType().getSafeSymbol()+" "+leftside.getSafeSymbol()+";");
1176             cr.outputline("SimpleHashget("+rd.getSafeSymbol()+"_hashinv,"+rightside.getSafeSymbol()+", &"+leftside.getSafeSymbol()+");");
1177         }
1178
1179         opcode=Opcode.translateOpcode(negated,opcode);
1180
1181         if (opcode==Opcode.GT) {
1182             cr.outputline(newvalue.getSafeSymbol()+"++;");
1183         } else if (opcode==Opcode.GE) {
1184             /* Equal */
1185         } else if (opcode==Opcode.LT) {
1186             cr.outputline(newvalue.getSafeSymbol()+"--;");
1187         } else if (opcode==Opcode.LE) {
1188             /* Equal */
1189         } else if (opcode==Opcode.EQ) {
1190             /* Equal */
1191         } else if (opcode==Opcode.NE) { /* search for FLAGNE if this is changed*/
1192             cr.outputline(newvalue.getSafeSymbol()+"++;");
1193         } else {
1194             throw new Error("Unrecognized Opcode");
1195         }
1196         /* Do abstract repairs */
1197         if (usageimage) {
1198             cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash, "+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1199         }
1200         if (usageinvimage) {
1201             cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv, "+rightside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
1202         }
1203
1204         if (needremoveloop) {
1205             if (!inverted) {
1206                 cr.outputline("if (SimpleHashcontainskey("+rd.getSafeSymbol()+"_hash, "+leftside.getSafeSymbol()+"))");
1207                 cr.startblock();
1208             } else {
1209                 cr.outputline("if (SimpleHashcontainskey("+rd.getSafeSymbol()+"_hashinv, "+rightside.getSafeSymbol()+"))");
1210                 cr.startblock();
1211             }
1212             for(int i=0;i<state.vRules.size();i++) {
1213                 Rule r=(Rule)state.vRules.get(i);
1214                 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1215                     for(int j=0;j<munremove.numUpdates();j++) {
1216                         UpdateNode un=munremove.getUpdate(i);
1217                         if (un.getRule()==r) {
1218                                 /* Update for rule r */
1219                             String name=(String)updatenames.get(un);
1220                             cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+");");
1221                         }
1222                     }
1223                 }
1224             }
1225             cr.outputline("continue;");
1226             cr.endblock();
1227         }
1228
1229         if (usageimage) {
1230             if (!inverted) {
1231                 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash,"+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1232             } else {
1233                 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, "+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1234             }
1235         }
1236         if (usageinvimage) {
1237             if (!inverted) {
1238                 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, "+newvalue.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
1239             } else {
1240                 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv,"+rightside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1241             }
1242         }
1243         /* Do concrete repairs */
1244         if (munmodify!=null&&(!ar.mayNeedFunctionEnforcement(state))||(munadd==null)||(ar.needsRemoves(state)&&(munremove==null))) {
1245             for(int i=0;i<state.vRules.size();i++) {
1246                 Rule r=(Rule)state.vRules.get(i);
1247                 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1248                     for(int j=0;j<munmodify.numUpdates();j++) {
1249                         UpdateNode un=munmodify.getUpdate(j);
1250                         if (un.getRule()==r) {
1251                             /* Update for rule r */
1252                             String name=(String)updatenames.get(un);
1253                             cr.outputline("RepairHashaddrelation2("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+","+newvalue.getSafeSymbol()+");");
1254                         }
1255                     }
1256                 }
1257             }
1258         } else {
1259             /* Start with scheduling removal */
1260             if (ar.needsRemoves(state))
1261                 for(int i=0;i<state.vRules.size();i++) {
1262                     Rule r=(Rule)state.vRules.get(i);
1263                     if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1264                         for(int j=0;j<munremove.numUpdates();j++) {
1265                             UpdateNode un=munremove.getUpdate(i);
1266                             if (un.getRule()==r) {
1267                                 /* Update for rule r */
1268                                 String name=(String)updatenames.get(un);
1269                                 cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+");");
1270                             }
1271                         }
1272                     }
1273                 }
1274             /* Now do addition */
1275             UpdateNode un=munadd.getUpdate(0);
1276             String name=(String)updatenames.get(un);
1277             if (!inverted) {
1278                 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1279             } else {
1280                 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1281             }
1282         }
1283         if (needremoveloop) {
1284             cr.outputline("break;");
1285             cr.endblock();
1286         }
1287     }
1288
1289     public void generatesizerepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr) {
1290         ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
1291         OpExpr expr=(OpExpr)ep.expr;
1292         Opcode opcode=expr.getOpcode();
1293         opcode=Opcode.translateOpcode(dpred.isNegated(),opcode);
1294
1295         MultUpdateNode munremove;
1296
1297         MultUpdateNode munadd;
1298         if (ep.getDescriptor() instanceof RelationDescriptor) {
1299             munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMRELATION);
1300             munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTORELATION);
1301         } else {
1302             munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMSET);
1303             munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTOSET);
1304         }
1305         int size=ep.rightSize();
1306         VarDescriptor sizevar=VarDescriptor.makeNew("size");
1307         ((OpExpr)expr).left.generate(cr, sizevar);
1308         VarDescriptor change=VarDescriptor.makeNew("change");
1309         cr.addDeclaration("int",change.getSafeSymbol());
1310         boolean generateadd=false;
1311         boolean generateremove=false;
1312         if (opcode==Opcode.GT) {
1313             cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
1314             generateadd=true;
1315             generateremove=false;
1316         } else if (opcode==Opcode.GE) {
1317             cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1318             generateadd=true;
1319             generateremove=false;
1320         } else if (opcode==Opcode.LT) {
1321             cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size-1)+"-"+sizevar.getSafeSymbol()+";");
1322             generateadd=false;
1323             generateremove=true;
1324         } else if (opcode==Opcode.LE) {
1325             cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1326             generateadd=false;
1327             generateremove=true;
1328         } else if (opcode==Opcode.EQ) {
1329             cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1330             if (size==0)
1331                 generateadd=false;
1332             else
1333                 generateadd=true;
1334             generateremove=true;
1335         } else if (opcode==Opcode.NE) {
1336             cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
1337             generateadd=true;
1338             generateremove=false;
1339         } else {
1340             throw new Error("Unrecognized Opcode");
1341         }
1342
1343 // In some cases the analysis has determined that generating removes
1344 // is unnecessary
1345         if (generateremove&&munremove==null)
1346             generateremove=false;
1347
1348         Descriptor d=ep.getDescriptor();
1349         if (generateremove) {
1350             cr.outputline("for(;"+change.getSafeSymbol()+"<0;"+change.getSafeSymbol()+"++)");
1351             cr.startblock();
1352             /* Find element to remove */
1353             VarDescriptor leftvar=VarDescriptor.makeNew("leftvar");
1354             VarDescriptor rightvar=VarDescriptor.makeNew("rightvar");
1355             if (d instanceof RelationDescriptor) {
1356                 if (ep.inverted()) {
1357                     ((ImageSetExpr)((SizeofExpr)expr.left).setexpr).generate_leftside(cr,rightvar);
1358                     cr.addDeclaration("int",leftvar.getSafeSymbol());
1359                     cr.outputline("SimpleHashget("+d.getSafeSymbol()+"_hashinv,(int)"+rightvar.getSafeSymbol()+", &"+leftvar.getSafeSymbol()+");");
1360                 } else {
1361                     ((ImageSetExpr)((SizeofExpr)expr.left).setexpr).generate_leftside(cr,leftvar);
1362                     cr.addDeclaration("int",rightvar.getSafeSymbol());
1363                     cr.outputline("SimpleHashget("+d.getSafeSymbol()+"_hash ,(int)"+leftvar.getSafeSymbol()+", &"+rightvar.getSafeSymbol()+");");
1364                 }
1365             } else {
1366                 cr.addDeclaration("int",leftvar.getSafeSymbol());
1367                 cr.outputline(leftvar.getSafeSymbol()+"= SimpleHashfirstkey("+d.getSafeSymbol()+"_hash);");
1368             }
1369             /* Generate abstract remove instruction */
1370             if (d instanceof RelationDescriptor) {
1371                 RelationDescriptor rd=(RelationDescriptor) d;
1372                 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1373                 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1374                 if (usageimage)
1375                     cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1376                 if (usageinvimage)
1377                     cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv ,(int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1378             } else {
1379                 cr.outputline("SimpleHashremove("+d.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1380             }
1381             /* Generate concrete remove instruction */
1382             for(int i=0;i<state.vRules.size();i++) {
1383                 Rule r=(Rule)state.vRules.get(i);
1384                 if (r.getInclusion().getTargetDescriptors().contains(d)) {
1385                     for(int j=0;j<munremove.numUpdates();j++) {
1386                         UpdateNode un=munremove.getUpdate(j);
1387                         if (un.getRule()==r) {
1388                                 /* Update for rule rule r */
1389                             String name=(String)updatenames.get(un);
1390                             if (d instanceof RelationDescriptor) {
1391                                 cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
1392                             } else {
1393                                 cr.outputline("RepairHashaddset("+repairtable.getSafeSymbol()+","+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1394                             }
1395                         }
1396                     }
1397                 }
1398             }
1399             cr.endblock();
1400         }
1401
1402 // In some cases the analysis has determined that generating removes
1403 // is unnecessary
1404         if (generateadd&&munadd==null)
1405             generateadd=false;
1406
1407         if (generateadd) {
1408
1409             cr.outputline("for(;"+change.getSafeSymbol()+">0;"+change.getSafeSymbol()+"--)");
1410             cr.startblock();
1411             VarDescriptor newobject=VarDescriptor.makeNew("newobject");
1412             if (d instanceof RelationDescriptor) {
1413                 VarDescriptor otherside=VarDescriptor.makeNew("otherside");
1414                 ((ImageSetExpr)((SizeofExpr)expr.left).setexpr).generate_leftside(cr,otherside);
1415
1416                 RelationDescriptor rd=(RelationDescriptor)d;
1417                 if (termination.sources.relsetSource(rd,!ep.inverted())) {
1418                     /* Set Source */
1419                     SetDescriptor sd=termination.sources.relgetSourceSet(rd,!ep.inverted());
1420                     VarDescriptor iterator=VarDescriptor.makeNew("iterator");
1421                     cr.addDeclaration(sd.getType().getGenerateType().getSafeSymbol(),newobject.getSafeSymbol());
1422                     cr.addDeclaration("struct SimpleIterator",iterator.getSafeSymbol());
1423                     cr.outputline("for(SimpleHashiterator("+sd.getSafeSymbol()+"_hash , &"+ iterator.getSafeSymbol() +"); hasNext(&"+iterator.getSafeSymbol()+");)");
1424                     cr.startblock();
1425                     if (ep.inverted()) {
1426                         cr.outputline("if (!SimpleHashcontainskeydata("+rd.getSafeSymbol()+"_hashinv,"+iterator.getSafeSymbol()+".key(),"+otherside.getSafeSymbol()+"))");
1427                     } else {
1428                         cr.outputline("if (!SimpleHashcontainskeydata("+rd.getSafeSymbol()+"_hash, "+otherside.getSafeSymbol()+","+iterator.getSafeSymbol()+".key()))");
1429                     }
1430                     cr.outputline(newobject.getSafeSymbol()+"=key(&"+iterator.getSafeSymbol()+");");
1431                     cr.outputline("next(&"+iterator.getSafeSymbol()+");");
1432                     cr.endblock();
1433                 } else if (termination.sources.relallocSource(rd,!ep.inverted())) {
1434                     /* Allocation Source*/
1435                     termination.sources.relgenerateSourceAlloc(cr,newobject,rd,!ep.inverted());
1436                 } else throw new Error("No source for adding to Relation");
1437                 if (ep.inverted()) {
1438                     boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1439                     boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1440                     if (usageimage)
1441                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, "+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1442                     if (usageinvimage)
1443                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, "+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1444
1445                     UpdateNode un=munadd.getUpdate(0);
1446                     String name=(String)updatenames.get(un);
1447                     cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1448                 } else {
1449                     boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1450                     boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1451                     if (usageimage)
1452                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, "+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1453                     if (usageinvimage)
1454                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, "+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1455                     UpdateNode un=munadd.getUpdate(0);
1456                     String name=(String)updatenames.get(un);
1457                     cr.outputline(name+"(thisvar, "+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1458                 }
1459             } else {
1460                 SetDescriptor sd=(SetDescriptor)d;
1461                 if (termination.sources.setSource(sd)) {
1462                     /* Set Source */
1463                     /* Set Source */
1464                     SetDescriptor sourcesd=termination.sources.getSourceSet(sd);
1465                     VarDescriptor iterator=VarDescriptor.makeNew("iterator");
1466                     cr.addDeclaration(sourcesd.getType().getGenerateType().getSafeSymbol(), newobject.getSafeSymbol());
1467                     cr.addDeclaration("struct SimpleIterator",iterator.getSafeSymbol());
1468                     cr.outputline("for(SimpleHashiterator("+sourcesd.getSafeSymbol()+"_hash, &"+iterator.getSafeSymbol()+"); hasNext(&"+iterator.getSafeSymbol()+");)");
1469                     cr.startblock();
1470                     cr.outputline("if (!SimpleHashcontainskey("+sd.getSafeSymbol()+"_hash, key(&"+iterator.getSafeSymbol()+")))");
1471                     cr.outputline(newobject.getSafeSymbol()+"=key(&"+iterator.getSafeSymbol()+");");
1472                     cr.outputline("next(&"+iterator.getSafeSymbol()+");");
1473                     cr.endblock();
1474                 } else if (termination.sources.allocSource(sd)) {
1475                     /* Allocation Source*/
1476                     termination.sources.generateSourceAlloc(cr,newobject,sd);
1477                 } else throw new Error("No source for adding to Set");
1478                 cr.outputline("SimpleHashadd("+sd.getSafeSymbol()+"_hash, "+newobject.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1479                 UpdateNode un=munadd.getUpdate(0);
1480                 String name=(String)updatenames.get(un);
1481                 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1482             }
1483             cr.endblock();
1484         }
1485     }
1486
1487     public void generateinclusionrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
1488         InclusionPredicate ip=(InclusionPredicate) dpred.getPredicate();
1489         boolean negated=dpred.isNegated();
1490         MultUpdateNode mun=getmultupdatenode(conj,dpred,-1);
1491         VarDescriptor leftvar=VarDescriptor.makeNew("left");
1492         ip.expr.generate(cr, leftvar);
1493
1494         if (negated) {
1495             if (ip.setexpr instanceof ImageSetExpr) {
1496                 ImageSetExpr ise=(ImageSetExpr) ip.setexpr;
1497                 VarDescriptor rightvar=ise.getVar();
1498                 boolean inverse=ise.inverted();
1499                 RelationDescriptor rd=ise.getRelation();
1500                 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1501                 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1502                 if (inverse) {
1503                     if (usageimage)
1504                         cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1505                     if (usageinvimage)
1506                         cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1507                 } else {
1508                     if (usageimage)
1509                         cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash ,(int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1510                     if (usageinvimage)
1511                         cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1512                 }
1513                 for(int i=0;i<state.vRules.size();i++) {
1514                     Rule r=(Rule)state.vRules.get(i);
1515                     if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1516                         for(int j=0;j<mun.numUpdates();j++) {
1517                             UpdateNode un=mun.getUpdate(j);
1518                             if (un.getRule()==r) {
1519                                 /* Update for rule rule r */
1520                                 String name=(String)updatenames.get(un);
1521                                 if (inverse) {
1522                                     cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1523                                 } else {
1524                                     cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
1525                                 }
1526                             }
1527                         }
1528                     }
1529                 }
1530             } else {
1531                 SetDescriptor sd=ip.setexpr.sd;
1532                 cr.outputline("SimpleHashremove("+sd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1533
1534                 for(int i=0;i<state.vRules.size();i++) {
1535                     Rule r=(Rule)state.vRules.get(i);
1536                     if (r.getInclusion().getTargetDescriptors().contains(sd)) {
1537                         for(int j=0;j<mun.numUpdates();j++) {
1538                             UpdateNode un=mun.getUpdate(j);
1539                             if (un.getRule()==r) {
1540                                 /* Update for rule rule r */
1541                                 String name=(String)updatenames.get(un);
1542                                 cr.outputline("RepairHashaddset("+repairtable.getSafeSymbol()+","+sd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1543                             }
1544                         }
1545                     }
1546                 }
1547             }
1548         } else {
1549             /* Generate update */
1550             if (ip.setexpr instanceof ImageSetExpr) {
1551                 ImageSetExpr ise=(ImageSetExpr) ip.setexpr;
1552                 VarDescriptor rightvar=ise.getVar();
1553                 boolean inverse=ise.inverted();
1554                 RelationDescriptor rd=ise.getRelation();
1555                 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1556                 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1557                 if (inverse) {
1558                     if (usageimage)
1559                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1560                     if (usageinvimage)
1561                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1562                 } else {
1563                     if (usageimage)
1564                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1565                     if (usageinvimage)
1566                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1567                 }
1568                 UpdateNode un=mun.getUpdate(0);
1569                 String name=(String)updatenames.get(un);
1570                 if (inverse) {
1571                     cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1572                 } else {
1573                     cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+");");
1574                 }
1575             } else {
1576                 SetDescriptor sd=ip.setexpr.sd;
1577                 cr.outputline("SimpleHashadd("+sd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1578
1579                 UpdateNode un=mun.getUpdate(0);
1580                 /* Update for rule rule r */
1581                 String name=(String)updatenames.get(un);
1582                 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1583             }
1584         }
1585     }
1586
1587     public static Vector getrulelist(Descriptor d) {
1588         Vector dispatchrules = new Vector();
1589         Vector rules = State.currentState.vRules;
1590
1591         for (int i = 0; i < rules.size(); i++) {
1592             Rule rule = (Rule) rules.elementAt(i);
1593             Set requiredsymbols = rule.getRequiredDescriptors();
1594
1595             // #TBD#: in general this is wrong because these descriptors may contain descriptors
1596             // bound in "in?" expressions which need to be dealt with in a topologically sorted
1597             // fashion...
1598
1599             if (rule.getRequiredDescriptors().contains(d)) {
1600                 dispatchrules.addElement(rule);
1601             }
1602         }
1603         return dispatchrules;
1604     }
1605
1606     private boolean need_compensation(Rule r) {
1607         if (!Compiler.REPAIR)
1608             return false;
1609         GraphNode gn=(GraphNode)termination.scopefalsify.get(r);
1610         for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1611             GraphNode.Edge edge=(GraphNode.Edge)edgeit.next();
1612             GraphNode gn2=edge.getTarget();
1613             if (!removed.contains(gn2)) {
1614                 TermNode tn2=(TermNode)gn2.getOwner();
1615                 if (tn2.getType()==TermNode.CONSEQUENCE)
1616                     return false;
1617             }
1618         }
1619         return true;
1620     }
1621
1622     private UpdateNode find_compensation(Rule r) {
1623         GraphNode gn=(GraphNode)termination.scopefalsify.get(r);
1624         for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1625             GraphNode.Edge edge=(GraphNode.Edge)edgeit.next();
1626             GraphNode gn2=edge.getTarget();
1627             if (!removed.contains(gn2)) {
1628                 TermNode tn2=(TermNode)gn2.getOwner();
1629                 if (tn2.getType()==TermNode.UPDATE) {
1630                     MultUpdateNode mun=tn2.getUpdate();
1631                     for(int i=0;i<mun.numUpdates();i++) {
1632                         UpdateNode un=mun.getUpdate(i);
1633                         if (un.getRule()==r)
1634                             return un;
1635                     }
1636                 }
1637             }
1638         }
1639         throw new Error("No Compensation Update could be found");
1640     }
1641
1642     public void generate_dispatch(CodeWriter cr, RelationDescriptor rd, String leftvar, String rightvar) {
1643         boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1644         boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1645
1646         if (!(usageinvimage||usageimage)) /* not used at all*/
1647             return;
1648
1649         cr.outputline("/* RELATION DISPATCH */");
1650         if (Compiler.REPAIR) {
1651             cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1652             if (usageimage)
1653                 cr.outputline("!SimpleHashcontainskeydata("+oldmodel.getSafeSymbol()+"->"+rd.getJustSafeSymbol() + "_hash, "+leftvar+","+rightvar+"))");
1654             else
1655                 cr.outputline("!SimpleHashcontainskeydata("+oldmodel.getSafeSymbol() +"->"+rd.getJustSafeSymbol()+"_hashinv, "+rightvar+","+leftvar+"))");
1656
1657             cr.startblock(); {
1658                 /* Adding new item */
1659                 /* Perform safety checks */
1660                 cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
1661                 cr.outputline("RepairHashcontainsrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+"))");
1662                 cr.startblock(); {
1663                     /* Have update to call into */
1664                     VarDescriptor mdfyptr=VarDescriptor.makeNew("modifyptr");
1665                     VarDescriptor ismdfyptr=VarDescriptor.makeNew("ismodifyptr");
1666                     cr.addDeclaration("int ",ismdfyptr.getSafeSymbol());
1667                     cr.outputline(ismdfyptr.getSafeSymbol()+"=RepairHashismodify("+repairtable.getSafeSymbol()+","+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1668
1669
1670
1671
1672                     String parttype="";
1673                     for(int i=0;i<currentrule.numQuantifiers();i++) {
1674                         if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1675                             parttype=parttype+", int, int";
1676                         else
1677                             parttype=parttype+", int";
1678                     }
1679
1680                     VarDescriptor tmpptr=VarDescriptor.makeNew("tempupdateptr");
1681
1682                     String methodcall="(thisvar,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
1683                     for(int i=0;i<currentrule.numQuantifiers();i++) {
1684                         Quantifier q=currentrule.getQuantifier(i);
1685                         if (q instanceof SetQuantifier) {
1686                             SetQuantifier sq=(SetQuantifier) q;
1687                             methodcall+=","+sq.getVar().getSafeSymbol();
1688                         } else if (q instanceof RelationQuantifier) {
1689                             RelationQuantifier rq=(RelationQuantifier) q;
1690                             methodcall+=","+rq.x.getSafeSymbol();
1691                             methodcall+=","+rq.y.getSafeSymbol();
1692                         } else if (q instanceof ForQuantifier) {
1693                             ForQuantifier fq=(ForQuantifier) q;
1694                             methodcall+=","+fq.getVar().getSafeSymbol();
1695                         }
1696                     }
1697
1698
1699
1700                     cr.addDeclaration("void *",tmpptr.getSafeSymbol());
1701                     cr.outputline(tmpptr.getSafeSymbol()+"=");
1702                     cr.outputline("(void *) RepairHashgetrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1703                     cr.outputline("if ("+ismdfyptr.getSafeSymbol()+")");
1704                     {
1705                         VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1706                         String methodcallprefix="("+funptr.getSafeSymbol()+") ";
1707                         cr.startblock();
1708                         cr.addDeclaration("int",mdfyptr.getSafeSymbol());
1709                         cr.outputline(mdfyptr.getSafeSymbol()+"=RepairHashgetrelation2("+repairtable.getSafeSymbol()+","+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1710                         cr.addDeclaration("void (*"+funptr.getSafeSymbol()+") (struct "+name+"_state *, struct "+name+"*, struct RepairHash *"+parttype+",int,int,int);");
1711                         cr.outputline(funptr.getSafeSymbol()+"="+"(void (*) (struct "+name+"_state *, struct "+name+"*, struct RepairHash *"+parttype+",int,int,int)) "+tmpptr.getSafeSymbol()+";");
1712                         cr.outputline(methodcallprefix+methodcall+","+leftvar+", "+rightvar+", "+mdfyptr.getSafeSymbol() +");");
1713                         cr.endblock();
1714                     }
1715                     cr.outputline("else ");
1716                     {
1717                         VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1718                         String methodcallprefix="("+funptr.getSafeSymbol()+") ";
1719                         cr.startblock();
1720                         cr.addDeclaration("void (*"+funptr.getSafeSymbol()+") (struct "+name+"_state *, struct "+name+"*,struct RepairHash *"+parttype+");");
1721                         cr.outputline(funptr.getSafeSymbol()+"="+"(void (*) (struct "+name+"_state *,struct "+name+"*,struct RepairHash *"+parttype+")) "+tmpptr.getSafeSymbol()+";");
1722                         cr.outputline(methodcallprefix+methodcall+");");
1723                         cr.endblock();
1724                     }
1725                     cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1726                     cr.outputline("goto rebuild;");
1727                 }
1728                 cr.endblock();
1729
1730                 /* Build standard compensation actions */
1731                 if (need_compensation(currentrule)) {
1732                     UpdateNode un=find_compensation(currentrule);
1733                     String name=(String)updatenames.get(un);
1734                     usedupdates.add(un); /* Mark as used */
1735                     String methodcall=name+"(thisvar,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
1736                     for(int i=0;i<currentrule.numQuantifiers();i++) {
1737                         Quantifier q=currentrule.getQuantifier(i);
1738                         if (q instanceof SetQuantifier) {
1739                             SetQuantifier sq=(SetQuantifier) q;
1740                             methodcall+=","+sq.getVar().getSafeSymbol();
1741                         } else if (q instanceof RelationQuantifier) {
1742                             RelationQuantifier rq=(RelationQuantifier) q;
1743                             methodcall+=","+rq.x.getSafeSymbol();
1744                             methodcall+=","+rq.y.getSafeSymbol();
1745                         } else if (q instanceof ForQuantifier) {
1746                             ForQuantifier fq=(ForQuantifier) q;
1747                             methodcall+=","+fq.getVar().getSafeSymbol();
1748                         }
1749                     }
1750                     methodcall+=");";
1751                     cr.outputline(methodcall);
1752                     cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1753                     cr.outputline("goto rebuild;");
1754                 }
1755             }
1756             cr.endblock();
1757         }
1758
1759         String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1760         cr.startblock();
1761         cr.addDeclaration("int" , addeditem);
1762         cr.outputline(addeditem + "=0;");
1763
1764         String ifstring="if (!maybe";
1765         if (rd.getDomain().getType() instanceof StructureTypeDescriptor)  {
1766             ifstring+="&&"+leftvar;
1767         }
1768
1769         if (rd.getRange().getType() instanceof StructureTypeDescriptor)  {
1770             ifstring+="&&"+rightvar;
1771         }
1772
1773         ifstring+=")";
1774
1775         if (rd.testUsage(RelationDescriptor.IMAGE)) {
1776             cr.outputline(ifstring);
1777             cr.outputline(addeditem + " = SimpleHashadd("+rd.getSafeSymbol()+"_hash, (int)" + leftvar + ", (int)" + rightvar+ ");");
1778         }
1779
1780         if (rd.testUsage(RelationDescriptor.INVIMAGE)) {
1781             cr.outputline(ifstring);
1782             cr.outputline(addeditem + " = SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, (int)" + rightvar + ", (int)" + leftvar + ");");
1783         }
1784
1785
1786         Vector dispatchrules = getrulelist(rd);
1787
1788         Set toremove=new HashSet();
1789         for(int i=0;i<dispatchrules.size();i++) {
1790             Rule r=(Rule)dispatchrules.get(i);
1791             if (!ruleset.contains(r))
1792                 toremove.add(r);
1793         }
1794         dispatchrules.removeAll(toremove);
1795         if (dispatchrules.size() == 0) {
1796             cr.outputline("/* nothing to dispatch */");
1797             cr.endblock();
1798             return;
1799         }
1800
1801         cr.outputline("if (" + addeditem + ")");
1802         cr.startblock();
1803
1804         for(int i = 0; i < dispatchrules.size(); i++) {
1805             Rule rule = (Rule) dispatchrules.elementAt(i);
1806             if (rule.getGuardExpr().getRequiredDescriptors().contains(rd)) {
1807                 /* Guard depends on this relation, so we recomput everything */
1808                 cr.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+",-1,0,0);");
1809             } else {
1810                 for (int j=0;j<rule.numQuantifiers();j++) {
1811                     Quantifier q=rule.getQuantifier(j);
1812                     if (q.getRequiredDescriptors().contains(rd)) {
1813                         /* Generate add */
1814                         cr.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+","+j+","+leftvar+","+rightvar+");");
1815                     }
1816                 }
1817             }
1818         }
1819         cr.endblock();
1820         cr.endblock();
1821     }
1822
1823
1824     public void generate_dispatch(CodeWriter cr, SetDescriptor sd, String setvar) {
1825         cr.outputline("/* SET DISPATCH */");
1826         if (Compiler.REPAIR) {
1827             cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1828             cr.outputline("!SimpleHashcontainskey("+oldmodel.getSafeSymbol() +"->"+sd.getJustSafeSymbol()+"_hash, "+setvar+"))");
1829             cr.startblock(); {
1830                 /* Adding new item */
1831                 /* See if there is an outstanding update in the repairtable */
1832                 cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
1833                 cr.outputline("RepairHashcontainsset("+repairtable.getSafeSymbol()+","+sd.getNum()+","+currentrule.getNum()+","+setvar+"))");
1834                 cr.startblock(); {
1835                     /* Have update to call into */
1836                     VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1837                     String parttype="";
1838                     for(int i=0;i<currentrule.numQuantifiers();i++) {
1839                         if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1840                             parttype=parttype+", int, int";
1841                         else
1842                             parttype=parttype+", int";
1843                     }
1844                     cr.addDeclaration("void (*"+funptr.getSafeSymbol()+") (struct "+name+"_state *,struct "+name+"*,struct RepairHash *"+parttype+");");
1845                     cr.outputline(funptr.getSafeSymbol()+"=");
1846                     cr.outputline("(void (*) (struct "+name+"_state *,struct "+name+"*,struct RepairHash *"+parttype+")) RepairHashgetset("+repairtable.getSafeSymbol()+","+sd.getNum()+","+currentrule.getNum()+","+setvar+");");
1847                     String methodcall="("+funptr.getSafeSymbol()+") (thisvar,"+oldmodel.getSafeSymbol()+","+
1848                         repairtable.getSafeSymbol();
1849                     for(int i=0;i<currentrule.numQuantifiers();i++) {
1850                         Quantifier q=currentrule.getQuantifier(i);
1851                         if (q instanceof SetQuantifier) {
1852                             SetQuantifier sq=(SetQuantifier) q;
1853                             methodcall+=","+sq.getVar().getSafeSymbol();
1854                         } else if (q instanceof RelationQuantifier) {
1855                             RelationQuantifier rq=(RelationQuantifier) q;
1856                             methodcall+=","+rq.x.getSafeSymbol();
1857                             methodcall+=","+rq.y.getSafeSymbol();
1858                         } else if (q instanceof ForQuantifier) {
1859                             ForQuantifier fq=(ForQuantifier) q;
1860                             methodcall+=","+fq.getVar().getSafeSymbol();
1861                         }
1862                     }
1863                     methodcall+=");";
1864                     cr.outputline(methodcall);
1865                     cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1866                     cr.outputline("goto rebuild;");
1867                 }
1868                 cr.endblock();
1869                 /* Build standard compensation actions */
1870                 Vector ruleset=new Vector();
1871                 ruleset.add(currentrule);
1872                 if (state.implicitruleinv.containsKey(currentrule))
1873                     ruleset.addAll((Set)state.implicitruleinv.get(currentrule));
1874                 for(int i=0;i<ruleset.size();i++) {
1875                     Rule itrule=(Rule)ruleset.get(i);
1876
1877                     if (need_compensation(itrule)) {
1878                         UpdateNode un=find_compensation(itrule);
1879                         String name=(String)updatenames.get(un);
1880                         usedupdates.add(un); /* Mark as used */
1881
1882                         String methodcall=name+"(thisvar,"+oldmodel.getSafeSymbol()+","+
1883                             repairtable.getSafeSymbol();
1884                         for(int j=0;j<currentrule.numQuantifiers();j++) {
1885                             Quantifier q=currentrule.getQuantifier(j);
1886                             if (q instanceof SetQuantifier) {
1887                                 SetQuantifier sq=(SetQuantifier) q;
1888                                 methodcall+=","+sq.getVar().getSafeSymbol();
1889                             } else if (q instanceof RelationQuantifier) {
1890                                 RelationQuantifier rq=(RelationQuantifier) q;
1891                                 methodcall+=","+rq.x.getSafeSymbol();
1892                                 methodcall+=","+rq.y.getSafeSymbol();
1893                             } else if (q instanceof ForQuantifier) {
1894                                 ForQuantifier fq=(ForQuantifier) q;
1895                                 methodcall+=","+fq.getVar().getSafeSymbol();
1896                             }
1897                         }
1898                         methodcall+=");";
1899                         if (currentrule!=itrule) {
1900                             SetDescriptor sdrule=((SetInclusion)itrule.getInclusion()).getSet();
1901                             cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1902                             cr.outputline("!SimpleHashcontainskey("+ oldmodel.getSafeSymbol() +"->"+sdrule.getJustSafeSymbol() +"_hash,"+setvar+"))");
1903                             cr.startblock();
1904                         }
1905                         cr.outputline(methodcall);
1906                         cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1907                         cr.outputline("goto rebuild;");
1908                         cr.endblock();
1909                     }
1910                     if (currentrule==itrule)
1911                         cr.endblock();
1912                 }
1913             }
1914         }
1915
1916         cr.startblock();
1917         String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1918         cr.addDeclaration("int", addeditem);
1919         cr.outputline(addeditem + " = 0;");
1920         if (sd.getType() instanceof StructureTypeDescriptor)  {
1921             cr.outputline("if (!maybe&&"+setvar+")");
1922         } else
1923             cr.outputline("if (!maybe)");
1924         cr.outputline(addeditem + " = SimpleHashadd("+sd.getSafeSymbol()+"_hash, (int)" + setvar +  ", (int)" + setvar + ");");
1925         cr.startblock();
1926         Vector dispatchrules = getrulelist(sd);
1927
1928         Set toremove=new HashSet();
1929         for(int i=0;i<dispatchrules.size();i++) {
1930             Rule r=(Rule)dispatchrules.get(i);
1931             if (!ruleset.contains(r))
1932                 toremove.add(r);
1933         }
1934         dispatchrules.removeAll(toremove);
1935
1936         if (dispatchrules.size() == 0) {
1937             cr.outputline("/* nothing to dispatch */");
1938             cr.endblock();
1939             cr.endblock();
1940             return;
1941         }
1942         /* Add item to worklist if new */
1943         cr.outputline("if ("+addeditem+")");
1944         cr.startblock();
1945         for(int i = 0; i < dispatchrules.size(); i++) {
1946             Rule rule = (Rule) dispatchrules.elementAt(i);
1947             if (SetDescriptor.expand(rule.getGuardExpr().getRequiredDescriptors()).contains(sd)) {
1948                 /* Guard depends on this relation, so we recompute everything */
1949                 cr.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+",-1,0,0);");
1950             } else {
1951                 for (int j=0;j<rule.numQuantifiers();j++) {
1952                     Quantifier q=rule.getQuantifier(j);
1953                     if (SetDescriptor.expand(q.getRequiredDescriptors()).contains(sd)) {
1954                         /* Generate add */
1955                         cr.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+","+j+","+setvar+",0);");
1956                     }
1957                 }
1958             }
1959         }
1960         cr.endblock();
1961         cr.endblock();
1962         cr.endblock();
1963     }
1964 }