X-Git-Url: http://git.megacz.com/?p=org.ibex.classgen.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fclassgen%2FJSSA.java;h=193cce9719ad0792e487c4faab06b31da2c6f649;hp=b8e57cad0002a9059dc268a8eb73174b138ea85c;hb=cb5850067f4640e4df632ac7b2737acbb0c6c9b6;hpb=20ae2b199fd34e70f56572caaeb38738ea1f96fc diff --git a/src/org/ibex/classgen/JSSA.java b/src/org/ibex/classgen/JSSA.java index b8e57ca..193cce9 100644 --- a/src/org/ibex/classgen/JSSA.java +++ b/src/org/ibex/classgen/JSSA.java @@ -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"); } } + 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 { @@ -236,13 +318,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 */ @@ -355,8 +435,8 @@ 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; + throw new IllegalStateException("unknown constant type"); } } @@ -523,8 +603,8 @@ public class JSSA extends MethodGen implements CGConst { // 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: push(new Cast(pop(), (Type.Ref)arg)); return null; + 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;