File: vm.c

package info (click to toggle)
yforth 0.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, jessie, jessie-kfreebsd, stretch
  • size: 396 kB
  • ctags: 788
  • sloc: ansic: 4,426; makefile: 23
file content (103 lines) | stat: -rw-r--r-- 2,699 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* yForth? - A Forth interpreter written in ANSI C
 * Copyright (C) 2012 Luca Padovani
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ------------------------------------------------------------------------
 * Module name:     vm.c
 * Abstract:        The Virtual Machine on which is based the whole
 *                  forth interpreter.
 */

#include <stdio.h>
#include <signal.h>
#include "yforth.h"
#include "core.h"

/* "ip" is the Instruction Pointer of the Virtual Machine. "ip" points to
 * an array of "pfp", which stands for "primitive function pointer",
 * in other words an array of pointers to primitive functions.
 * Roughly speaking, primitive functions are the valid instructions of
 * the Virtual Machine.
 */

pfp *ip;			/* Instruction Pointer */

Cell *sp, *sp_top, *sp_base;	/* various stack pointers... */
Cell *rp, *rp_top, *rp_base;
Real *fp, *fp_top, *fp_base;
Cell *bp;

#ifdef DCELL_MEM
static union double_cell dcell;	/* Used for double-cell transfer */
#endif

/* stacks_recovery: called when an exception occurs, it sets all stack
 * ptrs to their original value.
 */
void 
stacks_recovery (void)
{
  sp = sp_top;
  rp = rp_top;
  fp = fp_top;
}

/* If double-cell transfer is realized with memory-copying, the following
 * auxiliary procedures are needed
 */
#ifdef DCELL_MEM
DCell 
get_dcell (Cell * ptr)
{
  dcell.d2.high = *ptr;
  dcell.d2.low = *(ptr + 1);
  return (dcell.d1);
}

void 
put_dcell (Cell * ptr, DCell d)
{
  dcell.d1 = d;
  *ptr = dcell.d2.high;
  *(ptr + 1) = dcell.d2.low;
}
#endif

/* sig_fpe_handler: signal handler for math exceptions */
void 
sig_fpe_handler (int sig)
{
  signal (SIGFPE, sig_fpe_handler);
  _error = E_FPE;
  _view_error_msg();
  longjmp(warm_start_jump, 1);
}

/* sig_segv_handler: signal handler for segmentation violation */
void 
sig_segv_handler (int sig)
{
  signal (SIGSEGV, sig_segv_handler);
  _error = E_SEGV;
  _view_error_msg();
  longjmp(warm_start_jump, 1);
}

/* init_signal: initialize signal handlers */
void 
init_signals ()
{
  signal (SIGFPE, sig_fpe_handler);
  signal (SIGSEGV, sig_segv_handler);
}