Need {} around case statements because we define variables...C++ is lame.
[repair.git] / Repair / RepairCompiler / MCC / IR / RepairGenerator.java
index 8bff95979b21a2b555a82e1a6d67102e13b9607f..1114e1af8876f682a4271289113db61aad282eb8 100755 (executable)
@@ -25,7 +25,6 @@ public class RepairGenerator {
     HashSet togenerate;
     static boolean DEBUG=false;
     Cost cost;
     HashSet togenerate;
     static boolean DEBUG=false;
     Cost cost;
-    Sources sources;
     ModelRuleDependence mrd;
 
     public RepairGenerator(State state, Termination t) {
     ModelRuleDependence mrd;
 
     public RepairGenerator(State state, Termination t) {
@@ -40,7 +39,6 @@ public class RepairGenerator {
            togenerate.removeAll(removed);
         GraphNode.computeclosure(togenerate,removed);
        cost=new Cost();
            togenerate.removeAll(removed);
         GraphNode.computeclosure(togenerate,removed);
        cost=new Cost();
-       sources=new Sources(state);
        mrd=ModelRuleDependence.doAnalysis(state);
        Repair.repairgenerator=this;
     }
        mrd=ModelRuleDependence.doAnalysis(state);
        Repair.repairgenerator=this;
     }
@@ -88,12 +86,20 @@ public class RepairGenerator {
        generate_call();
        generate_start();
         generate_rules();
        generate_call();
        generate_start();
         generate_rules();
+       if (!Compiler.REPAIR||Compiler.GENERATEDEBUGPRINT) {
+           generate_print();
+       }
         generate_checks();
         generate_teardown();
        CodeWriter crhead = new StandardCodeWriter(this.outputhead);
        CodeWriter craux = new StandardCodeWriter(this.outputaux);
        crhead.outputline("};");
        craux.outputline("}");
         generate_checks();
         generate_teardown();
        CodeWriter crhead = new StandardCodeWriter(this.outputhead);
        CodeWriter craux = new StandardCodeWriter(this.outputaux);
        crhead.outputline("};");
        craux.outputline("}");
+
+       if (Compiler.GENERATEDEBUGHOOKS) {
+           crhead.outputline("void debughook();");
+           craux.outputline("void debughook() {}");
+       }
        generatetypechecks(false);
        generate_computesizes();
        generatetypechecks(true);
        generatetypechecks(false);
        generate_computesizes();
        generatetypechecks(true);
@@ -116,6 +122,8 @@ public class RepairGenerator {
        int count=0;
         CodeWriter crhead = new StandardCodeWriter(outputhead);        
         CodeWriter craux = new StandardCodeWriter(outputaux);        
        int count=0;
         CodeWriter crhead = new StandardCodeWriter(outputhead);        
         CodeWriter craux = new StandardCodeWriter(outputaux);        
+       RelationDescriptor.prefix = "model->";
+       SetDescriptor.prefix = "model->";
 
        /* Rewrite globals */
 
 
        /* Rewrite globals */
 
@@ -203,7 +211,7 @@ public class RepairGenerator {
                            methodcall+=", int "+fq.getVar().getSafeSymbol();
                        }
                    }
                            methodcall+=", int "+fq.getVar().getSafeSymbol();
                        }
                    }
-                   methodcall+=", "+stleft+", "+stright+", "+stnew;
+                   methodcall+=", int "+stleft+", int "+stright+", int "+stnew;
                    methodcall+=")";
                    crhead.outputline(methodcall+";");
                    craux.outputline(methodcall);
                    methodcall+=")";
                    crhead.outputline(methodcall+";");
                    craux.outputline(methodcall);
@@ -288,6 +296,7 @@ public class RepairGenerator {
            };
 
        cr.outputline("void "+name+"_state::computesizes(int *sizearray,int **numele) {");
            };
 
        cr.outputline("void "+name+"_state::computesizes(int *sizearray,int **numele) {");
+       cr.outputline("int maybe=0;");
        for(int i=0;i<max;i++) {
            TypeDescriptor td=tdarray[i];
            Expr size=td.getSizeExpr();
        for(int i=0;i<max;i++) {
            TypeDescriptor td=tdarray[i];
            Expr size=td.getSizeExpr();
@@ -311,6 +320,7 @@ public class RepairGenerator {
                }
            }
        }
                }
            }
        }
+       cr.outputline("if (maybe) printf(\"BAD ERROR\");");
        cr.outputline("}");
     }
 
        cr.outputline("}");
     }
 
@@ -326,6 +336,7 @@ public class RepairGenerator {
                public SymbolTable getSymbolTable() { return st; }
            };
        cr.outputline("void "+name+"_state::recomputesizes() {");
                public SymbolTable getSymbolTable() { return st; }
            };
        cr.outputline("void "+name+"_state::recomputesizes() {");
+       cr.outputline("int maybe=0;");
        for(int i=0;i<max;i++) {
            TypeDescriptor td=tdarray[i];
            Expr size=td.getSizeExpr();
        for(int i=0;i<max;i++) {
            TypeDescriptor td=tdarray[i];
            Expr size=td.getSizeExpr();
@@ -347,6 +358,7 @@ public class RepairGenerator {
                }
            }
        }
                }
            }
        }
+       cr.outputline("if (maybe) printf(\"BAD ERROR\");");
        cr.outputline("}");
     }
 
        cr.outputline("}");
     }
 
@@ -474,6 +486,52 @@ public class RepairGenerator {
        cr.endblock();
     }
 
        cr.endblock();
     }
 
+    private void generate_print() {
+       
+       final SymbolTable st = new SymbolTable();
+
+       CodeWriter cr = new StandardCodeWriter(outputaux) {
+               public SymbolTable getSymbolTable() { return st; }
+           };
+
+       cr.outputline("// printing sets!");
+       cr.outputline("printf(\"\\n\\nPRINTING SETS AND RELATIONS\\n\");");
+
+        Iterator setiterator = state.stSets.descriptors();
+       while (setiterator.hasNext()) {
+           SetDescriptor sd = (SetDescriptor) setiterator.next();
+           if (sd.getSymbol().equals("int") || sd.getSymbol().equals("token")) {
+               continue;
+           }
+
+           String setname = sd.getSafeSymbol();
+
+           cr.startblock();
+           cr.outputline("// printing set " + setname);
+           cr.outputline("printf(\"\\nPrinting set " + sd.getSymbol() + " - %d elements \\n\", " + setname + "_hash->count());");
+           cr.outputline("SimpleIterator __setiterator;");
+           cr.outputline("" + setname + "_hash->iterator(__setiterator);");
+           cr.outputline("while (__setiterator.hasNext())");
+           cr.startblock();
+           cr.outputline("int __setval = (int) __setiterator.next();");
+
+           TypeDescriptor td = sd.getType();
+           if (td instanceof StructureTypeDescriptor) {
+               StructureTypeDescriptor std = (StructureTypeDescriptor) td;
+               VarDescriptor vd = new VarDescriptor ("__setval", "__setval", td, false);
+               std.generate_printout(cr, vd);
+           } else { // Missing type descriptor or reserved type, just print int
+               cr.outputline("printf(\"<%d> \", __setval);");                  
+           }
+
+
+           cr.endblock();
+           cr.endblock();
+       }
+
+       cr.outputline("printf(\"\\n\\n------------------- END PRINTING\\n\");");
+    }
+
     Set ruleset=null;
     private void generate_rules() {
        /* first we must sort the rules */
     Set ruleset=null;
     private void generate_rules() {
        /* first we must sort the rules */
@@ -630,10 +688,13 @@ public class RepairGenerator {
     private void generate_checks() {
 
         /* do constraint checks */
     private void generate_checks() {
 
         /* do constraint checks */
-        Vector constraints = state.vConstraints;
+       //        Vector constraints = state.vConstraints;
+
 
 
-        for (int i = 0; i < constraints.size(); i++) {
-            Constraint constraint = (Constraint) constraints.elementAt(i); 
+       //        for (int i = 0; i < constraints.size(); i++) {
+       //            Constraint constraint = (Constraint) constraints.elementAt(i); 
+       for (Iterator i = termination.constraintdependence.computeOrdering().iterator(); i.hasNext();) {
+           Constraint constraint = (Constraint) ((GraphNode)i.next()).getOwner();
            
             {
                final SymbolTable st = constraint.getSymbolTable();
            
             {
                final SymbolTable st = constraint.getSymbolTable();
@@ -665,20 +726,24 @@ public class RepairGenerator {
 
                 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
                 cr.startblock();
 
                 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
                 cr.startblock();
-                if (!Compiler.REPAIR)
+                if (!Compiler.REPAIR||Compiler.GENERATEDEBUGHOOKS)
                    cr.outputline("printf(\"fail " + escape(constraint.toString()) + ". \\n\");");
                    cr.outputline("printf(\"fail " + escape(constraint.toString()) + ". \\n\");");
-               else {
+               
+               if (Compiler.REPAIR) {
                /* Do repairs */
                /* Build new repair table */
                cr.outputline("if ("+repairtable.getSafeSymbol()+")");
                cr.outputline("delete "+repairtable.getSafeSymbol()+";");
                 cr.outputline(repairtable.getSafeSymbol()+"=new RepairHash();");
                /* Do repairs */
                /* Build new repair table */
                cr.outputline("if ("+repairtable.getSafeSymbol()+")");
                cr.outputline("delete "+repairtable.getSafeSymbol()+";");
                 cr.outputline(repairtable.getSafeSymbol()+"=new RepairHash();");
-
                
                
+               if (Compiler.GENERATEDEBUGHOOKS)
+                   cr.outputline("debughook();");
                /* Compute cost of each repair */
                VarDescriptor mincost=VarDescriptor.makeNew("mincost");
                VarDescriptor mincostindex=VarDescriptor.makeNew("mincostindex");
                /* Compute cost of each repair */
                VarDescriptor mincost=VarDescriptor.makeNew("mincost");
                VarDescriptor mincostindex=VarDescriptor.makeNew("mincostindex");
-               DNFConstraint dnfconst=constraint.dnfconstraint;
+               Vector dnfconst=new Vector();
+               dnfconst.addAll((Set)termination.conjunctionmap.get(constraint));
+
                if (dnfconst.size()<=1) {
                    cr.outputline("int "+mincostindex.getSafeSymbol()+"=0;");
                }
                if (dnfconst.size()<=1) {
                    cr.outputline("int "+mincostindex.getSafeSymbol()+"=0;");
                }
@@ -686,8 +751,8 @@ public class RepairGenerator {
                    cr.outputline("int "+mincostindex.getSafeSymbol()+";");
                    boolean first=true;
                    for(int j=0;j<dnfconst.size();j++) {
                    cr.outputline("int "+mincostindex.getSafeSymbol()+";");
                    boolean first=true;
                    for(int j=0;j<dnfconst.size();j++) {
-                       Conjunction conj=dnfconst.get(j);
-                       GraphNode gn=(GraphNode)termination.conjtonodemap.get(conj);
+                       GraphNode gn=(GraphNode)dnfconst.get(j);
+                       Conjunction conj=((TermNode)gn.getOwner()).getConjunction();
                        if (removed.contains(gn))
                            continue;
                        
                        if (removed.contains(gn))
                            continue;
                        
@@ -702,14 +767,14 @@ public class RepairGenerator {
                            boolean negate=dpred.isNegated();
                            VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
                            p.generate(cr,predvalue);
                            boolean negate=dpred.isNegated();
                            VarDescriptor predvalue=VarDescriptor.makeNew("Predicatevalue");
                            p.generate(cr,predvalue);
+                           if (k==0)
+                               cr.outputline("int "+costvar.getSafeSymbol()+"=0;");
+
                            if (negate)
                                cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
                            else
                                cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
                            if (negate)
                                cr.outputline("if (maybe||"+predvalue.getSafeSymbol()+")");
                            else
                                cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
-                           if (k==0)
-                               cr.outputline("int "+costvar.getSafeSymbol()+"="+cost.getCost(dpred)+";");
-                           else
-                               cr.outputline(costvar.getSafeSymbol()+"+="+cost.getCost(dpred)+";");
+                           cr.outputline(costvar.getSafeSymbol()+"+="+cost.getCost(dpred)+";");
                        }
 
                        if(!first) {
                        }
 
                        if(!first) {
@@ -725,11 +790,13 @@ public class RepairGenerator {
                }
                cr.outputline("switch("+mincostindex.getSafeSymbol()+") {");
                for(int j=0;j<dnfconst.size();j++) {
                }
                cr.outputline("switch("+mincostindex.getSafeSymbol()+") {");
                for(int j=0;j<dnfconst.size();j++) {
-                   Conjunction conj=dnfconst.get(j);
-                   GraphNode gn=(GraphNode)termination.conjtonodemap.get(conj);
+                   GraphNode gn=(GraphNode)dnfconst.get(j);
+                   Conjunction conj=((TermNode)gn.getOwner()).getConjunction();
+
                    if (removed.contains(gn))
                        continue;
                    cr.outputline("case "+j+":");
                    if (removed.contains(gn))
                        continue;
                    cr.outputline("case "+j+":");
+                   cr.startblock();
                    for(int k=0;k<conj.size();k++) {
                        DNFPredicate dpred=conj.get(k);
                        Predicate p=dpred.getPredicate();
                    for(int k=0;k<conj.size();k++) {
                        DNFPredicate dpred=conj.get(k);
                        Predicate p=dpred.getPredicate();
@@ -753,6 +820,7 @@ public class RepairGenerator {
                        cr.endblock();
                    }
                    /* Update model */
                        cr.endblock();
                    }
                    /* Update model */
+                   cr.endblock();
                    cr.outputline("break;");
                }
                cr.outputline("}");
                    cr.outputline("break;");
                }
                cr.outputline("}");
@@ -833,15 +901,15 @@ public class RepairGenerator {
        VarDescriptor rightside=VarDescriptor.makeNew("rightside");
        VarDescriptor newvalue=VarDescriptor.makeNew("newvalue");
        if (!inverted) {
        VarDescriptor rightside=VarDescriptor.makeNew("rightside");
        VarDescriptor newvalue=VarDescriptor.makeNew("newvalue");
        if (!inverted) {
-           expr.getLeftExpr().generate(cr,leftside);
+           ((RelationExpr)expr.getLeftExpr()).getExpr().generate(cr,leftside);
            expr.getRightExpr().generate(cr,newvalue);
            cr.outputline(rd.getRange().getType().getGenerateType().getSafeSymbol()+" "+rightside.getSafeSymbol()+";");
            cr.outputline(rd.getSafeSymbol()+"_hash->get("+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
        } else {
            expr.getRightExpr().generate(cr,newvalue);
            cr.outputline(rd.getRange().getType().getGenerateType().getSafeSymbol()+" "+rightside.getSafeSymbol()+";");
            cr.outputline(rd.getSafeSymbol()+"_hash->get("+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
        } else {
-           expr.getLeftExpr().generate(cr,rightside);
+           ((RelationExpr)expr.getLeftExpr()).getExpr().generate(cr,rightside);
            expr.getRightExpr().generate(cr,newvalue);
            cr.outputline(rd.getDomain().getType().getGenerateType().getSafeSymbol()+" "+leftside.getSafeSymbol()+";");
            expr.getRightExpr().generate(cr,newvalue);
            cr.outputline(rd.getDomain().getType().getGenerateType().getSafeSymbol()+" "+leftside.getSafeSymbol()+";");
-           cr.outputline(rd.getSafeSymbol()+"_hashinv->get("+leftside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
+           cr.outputline(rd.getSafeSymbol()+"_hashinv->get("+rightside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
        }
        if (negated)
            if (opcode==Opcode.GT) {
        }
        if (negated)
            if (opcode==Opcode.GT) {
@@ -1053,24 +1121,25 @@ public class RepairGenerator {
            if (d instanceof RelationDescriptor) {
                VarDescriptor otherside=((ImageSetExpr)((SizeofExpr)((OpExpr)ep.expr).left).setexpr).vd;
                RelationDescriptor rd=(RelationDescriptor)d;
            if (d instanceof RelationDescriptor) {
                VarDescriptor otherside=((ImageSetExpr)((SizeofExpr)((OpExpr)ep.expr).left).setexpr).vd;
                RelationDescriptor rd=(RelationDescriptor)d;
-               if (sources.relsetSource(rd,!ep.inverted())) {
+               if (termination.sources.relsetSource(rd,!ep.inverted())) {
                    /* Set Source */
                    /* Set Source */
-                   SetDescriptor sd=sources.relgetSourceSet(rd,!ep.inverted());
+                   SetDescriptor sd=termination.sources.relgetSourceSet(rd,!ep.inverted());
                    VarDescriptor iterator=VarDescriptor.makeNew("iterator");
                    cr.outputline(sd.getType().getGenerateType().getSafeSymbol() +" "+newobject.getSafeSymbol()+";");
                    VarDescriptor iterator=VarDescriptor.makeNew("iterator");
                    cr.outputline(sd.getType().getGenerateType().getSafeSymbol() +" "+newobject.getSafeSymbol()+";");
-                   cr.outputline("for("+iterator.getSafeSymbol()+"="+sd.getSafeSymbol()+"_hash->iterator();"+iterator.getSafeSymbol()+".hasNext();)");
+                   cr.outputline("SimpleIterator "+iterator.getSafeSymbol()+";");
+                   cr.outputline("for("+sd.getSafeSymbol()+"_hash->iterator("+ iterator.getSafeSymbol() +");"+iterator.getSafeSymbol()+".hasNext();)");
                    cr.startblock();
                    if (ep.inverted()) {
                    cr.startblock();
                    if (ep.inverted()) {
-                       cr.outputline("if !"+rd.getSafeSymbol()+"_hashinv->contains("+iterator.getSafeSymbol()+"->key(),"+otherside.getSafeSymbol()+")");
+                       cr.outputline("if (!"+rd.getSafeSymbol()+"_hashinv->contains("+iterator.getSafeSymbol()+".key(),"+otherside.getSafeSymbol()+"))");
                    } else {
                    } else {
-                       cr.outputline("if !"+rd.getSafeSymbol()+"_hash->contains("+otherside.getSafeSymbol()+","+iterator.getSafeSymbol()+"->key())");
+                       cr.outputline("if (!"+rd.getSafeSymbol()+"_hash->contains("+otherside.getSafeSymbol()+","+iterator.getSafeSymbol()+".key()))");
                    }
                    cr.outputline(newobject.getSafeSymbol()+"="+iterator.getSafeSymbol()+".key();");
                    }
                    cr.outputline(newobject.getSafeSymbol()+"="+iterator.getSafeSymbol()+".key();");
-                   cr.outputline(iterator.getSafeSymbol()+"->next();");
+                   cr.outputline(iterator.getSafeSymbol()+".next();");
                    cr.endblock();
                    cr.endblock();
-               } else if (sources.relallocSource(rd,!ep.inverted())) {
+               } else if (termination.sources.relallocSource(rd,!ep.inverted())) {
                    /* Allocation Source*/
                    /* Allocation Source*/
-                   sources.relgenerateSourceAlloc(cr,newobject,rd,!ep.inverted());
+                   termination.sources.relgenerateSourceAlloc(cr,newobject,rd,!ep.inverted());
                } else throw new Error("No source for adding to Relation");
                if (ep.inverted()) {
                    boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
                } else throw new Error("No source for adding to Relation");
                if (ep.inverted()) {
                    boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
@@ -1096,21 +1165,22 @@ public class RepairGenerator {
                }
            } else {
                SetDescriptor sd=(SetDescriptor)d;
                }
            } else {
                SetDescriptor sd=(SetDescriptor)d;
-               if (sources.setSource(sd)) {
+               if (termination.sources.setSource(sd)) {
                    /* Set Source */
                    /* Set Source */
                    /* Set Source */
                    /* Set Source */
-                   SetDescriptor sourcesd=sources.getSourceSet(sd);
+                   SetDescriptor sourcesd=termination.sources.getSourceSet(sd);
                    VarDescriptor iterator=VarDescriptor.makeNew("iterator");
                    cr.outputline(sourcesd.getType().getGenerateType().getSafeSymbol() +" "+newobject.getSafeSymbol()+";");
                    VarDescriptor iterator=VarDescriptor.makeNew("iterator");
                    cr.outputline(sourcesd.getType().getGenerateType().getSafeSymbol() +" "+newobject.getSafeSymbol()+";");
-                   cr.outputline("for("+iterator.getSafeSymbol()+"="+sourcesd.getSafeSymbol()+"_hash->iterator();"+iterator.getSafeSymbol()+".hasNext();)");
+                   cr.outputline("SimpleIterator "+iterator.getSafeSymbol()+";");
+                   cr.outputline("for("+sourcesd.getSafeSymbol()+"_hash->iterator("+iterator.getSafeSymbol()+");"+iterator.getSafeSymbol()+".hasNext();)");
                    cr.startblock();
                    cr.startblock();
-                   cr.outputline("if !"+sd.getSafeSymbol()+"_hash->contains("+iterator.getSafeSymbol()+"->key())");
+                   cr.outputline("if (!"+sd.getSafeSymbol()+"_hash->contains("+iterator.getSafeSymbol()+".key()))");
                    cr.outputline(newobject.getSafeSymbol()+"="+iterator.getSafeSymbol()+".key();");
                    cr.outputline(newobject.getSafeSymbol()+"="+iterator.getSafeSymbol()+".key();");
-                   cr.outputline(iterator.getSafeSymbol()+"->next();");
+                   cr.outputline(iterator.getSafeSymbol()+".next();");
                    cr.endblock();
                    cr.endblock();
-               } else if (sources.allocSource(sd)) {
+               } else if (termination.sources.allocSource(sd)) {
                    /* Allocation Source*/
                    /* Allocation Source*/
-                   sources.generateSourceAlloc(cr,newobject,sd);
+                   termination.sources.generateSourceAlloc(cr,newobject,sd);
                } else throw new Error("No source for adding to Set");
                cr.outputline(sd.getSafeSymbol()+"_hash->add("+newobject.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
                UpdateNode un=munadd.getUpdate(0);
                } else throw new Error("No source for adding to Set");
                cr.outputline(sd.getSafeSymbol()+"_hash->add("+newobject.getSafeSymbol()+","+newobject.getSafeSymbol()+");");
                UpdateNode un=munadd.getUpdate(0);
@@ -1151,7 +1221,7 @@ public class RepairGenerator {
                    Rule r=(Rule)state.vRules.get(i);
                    if (r.getInclusion().getTargetDescriptors().contains(rd)) {
                        for(int j=0;j<mun.numUpdates();j++) {
                    Rule r=(Rule)state.vRules.get(i);
                    if (r.getInclusion().getTargetDescriptors().contains(rd)) {
                        for(int j=0;j<mun.numUpdates();j++) {
-                           UpdateNode un=mun.getUpdate(i);
+                           UpdateNode un=mun.getUpdate(j);
                            if (un.getRule()==r) {
                                /* Update for rule rule r */
                                String name=(String)updatenames.get(un);
                            if (un.getRule()==r) {
                                /* Update for rule rule r */
                                String name=(String)updatenames.get(un);
@@ -1172,7 +1242,7 @@ public class RepairGenerator {
                    Rule r=(Rule)state.vRules.get(i);
                    if (r.getInclusion().getTargetDescriptors().contains(sd)) {
                        for(int j=0;j<mun.numUpdates();j++) {
                    Rule r=(Rule)state.vRules.get(i);
                    if (r.getInclusion().getTargetDescriptors().contains(sd)) {
                        for(int j=0;j<mun.numUpdates();j++) {
-                           UpdateNode un=mun.getUpdate(i);
+                           UpdateNode un=mun.getUpdate(j);
                            if (un.getRule()==r) {
                                /* Update for rule rule r */
                                String name=(String)updatenames.get(un);
                            if (un.getRule()==r) {
                                /* Update for rule rule r */
                                String name=(String)updatenames.get(un);
@@ -1299,7 +1369,11 @@ public class RepairGenerator {
                cr.startblock(); {
                    /* Have update to call into */
                    VarDescriptor mdfyptr=VarDescriptor.makeNew("modifyptr");
                cr.startblock(); {
                    /* Have update to call into */
                    VarDescriptor mdfyptr=VarDescriptor.makeNew("modifyptr");
-                   cr.outputline("int "+mdfyptr.getSafeSymbol()+"="+repairtable.getSafeSymbol()+"->getrelation2("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
+                   VarDescriptor ismdfyptr=VarDescriptor.makeNew("ismodifyptr");
+                   cr.outputline("int "+ismdfyptr.getSafeSymbol()+"="+repairtable.getSafeSymbol()+"->ismodify("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
+
+
+
                    
                    String parttype="";
                    for(int i=0;i<currentrule.numQuantifiers();i++) {
                    
                    String parttype="";
                    for(int i=0;i<currentrule.numQuantifiers();i++) {
@@ -1328,23 +1402,25 @@ public class RepairGenerator {
                    }
                    
                    
                    }
                    
                    
-               
+                   
                    cr.outputline("void *"+tmpptr.getSafeSymbol()+"=");
                    cr.outputline("(void *) "+repairtable.getSafeSymbol()+"->getrelation("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
                    cr.outputline("void *"+tmpptr.getSafeSymbol()+"=");
                    cr.outputline("(void *) "+repairtable.getSafeSymbol()+"->getrelation("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
-                   cr.outputline("if ("+mdfyptr.getSafeSymbol()+")");
+                   cr.outputline("if ("+ismdfyptr.getSafeSymbol()+")");
                    {
                        cr.startblock();
                    {
                        cr.startblock();
-                       cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+",int,int,int)="+"(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+",int,int,int)) "+tmpptr.getSafeSymbol());
-                       cr.outputline(methodcall+leftvar+", "+rightvar+", "+mdfyptr.getSafeSymbol() +");");
+                       cr.outputline("int "+mdfyptr.getSafeSymbol()+"="+repairtable.getSafeSymbol()+"->getrelation2("+rd.getNum()+","+currentrule.getNum()+","+leftvar+","+rightvar+");");
+                       cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+",int,int,int)="+"(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+",int,int,int)) "+tmpptr.getSafeSymbol()+";");
+                       cr.outputline(methodcall+","+leftvar+", "+rightvar+", "+mdfyptr.getSafeSymbol() +");");
                        cr.endblock();
                    }
                    cr.outputline("else ");
                    {
                        cr.startblock();
                        cr.endblock();
                    }
                    cr.outputline("else ");
                    {
                        cr.startblock();
-                       cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")="+"(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")) "+tmpptr.getSafeSymbol());
+                       cr.outputline("void (*"+funptr.getSafeSymbol()+") ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")="+"(void (*) ("+name+"_state *,"+name+"*,RepairHash *"+parttype+")) "+tmpptr.getSafeSymbol()+";");
                        cr.outputline(methodcall+");");
                        cr.endblock();
                    }
                        cr.outputline(methodcall+");");
                        cr.endblock();
                    }
+                   cr.outputline("delete "+newmodel.getSafeSymbol()+";");
                    cr.outputline("goto rebuild;");
                }
                cr.endblock();
                    cr.outputline("goto rebuild;");
                }
                cr.endblock();
@@ -1371,6 +1447,7 @@ public class RepairGenerator {
                    }
                    methodcall+=");";
                    cr.outputline(methodcall);
                    }
                    methodcall+=");";
                    cr.outputline(methodcall);
+                   cr.outputline("delete "+newmodel.getSafeSymbol()+";");
                    cr.outputline("goto rebuild;");
                }
            }
                    cr.outputline("goto rebuild;");
                }
            }
@@ -1378,17 +1455,36 @@ public class RepairGenerator {
        }
 
         String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
        }
 
         String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
-       cr.outputline("int " + addeditem + ";");
+       cr.outputline("int " + addeditem + "=0;");
+
+       String ifstring="if (!maybe&&";
+       boolean dogenerate=false;
+       if (rd.getDomain().getType() instanceof StructureTypeDescriptor)  {
+           dogenerate=true;
+           ifstring+=leftvar;
+       }
+
+       if (rd.getRange().getType() instanceof StructureTypeDescriptor)  {
+           if (dogenerate)
+               ifstring+="&&"+rightvar;
+           else
+               ifstring+=rightvar;
+           dogenerate=true;
+       }
+
+       ifstring+=")";
+
        if (rd.testUsage(RelationDescriptor.IMAGE)) {
        if (rd.testUsage(RelationDescriptor.IMAGE)) {
+           cr.outputline(ifstring);
            cr.outputline(addeditem + " = " + rd.getSafeSymbol() + "_hash->add((int)" + leftvar + ", (int)" + rightvar+ ");");
        }
        
        if (rd.testUsage(RelationDescriptor.INVIMAGE)) {
            cr.outputline(addeditem + " = " + rd.getSafeSymbol() + "_hash->add((int)" + leftvar + ", (int)" + rightvar+ ");");
        }
        
        if (rd.testUsage(RelationDescriptor.INVIMAGE)) {
+           cr.outputline(ifstring);
            cr.outputline(addeditem + " = " + rd.getSafeSymbol() + "_hashinv->add((int)" + rightvar + ", (int)" + leftvar + ");");
        }
        
 
            cr.outputline(addeditem + " = " + rd.getSafeSymbol() + "_hashinv->add((int)" + rightvar + ", (int)" + leftvar + ");");
        }
        
 
-
         Vector dispatchrules = getrulelist(rd);
         
        Set toremove=new HashSet();
         Vector dispatchrules = getrulelist(rd);
         
        Set toremove=new HashSet();
@@ -1467,6 +1563,7 @@ public class RepairGenerator {
                    }
                    methodcall+=");";
                    cr.outputline(methodcall);
                    }
                    methodcall+=");";
                    cr.outputline(methodcall);
+                   cr.outputline("delete "+newmodel.getSafeSymbol()+";");
                    cr.outputline("goto rebuild;");
                }
                cr.endblock();
                    cr.outputline("goto rebuild;");
                }
                cr.endblock();
@@ -1494,6 +1591,7 @@ public class RepairGenerator {
                    }
                    methodcall+=");";
                    cr.outputline(methodcall);
                    }
                    methodcall+=");";
                    cr.outputline(methodcall);
+                   cr.outputline("delete "+newmodel.getSafeSymbol()+";");
                    cr.outputline("goto rebuild;");
                }
            }
                    cr.outputline("goto rebuild;");
                }
            }
@@ -1501,7 +1599,11 @@ public class RepairGenerator {
        }
 
         String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
        }
 
         String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
-       cr.outputline("int " + addeditem + " = 1;");
+       cr.outputline("int " + addeditem + " = 0;");
+       if (sd.getType() instanceof StructureTypeDescriptor)  {
+           cr.outputline("if (!maybe&&"+setvar+")");
+       } else
+           cr.outputline("if (!maybe)");
        cr.outputline(addeditem + " = " + sd.getSafeSymbol() + "_hash->add((int)" + setvar +  ", (int)" + setvar + ");");
        cr.startblock();
         Vector dispatchrules = getrulelist(sd);
        cr.outputline(addeditem + " = " + sd.getSafeSymbol() + "_hash->add((int)" + setvar +  ", (int)" + setvar + ");");
        cr.startblock();
         Vector dispatchrules = getrulelist(sd);