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
14 private boolean sealed;
21 abstract static class Ent implements Sort.Comparable {
25 Ent(int tag) { this.tag = tag; }
27 int getIndex() { return index; }
29 void dump(DataOutput o) throws IOException { o.writeByte(tag); }
31 public int compareTo(Object o) {
32 if(!(o instanceof Ent)) return 1;
33 int oi = ((Ent)o).index;
34 if(index < oi) return -1;
35 if(index > oi) return 1;
40 static class OneU4Ent extends Ent {
42 OneU4Ent(int tag) { super(tag); }
43 void dump(DataOutput o) throws IOException { super.dump(o); o.writeInt(i); }
46 static class LongEnt extends Ent {
48 LongEnt(int tag) { super(tag); }
49 void dump(DataOutput o) throws IOException { super.dump(o); o.writeLong(l); }
52 static class CPRefEnt extends Ent {
55 CPRefEnt(int tag) { super(tag); }
56 void dump(DataOutput o) throws IOException {
58 o.writeShort(e1.index);
59 if(e2 != null) o.writeShort(e2.index);
63 static class Utf8Ent extends Ent {
65 Utf8Ent() { super(1); }
66 void dump(DataOutput o) throws IOException { super.dump(o); o.writeUTF(s); }
72 static class Utf8Key {
74 public Utf8Key(String s) { this.s = s; }
75 public boolean equals(Object o) { return o instanceof Utf8Key && ((Utf8Key)o).s.equals(s); }
76 public int hashCode() { return ~s.hashCode(); }
82 public void seal() { sealed = true; }
84 public final Ent get(Object o) { return (Ent) entries.get(o); }
85 public final Ent getUtf8(String s) { return get(new Utf8Key(s)); }
86 public final int getIndex(Object o) {
88 if(e == null) throw new IllegalStateException("entry not found");
91 public final int getUtf8Index(String s) {
93 if(e == null) throw new IllegalStateException("entry not found");
97 public final Ent addNameAndType(String name, String descriptor) { return add(new ClassGen.NameAndType(name,descriptor)); }
98 public final Ent addUtf8(String s) { return add(new Utf8Key(s)); }
100 // FEATURE: Don't resolve indexes until dump (for optimize)
101 public final Ent add(Object o) {
102 if(sealed) throw new IllegalStateException("constant pool is sealed");
105 if(ent != null) return ent;
107 if(nextIndex == 65536) throw new ClassGen.Exn("constant pool full");
109 if(o instanceof Type.Object) {
110 CPRefEnt ce = new CPRefEnt(7);
111 ce.e1 = addUtf8(((Type.Object)o).internalForm());
113 } else if(o instanceof String) {
114 CPRefEnt ce = new CPRefEnt(8);
115 ce.e1 = addUtf8((String)o);
117 } else if(o instanceof Integer) {
118 OneU4Ent ue = new OneU4Ent(3);
119 ue.i = ((Integer)o).intValue();
121 } else if(o instanceof Float) {
122 OneU4Ent ue = new OneU4Ent(4);
123 ue.i = Float.floatToIntBits(((Float)o).floatValue());
125 } else if(o instanceof Long) {
126 LongEnt le = new LongEnt(5);
127 le.l = ((Long)o).longValue();
129 } else if(o instanceof Double) {
130 LongEnt le = new LongEnt(6);
131 le.l = Double.doubleToLongBits(((Double)o).doubleValue());
133 } else if(o instanceof Utf8Key) {
134 Utf8Ent ue = new Utf8Ent();
135 ue.s = ((Utf8Key)o).s;
137 } else if(o instanceof ClassGen.NameAndType) {
138 CPRefEnt ce = new CPRefEnt(12);
139 ClassGen.NameAndType key = (ClassGen.NameAndType) o;
140 ce.e1 = addUtf8(key.name);
141 ce.e2 = addUtf8(key.type);
143 } else if(o instanceof ClassGen.FieldMethodRef) {
144 ClassGen.FieldMethodRef key = (ClassGen.FieldMethodRef) o;
145 int tag = o instanceof FieldRef ? 9 : o instanceof MethodRef ? 10 : o instanceof ClassGen.InterfaceMethodRef ? 11 : 0;
146 if(tag == 0) throw new Error("should never happen");
147 CPRefEnt ce = new CPRefEnt(tag);
148 ce.e1 = add(key.klass);
149 ce.e2 = add(key.nameAndType);
152 throw new IllegalArgumentException("Unknown type passed to add");
155 ent.index = nextIndex++;
156 if(ent instanceof LongEnt) nextIndex++;
163 public int size() { return nextIndex; }
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");
172 for(i=0;i<ents.length;i++) {
173 System.err.println("" + (i+1) + ": " + ents[i]);