This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
RF[CT]: IMA repairs 2/n: maybe_apply_renaming_pragma rawrite
- From: Zack Weinberg <zack at codesourcery dot com>
- To: gcc-patches at gcc dot gnu dot org
- Cc: Rainer Orth <ro at TechFak dot Uni-Bielefeld dot DE>, Andreas Tobler<toa at pop dot agri dot ch>
- Date: Sat, 29 May 2004 00:55:07 -0700
- Subject: RF[CT]: IMA repairs 2/n: maybe_apply_renaming_pragma rawrite
This patch cleans up the way maybe_apply_renaming_pragma works, so
that it doesn't have to instantiate DECL_ASSEMBLER_NAME for the vast
majority of cases. This involves a certain number of semantic
changes; we discussed these last week and agreed to the semantics
implemented, with one exception - it proved impossible to make #pragma
redefine_extname always beat #pragma extern_prefix, because there's no
way to tell that a DECL_ASSEMBLER_NAME came from #pragma extern_prefix
after the fact. See the comment in c-pragma.c for details.
I attempted to bootstrap this patch on sparc-solaris2.8 but failed; I
got an ICE in calls.c on libgcc2.c::_trampoline, here:
3301 /* Verify that we've deallocated all the stack we used. */
3302 if (! (flags & (ECF_NORETURN | ECF_LONGJMP))
3303 && old_stack_allocated != stack_pointer_delta
3304 - pending_stack_adjust)
3305 abort ();
This appears to be a case of the stage 1 compiler miscompiling the
stage 2 compiler -- given the same input, the stage 1 compiler does
not crash.
I would appreciate comments on the patch, help with the Solaris crash,
and testing on alpha-dec-osf4 (which is the only target that enables
#pragma extern_prefix). There is no user documentation in this patch;
I will add it when I revise the patch.
zw
* c-pragma.c: Include target.h. Add commentary on corner
cases in redefine_extname and extern_prefix pragmas.
(handle_pragma_redefine_extname): Use GCC_BAD. Use string
comparison to detect a redundant rename, having applied
targetm.strip_name_encoding first. Warn when the pragma is
being ignored, and we would have warned about an unknown
pragma. Do not put entries on the pending rename list when
identifier_global_value returned something other than a
FUNCTION_ or VAR_DECL. Do not rename non-external or
non-"C"-linkage names.
(handle_pragma_extern_prefix): Likewise. Define unconditionally.
(maybe_apply_renaming_pragma): Likewise; also, take care not
to instantiate DECL_ASSEMBLER_NAME unnecessarily.
Unconditionally include code for #pragma extern_prefix.
(init_pragma): Unconditionally register #pragma extern_prefix
and #pragma redefine_extname.
* c-objc-common.c (has_c_linkage): New function.
* c-common.h: Prototype it.
* target.h (targetm): Add handle_pragma_redefine_extname and
handle_pragma_extern_prefix booleans.
* target-def.h: Default TARGET_HANDLE_PRAGMA_REDEFINE_EXTNAME
and TARGET_HANDLE_PRAGMA_EXTERN_PREFIX to 0. Update
TARGET_INITIALIZER.
* system.h: Poison HANDLE_PRAGMA_REDEFINE_EXTNAME and
HANDLE_PRAGMA_EXTERN_PREFIX.
* config/sol2.h, config/alpha/osf.h: Update to match.
cp:
* cp-lang.c (has_c_linkage): New function.
===================================================================
Index: c-common.h
--- c-common.h 28 May 2004 20:32:20 -0000 1.228
+++ c-common.h 29 May 2004 07:44:31 -0000
@@ -336,6 +336,7 @@ extern void c_finish_while_stmt_cond (tr
extern int field_decl_cmp (const void *, const void *);
extern void resort_sorted_fields (void *, void *, gt_pointer_operator,
void *);
+extern bool has_c_linkage (tree decl);
/* Switches common to the C front ends. */
===================================================================
Index: c-objc-common.c
--- c-objc-common.c 13 May 2004 06:39:29 -0000 1.44
+++ c-objc-common.c 29 May 2004 07:44:31 -0000
@@ -327,3 +327,9 @@ c_objc_common_truthvalue_conversion (tre
return c_common_truthvalue_conversion (expr);
}
+/* In C and ObjC, all decls have "C" linkage. */
+bool
+has_c_linkage (tree decl ATTRIBUTE_UNUSED)
+{
+ return true;
+}
===================================================================
Index: c-pragma.c
--- c-pragma.c 13 May 2004 06:39:29 -0000 1.69
+++ c-pragma.c 29 May 2004 07:44:31 -0000
@@ -34,6 +34,7 @@ Software Foundation, 59 Temple Place - S
#include "c-common.h"
#include "output.h"
#include "tm_p.h"
+#include "target.h"
#define GCC_BAD(msgid) do { warning (msgid); return; } while (0)
#define GCC_BAD2(msgid, arg) do { warning (msgid, arg); return; } while (0)
@@ -350,11 +351,47 @@ maybe_apply_pragma_weak (tree decl ATTRI
}
#endif /* HANDLE_PRAGMA_WEAK */
+/* GCC supports two #pragma directives for renaming the external
+ symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
+ compatibility with the Solaris and Tru64 system headers. GCC also
+ has its own notation for this, __asm__("name") annotations.
+
+ Corner cases of these features and their interaction:
+
+ 1) Both pragmas silently apply only to declarations with external
+ linkage (that is, TREE_PUBLIC || DECL_EXTERNAL).
+
+ 2) If any of the three ways of changing DECL_ASSEMBLER_NAME is
+ applied to a decl whose DECL_ASSEMBLER_NAME is already set, a
+ warning issues and the name does not change. (The warning does
+ not issue if it's a redundant operation - only if it's
+ inconsistent.)
+
+ 3) The "source name" for #pragma redefine_extname is the DECL_NAME,
+ *not* the DECL_ASSEMBLER_NAME.
+
+ 4) If #pragma extern_prefix is in effect and a declaration occurs
+ with an __asm__ name, the #pragma extern_prefix is silently
+ ignored for that declaration.
+
+ 5) If #pragma extern_prefix and #pragma redefine_extname apply to
+ the same declaration, whichever triggered first wins, and a warning
+ is issued. (We would like to have #pragma redefine_extname always
+ win, but it can appear either before or after the declaration, and
+ if it appears afterward, we have no way of knowing whether a modified
+ DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.)
+
+ is applied to a declaration, the #pragma
+ extern_prefix is silently ignored for that declaration.
+
+ 6) In C++, both #pragmas silently apply only to extern "C" declarations. */
+
+
static GTY(()) tree pending_redefine_extname;
static void handle_pragma_redefine_extname (cpp_reader *);
-/* #pragma redefined_extname oldname newname */
+/* #pragma redefine_extname oldname newname */
static void
handle_pragma_redefine_extname (cpp_reader *dummy ATTRIBUTE_UNUSED)
{
@@ -362,44 +399,62 @@ handle_pragma_redefine_extname (cpp_read
enum cpp_ttype t;
if (c_lex (&oldname) != CPP_NAME)
- {
- warning ("malformed #pragma redefine_extname, ignored");
- return;
- }
+ GCC_BAD ("malformed #pragma redefine_extname, ignored");
if (c_lex (&newname) != CPP_NAME)
- {
- warning ("malformed #pragma redefine_extname, ignored");
- return;
- }
+ GCC_BAD ("malformed #pragma redefine_extname, ignored");
t = c_lex (&x);
if (t != CPP_EOF)
warning ("junk at end of #pragma redefine_extname");
+ if (!flag_mudflap && !targetm.handle_pragma_redefine_extname)
+ {
+ if (warn_unknown_pragmas > in_system_header)
+ warning ("#pragma redefine_extname not supported on this target");
+ return;
+ }
+
decl = identifier_global_value (oldname);
- if (decl && (TREE_CODE (decl) == FUNCTION_DECL
- || TREE_CODE (decl) == VAR_DECL))
+ if (!decl)
+ add_to_renaming_pragma_list (oldname, newname);
+ else if ((TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
+ && (TREE_CODE (decl) == FUNCTION_DECL
+ || TREE_CODE (decl) == VAR_DECL)
+ && has_c_linkage (decl))
{
- if (DECL_ASSEMBLER_NAME_SET_P (decl)
- && DECL_ASSEMBLER_NAME (decl) != newname)
- warning ("#pragma redefine_extname conflicts with declaration");
- change_decl_assembler_name (decl, newname);
+ if (DECL_ASSEMBLER_NAME_SET_P (decl))
+ {
+ const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
+ name = targetm.strip_name_encoding (name);
+
+ if (strcmp (name, IDENTIFIER_POINTER (newname)))
+ warning ("#pragma redefine_extname ignored due to conflict with "
+ "previous rename");
+ }
+ else
+ change_decl_assembler_name (decl, newname);
}
- else
- add_to_renaming_pragma_list(oldname, newname);
+ /* else ignore */
}
+/* This is called from here and from ia64.c. */
void
add_to_renaming_pragma_list (tree oldname, tree newname)
{
+ tree previous = purpose_member (oldname, pending_redefine_extname);
+ if (previous)
+ {
+ if (TREE_VALUE (previous) != newname)
+ warning ("#pragma redefine_extname ignored due to conflict with "
+ "previous #pragma redefine_extname");
+ return;
+ }
+
pending_redefine_extname
= tree_cons (oldname, newname, pending_redefine_extname);
}
static GTY(()) tree pragma_extern_prefix;
-#ifdef HANDLE_PRAGMA_EXTERN_PREFIX
-static void handle_pragma_extern_prefix (cpp_reader *);
-
/* #pragma extern_prefix "prefix" */
static void
handle_pragma_extern_prefix (cpp_reader *dummy ATTRIBUTE_UNUSED)
@@ -408,18 +463,17 @@ handle_pragma_extern_prefix (cpp_reader
enum cpp_ttype t;
if (c_lex (&prefix) != CPP_STRING)
- {
- warning ("malformed #pragma extern_prefix, ignored");
- return;
- }
+ GCC_BAD ("malformed #pragma extern_prefix, ignored");
t = c_lex (&x);
if (t != CPP_EOF)
warning ("junk at end of #pragma extern_prefix");
- /* Note that the length includes the null terminator. */
- pragma_extern_prefix = (TREE_STRING_LENGTH (prefix) > 1 ? prefix : NULL);
+ if (targetm.handle_pragma_extern_prefix)
+ /* Note that the length includes the null terminator. */
+ pragma_extern_prefix = (TREE_STRING_LENGTH (prefix) > 1 ? prefix : NULL);
+ else if (warn_unknown_pragmas > in_system_header)
+ warning ("#pragma extern_prefix not supported on this target");
}
-#endif
/* Hook from the front ends to apply the results of one of the preceding
pragmas that rename variables. */
@@ -427,56 +481,88 @@ handle_pragma_extern_prefix (cpp_reader
tree
maybe_apply_renaming_pragma (tree decl, tree asmname)
{
- tree oldname;
+ tree *p, t;
- /* Copied from the check in set_decl_assembler_name. */
- if (TREE_CODE (decl) == FUNCTION_DECL
- || (TREE_CODE (decl) == VAR_DECL
- && (TREE_STATIC (decl)
- || DECL_EXTERNAL (decl)
- || TREE_PUBLIC (decl))))
- oldname = DECL_ASSEMBLER_NAME (decl);
- else
+ /* The renaming pragmas are only applied to declarations with
+ external linkage. */
+ if ((TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
+ || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)))
return asmname;
- /* If the name begins with a *, that's a sign of an asmname attached to
- a previous declaration. */
- if (IDENTIFIER_POINTER (oldname)[0] == '*')
- {
- const char *oldasmname = IDENTIFIER_POINTER (oldname) + 1;
- if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldasmname) != 0)
- warning ("asm declaration conflicts with previous rename");
- asmname = build_string (strlen (oldasmname), oldasmname);
+ /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
+ but we may warn about a rename that conflicts. */
+ if (DECL_ASSEMBLER_NAME_SET_P (decl))
+ {
+ const char *oldname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
+ oldname = targetm.strip_name_encoding (oldname);
+
+ if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldname))
+ warning ("asm declaration ignored due to "
+ "conflict with previous rename");
+
+ /* Take any pending redefine_extname off the list. */
+ for (p = &pending_redefine_extname; (t = *p); p = &TREE_CHAIN (t))
+ if (DECL_NAME (decl) == TREE_PURPOSE (t))
+ {
+ /* Only warn if there is a conflict. */
+ if (strcmp (IDENTIFIER_POINTER (TREE_VALUE (t)), oldname))
+ warning ("#pragma redefine_extname ignored due to "
+ "conflict with previous rename");
+
+ *p = TREE_CHAIN (t);
+ break;
+ }
+ return 0;
}
- {
- tree *p, t;
+ /* Find out if we have a pending #pragma redefine_extname. */
+ for (p = &pending_redefine_extname; (t = *p); p = &TREE_CHAIN (t))
+ if (DECL_NAME (decl) == TREE_PURPOSE (t))
+ {
+ tree newname = TREE_VALUE (t);
+ *p = TREE_CHAIN (t);
- for (p = &pending_redefine_extname; (t = *p) ; p = &TREE_CHAIN (t))
- if (oldname == TREE_PURPOSE (t))
- {
- const char *newname = IDENTIFIER_POINTER (TREE_VALUE (t));
+ /* If we already have an asmname, #pragma redefine_extname is
+ ignored (with a warning if it conflicts). */
+ if (asmname)
+ {
- if (asmname && strcmp (TREE_STRING_POINTER (asmname), newname) != 0)
- warning ("#pragma redefine_extname conflicts with declaration");
- *p = TREE_CHAIN (t);
+ if (strcmp (TREE_STRING_POINTER (asmname),
+ IDENTIFIER_POINTER (newname)) != 0)
+ warning ("#pragma redefine_extname ignored due to "
+ "conflict with __asm__ declaration");
+ return asmname;
+ }
- return build_string (strlen (newname), newname);
- }
- }
+ /* Otherwise we use what we've got; #pragma extern_prefix is
+ silently ignored. */
+ return build_string (IDENTIFIER_LENGTH (newname),
+ IDENTIFIER_POINTER (newname));
+ }
+
+ /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
+ if (asmname)
+ return asmname;
-#ifdef HANDLE_PRAGMA_EXTERN_PREFIX
- if (pragma_extern_prefix && !asmname)
+ /* If #pragma extern_prefix is in effect, apply it. */
+ if (pragma_extern_prefix)
{
- char *x = concat (TREE_STRING_POINTER (pragma_extern_prefix),
- IDENTIFIER_POINTER (oldname), NULL);
- asmname = build_string (strlen (x), x);
- free (x);
- return asmname;
+ const char *prefix = TREE_STRING_POINTER (pragma_extern_prefix);
+ size_t plen = TREE_STRING_LENGTH (pragma_extern_prefix);
+
+ const char *id = IDENTIFIER_POINTER (DECL_NAME (decl));
+ size_t ilen = IDENTIFIER_LENGTH (DECL_NAME (decl));
+
+ char *newname = alloca (plen + ilen + 1);
+
+ memcpy (newname, prefix, plen);
+ memcpy (newname + plen, id, ilen + 1);
+
+ return build_string (plen + ilen, newname);
}
-#endif
- return asmname;
+ /* Nada. */
+ return 0;
}
/* Front-end wrapper for pragma registration to avoid dragging
@@ -498,15 +584,10 @@ init_pragma (void)
#ifdef HANDLE_PRAGMA_WEAK
c_register_pragma (0, "weak", handle_pragma_weak);
#endif
-#ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
+
c_register_pragma (0, "redefine_extname", handle_pragma_redefine_extname);
-#else
- if (flag_mudflap)
- c_register_pragma (0, "redefine_extname", handle_pragma_redefine_extname);
-#endif
-#ifdef HANDLE_PRAGMA_EXTERN_PREFIX
c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix);
-#endif
+
#ifdef REGISTER_TARGET_PRAGMAS
REGISTER_TARGET_PRAGMAS ();
===================================================================
Index: system.h
--- system.h 24 May 2004 10:50:40 -0000 1.213
+++ system.h 29 May 2004 07:44:31 -0000
@@ -596,7 +596,8 @@ typedef char _Bool;
STRUCT_VALUE_INCOMING STRICT_ARGUMENT_NAMING \
PROMOTE_FUNCTION_RETURN PROMOTE_PROTOTYPES STRUCT_VALUE_REGNUM \
SETUP_INCOMING_VARARGS EXPAND_BUILTIN_SAVEREGS \
- DEFAULT_SHORT_ENUMS SPLIT_COMPLEX_ARGS MD_ASM_CLOBBERS
+ DEFAULT_SHORT_ENUMS SPLIT_COMPLEX_ARGS MD_ASM_CLOBBERS \
+ HANDLE_PRAGMA_REDEFINE_EXTNAME HANDLE_PRAGMA_EXTERN_PREFIX
/* Other obsolete target macros, or macros that used to be in target
headers and were not used, and may be obsolete or may never have
===================================================================
Index: target-def.h
--- target-def.h 14 May 2004 12:53:09 -0000 1.78
+++ target-def.h 29 May 2004 07:44:31 -0000
@@ -372,6 +372,15 @@ Foundation, 59 Temple Place - Suite 330,
TARGET_SPLIT_COMPLEX_ARG, \
}
+
+#ifndef TARGET_HANDLE_PRAGMA_REDEFINE_EXTNAME
+#define TARGET_HANDLE_PRAGMA_REDEFINE_EXTNAME 0
+#endif
+
+#ifndef TARGET_HANDLE_PRAGMA_EXTERN_PREFIX
+#define TARGET_HANDLE_PRAGMA_EXTERN_PREFIX 0
+#endif
+
/* The whole shebang. */
#define TARGET_INITIALIZER \
{ \
@@ -424,6 +433,8 @@ Foundation, 59 Temple Place - Suite 330,
TARGET_TERMINATE_DW2_EH_FRAME_INFO, \
TARGET_ASM_FILE_START_APP_OFF, \
TARGET_ASM_FILE_START_FILE_DIRECTIVE, \
+ TARGET_HANDLE_PRAGMA_REDEFINE_EXTNAME, \
+ TARGET_HANDLE_PRAGMA_EXTERN_PREFIX, \
}
#include "hooks.h"
===================================================================
Index: target.h
--- target.h 19 May 2004 02:11:41 -0000 1.87
+++ target.h 29 May 2004 07:44:31 -0000
@@ -494,6 +494,12 @@ struct gcc_target
at the beginning of assembly output. */
bool file_start_file_directive;
+ /* True if #pragma redefine_extname is to be supported. */
+ bool handle_pragma_redefine_extname;
+
+ /* True if #pragma extern_prefix is to be supported. */
+ bool handle_pragma_extern_prefix;
+
/* Leave the boolean fields at the end. */
};
===================================================================
Index: config/sol2.h
--- config/sol2.h 18 Dec 2003 20:13:50 -0000 1.9
+++ config/sol2.h 29 May 2004 07:44:32 -0000
@@ -39,7 +39,7 @@ Boston, MA 02111-1307, USA. */
#undef WINT_TYPE_SIZE
#define WINT_TYPE_SIZE BITS_PER_WORD
-#define HANDLE_PRAGMA_REDEFINE_EXTNAME 1
+#define TARGET_HANDLE_PRAGMA_REDEFINE_EXTNAME 1
/* ??? Note: in order for -compat-bsd to work fully,
we must somehow arrange to fixincludes /usr/ucbinclude
===================================================================
Index: config/alpha/osf.h
--- config/alpha/osf.h 30 Apr 2004 20:09:51 -0000 1.36
+++ config/alpha/osf.h 29 May 2004 07:44:32 -0000
@@ -211,4 +211,4 @@ __enable_execute_stack (void *addr)
/* Handle #pragma extern_prefix. Technically only needed for Tru64 5.x,
but easier to manipulate preprocessor bits from here. */
-#define HANDLE_PRAGMA_EXTERN_PREFIX 1
+#define TARGET_HANDLE_PRAGMA_EXTERN_PREFIX 1
===================================================================
Index: cp/cp-lang.c
--- cp/cp-lang.c 13 May 2004 06:40:13 -0000 1.78
+++ cp/cp-lang.c 29 May 2004 07:44:32 -0000
@@ -386,3 +386,10 @@ void
pop_file_scope (void)
{
}
+
+/* c-pragma.c needs to query whether a decl has extern "C" linkage. */
+bool
+has_c_linkage (tree decl)
+{
+ return DECL_EXTERN_C_P (decl);
+}