Bug with time instrumentation code.
[repair.git] / Repair / RepairCompiler / MCC / IR / RepairGenerator.java
index 6074bbd504f07b78536f80471bdbacc67393d079..06252cd6764a4484bc88edca126e7a5f6b8f4288 100755 (executable)
@@ -387,7 +387,25 @@ public class RepairGenerator {
        crhead.outputline("~"+name+"();");
         craux.outputline("#include \""+headername+"\"");
         craux.outputline("#include \"size.h\"");
-
+       if (Compiler.TIME) {
+           craux.outputline("#include <sys/time.h>");
+       }
+       if (Compiler.ALLOCATECPLUSPLUS) {
+           for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
+               TypeDescriptor td=(TypeDescriptor)it.next();
+               if (td instanceof StructureTypeDescriptor) {
+                   if (((StructureTypeDescriptor)td).size()>0) {
+                       FieldDescriptor fd=((StructureTypeDescriptor)td).get(0);
+                       if (fd.getSymbol().startsWith("_vptr_")) {
+                           String vtable="_ZTV";
+                           vtable+=td.getSymbol().length();
+                           vtable+=td.getSymbol();
+                           craux.outputline("extern void * "+vtable+";");
+                       }
+                   }
+               }
+           }
+       }
         craux.outputline(name+"::"+name+"() {");
         craux.outputline("// creating hashtables ");
         
@@ -482,6 +500,11 @@ public class RepairGenerator {
        crhead.outputline("void doanalysis();");
        craux.outputline("void "+name +"_state::doanalysis()");
        craux.startblock();
+       if (Compiler.TIME) {
+           craux.outputline("struct timeval _begin_time,_end_time;");
+           craux.outputline("gettimeofday(&_begin_time,NULL);");
+       }
+
        if (Compiler.GENERATEINSTRUMENT) {
            craux.outputline("updatecount=0;");
            craux.outputline("rebuildcount=0;");
@@ -506,6 +529,11 @@ public class RepairGenerator {
     private void generate_teardown() {
        CodeWriter cr = new StandardCodeWriter(outputaux);        
        cr.endblock();
+       if (Compiler.TIME) {
+           cr.outputline("gettimeofday(&_end_time,NULL);");
+           cr.outputline("printf(\"time=%ld uS\\n\",(_end_time.tv_sec-_begin_time.tv_sec)*1000000+_end_time.tv_usec-_begin_time.tv_usec);");
+       }
+
        if (Compiler.GENERATEINSTRUMENT) {
            cr.outputline("printf(\"updatecount=%d\\n\",updatecount);");
            cr.outputline("printf(\"rebuildcount=%d\\n\",rebuildcount);");
@@ -753,15 +781,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();
@@ -788,7 +819,7 @@ public class RepairGenerator {
                 cr.outputline("if (maybe)");
                 cr.startblock();
                 cr.outputline("printf(\"maybe fail " +  escape(constraint.toString()) + ". \\n\");");
-                cr.outputline("exit(1);");
+               //cr.outputline("exit(1);");
                 cr.endblock();
 
                 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
@@ -836,14 +867,14 @@ public class RepairGenerator {
                            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()+")");
                            cr.outputline(costvar.getSafeSymbol()+"+="+cost.getCost(dpred)+";");
                        }
-
+                       
                        if(!first) {
                            cr.outputline("if ("+costvar.getSafeSymbol()+"<"+mincost.getSafeSymbol()+")");
                            cr.startblock();
@@ -859,7 +890,6 @@ 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+":");
@@ -893,7 +923,7 @@ public class RepairGenerator {
                    cr.outputline("break;");
                }
                cr.outputline("}");
-
+               
                cr.outputline("if ("+oldmodel.getSafeSymbol()+")");
                cr.outputline("delete "+oldmodel.getSafeSymbol()+";");
                cr.outputline(oldmodel.getSafeSymbol()+"="+newmodel.getSafeSymbol()+";");
@@ -923,13 +953,21 @@ public class RepairGenerator {
        cr.endblock();
        cr.outputline("rebuild:");
        cr.outputline(";");     
-       
     }
     
     private MultUpdateNode getmultupdatenode(Conjunction conj, DNFPredicate dpred, int repairtype) {
-       MultUpdateNode mun=null;
+       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();
        GraphNode gn=(GraphNode) termination.conjtonodemap.get(conj);
-       for(Iterator edgeit=gn.edges();(mun==null)&&edgeit.hasNext();) {
+       for(Iterator edgeit=gn.edges();edgeit.hasNext();) {
            GraphNode gn2=((GraphNode.Edge) edgeit.next()).getTarget();
            TermNode tn2=(TermNode)gn2.getOwner();
            if (tn2.getType()==TermNode.ABSTRACT) {
@@ -941,23 +979,54 @@ public class RepairGenerator {
                        if (!removed.contains(gn3)) {
                            TermNode tn3=(TermNode)gn3.getOwner();
                            if (tn3.getType()==TermNode.UPDATE) {
-                               mun=tn3.getUpdate();
-                               break;
+                               hs.add(tn3.getUpdate());
                            }
                        }
                    }
                }
            }
        }
-       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);
@@ -969,7 +1038,16 @@ public class RepairGenerator {
        VarDescriptor leftside=VarDescriptor.makeNew("leftside");
        VarDescriptor rightside=VarDescriptor.makeNew("rightside");
        VarDescriptor newvalue=VarDescriptor.makeNew("newvalue");
-       if (!inverted) {
+       boolean needremoveloop=ar.mayNeedFunctionEnforcement(state)&&ar.needsRemoves(state);
+
+       if (needremoveloop&&((munadd==null)||(munremove==null))) {
+           System.out.println("Warning:  need to have individual remove operations for"+dpred.name());
+           needremoveloop=false;
+       }
+       if (needremoveloop)
+           cr.outputline("while (1) {");
+
+       if (!inverted) {
            ((RelationExpr)expr.getLeftExpr()).getExpr().generate(cr,leftside);
            expr.getRightExpr().generate(cr,newvalue);
            cr.outputline(rd.getRange().getType().getGenerateType().getSafeSymbol()+" "+rightside.getSafeSymbol()+";");
@@ -980,22 +1058,8 @@ public class RepairGenerator {
            cr.outputline(rd.getDomain().getType().getGenerateType().getSafeSymbol()+" "+leftside.getSafeSymbol()+";");
            cr.outputline(rd.getSafeSymbol()+"_hashinv->get("+rightside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
        }
-       if (negated)
-           if (opcode==Opcode.GT) {
-               opcode=Opcode.LE;
-           } else if (opcode==Opcode.GE) {
-               opcode=Opcode.LT;
-           } else if (opcode==Opcode.LT) {
-               opcode=Opcode.GE;
-           } else if (opcode==Opcode.LE) {
-               opcode=Opcode.GT;
-           } else if (opcode==Opcode.EQ) {
-               opcode=Opcode.NE;
-           } else if (opcode==Opcode.NE) {
-               opcode=Opcode.EQ;
-           } else {
-               throw new Error("Unrecognized Opcode");
-           }
+
+       opcode=Opcode.translateOpcode(negated,opcode);
 
        if (opcode==Opcode.GT) {
            cr.outputline(newvalue.getSafeSymbol()+"++;");
@@ -1015,6 +1079,35 @@ public class RepairGenerator {
        /* Do abstract repairs */
        if (usageimage) {
            cr.outputline(rd.getSafeSymbol()+"_hash->remove("+leftside.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
+       }
+       if (usageinvimage) {
+           cr.outputline(rd.getSafeSymbol()+"_hashinv->remove("+rightside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
+       }
+
+       if (needremoveloop) {
+           if (!inverted) {
+               cr.outputline("if ("+rd.getSafeSymbol()+"_hash->contains("+leftside.getSafeSymbol()+")) {");
+           } else {
+               cr.outputline("if ("+rd.getSafeSymbol()+"_hashinv->contains("+rightside.getSafeSymbol()+")) {");
+           }
+           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+");");
+                       }
+                   }
+               }
+           }
+           cr.outputline("continue;");
+           cr.outputline("}");
+       }
+
+       if (usageimage) {
            if (!inverted) {
                cr.outputline(rd.getSafeSymbol()+"_hash->add("+leftside.getSafeSymbol()+","+newvalue.getSafeSymbol()+");");
            } else {
@@ -1022,7 +1115,6 @@ public class RepairGenerator {
            }
        }
        if (usageinvimage) {
-           cr.outputline(rd.getSafeSymbol()+"_hashinv->remove("+rightside.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
            if (!inverted) {
                cr.outputline(rd.getSafeSymbol()+"_hashinv->add("+newvalue.getSafeSymbol()+","+leftside.getSafeSymbol()+");");
            } else {
@@ -1030,7 +1122,7 @@ public class RepairGenerator {
            }
        }
        /* Do concrete repairs */
-       if (munmodify!=null) {
+       if (munmodify!=null&&(!ar.mayNeedFunctionEnforcement(state))||(munadd==null)||(ar.needsRemoves(state)&&(munremove==null))) {
            for(int i=0;i<state.vRules.size();i++) {
                Rule r=(Rule)state.vRules.get(i);
                if (r.getInclusion().getTargetDescriptors().contains(rd)) {
@@ -1044,22 +1136,22 @@ 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);
@@ -1069,6 +1161,10 @@ public class RepairGenerator {
                cr.outputline(name+"(this,"+newmodel.getSafeSymbol()+","+repairtable.getSafeSymbol()+","+newvalue.getSafeSymbol()+","+rightside.getSafeSymbol()+");");
            }
        }
+       if (needremoveloop) {
+           cr.outputline("break;");
+           cr.outputline("}");
+       }
     }
 
     public void generatesizerepair(Conjunction conj, DNFPredicate dpred, CodeWriter cr) {
@@ -1139,11 +1235,11 @@ public class RepairGenerator {
            VarDescriptor rightvar=VarDescriptor.makeNew("rightvar");
            if (d instanceof RelationDescriptor) {
                if (ep.inverted()) {
-                   rightvar=((ImageSetExpr)((SizeofExpr)((OpExpr)expr).left).setexpr).getVar();
+                   ((ImageSetExpr)((SizeofExpr)expr.left).setexpr).generate_leftside(cr,rightvar);
                    cr.outputline("int "+leftvar.getSafeSymbol()+";");
                    cr.outputline(d.getSafeSymbol()+"_hashinv->get((int)"+rightvar.getSafeSymbol()+","+leftvar.getSafeSymbol()+");");
                } else {
-                   leftvar=((ImageSetExpr)((SizeofExpr)((OpExpr)expr).left).setexpr).getVar();
+                   ((ImageSetExpr)((SizeofExpr)expr.left).setexpr).generate_leftside(cr,leftvar);
                    cr.outputline("int "+rightvar.getSafeSymbol()+"=0;");
                    cr.outputline(d.getSafeSymbol()+"_hash->get((int)"+leftvar.getSafeSymbol()+","+rightvar.getSafeSymbol()+");");
                }
@@ -1182,13 +1278,21 @@ public class RepairGenerator {
            }
            cr.endblock();
        }
+
+// In some cases the analysis has determined that generating removes
+// is unnecessary
+       if (generateadd&&munadd==null) 
+           generateadd=false;
+
        if (generateadd) {
 
            cr.outputline("for(;"+change.getSafeSymbol()+">0;"+change.getSafeSymbol()+"--)");
            cr.startblock();
            VarDescriptor newobject=VarDescriptor.makeNew("newobject");
            if (d instanceof RelationDescriptor) {
-               VarDescriptor otherside=((ImageSetExpr)((SizeofExpr)((OpExpr)ep.expr).left).setexpr).vd;
+               VarDescriptor otherside=VarDescriptor.makeNew("otherside");
+               ((ImageSetExpr)((SizeofExpr)expr.left).setexpr).generate_leftside(cr,otherside);
+               
                RelationDescriptor rd=(RelationDescriptor)d;
                if (termination.sources.relsetSource(rd,!ep.inverted())) {
                    /* Set Source */