X-Git-Url: http://git.megacz.com/?p=org.ibex.classgen.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fclassgen%2FJSSA.java;h=55ed8dbb06cef09f087632e4486c6b1e9e358f85;hp=55c67938e0f88527c6f5cd4cf7c20fabfbcd21e8;hb=5186b123d341165e00082608ad38500f076f90f8;hpb=505ea0c18222ad300c6886dece45c6c0a2790856 diff --git a/src/org/ibex/classgen/JSSA.java b/src/org/ibex/classgen/JSSA.java index 55c6793..55ed8db 100644 --- a/src/org/ibex/classgen/JSSA.java +++ b/src/org/ibex/classgen/JSSA.java @@ -13,45 +13,26 @@ public class JSSA extends MethodGen implements CGConst { public JSSA(Type.Class c, DataInput in, ConstantPool cp) throws IOException { super(c, in, cp); - for(int i=0; i"); } } + 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 { @@ -224,6 +295,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 { @@ -232,15 +304,29 @@ public class JSSA extends MethodGen implements CGConst { public NewArray(Type.Array t, Expr[] dims) { this.t = t; this.dims = dims; } public NewArray(Type.Array t, Expr dim) { this(t,new Expr[]{dim}); } public Type getType() { return t; } + public String _toString() { + Type base = t; + int totalDims = 0; + while(base.isArray()) { + totalDims++; + base = base.asArray().getElementType(); + } + StringBuffer sb = new StringBuffer("new " + base); + for(int i=0;i") ? "super" : method.name); - args(sb); - return sb.toString(); - } + public String _toString() { return _toString(method.name.equals("") ? 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(); } @@ -344,8 +429,9 @@ 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 == null ? "null" : o instanceof String ? "\"" + o + "\"" : o.toString(); } public Type getType() { + if (o == null) return Type.NULL; if (o instanceof Byte) return Type.BYTE; if (o instanceof Short) return Type.SHORT; if (o instanceof Character) return Type.CHAR; @@ -353,8 +439,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"); } } @@ -383,28 +470,28 @@ public class JSSA extends MethodGen implements CGConst { // Stack manipulations ////////////////////////////////////////////////////////////////////////////// - case ACONST_NULL: return stack[sp++] = new Constant(null); - case ICONST_M1: return stack[sp++] = new Constant(-1); + case ACONST_NULL: push(new Constant(null)); return null; + case ICONST_M1: push(new Constant(-1)); return null; case ICONST_0: case LCONST_0: case FCONST_0: case DCONST_0: push(new Constant(0)); return null; case ICONST_1: case LCONST_1: case FCONST_1: case DCONST_1: push(new Constant(1)); return null; case ICONST_2: case FCONST_2: push(new Constant(2)); return null; case ICONST_3: push(new Constant(3)); return null; case ICONST_4: push(new Constant(4)); return null; case ICONST_5: push(new Constant(5)); return null; - case ILOAD: case LLOAD: case FLOAD: case DLOAD: case ALOAD: return push(local[i1]); - case ILOAD_0: case LLOAD_0: case FLOAD_0: case DLOAD_0: case ALOAD_0: return push(local[0]); - case ILOAD_1: case LLOAD_1: case FLOAD_1: case DLOAD_1: case ALOAD_1: return push(local[1]); - case ALOAD_2: case DLOAD_2: case FLOAD_2: case LLOAD_2: case ILOAD_2: return push(local[2]); - case ILOAD_3: case LLOAD_3: case FLOAD_3: case DLOAD_3: case ALOAD_3: return push(local[3]); + case ILOAD: case LLOAD: case FLOAD: case DLOAD: case ALOAD: push(local[i1]); return null; + case ILOAD_0: case LLOAD_0: case FLOAD_0: case DLOAD_0: case ALOAD_0: push(local[0]); return null; + case ILOAD_1: case LLOAD_1: case FLOAD_1: case DLOAD_1: case ALOAD_1: push(local[1]); return null; + case ALOAD_2: case DLOAD_2: case FLOAD_2: case LLOAD_2: case ILOAD_2: push(local[2]); return null; + case ILOAD_3: case LLOAD_3: case FLOAD_3: case DLOAD_3: case ALOAD_3: push(local[3]); return null; case ISTORE: case LSTORE: case FSTORE: case DSTORE: case ASTORE: local[i1] = pop(); return null; case ISTORE_0: case LSTORE_0: case FSTORE_0: case DSTORE_0: case ASTORE_0: local[0] = pop(); return null; 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 ////////////////////////////////////////////////////////////////////////////// @@ -465,9 +552,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 ////////////////////////////////////////////////////////////////////////////// @@ -475,19 +564,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