cselib.c (cselib_current_insn_in_libcall): New static variable.
[official-gcc.git] / gcc / java / class.c
blob0818bc2107906f4be0abb9b425803555016e99f5
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of GCC.
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 2, or (at your option)
10 any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 /* Written by Per Bothner <bothner@cygnus.com> */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "rtl.h"
34 #include "flags.h"
35 #include "java-tree.h"
36 #include "jcf.h"
37 #include "obstack.h"
38 #include "toplev.h"
39 #include "output.h"
40 #include "parse.h"
41 #include "function.h"
42 #include "ggc.h"
43 #include "stdio.h"
44 #include "target.h"
46 /* DOS brain-damage */
47 #ifndef O_BINARY
48 #define O_BINARY 0 /* MS-DOS brain-damage */
49 #endif
51 static tree make_method_value PARAMS ((tree));
52 static tree build_java_method_type PARAMS ((tree, tree, int));
53 static int32 hashUtf8String PARAMS ((const char *, int));
54 static tree make_field_value PARAMS ((tree));
55 static tree get_dispatch_vector PARAMS ((tree));
56 static tree get_dispatch_table PARAMS ((tree, tree));
57 static void add_interface_do PARAMS ((tree, tree, int));
58 static tree maybe_layout_super_class PARAMS ((tree, tree));
59 static int assume_compiled PARAMS ((const char *));
60 static tree build_method_symbols_entry PARAMS ((tree));
62 static GTY(()) rtx registerClass_libfunc;
63 static GTY(()) rtx registerResource_libfunc;
65 struct obstack temporary_obstack;
67 /* The compiler generates different code depending on whether or not
68 it can assume certain classes have been compiled down to native
69 code or not. The compiler options -fassume-compiled= and
70 -fno-assume-compiled= are used to create a tree of
71 assume_compiled_node objects. This tree is queried to determine if
72 a class is assume to be compiled or not. Each node in the tree
73 represents either a package or a specific class. */
75 typedef struct assume_compiled_node_struct
77 /* The class or package name. */
78 const char *ident;
80 /* Nonzero if this represents an exclusion. */
81 int excludep;
83 /* Pointers to other nodes in the tree. */
84 struct assume_compiled_node_struct *parent;
85 struct assume_compiled_node_struct *sibling;
86 struct assume_compiled_node_struct *child;
87 } assume_compiled_node;
89 static assume_compiled_node *find_assume_compiled_node
90 PARAMS ((assume_compiled_node *, const char *));
92 /* This is the root of the include/exclude tree. */
94 static assume_compiled_node *assume_compiled_tree;
96 static GTY(()) tree class_roots[5];
97 #define registered_class class_roots[0]
98 #define fields_ident class_roots[1] /* get_identifier ("fields") */
99 #define info_ident class_roots[2] /* get_identifier ("info") */
100 #define class_list class_roots[3]
101 #define class_dtable_decl class_roots[4]
103 /* Return the node that most closely represents the class whose name
104 is IDENT. Start the search from NODE. Return NULL if an
105 appropriate node does not exist. */
107 static assume_compiled_node *
108 find_assume_compiled_node (node, ident)
109 assume_compiled_node *node;
110 const char *ident;
112 while (node)
114 size_t node_ident_length = strlen (node->ident);
116 /* node_ident_length is zero at the root of the tree. If the
117 identifiers are the same length, then we have matching
118 classes. Otherwise check if we've matched an enclosing
119 package name. */
121 if (node_ident_length == 0
122 || (strncmp (ident, node->ident, node_ident_length) == 0
123 && (strlen (ident) == node_ident_length
124 || ident[node_ident_length] == '.')))
126 /* We've found a match, however, there might be a more
127 specific match. */
129 assume_compiled_node *found = find_assume_compiled_node (node->child,
130 ident);
131 if (found)
132 return found;
133 else
134 return node;
137 /* No match yet. Continue through the sibling list. */
138 node = node->sibling;
141 /* No match at all in this tree. */
142 return NULL;
145 /* Add a new IDENT to the include/exclude tree. It's an exclusion
146 if EXCLUDEP is nonzero. */
148 void
149 add_assume_compiled (ident, excludep)
150 const char *ident;
151 int excludep;
153 int len;
154 assume_compiled_node *parent;
155 assume_compiled_node *node =
156 xmalloc (sizeof (assume_compiled_node));
158 node->ident = xstrdup (ident);
159 node->excludep = excludep;
160 node->child = NULL;
162 /* Create the root of the tree if it doesn't exist yet. */
164 if (NULL == assume_compiled_tree)
166 assume_compiled_tree = xmalloc (sizeof (assume_compiled_node));
167 assume_compiled_tree->ident = "";
168 assume_compiled_tree->excludep = 0;
169 assume_compiled_tree->sibling = NULL;
170 assume_compiled_tree->child = NULL;
171 assume_compiled_tree->parent = NULL;
174 /* Calling the function with the empty string means we're setting
175 excludep for the root of the hierarchy. */
177 if (0 == ident[0])
179 assume_compiled_tree->excludep = excludep;
180 return;
183 /* Find the parent node for this new node. PARENT will either be a
184 class or a package name. Adjust PARENT accordingly. */
186 parent = find_assume_compiled_node (assume_compiled_tree, ident);
187 len = strlen (parent->ident);
188 if (parent->ident[len] && parent->ident[len] != '.')
189 parent = parent->parent;
191 /* Insert NODE into the tree. */
193 node->parent = parent;
194 node->sibling = parent->child;
195 parent->child = node;
198 /* Returns nonzero if IDENT is the name of a class that the compiler
199 should assume has been compiled to object code. */
201 static int
202 assume_compiled (ident)
203 const char *ident;
205 assume_compiled_node *i;
206 int result;
208 if (NULL == assume_compiled_tree)
209 return 1;
211 i = find_assume_compiled_node (assume_compiled_tree,
212 ident);
214 result = ! i->excludep;
216 return (result);
219 /* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
220 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
221 Also, PREFIX is prepended, and SUFFIX is appended. */
223 tree
224 ident_subst (old_name, old_length, prefix, old_char, new_char, suffix)
225 const char* old_name;
226 int old_length;
227 const char *prefix;
228 int old_char;
229 int new_char;
230 const char *suffix;
232 int prefix_len = strlen (prefix);
233 int suffix_len = strlen (suffix);
234 int i = prefix_len + old_length + suffix_len + 1;
235 #ifdef __GNUC__
236 char buffer[i];
237 #else
238 char *buffer = alloca (i);
239 #endif
240 strcpy (buffer, prefix);
241 for (i = 0; i < old_length; i++)
243 char ch = old_name[i];
244 if (ch == old_char)
245 ch = new_char;
246 buffer[prefix_len + i] = ch;
248 strcpy (buffer + prefix_len + old_length, suffix);
249 return get_identifier (buffer);
252 /* Return an IDENTIFIER_NODE the same as OLD_ID,
253 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
254 Also, PREFIX is prepended, and SUFFIX is appended. */
256 tree
257 identifier_subst (old_id, prefix, old_char, new_char, suffix)
258 const tree old_id;
259 const char *prefix;
260 int old_char;
261 int new_char;
262 const char *suffix;
264 return ident_subst (IDENTIFIER_POINTER (old_id), IDENTIFIER_LENGTH (old_id),
265 prefix, old_char, new_char, suffix);
268 /* Generate a valid C identifier from the name of the class TYPE,
269 prefixed by PREFIX. */
271 tree
272 mangled_classname (prefix, type)
273 const char *prefix;
274 tree type;
276 tree ident = TYPE_NAME (type);
277 if (TREE_CODE (ident) != IDENTIFIER_NODE)
278 ident = DECL_NAME (ident);
279 return identifier_subst (ident, prefix, '.', '_', "");
282 tree
283 make_class ()
285 tree type;
286 type = make_node (RECORD_TYPE);
287 TYPE_BINFO (type) = make_tree_vec (6);
288 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
290 return type;
293 /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
294 and where each of the constituents is separated by '/',
295 return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
297 tree
298 unmangle_classname (name, name_length)
299 const char *name; int name_length;
301 tree to_return = ident_subst (name, name_length, "", '/', '.', "");
302 /* It's not sufficient to compare to_return and get_identifier
303 (name) to determine whether to_return is qualified. There are
304 cases in signature analysis where name will be stripped of a
305 trailing ';'. */
306 name = IDENTIFIER_POINTER (to_return);
307 while (*name)
308 if (*name++ == '.')
310 QUALIFIED_P (to_return) = 1;
311 break;
314 return to_return;
317 tree
318 push_class (class_type, class_name)
319 tree class_type, class_name;
321 tree decl, signature;
322 const char *save_input_filename = input_filename;
323 int save_lineno = lineno;
324 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
325 CLASS_P (class_type) = 1;
326 input_filename = IDENTIFIER_POINTER (source_name);
327 lineno = 0;
328 decl = build_decl (TYPE_DECL, class_name, class_type);
330 /* dbxout needs a DECL_SIZE if in gstabs mode */
331 DECL_SIZE (decl) = integer_zero_node;
333 input_filename = save_input_filename;
334 lineno = save_lineno;
335 signature = identifier_subst (class_name, "L", '.', '/', ";");
336 IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
338 /* Setting DECL_ARTIFICIAL forces dbxout.c to specific the type is
339 both a typedef and in the struct name-space. We may want to re-visit
340 this later, but for now it reduces the changes needed for gdb. */
341 DECL_ARTIFICIAL (decl) = 1;
343 pushdecl_top_level (decl);
345 return decl;
348 /* Finds the (global) class named NAME. Creates the class if not found.
349 Also creates associated TYPE_DECL.
350 Does not check if the class actually exists, load the class,
351 fill in field or methods, or do layout_type. */
353 tree
354 lookup_class (name)
355 tree name;
357 tree decl = IDENTIFIER_CLASS_VALUE (name);
358 if (decl == NULL_TREE)
359 decl = push_class (make_class (), name);
360 return TREE_TYPE (decl);
363 void
364 set_super_info (access_flags, this_class, super_class, interfaces_count)
365 int access_flags;
366 tree this_class;
367 tree super_class;
368 int interfaces_count;
370 int total_supers = interfaces_count;
371 tree class_decl = TYPE_NAME (this_class);
372 if (super_class)
373 total_supers++;
375 TYPE_BINFO_BASETYPES (this_class) = make_tree_vec (total_supers);
376 if (super_class)
378 tree super_binfo = make_tree_vec (6);
379 BINFO_TYPE (super_binfo) = super_class;
380 BINFO_OFFSET (super_binfo) = integer_zero_node;
381 TREE_VIA_PUBLIC (super_binfo) = 1;
382 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (this_class)), 0)
383 = super_binfo;
384 CLASS_HAS_SUPER (this_class) = 1;
387 set_class_decl_access_flags (access_flags, class_decl);
390 void
391 set_class_decl_access_flags (access_flags, class_decl)
392 int access_flags;
393 tree class_decl;
395 if (access_flags & ACC_PUBLIC) CLASS_PUBLIC (class_decl) = 1;
396 if (access_flags & ACC_FINAL) CLASS_FINAL (class_decl) = 1;
397 if (access_flags & ACC_SUPER) CLASS_SUPER (class_decl) = 1;
398 if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
399 if (access_flags & ACC_ABSTRACT) CLASS_ABSTRACT (class_decl) = 1;
400 if (access_flags & ACC_STATIC) CLASS_STATIC (class_decl) = 1;
401 if (access_flags & ACC_PRIVATE) CLASS_PRIVATE (class_decl) = 1;
402 if (access_flags & ACC_PROTECTED) CLASS_PROTECTED (class_decl) = 1;
403 if (access_flags & ACC_STRICT) CLASS_STRICTFP (class_decl) = 1;
406 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
407 direct sub-classes of Object are 1, and so on. */
410 class_depth (clas)
411 tree clas;
413 int depth = 0;
414 if (! CLASS_LOADED_P (clas))
415 load_class (clas, 1);
416 if (TYPE_SIZE (clas) == error_mark_node)
417 return -1;
418 while (clas != object_type_node)
420 depth++;
421 clas = TYPE_BINFO_BASETYPE (clas, 0);
423 return depth;
426 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
429 interface_of_p (type1, type2)
430 tree type1, type2;
432 int n, i;
433 tree basetype_vec;
435 if (!(basetype_vec = TYPE_BINFO_BASETYPES (type2)))
436 return 0;
437 n = TREE_VEC_LENGTH (basetype_vec);
438 for (i = 0; i < n; i++)
440 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
441 if (vec_elt && BINFO_TYPE (vec_elt) == type1)
442 return 1;
444 for (i = 0; i < n; i++)
446 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
447 if (vec_elt && BINFO_TYPE (vec_elt)
448 && interface_of_p (type1, BINFO_TYPE (vec_elt)))
449 return 1;
451 return 0;
454 /* Return true iff TYPE1 inherits from TYPE2. */
457 inherits_from_p (type1, type2)
458 tree type1, type2;
460 while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
462 if (type1 == type2)
463 return 1;
464 type1 = CLASSTYPE_SUPER (type1);
466 return 0;
469 /* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */
472 enclosing_context_p (type1, type2)
473 tree type1, type2;
475 if (!INNER_CLASS_TYPE_P (type2))
476 return 0;
478 for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
479 type2;
480 type2 = (INNER_CLASS_TYPE_P (type2) ?
481 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
483 if (type2 == type1)
484 return 1;
487 return 0;
490 /* Return 1 iff there exists a common enclosing context between TYPE1
491 and TYPE2. */
493 int common_enclosing_context_p (type1, type2)
494 tree type1, type2;
496 if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
497 return 0;
499 for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1;
500 type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
501 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
503 tree current;
504 for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
505 current = (PURE_INNER_CLASS_TYPE_P (current) ?
506 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
507 NULL_TREE))
508 if (type1 == current)
509 return 1;
511 return 0;
514 static void
515 add_interface_do (basetype_vec, interface_class, i)
516 tree basetype_vec, interface_class;
517 int i;
519 tree interface_binfo = make_tree_vec (6);
520 BINFO_TYPE (interface_binfo) = interface_class;
521 BINFO_OFFSET (interface_binfo) = integer_zero_node;
522 BINFO_VPTR_FIELD (interface_binfo) = integer_zero_node;
523 TREE_VIA_VIRTUAL (interface_binfo) = 1;
524 TREE_VIA_PUBLIC (interface_binfo) = 1;
525 TREE_VEC_ELT (basetype_vec, i) = interface_binfo;
528 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
529 found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
530 if attempt is made to add it twice. */
532 tree
533 maybe_add_interface (this_class, interface_class)
534 tree this_class, interface_class;
536 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
537 int i;
538 int n = TREE_VEC_LENGTH (basetype_vec);
539 for (i = 0; ; i++)
541 if (i >= n)
543 error ("internal error - too many interface type");
544 return NULL_TREE;
546 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
547 break;
548 else if (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)) == interface_class)
549 return interface_class;
551 add_interface_do (basetype_vec, interface_class, i);
552 return NULL_TREE;
555 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
557 void
558 add_interface (this_class, interface_class)
559 tree this_class, interface_class;
561 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
562 int i;
563 int n = TREE_VEC_LENGTH (basetype_vec);
564 for (i = 0; ; i++)
566 if (i >= n)
568 error ("internal error - too many interface type");
569 return;
571 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
572 break;
574 add_interface_do (basetype_vec, interface_class, i);
577 #if 0
578 /* Return the address of a pointer to the first FUNCTION_DECL
579 in the list (*LIST) whose DECL_NAME is NAME. */
581 static tree *
582 find_named_method (list, name)
583 tree *list;
584 tree name;
586 while (*list && DECL_NAME (*list) != name)
587 list = &TREE_CHAIN (*list);
588 return list;
590 #endif
592 static tree
593 build_java_method_type (fntype, this_class, access_flags)
594 tree fntype;
595 tree this_class;
596 int access_flags;
598 if (access_flags & ACC_STATIC)
599 return fntype;
600 return build_method_type (this_class, fntype);
603 tree
604 add_method_1 (this_class, access_flags, name, function_type)
605 tree this_class;
606 int access_flags;
607 tree name;
608 tree function_type;
610 tree method_type, fndecl;
612 method_type = build_java_method_type (function_type,
613 this_class, access_flags);
615 fndecl = build_decl (FUNCTION_DECL, name, method_type);
616 DECL_CONTEXT (fndecl) = this_class;
618 DECL_LANG_SPECIFIC (fndecl)
619 = ggc_alloc_cleared (sizeof (struct lang_decl));
620 DECL_LANG_SPECIFIC (fndecl)->desc = LANG_DECL_FUNC;
622 /* Initialize the static initializer test table. */
624 DECL_FUNCTION_INIT_TEST_TABLE (fndecl) =
625 java_treetreehash_create (10, 1);
627 /* Initialize the initialized (static) class table. */
628 if (access_flags & ACC_STATIC)
629 DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl) =
630 htab_create_ggc (50, htab_hash_pointer, htab_eq_pointer, NULL);
632 /* Initialize the static method invocation compound list */
633 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = NULL_TREE;
635 TREE_CHAIN (fndecl) = TYPE_METHODS (this_class);
636 TYPE_METHODS (this_class) = fndecl;
638 /* Notice that this is a finalizer and update the class type
639 accordingly. This is used to optimize instance allocation. */
640 if (name == finalize_identifier_node
641 && TREE_TYPE (function_type) == void_type_node
642 && TREE_VALUE (TYPE_ARG_TYPES (function_type)) == void_type_node)
643 HAS_FINALIZER_P (this_class) = 1;
645 if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
646 if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
647 if (access_flags & ACC_PRIVATE)
648 METHOD_PRIVATE (fndecl) = DECL_INLINE (fndecl) = 1;
649 if (access_flags & ACC_NATIVE)
651 METHOD_NATIVE (fndecl) = 1;
652 DECL_EXTERNAL (fndecl) = 1;
654 if (access_flags & ACC_STATIC)
655 METHOD_STATIC (fndecl) = DECL_INLINE (fndecl) = 1;
656 if (access_flags & ACC_FINAL)
657 METHOD_FINAL (fndecl) = DECL_INLINE (fndecl) = 1;
658 if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
659 if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
660 if (access_flags & ACC_TRANSIENT) METHOD_TRANSIENT (fndecl) = 1;
661 if (access_flags & ACC_STRICT) METHOD_STRICTFP (fndecl) = 1;
662 return fndecl;
665 /* Add a method to THIS_CLASS.
666 The method's name is NAME.
667 Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
669 tree
670 add_method (this_class, access_flags, name, method_sig)
671 tree this_class;
672 int access_flags;
673 tree name;
674 tree method_sig;
676 tree function_type, fndecl;
677 const unsigned char *sig
678 = (const unsigned char *) IDENTIFIER_POINTER (method_sig);
680 if (sig[0] != '(')
681 fatal_error ("bad method signature");
683 function_type = get_type_from_signature (method_sig);
684 fndecl = add_method_1 (this_class, access_flags, name, function_type);
685 set_java_signature (TREE_TYPE (fndecl), method_sig);
686 return fndecl;
689 tree
690 add_field (class, name, field_type, flags)
691 tree class;
692 tree name;
693 tree field_type;
694 int flags;
696 int is_static = (flags & ACC_STATIC) != 0;
697 tree field;
698 field = build_decl (is_static ? VAR_DECL : FIELD_DECL, name, field_type);
699 TREE_CHAIN (field) = TYPE_FIELDS (class);
700 TYPE_FIELDS (class) = field;
701 DECL_CONTEXT (field) = class;
703 if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
704 if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
705 if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
706 if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
707 if (flags & ACC_VOLATILE) FIELD_VOLATILE (field) = 1;
708 if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
709 if (is_static)
711 FIELD_STATIC (field) = 1;
712 /* Always make field externally visible. This is required so
713 that native methods can always access the field. */
714 TREE_PUBLIC (field) = 1;
715 /* Considered external until we know what classes are being
716 compiled into this object file. */
717 DECL_EXTERNAL (field) = 1;
720 return field;
723 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
725 void
726 set_constant_value (field, constant)
727 tree field, constant;
729 if (field == NULL_TREE)
730 warning ("misplaced ConstantValue attribute (not in any field)");
731 else if (DECL_INITIAL (field) != NULL_TREE)
732 warning ("duplicate ConstantValue attribute for field '%s'",
733 IDENTIFIER_POINTER (DECL_NAME (field)));
734 else
736 DECL_INITIAL (field) = constant;
737 if (TREE_TYPE (constant) != TREE_TYPE (field)
738 && ! (TREE_TYPE (constant) == int_type_node
739 && INTEGRAL_TYPE_P (TREE_TYPE (field))
740 && TYPE_PRECISION (TREE_TYPE (field)) <= 32)
741 && ! (TREE_TYPE (constant) == utf8const_ptr_type
742 && TREE_TYPE (field) == string_ptr_type_node))
743 error ("ConstantValue attribute of field '%s' has wrong type",
744 IDENTIFIER_POINTER (DECL_NAME (field)));
745 if (FIELD_FINAL (field))
746 DECL_FIELD_FINAL_IUD (field) = 1;
750 /* Count the number of Unicode chars encoded in a given Ut8 string. */
752 #if 0
754 strLengthUtf8 (str, len)
755 char *str;
756 int len;
758 register unsigned char* ptr = (unsigned char*) str;
759 register unsigned char *limit = ptr + len;
760 int str_length = 0;
761 for (; ptr < limit; str_length++) {
762 if (UTF8_GET (ptr, limit) < 0)
763 return -1;
765 return str_length;
767 #endif
770 /* Calculate a hash value for a string encoded in Utf8 format.
771 * This returns the same hash value as specified for java.lang.String.hashCode.
774 static int32
775 hashUtf8String (str, len)
776 const char *str;
777 int len;
779 register const unsigned char* ptr = (const unsigned char*) str;
780 register const unsigned char *limit = ptr + len;
781 int32 hash = 0;
782 for (; ptr < limit;)
784 int ch = UTF8_GET (ptr, limit);
785 /* Updated specification from
786 http://www.javasoft.com/docs/books/jls/clarify.html. */
787 hash = (31 * hash) + ch;
789 return hash;
792 /* Generate a byte array representing the contents of FILENAME. The
793 array is assigned a unique local symbol. The array represents a
794 compiled Java resource, which is accessed by the runtime using
795 NAME. */
796 void
797 compile_resource_file (name, filename)
798 char *name;
799 const char *filename;
801 struct stat stat_buf;
802 int fd;
803 char *buffer;
804 char buf[60];
805 tree rtype, field = NULL_TREE, data_type, rinit, data, decl;
806 static int Jr_count = 0;
808 fd = open (filename, O_RDONLY | O_BINARY);
809 if (fd < 0)
811 perror ("Failed to read resource file");
812 return;
814 if (fstat (fd, &stat_buf) != 0
815 || ! S_ISREG (stat_buf.st_mode))
817 perror ("Could not figure length of resource file");
818 return;
820 buffer = xmalloc (strlen (name) + stat_buf.st_size);
821 strcpy (buffer, name);
822 read (fd, buffer + strlen (name), stat_buf.st_size);
823 close (fd);
824 data_type = build_prim_array_type (unsigned_byte_type_node,
825 strlen (name) + stat_buf.st_size);
826 rtype = make_node (RECORD_TYPE);
827 PUSH_FIELD (rtype, field, "name_length", unsigned_int_type_node);
828 PUSH_FIELD (rtype, field, "resource_length", unsigned_int_type_node);
829 PUSH_FIELD (rtype, field, "data", data_type);
830 FINISH_RECORD (rtype);
831 START_RECORD_CONSTRUCTOR (rinit, rtype);
832 PUSH_FIELD_VALUE (rinit, "name_length",
833 build_int_2 (strlen (name), 0));
834 PUSH_FIELD_VALUE (rinit, "resource_length",
835 build_int_2 (stat_buf.st_size, 0));
836 data = build_string (strlen(name) + stat_buf.st_size, buffer);
837 TREE_TYPE (data) = data_type;
838 PUSH_FIELD_VALUE (rinit, "data", data);
839 FINISH_RECORD_CONSTRUCTOR (rinit);
840 TREE_CONSTANT (rinit) = 1;
842 /* Generate a unique-enough identifier. */
843 sprintf(buf, "_Jr%d", ++Jr_count);
845 decl = build_decl (VAR_DECL, get_identifier (buf), rtype);
846 TREE_STATIC (decl) = 1;
847 DECL_ARTIFICIAL (decl) = 1;
848 DECL_IGNORED_P (decl) = 1;
849 TREE_READONLY (decl) = 1;
850 TREE_THIS_VOLATILE (decl) = 0;
851 DECL_INITIAL (decl) = rinit;
852 layout_decl (decl, 0);
853 pushdecl (decl);
854 rest_of_decl_compilation (decl, (char*) 0, global_bindings_p (), 0);
855 make_decl_rtl (decl, (char*) 0);
856 assemble_variable (decl, 1, 0, 0);
859 tree init_name = get_file_function_name ('I');
860 tree init_type = build_function_type (void_type_node, end_params_node);
861 tree init_decl;
863 init_decl = build_decl (FUNCTION_DECL, init_name, init_type);
864 SET_DECL_ASSEMBLER_NAME (init_decl, init_name);
865 TREE_STATIC (init_decl) = 1;
866 current_function_decl = init_decl;
867 DECL_RESULT (init_decl) = build_decl (RESULT_DECL,
868 NULL_TREE, void_type_node);
870 /* It can be a static function as long as collect2 does not have
871 to scan the object file to find its ctor/dtor routine. */
872 TREE_PUBLIC (init_decl) = ! targetm.have_ctors_dtors;
874 pushlevel (0);
875 make_decl_rtl (init_decl, NULL);
876 init_function_start (init_decl, input_filename, 0);
877 expand_function_start (init_decl, 0);
879 emit_library_call (registerResource_libfunc, 0, VOIDmode, 1,
880 gen_rtx (SYMBOL_REF, Pmode, buf),
881 Pmode);
883 expand_function_end (input_filename, 0, 0);
884 poplevel (1, 0, 1);
886 /* Force generation, even with -O3 or deeper. Gross hack. FIXME */
887 int saved_flag = flag_inline_functions;
888 flag_inline_functions = 0;
889 rest_of_compilation (init_decl);
890 flag_inline_functions = saved_flag;
892 current_function_decl = NULL_TREE;
893 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (init_decl), 0),
894 DEFAULT_INIT_PRIORITY);
898 tree utf8_decl_list = NULL_TREE;
900 tree
901 build_utf8_ref (name)
902 tree name;
904 const char * name_ptr = IDENTIFIER_POINTER(name);
905 int name_len = IDENTIFIER_LENGTH(name);
906 char buf[60];
907 tree ctype, field = NULL_TREE, str_type, cinit, string;
908 static int utf8_count = 0;
909 int name_hash;
910 tree ref = IDENTIFIER_UTF8_REF (name);
911 tree decl;
912 if (ref != NULL_TREE)
913 return ref;
915 ctype = make_node (RECORD_TYPE);
916 str_type = build_prim_array_type (unsigned_byte_type_node,
917 name_len + 1); /* Allow for final '\0'. */
918 PUSH_FIELD (ctype, field, "hash", unsigned_short_type_node);
919 PUSH_FIELD (ctype, field, "length", unsigned_short_type_node);
920 PUSH_FIELD (ctype, field, "data", str_type);
921 FINISH_RECORD (ctype);
922 START_RECORD_CONSTRUCTOR (cinit, ctype);
923 name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
924 PUSH_FIELD_VALUE (cinit, "hash", build_int_2 (name_hash, 0));
925 PUSH_FIELD_VALUE (cinit, "length", build_int_2 (name_len, 0));
926 string = build_string (name_len, name_ptr);
927 TREE_TYPE (string) = str_type;
928 PUSH_FIELD_VALUE (cinit, "data", string);
929 FINISH_RECORD_CONSTRUCTOR (cinit);
930 TREE_CONSTANT (cinit) = 1;
932 /* Generate a unique-enough identifier. */
933 sprintf(buf, "_Utf%d", ++utf8_count);
935 decl = build_decl (VAR_DECL, get_identifier (buf), utf8const_type);
936 TREE_STATIC (decl) = 1;
937 DECL_ARTIFICIAL (decl) = 1;
938 DECL_IGNORED_P (decl) = 1;
939 TREE_READONLY (decl) = 1;
940 TREE_THIS_VOLATILE (decl) = 0;
941 DECL_INITIAL (decl) = cinit;
942 #ifdef HAVE_GAS_SHF_MERGE
944 int decl_size;
945 /* Ensure decl_size is a multiple of utf8const_type's alignment. */
946 decl_size = (name_len + 5 + TYPE_ALIGN_UNIT (utf8const_type) - 1)
947 & ~(TYPE_ALIGN_UNIT (utf8const_type) - 1);
948 if (flag_merge_constants && decl_size < 256)
950 char buf[32];
951 int flags = (SECTION_OVERRIDE
952 | SECTION_MERGE | (SECTION_ENTSIZE & decl_size));
953 sprintf (buf, ".rodata.jutf8.%d", decl_size);
954 named_section_flags (buf, flags);
955 DECL_SECTION_NAME (decl) = build_string (strlen (buf), buf);
958 #endif
959 TREE_CHAIN (decl) = utf8_decl_list;
960 layout_decl (decl, 0);
961 pushdecl (decl);
962 rest_of_decl_compilation (decl, (char*) 0, global_bindings_p (), 0);
963 utf8_decl_list = decl;
964 make_decl_rtl (decl, (char*) 0);
965 ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
966 IDENTIFIER_UTF8_REF (name) = ref;
967 return ref;
970 /* Build a reference to the class TYPE.
971 Also handles primitive types and array types. */
973 tree
974 build_class_ref (type)
975 tree type;
977 int is_compiled = is_compiled_class (type);
978 if (is_compiled)
980 tree ref, decl_name, decl;
981 if (TREE_CODE (type) == POINTER_TYPE)
982 type = TREE_TYPE (type);
983 if (TREE_CODE (type) == RECORD_TYPE)
985 if (TYPE_SIZE (type) == error_mark_node)
986 return null_pointer_node;
987 decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
988 "", '/', '/', ".class");
989 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
990 if (decl == NULL_TREE)
992 decl = build_decl (VAR_DECL, decl_name, class_type_node);
993 DECL_SIZE (decl) = TYPE_SIZE (class_type_node);
994 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (class_type_node);
995 TREE_STATIC (decl) = 1;
996 TREE_PUBLIC (decl) = 1;
997 DECL_IGNORED_P (decl) = 1;
998 DECL_ARTIFICIAL (decl) = 1;
999 if (is_compiled == 1)
1000 DECL_EXTERNAL (decl) = 1;
1001 SET_DECL_ASSEMBLER_NAME (decl,
1002 java_mangle_class_field
1003 (&temporary_obstack, type));
1004 make_decl_rtl (decl, NULL);
1005 pushdecl_top_level (decl);
1008 else
1010 const char *name;
1011 char buffer[25];
1012 if (flag_emit_class_files)
1014 const char *prim_class_name;
1015 tree prim_class;
1016 if (type == char_type_node)
1017 prim_class_name = "java.lang.Character";
1018 else if (type == boolean_type_node)
1019 prim_class_name = "java.lang.Boolean";
1020 else if (type == byte_type_node)
1021 prim_class_name = "java.lang.Byte";
1022 else if (type == short_type_node)
1023 prim_class_name = "java.lang.Short";
1024 else if (type == int_type_node)
1025 prim_class_name = "java.lang.Integer";
1026 else if (type == long_type_node)
1027 prim_class_name = "java.lang.Long";
1028 else if (type == float_type_node)
1029 prim_class_name = "java.lang.Float";
1030 else if (type == double_type_node)
1031 prim_class_name = "java.lang.Double";
1032 else if (type == void_type_node)
1033 prim_class_name = "java.lang.Void";
1034 else
1035 abort ();
1037 prim_class = lookup_class (get_identifier (prim_class_name));
1038 return build (COMPONENT_REF, NULL_TREE,
1039 prim_class, TYPE_identifier_node);
1041 decl_name = TYPE_NAME (type);
1042 if (TREE_CODE (decl_name) == TYPE_DECL)
1043 decl_name = DECL_NAME (decl_name);
1044 name = IDENTIFIER_POINTER (decl_name);
1045 if (strncmp (name, "promoted_", 9) == 0)
1046 name += 9;
1047 sprintf (buffer, "_Jv_%sClass", name);
1048 decl_name = get_identifier (buffer);
1049 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1050 if (decl == NULL_TREE)
1052 decl = build_decl (VAR_DECL, decl_name, class_type_node);
1053 TREE_STATIC (decl) = 1;
1054 TREE_PUBLIC (decl) = 1;
1055 DECL_EXTERNAL (decl) = 1;
1056 make_decl_rtl (decl, NULL);
1057 pushdecl_top_level (decl);
1061 ref = build1 (ADDR_EXPR, class_ptr_type, decl);
1062 return ref;
1064 else
1066 int index;
1067 tree cl;
1068 index = alloc_class_constant (type);
1069 cl = build_ref_from_constant_pool (index);
1070 TREE_TYPE (cl) = promote_type (class_ptr_type);
1071 return cl;
1075 tree
1076 build_static_field_ref (fdecl)
1077 tree fdecl;
1079 tree fclass = DECL_CONTEXT (fdecl);
1080 int is_compiled = is_compiled_class (fclass);
1082 /* Allow static final fields to fold to a constant. */
1083 if (is_compiled || FIELD_FINAL (fdecl))
1085 if (!DECL_RTL_SET_P (fdecl))
1087 if (is_compiled == 1)
1088 DECL_EXTERNAL (fdecl) = 1;
1089 make_decl_rtl (fdecl, NULL);
1091 return fdecl;
1093 else
1095 /* Compile as:
1096 * *(FTYPE*)build_class_ref(FCLASS)->fields[INDEX].info.addr */
1097 tree ref = build_class_ref (fclass);
1098 tree fld;
1099 int field_index = 0;
1100 ref = build1 (INDIRECT_REF, class_type_node, ref);
1101 ref = build (COMPONENT_REF, field_ptr_type_node, ref,
1102 lookup_field (&class_type_node, fields_ident));
1104 for (fld = TYPE_FIELDS (fclass); ; fld = TREE_CHAIN (fld))
1106 if (fld == fdecl)
1107 break;
1108 if (fld == NULL_TREE)
1109 fatal_error ("field '%s' not found in class",
1110 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1111 if (FIELD_STATIC (fld))
1112 field_index++;
1114 field_index *= int_size_in_bytes (field_type_node);
1115 ref = fold (build (PLUS_EXPR, field_ptr_type_node,
1116 ref, build_int_2 (field_index, 0)));
1117 ref = build1 (INDIRECT_REF, field_type_node, ref);
1118 ref = build (COMPONENT_REF, field_info_union_node,
1119 ref, lookup_field (&field_type_node, info_ident));
1120 ref = build (COMPONENT_REF, ptr_type_node,
1121 ref, TREE_CHAIN (TYPE_FIELDS (field_info_union_node)));
1122 return fold (build1 (INDIRECT_REF, TREE_TYPE(fdecl), ref));
1127 get_access_flags_from_decl (decl)
1128 tree decl;
1130 int access_flags = 0;
1131 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
1133 if (FIELD_STATIC (decl))
1134 access_flags |= ACC_STATIC;
1135 if (FIELD_PUBLIC (decl))
1136 access_flags |= ACC_PUBLIC;
1137 if (FIELD_PROTECTED (decl))
1138 access_flags |= ACC_PROTECTED;
1139 if (FIELD_PRIVATE (decl))
1140 access_flags |= ACC_PRIVATE;
1141 if (FIELD_FINAL (decl))
1142 access_flags |= ACC_FINAL;
1143 if (FIELD_VOLATILE (decl))
1144 access_flags |= ACC_VOLATILE;
1145 if (FIELD_TRANSIENT (decl))
1146 access_flags |= ACC_TRANSIENT;
1147 return access_flags;
1149 if (TREE_CODE (decl) == TYPE_DECL)
1151 if (CLASS_PUBLIC (decl))
1152 access_flags |= ACC_PUBLIC;
1153 if (CLASS_FINAL (decl))
1154 access_flags |= ACC_FINAL;
1155 if (CLASS_SUPER (decl))
1156 access_flags |= ACC_SUPER;
1157 if (CLASS_INTERFACE (decl))
1158 access_flags |= ACC_INTERFACE;
1159 if (CLASS_ABSTRACT (decl))
1160 access_flags |= ACC_ABSTRACT;
1161 if (CLASS_STATIC (decl))
1162 access_flags |= ACC_STATIC;
1163 if (CLASS_PRIVATE (decl))
1164 access_flags |= ACC_PRIVATE;
1165 if (CLASS_PROTECTED (decl))
1166 access_flags |= ACC_PROTECTED;
1167 if (CLASS_STRICTFP (decl))
1168 access_flags |= ACC_STRICT;
1169 return access_flags;
1171 if (TREE_CODE (decl) == FUNCTION_DECL)
1173 if (METHOD_PUBLIC (decl))
1174 access_flags |= ACC_PUBLIC;
1175 if (METHOD_PRIVATE (decl))
1176 access_flags |= ACC_PRIVATE;
1177 if (METHOD_PROTECTED (decl))
1178 access_flags |= ACC_PROTECTED;
1179 if (METHOD_STATIC (decl))
1180 access_flags |= ACC_STATIC;
1181 if (METHOD_FINAL (decl))
1182 access_flags |= ACC_FINAL;
1183 if (METHOD_SYNCHRONIZED (decl))
1184 access_flags |= ACC_SYNCHRONIZED;
1185 if (METHOD_NATIVE (decl))
1186 access_flags |= ACC_NATIVE;
1187 if (METHOD_ABSTRACT (decl))
1188 access_flags |= ACC_ABSTRACT;
1189 if (METHOD_TRANSIENT (decl))
1190 access_flags |= ACC_TRANSIENT;
1191 if (METHOD_STRICTFP (decl))
1192 access_flags |= ACC_STRICT;
1193 return access_flags;
1195 abort ();
1198 static tree
1199 make_field_value (fdecl)
1200 tree fdecl;
1202 tree finit;
1203 int flags;
1204 tree type = TREE_TYPE (fdecl);
1205 int resolved = is_compiled_class (type);
1207 START_RECORD_CONSTRUCTOR (finit, field_type_node);
1208 PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
1209 if (resolved)
1210 type = build_class_ref (type);
1211 else
1213 tree signature = build_java_signature (type);
1215 type = build_utf8_ref (unmangle_classname
1216 (IDENTIFIER_POINTER (signature),
1217 IDENTIFIER_LENGTH (signature)));
1219 PUSH_FIELD_VALUE (finit, "type", type);
1221 flags = get_access_flags_from_decl (fdecl);
1222 if (! resolved)
1223 flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1225 PUSH_FIELD_VALUE (finit, "accflags", build_int_2 (flags, 0));
1226 PUSH_FIELD_VALUE (finit, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1228 PUSH_FIELD_VALUE
1229 (finit, "info",
1230 build (CONSTRUCTOR, field_info_union_node, NULL_TREE,
1231 build_tree_list
1232 ((FIELD_STATIC (fdecl)
1233 ? TREE_CHAIN (TYPE_FIELDS (field_info_union_node))
1234 : TYPE_FIELDS (field_info_union_node)),
1235 (FIELD_STATIC (fdecl)
1236 ? build_address_of (build_static_field_ref (fdecl))
1237 : byte_position (fdecl)))));
1239 FINISH_RECORD_CONSTRUCTOR (finit);
1240 return finit;
1243 static tree
1244 make_method_value (mdecl)
1245 tree mdecl;
1247 static int method_name_count = 0;
1248 tree minit;
1249 tree index;
1250 tree code;
1251 #define ACC_TRANSLATED 0x4000
1252 int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1254 if (!flag_indirect_dispatch && DECL_VINDEX (mdecl) != NULL_TREE)
1255 index = DECL_VINDEX (mdecl);
1256 else
1257 index = integer_minus_one_node;
1259 code = null_pointer_node;
1260 if (DECL_RTL_SET_P (mdecl))
1261 code = build1 (ADDR_EXPR, nativecode_ptr_type_node, mdecl);
1262 START_RECORD_CONSTRUCTOR (minit, method_type_node);
1263 PUSH_FIELD_VALUE (minit, "name",
1264 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1265 init_identifier_node
1266 : DECL_NAME (mdecl)));
1268 tree signature = build_java_signature (TREE_TYPE (mdecl));
1269 PUSH_FIELD_VALUE (minit, "signature",
1270 (build_utf8_ref
1271 (unmangle_classname
1272 (IDENTIFIER_POINTER(signature),
1273 IDENTIFIER_LENGTH(signature)))));
1275 PUSH_FIELD_VALUE (minit, "accflags", build_int_2 (accflags, 0));
1276 PUSH_FIELD_VALUE (minit, "index", index);
1277 PUSH_FIELD_VALUE (minit, "ncode", code);
1280 /* Compute the `throws' information for the method. */
1281 tree table = null_pointer_node;
1282 if (DECL_FUNCTION_THROWS (mdecl) != NULL_TREE)
1284 int length = 1 + list_length (DECL_FUNCTION_THROWS (mdecl));
1285 tree iter, type, array;
1286 char buf[60];
1288 table = tree_cons (NULL_TREE, table, NULL_TREE);
1289 for (iter = DECL_FUNCTION_THROWS (mdecl);
1290 iter != NULL_TREE;
1291 iter = TREE_CHAIN (iter))
1293 tree sig = DECL_NAME (TYPE_NAME (TREE_VALUE (iter)));
1294 tree utf8
1295 = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
1296 IDENTIFIER_LENGTH (sig)));
1297 table = tree_cons (NULL_TREE, utf8, table);
1299 type = build_prim_array_type (ptr_type_node, length);
1300 table = build (CONSTRUCTOR, type, NULL_TREE, table);
1301 /* Compute something unique enough. */
1302 sprintf (buf, "_methods%d", method_name_count++);
1303 array = build_decl (VAR_DECL, get_identifier (buf), type);
1304 DECL_INITIAL (array) = table;
1305 TREE_STATIC (array) = 1;
1306 DECL_ARTIFICIAL (array) = 1;
1307 DECL_IGNORED_P (array) = 1;
1308 rest_of_decl_compilation (array, (char*) 0, 1, 0);
1310 table = build1 (ADDR_EXPR, ptr_type_node, array);
1313 PUSH_FIELD_VALUE (minit, "throws", table);
1316 FINISH_RECORD_CONSTRUCTOR (minit);
1317 return minit;
1320 static tree
1321 get_dispatch_vector (type)
1322 tree type;
1324 tree vtable = TYPE_VTABLE (type);
1325 if (vtable == NULL)
1327 HOST_WIDE_INT i;
1328 tree method;
1329 tree super = CLASSTYPE_SUPER (type);
1330 HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1331 vtable = make_tree_vec (nvirtuals);
1332 TYPE_VTABLE (type) = vtable;
1333 if (super != NULL_TREE)
1335 tree super_vtable = get_dispatch_vector (super);
1337 for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1338 TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1341 for (method = TYPE_METHODS (type); method != NULL_TREE;
1342 method = TREE_CHAIN (method))
1343 if (DECL_VINDEX (method) != NULL_TREE
1344 && host_integerp (DECL_VINDEX (method), 0))
1345 TREE_VEC_ELT (vtable, tree_low_cst (DECL_VINDEX (method), 0))
1346 = method;
1349 return vtable;
1352 static tree
1353 get_dispatch_table (type, this_class_addr)
1354 tree type, this_class_addr;
1356 int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1357 tree vtable = get_dispatch_vector (type);
1358 int i, j;
1359 tree list = NULL_TREE;
1360 int nvirtuals = TREE_VEC_LENGTH (vtable);
1361 int arraysize;
1362 tree gc_descr;
1364 for (i = nvirtuals; --i >= 0; )
1366 tree method = TREE_VEC_ELT (vtable, i);
1367 if (METHOD_ABSTRACT (method))
1369 if (! abstract_p)
1370 warning_with_decl (method,
1371 "abstract method in non-abstract class");
1373 if (TARGET_VTABLE_USES_DESCRIPTORS)
1374 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1375 list = tree_cons (NULL_TREE, null_pointer_node, list);
1376 else
1377 list = tree_cons (NULL_TREE, null_pointer_node, list);
1379 else
1381 if (!DECL_RTL_SET_P (method))
1382 make_decl_rtl (method, NULL);
1384 if (TARGET_VTABLE_USES_DESCRIPTORS)
1385 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1387 tree fdesc = build (FDESC_EXPR, nativecode_ptr_type_node,
1388 method, build_int_2 (j, 0));
1389 TREE_CONSTANT (fdesc) = 1;
1390 list = tree_cons (NULL_TREE, fdesc, list);
1392 else
1393 list = tree_cons (NULL_TREE,
1394 build1 (ADDR_EXPR, nativecode_ptr_type_node,
1395 method),
1396 list);
1400 /* Dummy entry for compatibility with G++ -fvtable-thunks. When
1401 using the Boehm GC we sometimes stash a GC type descriptor
1402 there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1403 the emitted byte count during the output to the assembly file. */
1404 /* With TARGET_VTABLE_USES_DESCRIPTORS, we only add one extra
1405 fake "function descriptor". It's first word is the is the class
1406 pointer, and subsequent words (usually one) contain the GC descriptor.
1407 In all other cases, we reserve two extra vtable slots. */
1408 gc_descr = get_boehm_type_descriptor (type);
1409 list = tree_cons (NULL_TREE, gc_descr, list);
1410 for (j = 1; j < TARGET_VTABLE_USES_DESCRIPTORS-1; ++j)
1411 list = tree_cons (NULL_TREE, gc_descr, list);
1412 list = tree_cons (NULL_TREE, this_class_addr, list);
1414 /** Pointer to type_info object (to be implemented), according to g++ ABI. */
1415 list = tree_cons (NULL_TREE, null_pointer_node, list);
1416 /** Offset to start of whole object. Always (ptrdiff_t)0 for Java. */
1417 list = tree_cons (integer_zero_node, null_pointer_node, list);
1419 arraysize = (TARGET_VTABLE_USES_DESCRIPTORS? nvirtuals + 1 : nvirtuals + 2);
1420 if (TARGET_VTABLE_USES_DESCRIPTORS)
1421 arraysize *= TARGET_VTABLE_USES_DESCRIPTORS;
1422 arraysize += 2;
1423 return build (CONSTRUCTOR,
1424 build_prim_array_type (nativecode_ptr_type_node, arraysize),
1425 NULL_TREE, list);
1428 void
1429 make_class_data (type)
1430 tree type;
1432 tree decl, cons, temp;
1433 tree field, fields_decl;
1434 tree static_fields = NULL_TREE;
1435 tree instance_fields = NULL_TREE;
1436 HOST_WIDE_INT static_field_count = 0;
1437 HOST_WIDE_INT instance_field_count = 0;
1438 HOST_WIDE_INT field_count;
1439 tree field_array_type;
1440 tree method;
1441 tree methods = NULL_TREE;
1442 tree dtable_decl = NULL_TREE;
1443 HOST_WIDE_INT method_count = 0;
1444 tree method_array_type;
1445 tree methods_decl;
1446 tree super;
1447 tree this_class_addr;
1448 tree constant_pool_constructor;
1449 tree interfaces = null_pointer_node;
1450 int interface_len = 0;
1451 tree type_decl = TYPE_NAME (type);
1452 /** Offset from start of virtual function table declaration
1453 to where objects actually point at, following new g++ ABI. */
1454 tree dtable_start_offset = build_int_2 (2 * POINTER_SIZE / BITS_PER_UNIT, 0);
1456 this_class_addr = build_class_ref (type);
1457 decl = TREE_OPERAND (this_class_addr, 0);
1459 /* Build Field array. */
1460 field = TYPE_FIELDS (type);
1461 if (DECL_NAME (field) == NULL_TREE)
1462 field = TREE_CHAIN (field); /* Skip dummy field for inherited data. */
1463 for ( ; field != NULL_TREE; field = TREE_CHAIN (field))
1465 if (! DECL_ARTIFICIAL (field))
1467 tree init = make_field_value (field);
1468 if (FIELD_STATIC (field))
1470 tree initial = DECL_INITIAL (field);
1471 static_field_count++;
1472 static_fields = tree_cons (NULL_TREE, init, static_fields);
1473 /* If the initial value is a string constant,
1474 prevent output_constant from trying to assemble the value. */
1475 if (initial != NULL_TREE
1476 && TREE_TYPE (initial) == string_ptr_type_node)
1477 DECL_INITIAL (field) = NULL_TREE;
1478 rest_of_decl_compilation (field, (char*) 0, 1, 1);
1479 DECL_INITIAL (field) = initial;
1481 else
1483 instance_field_count++;
1484 instance_fields = tree_cons (NULL_TREE, init, instance_fields);
1488 field_count = static_field_count + instance_field_count;
1489 if (field_count > 0)
1491 static_fields = nreverse (static_fields);
1492 instance_fields = nreverse (instance_fields);
1493 static_fields = chainon (static_fields, instance_fields);
1494 field_array_type = build_prim_array_type (field_type_node, field_count);
1495 fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
1496 field_array_type);
1497 DECL_INITIAL (fields_decl) = build (CONSTRUCTOR, field_array_type,
1498 NULL_TREE, static_fields);
1499 TREE_STATIC (fields_decl) = 1;
1500 DECL_ARTIFICIAL (fields_decl) = 1;
1501 DECL_IGNORED_P (fields_decl) = 1;
1502 rest_of_decl_compilation (fields_decl, (char*) 0, 1, 0);
1504 else
1505 fields_decl = NULL_TREE;
1507 /* Build Method array. */
1508 for (method = TYPE_METHODS (type);
1509 method != NULL_TREE; method = TREE_CHAIN (method))
1511 tree init;
1512 if (METHOD_PRIVATE (method)
1513 && ! flag_keep_inline_functions
1514 && (flag_inline_functions || optimize))
1515 continue;
1516 init = make_method_value (method);
1517 method_count++;
1518 methods = tree_cons (NULL_TREE, init, methods);
1520 method_array_type = build_prim_array_type (method_type_node, method_count);
1521 methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1522 method_array_type);
1523 DECL_INITIAL (methods_decl) = build (CONSTRUCTOR, method_array_type,
1524 NULL_TREE, nreverse (methods));
1525 TREE_STATIC (methods_decl) = 1;
1526 DECL_ARTIFICIAL (methods_decl) = 1;
1527 DECL_IGNORED_P (methods_decl) = 1;
1528 rest_of_decl_compilation (methods_decl, (char*) 0, 1, 0);
1530 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
1531 && ! CLASS_INTERFACE (type_decl) && !flag_indirect_dispatch)
1533 tree dtable = get_dispatch_table (type, this_class_addr);
1534 dtable_decl = build_dtable_decl (type);
1535 DECL_INITIAL (dtable_decl) = dtable;
1536 TREE_STATIC (dtable_decl) = 1;
1537 DECL_ARTIFICIAL (dtable_decl) = 1;
1538 DECL_IGNORED_P (dtable_decl) = 1;
1539 TREE_PUBLIC (dtable_decl) = 1;
1540 rest_of_decl_compilation (dtable_decl, (char*) 0, 1, 0);
1541 if (type == class_type_node)
1542 class_dtable_decl = dtable_decl;
1545 if (class_dtable_decl == NULL_TREE)
1547 class_dtable_decl = build_dtable_decl (class_type_node);
1548 TREE_STATIC (class_dtable_decl) = 1;
1549 DECL_ARTIFICIAL (class_dtable_decl) = 1;
1550 DECL_IGNORED_P (class_dtable_decl) = 1;
1551 if (is_compiled_class (class_type_node) != 2)
1552 DECL_EXTERNAL (class_dtable_decl) = 1;
1553 rest_of_decl_compilation (class_dtable_decl, (char*) 0, 1, 0);
1556 super = CLASSTYPE_SUPER (type);
1557 if (super == NULL_TREE)
1558 super = null_pointer_node;
1559 else if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl))))
1560 super = build_class_ref (super);
1561 else
1563 int super_index = alloc_class_constant (super);
1564 super = build_int_2 (super_index, 0);
1565 TREE_TYPE (super) = ptr_type_node;
1568 /* Build and emit the array of implemented interfaces. */
1569 if (type != object_type_node)
1570 interface_len = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)) - 1;
1571 if (interface_len > 0)
1573 tree init = NULL_TREE;
1574 int i;
1575 tree interface_array_type, idecl;
1576 interface_array_type
1577 = build_prim_array_type (class_ptr_type, interface_len);
1578 idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1579 interface_array_type);
1580 for (i = interface_len; i > 0; i--)
1582 tree child = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), i);
1583 tree iclass = BINFO_TYPE (child);
1584 tree index;
1585 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass)))))
1586 index = build_class_ref (iclass);
1587 else
1589 int int_index = alloc_class_constant (iclass);
1590 index = build_int_2 (int_index, 0);
1591 TREE_TYPE (index) = ptr_type_node;
1593 init = tree_cons (NULL_TREE, index, init);
1595 DECL_INITIAL (idecl) = build (CONSTRUCTOR, interface_array_type,
1596 NULL_TREE, init);
1597 TREE_STATIC (idecl) = 1;
1598 DECL_ARTIFICIAL (idecl) = 1;
1599 DECL_IGNORED_P (idecl) = 1;
1600 interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1601 rest_of_decl_compilation (idecl, (char*) 0, 1, 0);
1604 constant_pool_constructor = build_constants_constructor ();
1606 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1607 PUSH_FIELD_VALUE (temp, "vtable",
1608 build (PLUS_EXPR, dtable_ptr_type,
1609 build1 (ADDR_EXPR, dtable_ptr_type, class_dtable_decl),
1610 dtable_start_offset));
1611 if (! flag_hash_synchronization)
1612 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1613 FINISH_RECORD_CONSTRUCTOR (temp);
1614 START_RECORD_CONSTRUCTOR (cons, class_type_node);
1615 PUSH_SUPER_VALUE (cons, temp);
1616 PUSH_FIELD_VALUE (cons, "next", null_pointer_node);
1617 PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
1618 PUSH_FIELD_VALUE (cons, "accflags",
1619 build_int_2 (get_access_flags_from_decl (type_decl), 0));
1621 PUSH_FIELD_VALUE (cons, "superclass",
1622 CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
1623 PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1624 PUSH_FIELD_VALUE (cons, "methods",
1625 build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1626 PUSH_FIELD_VALUE (cons, "method_count", build_int_2 (method_count, 0));
1628 if (flag_indirect_dispatch)
1629 PUSH_FIELD_VALUE (cons, "vtable_method_count", integer_minus_one_node)
1630 else
1631 PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
1633 PUSH_FIELD_VALUE (cons, "fields",
1634 fields_decl == NULL_TREE ? null_pointer_node
1635 : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1636 PUSH_FIELD_VALUE (cons, "size_in_bytes", size_in_bytes (type));
1637 PUSH_FIELD_VALUE (cons, "field_count", build_int_2 (field_count, 0));
1638 PUSH_FIELD_VALUE (cons, "static_field_count",
1639 build_int_2 (static_field_count, 0));
1641 if (flag_indirect_dispatch)
1642 PUSH_FIELD_VALUE (cons, "vtable", null_pointer_node)
1643 else
1644 PUSH_FIELD_VALUE (cons, "vtable",
1645 dtable_decl == NULL_TREE ? null_pointer_node
1646 : build (PLUS_EXPR, dtable_ptr_type,
1647 build1 (ADDR_EXPR, dtable_ptr_type, dtable_decl),
1648 dtable_start_offset));
1650 if (otable_methods == NULL_TREE)
1652 PUSH_FIELD_VALUE (cons, "otable", null_pointer_node);
1653 PUSH_FIELD_VALUE (cons, "otable_syms", null_pointer_node);
1655 else
1657 PUSH_FIELD_VALUE (cons, "otable",
1658 build1 (ADDR_EXPR, otable_ptr_type, otable_decl));
1659 PUSH_FIELD_VALUE (cons, "otable_syms",
1660 build1 (ADDR_EXPR, method_symbols_array_ptr_type,
1661 otable_syms_decl));
1663 PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1664 PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1665 PUSH_FIELD_VALUE (cons, "interface_count", build_int_2 (interface_len, 0));
1666 PUSH_FIELD_VALUE (cons, "state", integer_zero_node);
1668 PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1669 PUSH_FIELD_VALUE (cons, "depth", integer_zero_node);
1670 PUSH_FIELD_VALUE (cons, "ancestors", null_pointer_node);
1671 PUSH_FIELD_VALUE (cons, "idt", null_pointer_node);
1672 PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
1673 PUSH_FIELD_VALUE (cons, "protectionDomain", null_pointer_node);
1674 PUSH_FIELD_VALUE (cons, "chain", null_pointer_node);
1676 FINISH_RECORD_CONSTRUCTOR (cons);
1678 DECL_INITIAL (decl) = cons;
1680 /* Hash synchronization requires at least 64-bit alignment. */
1681 if (flag_hash_synchronization && POINTER_SIZE < 64)
1682 DECL_ALIGN (decl) = 64;
1684 rest_of_decl_compilation (decl, (char*) 0, 1, 0);
1687 void
1688 finish_class ()
1690 tree method;
1691 tree type_methods = TYPE_METHODS (current_class);
1692 int saw_native_method = 0;
1694 /* Find out if we have any native methods. We use this information
1695 later. */
1696 for (method = type_methods;
1697 method != NULL_TREE;
1698 method = TREE_CHAIN (method))
1700 if (METHOD_NATIVE (method))
1702 saw_native_method = 1;
1703 break;
1707 /* Emit deferred inline methods. */
1708 for (method = type_methods; method != NULL_TREE; )
1710 if (! TREE_ASM_WRITTEN (method) && DECL_SAVED_INSNS (method) != 0)
1712 output_inline_function (method);
1713 /* Scan the list again to see if there are any earlier
1714 methods to emit. */
1715 method = type_methods;
1716 continue;
1718 method = TREE_CHAIN (method);
1721 current_function_decl = NULL_TREE;
1722 make_class_data (current_class);
1723 register_class ();
1724 rest_of_decl_compilation (TYPE_NAME (current_class), (char*) 0, 1, 0);
1727 /* Return 2 if CLASS is compiled by this compilation job;
1728 return 1 if CLASS can otherwise be assumed to be compiled;
1729 return 0 if we cannot assume that CLASS is compiled.
1730 Returns 1 for primitive and 0 for array types. */
1732 is_compiled_class (class)
1733 tree class;
1735 int seen_in_zip;
1736 if (TREE_CODE (class) == POINTER_TYPE)
1737 class = TREE_TYPE (class);
1738 if (TREE_CODE (class) != RECORD_TYPE) /* Primitive types are static. */
1739 return 1;
1740 if (TYPE_ARRAY_P (class))
1741 return 0;
1742 if (class == current_class)
1743 return 2;
1745 seen_in_zip = (TYPE_JCF (class) && JCF_SEEN_IN_ZIP (TYPE_JCF (class)));
1746 if (CLASS_FROM_CURRENTLY_COMPILED_P (class) || seen_in_zip)
1748 /* The class was seen in the current ZIP file and will be
1749 available as a compiled class in the future but may not have
1750 been loaded already. Load it if necessary. This prevent
1751 build_class_ref () from crashing. */
1753 if (seen_in_zip && !CLASS_LOADED_P (class))
1754 load_class (class, 1);
1756 /* We return 2 for class seen in ZIP and class from files
1757 belonging to the same compilation unit */
1758 return 2;
1761 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class)))))
1763 if (!CLASS_LOADED_P (class))
1765 if (CLASS_FROM_SOURCE_P (class))
1766 safe_layout_class (class);
1767 else
1768 load_class (class, 1);
1770 return 1;
1773 return 0;
1776 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
1778 tree
1779 build_dtable_decl (type)
1780 tree type;
1782 tree dtype;
1784 /* We need to build a new dtable type so that its size is uniquely
1785 computed when we're dealing with the class for real and not just
1786 faking it (like java.lang.Class during the initialization of the
1787 compiler.) We know we're not faking a class when CURRENT_CLASS is
1788 TYPE. */
1789 if (current_class == type)
1791 tree dummy = NULL_TREE;
1792 int n;
1794 dtype = make_node (RECORD_TYPE);
1796 PUSH_FIELD (dtype, dummy, "top_offset", ptr_type_node);
1797 PUSH_FIELD (dtype, dummy, "type_info", ptr_type_node);
1799 PUSH_FIELD (dtype, dummy, "class", class_ptr_type);
1800 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
1802 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
1803 TREE_CHAIN (dummy) = tmp_field;
1804 DECL_CONTEXT (tmp_field) = dtype;
1805 DECL_ARTIFICIAL (tmp_field) = 1;
1806 dummy = tmp_field;
1809 PUSH_FIELD (dtype, dummy, "gc_descr", ptr_type_node);
1810 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
1812 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
1813 TREE_CHAIN (dummy) = tmp_field;
1814 DECL_CONTEXT (tmp_field) = dtype;
1815 DECL_ARTIFICIAL (tmp_field) = 1;
1816 dummy = tmp_field;
1819 n = TREE_VEC_LENGTH (get_dispatch_vector (type));
1820 if (TARGET_VTABLE_USES_DESCRIPTORS)
1821 n *= TARGET_VTABLE_USES_DESCRIPTORS;
1823 PUSH_FIELD (dtype, dummy, "methods",
1824 build_prim_array_type (nativecode_ptr_type_node, n));
1825 layout_type (dtype);
1827 else
1828 dtype = dtable_type;
1830 return build_decl (VAR_DECL,
1831 java_mangle_vtable (&temporary_obstack, type), dtype);
1834 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
1835 fields inherited from SUPER_CLASS. */
1837 void
1838 push_super_field (this_class, super_class)
1839 tree this_class, super_class;
1841 tree base_decl;
1842 /* Don't insert the field if we're just re-laying the class out. */
1843 if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
1844 return;
1845 base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
1846 DECL_IGNORED_P (base_decl) = 1;
1847 TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
1848 TYPE_FIELDS (this_class) = base_decl;
1849 DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
1850 DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
1853 /* Handle the different manners we may have to lay out a super class. */
1855 static tree
1856 maybe_layout_super_class (super_class, this_class)
1857 tree super_class;
1858 tree this_class;
1860 if (TREE_CODE (super_class) == RECORD_TYPE)
1862 if (!CLASS_LOADED_P (super_class) && CLASS_FROM_SOURCE_P (super_class))
1863 safe_layout_class (super_class);
1864 if (!CLASS_LOADED_P (super_class))
1865 load_class (super_class, 1);
1867 /* We might have to layout the class before its dependency on
1868 the super class gets resolved by java_complete_class */
1869 else if (TREE_CODE (super_class) == POINTER_TYPE)
1871 if (TREE_TYPE (super_class) != NULL_TREE)
1872 super_class = TREE_TYPE (super_class);
1873 else
1875 super_class = do_resolve_class (NULL_TREE, /* FIXME? */
1876 super_class, NULL_TREE, this_class);
1877 if (!super_class)
1878 return NULL_TREE; /* FIXME, NULL_TREE not checked by caller. */
1879 super_class = TREE_TYPE (super_class);
1882 if (!TYPE_SIZE (super_class))
1883 safe_layout_class (super_class);
1885 return super_class;
1888 void
1889 layout_class (this_class)
1890 tree this_class;
1892 tree super_class = CLASSTYPE_SUPER (this_class);
1893 tree field;
1895 class_list = tree_cons (this_class, NULL_TREE, class_list);
1896 if (CLASS_BEING_LAIDOUT (this_class))
1898 char buffer [1024];
1899 char *report;
1900 tree current;
1902 sprintf (buffer, " with `%s'",
1903 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
1904 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1906 for (current = TREE_CHAIN (class_list); current;
1907 current = TREE_CHAIN (current))
1909 tree decl = TYPE_NAME (TREE_PURPOSE (current));
1910 sprintf (buffer, "\n which inherits from `%s' (%s:%d)",
1911 IDENTIFIER_POINTER (DECL_NAME (decl)),
1912 DECL_SOURCE_FILE (decl),
1913 DECL_SOURCE_LINE (decl));
1914 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1916 obstack_1grow (&temporary_obstack, '\0');
1917 report = obstack_finish (&temporary_obstack);
1918 cyclic_inheritance_report = ggc_strdup (report);
1919 obstack_free (&temporary_obstack, report);
1920 TYPE_SIZE (this_class) = error_mark_node;
1921 return;
1923 CLASS_BEING_LAIDOUT (this_class) = 1;
1925 if (super_class && !CLASS_BEING_LAIDOUT (super_class))
1927 tree maybe_super_class
1928 = maybe_layout_super_class (super_class, this_class);
1929 if (maybe_super_class == NULL
1930 || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
1932 TYPE_SIZE (this_class) = error_mark_node;
1933 CLASS_BEING_LAIDOUT (this_class) = 0;
1934 class_list = TREE_CHAIN (class_list);
1935 return;
1937 if (TYPE_SIZE (this_class) == NULL_TREE)
1938 push_super_field (this_class, maybe_super_class);
1941 for (field = TYPE_FIELDS (this_class);
1942 field != NULL_TREE; field = TREE_CHAIN (field))
1944 if (FIELD_STATIC (field))
1946 /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
1947 SET_DECL_ASSEMBLER_NAME (field,
1948 java_mangle_decl
1949 (&temporary_obstack, field));
1953 layout_type (this_class);
1955 /* Also recursively load/layout any superinterfaces, but only if class was
1956 loaded from bytecode. The source parser will take care of this itself. */
1957 if (!CLASS_FROM_SOURCE_P (this_class))
1959 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
1961 if (basetype_vec)
1963 int n = TREE_VEC_LENGTH (basetype_vec) - 1;
1964 int i;
1965 for (i = n; i > 0; i--)
1967 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
1968 tree super_interface = BINFO_TYPE (vec_elt);
1970 tree maybe_super_interface
1971 = maybe_layout_super_class (super_interface, NULL_TREE);
1972 if (maybe_super_interface == NULL
1973 || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
1975 TYPE_SIZE (this_class) = error_mark_node;
1976 CLASS_BEING_LAIDOUT (this_class) = 0;
1977 class_list = TREE_CHAIN (class_list);
1978 return;
1984 /* Convert the size back to an SI integer value */
1985 TYPE_SIZE_UNIT (this_class) =
1986 fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
1988 CLASS_BEING_LAIDOUT (this_class) = 0;
1989 class_list = TREE_CHAIN (class_list);
1992 void
1993 layout_class_methods (this_class)
1994 tree this_class;
1996 tree method_decl, dtable_count;
1997 tree super_class;
1999 if (TYPE_NVIRTUALS (this_class))
2000 return;
2002 super_class = CLASSTYPE_SUPER (this_class);
2004 if (super_class)
2006 super_class = maybe_layout_super_class (super_class, this_class);
2007 if (!TYPE_NVIRTUALS (super_class))
2008 layout_class_methods (super_class);
2009 dtable_count = TYPE_NVIRTUALS (super_class);
2011 else
2012 dtable_count = integer_zero_node;
2014 TYPE_METHODS (this_class) = nreverse (TYPE_METHODS (this_class));
2016 for (method_decl = TYPE_METHODS (this_class);
2017 method_decl; method_decl = TREE_CHAIN (method_decl))
2018 dtable_count = layout_class_method (this_class, super_class,
2019 method_decl, dtable_count);
2021 TYPE_NVIRTUALS (this_class) = dtable_count;
2024 /* Return 0 if NAME is equal to STR, -1 if STR is "less" than NAME,
2025 and 1 if STR is "greater" than NAME. */
2027 /* Lay METHOD_DECL out, returning a possibly new value of
2028 DTABLE_COUNT. Also mangle the method's name. */
2030 tree
2031 layout_class_method (this_class, super_class, method_decl, dtable_count)
2032 tree this_class, super_class, method_decl, dtable_count;
2034 tree method_name = DECL_NAME (method_decl);
2036 TREE_PUBLIC (method_decl) = 1;
2038 /* This is a good occasion to mangle the method's name */
2039 SET_DECL_ASSEMBLER_NAME (method_decl,
2040 java_mangle_decl (&temporary_obstack,
2041 method_decl));
2042 /* We don't generate a RTL for the method if it's abstract, or if
2043 it's an interface method that isn't clinit. */
2044 if (! METHOD_ABSTRACT (method_decl)
2045 || (CLASS_INTERFACE (TYPE_NAME (this_class))
2046 && (DECL_CLINIT_P (method_decl))))
2047 make_decl_rtl (method_decl, NULL);
2049 if (ID_INIT_P (method_name))
2051 const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
2052 const char *ptr;
2053 for (ptr = p; *ptr; )
2055 if (*ptr++ == '.')
2056 p = ptr;
2058 DECL_CONSTRUCTOR_P (method_decl) = 1;
2059 build_java_argument_signature (TREE_TYPE (method_decl));
2061 else if (! METHOD_STATIC (method_decl) && !DECL_ARTIFICIAL (method_decl))
2063 tree method_sig =
2064 build_java_argument_signature (TREE_TYPE (method_decl));
2065 tree super_method = lookup_argument_method (super_class, method_name,
2066 method_sig);
2067 if (super_method != NULL_TREE && ! METHOD_PRIVATE (super_method))
2069 DECL_VINDEX (method_decl) = DECL_VINDEX (super_method);
2070 if (DECL_VINDEX (method_decl) == NULL_TREE
2071 && !CLASS_FROM_SOURCE_P (this_class))
2072 error_with_decl (method_decl,
2073 "non-static method '%s' overrides static method");
2075 else if (! METHOD_FINAL (method_decl)
2076 && ! METHOD_PRIVATE (method_decl)
2077 && ! CLASS_FINAL (TYPE_NAME (this_class))
2078 && dtable_count)
2080 DECL_VINDEX (method_decl) = dtable_count;
2081 dtable_count = fold (build (PLUS_EXPR, integer_type_node,
2082 dtable_count, integer_one_node));
2086 return dtable_count;
2089 void
2090 register_class ()
2092 /* END does not need to be registered with the garbage collector
2093 because it always points into the list given by REGISTERED_CLASS,
2094 and that variable is registered with the collector. */
2095 static tree end;
2096 tree node = TREE_OPERAND (build_class_ref (current_class), 0);
2097 tree current = copy_node (node);
2099 XEXP (DECL_RTL (current), 0) = copy_rtx (XEXP (DECL_RTL(node), 0));
2100 if (!registered_class)
2101 registered_class = current;
2102 else
2103 TREE_CHAIN (end) = current;
2105 end = current;
2108 /* Emit something to register classes at start-up time.
2110 The preferred mechanism is through the .jcr section, which contain
2111 a list of pointers to classes which get registered during
2112 constructor invoction time. The fallback mechanism is to generate
2113 a `constructor' function which calls _Jv_RegisterClass for each
2114 class in this file. */
2116 void
2117 emit_register_classes ()
2119 /* ??? This isn't quite the correct test. We also have to know
2120 that the target is using gcc's crtbegin/crtend objects rather
2121 than the ones that come with the operating system. */
2122 if (SUPPORTS_WEAK && targetm.have_named_sections)
2124 #ifdef JCR_SECTION_NAME
2125 tree t;
2126 named_section_flags (JCR_SECTION_NAME, SECTION_WRITE);
2127 assemble_align (POINTER_SIZE);
2128 for (t = registered_class; t; t = TREE_CHAIN (t))
2129 assemble_integer (XEXP (DECL_RTL (t), 0),
2130 POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
2131 #else
2132 abort ();
2133 #endif
2135 else
2137 extern tree get_file_function_name PARAMS ((int));
2138 tree init_name = get_file_function_name ('I');
2139 tree init_type = build_function_type (void_type_node, end_params_node);
2140 tree init_decl;
2141 tree t;
2143 init_decl = build_decl (FUNCTION_DECL, init_name, init_type);
2144 SET_DECL_ASSEMBLER_NAME (init_decl, init_name);
2145 TREE_STATIC (init_decl) = 1;
2146 current_function_decl = init_decl;
2147 DECL_RESULT (init_decl) = build_decl (RESULT_DECL, NULL_TREE,
2148 void_type_node);
2150 /* It can be a static function as long as collect2 does not have
2151 to scan the object file to find its ctor/dtor routine. */
2152 TREE_PUBLIC (init_decl) = ! targetm.have_ctors_dtors;
2154 /* Suppress spurious warnings. */
2155 TREE_USED (init_decl) = 1;
2157 pushlevel (0);
2158 make_decl_rtl (init_decl, NULL);
2159 init_function_start (init_decl, input_filename, 0);
2160 expand_function_start (init_decl, 0);
2162 /* Do not allow the function to be deferred. */
2163 current_function_cannot_inline
2164 = "static constructors and destructors cannot be inlined";
2166 for ( t = registered_class; t; t = TREE_CHAIN (t))
2167 emit_library_call (registerClass_libfunc, 0, VOIDmode, 1,
2168 XEXP (DECL_RTL (t), 0), Pmode);
2170 expand_function_end (input_filename, 0, 0);
2171 poplevel (1, 0, 1);
2172 rest_of_compilation (init_decl);
2173 current_function_decl = NULL_TREE;
2175 if (targetm.have_ctors_dtors)
2176 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (init_decl), 0),
2177 DEFAULT_INIT_PRIORITY);
2181 /* Make a method_symbol_type (_Jv_MethodSymbol) node for METHOD. */
2183 tree
2184 build_method_symbols_entry (tree method)
2186 tree clname, name, signature, method_symbol;
2188 clname = build_utf8_ref (DECL_NAME (TYPE_NAME (DECL_CONTEXT (method))));
2189 name = build_utf8_ref (DECL_NAME (method));
2190 signature = build_java_signature (TREE_TYPE (method));
2191 signature = build_utf8_ref (unmangle_classname
2192 (IDENTIFIER_POINTER (signature),
2193 IDENTIFIER_LENGTH (signature)));
2195 START_RECORD_CONSTRUCTOR (method_symbol, method_symbol_type);
2196 PUSH_FIELD_VALUE (method_symbol, "clname", clname);
2197 PUSH_FIELD_VALUE (method_symbol, "name", name);
2198 PUSH_FIELD_VALUE (method_symbol, "signature", signature);
2199 FINISH_RECORD_CONSTRUCTOR (method_symbol);
2200 TREE_CONSTANT (method_symbol) = 1;
2202 return method_symbol;
2205 /* Emit the offset symbols table for indirect virtual dispatch. */
2207 void
2208 emit_offset_symbol_table ()
2210 tree method_list, method, table, list, null_symbol;
2211 tree otable_bound, otable_array_type;
2212 int index;
2214 /* Only emit an offset table if this translation unit actually made virtual
2215 calls. */
2216 if (otable_methods == NULL_TREE)
2217 return;
2219 /* Build a list of _Jv_MethodSymbols for each entry in otable_methods. */
2220 index = 0;
2221 method_list = otable_methods;
2222 list = NULL_TREE;
2223 while (method_list != NULL_TREE)
2225 method = TREE_VALUE (method_list);
2226 list = tree_cons (NULL_TREE, build_method_symbols_entry (method), list);
2227 method_list = TREE_CHAIN (method_list);
2228 index++;
2231 /* Terminate the list with a "null" entry. */
2232 START_RECORD_CONSTRUCTOR (null_symbol, method_symbol_type);
2233 PUSH_FIELD_VALUE (null_symbol, "clname", null_pointer_node);
2234 PUSH_FIELD_VALUE (null_symbol, "name", null_pointer_node);
2235 PUSH_FIELD_VALUE (null_symbol, "signature", null_pointer_node);
2236 FINISH_RECORD_CONSTRUCTOR (null_symbol);
2237 TREE_CONSTANT (null_symbol) = 1;
2238 list = tree_cons (NULL_TREE, null_symbol, list);
2240 /* Put the list in the right order and make it a constructor. */
2241 list = nreverse (list);
2242 table = build (CONSTRUCTOR, method_symbols_array_type, NULL_TREE, list);
2244 /* Make it the initial value for otable_syms and emit the decl. */
2245 DECL_INITIAL (otable_syms_decl) = table;
2246 DECL_ARTIFICIAL (otable_syms_decl) = 1;
2247 DECL_IGNORED_P (otable_syms_decl) = 1;
2248 rest_of_decl_compilation (otable_syms_decl, NULL, 1, 0);
2250 /* Now that its size is known, redefine otable as an uninitialized static
2251 array of INDEX + 1 integers. The extra entry is used by the runtime
2252 to track whether the otable has been initialized. */
2253 otable_bound = build_index_type (build_int_2 (index, 0));
2254 otable_array_type = build_array_type (integer_type_node, otable_bound);
2255 otable_decl = build_decl (VAR_DECL, get_identifier ("otable"),
2256 otable_array_type);
2257 TREE_STATIC (otable_decl) = 1;
2258 TREE_READONLY (otable_decl) = 1;
2259 rest_of_decl_compilation (otable_decl, NULL, 1, 0);
2262 void
2263 init_class_processing ()
2265 registerClass_libfunc = gen_rtx_SYMBOL_REF (Pmode, "_Jv_RegisterClass");
2266 registerResource_libfunc =
2267 gen_rtx_SYMBOL_REF (Pmode, "_Jv_RegisterResource");
2268 fields_ident = get_identifier ("fields");
2269 info_ident = get_identifier ("info");
2270 gcc_obstack_init (&temporary_obstack);
2273 static hashval_t java_treetreehash_hash PARAMS ((const void *));
2274 static int java_treetreehash_compare PARAMS ((const void *, const void *));
2276 /* A hash table mapping trees to trees. Used generally. */
2278 #define JAVA_TREEHASHHASH_H(t) (htab_hash_pointer (t))
2280 static hashval_t
2281 java_treetreehash_hash (k_p)
2282 const void *k_p;
2284 struct treetreehash_entry *k = (struct treetreehash_entry *) k_p;
2285 return JAVA_TREEHASHHASH_H (k->key);
2288 static int
2289 java_treetreehash_compare (k1_p, k2_p)
2290 const void * k1_p;
2291 const void * k2_p;
2293 struct treetreehash_entry * k1 = (struct treetreehash_entry *) k1_p;
2294 tree k2 = (tree) k2_p;
2295 return (k1->key == k2);
2298 tree
2299 java_treetreehash_find (ht, t)
2300 htab_t ht;
2301 tree t;
2303 struct treetreehash_entry *e;
2304 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2305 e = (struct treetreehash_entry *) htab_find_with_hash (ht, t, hv);
2306 if (e == NULL)
2307 return NULL;
2308 else
2309 return e->value;
2312 tree *
2313 java_treetreehash_new (ht, t)
2314 htab_t ht;
2315 tree t;
2317 PTR *e;
2318 struct treetreehash_entry *tthe;
2319 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2321 e = htab_find_slot_with_hash (ht, t, hv, INSERT);
2322 if (*e == NULL)
2324 tthe = (*ht->alloc_f) (1, sizeof (*tthe));
2325 tthe->key = t;
2326 *e = (PTR) tthe;
2328 else
2329 tthe = (struct treetreehash_entry *) *e;
2330 return &tthe->value;
2333 htab_t
2334 java_treetreehash_create (size, gc)
2335 size_t size;
2336 int gc;
2338 if (gc)
2339 return htab_create_ggc (size, java_treetreehash_hash,
2340 java_treetreehash_compare, NULL);
2341 else
2342 return htab_create_alloc (size, java_treetreehash_hash,
2343 java_treetreehash_compare, free, xcalloc, free);
2346 #include "gt-java-class.h"