1 /* Copyright (C) 2001, 2009 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 "vm-bootstrap.h"
29 #include "instructions.h"
32 #include "lang.h" /* NULL_OR_NIL_P */
35 /* I sometimes use this for debugging. */
36 #define vm_puts(OBJ) \
38 scm_display (OBJ, scm_current_error_port ()); \
39 scm_newline (scm_current_error_port ()); \
42 /* The VM has a number of internal assertions that shouldn't normally be
43 necessary, but might be if you think you found a bug in the VM. */
44 #define VM_ENABLE_ASSERTIONS
46 /* We can add a mode that ensures that all stack items above the stack pointer
47 are NULL. This is useful for checking the internal consistency of the VM's
48 assumptions and its operators, but isn't necessary for normal operation. It
49 will ensure that assertions are enabled. Slows down the VM by about 30%. */
50 /* NB! If you enable this, search for NULLING in throw.c */
51 /* #define VM_ENABLE_STACK_NULLING */
53 /* #define VM_ENABLE_PARANOID_ASSERTIONS */
55 #if defined (VM_ENABLE_STACK_NULLING) && !defined (VM_ENABLE_ASSERTIONS)
56 #define VM_ENABLE_ASSERTIONS
64 scm_t_bits scm_tc16_vm_cont
;
67 vm_mark_stack (SCM
*base
, scm_t_ptrdiff size
, SCM
*fp
, scm_t_ptrdiff reloc
)
69 SCM
*sp
, *upper
, *lower
;
72 while (sp
> base
&& fp
)
74 upper
= SCM_FRAME_UPPER_ADDRESS (fp
);
75 lower
= SCM_FRAME_LOWER_ADDRESS (fp
);
77 for (; sp
>= upper
; sp
--)
80 if (scm_in_heap_p (*sp
))
83 fprintf (stderr
, "BADNESS: crap on the stack: %p\n", *sp
);
90 /* update fp from the dynamic link */
91 fp
= (SCM
*)*sp
-- + reloc
;
93 /* mark from the el down to the lower address */
94 for (; sp
>= lower
; sp
--)
95 if (*sp
&& SCM_NIMP (*sp
))
101 vm_cont_mark (SCM obj
)
103 struct scm_vm_cont
*p
= SCM_VM_CONT_DATA (obj
);
106 vm_mark_stack (p
->stack_base
, p
->stack_size
, p
->fp
+ p
->reloc
, p
->reloc
);
112 vm_cont_free (SCM obj
)
114 struct scm_vm_cont
*p
= SCM_VM_CONT_DATA (obj
);
116 scm_gc_free (p
->stack_base
, p
->stack_size
* sizeof (SCM
), "stack-base");
117 scm_gc_free (p
, sizeof (*p
), "vm-cont");
123 capture_vm_cont (struct scm_vm
*vp
)
125 struct scm_vm_cont
*p
= scm_gc_malloc (sizeof (*p
), "capture_vm_cont");
126 p
->stack_size
= vp
->sp
- vp
->stack_base
+ 1;
127 p
->stack_base
= scm_gc_malloc (p
->stack_size
* sizeof (SCM
),
129 #ifdef VM_ENABLE_STACK_NULLING
130 if (vp
->sp
>= vp
->stack_base
)
131 if (!vp
->sp
[0] || vp
->sp
[1])
133 memset (p
->stack_base
, 0, p
->stack_size
* sizeof (SCM
));
138 memcpy (p
->stack_base
, vp
->stack_base
, p
->stack_size
* sizeof (SCM
));
139 p
->reloc
= p
->stack_base
- vp
->stack_base
;
140 SCM_RETURN_NEWSMOB (scm_tc16_vm_cont
, p
);
144 reinstate_vm_cont (struct scm_vm
*vp
, SCM cont
)
146 struct scm_vm_cont
*p
= SCM_VM_CONT_DATA (cont
);
147 if (vp
->stack_size
< p
->stack_size
)
149 /* puts ("FIXME: Need to expand"); */
152 #ifdef VM_ENABLE_STACK_NULLING
154 scm_t_ptrdiff nzero
= (vp
->sp
- p
->sp
);
156 memset (vp
->stack_base
+ p
->stack_size
, 0, nzero
* sizeof (SCM
));
157 /* actually nzero should always be negative, because vm_reset_stack will
158 unwind the stack to some point *below* this continuation */
164 memcpy (vp
->stack_base
, p
->stack_base
, p
->stack_size
* sizeof (SCM
));
167 /* In theory, a number of vm instances can be active in the call trace, and we
168 only want to reify the continuations of those in the current continuation
169 root. I don't see a nice way to do this -- ideally it would involve dynwinds,
170 and previous values of the *the-vm* fluid within the current continuation
171 root. But we don't have access to continuation roots in the dynwind stack.
172 So, just punt for now -- take the current value of *the-vm*.
174 While I'm on the topic, ideally we could avoid copying the C stack if the
175 continuation root is inside VM code, and call/cc was invoked within that same
176 call to vm_run; but that's currently not implemented.
179 scm_vm_capture_continuations (void)
181 SCM vm
= scm_the_vm ();
182 return scm_acons (vm
, capture_vm_cont (SCM_VM_DATA (vm
)), SCM_EOL
);
186 scm_vm_reinstate_continuations (SCM conts
)
188 for (; conts
!= SCM_EOL
; conts
= SCM_CDR (conts
))
189 reinstate_vm_cont (SCM_VM_DATA (SCM_CAAR (conts
)), SCM_CDAR (conts
));
192 static void enfalsen_frame (void *p
)
194 struct scm_vm
*vp
= p
;
195 vp
->trace_frame
= SCM_BOOL_F
;
199 vm_dispatch_hook (struct scm_vm
*vp
, SCM hook
, SCM hook_args
)
201 if (!SCM_FALSEP (vp
->trace_frame
))
204 scm_dynwind_begin (0);
205 // FIXME, stack holder should be the vm
206 vp
->trace_frame
= scm_c_make_vm_frame (SCM_BOOL_F
, vp
->fp
, vp
->sp
, vp
->ip
, 0);
207 scm_dynwind_unwind_handler (enfalsen_frame
, vp
, SCM_F_WIND_EXPLICITLY
);
209 scm_c_run_hook (hook
, hook_args
);
216 * VM Internal functions
219 static SCM sym_vm_run
;
220 static SCM sym_vm_error
;
221 static SCM sym_debug
;
224 really_make_boot_program (long nargs
)
227 /* Make sure "bytes" is 64-bit aligned. */
228 scm_t_uint8 text
[] = { scm_op_mv_call
, 0, 0, 1,
231 struct scm_objcode
*bp
;
234 if (SCM_UNLIKELY (nargs
> 255 || nargs
< 0))
236 text
[1] = (scm_t_uint8
)nargs
;
238 bp
= scm_gc_malloc (sizeof (struct scm_objcode
) + sizeof (text
),
240 memcpy (bp
->base
, text
, sizeof (text
));
244 bp
->len
= sizeof(text
);
248 u8vec
= scm_take_u8vector ((scm_t_uint8
*)bp
,
249 sizeof (struct scm_objcode
) + sizeof (text
));
250 ret
= scm_make_program (scm_bytecode_to_objcode (u8vec
),
251 SCM_BOOL_F
, SCM_BOOL_F
);
252 SCM_SET_SMOB_FLAGS (ret
, SCM_F_PROGRAM_IS_BOOT
);
256 #define NUM_BOOT_PROGS 8
258 vm_make_boot_program (long nargs
)
260 static SCM programs
[NUM_BOOT_PROGS
] = { 0, };
262 if (SCM_UNLIKELY (!programs
[0]))
265 for (i
= 0; i
< NUM_BOOT_PROGS
; i
++)
266 programs
[i
] = scm_permanent_object (really_make_boot_program (i
));
269 if (SCM_LIKELY (nargs
< NUM_BOOT_PROGS
))
270 return programs
[nargs
];
272 return really_make_boot_program (nargs
);
281 resolve_variable (SCM what
, SCM program_module
)
283 if (SCM_LIKELY (SCM_SYMBOLP (what
)))
285 if (SCM_LIKELY (scm_module_system_booted_p
286 && scm_is_true (program_module
)))
288 return scm_module_lookup (program_module
, what
);
291 SCM v
= scm_sym2var (what
, SCM_BOOL_F
, SCM_BOOL_F
);
292 if (scm_is_false (v
))
293 scm_misc_error (NULL
, "unbound variable: ~S", scm_list_1 (what
));
301 /* compilation of @ or @@
302 `what' is a three-element list: (MODNAME SYM INTERFACE?)
303 INTERFACE? is #t if we compiled @ or #f if we compiled @@
305 mod
= scm_resolve_module (SCM_CAR (what
));
306 if (scm_is_true (SCM_CADDR (what
)))
307 mod
= scm_module_public_interface (mod
);
308 if (SCM_FALSEP (mod
))
309 scm_misc_error (NULL
, "no such module: ~S",
310 scm_list_1 (SCM_CAR (what
)));
312 return scm_module_lookup (mod
, SCM_CADR (what
));
317 #define VM_DEFAULT_STACK_SIZE (64 * 1024)
319 #define VM_NAME vm_regular_engine
320 #define FUNC_NAME "vm-regular-engine"
321 #define VM_ENGINE SCM_VM_REGULAR_ENGINE
322 #include "vm-engine.c"
327 #define VM_NAME vm_debug_engine
328 #define FUNC_NAME "vm-debug-engine"
329 #define VM_ENGINE SCM_VM_DEBUG_ENGINE
330 #include "vm-engine.c"
335 static const scm_t_vm_engine vm_engines
[] =
336 { vm_regular_engine
, vm_debug_engine
};
338 scm_t_bits scm_tc16_vm
;
342 #define FUNC_NAME "make_vm"
347 return SCM_BOOL_F
; /* not booted yet */
349 struct scm_vm
*vp
= scm_gc_malloc (sizeof (struct scm_vm
), "vm");
351 vp
->stack_size
= VM_DEFAULT_STACK_SIZE
;
352 vp
->stack_base
= scm_gc_malloc (vp
->stack_size
* sizeof (SCM
),
354 #ifdef VM_ENABLE_STACK_NULLING
355 memset (vp
->stack_base
, 0, vp
->stack_size
* sizeof (SCM
));
357 vp
->stack_limit
= vp
->stack_base
+ vp
->stack_size
- 3;
359 vp
->sp
= vp
->stack_base
- 1;
361 vp
->engine
= SCM_VM_DEBUG_ENGINE
;
364 vp
->options
= SCM_EOL
;
365 for (i
= 0; i
< SCM_VM_NUM_HOOKS
; i
++)
366 vp
->hooks
[i
] = SCM_BOOL_F
;
367 vp
->trace_frame
= SCM_BOOL_F
;
368 SCM_RETURN_NEWSMOB (scm_tc16_vm
, vp
);
376 struct scm_vm
*vp
= SCM_VM_DATA (obj
);
378 #ifdef VM_ENABLE_STACK_NULLING
379 if (vp
->sp
>= vp
->stack_base
)
380 if (!vp
->sp
[0] || vp
->sp
[1])
384 /* mark the stack, precisely */
385 vm_mark_stack (vp
->stack_base
, vp
->sp
+ 1 - vp
->stack_base
, vp
->fp
, 0);
387 /* mark other objects */
388 for (i
= 0; i
< SCM_VM_NUM_HOOKS
; i
++)
389 scm_gc_mark (vp
->hooks
[i
]);
391 scm_gc_mark (vp
->trace_frame
);
399 struct scm_vm
*vp
= SCM_VM_DATA (obj
);
401 scm_gc_free (vp
->stack_base
, vp
->stack_size
* sizeof (SCM
),
403 scm_gc_free (vp
, sizeof (struct scm_vm
), "vm");
409 scm_c_vm_run (SCM vm
, SCM program
, SCM
*argv
, int nargs
)
411 struct scm_vm
*vp
= SCM_VM_DATA (vm
);
412 return vm_engines
[vp
->engine
](vp
, program
, argv
, nargs
);
416 scm_vm_apply (SCM vm
, SCM program
, SCM args
)
417 #define FUNC_NAME "scm_vm_apply"
422 SCM_VALIDATE_VM (1, vm
);
423 SCM_VALIDATE_PROGRAM (2, program
);
425 nargs
= scm_ilength (args
);
426 if (SCM_UNLIKELY (nargs
< 0))
427 scm_wrong_type_arg_msg (FUNC_NAME
, 3, args
, "list");
429 argv
= alloca(nargs
* sizeof(SCM
));
430 for (i
= 0; i
< nargs
; i
++)
432 argv
[i
] = SCM_CAR (args
);
433 args
= SCM_CDR (args
);
436 return scm_c_vm_run (vm
, program
, argv
, nargs
);
440 /* Scheme interface */
442 SCM_DEFINE (scm_vm_version
, "vm-version", 0, 0, 0,
445 #define FUNC_NAME s_scm_vm_version
447 return scm_from_locale_string (PACKAGE_VERSION
);
451 SCM_DEFINE (scm_the_vm
, "the-vm", 0, 0, 0,
454 #define FUNC_NAME s_scm_the_vm
456 scm_i_thread
*t
= SCM_I_CURRENT_THREAD
;
458 if (SCM_UNLIKELY (SCM_FALSEP ((t
->vm
))))
466 SCM_DEFINE (scm_vm_p
, "vm?", 1, 0, 0,
469 #define FUNC_NAME s_scm_vm_p
471 return SCM_BOOL (SCM_VM_P (obj
));
475 SCM_DEFINE (scm_make_vm
, "make-vm", 0, 0, 0,
478 #define FUNC_NAME s_scm_make_vm,
484 SCM_DEFINE (scm_vm_ip
, "vm:ip", 1, 0, 0,
487 #define FUNC_NAME s_scm_vm_ip
489 SCM_VALIDATE_VM (1, vm
);
490 return scm_from_ulong ((unsigned long) SCM_VM_DATA (vm
)->ip
);
494 SCM_DEFINE (scm_vm_sp
, "vm:sp", 1, 0, 0,
497 #define FUNC_NAME s_scm_vm_sp
499 SCM_VALIDATE_VM (1, vm
);
500 return scm_from_ulong ((unsigned long) SCM_VM_DATA (vm
)->sp
);
504 SCM_DEFINE (scm_vm_fp
, "vm:fp", 1, 0, 0,
507 #define FUNC_NAME s_scm_vm_fp
509 SCM_VALIDATE_VM (1, vm
);
510 return scm_from_ulong ((unsigned long) SCM_VM_DATA (vm
)->fp
);
514 #define VM_DEFINE_HOOK(n) \
517 SCM_VALIDATE_VM (1, vm); \
518 vp = SCM_VM_DATA (vm); \
519 if (SCM_FALSEP (vp->hooks[n])) \
520 vp->hooks[n] = scm_make_hook (SCM_I_MAKINUM (1)); \
521 return vp->hooks[n]; \
524 SCM_DEFINE (scm_vm_boot_hook
, "vm-boot-hook", 1, 0, 0,
527 #define FUNC_NAME s_scm_vm_boot_hook
529 VM_DEFINE_HOOK (SCM_VM_BOOT_HOOK
);
533 SCM_DEFINE (scm_vm_halt_hook
, "vm-halt-hook", 1, 0, 0,
536 #define FUNC_NAME s_scm_vm_halt_hook
538 VM_DEFINE_HOOK (SCM_VM_HALT_HOOK
);
542 SCM_DEFINE (scm_vm_next_hook
, "vm-next-hook", 1, 0, 0,
545 #define FUNC_NAME s_scm_vm_next_hook
547 VM_DEFINE_HOOK (SCM_VM_NEXT_HOOK
);
551 SCM_DEFINE (scm_vm_break_hook
, "vm-break-hook", 1, 0, 0,
554 #define FUNC_NAME s_scm_vm_break_hook
556 VM_DEFINE_HOOK (SCM_VM_BREAK_HOOK
);
560 SCM_DEFINE (scm_vm_enter_hook
, "vm-enter-hook", 1, 0, 0,
563 #define FUNC_NAME s_scm_vm_enter_hook
565 VM_DEFINE_HOOK (SCM_VM_ENTER_HOOK
);
569 SCM_DEFINE (scm_vm_apply_hook
, "vm-apply-hook", 1, 0, 0,
572 #define FUNC_NAME s_scm_vm_apply_hook
574 VM_DEFINE_HOOK (SCM_VM_APPLY_HOOK
);
578 SCM_DEFINE (scm_vm_exit_hook
, "vm-exit-hook", 1, 0, 0,
581 #define FUNC_NAME s_scm_vm_exit_hook
583 VM_DEFINE_HOOK (SCM_VM_EXIT_HOOK
);
587 SCM_DEFINE (scm_vm_return_hook
, "vm-return-hook", 1, 0, 0,
590 #define FUNC_NAME s_scm_vm_return_hook
592 VM_DEFINE_HOOK (SCM_VM_RETURN_HOOK
);
596 SCM_DEFINE (scm_vm_option
, "vm-option", 2, 0, 0,
599 #define FUNC_NAME s_scm_vm_option
601 SCM_VALIDATE_VM (1, vm
);
602 return scm_assq_ref (SCM_VM_DATA (vm
)->options
, key
);
606 SCM_DEFINE (scm_set_vm_option_x
, "set-vm-option!", 3, 0, 0,
607 (SCM vm
, SCM key
, SCM val
),
609 #define FUNC_NAME s_scm_set_vm_option_x
611 SCM_VALIDATE_VM (1, vm
);
612 SCM_VM_DATA (vm
)->options
613 = scm_assq_set_x (SCM_VM_DATA (vm
)->options
, key
, val
);
614 return SCM_UNSPECIFIED
;
618 SCM_DEFINE (scm_vm_stats
, "vm-stats", 1, 0, 0,
621 #define FUNC_NAME s_scm_vm_stats
625 SCM_VALIDATE_VM (1, vm
);
627 stats
= scm_make_vector (SCM_I_MAKINUM (2), SCM_UNSPECIFIED
);
628 scm_vector_set_x (stats
, SCM_I_MAKINUM (0),
629 scm_from_ulong (SCM_VM_DATA (vm
)->time
));
630 scm_vector_set_x (stats
, SCM_I_MAKINUM (1),
631 scm_from_ulong (SCM_VM_DATA (vm
)->clock
));
637 SCM_DEFINE (scm_vm_trace_frame
, "vm-trace-frame", 1, 0, 0,
640 #define FUNC_NAME s_scm_vm_trace_frame
642 SCM_VALIDATE_VM (1, vm
);
643 return SCM_VM_DATA (vm
)->trace_frame
;
652 SCM
scm_load_compiled_with_vm (SCM file
)
654 SCM program
= scm_make_program (scm_load_objcode (file
),
655 SCM_BOOL_F
, SCM_BOOL_F
);
657 return scm_c_vm_run (scm_the_vm (), program
, NULL
, 0);
661 scm_bootstrap_vm (void)
663 static int strappage
= 0;
668 scm_bootstrap_frames ();
669 scm_bootstrap_instructions ();
670 scm_bootstrap_objcodes ();
671 scm_bootstrap_programs ();
673 scm_tc16_vm_cont
= scm_make_smob_type ("vm-cont", 0);
674 scm_set_smob_mark (scm_tc16_vm_cont
, vm_cont_mark
);
675 scm_set_smob_free (scm_tc16_vm_cont
, vm_cont_free
);
677 scm_tc16_vm
= scm_make_smob_type ("vm", 0);
678 scm_set_smob_mark (scm_tc16_vm
, vm_mark
);
679 scm_set_smob_free (scm_tc16_vm
, vm_free
);
680 scm_set_smob_apply (scm_tc16_vm
, scm_vm_apply
, 1, 0, 1);
682 scm_c_define ("load-compiled",
683 scm_c_make_gsubr ("load-compiled/vm", 1, 0, 0,
684 scm_load_compiled_with_vm
));
686 sym_vm_run
= scm_permanent_object (scm_from_locale_symbol ("vm-run"));
687 sym_vm_error
= scm_permanent_object (scm_from_locale_symbol ("vm-error"));
688 sym_debug
= scm_permanent_object (scm_from_locale_symbol ("debug"));
690 scm_c_register_extension ("libguile", "scm_init_vm",
691 (scm_t_extension_init_func
)scm_init_vm
, NULL
);
701 #ifndef SCM_MAGIC_SNARFER
702 #include "libguile/vm.x"