1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC 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)
12 GNU CC 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 GNU CC; 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> */
30 #include "coretypes.h"
35 #include "java-tree.h"
46 /* DOS brain-damage */
48 #define O_BINARY 0 /* MS-DOS brain-damage */
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. */
80 /* Nonzero if this represents an exclusion. */
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;
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
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
129 assume_compiled_node *found = find_assume_compiled_node (node->child,
137 /* No match yet. Continue through the sibling list. */
138 node = node->sibling;
141 /* No match at all in this tree. */
145 /* Add a new IDENT to the include/exclude tree. It's an exclusion
146 if EXCLUDEP is nonzero. */
149 add_assume_compiled (ident, excludep)
153 assume_compiled_node *parent;
154 assume_compiled_node *node =
155 xmalloc (sizeof (assume_compiled_node));
157 node->ident = xstrdup (ident);
158 node->excludep = excludep;
161 /* Create the root of the tree if it doesn't exist yet. */
163 if (NULL == assume_compiled_tree)
165 assume_compiled_tree = xmalloc (sizeof (assume_compiled_node));
166 assume_compiled_tree->ident = "";
167 assume_compiled_tree->excludep = 0;
168 assume_compiled_tree->sibling = NULL;
169 assume_compiled_tree->child = NULL;
170 assume_compiled_tree->parent = NULL;
173 /* Calling the function with the empty string means we're setting
174 excludep for the root of the hierarchy. */
178 assume_compiled_tree->excludep = excludep;
182 /* Find the parent node for this new node. PARENT will either be a
183 class or a package name. Adjust PARENT accordingly. */
185 parent = find_assume_compiled_node (assume_compiled_tree, ident);
186 if (ident[strlen (parent->ident)] != '.')
187 parent = parent->parent;
189 /* Insert NODE into the tree. */
191 node->parent = parent;
192 node->sibling = parent->child;
193 parent->child = node;
196 /* Returns nonzero if IDENT is the name of a class that the compiler
197 should assume has been compiled to FIXME */
200 assume_compiled (ident)
203 assume_compiled_node *i;
206 if (NULL == assume_compiled_tree)
209 i = find_assume_compiled_node (assume_compiled_tree,
212 result = ! i->excludep;
217 /* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
218 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
219 Also, PREFIX is prepended, and SUFFIX is appended. */
222 ident_subst (old_name, old_length, prefix, old_char, new_char, suffix)
223 const char* old_name;
230 int prefix_len = strlen (prefix);
231 int suffix_len = strlen (suffix);
232 int i = prefix_len + old_length + suffix_len + 1;
236 char *buffer = alloca (i);
238 strcpy (buffer, prefix);
239 for (i = 0; i < old_length; i++)
241 char ch = old_name[i];
244 buffer[prefix_len + i] = ch;
246 strcpy (buffer + prefix_len + old_length, suffix);
247 return get_identifier (buffer);
250 /* Return an IDENTIFIER_NODE the same as OLD_ID,
251 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
252 Also, PREFIX is prepended, and SUFFIX is appended. */
255 identifier_subst (old_id, prefix, old_char, new_char, suffix)
262 return ident_subst (IDENTIFIER_POINTER (old_id), IDENTIFIER_LENGTH (old_id),
263 prefix, old_char, new_char, suffix);
266 /* Generate a valid C identifier from the name of the class TYPE,
267 prefixed by PREFIX. */
270 mangled_classname (prefix, type)
274 tree ident = TYPE_NAME (type);
275 if (TREE_CODE (ident) != IDENTIFIER_NODE)
276 ident = DECL_NAME (ident);
277 return identifier_subst (ident, prefix, '.', '_', "");
284 type = make_node (RECORD_TYPE);
285 TYPE_BINFO (type) = make_tree_vec (6);
286 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
291 /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
292 and where each of the constituents is separated by '/',
293 return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
296 unmangle_classname (name, name_length)
297 const char *name; int name_length;
299 tree to_return = ident_subst (name, name_length, "", '/', '.', "");
300 /* It's not sufficient to compare to_return and get_identifier
301 (name) to determine whether to_return is qualified. There are
302 cases in signature analysis where name will be stripped of a
304 name = IDENTIFIER_POINTER (to_return);
308 QUALIFIED_P (to_return) = 1;
316 push_class (class_type, class_name)
317 tree class_type, class_name;
319 tree decl, signature;
320 const char *save_input_filename = input_filename;
321 int save_lineno = lineno;
322 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
323 CLASS_P (class_type) = 1;
324 input_filename = IDENTIFIER_POINTER (source_name);
326 decl = build_decl (TYPE_DECL, class_name, class_type);
328 /* dbxout needs a DECL_SIZE if in gstabs mode */
329 DECL_SIZE (decl) = integer_zero_node;
331 input_filename = save_input_filename;
332 lineno = save_lineno;
333 signature = identifier_subst (class_name, "L", '.', '/', ";");
334 IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
336 /* Setting DECL_ARTIFICIAL forces dbxout.c to specific the type is
337 both a typedef and in the struct name-space. We may want to re-visit
338 this later, but for now it reduces the changes needed for gdb. */
339 DECL_ARTIFICIAL (decl) = 1;
341 pushdecl_top_level (decl);
346 /* Finds the (global) class named NAME. Creates the class if not found.
347 Also creates associated TYPE_DECL.
348 Does not check if the class actually exists, load the class,
349 fill in field or methods, or do layout_type. */
355 tree decl = IDENTIFIER_CLASS_VALUE (name);
356 if (decl == NULL_TREE)
357 decl = push_class (make_class (), name);
358 return TREE_TYPE (decl);
362 set_super_info (access_flags, this_class, super_class, interfaces_count)
366 int interfaces_count;
368 int total_supers = interfaces_count;
369 tree class_decl = TYPE_NAME (this_class);
373 TYPE_BINFO_BASETYPES (this_class) = make_tree_vec (total_supers);
376 tree super_binfo = make_tree_vec (6);
377 BINFO_TYPE (super_binfo) = super_class;
378 BINFO_OFFSET (super_binfo) = integer_zero_node;
379 TREE_VIA_PUBLIC (super_binfo) = 1;
380 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (this_class)), 0)
382 CLASS_HAS_SUPER (this_class) = 1;
385 set_class_decl_access_flags (access_flags, class_decl);
389 set_class_decl_access_flags (access_flags, class_decl)
393 if (access_flags & ACC_PUBLIC) CLASS_PUBLIC (class_decl) = 1;
394 if (access_flags & ACC_FINAL) CLASS_FINAL (class_decl) = 1;
395 if (access_flags & ACC_SUPER) CLASS_SUPER (class_decl) = 1;
396 if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
397 if (access_flags & ACC_ABSTRACT) CLASS_ABSTRACT (class_decl) = 1;
398 if (access_flags & ACC_STATIC) CLASS_STATIC (class_decl) = 1;
399 if (access_flags & ACC_PRIVATE) CLASS_PRIVATE (class_decl) = 1;
400 if (access_flags & ACC_PROTECTED) CLASS_PROTECTED (class_decl) = 1;
401 if (access_flags & ACC_STRICT) CLASS_STRICTFP (class_decl) = 1;
404 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
405 direct sub-classes of Object are 1, and so on. */
412 if (! CLASS_LOADED_P (clas))
413 load_class (clas, 1);
414 if (TYPE_SIZE (clas) == error_mark_node)
416 while (clas != object_type_node)
419 clas = TYPE_BINFO_BASETYPE (clas, 0);
424 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
427 interface_of_p (type1, type2)
433 if (!(basetype_vec = TYPE_BINFO_BASETYPES (type2)))
435 n = TREE_VEC_LENGTH (basetype_vec);
436 for (i = 0; i < n; i++)
438 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
439 if (vec_elt && BINFO_TYPE (vec_elt) == type1)
442 for (i = 0; i < n; i++)
444 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
445 if (vec_elt && BINFO_TYPE (vec_elt)
446 && interface_of_p (type1, BINFO_TYPE (vec_elt)))
452 /* Return true iff TYPE1 inherits from TYPE2. */
455 inherits_from_p (type1, type2)
458 while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
462 type1 = CLASSTYPE_SUPER (type1);
467 /* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */
470 enclosing_context_p (type1, type2)
473 if (!INNER_CLASS_TYPE_P (type2))
476 for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
478 type2 = (INNER_CLASS_TYPE_P (type2) ?
479 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
488 /* Return 1 iff there exists a common enclosing context between TYPE1
491 int common_enclosing_context_p (type1, type2)
494 if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
497 for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1;
498 type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
499 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
502 for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
503 current = (PURE_INNER_CLASS_TYPE_P (current) ?
504 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
506 if (type1 == current)
513 add_interface_do (basetype_vec, interface_class, i)
514 tree basetype_vec, interface_class;
517 tree interface_binfo = make_tree_vec (6);
518 BINFO_TYPE (interface_binfo) = interface_class;
519 BINFO_OFFSET (interface_binfo) = integer_zero_node;
520 BINFO_VPTR_FIELD (interface_binfo) = integer_zero_node;
521 TREE_VIA_VIRTUAL (interface_binfo) = 1;
522 TREE_VIA_PUBLIC (interface_binfo) = 1;
523 TREE_VEC_ELT (basetype_vec, i) = interface_binfo;
526 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
527 found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
528 if attempt is made to add it twice. */
531 maybe_add_interface (this_class, interface_class)
532 tree this_class, interface_class;
534 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
536 int n = TREE_VEC_LENGTH (basetype_vec);
541 error ("internal error - too many interface type");
544 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
546 else if (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)) == interface_class)
547 return interface_class;
549 add_interface_do (basetype_vec, interface_class, i);
553 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
556 add_interface (this_class, interface_class)
557 tree this_class, interface_class;
559 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
561 int n = TREE_VEC_LENGTH (basetype_vec);
566 error ("internal error - too many interface type");
569 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
572 add_interface_do (basetype_vec, interface_class, i);
576 /* Return the address of a pointer to the first FUNCTION_DECL
577 in the list (*LIST) whose DECL_NAME is NAME. */
580 find_named_method (list, name)
584 while (*list && DECL_NAME (*list) != name)
585 list = &TREE_CHAIN (*list);
591 build_java_method_type (fntype, this_class, access_flags)
596 if (access_flags & ACC_STATIC)
598 return build_method_type (this_class, fntype);
602 add_method_1 (this_class, access_flags, name, function_type)
608 tree method_type, fndecl;
610 method_type = build_java_method_type (function_type,
611 this_class, access_flags);
613 fndecl = build_decl (FUNCTION_DECL, name, method_type);
614 DECL_CONTEXT (fndecl) = this_class;
616 DECL_LANG_SPECIFIC (fndecl)
617 = ggc_alloc_cleared (sizeof (struct lang_decl));
618 DECL_LANG_SPECIFIC (fndecl)->desc = LANG_DECL_FUNC;
620 /* Initialize the static initializer test table. */
622 DECL_FUNCTION_INIT_TEST_TABLE (fndecl) =
623 java_treetreehash_create (10, 1);
625 /* Initialize the initialized (static) class table. */
626 if (access_flags & ACC_STATIC)
627 DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl) =
628 htab_create_ggc (50, htab_hash_pointer, htab_eq_pointer, NULL);
630 /* Initialize the static method invocation compound list */
631 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = NULL_TREE;
633 TREE_CHAIN (fndecl) = TYPE_METHODS (this_class);
634 TYPE_METHODS (this_class) = fndecl;
636 /* Notice that this is a finalizer and update the class type
637 accordingly. This is used to optimize instance allocation. */
638 if (name == finalize_identifier_node
639 && TREE_TYPE (function_type) == void_type_node
640 && TREE_VALUE (TYPE_ARG_TYPES (function_type)) == void_type_node)
641 HAS_FINALIZER_P (this_class) = 1;
643 if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
644 if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
645 if (access_flags & ACC_PRIVATE)
646 METHOD_PRIVATE (fndecl) = DECL_INLINE (fndecl) = 1;
647 if (access_flags & ACC_NATIVE)
649 METHOD_NATIVE (fndecl) = 1;
650 DECL_EXTERNAL (fndecl) = 1;
652 if (access_flags & ACC_STATIC)
653 METHOD_STATIC (fndecl) = DECL_INLINE (fndecl) = 1;
654 if (access_flags & ACC_FINAL)
655 METHOD_FINAL (fndecl) = DECL_INLINE (fndecl) = 1;
656 if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
657 if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
658 if (access_flags & ACC_TRANSIENT) METHOD_TRANSIENT (fndecl) = 1;
659 if (access_flags & ACC_STRICT) METHOD_STRICTFP (fndecl) = 1;
663 /* Add a method to THIS_CLASS.
664 The method's name is NAME.
665 Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
668 add_method (this_class, access_flags, name, method_sig)
674 tree function_type, fndecl;
675 const unsigned char *sig
676 = (const unsigned char *) IDENTIFIER_POINTER (method_sig);
679 fatal_error ("bad method signature");
681 function_type = get_type_from_signature (method_sig);
682 fndecl = add_method_1 (this_class, access_flags, name, function_type);
683 set_java_signature (TREE_TYPE (fndecl), method_sig);
688 add_field (class, name, field_type, flags)
694 int is_static = (flags & ACC_STATIC) != 0;
696 field = build_decl (is_static ? VAR_DECL : FIELD_DECL, name, field_type);
697 TREE_CHAIN (field) = TYPE_FIELDS (class);
698 TYPE_FIELDS (class) = field;
699 DECL_CONTEXT (field) = class;
701 if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
702 if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
703 if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
704 if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
705 if (flags & ACC_VOLATILE) FIELD_VOLATILE (field) = 1;
706 if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
709 FIELD_STATIC (field) = 1;
710 /* Always make field externally visible. This is required so
711 that native methods can always access the field. */
712 TREE_PUBLIC (field) = 1;
713 /* Considered external until we know what classes are being
714 compiled into this object file. */
715 DECL_EXTERNAL (field) = 1;
721 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
724 set_constant_value (field, constant)
725 tree field, constant;
727 if (field == NULL_TREE)
728 warning ("misplaced ConstantValue attribute (not in any field)");
729 else if (DECL_INITIAL (field) != NULL_TREE)
730 warning ("duplicate ConstantValue attribute for field '%s'",
731 IDENTIFIER_POINTER (DECL_NAME (field)));
734 DECL_INITIAL (field) = constant;
735 if (TREE_TYPE (constant) != TREE_TYPE (field)
736 && ! (TREE_TYPE (constant) == int_type_node
737 && INTEGRAL_TYPE_P (TREE_TYPE (field))
738 && TYPE_PRECISION (TREE_TYPE (field)) <= 32)
739 && ! (TREE_TYPE (constant) == utf8const_ptr_type
740 && TREE_TYPE (field) == string_ptr_type_node))
741 error ("ConstantValue attribute of field '%s' has wrong type",
742 IDENTIFIER_POINTER (DECL_NAME (field)));
743 if (FIELD_FINAL (field))
744 DECL_FIELD_FINAL_IUD (field) = 1;
748 /* Count the number of Unicode chars encoded in a given Ut8 string. */
752 strLengthUtf8 (str, len)
756 register unsigned char* ptr = (unsigned char*) str;
757 register unsigned char *limit = ptr + len;
759 for (; ptr < limit; str_length++) {
760 if (UTF8_GET (ptr, limit) < 0)
768 /* Calculate a hash value for a string encoded in Utf8 format.
769 * This returns the same hash value as specified for java.lang.String.hashCode.
773 hashUtf8String (str, len)
777 register const unsigned char* ptr = (const unsigned char*) str;
778 register const unsigned char *limit = ptr + len;
782 int ch = UTF8_GET (ptr, limit);
783 /* Updated specification from
784 http://www.javasoft.com/docs/books/jls/clarify.html. */
785 hash = (31 * hash) + ch;
790 /* Generate a byte array representing the contents of FILENAME. The
791 array is assigned a unique local symbol. The array represents a
792 compiled Java resource, which is accessed by the runtime using
795 compile_resource_file (name, filename)
797 const char *filename;
799 struct stat stat_buf;
803 tree rtype, field = NULL_TREE, data_type, rinit, data, decl;
804 static int Jr_count = 0;
806 fd = open (filename, O_RDONLY | O_BINARY);
809 perror ("Failed to read resource file");
812 if (fstat (fd, &stat_buf) != 0
813 || ! S_ISREG (stat_buf.st_mode))
815 perror ("Could not figure length of resource file");
818 buffer = xmalloc (strlen (name) + stat_buf.st_size);
819 strcpy (buffer, name);
820 read (fd, buffer + strlen (name), stat_buf.st_size);
822 data_type = build_prim_array_type (unsigned_byte_type_node,
823 strlen (name) + stat_buf.st_size);
824 rtype = make_node (RECORD_TYPE);
825 PUSH_FIELD (rtype, field, "name_length", unsigned_int_type_node);
826 PUSH_FIELD (rtype, field, "resource_length", unsigned_int_type_node);
827 PUSH_FIELD (rtype, field, "data", data_type);
828 FINISH_RECORD (rtype);
829 START_RECORD_CONSTRUCTOR (rinit, rtype);
830 PUSH_FIELD_VALUE (rinit, "name_length",
831 build_int_2 (strlen (name), 0));
832 PUSH_FIELD_VALUE (rinit, "resource_length",
833 build_int_2 (stat_buf.st_size, 0));
834 data = build_string (strlen(name) + stat_buf.st_size, buffer);
835 TREE_TYPE (data) = data_type;
836 PUSH_FIELD_VALUE (rinit, "data", data);
837 FINISH_RECORD_CONSTRUCTOR (rinit);
838 TREE_CONSTANT (rinit) = 1;
840 /* Generate a unique-enough identifier. */
841 sprintf(buf, "_Jr%d", ++Jr_count);
843 decl = build_decl (VAR_DECL, get_identifier (buf), rtype);
844 TREE_STATIC (decl) = 1;
845 DECL_ARTIFICIAL (decl) = 1;
846 DECL_IGNORED_P (decl) = 1;
847 TREE_READONLY (decl) = 1;
848 TREE_THIS_VOLATILE (decl) = 0;
849 DECL_INITIAL (decl) = rinit;
850 layout_decl (decl, 0);
852 rest_of_decl_compilation (decl, (char*) 0, global_bindings_p (), 0);
853 make_decl_rtl (decl, (char*) 0);
854 assemble_variable (decl, 1, 0, 0);
857 tree init_name = get_file_function_name ('I');
858 tree init_type = build_function_type (void_type_node, end_params_node);
861 init_decl = build_decl (FUNCTION_DECL, init_name, init_type);
862 SET_DECL_ASSEMBLER_NAME (init_decl, init_name);
863 TREE_STATIC (init_decl) = 1;
864 current_function_decl = init_decl;
865 DECL_RESULT (init_decl) = build_decl (RESULT_DECL,
866 NULL_TREE, void_type_node);
868 /* It can be a static function as long as collect2 does not have
869 to scan the object file to find its ctor/dtor routine. */
870 TREE_PUBLIC (init_decl) = ! targetm.have_ctors_dtors;
873 make_decl_rtl (init_decl, NULL);
874 init_function_start (init_decl, input_filename, 0);
875 expand_function_start (init_decl, 0);
877 emit_library_call (registerResource_libfunc, 0, VOIDmode, 1,
878 gen_rtx (SYMBOL_REF, Pmode, buf),
881 expand_function_end (input_filename, 0, 0);
884 /* Force generation, even with -O3 or deeper. Gross hack. FIXME */
885 int saved_flag = flag_inline_functions;
886 flag_inline_functions = 0;
887 rest_of_compilation (init_decl);
888 flag_inline_functions = saved_flag;
890 current_function_decl = NULL_TREE;
891 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (init_decl), 0),
892 DEFAULT_INIT_PRIORITY);
896 tree utf8_decl_list = NULL_TREE;
899 build_utf8_ref (name)
902 const char * name_ptr = IDENTIFIER_POINTER(name);
903 int name_len = IDENTIFIER_LENGTH(name);
905 tree ctype, field = NULL_TREE, str_type, cinit, string;
906 static int utf8_count = 0;
908 tree ref = IDENTIFIER_UTF8_REF (name);
910 if (ref != NULL_TREE)
913 ctype = make_node (RECORD_TYPE);
914 str_type = build_prim_array_type (unsigned_byte_type_node,
915 name_len + 1); /* Allow for final '\0'. */
916 PUSH_FIELD (ctype, field, "hash", unsigned_short_type_node);
917 PUSH_FIELD (ctype, field, "length", unsigned_short_type_node);
918 PUSH_FIELD (ctype, field, "data", str_type);
919 FINISH_RECORD (ctype);
920 START_RECORD_CONSTRUCTOR (cinit, ctype);
921 name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
922 PUSH_FIELD_VALUE (cinit, "hash", build_int_2 (name_hash, 0));
923 PUSH_FIELD_VALUE (cinit, "length", build_int_2 (name_len, 0));
924 string = build_string (name_len, name_ptr);
925 TREE_TYPE (string) = str_type;
926 PUSH_FIELD_VALUE (cinit, "data", string);
927 FINISH_RECORD_CONSTRUCTOR (cinit);
928 TREE_CONSTANT (cinit) = 1;
930 /* Generate a unique-enough identifier. */
931 sprintf(buf, "_Utf%d", ++utf8_count);
933 decl = build_decl (VAR_DECL, get_identifier (buf), utf8const_type);
934 TREE_STATIC (decl) = 1;
935 DECL_ARTIFICIAL (decl) = 1;
936 DECL_IGNORED_P (decl) = 1;
937 TREE_READONLY (decl) = 1;
938 TREE_THIS_VOLATILE (decl) = 0;
939 DECL_INITIAL (decl) = cinit;
940 #ifdef HAVE_GAS_SHF_MERGE
943 /* Ensure decl_size is a multiple of utf8const_type's alignment. */
944 decl_size = (name_len + 5 + TYPE_ALIGN_UNIT (utf8const_type) - 1)
945 & ~(TYPE_ALIGN_UNIT (utf8const_type) - 1);
946 if (flag_merge_constants && decl_size < 256)
949 int flags = (SECTION_OVERRIDE
950 | SECTION_MERGE | (SECTION_ENTSIZE & decl_size));
951 sprintf (buf, ".rodata.jutf8.%d", decl_size);
952 named_section_flags (buf, flags);
953 DECL_SECTION_NAME (decl) = build_string (strlen (buf), buf);
957 TREE_CHAIN (decl) = utf8_decl_list;
958 layout_decl (decl, 0);
960 rest_of_decl_compilation (decl, (char*) 0, global_bindings_p (), 0);
961 utf8_decl_list = decl;
962 make_decl_rtl (decl, (char*) 0);
963 ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
964 IDENTIFIER_UTF8_REF (name) = ref;
968 /* Build a reference to the class TYPE.
969 Also handles primitive types and array types. */
972 build_class_ref (type)
975 int is_compiled = is_compiled_class (type);
978 tree ref, decl_name, decl;
979 if (TREE_CODE (type) == POINTER_TYPE)
980 type = TREE_TYPE (type);
981 if (TREE_CODE (type) == RECORD_TYPE)
983 if (TYPE_SIZE (type) == error_mark_node)
984 return null_pointer_node;
985 decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
986 "", '/', '/', ".class");
987 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
988 if (decl == NULL_TREE)
990 decl = build_decl (VAR_DECL, decl_name, class_type_node);
991 DECL_SIZE (decl) = TYPE_SIZE (class_type_node);
992 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (class_type_node);
993 TREE_STATIC (decl) = 1;
994 TREE_PUBLIC (decl) = 1;
995 DECL_IGNORED_P (decl) = 1;
996 DECL_ARTIFICIAL (decl) = 1;
997 if (is_compiled == 1)
998 DECL_EXTERNAL (decl) = 1;
999 SET_DECL_ASSEMBLER_NAME (decl,
1000 java_mangle_class_field
1001 (&temporary_obstack, type));
1002 make_decl_rtl (decl, NULL);
1003 pushdecl_top_level (decl);
1010 if (flag_emit_class_files)
1012 const char *prim_class_name;
1014 if (type == char_type_node)
1015 prim_class_name = "java.lang.Character";
1016 else if (type == boolean_type_node)
1017 prim_class_name = "java.lang.Boolean";
1018 else if (type == byte_type_node)
1019 prim_class_name = "java.lang.Byte";
1020 else if (type == short_type_node)
1021 prim_class_name = "java.lang.Short";
1022 else if (type == int_type_node)
1023 prim_class_name = "java.lang.Integer";
1024 else if (type == long_type_node)
1025 prim_class_name = "java.lang.Long";
1026 else if (type == float_type_node)
1027 prim_class_name = "java.lang.Float";
1028 else if (type == double_type_node)
1029 prim_class_name = "java.lang.Double";
1030 else if (type == void_type_node)
1031 prim_class_name = "java.lang.Void";
1035 prim_class = lookup_class (get_identifier (prim_class_name));
1036 return build (COMPONENT_REF, NULL_TREE,
1037 prim_class, TYPE_identifier_node);
1039 decl_name = TYPE_NAME (type);
1040 if (TREE_CODE (decl_name) == TYPE_DECL)
1041 decl_name = DECL_NAME (decl_name);
1042 name = IDENTIFIER_POINTER (decl_name);
1043 if (strncmp (name, "promoted_", 9) == 0)
1045 sprintf (buffer, "_Jv_%sClass", name);
1046 decl_name = get_identifier (buffer);
1047 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1048 if (decl == NULL_TREE)
1050 decl = build_decl (VAR_DECL, decl_name, class_type_node);
1051 TREE_STATIC (decl) = 1;
1052 TREE_PUBLIC (decl) = 1;
1053 DECL_EXTERNAL (decl) = 1;
1054 make_decl_rtl (decl, NULL);
1055 pushdecl_top_level (decl);
1059 ref = build1 (ADDR_EXPR, class_ptr_type, decl);
1066 index = alloc_class_constant (type);
1067 cl = build_ref_from_constant_pool (index);
1068 TREE_TYPE (cl) = promote_type (class_ptr_type);
1074 build_static_field_ref (fdecl)
1077 tree fclass = DECL_CONTEXT (fdecl);
1078 int is_compiled = is_compiled_class (fclass);
1081 if (!DECL_RTL_SET_P (fdecl))
1083 if (is_compiled == 1)
1084 DECL_EXTERNAL (fdecl) = 1;
1085 make_decl_rtl (fdecl, NULL);
1092 * *(FTYPE*)build_class_ref(FCLASS)->fields[INDEX].info.addr */
1093 tree ref = build_class_ref (fclass);
1095 int field_index = 0;
1096 ref = build1 (INDIRECT_REF, class_type_node, ref);
1097 ref = build (COMPONENT_REF, field_ptr_type_node, ref,
1098 lookup_field (&class_type_node, fields_ident));
1100 for (fld = TYPE_FIELDS (fclass); ; fld = TREE_CHAIN (fld))
1104 if (fld == NULL_TREE)
1105 fatal_error ("field '%s' not found in class",
1106 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1107 if (FIELD_STATIC (fld))
1110 field_index *= int_size_in_bytes (field_type_node);
1111 ref = fold (build (PLUS_EXPR, field_ptr_type_node,
1112 ref, build_int_2 (field_index, 0)));
1113 ref = build1 (INDIRECT_REF, field_type_node, ref);
1114 ref = build (COMPONENT_REF, field_info_union_node,
1115 ref, lookup_field (&field_type_node, info_ident));
1116 ref = build (COMPONENT_REF, ptr_type_node,
1117 ref, TREE_CHAIN (TYPE_FIELDS (field_info_union_node)));
1118 return fold (build1 (INDIRECT_REF, TREE_TYPE(fdecl), ref));
1123 get_access_flags_from_decl (decl)
1126 int access_flags = 0;
1127 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
1129 if (FIELD_STATIC (decl))
1130 access_flags |= ACC_STATIC;
1131 if (FIELD_PUBLIC (decl))
1132 access_flags |= ACC_PUBLIC;
1133 if (FIELD_PROTECTED (decl))
1134 access_flags |= ACC_PROTECTED;
1135 if (FIELD_PRIVATE (decl))
1136 access_flags |= ACC_PRIVATE;
1137 if (FIELD_FINAL (decl))
1138 access_flags |= ACC_FINAL;
1139 if (FIELD_VOLATILE (decl))
1140 access_flags |= ACC_VOLATILE;
1141 if (FIELD_TRANSIENT (decl))
1142 access_flags |= ACC_TRANSIENT;
1143 return access_flags;
1145 if (TREE_CODE (decl) == TYPE_DECL)
1147 if (CLASS_PUBLIC (decl))
1148 access_flags |= ACC_PUBLIC;
1149 if (CLASS_FINAL (decl))
1150 access_flags |= ACC_FINAL;
1151 if (CLASS_SUPER (decl))
1152 access_flags |= ACC_SUPER;
1153 if (CLASS_INTERFACE (decl))
1154 access_flags |= ACC_INTERFACE;
1155 if (CLASS_ABSTRACT (decl))
1156 access_flags |= ACC_ABSTRACT;
1157 if (CLASS_STATIC (decl))
1158 access_flags |= ACC_STATIC;
1159 if (CLASS_PRIVATE (decl))
1160 access_flags |= ACC_PRIVATE;
1161 if (CLASS_PROTECTED (decl))
1162 access_flags |= ACC_PROTECTED;
1163 if (CLASS_STRICTFP (decl))
1164 access_flags |= ACC_STRICT;
1165 return access_flags;
1167 if (TREE_CODE (decl) == FUNCTION_DECL)
1169 if (METHOD_PUBLIC (decl))
1170 access_flags |= ACC_PUBLIC;
1171 if (METHOD_PRIVATE (decl))
1172 access_flags |= ACC_PRIVATE;
1173 if (METHOD_PROTECTED (decl))
1174 access_flags |= ACC_PROTECTED;
1175 if (METHOD_STATIC (decl))
1176 access_flags |= ACC_STATIC;
1177 if (METHOD_FINAL (decl))
1178 access_flags |= ACC_FINAL;
1179 if (METHOD_SYNCHRONIZED (decl))
1180 access_flags |= ACC_SYNCHRONIZED;
1181 if (METHOD_NATIVE (decl))
1182 access_flags |= ACC_NATIVE;
1183 if (METHOD_ABSTRACT (decl))
1184 access_flags |= ACC_ABSTRACT;
1185 if (METHOD_TRANSIENT (decl))
1186 access_flags |= ACC_TRANSIENT;
1187 if (METHOD_STRICTFP (decl))
1188 access_flags |= ACC_STRICT;
1189 return access_flags;
1195 make_field_value (fdecl)
1200 tree type = TREE_TYPE (fdecl);
1201 int resolved = is_compiled_class (type);
1203 START_RECORD_CONSTRUCTOR (finit, field_type_node);
1204 PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
1206 type = build_class_ref (type);
1209 tree signature = build_java_signature (type);
1211 type = build_utf8_ref (unmangle_classname
1212 (IDENTIFIER_POINTER (signature),
1213 IDENTIFIER_LENGTH (signature)));
1215 PUSH_FIELD_VALUE (finit, "type", type);
1217 flags = get_access_flags_from_decl (fdecl);
1219 flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1221 PUSH_FIELD_VALUE (finit, "accflags", build_int_2 (flags, 0));
1222 PUSH_FIELD_VALUE (finit, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1226 build (CONSTRUCTOR, field_info_union_node, NULL_TREE,
1228 ((FIELD_STATIC (fdecl)
1229 ? TREE_CHAIN (TYPE_FIELDS (field_info_union_node))
1230 : TYPE_FIELDS (field_info_union_node)),
1231 (FIELD_STATIC (fdecl)
1232 ? build_address_of (build_static_field_ref (fdecl))
1233 : byte_position (fdecl)))));
1235 FINISH_RECORD_CONSTRUCTOR (finit);
1240 make_method_value (mdecl)
1243 static int method_name_count = 0;
1247 #define ACC_TRANSLATED 0x4000
1248 int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1250 if (!flag_indirect_dispatch && DECL_VINDEX (mdecl) != NULL_TREE)
1251 index = DECL_VINDEX (mdecl);
1253 index = integer_minus_one_node;
1255 code = null_pointer_node;
1256 if (DECL_RTL_SET_P (mdecl))
1257 code = build1 (ADDR_EXPR, nativecode_ptr_type_node, mdecl);
1258 START_RECORD_CONSTRUCTOR (minit, method_type_node);
1259 PUSH_FIELD_VALUE (minit, "name",
1260 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1261 init_identifier_node
1262 : DECL_NAME (mdecl)));
1264 tree signature = build_java_signature (TREE_TYPE (mdecl));
1265 PUSH_FIELD_VALUE (minit, "signature",
1268 (IDENTIFIER_POINTER(signature),
1269 IDENTIFIER_LENGTH(signature)))));
1271 PUSH_FIELD_VALUE (minit, "accflags", build_int_2 (accflags, 0));
1272 PUSH_FIELD_VALUE (minit, "index", index);
1273 PUSH_FIELD_VALUE (minit, "ncode", code);
1276 /* Compute the `throws' information for the method. */
1277 tree table = null_pointer_node;
1278 if (DECL_FUNCTION_THROWS (mdecl) != NULL_TREE)
1280 int length = 1 + list_length (DECL_FUNCTION_THROWS (mdecl));
1281 tree iter, type, array;
1284 table = tree_cons (NULL_TREE, table, NULL_TREE);
1285 for (iter = DECL_FUNCTION_THROWS (mdecl);
1287 iter = TREE_CHAIN (iter))
1289 tree sig = DECL_NAME (TYPE_NAME (TREE_VALUE (iter)));
1291 = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
1292 IDENTIFIER_LENGTH (sig)));
1293 table = tree_cons (NULL_TREE, utf8, table);
1295 type = build_prim_array_type (ptr_type_node, length);
1296 table = build (CONSTRUCTOR, type, NULL_TREE, table);
1297 /* Compute something unique enough. */
1298 sprintf (buf, "_methods%d", method_name_count++);
1299 array = build_decl (VAR_DECL, get_identifier (buf), type);
1300 DECL_INITIAL (array) = table;
1301 TREE_STATIC (array) = 1;
1302 DECL_ARTIFICIAL (array) = 1;
1303 DECL_IGNORED_P (array) = 1;
1304 rest_of_decl_compilation (array, (char*) 0, 1, 0);
1306 table = build1 (ADDR_EXPR, ptr_type_node, array);
1309 PUSH_FIELD_VALUE (minit, "throws", table);
1312 FINISH_RECORD_CONSTRUCTOR (minit);
1317 get_dispatch_vector (type)
1320 tree vtable = TYPE_VTABLE (type);
1325 tree super = CLASSTYPE_SUPER (type);
1326 HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1327 vtable = make_tree_vec (nvirtuals);
1328 TYPE_VTABLE (type) = vtable;
1329 if (super != NULL_TREE)
1331 tree super_vtable = get_dispatch_vector (super);
1333 for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1334 TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1337 for (method = TYPE_METHODS (type); method != NULL_TREE;
1338 method = TREE_CHAIN (method))
1339 if (DECL_VINDEX (method) != NULL_TREE
1340 && host_integerp (DECL_VINDEX (method), 0))
1341 TREE_VEC_ELT (vtable, tree_low_cst (DECL_VINDEX (method), 0))
1349 get_dispatch_table (type, this_class_addr)
1350 tree type, this_class_addr;
1352 int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1353 tree vtable = get_dispatch_vector (type);
1355 tree list = NULL_TREE;
1356 int nvirtuals = TREE_VEC_LENGTH (vtable);
1360 for (i = nvirtuals; --i >= 0; )
1362 tree method = TREE_VEC_ELT (vtable, i);
1363 if (METHOD_ABSTRACT (method))
1366 warning_with_decl (method,
1367 "abstract method in non-abstract class");
1369 if (TARGET_VTABLE_USES_DESCRIPTORS)
1370 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1371 list = tree_cons (NULL_TREE, null_pointer_node, list);
1373 list = tree_cons (NULL_TREE, null_pointer_node, list);
1377 if (!DECL_RTL_SET_P (method))
1378 make_decl_rtl (method, NULL);
1380 if (TARGET_VTABLE_USES_DESCRIPTORS)
1381 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1383 tree fdesc = build (FDESC_EXPR, nativecode_ptr_type_node,
1384 method, build_int_2 (j, 0));
1385 TREE_CONSTANT (fdesc) = 1;
1386 list = tree_cons (NULL_TREE, fdesc, list);
1389 list = tree_cons (NULL_TREE,
1390 build1 (ADDR_EXPR, nativecode_ptr_type_node,
1396 /* Dummy entry for compatibility with G++ -fvtable-thunks. When
1397 using the Boehm GC we sometimes stash a GC type descriptor
1398 there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1399 the emitted byte count during the output to the assembly file. */
1400 /* With TARGET_VTABLE_USES_DESCRIPTORS, we only add one extra
1401 fake "function descriptor". It's first word is the is the class
1402 pointer, and subsequent words (usually one) contain the GC descriptor.
1403 In all other cases, we reserve two extra vtable slots. */
1404 gc_descr = get_boehm_type_descriptor (type);
1405 list = tree_cons (NULL_TREE, gc_descr, list);
1406 for (j = 1; j < TARGET_VTABLE_USES_DESCRIPTORS-1; ++j)
1407 list = tree_cons (NULL_TREE, gc_descr, list);
1408 list = tree_cons (NULL_TREE, this_class_addr, list);
1410 /** Pointer to type_info object (to be implemented), according to g++ ABI. */
1411 list = tree_cons (NULL_TREE, null_pointer_node, list);
1412 /** Offset to start of whole object. Always (ptrdiff_t)0 for Java. */
1413 list = tree_cons (integer_zero_node, null_pointer_node, list);
1415 arraysize = (TARGET_VTABLE_USES_DESCRIPTORS? nvirtuals + 1 : nvirtuals + 2);
1416 if (TARGET_VTABLE_USES_DESCRIPTORS)
1417 arraysize *= TARGET_VTABLE_USES_DESCRIPTORS;
1419 return build (CONSTRUCTOR,
1420 build_prim_array_type (nativecode_ptr_type_node, arraysize),
1425 make_class_data (type)
1428 tree decl, cons, temp;
1429 tree field, fields_decl;
1430 tree static_fields = NULL_TREE;
1431 tree instance_fields = NULL_TREE;
1432 HOST_WIDE_INT static_field_count = 0;
1433 HOST_WIDE_INT instance_field_count = 0;
1434 HOST_WIDE_INT field_count;
1435 tree field_array_type;
1437 tree methods = NULL_TREE;
1438 tree dtable_decl = NULL_TREE;
1439 HOST_WIDE_INT method_count = 0;
1440 tree method_array_type;
1443 tree this_class_addr;
1444 tree constant_pool_constructor;
1445 tree interfaces = null_pointer_node;
1446 int interface_len = 0;
1447 tree type_decl = TYPE_NAME (type);
1448 /** Offset from start of virtual function table declaration
1449 to where objects actually point at, following new g++ ABI. */
1450 tree dtable_start_offset = build_int_2 (2 * POINTER_SIZE / BITS_PER_UNIT, 0);
1452 this_class_addr = build_class_ref (type);
1453 decl = TREE_OPERAND (this_class_addr, 0);
1455 /* Build Field array. */
1456 field = TYPE_FIELDS (type);
1457 if (DECL_NAME (field) == NULL_TREE)
1458 field = TREE_CHAIN (field); /* Skip dummy field for inherited data. */
1459 for ( ; field != NULL_TREE; field = TREE_CHAIN (field))
1461 if (! DECL_ARTIFICIAL (field))
1463 tree init = make_field_value (field);
1464 if (FIELD_STATIC (field))
1466 tree initial = DECL_INITIAL (field);
1467 static_field_count++;
1468 static_fields = tree_cons (NULL_TREE, init, static_fields);
1469 /* If the initial value is a string constant,
1470 prevent output_constant from trying to assemble the value. */
1471 if (initial != NULL_TREE
1472 && TREE_TYPE (initial) == string_ptr_type_node)
1473 DECL_INITIAL (field) = NULL_TREE;
1474 rest_of_decl_compilation (field, (char*) 0, 1, 1);
1475 DECL_INITIAL (field) = initial;
1479 instance_field_count++;
1480 instance_fields = tree_cons (NULL_TREE, init, instance_fields);
1484 field_count = static_field_count + instance_field_count;
1485 if (field_count > 0)
1487 static_fields = nreverse (static_fields);
1488 instance_fields = nreverse (instance_fields);
1489 static_fields = chainon (static_fields, instance_fields);
1490 field_array_type = build_prim_array_type (field_type_node, field_count);
1491 fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
1493 DECL_INITIAL (fields_decl) = build (CONSTRUCTOR, field_array_type,
1494 NULL_TREE, static_fields);
1495 TREE_STATIC (fields_decl) = 1;
1496 DECL_ARTIFICIAL (fields_decl) = 1;
1497 DECL_IGNORED_P (fields_decl) = 1;
1498 rest_of_decl_compilation (fields_decl, (char*) 0, 1, 0);
1501 fields_decl = NULL_TREE;
1503 /* Build Method array. */
1504 for (method = TYPE_METHODS (type);
1505 method != NULL_TREE; method = TREE_CHAIN (method))
1508 if (METHOD_PRIVATE (method)
1509 && ! flag_keep_inline_functions
1510 && (flag_inline_functions || optimize))
1512 init = make_method_value (method);
1514 methods = tree_cons (NULL_TREE, init, methods);
1516 method_array_type = build_prim_array_type (method_type_node, method_count);
1517 methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1519 DECL_INITIAL (methods_decl) = build (CONSTRUCTOR, method_array_type,
1520 NULL_TREE, nreverse (methods));
1521 TREE_STATIC (methods_decl) = 1;
1522 DECL_ARTIFICIAL (methods_decl) = 1;
1523 DECL_IGNORED_P (methods_decl) = 1;
1524 rest_of_decl_compilation (methods_decl, (char*) 0, 1, 0);
1526 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
1527 && ! CLASS_INTERFACE (type_decl) && !flag_indirect_dispatch)
1529 tree dtable = get_dispatch_table (type, this_class_addr);
1530 dtable_decl = build_dtable_decl (type);
1531 DECL_INITIAL (dtable_decl) = dtable;
1532 TREE_STATIC (dtable_decl) = 1;
1533 DECL_ARTIFICIAL (dtable_decl) = 1;
1534 DECL_IGNORED_P (dtable_decl) = 1;
1535 TREE_PUBLIC (dtable_decl) = 1;
1536 rest_of_decl_compilation (dtable_decl, (char*) 0, 1, 0);
1537 if (type == class_type_node)
1538 class_dtable_decl = dtable_decl;
1541 if (class_dtable_decl == NULL_TREE)
1543 class_dtable_decl = build_dtable_decl (class_type_node);
1544 TREE_STATIC (class_dtable_decl) = 1;
1545 DECL_ARTIFICIAL (class_dtable_decl) = 1;
1546 DECL_IGNORED_P (class_dtable_decl) = 1;
1547 if (is_compiled_class (class_type_node) != 2)
1548 DECL_EXTERNAL (class_dtable_decl) = 1;
1549 rest_of_decl_compilation (class_dtable_decl, (char*) 0, 1, 0);
1552 super = CLASSTYPE_SUPER (type);
1553 if (super == NULL_TREE)
1554 super = null_pointer_node;
1555 else if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl))))
1556 super = build_class_ref (super);
1559 int super_index = alloc_class_constant (super);
1560 super = build_int_2 (super_index, 0);
1561 TREE_TYPE (super) = ptr_type_node;
1564 /* Build and emit the array of implemented interfaces. */
1565 if (type != object_type_node)
1566 interface_len = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)) - 1;
1567 if (interface_len > 0)
1569 tree init = NULL_TREE;
1571 tree interface_array_type, idecl;
1572 interface_array_type
1573 = build_prim_array_type (class_ptr_type, interface_len);
1574 idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1575 interface_array_type);
1576 for (i = interface_len; i > 0; i--)
1578 tree child = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), i);
1579 tree iclass = BINFO_TYPE (child);
1581 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass)))))
1582 index = build_class_ref (iclass);
1585 int int_index = alloc_class_constant (iclass);
1586 index = build_int_2 (int_index, 0);
1587 TREE_TYPE (index) = ptr_type_node;
1589 init = tree_cons (NULL_TREE, index, init);
1591 DECL_INITIAL (idecl) = build (CONSTRUCTOR, interface_array_type,
1593 TREE_STATIC (idecl) = 1;
1594 DECL_ARTIFICIAL (idecl) = 1;
1595 DECL_IGNORED_P (idecl) = 1;
1596 interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1597 rest_of_decl_compilation (idecl, (char*) 0, 1, 0);
1600 constant_pool_constructor = build_constants_constructor ();
1602 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1603 PUSH_FIELD_VALUE (temp, "vtable",
1604 build (PLUS_EXPR, dtable_ptr_type,
1605 build1 (ADDR_EXPR, dtable_ptr_type, class_dtable_decl),
1606 dtable_start_offset));
1607 if (! flag_hash_synchronization)
1608 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1609 FINISH_RECORD_CONSTRUCTOR (temp);
1610 START_RECORD_CONSTRUCTOR (cons, class_type_node);
1611 PUSH_SUPER_VALUE (cons, temp);
1612 PUSH_FIELD_VALUE (cons, "next", null_pointer_node);
1613 PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
1614 PUSH_FIELD_VALUE (cons, "accflags",
1615 build_int_2 (get_access_flags_from_decl (type_decl), 0));
1617 PUSH_FIELD_VALUE (cons, "superclass",
1618 CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
1619 PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1620 PUSH_FIELD_VALUE (cons, "methods",
1621 build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1622 PUSH_FIELD_VALUE (cons, "method_count", build_int_2 (method_count, 0));
1624 if (flag_indirect_dispatch)
1625 PUSH_FIELD_VALUE (cons, "vtable_method_count", integer_minus_one_node)
1627 PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
1629 PUSH_FIELD_VALUE (cons, "fields",
1630 fields_decl == NULL_TREE ? null_pointer_node
1631 : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1632 PUSH_FIELD_VALUE (cons, "size_in_bytes", size_in_bytes (type));
1633 PUSH_FIELD_VALUE (cons, "field_count", build_int_2 (field_count, 0));
1634 PUSH_FIELD_VALUE (cons, "static_field_count",
1635 build_int_2 (static_field_count, 0));
1637 if (flag_indirect_dispatch)
1638 PUSH_FIELD_VALUE (cons, "vtable", null_pointer_node)
1640 PUSH_FIELD_VALUE (cons, "vtable",
1641 dtable_decl == NULL_TREE ? null_pointer_node
1642 : build (PLUS_EXPR, dtable_ptr_type,
1643 build1 (ADDR_EXPR, dtable_ptr_type, dtable_decl),
1644 dtable_start_offset));
1646 if (otable_methods == NULL_TREE)
1648 PUSH_FIELD_VALUE (cons, "otable", null_pointer_node);
1649 PUSH_FIELD_VALUE (cons, "otable_syms", null_pointer_node);
1653 PUSH_FIELD_VALUE (cons, "otable",
1654 build1 (ADDR_EXPR, otable_ptr_type, otable_decl));
1655 PUSH_FIELD_VALUE (cons, "otable_syms",
1656 build1 (ADDR_EXPR, method_symbols_array_ptr_type,
1659 PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1660 PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1661 PUSH_FIELD_VALUE (cons, "interface_count", build_int_2 (interface_len, 0));
1662 PUSH_FIELD_VALUE (cons, "state", integer_zero_node);
1664 PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1665 PUSH_FIELD_VALUE (cons, "depth", integer_zero_node);
1666 PUSH_FIELD_VALUE (cons, "ancestors", null_pointer_node);
1667 PUSH_FIELD_VALUE (cons, "idt", null_pointer_node);
1668 PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
1669 PUSH_FIELD_VALUE (cons, "protectionDomain", null_pointer_node);
1670 PUSH_FIELD_VALUE (cons, "chain", null_pointer_node);
1672 FINISH_RECORD_CONSTRUCTOR (cons);
1674 DECL_INITIAL (decl) = cons;
1676 /* Hash synchronization requires at least 64-bit alignment. */
1677 if (flag_hash_synchronization && POINTER_SIZE < 64)
1678 DECL_ALIGN (decl) = 64;
1680 rest_of_decl_compilation (decl, (char*) 0, 1, 0);
1687 tree type_methods = TYPE_METHODS (current_class);
1688 int saw_native_method = 0;
1690 /* Find out if we have any native methods. We use this information
1692 for (method = type_methods;
1693 method != NULL_TREE;
1694 method = TREE_CHAIN (method))
1696 if (METHOD_NATIVE (method))
1698 saw_native_method = 1;
1703 /* Emit deferred inline methods. */
1704 for (method = type_methods; method != NULL_TREE; )
1706 if (! TREE_ASM_WRITTEN (method) && DECL_SAVED_INSNS (method) != 0)
1708 output_inline_function (method);
1709 /* Scan the list again to see if there are any earlier
1711 method = type_methods;
1714 method = TREE_CHAIN (method);
1717 current_function_decl = NULL_TREE;
1718 make_class_data (current_class);
1720 rest_of_decl_compilation (TYPE_NAME (current_class), (char*) 0, 1, 0);
1723 /* Return 2 if CLASS is compiled by this compilation job;
1724 return 1 if CLASS can otherwise be assumed to be compiled;
1725 return 0 if we cannot assume that CLASS is compiled.
1726 Returns 1 for primitive and 0 for array types. */
1728 is_compiled_class (class)
1732 if (TREE_CODE (class) == POINTER_TYPE)
1733 class = TREE_TYPE (class);
1734 if (TREE_CODE (class) != RECORD_TYPE) /* Primitive types are static. */
1736 if (TYPE_ARRAY_P (class))
1738 if (class == current_class)
1741 seen_in_zip = (TYPE_JCF (class) && JCF_SEEN_IN_ZIP (TYPE_JCF (class)));
1742 if (CLASS_FROM_CURRENTLY_COMPILED_P (class) || seen_in_zip)
1744 /* The class was seen in the current ZIP file and will be
1745 available as a compiled class in the future but may not have
1746 been loaded already. Load it if necessary. This prevent
1747 build_class_ref () from crashing. */
1749 if (seen_in_zip && !CLASS_LOADED_P (class))
1750 load_class (class, 1);
1752 /* We return 2 for class seen in ZIP and class from files
1753 belonging to the same compilation unit */
1757 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class)))))
1759 if (!CLASS_LOADED_P (class))
1761 if (CLASS_FROM_SOURCE_P (class))
1762 safe_layout_class (class);
1764 load_class (class, 1);
1772 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
1775 build_dtable_decl (type)
1780 /* We need to build a new dtable type so that its size is uniquely
1781 computed when we're dealing with the class for real and not just
1782 faking it (like java.lang.Class during the initialization of the
1783 compiler.) We know we're not faking a class when CURRENT_CLASS is
1785 if (current_class == type)
1787 tree dummy = NULL_TREE;
1790 dtype = make_node (RECORD_TYPE);
1792 PUSH_FIELD (dtype, dummy, "top_offset", ptr_type_node);
1793 PUSH_FIELD (dtype, dummy, "type_info", ptr_type_node);
1795 PUSH_FIELD (dtype, dummy, "class", class_ptr_type);
1796 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
1798 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
1799 TREE_CHAIN (dummy) = tmp_field;
1800 DECL_CONTEXT (tmp_field) = dtype;
1801 DECL_ARTIFICIAL (tmp_field) = 1;
1805 PUSH_FIELD (dtype, dummy, "gc_descr", ptr_type_node);
1806 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
1808 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
1809 TREE_CHAIN (dummy) = tmp_field;
1810 DECL_CONTEXT (tmp_field) = dtype;
1811 DECL_ARTIFICIAL (tmp_field) = 1;
1815 n = TREE_VEC_LENGTH (get_dispatch_vector (type));
1816 if (TARGET_VTABLE_USES_DESCRIPTORS)
1817 n *= TARGET_VTABLE_USES_DESCRIPTORS;
1819 PUSH_FIELD (dtype, dummy, "methods",
1820 build_prim_array_type (nativecode_ptr_type_node, n));
1821 layout_type (dtype);
1824 dtype = dtable_type;
1826 return build_decl (VAR_DECL,
1827 java_mangle_vtable (&temporary_obstack, type), dtype);
1830 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
1831 fields inherited from SUPER_CLASS. */
1834 push_super_field (this_class, super_class)
1835 tree this_class, super_class;
1838 /* Don't insert the field if we're just re-laying the class out. */
1839 if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
1841 base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
1842 DECL_IGNORED_P (base_decl) = 1;
1843 TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
1844 TYPE_FIELDS (this_class) = base_decl;
1845 DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
1846 DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
1849 /* Handle the different manners we may have to lay out a super class. */
1852 maybe_layout_super_class (super_class, this_class)
1856 if (TREE_CODE (super_class) == RECORD_TYPE)
1858 if (!CLASS_LOADED_P (super_class) && CLASS_FROM_SOURCE_P (super_class))
1859 safe_layout_class (super_class);
1860 if (!CLASS_LOADED_P (super_class))
1861 load_class (super_class, 1);
1863 /* We might have to layout the class before its dependency on
1864 the super class gets resolved by java_complete_class */
1865 else if (TREE_CODE (super_class) == POINTER_TYPE)
1867 if (TREE_TYPE (super_class) != NULL_TREE)
1868 super_class = TREE_TYPE (super_class);
1871 super_class = do_resolve_class (NULL_TREE, /* FIXME? */
1872 super_class, NULL_TREE, this_class);
1874 return NULL_TREE; /* FIXME, NULL_TREE not checked by caller. */
1875 super_class = TREE_TYPE (super_class);
1878 if (!TYPE_SIZE (super_class))
1879 safe_layout_class (super_class);
1885 layout_class (this_class)
1888 tree super_class = CLASSTYPE_SUPER (this_class);
1891 class_list = tree_cons (this_class, NULL_TREE, class_list);
1892 if (CLASS_BEING_LAIDOUT (this_class))
1898 sprintf (buffer, " with `%s'",
1899 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
1900 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1902 for (current = TREE_CHAIN (class_list); current;
1903 current = TREE_CHAIN (current))
1905 tree decl = TYPE_NAME (TREE_PURPOSE (current));
1906 sprintf (buffer, "\n which inherits from `%s' (%s:%d)",
1907 IDENTIFIER_POINTER (DECL_NAME (decl)),
1908 DECL_SOURCE_FILE (decl),
1909 DECL_SOURCE_LINE (decl));
1910 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1912 obstack_1grow (&temporary_obstack, '\0');
1913 report = obstack_finish (&temporary_obstack);
1914 cyclic_inheritance_report = ggc_strdup (report);
1915 obstack_free (&temporary_obstack, report);
1916 TYPE_SIZE (this_class) = error_mark_node;
1919 CLASS_BEING_LAIDOUT (this_class) = 1;
1921 if (super_class && !CLASS_BEING_LAIDOUT (super_class))
1923 tree maybe_super_class
1924 = maybe_layout_super_class (super_class, this_class);
1925 if (maybe_super_class == NULL
1926 || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
1928 TYPE_SIZE (this_class) = error_mark_node;
1929 CLASS_BEING_LAIDOUT (this_class) = 0;
1930 class_list = TREE_CHAIN (class_list);
1933 if (TYPE_SIZE (this_class) == NULL_TREE)
1934 push_super_field (this_class, maybe_super_class);
1937 for (field = TYPE_FIELDS (this_class);
1938 field != NULL_TREE; field = TREE_CHAIN (field))
1940 if (FIELD_STATIC (field))
1942 /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
1943 SET_DECL_ASSEMBLER_NAME (field,
1945 (&temporary_obstack, field));
1949 layout_type (this_class);
1951 /* Also recursively load/layout any superinterfaces, but only if class was
1952 loaded from bytecode. The source parser will take care of this itself. */
1953 if (!CLASS_FROM_SOURCE_P (this_class))
1955 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
1959 int n = TREE_VEC_LENGTH (basetype_vec) - 1;
1961 for (i = n; i > 0; i--)
1963 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
1964 tree super_interface = BINFO_TYPE (vec_elt);
1966 tree maybe_super_interface
1967 = maybe_layout_super_class (super_interface, NULL_TREE);
1968 if (maybe_super_interface == NULL
1969 || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
1971 TYPE_SIZE (this_class) = error_mark_node;
1972 CLASS_BEING_LAIDOUT (this_class) = 0;
1973 class_list = TREE_CHAIN (class_list);
1980 /* Convert the size back to an SI integer value */
1981 TYPE_SIZE_UNIT (this_class) =
1982 fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
1984 CLASS_BEING_LAIDOUT (this_class) = 0;
1985 class_list = TREE_CHAIN (class_list);
1989 layout_class_methods (this_class)
1992 tree method_decl, dtable_count;
1995 if (TYPE_NVIRTUALS (this_class))
1998 super_class = CLASSTYPE_SUPER (this_class);
2002 super_class = maybe_layout_super_class (super_class, this_class);
2003 if (!TYPE_NVIRTUALS (super_class))
2004 layout_class_methods (super_class);
2005 dtable_count = TYPE_NVIRTUALS (super_class);
2008 dtable_count = integer_zero_node;
2010 TYPE_METHODS (this_class) = nreverse (TYPE_METHODS (this_class));
2012 for (method_decl = TYPE_METHODS (this_class);
2013 method_decl; method_decl = TREE_CHAIN (method_decl))
2014 dtable_count = layout_class_method (this_class, super_class,
2015 method_decl, dtable_count);
2017 TYPE_NVIRTUALS (this_class) = dtable_count;
2020 /* Return 0 if NAME is equal to STR, -1 if STR is "less" than NAME,
2021 and 1 if STR is "greater" than NAME. */
2023 /* Lay METHOD_DECL out, returning a possibly new value of
2024 DTABLE_COUNT. Also mangle the method's name. */
2027 layout_class_method (this_class, super_class, method_decl, dtable_count)
2028 tree this_class, super_class, method_decl, dtable_count;
2030 tree method_name = DECL_NAME (method_decl);
2032 TREE_PUBLIC (method_decl) = 1;
2034 /* This is a good occasion to mangle the method's name */
2035 SET_DECL_ASSEMBLER_NAME (method_decl,
2036 java_mangle_decl (&temporary_obstack,
2038 /* We don't generate a RTL for the method if it's abstract, or if
2039 it's an interface method that isn't clinit. */
2040 if (! METHOD_ABSTRACT (method_decl)
2041 || (CLASS_INTERFACE (TYPE_NAME (this_class))
2042 && (DECL_CLINIT_P (method_decl))))
2043 make_decl_rtl (method_decl, NULL);
2045 if (ID_INIT_P (method_name))
2047 const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
2049 for (ptr = p; *ptr; )
2054 DECL_CONSTRUCTOR_P (method_decl) = 1;
2055 build_java_argument_signature (TREE_TYPE (method_decl));
2057 else if (! METHOD_STATIC (method_decl) && !DECL_ARTIFICIAL (method_decl))
2060 build_java_argument_signature (TREE_TYPE (method_decl));
2061 tree super_method = lookup_argument_method (super_class, method_name,
2063 if (super_method != NULL_TREE && ! METHOD_PRIVATE (super_method))
2065 DECL_VINDEX (method_decl) = DECL_VINDEX (super_method);
2066 if (DECL_VINDEX (method_decl) == NULL_TREE
2067 && !CLASS_FROM_SOURCE_P (this_class))
2068 error_with_decl (method_decl,
2069 "non-static method '%s' overrides static method");
2071 else if (! METHOD_FINAL (method_decl)
2072 && ! METHOD_PRIVATE (method_decl)
2073 && ! CLASS_FINAL (TYPE_NAME (this_class))
2076 DECL_VINDEX (method_decl) = dtable_count;
2077 dtable_count = fold (build (PLUS_EXPR, integer_type_node,
2078 dtable_count, integer_one_node));
2082 return dtable_count;
2088 /* END does not need to be registered with the garbage collector
2089 because it always points into the list given by REGISTERED_CLASS,
2090 and that variable is registered with the collector. */
2092 tree node = TREE_OPERAND (build_class_ref (current_class), 0);
2093 tree current = copy_node (node);
2095 XEXP (DECL_RTL (current), 0) = copy_rtx (XEXP (DECL_RTL(node), 0));
2096 if (!registered_class)
2097 registered_class = current;
2099 TREE_CHAIN (end) = current;
2104 /* Emit something to register classes at start-up time.
2106 The preferred mechanism is through the .jcr section, which contain
2107 a list of pointers to classes which get registered during
2108 constructor invoction time. The fallback mechanism is to generate
2109 a `constructor' function which calls _Jv_RegisterClass for each
2110 class in this file. */
2113 emit_register_classes ()
2115 /* ??? This isn't quite the correct test. We also have to know
2116 that the target is using gcc's crtbegin/crtend objects rather
2117 than the ones that come with the operating system. */
2118 if (SUPPORTS_WEAK && targetm.have_named_sections)
2120 #ifdef JCR_SECTION_NAME
2122 named_section_flags (JCR_SECTION_NAME, SECTION_WRITE);
2123 assemble_align (POINTER_SIZE);
2124 for (t = registered_class; t; t = TREE_CHAIN (t))
2125 assemble_integer (XEXP (DECL_RTL (t), 0),
2126 POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
2133 extern tree get_file_function_name PARAMS ((int));
2134 tree init_name = get_file_function_name ('I');
2135 tree init_type = build_function_type (void_type_node, end_params_node);
2139 init_decl = build_decl (FUNCTION_DECL, init_name, init_type);
2140 SET_DECL_ASSEMBLER_NAME (init_decl, init_name);
2141 TREE_STATIC (init_decl) = 1;
2142 current_function_decl = init_decl;
2143 DECL_RESULT (init_decl) = build_decl (RESULT_DECL, NULL_TREE,
2146 /* It can be a static function as long as collect2 does not have
2147 to scan the object file to find its ctor/dtor routine. */
2148 TREE_PUBLIC (init_decl) = ! targetm.have_ctors_dtors;
2150 /* Suppress spurious warnings. */
2151 TREE_USED (init_decl) = 1;
2154 make_decl_rtl (init_decl, NULL);
2155 init_function_start (init_decl, input_filename, 0);
2156 expand_function_start (init_decl, 0);
2158 /* Do not allow the function to be deferred. */
2159 current_function_cannot_inline
2160 = "static constructors and destructors cannot be inlined";
2162 for ( t = registered_class; t; t = TREE_CHAIN (t))
2163 emit_library_call (registerClass_libfunc, 0, VOIDmode, 1,
2164 XEXP (DECL_RTL (t), 0), Pmode);
2166 expand_function_end (input_filename, 0, 0);
2168 rest_of_compilation (init_decl);
2169 current_function_decl = NULL_TREE;
2171 if (targetm.have_ctors_dtors)
2172 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (init_decl), 0),
2173 DEFAULT_INIT_PRIORITY);
2177 /* Make a method_symbol_type (_Jv_MethodSymbol) node for METHOD. */
2180 build_method_symbols_entry (tree method)
2182 tree clname, name, signature, method_symbol;
2184 clname = build_utf8_ref (DECL_NAME (TYPE_NAME (DECL_CONTEXT (method))));
2185 name = build_utf8_ref (DECL_NAME (method));
2186 signature = build_java_signature (TREE_TYPE (method));
2187 signature = build_utf8_ref (unmangle_classname
2188 (IDENTIFIER_POINTER (signature),
2189 IDENTIFIER_LENGTH (signature)));
2191 START_RECORD_CONSTRUCTOR (method_symbol, method_symbol_type);
2192 PUSH_FIELD_VALUE (method_symbol, "clname", clname);
2193 PUSH_FIELD_VALUE (method_symbol, "name", name);
2194 PUSH_FIELD_VALUE (method_symbol, "signature", signature);
2195 FINISH_RECORD_CONSTRUCTOR (method_symbol);
2196 TREE_CONSTANT (method_symbol) = 1;
2198 return method_symbol;
2201 /* Emit the offset symbols table for indirect virtual dispatch. */
2204 emit_offset_symbol_table ()
2206 tree method_list, method, table, list, null_symbol;
2207 tree otable_bound, otable_array_type;
2210 /* Only emit an offset table if this translation unit actually made virtual
2212 if (otable_methods == NULL_TREE)
2215 /* Build a list of _Jv_MethodSymbols for each entry in otable_methods. */
2217 method_list = otable_methods;
2219 while (method_list != NULL_TREE)
2221 method = TREE_VALUE (method_list);
2222 list = tree_cons (NULL_TREE, build_method_symbols_entry (method), list);
2223 method_list = TREE_CHAIN (method_list);
2227 /* Terminate the list with a "null" entry. */
2228 START_RECORD_CONSTRUCTOR (null_symbol, method_symbol_type);
2229 PUSH_FIELD_VALUE (null_symbol, "clname", null_pointer_node);
2230 PUSH_FIELD_VALUE (null_symbol, "name", null_pointer_node);
2231 PUSH_FIELD_VALUE (null_symbol, "signature", null_pointer_node);
2232 FINISH_RECORD_CONSTRUCTOR (null_symbol);
2233 TREE_CONSTANT (null_symbol) = 1;
2234 list = tree_cons (NULL_TREE, null_symbol, list);
2236 /* Put the list in the right order and make it a constructor. */
2237 list = nreverse (list);
2238 table = build (CONSTRUCTOR, method_symbols_array_type, NULL_TREE, list);
2240 /* Make it the initial value for otable_syms and emit the decl. */
2241 DECL_INITIAL (otable_syms_decl) = table;
2242 DECL_ARTIFICIAL (otable_syms_decl) = 1;
2243 DECL_IGNORED_P (otable_syms_decl) = 1;
2244 rest_of_decl_compilation (otable_syms_decl, NULL, 1, 0);
2246 /* Now that its size is known, redefine otable as an uninitialized static
2247 array of INDEX + 1 integers. The extra entry is used by the runtime
2248 to track whether the otable has been initialized. */
2249 otable_bound = build_index_type (build_int_2 (index, 0));
2250 otable_array_type = build_array_type (integer_type_node, otable_bound);
2251 otable_decl = build_decl (VAR_DECL, get_identifier ("otable"),
2253 TREE_STATIC (otable_decl) = 1;
2254 TREE_READONLY (otable_decl) = 1;
2255 rest_of_decl_compilation (otable_decl, NULL, 1, 0);
2259 init_class_processing ()
2261 registerClass_libfunc = gen_rtx_SYMBOL_REF (Pmode, "_Jv_RegisterClass");
2262 registerResource_libfunc =
2263 gen_rtx_SYMBOL_REF (Pmode, "_Jv_RegisterResource");
2264 fields_ident = get_identifier ("fields");
2265 info_ident = get_identifier ("info");
2266 gcc_obstack_init (&temporary_obstack);
2269 static hashval_t java_treetreehash_hash PARAMS ((const void *));
2270 static int java_treetreehash_compare PARAMS ((const void *, const void *));
2272 /* A hash table mapping trees to trees. Used generally. */
2274 #define JAVA_TREEHASHHASH_H(t) (htab_hash_pointer (t))
2277 java_treetreehash_hash (k_p)
2280 struct treetreehash_entry *k = (struct treetreehash_entry *) k_p;
2281 return JAVA_TREEHASHHASH_H (k->key);
2285 java_treetreehash_compare (k1_p, k2_p)
2289 struct treetreehash_entry * k1 = (struct treetreehash_entry *) k1_p;
2290 tree k2 = (tree) k2_p;
2291 return (k1->key == k2);
2295 java_treetreehash_find (ht, t)
2299 struct treetreehash_entry *e;
2300 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2301 e = (struct treetreehash_entry *) htab_find_with_hash (ht, t, hv);
2309 java_treetreehash_new (ht, t)
2314 struct treetreehash_entry *tthe;
2315 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2317 e = htab_find_slot_with_hash (ht, t, hv, INSERT);
2320 tthe = (*ht->alloc_f) (1, sizeof (*tthe));
2325 tthe = (struct treetreehash_entry *) *e;
2326 return &tthe->value;
2330 java_treetreehash_create (size, gc)
2335 return htab_create_ggc (size, java_treetreehash_hash,
2336 java_treetreehash_compare, NULL);
2338 return htab_create_alloc (size, java_treetreehash_hash,
2339 java_treetreehash_compare, free, xcalloc, free);
2342 #include "gt-java-class.h"