OSDN Git Service

* doc/install.texi (xtensa-*-elf): New target.
[pf3gnuchains/gcc-fork.git] / gcc / config / darwin.c
index 3e2a000..6864f0c 100644 (file)
@@ -35,10 +35,9 @@ Boston, MA 02111-1307, USA.  */
 #include "tree.h"
 #include "expr.h"
 #include "reload.h"
-/* need for IDENTIFIER_GLOBAL_VALUE and IDENTIFIER_LOCAL_VALUE */
-#include "c-tree.h"
 #include "function.h"
 #include "ggc.h"
+#include "langhooks.h"
 
 #include "darwin-protos.h"
 
@@ -46,10 +45,8 @@ extern void machopic_output_stub PARAMS ((FILE *, const char *, const char *));
 
 static int machopic_data_defined_p PARAMS ((const char *));
 static int func_name_maybe_scoped PARAMS ((const char *));
-
-/* Make everything that used to go in the text section really go there.  */
-
-int flag_no_mach_text_sections = 0;
+static void update_non_lazy_ptrs PARAMS ((const char *));
+static void update_stubs PARAMS ((const char *));
 
 int
 name_needs_quotes (name)
@@ -57,7 +54,7 @@ name_needs_quotes (name)
 {
   int c;
   while ((c = *name++) != '\0')
-    if (!isalnum (c) && c != '_')
+    if (! ISIDNUM (c))
       return 1;
   return 0;
 }
@@ -85,10 +82,11 @@ machopic_classify_ident (ident)
                     && name[3] == 'J'
                     && name[4] == 'C'
                     && name[5] == '_'));
-  tree temp, decl = lookup_name_darwin (ident);
+  tree temp;
 
-  if (!decl)
+  if (name[0] != '!')
     {
+      /* Here if no special encoding to be found.  */
       if (lprefix)
        {
          const char *name = IDENTIFIER_POINTER (ident);
@@ -114,36 +112,40 @@ machopic_classify_ident (ident)
       return MACHOPIC_UNDEFINED;
     }
 
-  /* variable declarations */
-  else if (TREE_CODE (decl) == VAR_DECL)
-    {
-      if ((DECL_INITIAL (decl)
-           || TREE_STATIC (decl))
-          && ! TREE_PUBLIC (decl))
-       return MACHOPIC_DEFINED_DATA;
-    }
+  else if (name[1] == 'D')
+    return MACHOPIC_DEFINED_DATA;
+
+  else if (name[1] == 'T')
+    return MACHOPIC_DEFINED_FUNCTION;
 
-  /* function declarations */
-  else if (TREE_CODE (decl) == FUNCTION_DECL
-          && (!DECL_EXTERNAL (decl)))
+  /* It is possible that someone is holding a "stale" name, which has
+     since been defined.  See if there is a "defined" name (i.e,
+     different from NAME only in having a '!D_' or a '!T_' instead of
+     a '!d_' or '!t_' prefix) in the identifier hash tables.  If so, say
+     that this identifier is defined.  */
+  else if (name[1] == 'd' || name[1] == 't')
     {
-      if (TREE_STATIC (decl)
-         || TREE_ASM_WRITTEN (decl))
-       return MACHOPIC_DEFINED_FUNCTION;
+      char *new_name;
+      new_name = (char *)alloca (strlen (name) + 1);
+      strcpy (new_name, name);
+      new_name[1] = (name[1] == 'd') ? 'D' : 'T';
+      if (maybe_get_identifier (new_name) != NULL)
+       return  (name[1] == 'd') ? MACHOPIC_DEFINED_DATA
+                                : MACHOPIC_DEFINED_FUNCTION;
     }
 
   for (temp = machopic_defined_list; temp != NULL_TREE; temp = TREE_CHAIN (temp))
     {
       if (ident == TREE_VALUE (temp))
        {
-         if (TREE_CODE (decl) == FUNCTION_DECL)
+         if (name[1] == 'T')
            return MACHOPIC_DEFINED_FUNCTION;
          else
            return MACHOPIC_DEFINED_DATA;
        }
     }
   
-  if (TREE_CODE (decl) == FUNCTION_DECL)
+  if (name[1] == 't' || name[1] == 'T')
     {
       if (lprefix)
        return MACHOPIC_DEFINED_FUNCTION;
@@ -218,20 +220,6 @@ machopic_define_name (name)
   machopic_define_ident (get_identifier (name));
 }
 
-tree
-lookup_name_darwin (name)
-     tree name;
-{
-  tree val;
-
-  if (!global_bindings_p()
-      && IDENTIFIER_LOCAL_VALUE (name))
-    val = IDENTIFIER_LOCAL_VALUE (name);
-  else
-    val = IDENTIFIER_GLOBAL_VALUE (name);
-  return val;
-}
-
 /* This is a static to make inline functions work.  The rtx
    representing the PIC base symbol always points to here. */
 
@@ -242,7 +230,7 @@ static int current_pic_label_num;
 char *
 machopic_function_base_name ()
 {
-  static char *name = NULL;
+  static const char *name = NULL;
   static const char *current_name;
 
   current_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
@@ -279,6 +267,7 @@ char *
 machopic_non_lazy_ptr_name (name)
      const char *name;
 {
+  char *temp_name;
   tree temp, ident = get_identifier (name);
   
   for (temp = machopic_non_lazy_pointers;
@@ -289,6 +278,22 @@ machopic_non_lazy_ptr_name (name)
        return IDENTIFIER_POINTER (TREE_PURPOSE (temp));
     }
 
+  STRIP_NAME_ENCODING (name, name);
+
+  /* Try again, but comparing names this time.  */
+  for (temp = machopic_non_lazy_pointers;
+       temp != NULL_TREE; 
+       temp = TREE_CHAIN (temp))
+    {
+      if (TREE_VALUE (temp))
+       {
+         temp_name = IDENTIFIER_POINTER (TREE_VALUE (temp));
+         STRIP_NAME_ENCODING (temp_name, temp_name);
+         if (strcmp (name, temp_name) == 0)
+           return IDENTIFIER_POINTER (TREE_PURPOSE (temp));
+       }
+    }
+
   {
     char *buffer;
     tree ptr_name;
@@ -336,15 +341,27 @@ machopic_stub_name (name)
      const char *name;
 {
   tree temp, ident = get_identifier (name);
-  
+  const char *tname;
+
   for (temp = machopic_stubs;
        temp != NULL_TREE; 
        temp = TREE_CHAIN (temp))
     {
       if (ident == TREE_VALUE (temp))
        return IDENTIFIER_POINTER (TREE_PURPOSE (temp));
+      tname = IDENTIFIER_POINTER (TREE_VALUE (temp));
+      if (strcmp (name, tname) == 0)
+       return IDENTIFIER_POINTER (TREE_PURPOSE (temp));
+      /* A library call name might not be section-encoded yet, so try
+        it against a stripped name.  */
+      if (name[0] != '!'
+         && tname[0] == '!'
+         && strcmp (name, tname + 4) == 0)
+       return IDENTIFIER_POINTER (TREE_PURPOSE (temp));
     }
 
+  STRIP_NAME_ENCODING (name, name);
+
   {
     char *buffer;
     tree ptr_name;
@@ -384,7 +401,8 @@ machopic_validate_stub_or_non_lazy_ptr (name, validate_stub)
      const char *name;
      int validate_stub;
 {
-    tree temp, ident = get_identifier (name);
+  char *real_name;
+  tree temp, ident = get_identifier (name), id2;
 
     for (temp = (validate_stub ? machopic_stubs : machopic_non_lazy_pointers);
          temp != NULL_TREE;
@@ -396,6 +414,10 @@ machopic_validate_stub_or_non_lazy_ptr (name, validate_stub)
           TREE_USED (temp) = 1;
          if (TREE_CODE (TREE_VALUE (temp)) == IDENTIFIER_NODE)
            TREE_SYMBOL_REFERENCED (TREE_VALUE (temp)) = 1;
+         STRIP_NAME_ENCODING (real_name, IDENTIFIER_POINTER (TREE_VALUE (temp)));
+         id2 = maybe_get_identifier (real_name);
+         if (id2)
+           TREE_SYMBOL_REFERENCED (id2) = 1;
        }
 }
 
@@ -476,13 +498,9 @@ machopic_indirect_data_reference (orig, reg)
        return orig;
 
       if (MACHOPIC_PURE && GET_CODE (orig) == CONST_INT)
-        {
-         result = plus_constant_for_output (base, INTVAL (orig));
-        }
+       result = plus_constant (base, INTVAL (orig));
       else
-        {
-           result = gen_rtx (PLUS, Pmode, base, orig);
-        }
+       result = gen_rtx (PLUS, Pmode, base, orig);
 
       if (RTX_UNCHANGING_P (base) && RTX_UNCHANGING_P (orig))
        RTX_UNCHANGING_P (result) = 1;
@@ -525,78 +543,6 @@ machopic_indirect_data_reference (orig, reg)
   return ptr_ref;
 }
 
-/* For MACHOPIC_INDIRECT_CALL_TARGET below, we need to beware of:
-
-       extern "C" { int f(); }
-       struct X { int f(); int g(); };
-       int X::f() { ::f(); }
-       int X::g() { ::f(); f();}
-
-  This is hairy.  Both calls to "::f()" need to be indirect (i.e., to
-  appropriate symbol stubs), but since MACHOPIC_NAME_DEFINED_P calls
-  GET_IDENTIFIER which treats "f" as "X::f", and "X::f" is indeed (being)
-  defined somewhere in "X"'s inheritance hierarchy, MACHOPIC_NAME_DEFINED_P
-  returns TRUE when called with "f", which means that
-  MACHOPIC_INDIRECT_CALL_TARGET uses an "internal" call instead of an
-  indirect one as it should.
-
-  Our quick-n-dirty solution to this is to call the following
-  FUNC_NAME_MAYBE_SCOPED routine which (only for C++) checks whether
-  FNAME -- the name of the function which we're calling -- is NOT a
-  mangled C++ name, AND if the current function being compiled is a
-  method, and if so, use an "external" or "indirect" call. 
-
-  Note that this function will be called ONLY when MACHOPIC_INDIRECT_TARGET_P
-  has already indicated that the target is NOT indirect.
-
-  This conservative solution will sometimes make indirect calls where
-  it might have been possible to make direct ones.
-
-  FUNC_NAME_MAYBE_SCOPED returns 1 to indicate a "C" name (not scoped),
-  which in turns means we should create a stub for an indirect call.
-  */
-
-static int is_cplusplus = -1;
-
-static int
-func_name_maybe_scoped (fname)
-     const char *fname;
-{
-
-  if (is_cplusplus < 0)
-    is_cplusplus = (strcmp (lang_identify (), "cplusplus") == 0);
-
-  if (is_cplusplus)
-    {
-      /* If we have a method, then check whether the function we're trying to
-         call is a "C" function.  If so, we should use an indirect call.
-
-         It turns out to be hard to tell whether "we have a method", since
-         static member functions have a TREE_CODE of FUNCTION_TYPE, as do
-         namespace-level non-member functions.  So here, we always look for
-         an extern-"C"-like name, and make stubs for them no matter the
-         calling context.  This is temporary, and leaves nagging suspicion
-        that improvements should be possible here.  (I.e., I suspect that
-         it can still sometimes make stubs where it needn't.)  */
-
-      /* if (1 || TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE) */
-       {
-         /* If fname is of the form "f__1X" or "f__Fv", it's C++.  */
-         while (*fname == '_') ++fname;        /* skip leading underscores  */
-         while (*fname != 0)
-           {
-             if (fname[0] == '_' && fname[1] == '_'
-                 && (fname[2] == 'F' || (fname[2] >= '0' && fname[2] <= '9')))
-               return 0;
-             ++fname;
-           }
-         /* Not a C++ mangled name: must be "C", in which case play safe.  */
-         return 1;
-       }
-    }
-  return 0;
-}
-
 /* Transform TARGET (a MEM), which is a function call target, to the
    corresponding symbol_stub if necessary.  Return a new MEM.  */
 
@@ -612,7 +558,11 @@ machopic_indirect_call_target (target)
       enum machine_mode mode = GET_MODE (XEXP (target, 0));
       const char *name = XSTR (XEXP (target, 0), 0);
 
-      if (!machopic_name_defined_p (name) || func_name_maybe_scoped (name)) 
+      /* If the name is already defined, we need do nothing.  */
+      if (name[0] == '!' && name[1] == 'T')
+       return target;
+
+      if (!machopic_name_defined_p (name))
        {
          const char *stub_name = machopic_stub_name (name);
 
@@ -837,13 +787,11 @@ machopic_legitimize_pic_address (orig, mode, reg)
                                              Pmode, (base == reg ? 0 : reg));
       if (GET_CODE (orig) == CONST_INT)
        {
-         pic_ref = plus_constant_for_output (base, INTVAL (orig));
+         pic_ref = plus_constant (base, INTVAL (orig));
          is_complex = 1;
        }
       else
-       {
-         pic_ref = gen_rtx (PLUS, Pmode, base, orig);
-       }
+       pic_ref = gen_rtx (PLUS, Pmode, base, orig);
 
       if (RTX_UNCHANGING_P (base) && RTX_UNCHANGING_P (orig))
        RTX_UNCHANGING_P (pic_ref) = 1;
@@ -886,24 +834,20 @@ machopic_finish (asm_out_file)
        temp != NULL_TREE;
        temp = TREE_CHAIN (temp))
     {
-      char *sym_name = IDENTIFIER_POINTER (TREE_VALUE (temp));
-      char *stub_name = IDENTIFIER_POINTER (TREE_PURPOSE (temp));
+      const char *sym_name = IDENTIFIER_POINTER (TREE_VALUE (temp));
+      const char *stub_name = IDENTIFIER_POINTER (TREE_PURPOSE (temp));
       char *sym;
       char *stub;
-      tree decl = lookup_name_darwin (TREE_VALUE (temp));
 
       if (! TREE_USED (temp))
        continue;
 
-      /* Don't emit stubs for static inline functions which have not
-         been compiled.  */
-      if (decl
-          && TREE_CODE (decl) == FUNCTION_DECL
-          && DECL_INLINE (decl)
-          && ! TREE_PUBLIC (decl)
-          && ! TREE_ASM_WRITTEN (decl))
+      /* If the symbol is actually defined, we don't need a stub.  */
+      if (sym_name[0] == '!' && sym_name[1] == 'T')
        continue;
 
+      STRIP_NAME_ENCODING (sym_name, sym_name);
+
       sym = alloca (strlen (sym_name) + 2);
       if (sym_name[0] == '*' || sym_name[0] == '&')
        strcpy (sym, sym_name + 1);
@@ -941,10 +885,11 @@ machopic_finish (asm_out_file)
          )
        {
          data_section ();
-         assemble_align (UNITS_PER_WORD * BITS_PER_UNIT);
+         assemble_align (GET_MODE_ALIGNMENT (Pmode));
          assemble_label (lazy_name);
          assemble_integer (gen_rtx (SYMBOL_REF, Pmode, sym_name),
-                           GET_MODE_SIZE (Pmode), 1);
+                           GET_MODE_SIZE (Pmode),
+                           GET_MODE_ALIGNMENT (Pmode), 1);
        }
       else
        {
@@ -956,7 +901,8 @@ machopic_finish (asm_out_file)
          assemble_name (asm_out_file, sym_name); 
          fprintf (asm_out_file, "\n");
 
-         assemble_integer (const0_rtx, GET_MODE_SIZE (Pmode), 1);
+         assemble_integer (const0_rtx, GET_MODE_SIZE (Pmode),
+                           GET_MODE_ALIGNMENT (Pmode), 1);
        }
     }
 }
@@ -1000,3 +946,196 @@ machopic_operand_p (op)
 
   return 0;
 }
+
+/* This function records whether a given name corresponds to a defined
+   or undefined function or variable, for machopic_classify_ident to
+   use later.  */
+
+void
+darwin_encode_section_info (decl)
+     tree decl;
+{
+  char code = '\0';
+  int defined = 0;
+  rtx sym_ref;
+  const char *orig_str;
+  char *new_str;
+  size_t len, new_len;
+
+  if ((TREE_CODE (decl) == FUNCTION_DECL
+       || TREE_CODE (decl) == VAR_DECL)
+      && !DECL_EXTERNAL (decl)
+      && ((TREE_STATIC (decl)
+          && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
+         || (DECL_INITIAL (decl)
+             && DECL_INITIAL (decl) != error_mark_node)))
+    defined = 1;
+
+  if (TREE_CODE (decl) == FUNCTION_DECL)
+    code = (defined ? 'T' : 't');
+  else if (TREE_CODE (decl) == VAR_DECL)
+    code = (defined ? 'D' : 'd');
+
+  if (code == '\0')
+    return;
+
+  sym_ref = XEXP (DECL_RTL (decl), 0);
+  orig_str = XSTR (sym_ref, 0);
+  len = strlen (orig_str) + 1;
+
+  if (orig_str[0] == '!')
+    {
+      /* Already encoded; see if we need to change it.  */
+      if (code == orig_str[1])
+       return;
+      /* Yes, tweak a copy of the name and put it in a new string.  */
+      new_str = alloca (len);
+      memcpy (new_str, orig_str, len);
+      new_str[1] = code;
+      XSTR (sym_ref, 0) = ggc_alloc_string (new_str, len);
+    }
+  else
+    {
+      /* Add the encoding.  */
+      new_len = len + 4;
+      new_str = alloca (new_len);
+      new_str[0] = '!';
+      new_str[1] = code;
+      new_str[2] = '_';
+      new_str[3] = '_';
+      memcpy (new_str + 4, orig_str, len);
+      XSTR (sym_ref, 0) = ggc_alloc_string (new_str, new_len);
+    }
+  /* The non-lazy pointer list may have captured references to the
+     old encoded name, change them.  */
+  if (TREE_CODE (decl) == VAR_DECL)
+    update_non_lazy_ptrs (XSTR (sym_ref, 0));
+  else
+    update_stubs (XSTR (sym_ref, 0));
+}
+
+/* Scan the list of non-lazy pointers and update any recorded names whose
+   stripped name matches the argument.  */
+
+static void
+update_non_lazy_ptrs (name)
+     const char *name;
+{
+  const char *name1, *name2;
+  tree temp;
+
+  STRIP_NAME_ENCODING (name1, name);
+
+  for (temp = machopic_non_lazy_pointers;
+       temp != NULL_TREE; 
+       temp = TREE_CHAIN (temp))
+    {
+      char *sym_name = IDENTIFIER_POINTER (TREE_VALUE (temp));
+
+      if (*sym_name == '!')
+       {
+         STRIP_NAME_ENCODING (name2, sym_name);
+         if (strcmp (name1, name2) == 0)
+           {
+             IDENTIFIER_POINTER (TREE_VALUE (temp)) = name;
+             break;
+           }
+       }
+    }
+}
+
+/* Function NAME is being defined, and its label has just been output.
+   If there's already a reference to a stub for this function, we can
+   just emit the stub label now and we don't bother emitting the stub later. */
+
+void
+machopic_output_possible_stub_label (file, name)
+     FILE *file;
+     const char *name;
+{
+  tree temp;
+
+
+  /* Ensure we're looking at a section-encoded name.  */
+  if (name[0] != '!' || (name[1] != 't' && name[1] != 'T'))
+    return;
+
+  for (temp = machopic_stubs;
+       temp != NULL_TREE;
+       temp = TREE_CHAIN (temp))
+    {
+      const char *sym_name;
+
+      sym_name = IDENTIFIER_POINTER (TREE_VALUE (temp));
+      if (sym_name[0] == '!' && sym_name[1] == 'T'
+         && ! strcmp (name+2, sym_name+2))
+       {
+         ASM_OUTPUT_LABEL (file, IDENTIFIER_POINTER (TREE_PURPOSE (temp)));
+         /* Avoid generating a stub for this.  */
+         TREE_USED (temp) = 0;
+         break;
+       }
+    }
+}
+
+/* Scan the list of stubs and update any recorded names whose
+   stripped name matches the argument.  */
+
+static void
+update_stubs (name)
+     const char *name;
+{
+  const char *name1, *name2;
+  tree temp;
+
+  STRIP_NAME_ENCODING (name1, name);
+
+  for (temp = machopic_stubs;
+       temp != NULL_TREE; 
+       temp = TREE_CHAIN (temp))
+    {
+      char *sym_name = IDENTIFIER_POINTER (TREE_VALUE (temp));
+
+      if (*sym_name == '!')
+       {
+         STRIP_NAME_ENCODING (name2, sym_name);
+         if (strcmp (name1, name2) == 0)
+           {
+             IDENTIFIER_POINTER (TREE_VALUE (temp)) = name;
+             break;
+           }
+       }
+    }
+}
+
+void
+machopic_asm_out_constructor (symbol, priority)
+     rtx symbol;
+     int priority ATTRIBUTE_UNUSED;
+{
+  if (flag_pic)
+    mod_init_section ();
+  else
+    constructor_section ();
+  assemble_align (POINTER_SIZE);
+  assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
+
+  if (!flag_pic)
+    fprintf (asm_out_file, ".reference .constructors_used\n");
+}
+
+void
+machopic_asm_out_destructor (symbol, priority)
+     rtx symbol;
+     int priority ATTRIBUTE_UNUSED;
+{
+  if (flag_pic)
+    mod_term_section ();
+  else
+    destructor_section ();
+  assemble_align (POINTER_SIZE);
+  assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
+
+  if (!flag_pic)
+    fprintf (asm_out_file, ".reference .destructors_used\n");
+}