This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC 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]

IMA: rearrange handling of file-scope constructors


The purpose of this patch is to move the creation of the
static-constructor helper functions (for systems without .ctors
sections) and the mudflap initializer routines, from a point where
they're done once per input file, to a point where they're done once
per object file.  This is slightly less necessary than it used to be -
before we had cgraph_build_static_cdtor, once-per-input-file would get
us multiply defined symbol errors; now it's just an inefficiency.
However, an inefficiency it remains, and I think it's good to have the
static-constructor-helper handling all in one file, too.

Contrariwise, we move the call to cgraph_finalize_compilation_unit
from a once-per-object-file location to a once-per-input-file
location, following the recommendation at the top of cgraphunit.c.

The Objective-C front end changes are the minimum necessary to avoid
regressions in 'make check-objc'; objc-act.c badly needs a going over
with a chain saw.

Bootstrapped amd64-linux, for a change, and I also built a cross
compiler to vax-bsd and verified that the static-constructor-helper
logic works.

Andrew: this should be the last patch necessary to make your patch
work.  I'll check this in tomorrow, when I'm not falling asleep, and
then I'll test your patch.

zw

        * c-decl.c (static_ctors, static_dtors): Make static.
        (pop_file_scope): Call c_common_write_pch and
        cgraph_finalize_compilation_unit here.
        (build_cdtor): Moved here from c-objc-common.c; simplify.
        (c_write_global_declarations_1): Clarify comment.
        (c_write_global_declarations): Close the external scope before
        doing anything else.  Call build_cdtor, cgraph_optimize, and
        mudflap_finish_file here.
        * c-lang.c (finish_file): Don't call c_objc_common_finish_file.
        Clarify comment.
        * c-objc-common.c: No need to include cgraph.h.
        (build_cdtor): Moved to c-decl.c.
        (c_objc_common_finish_file): Delete.
        * c-tree.h: Update to match.
        * objc/objc-act.c (finish_file): Don't call c_objc_common_finish_file.
        (generate_static_references): Set TREE_USED before calling finish_decl.
        Eliminate unnecessary dummy declaration.  Call rest_of_decl_compilation
        on the static_instances_decl.

===================================================================
Index: c-decl.c
--- c-decl.c	6 Jul 2004 02:20:05 -0000	1.528
+++ c-decl.c	8 Jul 2004 06:57:39 -0000
@@ -377,7 +377,8 @@ static bool next_is_function_body;
 
 /* Functions called automatically at the beginning and end of execution.  */
 
-tree static_ctors, static_dtors;
+static GTY(()) tree static_ctors;
+static GTY(()) tree static_dtors;
 
 /* Forward declarations.  */
 static tree lookup_name_in_scope (tree, struct c_scope *);
@@ -851,17 +852,19 @@ pop_file_scope (void)
      still works without it.  */
   finish_fname_decls ();
 
-  /* Kludge: don't actually pop the file scope if generating a
-     precompiled header, so that macros and local symbols are still
-     visible to the PCH generator.  */
+  /* This is the point to write out a PCH if we're doing that.
+     In that case we do not want to do anything else.  */
   if (pch_file)
-    return;
+    {
+      c_common_write_pch ();
+      return;
+    }
 
-  /* And pop off the file scope.  */
+  /* Pop off the file scope and close this translation unit.  */
   pop_scope ();
   file_scope = 0;
-
   cpp_undef_all (parse_in);
+  cgraph_finalize_compilation_unit ();
 }
 
 /* Insert BLOCK at the end of the list of subblocks of the current
@@ -6569,7 +6572,26 @@ make_pointer_declarator (tree type_quals
   return build1 (INDIRECT_REF, quals, itarget);
 }
 
-/* Perform final processing on file-scope data.  */
+/* Synthesize a function which calls all the global ctors or global
+   dtors in this file.  This is only used for targets which do not
+   support .ctors/.dtors sections.  FIXME: Migrate into cgraph.  */
+static void
+build_cdtor (int method_type, tree cdtors)
+{
+  tree body = 0;
+
+  if (!cdtors)
+    return;
+
+  for (; cdtors; cdtors = TREE_CHAIN (cdtors))
+    append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
+			      &body);
+
+  cgraph_build_static_cdtor (method_type, body);
+}
+
+/* Perform final processing on one file scope's declarations (or the
+   external scope's declarations), GLOBALS.  */
 static void
 c_write_global_declarations_1 (tree globals)
 {
@@ -6591,20 +6613,38 @@ c_write_global_declarations_1 (tree glob
 void
 c_write_global_declarations (void)
 {
-  tree t;
+  tree ext_block, t;
 
   /* We don't want to do this if generating a PCH.  */
   if (pch_file)
     return;
 
-  /* Process all file scopes in this compilation.  */
+  /* Close the external scope.  */
+  ext_block = pop_scope ();
+  external_scope = 0;
+  if (current_scope)
+    abort ();
+
+  /* Process all file scopes in this compilation, and the external_scope,
+     through wrapup_global_declarations and check_global_declarations.  */
   for (t = all_translation_units; t; t = TREE_CHAIN (t))
     c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
+  c_write_global_declarations_1 (BLOCK_VARS (ext_block));
 
-  /* Now do the same for the externals scope.  */
-  t = pop_scope ();
-  if (t)
-    c_write_global_declarations_1 (BLOCK_VARS (t));
+  /* Generate functions to call static constructors and destructors
+     for targets that do not support .ctors/.dtors sections.  These
+     functions have magic names which are detected by collect2.  */
+  build_cdtor ('I', static_ctors); static_ctors = 0;
+  build_cdtor ('D', static_dtors); static_dtors = 0;
+
+  /* We're done parsing; proceed to optimize and emit assembly.
+     FIXME: shouldn't be the front end's responsibility to call this.  */
+  cgraph_optimize ();
+
+  /* Presently this has to happen after cgraph_optimize.
+     FIXME: shouldn't be the front end's responsibility to call this.  */
+  if (flag_mudflap)
+    mudflap_finish_file ();
 }
 
 #include "gt-c-decl.h"
===================================================================
Index: c-lang.c
--- c-lang.c	5 Jul 2004 17:28:33 -0000	1.129
+++ c-lang.c	8 Jul 2004 06:57:39 -0000
@@ -195,10 +195,11 @@ const char *const tree_code_name[] = {
 };
 #undef DEFTREECODE
 
+/* Final processing of file-scope data.  The Objective-C version of
+   this function still does something.  */
 void
 finish_file (void)
 {
-  c_objc_common_finish_file ();
 }
 
 int
===================================================================
Index: c-objc-common.c
--- c-objc-common.c	1 Jul 2004 08:52:25 -0000	1.50
+++ c-objc-common.c	8 Jul 2004 06:57:39 -0000
@@ -38,7 +38,6 @@ Software Foundation, 59 Temple Place - S
 #include "langhooks.h"
 #include "tree-mudflap.h"
 #include "target.h"
-#include "cgraph.h"
 
 static bool c_tree_printer (pretty_printer *, text_info *);
 
@@ -183,50 +182,6 @@ c_objc_common_init (void)
   return true;
 }
 
-/* Synthesize a function which calls all the global ctors or global dtors
-   in this file.  */
-
-static void
-build_cdtor (int method_type, tree cdtors)
-{
-  tree body;
-
-  body = push_stmt_list ();
-
-  for (; cdtors; cdtors = TREE_CHAIN (cdtors))
-    add_stmt (build_function_call (TREE_VALUE (cdtors), NULL_TREE));
-
-  body = pop_stmt_list (body);
-
-  cgraph_build_static_cdtor (method_type, body);
-}
-
-/* Called at end of parsing, but before end-of-file processing.  */
-
-void
-c_objc_common_finish_file (void)
-{
-  if (pch_file)
-    c_common_write_pch ();
-
-  if (static_ctors)
-    {
-      build_cdtor ('I', static_ctors);
-      static_ctors = 0;
-    }
-  if (static_dtors)
-    {
-      build_cdtor ('D', static_dtors);
-      static_dtors = 0;
-    }
-
-  cgraph_finalize_compilation_unit ();
-  cgraph_optimize ();
-
-  if (flag_mudflap)
-    mudflap_finish_file ();
-}
-
 /* Called during diagnostic message formatting process to print a
    source-level entity onto BUFFER.  The meaning of the format specifiers
    is as follows:
===================================================================
Index: c-tree.h
--- c-tree.h	5 Jul 2004 17:28:33 -0000	1.161
+++ c-tree.h	8 Jul 2004 06:57:39 -0000
@@ -197,7 +197,6 @@ extern int c_cannot_inline_tree_fn (tree
 extern bool c_objc_common_init (void);
 extern bool c_missing_noreturn_ok_p (tree);
 extern tree c_objc_common_truthvalue_conversion (tree expr);
-extern void c_objc_common_finish_file (void);
 extern int defer_fn (tree);
 extern bool c_warn_unused_global_decl (tree);
 extern void c_initialize_diagnostics (diagnostic_context *);
@@ -298,9 +297,6 @@ extern void *get_current_scope (void);
 extern void objc_mark_locals_volatile (void *);
 extern void c_write_global_declarations (void);
 
-extern GTY(()) tree static_ctors;
-extern GTY(()) tree static_dtors;
-
 /* In order for the format checking to accept the C frontend
    diagnostic framework extensions, you must include this file before
    toplev.h, not after.  */
===================================================================
Index: objc/objc-act.c
--- objc/objc-act.c	5 Jul 2004 09:35:43 -0000	1.229
+++ objc/objc-act.c	8 Jul 2004 06:57:39 -0000
@@ -519,7 +519,6 @@ void
 finish_file (void)
 {
   mark_referenced_methods ();
-  c_objc_common_finish_file ();
 
   /* Finalize Objective-C runtime data.  No need to generate tables
      and code if only checking syntax.  */
@@ -1959,7 +1958,7 @@ generate_static_references (void)
 {
   tree decls = NULL_TREE, ident, decl_spec, expr_decl, expr = NULL_TREE;
   tree class_name, class, decl, initlist;
-  tree cl_chain, in_chain, type;
+  tree cl_chain, in_chain;
   int num_inst, num_class;
   char buf[256];
 
@@ -1982,6 +1981,7 @@ generate_static_references (void)
       decl = start_decl (expr_decl, decl_spec, 1, NULL_TREE);
       DECL_CONTEXT (decl) = 0;
       DECL_ARTIFICIAL (decl) = 1;
+      TREE_USED (decl) = 1;
 
       /* Output {class_name, ...}.  */
       class = TREE_VALUE (cl_chain);
@@ -2002,12 +2002,6 @@ generate_static_references (void)
 
       expr = objc_build_constructor (TREE_TYPE (decl), nreverse (initlist));
       finish_decl (decl, expr, NULL_TREE);
-      TREE_USED (decl) = 1;
-
-      type = build_array_type (build_pointer_type (void_type_node), 0);
-      decl = build_decl (VAR_DECL, ident, type);
-      TREE_USED (decl) = 1;
-      TREE_STATIC (decl) = 1;
       decls
 	= tree_cons (NULL_TREE, build_unary_op (ADDR_EXPR, decl, 1), decls);
     }
@@ -2026,6 +2020,7 @@ generate_static_references (void)
   expr = objc_build_constructor (TREE_TYPE (static_instances_decl),
 			    nreverse (decls));
   finish_decl (static_instances_decl, expr, NULL_TREE);
+  rest_of_decl_compilation (static_instances_decl, 0, 0, 0);
 }
 
 /* Output all strings.  */


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