1 /* Handle the constant pool of the Java(TM) Virtual Machine.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003, 2004 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)
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. */
26 #include "coretypes.h"
30 #include "java-tree.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);
40 static CPool *cpool_for_class (tree);
42 /* Set the INDEX'th constant in CPOOL to have the given TAG and VALUE. */
45 set_constant_entry (CPool *cpool, int index, int tag, jword value)
47 if (cpool->data == NULL)
49 cpool->capacity = 100;
50 cpool->tags = ggc_alloc_cleared (sizeof(uint8) * cpool->capacity);
51 cpool->data = ggc_alloc_cleared (sizeof(union cpool_entry)
55 if (index >= cpool->capacity)
57 int old_cap = cpool->capacity;
59 if (index >= cpool->capacity)
60 cpool->capacity = index + 10;
61 cpool->tags = ggc_realloc (cpool->tags,
62 sizeof(uint8) * cpool->capacity);
63 cpool->data = ggc_realloc (cpool->data,
64 sizeof(union cpool_entry) * cpool->capacity);
66 /* Make sure GC never sees uninitialized tag values. */
67 memset (cpool->tags + old_cap, 0, cpool->capacity - old_cap);
68 memset (cpool->data + old_cap, 0,
69 (cpool->capacity - old_cap) * sizeof (union cpool_entry));
71 if (index >= cpool->count)
72 cpool->count = index + 1;
73 cpool->tags[index] = tag;
74 cpool->data[index].w = value;
77 /* Find (or create) a constant pool entry matching TAG and VALUE. */
80 find_constant1 (CPool *cpool, int tag, jword value)
83 for (i = cpool->count; --i > 0; )
85 if (cpool->tags[i] == tag && cpool->data[i].w == value)
88 i = cpool->count == 0 ? 1 : cpool->count;
89 set_constant_entry (cpool, i, tag, value);
93 /* Find a double-word constant pool entry matching TAG and WORD1/WORD2. */
96 find_constant2 (CPool *cpool, int tag, jword word1, jword word2)
99 for (i = cpool->count - 1; --i > 0; )
101 if (cpool->tags[i] == tag
102 && cpool->data[i].w == word1
103 && cpool->data[i+1].w == word2)
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);
113 find_tree_constant (CPool *cpool, int tag, tree value)
116 for (i = cpool->count; --i > 0; )
118 if (cpool->tags[i] == tag && cpool->data[i].t == value)
121 i = cpool->count == 0 ? 1 : cpool->count;
122 set_constant_entry (cpool, i, tag, 0);
123 cpool->data[i].t = value;
129 find_utf8_constant (CPool *cpool, tree name)
131 if (name == NULL_TREE)
133 return find_tree_constant (cpool, CONSTANT_Utf8, name);
137 find_class_or_string_constant (CPool *cpool, int tag, tree name)
139 jword j = find_utf8_constant (cpool, name);
141 for (i = cpool->count; --i > 0; )
143 if (cpool->tags[i] == tag && cpool->data[i].w == j)
147 set_constant_entry (cpool, i, tag, j);
152 find_class_constant (CPool *cpool, tree type)
154 return find_class_or_string_constant (cpool, CONSTANT_Class,
155 build_internal_class_name (type));
158 /* Allocate a CONSTANT_string entry given a STRING_CST. */
161 find_string_constant (CPool *cpool, tree string)
163 string = get_identifier (TREE_STRING_POINTER (string));
164 return find_class_or_string_constant (cpool, CONSTANT_String, string);
168 /* Find (or create) a CONSTANT_NameAndType matching NAME and TYPE.
169 Return its index in the constant pool CPOOL. */
172 find_name_and_type_constant (CPool *cpool, tree name, tree type)
174 int name_index = find_utf8_constant (cpool, name);
175 int type_index = find_utf8_constant (cpool, build_java_signature (type));
176 return find_constant1 (cpool, CONSTANT_NameAndType,
177 (name_index << 16) | type_index);
180 /* Find (or create) a CONSTANT_Fieldref for DECL (a FIELD_DECL or VAR_DECL).
181 Return its index in the constant pool CPOOL. */
184 find_fieldref_index (CPool *cpool, tree decl)
186 int class_index = find_class_constant (cpool, DECL_CONTEXT (decl));
188 = find_name_and_type_constant (cpool, DECL_NAME (decl), TREE_TYPE (decl));
189 return find_constant1 (cpool, CONSTANT_Fieldref,
190 (class_index << 16) | name_type_index);
193 /* Find (or create) a CONSTANT_Methodref for DECL (a FUNCTION_DECL).
194 Return its index in the constant pool CPOOL. */
197 find_methodref_index (CPool *cpool, tree decl)
199 return find_methodref_with_class_index (cpool, decl, DECL_CONTEXT (decl));
203 find_methodref_with_class_index (CPool *cpool, tree decl, tree mclass)
205 int class_index = find_class_constant (cpool, mclass);
206 tree name = DECL_CONSTRUCTOR_P (decl) ? init_identifier_node
210 find_name_and_type_constant (cpool, name, TREE_TYPE (decl));
211 return find_constant1 (cpool,
212 CLASS_INTERFACE (TYPE_NAME (mclass))
213 ? CONSTANT_InterfaceMethodref
214 : CONSTANT_Methodref,
215 (class_index << 16) | name_type_index);
218 #define PUT1(X) (*ptr++ = (X))
219 #define PUT2(X) (PUT1((X) >> 8), PUT1(X))
220 #define PUT4(X) (PUT2((X) >> 16), PUT2(X))
221 #define PUTN(P, N) (memcpy(ptr, (P), (N)), ptr += (N))
223 /* Give the number of bytes needed in a .class file for the CPOOL
224 constant pool. Includes the 2-byte constant_pool_count. */
227 count_constant_pool_bytes (CPool *cpool)
231 for ( ; i < cpool->count; i++)
234 switch (cpool->tags[i])
236 case CONSTANT_NameAndType:
237 case CONSTANT_Fieldref:
238 case CONSTANT_Methodref:
239 case CONSTANT_InterfaceMethodref:
241 case CONSTANT_Integer:
245 case CONSTANT_String:
249 case CONSTANT_Double:
255 tree t = cpool->data[i].t;
256 int len = IDENTIFIER_LENGTH (t);
261 /* Second word of CONSTANT_Long and CONSTANT_Double. */
268 /* Write the constant pool CPOOL into BUFFER.
269 The length of BUFFER is LENGTH, which must match the needed length. */
272 write_constant_pool (CPool *cpool, unsigned char *buffer, int length)
274 unsigned char *ptr = buffer;
276 union cpool_entry *datap = &cpool->data[1];
278 for ( ; i < cpool->count; i++, datap++)
280 int tag = cpool->tags[i];
284 case CONSTANT_NameAndType:
285 case CONSTANT_Fieldref:
286 case CONSTANT_Methodref:
287 case CONSTANT_InterfaceMethodref:
289 case CONSTANT_Integer:
293 case CONSTANT_String:
298 case CONSTANT_Double:
307 int len = IDENTIFIER_LENGTH (t);
309 PUTN (IDENTIFIER_POINTER (t), len);
315 if (ptr != buffer + length)
319 static GTY(()) tree tag_nodes[13];
321 get_tag_node (int tag)
323 /* A Cache for build_int_2 (CONSTANT_XXX, 0). */
325 if (tag_nodes[tag] == NULL_TREE)
326 tag_nodes[tag] = build_int_2 (tag, 0);
327 return tag_nodes[tag];
330 /* Given a class, return its constant pool, creating one if necessary. */
333 cpool_for_class (tree class)
335 CPool *cpool = TYPE_CPOOL (class);
339 cpool = ggc_alloc_cleared (sizeof (struct CPool));
340 TYPE_CPOOL (class) = cpool;
345 /* Look for a constant pool entry that matches TAG and NAME.
346 Creates a new entry if not found.
347 TAG is one of CONSTANT_Utf8, CONSTANT_String or CONSTANT_Class.
348 NAME is an IDENTIFIER_NODE naming the Utf8 constant, string, or class.
349 Returns the index of the entry. */
352 alloc_name_constant (int tag, tree name)
354 CPool *outgoing_cpool = cpool_for_class (output_class);
355 return find_tree_constant (outgoing_cpool, tag, name);
358 /* Build an identifier for the internal name of reference type TYPE. */
361 build_internal_class_name (tree type)
364 if (TYPE_ARRAY_P (type))
365 name = build_java_signature (type);
368 name = TYPE_NAME (type);
369 if (TREE_CODE (name) != IDENTIFIER_NODE)
370 name = DECL_NAME (name);
371 name = identifier_subst (name, "", '.', '/', "");
376 /* Look for a CONSTANT_Class entry for CLAS, creating a new one if needed. */
379 alloc_class_constant (tree clas)
381 tree class_name = build_internal_class_name (clas);
383 return alloc_name_constant (CONSTANT_Class,
385 (IDENTIFIER_POINTER(class_name),
386 IDENTIFIER_LENGTH(class_name))));
389 /* Return a reference to the data array of the current constant pool. */
392 build_constant_data_ref (void)
394 tree cpool_data_ref = NULL_TREE;
396 if (TYPE_CPOOL_DATA_REF (output_class))
397 cpool_data_ref = TYPE_CPOOL_DATA_REF (output_class);
399 if (cpool_data_ref == NULL_TREE)
402 tree decl_name = mangled_classname ("_CD_", output_class);
403 decl = build_decl (VAR_DECL, decl_name,
404 build_array_type (ptr_type_node,
405 one_elt_array_domain_type));
406 TREE_STATIC (decl) = 1;
407 make_decl_rtl (decl, NULL);
408 TYPE_CPOOL_DATA_REF (output_class) = cpool_data_ref
409 = build1 (ADDR_EXPR, ptr_type_node, decl);
411 return cpool_data_ref;
414 /* Get the pointer value at the INDEX'th element of the constant pool. */
417 build_ref_from_constant_pool (int index)
419 tree t = build_constant_data_ref ();
420 index *= int_size_in_bytes (ptr_type_node);
421 t = fold (build (PLUS_EXPR, ptr_type_node,
422 t, build_int_2 (index, 0)));
423 return build1 (INDIRECT_REF, ptr_type_node, t);
426 /* Build an initializer for the constants field of the current constant pool.
427 Should only be called at top-level, since it may emit declarations. */
430 build_constants_constructor (void)
432 CPool *outgoing_cpool = cpool_for_class (current_class);
433 tree tags_value, data_value;
435 tree tags_list = NULL_TREE;
436 tree data_list = NULL_TREE;
438 for (i = outgoing_cpool->count; --i > 0; )
441 = tree_cons (NULL_TREE, get_tag_node (outgoing_cpool->tags[i]),
444 = tree_cons (NULL_TREE, build_utf8_ref (outgoing_cpool->data[i].t),
447 if (outgoing_cpool->count > 0)
450 tree data_decl, tags_decl, tags_type;
451 tree max_index = build_int_2 (outgoing_cpool->count - 1, 0);
452 TREE_TYPE (max_index) = sizetype;
453 index_type = build_index_type (max_index);
455 /* Add dummy 0'th element of constant pool. */
456 tags_list = tree_cons (NULL_TREE, get_tag_node (0), tags_list);
457 data_list = tree_cons (NULL_TREE, null_pointer_node, data_list);
459 data_decl = TREE_OPERAND (build_constant_data_ref (), 0);
460 TREE_TYPE (data_decl) = build_array_type (ptr_type_node, index_type),
461 DECL_INITIAL (data_decl) = build_constructor (TREE_TYPE (data_decl),
463 DECL_SIZE (data_decl) = TYPE_SIZE (TREE_TYPE (data_decl));
464 DECL_SIZE_UNIT (data_decl) = TYPE_SIZE_UNIT (TREE_TYPE (data_decl));
465 rest_of_decl_compilation (data_decl, (char *) 0, 1, 0);
466 data_value = build_address_of (data_decl);
468 tags_type = build_array_type (unsigned_byte_type_node, index_type);
469 tags_decl = build_decl (VAR_DECL, mangled_classname ("_CT_",
472 TREE_STATIC (tags_decl) = 1;
473 DECL_INITIAL (tags_decl) = build_constructor (tags_type, tags_list);
474 rest_of_decl_compilation (tags_decl, (char*) 0, 1, 0);
475 tags_value = build_address_of (tags_decl);
479 data_value = null_pointer_node;
480 tags_value = null_pointer_node;
482 START_RECORD_CONSTRUCTOR (cons, constants_type_node);
483 PUSH_FIELD_VALUE (cons, "size", build_int_2 (outgoing_cpool->count, 0));
484 PUSH_FIELD_VALUE (cons, "tags", tags_value);
485 PUSH_FIELD_VALUE (cons, "data", data_value);
486 FINISH_RECORD_CONSTRUCTOR (cons);
490 #include "gt-java-constants.h"