Merge commit 'feccd2d3100fd2964d4c2df58ab3da7ce4949a66' into vm-check
[bpt/guile.git] / libguile / vm-engine.c
1 /* Copyright (C) 2001 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41
42 /* This file is included in vm.c multiple times */
43
44 #if (VM_ENGINE == SCM_VM_REGULAR_ENGINE)
45 #define VM_USE_HOOKS 0 /* Various hooks */
46 #define VM_USE_CLOCK 0 /* Bogoclock */
47 #define VM_CHECK_EXTERNAL 1 /* Check external link */
48 #define VM_CHECK_OBJECT 1 /* Check object table */
49 #define VM_PUSH_DEBUG_FRAMES 0 /* Push frames onto the evaluator debug stack */
50 #elif (VM_ENGINE == SCM_VM_DEBUG_ENGINE)
51 #define VM_USE_HOOKS 1
52 #define VM_USE_CLOCK 1
53 #define VM_CHECK_EXTERNAL 1
54 #define VM_CHECK_OBJECT 1
55 #define VM_PUSH_DEBUG_FRAMES 1
56 #else
57 #error unknown debug engine VM_ENGINE
58 #endif
59
60 #include "vm-engine.h"
61
62
63 static SCM
64 VM_NAME (struct scm_vm *vp, SCM program, SCM *argv, int nargs)
65 {
66 /* VM registers */
67 register scm_byte_t *ip IP_REG; /* instruction pointer */
68 register SCM *sp SP_REG; /* stack pointer */
69 register SCM *fp FP_REG; /* frame pointer */
70
71 /* Cache variables */
72 struct scm_objcode *bp = NULL; /* program base pointer */
73 SCM external = SCM_EOL; /* external environment */
74 SCM *objects = NULL; /* constant objects */
75 size_t object_count = 0; /* length of OBJECTS */
76 SCM *stack_base = vp->stack_base; /* stack base address */
77 SCM *stack_limit = vp->stack_limit; /* stack limit address */
78
79 /* Internal variables */
80 int nvalues = 0;
81 long start_time = scm_c_get_internal_run_time ();
82 SCM finish_args; /* used both for returns: both in error
83 and normal situations */
84 #if VM_USE_HOOKS
85 SCM hook_args = SCM_EOL;
86 #endif
87
88 #ifdef HAVE_LABELS_AS_VALUES
89 static void **jump_table = NULL;
90 #endif
91
92 #if VM_PUSH_DEBUG_FRAMES
93 scm_t_debug_frame debug;
94 scm_t_debug_info debug_vect_body;
95 debug.status = SCM_VOIDFRAME;
96 #endif
97
98 #ifdef HAVE_LABELS_AS_VALUES
99 if (SCM_UNLIKELY (!jump_table))
100 {
101 int i;
102 jump_table = malloc (SCM_VM_NUM_INSTRUCTIONS * sizeof(void*));
103 for (i = 0; i < SCM_VM_NUM_INSTRUCTIONS; i++)
104 jump_table[i] = &&vm_error_bad_instruction;
105 #define VM_INSTRUCTION_TO_LABEL 1
106 #include "vm-expand.h"
107 #include "vm-i-system.i"
108 #include "vm-i-scheme.i"
109 #include "vm-i-loader.i"
110 #undef VM_INSTRUCTION_TO_LABEL
111 }
112 #endif
113
114 /* Initialization */
115 {
116 SCM prog = program;
117
118 /* Boot program */
119 program = vm_make_boot_program (nargs);
120
121 #if VM_PUSH_DEBUG_FRAMES
122 debug.prev = scm_i_last_debug_frame ();
123 debug.status = SCM_APPLYFRAME;
124 debug.vect = &debug_vect_body;
125 debug.vect[0].a.proc = program; /* the boot program */
126 debug.vect[0].a.args = SCM_EOL;
127 scm_i_set_last_debug_frame (&debug);
128 #endif
129
130 /* Initial frame */
131 CACHE_REGISTER ();
132 CACHE_PROGRAM ();
133 PUSH (program);
134 NEW_FRAME ();
135
136 /* Initial arguments */
137 PUSH (prog);
138 if (SCM_UNLIKELY (sp + nargs >= stack_limit))
139 goto vm_error_too_many_args;
140 while (nargs--)
141 PUSH (*argv++);
142 }
143
144 /* Let's go! */
145 BOOT_HOOK ();
146 NEXT;
147
148 #ifndef HAVE_LABELS_AS_VALUES
149 vm_start:
150 switch ((*ip++) & SCM_VM_INSTRUCTION_MASK) {
151 #endif
152
153 #include "vm-expand.h"
154 #include "vm-i-system.c"
155 #include "vm-i-scheme.c"
156 #include "vm-i-loader.c"
157
158 #ifndef HAVE_LABELS_AS_VALUES
159 default:
160 goto vm_error_bad_instruction;
161 }
162 #endif
163
164
165 vm_done:
166 SYNC_ALL ();
167 #if VM_PUSH_DEBUG_FRAMES
168 scm_i_set_last_debug_frame (debug.prev);
169 #endif
170 return finish_args;
171
172 /* Errors */
173 {
174 SCM err_msg;
175
176 vm_error_bad_instruction:
177 err_msg = scm_from_locale_string ("VM: Bad instruction: ~A");
178 finish_args = SCM_LIST1 (scm_from_uchar (ip[-1]));
179 goto vm_error;
180
181 vm_error_unbound:
182 err_msg = scm_from_locale_string ("VM: Unbound variable: ~A");
183 goto vm_error;
184
185 vm_error_wrong_type_arg:
186 err_msg = scm_from_locale_string ("VM: Wrong type argument");
187 finish_args = SCM_EOL;
188 goto vm_error;
189
190 vm_error_too_many_args:
191 err_msg = scm_from_locale_string ("VM: Too many arguments");
192 finish_args = SCM_LIST1 (scm_from_int (nargs));
193 goto vm_error;
194
195 vm_error_wrong_num_args:
196 /* nargs and program are valid */
197 SYNC_ALL ();
198 scm_wrong_num_args (program);
199 /* shouldn't get here */
200 goto vm_error;
201
202 vm_error_wrong_type_apply:
203 err_msg = scm_from_locale_string ("VM: Wrong type to apply: ~S "
204 "[IP offset: ~a]");
205 finish_args = SCM_LIST2 (program,
206 SCM_I_MAKINUM (ip - bp->base));
207 goto vm_error;
208
209 vm_error_stack_overflow:
210 err_msg = scm_from_locale_string ("VM: Stack overflow");
211 finish_args = SCM_EOL;
212 goto vm_error;
213
214 vm_error_stack_underflow:
215 err_msg = scm_from_locale_string ("VM: Stack underflow");
216 finish_args = SCM_EOL;
217 goto vm_error;
218
219 vm_error_improper_list:
220 err_msg = scm_from_locale_string ("VM: Attempt to unroll an improper list: tail is ~A");
221 goto vm_error;
222
223 vm_error_not_a_pair:
224 SYNC_ALL ();
225 scm_wrong_type_arg_msg (FUNC_NAME, 1, finish_args, "pair");
226 /* shouldn't get here */
227 goto vm_error;
228
229 vm_error_no_values:
230 err_msg = scm_from_locale_string ("VM: 0-valued return");
231 finish_args = SCM_EOL;
232 goto vm_error;
233
234 vm_error_not_enough_values:
235 err_msg = scm_from_locale_string ("VM: Not enough values for mv-bind");
236 finish_args = SCM_EOL;
237 goto vm_error;
238
239 vm_error_no_such_module:
240 err_msg = scm_from_locale_string ("VM: No such module: ~A");
241 goto vm_error;
242
243 #if VM_CHECK_IP
244 vm_error_invalid_address:
245 err_msg = scm_from_locale_string ("VM: Invalid program address");
246 finish_args = SCM_EOL;
247 goto vm_error;
248 #endif
249
250 #if VM_CHECK_EXTERNAL
251 vm_error_external:
252 err_msg = scm_from_locale_string ("VM: Invalid external access");
253 finish_args = SCM_EOL;
254 goto vm_error;
255 #endif
256
257 #if VM_CHECK_OBJECT
258 vm_error_object:
259 err_msg = scm_from_locale_string ("VM: Invalid object table access");
260 finish_args = SCM_EOL;
261 goto vm_error;
262 #endif
263
264 vm_error:
265 SYNC_ALL ();
266
267 scm_ithrow (sym_vm_error, SCM_LIST3 (sym_vm_run, err_msg, finish_args), 1);
268 }
269
270 abort (); /* never reached */
271 }
272
273 #undef VM_USE_HOOKS
274 #undef VM_USE_CLOCK
275 #undef VM_CHECK_EXTERNAL
276 #undef VM_CHECK_OBJECT
277 #undef VM_PUSH_DEBUG_FRAMES
278
279 /*
280 Local Variables:
281 c-file-style: "gnu"
282 End:
283 */