Fixed some errors in the Repair Generator code.
[repair.git] / Repair / RepairCompiler / MCC / IR / RepairGenerator.java
index a3b8efdd219cc208aa33578151c3d9991efdc69f..79b89a65c7cda0189d94cd05ad8b30146d42f3e0 100755 (executable)
@@ -153,6 +153,9 @@ public class RepairGenerator {
                    }
                    craux.startblock();
                    craux.outputline("int maybe=0;");
+                   if (Compiler.GENERATEINSTRUMENT)
+                       craux.outputline("updatecount++;");
+
                    final SymbolTable st = un.getRule().getSymbolTable();                
                    CodeWriter cr = new StandardCodeWriter(outputaux) {
                         public SymbolTable getSymbolTable() { return st; }
@@ -184,6 +187,8 @@ public class RepairGenerator {
                    craux.outputline(methodcall);
                    craux.startblock();
                    craux.outputline("int maybe=0;");
+                   if (Compiler.GENERATEINSTRUMENT)
+                       craux.outputline("updatecount++;");
                    final SymbolTable st2 = un.getRule().getSymbolTable();
                    CodeWriter cr2 = new StandardCodeWriter(outputaux) {
                         public SymbolTable getSymbolTable() { return st2; }
@@ -217,6 +222,8 @@ public class RepairGenerator {
                    craux.outputline(methodcall);
                    craux.startblock();
                    craux.outputline("int maybe=0;");
+                   if (Compiler.GENERATEINSTRUMENT)
+                       craux.outputline("updatecount++;");
                    final SymbolTable st2 = un.getRule().getSymbolTable();
                    CodeWriter cr2 = new StandardCodeWriter(outputaux) {
                         public SymbolTable getSymbolTable() { return st2; }
@@ -465,9 +472,21 @@ public class RepairGenerator {
        worklist=VarDescriptor.makeNew("worklist");
        goodflag=VarDescriptor.makeNew("goodflag");
        repairtable=VarDescriptor.makeNew("repairtable");
+
+       if (Compiler.GENERATEINSTRUMENT) {
+           craux.outputline("int updatecount;");
+           craux.outputline("int rebuildcount;");
+           craux.outputline("int abstractcount;");
+       }
+       
        crhead.outputline("void doanalysis();");
        craux.outputline("void "+name +"_state::doanalysis()");
        craux.startblock();
+       if (Compiler.GENERATEINSTRUMENT) {
+           craux.outputline("updatecount=0;");
+           craux.outputline("rebuildcount=0;");
+           craux.outputline("abstractcount=0;");
+       }
        craux.outputline("int highmark;");
        craux.outputline("initializestack(&highmark);");
        craux.outputline("typeobject *typeobject1=gettypeobject();");
@@ -479,11 +498,20 @@ public class RepairGenerator {
        craux.outputline("while (1)");
        craux.startblock();
        craux.outputline(name+ " * "+newmodel.getSafeSymbol()+"=new "+name+"();");
+       craux.outputline(worklist.getSafeSymbol()+"->reset();");
+       if (Compiler.GENERATEINSTRUMENT)
+           craux.outputline("rebuildcount++;");
     }
     
     private void generate_teardown() {
        CodeWriter cr = new StandardCodeWriter(outputaux);        
        cr.endblock();
+       if (Compiler.GENERATEINSTRUMENT) {
+           cr.outputline("printf(\"updatecount=%d\\n\",updatecount);");
+           cr.outputline("printf(\"rebuildcount=%d\\n\",rebuildcount);");
+           cr.outputline("printf(\"abstractcount=%d\\n\",abstractcount);");
+       }
+
     }
 
     private void generate_print() {
@@ -546,15 +574,54 @@ public class RepairGenerator {
                Iterator iterator_rs = ruleset.iterator();
                while (iterator_rs.hasNext()) {
                    Rule rule = (Rule) iterator_rs.next();
+                   if (rule.getnogenerate())
+                       continue;
                    {
                        final SymbolTable st = rule.getSymbolTable();
                        CodeWriter cr = new StandardCodeWriter(outputaux) {
                                public SymbolTable getSymbolTable() { return st; }
                            };
+                       InvariantValue ivalue=new InvariantValue();
+                       cr.setInvariantValue(ivalue);
+
                        cr.outputline("// build " +escape(rule.toString()));
                        cr.startblock();
                        cr.outputline("int maybe=0;");
-                       ListIterator quantifiers = rule.quantifiers();
+
+                       Expr ruleexpr=rule.getGuardExpr();
+                       HashSet invariantvars=new HashSet();
+                       Set invariants=ruleexpr.findInvariants(invariantvars);
+
+                       if ((ruleexpr instanceof BooleanLiteralExpr)&&
+                           ((BooleanLiteralExpr)ruleexpr).getValue()) {
+                           if (rule.getInclusion() instanceof SetInclusion) {
+                               invariants.addAll(((SetInclusion)rule.getInclusion()).getExpr().findInvariants(invariantvars));
+                           } else if (rule.getInclusion() instanceof RelationInclusion) {
+                               invariants.addAll(((RelationInclusion)rule.getInclusion()).getLeftExpr().findInvariants(invariantvars));
+                               invariants.addAll(((RelationInclusion)rule.getInclusion()).getRightExpr().findInvariants(invariantvars));
+                           }
+                       }
+                       ListIterator quantifiers = rule.quantifiers();                  
+                       while (quantifiers.hasNext()) {
+                           Quantifier quantifier = (Quantifier) quantifiers.next();
+                           if (quantifier instanceof ForQuantifier) {
+                               ForQuantifier fq=(ForQuantifier)quantifier;
+                               invariants.addAll(fq.lower.findInvariants(invariantvars));
+                               invariants.addAll(fq.upper.findInvariants(invariantvars));
+                           }
+                       }
+
+                       for(Iterator invit=invariants.iterator();invit.hasNext();) {
+                           Expr invexpr=(Expr)invit.next();
+                           VarDescriptor tmpvd=VarDescriptor.makeNew("tmpvar");
+                           VarDescriptor maybevd=VarDescriptor.makeNew("maybevar");
+                           invexpr.generate(cr,tmpvd);
+                           cr.outputline("int "+maybevd.getSafeSymbol()+"=maybe;");
+                           cr.outputline("maybe=0;");
+                           ivalue.assignPair(invexpr,tmpvd,maybevd);
+                       }
+
+                       quantifiers = rule.quantifiers();
                        while (quantifiers.hasNext()) {
                            Quantifier quantifier = (Quantifier) quantifiers.next();
                            quantifier.generate_open(cr);
@@ -686,15 +753,18 @@ public class RepairGenerator {
     }
 
     private void generate_checks() {
-
         /* do constraint checks */
-       //        Vector constraints = state.vConstraints;
-
-
-       //        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();
+       Iterator i;
+       if (Compiler.REPAIR)
+           i=termination.constraintdependence.computeOrdering().iterator();
+       else
+           i=state.vConstraints.iterator();
+       for (; i.hasNext();) {
+           Constraint constraint;
+           if (Compiler.REPAIR)
+               constraint= (Constraint) ((GraphNode)i.next()).getOwner();
+           else
+               constraint=(Constraint)i.next();
            
             {
                final SymbolTable st = constraint.getSymbolTable();
@@ -767,16 +837,16 @@ public class RepairGenerator {
                            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 (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) {
                            cr.outputline("if ("+costvar.getSafeSymbol()+"<"+mincost.getSafeSymbol()+")");
                            cr.startblock();
@@ -792,10 +862,10 @@ public class RepairGenerator {
                for(int j=0;j<dnfconst.size();j++) {
                    GraphNode gn=(GraphNode)dnfconst.get(j);
                    Conjunction conj=((TermNode)gn.getOwner()).getConjunction();
-
                    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();
@@ -807,6 +877,8 @@ public class RepairGenerator {
                        else
                            cr.outputline("if (maybe||!"+predvalue.getSafeSymbol()+")");
                        cr.startblock();
+                       if (Compiler.GENERATEINSTRUMENT)
+                           cr.outputline("abstractcount++;");
                        if (p instanceof InclusionPredicate)
                            generateinclusionrepair(conj,dpred, cr);
                        else if (p instanceof ExprPredicate) {
@@ -819,10 +891,11 @@ public class RepairGenerator {
                        cr.endblock();
                    }
                    /* Update model */
+                   cr.endblock();
                    cr.outputline("break;");
                }
                cr.outputline("}");
-
+               
                cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
                cr.outputline("delete "+oldmodel.getSafeSymbol()+";");
                cr.outputline(oldmodel.getSafeSymbol()+"="+newmodel.getSafeSymbol()+";");
@@ -852,10 +925,19 @@ public class RepairGenerator {
        cr.endblock();
        cr.outputline("rebuild:");
        cr.outputline(";");     
-       
     }
     
     private MultUpdateNode getmultupdatenode(Conjunction conj, DNFPredicate dpred, int repairtype) {
+       Set nodes=getmultupdatenodeset(conj,dpred,repairtype);
+       Iterator it=nodes.iterator();
+       if (it.hasNext())
+           return (MultUpdateNode)it.next();
+       else
+           return null;
+    }
+
+    private Set getmultupdatenodeset(Conjunction conj, DNFPredicate dpred, int repairtype) {
+       HashSet hs=new HashSet();
        MultUpdateNode mun=null;
        GraphNode gn=(GraphNode) termination.conjtonodemap.get(conj);
        for(Iterator edgeit=gn.edges();(mun==null)&&edgeit.hasNext();) {
@@ -870,23 +952,54 @@ public class RepairGenerator {
                        if (!removed.contains(gn3)) {
                            TermNode tn3=(TermNode)gn3.getOwner();
                            if (tn3.getType()==TermNode.UPDATE) {
-                               mun=tn3.getUpdate();
-                               break;
+                               hs.add(mun);
                            }
                        }
                    }
                }
            }
        }
-       return mun;
+       return hs;
+    }
+
+    private AbstractRepair getabstractrepair(Conjunction conj, DNFPredicate dpred, int repairtype) {
+       HashSet hs=new HashSet();
+       MultUpdateNode mun=null;
+       GraphNode gn=(GraphNode) termination.conjtonodemap.get(conj);
+       for(Iterator edgeit=gn.edges();(mun==null)&&edgeit.hasNext();) {
+           GraphNode gn2=((GraphNode.Edge) edgeit.next()).getTarget();
+           TermNode tn2=(TermNode)gn2.getOwner();
+           if (tn2.getType()==TermNode.ABSTRACT) {
+               AbstractRepair ar=tn2.getAbstract();
+               if (((repairtype==-1)||(ar.getType()==repairtype))&&
+                   ar.getPredicate()==dpred) {
+                   return ar;
+               }
+           }
+       }
+       return null;
     }
 
+
     /** Generates abstract (and concrete) repair for a comparison */
 
     private void generatecomparisonrepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr){
-       MultUpdateNode munmodify=getmultupdatenode(conj,dpred,AbstractRepair.MODIFYRELATION);
-       MultUpdateNode munremove=getmultupdatenode(conj,dpred,AbstractRepair.REMOVEFROMRELATION);
-       MultUpdateNode munadd=getmultupdatenode(conj,dpred,AbstractRepair.ADDTORELATION);
+       Set updates=getmultupdatenodeset(conj,dpred,AbstractRepair.MODIFYRELATION);
+       AbstractRepair ar=getabstractrepair(conj,dpred,AbstractRepair.MODIFYRELATION);
+       MultUpdateNode munmodify=null;
+       MultUpdateNode munadd=null;
+       MultUpdateNode munremove=null;
+       for(Iterator it=updates.iterator();it.hasNext();) {
+           MultUpdateNode mun=(MultUpdateNode)it.next();
+           if (mun.getType()==MultUpdateNode.ADD) {
+               munadd=mun;
+           } else if (mun.getType()==MultUpdateNode.REMOVE) { 
+               munremove=mun;
+           } else if (mun.getType()==MultUpdateNode.MODIFY) {
+               munmodify=mun;
+           }
+       }
+       
        ExprPredicate ep=(ExprPredicate)dpred.getPredicate();
        RelationDescriptor rd=(RelationDescriptor)ep.getDescriptor();
        boolean usageimage=rd.testUsage(RelationDescriptor.IMAGE);
@@ -973,22 +1086,23 @@ public class RepairGenerator {
                    }
                }
            }
-
        } else {
            /* Start with scheduling removal */
-           for(int i=0;i<state.vRules.size();i++) {
-               Rule r=(Rule)state.vRules.get(i);
-               if (r.getInclusion().getTargetDescriptors().contains(rd)) {
-                   for(int j=0;j<munremove.numUpdates();j++) {
-                       UpdateNode un=munremove.getUpdate(i);
-                       if (un.getRule()==r) {
-                           /* Update for rule r */
-                           String name=(String)updatenames.get(un);
-                           cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+");");
+           
+           if (ar.needsRemoves(state))
+               for(int i=0;i<state.vRules.size();i++) {
+                   Rule r=(Rule)state.vRules.get(i);
+                   if (r.getInclusion().getTargetDescriptors().contains(rd)) {
+                       for(int j=0;j<munremove.numUpdates();j++) {
+                           UpdateNode un=munremove.getUpdate(i);
+                           if (un.getRule()==r) {
+                               /* Update for rule r */
+                               String name=(String)updatenames.get(un);
+                               cr.outputline(repairtable.getSafeSymbol()+"->addrelation("+rd.getNum()+","+r.getNum()+","+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+",(int) &"+name+");");
+                           }
                        }
                    }
                }
-           }
            /* Now do addition */
            UpdateNode un=munadd.getUpdate(0);
            String name=(String)updatenames.get(un);
@@ -1367,7 +1481,11 @@ public class RepairGenerator {
                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++) {
@@ -1399,9 +1517,10 @@ public class RepairGenerator {
                    
                    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.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();
@@ -1516,14 +1635,13 @@ public class RepairGenerator {
 
 
     public void generate_dispatch(CodeWriter cr, SetDescriptor sd, String setvar) {
-               
-        cr.outputline("// SET DISPATCH ");
+       cr.outputline("// SET DISPATCH ");
        if (Compiler.REPAIR) {
            cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
            cr.outputline("!"+oldmodel.getSafeSymbol() +"->"+sd.getJustSafeSymbol()+"_hash->contains("+setvar+"))");
            cr.startblock(); {
                /* Adding new item */
-               /* Perform safety checks */
+               /* See if there is an outstanding update in the repairtable */
                cr.outputline("if ("+repairtable.getSafeSymbol()+"&&");
                cr.outputline(repairtable.getSafeSymbol()+"->containsset("+sd.getNum()+","+currentrule.getNum()+","+setvar+"))");
                cr.startblock(); {
@@ -1561,34 +1679,50 @@ public class RepairGenerator {
                }
                cr.endblock();
                /* Build standard compensation actions */
-               if (need_compensation(currentrule)) {
-                   UpdateNode un=find_compensation(currentrule);
-                   String name=(String)updatenames.get(un);
-                   usedupdates.add(un); /* Mark as used */
+               Vector ruleset=new Vector();
+               ruleset.add(currentrule);
+               if (state.implicitruleinv.containsKey(currentrule))
+                   ruleset.addAll((Set)state.implicitruleinv.get(currentrule));
+               for(int i=0;i<ruleset.size();i++) {
+                   Rule itrule=(Rule)ruleset.get(i);
                    
-                   String methodcall=name+"(this,"+oldmodel.getSafeSymbol()+","+
-                       repairtable.getSafeSymbol();
-                   for(int i=0;i<currentrule.numQuantifiers();i++) {
-                       Quantifier q=currentrule.getQuantifier(i);
-                       if (q instanceof SetQuantifier) {
-                           SetQuantifier sq=(SetQuantifier) q;
-                           methodcall+=","+sq.getVar().getSafeSymbol();
-                       } else if (q instanceof RelationQuantifier) {
-                           RelationQuantifier rq=(RelationQuantifier) q;
-                           methodcall+=","+rq.x.getSafeSymbol();
-                           methodcall+=","+rq.y.getSafeSymbol();
-                       } else if (q instanceof ForQuantifier) {
-                           ForQuantifier fq=(ForQuantifier) q;
-                           methodcall+=","+fq.getVar().getSafeSymbol();
+                   if (need_compensation(itrule)) {
+                       UpdateNode un=find_compensation(itrule);
+                       String name=(String)updatenames.get(un);
+                       usedupdates.add(un); /* Mark as used */
+                       
+                       String methodcall=name+"(this,"+oldmodel.getSafeSymbol()+","+
+                           repairtable.getSafeSymbol();
+                       for(int j=0;j<currentrule.numQuantifiers();j++) {
+                           Quantifier q=currentrule.getQuantifier(j);
+                           if (q instanceof SetQuantifier) {
+                               SetQuantifier sq=(SetQuantifier) q;
+                               methodcall+=","+sq.getVar().getSafeSymbol();
+                           } else if (q instanceof RelationQuantifier) {
+                               RelationQuantifier rq=(RelationQuantifier) q;
+                               methodcall+=","+rq.x.getSafeSymbol();
+                               methodcall+=","+rq.y.getSafeSymbol();
+                           } else if (q instanceof ForQuantifier) {
+                               ForQuantifier fq=(ForQuantifier) q;
+                               methodcall+=","+fq.getVar().getSafeSymbol();
+                           }
                        }
+                       methodcall+=");";
+                       if (currentrule!=itrule) {
+                           SetDescriptor sdrule=((SetInclusion)itrule.getInclusion()).getSet();
+                           cr.outputline("if ("+oldmodel.getSafeSymbol()+"&&");
+                           cr.outputline("!"+oldmodel.getSafeSymbol() +"->"+sdrule.getJustSafeSymbol()+"_hash->contains("+setvar+"))");
+                           cr.startblock();
+                       }
+                       cr.outputline(methodcall);
+                       cr.outputline("delete "+newmodel.getSafeSymbol()+";");
+                       cr.outputline("goto rebuild;");
+                       cr.endblock();
                    }
-                   methodcall+=");";
-                   cr.outputline(methodcall);
-                   cr.outputline("delete "+newmodel.getSafeSymbol()+";");
-                   cr.outputline("goto rebuild;");
+                   if (currentrule==itrule)
+                       cr.endblock();
                }
            }
-           cr.endblock();
        }
 
         String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
@@ -1614,6 +1748,7 @@ public class RepairGenerator {
            cr.endblock();
             return;
         }
+       /* Add item to worklist if new */
        cr.outputline("if ("+addeditem+")");
        cr.startblock();
         for(int i = 0; i < dispatchrules.size(); i++) {