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. */
47 #include "vm-bootstrap.h"
49 #include "instructions.h"
52 #include "lang.h" /* NULL_OR_NIL_P */
55 /* I sometimes use this for debugging. */
56 #define vm_puts(OBJ) \
58 scm_display (OBJ, scm_current_error_port ()); \
59 scm_newline (scm_current_error_port ()); \
62 /* The VM has a number of internal assertions that shouldn't normally be
63 necessary, but might be if you think you found a bug in the VM. */
64 #define VM_ENABLE_ASSERTIONS
66 /* We can add a mode that ensures that all stack items above the stack pointer
67 are NULL. This is useful for checking the internal consistency of the VM's
68 assumptions and its operators, but isn't necessary for normal operation. It
69 will ensure that assertions are enabled. */
70 #define VM_ENABLE_STACK_NULLING
72 #if defined (VM_ENABLE_STACK_NULLING) && !defined (VM_ENABLE_ASSERTIONS)
73 #define VM_ENABLE_ASSERTIONS
81 scm_t_bits scm_tc16_vm_cont
;
84 vm_mark_stack (SCM
*base
, scm_t_ptrdiff size
, SCM
*fp
, scm_t_ptrdiff reloc
)
86 SCM
*sp
, *upper
, *lower
;
89 while (sp
> base
&& fp
)
91 upper
= SCM_FRAME_UPPER_ADDRESS (fp
);
92 lower
= SCM_FRAME_LOWER_ADDRESS (fp
);
94 for (; sp
>= upper
; sp
--)
97 if (scm_in_heap_p (*sp
))
100 fprintf (stderr
, "BADNESS: crap on the stack: %p\n", *sp
);
107 /* update fp from the dynamic link */
108 fp
= (SCM
*)*sp
-- + reloc
;
110 /* mark from the el down to the lower address */
111 for (; sp
>= lower
; sp
--)
112 if (*sp
&& SCM_NIMP (*sp
))
118 vm_cont_mark (SCM obj
)
120 struct scm_vm_cont
*p
= SCM_VM_CONT_DATA (obj
);
122 vm_mark_stack (p
->stack_base
, p
->stack_size
, p
->stack_base
+ p
->fp
, p
->reloc
);
128 vm_cont_free (SCM obj
)
130 struct scm_vm_cont
*p
= SCM_VM_CONT_DATA (obj
);
132 scm_gc_free (p
->stack_base
, p
->stack_size
* sizeof (SCM
), "stack-base");
133 scm_gc_free (p
, sizeof (struct scm_vm
), "vm");
139 capture_vm_cont (struct scm_vm
*vp
)
141 struct scm_vm_cont
*p
= scm_gc_malloc (sizeof (*p
), "capture_vm_cont");
142 p
->stack_size
= vp
->sp
- vp
->stack_base
+ 1;
143 p
->stack_base
= scm_gc_malloc (p
->stack_size
* sizeof (SCM
),
145 #ifdef VM_ENABLE_STACK_NULLING
146 if (vp
->sp
>= vp
->stack_base
)
147 if (!vp
->sp
[0] || vp
->sp
[1])
149 memset (p
->stack_base
, 0, p
->stack_size
* sizeof (SCM
));
152 p
->sp
= vp
->sp
- vp
->stack_base
;
153 p
->fp
= vp
->fp
- vp
->stack_base
;
154 memcpy (p
->stack_base
, vp
->stack_base
, p
->stack_size
* sizeof (SCM
));
155 p
->reloc
= p
->stack_base
- vp
->stack_base
;
156 SCM_RETURN_NEWSMOB (scm_tc16_vm_cont
, p
);
160 reinstate_vm_cont (struct scm_vm
*vp
, SCM cont
)
162 struct scm_vm_cont
*p
= SCM_VM_CONT_DATA (cont
);
163 if (vp
->stack_size
< p
->stack_size
)
165 /* puts ("FIXME: Need to expand"); */
168 #ifdef VM_ENABLE_STACK_NULLING
170 scm_t_ptrdiff nzero
= (vp
->sp
- vp
->stack_base
) - p
->sp
;
172 memset (vp
->stack_base
+ p
->stack_size
, 0, nzero
* sizeof (SCM
));
173 /* actually nzero should always be negative, because vm_reset_stack will
174 unwind the stack to some point *below* this continuation */
178 vp
->sp
= vp
->stack_base
+ p
->sp
;
179 vp
->fp
= vp
->stack_base
+ p
->fp
;
180 memcpy (vp
->stack_base
, p
->stack_base
, p
->stack_size
* sizeof (SCM
));
183 /* In theory, a number of vm instances can be active in the call trace, and we
184 only want to reify the continuations of those in the current continuation
185 root. I don't see a nice way to do this -- ideally it would involve dynwinds,
186 and previous values of the *the-vm* fluid within the current continuation
187 root. But we don't have access to continuation roots in the dynwind stack.
188 So, just punt for now -- take the current value of *the-vm*.
190 While I'm on the topic, ideally we could avoid copying the C stack if the
191 continuation root is inside VM code, and call/cc was invoked within that same
192 call to vm_run; but that's currently not implemented.
195 scm_vm_capture_continuations (void)
197 SCM vm
= scm_the_vm ();
198 return scm_acons (vm
, capture_vm_cont (SCM_VM_DATA (vm
)), SCM_EOL
);
202 scm_vm_reinstate_continuations (SCM conts
)
204 for (; conts
!= SCM_EOL
; conts
= SCM_CDR (conts
))
205 reinstate_vm_cont (SCM_VM_DATA (SCM_CAAR (conts
)), SCM_CDAR (conts
));
208 struct vm_unwind_data
216 vm_reset_stack (void *data
)
218 struct vm_unwind_data
*w
= data
;
219 struct scm_vm
*vp
= w
->vp
;
223 #ifdef VM_ENABLE_STACK_NULLING
224 memset (vp
->sp
+ 1, 0, (vp
->stack_size
- (vp
->sp
+ 1 - vp
->stack_base
)) * sizeof(SCM
));
228 static void enfalsen_frame (void *p
)
230 struct scm_vm
*vp
= p
;
231 vp
->trace_frame
= SCM_BOOL_F
;
235 vm_dispatch_hook (SCM vm
, SCM hook
, SCM hook_args
)
237 struct scm_vm
*vp
= SCM_VM_DATA (vm
);
239 if (!SCM_FALSEP (vp
->trace_frame
))
242 scm_dynwind_begin (0);
243 vp
->trace_frame
= scm_c_make_vm_frame (vm
, vp
->fp
, vp
->sp
, vp
->ip
, 0);
244 scm_dynwind_unwind_handler (enfalsen_frame
, vp
, SCM_F_WIND_EXPLICITLY
);
246 scm_c_run_hook (hook
, hook_args
);
253 * VM Internal functions
256 static SCM sym_vm_run
;
257 static SCM sym_vm_error
;
258 static SCM sym_debug
;
261 vm_fetch_length (scm_byte_t
*ip
, size_t *lenp
)
263 /* NOTE: format defined in system/vm/conv.scm */
267 else if (*lenp
== 254)
271 *lenp
= (b1
<< 8) + b2
;
279 *lenp
= (b1
<< 24) + (b2
<< 16) + (b3
<< 8) + b4
;
289 #define VM_DEFAULT_STACK_SIZE (16 * 1024)
291 #define VM_REGULAR_ENGINE 0
292 #define VM_DEBUG_ENGINE 1
295 #define VM_NAME vm_regular_engine
296 #define VM_ENGINE VM_REGULAR_ENGINE
297 #include "vm-engine.c"
302 #define VM_NAME vm_debug_engine
303 #define VM_ENGINE VM_DEBUG_ENGINE
304 #include "vm-engine.c"
308 scm_t_bits scm_tc16_vm
;
312 #define FUNC_NAME "make_vm"
315 struct scm_vm
*vp
= scm_gc_malloc (sizeof (struct scm_vm
), "vm");
317 vp
->stack_size
= VM_DEFAULT_STACK_SIZE
;
318 vp
->stack_base
= scm_gc_malloc (vp
->stack_size
* sizeof (SCM
),
320 #ifdef VM_ENABLE_STACK_NULLING
321 memset (vp
->stack_base
, 0, vp
->stack_size
* sizeof (SCM
));
323 vp
->stack_limit
= vp
->stack_base
+ vp
->stack_size
- 3;
325 vp
->sp
= vp
->stack_base
- 1;
329 vp
->options
= SCM_EOL
;
330 for (i
= 0; i
< SCM_VM_NUM_HOOKS
; i
++)
331 vp
->hooks
[i
] = SCM_BOOL_F
;
332 vp
->trace_frame
= SCM_BOOL_F
;
333 SCM_RETURN_NEWSMOB (scm_tc16_vm
, vp
);
341 struct scm_vm
*vp
= SCM_VM_DATA (obj
);
343 #ifdef VM_ENABLE_STACK_NULLING
344 if (vp
->sp
>= vp
->stack_base
)
345 if (!vp
->sp
[0] || vp
->sp
[1])
349 /* mark the stack, precisely */
350 vm_mark_stack (vp
->stack_base
, vp
->sp
+ 1 - vp
->stack_base
, vp
->fp
, 0);
352 /* mark other objects */
353 for (i
= 0; i
< SCM_VM_NUM_HOOKS
; i
++)
354 scm_gc_mark (vp
->hooks
[i
]);
356 scm_gc_mark (vp
->trace_frame
);
364 struct scm_vm
*vp
= SCM_VM_DATA (obj
);
366 scm_gc_free (vp
->stack_base
, vp
->stack_size
* sizeof (SCM
),
368 scm_gc_free (vp
, sizeof (struct scm_vm
), "vm");
374 scm_vm_apply (SCM vm
, SCM program
, SCM args
)
375 #define FUNC_NAME "scm_vm_apply"
377 SCM_VALIDATE_PROGRAM (1, program
);
378 return vm_run (vm
, program
, args
);
382 /* Scheme interface */
384 SCM_DEFINE (scm_vm_version
, "vm-version", 0, 0, 0,
387 #define FUNC_NAME s_scm_vm_version
389 return scm_from_locale_string (PACKAGE_VERSION
);
393 SCM_DEFINE (scm_the_vm
, "the-vm", 0, 0, 0,
396 #define FUNC_NAME s_scm_the_vm
398 scm_i_thread
*t
= SCM_I_CURRENT_THREAD
;
400 if (SCM_UNLIKELY (SCM_FALSEP ((t
->vm
))))
408 SCM_DEFINE (scm_vm_p
, "vm?", 1, 0, 0,
411 #define FUNC_NAME s_scm_vm_p
413 return SCM_BOOL (SCM_VM_P (obj
));
417 SCM_DEFINE (scm_make_vm
, "make-vm", 0, 0, 0,
420 #define FUNC_NAME s_scm_make_vm,
426 SCM_DEFINE (scm_vm_ip
, "vm:ip", 1, 0, 0,
429 #define FUNC_NAME s_scm_vm_ip
431 SCM_VALIDATE_VM (1, vm
);
432 return scm_from_ulong ((unsigned long) SCM_VM_DATA (vm
)->ip
);
436 SCM_DEFINE (scm_vm_sp
, "vm:sp", 1, 0, 0,
439 #define FUNC_NAME s_scm_vm_sp
441 SCM_VALIDATE_VM (1, vm
);
442 return scm_from_ulong ((unsigned long) SCM_VM_DATA (vm
)->sp
);
446 SCM_DEFINE (scm_vm_fp
, "vm:fp", 1, 0, 0,
449 #define FUNC_NAME s_scm_vm_fp
451 SCM_VALIDATE_VM (1, vm
);
452 return scm_from_ulong ((unsigned long) SCM_VM_DATA (vm
)->fp
);
456 #define VM_DEFINE_HOOK(n) \
459 SCM_VALIDATE_VM (1, vm); \
460 vp = SCM_VM_DATA (vm); \
461 if (SCM_FALSEP (vp->hooks[n])) \
462 vp->hooks[n] = scm_make_hook (SCM_I_MAKINUM (1)); \
463 return vp->hooks[n]; \
466 SCM_DEFINE (scm_vm_boot_hook
, "vm-boot-hook", 1, 0, 0,
469 #define FUNC_NAME s_scm_vm_boot_hook
471 VM_DEFINE_HOOK (SCM_VM_BOOT_HOOK
);
475 SCM_DEFINE (scm_vm_halt_hook
, "vm-halt-hook", 1, 0, 0,
478 #define FUNC_NAME s_scm_vm_halt_hook
480 VM_DEFINE_HOOK (SCM_VM_HALT_HOOK
);
484 SCM_DEFINE (scm_vm_next_hook
, "vm-next-hook", 1, 0, 0,
487 #define FUNC_NAME s_scm_vm_next_hook
489 VM_DEFINE_HOOK (SCM_VM_NEXT_HOOK
);
493 SCM_DEFINE (scm_vm_break_hook
, "vm-break-hook", 1, 0, 0,
496 #define FUNC_NAME s_scm_vm_break_hook
498 VM_DEFINE_HOOK (SCM_VM_BREAK_HOOK
);
502 SCM_DEFINE (scm_vm_enter_hook
, "vm-enter-hook", 1, 0, 0,
505 #define FUNC_NAME s_scm_vm_enter_hook
507 VM_DEFINE_HOOK (SCM_VM_ENTER_HOOK
);
511 SCM_DEFINE (scm_vm_apply_hook
, "vm-apply-hook", 1, 0, 0,
514 #define FUNC_NAME s_scm_vm_apply_hook
516 VM_DEFINE_HOOK (SCM_VM_APPLY_HOOK
);
520 SCM_DEFINE (scm_vm_exit_hook
, "vm-exit-hook", 1, 0, 0,
523 #define FUNC_NAME s_scm_vm_exit_hook
525 VM_DEFINE_HOOK (SCM_VM_EXIT_HOOK
);
529 SCM_DEFINE (scm_vm_return_hook
, "vm-return-hook", 1, 0, 0,
532 #define FUNC_NAME s_scm_vm_return_hook
534 VM_DEFINE_HOOK (SCM_VM_RETURN_HOOK
);
538 SCM_DEFINE (scm_vm_option
, "vm-option", 2, 0, 0,
541 #define FUNC_NAME s_scm_vm_option
543 SCM_VALIDATE_VM (1, vm
);
544 return scm_assq_ref (SCM_VM_DATA (vm
)->options
, key
);
548 SCM_DEFINE (scm_set_vm_option_x
, "set-vm-option!", 3, 0, 0,
549 (SCM vm
, SCM key
, SCM val
),
551 #define FUNC_NAME s_scm_set_vm_option_x
553 SCM_VALIDATE_VM (1, vm
);
554 SCM_VM_DATA (vm
)->options
555 = scm_assq_set_x (SCM_VM_DATA (vm
)->options
, key
, val
);
556 return SCM_UNSPECIFIED
;
560 SCM_DEFINE (scm_vm_stats
, "vm-stats", 1, 0, 0,
563 #define FUNC_NAME s_scm_vm_stats
567 SCM_VALIDATE_VM (1, vm
);
569 stats
= scm_make_vector (SCM_I_MAKINUM (2), SCM_UNSPECIFIED
);
570 scm_vector_set_x (stats
, SCM_I_MAKINUM (0),
571 scm_from_ulong (SCM_VM_DATA (vm
)->time
));
572 scm_vector_set_x (stats
, SCM_I_MAKINUM (1),
573 scm_from_ulong (SCM_VM_DATA (vm
)->clock
));
579 SCM_DEFINE (scm_vm_trace_frame
, "vm-trace-frame", 1, 0, 0,
582 #define FUNC_NAME s_scm_vm_trace_frame
584 SCM_VALIDATE_VM (1, vm
);
585 return SCM_VM_DATA (vm
)->trace_frame
;
594 SCM
scm_load_compiled_with_vm (SCM file
)
596 SCM program
= scm_objcode_to_program (scm_load_objcode (file
), SCM_EOL
);
598 return vm_run (scm_the_vm (), program
, SCM_EOL
);
602 scm_bootstrap_vm (void)
604 static int strappage
= 0;
609 scm_bootstrap_frames ();
610 scm_bootstrap_instructions ();
611 scm_bootstrap_objcodes ();
612 scm_bootstrap_programs ();
614 scm_tc16_vm_cont
= scm_make_smob_type ("vm-cont", 0);
615 scm_set_smob_mark (scm_tc16_vm_cont
, vm_cont_mark
);
616 scm_set_smob_free (scm_tc16_vm_cont
, vm_cont_free
);
618 scm_tc16_vm
= scm_make_smob_type ("vm", 0);
619 scm_set_smob_mark (scm_tc16_vm
, vm_mark
);
620 scm_set_smob_free (scm_tc16_vm
, vm_free
);
621 scm_set_smob_apply (scm_tc16_vm
, scm_vm_apply
, 1, 0, 1);
623 scm_c_define ("load-compiled",
624 scm_c_make_gsubr ("load-compiled/vm", 1, 0, 0,
625 scm_load_compiled_with_vm
));
627 sym_vm_run
= scm_permanent_object (scm_from_locale_symbol ("vm-run"));
628 sym_vm_error
= scm_permanent_object (scm_from_locale_symbol ("vm-error"));
629 sym_debug
= scm_permanent_object (scm_from_locale_symbol ("debug"));
639 #ifndef SCM_MAGIC_SNARFER