* config/arm/arm.c (use_return_insn): Do not use a single return instruction
[official-gcc.git] / gcc / java / constants.c
blob1a56df0646149000ee236bc6243e4cdb2066532e
1 /* Handle the constant pool of the Java(TM) Virtual Machine.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC 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 GCC 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 GCC; 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 "coretypes.h"
27 #include "tm.h"
28 #include "jcf.h"
29 #include "tree.h"
30 #include "java-tree.h"
31 #include "toplev.h"
32 #include "ggc.h"
34 static void set_constant_entry (CPool *, int, int, jword);
35 static int find_tree_constant (CPool *, int, tree);
36 static int find_class_or_string_constant (CPool *, int, tree);
37 static int find_name_and_type_constant (CPool *, tree, tree);
38 static tree get_tag_node (int);
39 static tree build_constant_data_ref (void);
41 /* Set the INDEX'th constant in CPOOL to have the given TAG and VALUE. */
43 static void
44 set_constant_entry (CPool *cpool, int index, int tag, jword value)
46 if (cpool->data == NULL)
48 cpool->capacity = 100;
49 cpool->tags = ggc_alloc_cleared (sizeof(uint8) * cpool->capacity);
50 cpool->data = ggc_alloc_cleared (sizeof(union cpool_entry)
51 * cpool->capacity);
52 cpool->count = 1;
54 if (index >= cpool->capacity)
56 int old_cap = cpool->capacity;
57 cpool->capacity *= 2;
58 if (index >= cpool->capacity)
59 cpool->capacity = index + 10;
60 cpool->tags = ggc_realloc (cpool->tags,
61 sizeof(uint8) * cpool->capacity);
62 cpool->data = ggc_realloc (cpool->data,
63 sizeof(union cpool_entry) * cpool->capacity);
65 /* Make sure GC never sees uninitialized tag values. */
66 memset (cpool->tags + old_cap, 0, cpool->capacity - old_cap);
67 memset (cpool->data + old_cap, 0,
68 (cpool->capacity - old_cap) * sizeof (union cpool_entry));
70 if (index >= cpool->count)
71 cpool->count = index + 1;
72 cpool->tags[index] = tag;
73 cpool->data[index].w = value;
76 /* Find (or create) a constant pool entry matching TAG and VALUE. */
78 int
79 find_constant1 (CPool *cpool, int tag, jword value)
81 int i;
82 for (i = cpool->count; --i > 0; )
84 if (cpool->tags[i] == tag && cpool->data[i].w == value)
85 return i;
87 i = cpool->count == 0 ? 1 : cpool->count;
88 set_constant_entry (cpool, i, tag, value);
89 return i;
92 /* Find a double-word constant pool entry matching TAG and WORD1/WORD2. */
94 int
95 find_constant2 (CPool *cpool, int tag, jword word1, jword word2)
97 int i;
98 for (i = cpool->count - 1; --i > 0; )
100 if (cpool->tags[i] == tag
101 && cpool->data[i].w == word1
102 && cpool->data[i+1].w == word2)
103 return i;
105 i = cpool->count == 0 ? 1 : cpool->count;
106 set_constant_entry (cpool, i, tag, word1);
107 set_constant_entry (cpool, i+1, 0, word2);
108 return i;
111 static int
112 find_tree_constant (CPool *cpool, int tag, tree value)
114 int i;
115 for (i = cpool->count; --i > 0; )
117 if (cpool->tags[i] == tag && cpool->data[i].t == value)
118 return i;
120 i = cpool->count == 0 ? 1 : cpool->count;
121 set_constant_entry (cpool, i, tag, 0);
122 cpool->data[i].t = value;
123 return i;
128 find_utf8_constant (CPool *cpool, tree name)
130 if (name == NULL_TREE)
131 return 0;
132 return find_tree_constant (cpool, CONSTANT_Utf8, name);
135 static int
136 find_class_or_string_constant (CPool *cpool, int tag, tree name)
138 jword j = find_utf8_constant (cpool, name);
139 int i;
140 for (i = cpool->count; --i > 0; )
142 if (cpool->tags[i] == tag && cpool->data[i].w == j)
143 return i;
145 i = cpool->count;
146 set_constant_entry (cpool, i, tag, j);
147 return i;
151 find_class_constant (CPool *cpool, tree type)
153 return find_class_or_string_constant (cpool, CONSTANT_Class,
154 build_internal_class_name (type));
157 /* Allocate a CONSTANT_string entry given a STRING_CST. */
160 find_string_constant (CPool *cpool, tree string)
162 string = get_identifier (TREE_STRING_POINTER (string));
163 return find_class_or_string_constant (cpool, CONSTANT_String, string);
167 /* Find (or create) a CONSTANT_NameAndType matching NAME and TYPE.
168 Return its index in the constant pool CPOOL. */
170 static int
171 find_name_and_type_constant (CPool *cpool, tree name, tree type)
173 int name_index = find_utf8_constant (cpool, name);
174 int type_index = find_utf8_constant (cpool, build_java_signature (type));
175 return find_constant1 (cpool, CONSTANT_NameAndType,
176 (name_index << 16) | type_index);
179 /* Find (or create) a CONSTANT_Fieldref for DECL (a FIELD_DECL or VAR_DECL).
180 Return its index in the constant pool CPOOL. */
183 find_fieldref_index (CPool *cpool, tree decl)
185 int class_index = find_class_constant (cpool, DECL_CONTEXT (decl));
186 int name_type_index
187 = find_name_and_type_constant (cpool, DECL_NAME (decl), TREE_TYPE (decl));
188 return find_constant1 (cpool, CONSTANT_Fieldref,
189 (class_index << 16) | name_type_index);
192 /* Find (or create) a CONSTANT_Methodref for DECL (a FUNCTION_DECL).
193 Return its index in the constant pool CPOOL. */
196 find_methodref_index (CPool *cpool, tree decl)
198 return find_methodref_with_class_index (cpool, decl, DECL_CONTEXT (decl));
202 find_methodref_with_class_index (CPool *cpool, tree decl, tree mclass)
204 int class_index = find_class_constant (cpool, mclass);
205 tree name = DECL_CONSTRUCTOR_P (decl) ? init_identifier_node
206 : DECL_NAME (decl);
207 int name_type_index;
208 name_type_index =
209 find_name_and_type_constant (cpool, name, TREE_TYPE (decl));
210 return find_constant1 (cpool,
211 CLASS_INTERFACE (TYPE_NAME (mclass))
212 ? CONSTANT_InterfaceMethodref
213 : CONSTANT_Methodref,
214 (class_index << 16) | name_type_index);
217 #define PUT1(X) (*ptr++ = (X))
218 #define PUT2(X) (PUT1((X) >> 8), PUT1(X))
219 #define PUT4(X) (PUT2((X) >> 16), PUT2(X))
220 #define PUTN(P, N) (memcpy(ptr, (P), (N)), ptr += (N))
222 /* Give the number of bytes needed in a .class file for the CPOOL
223 constant pool. Includes the 2-byte constant_pool_count. */
226 count_constant_pool_bytes (CPool *cpool)
228 int size = 2;
229 int i = 1;
230 for ( ; i < cpool->count; i++)
232 size++;
233 switch (cpool->tags[i])
235 case CONSTANT_NameAndType:
236 case CONSTANT_Fieldref:
237 case CONSTANT_Methodref:
238 case CONSTANT_InterfaceMethodref:
239 case CONSTANT_Float:
240 case CONSTANT_Integer:
241 size += 4;
242 break;
243 case CONSTANT_Class:
244 case CONSTANT_String:
245 size += 2;
246 break;
247 case CONSTANT_Long:
248 case CONSTANT_Double:
249 size += 8;
250 i++;
251 break;
252 case CONSTANT_Utf8:
254 tree t = cpool->data[i].t;
255 int len = IDENTIFIER_LENGTH (t);
256 size += len + 2;
258 break;
259 default:
260 /* Second word of CONSTANT_Long and CONSTANT_Double. */
261 size--;
264 return size;
267 /* Write the constant pool CPOOL into BUFFER.
268 The length of BUFFER is LENGTH, which must match the needed length. */
270 void
271 write_constant_pool (CPool *cpool, unsigned char *buffer, int length)
273 unsigned char *ptr = buffer;
274 int i = 1;
275 union cpool_entry *datap = &cpool->data[1];
276 PUT2 (cpool->count);
277 for ( ; i < cpool->count; i++, datap++)
279 int tag = cpool->tags[i];
280 PUT1 (tag);
281 switch (tag)
283 case CONSTANT_NameAndType:
284 case CONSTANT_Fieldref:
285 case CONSTANT_Methodref:
286 case CONSTANT_InterfaceMethodref:
287 case CONSTANT_Float:
288 case CONSTANT_Integer:
289 PUT4 (datap->w);
290 break;
291 case CONSTANT_Class:
292 case CONSTANT_String:
293 PUT2 (datap->w);
294 break;
295 break;
296 case CONSTANT_Long:
297 case CONSTANT_Double:
298 PUT4(datap->w);
299 i++;
300 datap++;
301 PUT4 (datap->w);
302 break;
303 case CONSTANT_Utf8:
305 tree t = datap->t;
306 int len = IDENTIFIER_LENGTH (t);
307 PUT2 (len);
308 PUTN (IDENTIFIER_POINTER (t), len);
310 break;
314 if (ptr != buffer + length)
315 abort ();
318 CPool *outgoing_cpool;
320 static GTY(()) tree tag_nodes[13];
321 static tree
322 get_tag_node (int tag)
324 /* A Cache for build_int_2 (CONSTANT_XXX, 0). */
326 if (tag_nodes[tag] == NULL_TREE)
327 tag_nodes[tag] = build_int_2 (tag, 0);
328 return tag_nodes[tag];
331 /* Look for a constant pool entry that matches TAG and NAME.
332 Creates a new entry if not found.
333 TAG is one of CONSTANT_Utf8, CONSTANT_String or CONSTANT_Class.
334 NAME is an IDENTIFIER_NODE naming the Utf8 constant, string, or class.
335 Returns the index of the entry. */
338 alloc_name_constant (int tag, tree name)
340 return find_tree_constant (outgoing_cpool, tag, name);
343 /* Build an identifier for the internal name of reference type TYPE. */
345 tree
346 build_internal_class_name (tree type)
348 tree name;
349 if (TYPE_ARRAY_P (type))
350 name = build_java_signature (type);
351 else
353 name = TYPE_NAME (type);
354 if (TREE_CODE (name) != IDENTIFIER_NODE)
355 name = DECL_NAME (name);
356 name = identifier_subst (name, "", '.', '/', "");
358 return name;
361 /* Look for a CONSTANT_Class entry for CLAS, creating a new one if needed. */
364 alloc_class_constant (tree clas)
366 tree class_name = build_internal_class_name (clas);
368 return alloc_name_constant (CONSTANT_Class,
369 (unmangle_classname
370 (IDENTIFIER_POINTER(class_name),
371 IDENTIFIER_LENGTH(class_name))));
374 /* Return a reference to the data array of the current constant pool. */
376 static tree
377 build_constant_data_ref (void)
379 tree cpool_data_ref = NULL_TREE;
381 if (TYPE_CPOOL_DATA_REF (current_class))
382 cpool_data_ref = TYPE_CPOOL_DATA_REF (current_class);
384 if (cpool_data_ref == NULL_TREE)
386 tree decl;
387 tree decl_name = mangled_classname ("_CD_", current_class);
388 decl = build_decl (VAR_DECL, decl_name,
389 build_array_type (ptr_type_node,
390 one_elt_array_domain_type));
391 TREE_STATIC (decl) = 1;
392 make_decl_rtl (decl, NULL);
393 TYPE_CPOOL_DATA_REF (current_class) = cpool_data_ref
394 = build1 (ADDR_EXPR, ptr_type_node, decl);
396 return cpool_data_ref;
399 /* Get the pointer value at the INDEX'th element of the constant pool. */
401 tree
402 build_ref_from_constant_pool (int index)
404 tree t = build_constant_data_ref ();
405 index *= int_size_in_bytes (ptr_type_node);
406 t = fold (build (PLUS_EXPR, ptr_type_node,
407 t, build_int_2 (index, 0)));
408 return build1 (INDIRECT_REF, ptr_type_node, t);
411 /* Build an initializer for the constants field of the current constant pool.
412 Should only be called at top-level, since it may emit declarations. */
414 tree
415 build_constants_constructor (void)
417 tree tags_value, data_value;
418 tree cons;
419 tree tags_list = NULL_TREE;
420 tree data_list = NULL_TREE;
421 int i;
422 for (i = outgoing_cpool->count; --i > 0; )
424 tags_list
425 = tree_cons (NULL_TREE, get_tag_node (outgoing_cpool->tags[i]),
426 tags_list);
427 data_list
428 = tree_cons (NULL_TREE, build_utf8_ref (outgoing_cpool->data[i].t),
429 data_list);
431 if (outgoing_cpool->count > 0)
433 tree index_type;
434 tree data_decl, tags_decl, tags_type;
435 tree max_index = build_int_2 (outgoing_cpool->count - 1, 0);
436 TREE_TYPE (max_index) = sizetype;
437 index_type = build_index_type (max_index);
439 /* Add dummy 0'th element of constant pool. */
440 tags_list = tree_cons (NULL_TREE, get_tag_node (0), tags_list);
441 data_list = tree_cons (NULL_TREE, null_pointer_node, data_list);
443 data_decl = TREE_OPERAND (build_constant_data_ref (), 0);
444 TREE_TYPE (data_decl) = build_array_type (ptr_type_node, index_type),
445 DECL_INITIAL (data_decl) = build (CONSTRUCTOR, TREE_TYPE (data_decl),
446 NULL_TREE, data_list);
447 DECL_SIZE (data_decl) = TYPE_SIZE (TREE_TYPE (data_decl));
448 DECL_SIZE_UNIT (data_decl) = TYPE_SIZE_UNIT (TREE_TYPE (data_decl));
449 rest_of_decl_compilation (data_decl, (char *) 0, 1, 0);
450 data_value = build_address_of (data_decl);
452 tags_type = build_array_type (unsigned_byte_type_node, index_type);
453 tags_decl = build_decl (VAR_DECL, mangled_classname ("_CT_",
454 current_class),
455 tags_type);
456 TREE_STATIC (tags_decl) = 1;
457 DECL_INITIAL (tags_decl) = build (CONSTRUCTOR, tags_type,
458 NULL_TREE, tags_list);
459 rest_of_decl_compilation (tags_decl, (char*) 0, 1, 0);
460 tags_value = build_address_of (tags_decl);
462 else
464 data_value = null_pointer_node;
465 tags_value = null_pointer_node;
467 START_RECORD_CONSTRUCTOR (cons, constants_type_node);
468 PUSH_FIELD_VALUE (cons, "size", build_int_2 (outgoing_cpool->count, 0));
469 PUSH_FIELD_VALUE (cons, "tags", tags_value);
470 PUSH_FIELD_VALUE (cons, "data", data_value);
471 FINISH_RECORD_CONSTRUCTOR (cons);
472 return cons;
475 #include "gt-java-constants.h"