c41d67d2ccc0444dc193ed793fb0b59151e518bd
[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 class CPGen {
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
15     
16     CPGen() { }
17     
18     /*
19      * Entries 
20      */
21     abstract static class Ent {
22         int n; // this is the refcount if state == OPEN, index if >= STABLE
23         int tag;
24         
25         Ent(int tag) { this.tag = tag; }
26         
27         void dump(DataOutput o) throws IOException { o.writeByte(tag); }
28         String debugToString() { return toString(); } // so we can remove this method when not debugging
29     }
30     
31     static class IntEnt extends Ent {
32         int i;
33         IntEnt(int tag) { super(tag); }
34         void dump(DataOutput o) throws IOException { super.dump(o); o.writeInt(i);  }
35     }
36     
37     static class LongEnt extends Ent {
38         long l;
39         LongEnt(int tag) { super(tag); }
40         void dump(DataOutput o) throws IOException { super.dump(o); o.writeLong(l); }
41     }
42     
43     static class CPRefEnt extends Ent {
44         Ent e1;
45         Ent e2;
46         CPRefEnt(int tag) { super(tag); }
47         
48         String debugToString() { return "[" + e1.n + ":" + e1.debugToString() + (e2 == null ? "" : " + " + e2.n + ":" + e2.debugToString()) + "]"; }
49         
50         void dump(DataOutput o) throws IOException {
51             super.dump(o);
52             if(e1.n == 6 || (e2!=null && e2.n == 6)) System.err.println(debugToString() + " refs 6");
53             o.writeShort(e1.n);
54             if(e2 != null) o.writeShort(e2.n);
55         }
56     }
57         
58     static class Utf8Ent extends Ent {
59         String s;
60         Utf8Ent() { super(1); }
61         String debugToString() { return s; }
62         void dump(DataOutput o) throws IOException { super.dump(o); o.writeUTF(s); }
63     }
64     
65     /*
66      * Cache Keys
67      */
68     static class Utf8Key {
69         String s;
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(); }
73     }
74         
75     static class NameAndTypeKey {
76         String name;
77         String type;
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);
83         }
84         public int hashCode() { return name.hashCode() ^ type.hashCode(); }
85     }
86     
87     /*
88      * Methods
89      */
90     
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) {
94         Ent e = get(o);
95         if(e == null) throw new IllegalStateException("entry not found");
96         return getIndex(e);
97     }
98     public final int getUtf8Index(String s) {
99         Ent e = getUtf8(s);
100         if(e == null) throw new IllegalStateException("entry not found");
101         return getIndex(e);
102     }
103     public final int getIndex(Ent ent) {
104         if(state < STABLE) throw new IllegalStateException("constant pool is not stable");
105         return ent.n;
106     }
107     
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)); }
110     
111     public final Ent add(Object o) {
112         if(state == SEALED) throw new IllegalStateException("constant pool is sealed");
113             
114         Ent ent = get(o);
115         if(ent != null) {
116             if(state == OPEN) ent.n++;
117             return ent;
118         }
119         
120         if(o instanceof Type.Object) {
121             CPRefEnt ce = new CPRefEnt(7);
122             ce.e1 = addUtf8(((Type.Object)o).internalForm());
123             ent = ce;
124         } else if(o instanceof String) {
125             CPRefEnt ce = new CPRefEnt(8);
126             ce.e1 = addUtf8((String)o);
127             ent = ce;
128         } else if(o instanceof Integer) {
129             IntEnt ue = new IntEnt(3);
130             ue.i = ((Integer)o).intValue();
131             ent = ue;
132         } else if(o instanceof Float) {
133             IntEnt ue = new IntEnt(4);
134             ue.i = Float.floatToIntBits(((Float)o).floatValue());
135             ent = ue;
136         } else if(o instanceof Long) {
137             LongEnt le = new LongEnt(5);
138             le.l = ((Long)o).longValue();
139             ent = le;
140         } else if(o instanceof Double) {
141             LongEnt le = new LongEnt(6);
142             le.l = Double.doubleToLongBits(((Double)o).doubleValue());
143             ent = le;
144         } else if(o instanceof Utf8Key) {
145             Utf8Ent ue = new Utf8Ent();
146             ue.s = ((Utf8Key)o).s;
147             ent = ue;
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);
153             ent = ce;
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);
161             ent = ce;
162         } else {
163             throw new IllegalArgumentException("Unknown type passed to add");
164         }
165         
166         int spaces = ent instanceof LongEnt ? 2 : 1;        
167         if(usedSlots + spaces > 65536) throw new ClassGen.Exn("constant pool full");
168         
169         ent.n = state == OPEN ? 1 : usedSlots; // refcount or index
170
171         usedSlots += spaces;        
172
173         entries.put(o,ent);
174         return ent;
175     }
176     
177     public int slots() { return usedSlots; }
178
179     public void seal() { state = SEALED; }
180     
181     private Ent[] asArray() {
182         int count = entries.size();
183         Ent[] ents = new Ent[count];
184         int i=0;
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");
188         return ents;
189     }
190     
191     private static void assignIndex(Ent[] ents) {
192         int index = 1;
193         for(int i=0;i<ents.length;i++) {
194             Ent ent = ents[i];
195             ent.n = index;
196             index += ent instanceof LongEnt ? 2 : 1;
197         }
198     }
199         
200     public void stable() {
201         if(state != OPEN) return;
202         state = STABLE;
203         assignIndex(asArray());
204     } 
205
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;
209         }
210     };
211     
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;
215         }
216     };
217     
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);
222         for(int i=0;i<ents.length;i++)
223             System.err.println("" + i + " -> " + ents[i].debugToString() + " (" + ents[i].n + ")");
224         state = STABLE;
225         assignIndex(ents);
226     }
227     
228     public void dump(DataOutput o) throws IOException {
229         Ent[] ents = asArray();
230         Sort.sort(ents,compareFunc);
231         for(int i=0;i<ents.length;i++) {
232             System.err.println("" + ents[i].n + ": " + ents[i].debugToString());
233             ents[i].dump(o);
234         }
235     }
236 }