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