OSDN Git Service

libgo: Solaris and Irix compatibility patches.
[pf3gnuchains/gcc-fork.git] / libgo / runtime / go-recover.c
1 /* go-recover.c -- support for the go recover function.
2
3    Copyright 2010 The Go Authors. All rights reserved.
4    Use of this source code is governed by a BSD-style
5    license that can be found in the LICENSE file.  */
6
7 #include "runtime.h"
8 #include "interface.h"
9 #include "go-panic.h"
10 #include "go-defer.h"
11
12 /* This is called by a thunk to see if the real function should be
13    permitted to recover a panic value.  Recovering a value is
14    permitted if the thunk was called directly by defer.  RETADDR is
15    the return address of the function which is calling
16    __go_can_recover--this is, the thunk.  */
17
18 _Bool
19 __go_can_recover (const void* retaddr)
20 {
21   G *g;
22   struct __go_defer_stack *d;
23   const char* ret;
24   const char* dret;
25
26   g = runtime_g ();
27
28   d = g->defer;
29   if (d == NULL)
30     return 0;
31
32   /* The panic which this function would recover is the one on the top
33      of the panic stack.  We do not want to recover it if that panic
34      was on the top of the panic stack when this function was
35      deferred.  */
36   if (d->__panic == g->panic)
37     return 0;
38
39   /* D->__RETADDR is the address of a label immediately following the
40      call to the thunk.  We can recover a panic if that is the same as
41      the return address of the thunk.  We permit a bit of slack in
42      case there is any code between the function return and the label,
43      such as an instruction to adjust the stack pointer.  */
44
45   ret = (const char *) retaddr;
46   dret = (const char *) d->__retaddr;
47   return ret <= dret && ret + 16 >= dret;
48 }
49
50 /* This is only called when it is valid for the caller to recover the
51    value on top of the panic stack, if there is one.  */
52
53 struct __go_empty_interface
54 __go_recover ()
55 {
56   G *g;
57   struct __go_panic_stack *p;
58
59   g = runtime_g ();
60
61   if (g->panic == NULL || g->panic->__was_recovered)
62     {
63       struct __go_empty_interface ret;
64
65       ret.__type_descriptor = NULL;
66       ret.__object = NULL;
67       return ret;
68     }
69   p = g->panic;
70   p->__was_recovered = 1;
71   return p->__arg;
72 }