modify upstream makefile to allow hardening build flags to apply
[debian/yforth] / exceptio.c
1 /* yForth? - A Forth interpreter written in ANSI C
2  * Copyright (C) 2012 Luca Padovani
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  * ------------------------------------------------------------------------
17  * Module name:     exceptio.c
18  * Abstract:        exception word set
19  */
20
21 #include <malloc.h>
22 #include <setjmp.h>
23 #include "yforth.h"
24 #include "core.h"
25 #include "exceptio.h"
26
27 /**************************************************************************/
28 /* VARIABLES **************************************************************/
29 /**************************************************************************/
30
31 struct exception_frame *top_frame;  /* ptr to the top of exception stack */
32
33 /**************************************************************************/
34 /* WORDS ******************************************************************/
35 /**************************************************************************/
36
37 void _catch() {
38         register struct exception_frame *frame =
39                 (struct exception_frame *) malloc(sizeof(struct exception_frame));
40         if (frame) {
41                 register int ret_val;
42                 if ((ret_val = setjmp(frame->catch_buf)) == 0) {
43             /* Executed when "catch" is invoked */
44                         save_input_specification();
45                         frame->sp = sp + 1;
46                         frame->rp = rp;
47                         frame->bp = bp;
48                         frame->fp = fp;
49                         frame->last = top_frame;
50                         top_frame = frame;
51                         exec_word((struct word_def *) *sp++);
52                         *--sp = 0;
53                 } else *--sp = ret_val;
54                 frame = top_frame;
55                 sp = frame->sp;
56                 rp = frame->rp;
57                 bp = frame->bp;
58                 top_frame = frame->last;
59                 free(frame);
60                 restore_input_specification();
61         }
62 }
63
64 void _throw() {
65         register Cell n = *sp++;
66         if (n) {
67                 if (top_frame) longjmp(top_frame->catch_buf, n);
68                 else if (n == -1) ;
69                 else if (n == -2) _type();
70                 sp = sp_top;
71                 longjmp(warm_start_jump, 1);
72         }
73 }
74