Better vm.c support for RTL programs
[bpt/guile.git] / libguile / vm.c
1 /* Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013 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 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <stdlib.h>
24 #include <alloca.h>
25 #include <alignof.h>
26 #include <string.h>
27 #include <stdint.h>
28
29 #include "libguile/bdw-gc.h"
30 #include <gc/gc_mark.h>
31
32 #include "_scm.h"
33 #include "control.h"
34 #include "frames.h"
35 #include "instructions.h"
36 #include "objcodes.h"
37 #include "programs.h"
38 #include "vm.h"
39
40 #include "private-gc.h" /* scm_getenv_int */
41
42 static int vm_default_engine = SCM_VM_REGULAR_ENGINE;
43
44 /* Unfortunately we can't snarf these: snarfed things are only loaded up from
45 (system vm vm), which might not be loaded before an error happens. */
46 static SCM sym_vm_run;
47 static SCM sym_vm_error;
48 static SCM sym_keyword_argument_error;
49 static SCM sym_regular;
50 static SCM sym_debug;
51
52 /* The VM has a number of internal assertions that shouldn't normally be
53 necessary, but might be if you think you found a bug in the VM. */
54 #define VM_ENABLE_ASSERTIONS
55
56 /* We can add a mode that ensures that all stack items above the stack pointer
57 are NULL. This is useful for checking the internal consistency of the VM's
58 assumptions and its operators, but isn't necessary for normal operation. It
59 will ensure that assertions are enabled. Slows down the VM by about 30%. */
60 /* NB! If you enable this, search for NULLING in throw.c */
61 /* #define VM_ENABLE_STACK_NULLING */
62
63 /* #define VM_ENABLE_PARANOID_ASSERTIONS */
64
65 #if defined (VM_ENABLE_STACK_NULLING) && !defined (VM_ENABLE_ASSERTIONS)
66 #define VM_ENABLE_ASSERTIONS
67 #endif
68
69 /* When defined, arrange so that the GC doesn't scan the VM stack beyond its
70 current SP. This should help avoid excess data retention. See
71 http://thread.gmane.org/gmane.comp.programming.garbage-collection.boehmgc/3001
72 for a discussion. */
73 #define VM_ENABLE_PRECISE_STACK_GC_SCAN
74
75 /* Size in SCM objects of the stack reserve. The reserve is used to run
76 exception handling code in case of a VM stack overflow. */
77 #define VM_STACK_RESERVE_SIZE 512
78
79
80 \f
81 /*
82 * VM Continuation
83 */
84
85 void
86 scm_i_vm_cont_print (SCM x, SCM port, scm_print_state *pstate)
87 {
88 scm_puts_unlocked ("#<vm-continuation ", port);
89 scm_uintprint (SCM_UNPACK (x), 16, port);
90 scm_puts_unlocked (">", port);
91 }
92
93 /* In theory, a number of vm instances can be active in the call trace, and we
94 only want to reify the continuations of those in the current continuation
95 root. I don't see a nice way to do this -- ideally it would involve dynwinds,
96 and previous values of the *the-vm* fluid within the current continuation
97 root. But we don't have access to continuation roots in the dynwind stack.
98 So, just punt for now, we just capture the continuation for the current VM.
99
100 While I'm on the topic, ideally we could avoid copying the C stack if the
101 continuation root is inside VM code, and call/cc was invoked within that same
102 call to vm_run; but that's currently not implemented.
103 */
104 SCM
105 scm_i_vm_capture_stack (SCM *stack_base, SCM *fp, SCM *sp, scm_t_uint8 *ra,
106 scm_t_uint8 *mvra, scm_t_dynstack *dynstack,
107 scm_t_uint32 flags)
108 {
109 struct scm_vm_cont *p;
110
111 p = scm_gc_malloc (sizeof (*p), "capture_vm_cont");
112 p->stack_size = sp - stack_base + 1;
113 p->stack_base = scm_gc_malloc (p->stack_size * sizeof (SCM),
114 "capture_vm_cont");
115 #if defined(VM_ENABLE_STACK_NULLING) && 0
116 /* Tail continuations leave their frame on the stack for subsequent
117 application, but don't capture the frame -- so there are some elements on
118 the stack then, and this check doesn't work, so disable it for now. */
119 if (sp >= vp->stack_base)
120 if (!vp->sp[0] || vp->sp[1])
121 abort ();
122 memset (p->stack_base, 0, p->stack_size * sizeof (SCM));
123 #endif
124 p->ra = ra;
125 p->mvra = mvra;
126 p->sp = sp;
127 p->fp = fp;
128 memcpy (p->stack_base, stack_base, (sp + 1 - stack_base) * sizeof (SCM));
129 p->reloc = p->stack_base - stack_base;
130 p->dynstack = dynstack;
131 p->flags = flags;
132 return scm_cell (scm_tc7_vm_cont, (scm_t_bits)p);
133 }
134
135 static void
136 vm_return_to_continuation (SCM vm, SCM cont, size_t n, SCM *argv)
137 {
138 struct scm_vm *vp;
139 struct scm_vm_cont *cp;
140 SCM *argv_copy;
141
142 argv_copy = alloca (n * sizeof(SCM));
143 memcpy (argv_copy, argv, n * sizeof(SCM));
144
145 vp = SCM_VM_DATA (vm);
146 cp = SCM_VM_CONT_DATA (cont);
147
148 if (n == 0 && !cp->mvra)
149 scm_misc_error (NULL, "Too few values returned to continuation",
150 SCM_EOL);
151
152 if (vp->stack_size < cp->stack_size + n + 1)
153 scm_misc_error ("vm-engine", "not enough space to reinstate continuation",
154 scm_list_2 (vm, cont));
155
156 #ifdef VM_ENABLE_STACK_NULLING
157 {
158 scm_t_ptrdiff nzero = (vp->sp - cp->sp);
159 if (nzero > 0)
160 memset (vp->stack_base + cp->stack_size, 0, nzero * sizeof (SCM));
161 /* actually nzero should always be negative, because vm_reset_stack will
162 unwind the stack to some point *below* this continuation */
163 }
164 #endif
165 vp->sp = cp->sp;
166 vp->fp = cp->fp;
167 memcpy (vp->stack_base, cp->stack_base, cp->stack_size * sizeof (SCM));
168
169 if (n == 1 || !cp->mvra)
170 {
171 vp->ip = cp->ra;
172 vp->sp++;
173 *vp->sp = argv_copy[0];
174 }
175 else
176 {
177 size_t i;
178 for (i = 0; i < n; i++)
179 {
180 vp->sp++;
181 *vp->sp = argv_copy[i];
182 }
183 vp->sp++;
184 *vp->sp = scm_from_size_t (n);
185 vp->ip = cp->mvra;
186 }
187 }
188
189 SCM
190 scm_i_capture_current_stack (void)
191 {
192 scm_i_thread *thread;
193 SCM vm;
194 struct scm_vm *vp;
195
196 thread = SCM_I_CURRENT_THREAD;
197 vm = scm_the_vm ();
198 vp = SCM_VM_DATA (vm);
199
200 return scm_i_vm_capture_stack (vp->stack_base, vp->fp, vp->sp, vp->ip, NULL,
201 scm_dynstack_capture_all (&thread->dynstack),
202 0);
203 }
204
205 static void vm_dispatch_hook (SCM vm, int hook_num,
206 SCM *argv, int n) SCM_NOINLINE;
207
208 static void
209 vm_dispatch_hook (SCM vm, int hook_num, SCM *argv, int n)
210 {
211 struct scm_vm *vp;
212 SCM hook;
213 struct scm_frame c_frame;
214 scm_t_cell *frame;
215 int saved_trace_level;
216
217 vp = SCM_VM_DATA (vm);
218 hook = vp->hooks[hook_num];
219
220 if (SCM_LIKELY (scm_is_false (hook))
221 || scm_is_null (SCM_HOOK_PROCEDURES (hook)))
222 return;
223
224 saved_trace_level = vp->trace_level;
225 vp->trace_level = 0;
226
227 /* Allocate a frame object on the stack. This is more efficient than calling
228 `scm_c_make_frame ()' to allocate on the heap, but it forces hooks to not
229 capture frame objects.
230
231 At the same time, procedures such as `frame-procedure' make sense only
232 while the stack frame represented by the frame object is visible, so it
233 seems reasonable to limit the lifetime of frame objects. */
234
235 c_frame.stack_holder = vm;
236 c_frame.fp = vp->fp;
237 c_frame.sp = vp->sp;
238 c_frame.ip = vp->ip;
239 c_frame.offset = 0;
240
241 /* Arrange for FRAME to be 8-byte aligned, like any other cell. */
242 frame = alloca (sizeof (*frame) + 8);
243 frame = (scm_t_cell *) ROUND_UP ((scm_t_uintptr) frame, 8UL);
244
245 frame->word_0 = SCM_PACK (scm_tc7_frame);
246 frame->word_1 = SCM_PACK_POINTER (&c_frame);
247
248 if (n == 0)
249 {
250 SCM args[1];
251
252 args[0] = SCM_PACK_POINTER (frame);
253 scm_c_run_hookn (hook, args, 1);
254 }
255 else if (n == 1)
256 {
257 SCM args[2];
258
259 args[0] = SCM_PACK_POINTER (frame);
260 args[1] = argv[0];
261 scm_c_run_hookn (hook, args, 2);
262 }
263 else
264 {
265 SCM args = SCM_EOL;
266
267 while (n--)
268 args = scm_cons (argv[n], args);
269 scm_c_run_hook (hook, scm_cons (SCM_PACK_POINTER (frame), args));
270 }
271
272 vp->trace_level = saved_trace_level;
273 }
274
275 static void
276 vm_abort (SCM vm, SCM tag, size_t nstack, SCM *stack_args, SCM tail, SCM *sp,
277 scm_i_jmp_buf *current_registers) SCM_NORETURN;
278
279 static void
280 vm_abort (SCM vm, SCM tag, size_t nstack, SCM *stack_args, SCM tail, SCM *sp,
281 scm_i_jmp_buf *current_registers)
282 {
283 size_t i;
284 ssize_t tail_len;
285 SCM *argv;
286
287 tail_len = scm_ilength (tail);
288 if (tail_len < 0)
289 scm_misc_error ("vm-engine", "tail values to abort should be a list",
290 scm_list_1 (tail));
291
292 argv = alloca ((nstack + tail_len) * sizeof (SCM));
293 for (i = 0; i < nstack; i++)
294 argv[i] = stack_args[i];
295 for (; i < nstack + tail_len; i++, tail = scm_cdr (tail))
296 argv[i] = scm_car (tail);
297
298 /* FIXME: NULLSTACK (SCM_VM_DATA (vp)->sp - sp) */
299 SCM_VM_DATA (vm)->sp = sp;
300
301 scm_c_abort (vm, tag, nstack + tail_len, argv, current_registers);
302 }
303
304 static void
305 vm_reinstate_partial_continuation (SCM vm, SCM cont, size_t n, SCM *argv,
306 scm_t_dynstack *dynstack,
307 scm_i_jmp_buf *registers)
308 {
309 struct scm_vm *vp;
310 struct scm_vm_cont *cp;
311 SCM *argv_copy, *base;
312 scm_t_ptrdiff reloc;
313 size_t i;
314
315 argv_copy = alloca (n * sizeof(SCM));
316 memcpy (argv_copy, argv, n * sizeof(SCM));
317
318 vp = SCM_VM_DATA (vm);
319 cp = SCM_VM_CONT_DATA (cont);
320 base = SCM_FRAME_UPPER_ADDRESS (vp->fp) + 1;
321 reloc = cp->reloc + (base - cp->stack_base);
322
323 #define RELOC(scm_p) \
324 (((SCM *) (scm_p)) + reloc)
325
326 if ((base - vp->stack_base) + cp->stack_size + n + 1 > vp->stack_size)
327 scm_misc_error ("vm-engine",
328 "not enough space to instate partial continuation",
329 scm_list_2 (vm, cont));
330
331 memcpy (base, cp->stack_base, cp->stack_size * sizeof (SCM));
332
333 /* now relocate frame pointers */
334 {
335 SCM *fp;
336 for (fp = RELOC (cp->fp);
337 SCM_FRAME_LOWER_ADDRESS (fp) > base;
338 fp = SCM_FRAME_DYNAMIC_LINK (fp))
339 SCM_FRAME_SET_DYNAMIC_LINK (fp, RELOC (SCM_FRAME_DYNAMIC_LINK (fp)));
340 }
341
342 vp->sp = base - 1 + cp->stack_size;
343 vp->fp = RELOC (cp->fp);
344 vp->ip = cp->mvra;
345
346 /* now push args. ip is in a MV context. */
347 for (i = 0; i < n; i++)
348 {
349 vp->sp++;
350 *vp->sp = argv_copy[i];
351 }
352 vp->sp++;
353 *vp->sp = scm_from_size_t (n);
354
355 /* The prompt captured a slice of the dynamic stack. Here we wind
356 those entries onto the current thread's stack. We also have to
357 relocate any prompts that we see along the way. */
358 {
359 scm_t_bits *walk;
360
361 for (walk = SCM_DYNSTACK_FIRST (cp->dynstack);
362 SCM_DYNSTACK_TAG (walk);
363 walk = SCM_DYNSTACK_NEXT (walk))
364 {
365 scm_t_bits tag = SCM_DYNSTACK_TAG (walk);
366
367 if (SCM_DYNSTACK_TAG_TYPE (tag) == SCM_DYNSTACK_TYPE_PROMPT)
368 scm_dynstack_wind_prompt (dynstack, walk, reloc, registers);
369 else
370 scm_dynstack_wind_1 (dynstack, walk);
371 }
372 }
373 #undef RELOC
374 }
375
376 \f
377 /*
378 * VM Internal functions
379 */
380
381 void
382 scm_i_vm_print (SCM x, SCM port, scm_print_state *pstate)
383 {
384 const struct scm_vm *vm;
385
386 vm = SCM_VM_DATA (x);
387
388 scm_puts_unlocked ("#<vm ", port);
389 switch (vm->engine)
390 {
391 case SCM_VM_REGULAR_ENGINE:
392 scm_puts_unlocked ("regular-engine ", port);
393 break;
394
395 case SCM_VM_DEBUG_ENGINE:
396 scm_puts_unlocked ("debug-engine ", port);
397 break;
398
399 default:
400 scm_puts_unlocked ("unknown-engine ", port);
401 }
402 scm_uintprint (SCM_UNPACK (x), 16, port);
403 scm_puts_unlocked (">", port);
404 }
405
406 \f
407 /*
408 * VM Error Handling
409 */
410
411 static void vm_error (const char *msg, SCM arg) SCM_NORETURN;
412 static void vm_error_bad_instruction (scm_t_uint32 inst) SCM_NORETURN SCM_NOINLINE;
413 static void vm_error_unbound (SCM proc, SCM sym) SCM_NORETURN SCM_NOINLINE;
414 static void vm_error_unbound_fluid (SCM proc, SCM fluid) SCM_NORETURN SCM_NOINLINE;
415 static void vm_error_not_a_variable (const char *func_name, SCM x) SCM_NORETURN SCM_NOINLINE;
416 static void vm_error_apply_to_non_list (SCM x) SCM_NORETURN SCM_NOINLINE;
417 static void vm_error_kwargs_length_not_even (SCM proc) SCM_NORETURN SCM_NOINLINE;
418 static void vm_error_kwargs_invalid_keyword (SCM proc, SCM obj) SCM_NORETURN SCM_NOINLINE;
419 static void vm_error_kwargs_unrecognized_keyword (SCM proc, SCM kw) SCM_NORETURN SCM_NOINLINE;
420 static void vm_error_too_many_args (int nargs) SCM_NORETURN SCM_NOINLINE;
421 static void vm_error_wrong_num_args (SCM proc) SCM_NORETURN SCM_NOINLINE;
422 static void vm_error_wrong_type_apply (SCM proc) SCM_NORETURN SCM_NOINLINE;
423 static void vm_error_stack_overflow (struct scm_vm *vp) SCM_NORETURN SCM_NOINLINE;
424 static void vm_error_stack_underflow (void) SCM_NORETURN SCM_NOINLINE;
425 static void vm_error_improper_list (SCM x) SCM_NORETURN SCM_NOINLINE;
426 static void vm_error_not_a_pair (const char *subr, SCM x) SCM_NORETURN SCM_NOINLINE;
427 static void vm_error_not_a_bytevector (const char *subr, SCM x) SCM_NORETURN SCM_NOINLINE;
428 static void vm_error_not_a_struct (const char *subr, SCM x) SCM_NORETURN SCM_NOINLINE;
429 static void vm_error_no_values (void) SCM_NORETURN SCM_NOINLINE;
430 static void vm_error_not_enough_values (void) SCM_NORETURN SCM_NOINLINE;
431 static void vm_error_wrong_number_of_values (scm_t_uint32 expected) SCM_NORETURN SCM_NOINLINE;
432 static void vm_error_continuation_not_rewindable (SCM cont) SCM_NORETURN SCM_NOINLINE;
433 static void vm_error_bad_wide_string_length (size_t len) SCM_NORETURN SCM_NOINLINE;
434
435 static void
436 vm_error (const char *msg, SCM arg)
437 {
438 scm_throw (sym_vm_error,
439 scm_list_3 (sym_vm_run, scm_from_latin1_string (msg),
440 SCM_UNBNDP (arg) ? SCM_EOL : scm_list_1 (arg)));
441 abort(); /* not reached */
442 }
443
444 static void
445 vm_error_bad_instruction (scm_t_uint32 inst)
446 {
447 vm_error ("VM: Bad instruction: ~s", scm_from_uint32 (inst));
448 }
449
450 static void
451 vm_error_unbound (SCM proc, SCM sym)
452 {
453 scm_error_scm (scm_misc_error_key, proc,
454 scm_from_latin1_string ("Unbound variable: ~s"),
455 scm_list_1 (sym), SCM_BOOL_F);
456 }
457
458 static void
459 vm_error_unbound_fluid (SCM proc, SCM fluid)
460 {
461 scm_error_scm (scm_misc_error_key, proc,
462 scm_from_latin1_string ("Unbound fluid: ~s"),
463 scm_list_1 (fluid), SCM_BOOL_F);
464 }
465
466 static void
467 vm_error_not_a_variable (const char *func_name, SCM x)
468 {
469 scm_error (scm_arg_type_key, func_name, "Not a variable: ~S",
470 scm_list_1 (x), scm_list_1 (x));
471 }
472
473 static void
474 vm_error_apply_to_non_list (SCM x)
475 {
476 scm_error (scm_arg_type_key, "apply", "Apply to non-list: ~S",
477 scm_list_1 (x), scm_list_1 (x));
478 }
479
480 static void
481 vm_error_kwargs_length_not_even (SCM proc)
482 {
483 scm_error_scm (sym_keyword_argument_error, proc,
484 scm_from_latin1_string ("Odd length of keyword argument list"),
485 SCM_EOL, SCM_BOOL_F);
486 }
487
488 static void
489 vm_error_kwargs_invalid_keyword (SCM proc, SCM obj)
490 {
491 scm_error_scm (sym_keyword_argument_error, proc,
492 scm_from_latin1_string ("Invalid keyword"),
493 SCM_EOL, scm_list_1 (obj));
494 }
495
496 static void
497 vm_error_kwargs_unrecognized_keyword (SCM proc, SCM kw)
498 {
499 scm_error_scm (sym_keyword_argument_error, proc,
500 scm_from_latin1_string ("Unrecognized keyword"),
501 SCM_EOL, scm_list_1 (kw));
502 }
503
504 static void
505 vm_error_too_many_args (int nargs)
506 {
507 vm_error ("VM: Too many arguments", scm_from_int (nargs));
508 }
509
510 static void
511 vm_error_wrong_num_args (SCM proc)
512 {
513 scm_wrong_num_args (proc);
514 }
515
516 static void
517 vm_error_wrong_type_apply (SCM proc)
518 {
519 scm_error (scm_arg_type_key, NULL, "Wrong type to apply: ~S",
520 scm_list_1 (proc), scm_list_1 (proc));
521 }
522
523 static void
524 vm_error_stack_overflow (struct scm_vm *vp)
525 {
526 if (vp->stack_limit < vp->stack_base + vp->stack_size)
527 /* There are VM_STACK_RESERVE_SIZE bytes left. Make them available so
528 that `throw' below can run on this VM. */
529 vp->stack_limit = vp->stack_base + vp->stack_size;
530 else
531 /* There is no space left on the stack. FIXME: Do something more
532 sensible here! */
533 abort ();
534 vm_error ("VM: Stack overflow", SCM_UNDEFINED);
535 }
536
537 static void
538 vm_error_stack_underflow (void)
539 {
540 vm_error ("VM: Stack underflow", SCM_UNDEFINED);
541 }
542
543 static void
544 vm_error_improper_list (SCM x)
545 {
546 vm_error ("Expected a proper list, but got object with tail ~s", x);
547 }
548
549 static void
550 vm_error_not_a_pair (const char *subr, SCM x)
551 {
552 scm_wrong_type_arg_msg (subr, 1, x, "pair");
553 }
554
555 static void
556 vm_error_not_a_bytevector (const char *subr, SCM x)
557 {
558 scm_wrong_type_arg_msg (subr, 1, x, "bytevector");
559 }
560
561 static void
562 vm_error_not_a_struct (const char *subr, SCM x)
563 {
564 scm_wrong_type_arg_msg (subr, 1, x, "struct");
565 }
566
567 static void
568 vm_error_no_values (void)
569 {
570 vm_error ("Zero values returned to single-valued continuation",
571 SCM_UNDEFINED);
572 }
573
574 static void
575 vm_error_not_enough_values (void)
576 {
577 vm_error ("Too few values returned to continuation", SCM_UNDEFINED);
578 }
579
580 static void
581 vm_error_wrong_number_of_values (scm_t_uint32 expected)
582 {
583 vm_error ("Wrong number of values returned to continuation (expected ~a)",
584 scm_from_uint32 (expected));
585 }
586
587 static void
588 vm_error_continuation_not_rewindable (SCM cont)
589 {
590 vm_error ("Unrewindable partial continuation", cont);
591 }
592
593 static void
594 vm_error_bad_wide_string_length (size_t len)
595 {
596 vm_error ("VM: Bad wide string length: ~S", scm_from_size_t (len));
597 }
598
599
600 \f
601
602 static SCM boot_continuation;
603
604 static SCM rtl_boot_continuation;
605 static SCM rtl_apply;
606 static SCM rtl_values;
607
608 static const scm_t_uint32 rtl_boot_continuation_code[] = {
609 SCM_PACK_RTL_24 (scm_rtl_op_halt, 0)
610 };
611
612 static const scm_t_uint32 rtl_apply_code[] = {
613 SCM_PACK_RTL_24 (scm_rtl_op_tail_apply, 0) /* proc in r1, args from r2, nargs set */
614 };
615
616 static const scm_t_uint32 rtl_values_code[] = {
617 SCM_PACK_RTL_24 (scm_rtl_op_return_values, 0) /* vals from r1 */
618 };
619
620
621 \f
622 /*
623 * VM
624 */
625
626 static SCM
627 resolve_variable (SCM what, SCM module)
628 {
629 if (SCM_LIKELY (scm_is_symbol (what)))
630 {
631 if (scm_is_true (module))
632 return scm_module_lookup (module, what);
633 else
634 return scm_module_lookup (scm_the_root_module (), what);
635 }
636 else
637 {
638 SCM modname, sym, public;
639
640 modname = SCM_CAR (what);
641 sym = SCM_CADR (what);
642 public = SCM_CADDR (what);
643
644 if (scm_is_true (public))
645 return scm_public_lookup (modname, sym);
646 else
647 return scm_private_lookup (modname, sym);
648 }
649 }
650
651 #define VM_MIN_STACK_SIZE (1024)
652 #define VM_DEFAULT_STACK_SIZE (64 * 1024)
653 static size_t vm_stack_size = VM_DEFAULT_STACK_SIZE;
654
655 static void
656 initialize_default_stack_size (void)
657 {
658 int size = scm_getenv_int ("GUILE_STACK_SIZE", vm_stack_size);
659 if (size >= VM_MIN_STACK_SIZE)
660 vm_stack_size = size;
661 }
662
663 #define VM_NAME vm_regular_engine
664 #define RTL_VM_NAME rtl_vm_regular_engine
665 #define FUNC_NAME "vm-regular-engine"
666 #define VM_ENGINE SCM_VM_REGULAR_ENGINE
667 #include "vm-engine.c"
668 #undef VM_NAME
669 #undef RTL_VM_NAME
670 #undef FUNC_NAME
671 #undef VM_ENGINE
672
673 #define VM_NAME vm_debug_engine
674 #define RTL_VM_NAME rtl_vm_debug_engine
675 #define FUNC_NAME "vm-debug-engine"
676 #define VM_ENGINE SCM_VM_DEBUG_ENGINE
677 #include "vm-engine.c"
678 #undef VM_NAME
679 #undef RTL_VM_NAME
680 #undef FUNC_NAME
681 #undef VM_ENGINE
682
683 static const scm_t_vm_engine vm_engines[] =
684 { vm_regular_engine, vm_debug_engine };
685
686 typedef SCM (*scm_t_rtl_vm_engine) (SCM vm, SCM program, SCM *argv, size_t nargs);
687
688 static const scm_t_rtl_vm_engine rtl_vm_engines[] =
689 { rtl_vm_regular_engine, rtl_vm_debug_engine };
690
691 #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN
692
693 /* The GC "kind" for the VM stack. */
694 static int vm_stack_gc_kind;
695
696 #endif
697
698 static SCM
699 make_vm (void)
700 #define FUNC_NAME "make_vm"
701 {
702 int i;
703 struct scm_vm *vp;
704
705 vp = scm_gc_malloc (sizeof (struct scm_vm), "vm");
706
707 vp->stack_size= vm_stack_size;
708
709 #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN
710 vp->stack_base = (SCM *)
711 GC_generic_malloc (vp->stack_size * sizeof (SCM), vm_stack_gc_kind);
712
713 /* Keep a pointer to VP so that `vm_stack_mark ()' can know what the stack
714 top is. */
715 *vp->stack_base = SCM_PACK_POINTER (vp);
716 vp->stack_base++;
717 vp->stack_size--;
718 #else
719 vp->stack_base = scm_gc_malloc (vp->stack_size * sizeof (SCM),
720 "stack-base");
721 #endif
722
723 #ifdef VM_ENABLE_STACK_NULLING
724 memset (vp->stack_base, 0, vp->stack_size * sizeof (SCM));
725 #endif
726 vp->stack_limit = vp->stack_base + vp->stack_size - VM_STACK_RESERVE_SIZE;
727 vp->ip = NULL;
728 vp->sp = vp->stack_base - 1;
729 vp->fp = NULL;
730 vp->engine = vm_default_engine;
731 vp->trace_level = 0;
732 for (i = 0; i < SCM_VM_NUM_HOOKS; i++)
733 vp->hooks[i] = SCM_BOOL_F;
734 return scm_cell (scm_tc7_vm, (scm_t_bits)vp);
735 }
736 #undef FUNC_NAME
737
738 #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN
739
740 /* Mark the VM stack region between its base and its current top. */
741 static struct GC_ms_entry *
742 vm_stack_mark (GC_word *addr, struct GC_ms_entry *mark_stack_ptr,
743 struct GC_ms_entry *mark_stack_limit, GC_word env)
744 {
745 GC_word *word;
746 const struct scm_vm *vm;
747
748 /* The first word of the VM stack should contain a pointer to the
749 corresponding VM. */
750 vm = * ((struct scm_vm **) addr);
751
752 if (vm == NULL
753 || (SCM *) addr != vm->stack_base - 1)
754 /* ADDR must be a pointer to a free-list element, which we must ignore
755 (see warning in <gc/gc_mark.h>). */
756 return mark_stack_ptr;
757
758 for (word = (GC_word *) vm->stack_base; word <= (GC_word *) vm->sp; word++)
759 mark_stack_ptr = GC_MARK_AND_PUSH ((* (GC_word **) word),
760 mark_stack_ptr, mark_stack_limit,
761 NULL);
762
763 return mark_stack_ptr;
764 }
765
766 #endif /* VM_ENABLE_PRECISE_STACK_GC_SCAN */
767
768
769 SCM
770 scm_c_vm_run (SCM vm, SCM program, SCM *argv, int nargs)
771 {
772 struct scm_vm *vp = SCM_VM_DATA (vm);
773 SCM_CHECK_STACK;
774 if (SCM_RTL_PROGRAM_P (program))
775 return rtl_vm_engines[vp->engine](vm, program, argv, nargs);
776 else
777 return vm_engines[vp->engine](vm, program, argv, nargs);
778 }
779
780 /* Scheme interface */
781
782 SCM_DEFINE (scm_the_vm, "the-vm", 0, 0, 0,
783 (void),
784 "Return the current thread's VM.")
785 #define FUNC_NAME s_scm_the_vm
786 {
787 scm_i_thread *t = SCM_I_CURRENT_THREAD;
788
789 if (SCM_UNLIKELY (scm_is_false (t->vm)))
790 t->vm = make_vm ();
791
792 return t->vm;
793 }
794 #undef FUNC_NAME
795
796
797 SCM_DEFINE (scm_vm_p, "vm?", 1, 0, 0,
798 (SCM obj),
799 "")
800 #define FUNC_NAME s_scm_vm_p
801 {
802 return scm_from_bool (SCM_VM_P (obj));
803 }
804 #undef FUNC_NAME
805
806 SCM_DEFINE (scm_make_vm, "make-vm", 0, 0, 0,
807 (void),
808 "")
809 #define FUNC_NAME s_scm_make_vm,
810 {
811 return make_vm ();
812 }
813 #undef FUNC_NAME
814
815 SCM_DEFINE (scm_vm_ip, "vm:ip", 1, 0, 0,
816 (SCM vm),
817 "")
818 #define FUNC_NAME s_scm_vm_ip
819 {
820 SCM_VALIDATE_VM (1, vm);
821 return scm_from_unsigned_integer ((scm_t_bits) SCM_VM_DATA (vm)->ip);
822 }
823 #undef FUNC_NAME
824
825 SCM_DEFINE (scm_vm_sp, "vm:sp", 1, 0, 0,
826 (SCM vm),
827 "")
828 #define FUNC_NAME s_scm_vm_sp
829 {
830 SCM_VALIDATE_VM (1, vm);
831 return scm_from_unsigned_integer ((scm_t_bits) SCM_VM_DATA (vm)->sp);
832 }
833 #undef FUNC_NAME
834
835 SCM_DEFINE (scm_vm_fp, "vm:fp", 1, 0, 0,
836 (SCM vm),
837 "")
838 #define FUNC_NAME s_scm_vm_fp
839 {
840 SCM_VALIDATE_VM (1, vm);
841 return scm_from_unsigned_integer ((scm_t_bits) SCM_VM_DATA (vm)->fp);
842 }
843 #undef FUNC_NAME
844
845 #define VM_DEFINE_HOOK(n) \
846 { \
847 struct scm_vm *vp; \
848 SCM_VALIDATE_VM (1, vm); \
849 vp = SCM_VM_DATA (vm); \
850 if (scm_is_false (vp->hooks[n])) \
851 vp->hooks[n] = scm_make_hook (SCM_I_MAKINUM (1)); \
852 return vp->hooks[n]; \
853 }
854
855 SCM_DEFINE (scm_vm_apply_hook, "vm-apply-hook", 1, 0, 0,
856 (SCM vm),
857 "")
858 #define FUNC_NAME s_scm_vm_apply_hook
859 {
860 VM_DEFINE_HOOK (SCM_VM_APPLY_HOOK);
861 }
862 #undef FUNC_NAME
863
864 SCM_DEFINE (scm_vm_push_continuation_hook, "vm-push-continuation-hook", 1, 0, 0,
865 (SCM vm),
866 "")
867 #define FUNC_NAME s_scm_vm_push_continuation_hook
868 {
869 VM_DEFINE_HOOK (SCM_VM_PUSH_CONTINUATION_HOOK);
870 }
871 #undef FUNC_NAME
872
873 SCM_DEFINE (scm_vm_pop_continuation_hook, "vm-pop-continuation-hook", 1, 0, 0,
874 (SCM vm),
875 "")
876 #define FUNC_NAME s_scm_vm_pop_continuation_hook
877 {
878 VM_DEFINE_HOOK (SCM_VM_POP_CONTINUATION_HOOK);
879 }
880 #undef FUNC_NAME
881
882 SCM_DEFINE (scm_vm_next_hook, "vm-next-hook", 1, 0, 0,
883 (SCM vm),
884 "")
885 #define FUNC_NAME s_scm_vm_next_hook
886 {
887 VM_DEFINE_HOOK (SCM_VM_NEXT_HOOK);
888 }
889 #undef FUNC_NAME
890
891 SCM_DEFINE (scm_vm_abort_continuation_hook, "vm-abort-continuation-hook", 1, 0, 0,
892 (SCM vm),
893 "")
894 #define FUNC_NAME s_scm_vm_abort_continuation_hook
895 {
896 VM_DEFINE_HOOK (SCM_VM_ABORT_CONTINUATION_HOOK);
897 }
898 #undef FUNC_NAME
899
900 SCM_DEFINE (scm_vm_restore_continuation_hook, "vm-restore-continuation-hook", 1, 0, 0,
901 (SCM vm),
902 "")
903 #define FUNC_NAME s_scm_vm_restore_continuation_hook
904 {
905 VM_DEFINE_HOOK (SCM_VM_RESTORE_CONTINUATION_HOOK);
906 }
907 #undef FUNC_NAME
908
909 SCM_DEFINE (scm_vm_trace_level, "vm-trace-level", 1, 0, 0,
910 (SCM vm),
911 "")
912 #define FUNC_NAME s_scm_vm_trace_level
913 {
914 SCM_VALIDATE_VM (1, vm);
915 return scm_from_int (SCM_VM_DATA (vm)->trace_level);
916 }
917 #undef FUNC_NAME
918
919 SCM_DEFINE (scm_set_vm_trace_level_x, "set-vm-trace-level!", 2, 0, 0,
920 (SCM vm, SCM level),
921 "")
922 #define FUNC_NAME s_scm_set_vm_trace_level_x
923 {
924 SCM_VALIDATE_VM (1, vm);
925 SCM_VM_DATA (vm)->trace_level = scm_to_int (level);
926 return SCM_UNSPECIFIED;
927 }
928 #undef FUNC_NAME
929
930 \f
931 /*
932 * VM engines
933 */
934
935 static int
936 symbol_to_vm_engine (SCM engine, const char *FUNC_NAME)
937 {
938 if (scm_is_eq (engine, sym_regular))
939 return SCM_VM_REGULAR_ENGINE;
940 else if (scm_is_eq (engine, sym_debug))
941 return SCM_VM_DEBUG_ENGINE;
942 else
943 SCM_MISC_ERROR ("Unknown VM engine: ~a", scm_list_1 (engine));
944 }
945
946 static SCM
947 vm_engine_to_symbol (int engine, const char *FUNC_NAME)
948 {
949 switch (engine)
950 {
951 case SCM_VM_REGULAR_ENGINE:
952 return sym_regular;
953 case SCM_VM_DEBUG_ENGINE:
954 return sym_debug;
955 default:
956 /* ? */
957 SCM_MISC_ERROR ("Unknown VM engine: ~a",
958 scm_list_1 (scm_from_int (engine)));
959 }
960 }
961
962 SCM_DEFINE (scm_vm_engine, "vm-engine", 1, 0, 0,
963 (SCM vm),
964 "")
965 #define FUNC_NAME s_scm_vm_engine
966 {
967 SCM_VALIDATE_VM (1, vm);
968 return vm_engine_to_symbol (SCM_VM_DATA (vm)->engine, FUNC_NAME);
969 }
970 #undef FUNC_NAME
971
972 void
973 scm_c_set_vm_engine_x (SCM vm, int engine)
974 #define FUNC_NAME "set-vm-engine!"
975 {
976 SCM_VALIDATE_VM (1, vm);
977
978 if (engine < 0 || engine >= SCM_VM_NUM_ENGINES)
979 SCM_MISC_ERROR ("Unknown VM engine: ~a",
980 scm_list_1 (scm_from_int (engine)));
981
982 SCM_VM_DATA (vm)->engine = engine;
983 }
984 #undef FUNC_NAME
985
986 SCM_DEFINE (scm_set_vm_engine_x, "set-vm-engine!", 2, 0, 0,
987 (SCM vm, SCM engine),
988 "")
989 #define FUNC_NAME s_scm_set_vm_engine_x
990 {
991 scm_c_set_vm_engine_x (vm, symbol_to_vm_engine (engine, FUNC_NAME));
992 return SCM_UNSPECIFIED;
993 }
994 #undef FUNC_NAME
995
996 void
997 scm_c_set_default_vm_engine_x (int engine)
998 #define FUNC_NAME "set-default-vm-engine!"
999 {
1000 if (engine < 0 || engine >= SCM_VM_NUM_ENGINES)
1001 SCM_MISC_ERROR ("Unknown VM engine: ~a",
1002 scm_list_1 (scm_from_int (engine)));
1003
1004 vm_default_engine = engine;
1005 }
1006 #undef FUNC_NAME
1007
1008 SCM_DEFINE (scm_set_default_vm_engine_x, "set-default-vm-engine!", 1, 0, 0,
1009 (SCM engine),
1010 "")
1011 #define FUNC_NAME s_scm_set_default_vm_engine_x
1012 {
1013 scm_c_set_default_vm_engine_x (symbol_to_vm_engine (engine, FUNC_NAME));
1014 return SCM_UNSPECIFIED;
1015 }
1016 #undef FUNC_NAME
1017
1018 static void reinstate_vm (SCM vm)
1019 {
1020 scm_i_thread *t = SCM_I_CURRENT_THREAD;
1021 t->vm = vm;
1022 }
1023
1024 SCM_DEFINE (scm_call_with_vm, "call-with-vm", 2, 0, 1,
1025 (SCM vm, SCM proc, SCM args),
1026 "Apply @var{proc} to @var{args} in a dynamic extent in which\n"
1027 "@var{vm} is the current VM.\n\n"
1028 "As an implementation restriction, if @var{vm} is not the same\n"
1029 "as the current thread's VM, continuations captured within the\n"
1030 "call to @var{proc} may not be reinstated once control leaves\n"
1031 "@var{proc}.")
1032 #define FUNC_NAME s_scm_call_with_vm
1033 {
1034 SCM prev_vm, ret;
1035 SCM *argv;
1036 int i, nargs;
1037 scm_t_wind_flags flags;
1038 scm_i_thread *t = SCM_I_CURRENT_THREAD;
1039
1040 SCM_VALIDATE_VM (1, vm);
1041 SCM_VALIDATE_PROC (2, proc);
1042
1043 nargs = scm_ilength (args);
1044 if (SCM_UNLIKELY (nargs < 0))
1045 scm_wrong_type_arg_msg (FUNC_NAME, 3, args, "list");
1046
1047 argv = alloca (nargs * sizeof(SCM));
1048 for (i = 0; i < nargs; i++)
1049 {
1050 argv[i] = SCM_CAR (args);
1051 args = SCM_CDR (args);
1052 }
1053
1054 prev_vm = t->vm;
1055
1056 /* Reentry can happen via invokation of a saved continuation, but
1057 continuations only save the state of the VM that they are in at
1058 capture-time, which might be different from this one. So, in the
1059 case that the VMs are different, set up a non-rewindable frame to
1060 prevent reinstating an incomplete continuation. */
1061 flags = scm_is_eq (prev_vm, vm) ? 0 : SCM_F_WIND_EXPLICITLY;
1062 if (flags)
1063 {
1064 scm_dynwind_begin (0);
1065 scm_dynwind_unwind_handler_with_scm (reinstate_vm, prev_vm, flags);
1066 t->vm = vm;
1067 }
1068
1069 ret = scm_c_vm_run (vm, proc, argv, nargs);
1070
1071 if (flags)
1072 scm_dynwind_end ();
1073
1074 return ret;
1075 }
1076 #undef FUNC_NAME
1077
1078 \f
1079 /*
1080 * Initialize
1081 */
1082
1083 SCM scm_load_compiled_with_vm (SCM file)
1084 {
1085 SCM program = scm_load_thunk_from_file (file);
1086
1087 return scm_c_vm_run (scm_the_vm (), program, NULL, 0);
1088 }
1089
1090
1091 static SCM
1092 make_boot_program (void)
1093 {
1094 struct scm_objcode *bp;
1095 size_t bp_size;
1096 SCM u8vec, ret;
1097
1098 const scm_t_uint8 text[] = {
1099 scm_op_make_int8_1,
1100 scm_op_halt
1101 };
1102
1103 bp_size = sizeof (struct scm_objcode) + sizeof (text);
1104 bp = scm_gc_malloc_pointerless (bp_size, "boot-program");
1105 memcpy (SCM_C_OBJCODE_BASE (bp), text, sizeof (text));
1106 bp->len = sizeof(text);
1107 bp->metalen = 0;
1108
1109 u8vec = scm_c_take_gc_bytevector ((scm_t_int8*)bp, bp_size, SCM_BOOL_F);
1110 ret = scm_make_program (scm_bytecode_to_objcode (u8vec, SCM_UNDEFINED),
1111 SCM_BOOL_F, SCM_BOOL_F);
1112 SCM_SET_CELL_WORD_0 (ret, (SCM_CELL_WORD_0 (ret) | SCM_F_PROGRAM_IS_BOOT));
1113
1114 return ret;
1115 }
1116
1117 void
1118 scm_bootstrap_vm (void)
1119 {
1120 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
1121 "scm_init_vm",
1122 (scm_t_extension_init_func)scm_init_vm, NULL);
1123
1124 initialize_default_stack_size ();
1125
1126 sym_vm_run = scm_from_latin1_symbol ("vm-run");
1127 sym_vm_error = scm_from_latin1_symbol ("vm-error");
1128 sym_keyword_argument_error = scm_from_latin1_symbol ("keyword-argument-error");
1129 sym_regular = scm_from_latin1_symbol ("regular");
1130 sym_debug = scm_from_latin1_symbol ("debug");
1131
1132 boot_continuation = make_boot_program ();
1133
1134 rtl_boot_continuation = scm_i_make_rtl_program (rtl_boot_continuation_code);
1135 SCM_SET_CELL_WORD_0 (rtl_boot_continuation,
1136 (SCM_CELL_WORD_0 (rtl_boot_continuation)
1137 | SCM_F_PROGRAM_IS_BOOT));
1138 rtl_apply = scm_i_make_rtl_program (rtl_apply_code);
1139 rtl_values = scm_i_make_rtl_program (rtl_values_code);
1140
1141 #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN
1142 vm_stack_gc_kind =
1143 GC_new_kind (GC_new_free_list (),
1144 GC_MAKE_PROC (GC_new_proc (vm_stack_mark), 0),
1145 0, 1);
1146
1147 #endif
1148 }
1149
1150 void
1151 scm_init_vm (void)
1152 {
1153 #ifndef SCM_MAGIC_SNARFER
1154 #include "libguile/vm.x"
1155 #endif
1156 }
1157
1158 /*
1159 Local Variables:
1160 c-file-style: "gnu"
1161 End:
1162 */