JSSA.Seq to for evaluation of an expr
[org.ibex.classgen.git] / src / org / ibex / classgen / JSSA.java
index b8e57ca..d43008b 100644 (file)
@@ -15,15 +15,24 @@ public class JSSA extends MethodGen implements CGConst {
         super(c, in, cp);
         local = new Expr[maxLocals];
         stack = new Expr[maxStack];
-        for(int i=0; i<this.method.getNumArgs(); i++)
-            local[i] = new Argument("arg"+i, this.method.getArgType(i));
+        int n=0;
+        if (!isStatic())
+            local[n++] = new Argument("this",method.getDeclaringClass());
+        for(int i=0;i<this.method.getNumArgs(); i++)
+            local[n++] = new Argument("arg"+i, this.method.getArgType(i));
         for(int i=0; i<size(); i++) {
             int    op  = get(i);
             Object arg = getArg(i);
-            Object o = addOp(op, arg);
-            if (o != null) {
-                ops[numOps] = o;
-                ofs[numOps++] = i;
+            try {
+                Object o = addOp(op, arg);
+                if (o != null) {
+                    ops[numOps] = o;
+                    ofs[numOps++] = i;
+                }
+            } catch(RuntimeException e) {
+                System.err.println("Had a problem at PC: " + i + " of " + method);
+                e.printStackTrace();
+                throw new IOException("invalid class file");
             }
         }
     }
@@ -73,12 +82,31 @@ public class JSSA extends MethodGen implements CGConst {
     /** JVM stack pointer */
     private int sp = 0;
     
-    private Expr push(Expr e) { return stack[sp++] = e; }
-    private Expr pop()        { return stack[--sp]; }
+    private Expr push(Expr e) {
+        if(sp == stack.length) {
+            for(int i=0;i<stack.length;i++) System.err.println("Stack " + i + ": " + stack[i]);
+            throw new IllegalStateException("stack overflow (" + stack.length + ")");
+        }
+        if(e.getType() == Type.VOID) throw new IllegalArgumentException("can't push a void");
+        return stack[sp++] = e;
+    }
+    private Expr pop() {
+        if(sp == 0) throw new IllegalStateException("stack underflow");
+        return stack[--sp];
+    }
+    
+    private Op seqPush(Expr e) {
+        push(e);
+        return new Seq(e);
+    }
 
 
     // SSA-node classes /////////////////////////////////////////////////////////////////////////////////////////
 
+    public final Expr VOID_EXPR = new Expr() {
+        public Type getType() { return Type.VOID; }
+    };
+    
     /** an purely imperative operation which does not generate data */
     public abstract class Op {
         //public abstract Op[] predecessors();  // not implemented yet
@@ -91,7 +119,16 @@ public class JSSA extends MethodGen implements CGConst {
             return name;
         }
     }
-
+    
+    /** A sequence point. expr is evaluated for side effects at this point, this does not generate data 
+        Expressions that haven't been evaluated with Seq are evaluated when they are first encountered
+      */
+    public class Seq extends Op {
+        private final Expr expr;
+        public String toString() { return expr.toString(); }
+        public Seq(Expr expr) { this.expr = expr; }
+    }
+    
     /** an operation which generates data */
     public abstract class Expr extends Op {
         //public abstract Expr[] contributors();  // not implemented yet
@@ -132,75 +169,134 @@ public class JSSA extends MethodGen implements CGConst {
         public String toString() { return name; }
         public Type getType() { return t; }
     }
-
+    
+    // Unary Operations
+    public class Not extends Expr {
+        public final Expr e;
+        public Not(Expr e) {
+            if(e.getType() != Type.BOOLEAN) throw new IllegalArgumentException("not needs a boolean expression");
+            this.e = e;
+        }
+        public Type getType() { return Type.BOOLEAN; }
+        public String toString() { return "!(" + e + ")"; }
+    }
+    
+    public class Neg extends Expr {
+        public final Expr e;
+        public Neg(Expr e) {
+            if(!e.getType().isPrimitive()) throw new IllegalArgumentException("can only negate a primitive");
+            this.e = e;
+        }
+        public Type getType() { return e.getType(); }
+        public String toString() { return "- (" + e + ")"; }
+    }
+    
     // Binary Operations //////////////////////////////////////////////////////////////////////////////
 
     public abstract class BinExpr extends Expr {
         public final Expr e1;
         public final Expr e2;
-        public BinExpr(Expr e1, Expr e2) { this.e1 = e1; this.e2 = e2; }
+        private final String show;
+        public BinExpr(Expr e1, Expr e2, String show) { this.e1 = e1; this.e2 = e2; this.show = show; }
         public String toString() {
-            return name() + "("+e1+", "+e2+")";
+            // FEATURE: should we be doing some precedence stuff here? probably no worth it for debugging output
+            return "(" + e1 + show + e2 + ")";
         }
     }
 
     public class Comparison extends BinExpr {
-        public Comparison(Expr e1, Expr e2) { super(e1, e2); }
+        public Comparison(Expr e1, Expr e2, String show) { super(e1, e2, show); }
         public Type getType() { return Type.BOOLEAN; }
     }
-    public class Gt extends Comparison { public Gt(Expr e1, Expr e2) { super(e1, e2); } }
-    public class Lt extends Comparison { public Lt(Expr e1, Expr e2) { super(e1, e2); } }
-    public class Eq extends Comparison { public Eq(Expr e1, Expr e2) { super(e1, e2); } }
-    public class Not extends Expr {
-        public final Expr e;
-        public Not(Expr e) { this.e = e; }
-        public Type getType() { return Type.BOOLEAN; }
+
+    public class Eq extends Comparison {
+        public Eq(Expr e1, Expr e2) {
+            super(e1, e2, "=="); 
+            if(e1.getType().isPrimitive() != e2.getType().isPrimitive())
+                throw new IllegalArgumentException("type mismatch");
+            if(e1.getType().isPrimitive() && e1.getType() != e2.getType())
+                throw new IllegalArgumentException("type mismatch");            
+            // FEATURE: Check if we can compare these classes
+        }
     }
+    
 
+    public class PrimitiveComparison extends Comparison {
+        public PrimitiveComparison(Expr e1, Expr e2, String show) {
+            super(e1, e2, show);
+            if(!e1.getType().isPrimitive() || e1.getType() != e2.getType()) throw new IllegalArgumentException("type mismatch");
+        }
+    }
+    
+    public class Gt extends PrimitiveComparison { public Gt(Expr e1, Expr e2) { super(e1, e2, ">"); } }
+    public class Lt extends PrimitiveComparison { public Lt(Expr e1, Expr e2) { super(e1, e2, "<"); } }
+    public class Ge extends PrimitiveComparison { public Ge(Expr e1, Expr e2) { super(e1, e2, ">="); } }
+    public class Le extends PrimitiveComparison { public Le(Expr e1, Expr e2) { super(e1, e2, "<="); } }
+    
     // Math Operations //////////////////////////////////////////////////////////////////////////////
 
-    public class Math extends BinExpr {
-        private final String show;
-        public Math(Expr e1, Expr e2, String show) { super(e2, e1); this.show = show; }
-        public String toString() { return e1+" "+show+" "+e2; }
-        public Type getType() {
+    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");
+        }
+        public Type getType() { return e1.getType(); }
+    }
+    
+    public class Add  extends BinMath { public  Add(Expr e, Expr e2) { super(e, e2, "+"); } }
+    public class Sub  extends BinMath { public  Sub(Expr e, Expr e2) { super(e, e2, "-"); } }
+    public class Mul  extends BinMath { public  Mul(Expr e, Expr e2) { super(e, e2, "*"); } }
+    public class Rem  extends BinMath { public  Rem(Expr e, Expr e2) { super(e, e2, "%"); } }
+    public class Div  extends BinMath { public  Div(Expr e, Expr e2) { super(e, e2, "/"); } }
+    public class And  extends BinMath { public  And(Expr e, Expr e2) { super(e, e2, "&"); } }
+    public class Or   extends BinMath { public   Or(Expr e, Expr e2) { super(e, e2, "|"); } }
+    public class Xor  extends BinMath { public  Xor(Expr e, Expr e2) { super(e, e2, "^"); } }
+    
+    public class BitShiftExpr extends BinExpr {
+        public BitShiftExpr(Expr e1, Expr e2, String show) {
+            super(e1,e2,show);
             Type t = e1.getType();
-            if (t != e2.getType()) throw new Error("types disagree");
-            return t;
+            if(t != Type.INT && t != Type.LONG) throw new IllegalArgumentException("type mismatch");
+            if(e2.getType() != Type.INT) throw new IllegalArgumentException("type mismatch");
         }
+        public Type getType() { return e1.getType(); }
     }
-    public class Add  extends Math { public  Add(Expr e, Expr e2) { super(e, e2, "+"); } }
-    public class Sub  extends Math { public  Sub(Expr e, Expr e2) { super(e, e2, "-"); } }
-    public class Mul  extends Math { public  Mul(Expr e, Expr e2) { super(e, e2, "*"); } }
-    public class Rem  extends Math { public  Rem(Expr e, Expr e2) { super(e, e2, "%"); } }
-    //public class Neg  extends Math { public  Neg(Expr e) { super(e, "-"); } }
-    public class Div  extends Math { public  Div(Expr e, Expr e2) { super(e, e2, "/"); } }
-    public class Shl  extends Math { public  Shl(Expr e, Expr e2) { super(e, e2, "<<"); } }
-    public class Shr  extends Math { public  Shr(Expr e, Expr e2) { super(e, e2, ">>"); } }
-    public class Ushr extends Math { public Ushr(Expr e, Expr e2) { super(e, e2, ">>>"); } }
-    public class And  extends Math { public  And(Expr e, Expr e2) { super(e, e2, "&"); } }
-    public class Or   extends Math { public   Or(Expr e, Expr e2) { super(e, e2, "|"); } }
-    public class Xor  extends Math { public  Xor(Expr e, Expr e2) { super(e, e2, "^"); } }
+    public class Shl  extends BitShiftExpr { public  Shl(Expr e, Expr e2) { super(e, e2, "<<"); } }
+    public class Shr  extends BitShiftExpr { public  Shr(Expr e, Expr e2) { super(e, e2, ">>"); } }
+    public class Ushr extends BitShiftExpr { public Ushr(Expr e, Expr e2) { super(e, e2, ">>>"); } }
 
     // Other operations //////////////////////////////////////////////////////////////////////////////
 
     public class Cast extends Expr {
         final Expr e;
         final Type t;
-        public Cast(Expr e, Type t) { this.e = e; this.t = t; }
+        public Cast(Expr e, Type t) {
+            if(e.getType().isRef() != t.isRef()) throw new IllegalArgumentException("invalid cast");
+            // FEATURE: Check that one is a subclass of the other if it is a ref
+            this.e = e;
+            this.t = t; 
+        }
         public Type getType() { return t; }
     }
 
     public class InstanceOf extends Expr {
         final Expr e;
-        final Type t;
-        public InstanceOf(Expr e, Type t) { this.e = e; this.t = t; }
+        final Type.Ref t;
+        public InstanceOf(Expr e, Type.Ref t) {
+            if(!e.getType().isRef()) throw new IllegalArgumentException("can't do an instanceof check on a non-ref");
+            this.e = e; 
+            this.t = t; 
+        }
         public Type getType() { return Type.BOOLEAN; }
     }
 
     public class Throw extends Op {
         public final Expr e;
-        public Throw(Expr e) { this.e = e; }
+        public Throw(Expr e) {
+            if(!e.getType().isRef()) throw new IllegalArgumentException("can't throw a non ref");
+            // FEATURE: CHeck that it is a subclass of Throwable
+            this.e = e; 
+        }
     }
 
     public class Branch extends Op {
@@ -226,6 +322,7 @@ public class JSSA extends MethodGen implements CGConst {
         public final Type.Class t;
         public Type getType() { return t; }
         public New(Type.Class t) { this.t = t; }
+        public String toString() { return "new " + t + "(...)"; }
     }
     
     public class NewArray extends Expr {
@@ -236,13 +333,11 @@ public class JSSA extends MethodGen implements CGConst {
         public Type getType() { return t; }
     }
     
-    // FEATURE: Array stuff
-
     public class Return extends Op {
         final Expr e;
-        public Return() { this(null); }
+        public Return() { this(VOID_EXPR); }
         public Return(Expr e) { this.e = e; }
-        public String toString() { return e==null?"return":("return "+e.toString()); }
+        public String toString() { return e.getType() == Type.VOID ? "return" : ("return "+e.toString()); }
     }
 
     /** GETFIELD and GETSTATIC */
@@ -323,20 +418,17 @@ public class JSSA extends MethodGen implements CGConst {
     public class InvokeStatic    extends Invoke  { public InvokeStatic(Type.Class.Method m, Expr[] a) { super(m,a); } }
     public class InvokeSpecial   extends InvokeVirtual {
         public InvokeSpecial(Type.Class.Method m, Expr[] a, Expr e) { super(m,a,e); }
-        public String toString() {
-            StringBuffer sb = new StringBuffer();
-            sb.append(method.name.equals("<init>") ? "super" : method.name);
-            args(sb);
-            return sb.toString();
-        }
+        public String toString() { return toString(method.name.equals("<init>") ? method.getDeclaringClass().getName() : method.name); }
     }
     public class InvokeInterface extends InvokeVirtual{public InvokeInterface(Type.Class.Method m, Expr[] a, Expr e){super(m,a,e);}}
     public class InvokeVirtual   extends Invoke  {
         public final Expr instance;
         public InvokeVirtual(Type.Class.Method m, Expr[] a, Expr e) { super(m, a); instance = e; }
-        public String toString() {
+        public String toString() { return toString(method.name); }
+        protected String toString(String name) {
             StringBuffer sb = new StringBuffer();
-            sb.append(method.name);
+            sb.append(instance+".");
+            sb.append(name);
             args(sb);
             return sb.toString();
         }
@@ -346,7 +438,7 @@ public class JSSA extends MethodGen implements CGConst {
         private final Object o;
         public Constant(int i) { this(new Integer(i)); }
         public Constant(Object o) { this.o = o; }
-        public String toString() { return o.toString(); }
+        public String toString() { return o instanceof String ? "\"" + o + "\"" : o.toString(); }
         public Type getType() {
             if (o instanceof Byte) return Type.BYTE;
             if (o instanceof Short) return Type.SHORT;
@@ -355,8 +447,9 @@ public class JSSA extends MethodGen implements CGConst {
             if (o instanceof Long) return Type.LONG;
             if (o instanceof Double) return Type.DOUBLE;
             if (o instanceof Float) return Type.FLOAT;
-            if (o instanceof ConstantPool.Ent) throw new Error("unimplemented");
-            throw new Error("this should not happen");
+            if (o instanceof Integer) return Type.INT;
+            if (o instanceof String) return Type.STRING;
+            throw new IllegalStateException("unknown constant type");
         }
     }
 
@@ -403,10 +496,10 @@ public class JSSA extends MethodGen implements CGConst {
             case ISTORE_1: case LSTORE_1: case FSTORE_1: case DSTORE_1: case ASTORE_1: local[1]  = pop(); return null;
             case ASTORE_2: case DSTORE_2: case FSTORE_2: case LSTORE_2: case ISTORE_2: local[2]  = pop(); return null;
             case ISTORE_3: case LSTORE_3: case FSTORE_3: case DSTORE_3: case ASTORE_3: local[3]  = pop(); return null;
-            case POP:      stack[--sp] = null;                    
-            case POP2:     stack[--sp] = null; stack[--sp] = null;   /** fixme: pops a WORD, not an item */
-            case DUP:      stack[sp] = stack[sp-1]; sp++;
-            case DUP2:     stack[sp] = stack[sp-2]; stack[sp+1] = stack[sp-1]; sp+=2;
+            case POP:      pop(); return null;
+            case POP2:     pop(); pop(); return null;
+            case DUP:      push(stack[sp-1]); return null;
+            case DUP2:     push(stack[sp-2]); push(stack[sp-2]); return null;
 
                 // Conversions //////////////////////////////////////////////////////////////////////////////
 
@@ -467,9 +560,11 @@ public class JSSA extends MethodGen implements CGConst {
                 // Array manipulations //////////////////////////////////////////////////////////////////////////////
 
             case IALOAD:  case LALOAD:  case FALOAD:  case DALOAD:  case AALOAD:
-            case BALOAD:  case CALOAD:  case SALOAD:                                  push(new ArrayGet(pop(), pop())); return null;
+            case BALOAD:  case CALOAD:  case SALOAD:
+                return seqPush(new ArrayGet(pop(), pop()));
             case IASTORE: case LASTORE: case FASTORE: case DASTORE: case AASTORE:
-            case BASTORE: case CASTORE: case SASTORE:                                 return new ArrayPut(pop(), pop(), pop());
+            case BASTORE: case CASTORE: case SASTORE:
+                return new ArrayPut(pop(), pop(), pop());
 
                 // Invocation //////////////////////////////////////////////////////////////////////////////
 
@@ -477,19 +572,23 @@ public class JSSA extends MethodGen implements CGConst {
                 Type.Class.Method method = (Type.Class.Method)arg;
                 Expr args[] = new Expr[method.getNumArgs()];
                 for(int i=0; i<args.length; i++) args[args.length-i-1] = pop();
+                Expr ret;
                 switch(op) {
-                    case INVOKEVIRTUAL:   return push(new InvokeVirtual(method, args, pop()));
-                    case INVOKEINTERFACE: return push(new InvokeInterface(method, args, pop()));
-                    case INVOKESPECIAL:   return push(new InvokeSpecial(method, args, pop()));
-                    case INVOKESTATIC:    return push(new InvokeStatic(method, args));
+                    case INVOKEVIRTUAL:   ret = new InvokeVirtual(method, args, pop()); break;
+                    case INVOKEINTERFACE: ret = new InvokeInterface(method, args, pop()); break;
+                    case INVOKESPECIAL:   ret = new InvokeSpecial(method, args, pop()); break;
+                    case INVOKESTATIC:    ret = new InvokeStatic(method, args); break;
+                    default: throw new Error("should never happen");
                 }
+                if(ret.getType() != Type.VOID) push(ret);
+                return new Seq(ret);
             }
 
                 // Field Access //////////////////////////////////////////////////////////////////////////////
 
-            case GETSTATIC:         push(new Get((Type.Class.Field)arg, null)); return null;
+            case GETSTATIC:         return seqPush(new Get((Type.Class.Field)arg, null));
             case PUTSTATIC:         return new Put((Type.Class.Field)arg, pop(), null);
-            case GETFIELD:          push(new Get((Type.Class.Field)arg, pop())); return null;
+            case GETFIELD:          return seqPush(new Get((Type.Class.Field)arg, pop()));
             case PUTFIELD:          return new Put((Type.Class.Field)arg, pop(), pop());
 
                 // Allocation //////////////////////////////////////////////////////////////////////////////
@@ -508,23 +607,21 @@ public class JSSA extends MethodGen implements CGConst {
                     case 11: base = Type.LONG; break;
                     default: throw new IllegalStateException("invalid array type");
                 }
-                push(new NewArray(base.makeArray(),pop()));
-                return null;
+                return seqPush(new NewArray(base.makeArray(),pop()));
             }
             case ANEWARRAY:         push(new NewArray(((Type.Ref)arg).makeArray(), pop())); return null;
             case MULTIANEWARRAY: {
                 MethodGen.MultiANewArray mana = (MethodGen.MultiANewArray) arg;
                 Expr[] dims = new Expr[mana.dims];
                 for(int i=0;i<dims.length;i++) dims[i] = pop();
-                push(new NewArray(mana.type, dims));
-                return null;
+                return seqPush(new NewArray(mana.type, dims));
             }
-            case ARRAYLENGTH:       push(new ArrayLength(pop())); return null;
+            case ARRAYLENGTH:       return seqPush(new ArrayLength(pop()));
 
                 // Runtime Type information //////////////////////////////////////////////////////////////////////////////
 
-            case CHECKCAST:         push(new Cast(pop(), (Type)arg)); return null;
-            case INSTANCEOF:        push(new InstanceOf(pop(), (Type)arg)); return null;
+            case CHECKCAST:         return seqPush(new Cast(pop(), (Type.Ref)arg));
+            case INSTANCEOF:        push(new InstanceOf(pop(), (Type.Ref)arg)); return null;
 
             case LDC: case LDC_W: case LDC2_W: push(new Constant(arg)); return null;