1 package org.ibex.classgen;
6 * a highly streamlined SSA-form intermediate representation of a
7 * sequence of JVM instructions; all stack manipulation is factored
10 public class JSSA extends MethodGen implements CGConst {
12 // Constructor //////////////////////////////////////////////////////////////////////////////
14 public JSSA(Type.Class c, DataInput in, ConstantPool cp) throws IOException {
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++) {
22 Object arg = getArg(i);
23 Object o = addOp(op, arg);
31 public void debugBodyToString(StringBuffer sb) {
32 StringBuffer sb0 = new StringBuffer();
33 super.debugBodyToString(sb0);
34 StringTokenizer st = new StringTokenizer(sb0.toString(), "\n");
35 String[] lines = new String[st.countTokens()];
36 for(int i=0; i<lines.length; i++) lines[i] = st.nextToken();
37 for(int j=0; j<ofs[0]; j++) {
38 String s = " /* " + lines[j].trim();
39 while(s.length() < 50) s += " ";
44 for(int i=0; i<numOps; i++) {
45 String s = " /* " + lines[ofs[i]].trim();
46 while(s.length() < 50) s += " ";
48 s += ops[i].toString();
51 for(int j=ofs[i]+1; j<(i==numOps-1?size():ofs[i+1]); j++) {
52 s = " /* " + lines[j].trim();
53 while(s.length() < 50) s += " ";
61 private Object[] ops = new Object[65535];
62 private int[] ofs = new int[65535];
63 private int numOps = 0;
65 // Instance Data; used ONLY during constructor; then thrown away /////////////////////////////////////////////////
67 /** this models the JVM locals; it is only used for unwinding stack-ops into an SSA-tree, then thrown away */
68 private final Expr[] local;
70 /** this models the JVM stack; it is only used for unwinding stack-ops into an SSA-tree, then thrown away */
71 private final Expr[] stack;
73 /** JVM stack pointer */
76 private Expr push(Expr e) { return stack[sp++] = e; }
77 private Expr pop() { return stack[--sp]; }
80 // SSA-node classes /////////////////////////////////////////////////////////////////////////////////////////
82 public final Expr VOID_EXPR = new Expr() {
83 public Type getType() { return Type.VOID; }
86 /** an purely imperative operation which does not generate data */
87 public abstract class Op {
88 //public abstract Op[] predecessors(); // not implemented yet
89 //public abstract Op[] successors(); // not implemented yet
90 public String toString() { return name(); }
92 String name = this.getClass().getName();
93 if (name.indexOf('$') != -1) name = name.substring(name.lastIndexOf('$')+1);
94 if (name.indexOf('.') != -1) name = name.substring(name.lastIndexOf('.')+1);
99 /** an operation which generates data */
100 public abstract class Expr extends Op {
101 //public abstract Expr[] contributors(); // not implemented yet
102 //public abstract Expr[] dependents(); // not implemented yet
104 /** every JSSA.Expr either remembers its type _OR_ knows how to figure it out (the latter is preferred to eliminate
105 * redundant information that could possibly "disagree" with itself -- this happened a LOT in Soot) */
106 public abstract Type getType();
110 * A "nondeterministic merge" -- for example when the first instruction in a loop reads from a local which could have been
111 * written to either by some instruction at the end of the previous iteration of the loop or by some instruction before
112 * the loop (on the first iteration).
114 public class Phi extends Expr {
115 private final Expr[] inputs;
116 public Phi(Expr[] inputs) {
117 this.inputs = new Expr[inputs.length];
118 System.arraycopy(inputs, 0, this.inputs, 0, inputs.length);
120 public Type getType() {
122 Type t = inputs[0].getType();
124 // FIXME: actually this should check type-unifiability... fe, the "type of null" unifies with any Type.Ref
125 for(int i=1; i<inputs.length; i++)
126 if (inputs[i].getType() != t)
127 throw new Error("Phi node with disagreeing types! Crisis!");
132 public class Argument extends Expr {
133 public final String name;
135 public Argument(String name, Type t) { this.name = name; this.t = t; }
136 public String toString() { return name; }
137 public Type getType() { return t; }
140 // Binary Operations //////////////////////////////////////////////////////////////////////////////
142 public abstract class BinExpr extends Expr {
143 public final Expr e1;
144 public final Expr e2;
145 public BinExpr(Expr e1, Expr e2) { this.e1 = e1; this.e2 = e2; }
146 public String toString() {
147 return name() + "("+e1+", "+e2+")";
151 public class Comparison extends BinExpr {
152 public Comparison(Expr e1, Expr e2) { super(e1, e2); }
153 public Type getType() { return Type.BOOLEAN; }
155 public class Gt extends Comparison { public Gt(Expr e1, Expr e2) { super(e1, e2); } }
156 public class Lt extends Comparison { public Lt(Expr e1, Expr e2) { super(e1, e2); } }
157 public class Eq extends Comparison { public Eq(Expr e1, Expr e2) { super(e1, e2); } }
158 public class Not extends Expr {
160 public Not(Expr e) { this.e = e; }
161 public Type getType() { return Type.BOOLEAN; }
164 // Math Operations //////////////////////////////////////////////////////////////////////////////
166 public class Math extends BinExpr {
167 private final String show;
168 public Math(Expr e1, Expr e2, String show) { super(e2, e1); this.show = show; }
169 public String toString() { return e1+" "+show+" "+e2; }
170 public Type getType() {
171 Type t = e1.getType();
172 if (t != e2.getType()) throw new Error("types disagree");
176 public class Add extends Math { public Add(Expr e, Expr e2) { super(e, e2, "+"); } }
177 public class Sub extends Math { public Sub(Expr e, Expr e2) { super(e, e2, "-"); } }
178 public class Mul extends Math { public Mul(Expr e, Expr e2) { super(e, e2, "*"); } }
179 public class Rem extends Math { public Rem(Expr e, Expr e2) { super(e, e2, "%"); } }
180 //public class Neg extends Math { public Neg(Expr e) { super(e, "-"); } }
181 public class Div extends Math { public Div(Expr e, Expr e2) { super(e, e2, "/"); } }
182 public class Shl extends Math { public Shl(Expr e, Expr e2) { super(e, e2, "<<"); } }
183 public class Shr extends Math { public Shr(Expr e, Expr e2) { super(e, e2, ">>"); } }
184 public class Ushr extends Math { public Ushr(Expr e, Expr e2) { super(e, e2, ">>>"); } }
185 public class And extends Math { public And(Expr e, Expr e2) { super(e, e2, "&"); } }
186 public class Or extends Math { public Or(Expr e, Expr e2) { super(e, e2, "|"); } }
187 public class Xor extends Math { public Xor(Expr e, Expr e2) { super(e, e2, "^"); } }
189 // Other operations //////////////////////////////////////////////////////////////////////////////
191 public class Cast extends Expr {
194 public Cast(Expr e, Type t) { this.e = e; this.t = t; }
195 public Type getType() { return t; }
198 public class InstanceOf extends Expr {
201 public InstanceOf(Expr e, Type t) { this.e = e; this.t = t; }
202 public Type getType() { return Type.BOOLEAN; }
205 public class Throw extends Op {
207 public Throw(Expr e) { this.e = e; }
210 public class Branch extends Op {
211 public Branch(Expr condition, Object destination) { }
212 public Branch(Label destination) { }
213 public Branch(MethodGen.Switch s) { }
216 public class Goto extends Branch { }
217 public class RET extends Branch { }
218 public class JSR extends Branch { public JSR(Label l) { super(l); } }
219 public class If extends Branch { }
221 /** represents a "returnaddr" pushed onto the stack */
222 public class Label extends Expr {
224 public Type getType() { throw new Error("attempted to call getType() on a Label"); }
225 public Label(Op op) { this.op = op; }
226 public Label(int i) { this.op = null; /* FIXME */ }
229 public class New extends Expr {
230 public final Type.Class t;
231 public Type getType() { return t; }
232 public New(Type.Class t) { this.t = t; }
235 public class NewArray extends Expr {
236 public final Type.Array t;
237 public final Expr[] dims;
238 public NewArray(Type.Array t, Expr[] dims) { this.t = t; this.dims = dims; }
239 public NewArray(Type.Array t, Expr dim) { this(t,new Expr[]{dim}); }
240 public Type getType() { return t; }
243 // FEATURE: Array stuff
245 public class Return extends Op {
247 public Return() { this(VOID_EXPR); }
248 public Return(Expr e) { this.e = e; }
249 public String toString() { return e.getType() == Type.VOID ? "return" : ("return "+e.toString()); }
252 /** GETFIELD and GETSTATIC */
253 public class Get extends Expr {
254 final Type.Class.Field f;
256 public Type getType() { return f.getType(); }
257 public Get(Type.Class.Field f) { this(f, null); }
258 public Get(Type.Class.Field f, Expr e) { this.f = f; this.e = e; }
259 public String toString() {
263 : f.getDeclaringClass() == JSSA.this.method.getDeclaringClass()
269 /** PUTFIELD and PUTSTATIC */
270 public class Put extends Op {
271 final Type.Class.Field f;
274 public Put(Type.Class.Field f, Expr v) { this(f, v, null); }
275 public Put(Type.Class.Field f, Expr v, Expr e) { this.f = f; this.v = v; this.e = e; }
276 public String toString() {
280 : f.getDeclaringClass() == JSSA.this.method.getDeclaringClass()
282 : f.toString()) + " = " + v;
286 public class ArrayPut extends Op {
288 public ArrayPut(Expr e, Expr i, Expr v) { this.e = e; this.i = i; this.v = v; }
291 public class ArrayGet extends Expr {
293 public ArrayGet(Expr e, Expr i) { this.e = e; this.i = i; }
294 public Type getType() { return e.getType().asArray().getElementType(); }
297 public class ArrayLength extends Expr {
299 public ArrayLength(Expr e) { this.e = e; }
300 public Type getType() { return Type.INT; }
303 public abstract class Invoke extends Expr {
304 public final Expr[] arguments;
305 public final Type.Class.Method method;
306 protected Invoke(Type.Class.Method m, Expr[] a) { this.arguments = a; this.method = m; }
308 public Type getType() { return method.getReturnType(); }
309 protected void args(StringBuffer sb) {
311 for(int i=0; i<arguments.length; i++) {
312 if (i>0) sb.append(", ");
313 sb.append(arguments[i]+"");
318 public String toString() {
319 StringBuffer sb = new StringBuffer();
320 sb.append(method.getDeclaringClass() == JSSA.this.method.getDeclaringClass()
322 : (method.getDeclaringClass() + "." + method.name));
324 return sb.toString();
327 public class InvokeStatic extends Invoke { public InvokeStatic(Type.Class.Method m, Expr[] a) { super(m,a); } }
328 public class InvokeSpecial extends InvokeVirtual {
329 public InvokeSpecial(Type.Class.Method m, Expr[] a, Expr e) { super(m,a,e); }
330 public String toString() {
331 StringBuffer sb = new StringBuffer();
332 sb.append(method.name.equals("<init>") ? "super" : method.name);
334 return sb.toString();
337 public class InvokeInterface extends InvokeVirtual{public InvokeInterface(Type.Class.Method m, Expr[] a, Expr e){super(m,a,e);}}
338 public class InvokeVirtual extends Invoke {
339 public final Expr instance;
340 public InvokeVirtual(Type.Class.Method m, Expr[] a, Expr e) { super(m, a); instance = e; }
341 public String toString() {
342 StringBuffer sb = new StringBuffer();
343 sb.append(method.name);
345 return sb.toString();
349 public class Constant extends Expr {
350 private final Object o;
351 public Constant(int i) { this(new Integer(i)); }
352 public Constant(Object o) { this.o = o; }
353 public String toString() { return o.toString(); }
354 public Type getType() {
355 if (o instanceof Byte) return Type.BYTE;
356 if (o instanceof Short) return Type.SHORT;
357 if (o instanceof Character) return Type.CHAR;
358 if (o instanceof Boolean) return Type.BOOLEAN;
359 if (o instanceof Long) return Type.LONG;
360 if (o instanceof Double) return Type.DOUBLE;
361 if (o instanceof Float) return Type.FLOAT;
362 if (o instanceof ConstantPool.Ent) throw new Error("unimplemented");
363 throw new Error("this should not happen");
368 // Implementation //////////////////////////////////////////////////////////////////////////////
370 private Object addOp(int op, Object arg) {
374 MethodGen.Wide w = (MethodGen.Wide)arg;
381 MethodGen.Pair p = (MethodGen.Pair)arg;
388 case NOP: return null;
390 // Stack manipulations //////////////////////////////////////////////////////////////////////////////
392 case ACONST_NULL: return stack[sp++] = new Constant(null);
393 case ICONST_M1: return stack[sp++] = new Constant(-1);
394 case ICONST_0: case LCONST_0: case FCONST_0: case DCONST_0: push(new Constant(0)); return null;
395 case ICONST_1: case LCONST_1: case FCONST_1: case DCONST_1: push(new Constant(1)); return null;
396 case ICONST_2: case FCONST_2: push(new Constant(2)); return null;
397 case ICONST_3: push(new Constant(3)); return null;
398 case ICONST_4: push(new Constant(4)); return null;
399 case ICONST_5: push(new Constant(5)); return null;
400 case ILOAD: case LLOAD: case FLOAD: case DLOAD: case ALOAD: return push(local[i1]);
401 case ILOAD_0: case LLOAD_0: case FLOAD_0: case DLOAD_0: case ALOAD_0: return push(local[0]);
402 case ILOAD_1: case LLOAD_1: case FLOAD_1: case DLOAD_1: case ALOAD_1: return push(local[1]);
403 case ALOAD_2: case DLOAD_2: case FLOAD_2: case LLOAD_2: case ILOAD_2: return push(local[2]);
404 case ILOAD_3: case LLOAD_3: case FLOAD_3: case DLOAD_3: case ALOAD_3: return push(local[3]);
405 case ISTORE: case LSTORE: case FSTORE: case DSTORE: case ASTORE: local[i1] = pop(); return null;
406 case ISTORE_0: case LSTORE_0: case FSTORE_0: case DSTORE_0: case ASTORE_0: local[0] = pop(); return null;
407 case ISTORE_1: case LSTORE_1: case FSTORE_1: case DSTORE_1: case ASTORE_1: local[1] = pop(); return null;
408 case ASTORE_2: case DSTORE_2: case FSTORE_2: case LSTORE_2: case ISTORE_2: local[2] = pop(); return null;
409 case ISTORE_3: case LSTORE_3: case FSTORE_3: case DSTORE_3: case ASTORE_3: local[3] = pop(); return null;
410 case POP: stack[--sp] = null;
411 case POP2: stack[--sp] = null; stack[--sp] = null; /** fixme: pops a WORD, not an item */
412 case DUP: stack[sp] = stack[sp-1]; sp++;
413 case DUP2: stack[sp] = stack[sp-2]; stack[sp+1] = stack[sp-1]; sp+=2;
415 // Conversions //////////////////////////////////////////////////////////////////////////////
417 // coercions are added as-needed when converting from JSSA back to bytecode, so we can
418 // simply discard them here (assuming the bytecode we're reading in was valid in the first place)
420 case I2L: case F2L: case D2L: push(new Cast(pop(), Type.LONG)); return null;
421 case I2F: case L2F: case D2F: push(new Cast(pop(), Type.FLOAT)); return null;
422 case I2D: case L2D: case F2D: push(new Cast(pop(), Type.DOUBLE)); return null;
423 case L2I: case F2I: case D2I: push(new Cast(pop(), Type.INT)); return null;
424 case I2B: push(new Cast(pop(), Type.BYTE)); return null;
425 case I2C: push(new Cast(pop(), Type.CHAR)); return null;
426 case I2S: push(new Cast(pop(), Type.SHORT)); return null;
427 case SWAP: { Expr e1 = pop(), e2 = pop(); push(e2); push(e1); return null; }
429 // Math //////////////////////////////////////////////////////////////////////////////
431 case IADD: case LADD: case FADD: case DADD: push(new Add(pop(), pop())); return null;
432 case ISUB: case LSUB: case FSUB: case DSUB: push(new Sub(pop(), pop())); return null;
433 case IMUL: case LMUL: case FMUL: case DMUL: push(new Mul(pop(), pop())); return null;
434 case IREM: case LREM: case FREM: case DREM: push(new Rem(pop(), pop())); return null;
435 //case INEG: case LNEG: case FNEG: case DNEG: push(new Neg(pop())); return null;
436 case IDIV: case LDIV: case FDIV: case DDIV: push(new Div(pop(), pop())); return null;
437 case ISHL: case LSHL: push(new Shl(pop(), pop())); return null;
438 case ISHR: case LSHR: push(new Shr(pop(), pop())); return null;
439 case IUSHR: case LUSHR: push(new Ushr(pop(), pop())); return null;
440 case IAND: case LAND: push(new And(pop(), pop())); return null;
441 case IOR: case LOR: push(new Or(pop(), pop())); return null;
442 case IXOR: case LXOR: push(new Xor(pop(), pop())); return null;
443 case IINC: return local[i1] = new Add(local[i1], new Constant(i2));
445 // Control and branching //////////////////////////////////////////////////////////////////////////////
447 case IFNULL: return new Branch(new Eq(pop(), new Constant(null)), new Label(i1));
448 case IFNONNULL: return new Branch(new Not(new Eq(pop(),new Constant(null))),new Label(i1));
449 case IFEQ: return new Branch( new Eq(new Constant(0), pop()), arg);
450 case IFNE: return new Branch(new Not(new Eq(new Constant(0), pop())), arg);
451 case IFLT: return new Branch( new Lt(new Constant(0), pop()), arg);
452 case IFGE: return new Branch(new Not(new Lt(new Constant(0), pop())), arg);
453 case IFGT: return new Branch( new Gt(new Constant(0), pop()), arg);
454 case IFLE: return new Branch(new Not(new Gt(new Constant(0), pop())), arg);
455 case IF_ICMPEQ: return new Branch( new Eq(pop(), pop()), arg);
456 case IF_ICMPNE: return new Branch(new Not(new Eq(pop(), pop())), arg);
457 case IF_ICMPLT: return new Branch( new Lt(pop(), pop()), arg);
458 case IF_ICMPGE: return new Branch(new Not(new Lt(pop(), pop())), arg);
459 case IF_ICMPGT: return new Branch( new Gt(pop(), pop()), arg);
460 case IF_ICMPLE: return new Branch(new Not(new Gt(pop(), pop())), arg);
461 case IF_ACMPEQ: return new Branch( new Eq(pop(), pop()), arg);
462 case IF_ACMPNE: return new Branch(new Not(new Eq(pop(), pop())), arg);
463 case ATHROW: return new Throw(pop());
464 case GOTO: return new Branch(new Label(i1));
465 case JSR: return new JSR(new Label(i1));
466 case RET: return new RET();
467 case RETURN: return new Return();
468 case IRETURN: case LRETURN: case FRETURN: case DRETURN: case ARETURN:
469 return new Return(pop());
471 // Array manipulations //////////////////////////////////////////////////////////////////////////////
473 case IALOAD: case LALOAD: case FALOAD: case DALOAD: case AALOAD:
474 case BALOAD: case CALOAD: case SALOAD: push(new ArrayGet(pop(), pop())); return null;
475 case IASTORE: case LASTORE: case FASTORE: case DASTORE: case AASTORE:
476 case BASTORE: case CASTORE: case SASTORE: return new ArrayPut(pop(), pop(), pop());
478 // Invocation //////////////////////////////////////////////////////////////////////////////
480 case INVOKEVIRTUAL: case INVOKESPECIAL: case INVOKESTATIC: case INVOKEINTERFACE: {
481 Type.Class.Method method = (Type.Class.Method)arg;
482 Expr args[] = new Expr[method.getNumArgs()];
483 for(int i=0; i<args.length; i++) args[args.length-i-1] = pop();
485 case INVOKEVIRTUAL: return push(new InvokeVirtual(method, args, pop()));
486 case INVOKEINTERFACE: return push(new InvokeInterface(method, args, pop()));
487 case INVOKESPECIAL: return push(new InvokeSpecial(method, args, pop()));
488 case INVOKESTATIC: return push(new InvokeStatic(method, args));
492 // Field Access //////////////////////////////////////////////////////////////////////////////
494 case GETSTATIC: push(new Get((Type.Class.Field)arg, null)); return null;
495 case PUTSTATIC: return new Put((Type.Class.Field)arg, pop(), null);
496 case GETFIELD: push(new Get((Type.Class.Field)arg, pop())); return null;
497 case PUTFIELD: return new Put((Type.Class.Field)arg, pop(), pop());
499 // Allocation //////////////////////////////////////////////////////////////////////////////
501 case NEW: push(new New((Type.Class)arg)); return null;
504 switch(((Integer)arg).intValue()) {
505 case 4: base = Type.BOOLEAN; break;
506 case 5: base = Type.CHAR; break;
507 case 6: base = Type.FLOAT; break;
508 case 7: base = Type.DOUBLE; break;
509 case 8: base = Type.BYTE; break;
510 case 9: base = Type.SHORT; break;
511 case 10: base = Type.INT; break;
512 case 11: base = Type.LONG; break;
513 default: throw new IllegalStateException("invalid array type");
515 push(new NewArray(base.makeArray(),pop()));
518 case ANEWARRAY: push(new NewArray(((Type.Ref)arg).makeArray(), pop())); return null;
519 case MULTIANEWARRAY: {
520 MethodGen.MultiANewArray mana = (MethodGen.MultiANewArray) arg;
521 Expr[] dims = new Expr[mana.dims];
522 for(int i=0;i<dims.length;i++) dims[i] = pop();
523 push(new NewArray(mana.type, dims));
526 case ARRAYLENGTH: push(new ArrayLength(pop())); return null;
528 // Runtime Type information //////////////////////////////////////////////////////////////////////////////
530 case CHECKCAST: push(new Cast(pop(), (Type)arg)); return null;
531 case INSTANCEOF: push(new InstanceOf(pop(), (Type)arg)); return null;
533 case LDC: case LDC_W: case LDC2_W: push(new Constant(arg)); return null;
535 case BIPUSH: push(new Constant(i1)); // FIXME return null;
536 case SIPUSH: push(new Constant(i1)); // FIXME return null;
538 case TABLESWITCH: new Branch((MethodGen.Switch)arg);
539 case LOOKUPSWITCH: new Branch((MethodGen.Switch)arg);
542 case MONITORENTER: Op.monitorEnter(pop());
543 case MONITOREXIT: Op.monitorExit(pop());
546 case DUP_X1: throw new Error("unimplemented");
547 case DUP_X2: throw new Error("unimplemented");
548 case DUP2_X1: throw new Error("unimplemented");
549 case DUP2_X2: throw new Error("unimplemented");
550 case LCMP: throw new Error("unimplemented");
551 case FCMPL: throw new Error("unimplemented");
552 case FCMPG: throw new Error("unimplemented");
553 case DCMPL: throw new Error("unimplemented");
554 case DCMPG: throw new Error("unimplemented");
555 case GOTO_W: throw new Error("unimplemented");
556 case JSR_W: throw new Error("unimplemented");
557 default: throw new Error("unhandled");
561 public static void main(String[] args) throws Exception {
562 InputStream is = Class.forName(args[0]).getClassLoader().getResourceAsStream(args[0].replace('.', '/')+".class");
563 System.out.println(new ClassFile(new DataInputStream(is), true).toString());