rewrite flags in DNF form
authorbdemsky <bdemsky>
Fri, 4 Aug 2006 23:00:52 +0000 (23:00 +0000)
committerbdemsky <bdemsky>
Fri, 4 Aug 2006 23:00:52 +0000 (23:00 +0000)
Robust/src/IR/Tree/DNFFlag.java [new file with mode: 0644]
Robust/src/IR/Tree/DNFFlagAtom.java [new file with mode: 0644]
Robust/src/IR/Tree/FlagExpressionNode.java
Robust/src/IR/Tree/FlagNode.java
Robust/src/IR/Tree/FlagOpNode.java

diff --git a/Robust/src/IR/Tree/DNFFlag.java b/Robust/src/IR/Tree/DNFFlag.java
new file mode 100644 (file)
index 0000000..67cdd3c
--- /dev/null
@@ -0,0 +1,88 @@
+package IR.Tree;
+import java.util.Vector;
+import IR.*;
+
+public class DNFFlag {
+    Vector conjunctions;
+    public DNFFlag(FlagNode flag) {
+       DNFFlagAtom dfa=new DNFFlagAtom(flag, false);
+       conjunctions=new Vector();
+       Vector conjunct=new Vector();
+       conjunct.add(dfa);
+       conjunctions.add(conjunct);
+    }
+    private DNFFlag() {
+       conjunctions=new Vector();
+    }
+
+    /** This method negates a DNFFlag expression. */
+
+    public DNFFlag not() {
+        DNFFlag notflag=null;
+        for (int i=0;i<conjunctions.size();i++) {
+            Vector conj=(Vector)conjunctions.get(i);
+            DNFFlag newflag=null;
+            for (int j=0;j<conj.size();j++) {
+                DNFFlagAtom dfa=(DNFFlagAtom) conj.get(j);
+               DNFFlagAtom negdfa=new DNFFlagAtom(dfa.flag,!dfa.negated);
+               DNFFlag tmp=new DNFFlag();
+               Vector v=new Vector();
+               tmp.conjunctions.add(v);
+               v.add(negdfa);
+
+                if (newflag==null)
+                   newflag=tmp;
+                else
+                   newflag=newflag.or(tmp);
+            }
+            if (notflag==null)
+               notflag=newflag;
+            else
+               notflag=notflag.and(newflag);
+        }
+        return notflag;
+    }
+
+    /** This method or's two DNFFlag expressions together. */
+    public DNFFlag or(DNFFlag dnf2) {
+       DNFFlag result=new DNFFlag();
+       for(int i=0;i<conjunctions.size();i++) {
+           Vector conjunct=(Vector)conjunctions.get(i);
+           Vector newvector=new Vector();
+           result.conjunctions.add(newvector);
+           for(int j=0;j<conjunct.size();j++) {
+               newvector.add(conjunct.get(j));
+           }
+       }
+
+       for(int i=0;i<dnf2.conjunctions.size();i++) {
+           Vector conjunct=(Vector)dnf2.conjunctions.get(i);
+           Vector newvector=new Vector();
+           result.conjunctions.add(newvector);
+           for(int j=0;j<conjunct.size();j++) {
+               newvector.add(conjunct.get(j));
+           }
+       }
+       return result;
+    }
+
+    /** This method and's two DNFFlag expressions together. */
+    public DNFFlag and(DNFFlag dnf2) {
+       DNFFlag result=new DNFFlag();
+       for(int i=0;i<conjunctions.size();i++) {
+           for(int i2=0;i2<dnf2.conjunctions.size();i2++) {
+               Vector conjunct=(Vector)conjunctions.get(i);
+               Vector conjunct2=(Vector)conjunctions.get(i2);
+               Vector newconjunct=new Vector();
+               result.conjunctions.add(newconjunct);
+               for(int j=0;j<conjunct.size();j++) {
+                   newconjunct.add(conjunct.get(j));
+               }
+               for(int j2=0;j2<conjunct2.size();j2++) {                
+                   newconjunct.add(conjunct2.get(j2));
+               }
+           }
+       }
+       return result;
+    }
+}
diff --git a/Robust/src/IR/Tree/DNFFlagAtom.java b/Robust/src/IR/Tree/DNFFlagAtom.java
new file mode 100644 (file)
index 0000000..e1fd3cc
--- /dev/null
@@ -0,0 +1,13 @@
+package IR.Tree;
+
+import IR.*;
+
+public class DNFFlagAtom {
+    final FlagNode flag;
+    final boolean negated;
+
+    public DNFFlagAtom(FlagNode flag, boolean negated) {
+       this.flag=flag;
+       this.negated=negated;
+    }
+}
index 6d27547ffcf89de3203892e0ef1c3c9f97464120..b00c8fe1eb855a7b07e3d5b067576658a4b43dca 100644 (file)
@@ -5,4 +5,8 @@ public class FlagExpressionNode extends TreeNode {
     public String printNode(int indentlevel) {
        return null;
     }
+
+    public DNFFlag getDNF() {
+       throw new Error();
+    }
 }
index 7b18b7f5eec1849860cc6651a856408588345614..68f2b2b46606abd667d4bc4b3b2bc7fd6dde3ca6 100644 (file)
@@ -1,4 +1,5 @@
 package IR.Tree;
+import java.util.Vector;
 
 import IR.*;
 
@@ -29,4 +30,8 @@ public class FlagNode extends FlagExpressionNode {
     public String printNode(int indent) {
        return name;
     }
+
+    public DNFFlag getDNF() {
+       return new DNFFlag(this);
+    }
 }
index 5ce57484606484ab1cc92b3be0afaf3e71f3795c..558ebabba708788cc968acf04d1aec21e03b95b2 100644 (file)
@@ -40,4 +40,17 @@ public class FlagOpNode extends FlagExpressionNode {
     public int kind() {
        return Kind.FlagOpNode;
     }
+
+    public DNFFlag getDNF() {
+       DNFFlag leftflag=left.getDNF();
+       DNFFlag rightflag=right!=null?right.getDNF():null;
+
+       if (op.getOp()==Operation.LOGIC_NOT) {
+           return leftflag.not();
+       } else if (op.getOp()==Operation.LOGIC_OR) {
+           return leftflag.or(rightflag);
+       } else if (op.getOp()==Operation.LOGIC_AND) {
+           return leftflag.and(rightflag);
+       } else throw new Error();
+    }
 }