1 /* Copyright (C) 2001, 2009, 2010 Free Software Foundation, Inc.
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.
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.
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
27 #include "libguile/bdw-gc.h"
28 #include <gc/gc_mark.h>
33 #include "instructions.h"
36 #include "lang.h" /* NULL_OR_NIL_P */
39 /* I sometimes use this for debugging. */
40 #define vm_puts(OBJ) \
42 scm_display (OBJ, scm_current_error_port ()); \
43 scm_newline (scm_current_error_port ()); \
46 /* The VM has a number of internal assertions that shouldn't normally be
47 necessary, but might be if you think you found a bug in the VM. */
48 #define VM_ENABLE_ASSERTIONS
50 /* We can add a mode that ensures that all stack items above the stack pointer
51 are NULL. This is useful for checking the internal consistency of the VM's
52 assumptions and its operators, but isn't necessary for normal operation. It
53 will ensure that assertions are enabled. Slows down the VM by about 30%. */
54 /* NB! If you enable this, search for NULLING in throw.c */
55 /* #define VM_ENABLE_STACK_NULLING */
57 /* #define VM_ENABLE_PARANOID_ASSERTIONS */
59 #if defined (VM_ENABLE_STACK_NULLING) && !defined (VM_ENABLE_ASSERTIONS)
60 #define VM_ENABLE_ASSERTIONS
63 /* When defined, arrange so that the GC doesn't scan the VM stack beyond its
64 current SP. This should help avoid excess data retention. See
65 http://thread.gmane.org/gmane.comp.programming.garbage-collection.boehmgc/3001
67 #define VM_ENABLE_PRECISE_STACK_GC_SCAN
76 scm_i_vm_cont_print (SCM x
, SCM port
, scm_print_state
*pstate
)
78 scm_puts ("#<vm-continuation ", port
);
79 scm_uintprint (SCM_UNPACK (x
), 16, port
);
83 /* In theory, a number of vm instances can be active in the call trace, and we
84 only want to reify the continuations of those in the current continuation
85 root. I don't see a nice way to do this -- ideally it would involve dynwinds,
86 and previous values of the *the-vm* fluid within the current continuation
87 root. But we don't have access to continuation roots in the dynwind stack.
88 So, just punt for now, we just capture the continuation for the current VM.
90 While I'm on the topic, ideally we could avoid copying the C stack if the
91 continuation root is inside VM code, and call/cc was invoked within that same
92 call to vm_run; but that's currently not implemented.
95 vm_capture_continuation (SCM
*stack_base
,
96 SCM
*fp
, SCM
*sp
, scm_t_uint8
*ra
, scm_t_uint8
*mvra
)
98 struct scm_vm_cont
*p
;
100 p
= scm_gc_malloc (sizeof (*p
), "capture_vm_cont");
101 p
->stack_size
= sp
- stack_base
+ 1;
102 p
->stack_base
= scm_gc_malloc (p
->stack_size
* sizeof (SCM
),
104 #if defined(VM_ENABLE_STACK_NULLING) && 0
105 /* Tail continuations leave their frame on the stack for subsequent
106 application, but don't capture the frame -- so there are some elements on
107 the stack then, and this check doesn't work, so disable it for now. */
108 if (sp
>= vp
->stack_base
)
109 if (!vp
->sp
[0] || vp
->sp
[1])
111 memset (p
->stack_base
, 0, p
->stack_size
* sizeof (SCM
));
117 memcpy (p
->stack_base
, stack_base
, (sp
+ 1 - stack_base
) * sizeof (SCM
));
118 p
->reloc
= p
->stack_base
- stack_base
;
119 return scm_cell (scm_tc7_vm_cont
, (scm_t_bits
)p
);
123 vm_return_to_continuation (SCM vm
, SCM cont
, size_t n
, SCM
*argv
)
126 struct scm_vm_cont
*cp
;
129 argv_copy
= alloca (n
* sizeof(SCM
));
130 memcpy (argv_copy
, argv
, n
* sizeof(SCM
));
132 vp
= SCM_VM_DATA (vm
);
133 cp
= SCM_VM_CONT_DATA (cont
);
135 if (n
== 0 && !cp
->mvra
)
136 scm_misc_error (NULL
, "Too few values returned to continuation",
139 if (vp
->stack_size
< cp
->stack_size
+ n
+ 1)
141 /* puts ("FIXME: Need to expand"); */
144 #ifdef VM_ENABLE_STACK_NULLING
146 scm_t_ptrdiff nzero
= (vp
->sp
- cp
->sp
);
148 memset (vp
->stack_base
+ cp
->stack_size
, 0, nzero
* sizeof (SCM
));
149 /* actually nzero should always be negative, because vm_reset_stack will
150 unwind the stack to some point *below* this continuation */
155 memcpy (vp
->stack_base
, cp
->stack_base
, cp
->stack_size
* sizeof (SCM
));
157 if (n
== 1 || !cp
->mvra
)
161 *vp
->sp
= argv_copy
[0];
166 for (i
= 0; i
< n
; i
++)
169 *vp
->sp
= argv_copy
[i
];
172 *vp
->sp
= scm_from_size_t (n
);
178 scm_i_vm_capture_continuation (SCM vm
)
180 struct scm_vm
*vp
= SCM_VM_DATA (vm
);
181 return vm_capture_continuation (vp
->stack_base
, vp
->fp
, vp
->sp
, vp
->ip
, NULL
);
185 vm_dispatch_hook (SCM vm
, int hook_num
)
191 vp
= SCM_VM_DATA (vm
);
192 hook
= vp
->hooks
[hook_num
];
194 if (SCM_LIKELY (scm_is_false (hook
))
195 || scm_is_null (SCM_HOOK_PROCEDURES (hook
)))
199 frame
= scm_c_make_frame (vm
, vp
->fp
, vp
->sp
, vp
->ip
, 0);
200 scm_c_run_hookn (hook
, &frame
, 1);
208 #define VM_SETJMP(jmpbuf) 0
210 static void vm_throw (SCM vm
, SCM k
, SCM args
) SCM_NORETURN
;
212 vm_throw (SCM vm
, SCM k
, SCM args
)
219 * VM Internal functions
222 SCM_SYMBOL (sym_vm_run
, "vm-run");
223 SCM_SYMBOL (sym_vm_error
, "vm-error");
224 SCM_SYMBOL (sym_keyword_argument_error
, "keyword-argument-error");
225 SCM_SYMBOL (sym_debug
, "debug");
228 scm_i_vm_print (SCM x
, SCM port
, scm_print_state
*pstate
)
230 scm_puts ("#<vm ", port
);
231 scm_uintprint (SCM_UNPACK (x
), 16, port
);
232 scm_puts (">", port
);
236 really_make_boot_program (long nargs
)
239 scm_t_uint8 text
[] = { scm_op_mv_call
, 0, 0, 0, 1,
240 scm_op_make_int8_1
, scm_op_halt
};
241 struct scm_objcode
*bp
;
244 if (SCM_UNLIKELY (nargs
> 255 || nargs
< 0))
246 text
[1] = (scm_t_uint8
)nargs
;
248 bp
= scm_malloc (sizeof (struct scm_objcode
) + sizeof (text
));
249 memcpy (SCM_C_OBJCODE_BASE (bp
), text
, sizeof (text
));
250 bp
->len
= sizeof(text
);
253 u8vec
= scm_c_take_bytevector ((scm_t_int8
*)bp
,
254 sizeof (struct scm_objcode
) + sizeof (text
));
255 ret
= scm_make_program (scm_bytecode_to_objcode (u8vec
),
256 SCM_BOOL_F
, SCM_BOOL_F
);
257 SCM_SET_CELL_WORD_0 (ret
, SCM_CELL_WORD_0 (ret
) | SCM_F_PROGRAM_IS_BOOT
);
261 #define NUM_BOOT_PROGS 8
263 vm_make_boot_program (long nargs
)
265 static SCM programs
[NUM_BOOT_PROGS
] = { 0, };
267 if (SCM_UNLIKELY (!programs
[0]))
270 for (i
= 0; i
< NUM_BOOT_PROGS
; i
++)
271 programs
[i
] = really_make_boot_program (i
);
274 if (SCM_LIKELY (nargs
< NUM_BOOT_PROGS
))
275 return programs
[nargs
];
277 return really_make_boot_program (nargs
);
286 resolve_variable (SCM what
, SCM program_module
)
288 if (SCM_LIKELY (scm_is_symbol (what
)))
290 if (SCM_LIKELY (scm_module_system_booted_p
291 && scm_is_true (program_module
)))
293 return scm_module_lookup (program_module
, what
);
296 SCM v
= scm_sym2var (what
, SCM_BOOL_F
, SCM_BOOL_F
);
297 if (scm_is_false (v
))
298 scm_misc_error (NULL
, "unbound variable: ~S", scm_list_1 (what
));
306 /* compilation of @ or @@
307 `what' is a three-element list: (MODNAME SYM INTERFACE?)
308 INTERFACE? is #t if we compiled @ or #f if we compiled @@
310 mod
= scm_resolve_module (SCM_CAR (what
));
311 if (scm_is_true (SCM_CADDR (what
)))
312 mod
= scm_module_public_interface (mod
);
313 if (scm_is_false (mod
))
314 scm_misc_error (NULL
, "no such module: ~S",
315 scm_list_1 (SCM_CAR (what
)));
317 return scm_module_lookup (mod
, SCM_CADR (what
));
321 #define VM_DEFAULT_STACK_SIZE (64 * 1024)
323 #define VM_NAME vm_regular_engine
324 #define FUNC_NAME "vm-regular-engine"
325 #define VM_ENGINE SCM_VM_REGULAR_ENGINE
326 #include "vm-engine.c"
331 #define VM_NAME vm_debug_engine
332 #define FUNC_NAME "vm-debug-engine"
333 #define VM_ENGINE SCM_VM_DEBUG_ENGINE
334 #include "vm-engine.c"
339 static const scm_t_vm_engine vm_engines
[] =
340 { vm_regular_engine
, vm_debug_engine
};
342 #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN
344 /* The GC "kind" for the VM stack. */
345 static int vm_stack_gc_kind
;
351 #define FUNC_NAME "make_vm"
356 vp
= scm_gc_malloc (sizeof (struct scm_vm
), "vm");
358 vp
->stack_size
= VM_DEFAULT_STACK_SIZE
;
360 #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN
361 vp
->stack_base
= (SCM
*)
362 GC_generic_malloc (vp
->stack_size
* sizeof (SCM
), vm_stack_gc_kind
);
364 /* Keep a pointer to VP so that `vm_stack_mark ()' can know what the stack
366 *vp
->stack_base
= PTR2SCM (vp
);
370 vp
->stack_base
= scm_gc_malloc (vp
->stack_size
* sizeof (SCM
),
374 #ifdef VM_ENABLE_STACK_NULLING
375 memset (vp
->stack_base
, 0, vp
->stack_size
* sizeof (SCM
));
377 vp
->stack_limit
= vp
->stack_base
+ vp
->stack_size
;
379 vp
->sp
= vp
->stack_base
- 1;
381 vp
->engine
= SCM_VM_DEBUG_ENGINE
;
382 vp
->options
= SCM_EOL
;
384 for (i
= 0; i
< SCM_VM_NUM_HOOKS
; i
++)
385 vp
->hooks
[i
] = SCM_BOOL_F
;
386 return scm_cell (scm_tc7_vm
, (scm_t_bits
)vp
);
390 #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN
392 /* Mark the VM stack region between its base and its current top. */
393 static struct GC_ms_entry
*
394 vm_stack_mark (GC_word
*addr
, struct GC_ms_entry
*mark_stack_ptr
,
395 struct GC_ms_entry
*mark_stack_limit
, GC_word env
)
398 const struct scm_vm
*vm
;
400 /* The first word of the VM stack should contain a pointer to the
402 vm
= * ((struct scm_vm
**) addr
);
405 || (SCM
*) addr
!= vm
->stack_base
- 1
406 || vm
->stack_limit
- vm
->stack_base
!= vm
->stack_size
)
407 /* ADDR must be a pointer to a free-list element, which we must ignore
408 (see warning in <gc/gc_mark.h>). */
409 return mark_stack_ptr
;
411 for (word
= (GC_word
*) vm
->stack_base
; word
<= (GC_word
*) vm
->sp
; word
++)
412 mark_stack_ptr
= GC_MARK_AND_PUSH ((* (GC_word
**) word
),
413 mark_stack_ptr
, mark_stack_limit
,
416 return mark_stack_ptr
;
419 #endif /* VM_ENABLE_PRECISE_STACK_GC_SCAN */
423 scm_c_vm_run (SCM vm
, SCM program
, SCM
*argv
, int nargs
)
425 struct scm_vm
*vp
= SCM_VM_DATA (vm
);
426 return vm_engines
[vp
->engine
](vm
, program
, argv
, nargs
);
429 SCM_DEFINE (scm_vm_apply
, "vm-apply", 3, 0, 0,
430 (SCM vm
, SCM program
, SCM args
),
432 #define FUNC_NAME s_scm_vm_apply
437 SCM_VALIDATE_VM (1, vm
);
438 SCM_VALIDATE_PROC (2, program
);
440 nargs
= scm_ilength (args
);
441 if (SCM_UNLIKELY (nargs
< 0))
442 scm_wrong_type_arg_msg (FUNC_NAME
, 3, args
, "list");
444 argv
= alloca(nargs
* sizeof(SCM
));
445 for (i
= 0; i
< nargs
; i
++)
447 argv
[i
] = SCM_CAR (args
);
448 args
= SCM_CDR (args
);
451 return scm_c_vm_run (vm
, program
, argv
, nargs
);
456 scm_vm_call_with_new_stack (SCM vm
, SCM thunk
, SCM id
)
458 return scm_c_vm_run (vm
, thunk
, NULL
, 0);
461 /* Scheme interface */
463 SCM_DEFINE (scm_vm_version
, "vm-version", 0, 0, 0,
466 #define FUNC_NAME s_scm_vm_version
468 return scm_from_locale_string (PACKAGE_VERSION
);
472 SCM_DEFINE (scm_the_vm
, "the-vm", 0, 0, 0,
475 #define FUNC_NAME s_scm_the_vm
477 scm_i_thread
*t
= SCM_I_CURRENT_THREAD
;
479 if (SCM_UNLIKELY (scm_is_false ((t
->vm
))))
487 SCM_DEFINE (scm_vm_p
, "vm?", 1, 0, 0,
490 #define FUNC_NAME s_scm_vm_p
492 return scm_from_bool (SCM_VM_P (obj
));
496 SCM_DEFINE (scm_make_vm
, "make-vm", 0, 0, 0,
499 #define FUNC_NAME s_scm_make_vm,
505 SCM_DEFINE (scm_vm_ip
, "vm:ip", 1, 0, 0,
508 #define FUNC_NAME s_scm_vm_ip
510 SCM_VALIDATE_VM (1, vm
);
511 return scm_from_ulong ((unsigned long) SCM_VM_DATA (vm
)->ip
);
515 SCM_DEFINE (scm_vm_sp
, "vm:sp", 1, 0, 0,
518 #define FUNC_NAME s_scm_vm_sp
520 SCM_VALIDATE_VM (1, vm
);
521 return scm_from_ulong ((unsigned long) SCM_VM_DATA (vm
)->sp
);
525 SCM_DEFINE (scm_vm_fp
, "vm:fp", 1, 0, 0,
528 #define FUNC_NAME s_scm_vm_fp
530 SCM_VALIDATE_VM (1, vm
);
531 return scm_from_ulong ((unsigned long) SCM_VM_DATA (vm
)->fp
);
535 #define VM_DEFINE_HOOK(n) \
538 SCM_VALIDATE_VM (1, vm); \
539 vp = SCM_VM_DATA (vm); \
540 if (scm_is_false (vp->hooks[n])) \
541 vp->hooks[n] = scm_make_hook (SCM_I_MAKINUM (1)); \
542 return vp->hooks[n]; \
545 SCM_DEFINE (scm_vm_boot_hook
, "vm-boot-hook", 1, 0, 0,
548 #define FUNC_NAME s_scm_vm_boot_hook
550 VM_DEFINE_HOOK (SCM_VM_BOOT_HOOK
);
554 SCM_DEFINE (scm_vm_halt_hook
, "vm-halt-hook", 1, 0, 0,
557 #define FUNC_NAME s_scm_vm_halt_hook
559 VM_DEFINE_HOOK (SCM_VM_HALT_HOOK
);
563 SCM_DEFINE (scm_vm_next_hook
, "vm-next-hook", 1, 0, 0,
566 #define FUNC_NAME s_scm_vm_next_hook
568 VM_DEFINE_HOOK (SCM_VM_NEXT_HOOK
);
572 SCM_DEFINE (scm_vm_break_hook
, "vm-break-hook", 1, 0, 0,
575 #define FUNC_NAME s_scm_vm_break_hook
577 VM_DEFINE_HOOK (SCM_VM_BREAK_HOOK
);
581 SCM_DEFINE (scm_vm_enter_hook
, "vm-enter-hook", 1, 0, 0,
584 #define FUNC_NAME s_scm_vm_enter_hook
586 VM_DEFINE_HOOK (SCM_VM_ENTER_HOOK
);
590 SCM_DEFINE (scm_vm_apply_hook
, "vm-apply-hook", 1, 0, 0,
593 #define FUNC_NAME s_scm_vm_apply_hook
595 VM_DEFINE_HOOK (SCM_VM_APPLY_HOOK
);
599 SCM_DEFINE (scm_vm_exit_hook
, "vm-exit-hook", 1, 0, 0,
602 #define FUNC_NAME s_scm_vm_exit_hook
604 VM_DEFINE_HOOK (SCM_VM_EXIT_HOOK
);
608 SCM_DEFINE (scm_vm_return_hook
, "vm-return-hook", 1, 0, 0,
611 #define FUNC_NAME s_scm_vm_return_hook
613 VM_DEFINE_HOOK (SCM_VM_RETURN_HOOK
);
617 SCM_DEFINE (scm_vm_option
, "vm-option", 2, 0, 0,
620 #define FUNC_NAME s_scm_vm_option
622 SCM_VALIDATE_VM (1, vm
);
623 return scm_assq_ref (SCM_VM_DATA (vm
)->options
, key
);
627 SCM_DEFINE (scm_set_vm_option_x
, "set-vm-option!", 3, 0, 0,
628 (SCM vm
, SCM key
, SCM val
),
630 #define FUNC_NAME s_scm_set_vm_option_x
632 SCM_VALIDATE_VM (1, vm
);
633 SCM_VM_DATA (vm
)->options
634 = scm_assq_set_x (SCM_VM_DATA (vm
)->options
, key
, val
);
635 return SCM_UNSPECIFIED
;
639 SCM_DEFINE (scm_vm_trace_level
, "vm-trace-level", 1, 0, 0,
642 #define FUNC_NAME s_scm_vm_trace_level
644 SCM_VALIDATE_VM (1, vm
);
645 return scm_from_int (SCM_VM_DATA (vm
)->trace_level
);
649 SCM_DEFINE (scm_set_vm_trace_level_x
, "set-vm-trace-level!", 2, 0, 0,
652 #define FUNC_NAME s_scm_set_vm_trace_level_x
654 SCM_VALIDATE_VM (1, vm
);
655 SCM_VM_DATA (vm
)->trace_level
= scm_to_int (level
);
656 return SCM_UNSPECIFIED
;
665 SCM
scm_load_compiled_with_vm (SCM file
)
667 SCM program
= scm_make_program (scm_load_objcode (file
),
668 SCM_BOOL_F
, SCM_BOOL_F
);
670 return scm_c_vm_run (scm_the_vm (), program
, NULL
, 0);
674 scm_bootstrap_vm (void)
676 scm_c_register_extension ("libguile", "scm_init_vm",
677 (scm_t_extension_init_func
)scm_init_vm
, NULL
);
679 #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN
681 GC_new_kind (GC_new_free_list (),
682 GC_MAKE_PROC (GC_new_proc (vm_stack_mark
), 0),
691 #ifndef SCM_MAGIC_SNARFER
692 #include "libguile/vm.x"