branches pretty much work
[org.ibex.classgen.git] / src / org / ibex / classgen / JSSA.java
index 16af019..2fc521a 100644 (file)
@@ -13,32 +13,32 @@ public class JSSA extends MethodGen implements CGConst {
     
     public JSSA(Type.Class c, DataInput in, ConstantPool cp) throws IOException {
         super(c, in, cp);
-        locals = new Phi[size()][maxLocals];
-        stacks = new Phi[size()][maxStack];
-        sps    = new int [size()];
-        sps[0] = 0;
-        int n=0;
-        locals[0] = new Phi[maxLocals];
-        if (!isStatic())
-            locals[0][n++] = phi(new Argument("this",method.getDeclaringClass()));
-        for(int i=0;i<this.method.getNumArgs(); i++)
-            locals[0][n++] = phi(new Argument("arg"+i, this.method.getArgType(i)));
+        sp = 0;
+        stacks = new Phi[size()][];
+        locals = new Phi[size()][];
+        for(int i=0; i<size(); i++) {
+            int n = 0;
+            locals[i] = new Phi[maxLocals];
+            for(int j=0; j<locals[i].length; j++) locals[i][j] = new Phi();
+            if (i==0) {
+                if (!isStatic()) locals[i][n++].merge(new Argument("this",method.getDeclaringClass()));
+                for(int j=0;j<this.method.getNumArgs(); j++) locals[i][n++].merge(new Argument("arg"+j,this.method.getArgType(j)));
+            }
+        }
+
+        stack = new Phi[maxStack];
+        branchTo(0);
         for(pc=0; pc<size(); pc++) {
             int    op  = get(pc);
             Object arg = getArg(pc);
             try {
-                if (pc<size()-1) {
-                    locals[pc+1] = new Phi[maxLocals];
-                    for(int j=0; j<maxLocals; j++) locals[pc+1][j] = locals[pc][j];
-                    sps[pc+1] = sps[pc];
-                    stacks[pc+1] = new Phi[maxStack];
-                    for(int j=0; j<sps[pc]; j++) stacks[pc+1][j] = stacks[pc][j];
-                }
                 Object o = addOp(op, arg);
                 if (o != null) {
                     ops[numOps] = o;
                     ofs[numOps++] = pc;
                 }
+                if (o!=null && o instanceof Branch) ((Branch)o).branchTo();
+                if (o==null || (!(o instanceof Branch))) branchTo(pc+1);
             } catch(RuntimeException e) {
                 System.err.println("Had a problem at PC: " + pc + " of " + method);
                 e.printStackTrace();
@@ -46,6 +46,18 @@ public class JSSA extends MethodGen implements CGConst {
             }
         }
     }
+
+    public void branchTo(int newPC) {
+        System.out.println("!!!branchTo("+newPC+")!!!");
+        if (stacks[newPC] == null) {
+            stacks[newPC] = new Phi[sp];
+            for(int i=0; i<sp; i++) stacks[newPC][i] = new Phi();
+        }
+        if (stacks[newPC].length != sp)
+            throw new IllegalArgumentException("stack depth disagreement: " + sp + " " + stacks[newPC].length);
+        for(int i=0; i<stacks[newPC].length; i++) stacks[newPC][i].merge(stack[i]);
+        for(int i=0; i<maxLocals; i++) locals[newPC][i].merge(locals[pc][i]);
+    }
     
     private Object[] ops = new Object[65535];
     private int[] ofs = new int[65535];
@@ -58,28 +70,21 @@ public class JSSA extends MethodGen implements CGConst {
     
     /** this models the JVM stack; it is only used for unwinding stack-ops into an SSA-tree, then thrown away */
     private final Phi[][] stacks;
+    private final Phi[] stack;
 
     /** JVM stack pointer */
-    private final int sps[];
     private int pc = 0;
+    private int sp = 0;
 
-    private Expr push(Expr e) {
-        // FIXME: need to verify that stacks are same size at target of branch
-        if (sps[pc+1] == maxStack) {
-            for(int i=0;i<maxStack;i++) System.err.println("Stack " + i + ": " + stacks[pc][i]);
-            throw new IllegalStateException("stack overflow (" + maxStack + ")");
-        }
+    private void push(Expr e) {
         if (e.getType() == Type.VOID) throw new IllegalArgumentException("can't push a void");
-        stacks[pc+1][sps[pc+1]++].merge(e);
-        return stacks[pc+1][sps[pc+1]++];
+        if (stack[sp] == null) stack[sp] = new Phi();
+        stack[sp++].merge(e);
     }
     private Expr pop() {
-        // FIXME: need to verify that stacks are same size at target of branch
-        if (pc == sps.length-1) { /* FIXME? */ return stacks[pc][sps[pc]-1]; }
-        if (sps[pc+1] == 0) throw new IllegalStateException("stack underflow");
-        sps[pc+1]--;
-        Expr ret = stacks[pc+1][sps[pc+1]];
-        stacks[pc+1][sps[pc+1]] = null;
+        Expr ret = stack[sp-1];
+        stack[sp-1] = null;
+        sp--;
         return ret;
     }
     
@@ -126,7 +131,7 @@ public class JSSA extends MethodGen implements CGConst {
          *  redundant information that could possibly "disagree" with itself -- this happened a LOT in Soot) */
         public abstract Type getType();
         public String _toString() { return super.toString(); }
-        public String toString() {
+        public String toString() { return _toString(); } /*
             String s = (String)bindingMap.get(this);
             if (s != null) return s;
             String prefix;
@@ -138,7 +143,7 @@ public class JSSA extends MethodGen implements CGConst {
             s = prefix + (nextVar++);
             bindingMap.put(this,s);
             return "(" + s + " = " + _toString() + ")";
-        }
+            }*/
     }
 
     /**
@@ -156,6 +161,7 @@ public class JSSA extends MethodGen implements CGConst {
     public class Phi extends Expr {
         Expr[] inputs;
         public Phi(Expr[] inputs) { this.inputs = inputs; }
+        public Phi() { this.inputs = new Expr[0]; }
         public Phi(Expr e1) {
             this.inputs = new Expr[1];
             inputs[0] = e1;
@@ -166,6 +172,8 @@ public class JSSA extends MethodGen implements CGConst {
             inputs[1] = e2;
         }
         public void merge(Expr e) {
+            if (e==this) return;
+            for(int i=0; i<inputs.length; i++) if (inputs[i]==e) return;
             Expr[] newinputs = new Expr[inputs.length + 1];
             System.arraycopy(inputs, 0, newinputs, 0, inputs.length);
             newinputs[newinputs.length-1] = e;
@@ -174,22 +182,31 @@ public class JSSA extends MethodGen implements CGConst {
         public String toString() {
             if (inputs.length == 1) return inputs[0].toString();
             StringBuffer ret = new StringBuffer();
-            ret.append("{{ ");
+            int count = 0;
             for(int i=0; i<inputs.length; i++) {
-                ret.append(inputs[i].toString());
+                String s = inputs[i].toString().trim();
+                if (s.length() == 0) continue;
+                count++;
+                ret.append(s);
                 ret.append(" ");
             }
-            ret.append("}}");
-            return ret.toString();
+            if (count == 0) return "";
+            if (count == 1) return ret.toString().trim();
+            return "{{ " + ret.toString() + "}}";
+            //return "{"+ret+"}";
         }
         public Type getType() {
+            if (inputs.length == 0) return null;
             // sanity check
             Type t = inputs[0].getType();
 
             // FIXME: actually this should check type-unifiability... fe, the "type of null" unifies with any Type.Ref
-            for(int i=1; i<inputs.length; i++)
+            for(int i=1; i<inputs.length; i++) {
+                if (t==null) { t = inputs[i].getType(); continue; }
+                if (inputs[i].getType() == null) continue;
                 if (inputs[i].getType() != t)
-                    throw new Error("Phi node with disagreeing types!  Crisis!");
+                    throw new Error("Phi node with disagreeing types: " + t + " " + inputs[i].getType() +"\n  Crisis!");
+            }
             return t;
         }
     }
@@ -277,7 +294,8 @@ public class JSSA extends MethodGen implements CGConst {
     public class BinMath extends BinExpr {
         public BinMath(Expr e1, Expr e2, String show) {
             super(e2, e1, show); 
-            if (e1.getType() != e2.getType()) throw new IllegalArgumentException("types disagree");
+            if (e1.getType() != null && e2.getType() != null && e1.getType() != e2.getType())
+                throw new IllegalArgumentException("types disagree");
         }
         public Type getType() { return e1.getType(); }
     }
@@ -330,7 +348,24 @@ public class JSSA extends MethodGen implements CGConst {
         public Type getType() { return Type.BOOLEAN; }
     }
 
-    public class Throw extends Op {
+    public class Branch extends Op {
+        Expr destination = null;
+        public Branch(Expr destination) { this.destination = destination; }
+        public Branch(MethodGen.Switch s) { /* FIXME */ }
+        public Branch() { }
+        public void branchTo() { if (destination != null) branchTo(destination); }
+        private void branchTo(Expr e) {
+            if (e instanceof Phi) {
+                Phi phi = (Phi)e;
+                for(int i=0; i<phi.inputs.length; i++) branchTo(phi.inputs[i]);
+            } else if (e instanceof Label) {
+                JSSA.this.branchTo(((Label)e).pc);
+            } else {
+                throw new IllegalArgumentException("can't branch to a " + e.getClass());
+            }
+        }
+    }
+    public class Throw extends Branch {
         public final Expr e;
         public Throw(Expr e) {
             if (!e.getType().isRef()) throw new IllegalArgumentException("can't throw a non ref");
@@ -338,24 +373,44 @@ public class JSSA extends MethodGen implements CGConst {
             this.e = e; 
         }
     }
-
-    public class Branch extends Op {
-        public Branch(Expr condition, Object destination) { }
-        public Branch(Label destination) { }
-        public Branch(MethodGen.Switch s) { }
-        public Branch() { }
+    public class Return extends Branch {
+        final Expr e;
+        public Return() { this(VOID_EXPR); }
+        public Return(Expr e) {
+            this.e = e; 
+            if (Type.unify(method.getReturnType(),e.getType()) != method.getReturnType())
+               throw new IllegalArgumentException("type mismatch");
+        }
+        public String toString() { return e.getType() == Type.VOID ? "return" : ("return "+e.toString()); }
+    }
+    public class Goto extends Branch {
+        public Goto(Expr destination) { super(destination); }
+        public String toString() { return "goto " + destination; }
+    }
+    public class RET extends Branch {
+        public RET(Expr destination) { super(destination); }
+        public String toString() { return "retsub [" + destination + "]"; }
+    }
+    public class JSR extends Branch {
+        public JSR(Expr destination) { super(destination); }
+        public String toString() { return "callsub " + destination; }
+    }
+    public class If extends Branch {
+        Expr condition = null;
+        public If(Expr condition, Expr destination) { super(destination); this.condition = condition; }
+        public String toString() { return "if (" + condition + ") goto " + destination; }
+        public void branchTo() {
+            if (condition != null) JSSA.this.branchTo(pc+1);
+            super.branchTo();
+        }
     }
-    public class Goto extends Branch { }
-    public class RET extends Branch { }
-    public class JSR extends Branch { public JSR(Label l) { super(l); } }
-    public class If extends Branch { }
 
     /** represents a "returnaddr" pushed onto the stack */
     public class Label extends Expr {
-        public final Op op;
+        public int pc;
+        public Label(int i) { this.pc = i; }
         public Type getType() { throw new Error("attempted to call getType() on a Label"); }
-        public Label(Op op) { this.op = op; }
-        public Label(int i) { this.op = null; /* FIXME */ }
+        public String toString() { return "<<label " + pc + ">>"; }
     }
 
     public class New extends Expr {
@@ -385,17 +440,6 @@ public class JSSA extends MethodGen implements CGConst {
         }
     }
     
-    public class Return extends Op {
-        final Expr e;
-        public Return() { this(VOID_EXPR); }
-        public Return(Expr e) {
-            this.e = e; 
-            if (Type.unify(method.getReturnType(),e.getType()) != method.getReturnType())
-               throw new IllegalArgumentException("type mismatch");
-        }
-        public String toString() { return e.getType() == Type.VOID ? "return" : ("return "+e.toString()); }
-    }
-
     /** GETFIELD and GETSTATIC */
     public class Get extends Expr {
         final Type.Class.Field f;
@@ -534,6 +578,8 @@ public class JSSA extends MethodGen implements CGConst {
             i1 = p.i1;
             i2 = p.i2;
         }
+        if (arg instanceof Number) i1 = ((Integer)arg).intValue();
+        Label label = (arg instanceof Label) ? (Label)arg : null;
         switch(op) {
 
             case NOP: return null;
@@ -565,8 +611,8 @@ public class JSSA extends MethodGen implements CGConst {
                 locals[pc+1][3].merge(pop()); return null;
             case POP:                                                                  pop();                    return null;
             case POP2:                                                                 pop(); pop();             return null;
-            case DUP:   push(stacks[pc][sps[pc]-1]);                  return null;
-            case DUP2:  push(stacks[pc][sps[pc]-2]); push(stacks[pc][sps[pc]-1]); return null;
+            case DUP:   push(stack[sp-1]);                  return null;
+            case DUP2:  push(stack[sp-2]); push(stack[sp-1]); return null;
 
                 // Conversions //////////////////////////////////////////////////////////////////////////////
 
@@ -600,27 +646,27 @@ public class JSSA extends MethodGen implements CGConst {
 
                 // Control and branching //////////////////////////////////////////////////////////////////////////////
 
-            case IFNULL:                                return new Branch(new Eq(pop(), new Constant(null)), new Label(i1));
-            case IFNONNULL:                             return new Branch(new Not(new Eq(pop(),new Constant(null))),new Label(i1));
-            case IFEQ:                                  return new Branch(    new Eq(new Constant(0), pop()),  arg);
-            case IFNE:                                  return new Branch(new Not(new Eq(new Constant(0), pop())), arg);
-            case IFLT:                                  return new Branch(    new Lt(new Constant(0), pop()),  arg);
-            case IFGE:                                  return new Branch(new Not(new Lt(new Constant(0), pop())), arg);
-            case IFGT:                                  return new Branch(    new Gt(new Constant(0), pop()),  arg);
-            case IFLE:                                  return new Branch(new Not(new Gt(new Constant(0), pop())), arg);
-            case IF_ICMPEQ:                             return new Branch(    new Eq(pop(), pop()),  arg);
-            case IF_ICMPNE:                             return new Branch(new Not(new Eq(pop(), pop())), arg);
-            case IF_ICMPLT:                             return new Branch(    new Lt(pop(), pop()),  arg);
-            case IF_ICMPGE:                             return new Branch(new Not(new Lt(pop(), pop())), arg);
-            case IF_ICMPGT:                             return new Branch(    new Gt(pop(), pop()),  arg);
-            case IF_ICMPLE:                             return new Branch(new Not(new Gt(pop(), pop())), arg);
-            case IF_ACMPEQ:                             return new Branch(    new Eq(pop(), pop()),  arg);
-            case IF_ACMPNE:                             return new Branch(new Not(new Eq(pop(), pop())), arg);
-            case ATHROW:                                return new Throw(pop());
-            case GOTO:                                  return new Branch(new Label(i1));
-            case JSR:                                   return new JSR(new Label(i1));
-            case RET:                                   return new RET();
-            case RETURN:                                return new Return();
+            case IFNULL:                                return new If(new Eq(pop(), new Constant(null)), new Label(i1));
+            case IFNONNULL:                             return new If(new Not(new Eq(pop(),new Constant(null))),new Label(i1));
+            case IFEQ:                                  return new If(    new Eq(new Constant(0), pop()), new Label(i1));
+            case IFNE:                                  return new If(new Not(new Eq(new Constant(0), pop())), new Label(i1));
+            case IFLT:                                  return new If(    new Lt(new Constant(0), pop()), new Label(i1));
+            case IFGE:                                  return new If(new Not(new Lt(new Constant(0), pop())), new Label(i1));
+            case IFGT:                                  return new If(    new Gt(new Constant(0), pop()), new Label(i1));
+            case IFLE:                                  return new If(new Not(new Gt(new Constant(0), pop())), new Label(i1));
+            case IF_ICMPEQ:                             return new If(    new Eq(pop(), pop()), new Label(i1));
+            case IF_ICMPNE:                             return new If(new Not(new Eq(pop(), pop())), new Label(i1));
+            case IF_ICMPLT:                             return new If(    new Lt(pop(), pop()), new Label(i1));
+            case IF_ICMPGE:                             return new If(new Not(new Lt(pop(), pop())), new Label(i1));
+            case IF_ICMPGT:                             return new If(    new Gt(pop(), pop()), new Label(i1));
+            case IF_ICMPLE:                             return new If(new Not(new Gt(pop(), pop())), new Label(i1));
+            case IF_ACMPEQ:                             return new If(    new Eq(pop(), pop()), new Label(i1));
+            case IF_ACMPNE:                             return new If(new Not(new Eq(pop(), pop())), new Label(i1));
+            case ATHROW:                                                       return new Throw(pop());
+            case GOTO:                                                         return new Goto(locals[pc][i1]);
+            case JSR:                                   push(new Label(pc));   return new JSR(new Label(i1));
+            case RET:                                                          return new RET(pop());
+            case RETURN:                                                       return new Return();
             case IRETURN: case LRETURN: case FRETURN: case DRETURN: case ARETURN:
                 return new Return(pop());