add internal SCM_NOINLINE definition
[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 const void **jump_table_pointer = NULL;
65 #endif
66
67 #ifdef HAVE_LABELS_AS_VALUES
68 register const void **jump_table JT_REG;
69
70 if (SCM_UNLIKELY (!jump_table_pointer))
71 {
72 int i;
73 jump_table_pointer = malloc (SCM_VM_NUM_INSTRUCTIONS * sizeof (void*));
74 for (i = 0; i < SCM_VM_NUM_INSTRUCTIONS; i++)
75 jump_table_pointer[i] = &&vm_error_bad_instruction;
76 #define VM_INSTRUCTION_TO_LABEL 1
77 #define jump_table jump_table_pointer
78 #include <libguile/vm-expand.h>
79 #include <libguile/vm-i-system.i>
80 #include <libguile/vm-i-scheme.i>
81 #include <libguile/vm-i-loader.i>
82 #undef jump_table
83 #undef VM_INSTRUCTION_TO_LABEL
84 }
85
86 /* Attempt to keep JUMP_TABLE_POINTER in a register. This saves one
87 load instruction at each instruction dispatch. */
88 jump_table = jump_table_pointer;
89 #endif
90
91 /* Initialization */
92 {
93 SCM prog = program;
94
95 /* Boot program */
96 program = vm_make_boot_program (nargs);
97
98 /* Initial frame */
99 CACHE_REGISTER ();
100 PUSH (SCM_PACK (fp)); /* dynamic link */
101 PUSH (SCM_PACK (0)); /* mvra */
102 PUSH (SCM_PACK (ip)); /* ra */
103 CACHE_PROGRAM ();
104 PUSH (program);
105 fp = sp + 1;
106 ip = SCM_C_OBJCODE_BASE (bp);
107 /* MV-call frame, function & arguments */
108 PUSH (SCM_PACK (0)); /* dynamic link */
109 PUSH (SCM_PACK (0)); /* mvra */
110 PUSH (SCM_PACK (0)); /* ra */
111 PUSH (prog);
112 if (SCM_UNLIKELY (sp + nargs >= stack_limit))
113 goto vm_error_too_many_args;
114 while (nargs--)
115 PUSH (*argv++);
116 }
117
118 /* Let's go! */
119 NEXT;
120
121 #ifndef HAVE_LABELS_AS_VALUES
122 vm_start:
123 switch ((*ip++) & SCM_VM_INSTRUCTION_MASK) {
124 #endif
125
126 #include "vm-expand.h"
127 #include "vm-i-system.c"
128 #include "vm-i-scheme.c"
129 #include "vm-i-loader.c"
130
131 #ifndef HAVE_LABELS_AS_VALUES
132 default:
133 goto vm_error_bad_instruction;
134 }
135 #endif
136
137
138 vm_done:
139 SYNC_ALL ();
140 return finish_args;
141
142 /* Errors */
143 {
144 SCM err_msg;
145
146 /* FIXME: need to sync regs before allocating anything, in each case. */
147
148 vm_error_bad_instruction:
149 err_msg = scm_from_latin1_string ("VM: Bad instruction: ~s");
150 finish_args = scm_list_1 (scm_from_uchar (ip[-1]));
151 goto vm_error;
152
153 vm_error_unbound:
154 /* FINISH_ARGS should be the name of the unbound variable. */
155 SYNC_ALL ();
156 err_msg = scm_from_latin1_string ("Unbound variable: ~s");
157 scm_error_scm (scm_misc_error_key, program, err_msg,
158 scm_list_1 (finish_args), SCM_BOOL_F);
159 goto vm_error;
160
161 vm_error_unbound_fluid:
162 SYNC_ALL ();
163 err_msg = scm_from_latin1_string ("Unbound fluid: ~s");
164 scm_error_scm (scm_misc_error_key, program, err_msg,
165 scm_list_1 (finish_args), SCM_BOOL_F);
166 goto vm_error;
167
168 vm_error_not_a_variable:
169 SYNC_ALL ();
170 scm_error (scm_arg_type_key, func_name, "Not a variable: ~S",
171 scm_list_1 (finish_args), scm_list_1 (finish_args));
172 goto vm_error;
173
174 vm_error_apply_to_non_list:
175 SYNC_ALL ();
176 scm_error (scm_arg_type_key, "apply", "Apply to non-list: ~S",
177 scm_list_1 (finish_args), scm_list_1 (finish_args));
178 goto vm_error;
179
180 vm_error_kwargs_length_not_even:
181 SYNC_ALL ();
182 err_msg = scm_from_latin1_string ("Odd length of keyword argument list");
183 scm_error_scm (sym_keyword_argument_error, program, err_msg,
184 SCM_EOL, SCM_BOOL_F);
185
186 vm_error_kwargs_invalid_keyword:
187 /* FIXME say which one it was */
188 SYNC_ALL ();
189 err_msg = scm_from_latin1_string ("Invalid keyword");
190 scm_error_scm (sym_keyword_argument_error, program, err_msg,
191 SCM_EOL, SCM_BOOL_F);
192
193 vm_error_kwargs_unrecognized_keyword:
194 /* FIXME say which one it was */
195 SYNC_ALL ();
196 err_msg = scm_from_latin1_string ("Unrecognized keyword");
197 scm_error_scm (sym_keyword_argument_error, program, err_msg,
198 SCM_EOL, SCM_BOOL_F);
199
200 vm_error_too_many_args:
201 err_msg = scm_from_latin1_string ("VM: Too many arguments");
202 finish_args = scm_list_1 (scm_from_int (nargs));
203 goto vm_error;
204
205 vm_error_wrong_num_args:
206 /* nargs and program are valid */
207 SYNC_ALL ();
208 scm_wrong_num_args (program);
209 /* shouldn't get here */
210 goto vm_error;
211
212 vm_error_wrong_type_apply:
213 SYNC_ALL ();
214 scm_error (scm_arg_type_key, NULL, "Wrong type to apply: ~S",
215 scm_list_1 (program), scm_list_1 (program));
216 goto vm_error;
217
218 vm_error_stack_overflow:
219 err_msg = scm_from_latin1_string ("VM: Stack overflow");
220 finish_args = SCM_EOL;
221 if (stack_limit < vp->stack_base + vp->stack_size)
222 /* There are VM_STACK_RESERVE_SIZE bytes left. Make them available so
223 that `throw' below can run on this VM. */
224 vp->stack_limit = vp->stack_base + vp->stack_size;
225 goto vm_error;
226
227 vm_error_stack_underflow:
228 err_msg = scm_from_latin1_string ("VM: Stack underflow");
229 finish_args = SCM_EOL;
230 goto vm_error;
231
232 vm_error_improper_list:
233 err_msg = scm_from_latin1_string ("Expected a proper list, but got object with tail ~s");
234 goto vm_error;
235
236 vm_error_not_a_pair:
237 SYNC_ALL ();
238 scm_wrong_type_arg_msg (func_name, 1, finish_args, "pair");
239 /* shouldn't get here */
240 goto vm_error;
241
242 vm_error_not_a_bytevector:
243 SYNC_ALL ();
244 scm_wrong_type_arg_msg (func_name, 1, finish_args, "bytevector");
245 /* shouldn't get here */
246 goto vm_error;
247
248 vm_error_not_a_struct:
249 SYNC_ALL ();
250 scm_wrong_type_arg_msg (func_name, 1, finish_args, "struct");
251 /* shouldn't get here */
252 goto vm_error;
253
254 vm_error_not_a_thunk:
255 SYNC_ALL ();
256 scm_wrong_type_arg_msg ("dynamic-wind", 1, finish_args, "thunk");
257 /* shouldn't get here */
258 goto vm_error;
259
260 vm_error_no_values:
261 err_msg = scm_from_latin1_string ("Zero values returned to single-valued continuation");
262 finish_args = SCM_EOL;
263 goto vm_error;
264
265 vm_error_not_enough_values:
266 err_msg = scm_from_latin1_string ("Too few values returned to continuation");
267 finish_args = SCM_EOL;
268 goto vm_error;
269
270 vm_error_continuation_not_rewindable:
271 err_msg = scm_from_latin1_string ("Unrewindable partial continuation");
272 finish_args = scm_cons (finish_args, SCM_EOL);
273 goto vm_error;
274
275 vm_error_bad_wide_string_length:
276 err_msg = scm_from_latin1_string ("VM: Bad wide string length: ~S");
277 goto vm_error;
278
279 #ifdef VM_CHECK_IP
280 vm_error_invalid_address:
281 err_msg = scm_from_latin1_string ("VM: Invalid program address");
282 finish_args = SCM_EOL;
283 goto vm_error;
284 #endif
285
286 #if VM_CHECK_OBJECT
287 vm_error_object:
288 err_msg = scm_from_latin1_string ("VM: Invalid object table access");
289 finish_args = SCM_EOL;
290 goto vm_error;
291 #endif
292
293 #if VM_CHECK_FREE_VARIABLES
294 vm_error_free_variable:
295 err_msg = scm_from_latin1_string ("VM: Invalid free variable access");
296 finish_args = SCM_EOL;
297 goto vm_error;
298 #endif
299
300 vm_error:
301 SYNC_ALL ();
302
303 scm_ithrow (sym_vm_error, scm_list_3 (sym_vm_run, err_msg, finish_args),
304 1);
305 }
306
307 abort (); /* never reached */
308 }
309
310 #undef VM_USE_HOOKS
311 #undef VM_CHECK_OBJECT
312 #undef VM_CHECK_FREE_VARIABLE
313 #undef VM_CHECK_UNDERFLOW
314
315 /*
316 Local Variables:
317 c-file-style: "gnu"
318 End:
319 */