PATCH: Better MAIN_NAME_P for all C-ish front-ends

Greg McGary greg@mcgary.org
Wed Aug 23 23:11:00 GMT 2000


This avoids multiple per-decl and per-function calls to strcmp to detect "main".

What does this have to do with BPs?  A later patch will add another
per-decl test for "main", and I just couldn't bear to add another
ugly strcmp, so I wanted to first clean this up.

native sparc-sun-solaris2.6: no build failures
			     no bootstrap-compare diffs
			     no testsuite regressions

native i686-pc-linux bootstrap & check is still in progress.
I assume it'll be OK, and I won't proceed unless it's clean.

OK to commit?

2000-08-23  Greg McGary  <greg@mcgary.org>

	* tree.h (enum tree_index): New member `TI_MAIN_IDENTIFIER'.
	(MAIN_NAME_P, main_identifier_node): New macros.
	* c-common.c (c_common_nodes_and_builtins): Init main_identifier_node.
	* c-decl.c (start_decl, grokdeclarator, start_function,
	store_parm_decls, finish_function): Use MAIN_NAME_P.
	* config/avr/avr.c (function_prologue, function_epilogue): Likewise.
	* config/i386/cygwin.h (SUBTARGET_PROLOGUE): Likewise.
	* config/i386/win32.h (SUBTARGET_PROLOGUE): Likewise.
	* config/pdp11/pdp11.c (output_function_prologue): Likewise.

cp:
	* cp-tree.h (MAIN_NAME_P): Remove macro.

Index: gcc/c-common.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/c-common.c,v
retrieving revision 1.140
diff -u -p -r1.140 c-common.c
--- c-common.c	2000/08/23 17:32:49	1.140
+++ c-common.c	2000/08/24 00:15:15
@@ -4181,6 +4181,8 @@ c_common_nodes_and_builtins (cplus_mode,
 		    BUILT_IN_NORMAL, NULL_PTR);
 #endif
 
+  main_identifier_node = get_identifier ("main");
+
   /* ??? Perhaps there's a better place to do this.  But it is related
      to __builtin_va_arg, so it isn't that off-the-wall.  */
   lang_type_promotes_to = simple_type_promotes_to;
Index: gcc/c-decl.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/c-decl.c,v
retrieving revision 1.144
diff -u -p -r1.144 c-decl.c
--- c-decl.c	2000/08/21 14:54:28	1.144
+++ c-decl.c	2000/08/24 00:15:17
@@ -3430,7 +3430,7 @@ start_decl (declarator, declspecs, initi
   register tree tem;
 
   if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
-      && !strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "main"))
+      && MAIN_NAME_P (DECL_NAME (decl)))
     warning_with_decl (decl, "`%s' is usually a function");
 
   if (initialized)
@@ -4827,7 +4827,7 @@ grokdeclarator (declarator, declspecs, d
 	/* Record presence of `inline', if it is reasonable.  */
 	if (inlinep)
 	  {
-	    if (! strcmp (IDENTIFIER_POINTER (declarator), "main"))
+	    if (MAIN_NAME_P (declarator))
 	      warning ("cannot inline function `main'");
 	    else
 	      /* Assume that otherwise the function can be inlined.  */
@@ -5892,7 +5892,7 @@ start_function (declspecs, declarator, p
   else if (warn_missing_prototypes
 	   && TREE_PUBLIC (decl1)
 	   && !(old_decl != 0 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) != 0)
-	   && strcmp ("main", IDENTIFIER_POINTER (DECL_NAME (decl1))))
+	   && ! MAIN_NAME_P (DECL_NAME (decl1)))
     warning_with_decl (decl1, "no previous prototype for `%s'");
   /* Optionally warn of any def with no previous prototype
      if the function has already been used.  */
@@ -5905,7 +5905,7 @@ start_function (declspecs, declarator, p
   else if (warn_missing_declarations
 	   && TREE_PUBLIC (decl1)
 	   && old_decl == 0
-	   && strcmp ("main", IDENTIFIER_POINTER (DECL_NAME (decl1))))
+	   && ! MAIN_NAME_P (DECL_NAME (decl1)))
     warning_with_decl (decl1, "no previous declaration for `%s'");
   /* Optionally warn of any def with no previous declaration
      if the function has already been used.  */
@@ -5934,8 +5934,7 @@ start_function (declspecs, declarator, p
     TREE_PUBLIC (decl1) = 0;
 
   /* Warn for unlikely, improbable, or stupid declarations of `main'.  */
-  if (warn_main > 0
-      && strcmp ("main", IDENTIFIER_POINTER (DECL_NAME (decl1))) == 0)
+  if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
     {
       tree args;
       int argct = 0;
@@ -6461,7 +6460,7 @@ store_parm_decls ()
   /* If this function is `main', emit a call to `__main'
      to run global initializers, etc.  */
   if (DECL_NAME (fndecl)
-      && strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "main") == 0
+      && MAIN_NAME_P (DECL_NAME (fndecl))
       && DECL_CONTEXT (fndecl) == NULL_TREE)
     expand_main_function ();
 }
@@ -6641,8 +6640,7 @@ finish_function (nested)
       setjmp_protect_args ();
     }
 
-  if (! strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "main")
-      && flag_hosted)
+  if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
     {
       if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
 	  != integer_type_node)
Index: gcc/tree.h
===================================================================
RCS file: /cvs/gcc/egcs/gcc/tree.h,v
retrieving revision 1.189
diff -u -p -r1.189 tree.h
--- tree.h	2000/08/22 08:04:21	1.189
+++ tree.h	2000/08/24 00:15:19
@@ -1745,6 +1748,8 @@ enum tree_index
   TI_V4HI_TYPE,
   TI_V2SI_TYPE,
 
+  TI_MAIN_IDENTIFIER,
+
   TI_MAX
 };
 
@@ -1790,6 +1795,9 @@ extern tree global_trees[TI_MAX];
 #define const_ptr_type_node		global_trees[TI_CONST_PTR_TYPE]
 #define ptrdiff_type_node		global_trees[TI_PTRDIFF_TYPE]
 #define va_list_type_node		global_trees[TI_VA_LIST_TYPE]
+
+#define main_identifier_node		global_trees[TI_MAIN_IDENTIFIER]
+#define MAIN_NAME_P(NODE) (IDENTIFIER_NODE_CHECK (NODE) == main_identifier_node)
 
 #define V4SF_type_node			global_trees[TI_V4SF_TYPE]
 #define V4SI_type_node			global_trees[TI_V4SI_TYPE]
Index: gcc/config/avr/avr.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/config/avr/avr.c,v
retrieving revision 1.16
diff -u -p -r1.16 avr.c
--- avr.c	2000/07/28 02:17:25	1.16
+++ avr.c	2000/08/24 00:15:19
@@ -469,7 +469,7 @@ function_prologue (FILE *file, int size)
   interrupt_func_p = interrupt_function_p (current_function_decl);
   signal_func_p = signal_function_p (current_function_decl);
   leaf_func_p = leaf_function_p ();
-  main_p = ! strcmp ("main", current_function_name);
+  main_p = MAIN_NAME_P (DECL_NAME (current_function_decl));
   live_seq = sequent_regs_live ();
   minimize = (TARGET_CALL_PROLOGUES
 	      && !interrupt_func_p && !signal_func_p && live_seq);
@@ -604,7 +604,7 @@ function_epilogue (FILE *file, int size)
   interrupt_func_p = interrupt_function_p (current_function_decl);
   signal_func_p = signal_function_p (current_function_decl);
   leaf_func_p = leaf_function_p ();
-  main_p = ! strcmp ("main", current_function_name);
+  main_p = MAIN_NAME_P (DECL_NAME (current_function_decl));
   function_size = (INSN_ADDRESSES (INSN_UID (get_last_insn ()))
 		   - INSN_ADDRESSES (INSN_UID (get_insns ())));
   live_seq = sequent_regs_live ();
Index: gcc/config/i386/cygwin.h
===================================================================
RCS file: /cvs/gcc/egcs/gcc/config/i386/cygwin.h,v
retrieving revision 1.23
diff -u -p -r1.23 cygwin.h
--- cygwin.h	2000/08/03 13:30:18	1.23
+++ cygwin.h	2000/08/24 00:15:20
@@ -489,8 +489,7 @@ do {									\
 
 #define SUBTARGET_PROLOGUE						\
   if (profile_flag 							\
-      && strcmp (IDENTIFIER_POINTER (DECL_NAME (current_function_decl)),\
-		 "main") == 0)						\
+      && MAIN_NAME_P (DECL_NAME (current_function_decl)))		\
      {									\
       emit_call_insn (gen_rtx (CALL, VOIDmode, 				\
         gen_rtx_MEM (FUNCTION_MODE,					\
Index: gcc/config/i386/win32.h
===================================================================
RCS file: /cvs/gcc/egcs/gcc/config/i386/win32.h,v
retrieving revision 1.11
diff -u -p -r1.11 win32.h
--- win32.h	2000/06/27 02:26:20	1.11
+++ win32.h	2000/08/24 00:15:20
@@ -272,8 +272,7 @@ do {								\
 
 #define SUBTARGET_PROLOGUE						\
   if (profile_flag 							\
-      && strcmp (IDENTIFIER_POINTER (DECL_NAME (current_function_decl)),\
-		 "main") == 0)						\
+      && MAIN_NAME_P (DECL_NAME (current_function_decl))		\
      {									\
       rtx xops[1];							\
       xops[0] = gen_rtx_MEM (FUNCTION_MODE,				\
Index: gcc/config/pdp11/pdp11.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/config/pdp11/pdp11.c,v
retrieving revision 1.13
diff -u -p -r1.13 pdp11.c
--- pdp11.c	2000/05/01 17:31:30	1.13
+++ pdp11.c	2000/08/24 00:15:20
@@ -110,8 +110,7 @@ output_function_prologue(stream, size)
 
     /* if we are outputting code for main, 
        the switch FPU to right mode if TARGET_FPU */
-    if ( (strcmp ("main", current_function_name) == 0)
-	 && TARGET_FPU)
+    if (MAIN_NAME_P (DECL_NAME (current_function_decl)) && TARGET_FPU)
     {
 	fprintf(stream, "\t;/* switch cpu to double float, single integer */\n");
 	fprintf(stream, "\tsetd\n");
Index: gcc/cp/cp-tree.h
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/cp-tree.h,v
retrieving revision 1.516
diff -u -p -r1.516 cp-tree.h
--- cp-tree.h	2000/08/23 17:12:23	1.516
+++ cp-tree.h	2000/08/24 00:15:22
@@ -376,12 +376,7 @@ typedef struct ptrmem_cst
     && DECL_NAME (NODE) != NULL_TREE			\
     && MAIN_NAME_P (DECL_NAME (NODE)))
 
-/* Returns non-zero iff ID_NODE is an IDENTIFIER_NODE whose name is
-   `main'.  */
-#define MAIN_NAME_P(ID_NODE) \
-   (strcmp (IDENTIFIER_POINTER (ID_NODE), "main") == 0)
 
-
 struct tree_binding
 {
   struct tree_common common;
@@ -3505,11 +3500,6 @@ extern varray_type local_classes;
   sprintf (wbuf, VBASE_NAME_FORMAT, TYPE_ASSEMBLER_NAME_STRING (TYPE));	\
   (BUF) = wbuf;								\
 } while (0)
-
-/* Returns non-zero iff ID_NODE is an IDENTIFIER_NODE whose name is
-   `main'.  */
-#define MAIN_NAME_P(ID_NODE) \
-   (strcmp (IDENTIFIER_POINTER (ID_NODE), "main") == 0)
 
 /* Returns non-zero iff NODE is a declaration for the global function
    `main'.  */


More information about the Gcc-patches mailing list