abort always dispatches to VM bytecode, to detect same-invocation aborts
[bpt/guile.git] / libguile / vm-engine.c
1 /* Copyright (C) 2001, 2009, 2010 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_CHECK_OBJECT 1 /* Check object table */
24 #define VM_CHECK_FREE_VARIABLES 1 /* Check free variable access */
25 #elif (VM_ENGINE == SCM_VM_DEBUG_ENGINE)
26 #define VM_USE_HOOKS 1
27 #define VM_CHECK_OBJECT 1
28 #define VM_CHECK_FREE_VARIABLES 1
29 #else
30 #error unknown debug engine VM_ENGINE
31 #endif
32
33 #include "vm-engine.h"
34
35
36 static SCM
37 VM_NAME (SCM vm, SCM program, SCM *argv, int nargs)
38 {
39 /* VM registers */
40 register scm_t_uint8 *ip IP_REG; /* instruction pointer */
41 register SCM *sp SP_REG; /* stack pointer */
42 register SCM *fp FP_REG; /* frame pointer */
43 struct scm_vm *vp = SCM_VM_DATA (vm);
44
45 /* Cache variables */
46 struct scm_objcode *bp = NULL; /* program base pointer */
47 SCM *objects = NULL; /* constant objects */
48 size_t object_count = 0; /* length of OBJECTS */
49 SCM *stack_limit = vp->stack_limit; /* stack limit address */
50
51 SCM dynstate = SCM_I_CURRENT_THREAD->dynamic_state;
52 scm_t_int64 vm_cookie = vp->cookie++;
53
54 /* Internal variables */
55 int nvalues = 0;
56 SCM finish_args; /* used both for returns: both in error
57 and normal situations */
58 #ifdef HAVE_LABELS_AS_VALUES
59 static void **jump_table = NULL;
60 #endif
61
62 #ifdef HAVE_LABELS_AS_VALUES
63 if (SCM_UNLIKELY (!jump_table))
64 {
65 int i;
66 jump_table = malloc (SCM_VM_NUM_INSTRUCTIONS * sizeof(void*));
67 for (i = 0; i < SCM_VM_NUM_INSTRUCTIONS; i++)
68 jump_table[i] = &&vm_error_bad_instruction;
69 #define VM_INSTRUCTION_TO_LABEL 1
70 #include <libguile/vm-expand.h>
71 #include <libguile/vm-i-system.i>
72 #include <libguile/vm-i-scheme.i>
73 #include <libguile/vm-i-loader.i>
74 #undef VM_INSTRUCTION_TO_LABEL
75 }
76 #endif
77
78 /* Initialization */
79 {
80 SCM prog = program;
81
82 /* Boot program */
83 program = vm_make_boot_program (nargs);
84
85 /* Initial frame */
86 CACHE_REGISTER ();
87 PUSH ((SCM)fp); /* dynamic link */
88 PUSH (0); /* mvra */
89 PUSH ((SCM)ip); /* ra */
90 CACHE_PROGRAM ();
91 PUSH (program);
92 fp = sp + 1;
93 ip = SCM_C_OBJCODE_BASE (bp);
94 /* MV-call frame, function & arguments */
95 PUSH ((SCM)fp); /* dynamic link */
96 PUSH (0); /* mvra */
97 PUSH (0); /* ra */
98 PUSH (prog);
99 if (SCM_UNLIKELY (sp + nargs >= stack_limit))
100 goto vm_error_too_many_args;
101 while (nargs--)
102 PUSH (*argv++);
103 }
104
105 /* Let's go! */
106 BOOT_HOOK ();
107 NEXT;
108
109 #ifndef HAVE_LABELS_AS_VALUES
110 vm_start:
111 switch ((*ip++) & SCM_VM_INSTRUCTION_MASK) {
112 #endif
113
114 #include "vm-expand.h"
115 #include "vm-i-system.c"
116 #include "vm-i-scheme.c"
117 #include "vm-i-loader.c"
118
119 #ifndef HAVE_LABELS_AS_VALUES
120 default:
121 goto vm_error_bad_instruction;
122 }
123 #endif
124
125
126 vm_done:
127 SYNC_ALL ();
128 return finish_args;
129
130 /* Errors */
131 {
132 SCM err_msg;
133
134 /* FIXME: need to sync regs before allocating anything, in each case. */
135
136 vm_error_bad_instruction:
137 err_msg = scm_from_locale_string ("VM: Bad instruction: ~s");
138 finish_args = scm_list_1 (scm_from_uchar (ip[-1]));
139 goto vm_error;
140
141 vm_error_unbound:
142 err_msg = scm_from_locale_string ("VM: Unbound variable: ~s");
143 goto vm_error;
144
145 vm_error_wrong_type_arg:
146 err_msg = scm_from_locale_string ("VM: Wrong type argument");
147 finish_args = SCM_EOL;
148 goto vm_error;
149
150 vm_error_kwargs_length_not_even:
151 SYNC_ALL ();
152 err_msg = scm_from_locale_string ("Odd length of keyword argument list");
153 scm_error_scm (sym_keyword_argument_error, program, err_msg,
154 SCM_EOL, SCM_BOOL_F);
155
156 vm_error_kwargs_invalid_keyword:
157 /* FIXME say which one it was */
158 SYNC_ALL ();
159 err_msg = scm_from_locale_string ("Invalid keyword");
160 scm_error_scm (sym_keyword_argument_error, program, err_msg,
161 SCM_EOL, SCM_BOOL_F);
162
163 vm_error_kwargs_unrecognized_keyword:
164 /* FIXME say which one it was */
165 SYNC_ALL ();
166 err_msg = scm_from_locale_string ("Unrecognized keyword");
167 scm_error_scm (sym_keyword_argument_error, program, err_msg,
168 SCM_EOL, SCM_BOOL_F);
169
170 vm_error_too_many_args:
171 err_msg = scm_from_locale_string ("VM: Too many arguments");
172 finish_args = scm_list_1 (scm_from_int (nargs));
173 goto vm_error;
174
175 vm_error_wrong_num_args:
176 /* nargs and program are valid */
177 SYNC_ALL ();
178 scm_wrong_num_args (program);
179 /* shouldn't get here */
180 goto vm_error;
181
182 vm_error_wrong_type_apply:
183 SYNC_ALL ();
184 scm_error (scm_arg_type_key, FUNC_NAME, "Wrong type to apply: ~S",
185 scm_list_1 (program), scm_list_1 (program));
186 goto vm_error;
187
188 vm_error_stack_overflow:
189 err_msg = scm_from_locale_string ("VM: Stack overflow");
190 finish_args = SCM_EOL;
191 goto vm_error;
192
193 vm_error_stack_underflow:
194 err_msg = scm_from_locale_string ("VM: Stack underflow");
195 finish_args = SCM_EOL;
196 goto vm_error;
197
198 vm_error_improper_list:
199 err_msg = scm_from_locale_string ("Expected a proper list, but got object with tail ~s");
200 goto vm_error;
201
202 vm_error_not_a_pair:
203 SYNC_ALL ();
204 scm_wrong_type_arg_msg (FUNC_NAME, 1, finish_args, "pair");
205 /* shouldn't get here */
206 goto vm_error;
207
208 vm_error_not_a_bytevector:
209 SYNC_ALL ();
210 scm_wrong_type_arg_msg (FUNC_NAME, 1, finish_args, "bytevector");
211 /* shouldn't get here */
212 goto vm_error;
213
214 vm_error_not_a_struct:
215 SYNC_ALL ();
216 scm_wrong_type_arg_msg (FUNC_NAME, 1, finish_args, "struct");
217 /* shouldn't get here */
218 goto vm_error;
219
220 vm_error_not_a_thunk:
221 SYNC_ALL ();
222 scm_wrong_type_arg_msg (FUNC_NAME, 1, finish_args, "thunk");
223 /* shouldn't get here */
224 goto vm_error;
225
226 vm_error_no_values:
227 err_msg = scm_from_locale_string ("Zero values returned to single-valued continuation");
228 finish_args = SCM_EOL;
229 goto vm_error;
230
231 vm_error_not_enough_values:
232 err_msg = scm_from_locale_string ("Too few values returned to continuation");
233 finish_args = SCM_EOL;
234 goto vm_error;
235
236 vm_error_bad_wide_string_length:
237 err_msg = scm_from_locale_string ("VM: Bad wide string length: ~S");
238 goto vm_error;
239
240 #ifdef VM_CHECK_IP
241 vm_error_invalid_address:
242 err_msg = scm_from_locale_string ("VM: Invalid program address");
243 finish_args = SCM_EOL;
244 goto vm_error;
245 #endif
246
247 #if VM_CHECK_OBJECT
248 vm_error_object:
249 err_msg = scm_from_locale_string ("VM: Invalid object table access");
250 finish_args = SCM_EOL;
251 goto vm_error;
252 #endif
253
254 #if VM_CHECK_FREE_VARIABLES
255 vm_error_free_variable:
256 err_msg = scm_from_locale_string ("VM: Invalid free variable access");
257 finish_args = SCM_EOL;
258 goto vm_error;
259 #endif
260
261 vm_error:
262 SYNC_ALL ();
263
264 scm_ithrow (sym_vm_error, scm_list_3 (sym_vm_run, err_msg, finish_args),
265 1);
266 }
267
268 abort (); /* never reached */
269 }
270
271 #undef VM_USE_HOOKS
272 #undef VM_CHECK_OBJECT
273 #undef VM_CHECK_FREE_VARIABLE
274
275 /*
276 Local Variables:
277 c-file-style: "gnu"
278 End:
279 */