add "this" for first arg
[org.ibex.classgen.git] / src / org / ibex / classgen / JSSA.java
index f3835ae..e608b42 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");
             }
         }
     }
@@ -74,7 +83,11 @@ public class JSSA extends MethodGen implements CGConst {
     private int sp = 0;
     
     private Expr push(Expr e) {
-        if(sp == stack.length-1) throw new IllegalStateException("stack overflow");
+        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() {