OSDN Git Service

Introduce G structure and thread-local global g.
[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   struct __go_defer_stack *d;
22   const char* ret;
23   const char* dret;
24
25   d = g->defer;
26   if (d == NULL)
27     return 0;
28
29   /* The panic which this function would recover is the one on the top
30      of the panic stack.  We do not want to recover it if that panic
31      was on the top of the panic stack when this function was
32      deferred.  */
33   if (d->__panic == g->panic)
34     return 0;
35
36   /* D->__RETADDR is the address of a label immediately following the
37      call to the thunk.  We can recover a panic if that is the same as
38      the return address of the thunk.  We permit a bit of slack in
39      case there is any code between the function return and the label,
40      such as an instruction to adjust the stack pointer.  */
41
42   ret = (const char *) retaddr;
43   dret = (const char *) d->__retaddr;
44   return ret <= dret && ret + 16 >= dret;
45 }
46
47 /* This is only called when it is valid for the caller to recover the
48    value on top of the panic stack, if there is one.  */
49
50 struct __go_empty_interface
51 __go_recover ()
52 {
53   struct __go_panic_stack *p;
54
55   if (g->panic == NULL || g->panic->__was_recovered)
56     {
57       struct __go_empty_interface ret;
58
59       ret.__type_descriptor = NULL;
60       ret.__object = NULL;
61       return ret;
62     }
63   p = g->panic;
64   p->__was_recovered = 1;
65   return p->__arg;
66 }