* semantics.c (finish_handler): End the scope of the handler
[official-gcc.git] / gcc / java / constants.c
bloba1f820e0dd2a4dbfa4814f784c0a0cf25a6ac04b
1 /* Handle the constant pool of the Java(TM) Virtual Machine.
2 Copyright (C) 1997 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU CC; see the file COPYING. If not, write to
17 the Free Software Foundation, 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
20 Java and all Java-based marks are trademarks or registered trademarks
21 of Sun Microsystems, Inc. in the United States and other countries.
22 The Free Software Foundation is independent of Sun Microsystems, Inc. */
24 #include "config.h"
25 #include "system.h"
26 #include "jcf.h"
27 #include "tree.h"
28 #include "java-tree.h"
29 #include "toplev.h"
31 extern struct obstack permanent_obstack;
33 static void set_constant_entry PROTO ((CPool *, int, int, jword));
34 static int find_class_or_string_constant PROTO ((CPool *, int, tree));
35 static int find_name_and_type_constant PROTO ((CPool *, tree, tree));
36 static tree get_tag_node PROTO ((int));
37 static tree build_constant_data_ref PROTO ((void));
39 /* Set the INDEX'th constant in CPOOL to have the given TAG and VALUE. */
41 static void
42 set_constant_entry (cpool, index, tag, value)
43 CPool *cpool;
44 int index;
45 int tag;
46 jword value;
48 if (cpool->data == NULL)
50 cpool->capacity = 100;
51 cpool->tags = (uint8*) xmalloc (sizeof(uint8) * cpool->capacity);
52 cpool->data = (jword*) xmalloc (sizeof(jword) * cpool->capacity);
53 cpool->count = 1;
55 if (index >= cpool->capacity)
57 cpool->capacity *= 2;
58 if (index >= cpool->capacity)
59 cpool->capacity = index + 10;
60 cpool->tags = (uint8*) xrealloc (cpool->tags,
61 sizeof(uint8) * cpool->capacity);
62 cpool->data = (jword*) xrealloc (cpool->data,
63 sizeof(jword) * cpool->capacity);
65 if (index >= cpool->count)
66 cpool->count = index + 1;
67 cpool->tags[index] = tag;
68 cpool->data[index] = value;
71 /* Find (or create) a constant pool entry matching TAG and VALUE. */
73 int
74 find_constant1 (cpool, tag, value)
75 CPool *cpool;
76 int tag;
77 jword value;
79 int i;
80 for (i = cpool->count; --i > 0; )
82 if (cpool->tags[i] == tag && cpool->data[i] == value)
83 return i;
85 i = cpool->count == 0 ? 1 : cpool->count;
86 set_constant_entry (cpool, i, tag, value);
87 return i;
90 /* Find a double-word constant pool entry matching TAG and WORD1/WORD2. */
92 int
93 find_constant2 (cpool, tag, word1, word2)
94 CPool *cpool;
95 int tag;
96 jword word1, word2;
98 int i;
99 for (i = cpool->count - 1; --i > 0; )
101 if (cpool->tags[i] == tag
102 && cpool->data[i] == word1
103 && cpool->data[i+1] == word2)
104 return i;
106 i = cpool->count == 0 ? 1 : cpool->count;
107 set_constant_entry (cpool, i, tag, word1);
108 set_constant_entry (cpool, i+1, 0, word2);
109 return i;
113 find_utf8_constant (cpool, name)
114 CPool *cpool;
115 tree name;
117 if (name == NULL_TREE)
118 return 0;
119 return find_constant1 (cpool, CONSTANT_Utf8, (jword) name);
122 static int
123 find_class_or_string_constant (cpool, tag, name)
124 CPool *cpool;
125 int tag;
126 tree name;
128 int j = find_utf8_constant (cpool, name);
129 int i;
130 for (i = cpool->count; --i > 0; )
132 if (cpool->tags[i] == tag && cpool->data[i] == (jword) j)
133 return i;
135 i = cpool->count;
136 set_constant_entry (cpool, i, tag, (jword) j);
137 return i;
141 find_class_constant (cpool, type)
142 CPool *cpool;
143 tree type;
145 return find_class_or_string_constant (cpool, CONSTANT_Class,
146 build_internal_class_name (type));
149 /* Allocate a CONSTANT_string entry given a STRING_CST. */
152 find_string_constant (cpool, string)
153 CPool *cpool;
154 tree string;
156 string = get_identifier (TREE_STRING_POINTER (string));
157 return find_class_or_string_constant (cpool, CONSTANT_String, string);
161 /* Find (or create) a CONSTANT_NameAndType matching NAME and TYPE.
162 Return its index in the constant pool CPOOL. */
164 static int
165 find_name_and_type_constant (cpool, name, type)
166 CPool *cpool;
167 tree name;
168 tree type;
170 int name_index = find_utf8_constant (cpool, name);
171 int type_index = find_utf8_constant (cpool, build_java_signature (type));
172 return find_constant1 (cpool, CONSTANT_NameAndType,
173 (name_index << 16) | type_index);
176 /* Find (or create) a CONSTANT_Fieldref for DECL (a FIELD_DECL or VAR_DECL).
177 Return its index in the constant pool CPOOL. */
180 find_fieldref_index (cpool, decl)
181 CPool *cpool;
182 tree decl;
184 int class_index = find_class_constant (cpool, DECL_CONTEXT (decl));
185 int name_type_index
186 = find_name_and_type_constant (cpool, DECL_NAME (decl), TREE_TYPE (decl));
187 return find_constant1 (cpool, CONSTANT_Fieldref,
188 (class_index << 16) | name_type_index);
191 /* Find (or create) a CONSTANT_Methodref for DECL (a FUNCTION_DECL).
192 Return its index in the constant pool CPOOL. */
195 find_methodref_index (cpool, decl)
196 CPool *cpool;
197 tree decl;
199 tree mclass = DECL_CONTEXT (decl);
200 int class_index = find_class_constant (cpool, mclass);
201 tree name = DECL_CONSTRUCTOR_P (decl) ? init_identifier_node
202 : DECL_NAME (decl);
203 int name_type_index
204 = find_name_and_type_constant (cpool, name, TREE_TYPE (decl));
205 return find_constant1 (cpool,
206 CLASS_INTERFACE (TYPE_NAME (mclass))
207 ? CONSTANT_InterfaceMethodref
208 : CONSTANT_Methodref,
209 (class_index << 16) | name_type_index);
212 #define PUT1(X) (*ptr++ = (X))
213 #define PUT2(X) (PUT1((X) >> 8), PUT1(X))
214 #define PUT4(X) (PUT2((X) >> 16), PUT2(X))
215 #define PUTN(P, N) (bcopy(P, ptr, N), ptr += (N))
217 /* Give the number of bytes needed in a .class file for the CPOOL
218 constant pool. Includes the 2-byte constant_pool_count. */
221 count_constant_pool_bytes (cpool)
222 CPool *cpool;
224 int size = 2;
225 int i = 1;
226 for ( ; i < cpool->count; i++)
228 size++;
229 switch (cpool->tags[i])
231 case CONSTANT_NameAndType:
232 case CONSTANT_Fieldref:
233 case CONSTANT_Methodref:
234 case CONSTANT_InterfaceMethodref:
235 case CONSTANT_Float:
236 case CONSTANT_Integer:
237 size += 4;
238 break;
239 case CONSTANT_Class:
240 case CONSTANT_String:
241 size += 2;
242 break;
243 case CONSTANT_Long:
244 case CONSTANT_Double:
245 size += 8;
246 i++;
247 break;
248 case CONSTANT_Utf8:
250 tree t = (tree) cpool->data[i];
251 int len = IDENTIFIER_LENGTH (t);
252 size += len + 2;
254 break;
255 default:
256 /* Second word of CONSTANT_Long and CONSTANT_Double. */
257 size--;
260 return size;
263 /* Write the constant pool CPOOL into BUFFER.
264 The length of BUFFER is LENGTH, which must match the needed length. */
266 void
267 write_constant_pool (cpool, buffer, length)
268 CPool *cpool;
269 unsigned char* buffer;
270 int length;
272 unsigned char* ptr = buffer;
273 int i = 1;
274 jword *datap = &cpool->data[1];
275 PUT2 (cpool->count);
276 for ( ; i < cpool->count; i++, datap++)
278 int tag = cpool->tags[i];
279 PUT1 (tag);
280 switch (tag)
282 case CONSTANT_NameAndType:
283 case CONSTANT_Fieldref:
284 case CONSTANT_Methodref:
285 case CONSTANT_InterfaceMethodref:
286 case CONSTANT_Float:
287 case CONSTANT_Integer:
288 PUT4 (*datap);
289 break;
290 case CONSTANT_Class:
291 case CONSTANT_String:
292 PUT2 (*datap);
293 break;
294 break;
295 case CONSTANT_Long:
296 case CONSTANT_Double:
297 PUT4(*datap);
298 i++;
299 datap++;
300 PUT4 (*datap);
301 break;
302 case CONSTANT_Utf8:
304 tree t = (tree) *datap;
305 int len = IDENTIFIER_LENGTH (t);
306 PUT2 (len);
307 PUTN (IDENTIFIER_POINTER (t), len);
309 break;
312 if (ptr != buffer + length)
313 fatal("internal error - incorrect constant pool");
316 CPool *outgoing_cpool;
318 /* If non-NULL, an ADDR_EXPR referencing a VAR_DECL containing
319 the constant data array for the current class. */
320 tree current_constant_pool_data_ref;
322 /* A Cache for build_int_2 (CONSTANT_XXX, 0). */
323 static tree tag_nodes[13];
325 static tree
326 get_tag_node (tag)
327 int tag;
329 if (tag_nodes[tag] == NULL_TREE)
331 push_obstacks (&permanent_obstack, &permanent_obstack);
332 tag_nodes[tag] = build_int_2 (tag, 0);
333 pop_obstacks ();
335 return tag_nodes[tag];
338 /* Look for a constant pool entry that matches TAG and NAME.
339 Creates a new entry if not found.
340 TAG is one of CONSTANT_Utf8, CONSTANT_String or CONSTANT_Class.
341 NAME is an IDENTIFIER_NODE naming the Utf8 constant, string, or class.
342 Returns the index of the entry. */
345 alloc_name_constant (tag, name)
346 int tag;
347 tree name;
349 return find_constant1 (outgoing_cpool, tag, (jword) name);
352 /* Build an identifier for the internal name of reference type TYPE. */
354 tree
355 build_internal_class_name (type)
356 tree type;
358 tree name;
359 if (TYPE_ARRAY_P (type))
360 name = build_java_signature (type);
361 else
363 name = TYPE_NAME (type);
364 if (TREE_CODE (name) != IDENTIFIER_NODE)
365 name = DECL_NAME (name);
366 name = identifier_subst (name, "", '.', '/', "");
368 return name;
371 /* Look for a CONSTANT_Class entry for CLAS, creating a new one if needed. */
374 alloc_class_constant (clas)
375 tree clas;
377 tree class_name = build_internal_class_name (clas);
379 return alloc_name_constant (CONSTANT_Class,
380 (unmangle_classname
381 (IDENTIFIER_POINTER(class_name),
382 IDENTIFIER_LENGTH(class_name))));
385 /* Return a reference to the data array of the current constant pool. */
387 static tree
388 build_constant_data_ref ()
390 if (current_constant_pool_data_ref == NULL_TREE)
392 tree decl;
393 tree decl_name = mangled_classname ("_CD_", current_class);
394 push_obstacks (&permanent_obstack, &permanent_obstack);
395 decl = build_decl (VAR_DECL, decl_name,
396 build_array_type (ptr_type_node,
397 one_elt_array_domain_type));
398 TREE_STATIC (decl) = 1;
399 make_decl_rtl (decl, NULL, 1);
400 pop_obstacks ();
401 current_constant_pool_data_ref
402 = build1 (ADDR_EXPR, ptr_type_node, decl);
404 return current_constant_pool_data_ref;
407 /* Get the pointer value at the INDEX'th element of the constant pool. */
409 tree
410 build_ref_from_constant_pool (index)
411 int index;
413 tree t = build_constant_data_ref ();
414 index *= int_size_in_bytes (ptr_type_node);
415 t = fold (build (PLUS_EXPR, ptr_type_node,
416 t, build_int_2 (index, 0)));
417 return build1 (INDIRECT_REF, ptr_type_node, t);
420 /* Build an initializer for the constants field of the current constal pool.
421 Should only be called at top-level, since it may emit declarations. */
423 tree
424 build_constants_constructor ()
426 tree tags_value, data_value;
427 tree cons;
428 tree tags_list = NULL_TREE;
429 tree data_list = NULL_TREE;
430 int i;
431 for (i = outgoing_cpool->count; --i > 0; )
433 tags_list
434 = tree_cons (NULL_TREE, get_tag_node (outgoing_cpool->tags[i]),
435 tags_list);
436 data_list
437 = tree_cons (NULL_TREE, build_utf8_ref ((tree)outgoing_cpool->data[i]),
438 data_list);
440 if (outgoing_cpool->count > 0)
442 tree index_type;
443 tree data_decl, tags_decl, tags_type;
444 tree max_index = build_int_2 (outgoing_cpool->count - 1, 0);
445 TREE_TYPE (max_index) = sizetype;
446 index_type = build_index_type (max_index);
448 /* Add dummy 0'th element of constant pool. */
449 tags_list = tree_cons (NULL_TREE, get_tag_node (0), tags_list);
450 data_list = tree_cons (NULL_TREE, null_pointer_node, data_list);
452 data_decl = TREE_OPERAND (build_constant_data_ref (), 0);
453 TREE_TYPE (data_decl) = build_array_type (ptr_type_node, index_type),
454 DECL_INITIAL (data_decl) = build (CONSTRUCTOR, TREE_TYPE (data_decl),
455 NULL_TREE, data_list);
456 DECL_SIZE (data_decl) = TYPE_SIZE (TREE_TYPE (data_decl));
457 rest_of_decl_compilation (data_decl, (char*) 0, 1, 0);
458 data_value = build_address_of (data_decl);
460 tags_type = build_array_type (unsigned_byte_type_node, index_type);
461 tags_decl = build_decl (VAR_DECL, mangled_classname ("_CT_",
462 current_class),
463 tags_type);
464 TREE_STATIC (tags_decl) = 1;
465 DECL_INITIAL (tags_decl) = build (CONSTRUCTOR, tags_type,
466 NULL_TREE, tags_list);
467 rest_of_decl_compilation (tags_decl, (char*) 0, 1, 0);
468 tags_value = build_address_of (tags_decl);
470 else
472 data_value = null_pointer_node;
473 tags_value = null_pointer_node;
475 START_RECORD_CONSTRUCTOR (cons, constants_type_node);
476 PUSH_FIELD_VALUE (cons, "size", build_int_2 (outgoing_cpool->count, 0));
477 PUSH_FIELD_VALUE (cons, "tags", tags_value);
478 PUSH_FIELD_VALUE (cons, "data", data_value);
479 FINISH_RECORD_CONSTRUCTOR (cons);
480 return cons;