X-Git-Url: http://git.megacz.com/?p=org.ibex.classgen.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fclassgen%2FCPGen.java;h=08bb904a9c180dd3e1893143e09485e6d48bf0c3;hp=25f22beccf1cf53d272efae836795f60632f33a9;hb=bb2af36ca9c48b45def87670c13dfbb80764e281;hpb=cd0ae78576749ddda5d9dfda5b9e88dd78fda0c9 diff --git a/src/org/ibex/classgen/CPGen.java b/src/org/ibex/classgen/CPGen.java index 25f22be..08bb904 100644 --- a/src/org/ibex/classgen/CPGen.java +++ b/src/org/ibex/classgen/CPGen.java @@ -20,13 +20,11 @@ class CPGen { /* * Entries */ - public abstract static class Ent { - CPGen owner; // don't need this yet, but we will to implement ref() and unref() + public abstract class Ent { int n; // this is the refcount if state == OPEN, index if >= STABLE int tag; - Ent e0; - Ent(CPGen owner, int tag) { this.owner = owner; this.tag = tag; } + Ent(int tag) { this.tag = tag; } void dump(DataOutput o) throws IOException { o.writeByte(tag); } String debugToString() { return toString(); } // so we can remove this method when not debugging @@ -34,9 +32,9 @@ class CPGen { } // INVARIANTS: tag == 3 || tag == 4 - static class IntEnt extends Ent { + class IntEnt extends Ent { int i; - IntEnt(CPGen owner, int tag) { super(owner,tag); } + IntEnt(int tag) { super(tag); } void dump(DataOutput o) throws IOException { super.dump(o); o.writeInt(i); } Object key() { switch(tag) { @@ -48,9 +46,9 @@ class CPGen { } // INVARIANTS: tag == 5 || tag == 6 - static class LongEnt extends Ent { + class LongEnt extends Ent { long l; - LongEnt(CPGen owner, int tag) { super(owner,tag); } + LongEnt(int tag) { super(tag); } void dump(DataOutput o) throws IOException { super.dump(o); o.writeLong(l); } Object key() { switch(tag) { @@ -72,10 +70,10 @@ class CPGen { e0 instanceof Utf8Ent } */ - static class CPRefEnt extends Ent { + class CPRefEnt extends Ent { Ent e1; Ent e2; - CPRefEnt(CPGen owner, int tag) { super(owner,tag); } + CPRefEnt(int tag) { super(tag); } String debugToString() { return "[" + e1.n + ":" + e1.debugToString() + (e2 == null ? "" : " + " + e2.n + ":" + e2.debugToString()) + "]"; } @@ -88,29 +86,35 @@ class CPGen { private String fixme() { throw new Error("fixme"); } Object key() throws ClassGen.ClassReadExn { switch(tag) { - case 7: return new Type.Object(((Utf8Ent)e0).s); + case 7: return Type.instance(((Utf8Ent)e1).s); case 8: return (((Utf8Ent)e1).s); case 9: { NameAndTypeKey nt = (NameAndTypeKey) e2.key(); - Type t = Type.fromDescriptor(nt.type); + Type t = Type.instance(nt.type); if(t == null) throw new ClassGen.ClassReadExn("invalid type descriptor"); - return new FieldRef((Type.Object)e1.key(), nt.name, t); + return new FieldRef((Type.Class)e1.key(), nt.name, t); } - case 10: { + case 10: case 11: { NameAndTypeKey nt = (NameAndTypeKey) e2.key(); - return new MethodRef((Type.Object)e1.key(), fixme(), null, null); + if (e1.key() == null) throw new Error(e1.tag + " => " + e1.key()); + return new MethodRef((Type.Class)e1.key(), "methodname", Type.VOID, new Type[0]); // FIXME FIXME + } + case 12: { + return new NameAndTypeKey(((Utf8Ent)e1).s, ((Utf8Ent)e2).s); } } - throw new Error("FIXME"); + throw new Error("FIXME " + tag); } } - static class Utf8Ent extends Ent { + class Utf8Ent extends Ent { String s; - Utf8Ent(CPGen owner) { super(owner,1); } + Utf8Ent() { super(1); } String debugToString() { return s; } void dump(DataOutput o) throws IOException { super.dump(o); o.writeUTF(s); } - Object key() { throw new Error("Brian is lame"); } + Object key() { + return s; + } } /* @@ -146,6 +150,9 @@ class CPGen { if(e == null) throw new IllegalStateException("entry not found"); return getIndex(e); } + public final String getUtf8ByIndex(int i) { + return ((Utf8Ent)getByIndex(i)).s; + } public final int getUtf8Index(String s) { Ent e = getUtf8(s); if(e == null) throw new IllegalStateException("entry not found"); @@ -156,7 +163,11 @@ class CPGen { return ent.n; } - public final Type.Object getType(int index) { return new Type.Object(((Utf8Ent)getByIndex(index)).s); } + public final Type getType(int index) throws ClassGen.ClassReadExn { + Ent e = getByIndex(index); + if (e instanceof Utf8Ent) return Type.instance(((Utf8Ent)e).s); + else return (Type)e.key(); + } public final Ent getByIndex(int index) { if(state < STABLE) throw new IllegalStateException("constant pool is not stable"); @@ -166,7 +177,7 @@ class CPGen { return e; } - public final Ent addNameAndType(String name, String descriptor) { return add(new NameAndTypeKey(name,descriptor)); } + public final Ent addNameAndType(String name, String descriptor) { return add(new NameAndTypeKey(name, descriptor)); } public final Ent addUtf8(String s) { return add(new Utf8Key(s)); } public final Ent add(Object o) { @@ -178,36 +189,36 @@ class CPGen { return ent; } - if(o instanceof Type.Object) { - CPRefEnt ce = new CPRefEnt(this,7); - ce.e1 = addUtf8(((Type.Object)o).internalForm()); + if(o instanceof Type.Class) { + CPRefEnt ce = new CPRefEnt(7); + ce.e1 = addUtf8(((Type.Class)o).internalForm()); ent = ce; } else if(o instanceof String) { - CPRefEnt ce = new CPRefEnt(this,8); + CPRefEnt ce = new CPRefEnt(8); ce.e1 = addUtf8((String)o); ent = ce; } else if(o instanceof Integer) { - IntEnt ue = new IntEnt(this,3); + IntEnt ue = new IntEnt(3); ue.i = ((Integer)o).intValue(); ent = ue; } else if(o instanceof Float) { - IntEnt ue = new IntEnt(this,4); + IntEnt ue = new IntEnt(4); ue.i = Float.floatToIntBits(((Float)o).floatValue()); ent = ue; } else if(o instanceof Long) { - LongEnt le = new LongEnt(this,5); + LongEnt le = new LongEnt(5); le.l = ((Long)o).longValue(); ent = le; } else if(o instanceof Double) { - LongEnt le = new LongEnt(this,6); + LongEnt le = new LongEnt(6); le.l = Double.doubleToLongBits(((Double)o).doubleValue()); ent = le; } else if(o instanceof Utf8Key) { - Utf8Ent ue = new Utf8Ent(this); + Utf8Ent ue = new Utf8Ent(); ue.s = ((Utf8Key)o).s; ent = ue; } else if(o instanceof NameAndTypeKey) { - CPRefEnt ce = new CPRefEnt(this,12); + CPRefEnt ce = new CPRefEnt(12); NameAndTypeKey key = (NameAndTypeKey) o; ce.e1 = addUtf8(key.name); ce.e2 = addUtf8(key.type); @@ -216,9 +227,9 @@ class CPGen { ClassGen.FieldOrMethodRef key = (ClassGen.FieldOrMethodRef) o; int tag = o instanceof FieldRef ? 9 : o instanceof MethodRef ? 10 : o instanceof MethodRef.I ? 11 : 0; if(tag == 0) throw new Error("should never happen"); - CPRefEnt ce = new CPRefEnt(this,tag); + CPRefEnt ce = new CPRefEnt(tag); ce.e1 = add(key.klass); - ce.e2 = addNameAndType(key.name,key.descriptor); + ce.e2 = addNameAndType(key.name, key.descriptor); ent = ce; } else { throw new IllegalArgumentException("Unknown type passed to add"); @@ -231,7 +242,7 @@ class CPGen { usedSlots += spaces; - entries.put(o,ent); + entries.put(o, ent); return ent; } @@ -281,7 +292,7 @@ class CPGen { public void optimize() { if(state != OPEN) throw new IllegalStateException("can't optimize a stable constant pool"); Ent[] ents = asArray(); - Sort.sort(ents,reverseCompareFunc); + Sort.sort(ents, reverseCompareFunc); state = STABLE; assignIndex(ents); } @@ -299,7 +310,7 @@ class CPGen { public void dump(DataOutput o) throws IOException { Ent[] ents = asArray(); - Sort.sort(ents,compareFunc); + Sort.sort(ents, compareFunc); o.writeShort(usedSlots); for(int i=0;i= usedSlots) throw new ClassGen.ClassReadExn("invalid cp index"); - ce.e0 = entriesByIndex[e0s[i]]; - if(ce.e0 == null) throw new ClassGen.ClassReadExn("invalid cp index"); - if(ce.tag != 7 && ce.tag != 8) { - if(e1s[i] == 0 || e1s[i] >= usedSlots) throw new ClassGen.ClassReadExn("invalid cp index"); - ce.e1 = entriesByIndex[e1s[i]]; - if(ce.e1 == null) throw new ClassGen.ClassReadExn("invalid cp index"); - } - switch(ce.tag) { - case 7: - case 8: - if(!(ce.e0 instanceof Utf8Ent)) throw new ClassGen.ClassReadExn("expected a utf8 ent"); - break; - case 9: - case 10: - case 11: - if(!(ce.e1 instanceof CPRefEnt) || ((CPRefEnt)ce.e1).tag != 7) - throw new ClassGen.ClassReadExn("expected a type ent"); - if(!(ce.e2 instanceof CPRefEnt) || ((CPRefEnt)ce.e2).tag != 12) - throw new ClassGen.ClassReadExn("expected a name and type ent"); - break; - case 12: - if(!(ce.e1 instanceof Utf8Ent)) throw new ClassGen.ClassReadExn("expected a utf8 ent"); - if(!(ce.e2 instanceof Utf8Ent)) throw new ClassGen.ClassReadExn("expected a utf8 ent"); - } - } else if(e instanceof LongEnt) { + if (e == null) throw new Error("should never happen: " + i + "/"+usedSlots); + if (e instanceof LongEnt) { index++; + continue; } - entries.put(e.key(),e); + if (!(e instanceof CPRefEnt)) continue; + CPRefEnt ce = (CPRefEnt) e; + if(e1s[i] == 0 || e1s[i] >= usedSlots) throw new ClassGen.ClassReadExn("invalid cp index"); + ce.e1 = entriesByIndex[e1s[i]]; + if(ce.e1 == null) throw new ClassGen.ClassReadExn("invalid cp index"); + if(ce.tag != 7 && ce.tag != 8) { + if(e2s[i] == 0 || e2s[i] >= usedSlots) throw new ClassGen.ClassReadExn("invalid cp index"); + ce.e2 = entriesByIndex[e2s[i]]; + if(ce.e2 == null) throw new ClassGen.ClassReadExn("invalid cp index"); + } + switch(ce.tag) { + case 7: + case 8: + if(!(ce.e1 instanceof Utf8Ent)) throw new ClassGen.ClassReadExn("expected a utf8 ent"); + break; + case 9: + case 10: + case 11: + if(!(ce.e1 instanceof CPRefEnt) || ((CPRefEnt)ce.e1).tag != 7) + throw new ClassGen.ClassReadExn("expected a type ent"); + if(!(ce.e2 instanceof CPRefEnt) || ((CPRefEnt)ce.e2).tag != 12) + throw new ClassGen.ClassReadExn("expected a name and type ent"); + break; + case 12: + if(!(ce.e1 instanceof Utf8Ent)) throw new ClassGen.ClassReadExn("expected a utf8 ent"); + if(!(ce.e2 instanceof Utf8Ent)) throw new ClassGen.ClassReadExn("expected a utf8 ent"); + break; + } + } + for(int i=1; i