090fc974cad98c5fa942e56cf2008183bd9e3c93
[org.ibex.classgen.git] / src / org / ibex / classgen / JSSA.java
1 package org.ibex.classgen;
2 import java.io.*;
3 import java.util.*;
4
5 /**
6  *  a highly streamlined SSA-form intermediate representation of a
7  *  sequence of JVM instructions; all stack manipulation is factored
8  *  out.
9  */
10 public class JSSA extends MethodGen implements CGConst {
11
12     // Constructor //////////////////////////////////////////////////////////////////////////////
13     
14     public JSSA(Type.Class c, DataInput in, ConstantPool cp) throws IOException {
15         super(c, in, cp);
16         local = new Expr[maxLocals];
17         stack = new Expr[maxStack];
18         for(int i=0; i<this.method.getNumArgs(); i++)
19             local[i] = new Argument("arg"+i, this.method.getArgType(i));
20         for(int i=0; i<size(); i++) {
21             int    op  = get(i);
22             Object arg = getArg(i);
23             try {
24                 Object o = addOp(op, arg);
25                 if (o != null) {
26                     ops[numOps] = o;
27                     ofs[numOps++] = i;
28                 }
29             } catch(RuntimeException e) {
30                 System.err.println("Had a problem at PC: " + i + " of " + method);
31                 e.printStackTrace();
32                 throw new IOException("invalid class file");
33             }
34         }
35     }
36
37     public void debugBodyToString(StringBuffer sb) {
38         StringBuffer sb0 = new StringBuffer();
39         super.debugBodyToString(sb0);
40         StringTokenizer st = new StringTokenizer(sb0.toString(), "\n");
41         String[] lines = new String[st.countTokens()];
42         for(int i=0; i<lines.length; i++) lines[i] = st.nextToken();
43         for(int j=0; j<ofs[0]; j++) {
44             String s = "    /* " + lines[j].trim();
45             while(s.length() < 50) s += " ";
46             s += " */";
47             sb.append(s);
48             sb.append("\n");
49         }
50         for(int i=0; i<numOps; i++) {
51             String s = "    /* " + lines[ofs[i]].trim();
52             while(s.length() < 50) s += " ";
53             s += " */  ";
54             s += ops[i].toString();
55             sb.append(s);
56             sb.append(";\n");
57             for(int j=ofs[i]+1; j<(i==numOps-1?size():ofs[i+1]); j++) {
58                 s = "    /* " + lines[j].trim();
59                 while(s.length() < 50) s += " ";
60                 s += " */";
61                 sb.append(s);
62                 sb.append("\n");
63             }
64         }
65     }
66     
67     private Object[] ops = new Object[65535];
68     private int[] ofs = new int[65535];
69     private int numOps = 0;
70
71     // Instance Data; used ONLY during constructor; then thrown away /////////////////////////////////////////////////
72
73     /** this models the JVM locals; it is only used for unwinding stack-ops into an SSA-tree, then thrown away */
74     private final Expr[] local;
75     
76     /** this models the JVM stack; it is only used for unwinding stack-ops into an SSA-tree, then thrown away */
77     private final Expr[] stack;
78
79     /** JVM stack pointer */
80     private int sp = 0;
81     
82     private Expr push(Expr e) {
83         if(sp == stack.length) {
84             for(int i=0;i<stack.length;i++) System.err.println("Stack " + i + ": " + stack[i]);
85             throw new IllegalStateException("stack overflow (" + stack.length + ")");
86         }
87         if(e.getType() == Type.VOID) throw new IllegalArgumentException("can't push a void");
88         return stack[sp++] = e;
89     }
90     private Expr pop() {
91         if(sp == 0) throw new IllegalStateException("stack underflow");
92         return stack[--sp];
93     }
94
95
96     // SSA-node classes /////////////////////////////////////////////////////////////////////////////////////////
97
98     public final Expr VOID_EXPR = new Expr() {
99         public Type getType() { return Type.VOID; }
100     };
101     
102     /** an purely imperative operation which does not generate data */
103     public abstract class Op {
104         //public abstract Op[] predecessors();  // not implemented yet
105         //public abstract Op[] successors();    // not implemented yet
106         public String toString() { return name(); }
107         String name() {
108             String name = this.getClass().getName();
109             if (name.indexOf('$') != -1) name = name.substring(name.lastIndexOf('$')+1);
110             if (name.indexOf('.') != -1) name = name.substring(name.lastIndexOf('.')+1);
111             return name;
112         }
113     }
114
115     /** an operation which generates data */
116     public abstract class Expr extends Op {
117         //public abstract Expr[] contributors();  // not implemented yet
118         //public abstract Expr[] dependents();    // not implemented yet
119
120         /** every JSSA.Expr either remembers its type _OR_ knows how to figure it out (the latter is preferred to eliminate
121          *  redundant information that could possibly "disagree" with itself -- this happened a LOT in Soot) */
122         public abstract Type getType();
123     }
124
125     /**
126      *  A "nondeterministic merge" -- for example when the first instruction in a loop reads from a local which could have been
127      *  written to either by some instruction at the end of the previous iteration of the loop or by some instruction before
128      *  the loop (on the first iteration).
129      */
130     public class Phi extends Expr {
131         private final Expr[] inputs;
132         public Phi(Expr[] inputs) {
133             this.inputs = new Expr[inputs.length];
134             System.arraycopy(inputs, 0, this.inputs, 0, inputs.length);
135         }
136         public Type getType() {
137             // sanity check
138             Type t = inputs[0].getType();
139
140             // FIXME: actually this should check type-unifiability... fe, the "type of null" unifies with any Type.Ref
141             for(int i=1; i<inputs.length; i++)
142                 if (inputs[i].getType() != t)
143                     throw new Error("Phi node with disagreeing types!  Crisis!");
144             return t;
145         }
146     }
147
148     public class Argument extends Expr {
149         public final String name;
150         public final Type t;
151         public Argument(String name, Type t) { this.name = name; this.t = t; }
152         public String toString() { return name; }
153         public Type getType() { return t; }
154     }
155     
156     // Unary Operations
157     public class Not extends Expr {
158         public final Expr e;
159         public Not(Expr e) {
160             if(e.getType() != Type.BOOLEAN) throw new IllegalArgumentException("not needs a boolean expression");
161             this.e = e;
162         }
163         public Type getType() { return Type.BOOLEAN; }
164         public String toString() { return "!(" + e + ")"; }
165     }
166     
167     public class Neg extends Expr {
168         public final Expr e;
169         public Neg(Expr e) {
170             if(!e.getType().isPrimitive()) throw new IllegalArgumentException("can only negate a primitive");
171             this.e = e;
172         }
173         public Type getType() { return e.getType(); }
174         public String toString() { return "- (" + e + ")"; }
175     }
176     
177     // Binary Operations //////////////////////////////////////////////////////////////////////////////
178
179     public abstract class BinExpr extends Expr {
180         public final Expr e1;
181         public final Expr e2;
182         private final String show;
183         public BinExpr(Expr e1, Expr e2, String show) { this.e1 = e1; this.e2 = e2; this.show = show; }
184         public String toString() {
185             // FEATURE: should we be doing some precedence stuff here? probably no worth it for debugging output
186             return "(" + e1 + show + e2 + ")";
187         }
188     }
189
190     public class Comparison extends BinExpr {
191         public Comparison(Expr e1, Expr e2, String show) { super(e1, e2, show); }
192         public Type getType() { return Type.BOOLEAN; }
193     }
194
195     public class Eq extends Comparison {
196         public Eq(Expr e1, Expr e2) {
197             super(e1, e2, "=="); 
198             if(e1.getType().isPrimitive() != e2.getType().isPrimitive())
199                 throw new IllegalArgumentException("type mismatch");
200             if(e1.getType().isPrimitive() && e1.getType() != e2.getType())
201                 throw new IllegalArgumentException("type mismatch");            
202             // FEATURE: Check if we can compare these classes
203         }
204     }
205     
206
207     public class PrimitiveComparison extends Comparison {
208         public PrimitiveComparison(Expr e1, Expr e2, String show) {
209             super(e1, e2, show);
210             if(!e1.getType().isPrimitive() || e1.getType() != e2.getType()) throw new IllegalArgumentException("type mismatch");
211         }
212     }
213     
214     public class Gt extends PrimitiveComparison { public Gt(Expr e1, Expr e2) { super(e1, e2, ">"); } }
215     public class Lt extends PrimitiveComparison { public Lt(Expr e1, Expr e2) { super(e1, e2, "<"); } }
216     public class Ge extends PrimitiveComparison { public Ge(Expr e1, Expr e2) { super(e1, e2, ">="); } }
217     public class Le extends PrimitiveComparison { public Le(Expr e1, Expr e2) { super(e1, e2, "<="); } }
218     
219     // Math Operations //////////////////////////////////////////////////////////////////////////////
220
221     public class BinMath extends BinExpr {
222         public BinMath(Expr e1, Expr e2, String show) {
223             super(e2, e1, show); 
224             if(e1.getType() != e2.getType()) throw new IllegalArgumentException("types disagree");
225         }
226         public Type getType() { return e1.getType(); }
227     }
228     
229     public class Add  extends BinMath { public  Add(Expr e, Expr e2) { super(e, e2, "+"); } }
230     public class Sub  extends BinMath { public  Sub(Expr e, Expr e2) { super(e, e2, "-"); } }
231     public class Mul  extends BinMath { public  Mul(Expr e, Expr e2) { super(e, e2, "*"); } }
232     public class Rem  extends BinMath { public  Rem(Expr e, Expr e2) { super(e, e2, "%"); } }
233     public class Div  extends BinMath { public  Div(Expr e, Expr e2) { super(e, e2, "/"); } }
234     public class And  extends BinMath { public  And(Expr e, Expr e2) { super(e, e2, "&"); } }
235     public class Or   extends BinMath { public   Or(Expr e, Expr e2) { super(e, e2, "|"); } }
236     public class Xor  extends BinMath { public  Xor(Expr e, Expr e2) { super(e, e2, "^"); } }
237     
238     public class BitShiftExpr extends BinExpr {
239         public BitShiftExpr(Expr e1, Expr e2, String show) {
240             super(e1,e2,show);
241             Type t = e1.getType();
242             if(t != Type.INT && t != Type.LONG) throw new IllegalArgumentException("type mismatch");
243             if(e2.getType() != Type.INT) throw new IllegalArgumentException("type mismatch");
244         }
245         public Type getType() { return e1.getType(); }
246     }
247     public class Shl  extends BitShiftExpr { public  Shl(Expr e, Expr e2) { super(e, e2, "<<"); } }
248     public class Shr  extends BitShiftExpr { public  Shr(Expr e, Expr e2) { super(e, e2, ">>"); } }
249     public class Ushr extends BitShiftExpr { public Ushr(Expr e, Expr e2) { super(e, e2, ">>>"); } }
250
251     // Other operations //////////////////////////////////////////////////////////////////////////////
252
253     public class Cast extends Expr {
254         final Expr e;
255         final Type t;
256         public Cast(Expr e, Type t) {
257             if(e.getType().isRef() != t.isRef()) throw new IllegalArgumentException("invalid cast");
258             // FEATURE: Check that one is a subclass of the other if it is a ref
259             this.e = e;
260             this.t = t; 
261         }
262         public Type getType() { return t; }
263     }
264
265     public class InstanceOf extends Expr {
266         final Expr e;
267         final Type.Ref t;
268         public InstanceOf(Expr e, Type.Ref t) {
269             if(!e.getType().isRef()) throw new IllegalArgumentException("can't do an instanceof check on a non-ref");
270             this.e = e; 
271             this.t = t; 
272         }
273         public Type getType() { return Type.BOOLEAN; }
274     }
275
276     public class Throw extends Op {
277         public final Expr e;
278         public Throw(Expr e) {
279             if(!e.getType().isRef()) throw new IllegalArgumentException("can't throw a non ref");
280             // FEATURE: CHeck that it is a subclass of Throwable
281             this.e = e; 
282         }
283     }
284
285     public class Branch extends Op {
286         public Branch(Expr condition, Object destination) { }
287         public Branch(Label destination) { }
288         public Branch(MethodGen.Switch s) { }
289         public Branch() { }
290     }
291     public class Goto extends Branch { }
292     public class RET extends Branch { }
293     public class JSR extends Branch { public JSR(Label l) { super(l); } }
294     public class If extends Branch { }
295
296     /** represents a "returnaddr" pushed onto the stack */
297     public class Label extends Expr {
298         public final Op op;
299         public Type getType() { throw new Error("attempted to call getType() on a Label"); }
300         public Label(Op op) { this.op = op; }
301         public Label(int i) { this.op = null; /* FIXME */ }
302     }
303
304     public class New extends Expr {
305         public final Type.Class t;
306         public Type getType() { return t; }
307         public New(Type.Class t) { this.t = t; }
308     }
309     
310     public class NewArray extends Expr {
311         public final Type.Array t;
312         public final Expr[] dims;
313         public NewArray(Type.Array t, Expr[] dims) { this.t = t; this.dims = dims; }
314         public NewArray(Type.Array t, Expr dim) { this(t,new Expr[]{dim}); }
315         public Type getType() { return t; }
316     }
317     
318     public class Return extends Op {
319         final Expr e;
320         public Return() { this(VOID_EXPR); }
321         public Return(Expr e) { this.e = e; }
322         public String toString() { return e.getType() == Type.VOID ? "return" : ("return "+e.toString()); }
323     }
324
325     /** GETFIELD and GETSTATIC */
326     public class Get extends Expr {
327         final Type.Class.Field f;
328         final Expr e;
329         public Type getType() { return f.getType(); }
330         public Get(Type.Class.Field f) { this(f, null); }
331         public Get(Type.Class.Field f, Expr e) { this.f = f; this.e = e; }
332         public String toString() {
333             return
334                 (e!=null
335                  ? e+"."+f.name
336                  : f.getDeclaringClass() == JSSA.this.method.getDeclaringClass()
337                  ? f.name
338                  : f.toString());
339         }
340     }
341
342     /** PUTFIELD and PUTSTATIC */
343     public class Put extends Op {
344         final Type.Class.Field f;
345         final Expr v;
346         final Expr e;
347         public Put(Type.Class.Field f, Expr v) { this(f, v, null); }
348         public Put(Type.Class.Field f, Expr v, Expr e) { this.f = f; this.v = v; this.e = e; }
349         public String toString() {
350             return
351                 (e!=null
352                  ? e+"."+f.name
353                  : f.getDeclaringClass() == JSSA.this.method.getDeclaringClass()
354                  ? f.name
355                  : f.toString()) + " = " + v;
356         }
357     }
358
359     public class ArrayPut extends Op {
360         final Expr e, i, v;
361         public ArrayPut(Expr e, Expr i, Expr v) { this.e = e; this.i = i; this.v = v; }
362     }
363
364     public class ArrayGet extends Expr {
365         final Expr e, i;
366         public ArrayGet(Expr e, Expr i) { this.e = e; this.i = i; }
367         public Type getType() { return e.getType().asArray().getElementType(); }
368     }
369
370     public class ArrayLength extends Expr {
371         final Expr e;
372         public ArrayLength(Expr e) { this.e = e; }
373         public Type getType() { return Type.INT; }
374     }
375
376     public abstract class Invoke extends Expr {
377         public final Expr[] arguments;
378         public final Type.Class.Method method;
379         protected Invoke(Type.Class.Method m, Expr[] a) { this.arguments = a; this.method = m; } 
380
381         public Type getType() { return method.getReturnType(); }
382         protected void args(StringBuffer sb) {
383             sb.append("(");
384             for(int i=0; i<arguments.length; i++) {
385                 if (i>0) sb.append(", ");
386                 sb.append(arguments[i]+"");
387             }
388             sb.append(")");
389         }
390
391         public String toString() {
392             StringBuffer sb = new StringBuffer();
393             sb.append(method.getDeclaringClass() == JSSA.this.method.getDeclaringClass()
394                       ? method.name
395                       : (method.getDeclaringClass() + "." + method.name));
396             args(sb);
397             return sb.toString();
398         }
399     }
400     public class InvokeStatic    extends Invoke  { public InvokeStatic(Type.Class.Method m, Expr[] a) { super(m,a); } }
401     public class InvokeSpecial   extends InvokeVirtual {
402         public InvokeSpecial(Type.Class.Method m, Expr[] a, Expr e) { super(m,a,e); }
403         public String toString() {
404             StringBuffer sb = new StringBuffer();
405             sb.append(method.name.equals("<init>") ? "super" : method.name);
406             args(sb);
407             return sb.toString();
408         }
409     }
410     public class InvokeInterface extends InvokeVirtual{public InvokeInterface(Type.Class.Method m, Expr[] a, Expr e){super(m,a,e);}}
411     public class InvokeVirtual   extends Invoke  {
412         public final Expr instance;
413         public InvokeVirtual(Type.Class.Method m, Expr[] a, Expr e) { super(m, a); instance = e; }
414         public String toString() {
415             StringBuffer sb = new StringBuffer();
416             sb.append(method.name);
417             args(sb);
418             return sb.toString();
419         }
420     }
421
422     public class Constant extends Expr {
423         private final Object o;
424         public Constant(int i) { this(new Integer(i)); }
425         public Constant(Object o) { this.o = o; }
426         public String toString() { return o.toString(); }
427         public Type getType() {
428             if (o instanceof Byte) return Type.BYTE;
429             if (o instanceof Short) return Type.SHORT;
430             if (o instanceof Character) return Type.CHAR;
431             if (o instanceof Boolean) return Type.BOOLEAN;
432             if (o instanceof Long) return Type.LONG;
433             if (o instanceof Double) return Type.DOUBLE;
434             if (o instanceof Float) return Type.FLOAT;
435             if (o instanceof ConstantPool.Ent) throw new Error("unimplemented");
436             throw new Error("this should not happen");
437         }
438     }
439
440
441     // Implementation //////////////////////////////////////////////////////////////////////////////
442
443     private Object addOp(int op, Object arg) {
444         int i1 = 0;
445         int i2 = 0;
446         if (op==WIDE) {
447             MethodGen.Wide w = (MethodGen.Wide)arg;
448             op = w.op;
449             arg = null;
450             i1 = w.varNum;
451             i2 = w.n;
452         }
453         if (op==IINC) {
454             MethodGen.Pair p = (MethodGen.Pair)arg;
455             arg = null;
456             i1 = p.i1;
457             i2 = p.i2;
458         }
459         switch(op) {
460
461             case NOP: return null;
462
463                 // Stack manipulations //////////////////////////////////////////////////////////////////////////////
464
465             case ACONST_NULL:                                                      return stack[sp++] = new Constant(null);
466             case ICONST_M1:                                                        return stack[sp++] = new Constant(-1);
467             case ICONST_0: case LCONST_0: case FCONST_0: case DCONST_0:            push(new Constant(0)); return null;
468             case ICONST_1: case LCONST_1: case FCONST_1: case DCONST_1:            push(new Constant(1)); return null;
469             case ICONST_2: case FCONST_2:                                          push(new Constant(2)); return null;
470             case ICONST_3:                                                         push(new Constant(3)); return null;
471             case ICONST_4:                                                         push(new Constant(4)); return null;
472             case ICONST_5:                                                         push(new Constant(5)); return null;
473             case ILOAD:    case LLOAD:    case FLOAD:    case DLOAD:    case ALOAD:    return push(local[i1]);
474             case ILOAD_0:  case LLOAD_0:  case FLOAD_0:  case DLOAD_0:  case ALOAD_0:  return push(local[0]);
475             case ILOAD_1:  case LLOAD_1:  case FLOAD_1:  case DLOAD_1:  case ALOAD_1:  return push(local[1]);
476             case ALOAD_2:  case DLOAD_2:  case FLOAD_2:  case LLOAD_2:  case ILOAD_2:  return push(local[2]);
477             case ILOAD_3:  case LLOAD_3:  case FLOAD_3:  case DLOAD_3:  case ALOAD_3:  return push(local[3]);
478             case ISTORE:   case LSTORE:   case FSTORE:   case DSTORE:   case ASTORE:   local[i1] = pop(); return null;
479             case ISTORE_0: case LSTORE_0: case FSTORE_0: case DSTORE_0: case ASTORE_0: local[0]  = pop(); return null;
480             case ISTORE_1: case LSTORE_1: case FSTORE_1: case DSTORE_1: case ASTORE_1: local[1]  = pop(); return null;
481             case ASTORE_2: case DSTORE_2: case FSTORE_2: case LSTORE_2: case ISTORE_2: local[2]  = pop(); return null;
482             case ISTORE_3: case LSTORE_3: case FSTORE_3: case DSTORE_3: case ASTORE_3: local[3]  = pop(); return null;
483             case POP:      stack[--sp] = null;                    
484             case POP2:     stack[--sp] = null; stack[--sp] = null;   /** fixme: pops a WORD, not an item */
485             case DUP:      stack[sp] = stack[sp-1]; sp++;
486             case DUP2:     stack[sp] = stack[sp-2]; stack[sp+1] = stack[sp-1]; sp+=2;
487
488                 // Conversions //////////////////////////////////////////////////////////////////////////////
489
490                 // coercions are added as-needed when converting from JSSA back to bytecode, so we can
491                 // simply discard them here (assuming the bytecode we're reading in was valid in the first place)
492
493             case I2L: case F2L: case D2L:               push(new Cast(pop(), Type.LONG)); return null;
494             case I2F: case L2F: case D2F:               push(new Cast(pop(), Type.FLOAT)); return null;
495             case I2D: case L2D: case F2D:               push(new Cast(pop(), Type.DOUBLE)); return null;
496             case L2I: case F2I: case D2I:               push(new Cast(pop(), Type.INT)); return null;
497             case I2B:                                   push(new Cast(pop(), Type.BYTE)); return null;
498             case I2C:                                   push(new Cast(pop(), Type.CHAR)); return null;
499             case I2S:                                   push(new Cast(pop(), Type.SHORT)); return null;
500             case SWAP:                                  { Expr e1 = pop(), e2 = pop(); push(e2);  push(e1); return null; }
501
502                 // Math //////////////////////////////////////////////////////////////////////////////
503                    
504             case IADD: case LADD: case FADD: case DADD: push(new Add(pop(), pop())); return null;
505             case ISUB: case LSUB: case FSUB: case DSUB: push(new Sub(pop(), pop())); return null;
506             case IMUL: case LMUL: case FMUL: case DMUL: push(new Mul(pop(), pop())); return null;
507             case IREM: case LREM: case FREM: case DREM: push(new Rem(pop(), pop())); return null;
508                 //case INEG: case LNEG: case FNEG: case DNEG: push(new Neg(pop())); return null;
509             case IDIV: case LDIV: case FDIV: case DDIV: push(new Div(pop(), pop())); return null;
510             case ISHL: case LSHL:                       push(new Shl(pop(), pop())); return null;
511             case ISHR: case LSHR:                       push(new Shr(pop(), pop())); return null;
512             case IUSHR: case LUSHR:                     push(new Ushr(pop(), pop())); return null;
513             case IAND: case LAND:                       push(new And(pop(), pop())); return null;
514             case IOR:  case LOR:                        push(new Or(pop(), pop())); return null;
515             case IXOR: case LXOR:                       push(new Xor(pop(), pop())); return null;
516             case IINC:                                  return local[i1] = new Add(local[i1], new Constant(i2));
517
518                 // Control and branching //////////////////////////////////////////////////////////////////////////////
519
520             case IFNULL:                                return new Branch(new Eq(pop(), new Constant(null)), new Label(i1));
521             case IFNONNULL:                             return new Branch(new Not(new Eq(pop(),new Constant(null))),new Label(i1));
522             case IFEQ:                                  return new Branch(    new Eq(new Constant(0), pop()),  arg);
523             case IFNE:                                  return new Branch(new Not(new Eq(new Constant(0), pop())), arg);
524             case IFLT:                                  return new Branch(    new Lt(new Constant(0), pop()),  arg);
525             case IFGE:                                  return new Branch(new Not(new Lt(new Constant(0), pop())), arg);
526             case IFGT:                                  return new Branch(    new Gt(new Constant(0), pop()),  arg);
527             case IFLE:                                  return new Branch(new Not(new Gt(new Constant(0), pop())), arg);
528             case IF_ICMPEQ:                             return new Branch(    new Eq(pop(), pop()),  arg);
529             case IF_ICMPNE:                             return new Branch(new Not(new Eq(pop(), pop())), arg);
530             case IF_ICMPLT:                             return new Branch(    new Lt(pop(), pop()),  arg);
531             case IF_ICMPGE:                             return new Branch(new Not(new Lt(pop(), pop())), arg);
532             case IF_ICMPGT:                             return new Branch(    new Gt(pop(), pop()),  arg);
533             case IF_ICMPLE:                             return new Branch(new Not(new Gt(pop(), pop())), arg);
534             case IF_ACMPEQ:                             return new Branch(    new Eq(pop(), pop()),  arg);
535             case IF_ACMPNE:                             return new Branch(new Not(new Eq(pop(), pop())), arg);
536             case ATHROW:                                return new Throw(pop());
537             case GOTO:                                  return new Branch(new Label(i1));
538             case JSR:                                   return new JSR(new Label(i1));
539             case RET:                                   return new RET();
540             case RETURN:                                return new Return();
541             case IRETURN: case LRETURN: case FRETURN: case DRETURN: case ARETURN:
542                 return new Return(pop());
543
544                 // Array manipulations //////////////////////////////////////////////////////////////////////////////
545
546             case IALOAD:  case LALOAD:  case FALOAD:  case DALOAD:  case AALOAD:
547             case BALOAD:  case CALOAD:  case SALOAD:                                  push(new ArrayGet(pop(), pop())); return null;
548             case IASTORE: case LASTORE: case FASTORE: case DASTORE: case AASTORE:
549             case BASTORE: case CASTORE: case SASTORE:                                 return new ArrayPut(pop(), pop(), pop());
550
551                 // Invocation //////////////////////////////////////////////////////////////////////////////
552
553             case INVOKEVIRTUAL: case INVOKESPECIAL: case INVOKESTATIC: case INVOKEINTERFACE: {
554                 Type.Class.Method method = (Type.Class.Method)arg;
555                 Expr args[] = new Expr[method.getNumArgs()];
556                 for(int i=0; i<args.length; i++) args[args.length-i-1] = pop();
557                 switch(op) {
558                     case INVOKEVIRTUAL:   return push(new InvokeVirtual(method, args, pop()));
559                     case INVOKEINTERFACE: return push(new InvokeInterface(method, args, pop()));
560                     case INVOKESPECIAL:   return push(new InvokeSpecial(method, args, pop()));
561                     case INVOKESTATIC:    return push(new InvokeStatic(method, args));
562                 }
563             }
564
565                 // Field Access //////////////////////////////////////////////////////////////////////////////
566
567             case GETSTATIC:         push(new Get((Type.Class.Field)arg, null)); return null;
568             case PUTSTATIC:         return new Put((Type.Class.Field)arg, pop(), null);
569             case GETFIELD:          push(new Get((Type.Class.Field)arg, pop())); return null;
570             case PUTFIELD:          return new Put((Type.Class.Field)arg, pop(), pop());
571
572                 // Allocation //////////////////////////////////////////////////////////////////////////////
573
574             case NEW:               push(new New((Type.Class)arg)); return null;
575             case NEWARRAY: {
576                 Type base;
577                 switch(((Integer)arg).intValue()) {
578                     case 4: base = Type.BOOLEAN; break;
579                     case 5: base = Type.CHAR; break;
580                     case 6: base = Type.FLOAT; break;
581                     case 7: base = Type.DOUBLE; break;
582                     case 8: base = Type.BYTE; break;
583                     case 9: base = Type.SHORT; break;
584                     case 10: base = Type.INT; break;
585                     case 11: base = Type.LONG; break;
586                     default: throw new IllegalStateException("invalid array type");
587                 }
588                 push(new NewArray(base.makeArray(),pop()));
589                 return null;
590             }
591             case ANEWARRAY:         push(new NewArray(((Type.Ref)arg).makeArray(), pop())); return null;
592             case MULTIANEWARRAY: {
593                 MethodGen.MultiANewArray mana = (MethodGen.MultiANewArray) arg;
594                 Expr[] dims = new Expr[mana.dims];
595                 for(int i=0;i<dims.length;i++) dims[i] = pop();
596                 push(new NewArray(mana.type, dims));
597                 return null;
598             }
599             case ARRAYLENGTH:       push(new ArrayLength(pop())); return null;
600
601                 // Runtime Type information //////////////////////////////////////////////////////////////////////////////
602
603             case CHECKCAST:         push(new Cast(pop(), (Type.Ref)arg)); return null;
604             case INSTANCEOF:        push(new InstanceOf(pop(), (Type.Ref)arg)); return null;
605
606             case LDC: case LDC_W: case LDC2_W: push(new Constant(arg)); return null;
607
608             case BIPUSH:    push(new Constant(i1));  // FIXME return null;
609             case SIPUSH:    push(new Constant(i1));  // FIXME return null;
610
611             case TABLESWITCH:    new Branch((MethodGen.Switch)arg);
612             case LOOKUPSWITCH:   new Branch((MethodGen.Switch)arg);
613
614                 /*
615             case MONITORENTER:   Op.monitorEnter(pop());
616             case MONITOREXIT:    Op.monitorExit(pop());
617                 */
618
619             case DUP_X1:         throw new Error("unimplemented");
620             case DUP_X2:         throw new Error("unimplemented");
621             case DUP2_X1:         throw new Error("unimplemented");
622             case DUP2_X2:         throw new Error("unimplemented");
623             case LCMP:         throw new Error("unimplemented");
624             case FCMPL:         throw new Error("unimplemented");
625             case FCMPG:         throw new Error("unimplemented");
626             case DCMPL:         throw new Error("unimplemented");
627             case DCMPG:         throw new Error("unimplemented");
628             case GOTO_W:         throw new Error("unimplemented");
629             case JSR_W:         throw new Error("unimplemented");
630             default:          throw new Error("unhandled");
631         }
632     }
633
634     public static void main(String[] args) throws Exception {
635         InputStream is = Class.forName(args[0]).getClassLoader().getResourceAsStream(args[0].replace('.', '/')+".class");
636         System.out.println(new ClassFile(new DataInputStream(is), true).toString());
637     }
638 }