top level fixes
[bpt/guile.git] / libguile / vm.c
1 /* Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013, 2014 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 /* For mremap(2) on GNU/Linux systems. */
20 #define _GNU_SOURCE
21
22 #if HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <alloca.h>
28 #include <alignof.h>
29 #include <string.h>
30 #include <stdint.h>
31 #include <unistd.h>
32
33 #ifdef HAVE_SYS_MMAN_H
34 #include <sys/mman.h>
35 #endif
36
37 #include "libguile/bdw-gc.h"
38 #include <gc/gc_mark.h>
39
40 #include "_scm.h"
41 #include "control.h"
42 #include "frames.h"
43 #include "instructions.h"
44 #include "loader.h"
45 #include "programs.h"
46 #include "simpos.h"
47 #include "vm.h"
48 #include "vm-builtins.h"
49
50 static int vm_default_engine = SCM_VM_REGULAR_ENGINE;
51
52 /* Unfortunately we can't snarf these: snarfed things are only loaded up from
53 (system vm vm), which might not be loaded before an error happens. */
54 static SCM sym_vm_run;
55 static SCM sym_vm_error;
56 static SCM sym_keyword_argument_error;
57 static SCM sym_regular;
58 static SCM sym_debug;
59
60 /* The page size. */
61 static size_t page_size;
62
63 /* The VM has a number of internal assertions that shouldn't normally be
64 necessary, but might be if you think you found a bug in the VM. */
65 /* #define VM_ENABLE_ASSERTIONS */
66
67 static void vm_expand_stack (struct scm_vm *vp, SCM *new_sp) SCM_NOINLINE;
68
69 /* RESTORE is for the case where we know we have done a PUSH of equal or
70 greater stack size in the past. Otherwise PUSH is the thing, which
71 may expand the stack. */
72 enum vm_increase_sp_kind { VM_SP_PUSH, VM_SP_RESTORE };
73
74 static inline void
75 vm_increase_sp (struct scm_vm *vp, SCM *new_sp, enum vm_increase_sp_kind kind)
76 {
77 if (new_sp <= vp->sp_max_since_gc)
78 {
79 vp->sp = new_sp;
80 return;
81 }
82
83 if (kind == VM_SP_PUSH && new_sp >= vp->stack_limit)
84 vm_expand_stack (vp, new_sp);
85 else
86 vp->sp_max_since_gc = vp->sp = new_sp;
87 }
88
89 static inline void
90 vm_push_sp (struct scm_vm *vp, SCM *new_sp)
91 {
92 vm_increase_sp (vp, new_sp, VM_SP_PUSH);
93 }
94
95 static inline void
96 vm_restore_sp (struct scm_vm *vp, SCM *new_sp)
97 {
98 vm_increase_sp (vp, new_sp, VM_SP_RESTORE);
99 }
100
101 \f
102 /*
103 * VM Continuation
104 */
105
106 void
107 scm_i_vm_cont_print (SCM x, SCM port, scm_print_state *pstate)
108 {
109 scm_puts_unlocked ("#<vm-continuation ", port);
110 scm_uintprint (SCM_UNPACK (x), 16, port);
111 scm_puts_unlocked (">", port);
112 }
113
114 int
115 scm_i_vm_cont_to_frame (SCM cont, struct scm_frame *frame)
116 {
117 struct scm_vm_cont *data = SCM_VM_CONT_DATA (cont);
118
119 frame->stack_holder = data;
120 frame->fp_offset = (data->fp + data->reloc) - data->stack_base;
121 frame->sp_offset = (data->sp + data->reloc) - data->stack_base;
122 frame->ip = data->ra;
123
124 return 1;
125 }
126
127 /* Ideally we could avoid copying the C stack if the continuation root
128 is inside VM code, and call/cc was invoked within that same call to
129 vm_run. That's currently not implemented. */
130 SCM
131 scm_i_vm_capture_stack (SCM *stack_base, SCM *fp, SCM *sp, scm_t_uint32 *ra,
132 scm_t_dynstack *dynstack, scm_t_uint32 flags)
133 {
134 struct scm_vm_cont *p;
135
136 p = scm_gc_malloc (sizeof (*p), "capture_vm_cont");
137 p->stack_size = sp - stack_base + 1;
138 p->stack_base = scm_gc_malloc (p->stack_size * sizeof (SCM),
139 "capture_vm_cont");
140 p->ra = ra;
141 p->sp = sp;
142 p->fp = fp;
143 memcpy (p->stack_base, stack_base, (sp + 1 - stack_base) * sizeof (SCM));
144 p->reloc = p->stack_base - stack_base;
145 p->dynstack = dynstack;
146 p->flags = flags;
147 return scm_cell (scm_tc7_vm_cont, (scm_t_bits)p);
148 }
149
150 struct return_to_continuation_data
151 {
152 struct scm_vm_cont *cp;
153 struct scm_vm *vp;
154 };
155
156 /* Called with the GC lock to prevent the stack marker from traversing a
157 stack in an inconsistent state. */
158 static void *
159 vm_return_to_continuation_inner (void *data_ptr)
160 {
161 struct return_to_continuation_data *data = data_ptr;
162 struct scm_vm *vp = data->vp;
163 struct scm_vm_cont *cp = data->cp;
164 scm_t_ptrdiff reloc;
165
166 /* We know that there is enough space for the continuation, because we
167 captured it in the past. However there may have been an expansion
168 since the capture, so we may have to re-link the frame
169 pointers. */
170 reloc = (vp->stack_base - (cp->stack_base - cp->reloc));
171 vp->fp = cp->fp + reloc;
172 memcpy (vp->stack_base, cp->stack_base, cp->stack_size * sizeof (SCM));
173 vm_restore_sp (vp, cp->sp + reloc);
174
175 if (reloc)
176 {
177 SCM *fp = vp->fp;
178 while (fp)
179 {
180 SCM *next_fp = SCM_FRAME_DYNAMIC_LINK (fp);
181 if (next_fp)
182 {
183 next_fp += reloc;
184 SCM_FRAME_SET_DYNAMIC_LINK (fp, next_fp);
185 }
186 fp = next_fp;
187 }
188 }
189
190 return NULL;
191 }
192
193 static void
194 vm_return_to_continuation (struct scm_vm *vp, SCM cont, size_t n, SCM *argv)
195 {
196 struct scm_vm_cont *cp;
197 SCM *argv_copy;
198 struct return_to_continuation_data data;
199
200 argv_copy = alloca (n * sizeof(SCM));
201 memcpy (argv_copy, argv, n * sizeof(SCM));
202
203 cp = SCM_VM_CONT_DATA (cont);
204
205 data.cp = cp;
206 data.vp = vp;
207 GC_call_with_alloc_lock (vm_return_to_continuation_inner, &data);
208
209 /* Now we have the continuation properly copied over. We just need to
210 copy the arguments. It is not guaranteed that there is actually
211 space for the arguments, though, so we have to bump the SP first. */
212 vm_push_sp (vp, vp->sp + 3 + n);
213
214 /* Now copy on an empty frame and the return values, as the
215 continuation expects. */
216 {
217 SCM *base = vp->sp + 1 - 3 - n;
218 size_t i;
219
220 for (i = 0; i < 3; i++)
221 base[i] = SCM_BOOL_F;
222
223 for (i = 0; i < n; i++)
224 base[i + 3] = argv_copy[i];
225 }
226
227 vp->ip = cp->ra;
228 }
229
230 static struct scm_vm * thread_vm (scm_i_thread *t);
231 SCM
232 scm_i_capture_current_stack (void)
233 {
234 scm_i_thread *thread;
235 struct scm_vm *vp;
236
237 thread = SCM_I_CURRENT_THREAD;
238 vp = thread_vm (thread);
239
240 return scm_i_vm_capture_stack (vp->stack_base, vp->fp, vp->sp, vp->ip,
241 scm_dynstack_capture_all (&thread->dynstack),
242 0);
243 }
244
245 static void vm_dispatch_apply_hook (struct scm_vm *vp) SCM_NOINLINE;
246 static void vm_dispatch_push_continuation_hook (struct scm_vm *vp) SCM_NOINLINE;
247 static void vm_dispatch_pop_continuation_hook (struct scm_vm *vp, SCM *old_fp) SCM_NOINLINE;
248 static void vm_dispatch_next_hook (struct scm_vm *vp) SCM_NOINLINE;
249 static void vm_dispatch_abort_hook (struct scm_vm *vp) SCM_NOINLINE;
250
251 static void
252 vm_dispatch_hook (struct scm_vm *vp, int hook_num, SCM *argv, int n)
253 {
254 SCM hook;
255 struct scm_frame c_frame;
256 scm_t_cell *frame;
257 int saved_trace_level;
258
259 hook = vp->hooks[hook_num];
260
261 if (SCM_LIKELY (scm_is_false (hook))
262 || scm_is_null (SCM_HOOK_PROCEDURES (hook)))
263 return;
264
265 saved_trace_level = vp->trace_level;
266 vp->trace_level = 0;
267
268 /* Allocate a frame object on the stack. This is more efficient than calling
269 `scm_c_make_frame ()' to allocate on the heap, but it forces hooks to not
270 capture frame objects.
271
272 At the same time, procedures such as `frame-procedure' make sense only
273 while the stack frame represented by the frame object is visible, so it
274 seems reasonable to limit the lifetime of frame objects. */
275
276 c_frame.stack_holder = vp;
277 c_frame.fp_offset = vp->fp - vp->stack_base;
278 c_frame.sp_offset = vp->sp - vp->stack_base;
279 c_frame.ip = vp->ip;
280
281 /* Arrange for FRAME to be 8-byte aligned, like any other cell. */
282 frame = alloca (sizeof (*frame) + 8);
283 frame = (scm_t_cell *) ROUND_UP ((scm_t_uintptr) frame, 8UL);
284
285 frame->word_0 = SCM_PACK (scm_tc7_frame | (SCM_VM_FRAME_KIND_VM << 8));
286 frame->word_1 = SCM_PACK_POINTER (&c_frame);
287
288 if (n == 0)
289 {
290 SCM args[1];
291
292 args[0] = SCM_PACK_POINTER (frame);
293 scm_c_run_hookn (hook, args, 1);
294 }
295 else if (n == 1)
296 {
297 SCM args[2];
298
299 args[0] = SCM_PACK_POINTER (frame);
300 args[1] = argv[0];
301 scm_c_run_hookn (hook, args, 2);
302 }
303 else
304 {
305 SCM args = SCM_EOL;
306
307 while (n--)
308 args = scm_cons (argv[n], args);
309 scm_c_run_hook (hook, scm_cons (SCM_PACK_POINTER (frame), args));
310 }
311
312 vp->trace_level = saved_trace_level;
313 }
314
315 static void
316 vm_dispatch_apply_hook (struct scm_vm *vp)
317 {
318 return vm_dispatch_hook (vp, SCM_VM_APPLY_HOOK, NULL, 0);
319 }
320 static void vm_dispatch_push_continuation_hook (struct scm_vm *vp)
321 {
322 return vm_dispatch_hook (vp, SCM_VM_PUSH_CONTINUATION_HOOK, NULL, 0);
323 }
324 static void vm_dispatch_pop_continuation_hook (struct scm_vm *vp, SCM *old_fp)
325 {
326 return vm_dispatch_hook (vp, SCM_VM_POP_CONTINUATION_HOOK,
327 &SCM_FRAME_LOCAL (old_fp, 1),
328 SCM_FRAME_NUM_LOCALS (old_fp, vp->sp) - 1);
329 }
330 static void vm_dispatch_next_hook (struct scm_vm *vp)
331 {
332 return vm_dispatch_hook (vp, SCM_VM_NEXT_HOOK, NULL, 0);
333 }
334 static void vm_dispatch_abort_hook (struct scm_vm *vp)
335 {
336 return vm_dispatch_hook (vp, SCM_VM_ABORT_CONTINUATION_HOOK,
337 &SCM_FRAME_LOCAL (vp->fp, 1),
338 SCM_FRAME_NUM_LOCALS (vp->fp, vp->sp) - 1);
339 }
340
341 static void
342 vm_abort (struct scm_vm *vp, SCM tag,
343 size_t nstack, SCM *stack_args, SCM tail, SCM *sp,
344 scm_i_jmp_buf *current_registers) SCM_NORETURN;
345
346 static void
347 vm_abort (struct scm_vm *vp, SCM tag,
348 size_t nstack, SCM *stack_args, SCM tail, SCM *sp,
349 scm_i_jmp_buf *current_registers)
350 {
351 size_t i;
352 ssize_t tail_len;
353 SCM *argv;
354
355 tail_len = scm_ilength (tail);
356 if (tail_len < 0)
357 scm_misc_error ("vm-engine", "tail values to abort should be a list",
358 scm_list_1 (tail));
359
360 argv = alloca ((nstack + tail_len) * sizeof (SCM));
361 for (i = 0; i < nstack; i++)
362 argv[i] = stack_args[i];
363 for (; i < nstack + tail_len; i++, tail = scm_cdr (tail))
364 argv[i] = scm_car (tail);
365
366 vp->sp = sp;
367
368 scm_c_abort (vp, tag, nstack + tail_len, argv, current_registers);
369 }
370
371 struct vm_reinstate_partial_continuation_data
372 {
373 struct scm_vm *vp;
374 struct scm_vm_cont *cp;
375 scm_t_ptrdiff reloc;
376 };
377
378 static void *
379 vm_reinstate_partial_continuation_inner (void *data_ptr)
380 {
381 struct vm_reinstate_partial_continuation_data *data = data_ptr;
382 struct scm_vm *vp = data->vp;
383 struct scm_vm_cont *cp = data->cp;
384 SCM *base;
385 scm_t_ptrdiff reloc;
386
387 base = SCM_FRAME_LOCALS_ADDRESS (vp->fp);
388 reloc = cp->reloc + (base - cp->stack_base);
389
390 memcpy (base, cp->stack_base, cp->stack_size * sizeof (SCM));
391
392 vp->fp = cp->fp + reloc;
393 vp->ip = cp->ra;
394
395 /* now relocate frame pointers */
396 {
397 SCM *fp;
398 for (fp = vp->fp;
399 SCM_FRAME_LOWER_ADDRESS (fp) >= base;
400 fp = SCM_FRAME_DYNAMIC_LINK (fp))
401 SCM_FRAME_SET_DYNAMIC_LINK (fp, SCM_FRAME_DYNAMIC_LINK (fp) + reloc);
402 }
403
404 data->reloc = reloc;
405
406 return NULL;
407 }
408
409 static void
410 vm_reinstate_partial_continuation (struct scm_vm *vp, SCM cont,
411 size_t n, SCM *argv,
412 scm_t_dynstack *dynstack,
413 scm_i_jmp_buf *registers)
414 {
415 struct vm_reinstate_partial_continuation_data data;
416 struct scm_vm_cont *cp;
417 SCM *argv_copy;
418 scm_t_ptrdiff reloc;
419 size_t i;
420
421 argv_copy = alloca (n * sizeof(SCM));
422 memcpy (argv_copy, argv, n * sizeof(SCM));
423
424 cp = SCM_VM_CONT_DATA (cont);
425
426 vm_push_sp (vp, SCM_FRAME_LOCALS_ADDRESS (vp->fp) + cp->stack_size + n - 1);
427
428 data.vp = vp;
429 data.cp = cp;
430 GC_call_with_alloc_lock (vm_reinstate_partial_continuation_inner, &data);
431 reloc = data.reloc;
432
433 /* Push the arguments. */
434 for (i = 0; i < n; i++)
435 vp->sp[i + 1 - n] = argv_copy[i];
436
437 /* The prompt captured a slice of the dynamic stack. Here we wind
438 those entries onto the current thread's stack. We also have to
439 relocate any prompts that we see along the way. */
440 {
441 scm_t_bits *walk;
442
443 for (walk = SCM_DYNSTACK_FIRST (cp->dynstack);
444 SCM_DYNSTACK_TAG (walk);
445 walk = SCM_DYNSTACK_NEXT (walk))
446 {
447 scm_t_bits tag = SCM_DYNSTACK_TAG (walk);
448
449 if (SCM_DYNSTACK_TAG_TYPE (tag) == SCM_DYNSTACK_TYPE_PROMPT)
450 scm_dynstack_wind_prompt (dynstack, walk, reloc, registers);
451 else
452 scm_dynstack_wind_1 (dynstack, walk);
453 }
454 }
455 }
456
457 \f
458 /*
459 * VM Error Handling
460 */
461
462 static void vm_error (const char *msg, SCM arg) SCM_NORETURN;
463 static void vm_error_bad_instruction (scm_t_uint32 inst) SCM_NORETURN SCM_NOINLINE;
464 static void vm_error_unbound (SCM sym) SCM_NORETURN SCM_NOINLINE;
465 static void vm_error_unbound_fluid (SCM fluid) SCM_NORETURN SCM_NOINLINE;
466 static void vm_error_not_a_variable (const char *func_name, SCM x) SCM_NORETURN SCM_NOINLINE;
467 static void vm_error_apply_to_non_list (SCM x) SCM_NORETURN SCM_NOINLINE;
468 static void vm_error_kwargs_length_not_even (SCM proc) SCM_NORETURN SCM_NOINLINE;
469 static void vm_error_kwargs_invalid_keyword (SCM proc, SCM obj) SCM_NORETURN SCM_NOINLINE;
470 static void vm_error_kwargs_unrecognized_keyword (SCM proc, SCM kw) SCM_NORETURN SCM_NOINLINE;
471 static void vm_error_too_many_args (int nargs) SCM_NORETURN SCM_NOINLINE;
472 static void vm_error_wrong_num_args (SCM proc) SCM_NORETURN SCM_NOINLINE;
473 static void vm_error_wrong_type_apply (SCM proc) SCM_NORETURN SCM_NOINLINE;
474 static void vm_error_stack_underflow (void) SCM_NORETURN SCM_NOINLINE;
475 static void vm_error_improper_list (SCM x) SCM_NORETURN SCM_NOINLINE;
476 static void vm_error_not_a_pair (const char *subr, SCM x) SCM_NORETURN SCM_NOINLINE;
477 static void vm_error_not_a_bytevector (const char *subr, SCM x) SCM_NORETURN SCM_NOINLINE;
478 static void vm_error_not_a_struct (const char *subr, SCM x) SCM_NORETURN SCM_NOINLINE;
479 static void vm_error_not_a_vector (const char *subr, SCM v) SCM_NORETURN SCM_NOINLINE;
480 static void vm_error_out_of_range (const char *subr, SCM k) SCM_NORETURN SCM_NOINLINE;
481 static void vm_error_no_values (void) SCM_NORETURN SCM_NOINLINE;
482 static void vm_error_not_enough_values (void) SCM_NORETURN SCM_NOINLINE;
483 static void vm_error_wrong_number_of_values (scm_t_uint32 expected) SCM_NORETURN SCM_NOINLINE;
484 static void vm_error_continuation_not_rewindable (SCM cont) SCM_NORETURN SCM_NOINLINE;
485 static void vm_error_bad_wide_string_length (size_t len) SCM_NORETURN SCM_NOINLINE;
486
487 static void
488 vm_error (const char *msg, SCM arg)
489 {
490 scm_throw (sym_vm_error,
491 scm_list_3 (sym_vm_run, scm_from_latin1_string (msg),
492 SCM_UNBNDP (arg) ? SCM_EOL : scm_list_1 (arg)));
493 abort(); /* not reached */
494 }
495
496 static void
497 vm_error_bad_instruction (scm_t_uint32 inst)
498 {
499 vm_error ("VM: Bad instruction: ~s", scm_from_uint32 (inst));
500 }
501
502 static void
503 vm_error_unbound (SCM sym)
504 {
505 scm_error_scm (scm_misc_error_key, SCM_BOOL_F,
506 scm_from_latin1_string ("Unbound variable: ~s"),
507 scm_list_1 (sym), SCM_BOOL_F);
508 }
509
510 static void
511 vm_error_unbound_fluid (SCM fluid)
512 {
513 scm_error_scm (scm_misc_error_key, SCM_BOOL_F,
514 scm_from_latin1_string ("Unbound fluid: ~s"),
515 scm_list_1 (fluid), SCM_BOOL_F);
516 }
517
518 static void
519 vm_error_not_a_variable (const char *func_name, SCM x)
520 {
521 scm_error (scm_arg_type_key, func_name, "Not a variable: ~S",
522 scm_list_1 (x), scm_list_1 (x));
523 }
524
525 static void
526 vm_error_apply_to_non_list (SCM x)
527 {
528 scm_error (scm_arg_type_key, "apply", "Apply to non-list: ~S",
529 scm_list_1 (x), scm_list_1 (x));
530 }
531
532 static void
533 vm_error_kwargs_length_not_even (SCM proc)
534 {
535 scm_error_scm (sym_keyword_argument_error, proc,
536 scm_from_latin1_string ("Odd length of keyword argument list"),
537 SCM_EOL, SCM_BOOL_F);
538 }
539
540 static void
541 vm_error_kwargs_invalid_keyword (SCM proc, SCM obj)
542 {
543 scm_error_scm (sym_keyword_argument_error, proc,
544 scm_from_latin1_string ("Invalid keyword"),
545 SCM_EOL, scm_list_1 (obj));
546 }
547
548 static void
549 vm_error_kwargs_unrecognized_keyword (SCM proc, SCM kw)
550 {
551 scm_error_scm (sym_keyword_argument_error, proc,
552 scm_from_latin1_string ("Unrecognized keyword"),
553 SCM_EOL, scm_list_1 (kw));
554 }
555
556 static void
557 vm_error_too_many_args (int nargs)
558 {
559 vm_error ("VM: Too many arguments", scm_from_int (nargs));
560 }
561
562 static void
563 vm_error_wrong_num_args (SCM proc)
564 {
565 scm_wrong_num_args (proc);
566 }
567
568 static void
569 vm_error_wrong_type_apply (SCM proc)
570 {
571 scm_error (scm_arg_type_key, NULL, "Wrong type to apply: ~S",
572 scm_list_1 (proc), scm_list_1 (proc));
573 }
574
575 static void
576 vm_error_stack_underflow (void)
577 {
578 vm_error ("VM: Stack underflow", SCM_UNDEFINED);
579 }
580
581 static void
582 vm_error_improper_list (SCM x)
583 {
584 vm_error ("Expected a proper list, but got object with tail ~s", x);
585 }
586
587 static void
588 vm_error_not_a_pair (const char *subr, SCM x)
589 {
590 scm_wrong_type_arg_msg (subr, 1, x, "pair");
591 }
592
593 static void
594 vm_error_not_a_bytevector (const char *subr, SCM x)
595 {
596 scm_wrong_type_arg_msg (subr, 1, x, "bytevector");
597 }
598
599 static void
600 vm_error_not_a_struct (const char *subr, SCM x)
601 {
602 scm_wrong_type_arg_msg (subr, 1, x, "struct");
603 }
604
605 static void
606 vm_error_not_a_vector (const char *subr, SCM x)
607 {
608 scm_wrong_type_arg_msg (subr, 1, x, "vector");
609 }
610
611 static void
612 vm_error_out_of_range (const char *subr, SCM k)
613 {
614 scm_to_size_t (k);
615 scm_out_of_range (subr, k);
616 }
617
618 static void
619 vm_error_no_values (void)
620 {
621 vm_error ("Zero values returned to single-valued continuation",
622 SCM_UNDEFINED);
623 }
624
625 static void
626 vm_error_not_enough_values (void)
627 {
628 vm_error ("Too few values returned to continuation", SCM_UNDEFINED);
629 }
630
631 static void
632 vm_error_wrong_number_of_values (scm_t_uint32 expected)
633 {
634 vm_error ("Wrong number of values returned to continuation (expected ~a)",
635 scm_from_uint32 (expected));
636 }
637
638 static void
639 vm_error_continuation_not_rewindable (SCM cont)
640 {
641 vm_error ("Unrewindable partial continuation", cont);
642 }
643
644 static void
645 vm_error_bad_wide_string_length (size_t len)
646 {
647 vm_error ("VM: Bad wide string length: ~S", scm_from_size_t (len));
648 }
649
650
651 \f
652
653 static SCM vm_boot_continuation;
654 static SCM vm_builtin_apply;
655 static SCM vm_builtin_values;
656 static SCM vm_builtin_abort_to_prompt;
657 static SCM vm_builtin_call_with_values;
658 static SCM vm_builtin_call_with_current_continuation;
659
660 static const scm_t_uint32 vm_boot_continuation_code[] = {
661 SCM_PACK_OP_24 (halt, 0)
662 };
663
664 static const scm_t_uint32 vm_builtin_apply_code[] = {
665 SCM_PACK_OP_24 (assert_nargs_ge, 3),
666 SCM_PACK_OP_24 (tail_apply, 0), /* proc in r1, args from r2 */
667 };
668
669 static const scm_t_uint32 vm_builtin_values_code[] = {
670 SCM_PACK_OP_24 (return_values, 0) /* vals from r1 */
671 };
672
673 static const scm_t_uint32 vm_builtin_abort_to_prompt_code[] = {
674 SCM_PACK_OP_24 (assert_nargs_ge, 2),
675 SCM_PACK_OP_24 (abort, 0), /* tag in r1, vals from r2 */
676 /* FIXME: Partial continuation should capture caller regs. */
677 SCM_PACK_OP_24 (return_values, 0) /* vals from r1 */
678 };
679
680 static const scm_t_uint32 vm_builtin_call_with_values_code[] = {
681 SCM_PACK_OP_24 (assert_nargs_ee, 3),
682 SCM_PACK_OP_24 (alloc_frame, 7),
683 SCM_PACK_OP_12_12 (mov, 6, 1),
684 SCM_PACK_OP_24 (call, 6), SCM_PACK_OP_ARG_8_24 (0, 1),
685 SCM_PACK_OP_12_12 (mov, 0, 2),
686 SCM_PACK_OP_24 (tail_call_shuffle, 7)
687 };
688
689 static const scm_t_uint32 vm_builtin_call_with_current_continuation_code[] = {
690 SCM_PACK_OP_24 (assert_nargs_ee, 2),
691 SCM_PACK_OP_24 (call_cc, 0)
692 };
693
694
695 static SCM
696 scm_vm_builtin_ref (unsigned idx)
697 {
698 switch (idx)
699 {
700 #define INDEX_TO_NAME(builtin, BUILTIN, req, opt, rest) \
701 case SCM_VM_BUILTIN_##BUILTIN: return vm_builtin_##builtin;
702 FOR_EACH_VM_BUILTIN(INDEX_TO_NAME)
703 #undef INDEX_TO_NAME
704 default: abort();
705 }
706 }
707
708 SCM scm_sym_apply;
709 static SCM scm_sym_values;
710 static SCM scm_sym_abort_to_prompt;
711 static SCM scm_sym_call_with_values;
712 static SCM scm_sym_call_with_current_continuation;
713
714 SCM
715 scm_vm_builtin_name_to_index (SCM name)
716 #define FUNC_NAME "builtin-name->index"
717 {
718 SCM_VALIDATE_SYMBOL (1, name);
719
720 #define NAME_TO_INDEX(builtin, BUILTIN, req, opt, rest) \
721 if (scm_is_eq (name, scm_sym_##builtin)) \
722 return scm_from_uint (SCM_VM_BUILTIN_##BUILTIN);
723 FOR_EACH_VM_BUILTIN(NAME_TO_INDEX)
724 #undef NAME_TO_INDEX
725
726 return SCM_BOOL_F;
727 }
728 #undef FUNC_NAME
729
730 SCM
731 scm_vm_builtin_index_to_name (SCM index)
732 #define FUNC_NAME "builtin-index->name"
733 {
734 unsigned idx;
735
736 SCM_VALIDATE_UINT_COPY (1, index, idx);
737
738 switch (idx)
739 {
740 #define INDEX_TO_NAME(builtin, BUILTIN, req, opt, rest) \
741 case SCM_VM_BUILTIN_##BUILTIN: return scm_sym_##builtin;
742 FOR_EACH_VM_BUILTIN(INDEX_TO_NAME)
743 #undef INDEX_TO_NAME
744 default: return SCM_BOOL_F;
745 }
746 }
747 #undef FUNC_NAME
748
749 static void
750 scm_init_vm_builtins (void)
751 {
752 scm_c_define_gsubr ("builtin-name->index", 1, 0, 0,
753 scm_vm_builtin_name_to_index);
754 scm_c_define_gsubr ("builtin-index->name", 1, 0, 0,
755 scm_vm_builtin_index_to_name);
756 }
757
758 SCM
759 scm_i_call_with_current_continuation (SCM proc)
760 {
761 return scm_call_1 (vm_builtin_call_with_current_continuation, proc);
762 }
763
764 \f
765 /*
766 * VM
767 */
768
769 #define VM_NAME vm_regular_engine
770 #define VM_USE_HOOKS 0
771 #define FUNC_NAME "vm-regular-engine"
772 #include "vm-engine.c"
773 #undef FUNC_NAME
774 #undef VM_USE_HOOKS
775 #undef VM_NAME
776
777 #define VM_NAME vm_debug_engine
778 #define VM_USE_HOOKS 1
779 #define FUNC_NAME "vm-debug-engine"
780 #include "vm-engine.c"
781 #undef FUNC_NAME
782 #undef VM_USE_HOOKS
783 #undef VM_NAME
784
785 typedef SCM (*scm_t_vm_engine) (scm_i_thread *current_thread, struct scm_vm *vp,
786 scm_i_jmp_buf *registers, int resume);
787
788 static const scm_t_vm_engine vm_engines[SCM_VM_NUM_ENGINES] =
789 { vm_regular_engine, vm_debug_engine };
790
791 static SCM*
792 allocate_stack (size_t size)
793 #define FUNC_NAME "make_vm"
794 {
795 void *ret;
796
797 if (size >= ((size_t) -1) / sizeof (SCM))
798 abort ();
799
800 size *= sizeof (SCM);
801
802 #if HAVE_SYS_MMAN_H
803 ret = mmap (NULL, size, PROT_READ | PROT_WRITE,
804 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
805 if (ret == MAP_FAILED)
806 ret = NULL;
807 #else
808 ret = malloc (size);
809 #endif
810
811 if (!ret)
812 {
813 perror ("allocate_stack failed");
814 return NULL;
815 }
816
817 return (SCM *) ret;
818 }
819 #undef FUNC_NAME
820
821 static void
822 free_stack (SCM *stack, size_t size)
823 {
824 size *= sizeof (SCM);
825
826 #if HAVE_SYS_MMAN_H
827 munmap (stack, size);
828 #else
829 free (stack);
830 #endif
831 }
832
833 static SCM*
834 expand_stack (SCM *old_stack, size_t old_size, size_t new_size)
835 #define FUNC_NAME "expand_stack"
836 {
837 #if defined MREMAP_MAYMOVE
838 void *new_stack;
839
840 if (new_size >= ((size_t) -1) / sizeof (SCM))
841 abort ();
842
843 old_size *= sizeof (SCM);
844 new_size *= sizeof (SCM);
845
846 new_stack = mremap (old_stack, old_size, new_size, MREMAP_MAYMOVE);
847 if (new_stack == MAP_FAILED)
848 return NULL;
849
850 return (SCM *) new_stack;
851 #else
852 SCM *new_stack;
853
854 new_stack = allocate_stack (new_size);
855 if (!new_stack)
856 return NULL;
857
858 memcpy (new_stack, old_stack, old_size * sizeof (SCM));
859 free_stack (old_stack, old_size);
860
861 return new_stack;
862 #endif
863 }
864 #undef FUNC_NAME
865
866 static struct scm_vm *
867 make_vm (void)
868 #define FUNC_NAME "make_vm"
869 {
870 int i;
871 struct scm_vm *vp;
872
873 vp = scm_gc_malloc (sizeof (struct scm_vm), "vm");
874
875 vp->stack_size = page_size / sizeof (SCM);
876 vp->stack_base = allocate_stack (vp->stack_size);
877 if (!vp->stack_base)
878 /* As in expand_stack, we don't have any way to throw an exception
879 if we can't allocate one measely page -- there's no stack to
880 handle it. For now, abort. */
881 abort ();
882 vp->stack_limit = vp->stack_base + vp->stack_size;
883 vp->overflow_handler_stack = SCM_EOL;
884 vp->ip = NULL;
885 vp->sp = vp->stack_base - 1;
886 vp->fp = NULL;
887 vp->engine = vm_default_engine;
888 vp->trace_level = 0;
889 for (i = 0; i < SCM_VM_NUM_HOOKS; i++)
890 vp->hooks[i] = SCM_BOOL_F;
891
892 return vp;
893 }
894 #undef FUNC_NAME
895
896 static void
897 return_unused_stack_to_os (struct scm_vm *vp)
898 {
899 #if HAVE_SYS_MMAN_H
900 scm_t_uintptr start = (scm_t_uintptr) (vp->sp + 1);
901 scm_t_uintptr end = (scm_t_uintptr) vp->stack_limit;
902 /* The second condition is needed to protect against wrap-around. */
903 if (vp->sp_max_since_gc < vp->stack_limit && vp->sp < vp->sp_max_since_gc)
904 end = (scm_t_uintptr) (vp->sp_max_since_gc + 1);
905
906 start = ((start - 1U) | (page_size - 1U)) + 1U; /* round up */
907 end = ((end - 1U) | (page_size - 1U)) + 1U; /* round up */
908
909 /* Return these pages to the OS. The next time they are paged in,
910 they will be zeroed. */
911 if (start < end)
912 {
913 int ret = 0;
914
915 do
916 ret = madvise ((void *) start, end - start, MADV_DONTNEED);
917 while (ret && errno == -EAGAIN);
918
919 if (ret)
920 perror ("madvise failed");
921 }
922
923 vp->sp_max_since_gc = vp->sp;
924 #endif
925 }
926
927 #define DEAD_SLOT_MAP_CACHE_SIZE 32U
928 struct dead_slot_map_cache_entry
929 {
930 scm_t_uint32 *ip;
931 const scm_t_uint8 *map;
932 };
933
934 struct dead_slot_map_cache
935 {
936 struct dead_slot_map_cache_entry entries[DEAD_SLOT_MAP_CACHE_SIZE];
937 };
938
939 static const scm_t_uint8 *
940 find_dead_slot_map (scm_t_uint32 *ip, struct dead_slot_map_cache *cache)
941 {
942 /* The lower two bits should be zero. FIXME: Use a better hash
943 function; we don't expose scm_raw_hashq currently. */
944 size_t slot = (((scm_t_uintptr) ip) >> 2) % DEAD_SLOT_MAP_CACHE_SIZE;
945 const scm_t_uint8 *map;
946
947 if (cache->entries[slot].ip == ip)
948 map = cache->entries[slot].map;
949 else
950 {
951 map = scm_find_dead_slot_map_unlocked (ip);
952 cache->entries[slot].ip = ip;
953 cache->entries[slot].map = map;
954 }
955
956 return map;
957 }
958
959 /* Mark the VM stack region between its base and its current top. */
960 struct GC_ms_entry *
961 scm_i_vm_mark_stack (struct scm_vm *vp, struct GC_ms_entry *mark_stack_ptr,
962 struct GC_ms_entry *mark_stack_limit)
963 {
964 SCM *sp, *fp;
965 /* The first frame will be marked conservatively (without a dead
966 slot map). This is because GC can happen at any point within the
967 hottest activation, due to multiple threads or per-instruction
968 hooks, and providing dead slot maps for all points in a program
969 would take a prohibitive amount of space. */
970 const scm_t_uint8 *dead_slots = NULL;
971 scm_t_uintptr upper = (scm_t_uintptr) GC_greatest_plausible_heap_addr;
972 scm_t_uintptr lower = (scm_t_uintptr) GC_least_plausible_heap_addr;
973 struct dead_slot_map_cache cache;
974
975 memset (&cache, 0, sizeof (cache));
976
977 for (fp = vp->fp, sp = vp->sp; fp; fp = SCM_FRAME_DYNAMIC_LINK (fp))
978 {
979 for (; sp >= &SCM_FRAME_LOCAL (fp, 0); sp--)
980 {
981 SCM elt = *sp;
982 if (SCM_NIMP (elt)
983 && SCM_UNPACK (elt) >= lower && SCM_UNPACK (elt) <= upper)
984 {
985 if (dead_slots)
986 {
987 size_t slot = sp - &SCM_FRAME_LOCAL (fp, 0);
988 if (dead_slots[slot / 8U] & (1U << (slot % 8U)))
989 {
990 /* This value may become dead as a result of GC,
991 so we can't just leave it on the stack. */
992 *sp = SCM_UNBOUND;
993 continue;
994 }
995 }
996
997 mark_stack_ptr = GC_mark_and_push ((void *) elt,
998 mark_stack_ptr,
999 mark_stack_limit,
1000 NULL);
1001 }
1002 }
1003 sp = SCM_FRAME_PREVIOUS_SP (fp);
1004 /* Inner frames may have a dead slots map for precise marking.
1005 Note that there may be other reasons to not have a dead slots
1006 map, e.g. if all of the frame's slots below the callee frame
1007 are live. */
1008 dead_slots = find_dead_slot_map (SCM_FRAME_RETURN_ADDRESS (fp), &cache);
1009 }
1010
1011 return_unused_stack_to_os (vp);
1012
1013 return mark_stack_ptr;
1014 }
1015
1016 /* Free the VM stack, as this thread is exiting. */
1017 void
1018 scm_i_vm_free_stack (struct scm_vm *vp)
1019 {
1020 free_stack (vp->stack_base, vp->stack_size);
1021 vp->stack_base = vp->stack_limit = NULL;
1022 vp->stack_size = 0;
1023 }
1024
1025 struct vm_expand_stack_data
1026 {
1027 struct scm_vm *vp;
1028 size_t stack_size;
1029 SCM *new_sp;
1030 };
1031
1032 static void *
1033 vm_expand_stack_inner (void *data_ptr)
1034 {
1035 struct vm_expand_stack_data *data = data_ptr;
1036
1037 struct scm_vm *vp = data->vp;
1038 SCM *old_stack, *new_stack;
1039 size_t new_size;
1040 scm_t_ptrdiff reloc;
1041
1042 new_size = vp->stack_size;
1043 while (new_size < data->stack_size)
1044 new_size *= 2;
1045 old_stack = vp->stack_base;
1046
1047 new_stack = expand_stack (vp->stack_base, vp->stack_size, new_size);
1048 if (!new_stack)
1049 return NULL;
1050
1051 vp->stack_base = new_stack;
1052 vp->stack_size = new_size;
1053 vp->stack_limit = vp->stack_base + new_size;
1054 reloc = vp->stack_base - old_stack;
1055
1056 if (reloc)
1057 {
1058 SCM *fp;
1059 if (vp->fp)
1060 vp->fp += reloc;
1061 data->new_sp += reloc;
1062 fp = vp->fp;
1063 while (fp)
1064 {
1065 SCM *next_fp = SCM_FRAME_DYNAMIC_LINK (fp);
1066 if (next_fp)
1067 {
1068 next_fp += reloc;
1069 SCM_FRAME_SET_DYNAMIC_LINK (fp, next_fp);
1070 }
1071 fp = next_fp;
1072 }
1073 }
1074
1075 return new_stack;
1076 }
1077
1078 static scm_t_ptrdiff
1079 current_overflow_size (struct scm_vm *vp)
1080 {
1081 if (scm_is_pair (vp->overflow_handler_stack))
1082 return scm_to_ptrdiff_t (scm_caar (vp->overflow_handler_stack));
1083 return -1;
1084 }
1085
1086 static int
1087 should_handle_stack_overflow (struct scm_vm *vp, scm_t_ptrdiff stack_size)
1088 {
1089 scm_t_ptrdiff overflow_size = current_overflow_size (vp);
1090 return overflow_size >= 0 && stack_size >= overflow_size;
1091 }
1092
1093 static void
1094 reset_stack_limit (struct scm_vm *vp)
1095 {
1096 if (should_handle_stack_overflow (vp, vp->stack_size))
1097 vp->stack_limit = vp->stack_base + current_overflow_size (vp);
1098 else
1099 vp->stack_limit = vp->stack_base + vp->stack_size;
1100 }
1101
1102 struct overflow_handler_data
1103 {
1104 struct scm_vm *vp;
1105 SCM overflow_handler_stack;
1106 };
1107
1108 static void
1109 wind_overflow_handler (void *ptr)
1110 {
1111 struct overflow_handler_data *data = ptr;
1112
1113 data->vp->overflow_handler_stack = data->overflow_handler_stack;
1114
1115 reset_stack_limit (data->vp);
1116 }
1117
1118 static void
1119 unwind_overflow_handler (void *ptr)
1120 {
1121 struct overflow_handler_data *data = ptr;
1122
1123 data->vp->overflow_handler_stack = scm_cdr (data->overflow_handler_stack);
1124
1125 reset_stack_limit (data->vp);
1126 }
1127
1128 static void
1129 vm_expand_stack (struct scm_vm *vp, SCM *new_sp)
1130 {
1131 scm_t_ptrdiff stack_size = new_sp + 1 - vp->stack_base;
1132
1133 if (stack_size > vp->stack_size)
1134 {
1135 struct vm_expand_stack_data data;
1136
1137 data.vp = vp;
1138 data.stack_size = stack_size;
1139 data.new_sp = new_sp;
1140
1141 if (!GC_call_with_alloc_lock (vm_expand_stack_inner, &data))
1142 /* Throw an unwind-only exception. */
1143 scm_report_stack_overflow ();
1144
1145 new_sp = data.new_sp;
1146 }
1147
1148 vp->sp_max_since_gc = vp->sp = new_sp;
1149
1150 if (should_handle_stack_overflow (vp, stack_size))
1151 {
1152 SCM more_stack, new_limit;
1153
1154 struct overflow_handler_data data;
1155 data.vp = vp;
1156 data.overflow_handler_stack = vp->overflow_handler_stack;
1157
1158 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
1159
1160 scm_dynwind_rewind_handler (unwind_overflow_handler, &data,
1161 SCM_F_WIND_EXPLICITLY);
1162 scm_dynwind_unwind_handler (wind_overflow_handler, &data,
1163 SCM_F_WIND_EXPLICITLY);
1164
1165 /* Call the overflow handler. */
1166 more_stack = scm_call_0 (scm_cdar (data.overflow_handler_stack));
1167
1168 /* If the overflow handler returns, its return value should be an
1169 integral number of words from the outer stack limit to transfer
1170 to the inner limit. */
1171 if (scm_to_ptrdiff_t (more_stack) <= 0)
1172 scm_out_of_range (NULL, more_stack);
1173 new_limit = scm_sum (scm_caar (data.overflow_handler_stack), more_stack);
1174 if (scm_is_pair (scm_cdr (data.overflow_handler_stack)))
1175 new_limit = scm_min (new_limit,
1176 scm_caadr (data.overflow_handler_stack));
1177
1178 /* Ensure the new limit is in range. */
1179 scm_to_ptrdiff_t (new_limit);
1180
1181 /* Increase the limit that we will restore. */
1182 scm_set_car_x (scm_car (data.overflow_handler_stack), new_limit);
1183
1184 scm_dynwind_end ();
1185
1186 /* Recurse */
1187 return vm_expand_stack (vp, new_sp);
1188 }
1189 }
1190
1191 static struct scm_vm *
1192 thread_vm (scm_i_thread *t)
1193 {
1194 if (SCM_UNLIKELY (!t->vp))
1195 t->vp = make_vm ();
1196
1197 return t->vp;
1198 }
1199
1200 struct scm_vm *
1201 scm_the_vm (void)
1202 {
1203 return thread_vm (SCM_I_CURRENT_THREAD);
1204 }
1205
1206 SCM
1207 scm_call_n (SCM proc, SCM *argv, size_t nargs)
1208 {
1209 scm_i_thread *thread;
1210 struct scm_vm *vp;
1211 SCM *base;
1212 ptrdiff_t base_frame_size;
1213 /* Cached variables. */
1214 scm_i_jmp_buf registers; /* used for prompts */
1215 size_t i;
1216
1217 thread = SCM_I_CURRENT_THREAD;
1218 vp = thread_vm (thread);
1219
1220 SCM_CHECK_STACK;
1221
1222 /* Check that we have enough space: 3 words for the boot continuation,
1223 and 3 + nargs for the procedure application. */
1224 base_frame_size = 3 + 3 + nargs;
1225 vm_push_sp (vp, vp->sp + base_frame_size);
1226 base = vp->sp + 1 - base_frame_size;
1227
1228 /* Since it's possible to receive the arguments on the stack itself,
1229 shuffle up the arguments first. */
1230 for (i = nargs; i > 0; i--)
1231 base[6 + i - 1] = argv[i - 1];
1232
1233 /* Push the boot continuation, which calls PROC and returns its
1234 result(s). */
1235 base[0] = SCM_PACK (vp->fp); /* dynamic link */
1236 base[1] = SCM_PACK (vp->ip); /* ra */
1237 base[2] = vm_boot_continuation;
1238 vp->fp = &base[2];
1239 vp->ip = (scm_t_uint32 *) vm_boot_continuation_code;
1240
1241 /* The pending call to PROC. */
1242 base[3] = SCM_PACK (vp->fp); /* dynamic link */
1243 base[4] = SCM_PACK (vp->ip); /* ra */
1244 base[5] = proc;
1245 vp->fp = &base[5];
1246
1247 {
1248 int resume = SCM_I_SETJMP (registers);
1249
1250 if (SCM_UNLIKELY (resume))
1251 {
1252 scm_gc_after_nonlocal_exit ();
1253 /* Non-local return. */
1254 vm_dispatch_abort_hook (vp);
1255 }
1256
1257 return vm_engines[vp->engine](thread, vp, &registers, resume);
1258 }
1259 }
1260
1261 /* Scheme interface */
1262
1263 #define VM_DEFINE_HOOK(n) \
1264 { \
1265 struct scm_vm *vp; \
1266 vp = scm_the_vm (); \
1267 if (scm_is_false (vp->hooks[n])) \
1268 vp->hooks[n] = scm_make_hook (SCM_I_MAKINUM (1)); \
1269 return vp->hooks[n]; \
1270 }
1271
1272 SCM_DEFINE (scm_vm_apply_hook, "vm-apply-hook", 0, 0, 0,
1273 (void),
1274 "")
1275 #define FUNC_NAME s_scm_vm_apply_hook
1276 {
1277 VM_DEFINE_HOOK (SCM_VM_APPLY_HOOK);
1278 }
1279 #undef FUNC_NAME
1280
1281 SCM_DEFINE (scm_vm_push_continuation_hook, "vm-push-continuation-hook", 0, 0, 0,
1282 (void),
1283 "")
1284 #define FUNC_NAME s_scm_vm_push_continuation_hook
1285 {
1286 VM_DEFINE_HOOK (SCM_VM_PUSH_CONTINUATION_HOOK);
1287 }
1288 #undef FUNC_NAME
1289
1290 SCM_DEFINE (scm_vm_pop_continuation_hook, "vm-pop-continuation-hook", 0, 0, 0,
1291 (void),
1292 "")
1293 #define FUNC_NAME s_scm_vm_pop_continuation_hook
1294 {
1295 VM_DEFINE_HOOK (SCM_VM_POP_CONTINUATION_HOOK);
1296 }
1297 #undef FUNC_NAME
1298
1299 SCM_DEFINE (scm_vm_next_hook, "vm-next-hook", 0, 0, 0,
1300 (void),
1301 "")
1302 #define FUNC_NAME s_scm_vm_next_hook
1303 {
1304 VM_DEFINE_HOOK (SCM_VM_NEXT_HOOK);
1305 }
1306 #undef FUNC_NAME
1307
1308 SCM_DEFINE (scm_vm_abort_continuation_hook, "vm-abort-continuation-hook", 0, 0, 0,
1309 (void),
1310 "")
1311 #define FUNC_NAME s_scm_vm_abort_continuation_hook
1312 {
1313 VM_DEFINE_HOOK (SCM_VM_ABORT_CONTINUATION_HOOK);
1314 }
1315 #undef FUNC_NAME
1316
1317 SCM_DEFINE (scm_vm_trace_level, "vm-trace-level", 0, 0, 0,
1318 (void),
1319 "")
1320 #define FUNC_NAME s_scm_vm_trace_level
1321 {
1322 return scm_from_int (scm_the_vm ()->trace_level);
1323 }
1324 #undef FUNC_NAME
1325
1326 SCM_DEFINE (scm_set_vm_trace_level_x, "set-vm-trace-level!", 1, 0, 0,
1327 (SCM level),
1328 "")
1329 #define FUNC_NAME s_scm_set_vm_trace_level_x
1330 {
1331 scm_the_vm ()->trace_level = scm_to_int (level);
1332 return SCM_UNSPECIFIED;
1333 }
1334 #undef FUNC_NAME
1335
1336 \f
1337 /*
1338 * VM engines
1339 */
1340
1341 static int
1342 symbol_to_vm_engine (SCM engine, const char *FUNC_NAME)
1343 {
1344 if (scm_is_eq (engine, sym_regular))
1345 return SCM_VM_REGULAR_ENGINE;
1346 else if (scm_is_eq (engine, sym_debug))
1347 return SCM_VM_DEBUG_ENGINE;
1348 else
1349 SCM_MISC_ERROR ("Unknown VM engine: ~a", scm_list_1 (engine));
1350 }
1351
1352 static SCM
1353 vm_engine_to_symbol (int engine, const char *FUNC_NAME)
1354 {
1355 switch (engine)
1356 {
1357 case SCM_VM_REGULAR_ENGINE:
1358 return sym_regular;
1359 case SCM_VM_DEBUG_ENGINE:
1360 return sym_debug;
1361 default:
1362 /* ? */
1363 SCM_MISC_ERROR ("Unknown VM engine: ~a",
1364 scm_list_1 (scm_from_int (engine)));
1365 }
1366 }
1367
1368 SCM_DEFINE (scm_vm_engine, "vm-engine", 0, 0, 0,
1369 (void),
1370 "")
1371 #define FUNC_NAME s_scm_vm_engine
1372 {
1373 return vm_engine_to_symbol (scm_the_vm ()->engine, FUNC_NAME);
1374 }
1375 #undef FUNC_NAME
1376
1377 void
1378 scm_c_set_vm_engine_x (int engine)
1379 #define FUNC_NAME "set-vm-engine!"
1380 {
1381 if (engine < 0 || engine >= SCM_VM_NUM_ENGINES)
1382 SCM_MISC_ERROR ("Unknown VM engine: ~a",
1383 scm_list_1 (scm_from_int (engine)));
1384
1385 scm_the_vm ()->engine = engine;
1386 }
1387 #undef FUNC_NAME
1388
1389 SCM_DEFINE (scm_set_vm_engine_x, "set-vm-engine!", 1, 0, 0,
1390 (SCM engine),
1391 "")
1392 #define FUNC_NAME s_scm_set_vm_engine_x
1393 {
1394 scm_c_set_vm_engine_x (symbol_to_vm_engine (engine, FUNC_NAME));
1395 return SCM_UNSPECIFIED;
1396 }
1397 #undef FUNC_NAME
1398
1399 void
1400 scm_c_set_default_vm_engine_x (int engine)
1401 #define FUNC_NAME "set-default-vm-engine!"
1402 {
1403 if (engine < 0 || engine >= SCM_VM_NUM_ENGINES)
1404 SCM_MISC_ERROR ("Unknown VM engine: ~a",
1405 scm_list_1 (scm_from_int (engine)));
1406
1407 vm_default_engine = engine;
1408 }
1409 #undef FUNC_NAME
1410
1411 SCM_DEFINE (scm_set_default_vm_engine_x, "set-default-vm-engine!", 1, 0, 0,
1412 (SCM engine),
1413 "")
1414 #define FUNC_NAME s_scm_set_default_vm_engine_x
1415 {
1416 scm_c_set_default_vm_engine_x (symbol_to_vm_engine (engine, FUNC_NAME));
1417 return SCM_UNSPECIFIED;
1418 }
1419 #undef FUNC_NAME
1420
1421 /* FIXME: This function makes no sense, but we keep it to make sure we
1422 have a way of switching to the debug or regular VM. */
1423 SCM_DEFINE (scm_call_with_vm, "call-with-vm", 1, 0, 1,
1424 (SCM proc, SCM args),
1425 "Apply @var{proc} to @var{args} in a dynamic extent in which\n"
1426 "@var{vm} is the current VM.")
1427 #define FUNC_NAME s_scm_call_with_vm
1428 {
1429 return scm_apply_0 (proc, args);
1430 }
1431 #undef FUNC_NAME
1432
1433 SCM_DEFINE (scm_call_with_stack_overflow_handler,
1434 "call-with-stack-overflow-handler", 3, 0, 0,
1435 (SCM limit, SCM thunk, SCM handler),
1436 "Call @var{thunk} in an environment in which the stack limit has\n"
1437 "been reduced to @var{limit} additional words. If the limit is\n"
1438 "reached, @var{handler} (a thunk) will be invoked in the dynamic\n"
1439 "environment of the error. For the extent of the call to\n"
1440 "@var{handler}, the stack limit and handler are restored to the\n"
1441 "values that were in place when\n"
1442 "@code{call-with-stack-overflow-handler} was called.")
1443 #define FUNC_NAME s_scm_call_with_stack_overflow_handler
1444 {
1445 struct scm_vm *vp;
1446 scm_t_ptrdiff c_limit, stack_size;
1447 struct overflow_handler_data data;
1448 SCM new_limit, ret;
1449
1450 vp = scm_the_vm ();
1451 stack_size = vp->sp - vp->stack_base;
1452
1453 c_limit = scm_to_ptrdiff_t (limit);
1454 if (c_limit <= 0)
1455 scm_out_of_range (FUNC_NAME, limit);
1456
1457 new_limit = scm_sum (scm_from_ptrdiff_t (stack_size), limit);
1458 if (scm_is_pair (vp->overflow_handler_stack))
1459 new_limit = scm_min (new_limit, scm_caar (vp->overflow_handler_stack));
1460
1461 /* Hacky check that the current stack depth plus the limit is within
1462 the range of a ptrdiff_t. */
1463 scm_to_ptrdiff_t (new_limit);
1464
1465 data.vp = vp;
1466 data.overflow_handler_stack =
1467 scm_acons (limit, handler, vp->overflow_handler_stack);
1468
1469 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
1470
1471 scm_dynwind_rewind_handler (wind_overflow_handler, &data,
1472 SCM_F_WIND_EXPLICITLY);
1473 scm_dynwind_unwind_handler (unwind_overflow_handler, &data,
1474 SCM_F_WIND_EXPLICITLY);
1475
1476 /* Reset vp->sp_max_since_gc so that the VM checks actually
1477 trigger. */
1478 return_unused_stack_to_os (vp);
1479
1480 ret = scm_call_0 (thunk);
1481
1482 scm_dynwind_end ();
1483
1484 return ret;
1485 }
1486 #undef FUNC_NAME
1487
1488 \f
1489 /*
1490 * Initialize
1491 */
1492
1493 SCM
1494 scm_load_compiled_with_vm (SCM file)
1495 {
1496 return scm_call_0 (scm_load_thunk_from_file (file));
1497 }
1498
1499
1500 void
1501 scm_init_vm_builtin_properties (void)
1502 {
1503 /* FIXME: Seems hacky to do this here, but oh well :/ */
1504 scm_sym_apply = scm_from_utf8_symbol ("apply");
1505 scm_sym_values = scm_from_utf8_symbol ("values");
1506 scm_sym_abort_to_prompt = scm_from_utf8_symbol ("abort-to-prompt");
1507 scm_sym_call_with_values = scm_from_utf8_symbol ("call-with-values");
1508 scm_sym_call_with_current_continuation =
1509 scm_from_utf8_symbol ("call-with-current-continuation");
1510
1511 #define INIT_BUILTIN(builtin, BUILTIN, req, opt, rest) \
1512 scm_set_procedure_property_x (vm_builtin_##builtin, scm_sym_name, \
1513 scm_sym_##builtin); \
1514 scm_set_procedure_minimum_arity_x (vm_builtin_##builtin, \
1515 SCM_I_MAKINUM (req), \
1516 SCM_I_MAKINUM (opt), \
1517 scm_from_bool (rest));
1518 FOR_EACH_VM_BUILTIN (INIT_BUILTIN);
1519 #undef INIT_BUILTIN
1520 }
1521
1522 void
1523 scm_bootstrap_vm (void)
1524 {
1525 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
1526 "scm_init_vm",
1527 (scm_t_extension_init_func)scm_init_vm, NULL);
1528 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
1529 "scm_init_vm_builtins",
1530 (scm_t_extension_init_func)scm_init_vm_builtins,
1531 NULL);
1532
1533 page_size = getpagesize ();
1534 /* page_size should be a power of two. */
1535 if (page_size & (page_size - 1))
1536 abort ();
1537
1538 sym_vm_run = scm_from_latin1_symbol ("vm-run");
1539 sym_vm_error = scm_from_latin1_symbol ("vm-error");
1540 sym_keyword_argument_error = scm_from_latin1_symbol ("keyword-argument-error");
1541 sym_regular = scm_from_latin1_symbol ("regular");
1542 sym_debug = scm_from_latin1_symbol ("debug");
1543
1544 vm_boot_continuation = scm_i_make_program (vm_boot_continuation_code);
1545 SCM_SET_CELL_WORD_0 (vm_boot_continuation,
1546 (SCM_CELL_WORD_0 (vm_boot_continuation)
1547 | SCM_F_PROGRAM_IS_BOOT));
1548
1549 #define DEFINE_BUILTIN(builtin, BUILTIN, req, opt, rest) \
1550 vm_builtin_##builtin = scm_i_make_program (vm_builtin_##builtin##_code);
1551 FOR_EACH_VM_BUILTIN (DEFINE_BUILTIN);
1552 #undef DEFINE_BUILTIN
1553 }
1554
1555 void
1556 scm_init_vm (void)
1557 {
1558 #ifndef SCM_MAGIC_SNARFER
1559 #include "libguile/vm.x"
1560 #endif
1561 }
1562
1563 /*
1564 Local Variables:
1565 c-file-style: "gnu"
1566 End:
1567 */