80400dc0a84e4bf56aa941dd58904ebc07b96faf
[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("computesizes(thisvar);");
572         craux.outputline(name+"_staterecomputesizes(thisvar);");
573         craux.outputline("while (1)");
574         craux.startblock();
575         craux.addDeclaration("struct "+name+ " * ",newmodel.getSafeSymbol());
576         craux.outputline(newmodel.getSafeSymbol()+"=allocate"+name+"();");
577         craux.outputline("WorkListreset("+worklist.getSafeSymbol()+");");
578         if (Compiler.GENERATEINSTRUMENT)
579             craux.outputline("rebuildcount++;");
580     }
581
582     private void generate_teardown() {
583         CodeWriter cr = new StandardCodeWriter(outputaux);
584         cr.endblock();
585         if (Compiler.TIME) {
586             cr.outputline("gettimeofday(&_end_time,NULL);");
587             cr.outputline("printf(\"time=%ld uS\\n\",(_end_time.tv_sec-_begin_time.tv_sec)*1000000+_end_time.tv_usec-_begin_time.tv_usec);");
588         }
589
590         if (Compiler.GENERATEINSTRUMENT) {
591             cr.outputline("printf(\"updatecount=%d\\n\",updatecount);");
592             cr.outputline("printf(\"rebuildcount=%d\\n\",rebuildcount);");
593             cr.outputline("printf(\"abstractcount=%d\\n\",abstractcount);");
594         }
595
596     }
597
598     private void generate_print() {
599
600         final SymbolTable st = new SymbolTable();
601
602         CodeWriter cr = new StandardCodeWriter(outputaux) {
603                 public SymbolTable getSymbolTable() { return st; }
604             };
605
606         cr.outputline("/* printing sets!*/");
607         cr.outputline("printf(\"\\n\\nPRINTING SETS AND RELATIONS\\n\");");
608
609         Iterator setiterator = state.stSets.descriptors();
610         while (setiterator.hasNext()) {
611             SetDescriptor sd = (SetDescriptor) setiterator.next();
612             if (sd.getSymbol().equals("int") || sd.getSymbol().equals("token")) {
613                 continue;
614             }
615
616             String setname = sd.getSafeSymbol();
617
618             cr.startblock();
619             cr.outputline("/* printing set " + setname+"*/");
620             cr.outputline("printf(\"\\nPrinting set " + sd.getSymbol() + " - %d elements \\n\", SimpleHashcountset("+setname+"_hash));");
621             cr.addDeclaration("struct SimpleIterator","__setiterator");
622             cr.outputline("SimpleHashiterator("+setname+"_hash,&__setiterator);");
623             cr.outputline("while (hasNext(&__setiterator))");
624             cr.startblock();
625             cr.addDeclaration("int","__setval");
626             cr.outputline("__setval = (int) next(&__setiterator);");
627
628             TypeDescriptor td = sd.getType();
629             if (td instanceof StructureTypeDescriptor) {
630                 StructureTypeDescriptor std = (StructureTypeDescriptor) td;
631                 VarDescriptor vd = new VarDescriptor ("__setval", "__setval", td, false);
632                 std.generate_printout(cr, vd);
633             } else { // Missing type descriptor or reserved type, just print int
634                 cr.outputline("printf(\"<%d> \", __setval);");
635             }
636
637
638             cr.endblock();
639             cr.endblock();
640         }
641
642         Iterator reliterator = state.stRelations.descriptors();
643         while (reliterator.hasNext()) {
644             RelationDescriptor rd = (RelationDescriptor) reliterator.next();
645
646
647             String relname = rd.getSafeSymbol();
648             if (rd.testUsage(RelationDescriptor.IMAGE)) {
649                 cr.startblock();
650                 cr.outputline("/* printing relation " + relname+"*/");
651                 cr.outputline("printf(\"\\nPrinting relation " + rd.getSymbol() + " - %d elements \\n\", SimpleHashcountset("+relname+"_hash));");
652                 cr.addDeclaration("struct SimpleIterator","__reliterator");
653                 cr.outputline("SimpleHashiterator("+relname+"_hash,&__reliterator);");
654                 cr.outputline("while (hasNext(&__reliterator))");
655                 cr.startblock();
656                 cr.addDeclaration("int","__relval");
657                 cr.addDeclaration("int","__relval2");
658                 cr.outputline("__relval2 = (int) key(&__reliterator);");
659                 cr.outputline("__relval = (int) next(&__reliterator);");
660
661                 cr.outputline("printf(\"<%ld,%ld> \", __relval2,__relval);");
662
663                 cr.endblock();
664                 cr.endblock();
665             } else if (rd.testUsage(RelationDescriptor.INVIMAGE)) {
666                 cr.startblock();
667                 cr.outputline("/* printing inv relation " + relname+"*/");
668                 cr.outputline("printf(\"\\nPrinting relation using inv" + rd.getSymbol() + " - %d elements \\n\", SimpleHashcountset("+relname+"_hash));");
669                 cr.addDeclaration("struct SimpleIterator","__reliterator");
670                 cr.outputline("SimpleHashiterator("+relname+"_hashinv,&__reliterator);");
671                 cr.outputline("while (hasNext(&__reliterator))");
672                 cr.startblock();
673                 cr.addDeclaration("int","__relval");
674                 cr.addDeclaration("int","__relval2");
675                 cr.outputline("__relval2 = (int) key(&__reliterator);");
676                 cr.outputline("__relval = (int) next(&__reliterator);");
677
678
679                 cr.outputline("printf(\"<%ld,%ld> \", __relval,__relval2);");
680
681                 cr.endblock();
682                 cr.endblock();
683             }
684
685         }
686
687         cr.outputline("printf(\"\\n\\n------------------- END PRINTING\\n\");");
688     }
689
690     Set ruleset=null;
691     private void generate_rules() {
692         /* first we must sort the rules */
693         RelationDescriptor.prefix = newmodel.getSafeSymbol()+"->";
694         SetDescriptor.prefix = newmodel.getSafeSymbol()+"->";
695         System.out.println("SCC="+(mrd.numSCC()-1));
696         for(int sccindex=0;sccindex<mrd.numSCC();sccindex++) {
697             ruleset=mrd.getSCC(sccindex);
698             boolean needworklist=mrd.hasCycle(sccindex);
699
700             if (!needworklist) {
701                 Iterator iterator_rs = ruleset.iterator();
702                 while (iterator_rs.hasNext()) {
703                     Rule rule = (Rule) iterator_rs.next();
704                     if (rule.getnogenerate())
705                         continue;
706                     {
707                         final SymbolTable st = rule.getSymbolTable();
708                         CodeWriter cr = new StandardCodeWriter(outputaux) {
709                                 public SymbolTable getSymbolTable() { return st; }
710                             };
711                         InvariantValue ivalue=new InvariantValue();
712                         cr.setInvariantValue(ivalue);
713
714                         cr.outputline("/* build " +escape(rule.toString())+"*/");
715                         cr.startblock();
716                         cr.addDeclaration("int","maybe");
717                         cr.outputline("maybe=0;");
718
719                         Expr ruleexpr=rule.getGuardExpr();
720                         HashSet invariantvars=new HashSet();
721                         Set invariants=ruleexpr.findInvariants(invariantvars);
722
723                         if ((ruleexpr instanceof BooleanLiteralExpr)&&
724                             ((BooleanLiteralExpr)ruleexpr).getValue()) {
725                             if (rule.getInclusion() instanceof SetInclusion) {
726                                 invariants.addAll(((SetInclusion)rule.getInclusion()).getExpr().findInvariants(invariantvars));
727                             } else if (rule.getInclusion() instanceof RelationInclusion) {
728                                 invariants.addAll(((RelationInclusion)rule.getInclusion()).getLeftExpr().findInvariants(invariantvars));
729                                 invariants.addAll(((RelationInclusion)rule.getInclusion()).getRightExpr().findInvariants(invariantvars));
730                             }
731                         }
732                         ListIterator quantifiers = rule.quantifiers();
733                         while (quantifiers.hasNext()) {
734                             Quantifier quantifier = (Quantifier) quantifiers.next();
735                             if (quantifier instanceof ForQuantifier) {
736                                 ForQuantifier fq=(ForQuantifier)quantifier;
737                                 invariants.addAll(fq.lower.findInvariants(invariantvars));
738                                 invariants.addAll(fq.upper.findInvariants(invariantvars));
739                             }
740                         }
741
742                         int openparencount=0;
743                         for(Iterator invit=invariants.iterator();invit.hasNext();) {
744                             Expr invexpr=(Expr)invit.next();
745                             VarDescriptor tmpvd=VarDescriptor.makeNew("tmpvar");
746                             VarDescriptor maybevd=VarDescriptor.makeNew("maybevar");
747                             invexpr.generate(cr,tmpvd);
748                             cr.addDeclaration("int ",maybevd.getSafeSymbol());
749                             cr.outputline(maybevd.getSafeSymbol()+"=maybe;");
750                             cr.outputline("maybe=0;");
751                             ivalue.assignPair(invexpr,tmpvd,maybevd);
752                             openparencount++;
753                             cr.startblock();
754                         }
755                         quantifiers = rule.quantifiers();
756                         while (quantifiers.hasNext()) {
757                             Quantifier quantifier = (Quantifier) quantifiers.next();
758                             quantifier.generate_open(cr);
759                         }
760
761                         /* pretty print! */
762                         cr.output("/*");
763                         rule.getGuardExpr().prettyPrint(cr);
764                         cr.outputline("*/");
765
766                         /* now we have to generate the guard test */
767                         VarDescriptor guardval = VarDescriptor.makeNew();
768                         rule.getGuardExpr().generate(cr, guardval);
769                         cr.outputline("if (" + guardval.getSafeSymbol() + ")");
770                         cr.startblock();
771
772                         /* now we have to generate the inclusion code */
773                         currentrule=rule;
774                         rule.getInclusion().generate(cr);
775                         cr.endblock();
776                         while (quantifiers.hasPrevious()) {
777                             Quantifier quantifier = (Quantifier) quantifiers.previous();
778                             cr.endblock();
779                         }
780                         cr.endblock();
781                         while((openparencount--)>0)
782                             cr.endblock();
783                         cr.outputline("");
784                         cr.outputline("");
785                     }
786                 }
787             } else {
788                 CodeWriter cr2 = new StandardCodeWriter(outputaux);
789
790                 for(Iterator initialworklist=ruleset.iterator();initialworklist.hasNext();) {
791                     /** Construct initial worklist set */
792                     Rule rule=(Rule)initialworklist.next();
793                     cr2.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+",-1,0,0);");
794                 }
795
796                 cr2.outputline("while (WorkListhasMoreElements("+worklist.getSafeSymbol()+"))");
797                 cr2.startblock();
798                 VarDescriptor idvar=VarDescriptor.makeNew("id");
799                 cr2.addDeclaration("int ",idvar.getSafeSymbol());
800                 cr2.outputline(idvar.getSafeSymbol()+"=WorkListgetid("+worklist.getSafeSymbol()+");");
801
802                 String elseladder = "if";
803
804                 Iterator iterator_rules = ruleset.iterator();
805                 while (iterator_rules.hasNext()) {
806
807                     Rule rule = (Rule) iterator_rules.next();
808                     int dispatchid = rule.getNum();
809
810                     {
811                         final SymbolTable st = rule.getSymbolTable();
812                         CodeWriter cr = new StandardCodeWriter(outputaux) {
813                                 public SymbolTable getSymbolTable() { return st; }
814                             };
815
816                         cr.indent();
817                         cr.outputline(elseladder + " ("+idvar.getSafeSymbol()+" == " + dispatchid + ")");
818                         cr.startblock();
819                         cr.addDeclaration("int","maybe");
820                         cr.outputline("maybe=0;");
821                         VarDescriptor typevar=VarDescriptor.makeNew("type");
822                         VarDescriptor leftvar=VarDescriptor.makeNew("left");
823                         VarDescriptor rightvar=VarDescriptor.makeNew("right");
824                         cr.addDeclaration("int",typevar.getSafeSymbol());
825                         cr.outputline(typevar.getSafeSymbol()+"= WorkListgettype("+worklist.getSafeSymbol()+");");
826                         cr.addDeclaration("int",leftvar.getSafeSymbol());
827                         cr.outputline(leftvar.getSafeSymbol()+"= WorkListgetlvalue("+worklist.getSafeSymbol()+");");
828                         cr.addDeclaration("int",rightvar.getSafeSymbol());
829                         cr.outputline(rightvar.getSafeSymbol()+"= WorkListgetrvalue("+worklist.getSafeSymbol()+");");
830                         cr.outputline("/* build " +escape(rule.toString())+"*/");
831
832
833                         for (int j=0;j<rule.numQuantifiers();j++) {
834                             Quantifier quantifier = rule.getQuantifier(j);
835                             quantifier.generate_open(cr, typevar.getSafeSymbol(),j,leftvar.getSafeSymbol(),rightvar.getSafeSymbol());
836                         }
837
838                         /* pretty print! */
839                         cr.output("/*");
840
841                         rule.getGuardExpr().prettyPrint(cr);
842                         cr.outputline("*/");
843
844                         /* now we have to generate the guard test */
845
846                         VarDescriptor guardval = VarDescriptor.makeNew();
847                         rule.getGuardExpr().generate(cr, guardval);
848
849                         cr.outputline("if (" + guardval.getSafeSymbol() + ")");
850                         cr.startblock();
851
852                         /* now we have to generate the inclusion code */
853                         currentrule=rule;
854                         rule.getInclusion().generate(cr);
855                         cr.endblock();
856
857                         for (int j=0;j<rule.numQuantifiers();j++) {
858                             cr.endblock();
859                         }
860
861                         // close startblocks generated by DotExpr memory checks
862                         //DotExpr.generate_memory_endblocks(cr);
863
864                         cr.endblock(); // end else-if WORKLIST ladder
865
866                         elseladder = "else if";
867                     }
868                 }
869                 cr2.outputline("else");
870                 cr2.startblock();
871                 cr2.outputline("printf(\"VERY BAD !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\\n\\n\");");
872                 cr2.outputline("exit(1);");
873                 cr2.endblock();
874                 // end block created for worklist
875                 cr2.outputline("WorkListpop("+worklist.getSafeSymbol()+");");
876                 cr2.endblock();
877             }
878         }
879     }
880
881     public static String escape(String s) {
882         String newstring="";
883         for(int i=0;i<s.length();i++) {
884             char c=s.charAt(i);
885             if (c=='"')
886                 newstring+="\"";
887             else
888                 newstring+=c;
889         }
890         return newstring;
891     }
892
893     private void generate_checks() {
894         /* do constraint checks */
895         Iterator i;
896         if (Compiler.REPAIR)
897             i=termination.constraintdependence.computeOrdering().iterator();
898         else
899             i=state.vConstraints.iterator();
900         for (; i.hasNext();) {
901             Constraint constraint;
902             if (Compiler.REPAIR)
903                 constraint= (Constraint) ((GraphNode)i.next()).getOwner();
904             else
905                 constraint=(Constraint)i.next();
906
907             {
908                 final SymbolTable st = constraint.getSymbolTable();
909                 CodeWriter cr = new StandardCodeWriter(outputaux);
910                 cr.pushSymbolTable(constraint.getSymbolTable());
911
912                 cr.outputline("/* checking " + escape(constraint.toString())+"*/");
913                 cr.startblock();
914
915                 ListIterator quantifiers = constraint.quantifiers();
916
917                 while (quantifiers.hasNext()) {
918                     Quantifier quantifier = (Quantifier) quantifiers.next();
919                     quantifier.generate_open(cr);
920                 }
921
922                 cr.addDeclaration("int","maybe");
923                 cr.outputline("maybe = 0;");
924
925                 /* now we have to generate the guard test */
926
927                 VarDescriptor constraintboolean = VarDescriptor.makeNew("constraintboolean");
928                 constraint.getLogicStatement().generate(cr, constraintboolean);
929
930                 cr.outputline("if (maybe)");
931                 cr.startblock();
932                 cr.outputline("printf(\"maybe fail " +  escape(constraint.toString()) + ". \\n\");");
933                 //cr.outputline("exit(1);");
934                 cr.endblock();
935
936                 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
937                 cr.startblock();
938                 if (!Compiler.REPAIR||Compiler.GENERATEDEBUGHOOKS)
939                     cr.outputline("printf(\"fail " + escape(constraint.toString()) + ". \\n\");");
940
941                 if (Compiler.REPAIR) {
942                 /* Do repairs */
943                 /* Build new repair table */
944
945                 cr.outputline("if ("+repairtable.getSafeSymbol()+")");
946                 cr.outputline("freeRepairHash("+repairtable.getSafeSymbol()+");");
947                 cr.outputline(repairtable.getSafeSymbol()+"=noargallocateRepairHash();");
948
949                 if (Compiler.GENERATEDEBUGHOOKS)
950                     cr.outputline("debughook();");
951                 /* Compute cost of each repair */
952                 VarDescriptor mincost=VarDescriptor.makeNew("mincost");
953                 VarDescriptor mincostindex=VarDescriptor.makeNew("mincostindex");
954                 Vector dnfconst=new Vector();
955                 dnfconst.addAll((Set)termination.conjunctionmap.get(constraint));
956
957                 if (dnfconst.size()<=1) {
958                     cr.addDeclaration("int",mincostindex.getSafeSymbol());
959                     cr.outputline(mincostindex.getSafeSymbol()+"=0;");
960                 }
961                 if (dnfconst.size()>1) {
962                     cr.addDeclaration("int",mincostindex.getSafeSymbol());
963                     boolean first=true;
964                     for(int j=0;j<dnfconst.size();j++) {
965                         GraphNode gn=(GraphNode)dnfconst.get(j);
966                         Conjunction conj=((TermNode)gn.getOwner()).getConjunction();
967                         if (removed.contains(gn))
968                             continue;
969
970                         VarDescriptor costvar;
971                         if (first) {
972                             costvar=mincost;
973                         } else
974                             costvar=VarDescriptor.makeNew("cost");
975                         for(int k=0;k<conj.size();k++) {
976                             DNFPredicate dpred=conj.get(k);
977                             Predicate p=dpred.getPredicate();
978                             boolean negate=dpred.isNegated();
979                             VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
980                             p.generate(cr,predvalue);
981                             if (k==0) {
982                                 cr.addDeclaration("int",costvar.getSafeSymbol());
983                                 cr.outputline(costvar.getSafeSymbol()+"=0;");
984                             }
985                             if (negate)
986                                 cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
987                             else
988                                 cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
989                             cr.outputline(costvar.getSafeSymbol()+"+="+cost.getCost(dpred)+";");
990                         }
991
992                         if(!first) {
993                             cr.outputline("if ("+costvar.getSafeSymbol()+"<"+mincost.getSafeSymbol()+")");
994                             cr.startblock();
995                             cr.outputline(mincost.getSafeSymbol()+"="+costvar.getSafeSymbol()+";");
996                             cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
997                             cr.endblock();
998                         } else
999                             cr.outputline(mincostindex.getSafeSymbol()+"="+j+";");
1000                         first=false;
1001                     }
1002                 }
1003                 cr.outputline("switch("+mincostindex.getSafeSymbol()+")");
1004                 cr.startblock();
1005                 for(int j=0;j<dnfconst.size();j++) {
1006                     GraphNode gn=(GraphNode)dnfconst.get(j);
1007                     Conjunction conj=((TermNode)gn.getOwner()).getConjunction();
1008                     if (removed.contains(gn))
1009                         continue;
1010                     cr.outputline("case "+j+":");
1011                     cr.startblock();
1012                     for(int k=0;k<conj.size();k++) {
1013                         DNFPredicate dpred=conj.get(k);
1014                         Predicate p=dpred.getPredicate();
1015                         boolean negate=dpred.isNegated();
1016                         VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
1017                         p.generate(cr,predvalue);
1018                         if (negate)
1019                             cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
1020                         else
1021                             cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
1022                         cr.startblock();
1023                         if (Compiler.GENERATEINSTRUMENT)
1024                             cr.outputline("abstractcount++;");
1025                         if (p instanceof InclusionPredicate)
1026                             generateinclusionrepair(conj,dpred, cr);
1027                         else if (p instanceof ExprPredicate) {
1028                             ExprPredicate ep=(ExprPredicate)p;
1029                             if (ep.getType()==ExprPredicate.SIZE)
1030                                 generatesizerepair(conj,dpred,cr);
1031                             else if (ep.getType()==ExprPredicate.COMPARISON)
1032                                 generatecomparisonrepair(conj,dpred,cr);
1033                         } else throw new Error("Unrecognized Predicate");
1034                         cr.endblock();
1035                     }
1036                     /* Update model */
1037                     cr.endblock();
1038                     cr.outputline("break;");
1039                 }
1040                 cr.endblock();
1041
1042                 cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
1043                 cr.outputline("free"+name+"("+oldmodel.getSafeSymbol()+");");
1044                 cr.outputline(oldmodel.getSafeSymbol()+"="+newmodel.getSafeSymbol()+";");
1045                 cr.outputline("goto rebuild;");  /* Rebuild model and all */
1046                 }
1047                 cr.endblock();
1048
1049                 while (quantifiers.hasPrevious()) {
1050                     Quantifier quantifier = (Quantifier) quantifiers.previous();
1051                     cr.endblock();
1052                 }
1053                 cr.endblock();
1054                 cr.outputline("");
1055                 cr.outputline("");
1056             }
1057         }
1058         CodeWriter cr = new StandardCodeWriter(outputaux);
1059         cr.startblock();
1060         cr.outputline("if ("+repairtable.getSafeSymbol()+")");
1061         cr.outputline("freeRepairHash("+repairtable.getSafeSymbol()+");");
1062         cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
1063         cr.outputline("free"+name+"("+oldmodel.getSafeSymbol()+");");
1064         cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1065         cr.outputline("freeWorkList("+worklist.getSafeSymbol()+");");
1066         cr.outputline("resettypemap();");
1067         cr.outputline("break;");
1068         cr.endblock();
1069         cr.outputline("rebuild:");
1070         cr.outputline(";");
1071     }
1072
1073     private MultUpdateNode getmultupdatenode(Conjunction conj, DNFPredicate dpred, int repairtype) {
1074         Set nodes=getmultupdatenodeset(conj,dpred,repairtype);
1075         Iterator it=nodes.iterator();
1076         if (it.hasNext())
1077             return (MultUpdateNode)it.next();
1078         else
1079             return null;
1080     }
1081
1082     private Set getmultupdatenodeset(Conjunction conj, DNFPredicate dpred, int repairtype) {
1083         HashSet hs=new HashSet();
1084         GraphNode gn=(GraphNode) termination.conjtonodemap.get(conj);
1085         for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1086             GraphNode gn2=((GraphNode.Edge) edgeit.next()).getTarget();
1087             TermNode tn2=(TermNode)gn2.getOwner();
1088             if (tn2.getType()==TermNode.ABSTRACT) {
1089                 AbstractRepair ar=tn2.getAbstract();
1090                 if (((repairtype==-1)||(ar.getType()==repairtype))&&
1091                     ar.getPredicate()==dpred) {
1092                     for(Iterator edgeit2=gn2.edges();edgeit2.hasNext();) {
1093                         GraphNode gn3=((GraphNode.Edge) edgeit2.next()).getTarget();
1094                         if (!removed.contains(gn3)) {
1095                             TermNode tn3=(TermNode)gn3.getOwner();
1096                             if (tn3.getType()==TermNode.UPDATE) {
1097                                 hs.add(tn3.getUpdate());
1098                             }
1099                         }
1100                     }
1101                 }
1102             }
1103         }
1104         return hs;
1105     }
1106
1107     private AbstractRepair getabstractrepair(Conjunction conj, DNFPredicate dpred, int repairtype) {
1108         HashSet hs=new HashSet();
1109         MultUpdateNode mun=null;
1110         GraphNode gn=(GraphNode) termination.conjtonodemap.get(conj);
1111         for(Iterator edgeit=gn.edges();(mun==null)&&edgeit.hasNext();) {
1112             GraphNode gn2=((GraphNode.Edge) edgeit.next()).getTarget();
1113             TermNode tn2=(TermNode)gn2.getOwner();
1114             if (tn2.getType()==TermNode.ABSTRACT) {
1115                 AbstractRepair ar=tn2.getAbstract();
1116                 if (((repairtype==-1)||(ar.getType()==repairtype))&&
1117                     ar.getPredicate()==dpred) {
1118                     return ar;
1119                 }
1120             }
1121         }
1122         return null;
1123     }
1124
1125
1126     /** Generates abstract (and concrete) repair for a comparison */
1127
1128     private void generatecomparisonrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
1129         Set updates=getmultupdatenodeset(conj,dpred,AbstractRepair.MODIFYRELATION);
1130         AbstractRepair ar=getabstractrepair(conj,dpred,AbstractRepair.MODIFYRELATION);
1131         MultUpdateNode munmodify=null;
1132         MultUpdateNode munadd=null;
1133         MultUpdateNode munremove=null;
1134         for(Iterator it=updates.iterator();it.hasNext();) {
1135             MultUpdateNode mun=(MultUpdateNode)it.next();
1136             if (mun.getType()==MultUpdateNode.ADD) {
1137                 munadd=mun;
1138             } else if (mun.getType()==MultUpdateNode.REMOVE) {
1139                 munremove=mun;
1140             } else if (mun.getType()==MultUpdateNode.MODIFY) {
1141                 munmodify=mun;
1142             }
1143         }
1144
1145         ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
1146         RelationDescriptor rd=(RelationDescriptor)ep.getDescriptor();
1147         boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1148         boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1149         boolean inverted=ep.inverted();
1150         boolean negated=dpred.isNegated();
1151         OpExpr expr=(OpExpr)ep.expr;
1152         Opcode opcode=expr.getOpcode();
1153         VarDescriptor leftside=VarDescriptor.makeNew("leftside");
1154         VarDescriptor rightside=VarDescriptor.makeNew("rightside");
1155         VarDescriptor newvalue=VarDescriptor.makeNew("newvalue");
1156         boolean needremoveloop=ar.mayNeedFunctionEnforcement(state)&&ar.needsRemoves(state);
1157
1158         if (needremoveloop&&((munadd==null)||(munremove==null))) {
1159             System.out.println("Warning:  need to have individual remove operations for"+dpred.name());
1160             needremoveloop=false;
1161         }
1162         if (needremoveloop) {
1163             cr.outputline("while (1)");
1164             cr.startblock();
1165         }
1166         if (!inverted) {
1167             ((RelationExpr)expr.getLeftExpr()).getExpr().generate(cr,leftside);
1168             expr.getRightExpr().generate(cr,newvalue);
1169             cr.addDeclaration(rd.getRange().getType().getGenerateType().getSafeSymbol(),rightside.getSafeSymbol());
1170             cr.outputline("SimpleHashget("+rd.getSafeSymbol()+"_hash,"+leftside.getSafeSymbol()+", &"+rightside.getSafeSymbol()+");");
1171         } else {
1172             ((RelationExpr)expr.getLeftExpr()).getExpr().generate(cr,rightside);
1173             expr.getRightExpr().generate(cr,newvalue);
1174             cr.outputline(rd.getDomain().getType().getGenerateType().getSafeSymbol()+" "+leftside.getSafeSymbol()+";");
1175             cr.outputline("SimpleHashget("+rd.getSafeSymbol()+"_hashinv,"+rightside.getSafeSymbol()+", &"+leftside.getSafeSymbol()+");");
1176         }
1177
1178         opcode=Opcode.translateOpcode(negated,opcode);
1179
1180         if (opcode==Opcode.GT) {
1181             cr.outputline(newvalue.getSafeSymbol()+"++;");
1182         } else if (opcode==Opcode.GE) {
1183             /* Equal */
1184         } else if (opcode==Opcode.LT) {
1185             cr.outputline(newvalue.getSafeSymbol()+"--;");
1186         } else if (opcode==Opcode.LE) {
1187             /* Equal */
1188         } else if (opcode==Opcode.EQ) {
1189             /* Equal */
1190         } else if (opcode==Opcode.NE) { /* search for FLAGNE if this is changed*/
1191             cr.outputline(newvalue.getSafeSymbol()+"++;");
1192         } else {
1193             throw new Error("Unrecognized Opcode");
1194         }
1195         /* Do abstract repairs */
1196         if (usageimage) {
1197             cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash, "+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1198         }
1199         if (usageinvimage) {
1200             cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv, "+rightside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
1201         }
1202
1203         if (needremoveloop) {
1204             if (!inverted) {
1205                 cr.outputline("if (SimpleHashcontainskey("+rd.getSafeSymbol()+"_hash, "+leftside.getSafeSymbol()+"))");
1206                 cr.startblock();
1207             } else {
1208                 cr.outputline("if (SimpleHashcontainskey("+rd.getSafeSymbol()+"_hashinv, "+rightside.getSafeSymbol()+"))");
1209                 cr.startblock();
1210             }
1211             for(int i=0;i<state.vRules.size();i++) {
1212                 Rule r=(Rule)state.vRules.get(i);
1213                 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1214                     for(int j=0;j<munremove.numUpdates();j++) {
1215                         UpdateNode un=munremove.getUpdate(i);
1216                         if (un.getRule()==r) {
1217                                 /* Update for rule r */
1218                             String name=(String)updatenames.get(un);
1219                             cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+");");
1220                         }
1221                     }
1222                 }
1223             }
1224             cr.outputline("continue;");
1225             cr.endblock();
1226         }
1227
1228         if (usageimage) {
1229             if (!inverted) {
1230                 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash,"+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1231             } else {
1232                 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, "+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1233             }
1234         }
1235         if (usageinvimage) {
1236             if (!inverted) {
1237                 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, "+newvalue.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
1238             } else {
1239                 cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv,"+rightside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1240             }
1241         }
1242         /* Do concrete repairs */
1243         if (munmodify!=null&&(!ar.mayNeedFunctionEnforcement(state))||(munadd==null)||(ar.needsRemoves(state)&&(munremove==null))) {
1244             for(int i=0;i<state.vRules.size();i++) {
1245                 Rule r=(Rule)state.vRules.get(i);
1246                 if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1247                     for(int j=0;j<munmodify.numUpdates();j++) {
1248                         UpdateNode un=munmodify.getUpdate(j);
1249                         if (un.getRule()==r) {
1250                             /* Update for rule r */
1251                             String name=(String)updatenames.get(un);
1252                             cr.outputline("RepairHashaddrelation2("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+","+newvalue.getSafeSymbol()+");");
1253                         }
1254                     }
1255                 }
1256             }
1257         } else {
1258             /* Start with scheduling removal */
1259             if (ar.needsRemoves(state))
1260                 for(int i=0;i<state.vRules.size();i++) {
1261                     Rule r=(Rule)state.vRules.get(i);
1262                     if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1263                         for(int j=0;j<munremove.numUpdates();j++) {
1264                             UpdateNode un=munremove.getUpdate(i);
1265                             if (un.getRule()==r) {
1266                                 /* Update for rule r */
1267                                 String name=(String)updatenames.get(un);
1268                                 cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+");");
1269                             }
1270                         }
1271                     }
1272                 }
1273             /* Now do addition */
1274             UpdateNode un=munadd.getUpdate(0);
1275             String name=(String)updatenames.get(un);
1276             if (!inverted) {
1277                 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
1278             } else {
1279                 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
1280             }
1281         }
1282         if (needremoveloop) {
1283             cr.outputline("break;");
1284             cr.endblock();
1285         }
1286     }
1287
1288     public void generatesizerepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr) {
1289         ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
1290         OpExpr expr=(OpExpr)ep.expr;
1291         Opcode opcode=expr.getOpcode();
1292         opcode=Opcode.translateOpcode(dpred.isNegated(),opcode);
1293
1294         MultUpdateNode munremove;
1295
1296         MultUpdateNode munadd;
1297         if (ep.getDescriptor() instanceof RelationDescriptor) {
1298             munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMRELATION);
1299             munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTORELATION);
1300         } else {
1301             munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMSET);
1302             munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTOSET);
1303         }
1304         int size=ep.rightSize();
1305         VarDescriptor sizevar=VarDescriptor.makeNew("size");
1306         ((OpExpr)expr).left.generate(cr, sizevar);
1307         VarDescriptor change=VarDescriptor.makeNew("change");
1308         cr.addDeclaration("int",change.getSafeSymbol());
1309         boolean generateadd=false;
1310         boolean generateremove=false;
1311         if (opcode==Opcode.GT) {
1312             cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
1313             generateadd=true;
1314             generateremove=false;
1315         } else if (opcode==Opcode.GE) {
1316             cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1317             generateadd=true;
1318             generateremove=false;
1319         } else if (opcode==Opcode.LT) {
1320             cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size-1)+"-"+sizevar.getSafeSymbol()+";");
1321             generateadd=false;
1322             generateremove=true;
1323         } else if (opcode==Opcode.LE) {
1324             cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1325             generateadd=false;
1326             generateremove=true;
1327         } else if (opcode==Opcode.EQ) {
1328             cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size)+"-"+sizevar.getSafeSymbol()+";");
1329             if (size==0)
1330                 generateadd=false;
1331             else
1332                 generateadd=true;
1333             generateremove=true;
1334         } else if (opcode==Opcode.NE) {
1335             cr.outputline(change.getSafeSymbol()+"="+String.valueOf(size+1)+"-"+sizevar.getSafeSymbol()+";");
1336             generateadd=true;
1337             generateremove=false;
1338         } else {
1339             throw new Error("Unrecognized Opcode");
1340         }
1341
1342 // In some cases the analysis has determined that generating removes
1343 // is unnecessary
1344         if (generateremove&&munremove==null)
1345             generateremove=false;
1346
1347         Descriptor d=ep.getDescriptor();
1348         if (generateremove) {
1349             cr.outputline("for(;"+change.getSafeSymbol()+"<0;"+change.getSafeSymbol()+"++)");
1350             cr.startblock();
1351             /* Find element to remove */
1352             VarDescriptor leftvar=VarDescriptor.makeNew("leftvar");
1353             VarDescriptor rightvar=VarDescriptor.makeNew("rightvar");
1354             if (d instanceof RelationDescriptor) {
1355                 if (ep.inverted()) {
1356                     ((ImageSetExpr)((SizeofExpr)expr.left).setexpr).generate_leftside(cr,rightvar);
1357                     cr.addDeclaration("int",leftvar.getSafeSymbol());
1358                     cr.outputline("SimpleHashget("+d.getSafeSymbol()+"_hashinv,(int)"+rightvar.getSafeSymbol()+", &"+leftvar.getSafeSymbol()+");");
1359                 } else {
1360                     ((ImageSetExpr)((SizeofExpr)expr.left).setexpr).generate_leftside(cr,leftvar);
1361                     cr.addDeclaration("int",rightvar.getSafeSymbol());
1362                     cr.outputline("SimpleHashget("+d.getSafeSymbol()+"_hash ,(int)"+leftvar.getSafeSymbol()+", &"+rightvar.getSafeSymbol()+");");
1363                 }
1364             } else {
1365                 cr.addDeclaration("int",leftvar.getSafeSymbol());
1366                 cr.outputline(leftvar.getSafeSymbol()+"= SimpleHashfirstkey("+d.getSafeSymbol()+"_hash);");
1367             }
1368             /* Generate abstract remove instruction */
1369             if (d instanceof RelationDescriptor) {
1370                 RelationDescriptor rd=(RelationDescriptor) d;
1371                 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1372                 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1373                 if (usageimage)
1374                     cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1375                 if (usageinvimage)
1376                     cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv ,(int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1377             } else {
1378                 cr.outputline("SimpleHashremove("+d.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1379             }
1380             /* Generate concrete remove instruction */
1381             for(int i=0;i<state.vRules.size();i++) {
1382                 Rule r=(Rule)state.vRules.get(i);
1383                 if (r.getInclusion().getTargetDescriptors().contains(d)) {
1384                     for(int j=0;j<munremove.numUpdates();j++) {
1385                         UpdateNode un=munremove.getUpdate(j);
1386                         if (un.getRule()==r) {
1387                                 /* Update for rule rule r */
1388                             String name=(String)updatenames.get(un);
1389                             if (d instanceof RelationDescriptor) {
1390                                 cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
1391                             } else {
1392                                 cr.outputline("RepairHashaddset("+repairtable.getSafeSymbol()+","+d.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1393                             }
1394                         }
1395                     }
1396                 }
1397             }
1398             cr.endblock();
1399         }
1400
1401 // In some cases the analysis has determined that generating removes
1402 // is unnecessary
1403         if (generateadd&&munadd==null)
1404             generateadd=false;
1405
1406         if (generateadd) {
1407
1408             cr.outputline("for(;"+change.getSafeSymbol()+">0;"+change.getSafeSymbol()+"--)");
1409             cr.startblock();
1410             VarDescriptor newobject=VarDescriptor.makeNew("newobject");
1411             if (d instanceof RelationDescriptor) {
1412                 VarDescriptor otherside=VarDescriptor.makeNew("otherside");
1413                 ((ImageSetExpr)((SizeofExpr)expr.left).setexpr).generate_leftside(cr,otherside);
1414
1415                 RelationDescriptor rd=(RelationDescriptor)d;
1416                 if (termination.sources.relsetSource(rd,!ep.inverted())) {
1417                     /* Set Source */
1418                     SetDescriptor sd=termination.sources.relgetSourceSet(rd,!ep.inverted());
1419                     VarDescriptor iterator=VarDescriptor.makeNew("iterator");
1420                     cr.addDeclaration(sd.getType().getGenerateType().getSafeSymbol(),newobject.getSafeSymbol());
1421                     cr.addDeclaration("struct SimpleIterator",iterator.getSafeSymbol());
1422                     cr.outputline("for(SimpleHashiterator("+sd.getSafeSymbol()+"_hash , &"+ iterator.getSafeSymbol() +"); hasNext(&"+iterator.getSafeSymbol()+");)");
1423                     cr.startblock();
1424                     if (ep.inverted()) {
1425                         cr.outputline("if (!SimpleHashcontainskeydata("+rd.getSafeSymbol()+"_hashinv,"+iterator.getSafeSymbol()+".key(),"+otherside.getSafeSymbol()+"))");
1426                     } else {
1427                         cr.outputline("if (!SimpleHashcontainskeydata("+rd.getSafeSymbol()+"_hash, "+otherside.getSafeSymbol()+","+iterator.getSafeSymbol()+".key()))");
1428                     }
1429                     cr.outputline(newobject.getSafeSymbol()+"=key(&"+iterator.getSafeSymbol()+");");
1430                     cr.outputline("next(&"+iterator.getSafeSymbol()+");");
1431                     cr.endblock();
1432                 } else if (termination.sources.relallocSource(rd,!ep.inverted())) {
1433                     /* Allocation Source*/
1434                     termination.sources.relgenerateSourceAlloc(cr,newobject,rd,!ep.inverted());
1435                 } else throw new Error("No source for adding to Relation");
1436                 if (ep.inverted()) {
1437                     boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1438                     boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1439                     if (usageimage)
1440                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, "+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1441                     if (usageinvimage)
1442                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, "+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1443
1444                     UpdateNode un=munadd.getUpdate(0);
1445                     String name=(String)updatenames.get(un);
1446                     cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1447                 } else {
1448                     boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1449                     boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1450                     if (usageimage)
1451                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, "+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1452                     if (usageinvimage)
1453                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, "+newobject.getSafeSymbol()+","+otherside.getSafeSymbol()+");");
1454                     UpdateNode un=munadd.getUpdate(0);
1455                     String name=(String)updatenames.get(un);
1456                     cr.outputline(name+"(thisvar, "+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+otherside.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1457                 }
1458             } else {
1459                 SetDescriptor sd=(SetDescriptor)d;
1460                 if (termination.sources.setSource(sd)) {
1461                     /* Set Source */
1462                     /* Set Source */
1463                     SetDescriptor sourcesd=termination.sources.getSourceSet(sd);
1464                     VarDescriptor iterator=VarDescriptor.makeNew("iterator");
1465                     cr.addDeclaration(sourcesd.getType().getGenerateType().getSafeSymbol(), newobject.getSafeSymbol());
1466                     cr.addDeclaration("struct SimpleIterator",iterator.getSafeSymbol());
1467                     cr.outputline("for(SimpleHashiterator("+sourcesd.getSafeSymbol()+"_hash, &"+iterator.getSafeSymbol()+"); hasNext(&"+iterator.getSafeSymbol()+");)");
1468                     cr.startblock();
1469                     cr.outputline("if (!SimpleHashcontainskey("+sd.getSafeSymbol()+"_hash, key(&"+iterator.getSafeSymbol()+")))");
1470                     cr.outputline(newobject.getSafeSymbol()+"=key(&"+iterator.getSafeSymbol()+");");
1471                     cr.outputline("next(&"+iterator.getSafeSymbol()+");");
1472                     cr.endblock();
1473                 } else if (termination.sources.allocSource(sd)) {
1474                     /* Allocation Source*/
1475                     termination.sources.generateSourceAlloc(cr,newobject,sd);
1476                 } else throw new Error("No source for adding to Set");
1477                 cr.outputline("SimpleHashadd("+sd.getSafeSymbol()+"_hash, "+newobject.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1478                 UpdateNode un=munadd.getUpdate(0);
1479                 String name=(String)updatenames.get(un);
1480                 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
1481             }
1482             cr.endblock();
1483         }
1484     }
1485
1486     public void generateinclusionrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
1487         InclusionPredicate ip=(InclusionPredicate) dpred.getPredicate();
1488         boolean negated=dpred.isNegated();
1489         MultUpdateNode mun=getmultupdatenode(conj,dpred,-1);
1490         VarDescriptor leftvar=VarDescriptor.makeNew("left");
1491         ip.expr.generate(cr, leftvar);
1492
1493         if (negated) {
1494             if (ip.setexpr instanceof ImageSetExpr) {
1495                 ImageSetExpr ise=(ImageSetExpr) ip.setexpr;
1496                 VarDescriptor rightvar=ise.getVar();
1497                 boolean inverse=ise.inverted();
1498                 RelationDescriptor rd=ise.getRelation();
1499                 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1500                 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1501                 if (inverse) {
1502                     if (usageimage)
1503                         cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1504                     if (usageinvimage)
1505                         cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1506                 } else {
1507                     if (usageimage)
1508                         cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hash ,(int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1509                     if (usageinvimage)
1510                         cr.outputline("SimpleHashremove("+rd.getSafeSymbol()+"_hashinv, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1511                 }
1512                 for(int i=0;i<state.vRules.size();i++) {
1513                     Rule r=(Rule)state.vRules.get(i);
1514                     if (r.getInclusion().getTargetDescriptors().contains(rd)) {
1515                         for(int j=0;j<mun.numUpdates();j++) {
1516                             UpdateNode un=mun.getUpdate(j);
1517                             if (un.getRule()==r) {
1518                                 /* Update for rule rule r */
1519                                 String name=(String)updatenames.get(un);
1520                                 if (inverse) {
1521                                     cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1522                                 } else {
1523                                     cr.outputline("RepairHashaddrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+",(int) &"+name+");");
1524                                 }
1525                             }
1526                         }
1527                     }
1528                 }
1529             } else {
1530                 SetDescriptor sd=ip.setexpr.sd;
1531                 cr.outputline("SimpleHashremove("+sd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1532
1533                 for(int i=0;i<state.vRules.size();i++) {
1534                     Rule r=(Rule)state.vRules.get(i);
1535                     if (r.getInclusion().getTargetDescriptors().contains(sd)) {
1536                         for(int j=0;j<mun.numUpdates();j++) {
1537                             UpdateNode un=mun.getUpdate(j);
1538                             if (un.getRule()==r) {
1539                                 /* Update for rule rule r */
1540                                 String name=(String)updatenames.get(un);
1541                                 cr.outputline("RepairHashaddset("+repairtable.getSafeSymbol()+","+sd.getNum()+","+r.getNum()+","+leftvar.getSafeSymbol()+",(int) &"+name+");");
1542                             }
1543                         }
1544                     }
1545                 }
1546             }
1547         } else {
1548             /* Generate update */
1549             if (ip.setexpr instanceof ImageSetExpr) {
1550                 ImageSetExpr ise=(ImageSetExpr) ip.setexpr;
1551                 VarDescriptor rightvar=ise.getVar();
1552                 boolean inverse=ise.inverted();
1553                 RelationDescriptor rd=ise.getRelation();
1554                 boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1555                 boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1556                 if (inverse) {
1557                     if (usageimage)
1558                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1559                     if (usageinvimage)
1560                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1561                 } else {
1562                     if (usageimage)
1563                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + rightvar.getSafeSymbol() + ");");
1564                     if (usageinvimage)
1565                         cr.outputline("SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, (int)" + rightvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1566                 }
1567                 UpdateNode un=mun.getUpdate(0);
1568                 String name=(String)updatenames.get(un);
1569                 if (inverse) {
1570                     cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1571                 } else {
1572                     cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+");");
1573                 }
1574             } else {
1575                 SetDescriptor sd=ip.setexpr.sd;
1576                 cr.outputline("SimpleHashadd("+sd.getSafeSymbol()+"_hash, (int)" + leftvar.getSafeSymbol() + ", (int)" + leftvar.getSafeSymbol() + ");");
1577
1578                 UpdateNode un=mun.getUpdate(0);
1579                 /* Update for rule rule r */
1580                 String name=(String)updatenames.get(un);
1581                 cr.outputline(name+"(thisvar,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
1582             }
1583         }
1584     }
1585
1586     public static Vector getrulelist(Descriptor d) {
1587         Vector dispatchrules = new Vector();
1588         Vector rules = State.currentState.vRules;
1589
1590         for (int i = 0; i < rules.size(); i++) {
1591             Rule rule = (Rule) rules.elementAt(i);
1592             Set requiredsymbols = rule.getRequiredDescriptors();
1593
1594             // #TBD#: in general this is wrong because these descriptors may contain descriptors
1595             // bound in "in?" expressions which need to be dealt with in a topologically sorted
1596             // fashion...
1597
1598             if (rule.getRequiredDescriptors().contains(d)) {
1599                 dispatchrules.addElement(rule);
1600             }
1601         }
1602         return dispatchrules;
1603     }
1604
1605     private boolean need_compensation(Rule r) {
1606         if (!Compiler.REPAIR)
1607             return false;
1608         GraphNode gn=(GraphNode)termination.scopefalsify.get(r);
1609         for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1610             GraphNode.Edge edge=(GraphNode.Edge)edgeit.next();
1611             GraphNode gn2=edge.getTarget();
1612             if (!removed.contains(gn2)) {
1613                 TermNode tn2=(TermNode)gn2.getOwner();
1614                 if (tn2.getType()==TermNode.CONSEQUENCE)
1615                     return false;
1616             }
1617         }
1618         return true;
1619     }
1620
1621     private UpdateNode find_compensation(Rule r) {
1622         GraphNode gn=(GraphNode)termination.scopefalsify.get(r);
1623         for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
1624             GraphNode.Edge edge=(GraphNode.Edge)edgeit.next();
1625             GraphNode gn2=edge.getTarget();
1626             if (!removed.contains(gn2)) {
1627                 TermNode tn2=(TermNode)gn2.getOwner();
1628                 if (tn2.getType()==TermNode.UPDATE) {
1629                     MultUpdateNode mun=tn2.getUpdate();
1630                     for(int i=0;i<mun.numUpdates();i++) {
1631                         UpdateNode un=mun.getUpdate(i);
1632                         if (un.getRule()==r)
1633                             return un;
1634                     }
1635                 }
1636             }
1637         }
1638         throw new Error("No Compensation Update could be found");
1639     }
1640
1641     public void generate_dispatch(CodeWriter cr, RelationDescriptor rd, String leftvar, String rightvar) {
1642         boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
1643         boolean usageinvimage=rd.testUsage(RelationDescriptor.INVIMAGE);
1644
1645         if (!(usageinvimage||usageimage)) /* not used at all*/
1646             return;
1647
1648         cr.outputline("/* RELATION DISPATCH */");
1649         if (Compiler.REPAIR) {
1650             cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1651             if (usageimage)
1652                 cr.outputline("!SimpleHashcontainskeydata("+oldmodel.getSafeSymbol()+"->"+rd.getJustSafeSymbol() + "_hash, "+leftvar+","+rightvar+"))");
1653             else
1654                 cr.outputline("!SimpleHashcontainskeydata("+oldmodel.getSafeSymbol() +"->"+rd.getJustSafeSymbol()+"_hashinv, "+rightvar+","+leftvar+"))");
1655
1656             cr.startblock(); {
1657                 /* Adding new item */
1658                 /* Perform safety checks */
1659                 cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
1660                 cr.outputline("RepairHashcontainsrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+"))");
1661                 cr.startblock(); {
1662                     /* Have update to call into */
1663                     VarDescriptor mdfyptr=VarDescriptor.makeNew("modifyptr");
1664                     VarDescriptor ismdfyptr=VarDescriptor.makeNew("ismodifyptr");
1665                     cr.addDeclaration("int ",ismdfyptr.getSafeSymbol());
1666                     cr.outputline(ismdfyptr.getSafeSymbol()+"=RepairHashismodify("+repairtable.getSafeSymbol()+","+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1667
1668
1669
1670
1671                     String parttype="";
1672                     for(int i=0;i<currentrule.numQuantifiers();i++) {
1673                         if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1674                             parttype=parttype+", int, int";
1675                         else
1676                             parttype=parttype+", int";
1677                     }
1678
1679                     VarDescriptor tmpptr=VarDescriptor.makeNew("tempupdateptr");
1680
1681                     String methodcall="(thisvar,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
1682                     for(int i=0;i<currentrule.numQuantifiers();i++) {
1683                         Quantifier q=currentrule.getQuantifier(i);
1684                         if (q instanceof SetQuantifier) {
1685                             SetQuantifier sq=(SetQuantifier) q;
1686                             methodcall+=","+sq.getVar().getSafeSymbol();
1687                         } else if (q instanceof RelationQuantifier) {
1688                             RelationQuantifier rq=(RelationQuantifier) q;
1689                             methodcall+=","+rq.x.getSafeSymbol();
1690                             methodcall+=","+rq.y.getSafeSymbol();
1691                         } else if (q instanceof ForQuantifier) {
1692                             ForQuantifier fq=(ForQuantifier) q;
1693                             methodcall+=","+fq.getVar().getSafeSymbol();
1694                         }
1695                     }
1696
1697
1698
1699                     cr.addDeclaration("void *",tmpptr.getSafeSymbol());
1700                     cr.outputline(tmpptr.getSafeSymbol()+"=");
1701                     cr.outputline("(void *) RepairHashgetrelation("+repairtable.getSafeSymbol()+","+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1702                     cr.outputline("if ("+ismdfyptr.getSafeSymbol()+")");
1703                     {
1704                         VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1705                         String methodcallprefix="("+funptr.getSafeSymbol()+") ";
1706                         cr.startblock();
1707                         cr.addDeclaration("int",mdfyptr.getSafeSymbol());
1708                         cr.outputline(mdfyptr.getSafeSymbol()+"=RepairHashgetrelation2("+repairtable.getSafeSymbol()+","+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
1709                         cr.addDeclaration("void (*"+funptr.getSafeSymbol()+") (struct "+name+"_state *, struct "+name+"*, struct RepairHash *"+parttype+",int,int,int);");
1710                         cr.outputline(funptr.getSafeSymbol()+"="+"(void (*) (struct "+name+"_state *, struct "+name+"*, struct RepairHash *"+parttype+",int,int,int)) "+tmpptr.getSafeSymbol()+";");
1711                         cr.outputline(methodcallprefix+methodcall+","+leftvar+", "+rightvar+", "+mdfyptr.getSafeSymbol() +");");
1712                         cr.endblock();
1713                     }
1714                     cr.outputline("else ");
1715                     {
1716                         VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1717                         String methodcallprefix="("+funptr.getSafeSymbol()+") ";
1718                         cr.startblock();
1719                         cr.addDeclaration("void (*"+funptr.getSafeSymbol()+") (struct "+name+"_state *, struct "+name+"*,struct RepairHash *"+parttype+");");
1720                         cr.outputline(funptr.getSafeSymbol()+"="+"(void (*) (struct "+name+"_state *,struct "+name+"*,struct RepairHash *"+parttype+")) "+tmpptr.getSafeSymbol()+";");
1721                         cr.outputline(methodcallprefix+methodcall+");");
1722                         cr.endblock();
1723                     }
1724                     cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1725                     cr.outputline("goto rebuild;");
1726                 }
1727                 cr.endblock();
1728
1729                 /* Build standard compensation actions */
1730                 if (need_compensation(currentrule)) {
1731                     UpdateNode un=find_compensation(currentrule);
1732                     String name=(String)updatenames.get(un);
1733                     usedupdates.add(un); /* Mark as used */
1734                     String methodcall=name+"(thisvar,"+oldmodel.getSafeSymbol()+","+repairtable.getSafeSymbol();
1735                     for(int i=0;i<currentrule.numQuantifiers();i++) {
1736                         Quantifier q=currentrule.getQuantifier(i);
1737                         if (q instanceof SetQuantifier) {
1738                             SetQuantifier sq=(SetQuantifier) q;
1739                             methodcall+=","+sq.getVar().getSafeSymbol();
1740                         } else if (q instanceof RelationQuantifier) {
1741                             RelationQuantifier rq=(RelationQuantifier) q;
1742                             methodcall+=","+rq.x.getSafeSymbol();
1743                             methodcall+=","+rq.y.getSafeSymbol();
1744                         } else if (q instanceof ForQuantifier) {
1745                             ForQuantifier fq=(ForQuantifier) q;
1746                             methodcall+=","+fq.getVar().getSafeSymbol();
1747                         }
1748                     }
1749                     methodcall+=");";
1750                     cr.outputline(methodcall);
1751                     cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1752                     cr.outputline("goto rebuild;");
1753                 }
1754             }
1755             cr.endblock();
1756         }
1757
1758         String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1759         cr.startblock();
1760         cr.addDeclaration("int" , addeditem);
1761         cr.outputline(addeditem + "=0;");
1762
1763         String ifstring="if (!maybe";
1764         if (rd.getDomain().getType() instanceof StructureTypeDescriptor)  {
1765             ifstring+="&&"+leftvar;
1766         }
1767
1768         if (rd.getRange().getType() instanceof StructureTypeDescriptor)  {
1769             ifstring+="&&"+rightvar;
1770         }
1771
1772         ifstring+=")";
1773
1774         if (rd.testUsage(RelationDescriptor.IMAGE)) {
1775             cr.outputline(ifstring);
1776             cr.outputline(addeditem + " = SimpleHashadd("+rd.getSafeSymbol()+"_hash, (int)" + leftvar + ", (int)" + rightvar+ ");");
1777         }
1778
1779         if (rd.testUsage(RelationDescriptor.INVIMAGE)) {
1780             cr.outputline(ifstring);
1781             cr.outputline(addeditem + " = SimpleHashadd("+rd.getSafeSymbol()+"_hashinv, (int)" + rightvar + ", (int)" + leftvar + ");");
1782         }
1783
1784
1785         Vector dispatchrules = getrulelist(rd);
1786
1787         Set toremove=new HashSet();
1788         for(int i=0;i<dispatchrules.size();i++) {
1789             Rule r=(Rule)dispatchrules.get(i);
1790             if (!ruleset.contains(r))
1791                 toremove.add(r);
1792         }
1793         dispatchrules.removeAll(toremove);
1794         if (dispatchrules.size() == 0) {
1795             cr.outputline("/* nothing to dispatch */");
1796             cr.endblock();
1797             return;
1798         }
1799
1800         cr.outputline("if (" + addeditem + ")");
1801         cr.startblock();
1802
1803         for(int i = 0; i < dispatchrules.size(); i++) {
1804             Rule rule = (Rule) dispatchrules.elementAt(i);
1805             if (rule.getGuardExpr().getRequiredDescriptors().contains(rd)) {
1806                 /* Guard depends on this relation, so we recomput everything */
1807                 cr.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+",-1,0,0);");
1808             } else {
1809                 for (int j=0;j<rule.numQuantifiers();j++) {
1810                     Quantifier q=rule.getQuantifier(j);
1811                     if (q.getRequiredDescriptors().contains(rd)) {
1812                         /* Generate add */
1813                         cr.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+","+j+","+leftvar+","+rightvar+");");
1814                     }
1815                 }
1816             }
1817         }
1818         cr.endblock();
1819         cr.endblock();
1820     }
1821
1822
1823     public void generate_dispatch(CodeWriter cr, SetDescriptor sd, String setvar) {
1824         cr.outputline("/* SET DISPATCH */");
1825         if (Compiler.REPAIR) {
1826             cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1827             cr.outputline("!SimpleHashcontainskey("+oldmodel.getSafeSymbol() +"->"+sd.getJustSafeSymbol()+"_hash, "+setvar+"))");
1828             cr.startblock(); {
1829                 /* Adding new item */
1830                 /* See if there is an outstanding update in the repairtable */
1831                 cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
1832                 cr.outputline("RepairHashcontainsset("+repairtable.getSafeSymbol()+","+sd.getNum()+","+currentrule.getNum()+","+setvar+"))");
1833                 cr.startblock(); {
1834                     /* Have update to call into */
1835                     VarDescriptor funptr=VarDescriptor.makeNew("updateptr");
1836                     String parttype="";
1837                     for(int i=0;i<currentrule.numQuantifiers();i++) {
1838                         if (currentrule.getQuantifier(i) instanceof RelationQuantifier)
1839                             parttype=parttype+", int, int";
1840                         else
1841                             parttype=parttype+", int";
1842                     }
1843                     cr.addDeclaration("void (*"+funptr.getSafeSymbol()+") (struct "+name+"_state *,struct "+name+"*,struct RepairHash *"+parttype+");");
1844                     cr.outputline(funptr.getSafeSymbol()+"=");
1845                     cr.outputline("(void (*) (struct "+name+"_state *,struct "+name+"*,struct RepairHash *"+parttype+")) RepairHashgetset("+repairtable.getSafeSymbol()+","+sd.getNum()+","+currentrule.getNum()+","+setvar+");");
1846                     String methodcall="("+funptr.getSafeSymbol()+") (thisvar,"+oldmodel.getSafeSymbol()+","+
1847                         repairtable.getSafeSymbol();
1848                     for(int i=0;i<currentrule.numQuantifiers();i++) {
1849                         Quantifier q=currentrule.getQuantifier(i);
1850                         if (q instanceof SetQuantifier) {
1851                             SetQuantifier sq=(SetQuantifier) q;
1852                             methodcall+=","+sq.getVar().getSafeSymbol();
1853                         } else if (q instanceof RelationQuantifier) {
1854                             RelationQuantifier rq=(RelationQuantifier) q;
1855                             methodcall+=","+rq.x.getSafeSymbol();
1856                             methodcall+=","+rq.y.getSafeSymbol();
1857                         } else if (q instanceof ForQuantifier) {
1858                             ForQuantifier fq=(ForQuantifier) q;
1859                             methodcall+=","+fq.getVar().getSafeSymbol();
1860                         }
1861                     }
1862                     methodcall+=");";
1863                     cr.outputline(methodcall);
1864                     cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1865                     cr.outputline("goto rebuild;");
1866                 }
1867                 cr.endblock();
1868                 /* Build standard compensation actions */
1869                 Vector ruleset=new Vector();
1870                 ruleset.add(currentrule);
1871                 if (state.implicitruleinv.containsKey(currentrule))
1872                     ruleset.addAll((Set)state.implicitruleinv.get(currentrule));
1873                 for(int i=0;i<ruleset.size();i++) {
1874                     Rule itrule=(Rule)ruleset.get(i);
1875
1876                     if (need_compensation(itrule)) {
1877                         UpdateNode un=find_compensation(itrule);
1878                         String name=(String)updatenames.get(un);
1879                         usedupdates.add(un); /* Mark as used */
1880
1881                         String methodcall=name+"(thisvar,"+oldmodel.getSafeSymbol()+","+
1882                             repairtable.getSafeSymbol();
1883                         for(int j=0;j<currentrule.numQuantifiers();j++) {
1884                             Quantifier q=currentrule.getQuantifier(j);
1885                             if (q instanceof SetQuantifier) {
1886                                 SetQuantifier sq=(SetQuantifier) q;
1887                                 methodcall+=","+sq.getVar().getSafeSymbol();
1888                             } else if (q instanceof RelationQuantifier) {
1889                                 RelationQuantifier rq=(RelationQuantifier) q;
1890                                 methodcall+=","+rq.x.getSafeSymbol();
1891                                 methodcall+=","+rq.y.getSafeSymbol();
1892                             } else if (q instanceof ForQuantifier) {
1893                                 ForQuantifier fq=(ForQuantifier) q;
1894                                 methodcall+=","+fq.getVar().getSafeSymbol();
1895                             }
1896                         }
1897                         methodcall+=");";
1898                         if (currentrule!=itrule) {
1899                             SetDescriptor sdrule=((SetInclusion)itrule.getInclusion()).getSet();
1900                             cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
1901                             cr.outputline("!SimpleHashcontainskey("+ oldmodel.getSafeSymbol() +"->"+sdrule.getJustSafeSymbol() +"_hash,"+setvar+"))");
1902                             cr.startblock();
1903                         }
1904                         cr.outputline(methodcall);
1905                         cr.outputline("free"+name+"("+newmodel.getSafeSymbol()+");");
1906                         cr.outputline("goto rebuild;");
1907                         cr.endblock();
1908                     }
1909                     if (currentrule==itrule)
1910                         cr.endblock();
1911                 }
1912             }
1913         }
1914
1915         cr.startblock();
1916         String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
1917         cr.addDeclaration("int", addeditem);
1918         cr.outputline(addeditem + " = 0;");
1919         if (sd.getType() instanceof StructureTypeDescriptor)  {
1920             cr.outputline("if (!maybe&&"+setvar+")");
1921         } else
1922             cr.outputline("if (!maybe)");
1923         cr.outputline(addeditem + " = SimpleHashadd("+sd.getSafeSymbol()+"_hash, (int)" + setvar +  ", (int)" + setvar + ");");
1924         cr.startblock();
1925         Vector dispatchrules = getrulelist(sd);
1926
1927         Set toremove=new HashSet();
1928         for(int i=0;i<dispatchrules.size();i++) {
1929             Rule r=(Rule)dispatchrules.get(i);
1930             if (!ruleset.contains(r))
1931                 toremove.add(r);
1932         }
1933         dispatchrules.removeAll(toremove);
1934
1935         if (dispatchrules.size() == 0) {
1936             cr.outputline("/* nothing to dispatch */");
1937             cr.endblock();
1938             cr.endblock();
1939             return;
1940         }
1941         /* Add item to worklist if new */
1942         cr.outputline("if ("+addeditem+")");
1943         cr.startblock();
1944         for(int i = 0; i < dispatchrules.size(); i++) {
1945             Rule rule = (Rule) dispatchrules.elementAt(i);
1946             if (SetDescriptor.expand(rule.getGuardExpr().getRequiredDescriptors()).contains(sd)) {
1947                 /* Guard depends on this relation, so we recompute everything */
1948                 cr.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+",-1,0,0);");
1949             } else {
1950                 for (int j=0;j<rule.numQuantifiers();j++) {
1951                     Quantifier q=rule.getQuantifier(j);
1952                     if (SetDescriptor.expand(q.getRequiredDescriptors()).contains(sd)) {
1953                         /* Generate add */
1954                         cr.outputline("WorkListadd("+worklist.getSafeSymbol()+","+rule.getNum()+","+j+","+setvar+",0);");
1955                     }
1956                 }
1957             }
1958         }
1959         cr.endblock();
1960         cr.endblock();
1961         cr.endblock();
1962     }
1963 }