1 package org.ibex.classgen;
6 import org.ibex.classgen.util.*;
8 // FEATURE: Add a "hit count" to each entry and optimize the table
11 private Hashtable entries = new Hashtable();
12 private int nextIndex = 1; // 0 is reserved
15 private static final int OPEN = 0;
16 private static final int STABLE = 1; // existing entries won't change
17 private static final int SEALED = 2; // no new entries
24 abstract static class Ent {
28 Ent(int tag) { this.tag = tag; }
30 int getIndex() { return index; }
32 void dump(DataOutput o) throws IOException { o.writeByte(tag); }
35 static class OneU4Ent extends Ent {
37 OneU4Ent(int tag) { super(tag); }
38 void dump(DataOutput o) throws IOException { super.dump(o); o.writeInt(i); }
41 static class LongEnt extends Ent {
43 LongEnt(int tag) { super(tag); }
44 void dump(DataOutput o) throws IOException { super.dump(o); o.writeLong(l); }
47 static class CPRefEnt extends Ent {
50 CPRefEnt(int tag) { super(tag); }
51 void dump(DataOutput o) throws IOException {
53 o.writeShort(e1.index);
54 if(e2 != null) o.writeShort(e2.index);
58 static class Utf8Ent extends Ent {
60 Utf8Ent() { super(1); }
61 void dump(DataOutput o) throws IOException { super.dump(o); o.writeUTF(s); }
67 static class Utf8Key {
69 public Utf8Key(String s) { this.s = s; }
70 public boolean equals(Object o) { return o instanceof Utf8Key && ((Utf8Key)o).s.equals(s); }
71 public int hashCode() { return ~s.hashCode(); }
77 public void seal() { if(state >= SEALED) throw new IllegalStateException(); state = SEALED; }
78 public void stable() { if(state >= STABLE) throw new IllegalStateException(); state = STABLE; }
80 public final Ent get(Object o) { return (Ent) entries.get(o); }
81 public final Ent getUtf8(String s) { return get(new Utf8Key(s)); }
82 public final int getIndex(Object o) {
84 if(e == null) throw new IllegalStateException("entry not found");
87 public final int getUtf8Index(String s) {
89 if(e == null) throw new IllegalStateException("entry not found");
93 public final Ent addNameAndType(String name, String descriptor) { return add(new ClassGen.NameAndType(name,descriptor)); }
94 public final Ent addUtf8(String s) { return add(new Utf8Key(s)); }
96 public final Ent add(Object o) {
97 if(state == SEALED) throw new IllegalStateException("constant pool is sealed");
100 if(ent != null) return ent;
102 if(nextIndex == 65536) throw new ClassGen.Exn("constant pool full");
104 if(o instanceof Type.Object) {
105 CPRefEnt ce = new CPRefEnt(7);
106 ce.e1 = addUtf8(((Type.Object)o).internalForm());
108 } else if(o instanceof String) {
109 CPRefEnt ce = new CPRefEnt(8);
110 ce.e1 = addUtf8((String)o);
112 } else if(o instanceof Integer) {
113 OneU4Ent ue = new OneU4Ent(3);
114 ue.i = ((Integer)o).intValue();
116 } else if(o instanceof Float) {
117 OneU4Ent ue = new OneU4Ent(4);
118 ue.i = Float.floatToIntBits(((Float)o).floatValue());
120 } else if(o instanceof Long) {
121 LongEnt le = new LongEnt(5);
122 le.l = ((Long)o).longValue();
124 } else if(o instanceof Double) {
125 LongEnt le = new LongEnt(6);
126 le.l = Double.doubleToLongBits(((Double)o).doubleValue());
128 } else if(o instanceof Utf8Key) {
129 Utf8Ent ue = new Utf8Ent();
130 ue.s = ((Utf8Key)o).s;
132 } else if(o instanceof ClassGen.NameAndType) {
133 CPRefEnt ce = new CPRefEnt(12);
134 ClassGen.NameAndType key = (ClassGen.NameAndType) o;
135 ce.e1 = addUtf8(key.name);
136 ce.e2 = addUtf8(key.type);
138 } else if(o instanceof ClassGen.FieldMethodRef) {
139 ClassGen.FieldMethodRef key = (ClassGen.FieldMethodRef) o;
140 int tag = o instanceof FieldRef ? 9 : o instanceof MethodRef ? 10 : o instanceof ClassGen.InterfaceMethodRef ? 11 : 0;
141 if(tag == 0) throw new Error("should never happen");
142 CPRefEnt ce = new CPRefEnt(tag);
143 ce.e1 = add(key.klass);
144 ce.e2 = add(key.nameAndType);
147 throw new IllegalArgumentException("Unknown type passed to add");
150 ent.index = nextIndex++;
151 if(ent instanceof LongEnt) nextIndex++;
158 public int size() { return nextIndex; }
160 private static final Sort.CompareFunc compareFunc = new Sort.CompareFunc() {
161 public int compare(Object a_, Object b_) {
162 return ((Ent)a_).index - ((Ent)b_).index;
165 public void dump(DataOutput o) throws IOException {
166 Ent[] ents = new Ent[count];
168 Enumeration e = entries.keys();
169 while(e.hasMoreElements()) ents[i++] = (Ent) entries.get(e.nextElement());
170 if(i != count) throw new Error("should never happen");
171 Sort.sort(ents,compareFunc);
172 for(i=0;i<ents.length;i++) {
173 //System.err.println("" + (i+1) + ": " + ents[i]);