Imported Upstream version 0.1beta
[debian/yforth] / vm.c
1 /* yForth? - Written by Luca Padovani (C) 1996/97
2  * ------------------------------------------------------------------------
3  * This software is FreeWare as long as it comes with this header in each
4  * source file, anyway you can use it or any part of it whatever
5  * you want. It comes without any warranty, so use it at your own risk.
6  * ------------------------------------------------------------------------
7  * Module name:     vm.c
8  * Abstract:        The Virtual Machine on which is based the whole
9  *                  forth interpreter.
10  */
11
12 #include <stdio.h>
13 #include <signal.h>
14 #include "yforth.h"
15 #include "core.h"
16
17 /* "ip" is the Instruction Pointer of the Virtual Machine. "ip" points to
18  * an array of "pfp", which stands for "primitive function pointer",
19  * in other words an array of pointers to primitive functions.
20  * Roughly speaking, primitive functions are the valid instructions of
21  * the Virtual Machine.
22  */
23
24 pfp *ip;                        /* Instruction Pointer */
25
26 Cell *sp, *sp_top, *sp_base;    /* various stack pointers... */
27 Cell *rp, *rp_top, *rp_base;
28 Real *fp, *fp_top, *fp_base;
29 Cell *bp;
30
31 #ifdef DCELL_MEM
32 static union double_cell dcell; /* Used for double-cell transfer */
33 #endif
34
35 /* stacks_recovery: called when an exception occurs, it sets all stack
36  * ptrs to their original value.
37  */
38 void 
39 stacks_recovery (void)
40 {
41   sp = sp_top;
42   rp = rp_top;
43   fp = fp_top;
44 }
45
46 /* If double-cell transfer is realized with memory-copying, the following
47  * auxiliary procedures are needed
48  */
49 #ifdef DCELL_MEM
50 DCell 
51 get_dcell (Cell * ptr)
52 {
53   dcell.d2.high = *ptr;
54   dcell.d2.low = *(ptr + 1);
55   return (dcell.d1);
56 }
57
58 void 
59 put_dcell (Cell * ptr, DCell d)
60 {
61   dcell.d1 = d;
62   *ptr = dcell.d2.high;
63   *(ptr + 1) = dcell.d2.low;
64 }
65 #endif
66
67 /* sig_fpe_handler: signal handler for math exceptions */
68 void 
69 sig_fpe_handler (int sig)
70 {
71   signal (SIGFPE, sig_fpe_handler);
72   _error = E_FPE;
73   _view_error_msg();
74   longjmp(warm_start_jump, 1);
75 }
76
77 /* sig_segv_handler: signal handler for segmentation violation */
78 void 
79 sig_segv_handler (int sig)
80 {
81   signal (SIGSEGV, sig_segv_handler);
82   _error = E_SEGV;
83   _view_error_msg();
84   longjmp(warm_start_jump, 1);
85 }
86
87 /* init_signal: initialize signal handlers */
88 void 
89 init_signals ()
90 {
91   signal (SIGFPE, sig_fpe_handler);
92   signal (SIGSEGV, sig_segv_handler);
93 }