1 package org.ibex.classgen;
6 import org.ibex.classgen.util.*;
9 private final Hashtable entries = new Hashtable();
10 private int usedSlots = 1; // 0 is reserved
11 private int state = OPEN;
12 private static final int OPEN = 0;
13 private static final int STABLE = 1; // existing entries won't change
14 private static final int SEALED = 2; // no new entries
21 abstract static class Ent {
22 int n; // this is the refcount if state == OPEN, index if >= STABLE
25 Ent(int tag) { this.tag = tag; }
27 void dump(DataOutput o) throws IOException { o.writeByte(tag); }
28 String debugToString() { return toString(); } // so we can remove this method when not debugging
31 static class IntEnt extends Ent {
33 IntEnt(int tag) { super(tag); }
34 void dump(DataOutput o) throws IOException { super.dump(o); o.writeInt(i); }
37 static class LongEnt extends Ent {
39 LongEnt(int tag) { super(tag); }
40 void dump(DataOutput o) throws IOException { super.dump(o); o.writeLong(l); }
43 static class CPRefEnt extends Ent {
46 CPRefEnt(int tag) { super(tag); }
48 String debugToString() { return "[" + e1.n + ":" + e1.debugToString() + (e2 == null ? "" : " + " + e2.n + ":" + e2.debugToString()) + "]"; }
50 void dump(DataOutput o) throws IOException {
52 if(e1.n == 6 || (e2!=null && e2.n == 6)) System.err.println(debugToString() + " refs 6");
54 if(e2 != null) o.writeShort(e2.n);
58 static class Utf8Ent extends Ent {
60 Utf8Ent() { super(1); }
61 String debugToString() { return s; }
62 void dump(DataOutput o) throws IOException { super.dump(o); o.writeUTF(s); }
68 static class Utf8Key {
70 public Utf8Key(String s) { this.s = s; }
71 public boolean equals(Object o) { return o instanceof Utf8Key && ((Utf8Key)o).s.equals(s); }
72 public int hashCode() { return ~s.hashCode(); }
75 static class NameAndTypeKey {
78 NameAndTypeKey(String name, String type) { this.name = name; this.type = type; }
79 public boolean equals(Object o_) {
80 if(!(o_ instanceof NameAndTypeKey)) return false;
81 NameAndTypeKey o = (NameAndTypeKey) o_;
82 return o.name.equals(name) && o.type.equals(type);
84 public int hashCode() { return name.hashCode() ^ type.hashCode(); }
91 public final Ent get(Object o) { return (Ent) entries.get(o); }
92 public final Ent getUtf8(String s) { return get(new Utf8Key(s)); }
93 public final int getIndex(Object o) {
95 if(e == null) throw new IllegalStateException("entry not found");
98 public final int getUtf8Index(String s) {
100 if(e == null) throw new IllegalStateException("entry not found");
103 public final int getIndex(Ent ent) {
104 if(state < STABLE) throw new IllegalStateException("constant pool is not stable");
108 public final Ent addNameAndType(String name, String descriptor) { return add(new NameAndTypeKey(name,descriptor)); }
109 public final Ent addUtf8(String s) { return add(new Utf8Key(s)); }
111 public final Ent add(Object o) {
112 if(state == SEALED) throw new IllegalStateException("constant pool is sealed");
116 if(state == OPEN) ent.n++;
120 if(o instanceof Type.Object) {
121 CPRefEnt ce = new CPRefEnt(7);
122 ce.e1 = addUtf8(((Type.Object)o).internalForm());
124 } else if(o instanceof String) {
125 CPRefEnt ce = new CPRefEnt(8);
126 ce.e1 = addUtf8((String)o);
128 } else if(o instanceof Integer) {
129 IntEnt ue = new IntEnt(3);
130 ue.i = ((Integer)o).intValue();
132 } else if(o instanceof Float) {
133 IntEnt ue = new IntEnt(4);
134 ue.i = Float.floatToIntBits(((Float)o).floatValue());
136 } else if(o instanceof Long) {
137 LongEnt le = new LongEnt(5);
138 le.l = ((Long)o).longValue();
140 } else if(o instanceof Double) {
141 LongEnt le = new LongEnt(6);
142 le.l = Double.doubleToLongBits(((Double)o).doubleValue());
144 } else if(o instanceof Utf8Key) {
145 Utf8Ent ue = new Utf8Ent();
146 ue.s = ((Utf8Key)o).s;
148 } else if(o instanceof NameAndTypeKey) {
149 CPRefEnt ce = new CPRefEnt(12);
150 NameAndTypeKey key = (NameAndTypeKey) o;
151 ce.e1 = addUtf8(key.name);
152 ce.e2 = addUtf8(key.type);
154 } else if(o instanceof ClassGen.FieldOrMethodRef) {
155 ClassGen.FieldOrMethodRef key = (ClassGen.FieldOrMethodRef) o;
156 int tag = o instanceof FieldRef ? 9 : o instanceof MethodRef ? 10 : o instanceof MethodRef.I ? 11 : 0;
157 if(tag == 0) throw new Error("should never happen");
158 CPRefEnt ce = new CPRefEnt(tag);
159 ce.e1 = add(key.klass);
160 ce.e2 = addNameAndType(key.name,key.descriptor);
163 throw new IllegalArgumentException("Unknown type passed to add");
166 int spaces = ent instanceof LongEnt ? 2 : 1;
167 if(usedSlots + spaces > 65536) throw new ClassGen.Exn("constant pool full");
169 ent.n = state == OPEN ? 1 : usedSlots; // refcount or index
177 public int slots() { return usedSlots; }
179 public void seal() { state = SEALED; }
181 private Ent[] asArray() {
182 int count = entries.size();
183 Ent[] ents = new Ent[count];
185 Enumeration e = entries.keys();
186 while(e.hasMoreElements()) ents[i++] = (Ent) entries.get(e.nextElement());
187 if(i != count) throw new Error("should never happen");
191 private static void assignIndex(Ent[] ents) {
193 for(int i=0;i<ents.length;i++) {
196 index += ent instanceof LongEnt ? 2 : 1;
200 public void stable() {
201 if(state != OPEN) return;
203 assignIndex(asArray());
206 private static final Sort.CompareFunc compareFunc = new Sort.CompareFunc() {
207 public int compare(Object a_, Object b_) {
208 return ((Ent)a_).n - ((Ent)b_).n;
212 private static final Sort.CompareFunc reverseCompareFunc = new Sort.CompareFunc() {
213 public int compare(Object a_, Object b_) {
214 return ((Ent)b_).n - ((Ent)a_).n;
218 public void optimize() {
219 if(state != OPEN) throw new IllegalStateException("can't optimize a stable constant pool");
220 Ent[] ents = asArray();
221 Sort.sort(ents,reverseCompareFunc);
226 public void dump(DataOutput o) throws IOException {
227 Ent[] ents = asArray();
228 Sort.sort(ents,compareFunc);
229 for(int i=0;i<ents.length;i++) {
230 //System.err.println("" + ents[i].n + ": " + ents[i].debugToString());