Some embarrassing erros. The DNF conversion for negation wasn't
[repair.git] / Repair / RepairCompiler / MCC / IR / OpExpr.java
index 957a35be2fc36d91229f885d19e2d7c2c8c1011a..e9cde37d174b730ca782e0f1a07cc603ac505afc 100755 (executable)
@@ -36,15 +36,37 @@ public class OpExpr extends Expr {
        } else return llower;
     }
 
+
     public boolean isSafe() {
        if (right==null)
            return left.isSafe();
        return left.isSafe()&&right.isSafe();
     }
 
+    public boolean isInvariant(Set vars) {
+       return left.isInvariant(vars)&&((right==null)||right.isInvariant(vars));
+    }
+
+    public Set findInvariants(Set vars) {
+       if (isInt(this)) {
+           /* Don't hoist ints */
+           return new HashSet();
+       } else if (isInvariant(vars)) {
+           Set s=new HashSet();
+           s.add(this);
+           return s;
+       } else {
+           Set ls=left.findInvariants(vars);
+           if (right!=null)
+               ls.addAll(right.findInvariants(vars));
+           return ls;
+       }
+    }
+
     public Set getfunctions() {
        Set leftfunctions=left.getfunctions();
-       Set rightfunctions=right.getfunctions();
+       Set rightfunctions=null;
+       if (right!=null) rightfunctions=right.getfunctions();
        if (leftfunctions!=null&&rightfunctions!=null) {
            HashSet functions=new HashSet();
            functions.addAll(leftfunctions);
@@ -56,12 +78,6 @@ public class OpExpr extends Expr {
        return rightfunctions;
     }
 
-    public void findmatch(Descriptor d, Set  s) {
-       left.findmatch(d,s);
-       if (right!=null)
-           right.findmatch(d,s);
-    }
-
     public static boolean isInt(Expr e) {
        if (e==null)
            return false;
@@ -175,8 +191,10 @@ public class OpExpr extends Expr {
        if (opcode==Opcode.RND)
            return "Round("+left.name()+")";
        String name=left.name()+opcode.toString();
-       if (right!=null)
+       if (right!=null) {
            name+=right.name();
+            name="("+name+")";
+        }
        return name;
     }
 
@@ -184,9 +202,6 @@ public class OpExpr extends Expr {
        return opcode;
     }
 
-
-
-
     public boolean equals(Map remap, Expr e) {
        if (e==null||!(e instanceof OpExpr))
            return false;
@@ -220,6 +235,12 @@ public class OpExpr extends Expr {
        return left.usesDescriptor(d)||(right!=null&&right.usesDescriptor(d));
     }
 
+    public void findmatch(Descriptor d, Set  s) {
+       left.findmatch(d,s);
+       if (right!=null)
+           right.findmatch(d,s);
+    }
+
     public Set useDescriptor(Descriptor d) {
        HashSet newset=new HashSet();
        newset.addAll(left.useDescriptor(d));
@@ -227,7 +248,7 @@ public class OpExpr extends Expr {
            newset.addAll(right.useDescriptor(d));
        return newset;
     }
-    
+
     public int[] getRepairs(boolean negated, Termination t) {
        if (left instanceof RelationExpr)
            return new int[] {AbstractRepair.MODIFYRELATION};
@@ -265,7 +286,7 @@ public class OpExpr extends Expr {
                                          AbstractRepair.REMOVEFROMRELATION};
                    }
                } else if (op==Opcode.GE||op==Opcode.GT) {
-                   return new int[]{AbstractRepair.ADDTORELATION}; 
+                   return new int[]{AbstractRepair.ADDTORELATION};
                } else if (op==Opcode.LE||op==Opcode.LT) {
                    if ((op==Opcode.LT&&maxsize!=-1&&maxsize<size)||(op==Opcode.LE&&maxsize!=-1&&maxsize<=size))
                        return new int[0];
@@ -278,7 +299,7 @@ public class OpExpr extends Expr {
            } else {
                if (op==Opcode.EQ) {
                    if (size==0)
-                       return new int[] {AbstractRepair.REMOVEFROMSET};                        
+                       return new int[] {AbstractRepair.REMOVEFROMSET};
                    else {
                        if (maxsize<=size&&maxsize!=-1)
                            return new int[] {AbstractRepair.ADDTOSET};
@@ -286,7 +307,7 @@ public class OpExpr extends Expr {
                                              AbstractRepair.REMOVEFROMSET};
                    }
                } else if (op==Opcode.GE||op==Opcode.GT) {
-                   return new int[] {AbstractRepair.ADDTOSET}; 
+                   return new int[] {AbstractRepair.ADDTOSET};
                } else if (op==Opcode.LE||op==Opcode.LT) {
                    if ((op==Opcode.LT&&maxsize<size&&maxsize!=-1)||(op==Opcode.LE&&maxsize<=size&&maxsize!=-1))
                        return new int[0];
@@ -300,7 +321,7 @@ public class OpExpr extends Expr {
        }
        throw new Error("BAD");
     }
-    
+
     public Descriptor getDescriptor() {
        return left.getDescriptor();
     }
@@ -319,17 +340,26 @@ public class OpExpr extends Expr {
 
     public Set getRequiredDescriptors() {
         Set v = left.getRequiredDescriptors();
-     
+
         if (right != null) {
             v.addAll(right.getRequiredDescriptors());
         }
 
         return v;
-    }   
+    }
 
     public void generate(CodeWriter writer, VarDescriptor dest) {
         VarDescriptor ld = VarDescriptor.makeNew("leftop");
-        left.generate(writer, ld);        
+       /* Check for loop invariant hoisting. */
+       if (writer.getInvariantValue()!=null&&
+           writer.getInvariantValue().isInvariant(this)) {
+           writer.addDeclaration("int",dest.getSafeSymbol());
+           writer.outputline(dest.getSafeSymbol()+"="+writer.getInvariantValue().getValue(this).getSafeSymbol()+";");
+           writer.outputline("maybe="+writer.getInvariantValue().getMaybe(this).getSafeSymbol()+";");
+           return;
+       }
+
+       left.generate(writer, ld);
         VarDescriptor rd = null;
        VarDescriptor lm=VarDescriptor.makeNew("lm");
        VarDescriptor rm=VarDescriptor.makeNew("rm");
@@ -337,8 +367,9 @@ public class OpExpr extends Expr {
         if (right != null) {
            if ((opcode==Opcode.OR)||
                (opcode==Opcode.AND)) {
-               writer.outputline("int "+lm.getSafeSymbol()+"=maybe;");
-               writer.outputline("int maybe=0;");
+               writer.addDeclaration("int",lm.getSafeSymbol());
+               writer.outputline(lm.getSafeSymbol()+"=maybe;");
+               writer.outputline("maybe=0;");
            }
 
             rd = VarDescriptor.makeNew("rightop");
@@ -347,42 +378,50 @@ public class OpExpr extends Expr {
 
         String code;
        if (opcode == Opcode.RND) {
-           writer.outputline("int " +dest.getSafeSymbol() + " = (" +
+           writer.addDeclaration("int",dest.getSafeSymbol());
+           writer.outputline(dest.getSafeSymbol() + " = (" +
                              ld.getSafeSymbol() + ">>3)<<3; ");
            writer.outputline("if ("+ld.getSafeSymbol()+" % 8) "+dest.getSafeSymbol()+"+=8;");
        } else if (opcode == Opcode.NOP) {
-           writer.outputline("int " +dest.getSafeSymbol() + " = " +
+           writer.addDeclaration("int", dest.getSafeSymbol());
+           writer.outputline(dest.getSafeSymbol() + " = " +
                              ld.getSafeSymbol() +"; ");
         } else if (opcode == Opcode.AND) {
-           writer.outputline("int "+rm.getSafeSymbol()+"=maybe;");
+           writer.addDeclaration("int",rm.getSafeSymbol());
+           writer.outputline(rm.getSafeSymbol()+"=maybe;");
            writer.outputline("maybe = (" + ld.getSafeSymbol() + " && " + rm.getSafeSymbol() + ") || (" + rd.getSafeSymbol() + " && " + lm.getSafeSymbol() + ") || (" + lm.getSafeSymbol() + " && " + rm.getSafeSymbol() + ");");
+           writer.addDeclaration("int",dest.getSafeSymbol());
            writer.outputline(dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " && " + rd.getSafeSymbol() + ";");
        } else if (opcode == Opcode.OR) {
-           writer.outputline("int "+rm.getSafeSymbol()+"=maybe;");
+           writer.addDeclaration("int",rm.getSafeSymbol());
+           writer.outputline(rm.getSafeSymbol()+"=maybe;");
            writer.outputline("maybe = (!" + ld.getSafeSymbol() + " && " + rm.getSafeSymbol() + ") || (!" + rd.getSafeSymbol() +
                              " && " + lm.getSafeSymbol() + ") || (" + lm.getSafeSymbol() + " && " + rm.getSafeSymbol() + ");");
-           writer.outputline(dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " || " + rd.getSafeSymbol() +
-                             ";");
+           writer.addDeclaration("int",dest.getSafeSymbol());
+           writer.outputline(dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " || " + rd.getSafeSymbol() + ";");
        } else if (opcode != Opcode.NOT) { /* two operands */
             assert rd != null;
-            writer.outputline("int " + dest.getSafeSymbol() + " = " + 
-                              ld.getSafeSymbol() + " " + opcode.toString() + " " + rd.getSafeSymbol() + ";");
+           writer.addDeclaration("int", dest.getSafeSymbol());
+           writer.outputline(dest.getSafeSymbol() + " = " +
+                             ld.getSafeSymbol() + " " + opcode.toString() + " " + rd.getSafeSymbol() + ";");
         } else if (opcode == Opcode.NOT) {
-            writer.outputline("int " + dest.getSafeSymbol() + " = !" + ld.getSafeSymbol() + ";");
+            writer.addDeclaration("int", dest.getSafeSymbol());
+            writer.outputline(dest.getSafeSymbol() + " = !" + ld.getSafeSymbol() + ";");
         }
     }
 
     public void prettyPrint(PrettyPrinter pp) {
         pp.output("(");
         if (opcode == Opcode.NOT) {
-           pp.output("!");
+           pp.output("!(");
             left.prettyPrint(pp);
+           pp.output(")");
        } else if (opcode == Opcode.NOP) {
             left.prettyPrint(pp);
        } else if (opcode == Opcode.RND) {
            pp.output("RND ");
             left.prettyPrint(pp);
-        } else {           
+        } else {
             left.prettyPrint(pp);
             pp.output(" " + opcode.toString() + " ");
             assert right != null;
@@ -403,23 +442,6 @@ public class OpExpr extends Expr {
 
         boolean ok = true;
 
-        // #ATTN#: if we want node.next != literal(0) to represent a null check than we need to allow ptr arithmetic
-        // either that or we use a isvalid clause to check for null
-
-        /*
-        if (lt != ReservedTypeDescriptor.INT) {
-            sa.getErrorReporter().report(null, "Left hand side of expression is of type '" + lt.getSymbol() + "' but must be type 'int'");
-            ok = false;
-        }
-
-        if (right != null) {
-            if (rt != ReservedTypeDescriptor.INT) {
-                sa.getErrorReporter().report(null, "Right hand side of expression is of type '" + rt.getSymbol() + "' but must be type 'int'");
-                ok = false;
-            }
-        }
-        */
-
         if (!ok) {
             return null;
         }