KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > compiler > ConstantPool


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.compiler;
3
4 import java.util.*;
5 import java.io.*;
6
7 class Bytes
8 {
9     public byte[] data;
10
11     Bytes(ByteArrayOutputStream data) {
12         this.data = data.toByteArray();
13     }
14
15     public boolean equals(Object JavaDoc o) {
16         if (o instanceof Bytes) {
17             byte[] odata = ((Bytes)o).data;
18             int n = data.length;
19             if (odata.length != n)
20                 return false;
21             for (int i=0; i<n; i++) {
22                 if (data[i] != odata[i])
23                     return false;
24             }
25             return true;
26         }
27         return false;
28     }
29
30     public int hashCode() {
31         int h = 0xa538;
32         int n = data.length;
33         for (int i=0; i<n; i++)
34             h = h ^ data[i];
35         return h;
36     }
37 }
38
39 public class ConstantPool
40 {
41     Hashtable constants;
42     int index;
43     DataOutputStream tdata;
44     ByteArrayOutputStream pool, tarray;
45     int[] sizes;
46
47     public ConstantPool() {
48         constants = new Hashtable();
49         index = 0;
50         pool = new ByteArrayOutputStream();
51         tarray = new ByteArrayOutputStream();
52         tdata = new DataOutputStream(tarray);
53         sizes = new int[256];
54     }
55
56     public void write(DataOutputStream stream) throws IOException {
57         stream.writeShort(index+1);
58         stream.write(pool.toByteArray());
59     }
60
61     public int addConstant(int slots) throws IOException {
62         //tarray.flush();
63
//byte[] data = tarray.toByteArray();
64
Bytes data = new Bytes(tarray);
65         tarray.reset();
66         Integer JavaDoc i = (Integer JavaDoc)constants.get(data);
67         if (i == null) {
68             pool.write(data.data);
69             i = new Integer JavaDoc(index);
70             constants.put(data, i);
71             if (index+1 >= sizes.length) {
72                 int[] new_sizes = new int[sizes.length*2];
73                 System.arraycopy(sizes, 0, new_sizes, 0, sizes.length);
74                 sizes = new_sizes;
75             }
76             sizes[index+1] = slots;
77             index += slots;
78         }
79         //System.out.print("Constant: ");
80
//for(int j=0; j<data.length; j++)
81
// System.out.print(Integer.toString(data[j])+", ");
82
//System.out.println("");
83
return i.intValue()+1;
84     }
85
86     public int UTF8(String JavaDoc s) throws IOException {
87         tdata.writeByte(1);
88         tdata.writeUTF(s);
89         return addConstant(1);
90     }
91
92     public int Class(String JavaDoc s) throws IOException {
93         int c = UTF8(s);
94         tdata.writeByte(7);
95         tdata.writeShort(c);
96         return addConstant(1);
97     }
98
99     public int Fieldref(String JavaDoc c, String JavaDoc name, String JavaDoc type)
100         throws IOException
101     {
102         int ic = Class(c);
103         int nt = NameAndType(name, type);
104         tdata.writeByte(9);
105         tdata.writeShort(ic);
106         tdata.writeShort(nt);
107         int size = 1;
108         if (type.equals("D") || type.equals("J"))
109             size = 2;
110         int index = addConstant(1);
111         sizes[index] = size;
112         //System.out.println("field: "+c+", "+name+", "+type+": "+index);
113
return index;
114     }
115
116     public static int sigSize(String JavaDoc sig, boolean includeReturn) {
117         int stack = 0;
118         int i = 0;
119         char[] c = sig.toCharArray();
120         int n = c.length;
121         boolean ret=false;
122         boolean array=false;
123
124         while (++i<n) {
125             switch (c[i]) {
126             case ')':
127                 if (!includeReturn)
128                     return stack;
129                 ret=true;
130                 continue;
131             case '[':
132                 array=true;
133                 continue;
134             case 'V':
135                 continue;
136             case 'D':
137             case 'J':
138                 if (array) {
139                     if (ret) stack += 1;
140                     else stack -=1;
141                     array = false;
142                 } else {
143                     if (ret) stack += 2;
144                     else stack -=2;
145                 }
146                 break;
147             case 'L':
148                 while (c[++i] != ';') {;}
149             default:
150                 if (ret) stack++;
151                 else stack--;
152                 array = false;
153             }
154         }
155         return stack;
156     }
157
158     public int Methodref(String JavaDoc c, String JavaDoc name, String JavaDoc type)
159         throws IOException
160     {
161         int ic = Class(c);
162         int nt = NameAndType(name, type);
163
164         tdata.writeByte(10);
165         tdata.writeShort(ic);
166         tdata.writeShort(nt);
167         int index = addConstant(1);
168         sizes[index] = sigSize(type, true);
169         //System.out.println("method: "+c+", "+name+", "+type+": "+index);
170
return index;
171     }
172
173     public int InterfaceMethodref(String JavaDoc c, String JavaDoc name, String JavaDoc type)
174         throws IOException
175     {
176         int ic = Class(c);
177         int nt = NameAndType(name, type);
178
179         tdata.writeByte(11);
180         tdata.writeShort(ic);
181         tdata.writeShort(nt);
182         int index = addConstant(1);
183         sizes[index] = sigSize(type, true);
184         return index;
185     }
186
187     public int String(String JavaDoc s) throws IOException {
188         int i = UTF8(s);
189         tdata.writeByte(8);
190         tdata.writeShort(i);
191         return addConstant(1);
192     }
193
194     public int Integer(int i) throws IOException {
195         tdata.writeByte(3);
196         tdata.writeInt(i);
197         return addConstant(1);
198     }
199
200     public int Float(float f) throws IOException {
201         tdata.writeByte(4);
202         tdata.writeFloat(f);
203         return addConstant(1);
204     }
205
206     public int Long(long l) throws IOException {
207         tdata.writeByte(5);
208         tdata.writeLong(l);
209         return addConstant(2);
210     }
211
212     public int Double(double d) throws IOException {
213         tdata.writeByte(6);
214         tdata.writeDouble(d);
215         return addConstant(2);
216     }
217
218     public int NameAndType(String JavaDoc name, String JavaDoc type) throws IOException {
219         int n = UTF8(name);
220         int t = UTF8(type);
221
222         tdata.writeByte(12);
223         tdata.writeShort(n);
224         tdata.writeShort(t);
225         return addConstant(1);
226     }
227
228     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
229         ConstantPool cp = new ConstantPool();
230
231         System.out.println("c: "+cp.Class("org/python/core/PyString"));
232         System.out.println("c: "+cp.Class("org/python/core/PyString"));
233
234         for (int i=0; i<args.length; i++)
235             System.out.println(args[i]+": "+sigSize(args[i], true));
236     }
237 }
238
Popular Tags