cleanup WIDE stuff
[org.ibex.classgen.git] / src / org / ibex / classgen / CPGen.java
1 package org.ibex.classgen;
2
3 import java.util.*;
4 import java.io.*;
5
6 import org.ibex.classgen.util.*;
7
8 // FEATURE: Add a "hit count" to each entry and optimize the table
9
10 class CPGen {
11     private Hashtable entries = new Hashtable();
12     private int nextIndex = 1; // 0 is reserved
13     private int count;
14     private int state;
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
18     
19     CPGen() { }
20     
21     /*
22      * Entries 
23      */
24     abstract static class Ent {
25         int index;
26         int tag;
27         
28         Ent(int tag) { this.tag = tag; }
29         
30         int getIndex() { return index; }
31         
32         void dump(DataOutput o) throws IOException { o.writeByte(tag); }
33     }
34     
35     static class OneU4Ent extends Ent {
36         int i;
37         OneU4Ent(int tag) { super(tag); }
38         void dump(DataOutput o) throws IOException { super.dump(o); o.writeInt(i);  }
39     }
40     
41     static class LongEnt extends Ent {
42         long l;
43         LongEnt(int tag) { super(tag); }
44         void dump(DataOutput o) throws IOException { super.dump(o); o.writeLong(l); }
45     }
46     
47     static class CPRefEnt extends Ent {
48         Ent e1;
49         Ent e2;
50         CPRefEnt(int tag) { super(tag); }
51         void dump(DataOutput o) throws IOException {
52             super.dump(o);
53             o.writeShort(e1.index);
54             if(e2 != null) o.writeShort(e2.index);
55         }
56     }
57         
58     static class Utf8Ent extends Ent {
59         String s;
60         Utf8Ent() { super(1); }
61         void dump(DataOutput o) throws IOException { super.dump(o); o.writeUTF(s); }
62     }
63     
64     /*
65      * Cache Keys
66      */
67     static class Utf8Key {
68         String s;
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(); }
72     }
73         
74     static class NameAndTypeKey {
75         String name;
76         String type;
77         NameAndTypeKey(String name, String type) { this.name = name; this.type = type; }
78         public boolean equals(Object o_) {
79             if(!(o_ instanceof NameAndTypeKey)) return false;
80             NameAndTypeKey o = (NameAndTypeKey) o_;
81             return o.name.equals(name) && o.type.equals(type);
82         }
83         public int hashCode() { return name.hashCode() ^ type.hashCode(); }
84     }
85     
86     /*
87      * Methods
88      */
89     public void seal() { if(state > SEALED) throw new IllegalStateException(); state = SEALED; }
90     public void stable() { if(state > STABLE) throw new IllegalStateException(); state = STABLE; }
91     
92     public final Ent get(Object o) { return (Ent) entries.get(o); }
93     public final Ent getUtf8(String s) { return get(new Utf8Key(s)); }
94     public final int getIndex(Object o) {
95         Ent e = get(o);
96         if(e == null) throw new IllegalStateException("entry not found");
97         return e.getIndex();
98     }
99     public final int getUtf8Index(String s) {
100         Ent e = getUtf8(s);
101         if(e == null) throw new IllegalStateException("entry not found");
102         return e.getIndex();
103     }
104     
105     public final Ent addNameAndType(String name, String descriptor) { return add(new NameAndTypeKey(name,descriptor)); }
106     public final Ent addUtf8(String s) { return add(new Utf8Key(s)); }
107     
108     public final Ent add(Object o) {
109         if(state == SEALED) throw new IllegalStateException("constant pool is sealed");
110             
111         Ent ent = get(o);
112         if(ent != null) return ent;
113         
114         if(o instanceof Type.Object) {
115             CPRefEnt ce = new CPRefEnt(7);
116             ce.e1 = addUtf8(((Type.Object)o).internalForm());
117             ent = ce;
118         } else if(o instanceof String) {
119             CPRefEnt ce = new CPRefEnt(8);
120             ce.e1 = addUtf8((String)o);
121             ent = ce;
122         } else if(o instanceof Integer) {
123             OneU4Ent ue = new OneU4Ent(3);
124             ue.i = ((Integer)o).intValue();
125             ent = ue;
126         } else if(o instanceof Float) {
127             OneU4Ent ue = new OneU4Ent(4);
128             ue.i = Float.floatToIntBits(((Float)o).floatValue());
129             ent = ue;
130         } else if(o instanceof Long) {
131             LongEnt le = new LongEnt(5);
132             le.l = ((Long)o).longValue();
133             ent = le;
134         } else if(o instanceof Double) {
135             LongEnt le = new LongEnt(6);
136             le.l = Double.doubleToLongBits(((Double)o).doubleValue());
137             ent = le;
138         } else if(o instanceof Utf8Key) {
139             Utf8Ent ue = new Utf8Ent();
140             ue.s = ((Utf8Key)o).s;
141             ent = ue;
142         } else if(o instanceof NameAndTypeKey) {
143             CPRefEnt ce = new CPRefEnt(12);
144             NameAndTypeKey key = (NameAndTypeKey) o;
145             ce.e1 = addUtf8(key.name);
146             ce.e2 = addUtf8(key.type);
147             ent = ce;
148         } else if(o instanceof ClassGen.FieldOrMethodRef) {
149             ClassGen.FieldOrMethodRef key = (ClassGen.FieldOrMethodRef) o;
150             int tag = o instanceof FieldRef ? 9 : o instanceof MethodRef ? 10 : o instanceof MethodRef.I ? 11 : 0;
151             if(tag == 0) throw new Error("should never happen");
152             CPRefEnt ce = new CPRefEnt(tag);
153             ce.e1 = add(key.klass);
154             ce.e2 = addNameAndType(key.name,key.descriptor);
155             ent = ce;
156         } else {
157             throw new IllegalArgumentException("Unknown type passed to add");
158         }
159         
160         int spaces = ent instanceof LongEnt ? 2 : 1;
161         
162         if(nextIndex + spaces > 65536) throw new ClassGen.Exn("constant pool full");
163         
164         ent.index = nextIndex;
165         nextIndex += spaces;        
166         count++;
167
168         entries.put(o,ent);
169         return ent;
170     }
171     
172     public int size() { return nextIndex; }
173     
174     private static final Sort.CompareFunc compareFunc = new Sort.CompareFunc() {
175         public int compare(Object a_, Object b_) {
176             return ((Ent)a_).index - ((Ent)b_).index;
177         }
178     };
179     public void dump(DataOutput o) throws IOException {
180         Ent[] ents = new Ent[count];
181         int i=0;
182         Enumeration e = entries.keys();
183         while(e.hasMoreElements()) ents[i++] = (Ent) entries.get(e.nextElement());
184         if(i != count) throw new Error("should never happen");
185         Sort.sort(ents,compareFunc);
186         for(i=0;i<ents.length;i++) {
187             //System.err.println("" + (i+1) + ": " + ents[i]);
188             ents[i].dump(o);
189         }
190     }
191 }