ipa-cp.c (ipcp_cloning_candidate_p): Use opt_for_fn.
[gcc.git] / gcc / java / constants.c
1 /* Handle the constant pool of the Java(TM) Virtual Machine.
2 Copyright (C) 1997-2014 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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)
9 any later version.
10
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/>.
18
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. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "jcf.h"
28 #include "tree.h"
29 #include "stringpool.h"
30 #include "stor-layout.h"
31 #include "java-tree.h"
32 #include "diagnostic-core.h"
33 #include "toplev.h"
34 #include "ggc.h"
35
36 static void set_constant_entry (CPool *, int, int, jword);
37 static int find_tree_constant (CPool *, int, tree);
38 static int find_name_and_type_constant (CPool *, tree, tree);
39 static tree get_tag_node (int);
40
41 /* Set the INDEX'th constant in CPOOL to have the given TAG and VALUE. */
42
43 static void
44 set_constant_entry (CPool *cpool, int index, int tag, jword value)
45 {
46 if (cpool->data == NULL)
47 {
48 cpool->capacity = 100;
49 cpool->tags = ggc_cleared_vec_alloc<uint8> (cpool->capacity);
50 cpool->data = ggc_cleared_vec_alloc<cpool_entry> (cpool->capacity);
51 cpool->count = 1;
52 }
53 if (index >= cpool->capacity)
54 {
55 int old_cap = cpool->capacity;
56 cpool->capacity *= 2;
57 if (index >= cpool->capacity)
58 cpool->capacity = index + 10;
59 cpool->tags = GGC_RESIZEVEC (uint8, cpool->tags, cpool->capacity);
60 cpool->data = GGC_RESIZEVEC (union cpool_entry, cpool->data,
61 cpool->capacity);
62
63 /* Make sure GC never sees uninitialized tag values. */
64 memset (cpool->tags + old_cap, 0, cpool->capacity - old_cap);
65 memset (cpool->data + old_cap, 0,
66 (cpool->capacity - old_cap) * sizeof (union cpool_entry));
67 }
68 if (index >= cpool->count)
69 cpool->count = index + 1;
70 cpool->tags[index] = tag;
71 cpool->data[index].w = value;
72 }
73
74 /* Find (or create) a constant pool entry matching TAG and VALUE. */
75
76 int
77 find_constant1 (CPool *cpool, int tag, jword value)
78 {
79 int i;
80 for (i = cpool->count; --i > 0; )
81 {
82 if (cpool->tags[i] == tag && cpool->data[i].w == value)
83 return i;
84 }
85 i = cpool->count == 0 ? 1 : cpool->count;
86 set_constant_entry (cpool, i, tag, value);
87 return i;
88 }
89
90 /* Find a double-word constant pool entry matching TAG and WORD1/WORD2. */
91
92 int
93 find_constant2 (CPool *cpool, int tag, jword word1, jword word2)
94 {
95 int i;
96 for (i = cpool->count - 1; --i > 0; )
97 {
98 if (cpool->tags[i] == tag
99 && cpool->data[i].w == word1
100 && cpool->data[i+1].w == word2)
101 return i;
102 }
103 i = cpool->count == 0 ? 1 : cpool->count;
104 set_constant_entry (cpool, i, tag, word1);
105 set_constant_entry (cpool, i+1, 0, word2);
106 return i;
107 }
108
109 static int
110 find_tree_constant (CPool *cpool, int tag, tree value)
111 {
112 int i;
113 for (i = cpool->count; --i > 0; )
114 {
115 if (cpool->tags[i] == tag && cpool->data[i].t == value)
116 return i;
117 }
118 i = cpool->count == 0 ? 1 : cpool->count;
119 set_constant_entry (cpool, i, tag, 0);
120 cpool->data[i].t = value;
121 return i;
122 }
123
124
125 int
126 find_utf8_constant (CPool *cpool, tree name)
127 {
128 if (name == NULL_TREE)
129 return 0;
130 return find_tree_constant (cpool, CONSTANT_Utf8, name);
131 }
132
133 int
134 find_class_or_string_constant (CPool *cpool, int tag, tree name)
135 {
136 jword j = find_utf8_constant (cpool, name);
137 int i;
138 for (i = cpool->count; --i > 0; )
139 {
140 if (cpool->tags[i] == tag && cpool->data[i].w == j)
141 return i;
142 }
143 i = cpool->count;
144 set_constant_entry (cpool, i, tag, j);
145 return i;
146 }
147
148 int
149 find_class_constant (CPool *cpool, tree type)
150 {
151 return find_class_or_string_constant (cpool, CONSTANT_Class,
152 build_internal_class_name (type));
153 }
154
155 /* Allocate a CONSTANT_string entry given a STRING_CST. */
156
157 int
158 find_string_constant (CPool *cpool, tree string)
159 {
160 string = get_identifier (TREE_STRING_POINTER (string));
161 return find_class_or_string_constant (cpool, CONSTANT_String, string);
162
163 }
164
165 /* Find (or create) a CONSTANT_NameAndType matching NAME and TYPE.
166 Return its index in the constant pool CPOOL. */
167
168 static int
169 find_name_and_type_constant (CPool *cpool, tree name, tree type)
170 {
171 int name_index = find_utf8_constant (cpool, name);
172 int type_index = find_utf8_constant (cpool, build_java_signature (type));
173 return find_constant1 (cpool, CONSTANT_NameAndType,
174 (name_index << 16) | type_index);
175 }
176
177 /* Find (or create) a CONSTANT_Fieldref for DECL (a FIELD_DECL or VAR_DECL).
178 Return its index in the constant pool CPOOL. */
179
180 int
181 find_fieldref_index (CPool *cpool, tree decl)
182 {
183 int class_index = find_class_constant (cpool, DECL_CONTEXT (decl));
184 int name_type_index
185 = find_name_and_type_constant (cpool, DECL_NAME (decl), TREE_TYPE (decl));
186 return find_constant1 (cpool, CONSTANT_Fieldref,
187 (class_index << 16) | name_type_index);
188 }
189
190 /* Find (or create) a CONSTANT_Methodref for DECL (a FUNCTION_DECL).
191 Return its index in the constant pool CPOOL. */
192
193 int
194 find_methodref_index (CPool *cpool, tree decl)
195 {
196 return find_methodref_with_class_index (cpool, decl, DECL_CONTEXT (decl));
197 }
198
199 int
200 find_methodref_with_class_index (CPool *cpool, tree decl, tree mclass)
201 {
202 int class_index = find_class_constant (cpool, mclass);
203 tree name = DECL_CONSTRUCTOR_P (decl) ? init_identifier_node
204 : DECL_NAME (decl);
205 int name_type_index;
206 name_type_index =
207 find_name_and_type_constant (cpool, name, TREE_TYPE (decl));
208 return find_constant1 (cpool,
209 CLASS_INTERFACE (TYPE_NAME (mclass))
210 ? CONSTANT_InterfaceMethodref
211 : CONSTANT_Methodref,
212 (class_index << 16) | name_type_index);
213 }
214
215 #define PUT1(X) (*ptr++ = (X))
216 #define PUT2(X) (PUT1((X) >> 8), PUT1(X))
217 #define PUT4(X) (PUT2((X) >> 16), PUT2(X))
218 #define PUTN(P, N) (memcpy(ptr, (P), (N)), ptr += (N))
219
220 /* Give the number of bytes needed in a .class file for the CPOOL
221 constant pool. Includes the 2-byte constant_pool_count. */
222
223 int
224 count_constant_pool_bytes (CPool *cpool)
225 {
226 int size = 2;
227 int i = 1;
228 for ( ; i < cpool->count; i++)
229 {
230 size++;
231 switch (cpool->tags[i])
232 {
233 case CONSTANT_NameAndType:
234 case CONSTANT_Fieldref:
235 case CONSTANT_Methodref:
236 case CONSTANT_InterfaceMethodref:
237 case CONSTANT_Float:
238 case CONSTANT_Integer:
239 size += 4;
240 break;
241 case CONSTANT_Class:
242 case CONSTANT_String:
243 size += 2;
244 break;
245 case CONSTANT_Long:
246 case CONSTANT_Double:
247 size += 8;
248 i++;
249 break;
250 case CONSTANT_Utf8:
251 {
252 tree t = cpool->data[i].t;
253 int len = IDENTIFIER_LENGTH (t);
254 size += len + 2;
255 }
256 break;
257 default:
258 /* Second word of CONSTANT_Long and CONSTANT_Double. */
259 size--;
260 }
261 }
262 return size;
263 }
264
265 /* Write the constant pool CPOOL into BUFFER.
266 The length of BUFFER is LENGTH, which must match the needed length. */
267
268 void
269 write_constant_pool (CPool *cpool, unsigned char *buffer, int length)
270 {
271 unsigned char *ptr = buffer;
272 int i = 1;
273 union cpool_entry *datap = &cpool->data[1];
274 PUT2 (cpool->count);
275 for ( ; i < cpool->count; i++, datap++)
276 {
277 int tag = cpool->tags[i];
278 PUT1 (tag);
279 switch (tag)
280 {
281 case CONSTANT_NameAndType:
282 case CONSTANT_Fieldref:
283 case CONSTANT_Methodref:
284 case CONSTANT_InterfaceMethodref:
285 case CONSTANT_Float:
286 case CONSTANT_Integer:
287 PUT4 (datap->w);
288 break;
289 case CONSTANT_Class:
290 case CONSTANT_String:
291 PUT2 (datap->w);
292 break;
293 break;
294 case CONSTANT_Long:
295 case CONSTANT_Double:
296 PUT4(datap->w);
297 i++;
298 datap++;
299 PUT4 (datap->w);
300 break;
301 case CONSTANT_Utf8:
302 {
303 tree t = datap->t;
304 int len = IDENTIFIER_LENGTH (t);
305 PUT2 (len);
306 PUTN (IDENTIFIER_POINTER (t), len);
307 }
308 break;
309 }
310 }
311
312 gcc_assert (ptr == buffer + length);
313 }
314
315 static GTY(()) tree tag_nodes[13];
316 static tree
317 get_tag_node (int tag)
318 {
319 /* A Cache for build_int_cst (CONSTANT_XXX, 0). */
320
321 if (tag >= 13)
322 return build_int_cst (NULL_TREE, tag);
323
324 if (tag_nodes[tag] == NULL_TREE)
325 tag_nodes[tag] = build_int_cst (NULL_TREE, tag);
326 return tag_nodes[tag];
327 }
328
329 /* Given a class, return its constant pool, creating one if necessary. */
330
331 CPool *
332 cpool_for_class (tree klass)
333 {
334 CPool *cpool = TYPE_CPOOL (klass);
335
336 if (cpool == NULL)
337 {
338 cpool = ggc_cleared_alloc<CPool> ();
339 TYPE_CPOOL (klass) = cpool;
340 }
341 return cpool;
342 }
343
344 /* Look for a constant pool entry that matches TAG and NAME.
345 Creates a new entry if not found.
346 TAG is one of CONSTANT_Utf8, CONSTANT_String or CONSTANT_Class.
347 NAME is an IDENTIFIER_NODE naming the Utf8 constant, string, or class.
348 Returns the index of the entry. */
349
350 int
351 alloc_name_constant (int tag, tree name)
352 {
353 CPool *outgoing_cpool = cpool_for_class (output_class);
354 return find_tree_constant (outgoing_cpool, tag, name);
355 }
356
357 /* Create a constant pool entry for a name_and_type. This one has '.'
358 rather than '/' because it isn't going into a class file, it's
359 going into a compiled object. We don't use the '/' separator in
360 compiled objects. */
361
362 static int
363 find_name_and_type_constant_tree (CPool *cpool, tree name, tree type)
364 {
365 int name_index = find_utf8_constant (cpool, name);
366 int type_index
367 = find_utf8_constant (cpool,
368 identifier_subst (build_java_signature (type),
369 "", '/', '.', ""));
370 return find_constant1 (cpool, CONSTANT_NameAndType,
371 (name_index << 16) | type_index);
372 }
373
374 /* Look for a field ref that matches DECL in the constant pool of
375 KLASS.
376 Return the index of the entry. */
377
378 int
379 alloc_constant_fieldref (tree klass, tree decl)
380 {
381 CPool *outgoing_cpool = cpool_for_class (klass);
382 int class_index
383 = find_tree_constant (outgoing_cpool, CONSTANT_Class,
384 DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))));
385 int name_type_index
386 = find_name_and_type_constant_tree (outgoing_cpool, DECL_NAME (decl),
387 TREE_TYPE (decl));
388 return find_constant1 (outgoing_cpool, CONSTANT_Fieldref,
389 (class_index << 16) | name_type_index);
390 }
391
392 /* Build an identifier for the internal name of reference type TYPE. */
393
394 tree
395 build_internal_class_name (tree type)
396 {
397 tree name;
398 if (TYPE_ARRAY_P (type))
399 name = build_java_signature (type);
400 else
401 {
402 name = TYPE_NAME (type);
403 if (TREE_CODE (name) != IDENTIFIER_NODE)
404 name = DECL_NAME (name);
405 name = identifier_subst (name, "", '.', '/', "");
406 }
407 return name;
408 }
409
410 /* Look for a CONSTANT_Class entry for CLAS, creating a new one if needed. */
411
412 int
413 alloc_class_constant (tree clas)
414 {
415 tree class_name = build_internal_class_name (clas);
416
417 return alloc_name_constant (CONSTANT_Class,
418 (unmangle_classname
419 (IDENTIFIER_POINTER(class_name),
420 IDENTIFIER_LENGTH(class_name))));
421 }
422
423 /* Return the decl of the data array of the current constant pool. */
424
425 tree
426 build_constant_data_ref (bool indirect)
427 {
428 if (indirect)
429 {
430 tree d;
431 tree cpool_type = build_array_type (ptr_type_node, NULL_TREE);
432 tree decl = build_class_ref (output_class);
433 tree klass = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (decl)),
434 decl);
435 tree constants = build3 (COMPONENT_REF,
436 TREE_TYPE (constants_field_decl_node), klass,
437 constants_field_decl_node,
438 NULL_TREE);
439 tree data = build3 (COMPONENT_REF,
440 TREE_TYPE (constants_data_field_decl_node),
441 constants,
442 constants_data_field_decl_node,
443 NULL_TREE);
444
445 TREE_THIS_NOTRAP (klass) = 1;
446 data = fold_convert (build_pointer_type (cpool_type), data);
447 d = build1 (INDIRECT_REF, cpool_type, data);
448
449 return d;
450 }
451 else
452 {
453 tree decl_name = mangled_classname ("_CD_", output_class);
454 tree decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
455
456 if (! decl)
457 {
458 /* Build a type with unspecified bounds. The will make sure
459 that targets do the right thing with whatever size we end
460 up with at the end. Using bounds that are too small risks
461 assuming the data is in the small data section. */
462 tree type = build_array_type (ptr_type_node, NULL_TREE);
463
464 /* We need to lay out the type ourselves, since build_array_type
465 thinks the type is incomplete. */
466 layout_type (type);
467
468 decl = build_decl (input_location, VAR_DECL, decl_name, type);
469 TREE_STATIC (decl) = 1;
470 IDENTIFIER_GLOBAL_VALUE (decl_name) = decl;
471 }
472
473 return decl;
474 }
475 }
476
477 /* Get the pointer value at the INDEX'th element of the constant pool. */
478
479 tree
480 build_ref_from_constant_pool (int index)
481 {
482 tree i;
483 tree d = TYPE_CPOOL_DATA_REF (output_class);
484
485 if (d == NULL_TREE)
486 d = build_constant_data_ref (flag_indirect_classes);
487
488 i = build_int_cst (NULL_TREE, index);
489 d = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (d)), d, i,
490 NULL_TREE, NULL_TREE);
491 return d;
492 }
493
494 /* Build an initializer for the constants field of the current constant pool.
495 Should only be called at top-level, since it may emit declarations. */
496
497 tree
498 build_constants_constructor (void)
499 {
500 CPool *outgoing_cpool = cpool_for_class (current_class);
501 tree tags_value, data_value;
502 tree cons;
503 vec<constructor_elt, va_gc> *v = NULL;
504 int i;
505 vec<constructor_elt, va_gc> *tags = NULL;
506 vec<constructor_elt, va_gc> *data = NULL;
507 constructor_elt *t = NULL;
508 constructor_elt *d = NULL;
509
510 if (outgoing_cpool->count > 0)
511 {
512 int c = outgoing_cpool->count;
513 vec_safe_grow_cleared (tags, c);
514 vec_safe_grow_cleared (data, c);
515 t = &(*tags)[c-1];
516 d = &(*data)[c-1];
517 }
518
519 #define CONSTRUCTOR_PREPEND_VALUE(E, V) E->value = V, E--
520 for (i = outgoing_cpool->count; --i > 0; )
521 switch (outgoing_cpool->tags[i] & ~CONSTANT_LazyFlag)
522 {
523 case CONSTANT_None: /* The second half of a Double or Long on a
524 32-bit target. */
525 case CONSTANT_Fieldref:
526 case CONSTANT_NameAndType:
527 case CONSTANT_Float:
528 case CONSTANT_Integer:
529 case CONSTANT_Double:
530 case CONSTANT_Long:
531 case CONSTANT_Methodref:
532 case CONSTANT_InterfaceMethodref:
533 {
534 unsigned HOST_WIDE_INT temp = outgoing_cpool->data[i].w;
535
536 /* Make sure that on a big-endian machine with 64-bit
537 pointers this 32-bit jint appears in the first word.
538 FIXME: This is a kludge. The field we're initializing is
539 not a scalar but a union, and that's how we should
540 represent it in the compiler. We should fix this. */
541 if (BYTES_BIG_ENDIAN)
542 temp <<= ((POINTER_SIZE > 32) ? POINTER_SIZE - 32 : 0);
543
544 CONSTRUCTOR_PREPEND_VALUE (t, get_tag_node (outgoing_cpool->tags[i]));
545 CONSTRUCTOR_PREPEND_VALUE (d, build_int_cst (ptr_type_node, temp));
546 }
547 break;
548
549 case CONSTANT_Class:
550 case CONSTANT_String:
551 case CONSTANT_Unicode:
552 case CONSTANT_Utf8:
553 CONSTRUCTOR_PREPEND_VALUE (t, get_tag_node (outgoing_cpool->tags[i]));
554 CONSTRUCTOR_PREPEND_VALUE (d, build_utf8_ref (outgoing_cpool->data[i].t));
555 break;
556
557 default:
558 gcc_assert (false);
559 }
560 #undef CONSTRUCTOR_PREPEND_VALUE
561
562 if (outgoing_cpool->count > 0)
563 {
564 tree data_decl, tags_decl, tags_type;
565 tree max_index = build_int_cst (sizetype, outgoing_cpool->count - 1);
566 tree index_type = build_index_type (max_index);
567 tree tem;
568
569 /* Add dummy 0'th element of constant pool. */
570 gcc_assert (t == tags->address ());
571 gcc_assert (d == data->address ());
572 t->value = get_tag_node (0);
573 d->value = null_pointer_node;
574
575 /* Change the type of the decl to have the proper array size.
576 ??? Make sure to transition the old type-pointer-to list to this
577 new type to not invalidate all build address expressions. */
578 data_decl = build_constant_data_ref (false);
579 tem = TYPE_POINTER_TO (TREE_TYPE (data_decl));
580 if (!tem)
581 tem = build_pointer_type (TREE_TYPE (data_decl));
582 TYPE_POINTER_TO (TREE_TYPE (data_decl)) = NULL_TREE;
583 TREE_TYPE (data_decl) = build_array_type (ptr_type_node, index_type);
584 TYPE_POINTER_TO (TREE_TYPE (data_decl)) = tem;
585 DECL_INITIAL (data_decl) = build_constructor (TREE_TYPE (data_decl), data);
586 DECL_SIZE (data_decl) = TYPE_SIZE (TREE_TYPE (data_decl));
587 DECL_SIZE_UNIT (data_decl) = TYPE_SIZE_UNIT (TREE_TYPE (data_decl));
588 rest_of_decl_compilation (data_decl, 1, 0);
589 data_value = build_address_of (data_decl);
590
591 tags_type = build_array_type (unsigned_byte_type_node, index_type);
592 tags_decl = build_decl (input_location,
593 VAR_DECL, mangled_classname ("_CT_",
594 current_class),
595 tags_type);
596 TREE_STATIC (tags_decl) = 1;
597 DECL_INITIAL (tags_decl) = build_constructor (tags_type, tags);
598 rest_of_decl_compilation (tags_decl, 1, 0);
599 tags_value = build_address_of (tags_decl);
600 }
601 else
602 {
603 data_value = null_pointer_node;
604 tags_value = null_pointer_node;
605 }
606 START_RECORD_CONSTRUCTOR (v, constants_type_node);
607 PUSH_FIELD_VALUE (v, "size",
608 build_int_cst (NULL_TREE, outgoing_cpool->count));
609 PUSH_FIELD_VALUE (v, "tags", tags_value);
610 PUSH_FIELD_VALUE (v, "data", data_value);
611 FINISH_RECORD_CONSTRUCTOR (cons, v, constants_type_node);
612 return cons;
613 }
614
615 #include "gt-java-constants.h"