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