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]

Implement pragma redefine_extname


This patch fixes the problem reported in c/4850, for C and C++.
#pragma redefine_extname is a SunPRO extension to rename library entry
points depending on the API version selected. If it is not supported,
the headers fall back to #defines, which may result in strange
renamings, e.g. of methods.

One open issue is whether the pragma should be available only on
Solaris, or on all systems.

Regards,
Martin

P.S. Please reply to me directly, since I'm not subscribed to any of
the gcc lists at the moment.

2001-11-10  Martin v. Löwis  <loewis@informatik.hu-berlin.de>

	* c-pragma.c (handle_pragma_redefine_extname): New function.
	(init_pragma): Register it.
	* c-pragma.h (HANDLE_PRAGMA_REDEFINE_EXTNAME): Define.
	* gcc.c (cpp_options): Add -D__PRAGMA_REDEFINE_EXTNAME.
	* tree.c (add_external_name, find_external_name): New functions.
	(set_decl_assembler_name): Use external name if available.
	* tree.h (TI_REDEFINED_EXTNAMES, redefined_extnames): Define.
	(add_external_name, find_external_name): Declare.
	* cp/mangle.c (mangle_decl_string): Use external name of extern
	"C" function if available.
	* doc/extend.texi: Document pragma redefine_extname.

Index: c-pragma.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/c-pragma.c,v
retrieving revision 1.45
diff -u -r1.45 c-pragma.c
--- c-pragma.c	2001/08/31 19:27:12	1.45
+++ c-pragma.c	2001/11/10 19:31:34
@@ -302,6 +302,26 @@
 }
 #endif
 
+#ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
+static void
+handle_pragma_redefine_extname (dummy)
+     cpp_reader *dummy ATTRIBUTE_UNUSED;
+{
+  tree oldname, newname, x;
+  enum cpp_ttype t;
+
+  if (c_lex (&oldname) != CPP_NAME)
+    BAD ("malformed #pragma redefine_extname, ignored");
+  if (c_lex (&newname) != CPP_NAME)
+	BAD ("malformed #pragma redefine_extname, ignored");
+  t = c_lex (&x);
+  if (t != CPP_EOF)
+    warning ("junk at end of #pragma redefine_extname");
+
+  add_external_name (oldname, newname);  
+}
+#endif
+
 void
 init_pragma ()
 {
@@ -310,6 +330,9 @@
 #endif
 #ifdef HANDLE_PRAGMA_WEAK
   cpp_register_pragma (parse_in, 0, "weak", handle_pragma_weak);
+#endif
+#ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
+  cpp_register_pragma (parse_in, 0, "redefine_extname", handle_pragma_redefine_extname);
 #endif
 #ifdef REGISTER_TARGET_PRAGMAS
   REGISTER_TARGET_PRAGMAS (parse_in);
Index: c-pragma.h
===================================================================
RCS file: /cvs/gcc/egcs/gcc/c-pragma.h,v
retrieving revision 1.25
diff -u -r1.25 c-pragma.h
--- c-pragma.h	2001/10/14 17:43:58	1.25
+++ c-pragma.h	2001/11/10 19:31:34
@@ -41,6 +41,8 @@
 #define HANDLE_PRAGMA_PACK 1
 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
 
+#define HANDLE_PRAGMA_REDEFINE_EXTNAME 1
+
 extern void init_pragma PARAMS ((void));
 
 /* Duplicate prototypes for the register_pragma stuff and the typedef for
Index: gcc.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/gcc.c,v
retrieving revision 1.263
diff -u -r1.263 gcc.c
--- gcc.c	2001/11/04 02:47:36	1.263
+++ gcc.c	2001/11/10 19:31:38
@@ -657,7 +657,7 @@
  %{fshort-wchar:-U__WCHAR_TYPE__ -D__WCHAR_TYPE__=short\\ unsigned\\ int}\
  %{ffreestanding:-D__STDC_HOSTED__=0} %{fno-hosted:-D__STDC_HOSTED__=0}\
  %{!ffreestanding:%{!fno-hosted:-D__STDC_HOSTED__=1}}\
- %{fshow-column} %{fno-show-column}\
+ %{fshow-column} %{fno-show-column} -D__PRAGMA_REDEFINE_EXTNAME\
  %{fleading-underscore} %{fno-leading-underscore}\
  %{fno-operator-names} %{ftabstop=*} %{remap}\
  %{g3:-dD} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*&U*&A*} %{i*} %Z %i\
Index: tree.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/tree.c,v
retrieving revision 1.218
diff -u -r1.218 tree.c
--- tree.c	2001/10/31 15:28:41	1.218
+++ tree.c	2001/11/10 19:31:40
@@ -187,11 +187,33 @@
 tree global_trees[TI_MAX];
 tree integer_types[itk_none];
 
+/* Record the association between a name and an external name.  */
+
+void
+add_external_name (old, new)
+     tree old, new;
+{
+  redefined_extnames = tree_cons (old, new, redefined_extnames);
+}
+
+/* Find the external name, given the original name.  */
+
+tree
+find_external_name (old)
+     tree old;
+{
+  old = purpose_member (old, redefined_extnames);
+  if (old)
+    old = TREE_VALUE (old);
+  return old;
+}
+
 /* Set the DECL_ASSEMBLER_NAME for DECL.  */
 void
 set_decl_assembler_name (decl)
      tree decl;
 {
+  tree extname;
   /* The language-independent code should never use the
      DECL_ASSEMBLER_NAME for lots of DECLs.  Only FUNCTION_DECLs and
      VAR_DECLs for variables with static storage duration need a real
@@ -206,7 +228,13 @@
        for C, and GCC used to set DECL_ASSEMBLER_NAME to the same
        value as DECL_NAME in build_decl, so this choice provides
        backwards compatibility with existing front-ends.  */
-    SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
+    {
+
+      if ((extname = find_external_name (DECL_NAME (decl))) != NULL)
+	SET_DECL_ASSEMBLER_NAME (decl, extname);
+      else
+	SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
+    }
   else
     /* Nobody should ever be asking for the DECL_ASSEMBLER_NAME of
        these DECLs -- unless they're in language-dependent code, in
Index: tree.h
===================================================================
RCS file: /cvs/gcc/egcs/gcc/tree.h,v
retrieving revision 1.279
diff -u -r1.279 tree.h
--- tree.h	2001/11/09 07:14:03	1.279
+++ tree.h	2001/11/10 19:31:42
@@ -1871,6 +1871,8 @@
 
   TI_MAIN_IDENTIFIER,
 
+  TI_REDEFINED_EXTNAMES,
+
   TI_MAX
 };
 
@@ -1937,6 +1939,9 @@
 #define V2SI_type_node			global_trees[TI_V2SI_TYPE]
 #define V2SF_type_node			global_trees[TI_V2SF_TYPE]
 
+/* List of redefined external names. */
+#define redefined_extnames		global_trees[TI_REDEFINED_EXTNAMES]
+
 /* An enumeration of the standard C integer types.  These must be
    ordered so that shorter types appear before longer ones.  */
 enum integer_type_kind 
@@ -2666,6 +2671,8 @@
 extern tree get_callee_fndecl                   PARAMS ((tree));
 extern void set_decl_assembler_name             PARAMS ((tree));
 extern int type_num_arguments                   PARAMS ((tree));
+extern void add_external_name			PARAMS ((tree, tree));
+extern tree find_external_name			PARAMS ((tree));
 
 /* In stmt.c */
 
Index: cp/mangle.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/mangle.c,v
retrieving revision 1.38
diff -u -r1.38 mangle.c
--- mangle.c	2001/10/23 09:14:12	1.38
+++ mangle.c	2001/11/10 19:31:44
@@ -2173,7 +2173,13 @@
 	   /* And neither are `extern "C"' variables.  */
 	   || (TREE_CODE (decl) == VAR_DECL
 	       && DECL_EXTERN_C_P (decl)))
-    write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
+    {
+      tree extname;
+      if ((extname = find_external_name (DECL_NAME (decl))) != NULL)
+	write_string (IDENTIFIER_POINTER (extname));
+      else
+	write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
+    }
   else
     {
       write_mangled_name (decl);
Index: doc/extend.texi
===================================================================
RCS file: /cvs/gcc/egcs/gcc/doc/extend.texi,v
retrieving revision 1.36
diff -u -r1.36 extend.texi
--- extend.texi	2001/11/06 12:39:28	1.36
+++ extend.texi	2001/11/10 19:31:48
@@ -4485,6 +4485,7 @@
 @menu
 * ARM Pragmas::
 * Darwin Pragmas::
+* Solaris Pragmas::
 @end menu
 
 @node ARM Pragmas
@@ -4547,6 +4548,23 @@
 produce warnings for the listed variables.  The effect is similar to
 that of the @code{unused} attribute, except that this pragma may appear
 anywhere within the variables' scopes.
+@end table
+
+@node Solaris Pragmas
+@subsection Solaris Pragmas
+
+For compatibility with the SunPRO compiler, the following pragma
+is supported.
+
+@table @code
+@item redefine_extname @var{oldname} @var{newname}
+@cindex pragma, redefine_extname
+
+This macro gives the C function @var{oldname} the assembler label
+@var{newname}. The pragma must appear before the function declaration.
+This pragma is equivalent to the asm labels extension (@pxref{Asm
+Labels}). The preprocessor defines @code{__PRAGMA_REDEFINE_EXTNAME}
+if the pragma is available.
 @end table
 
 @node Unnamed Fields


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