1 /* Copyright (C) 2001 Free Software Foundation, Inc.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
49 #include "vm-bootstrap.h"
51 #include "instructions.h"
54 #include "lang.h" /* NULL_OR_NIL_P */
57 /* I sometimes use this for debugging. */
58 #define vm_puts(OBJ) \
60 scm_display (OBJ, scm_current_error_port ()); \
61 scm_newline (scm_current_error_port ()); \
64 /* The VM has a number of internal assertions that shouldn't normally be
65 necessary, but might be if you think you found a bug in the VM. */
66 #define VM_ENABLE_ASSERTIONS
68 /* We can add a mode that ensures that all stack items above the stack pointer
69 are NULL. This is useful for checking the internal consistency of the VM's
70 assumptions and its operators, but isn't necessary for normal operation. It
71 will ensure that assertions are enabled. Slows down the VM by about 30%. */
72 /* NB! If you enable this, search for NULLING in throw.c */
73 /* #define VM_ENABLE_STACK_NULLING */
75 /* #define VM_ENABLE_PARANOID_ASSERTIONS */
77 #if defined (VM_ENABLE_STACK_NULLING) && !defined (VM_ENABLE_ASSERTIONS)
78 #define VM_ENABLE_ASSERTIONS
86 scm_t_bits scm_tc16_vm_cont
;
89 vm_mark_stack (SCM
*base
, scm_t_ptrdiff size
, SCM
*fp
, scm_t_ptrdiff reloc
)
91 SCM
*sp
, *upper
, *lower
;
94 while (sp
> base
&& fp
)
96 upper
= SCM_FRAME_UPPER_ADDRESS (fp
);
97 lower
= SCM_FRAME_LOWER_ADDRESS (fp
);
99 for (; sp
>= upper
; sp
--)
102 if (scm_in_heap_p (*sp
))
105 fprintf (stderr
, "BADNESS: crap on the stack: %p\n", *sp
);
112 /* update fp from the dynamic link */
113 fp
= (SCM
*)*sp
-- + reloc
;
115 /* mark from the el down to the lower address */
116 for (; sp
>= lower
; sp
--)
117 if (*sp
&& SCM_NIMP (*sp
))
123 vm_cont_mark (SCM obj
)
125 struct scm_vm_cont
*p
= SCM_VM_CONT_DATA (obj
);
128 vm_mark_stack (p
->stack_base
, p
->stack_size
, p
->fp
+ p
->reloc
, p
->reloc
);
134 vm_cont_free (SCM obj
)
136 struct scm_vm_cont
*p
= SCM_VM_CONT_DATA (obj
);
138 scm_gc_free (p
->stack_base
, p
->stack_size
* sizeof (SCM
), "stack-base");
139 scm_gc_free (p
, sizeof (*p
), "vm-cont");
145 capture_vm_cont (struct scm_vm
*vp
)
147 struct scm_vm_cont
*p
= scm_gc_malloc (sizeof (*p
), "capture_vm_cont");
148 p
->stack_size
= vp
->sp
- vp
->stack_base
+ 1;
149 p
->stack_base
= scm_gc_malloc (p
->stack_size
* sizeof (SCM
),
151 #ifdef VM_ENABLE_STACK_NULLING
152 if (vp
->sp
>= vp
->stack_base
)
153 if (!vp
->sp
[0] || vp
->sp
[1])
155 memset (p
->stack_base
, 0, p
->stack_size
* sizeof (SCM
));
160 memcpy (p
->stack_base
, vp
->stack_base
, p
->stack_size
* sizeof (SCM
));
161 p
->reloc
= p
->stack_base
- vp
->stack_base
;
162 SCM_RETURN_NEWSMOB (scm_tc16_vm_cont
, p
);
166 reinstate_vm_cont (struct scm_vm
*vp
, SCM cont
)
168 struct scm_vm_cont
*p
= SCM_VM_CONT_DATA (cont
);
169 if (vp
->stack_size
< p
->stack_size
)
171 /* puts ("FIXME: Need to expand"); */
174 #ifdef VM_ENABLE_STACK_NULLING
176 scm_t_ptrdiff nzero
= (vp
->sp
- p
->sp
);
178 memset (vp
->stack_base
+ p
->stack_size
, 0, nzero
* sizeof (SCM
));
179 /* actually nzero should always be negative, because vm_reset_stack will
180 unwind the stack to some point *below* this continuation */
186 memcpy (vp
->stack_base
, p
->stack_base
, p
->stack_size
* sizeof (SCM
));
189 /* In theory, a number of vm instances can be active in the call trace, and we
190 only want to reify the continuations of those in the current continuation
191 root. I don't see a nice way to do this -- ideally it would involve dynwinds,
192 and previous values of the *the-vm* fluid within the current continuation
193 root. But we don't have access to continuation roots in the dynwind stack.
194 So, just punt for now -- take the current value of *the-vm*.
196 While I'm on the topic, ideally we could avoid copying the C stack if the
197 continuation root is inside VM code, and call/cc was invoked within that same
198 call to vm_run; but that's currently not implemented.
201 scm_vm_capture_continuations (void)
203 SCM vm
= scm_the_vm ();
204 return scm_acons (vm
, capture_vm_cont (SCM_VM_DATA (vm
)), SCM_EOL
);
208 scm_vm_reinstate_continuations (SCM conts
)
210 for (; conts
!= SCM_EOL
; conts
= SCM_CDR (conts
))
211 reinstate_vm_cont (SCM_VM_DATA (SCM_CAAR (conts
)), SCM_CDAR (conts
));
214 static void enfalsen_frame (void *p
)
216 struct scm_vm
*vp
= p
;
217 vp
->trace_frame
= SCM_BOOL_F
;
221 vm_dispatch_hook (struct scm_vm
*vp
, SCM hook
, SCM hook_args
)
223 if (!SCM_FALSEP (vp
->trace_frame
))
226 scm_dynwind_begin (0);
227 // FIXME, stack holder should be the vm
228 vp
->trace_frame
= scm_c_make_vm_frame (SCM_BOOL_F
, vp
->fp
, vp
->sp
, vp
->ip
, 0);
229 scm_dynwind_unwind_handler (enfalsen_frame
, vp
, SCM_F_WIND_EXPLICITLY
);
231 scm_c_run_hook (hook
, hook_args
);
238 * VM Internal functions
241 static SCM sym_vm_run
;
242 static SCM sym_vm_error
;
243 static SCM sym_debug
;
245 static SCM
make_u8vector (const scm_t_uint8
*bytes
, size_t len
)
247 scm_t_uint8
*new_bytes
= scm_gc_malloc (len
, "make-u8vector");
248 memcpy (new_bytes
, bytes
, len
);
249 return scm_take_u8vector (new_bytes
, len
);
253 really_make_boot_program (long nargs
)
255 scm_byte_t bytes
[] = {0, 0, 0, 0,
258 scm_op_mv_call
, 0, 0, 1, scm_op_make_int8_1
, scm_op_halt
};
260 ((scm_t_uint32
*)bytes
)[1] = 6; /* set len in current endianness, no meta */
261 if (SCM_UNLIKELY (nargs
> 255 || nargs
< 0))
263 bytes
[13] = (scm_byte_t
)nargs
;
264 ret
= scm_make_program (scm_bytecode_to_objcode (make_u8vector (bytes
, sizeof(bytes
))),
265 SCM_BOOL_F
, SCM_EOL
);
266 SCM_SET_SMOB_FLAGS (ret
, SCM_F_PROGRAM_IS_BOOT
);
269 #define NUM_BOOT_PROGS 8
271 vm_make_boot_program (long nargs
)
273 static SCM programs
[NUM_BOOT_PROGS
] = { 0, };
275 if (SCM_UNLIKELY (!programs
[0]))
278 for (i
= 0; i
< NUM_BOOT_PROGS
; i
++)
279 programs
[i
] = scm_permanent_object (really_make_boot_program (i
));
282 if (SCM_LIKELY (nargs
< NUM_BOOT_PROGS
))
283 return programs
[nargs
];
285 return really_make_boot_program (nargs
);
293 #define VM_DEFAULT_STACK_SIZE (16 * 1024)
295 #define VM_NAME vm_regular_engine
296 #define FUNC_NAME "vm-regular-engine"
297 #define VM_ENGINE SCM_VM_REGULAR_ENGINE
298 #include "vm-engine.c"
303 #define VM_NAME vm_debug_engine
304 #define FUNC_NAME "vm-debug-engine"
305 #define VM_ENGINE SCM_VM_DEBUG_ENGINE
306 #include "vm-engine.c"
311 static const scm_t_vm_engine vm_engines
[] =
312 { vm_regular_engine
, vm_debug_engine
};
314 scm_t_bits scm_tc16_vm
;
318 #define FUNC_NAME "make_vm"
323 return SCM_BOOL_F
; /* not booted yet */
325 struct scm_vm
*vp
= scm_gc_malloc (sizeof (struct scm_vm
), "vm");
327 vp
->stack_size
= VM_DEFAULT_STACK_SIZE
;
328 vp
->stack_base
= scm_gc_malloc (vp
->stack_size
* sizeof (SCM
),
330 #ifdef VM_ENABLE_STACK_NULLING
331 memset (vp
->stack_base
, 0, vp
->stack_size
* sizeof (SCM
));
333 vp
->stack_limit
= vp
->stack_base
+ vp
->stack_size
- 3;
335 vp
->sp
= vp
->stack_base
- 1;
337 vp
->engine
= SCM_VM_DEBUG_ENGINE
;
340 vp
->options
= SCM_EOL
;
341 for (i
= 0; i
< SCM_VM_NUM_HOOKS
; i
++)
342 vp
->hooks
[i
] = SCM_BOOL_F
;
343 vp
->trace_frame
= SCM_BOOL_F
;
344 SCM_RETURN_NEWSMOB (scm_tc16_vm
, vp
);
352 struct scm_vm
*vp
= SCM_VM_DATA (obj
);
354 #ifdef VM_ENABLE_STACK_NULLING
355 if (vp
->sp
>= vp
->stack_base
)
356 if (!vp
->sp
[0] || vp
->sp
[1])
360 /* mark the stack, precisely */
361 vm_mark_stack (vp
->stack_base
, vp
->sp
+ 1 - vp
->stack_base
, vp
->fp
, 0);
363 /* mark other objects */
364 for (i
= 0; i
< SCM_VM_NUM_HOOKS
; i
++)
365 scm_gc_mark (vp
->hooks
[i
]);
367 scm_gc_mark (vp
->trace_frame
);
375 struct scm_vm
*vp
= SCM_VM_DATA (obj
);
377 scm_gc_free (vp
->stack_base
, vp
->stack_size
* sizeof (SCM
),
379 scm_gc_free (vp
, sizeof (struct scm_vm
), "vm");
385 scm_c_vm_run (SCM vm
, SCM program
, SCM
*argv
, int nargs
)
387 struct scm_vm
*vp
= SCM_VM_DATA (vm
);
388 return vm_engines
[vp
->engine
](vp
, program
, argv
, nargs
);
392 scm_vm_apply (SCM vm
, SCM program
, SCM args
)
393 #define FUNC_NAME "scm_vm_apply"
398 SCM_VALIDATE_VM (1, vm
);
399 SCM_VALIDATE_PROGRAM (2, program
);
401 nargs
= scm_ilength (args
);
402 if (SCM_UNLIKELY (nargs
< 0))
403 scm_wrong_type_arg_msg (FUNC_NAME
, 3, args
, "list");
405 argv
= alloca(nargs
* sizeof(SCM
));
406 for (i
= 0; i
< nargs
; i
++)
408 argv
[i
] = SCM_CAR (args
);
409 args
= SCM_CDR (args
);
412 return scm_c_vm_run (vm
, program
, argv
, nargs
);
416 /* Scheme interface */
418 SCM_DEFINE (scm_vm_version
, "vm-version", 0, 0, 0,
421 #define FUNC_NAME s_scm_vm_version
423 return scm_from_locale_string (PACKAGE_VERSION
);
427 SCM_DEFINE (scm_the_vm
, "the-vm", 0, 0, 0,
430 #define FUNC_NAME s_scm_the_vm
432 scm_i_thread
*t
= SCM_I_CURRENT_THREAD
;
434 if (SCM_UNLIKELY (SCM_FALSEP ((t
->vm
))))
442 SCM_DEFINE (scm_vm_p
, "vm?", 1, 0, 0,
445 #define FUNC_NAME s_scm_vm_p
447 return SCM_BOOL (SCM_VM_P (obj
));
451 SCM_DEFINE (scm_make_vm
, "make-vm", 0, 0, 0,
454 #define FUNC_NAME s_scm_make_vm,
460 SCM_DEFINE (scm_vm_ip
, "vm:ip", 1, 0, 0,
463 #define FUNC_NAME s_scm_vm_ip
465 SCM_VALIDATE_VM (1, vm
);
466 return scm_from_ulong ((unsigned long) SCM_VM_DATA (vm
)->ip
);
470 SCM_DEFINE (scm_vm_sp
, "vm:sp", 1, 0, 0,
473 #define FUNC_NAME s_scm_vm_sp
475 SCM_VALIDATE_VM (1, vm
);
476 return scm_from_ulong ((unsigned long) SCM_VM_DATA (vm
)->sp
);
480 SCM_DEFINE (scm_vm_fp
, "vm:fp", 1, 0, 0,
483 #define FUNC_NAME s_scm_vm_fp
485 SCM_VALIDATE_VM (1, vm
);
486 return scm_from_ulong ((unsigned long) SCM_VM_DATA (vm
)->fp
);
490 #define VM_DEFINE_HOOK(n) \
493 SCM_VALIDATE_VM (1, vm); \
494 vp = SCM_VM_DATA (vm); \
495 if (SCM_FALSEP (vp->hooks[n])) \
496 vp->hooks[n] = scm_make_hook (SCM_I_MAKINUM (1)); \
497 return vp->hooks[n]; \
500 SCM_DEFINE (scm_vm_boot_hook
, "vm-boot-hook", 1, 0, 0,
503 #define FUNC_NAME s_scm_vm_boot_hook
505 VM_DEFINE_HOOK (SCM_VM_BOOT_HOOK
);
509 SCM_DEFINE (scm_vm_halt_hook
, "vm-halt-hook", 1, 0, 0,
512 #define FUNC_NAME s_scm_vm_halt_hook
514 VM_DEFINE_HOOK (SCM_VM_HALT_HOOK
);
518 SCM_DEFINE (scm_vm_next_hook
, "vm-next-hook", 1, 0, 0,
521 #define FUNC_NAME s_scm_vm_next_hook
523 VM_DEFINE_HOOK (SCM_VM_NEXT_HOOK
);
527 SCM_DEFINE (scm_vm_break_hook
, "vm-break-hook", 1, 0, 0,
530 #define FUNC_NAME s_scm_vm_break_hook
532 VM_DEFINE_HOOK (SCM_VM_BREAK_HOOK
);
536 SCM_DEFINE (scm_vm_enter_hook
, "vm-enter-hook", 1, 0, 0,
539 #define FUNC_NAME s_scm_vm_enter_hook
541 VM_DEFINE_HOOK (SCM_VM_ENTER_HOOK
);
545 SCM_DEFINE (scm_vm_apply_hook
, "vm-apply-hook", 1, 0, 0,
548 #define FUNC_NAME s_scm_vm_apply_hook
550 VM_DEFINE_HOOK (SCM_VM_APPLY_HOOK
);
554 SCM_DEFINE (scm_vm_exit_hook
, "vm-exit-hook", 1, 0, 0,
557 #define FUNC_NAME s_scm_vm_exit_hook
559 VM_DEFINE_HOOK (SCM_VM_EXIT_HOOK
);
563 SCM_DEFINE (scm_vm_return_hook
, "vm-return-hook", 1, 0, 0,
566 #define FUNC_NAME s_scm_vm_return_hook
568 VM_DEFINE_HOOK (SCM_VM_RETURN_HOOK
);
572 SCM_DEFINE (scm_vm_option
, "vm-option", 2, 0, 0,
575 #define FUNC_NAME s_scm_vm_option
577 SCM_VALIDATE_VM (1, vm
);
578 return scm_assq_ref (SCM_VM_DATA (vm
)->options
, key
);
582 SCM_DEFINE (scm_set_vm_option_x
, "set-vm-option!", 3, 0, 0,
583 (SCM vm
, SCM key
, SCM val
),
585 #define FUNC_NAME s_scm_set_vm_option_x
587 SCM_VALIDATE_VM (1, vm
);
588 SCM_VM_DATA (vm
)->options
589 = scm_assq_set_x (SCM_VM_DATA (vm
)->options
, key
, val
);
590 return SCM_UNSPECIFIED
;
594 SCM_DEFINE (scm_vm_stats
, "vm-stats", 1, 0, 0,
597 #define FUNC_NAME s_scm_vm_stats
601 SCM_VALIDATE_VM (1, vm
);
603 stats
= scm_make_vector (SCM_I_MAKINUM (2), SCM_UNSPECIFIED
);
604 scm_vector_set_x (stats
, SCM_I_MAKINUM (0),
605 scm_from_ulong (SCM_VM_DATA (vm
)->time
));
606 scm_vector_set_x (stats
, SCM_I_MAKINUM (1),
607 scm_from_ulong (SCM_VM_DATA (vm
)->clock
));
613 SCM_DEFINE (scm_vm_trace_frame
, "vm-trace-frame", 1, 0, 0,
616 #define FUNC_NAME s_scm_vm_trace_frame
618 SCM_VALIDATE_VM (1, vm
);
619 return SCM_VM_DATA (vm
)->trace_frame
;
628 SCM
scm_load_compiled_with_vm (SCM file
)
630 SCM program
= scm_make_program (scm_load_objcode (file
),
631 SCM_BOOL_F
, SCM_EOL
);
633 return scm_c_vm_run (scm_the_vm (), program
, NULL
, 0);
637 scm_bootstrap_vm (void)
639 static int strappage
= 0;
644 scm_bootstrap_frames ();
645 scm_bootstrap_instructions ();
646 scm_bootstrap_objcodes ();
647 scm_bootstrap_programs ();
649 scm_tc16_vm_cont
= scm_make_smob_type ("vm-cont", 0);
650 scm_set_smob_mark (scm_tc16_vm_cont
, vm_cont_mark
);
651 scm_set_smob_free (scm_tc16_vm_cont
, vm_cont_free
);
653 scm_tc16_vm
= scm_make_smob_type ("vm", 0);
654 scm_set_smob_mark (scm_tc16_vm
, vm_mark
);
655 scm_set_smob_free (scm_tc16_vm
, vm_free
);
656 scm_set_smob_apply (scm_tc16_vm
, scm_vm_apply
, 1, 0, 1);
658 scm_c_define ("load-compiled",
659 scm_c_make_gsubr ("load-compiled/vm", 1, 0, 0,
660 scm_load_compiled_with_vm
));
662 sym_vm_run
= scm_permanent_object (scm_from_locale_symbol ("vm-run"));
663 sym_vm_error
= scm_permanent_object (scm_from_locale_symbol ("vm-error"));
664 sym_debug
= scm_permanent_object (scm_from_locale_symbol ("debug"));
666 scm_c_register_extension ("libguile", "scm_init_vm",
667 (scm_t_extension_init_func
)scm_init_vm
, NULL
);
677 #ifndef SCM_MAGIC_SNARFER
678 #include "libguile/vm.x"