better VM error messages
[bpt/guile.git] / libguile / vm-engine.c
1 /* Copyright (C) 2001, 2009 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
17 */
18
19 /* This file is included in vm.c multiple times */
20
21 #if (VM_ENGINE == SCM_VM_REGULAR_ENGINE)
22 #define VM_USE_HOOKS 0 /* Various hooks */
23 #define VM_USE_CLOCK 0 /* Bogoclock */
24 #define VM_CHECK_OBJECT 1 /* Check object table */
25 #define VM_CHECK_FREE_VARIABLES 1 /* Check free variable access */
26 #define VM_PUSH_DEBUG_FRAMES 0 /* Push frames onto the evaluator debug stack */
27 #elif (VM_ENGINE == SCM_VM_DEBUG_ENGINE)
28 #define VM_USE_HOOKS 1
29 #define VM_USE_CLOCK 1
30 #define VM_CHECK_OBJECT 1
31 #define VM_CHECK_FREE_VARIABLES 1
32 #define VM_PUSH_DEBUG_FRAMES 1
33 #else
34 #error unknown debug engine VM_ENGINE
35 #endif
36
37 #include "vm-engine.h"
38
39
40 static SCM
41 VM_NAME (struct scm_vm *vp, SCM program, SCM *argv, int nargs)
42 {
43 /* VM registers */
44 register scm_byte_t *ip IP_REG; /* instruction pointer */
45 register SCM *sp SP_REG; /* stack pointer */
46 register SCM *fp FP_REG; /* frame pointer */
47
48 /* Cache variables */
49 struct scm_objcode *bp = NULL; /* program base pointer */
50 SCM *free_vars = NULL; /* free variables */
51 size_t free_vars_count = 0; /* length of FREE_VARS */
52 SCM *objects = NULL; /* constant objects */
53 size_t object_count = 0; /* length of OBJECTS */
54 SCM *stack_base = vp->stack_base; /* stack base address */
55 SCM *stack_limit = vp->stack_limit; /* stack limit address */
56
57 /* Internal variables */
58 int nvalues = 0;
59 long start_time = scm_c_get_internal_run_time ();
60 SCM finish_args; /* used both for returns: both in error
61 and normal situations */
62 #if VM_USE_HOOKS
63 SCM hook_args = SCM_EOL;
64 #endif
65
66 #ifdef HAVE_LABELS_AS_VALUES
67 static void **jump_table = NULL;
68 #endif
69
70 #if VM_PUSH_DEBUG_FRAMES
71 scm_t_debug_frame debug;
72 scm_t_debug_info debug_vect_body;
73 debug.status = SCM_VOIDFRAME;
74 #endif
75
76 #ifdef HAVE_LABELS_AS_VALUES
77 if (SCM_UNLIKELY (!jump_table))
78 {
79 int i;
80 jump_table = malloc (SCM_VM_NUM_INSTRUCTIONS * sizeof(void*));
81 for (i = 0; i < SCM_VM_NUM_INSTRUCTIONS; i++)
82 jump_table[i] = &&vm_error_bad_instruction;
83 #define VM_INSTRUCTION_TO_LABEL 1
84 #include <libguile/vm-expand.h>
85 #include <libguile/vm-i-system.i>
86 #include <libguile/vm-i-scheme.i>
87 #include <libguile/vm-i-loader.i>
88 #undef VM_INSTRUCTION_TO_LABEL
89 }
90 #endif
91
92 /* Initialization */
93 {
94 SCM prog = program;
95
96 /* Boot program */
97 program = vm_make_boot_program (nargs);
98
99 #if VM_PUSH_DEBUG_FRAMES
100 debug.prev = scm_i_last_debug_frame ();
101 debug.status = SCM_APPLYFRAME;
102 debug.vect = &debug_vect_body;
103 debug.vect[0].a.proc = program; /* the boot program */
104 debug.vect[0].a.args = SCM_EOL;
105 scm_i_set_last_debug_frame (&debug);
106 #endif
107
108 /* Initial frame */
109 CACHE_REGISTER ();
110 PUSH ((SCM)fp); /* dynamic link */
111 PUSH (0); /* ra */
112 PUSH (0); /* mvra */
113 CACHE_PROGRAM ();
114 PUSH (program);
115 fp = sp + 1;
116 INIT_FRAME ();
117 /* MV-call frame, function & arguments */
118 PUSH ((SCM)fp); /* dynamic link */
119 PUSH (0); /* ra */
120 PUSH (0); /* mvra */
121 PUSH (prog);
122 if (SCM_UNLIKELY (sp + nargs >= stack_limit))
123 goto vm_error_too_many_args;
124 while (nargs--)
125 PUSH (*argv++);
126 }
127
128 /* Let's go! */
129 BOOT_HOOK ();
130 NEXT;
131
132 #ifndef HAVE_LABELS_AS_VALUES
133 vm_start:
134 switch ((*ip++) & SCM_VM_INSTRUCTION_MASK) {
135 #endif
136
137 #include "vm-expand.h"
138 #include "vm-i-system.c"
139 #include "vm-i-scheme.c"
140 #include "vm-i-loader.c"
141
142 #ifndef HAVE_LABELS_AS_VALUES
143 default:
144 goto vm_error_bad_instruction;
145 }
146 #endif
147
148
149 vm_done:
150 SYNC_ALL ();
151 #if VM_PUSH_DEBUG_FRAMES
152 scm_i_set_last_debug_frame (debug.prev);
153 #endif
154 return finish_args;
155
156 /* Errors */
157 {
158 SCM err_msg;
159
160 vm_error_bad_instruction:
161 err_msg = scm_from_locale_string ("VM: Bad instruction: ~s");
162 finish_args = scm_list_1 (scm_from_uchar (ip[-1]));
163 goto vm_error;
164
165 vm_error_unbound:
166 err_msg = scm_from_locale_string ("VM: Unbound variable: ~s");
167 goto vm_error;
168
169 vm_error_wrong_type_arg:
170 err_msg = scm_from_locale_string ("VM: Wrong type argument");
171 finish_args = SCM_EOL;
172 goto vm_error;
173
174 vm_error_too_many_args:
175 err_msg = scm_from_locale_string ("VM: Too many arguments");
176 finish_args = scm_list_1 (scm_from_int (nargs));
177 goto vm_error;
178
179 vm_error_wrong_num_args:
180 /* nargs and program are valid */
181 SYNC_ALL ();
182 scm_wrong_num_args (program);
183 /* shouldn't get here */
184 goto vm_error;
185
186 vm_error_wrong_type_apply:
187 SYNC_ALL ();
188 scm_error (scm_misc_error_key, FUNC_NAME, "Wrong type to apply: ~S",
189 scm_list_1 (program), SCM_BOOL_F);
190 goto vm_error;
191
192 vm_error_stack_overflow:
193 err_msg = scm_from_locale_string ("VM: Stack overflow");
194 finish_args = SCM_EOL;
195 goto vm_error;
196
197 vm_error_stack_underflow:
198 err_msg = scm_from_locale_string ("VM: Stack underflow");
199 finish_args = SCM_EOL;
200 goto vm_error;
201
202 vm_error_improper_list:
203 err_msg = scm_from_locale_string ("Expected a proper list, but got object with tail ~s");
204 goto vm_error;
205
206 vm_error_not_a_pair:
207 SYNC_ALL ();
208 scm_wrong_type_arg_msg (FUNC_NAME, 1, finish_args, "pair");
209 /* shouldn't get here */
210 goto vm_error;
211
212 vm_error_not_a_bytevector:
213 SYNC_ALL ();
214 scm_wrong_type_arg_msg (FUNC_NAME, 1, finish_args, "bytevector");
215 /* shouldn't get here */
216 goto vm_error;
217
218 vm_error_no_values:
219 err_msg = scm_from_locale_string ("Zero values returned to single-valued continuation");
220 finish_args = SCM_EOL;
221 goto vm_error;
222
223 vm_error_not_enough_values:
224 err_msg = scm_from_locale_string ("Too few values returned to continuation");
225 finish_args = SCM_EOL;
226 goto vm_error;
227
228 vm_error_bad_wide_string_length:
229 err_msg = scm_from_locale_string ("VM: Bad wide string length: ~S");
230 goto vm_error;
231
232 #if VM_CHECK_IP
233 vm_error_invalid_address:
234 err_msg = scm_from_locale_string ("VM: Invalid program address");
235 finish_args = SCM_EOL;
236 goto vm_error;
237 #endif
238
239 #if VM_CHECK_OBJECT
240 vm_error_object:
241 err_msg = scm_from_locale_string ("VM: Invalid object table access");
242 finish_args = SCM_EOL;
243 goto vm_error;
244 #endif
245
246 #if VM_CHECK_FREE_VARIABLES
247 vm_error_free_variable:
248 err_msg = scm_from_locale_string ("VM: Invalid free variable access");
249 finish_args = SCM_EOL;
250 goto vm_error;
251 #endif
252
253 vm_error:
254 SYNC_ALL ();
255
256 scm_ithrow (sym_vm_error, scm_list_3 (sym_vm_run, err_msg, finish_args),
257 1);
258 }
259
260 abort (); /* never reached */
261 }
262
263 #undef VM_USE_HOOKS
264 #undef VM_USE_CLOCK
265 #undef VM_CHECK_OBJECT
266 #undef VM_CHECK_FREE_VARIABLE
267 #undef VM_PUSH_DEBUG_FRAMES
268
269 /*
270 Local Variables:
271 c-file-style: "gnu"
272 End:
273 */