This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] java/13183: Use correct class context for constant pool


PR 13183 includes a small test case:

class procinfo { }

class autogen {
  static String procs[][] = {{"Catchtable", "CatchEntry"}};
  public static void main(String argv[]) { }
}

When NEW_ARRAY_INIT is expanded it builds a constant pool reference for
the array element's class.  If compiled with -O2, implying
-funit-at-a-time, the test case places this constant pool entry into the
wrong class because expansion is deferred until the end of the translation
unit.  Since procinfo isn't initialized, the constant pool entry is
unresolved at the time it is needed.  Kaboom.

The fix is twofold: set current_class when expanding the method's body,
and have outgoing_cpool track current_class so the former changes when the
latter changes.  This patch removes the outgoing_cpool global
variable entirely, eliminating existing workarounds for cpool in the
bytecode and source parsers.

This fix allows me to almost build rhug with mainline gcj.  (The few
problems I encountered appear to be caused by rhug's build system, not
gcj.)

Bootstrapped on i686-pc-linux-gnu.  No regressions in libjava, mauve,
jacks etc.  OK for 3.4?

Jeff

2003-11-25  Jeff Sturm  <jsturm@one-point.com>

	Fix PR java/13183.
	* constants.c (cpool_for_class): New function.
	(outgoing_cpool): Remove global variable.
	(alloc_name_constant): Use cpool_for_class.
	(build_constants_constructor): Likewise.
	* decl.c (java_expand_body): Set current_class.
	* java-tree.h (outgoing_cpool) Remove declaration.
	(init_outgoing_cpool): Likewise.
	* jcf-parse.c (init_outgoing_cpool): Remove function.
	(parse_class_file): Don't call init_outgoing_cpool.
	* parse.y (java_complete_expand_methods): Don't call
	init_outgoing_cpool.  Don't save outgoing_cpool.
	(java_expand_classes): Don't restore outgoing_cpool.
	(java_finish_classes): Likewise.

Index: constants.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/constants.c,v
retrieving revision 1.31
diff -u -p -r1.31 constants.c
--- constants.c	13 Apr 2003 01:45:34 -0000	1.31
+++ constants.c	25 Nov 2003 12:45:47 -0000
@@ -37,6 +37,7 @@ static int find_class_or_string_constant
 static int find_name_and_type_constant (CPool *, tree, tree);
 static tree get_tag_node (int);
 static tree build_constant_data_ref (void);
+static CPool *cpool_for_class (tree);

 /* Set the INDEX'th constant in CPOOL to have the given TAG and VALUE. */

@@ -315,8 +316,6 @@ write_constant_pool (CPool *cpool, unsig
     abort ();
 }

-CPool *outgoing_cpool;
-
 static GTY(()) tree tag_nodes[13];
 static tree
 get_tag_node (int tag)
@@ -328,6 +327,21 @@ get_tag_node (int tag)
   return tag_nodes[tag];
 }

+/* Given a class, return its constant pool, creating one if necessary.  */
+
+static CPool *
+cpool_for_class (tree class)
+{
+  CPool *cpool = TYPE_CPOOL (class);
+
+  if (cpool == NULL)
+    {
+      cpool = ggc_alloc_cleared (sizeof (struct CPool));
+      TYPE_CPOOL (class) = cpool;
+    }
+  return cpool;
+}
+
 /* Look for a constant pool entry that matches TAG and NAME.
    Creates a new entry if not found.
    TAG is one of CONSTANT_Utf8, CONSTANT_String or CONSTANT_Class.
@@ -337,6 +351,7 @@ get_tag_node (int tag)
 int
 alloc_name_constant (int tag, tree name)
 {
+  CPool *outgoing_cpool = cpool_for_class (current_class);
   return find_tree_constant (outgoing_cpool, tag, name);
 }

@@ -414,6 +429,7 @@ build_ref_from_constant_pool (int index)
 tree
 build_constants_constructor (void)
 {
+  CPool *outgoing_cpool = cpool_for_class (current_class);
   tree tags_value, data_value;
   tree cons;
   tree tags_list = NULL_TREE;
Index: decl.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/decl.c,v
retrieving revision 1.171
diff -u -p -r1.171 decl.c
--- decl.c	18 Nov 2003 03:57:08 -0000	1.171
+++ decl.c	25 Nov 2003 12:45:48 -0000
@@ -1860,6 +1860,7 @@ java_expand_body (tree fndecl)

   current_function_decl = fndecl;
   input_location = DECL_SOURCE_LOCATION (fndecl);
+  current_class = DECL_CONTEXT (fndecl);

   timevar_push (TV_EXPAND);

Index: java-tree.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/java-tree.h,v
retrieving revision 1.189
diff -u -p -r1.189 java-tree.h
--- java-tree.h	18 Nov 2003 03:57:08 -0000	1.189
+++ java-tree.h	25 Nov 2003 12:45:48 -0000
@@ -694,9 +694,6 @@ extern GTY(()) tree java_global_trees[JT

 #define nativecode_ptr_type_node ptr_type_node

-/* They need to be reset before processing each class */
-extern GTY(()) struct CPool *outgoing_cpool;
-
 #define wfl_operator \
   java_global_trees[JTI_WFL_OPERATOR]

@@ -1219,7 +1216,6 @@ extern int enclosing_context_p (tree, tr
 extern void complete_start_java_method (tree);
 extern tree build_result_decl (tree);
 extern void emit_handlers (void);
-extern void init_outgoing_cpool (void);
 extern void make_class_data (tree);
 extern void register_class (void);
 extern int alloc_name_constant (int, tree);
Index: jcf-parse.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/jcf-parse.c,v
retrieving revision 1.153
diff -u -p -r1.153 jcf-parse.c
--- jcf-parse.c	10 Nov 2003 22:13:21 -0000	1.153
+++ jcf-parse.c	25 Nov 2003 12:45:49 -0000
@@ -693,12 +693,6 @@ load_inner_classes (tree cur_class)
     }
 }

-void
-init_outgoing_cpool (void)
-{
-  outgoing_cpool = ggc_alloc_cleared (sizeof (struct CPool));
-}
-
 static void
 parse_class_file (void)
 {
@@ -710,7 +704,6 @@ parse_class_file (void)
   input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
   input_line = 0;
   (*debug_hooks->start_source_file) (input_line, input_filename);
-  init_outgoing_cpool ();

   /* Currently we always have to emit calls to _Jv_InitClass when
      compiling from class files.  */
Index: parse.y
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/parse.y,v
retrieving revision 1.457
diff -u -p -r1.457 parse.y
--- parse.y	18 Nov 2003 03:57:08 -0000	1.457
+++ parse.y	25 Nov 2003 12:45:54 -0000
@@ -7689,9 +7689,6 @@ java_complete_expand_methods (tree class

   current_class = TREE_TYPE (class_decl);

-  /* Initialize a new constant pool */
-  init_outgoing_cpool ();
-
   /* Pre-expand <clinit> to figure whether we really need it or
      not. If we do need it, we pre-expand the static fields so they're
      ready to be used somewhere else. <clinit> will be fully expanded
@@ -7775,9 +7772,6 @@ java_complete_expand_methods (tree class
       if (DECL_CONSTRUCTOR_P (decl)
 	  && verify_constructor_circularity (decl, decl))
 	break;
-
-  /* Save the constant pool. We'll need to restore it later. */
-  TYPE_CPOOL (current_class) = outgoing_cpool;
 }

 /* Attempt to create <clinit>. Pre-expand static fields so they can be
@@ -9157,7 +9151,6 @@ java_expand_classes (void)
 	   current = TREE_CHAIN (current))
 	{
 	  current_class = TREE_TYPE (TREE_VALUE (current));
-	  outgoing_cpool = TYPE_CPOOL (current_class);
 	  if (flag_emit_class_files)
 	    write_classfile (current_class);
 	  if (flag_emit_xref)
@@ -9179,7 +9172,6 @@ java_finish_classes (void)
       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
 	{
 	  current_class = TREE_TYPE (current);
-	  outgoing_cpool = TYPE_CPOOL (current_class);
 	  finish_class ();
 	}
     }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]