OSDN Git Service

* reorg.c (fill_slots_from_thread): Check side_effects_p when
[pf3gnuchains/gcc-fork.git] / gcc / frame.c
index 4b62759..cfd979b 100644 (file)
@@ -1,6 +1,6 @@
 /* Subroutines needed for unwinding stack frames for exception handling.  */
 /* Compile this one with gcc.  */
-/* Copyright (C) 1997 Free Software Foundation, Inc.
+/* Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
    Contributed by Jason Merrill <jason@cygnus.com>.
 
 This file is part of GNU CC.
@@ -32,23 +32,12 @@ Boston, MA 02111-1307, USA.  */
    do not apply.  */
 
 #include "tconfig.h"
-
-/* We disable this when inhibit_libc, so that gcc can still be built without
-   needing header files first.  */
-/* ??? This is not a good solution, since prototypes may be required in
-   some cases for correct code.  See also libgcc2.c.  */
-#ifndef inhibit_libc
-/* fixproto guarantees these system headers exist. */
-#include <stdlib.h>
-#include <unistd.h>
-#endif
+#include "tsystem.h"
 
 #include "defaults.h"
 
 #ifdef DWARF2_UNWIND_INFO
-#include "gansidecl.h"
 #include "dwarf2.h"
-#include <stddef.h>
 #include "frame.h"
 #include "gthr.h"
 
@@ -71,6 +60,25 @@ typedef unsigned int  uaddr __attribute__ ((mode (pointer)));
 typedef          int  saddr __attribute__ ((mode (pointer)));
 typedef unsigned char ubyte;
 
+/* Terminology:
+   CIE - Common Information Element
+   FDE - Frame Descriptor Element
+
+   There is one per function, and it describes where the function code
+   is located, and what the register lifetimes and stack layout are
+   within the function.
+
+   The data structures are defined in the DWARF specfication, although
+   not in a very readable way (see LITERATURE).
+
+   Every time an exception is thrown, the code needs to locate the FDE
+   for the current function, and starts to look for exception regions
+   from that FDE. This works in a two-level search:
+   a) in a linear search, find the shared image (i.e. DLL) containing
+      the PC
+   b) using the FDE table for that shared object, locate the FDE using
+      binary search (which requires the sorting).  */   
+
 /* The first few fields of a CIE.  The CIE_id field is 0 for a CIE,
    to distinguish it from a valid FDE.  FDEs are aligned to an addressing
    unit boundary, but the fields within are unaligned.  */
@@ -114,6 +122,33 @@ struct frame_state_internal
   struct frame_state s;
   struct frame_state_internal *saved_state;
 };
+\f
+/* This is undefined below if we need it to be an actual function.  */
+#define init_object_mutex_once()
+
+#if __GTHREADS
+#ifdef __GTHREAD_MUTEX_INIT_FUNCTION
+
+/* Helper for init_object_mutex_once.  */
+
+static void
+init_object_mutex (void)
+{
+  __GTHREAD_MUTEX_INIT_FUNCTION (&object_mutex);
+}
+
+/* Call this to arrange to initialize the object mutex.  */
+
+#undef init_object_mutex_once
+static void
+init_object_mutex_once (void)
+{
+  static __gthread_once_t once = __GTHREAD_ONCE_INIT;
+  __gthread_once (&once, init_object_mutex);
+}
+
+#endif /* __GTHREAD_MUTEX_INIT_FUNCTION */
+#endif /* __GTHREADS */
 \f  
 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
    by R, and return the new value of BUF.  */
@@ -229,50 +264,75 @@ typedef struct fde_accumulator
   fde_vector erratic;
 } fde_accumulator;
 
-static inline void
+static inline int
 start_fde_sort (fde_accumulator *accu, size_t count)
 {
   accu->linear.array = (fde **) malloc (sizeof (fde *) * count);
-  accu->erratic.array = (fde **) malloc (sizeof (fde *) * count);
+  accu->erratic.array = accu->linear.array ?
+      (fde **) malloc (sizeof (fde *) * count) : NULL;
   accu->linear.count = 0;
   accu->erratic.count = 0;
+  
+  return accu->linear.array != NULL;
 }
 
 static inline void
 fde_insert (fde_accumulator *accu, fde *this_fde)
 {
-  accu->linear.array[accu->linear.count++] = this_fde;
+  if (accu->linear.array)
+    accu->linear.array[accu->linear.count++] = this_fde;
 }
 
 /* Split LINEAR into a linear sequence with low values and an erratic
    sequence with high values, put the linear one (of longest possible
-   length) into LINEAR and the erratic one into ERRATIC. This is O(N).  */
+   length) into LINEAR and the erratic one into ERRATIC. This is O(N).
+   
+   Because the longest linear sequence we are trying to locate within the
+   incoming LINEAR array can be interspersed with (high valued) erratic
+   entries.  We construct a chain indicating the sequenced entries.
+   To avoid having to allocate this chain, we overlay it onto the space of
+   the ERRATIC array during construction.  A final pass iterates over the
+   chain to determine what should be placed in the ERRATIC array, and
+   what is the linear sequence.  This overlay is safe from aliasing.  */
 static inline void
 fde_split (fde_vector *linear, fde_vector *erratic)
 {
+  static fde *marker;
   size_t count = linear->count;
-  size_t linear_max = (size_t) -1;
-  size_t previous_max[count];
-  size_t i, j;
+  fde **chain_end = &marker;
+  size_t i, j, k;
 
+  /* This should optimize out, but it is wise to make sure this assumption
+     is correct. Should these have different sizes, we cannot cast between
+     them and the overlaying onto ERRATIC will not work.  */
+  if (sizeof (fde *) != sizeof (fde **))
+    abort ();
+  
   for (i = 0; i < count; i++)
     {
-      for (j = linear_max;
-           j != (size_t) -1
-           && fde_compare (linear->array[i], linear->array[j]) < 0;
-           j = previous_max[j])
+      fde **probe;
+      
+      for (probe = chain_end;
+           probe != &marker && fde_compare (linear->array[i], *probe) < 0;
+           probe = chain_end)
         {
-          erratic->array[erratic->count++] = linear->array[j];
-          linear->array[j] = (fde *) NULL;
+          chain_end = (fde **)erratic->array[probe - linear->array];
+          erratic->array[probe - linear->array] = NULL;
         }
-      previous_max[i] = j;
-      linear_max = i;
+      erratic->array[i] = (fde *)chain_end;
+      chain_end = &linear->array[i];
     }
 
-  for (i = 0, j = 0; i < count; i++)
-    if (linear->array[i] != (fde *) NULL)
+  /* Each entry in LINEAR which is part of the linear sequence we have
+     discovered will correspond to a non-NULL entry in the chain we built in
+     the ERRATIC array.  */
+  for (i = j = k = 0; i < count; i++)
+    if (erratic->array[i])
       linear->array[j++] = linear->array[i];
+    else
+      erratic->array[k++] = linear->array[i];
   linear->count = j;
+  erratic->count = k;
 }
 
 /* This is O(n log(n)).  BSD/OS defines heapsort in stdlib.h, so we must
@@ -368,14 +428,25 @@ fde_merge (fde_vector *v1, const fde_vector *v2)
 static fde **
 end_fde_sort (fde_accumulator *accu, size_t count)
 {
-  if (accu->linear.count != count)
-    abort ();
-  fde_split (&accu->linear, &accu->erratic);
-  if (accu->linear.count + accu->erratic.count != count)
+  if (accu->linear.array && accu->linear.count != count)
     abort ();
-  frame_heapsort (&accu->erratic);
-  fde_merge (&accu->linear, &accu->erratic);
-  free (accu->erratic.array);
+  
+  if (accu->erratic.array)
+    {
+      fde_split (&accu->linear, &accu->erratic);
+      if (accu->linear.count + accu->erratic.count != count)
+       abort ();
+      frame_heapsort (&accu->erratic);
+      fde_merge (&accu->linear, &accu->erratic);
+      if (accu->erratic.array)
+        free (accu->erratic.array);
+    }
+  else
+    {
+      /* We've not managed to malloc an erratic array, so heap sort in the
+         linear one.  */
+      frame_heapsort (&accu->linear);
+    }
   return accu->linear.array;
 }
 
@@ -420,9 +491,26 @@ add_fdes (fde *this_fde, fde_accumulator *accu, void **beg_ptr, void **end_ptr)
   *end_ptr = pc_end;
 }
 
+/* search this fde table for the one containing the pc */
+static fde *
+search_fdes (fde *this_fde, void *pc)
+{
+  for (; this_fde->length != 0; this_fde = next_fde (this_fde))
+    {
+      /* Skip CIEs and linked once FDE entries.  */
+      if (this_fde->CIE_delta == 0 || this_fde->pc_begin == 0)
+       continue;
+
+      if ((uaddr)((char *)pc - (char *)this_fde->pc_begin) < this_fde->pc_range)
+       return this_fde;
+    }
+  return NULL;
+}
+
 /* Set up a sorted array of pointers to FDEs for a loaded object.  We
    count up the entries before allocating the array because it's likely to
-   be faster.  */
+   be faster.  We can be called multiple times, should we have failed to
+   allocate a sorted fde array on a previous occasion.  */
 
 static void
 frame_init (struct object* ob)
@@ -430,8 +518,11 @@ frame_init (struct object* ob)
   size_t count;
   fde_accumulator accu;
   void *pc_begin, *pc_end;
+  fde **array;
 
-  if (ob->fde_array)
+  if (ob->pc_begin)
+    count = ob->count;
+  else if (ob->fde_array)
     {
       fde **p = ob->fde_array;
       for (count = 0; *p; ++p)
@@ -439,10 +530,11 @@ frame_init (struct object* ob)
     }
   else
     count = count_fdes (ob->fde_begin);
-
   ob->count = count;
 
-  start_fde_sort (&accu, count);
+  if (!start_fde_sort (&accu, count) && ob->pc_begin)
+    return;
+
   pc_begin = (void*)(uaddr)-1;
   pc_end = 0;
 
@@ -455,7 +547,9 @@ frame_init (struct object* ob)
   else
     add_fdes (ob->fde_begin, &accu, &pc_begin, &pc_end);
 
-  ob->fde_array = end_fde_sort (&accu, count);
+  array = end_fde_sort (&accu, count);
+  if (array)
+    ob->fde_array = array;
   ob->pc_begin = pc_begin;
   ob->pc_end = pc_end;
 }
@@ -468,8 +562,10 @@ find_fde (void *pc)
   struct object *ob;
   size_t lo, hi;
 
+  init_object_mutex_once ();
   __gthread_mutex_lock (&object_mutex);
 
+  /* Linear search through the objects, to find the one containing the pc. */
   for (ob = objects; ob; ob = ob->next)
     {
       if (ob->pc_begin == 0)
@@ -478,25 +574,56 @@ find_fde (void *pc)
        break;
     }
 
-  __gthread_mutex_unlock (&object_mutex);
-
   if (ob == 0)
-    return 0;
-
-  /* Standard binary search algorithm.  */
-  for (lo = 0, hi = ob->count; lo < hi; )
     {
-      size_t i = (lo + hi) / 2;
-      fde *f = ob->fde_array[i];
+      __gthread_mutex_unlock (&object_mutex);
+      return 0;
+    }
+
+  if (!ob->fde_array || (void *)ob->fde_array == (void *)ob->fde_begin)
+    frame_init (ob);
 
-      if (pc < f->pc_begin)
-       hi = i;
-      else if (pc >= f->pc_begin + f->pc_range)
-       lo = i + 1;
+  if (ob->fde_array && (void *)ob->fde_array != (void *)ob->fde_begin)
+    {
+      __gthread_mutex_unlock (&object_mutex);
+      
+      /* Standard binary search algorithm.  */
+      for (lo = 0, hi = ob->count; lo < hi; )
+       {
+         size_t i = (lo + hi) / 2;
+         fde *f = ob->fde_array[i];
+
+         if (pc < f->pc_begin)
+           hi = i;
+         else if (pc >= f->pc_begin + f->pc_range)
+           lo = i + 1;
+         else
+           return f;
+       }
+    }
+  else
+    {
+      /* Long slow labourious linear search, cos we've no memory. */
+      fde *f;
+      
+      if (ob->fde_array)
+       {
+         fde **p = ob->fde_array;
+         
+         do
+           {
+             f = search_fdes (*p, pc);
+             if (f)
+               break;
+             p++;
+           }
+         while (*p);
+       }
       else
-       return f;
+       f = search_fdes (ob->fde_begin, pc);
+      __gthread_mutex_unlock (&object_mutex);
+      return f;
     }
-
   return 0;
 }
 \f
@@ -567,9 +694,16 @@ execute_cfa_insn (void *p, struct frame_state_internal *state,
     {
       reg = (insn & 0x3f);
       p = decode_uleb128 (p, &offset);
-      offset *= info->data_align;
-      state->s.saved[reg] = REG_SAVED_OFFSET;
-      state->s.reg_or_offset[reg] = offset;
+      if (reg == state->s.cfa_reg)
+       /* Don't record anything about this register; it's only used to
+          reload SP in the epilogue.  We don't want to copy in SP
+          values for outer frames; we handle restoring SP specially.  */;
+      else
+       {
+         offset *= info->data_align;
+         state->s.saved[reg] = REG_SAVED_OFFSET;
+         state->s.reg_or_offset[reg] = offset;
+       }
     }
   else if (insn & DW_CFA_restore)
     {
@@ -598,9 +732,14 @@ execute_cfa_insn (void *p, struct frame_state_internal *state,
     case DW_CFA_offset_extended:
       p = decode_uleb128 (p, &reg);
       p = decode_uleb128 (p, &offset);
-      offset *= info->data_align;
-      state->s.saved[reg] = REG_SAVED_OFFSET;
-      state->s.reg_or_offset[reg] = offset;
+      if (reg == state->s.cfa_reg)
+       /* Don't record anything; see above.  */;
+      else
+       {
+         offset *= info->data_align;
+         state->s.saved[reg] = REG_SAVED_OFFSET;
+         state->s.reg_or_offset[reg] = offset;
+       }
       break;
     case DW_CFA_restore_extended:
       p = decode_uleb128 (p, &reg);
@@ -668,6 +807,14 @@ execute_cfa_insn (void *p, struct frame_state_internal *state,
       state->s.args_size = offset;
       break;
 
+    case DW_CFA_GNU_negative_offset_extended:
+      p = decode_uleb128 (p, &reg);
+      p = decode_uleb128 (p, &offset);
+      offset *= info->data_align;
+      state->s.saved[reg] = REG_SAVED_OFFSET;
+      state->s.reg_or_offset[reg] = -offset;
+      break;
+
     default:
       abort ();
     }
@@ -685,6 +832,7 @@ __register_frame_info (void *begin, struct object *ob)
   ob->fde_array = 0;
   ob->count = 0;
 
+  init_object_mutex_once ();
   __gthread_mutex_lock (&object_mutex);
 
   ob->next = objects;
@@ -713,6 +861,7 @@ __register_frame_info_table (void *begin, struct object *ob)
   ob->pc_begin = ob->pc_end = 0;
   ob->count = 0;
 
+  init_object_mutex_once ();
   __gthread_mutex_lock (&object_mutex);
 
   ob->next = objects;
@@ -735,6 +884,7 @@ __deregister_frame_info (void *begin)
 {
   struct object **p;
 
+  init_object_mutex_once ();
   __gthread_mutex_lock (&object_mutex);
 
   p = &objects;
@@ -746,7 +896,7 @@ __deregister_frame_info (void *begin)
          *p = (*p)->next;
 
          /* If we've run init_frame for this object, free the FDE array.  */
-         if (ob->pc_begin)
+         if (ob->fde_array && ob->fde_array != begin)
            free (ob->fde_array);
 
          __gthread_mutex_unlock (&object_mutex);