X-Git-Url: http://git.megacz.com/?p=org.ibex.classgen.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fclassgen%2FCPGen.java;h=1919caee6c61fb075bc1cc3361516caa2d42f675;hp=dece6c2a4a449707a6aed1fe6caedb63483071cd;hb=017306c392932fe7db2f1123b1bf733832a03419;hpb=424a26b0bce440b239eb4977304dd7ed89e37ec8 diff --git a/src/org/ibex/classgen/CPGen.java b/src/org/ibex/classgen/CPGen.java index dece6c2..1919cae 100644 --- a/src/org/ibex/classgen/CPGen.java +++ b/src/org/ibex/classgen/CPGen.java @@ -11,7 +11,10 @@ class CPGen { private Hashtable entries = new Hashtable(); private int nextIndex = 1; // 0 is reserved private int count; - private boolean sealed; + private int state; + private static final int OPEN = 0; + private static final int STABLE = 1; // existing entries won't change + private static final int SEALED = 2; // no new entries CPGen() { } @@ -68,10 +71,23 @@ class CPGen { public int hashCode() { return ~s.hashCode(); } } + static class NameAndTypeKey { + String name; + String type; + NameAndTypeKey(String name, String type) { this.name = name; this.type = type; } + public boolean equals(Object o_) { + if(!(o_ instanceof NameAndTypeKey)) return false; + NameAndTypeKey o = (NameAndTypeKey) o_; + return o.name.equals(name) && o.type.equals(type); + } + public int hashCode() { return name.hashCode() ^ type.hashCode(); } + } + /* * Methods */ - public void seal() { sealed = true; } + public void seal() { if(state > SEALED) throw new IllegalStateException(); state = SEALED; } + public void stable() { if(state > STABLE) throw new IllegalStateException(); state = STABLE; } public final Ent get(Object o) { return (Ent) entries.get(o); } public final Ent getUtf8(String s) { return get(new Utf8Key(s)); } @@ -86,18 +102,15 @@ class CPGen { return e.getIndex(); } - public final Ent addNameAndType(String name, String descriptor) { return add(new ClassGen.NameAndType(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)); } - // FEATURE: Don't resolve indexes until dump (for optimize) public final Ent add(Object o) { - if(sealed) throw new IllegalStateException("constant pool is sealed"); + if(state == SEALED) throw new IllegalStateException("constant pool is sealed"); Ent ent = get(o); if(ent != null) return ent; - if(nextIndex == 65536) throw new ClassGen.Exn("constant pool full"); - if(o instanceof Type.Object) { CPRefEnt ce = new CPRefEnt(7); ce.e1 = addUtf8(((Type.Object)o).internalForm()); @@ -126,26 +139,30 @@ class CPGen { Utf8Ent ue = new Utf8Ent(); ue.s = ((Utf8Key)o).s; ent = ue; - } else if(o instanceof ClassGen.NameAndType) { + } else if(o instanceof NameAndTypeKey) { CPRefEnt ce = new CPRefEnt(12); - ClassGen.NameAndType key = (ClassGen.NameAndType) o; + NameAndTypeKey key = (NameAndTypeKey) o; ce.e1 = addUtf8(key.name); ce.e2 = addUtf8(key.type); ent = ce; - } else if(o instanceof ClassGen.FieldMethodRef) { - ClassGen.FieldMethodRef key = (ClassGen.FieldMethodRef) o; - int tag = o instanceof FieldRef ? 9 : o instanceof MethodRef ? 10 : o instanceof ClassGen.InterfaceMethodRef ? 11 : 0; + } else if(o instanceof ClassGen.FieldOrMethodRef) { + 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(tag); ce.e1 = add(key.klass); - ce.e2 = add(key.nameAndType); + ce.e2 = addNameAndType(key.name,key.descriptor); ent = ce; } else { throw new IllegalArgumentException("Unknown type passed to add"); } - ent.index = nextIndex++; - if(ent instanceof LongEnt) nextIndex++; + int spaces = ent instanceof LongEnt ? 2 : 1; + + if(nextIndex + spaces > 65536) throw new ClassGen.Exn("constant pool full"); + + ent.index = nextIndex; + nextIndex += spaces; count++; entries.put(o,ent);