DEADSOFTWARE

Patched for Linux
[mp3cc.git] / MPC.3.5.LINUX / classgen / constant_pool.c
1 /********************************************************************
3 constant_pool.c - methods for handling constant pool
5 Niksa Orlic, 2004-06-11
7 ********************************************************************/
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdio.h>
13 #include "constant_pool.h"
14 //#include "../util/message.h"
15 #include "../util/error.h"
16 #include "../util/strings.h"
17 #include "../structures/type_list.h"
18 #include "../structures/string_list.h"
19 #include "../structures/type.h"
20 #include "../structures/identifier.h"
21 #include "../structures/name_table.h"
22 #include "../classgen/bytecode.h"
23 #include "../structures/block.h"
24 #include "../util/memory.h"
26 #include "classgen.h"
28 constant_pool *constants;
29 int constant_pool_size = 0;
31 /*
32 Insert a utf8 string into the constant pool table and return its index in the table.
33 */
34 int cp_add_utf8(char *str)
35 {
36 int i, j, len;
37 int size = 0;
38 constant_pool *entry;
40 /* check if the string already exists in the table */
41 for (i=0; i<constant_pool_size; i++)
42 {
43 if ((constants[i].tag == 1)
44 && (strcmp(constants[i].data, str) == 0))
45 {
46 return i+1; /* the indexes for the constant pool are in range 1..N (not zero based) */
47 }
48 }
50 /* create a new entry */
51 entry = cp_create_new_entry();
52 entry->tag = 1;
54 /* calculate the needeed size for the string */
55 len = strlen(str);
56 for (i=0; i<len; i++)
57 {
58 /* if ((unsigned char)str[i] > 127)
59 size += 2;
60 else*/
61 size ++;
62 }
64 entry->data = (char*) mem_alloc(size + 1);
65 entry->data_len = size;
67 if (entry->data == NULL)
68 die(1);
70 for(i=0, j=0; i<len; i++)
71 {
72 /* if ((unsigned char)str[i] <= 127)
73 {*/
74 entry->data[j] = str[i];
75 j++;
76 /* }
77 else
78 {
79 entry->data[j] = (char)0xC2 | (str[i]>>7)&0x01;
80 j++;
81 entry->data[j] = (str[i] & 0x3F) | 0x80;
82 j++;
83 }*/
84 }
86 entry->data[j] = '\0';
88 return constant_pool_size;
89 }
91 /*
92 Add a string entry into the constant pool.
93 */
94 int cp_add_string(char *str)
95 {
96 int i;
97 constant_pool *entry;
99 /* first add the utf8 constant */
100 int utf8_const = cp_add_utf8(str);
102 /* search if the same string already exists */
103 for(i=0; i<constant_pool_size; i++)
105 if ((constants[i].tag == 8)
106 && (constants[i].param1 == utf8_const))
108 return i+1;
112 /* create the new entry */
113 entry = cp_create_new_entry();
114 entry->tag = 8;
115 entry->param1 = utf8_const;
117 return constant_pool_size;
120 /*
121 Add an integer into the constant pool.
122 */
123 int cp_add_integer(int data)
125 int i;
126 constant_pool *entry;
128 /* check if the string already exists in the table */
129 for (i=0; i<constant_pool_size; i++)
131 if (constants[i].tag == 3)
133 int *p_value;
134 p_value = (int*)(constants[i].data);
135 if(*p_value == data)
136 return i+1; /* the indexes for the constant pool are in range 1..N (not zero based) */
140 /* create a new entry */
141 entry = cp_create_new_entry();
142 entry->tag = 3;
143 entry->data = (char*) mem_alloc(sizeof(int));
145 if (entry->data == NULL)
146 die(1);
148 *((int*)(entry->data)) = data;
150 return constant_pool_size;
153 /*
154 Add a long constant into the constant pool
155 */
156 int cp_add_long(long data)
158 die(18);
160 return constant_pool_size;
163 /*
164 Add a float constant into the constant pool
165 */
166 int cp_add_double(double data)
168 die(18);
170 return constant_pool_size;
173 /*
174 Add a double constant into the constant pool
175 */
176 int cp_add_float(float data)
178 int i;
179 constant_pool *entry;
181 /* check if the string already exists in the table */
182 for (i=0; i<constant_pool_size; i++)
184 if (constants[i].tag == 6)
186 float *p_value;
187 p_value = (float*)(constants[i].data);
188 if(*p_value == data)
189 return i+1; /* the indexes for the constant pool are in range 1..N (not zero based) */
193 /* create a new entry */
194 entry = cp_create_new_entry();
195 entry->tag = 6;
196 entry->data = (char*) mem_alloc(sizeof(float));
198 if (entry->data == NULL)
199 die(1);
201 *((float*)(entry->data)) = data;
203 return constant_pool_size;
207 /*
208 Adds a class information into the constants pool
209 */
210 int cp_add_class(char *class_name)
212 int i;
213 constant_pool *entry;
215 /* first add the utf8 constant */
216 int utf8_const = cp_add_utf8(class_name);
218 /* search if the same string already exists */
219 for(i=0; i<constant_pool_size; i++)
221 if ((constants[i].tag == 7)
222 && (constants[i].param1 == utf8_const))
224 return i+1;
228 /* create the new entry */
229 entry = cp_create_new_entry();
230 entry->tag = 7;
231 entry->param1 = utf8_const;
233 return constant_pool_size;
236 /*
237 Add name and type entry into the constant pool
238 */
239 int cp_add_nameandtype(char *name, char *type)
241 int i;
242 int name_index;
243 int type_index;
244 constant_pool *entry;
246 /* first add the utf8 constant */
247 name_index = cp_add_utf8(name);
248 type_index = cp_add_utf8(type);
250 /* search if the same string already exists */
251 for(i=0; i<constant_pool_size; i++)
253 if ((constants[i].tag == 12)
254 && (constants[i].param1 == name_index)
255 && (constants[i].param2 == type_index))
257 return i+1;
261 /* create the new entry */
262 entry = cp_create_new_entry();
263 entry->tag = 12;
264 entry->param1 = name_index;
265 entry->param2 = type_index;
267 return constant_pool_size;
270 /*
271 Adds the fieldref entry into the constant pool
272 */
273 int cp_add_fieldref(char *class_name, char *name, char *type)
275 int i;
276 int class_index;
277 int nameandtype_index;
278 constant_pool *entry;
280 /* first add the utf8 constant */
281 class_index = cp_add_class(class_name);
282 nameandtype_index = cp_add_nameandtype(name, type);
284 /* search if the same string already exists */
285 for(i=0; i<constant_pool_size; i++)
287 if ((constants[i].tag == 9)
288 && (constants[i].param1 == class_index)
289 && (constants[i].param2 == nameandtype_index))
291 return i+1;
295 /* create the new entry */
296 entry = cp_create_new_entry();
297 entry->tag = 9;
298 entry->param1 = class_index;
299 entry->param2 = nameandtype_index;
301 return constant_pool_size;
304 /*
305 Add the fieldref entry into the constant pool
306 */
307 int cp_add_methodref(char *class_name, char *name, char *type)
309 int i;
310 int class_index;
311 int nameandtype_index;
312 constant_pool *entry;
314 /* first add the utf8 constant */
315 class_index = cp_add_class(class_name);
316 nameandtype_index = cp_add_nameandtype(name, type);
318 /* search if the same string already exists */
319 for(i=0; i<constant_pool_size; i++)
321 if ((constants[i].tag == 10)
322 && (constants[i].param1 == class_index)
323 && (constants[i].param2 == nameandtype_index))
325 return i+1;
329 /* create the new entry */
330 entry = cp_create_new_entry();
331 entry->tag = 10;
332 entry->param1 = class_index;
333 entry->param2 = nameandtype_index;
335 return constant_pool_size;
338 /*
339 Add the interface entry into the constant pool
340 */
341 int cp_add_interface(char *class_name, char *name, char *type)
343 int i;
344 int class_index;
345 int nameandtype_index;
346 constant_pool *entry;
347 /* first add the utf8 constant */
348 class_index = cp_add_class(class_name);
349 nameandtype_index = cp_add_nameandtype(name, type);
350 /* search if the same string already exists */
351 for(i=0; i<constant_pool_size; i++)
353 if ((constants[i].tag == 10) && (constants[i].param1 == class_index) && (constants[i].param2 == nameandtype_index))
354 return i+1;
356 /* create the new entry */
357 entry = cp_create_new_entry();
358 entry->tag = 11;
359 entry->param1 = class_index;
360 entry->param2 = nameandtype_index;
361 return constant_pool_size;
364 /*
365 Creates a new entry in the constant pool.
366 */
367 constant_pool *cp_create_new_entry()
369 constant_pool_size ++;
371 if (constant_pool_size == 1)
372 constants = (constant_pool*) mem_alloc(sizeof(constant_pool));
373 else
374 constants = (constant_pool*) mem_realloc(constants,
375 sizeof(constant_pool) * constant_pool_size);
377 if (constants == NULL)
378 die(1);
380 return &constants[constant_pool_size-1];
383 /*
384 Writes the constant pool to the disk
385 */
386 void write_constant_pool(FILE *fp)
388 int i;
390 /* write the constant pool count */
391 write_short_int(fp, (short)(constant_pool_size + 1));
393 /* write the constant pool entries */
394 for(i=0; i<constant_pool_size; i++)
396 /* write the tag */
397 char tag = constants[i].tag;
399 fwrite(&tag, 1, 1, fp);
401 switch (tag)
403 case 7: /* constant_class */
405 write_short_int(fp, constants[i].param1);
407 break;
409 case 9: /* fieldref */
410 case 10: /* methodref */
411 case 12: /* name and type */
413 write_short_int(fp, constants[i].param1);
414 write_short_int(fp, constants[i].param2);
416 break;
418 case 8: /* string */
420 write_short_int(fp, constants[i].param1);
422 break;
424 case 3: /* integer */
426 write_long_int(fp, *((int*)constants[i].data));
428 break;
430 case 4: /* float */
432 int *data;
433 data = (int*) constants[i].data;
434 write_long_int(fp, *data);
436 break;
438 case 1: /* Utf-8 */
440 write_short_int(fp, (short) constants[i].data_len);
441 fwrite(constants[i].data, 1, constants[i].data_len, fp);
443 break;
445 default:
446 die(16);