OSDN Git Service

- update comment to clarify that this file is specific to AIX and 32-bit mode.
[pf3gnuchains/gcc-fork.git] / libchill / exh.c
1 /* Implement runtime actions for CHILL.
2    Copyright (C) 1992,1993 Free Software Foundation, Inc.
3    Author: Wilfried Moser
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 /* As a special exception, if you link this library with other files,
22    some of which are compiled with GCC, to produce an executable,
23    this library does not by itself cause the resulting executable
24    to be covered by the GNU General Public License.
25    This exception does not however invalidate any other reasons why
26    the executable file might be covered by the GNU General Public License.  */
27
28
29 #define __CHILL_LIB__
30
31 #include <stdio.h>
32 #include <setjmp.h>
33 #include "rtltypes.h"
34
35 extern void cause_exception (char *exname, char *file, int lineno, int user_arg);
36 extern void unhandled_exception (char *exname, char *file, int lineno, int user_arg);
37
38 /* An action with a handler:
39         BODY ON (e1, e2): H12; (e3): H3; ELSE HE; END;
40    is translated into:
41
42     struct __ch_handler __tmp;
43     static struct __ch_handler_excepts _H[4] =
44       {
45         { <<e1>>, 1 },
46         { <<e2>>, 1 },
47         { <<e3>>, 2 },
48         { __ch_else_except, 3 },
49       };
50     __ch_link_handler(&__tmp);
51     __tmp.handlers = _H;
52     switch (setmp(&__tmp.jbuf))
53       { 
54       case 0:  BODY; __ch_unlink_handler(&__tmp); break;
55       case 1:  H12; break;
56       case 2:  H3; break;
57       case 3:  HE; break;
58       }
59 */
60
61 /* this part contains all neccessary functions to handle exceptions in CHILL */
62
63 /* These two trivial function aren't inlines, to allow for
64    more flexibility (e.g. a per-thread exception stack). */
65
66 extern void __setexceptionStack (TExceptionHandlerStack *new);
67 extern TExceptionHandlerStack * __getexceptionStack (void);
68
69 void
70 __ch_link_handler (handler)
71      struct __ch_handler *handler;
72 {
73   handler->prev = __getexceptionStack ();
74   __setexceptionStack (handler);
75 }
76
77  void
78 __ch_unlink_handler (handler)
79      struct __ch_handler *handler;
80 {
81   __setexceptionStack (handler->prev);
82 }
83
84 /*
85  * function __cause_exception
86  *
87  * parameters:
88  *  exnum               name string of exception to raise
89  *  file                filename of CAUSE statement
90  *  lineno              linenumber of CAUSE statement
91  *  user_arg            user specified argument
92  *
93  * returns:
94  *  never               leave function with longjmp or abort
95  *
96  * abstract:
97  *  search exceptionstack for last handler of caused exception,
98  *  call userdefined function to signal exception,
99  *  jump to handler with longjmp or call unhandled_exception
100  *
101  */
102
103 void
104 __cause_exception  (ex, file, lineno, user_arg)
105      char *ex;
106      char *file;
107      int  lineno;
108      int user_arg;
109 {
110   register struct __ch_handler *handler = __getexceptionStack();
111
112   /* call user defined cause function */
113   cause_exception (ex, file, lineno, user_arg);
114   
115   for ( ; handler != NULL; handler = handler->prev)
116     {
117       register struct __ch_handled_excepts *list = handler->handlers;
118       for ( ; list->code != 0; list++ )
119         {
120           if (list->ex == __ch_else_except || EX_EQ(list->ex, ex)) /* found */
121             {
122               __setexceptionStack (handler->prev);
123               longjmp(handler->jbuf, list->code);         
124             }
125         }
126     }
127
128   /* no handler found -- call unhandled_exception */
129   unhandled_exception (ex, file, lineno, user_arg);
130   abort ();
131 }
132
133 /*
134  * function __cause_ex1
135  *
136  * parameters:
137  *  exnum               name string of exception to raise
138  *  file                filename of CAUSE statement
139  *  lineno              linenumber of CAUSE statement
140  *
141  * returns:
142  *  never               leave function with longjmp or abort
143  *
144  * abstract:
145  *  This is the function the compiler generated code calls.
146  *  Search exceptionstack for last handler of caused exception,
147  *  call userdefined function to signal exception,
148  *  jump to handler with longjmp or call unhandled_exception
149  *
150  */
151
152 void
153 __cause_ex1  (ex, file, lineno)
154      char *ex;
155      char *file;
156      int  lineno;
157 {
158   __cause_exception (ex, file, lineno, 0);
159 }