Options to print prettier graphs...
[repair.git] / Repair / RepairCompiler / MCC / IR / Termination.java
index 09e1abe62bf8e99506fea8aabb7101a01d0a5d28..5d25732106884d1a26f81fce5a483f974d3559be 100755 (executable)
@@ -31,6 +31,12 @@ public class Termination {
     ExactSize exactsize;
     ArrayAnalysis arrayanalysis;
     Sources sources;
+    static String conjoption="style=bold";
+    static String absrepoption="shape=box,color=blue,style=bold";
+    static String updateoption="shape=box,color=red,style=bold";
+    static String scopeoption="color=brown";
+    static String conseqoption="style=bold,color=green";
+    static String compoption="shape=box,color=purple,style=bold";
 
     public Termination(State state) {
        this.state=state;
@@ -50,7 +56,7 @@ public class Termination {
        predtoabstractmap=new Hashtable();
        if (!Compiler.REPAIR)
            return;
-       
+
 
        for(int i=0;i<state.vRules.size();i++)
            System.out.println(state.vRules.get(i));
@@ -67,20 +73,27 @@ public class Termination {
        generateconjunctionnodes();
        constraintdependence=new ConstraintDependence(state,this);
 
+        debugmsg("Generating scope nodes");
        generatescopenodes();
+        debugmsg("Generating repair nodes");
        generaterepairnodes();
+        debugmsg("Generating data structure nodes");
        generatedatastructureupdatenodes();
-       generatecompensationnodes();
-
+        debugmsg("Generating compensation nodes");
+       if (!Compiler.OMITCOMP)
+           generatecompensationnodes();
+        debugmsg("Generating abstract edges");
        generateabstractedges();
+        debugmsg("Generating scope edges");
        generatescopeedges();
+        debugmsg("Generating update edges");
        generateupdateedges();
 
 
        HashSet superset=new HashSet();
        superset.addAll(conjunctions);
        HashSet closureset=new HashSet();
-
+        debugmsg("Computing closure");
        GraphNode.computeclosure(superset,closureset);
        try {
            GraphNode.DOTVisitor.visit(new FileOutputStream("graph.dot"),superset);
@@ -129,14 +142,14 @@ public class Termination {
            DebugItem di=(DebugItem) Compiler.debuggraphs.get(i);
            HashSet superset=new HashSet();
            Constraint constr=(Constraint)state.vConstraints.get(di.constraintnum);
-           
+
            if (di.conjunctionnum==-1) {
                superset.addAll((Set)conjunctionmap.get(constr));
            } else {
                DNFConstraint dnf=constr.dnfconstraint;
                superset.add(conjtonodemap.get(dnf.get(di.conjunctionnum)));
            }
-           
+
            GraphNode.boundedcomputeclosure(superset,null,di.depth);
            try {
                GraphNode.DOTVisitor.visit(new FileOutputStream("graph"+di.constraintnum+"-"+di.depth+(di.conjunctionnum==-1?"":"-"+di.conjunctionnum)+".dot"),superset);
@@ -164,6 +177,7 @@ public class Termination {
                GraphNode gn=new GraphNode("Conj"+i+"A"+j,
                                           "Conj ("+i+","+j+") "+dnf.get(j).name()
                                           ,tn);
+               gn.setOption(conjoption);
                conjunctions.add(gn);
                if (!conjunctionmap.containsKey(c))
                    conjunctionmap.put(c,new HashSet());
@@ -184,6 +198,7 @@ public class Termination {
                    GraphNode gn=new GraphNode("Conj"+i+"AQ"+j,
                                               "Conj ("+i+","+j+") "+dconst.get(0).name()
                                               ,tn);
+                   gn.setOption(conjoption);
                    conjunctions.add(gn);
                    if (!conjunctionmap.containsKey(c))
                        conjunctionmap.put(c,new HashSet());
@@ -201,6 +216,7 @@ public class Termination {
                    GraphNode gn=new GraphNode("Conj"+i+"AQ"+j,
                                               "Conj ("+i+","+j+") "+dconst.get(0).name()
                                               ,tn);
+                   gn.setOption(conjoption);
                    conjunctions.add(gn);
                    if (!conjunctionmap.containsKey(c))
                        conjunctionmap.put(c,new HashSet());
@@ -251,7 +267,7 @@ public class Termination {
            }
            /* Cycle through the rules to look for possible conflicts */
            for(int i=0;i<state.vRules.size();i++) {
-               Rule r=(Rule) state.vRules.get(i);  
+               Rule r=(Rule) state.vRules.get(i);
                if (concreteinterferes.interferes(mun,r,true)) {
                    GraphNode scopenode=(GraphNode)scopesatisfy.get(r);
                    GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
@@ -271,7 +287,7 @@ public class Termination {
            GraphNode gn=(GraphNode)absiterator.next();
            TermNode tn=(TermNode)gn.getOwner();
            AbstractRepair ar=(AbstractRepair)tn.getAbstract();
-       
+
            for(Iterator conjiterator=conjunctions.iterator();conjiterator.hasNext();) {
                GraphNode gn2=(GraphNode)conjiterator.next();
                TermNode tn2=(TermNode)gn2.getOwner();
@@ -279,19 +295,25 @@ public class Termination {
                Constraint cons=tn2.getConstraint();
                /* See if this is a relation wellformedness constraint
                    that is trivially satisfied. */
-               System.out.println(gn.getTextLabel()+"---"+gn2.getTextLabel());
                if (abstractinterferes.checkrelationconstraint(ar, cons))
                    continue;
-
-               for(int i=0;i<conj.size();i++) {
-                   DNFPredicate dp=conj.get(i);
-                   if (AbstractInterferes.interferesquantifier(ar,cons)||
-                       abstractinterferes.interfereswithpredicate(ar,dp)) {
-                       GraphNode.Edge e=new GraphNode.Edge("interferes",gn2);
-                       gn.addEdge(e);
-                       break;
-                   }
-               }
+                if (AbstractInterferes.interferesquantifier(ar,cons)) {
+                    GraphNode.Edge e=new GraphNode.Edge("interferes",gn2);
+                    gn.addEdge(e);
+                } else {
+                    for(int i=0;i<conj.size();i++) {
+                        DNFPredicate dp=conj.get(i);
+
+                        if (!abstractinterferes.interfereswithpredicate(ar,dp))
+                            continue;
+                        if (getConstraint(gn)==null||
+                            !abstractinterferes.interferemodifies(ar,getConstraint(gn),dp,cons)) {
+                            GraphNode.Edge e=new GraphNode.Edge("interferes",gn2);
+                            gn.addEdge(e);
+                            break;
+                        }
+                    }
+                }
            }
            for(Iterator scopeiterator=scopenodes.iterator();scopeiterator.hasNext();) {
                GraphNode gn2=(GraphNode)scopeiterator.next();
@@ -305,15 +327,30 @@ public class Termination {
        }
     }
 
+    Constraint getConstraint(GraphNode gn) {
+        for(Iterator it=gn.inedges();it.hasNext();) {
+            GraphNode.Edge e=(GraphNode.Edge)it.next();
+            GraphNode gnsource=e.getSource();
+            TermNode tnsource=(TermNode)gnsource.getOwner();
+            if (tnsource.getType()==TermNode.CONJUNCTION) {
+                return tnsource.getConstraint();
+            }
+        }
+        return null;
+    }
+
     void generatescopenodes() {
        for(int i=0;i<state.vRules.size();i++) {
            Rule r=(Rule) state.vRules.get(i);
            ScopeNode satisfy=new ScopeNode(r,true);
            TermNode tnsatisfy=new TermNode(satisfy);
            GraphNode gnsatisfy=new GraphNode("SatisfyRule"+i,tnsatisfy);
+           gnsatisfy.setOption(scopeoption);
+           gnsatisfy.setMerge();
            ConsequenceNode cnsatisfy=new ConsequenceNode();
            TermNode ctnsatisfy=new TermNode(cnsatisfy);
            GraphNode cgnsatisfy=new GraphNode("ConseqSatisfyRule"+i,ctnsatisfy);
+           cgnsatisfy.setOption(conseqoption);
            consequence.put(satisfy,cgnsatisfy);
            GraphNode.Edge esat=new GraphNode.Edge("consequencesatisfy"+i,cgnsatisfy);
            gnsatisfy.addEdge(esat);
@@ -324,9 +361,12 @@ public class Termination {
            ScopeNode falsify=new ScopeNode(r,false);
            TermNode tnfalsify=new TermNode(falsify);
            GraphNode gnfalsify=new GraphNode("FalsifyRule"+i,tnfalsify);
+           gnfalsify.setOption(scopeoption);
+           gnfalsify.setMerge();
            ConsequenceNode cnfalsify=new ConsequenceNode();
            TermNode ctnfalsify=new TermNode(cnfalsify);
            GraphNode cgnfalsify=new GraphNode("ConseqFalsifyRule"+i,ctnfalsify);
+           cgnfalsify.setOption(conseqoption);
            consequence.put(falsify,cgnfalsify);
            GraphNode.Edge efals=new GraphNode.Edge("consequencefalsify"+i,cgnfalsify);
            gnfalsify.addEdge(efals);
@@ -335,13 +375,13 @@ public class Termination {
            scopenodes.add(gnfalsify);
        }
     }
-    
+
     void generatescopeedges() {
        for(Iterator scopeiterator=scopenodes.iterator();scopeiterator.hasNext();) {
            GraphNode gn=(GraphNode)scopeiterator.next();
            TermNode tn=(TermNode)gn.getOwner();
            ScopeNode sn=tn.getScope();
-           
+
            /* Interference edges with conjunctions */
            for(Iterator conjiterator=conjunctions.iterator();conjiterator.hasNext();) {
                GraphNode gn2=(GraphNode)conjiterator.next();
@@ -418,7 +458,9 @@ public class Termination {
                for(int j=0;j<array.length;j++) {
                    AbstractRepair ar=new AbstractRepair(dp,array[j],d,sources);
                    TermNode tn2=new TermNode(ar);
-                   GraphNode gn2=new GraphNode(gn.getLabel()+"A"+i+"B"+ar.type(),gn.getTextLabel()+" #"+i+" "+ar.type(),tn2);
+                   //              GraphNode gn2=new GraphNode(gn.getLabel()+"A"+i+"B"+ar.type(),gn.getTextLabel()+" #"+i+" "+ar.type(),tn2);
+                   GraphNode gn2=new GraphNode(gn.getLabel()+"A"+i+"B"+ar.type(),dp.name()+" "+ar.type(),tn2);
+                   gn2.setOption(absrepoption);
                    GraphNode.Edge e=new GraphNode.Edge("abstract",gn2);
                    gn.addEdge(e);
                    if (!predtoabstractmap.containsKey(dp))
@@ -439,17 +481,19 @@ public class Termination {
            AbstractRepair ar=new AbstractRepair(tp, AbstractRepair.ADDTOSET, sd,sources);
            TermNode tn=new TermNode(ar);
            GraphNode gn=new GraphNode("AbstractAddSetRule"+i,tn);
+           gn.setOption(absrepoption);
            if (!predtoabstractmap.containsKey(tp))
                predtoabstractmap.put(tp,new HashSet());
            ((Set)predtoabstractmap.get(tp)).add(gn);
            abstractrepair.add(gn);
            abstractrepairadd.add(gn);
            abstractadd.put(sd,gn);
-           
+
            DNFPredicate tp2=new DNFPredicate(true,ip);
            AbstractRepair ar2=new AbstractRepair(tp2, AbstractRepair.REMOVEFROMSET, sd,sources);
            TermNode tn2=new TermNode(ar2);
            GraphNode gn2=new GraphNode("AbstractRemSetRule"+i,tn2);
+           gn2.setOption(absrepoption);
            if (!predtoabstractmap.containsKey(tp2))
                predtoabstractmap.put(tp2,new HashSet());
            ((Set)predtoabstractmap.get(tp2)).add(gn2);
@@ -465,22 +509,24 @@ public class Termination {
            VarExpr ve2=new VarExpr("DUMMY2");
 
            InclusionPredicate ip=new InclusionPredicate(ve2,new ImageSetExpr(vd1, rd));
-           
+
            DNFPredicate tp=new DNFPredicate(false,ip);
            AbstractRepair ar=new AbstractRepair(tp, AbstractRepair.ADDTORELATION, rd,sources);
            TermNode tn=new TermNode(ar);
            GraphNode gn=new GraphNode("AbstractAddRelRule"+i,tn);
+           gn.setOption(absrepoption);
            if (!predtoabstractmap.containsKey(tp))
                predtoabstractmap.put(tp,new HashSet());
            ((Set)predtoabstractmap.get(tp)).add(gn);
            abstractrepair.add(gn);
            abstractrepairadd.add(gn);
            abstractadd.put(rd,gn);
-           
+
            DNFPredicate tp2=new DNFPredicate(true,ip);
            AbstractRepair ar2=new AbstractRepair(tp2, AbstractRepair.REMOVEFROMRELATION, rd,sources);
            TermNode tn2=new TermNode(ar2);
            GraphNode gn2=new GraphNode("AbstractRemRelRule"+i,tn2);
+           gn2.setOption(absrepoption);
            if (!predtoabstractmap.containsKey(tp2))
                predtoabstractmap.put(tp2,new HashSet());
            ((Set)predtoabstractmap.get(tp2)).add(gn2);
@@ -503,6 +549,7 @@ public class Termination {
                MultUpdateNode mun=new MultUpdateNode(sn);
                TermNode tn2=new TermNode(mun);
                GraphNode gn2=new GraphNode("CompRem"+compensationcount,tn2);
+               gn2.setOption(compoption);
                UpdateNode un=new UpdateNode(r);
 
                if (j<r.numQuantifiers()) {
@@ -546,7 +593,7 @@ public class Termination {
                }
                if (!un.checkupdates()) /* Make sure we have a good update */
                    continue;
-               
+
                mun.addUpdate(un);
                GraphNode.Edge e=new GraphNode.Edge("abstract"+compensationcount,gn2);
                compensationcount++;
@@ -601,13 +648,14 @@ public class Termination {
        }
        if (possiblerules.size()==0)
            return;
-       
+
        /* Loop through different ways of falsifying each of these rules */
        int[] count=new int[possiblerules.size()];
        while(remains(count,possiblerules,true)) {
            MultUpdateNode mun=new MultUpdateNode(ar,MultUpdateNode.REMOVE);
            TermNode tn=new TermNode(mun);
            GraphNode gn2=new GraphNode("UpdateRem"+removefromcount,tn);
+           gn2.setOption(updateoption);
 
            boolean goodflag=true;
            for(int i=0;i<possiblerules.size();i++) {
@@ -657,8 +705,7 @@ public class Termination {
                mun.addUpdate(un);
            }
            if (goodflag) {
-               GraphNode.Edge e=new GraphNode.Edge("abstract"+removefromcount,gn2);
-               removefromcount++;
+               GraphNode.Edge e=new GraphNode.Edge("abstract"+(removefromcount++),gn2);
                gn.addEdge(e);
                updatenodes.add(gn2);
            }
@@ -704,7 +751,7 @@ public class Termination {
        int rightindex=1;
        if (inverted)
            leftindex=2;
-       else 
+       else
            rightindex=2;
 
        // construct set of possible rules
@@ -723,13 +770,14 @@ public class Termination {
        while(remains(count,possiblerules,false)) {
            MultUpdateNode mun=new MultUpdateNode(ar,MultUpdateNode.MODIFY);
            TermNode tn=new TermNode(mun);
-           GraphNode gn2=new GraphNode("UpdateMod"+removefromcount,tn);
+           GraphNode gn2=new GraphNode("UpdateMod"+modifycount,tn);
+           gn2.setOption(updateoption);
 
            boolean goodflag=true;
            for(int i=0;i<possiblerules.size();i++) {
                Rule r=(Rule)possiblerules.get(i);
                UpdateNode un=new UpdateNode(r);
-               
+
                int c=count[i];
                if (!processconjunction(un,r.getDNFGuardExpr().get(c),null)) {
                    goodflag=false;break;
@@ -767,10 +815,10 @@ public class Termination {
                    if (vd.isGlobal()) {
                        Updates up=new Updates(ri.getRightExpr(),rightindex,null);
                        un.addUpdate(up);
-                   } else if (!inverted) 
+                   } else if (!inverted)
                        goodflag=false;
                }
-                               
+
                if (!un.checkupdates()) {
                    goodflag=false;
                    break;
@@ -801,7 +849,7 @@ public class Termination {
                 ar.getDescriptor()==((SetInclusion)r.getInclusion()).getSet())||
                (r.getInclusion() instanceof RelationInclusion&&
                 ar.getDescriptor()==((RelationInclusion)r.getInclusion()).getRelation())) {
-               
+
                /* First solve for quantifiers */
                Vector bindings=new Vector();
                /* Construct bindings */
@@ -823,7 +871,7 @@ public class Termination {
                    if(inc instanceof SetInclusion) {
                        SetInclusion si=(SetInclusion)inc;
                        Expr e=si.elementexpr;
-                       
+
                        while(e instanceof CastExpr) {
                            CastExpr ce=(CastExpr)e;
                            if (ce.getType()!=si.getSet().getType())
@@ -861,7 +909,7 @@ public class Termination {
                        RelationInclusion ri=(RelationInclusion)inc;
 
                        Expr e=ri.getLeftExpr();
-                       
+
                        while(e instanceof CastExpr) {
                            CastExpr ce=(CastExpr)e;
                            if (ce.getType()!=ri.getRelation().getDomain().getType())
@@ -902,9 +950,9 @@ public class Termination {
                                un.addUpdate(up);
                            }
                        }
-                       
+
                        e=ri.getRightExpr();
-                       
+
                        while(e instanceof CastExpr) {
                            CastExpr ce=(CastExpr)e;
                            if (ce.getType()!=ri.getRelation().getRange().getType())
@@ -926,7 +974,7 @@ public class Termination {
                                if (set==null)
                                    continue;
                                ArrayAnalysis.AccessPath ap=arrayanalysis.getSet(set);
-                               
+
                                if (rap==ArrayAnalysis.AccessPath.NONE||
                                    !rap.equal(ap)||
                                    !constructarrayupdate(un, e, rap, 1))
@@ -952,6 +1000,7 @@ public class Termination {
                    MultUpdateNode mun=new MultUpdateNode(ar,MultUpdateNode.ADD);
                    TermNode tn=new TermNode(mun);
                    GraphNode gn2=new GraphNode("UpdateAdd"+addtocount,tn);
+                   gn2.setOption(updateoption);
 
                    if (debugmsg("Start processing quantifiers")&&
                        processquantifiers(gn2,un, r,setmapping)&&
@@ -982,7 +1031,7 @@ public class Termination {
        for (int i=ap.numFields()-1;i>=0;i--) {
            if (e==null)
                e=lexpr;
-           else 
+           else
                e=((DotExpr)e).getExpr();
 
            while (e instanceof CastExpr)
@@ -1035,11 +1084,11 @@ public class Termination {
                            return false;
                        e=ce.getExpr();
                    }
-                   
+
                    if ((e instanceof VarExpr)&&
                        (((VarExpr)e).getVar()==vd)) {
                        /* Can solve for v */
-                       if (!si.getSet().getType().isSubtypeOf(set.getType()))
+                       if (set==null||!si.getSet().getType().isSubtypeOf(set.getType()))
                            return false;
                        Binding binding=new Binding(vd,0);
                        bindings.add(binding);
@@ -1059,7 +1108,7 @@ public class Termination {
                            return false;
                        e=ce.getExpr();
                    }
-                   
+
                    if ((e instanceof VarExpr)&&
                        (((VarExpr)e).getVar()==vd)) {
                                /* Can solve for v */
@@ -1080,7 +1129,7 @@ public class Termination {
                            return false;
                        e=ce.getExpr();
                    }
-                   
+
                    if ((e instanceof VarExpr)&&
                        (((VarExpr)e).getVar()==vd)) {
                                /* Can solve for v */
@@ -1118,7 +1167,7 @@ public class Termination {
                        SetInclusion si=(SetInclusion)inc;
 
                        Expr e=si.elementexpr;
-                       
+
                        while(e instanceof CastExpr) {
                            CastExpr ce=(CastExpr)e;
                            if (ce.getType()!=td)
@@ -1143,7 +1192,7 @@ public class Termination {
 
 
                        Expr e=ri.getLeftExpr();
-                       
+
                        while(e instanceof CastExpr) {
                            CastExpr ce=(CastExpr)e;
                            if (ce.getType()!=ri.getRelation().getDomain().getType())
@@ -1163,7 +1212,7 @@ public class Termination {
 
 
                        e=ri.getRightExpr();
-                       
+
                        while(e instanceof CastExpr) {
                            CastExpr ce=(CastExpr)e;
                            if (ce.getType()!=ri.getRelation().getRange().getType())
@@ -1198,10 +1247,10 @@ public class Termination {
        }
        return goodupdate;
     }
-    
+
     /** Adds updates that add an item to the appropriate set or
      * relation quantified over by the model definition rule.. */
-    
+
     boolean processquantifiers(GraphNode gn,UpdateNode un, Rule r,Hashtable setmapping) {
        Inclusion inc=r.getInclusion();
        for(Iterator iterator=r.quantifiers();iterator.hasNext();) {
@@ -1284,7 +1333,7 @@ public class Termination {
            } else if (e instanceof TupleOfExpr) {
                Updates up=new Updates(e,de.getNegation());
                un.addUpdate(up);
-           } else if (e instanceof BooleanLiteralExpr) { 
+           } else if (e instanceof BooleanLiteralExpr) {
                boolean truth=((BooleanLiteralExpr)e).getValue();
                if (de.getNegation())
                    truth=!truth;