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 {
25 Ent(int tag) { this.tag = tag; }
27 int getIndex() { return index; }
29 void dump(DataOutput o) throws IOException { o.writeByte(tag); }
32 static class OneU4Ent extends Ent {
34 OneU4Ent(int tag) { super(tag); }
35 void dump(DataOutput o) throws IOException { super.dump(o); o.writeInt(i); }
38 static class LongEnt extends Ent {
40 LongEnt(int tag) { super(tag); }
41 void dump(DataOutput o) throws IOException { super.dump(o); o.writeLong(l); }
44 static class CPRefEnt extends Ent {
47 CPRefEnt(int tag) { super(tag); }
48 void dump(DataOutput o) throws IOException {
50 o.writeShort(e1.index);
51 if(e2 != null) o.writeShort(e2.index);
55 static class Utf8Ent extends Ent {
57 Utf8Ent() { super(1); }
58 void dump(DataOutput o) throws IOException { super.dump(o); o.writeUTF(s); }
64 static class Utf8Key {
66 public Utf8Key(String s) { this.s = s; }
67 public boolean equals(Object o) { return o instanceof Utf8Key && ((Utf8Key)o).s.equals(s); }
68 public int hashCode() { return ~s.hashCode(); }
74 public void seal() { sealed = true; }
76 public final Ent get(Object o) { return (Ent) entries.get(o); }
77 public final Ent getUtf8(String s) { return get(new Utf8Key(s)); }
78 public final int getIndex(Object o) {
80 if(e == null) throw new IllegalStateException("entry not found");
83 public final int getUtf8Index(String s) {
85 if(e == null) throw new IllegalStateException("entry not found");
89 public final Ent addNameAndType(String name, String descriptor) { return add(new ClassGen.NameAndType(name,descriptor)); }
90 public final Ent addUtf8(String s) { return add(new Utf8Key(s)); }
92 // FEATURE: Don't resolve indexes until dump (for optimize)
93 public final Ent add(Object o) {
94 if(sealed) throw new IllegalStateException("constant pool is sealed");
97 if(ent != null) return ent;
99 if(nextIndex == 65536) throw new ClassGen.Exn("constant pool full");
101 if(o instanceof Type.Object) {
102 CPRefEnt ce = new CPRefEnt(7);
103 ce.e1 = addUtf8(((Type.Object)o).internalForm());
105 } else if(o instanceof String) {
106 CPRefEnt ce = new CPRefEnt(8);
107 ce.e1 = addUtf8((String)o);
109 } else if(o instanceof Integer) {
110 OneU4Ent ue = new OneU4Ent(3);
111 ue.i = ((Integer)o).intValue();
113 } else if(o instanceof Float) {
114 OneU4Ent ue = new OneU4Ent(4);
115 ue.i = Float.floatToIntBits(((Float)o).floatValue());
117 } else if(o instanceof Long) {
118 LongEnt le = new LongEnt(5);
119 le.l = ((Long)o).longValue();
121 } else if(o instanceof Double) {
122 LongEnt le = new LongEnt(6);
123 le.l = Double.doubleToLongBits(((Double)o).doubleValue());
125 } else if(o instanceof Utf8Key) {
126 Utf8Ent ue = new Utf8Ent();
127 ue.s = ((Utf8Key)o).s;
129 } else if(o instanceof ClassGen.NameAndType) {
130 CPRefEnt ce = new CPRefEnt(12);
131 ClassGen.NameAndType key = (ClassGen.NameAndType) o;
132 ce.e1 = addUtf8(key.name);
133 ce.e2 = addUtf8(key.type);
135 } else if(o instanceof ClassGen.FieldMethodRef) {
136 ClassGen.FieldMethodRef key = (ClassGen.FieldMethodRef) o;
137 int tag = o instanceof FieldRef ? 9 : o instanceof MethodRef ? 10 : o instanceof ClassGen.InterfaceMethodRef ? 11 : 0;
138 if(tag == 0) throw new Error("should never happen");
139 CPRefEnt ce = new CPRefEnt(tag);
140 ce.e1 = add(key.klass);
141 ce.e2 = add(key.nameAndType);
144 throw new IllegalArgumentException("Unknown type passed to add");
147 ent.index = nextIndex++;
148 if(ent instanceof LongEnt) nextIndex++;
155 public int size() { return nextIndex; }
157 private static final Sort.CompareFunc compareFunc = new Sort.CompareFunc() {
158 public int compare(Object a_, Object b_) {
159 return ((Ent)a_).index - ((Ent)b_).index;
162 public void dump(DataOutput o) throws IOException {
163 Ent[] ents = new Ent[count];
165 Enumeration e = entries.keys();
166 while(e.hasMoreElements()) ents[i++] = (Ent) entries.get(e.nextElement());
167 if(i != count) throw new Error("should never happen");
168 Sort.sort(ents,compareFunc);
169 for(i=0;i<ents.length;i++) {
170 //System.err.println("" + (i+1) + ": " + ents[i]);