in debug mode, make sure that calls to the vm can be captured via make-stack
[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 if (!(debug.prev && debug.prev->status == SCM_APPLYFRAME
124 && debug.prev->vect[0].a.proc != prog))
125 {
126 debug.status = SCM_APPLYFRAME;
127 debug.vect = &debug_vect_body;
128 debug.vect[0].a.proc = program; /* the boot program */
129 debug.vect[0].a.args = SCM_EOL;
130 scm_i_set_last_debug_frame (&debug);
131 }
132 #endif
133
134 /* Initial frame */
135 CACHE_REGISTER ();
136 CACHE_PROGRAM ();
137 PUSH (program);
138 NEW_FRAME ();
139
140 /* Initial arguments */
141 PUSH (prog);
142 if (SCM_UNLIKELY (sp + nargs >= stack_limit))
143 goto vm_error_too_many_args;
144 while (nargs--)
145 PUSH (*argv++);
146 }
147
148 /* Let's go! */
149 BOOT_HOOK ();
150 NEXT;
151
152 #ifndef HAVE_LABELS_AS_VALUES
153 vm_start:
154 switch ((*ip++) & SCM_VM_INSTRUCTION_MASK) {
155 #endif
156
157 #include "vm-expand.h"
158 #include "vm-i-system.c"
159 #include "vm-i-scheme.c"
160 #include "vm-i-loader.c"
161
162 #ifndef HAVE_LABELS_AS_VALUES
163 default:
164 goto vm_error_bad_instruction;
165 }
166 #endif
167
168
169 vm_done:
170 SYNC_ALL ();
171 #if VM_PUSH_DEBUG_FRAMES
172 if (debug.status == SCM_APPLYFRAME)
173 scm_i_set_last_debug_frame (debug.prev);
174 #endif
175 return finish_args;
176
177 /* Errors */
178 {
179 SCM err_msg;
180
181 vm_error_bad_instruction:
182 err_msg = scm_from_locale_string ("VM: Bad instruction: ~A");
183 finish_args = SCM_LIST1 (scm_from_uchar (ip[-1]));
184 goto vm_error;
185
186 vm_error_unbound:
187 err_msg = scm_from_locale_string ("VM: Unbound variable: ~A");
188 goto vm_error;
189
190 vm_error_wrong_type_arg:
191 err_msg = scm_from_locale_string ("VM: Wrong type argument");
192 finish_args = SCM_EOL;
193 goto vm_error;
194
195 vm_error_too_many_args:
196 err_msg = scm_from_locale_string ("VM: Too many arguments");
197 finish_args = SCM_LIST1 (scm_from_int (nargs));
198 goto vm_error;
199
200 vm_error_wrong_num_args:
201 /* nargs and program are valid */
202 SYNC_ALL ();
203 scm_wrong_num_args (program);
204 /* shouldn't get here */
205 goto vm_error;
206
207 vm_error_wrong_type_apply:
208 err_msg = scm_from_locale_string ("VM: Wrong type to apply: ~S "
209 "[IP offset: ~a]");
210 finish_args = SCM_LIST2 (program,
211 SCM_I_MAKINUM (ip - bp->base));
212 goto vm_error;
213
214 vm_error_stack_overflow:
215 err_msg = scm_from_locale_string ("VM: Stack overflow");
216 finish_args = SCM_EOL;
217 goto vm_error;
218
219 vm_error_stack_underflow:
220 err_msg = scm_from_locale_string ("VM: Stack underflow");
221 finish_args = SCM_EOL;
222 goto vm_error;
223
224 vm_error_improper_list:
225 err_msg = scm_from_locale_string ("VM: Attempt to unroll an improper list: tail is ~A");
226 goto vm_error;
227
228 vm_error_not_a_pair:
229 SYNC_ALL ();
230 scm_wrong_type_arg_msg (FUNC_NAME, 1, finish_args, "pair");
231 /* shouldn't get here */
232 goto vm_error;
233
234 vm_error_no_values:
235 err_msg = scm_from_locale_string ("VM: 0-valued return");
236 finish_args = SCM_EOL;
237 goto vm_error;
238
239 vm_error_not_enough_values:
240 err_msg = scm_from_locale_string ("VM: Not enough values for mv-bind");
241 finish_args = SCM_EOL;
242 goto vm_error;
243
244 vm_error_no_such_module:
245 err_msg = scm_from_locale_string ("VM: No such module: ~A");
246 goto vm_error;
247
248 #if VM_CHECK_IP
249 vm_error_invalid_address:
250 err_msg = scm_from_locale_string ("VM: Invalid program address");
251 finish_args = SCM_EOL;
252 goto vm_error;
253 #endif
254
255 #if VM_CHECK_EXTERNAL
256 vm_error_external:
257 err_msg = scm_from_locale_string ("VM: Invalid external access");
258 finish_args = SCM_EOL;
259 goto vm_error;
260 #endif
261
262 #if VM_CHECK_OBJECT
263 vm_error_object:
264 err_msg = scm_from_locale_string ("VM: Invalid object table access");
265 finish_args = SCM_EOL;
266 goto vm_error;
267 #endif
268
269 vm_error:
270 SYNC_ALL ();
271
272 scm_ithrow (sym_vm_error, SCM_LIST3 (sym_vm_run, err_msg, finish_args), 1);
273 }
274
275 abort (); /* never reached */
276 }
277
278 #undef VM_USE_HOOKS
279 #undef VM_USE_CLOCK
280 #undef VM_CHECK_EXTERNAL
281 #undef VM_CHECK_OBJECT
282 #undef VM_PUSH_DEBUG_FRAMES
283
284 /*
285 Local Variables:
286 c-file-style: "gnu"
287 End:
288 */