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=c41d67d2ccc0444dc193ed793fb0b59151e518bd;hb=bb2af36ca9c48b45def87670c13dfbb80764e281;hpb=161a72a3b197b7f1c84954475aac89a7b191745d diff --git a/src/org/ibex/classgen/CPGen.java b/src/org/ibex/classgen/CPGen.java index c41d67d..08bb904 100644 --- a/src/org/ibex/classgen/CPGen.java +++ b/src/org/ibex/classgen/CPGen.java @@ -7,6 +7,8 @@ import org.ibex.classgen.util.*; class CPGen { private final Hashtable entries = new Hashtable(); + private Ent[] entriesByIndex; // only valid when stable + private int usedSlots = 1; // 0 is reserved private int state = OPEN; private static final int OPEN = 0; @@ -18,7 +20,7 @@ class CPGen { /* * Entries */ - abstract static class Ent { + public abstract class Ent { int n; // this is the refcount if state == OPEN, index if >= STABLE int tag; @@ -26,21 +28,49 @@ class CPGen { void dump(DataOutput o) throws IOException { o.writeByte(tag); } String debugToString() { return toString(); } // so we can remove this method when not debugging + abstract Object key() throws ClassGen.ClassReadExn; // be careful using this, it drags in a bunch of code } - static class IntEnt extends Ent { + // INVARIANTS: tag == 3 || tag == 4 + class IntEnt extends Ent { int i; IntEnt(int tag) { super(tag); } void dump(DataOutput o) throws IOException { super.dump(o); o.writeInt(i); } + Object key() { + switch(tag) { + case 3: return new Integer(i); + case 4: return new Float(Float.intBitsToFloat(i)); + default: throw new Error("should never happen"); + } + } } - static class LongEnt extends Ent { + // INVARIANTS: tag == 5 || tag == 6 + class LongEnt extends Ent { long l; LongEnt(int tag) { super(tag); } void dump(DataOutput o) throws IOException { super.dump(o); o.writeLong(l); } + Object key() { + switch(tag) { + case 5: return new Long(l); + case 6: return new Double(Double.longBitsToDouble(l)); + default: throw new Error("should never happen"); + } + } } - static class CPRefEnt extends Ent { + /* INVARIANTS: + tag >= 7 && tag <= 12 + if(tag == 7 || tag == 8) e0 instanceof Utf8Ent + if(tag == 9 || tag == 10 || tag == 11) { + e0 instanceof CPRefEnt && e0.tag == 7 + e1 instanceof CPRefEnt && e0.tag == 12 + } + if(tag == 12) { + e0 instanceof Utf8Ent + } + */ + class CPRefEnt extends Ent { Ent e1; Ent e2; CPRefEnt(int tag) { super(tag); } @@ -49,30 +79,55 @@ class CPGen { void dump(DataOutput o) throws IOException { super.dump(o); - if(e1.n == 6 || (e2!=null && e2.n == 6)) System.err.println(debugToString() + " refs 6"); o.writeShort(e1.n); if(e2 != null) o.writeShort(e2.n); } + + private String fixme() { throw new Error("fixme"); } + Object key() throws ClassGen.ClassReadExn { + switch(tag) { + case 7: return Type.instance(((Utf8Ent)e1).s); + case 8: return (((Utf8Ent)e1).s); + case 9: { + NameAndTypeKey nt = (NameAndTypeKey) e2.key(); + Type t = Type.instance(nt.type); + if(t == null) throw new ClassGen.ClassReadExn("invalid type descriptor"); + return new FieldRef((Type.Class)e1.key(), nt.name, t); + } + case 10: case 11: { + NameAndTypeKey nt = (NameAndTypeKey) e2.key(); + 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 " + tag); + } } - static class Utf8Ent extends Ent { + class Utf8Ent extends Ent { String s; Utf8Ent() { super(1); } String debugToString() { return s; } void dump(DataOutput o) throws IOException { super.dump(o); o.writeUTF(s); } + Object key() { + return s; + } } /* * Cache Keys */ - static class Utf8Key { + public static class Utf8Key { String s; public Utf8Key(String s) { this.s = s; } public boolean equals(Object o) { return o instanceof Utf8Key && ((Utf8Key)o).s.equals(s); } public int hashCode() { return ~s.hashCode(); } } - static class NameAndTypeKey { + public static class NameAndTypeKey { String name; String type; NameAndTypeKey(String name, String type) { this.name = name; this.type = type; } @@ -95,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"); @@ -104,8 +162,22 @@ class CPGen { if(state < STABLE) throw new IllegalStateException("constant pool is not stable"); return ent.n; } + + 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"); + Ent e; + if(index >= 65536 || index >= entriesByIndex.length || (e = entriesByIndex[index]) == null) + throw new IllegalStateException("invalid cp index"); + 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) { @@ -117,9 +189,9 @@ class CPGen { return ent; } - if(o instanceof Type.Object) { + if(o instanceof Type.Class) { CPRefEnt ce = new CPRefEnt(7); - ce.e1 = addUtf8(((Type.Object)o).internalForm()); + ce.e1 = addUtf8(((Type.Class)o).internalForm()); ent = ce; } else if(o instanceof String) { CPRefEnt ce = new CPRefEnt(8); @@ -157,7 +229,7 @@ class CPGen { if(tag == 0) throw new Error("should never happen"); 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"); @@ -170,7 +242,7 @@ class CPGen { usedSlots += spaces; - entries.put(o,ent); + entries.put(o, ent); return ent; } @@ -188,11 +260,13 @@ class CPGen { return ents; } - private static void assignIndex(Ent[] ents) { + private void assignIndex(Ent[] ents) { int index = 1; + entriesByIndex = new Ent[ents.length*2]; for(int i=0;i " + ents[i].debugToString() + " (" + ents[i].n + ")"); + Sort.sort(ents, reverseCompareFunc); state = STABLE; assignIndex(ents); } + public void unsafeReopen() { + if(state == OPEN) return; + for(int i=1;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