OSDN Git Service

2007-08-20 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / java / constants.c
1 /* Handle the constant pool of the Java(TM) Virtual Machine.
2    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006,
3    2007  Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>. 
19
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.  */
23
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"
33
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);
38
39 /* Set the INDEX'th constant in CPOOL to have the given TAG and VALUE. */
40
41 static void
42 set_constant_entry (CPool *cpool, int index, int tag, jword value)
43 {
44   if (cpool->data == NULL)
45     {
46       cpool->capacity = 100;
47       cpool->tags = ggc_alloc_cleared (sizeof(uint8) * cpool->capacity);
48       cpool->data = ggc_alloc_cleared (sizeof(union cpool_entry)
49                                        * cpool->capacity);
50       cpool->count = 1;
51     }
52   if (index >= cpool->capacity)
53     {
54       int old_cap = cpool->capacity;
55       cpool->capacity *= 2;
56       if (index >= cpool->capacity)
57         cpool->capacity = index + 10;
58       cpool->tags = ggc_realloc (cpool->tags, 
59                                  sizeof(uint8) * cpool->capacity);
60       cpool->data = ggc_realloc (cpool->data,
61                                  sizeof(union cpool_entry) * 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 class)
333 {
334   CPool *cpool = TYPE_CPOOL (class);
335
336   if (cpool == NULL)
337     {
338       cpool = ggc_alloc_cleared (sizeof (struct CPool));
339       TYPE_CPOOL (class) = 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    CLASS.  
376    Return the index of the entry.  */
377
378 int
379 alloc_constant_fieldref (tree class, tree decl)
380 {
381   CPool *outgoing_cpool = cpool_for_class (class);
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       TREE_INVARIANT (d) = 1;
449
450       return d;
451     }
452   else
453     {
454       tree type, decl;
455       tree decl_name = mangled_classname ("_CD_", output_class);
456
457       /* Build a type with unspecified bounds.  The will make sure
458          that targets do the right thing with whatever size we end
459          up with at the end.  Using bounds that are too small risks
460          assuming the data is in the small data section.  */
461       type = build_array_type (ptr_type_node, NULL_TREE);
462
463       /* We need to lay out the type ourselves, since build_array_type
464          thinks the type is incomplete.  */
465       layout_type (type);
466
467       decl = build_decl (VAR_DECL, decl_name, type);
468       TREE_STATIC (decl) = 1;
469
470       return decl;
471     }
472 }
473
474 /* Get the pointer value at the INDEX'th element of the constant pool. */
475
476 tree
477 build_ref_from_constant_pool (int index)
478 {
479   tree i;
480   tree d = TYPE_CPOOL_DATA_REF (output_class);
481
482   if (d == NULL_TREE)
483     d = build_constant_data_ref (flag_indirect_classes);
484
485   i = build_int_cst (NULL_TREE, index);
486   d = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (d)), d, i,
487                  NULL_TREE, NULL_TREE);
488   TREE_INVARIANT (d) = 1;
489   return d;
490 }
491
492 /* Build an initializer for the constants field of the current constant pool.
493    Should only be called at top-level, since it may emit declarations. */
494
495 tree
496 build_constants_constructor (void)
497 {
498   CPool *outgoing_cpool = cpool_for_class (current_class);
499   tree tags_value, data_value;
500   tree cons;
501   tree tags_list = NULL_TREE;
502   tree data_list = NULL_TREE;
503   int i;
504
505   for (i = outgoing_cpool->count;  --i > 0; )
506     switch (outgoing_cpool->tags[i] & ~CONSTANT_LazyFlag)
507       {
508       case CONSTANT_None:  /* The second half of a Double or Long on a
509                               32-bit target.  */
510       case CONSTANT_Fieldref:
511       case CONSTANT_NameAndType:
512       case CONSTANT_Float:
513       case CONSTANT_Integer:
514       case CONSTANT_Double:
515       case CONSTANT_Long:
516       case CONSTANT_Methodref:
517       case CONSTANT_InterfaceMethodref:
518         {
519           unsigned HOST_WIDE_INT temp = outgoing_cpool->data[i].w;
520
521           /* Make sure that on a 64-bit big-endian machine this
522              32-bit jint appears in the first word.  
523              FIXME: This is a kludge.  The field we're initializing is
524              not a scalar but a union, and that's how we should
525              represent it in the compiler.  We should fix this.  */
526           if (BYTES_BIG_ENDIAN && BITS_PER_WORD > 32)
527             temp <<= BITS_PER_WORD - 32;
528
529           tags_list
530             = tree_cons (NULL_TREE, get_tag_node (outgoing_cpool->tags[i]),
531                          tags_list);
532           data_list
533             = tree_cons (NULL_TREE, 
534                          fold_convert (ptr_type_node, 
535                                        (build_int_cst (NULL_TREE, temp))),
536                          data_list);
537         }
538         break;
539
540       case CONSTANT_Class:
541       case CONSTANT_String:
542       case CONSTANT_Unicode:
543       case CONSTANT_Utf8:
544         tags_list
545           = tree_cons (NULL_TREE, get_tag_node (outgoing_cpool->tags[i]),
546                        tags_list);
547         data_list
548           = tree_cons (NULL_TREE, build_utf8_ref (outgoing_cpool->data[i].t),
549                        data_list);
550         break;
551
552       default:
553         gcc_assert (false);
554       }
555   if (outgoing_cpool->count > 0)
556     {
557       tree data_decl, tags_decl, tags_type;
558       tree max_index = build_int_cst (sizetype, outgoing_cpool->count - 1);
559       tree index_type = build_index_type (max_index);
560
561       /* Add dummy 0'th element of constant pool. */
562       tags_list = tree_cons (NULL_TREE, get_tag_node (0), tags_list);
563       data_list = tree_cons (NULL_TREE, null_pointer_node, data_list);
564   
565       data_decl = build_constant_data_ref (false);
566       TREE_TYPE (data_decl) = build_array_type (ptr_type_node, index_type);
567       DECL_INITIAL (data_decl) = build_constructor_from_list
568                                   (TREE_TYPE (data_decl), data_list);
569       DECL_SIZE (data_decl) = TYPE_SIZE (TREE_TYPE (data_decl));
570       DECL_SIZE_UNIT (data_decl) = TYPE_SIZE_UNIT (TREE_TYPE (data_decl));
571       rest_of_decl_compilation (data_decl, 1, 0);
572       data_value = build_address_of (data_decl);
573
574       tags_type = build_array_type (unsigned_byte_type_node, index_type);
575       tags_decl = build_decl (VAR_DECL, mangled_classname ("_CT_", 
576                                                            current_class),
577                               tags_type);
578       TREE_STATIC (tags_decl) = 1;
579       DECL_INITIAL (tags_decl) = build_constructor_from_list
580                                  (tags_type, tags_list);
581       rest_of_decl_compilation (tags_decl, 1, 0);
582       tags_value = build_address_of (tags_decl);
583     }
584   else
585     {
586       data_value = null_pointer_node;
587       tags_value = null_pointer_node;
588     }
589   START_RECORD_CONSTRUCTOR (cons, constants_type_node);
590   PUSH_FIELD_VALUE (cons, "size",
591                     build_int_cst (NULL_TREE, outgoing_cpool->count));
592   PUSH_FIELD_VALUE (cons, "tags", tags_value);
593   PUSH_FIELD_VALUE (cons, "data", data_value);
594   FINISH_RECORD_CONSTRUCTOR (cons);
595   return cons;
596 }
597
598 #include "gt-java-constants.h"