OSDN Git Service

Add comment saying file is deprecated
[pf3gnuchains/gcc-fork.git] / libobjc / exception.c
index d788c41..0ac0b66 100644 (file)
@@ -1,11 +1,11 @@
 /* The implementation of exception handling primitives for Objective-C.
-   Copyright (C) 2004 Free Software Foundation, Inc.
+   Copyright (C) 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
 
 This file is part of GCC.
 
 GCC is free software; you can redistribute it and/or modify it
 under the terms of the GNU General Public License as published by the
-Free Software Foundation; either version 2, or (at your option) any
+Free Software Foundation; either version 3, or (at your option) any
 later version.
 
 GCC is distributed in the hope that it will be useful, but WITHOUT
@@ -13,33 +13,41 @@ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
 License for more details.
 
-You should have received a copy of the GNU General Public License
-along with GCC; see the file COPYING.  If not, write to
-the Free Software Foundation, 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+Under Section 7 of GPL version 3, you are granted additional
+permissions described in the GCC Runtime Library Exception, version
+3.1, as published by the Free Software Foundation.
 
-/* As a special exception, if you link this library with files compiled
-   with GCC to produce an executable, this does not cause the resulting
-   executable to be covered by the GNU General Public License.  This
-   exception does not however invalidate any other reasons why the
-   executable file might be covered by the GNU General Public License. */
+You should have received a copy of the GNU General Public License and
+a copy of the GCC Runtime Library Exception along with this program;
+see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+<http://www.gnu.org/licenses/>.  */
 
 #include <stdlib.h>
+#include "config.h"
 #include "objc/objc-api.h"
 #include "unwind.h"
 #include "unwind-pe.h"
 
 \f
+#ifdef __ARM_EABI_UNWINDER__
+
+const _Unwind_Exception_Class __objc_exception_class
+  = {'G', 'N', 'U', 'C', 'O', 'B', 'J', 'C'};
+  
+#else
+
 /* This is the exception class we report -- "GNUCOBJC".  */
-#define __objc_exception_class                 \
-  ((((((((_Unwind_Exception_Class) 'G'         \
-         << 8 | (_Unwind_Exception_Class) 'N') \
-        << 8 | (_Unwind_Exception_Class) 'U')  \
-       << 8 | (_Unwind_Exception_Class) 'C')   \
-      << 8 | (_Unwind_Exception_Class) 'O')    \
-     << 8 | (_Unwind_Exception_Class) 'B')     \
-    << 8 | (_Unwind_Exception_Class) 'J')      \
-   << 8 | (_Unwind_Exception_Class) 'C')
+static const _Unwind_Exception_Class __objc_exception_class
+  = ((((((((_Unwind_Exception_Class) 'G'
+            << 8 | (_Unwind_Exception_Class) 'N')
+           << 8 | (_Unwind_Exception_Class) 'U')
+          << 8 | (_Unwind_Exception_Class) 'C')
+         << 8 | (_Unwind_Exception_Class) 'O')
+        << 8 | (_Unwind_Exception_Class) 'B')
+       << 8 | (_Unwind_Exception_Class) 'J')
+      << 8 | (_Unwind_Exception_Class) 'C');
+
+#endif
 
 /* This is the object that is passed around by the Objective C runtime
    to represent the exception in flight.  */
@@ -49,12 +57,18 @@ struct ObjcException
   /* This bit is needed in order to interact with the unwind runtime.  */
   struct _Unwind_Exception base;
 
-  /* The actual object we want to throw.  */
+  /* The actual object we want to throw. Note: must come immediately after
+     unwind header.  */
   id value;
 
+#ifdef __ARM_EABI_UNWINDER__
+  /* Note: we use the barrier cache defined in the unwind control block for
+     ARM EABI.  */
+#else
   /* Cache some internal unwind data between phase 1 and phase 2.  */
   _Unwind_Ptr landingPad;
   int handlerSwitchValue;
+#endif
 };
 
 \f
@@ -70,11 +84,16 @@ struct lsda_header_info
   unsigned char call_site_encoding;
 };
 
+/* This hook allows libraries to sepecify special actions when an
+   exception is thrown without a handler in place.
+ */
+void (*_objc_unexpected_exception) (id exception); /* !T:SAFE */
+
 static const unsigned char *
 parse_lsda_header (struct _Unwind_Context *context, const unsigned char *p,
                   struct lsda_header_info *info)
 {
-  _Unwind_Word tmp;
+  _uleb128_t tmp;
   unsigned char lpstart_encoding;
 
   info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
@@ -105,6 +124,24 @@ parse_lsda_header (struct _Unwind_Context *context, const unsigned char *p,
   return p;
 }
 
+#ifdef __ARM_EABI_UNWINDER__
+
+static Class
+get_ttype_entry (struct lsda_header_info *info, _uleb128_t i)
+{
+  _Unwind_Ptr ptr;
+  
+  ptr = (_Unwind_Ptr) (info->TType - (i * 4));
+  ptr = _Unwind_decode_target2 (ptr);
+  
+  if (ptr)
+    return objc_get_class ((const char *) ptr);
+  else
+    return 0;
+}
+
+#else
+
 static Class
 get_ttype_entry (struct lsda_header_info *info, _Unwind_Word i)
 {
@@ -121,6 +158,8 @@ get_ttype_entry (struct lsda_header_info *info, _Unwind_Word i)
     return 0;
 }
 
+#endif
+
 /* Like unto the method of the same name on Object, but takes an id.  */
 /* ??? Does this bork the meta-type system?  Can/should we look up an
    isKindOf method on the id?  */
@@ -149,12 +188,32 @@ isKindOf (id value, Class target)
 #define PERSONALITY_FUNCTION   __gnu_objc_personality_v0
 #endif
 
+#ifdef __ARM_EABI_UNWINDER__
+
+#define CONTINUE_UNWINDING \
+  do                                                           \
+    {                                                          \
+      if (__gnu_unwind_frame(ue_header, context) != _URC_OK)   \
+       return _URC_FAILURE;                                    \
+      return _URC_CONTINUE_UNWIND;                             \
+    }                                                          \
+  while (0)
+
+_Unwind_Reason_Code
+PERSONALITY_FUNCTION (_Unwind_State state,
+                     struct _Unwind_Exception *ue_header,
+                     struct _Unwind_Context *context)
+#else
+
+#define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
+
 _Unwind_Reason_Code
 PERSONALITY_FUNCTION (int version,
                      _Unwind_Action actions,
                      _Unwind_Exception_Class exception_class,
                      struct _Unwind_Exception *ue_header,
                      struct _Unwind_Context *context)
+#endif
 {
   struct ObjcException *xh = (struct ObjcException *) ue_header;
 
@@ -164,18 +223,65 @@ PERSONALITY_FUNCTION (int version,
   const unsigned char *p;
   _Unwind_Ptr landing_pad, ip;
   int handler_switch_value;
-  int saw_cleanup, saw_handler;
+  int saw_cleanup = 0, saw_handler, foreign_exception;
+  void *return_object;
+  int ip_before_insn = 0;
+
+#ifdef __ARM_EABI_UNWINDER__
+  _Unwind_Action actions;
+  
+  switch (state & _US_ACTION_MASK)
+    {
+    case _US_VIRTUAL_UNWIND_FRAME:
+      actions = _UA_SEARCH_PHASE;
+      break;
+
+    case _US_UNWIND_FRAME_STARTING:
+      actions = _UA_CLEANUP_PHASE;
+      if (!(state & _US_FORCE_UNWIND)
+         && ue_header->barrier_cache.sp == _Unwind_GetGR (context, 13))
+       actions |= _UA_HANDLER_FRAME;
+      break;
+
+    case _US_UNWIND_FRAME_RESUME:
+      CONTINUE_UNWINDING;
+      break;
+
+    default:
+      abort();
+    }
+  actions |= state & _US_FORCE_UNWIND;
+
+  /* TODO: Foreign exceptions need some attention (e.g. rethrowing doesn't
+     work).  */
+  foreign_exception = 0;
 
+  /* The dwarf unwinder assumes the context structure holds things like the
+     function and LSDA pointers.  The ARM implementation caches these in
+     the exception header (UCB).  To avoid rewriting everything we make the
+     virtual IP register point at the UCB.  */
+  ip = (_Unwind_Ptr) ue_header;
+  _Unwind_SetGR (context, 12, ip);
+
+#else  /* !__ARM_EABI_UNWINDER.  */
   /* Interface version check.  */
   if (version != 1)
     return _URC_FATAL_PHASE1_ERROR;
+  
+  foreign_exception = (exception_class != __objc_exception_class);
+#endif
 
   /* Shortcut for phase 2 found handler for domestic exception.  */
   if (actions == (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME)
-      && exception_class == __objc_exception_class)
+      && !foreign_exception)
     {
+#ifdef __ARM_EABI_UNWINDER__
+      handler_switch_value = (int) ue_header->barrier_cache.bitpattern[1];
+      landing_pad = (_Unwind_Ptr) ue_header->barrier_cache.bitpattern[3];
+#else
       handler_switch_value = xh->handlerSwitchValue;
       landing_pad = xh->landingPad;
+#endif
       goto install_context;
     }
 
@@ -184,12 +290,18 @@ PERSONALITY_FUNCTION (int version,
 
   /* If no LSDA, then there are no handlers or cleanups.  */
   if (! language_specific_data)
-    return _URC_CONTINUE_UNWIND;
+    CONTINUE_UNWINDING;
 
   /* Parse the LSDA header.  */
   p = parse_lsda_header (context, language_specific_data, &info);
   info.ttype_base = base_of_encoded_value (info.ttype_encoding, context);
-  ip = _Unwind_GetIP (context) - 1;
+#ifdef HAVE_GETIPINFO
+  ip = _Unwind_GetIPInfo (context, &ip_before_insn);
+#else
+  ip = _Unwind_GetIP (context);
+#endif
+  if (!ip_before_insn)
+    --ip;
   landing_pad = 0;
   action_record = 0;
   handler_switch_value = 0;
@@ -203,7 +315,7 @@ PERSONALITY_FUNCTION (int version,
     return _URC_CONTINUE_UNWIND;
   else
     {
-      _Unwind_Word cs_lp, cs_action;
+      _uleb128_t cs_lp, cs_action;
       do
        {
          p = read_uleb128 (p, &cs_lp);
@@ -223,7 +335,7 @@ PERSONALITY_FUNCTION (int version,
   while (p < info.action_table)
     {
       _Unwind_Ptr cs_start, cs_len, cs_lp;
-      _Unwind_Word cs_action;
+      _uleb128_t cs_action;
 
       /* Note that all call-site encodings are "absolute" displacements.  */
       p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
@@ -248,7 +360,7 @@ PERSONALITY_FUNCTION (int version,
   /* If ip is not present in the table, C++ would call terminate.  */
   /* ??? As with Java, it's perhaps better to tweek the LSDA to
      that no-action is mapped to no-entry.  */
-  return _URC_CONTINUE_UNWIND;
+  CONTINUE_UNWINDING;
 
  found_something:
   saw_cleanup = 0;
@@ -269,7 +381,7 @@ PERSONALITY_FUNCTION (int version,
   else
     {
       /* Otherwise we have a catch handler.  */
-      _Unwind_Sword ar_filter, ar_disp;
+      _sleb128_t ar_filter, ar_disp;
 
       while (1)
        {
@@ -285,8 +397,7 @@ PERSONALITY_FUNCTION (int version,
 
          /* During forced unwinding, we only run cleanups.  With a
             foreign exception class, we have no class info to match.  */
-         else if ((actions & _UA_FORCE_UNWIND)
-                  || exception_class != __objc_exception_class)
+         else if ((actions & _UA_FORCE_UNWIND) || foreign_exception)
            ;
 
          else if (ar_filter > 0)
@@ -316,25 +427,38 @@ PERSONALITY_FUNCTION (int version,
     }
 
   if (! saw_handler && ! saw_cleanup)
-    return _URC_CONTINUE_UNWIND;
+    CONTINUE_UNWINDING;
 
   if (actions & _UA_SEARCH_PHASE)
     {
       if (!saw_handler)
-       return _URC_CONTINUE_UNWIND;
+       CONTINUE_UNWINDING;
 
       /* For domestic exceptions, we cache data from phase 1 for phase 2.  */
-      if (exception_class == __objc_exception_class)
+      if (!foreign_exception)
         {
+#ifdef __ARM_EABI_UNWINDER__
+         ue_header->barrier_cache.sp = _Unwind_GetGR (context, 13);
+         ue_header->barrier_cache.bitpattern[1] = (_uw) handler_switch_value;
+         ue_header->barrier_cache.bitpattern[3] = (_uw) landing_pad;
+#else
           xh->handlerSwitchValue = handler_switch_value;
           xh->landingPad = landing_pad;
+#endif
        }
       return _URC_HANDLER_FOUND;
     }
 
  install_context:
+  if (saw_cleanup == 0)
+    {
+      return_object = xh->value;
+      if (!(actions & _UA_SEARCH_PHASE))
+       _Unwind_DeleteException(&xh->base);
+    }
+  
   _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
-                __builtin_extend_pointer (xh->value));
+                __builtin_extend_pointer (saw_cleanup ? xh : return_object));
   _Unwind_SetGR (context, __builtin_eh_return_data_regno (1),
                 handler_switch_value);
   _Unwind_SetIP (context, landing_pad);
@@ -352,16 +476,22 @@ void
 objc_exception_throw (id value)
 {
   struct ObjcException *header = calloc (1, sizeof (*header));
-  header->base.exception_class = __objc_exception_class;
+  
+  memcpy (&header->base.exception_class, &__objc_exception_class,
+         sizeof (__objc_exception_class));
   header->base.exception_cleanup = __objc_exception_cleanup;
   header->value = value;
 
-#ifdef _GLIBCXX_SJLJ_EXCEPTIONS
+#ifdef SJLJ_EXCEPTIONS
   _Unwind_SjLj_RaiseException (&header->base);
 #else
   _Unwind_RaiseException (&header->base);
 #endif
 
   /* Some sort of unwinding error.  */
+  if (_objc_unexpected_exception != 0)
+    {
+      (*_objc_unexpected_exception) (value);
+    }
   abort ();
 }