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)
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> */
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);
1080 /* Allow static final fields to fold to a constant. */
1081 if (is_compiled || FIELD_FINAL (fdecl))
1083 if (!DECL_RTL_SET_P (fdecl))
1085 if (is_compiled == 1)
1086 DECL_EXTERNAL (fdecl) = 1;
1087 make_decl_rtl (fdecl, NULL);
1094 * *(FTYPE*)build_class_ref(FCLASS)->fields[INDEX].info.addr */
1095 tree ref = build_class_ref (fclass);
1097 int field_index = 0;
1098 ref = build1 (INDIRECT_REF, class_type_node, ref);
1099 ref = build (COMPONENT_REF, field_ptr_type_node, ref,
1100 lookup_field (&class_type_node, fields_ident));
1102 for (fld = TYPE_FIELDS (fclass); ; fld = TREE_CHAIN (fld))
1106 if (fld == NULL_TREE)
1107 fatal_error ("field '%s' not found in class",
1108 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1109 if (FIELD_STATIC (fld))
1112 field_index *= int_size_in_bytes (field_type_node);
1113 ref = fold (build (PLUS_EXPR, field_ptr_type_node,
1114 ref, build_int_2 (field_index, 0)));
1115 ref = build1 (INDIRECT_REF, field_type_node, ref);
1116 ref = build (COMPONENT_REF, field_info_union_node,
1117 ref, lookup_field (&field_type_node, info_ident));
1118 ref = build (COMPONENT_REF, ptr_type_node,
1119 ref, TREE_CHAIN (TYPE_FIELDS (field_info_union_node)));
1120 return fold (build1 (INDIRECT_REF, TREE_TYPE(fdecl), ref));
1125 get_access_flags_from_decl (decl)
1128 int access_flags = 0;
1129 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
1131 if (FIELD_STATIC (decl))
1132 access_flags |= ACC_STATIC;
1133 if (FIELD_PUBLIC (decl))
1134 access_flags |= ACC_PUBLIC;
1135 if (FIELD_PROTECTED (decl))
1136 access_flags |= ACC_PROTECTED;
1137 if (FIELD_PRIVATE (decl))
1138 access_flags |= ACC_PRIVATE;
1139 if (FIELD_FINAL (decl))
1140 access_flags |= ACC_FINAL;
1141 if (FIELD_VOLATILE (decl))
1142 access_flags |= ACC_VOLATILE;
1143 if (FIELD_TRANSIENT (decl))
1144 access_flags |= ACC_TRANSIENT;
1145 return access_flags;
1147 if (TREE_CODE (decl) == TYPE_DECL)
1149 if (CLASS_PUBLIC (decl))
1150 access_flags |= ACC_PUBLIC;
1151 if (CLASS_FINAL (decl))
1152 access_flags |= ACC_FINAL;
1153 if (CLASS_SUPER (decl))
1154 access_flags |= ACC_SUPER;
1155 if (CLASS_INTERFACE (decl))
1156 access_flags |= ACC_INTERFACE;
1157 if (CLASS_ABSTRACT (decl))
1158 access_flags |= ACC_ABSTRACT;
1159 if (CLASS_STATIC (decl))
1160 access_flags |= ACC_STATIC;
1161 if (CLASS_PRIVATE (decl))
1162 access_flags |= ACC_PRIVATE;
1163 if (CLASS_PROTECTED (decl))
1164 access_flags |= ACC_PROTECTED;
1165 if (CLASS_STRICTFP (decl))
1166 access_flags |= ACC_STRICT;
1167 return access_flags;
1169 if (TREE_CODE (decl) == FUNCTION_DECL)
1171 if (METHOD_PUBLIC (decl))
1172 access_flags |= ACC_PUBLIC;
1173 if (METHOD_PRIVATE (decl))
1174 access_flags |= ACC_PRIVATE;
1175 if (METHOD_PROTECTED (decl))
1176 access_flags |= ACC_PROTECTED;
1177 if (METHOD_STATIC (decl))
1178 access_flags |= ACC_STATIC;
1179 if (METHOD_FINAL (decl))
1180 access_flags |= ACC_FINAL;
1181 if (METHOD_SYNCHRONIZED (decl))
1182 access_flags |= ACC_SYNCHRONIZED;
1183 if (METHOD_NATIVE (decl))
1184 access_flags |= ACC_NATIVE;
1185 if (METHOD_ABSTRACT (decl))
1186 access_flags |= ACC_ABSTRACT;
1187 if (METHOD_TRANSIENT (decl))
1188 access_flags |= ACC_TRANSIENT;
1189 if (METHOD_STRICTFP (decl))
1190 access_flags |= ACC_STRICT;
1191 return access_flags;
1197 make_field_value (fdecl)
1202 tree type = TREE_TYPE (fdecl);
1203 int resolved = is_compiled_class (type);
1205 START_RECORD_CONSTRUCTOR (finit, field_type_node);
1206 PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
1208 type = build_class_ref (type);
1211 tree signature = build_java_signature (type);
1213 type = build_utf8_ref (unmangle_classname
1214 (IDENTIFIER_POINTER (signature),
1215 IDENTIFIER_LENGTH (signature)));
1217 PUSH_FIELD_VALUE (finit, "type", type);
1219 flags = get_access_flags_from_decl (fdecl);
1221 flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1223 PUSH_FIELD_VALUE (finit, "accflags", build_int_2 (flags, 0));
1224 PUSH_FIELD_VALUE (finit, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1228 build (CONSTRUCTOR, field_info_union_node, NULL_TREE,
1230 ((FIELD_STATIC (fdecl)
1231 ? TREE_CHAIN (TYPE_FIELDS (field_info_union_node))
1232 : TYPE_FIELDS (field_info_union_node)),
1233 (FIELD_STATIC (fdecl)
1234 ? build_address_of (build_static_field_ref (fdecl))
1235 : byte_position (fdecl)))));
1237 FINISH_RECORD_CONSTRUCTOR (finit);
1242 make_method_value (mdecl)
1245 static int method_name_count = 0;
1249 #define ACC_TRANSLATED 0x4000
1250 int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1252 if (!flag_indirect_dispatch && DECL_VINDEX (mdecl) != NULL_TREE)
1253 index = DECL_VINDEX (mdecl);
1255 index = integer_minus_one_node;
1257 code = null_pointer_node;
1258 if (DECL_RTL_SET_P (mdecl))
1259 code = build1 (ADDR_EXPR, nativecode_ptr_type_node, mdecl);
1260 START_RECORD_CONSTRUCTOR (minit, method_type_node);
1261 PUSH_FIELD_VALUE (minit, "name",
1262 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1263 init_identifier_node
1264 : DECL_NAME (mdecl)));
1266 tree signature = build_java_signature (TREE_TYPE (mdecl));
1267 PUSH_FIELD_VALUE (minit, "signature",
1270 (IDENTIFIER_POINTER(signature),
1271 IDENTIFIER_LENGTH(signature)))));
1273 PUSH_FIELD_VALUE (minit, "accflags", build_int_2 (accflags, 0));
1274 PUSH_FIELD_VALUE (minit, "index", index);
1275 PUSH_FIELD_VALUE (minit, "ncode", code);
1278 /* Compute the `throws' information for the method. */
1279 tree table = null_pointer_node;
1280 if (DECL_FUNCTION_THROWS (mdecl) != NULL_TREE)
1282 int length = 1 + list_length (DECL_FUNCTION_THROWS (mdecl));
1283 tree iter, type, array;
1286 table = tree_cons (NULL_TREE, table, NULL_TREE);
1287 for (iter = DECL_FUNCTION_THROWS (mdecl);
1289 iter = TREE_CHAIN (iter))
1291 tree sig = DECL_NAME (TYPE_NAME (TREE_VALUE (iter)));
1293 = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
1294 IDENTIFIER_LENGTH (sig)));
1295 table = tree_cons (NULL_TREE, utf8, table);
1297 type = build_prim_array_type (ptr_type_node, length);
1298 table = build (CONSTRUCTOR, type, NULL_TREE, table);
1299 /* Compute something unique enough. */
1300 sprintf (buf, "_methods%d", method_name_count++);
1301 array = build_decl (VAR_DECL, get_identifier (buf), type);
1302 DECL_INITIAL (array) = table;
1303 TREE_STATIC (array) = 1;
1304 DECL_ARTIFICIAL (array) = 1;
1305 DECL_IGNORED_P (array) = 1;
1306 rest_of_decl_compilation (array, (char*) 0, 1, 0);
1308 table = build1 (ADDR_EXPR, ptr_type_node, array);
1311 PUSH_FIELD_VALUE (minit, "throws", table);
1314 FINISH_RECORD_CONSTRUCTOR (minit);
1319 get_dispatch_vector (type)
1322 tree vtable = TYPE_VTABLE (type);
1327 tree super = CLASSTYPE_SUPER (type);
1328 HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1329 vtable = make_tree_vec (nvirtuals);
1330 TYPE_VTABLE (type) = vtable;
1331 if (super != NULL_TREE)
1333 tree super_vtable = get_dispatch_vector (super);
1335 for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1336 TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1339 for (method = TYPE_METHODS (type); method != NULL_TREE;
1340 method = TREE_CHAIN (method))
1341 if (DECL_VINDEX (method) != NULL_TREE
1342 && host_integerp (DECL_VINDEX (method), 0))
1343 TREE_VEC_ELT (vtable, tree_low_cst (DECL_VINDEX (method), 0))
1351 get_dispatch_table (type, this_class_addr)
1352 tree type, this_class_addr;
1354 int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1355 tree vtable = get_dispatch_vector (type);
1357 tree list = NULL_TREE;
1358 int nvirtuals = TREE_VEC_LENGTH (vtable);
1362 for (i = nvirtuals; --i >= 0; )
1364 tree method = TREE_VEC_ELT (vtable, i);
1365 if (METHOD_ABSTRACT (method))
1368 warning_with_decl (method,
1369 "abstract method in non-abstract class");
1371 if (TARGET_VTABLE_USES_DESCRIPTORS)
1372 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1373 list = tree_cons (NULL_TREE, null_pointer_node, list);
1375 list = tree_cons (NULL_TREE, null_pointer_node, list);
1379 if (!DECL_RTL_SET_P (method))
1380 make_decl_rtl (method, NULL);
1382 if (TARGET_VTABLE_USES_DESCRIPTORS)
1383 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1385 tree fdesc = build (FDESC_EXPR, nativecode_ptr_type_node,
1386 method, build_int_2 (j, 0));
1387 TREE_CONSTANT (fdesc) = 1;
1388 list = tree_cons (NULL_TREE, fdesc, list);
1391 list = tree_cons (NULL_TREE,
1392 build1 (ADDR_EXPR, nativecode_ptr_type_node,
1398 /* Dummy entry for compatibility with G++ -fvtable-thunks. When
1399 using the Boehm GC we sometimes stash a GC type descriptor
1400 there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1401 the emitted byte count during the output to the assembly file. */
1402 /* With TARGET_VTABLE_USES_DESCRIPTORS, we only add one extra
1403 fake "function descriptor". It's first word is the is the class
1404 pointer, and subsequent words (usually one) contain the GC descriptor.
1405 In all other cases, we reserve two extra vtable slots. */
1406 gc_descr = get_boehm_type_descriptor (type);
1407 list = tree_cons (NULL_TREE, gc_descr, list);
1408 for (j = 1; j < TARGET_VTABLE_USES_DESCRIPTORS-1; ++j)
1409 list = tree_cons (NULL_TREE, gc_descr, list);
1410 list = tree_cons (NULL_TREE, this_class_addr, list);
1412 /** Pointer to type_info object (to be implemented), according to g++ ABI. */
1413 list = tree_cons (NULL_TREE, null_pointer_node, list);
1414 /** Offset to start of whole object. Always (ptrdiff_t)0 for Java. */
1415 list = tree_cons (integer_zero_node, null_pointer_node, list);
1417 arraysize = (TARGET_VTABLE_USES_DESCRIPTORS? nvirtuals + 1 : nvirtuals + 2);
1418 if (TARGET_VTABLE_USES_DESCRIPTORS)
1419 arraysize *= TARGET_VTABLE_USES_DESCRIPTORS;
1421 return build (CONSTRUCTOR,
1422 build_prim_array_type (nativecode_ptr_type_node, arraysize),
1427 make_class_data (type)
1430 tree decl, cons, temp;
1431 tree field, fields_decl;
1432 tree static_fields = NULL_TREE;
1433 tree instance_fields = NULL_TREE;
1434 HOST_WIDE_INT static_field_count = 0;
1435 HOST_WIDE_INT instance_field_count = 0;
1436 HOST_WIDE_INT field_count;
1437 tree field_array_type;
1439 tree methods = NULL_TREE;
1440 tree dtable_decl = NULL_TREE;
1441 HOST_WIDE_INT method_count = 0;
1442 tree method_array_type;
1445 tree this_class_addr;
1446 tree constant_pool_constructor;
1447 tree interfaces = null_pointer_node;
1448 int interface_len = 0;
1449 tree type_decl = TYPE_NAME (type);
1450 /** Offset from start of virtual function table declaration
1451 to where objects actually point at, following new g++ ABI. */
1452 tree dtable_start_offset = build_int_2 (2 * POINTER_SIZE / BITS_PER_UNIT, 0);
1454 this_class_addr = build_class_ref (type);
1455 decl = TREE_OPERAND (this_class_addr, 0);
1457 /* Build Field array. */
1458 field = TYPE_FIELDS (type);
1459 if (DECL_NAME (field) == NULL_TREE)
1460 field = TREE_CHAIN (field); /* Skip dummy field for inherited data. */
1461 for ( ; field != NULL_TREE; field = TREE_CHAIN (field))
1463 if (! DECL_ARTIFICIAL (field))
1465 tree init = make_field_value (field);
1466 if (FIELD_STATIC (field))
1468 tree initial = DECL_INITIAL (field);
1469 static_field_count++;
1470 static_fields = tree_cons (NULL_TREE, init, static_fields);
1471 /* If the initial value is a string constant,
1472 prevent output_constant from trying to assemble the value. */
1473 if (initial != NULL_TREE
1474 && TREE_TYPE (initial) == string_ptr_type_node)
1475 DECL_INITIAL (field) = NULL_TREE;
1476 rest_of_decl_compilation (field, (char*) 0, 1, 1);
1477 DECL_INITIAL (field) = initial;
1481 instance_field_count++;
1482 instance_fields = tree_cons (NULL_TREE, init, instance_fields);
1486 field_count = static_field_count + instance_field_count;
1487 if (field_count > 0)
1489 static_fields = nreverse (static_fields);
1490 instance_fields = nreverse (instance_fields);
1491 static_fields = chainon (static_fields, instance_fields);
1492 field_array_type = build_prim_array_type (field_type_node, field_count);
1493 fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
1495 DECL_INITIAL (fields_decl) = build (CONSTRUCTOR, field_array_type,
1496 NULL_TREE, static_fields);
1497 TREE_STATIC (fields_decl) = 1;
1498 DECL_ARTIFICIAL (fields_decl) = 1;
1499 DECL_IGNORED_P (fields_decl) = 1;
1500 rest_of_decl_compilation (fields_decl, (char*) 0, 1, 0);
1503 fields_decl = NULL_TREE;
1505 /* Build Method array. */
1506 for (method = TYPE_METHODS (type);
1507 method != NULL_TREE; method = TREE_CHAIN (method))
1510 if (METHOD_PRIVATE (method)
1511 && ! flag_keep_inline_functions
1512 && (flag_inline_functions || optimize))
1514 init = make_method_value (method);
1516 methods = tree_cons (NULL_TREE, init, methods);
1518 method_array_type = build_prim_array_type (method_type_node, method_count);
1519 methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1521 DECL_INITIAL (methods_decl) = build (CONSTRUCTOR, method_array_type,
1522 NULL_TREE, nreverse (methods));
1523 TREE_STATIC (methods_decl) = 1;
1524 DECL_ARTIFICIAL (methods_decl) = 1;
1525 DECL_IGNORED_P (methods_decl) = 1;
1526 rest_of_decl_compilation (methods_decl, (char*) 0, 1, 0);
1528 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
1529 && ! CLASS_INTERFACE (type_decl) && !flag_indirect_dispatch)
1531 tree dtable = get_dispatch_table (type, this_class_addr);
1532 dtable_decl = build_dtable_decl (type);
1533 DECL_INITIAL (dtable_decl) = dtable;
1534 TREE_STATIC (dtable_decl) = 1;
1535 DECL_ARTIFICIAL (dtable_decl) = 1;
1536 DECL_IGNORED_P (dtable_decl) = 1;
1537 TREE_PUBLIC (dtable_decl) = 1;
1538 rest_of_decl_compilation (dtable_decl, (char*) 0, 1, 0);
1539 if (type == class_type_node)
1540 class_dtable_decl = dtable_decl;
1543 if (class_dtable_decl == NULL_TREE)
1545 class_dtable_decl = build_dtable_decl (class_type_node);
1546 TREE_STATIC (class_dtable_decl) = 1;
1547 DECL_ARTIFICIAL (class_dtable_decl) = 1;
1548 DECL_IGNORED_P (class_dtable_decl) = 1;
1549 if (is_compiled_class (class_type_node) != 2)
1550 DECL_EXTERNAL (class_dtable_decl) = 1;
1551 rest_of_decl_compilation (class_dtable_decl, (char*) 0, 1, 0);
1554 super = CLASSTYPE_SUPER (type);
1555 if (super == NULL_TREE)
1556 super = null_pointer_node;
1557 else if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl))))
1558 super = build_class_ref (super);
1561 int super_index = alloc_class_constant (super);
1562 super = build_int_2 (super_index, 0);
1563 TREE_TYPE (super) = ptr_type_node;
1566 /* Build and emit the array of implemented interfaces. */
1567 if (type != object_type_node)
1568 interface_len = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)) - 1;
1569 if (interface_len > 0)
1571 tree init = NULL_TREE;
1573 tree interface_array_type, idecl;
1574 interface_array_type
1575 = build_prim_array_type (class_ptr_type, interface_len);
1576 idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1577 interface_array_type);
1578 for (i = interface_len; i > 0; i--)
1580 tree child = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), i);
1581 tree iclass = BINFO_TYPE (child);
1583 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass)))))
1584 index = build_class_ref (iclass);
1587 int int_index = alloc_class_constant (iclass);
1588 index = build_int_2 (int_index, 0);
1589 TREE_TYPE (index) = ptr_type_node;
1591 init = tree_cons (NULL_TREE, index, init);
1593 DECL_INITIAL (idecl) = build (CONSTRUCTOR, interface_array_type,
1595 TREE_STATIC (idecl) = 1;
1596 DECL_ARTIFICIAL (idecl) = 1;
1597 DECL_IGNORED_P (idecl) = 1;
1598 interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1599 rest_of_decl_compilation (idecl, (char*) 0, 1, 0);
1602 constant_pool_constructor = build_constants_constructor ();
1604 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1605 PUSH_FIELD_VALUE (temp, "vtable",
1606 build (PLUS_EXPR, dtable_ptr_type,
1607 build1 (ADDR_EXPR, dtable_ptr_type, class_dtable_decl),
1608 dtable_start_offset));
1609 if (! flag_hash_synchronization)
1610 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1611 FINISH_RECORD_CONSTRUCTOR (temp);
1612 START_RECORD_CONSTRUCTOR (cons, class_type_node);
1613 PUSH_SUPER_VALUE (cons, temp);
1614 PUSH_FIELD_VALUE (cons, "next", null_pointer_node);
1615 PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
1616 PUSH_FIELD_VALUE (cons, "accflags",
1617 build_int_2 (get_access_flags_from_decl (type_decl), 0));
1619 PUSH_FIELD_VALUE (cons, "superclass",
1620 CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
1621 PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1622 PUSH_FIELD_VALUE (cons, "methods",
1623 build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1624 PUSH_FIELD_VALUE (cons, "method_count", build_int_2 (method_count, 0));
1626 if (flag_indirect_dispatch)
1627 PUSH_FIELD_VALUE (cons, "vtable_method_count", integer_minus_one_node)
1629 PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
1631 PUSH_FIELD_VALUE (cons, "fields",
1632 fields_decl == NULL_TREE ? null_pointer_node
1633 : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1634 PUSH_FIELD_VALUE (cons, "size_in_bytes", size_in_bytes (type));
1635 PUSH_FIELD_VALUE (cons, "field_count", build_int_2 (field_count, 0));
1636 PUSH_FIELD_VALUE (cons, "static_field_count",
1637 build_int_2 (static_field_count, 0));
1639 if (flag_indirect_dispatch)
1640 PUSH_FIELD_VALUE (cons, "vtable", null_pointer_node)
1642 PUSH_FIELD_VALUE (cons, "vtable",
1643 dtable_decl == NULL_TREE ? null_pointer_node
1644 : build (PLUS_EXPR, dtable_ptr_type,
1645 build1 (ADDR_EXPR, dtable_ptr_type, dtable_decl),
1646 dtable_start_offset));
1648 if (otable_methods == NULL_TREE)
1650 PUSH_FIELD_VALUE (cons, "otable", null_pointer_node);
1651 PUSH_FIELD_VALUE (cons, "otable_syms", null_pointer_node);
1655 PUSH_FIELD_VALUE (cons, "otable",
1656 build1 (ADDR_EXPR, otable_ptr_type, otable_decl));
1657 PUSH_FIELD_VALUE (cons, "otable_syms",
1658 build1 (ADDR_EXPR, method_symbols_array_ptr_type,
1661 PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1662 PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1663 PUSH_FIELD_VALUE (cons, "interface_count", build_int_2 (interface_len, 0));
1664 PUSH_FIELD_VALUE (cons, "state", integer_zero_node);
1666 PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1667 PUSH_FIELD_VALUE (cons, "depth", integer_zero_node);
1668 PUSH_FIELD_VALUE (cons, "ancestors", null_pointer_node);
1669 PUSH_FIELD_VALUE (cons, "idt", null_pointer_node);
1670 PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
1671 PUSH_FIELD_VALUE (cons, "protectionDomain", null_pointer_node);
1672 PUSH_FIELD_VALUE (cons, "chain", null_pointer_node);
1674 FINISH_RECORD_CONSTRUCTOR (cons);
1676 DECL_INITIAL (decl) = cons;
1678 /* Hash synchronization requires at least 64-bit alignment. */
1679 if (flag_hash_synchronization && POINTER_SIZE < 64)
1680 DECL_ALIGN (decl) = 64;
1682 rest_of_decl_compilation (decl, (char*) 0, 1, 0);
1689 tree type_methods = TYPE_METHODS (current_class);
1690 int saw_native_method = 0;
1692 /* Find out if we have any native methods. We use this information
1694 for (method = type_methods;
1695 method != NULL_TREE;
1696 method = TREE_CHAIN (method))
1698 if (METHOD_NATIVE (method))
1700 saw_native_method = 1;
1705 /* Emit deferred inline methods. */
1706 for (method = type_methods; method != NULL_TREE; )
1708 if (! TREE_ASM_WRITTEN (method) && DECL_SAVED_INSNS (method) != 0)
1710 output_inline_function (method);
1711 /* Scan the list again to see if there are any earlier
1713 method = type_methods;
1716 method = TREE_CHAIN (method);
1719 current_function_decl = NULL_TREE;
1720 make_class_data (current_class);
1722 rest_of_decl_compilation (TYPE_NAME (current_class), (char*) 0, 1, 0);
1725 /* Return 2 if CLASS is compiled by this compilation job;
1726 return 1 if CLASS can otherwise be assumed to be compiled;
1727 return 0 if we cannot assume that CLASS is compiled.
1728 Returns 1 for primitive and 0 for array types. */
1730 is_compiled_class (class)
1734 if (TREE_CODE (class) == POINTER_TYPE)
1735 class = TREE_TYPE (class);
1736 if (TREE_CODE (class) != RECORD_TYPE) /* Primitive types are static. */
1738 if (TYPE_ARRAY_P (class))
1740 if (class == current_class)
1743 seen_in_zip = (TYPE_JCF (class) && JCF_SEEN_IN_ZIP (TYPE_JCF (class)));
1744 if (CLASS_FROM_CURRENTLY_COMPILED_P (class) || seen_in_zip)
1746 /* The class was seen in the current ZIP file and will be
1747 available as a compiled class in the future but may not have
1748 been loaded already. Load it if necessary. This prevent
1749 build_class_ref () from crashing. */
1751 if (seen_in_zip && !CLASS_LOADED_P (class))
1752 load_class (class, 1);
1754 /* We return 2 for class seen in ZIP and class from files
1755 belonging to the same compilation unit */
1759 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class)))))
1761 if (!CLASS_LOADED_P (class))
1763 if (CLASS_FROM_SOURCE_P (class))
1764 safe_layout_class (class);
1766 load_class (class, 1);
1774 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
1777 build_dtable_decl (type)
1782 /* We need to build a new dtable type so that its size is uniquely
1783 computed when we're dealing with the class for real and not just
1784 faking it (like java.lang.Class during the initialization of the
1785 compiler.) We know we're not faking a class when CURRENT_CLASS is
1787 if (current_class == type)
1789 tree dummy = NULL_TREE;
1792 dtype = make_node (RECORD_TYPE);
1794 PUSH_FIELD (dtype, dummy, "top_offset", ptr_type_node);
1795 PUSH_FIELD (dtype, dummy, "type_info", ptr_type_node);
1797 PUSH_FIELD (dtype, dummy, "class", class_ptr_type);
1798 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
1800 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
1801 TREE_CHAIN (dummy) = tmp_field;
1802 DECL_CONTEXT (tmp_field) = dtype;
1803 DECL_ARTIFICIAL (tmp_field) = 1;
1807 PUSH_FIELD (dtype, dummy, "gc_descr", ptr_type_node);
1808 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
1810 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
1811 TREE_CHAIN (dummy) = tmp_field;
1812 DECL_CONTEXT (tmp_field) = dtype;
1813 DECL_ARTIFICIAL (tmp_field) = 1;
1817 n = TREE_VEC_LENGTH (get_dispatch_vector (type));
1818 if (TARGET_VTABLE_USES_DESCRIPTORS)
1819 n *= TARGET_VTABLE_USES_DESCRIPTORS;
1821 PUSH_FIELD (dtype, dummy, "methods",
1822 build_prim_array_type (nativecode_ptr_type_node, n));
1823 layout_type (dtype);
1826 dtype = dtable_type;
1828 return build_decl (VAR_DECL,
1829 java_mangle_vtable (&temporary_obstack, type), dtype);
1832 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
1833 fields inherited from SUPER_CLASS. */
1836 push_super_field (this_class, super_class)
1837 tree this_class, super_class;
1840 /* Don't insert the field if we're just re-laying the class out. */
1841 if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
1843 base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
1844 DECL_IGNORED_P (base_decl) = 1;
1845 TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
1846 TYPE_FIELDS (this_class) = base_decl;
1847 DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
1848 DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
1851 /* Handle the different manners we may have to lay out a super class. */
1854 maybe_layout_super_class (super_class, this_class)
1858 if (TREE_CODE (super_class) == RECORD_TYPE)
1860 if (!CLASS_LOADED_P (super_class) && CLASS_FROM_SOURCE_P (super_class))
1861 safe_layout_class (super_class);
1862 if (!CLASS_LOADED_P (super_class))
1863 load_class (super_class, 1);
1865 /* We might have to layout the class before its dependency on
1866 the super class gets resolved by java_complete_class */
1867 else if (TREE_CODE (super_class) == POINTER_TYPE)
1869 if (TREE_TYPE (super_class) != NULL_TREE)
1870 super_class = TREE_TYPE (super_class);
1873 super_class = do_resolve_class (NULL_TREE, /* FIXME? */
1874 super_class, NULL_TREE, this_class);
1876 return NULL_TREE; /* FIXME, NULL_TREE not checked by caller. */
1877 super_class = TREE_TYPE (super_class);
1880 if (!TYPE_SIZE (super_class))
1881 safe_layout_class (super_class);
1887 layout_class (this_class)
1890 tree super_class = CLASSTYPE_SUPER (this_class);
1893 class_list = tree_cons (this_class, NULL_TREE, class_list);
1894 if (CLASS_BEING_LAIDOUT (this_class))
1900 sprintf (buffer, " with `%s'",
1901 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
1902 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1904 for (current = TREE_CHAIN (class_list); current;
1905 current = TREE_CHAIN (current))
1907 tree decl = TYPE_NAME (TREE_PURPOSE (current));
1908 sprintf (buffer, "\n which inherits from `%s' (%s:%d)",
1909 IDENTIFIER_POINTER (DECL_NAME (decl)),
1910 DECL_SOURCE_FILE (decl),
1911 DECL_SOURCE_LINE (decl));
1912 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1914 obstack_1grow (&temporary_obstack, '\0');
1915 report = obstack_finish (&temporary_obstack);
1916 cyclic_inheritance_report = ggc_strdup (report);
1917 obstack_free (&temporary_obstack, report);
1918 TYPE_SIZE (this_class) = error_mark_node;
1921 CLASS_BEING_LAIDOUT (this_class) = 1;
1923 if (super_class && !CLASS_BEING_LAIDOUT (super_class))
1925 tree maybe_super_class
1926 = maybe_layout_super_class (super_class, this_class);
1927 if (maybe_super_class == NULL
1928 || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
1930 TYPE_SIZE (this_class) = error_mark_node;
1931 CLASS_BEING_LAIDOUT (this_class) = 0;
1932 class_list = TREE_CHAIN (class_list);
1935 if (TYPE_SIZE (this_class) == NULL_TREE)
1936 push_super_field (this_class, maybe_super_class);
1939 for (field = TYPE_FIELDS (this_class);
1940 field != NULL_TREE; field = TREE_CHAIN (field))
1942 if (FIELD_STATIC (field))
1944 /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
1945 SET_DECL_ASSEMBLER_NAME (field,
1947 (&temporary_obstack, field));
1951 layout_type (this_class);
1953 /* Also recursively load/layout any superinterfaces, but only if class was
1954 loaded from bytecode. The source parser will take care of this itself. */
1955 if (!CLASS_FROM_SOURCE_P (this_class))
1957 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
1961 int n = TREE_VEC_LENGTH (basetype_vec) - 1;
1963 for (i = n; i > 0; i--)
1965 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
1966 tree super_interface = BINFO_TYPE (vec_elt);
1968 tree maybe_super_interface
1969 = maybe_layout_super_class (super_interface, NULL_TREE);
1970 if (maybe_super_interface == NULL
1971 || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
1973 TYPE_SIZE (this_class) = error_mark_node;
1974 CLASS_BEING_LAIDOUT (this_class) = 0;
1975 class_list = TREE_CHAIN (class_list);
1982 /* Convert the size back to an SI integer value */
1983 TYPE_SIZE_UNIT (this_class) =
1984 fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
1986 CLASS_BEING_LAIDOUT (this_class) = 0;
1987 class_list = TREE_CHAIN (class_list);
1991 layout_class_methods (this_class)
1994 tree method_decl, dtable_count;
1997 if (TYPE_NVIRTUALS (this_class))
2000 super_class = CLASSTYPE_SUPER (this_class);
2004 super_class = maybe_layout_super_class (super_class, this_class);
2005 if (!TYPE_NVIRTUALS (super_class))
2006 layout_class_methods (super_class);
2007 dtable_count = TYPE_NVIRTUALS (super_class);
2010 dtable_count = integer_zero_node;
2012 TYPE_METHODS (this_class) = nreverse (TYPE_METHODS (this_class));
2014 for (method_decl = TYPE_METHODS (this_class);
2015 method_decl; method_decl = TREE_CHAIN (method_decl))
2016 dtable_count = layout_class_method (this_class, super_class,
2017 method_decl, dtable_count);
2019 TYPE_NVIRTUALS (this_class) = dtable_count;
2022 /* Return 0 if NAME is equal to STR, -1 if STR is "less" than NAME,
2023 and 1 if STR is "greater" than NAME. */
2025 /* Lay METHOD_DECL out, returning a possibly new value of
2026 DTABLE_COUNT. Also mangle the method's name. */
2029 layout_class_method (this_class, super_class, method_decl, dtable_count)
2030 tree this_class, super_class, method_decl, dtable_count;
2032 tree method_name = DECL_NAME (method_decl);
2034 TREE_PUBLIC (method_decl) = 1;
2036 /* This is a good occasion to mangle the method's name */
2037 SET_DECL_ASSEMBLER_NAME (method_decl,
2038 java_mangle_decl (&temporary_obstack,
2040 /* We don't generate a RTL for the method if it's abstract, or if
2041 it's an interface method that isn't clinit. */
2042 if (! METHOD_ABSTRACT (method_decl)
2043 || (CLASS_INTERFACE (TYPE_NAME (this_class))
2044 && (DECL_CLINIT_P (method_decl))))
2045 make_decl_rtl (method_decl, NULL);
2047 if (ID_INIT_P (method_name))
2049 const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
2051 for (ptr = p; *ptr; )
2056 DECL_CONSTRUCTOR_P (method_decl) = 1;
2057 build_java_argument_signature (TREE_TYPE (method_decl));
2059 else if (! METHOD_STATIC (method_decl) && !DECL_ARTIFICIAL (method_decl))
2062 build_java_argument_signature (TREE_TYPE (method_decl));
2063 tree super_method = lookup_argument_method (super_class, method_name,
2065 if (super_method != NULL_TREE && ! METHOD_PRIVATE (super_method))
2067 DECL_VINDEX (method_decl) = DECL_VINDEX (super_method);
2068 if (DECL_VINDEX (method_decl) == NULL_TREE
2069 && !CLASS_FROM_SOURCE_P (this_class))
2070 error_with_decl (method_decl,
2071 "non-static method '%s' overrides static method");
2073 else if (! METHOD_FINAL (method_decl)
2074 && ! METHOD_PRIVATE (method_decl)
2075 && ! CLASS_FINAL (TYPE_NAME (this_class))
2078 DECL_VINDEX (method_decl) = dtable_count;
2079 dtable_count = fold (build (PLUS_EXPR, integer_type_node,
2080 dtable_count, integer_one_node));
2084 return dtable_count;
2090 /* END does not need to be registered with the garbage collector
2091 because it always points into the list given by REGISTERED_CLASS,
2092 and that variable is registered with the collector. */
2094 tree node = TREE_OPERAND (build_class_ref (current_class), 0);
2095 tree current = copy_node (node);
2097 XEXP (DECL_RTL (current), 0) = copy_rtx (XEXP (DECL_RTL(node), 0));
2098 if (!registered_class)
2099 registered_class = current;
2101 TREE_CHAIN (end) = current;
2106 /* Emit something to register classes at start-up time.
2108 The preferred mechanism is through the .jcr section, which contain
2109 a list of pointers to classes which get registered during
2110 constructor invoction time. The fallback mechanism is to generate
2111 a `constructor' function which calls _Jv_RegisterClass for each
2112 class in this file. */
2115 emit_register_classes ()
2117 /* ??? This isn't quite the correct test. We also have to know
2118 that the target is using gcc's crtbegin/crtend objects rather
2119 than the ones that come with the operating system. */
2120 if (SUPPORTS_WEAK && targetm.have_named_sections)
2122 #ifdef JCR_SECTION_NAME
2124 named_section_flags (JCR_SECTION_NAME, SECTION_WRITE);
2125 assemble_align (POINTER_SIZE);
2126 for (t = registered_class; t; t = TREE_CHAIN (t))
2127 assemble_integer (XEXP (DECL_RTL (t), 0),
2128 POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
2135 extern tree get_file_function_name PARAMS ((int));
2136 tree init_name = get_file_function_name ('I');
2137 tree init_type = build_function_type (void_type_node, end_params_node);
2141 init_decl = build_decl (FUNCTION_DECL, init_name, init_type);
2142 SET_DECL_ASSEMBLER_NAME (init_decl, init_name);
2143 TREE_STATIC (init_decl) = 1;
2144 current_function_decl = init_decl;
2145 DECL_RESULT (init_decl) = build_decl (RESULT_DECL, NULL_TREE,
2148 /* It can be a static function as long as collect2 does not have
2149 to scan the object file to find its ctor/dtor routine. */
2150 TREE_PUBLIC (init_decl) = ! targetm.have_ctors_dtors;
2152 /* Suppress spurious warnings. */
2153 TREE_USED (init_decl) = 1;
2156 make_decl_rtl (init_decl, NULL);
2157 init_function_start (init_decl, input_filename, 0);
2158 expand_function_start (init_decl, 0);
2160 /* Do not allow the function to be deferred. */
2161 current_function_cannot_inline
2162 = "static constructors and destructors cannot be inlined";
2164 for ( t = registered_class; t; t = TREE_CHAIN (t))
2165 emit_library_call (registerClass_libfunc, 0, VOIDmode, 1,
2166 XEXP (DECL_RTL (t), 0), Pmode);
2168 expand_function_end (input_filename, 0, 0);
2170 rest_of_compilation (init_decl);
2171 current_function_decl = NULL_TREE;
2173 if (targetm.have_ctors_dtors)
2174 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (init_decl), 0),
2175 DEFAULT_INIT_PRIORITY);
2179 /* Make a method_symbol_type (_Jv_MethodSymbol) node for METHOD. */
2182 build_method_symbols_entry (tree method)
2184 tree clname, name, signature, method_symbol;
2186 clname = build_utf8_ref (DECL_NAME (TYPE_NAME (DECL_CONTEXT (method))));
2187 name = build_utf8_ref (DECL_NAME (method));
2188 signature = build_java_signature (TREE_TYPE (method));
2189 signature = build_utf8_ref (unmangle_classname
2190 (IDENTIFIER_POINTER (signature),
2191 IDENTIFIER_LENGTH (signature)));
2193 START_RECORD_CONSTRUCTOR (method_symbol, method_symbol_type);
2194 PUSH_FIELD_VALUE (method_symbol, "clname", clname);
2195 PUSH_FIELD_VALUE (method_symbol, "name", name);
2196 PUSH_FIELD_VALUE (method_symbol, "signature", signature);
2197 FINISH_RECORD_CONSTRUCTOR (method_symbol);
2198 TREE_CONSTANT (method_symbol) = 1;
2200 return method_symbol;
2203 /* Emit the offset symbols table for indirect virtual dispatch. */
2206 emit_offset_symbol_table ()
2208 tree method_list, method, table, list, null_symbol;
2209 tree otable_bound, otable_array_type;
2212 /* Only emit an offset table if this translation unit actually made virtual
2214 if (otable_methods == NULL_TREE)
2217 /* Build a list of _Jv_MethodSymbols for each entry in otable_methods. */
2219 method_list = otable_methods;
2221 while (method_list != NULL_TREE)
2223 method = TREE_VALUE (method_list);
2224 list = tree_cons (NULL_TREE, build_method_symbols_entry (method), list);
2225 method_list = TREE_CHAIN (method_list);
2229 /* Terminate the list with a "null" entry. */
2230 START_RECORD_CONSTRUCTOR (null_symbol, method_symbol_type);
2231 PUSH_FIELD_VALUE (null_symbol, "clname", null_pointer_node);
2232 PUSH_FIELD_VALUE (null_symbol, "name", null_pointer_node);
2233 PUSH_FIELD_VALUE (null_symbol, "signature", null_pointer_node);
2234 FINISH_RECORD_CONSTRUCTOR (null_symbol);
2235 TREE_CONSTANT (null_symbol) = 1;
2236 list = tree_cons (NULL_TREE, null_symbol, list);
2238 /* Put the list in the right order and make it a constructor. */
2239 list = nreverse (list);
2240 table = build (CONSTRUCTOR, method_symbols_array_type, NULL_TREE, list);
2242 /* Make it the initial value for otable_syms and emit the decl. */
2243 DECL_INITIAL (otable_syms_decl) = table;
2244 DECL_ARTIFICIAL (otable_syms_decl) = 1;
2245 DECL_IGNORED_P (otable_syms_decl) = 1;
2246 rest_of_decl_compilation (otable_syms_decl, NULL, 1, 0);
2248 /* Now that its size is known, redefine otable as an uninitialized static
2249 array of INDEX + 1 integers. The extra entry is used by the runtime
2250 to track whether the otable has been initialized. */
2251 otable_bound = build_index_type (build_int_2 (index, 0));
2252 otable_array_type = build_array_type (integer_type_node, otable_bound);
2253 otable_decl = build_decl (VAR_DECL, get_identifier ("otable"),
2255 TREE_STATIC (otable_decl) = 1;
2256 TREE_READONLY (otable_decl) = 1;
2257 rest_of_decl_compilation (otable_decl, NULL, 1, 0);
2261 init_class_processing ()
2263 registerClass_libfunc = gen_rtx_SYMBOL_REF (Pmode, "_Jv_RegisterClass");
2264 registerResource_libfunc =
2265 gen_rtx_SYMBOL_REF (Pmode, "_Jv_RegisterResource");
2266 fields_ident = get_identifier ("fields");
2267 info_ident = get_identifier ("info");
2268 gcc_obstack_init (&temporary_obstack);
2271 static hashval_t java_treetreehash_hash PARAMS ((const void *));
2272 static int java_treetreehash_compare PARAMS ((const void *, const void *));
2274 /* A hash table mapping trees to trees. Used generally. */
2276 #define JAVA_TREEHASHHASH_H(t) (htab_hash_pointer (t))
2279 java_treetreehash_hash (k_p)
2282 struct treetreehash_entry *k = (struct treetreehash_entry *) k_p;
2283 return JAVA_TREEHASHHASH_H (k->key);
2287 java_treetreehash_compare (k1_p, k2_p)
2291 struct treetreehash_entry * k1 = (struct treetreehash_entry *) k1_p;
2292 tree k2 = (tree) k2_p;
2293 return (k1->key == k2);
2297 java_treetreehash_find (ht, t)
2301 struct treetreehash_entry *e;
2302 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2303 e = (struct treetreehash_entry *) htab_find_with_hash (ht, t, hv);
2311 java_treetreehash_new (ht, t)
2316 struct treetreehash_entry *tthe;
2317 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2319 e = htab_find_slot_with_hash (ht, t, hv, INSERT);
2322 tthe = (*ht->alloc_f) (1, sizeof (*tthe));
2327 tthe = (struct treetreehash_entry *) *e;
2328 return &tthe->value;
2332 java_treetreehash_create (size, gc)
2337 return htab_create_ggc (size, java_treetreehash_hash,
2338 java_treetreehash_compare, NULL);
2340 return htab_create_alloc (size, java_treetreehash_hash,
2341 java_treetreehash_compare, free, xcalloc, free);
2344 #include "gt-java-class.h"