1 /* Handle the constant pool of the Java(TM) Virtual Machine.
2 Copyright (C) 1997-2013 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 3, 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 COPYING3. If not see
17 <http://www.gnu.org/licenses/>.
19 Java and all Java-based marks are trademarks or registered trademarks
20 of Sun Microsystems, Inc. in the United States and other countries.
21 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25 #include "coretypes.h"
29 #include "java-tree.h"
30 #include "diagnostic-core.h"
34 static void set_constant_entry (CPool
*, int, int, jword
);
35 static int find_tree_constant (CPool
*, int, tree
);
36 static int find_name_and_type_constant (CPool
*, tree
, tree
);
37 static tree
get_tag_node (int);
39 /* Set the INDEX'th constant in CPOOL to have the given TAG and VALUE. */
42 set_constant_entry (CPool
*cpool
, int index
, int tag
, jword value
)
44 if (cpool
->data
== NULL
)
46 cpool
->capacity
= 100;
47 cpool
->tags
= (uint8
*) ggc_alloc_cleared_atomic (sizeof (uint8
)
49 cpool
->data
= ggc_alloc_cleared_vec_cpool_entry (sizeof
54 if (index
>= cpool
->capacity
)
56 int old_cap
= cpool
->capacity
;
58 if (index
>= cpool
->capacity
)
59 cpool
->capacity
= index
+ 10;
60 cpool
->tags
= GGC_RESIZEVEC (uint8
, cpool
->tags
, cpool
->capacity
);
61 cpool
->data
= GGC_RESIZEVEC (union cpool_entry
, cpool
->data
,
64 /* Make sure GC never sees uninitialized tag values. */
65 memset (cpool
->tags
+ old_cap
, 0, cpool
->capacity
- old_cap
);
66 memset (cpool
->data
+ old_cap
, 0,
67 (cpool
->capacity
- old_cap
) * sizeof (union cpool_entry
));
69 if (index
>= cpool
->count
)
70 cpool
->count
= index
+ 1;
71 cpool
->tags
[index
] = tag
;
72 cpool
->data
[index
].w
= value
;
75 /* Find (or create) a constant pool entry matching TAG and VALUE. */
78 find_constant1 (CPool
*cpool
, int tag
, jword value
)
81 for (i
= cpool
->count
; --i
> 0; )
83 if (cpool
->tags
[i
] == tag
&& cpool
->data
[i
].w
== value
)
86 i
= cpool
->count
== 0 ? 1 : cpool
->count
;
87 set_constant_entry (cpool
, i
, tag
, value
);
91 /* Find a double-word constant pool entry matching TAG and WORD1/WORD2. */
94 find_constant2 (CPool
*cpool
, int tag
, jword word1
, jword word2
)
97 for (i
= cpool
->count
- 1; --i
> 0; )
99 if (cpool
->tags
[i
] == tag
100 && cpool
->data
[i
].w
== word1
101 && cpool
->data
[i
+1].w
== word2
)
104 i
= cpool
->count
== 0 ? 1 : cpool
->count
;
105 set_constant_entry (cpool
, i
, tag
, word1
);
106 set_constant_entry (cpool
, i
+1, 0, word2
);
111 find_tree_constant (CPool
*cpool
, int tag
, tree value
)
114 for (i
= cpool
->count
; --i
> 0; )
116 if (cpool
->tags
[i
] == tag
&& cpool
->data
[i
].t
== value
)
119 i
= cpool
->count
== 0 ? 1 : cpool
->count
;
120 set_constant_entry (cpool
, i
, tag
, 0);
121 cpool
->data
[i
].t
= value
;
127 find_utf8_constant (CPool
*cpool
, tree name
)
129 if (name
== NULL_TREE
)
131 return find_tree_constant (cpool
, CONSTANT_Utf8
, name
);
135 find_class_or_string_constant (CPool
*cpool
, int tag
, tree name
)
137 jword j
= find_utf8_constant (cpool
, name
);
139 for (i
= cpool
->count
; --i
> 0; )
141 if (cpool
->tags
[i
] == tag
&& cpool
->data
[i
].w
== j
)
145 set_constant_entry (cpool
, i
, tag
, j
);
150 find_class_constant (CPool
*cpool
, tree type
)
152 return find_class_or_string_constant (cpool
, CONSTANT_Class
,
153 build_internal_class_name (type
));
156 /* Allocate a CONSTANT_string entry given a STRING_CST. */
159 find_string_constant (CPool
*cpool
, tree string
)
161 string
= get_identifier (TREE_STRING_POINTER (string
));
162 return find_class_or_string_constant (cpool
, CONSTANT_String
, string
);
166 /* Find (or create) a CONSTANT_NameAndType matching NAME and TYPE.
167 Return its index in the constant pool CPOOL. */
170 find_name_and_type_constant (CPool
*cpool
, tree name
, tree type
)
172 int name_index
= find_utf8_constant (cpool
, name
);
173 int type_index
= find_utf8_constant (cpool
, build_java_signature (type
));
174 return find_constant1 (cpool
, CONSTANT_NameAndType
,
175 (name_index
<< 16) | type_index
);
178 /* Find (or create) a CONSTANT_Fieldref for DECL (a FIELD_DECL or VAR_DECL).
179 Return its index in the constant pool CPOOL. */
182 find_fieldref_index (CPool
*cpool
, tree decl
)
184 int class_index
= find_class_constant (cpool
, DECL_CONTEXT (decl
));
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
*cpool
, tree decl
)
197 return find_methodref_with_class_index (cpool
, decl
, DECL_CONTEXT (decl
));
201 find_methodref_with_class_index (CPool
*cpool
, tree decl
, tree mclass
)
203 int class_index
= find_class_constant (cpool
, mclass
);
204 tree name
= DECL_CONSTRUCTOR_P (decl
) ? init_identifier_node
208 find_name_and_type_constant (cpool
, name
, TREE_TYPE (decl
));
209 return find_constant1 (cpool
,
210 CLASS_INTERFACE (TYPE_NAME (mclass
))
211 ? CONSTANT_InterfaceMethodref
212 : CONSTANT_Methodref
,
213 (class_index
<< 16) | name_type_index
);
216 #define PUT1(X) (*ptr++ = (X))
217 #define PUT2(X) (PUT1((X) >> 8), PUT1(X))
218 #define PUT4(X) (PUT2((X) >> 16), PUT2(X))
219 #define PUTN(P, N) (memcpy(ptr, (P), (N)), ptr += (N))
221 /* Give the number of bytes needed in a .class file for the CPOOL
222 constant pool. Includes the 2-byte constant_pool_count. */
225 count_constant_pool_bytes (CPool
*cpool
)
229 for ( ; i
< cpool
->count
; i
++)
232 switch (cpool
->tags
[i
])
234 case CONSTANT_NameAndType
:
235 case CONSTANT_Fieldref
:
236 case CONSTANT_Methodref
:
237 case CONSTANT_InterfaceMethodref
:
239 case CONSTANT_Integer
:
243 case CONSTANT_String
:
247 case CONSTANT_Double
:
253 tree t
= cpool
->data
[i
].t
;
254 int len
= IDENTIFIER_LENGTH (t
);
259 /* Second word of CONSTANT_Long and CONSTANT_Double. */
266 /* Write the constant pool CPOOL into BUFFER.
267 The length of BUFFER is LENGTH, which must match the needed length. */
270 write_constant_pool (CPool
*cpool
, unsigned char *buffer
, int length
)
272 unsigned char *ptr
= buffer
;
274 union cpool_entry
*datap
= &cpool
->data
[1];
276 for ( ; i
< cpool
->count
; i
++, datap
++)
278 int tag
= cpool
->tags
[i
];
282 case CONSTANT_NameAndType
:
283 case CONSTANT_Fieldref
:
284 case CONSTANT_Methodref
:
285 case CONSTANT_InterfaceMethodref
:
287 case CONSTANT_Integer
:
291 case CONSTANT_String
:
296 case CONSTANT_Double
:
305 int len
= IDENTIFIER_LENGTH (t
);
307 PUTN (IDENTIFIER_POINTER (t
), len
);
313 gcc_assert (ptr
== buffer
+ length
);
316 static GTY(()) tree tag_nodes
[13];
318 get_tag_node (int tag
)
320 /* A Cache for build_int_cst (CONSTANT_XXX, 0). */
323 return build_int_cst (NULL_TREE
, tag
);
325 if (tag_nodes
[tag
] == NULL_TREE
)
326 tag_nodes
[tag
] = build_int_cst (NULL_TREE
, tag
);
327 return tag_nodes
[tag
];
330 /* Given a class, return its constant pool, creating one if necessary. */
333 cpool_for_class (tree klass
)
335 CPool
*cpool
= TYPE_CPOOL (klass
);
339 cpool
= ggc_alloc_cleared_CPool ();
340 TYPE_CPOOL (klass
) = 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 /* Create a constant pool entry for a name_and_type. This one has '.'
359 rather than '/' because it isn't going into a class file, it's
360 going into a compiled object. We don't use the '/' separator in
364 find_name_and_type_constant_tree (CPool
*cpool
, tree name
, tree type
)
366 int name_index
= find_utf8_constant (cpool
, name
);
368 = find_utf8_constant (cpool
,
369 identifier_subst (build_java_signature (type
),
371 return find_constant1 (cpool
, CONSTANT_NameAndType
,
372 (name_index
<< 16) | type_index
);
375 /* Look for a field ref that matches DECL in the constant pool of
377 Return the index of the entry. */
380 alloc_constant_fieldref (tree klass
, tree decl
)
382 CPool
*outgoing_cpool
= cpool_for_class (klass
);
384 = find_tree_constant (outgoing_cpool
, CONSTANT_Class
,
385 DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl
))));
387 = find_name_and_type_constant_tree (outgoing_cpool
, DECL_NAME (decl
),
389 return find_constant1 (outgoing_cpool
, CONSTANT_Fieldref
,
390 (class_index
<< 16) | name_type_index
);
393 /* Build an identifier for the internal name of reference type TYPE. */
396 build_internal_class_name (tree type
)
399 if (TYPE_ARRAY_P (type
))
400 name
= build_java_signature (type
);
403 name
= TYPE_NAME (type
);
404 if (TREE_CODE (name
) != IDENTIFIER_NODE
)
405 name
= DECL_NAME (name
);
406 name
= identifier_subst (name
, "", '.', '/', "");
411 /* Look for a CONSTANT_Class entry for CLAS, creating a new one if needed. */
414 alloc_class_constant (tree clas
)
416 tree class_name
= build_internal_class_name (clas
);
418 return alloc_name_constant (CONSTANT_Class
,
420 (IDENTIFIER_POINTER(class_name
),
421 IDENTIFIER_LENGTH(class_name
))));
424 /* Return the decl of the data array of the current constant pool. */
427 build_constant_data_ref (bool indirect
)
432 tree cpool_type
= build_array_type (ptr_type_node
, NULL_TREE
);
433 tree decl
= build_class_ref (output_class
);
434 tree klass
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (decl
)),
436 tree constants
= build3 (COMPONENT_REF
,
437 TREE_TYPE (constants_field_decl_node
), klass
,
438 constants_field_decl_node
,
440 tree data
= build3 (COMPONENT_REF
,
441 TREE_TYPE (constants_data_field_decl_node
),
443 constants_data_field_decl_node
,
446 TREE_THIS_NOTRAP (klass
) = 1;
447 data
= fold_convert (build_pointer_type (cpool_type
), data
);
448 d
= build1 (INDIRECT_REF
, cpool_type
, data
);
454 tree decl_name
= mangled_classname ("_CD_", output_class
);
455 tree decl
= IDENTIFIER_GLOBAL_VALUE (decl_name
);
459 /* Build a type with unspecified bounds. The will make sure
460 that targets do the right thing with whatever size we end
461 up with at the end. Using bounds that are too small risks
462 assuming the data is in the small data section. */
463 tree type
= build_array_type (ptr_type_node
, NULL_TREE
);
465 /* We need to lay out the type ourselves, since build_array_type
466 thinks the type is incomplete. */
469 decl
= build_decl (input_location
, VAR_DECL
, decl_name
, type
);
470 TREE_STATIC (decl
) = 1;
471 IDENTIFIER_GLOBAL_VALUE (decl_name
) = decl
;
478 /* Get the pointer value at the INDEX'th element of the constant pool. */
481 build_ref_from_constant_pool (int index
)
484 tree d
= TYPE_CPOOL_DATA_REF (output_class
);
487 d
= build_constant_data_ref (flag_indirect_classes
);
489 i
= build_int_cst (NULL_TREE
, index
);
490 d
= build4 (ARRAY_REF
, TREE_TYPE (TREE_TYPE (d
)), d
, i
,
491 NULL_TREE
, NULL_TREE
);
495 /* Build an initializer for the constants field of the current constant pool.
496 Should only be called at top-level, since it may emit declarations. */
499 build_constants_constructor (void)
501 CPool
*outgoing_cpool
= cpool_for_class (current_class
);
502 tree tags_value
, data_value
;
504 vec
<constructor_elt
, va_gc
> *v
= NULL
;
506 vec
<constructor_elt
, va_gc
> *tags
= NULL
;
507 vec
<constructor_elt
, va_gc
> *data
= NULL
;
508 constructor_elt
*t
= NULL
;
509 constructor_elt
*d
= NULL
;
511 if (outgoing_cpool
->count
> 0)
513 int c
= outgoing_cpool
->count
;
514 vec_safe_grow_cleared (tags
, c
);
515 vec_safe_grow_cleared (data
, c
);
520 #define CONSTRUCTOR_PREPEND_VALUE(E, V) E->value = V, E--
521 for (i
= outgoing_cpool
->count
; --i
> 0; )
522 switch (outgoing_cpool
->tags
[i
] & ~CONSTANT_LazyFlag
)
524 case CONSTANT_None
: /* The second half of a Double or Long on a
526 case CONSTANT_Fieldref
:
527 case CONSTANT_NameAndType
:
529 case CONSTANT_Integer
:
530 case CONSTANT_Double
:
532 case CONSTANT_Methodref
:
533 case CONSTANT_InterfaceMethodref
:
535 unsigned HOST_WIDE_INT temp
= outgoing_cpool
->data
[i
].w
;
537 /* Make sure that on a big-endian machine with 64-bit
538 pointers this 32-bit jint appears in the first word.
539 FIXME: This is a kludge. The field we're initializing is
540 not a scalar but a union, and that's how we should
541 represent it in the compiler. We should fix this. */
542 if (BYTES_BIG_ENDIAN
)
543 temp
<<= ((POINTER_SIZE
> 32) ? POINTER_SIZE
- 32 : 0);
545 CONSTRUCTOR_PREPEND_VALUE (t
, get_tag_node (outgoing_cpool
->tags
[i
]));
546 CONSTRUCTOR_PREPEND_VALUE (d
, build_int_cst (ptr_type_node
, temp
));
551 case CONSTANT_String
:
552 case CONSTANT_Unicode
:
554 CONSTRUCTOR_PREPEND_VALUE (t
, get_tag_node (outgoing_cpool
->tags
[i
]));
555 CONSTRUCTOR_PREPEND_VALUE (d
, build_utf8_ref (outgoing_cpool
->data
[i
].t
));
561 #undef CONSTRUCTOR_PREPEND_VALUE
563 if (outgoing_cpool
->count
> 0)
565 tree data_decl
, tags_decl
, tags_type
;
566 tree max_index
= build_int_cst (sizetype
, outgoing_cpool
->count
- 1);
567 tree index_type
= build_index_type (max_index
);
570 /* Add dummy 0'th element of constant pool. */
571 gcc_assert (t
== tags
->address ());
572 gcc_assert (d
== data
->address ());
573 t
->value
= get_tag_node (0);
574 d
->value
= null_pointer_node
;
576 /* Change the type of the decl to have the proper array size.
577 ??? Make sure to transition the old type-pointer-to list to this
578 new type to not invalidate all build address expressions. */
579 data_decl
= build_constant_data_ref (false);
580 tem
= TYPE_POINTER_TO (TREE_TYPE (data_decl
));
582 tem
= build_pointer_type (TREE_TYPE (data_decl
));
583 TYPE_POINTER_TO (TREE_TYPE (data_decl
)) = NULL_TREE
;
584 TREE_TYPE (data_decl
) = build_array_type (ptr_type_node
, index_type
);
585 TYPE_POINTER_TO (TREE_TYPE (data_decl
)) = tem
;
586 DECL_INITIAL (data_decl
) = build_constructor (TREE_TYPE (data_decl
), data
);
587 DECL_SIZE (data_decl
) = TYPE_SIZE (TREE_TYPE (data_decl
));
588 DECL_SIZE_UNIT (data_decl
) = TYPE_SIZE_UNIT (TREE_TYPE (data_decl
));
589 rest_of_decl_compilation (data_decl
, 1, 0);
590 data_value
= build_address_of (data_decl
);
592 tags_type
= build_array_type (unsigned_byte_type_node
, index_type
);
593 tags_decl
= build_decl (input_location
,
594 VAR_DECL
, mangled_classname ("_CT_",
597 TREE_STATIC (tags_decl
) = 1;
598 DECL_INITIAL (tags_decl
) = build_constructor (tags_type
, tags
);
599 rest_of_decl_compilation (tags_decl
, 1, 0);
600 tags_value
= build_address_of (tags_decl
);
604 data_value
= null_pointer_node
;
605 tags_value
= null_pointer_node
;
607 START_RECORD_CONSTRUCTOR (v
, constants_type_node
);
608 PUSH_FIELD_VALUE (v
, "size",
609 build_int_cst (NULL_TREE
, outgoing_cpool
->count
));
610 PUSH_FIELD_VALUE (v
, "tags", tags_value
);
611 PUSH_FIELD_VALUE (v
, "data", data_value
);
612 FINISH_RECORD_CONSTRUCTOR (cons
, v
, constants_type_node
);
616 #include "gt-java-constants.h"