Expandable stacks.
[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 /* For mremap(2) on GNU/Linux systems. */
20 #define _GNU_SOURCE
21
22 #if HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <alloca.h>
28 #include <alignof.h>
29 #include <string.h>
30 #include <stdint.h>
31
32 #ifdef HAVE_SYS_MMAN_H
33 #include <sys/mman.h>
34 #endif
35
36 #include "libguile/bdw-gc.h"
37 #include <gc/gc_mark.h>
38
39 #include "_scm.h"
40 #include "control.h"
41 #include "frames.h"
42 #include "instructions.h"
43 #include "loader.h"
44 #include "programs.h"
45 #include "vm.h"
46 #include "vm-builtins.h"
47
48 #include "private-gc.h" /* scm_getenv_int */
49
50 static int vm_default_engine = SCM_VM_REGULAR_ENGINE;
51
52 /* Unfortunately we can't snarf these: snarfed things are only loaded up from
53 (system vm vm), which might not be loaded before an error happens. */
54 static SCM sym_vm_run;
55 static SCM sym_vm_error;
56 static SCM sym_keyword_argument_error;
57 static SCM sym_regular;
58 static SCM sym_debug;
59
60 /* The VM has a number of internal assertions that shouldn't normally be
61 necessary, but might be if you think you found a bug in the VM. */
62 #define VM_ENABLE_ASSERTIONS
63
64 /* #define VM_ENABLE_PARANOID_ASSERTIONS */
65
66
67 \f
68 /*
69 * VM Continuation
70 */
71
72 void
73 scm_i_vm_cont_print (SCM x, SCM port, scm_print_state *pstate)
74 {
75 scm_puts_unlocked ("#<vm-continuation ", port);
76 scm_uintprint (SCM_UNPACK (x), 16, port);
77 scm_puts_unlocked (">", port);
78 }
79
80 /* In theory, a number of vm instances can be active in the call trace, and we
81 only want to reify the continuations of those in the current continuation
82 root. I don't see a nice way to do this -- ideally it would involve dynwinds,
83 and previous values of the *the-vm* fluid within the current continuation
84 root. But we don't have access to continuation roots in the dynwind stack.
85 So, just punt for now, we just capture the continuation for the current VM.
86
87 While I'm on the topic, ideally we could avoid copying the C stack if the
88 continuation root is inside VM code, and call/cc was invoked within that same
89 call to vm_run; but that's currently not implemented.
90 */
91 SCM
92 scm_i_vm_capture_stack (SCM *stack_base, SCM *fp, SCM *sp, scm_t_uint32 *ra,
93 scm_t_dynstack *dynstack, scm_t_uint32 flags)
94 {
95 struct scm_vm_cont *p;
96
97 p = scm_gc_malloc (sizeof (*p), "capture_vm_cont");
98 p->stack_size = sp - stack_base + 1;
99 p->stack_base = scm_gc_malloc (p->stack_size * sizeof (SCM),
100 "capture_vm_cont");
101 p->ra = ra;
102 p->sp = sp;
103 p->fp = fp;
104 memcpy (p->stack_base, stack_base, (sp + 1 - stack_base) * sizeof (SCM));
105 p->reloc = p->stack_base - stack_base;
106 p->dynstack = dynstack;
107 p->flags = flags;
108 return scm_cell (scm_tc7_vm_cont, (scm_t_bits)p);
109 }
110
111 static void
112 vm_return_to_continuation (struct scm_vm *vp, SCM cont, size_t n, SCM *argv)
113 {
114 struct scm_vm_cont *cp;
115 SCM *argv_copy;
116
117 argv_copy = alloca (n * sizeof(SCM));
118 memcpy (argv_copy, argv, n * sizeof(SCM));
119
120 cp = SCM_VM_CONT_DATA (cont);
121
122 if (vp->stack_size < cp->stack_size + n + 3)
123 scm_misc_error ("vm-engine", "not enough space to reinstate continuation",
124 scm_list_1 (cont));
125
126 vp->sp = cp->sp;
127 vp->fp = cp->fp;
128 memcpy (vp->stack_base, cp->stack_base, cp->stack_size * sizeof (SCM));
129
130 {
131 size_t i;
132
133 /* Push on an empty frame, as the continuation expects. */
134 for (i = 0; i < 3; i++)
135 {
136 vp->sp++;
137 *vp->sp = SCM_BOOL_F;
138 }
139
140 /* Push the return values. */
141 for (i = 0; i < n; i++)
142 {
143 vp->sp++;
144 *vp->sp = argv_copy[i];
145 }
146 vp->ip = cp->ra;
147 }
148 }
149
150 static struct scm_vm * thread_vm (scm_i_thread *t);
151 SCM
152 scm_i_capture_current_stack (void)
153 {
154 scm_i_thread *thread;
155 struct scm_vm *vp;
156
157 thread = SCM_I_CURRENT_THREAD;
158 vp = thread_vm (thread);
159
160 return scm_i_vm_capture_stack (vp->stack_base, vp->fp, vp->sp, vp->ip,
161 scm_dynstack_capture_all (&thread->dynstack),
162 0);
163 }
164
165 static void vm_dispatch_apply_hook (struct scm_vm *vp) SCM_NOINLINE;
166 static void vm_dispatch_push_continuation_hook (struct scm_vm *vp) SCM_NOINLINE;
167 static void vm_dispatch_pop_continuation_hook (struct scm_vm *vp, SCM *old_fp) SCM_NOINLINE;
168 static void vm_dispatch_next_hook (struct scm_vm *vp) SCM_NOINLINE;
169 static void vm_dispatch_abort_hook (struct scm_vm *vp) SCM_NOINLINE;
170 static void vm_dispatch_restore_continuation_hook (struct scm_vm *vp) SCM_NOINLINE;
171
172 static void
173 vm_dispatch_hook (struct scm_vm *vp, int hook_num, SCM *argv, int n)
174 {
175 SCM hook;
176 struct scm_frame c_frame;
177 scm_t_cell *frame;
178 int saved_trace_level;
179
180 hook = vp->hooks[hook_num];
181
182 if (SCM_LIKELY (scm_is_false (hook))
183 || scm_is_null (SCM_HOOK_PROCEDURES (hook)))
184 return;
185
186 saved_trace_level = vp->trace_level;
187 vp->trace_level = 0;
188
189 /* Allocate a frame object on the stack. This is more efficient than calling
190 `scm_c_make_frame ()' to allocate on the heap, but it forces hooks to not
191 capture frame objects.
192
193 At the same time, procedures such as `frame-procedure' make sense only
194 while the stack frame represented by the frame object is visible, so it
195 seems reasonable to limit the lifetime of frame objects. */
196
197 c_frame.stack_holder = vp;
198 c_frame.fp_offset = vp->fp - vp->stack_base;
199 c_frame.sp_offset = vp->sp - vp->stack_base;
200 c_frame.ip = vp->ip;
201
202 /* Arrange for FRAME to be 8-byte aligned, like any other cell. */
203 frame = alloca (sizeof (*frame) + 8);
204 frame = (scm_t_cell *) ROUND_UP ((scm_t_uintptr) frame, 8UL);
205
206 frame->word_0 = SCM_PACK (scm_tc7_frame | (SCM_VM_FRAME_KIND_VM << 8));
207 frame->word_1 = SCM_PACK_POINTER (&c_frame);
208
209 if (n == 0)
210 {
211 SCM args[1];
212
213 args[0] = SCM_PACK_POINTER (frame);
214 scm_c_run_hookn (hook, args, 1);
215 }
216 else if (n == 1)
217 {
218 SCM args[2];
219
220 args[0] = SCM_PACK_POINTER (frame);
221 args[1] = argv[0];
222 scm_c_run_hookn (hook, args, 2);
223 }
224 else
225 {
226 SCM args = SCM_EOL;
227
228 while (n--)
229 args = scm_cons (argv[n], args);
230 scm_c_run_hook (hook, scm_cons (SCM_PACK_POINTER (frame), args));
231 }
232
233 vp->trace_level = saved_trace_level;
234 }
235
236 static void
237 vm_dispatch_apply_hook (struct scm_vm *vp)
238 {
239 return vm_dispatch_hook (vp, SCM_VM_APPLY_HOOK, NULL, 0);
240 }
241 static void vm_dispatch_push_continuation_hook (struct scm_vm *vp)
242 {
243 return vm_dispatch_hook (vp, SCM_VM_PUSH_CONTINUATION_HOOK, NULL, 0);
244 }
245 static void vm_dispatch_pop_continuation_hook (struct scm_vm *vp, SCM *old_fp)
246 {
247 return vm_dispatch_hook (vp, SCM_VM_POP_CONTINUATION_HOOK,
248 &SCM_FRAME_LOCAL (old_fp, 1),
249 SCM_FRAME_NUM_LOCALS (old_fp, vp->sp) - 1);
250 }
251 static void vm_dispatch_next_hook (struct scm_vm *vp)
252 {
253 return vm_dispatch_hook (vp, SCM_VM_NEXT_HOOK, NULL, 0);
254 }
255 static void vm_dispatch_abort_hook (struct scm_vm *vp)
256 {
257 return vm_dispatch_hook (vp, SCM_VM_ABORT_CONTINUATION_HOOK,
258 &SCM_FRAME_LOCAL (vp->fp, 1),
259 SCM_FRAME_NUM_LOCALS (vp->fp, vp->sp) - 1);
260 }
261 static void vm_dispatch_restore_continuation_hook (struct scm_vm *vp)
262 {
263 return vm_dispatch_hook (vp, SCM_VM_RESTORE_CONTINUATION_HOOK, NULL, 0);
264 }
265
266 static void
267 vm_abort (struct scm_vm *vp, SCM tag,
268 size_t nstack, SCM *stack_args, SCM tail, SCM *sp,
269 scm_i_jmp_buf *current_registers) SCM_NORETURN;
270
271 static void
272 vm_abort (struct scm_vm *vp, SCM tag,
273 size_t nstack, SCM *stack_args, SCM tail, SCM *sp,
274 scm_i_jmp_buf *current_registers)
275 {
276 size_t i;
277 ssize_t tail_len;
278 SCM *argv;
279
280 tail_len = scm_ilength (tail);
281 if (tail_len < 0)
282 scm_misc_error ("vm-engine", "tail values to abort should be a list",
283 scm_list_1 (tail));
284
285 argv = alloca ((nstack + tail_len) * sizeof (SCM));
286 for (i = 0; i < nstack; i++)
287 argv[i] = stack_args[i];
288 for (; i < nstack + tail_len; i++, tail = scm_cdr (tail))
289 argv[i] = scm_car (tail);
290
291 /* FIXME: NULLSTACK (SCM_VM_DATA (vp)->sp - sp) */
292 vp->sp = sp;
293
294 scm_c_abort (vp, tag, nstack + tail_len, argv, current_registers);
295 }
296
297 static void
298 vm_reinstate_partial_continuation (struct scm_vm *vp, SCM cont,
299 size_t n, SCM *argv,
300 scm_t_dynstack *dynstack,
301 scm_i_jmp_buf *registers)
302 {
303 struct scm_vm_cont *cp;
304 SCM *argv_copy, *base;
305 scm_t_ptrdiff reloc;
306 size_t i;
307
308 argv_copy = alloca (n * sizeof(SCM));
309 memcpy (argv_copy, argv, n * sizeof(SCM));
310
311 cp = SCM_VM_CONT_DATA (cont);
312 base = SCM_FRAME_LOCALS_ADDRESS (vp->fp);
313 reloc = cp->reloc + (base - cp->stack_base);
314
315 #define RELOC(scm_p) \
316 (((SCM *) (scm_p)) + reloc)
317
318 if ((base - vp->stack_base) + cp->stack_size + n + 1 > vp->stack_size)
319 scm_misc_error ("vm-engine",
320 "not enough space to instate partial continuation",
321 scm_list_1 (cont));
322
323 memcpy (base, cp->stack_base, cp->stack_size * sizeof (SCM));
324
325 /* now relocate frame pointers */
326 {
327 SCM *fp;
328 for (fp = RELOC (cp->fp);
329 SCM_FRAME_LOWER_ADDRESS (fp) > base;
330 fp = SCM_FRAME_DYNAMIC_LINK (fp))
331 SCM_FRAME_SET_DYNAMIC_LINK (fp, RELOC (SCM_FRAME_DYNAMIC_LINK (fp)));
332 }
333
334 vp->sp = base - 1 + cp->stack_size;
335 vp->fp = RELOC (cp->fp);
336 vp->ip = cp->ra;
337
338 /* Push the arguments. */
339 for (i = 0; i < n; i++)
340 {
341 vp->sp++;
342 *vp->sp = argv_copy[i];
343 }
344
345 /* The prompt captured a slice of the dynamic stack. Here we wind
346 those entries onto the current thread's stack. We also have to
347 relocate any prompts that we see along the way. */
348 {
349 scm_t_bits *walk;
350
351 for (walk = SCM_DYNSTACK_FIRST (cp->dynstack);
352 SCM_DYNSTACK_TAG (walk);
353 walk = SCM_DYNSTACK_NEXT (walk))
354 {
355 scm_t_bits tag = SCM_DYNSTACK_TAG (walk);
356
357 if (SCM_DYNSTACK_TAG_TYPE (tag) == SCM_DYNSTACK_TYPE_PROMPT)
358 scm_dynstack_wind_prompt (dynstack, walk, reloc, registers);
359 else
360 scm_dynstack_wind_1 (dynstack, walk);
361 }
362 }
363 #undef RELOC
364 }
365
366 \f
367 /*
368 * VM Error Handling
369 */
370
371 static void vm_error (const char *msg, SCM arg) SCM_NORETURN;
372 static void vm_error_bad_instruction (scm_t_uint32 inst) SCM_NORETURN SCM_NOINLINE;
373 static void vm_error_unbound (SCM proc, SCM sym) SCM_NORETURN SCM_NOINLINE;
374 static void vm_error_unbound_fluid (SCM proc, SCM fluid) SCM_NORETURN SCM_NOINLINE;
375 static void vm_error_not_a_variable (const char *func_name, SCM x) SCM_NORETURN SCM_NOINLINE;
376 static void vm_error_apply_to_non_list (SCM x) SCM_NORETURN SCM_NOINLINE;
377 static void vm_error_kwargs_length_not_even (SCM proc) SCM_NORETURN SCM_NOINLINE;
378 static void vm_error_kwargs_invalid_keyword (SCM proc, SCM obj) SCM_NORETURN SCM_NOINLINE;
379 static void vm_error_kwargs_unrecognized_keyword (SCM proc, SCM kw) SCM_NORETURN SCM_NOINLINE;
380 static void vm_error_too_many_args (int nargs) SCM_NORETURN SCM_NOINLINE;
381 static void vm_error_wrong_num_args (SCM proc) SCM_NORETURN SCM_NOINLINE;
382 static void vm_error_wrong_type_apply (SCM proc) SCM_NORETURN SCM_NOINLINE;
383 static void vm_error_stack_underflow (void) SCM_NORETURN SCM_NOINLINE;
384 static void vm_error_improper_list (SCM x) SCM_NORETURN SCM_NOINLINE;
385 static void vm_error_not_a_pair (const char *subr, SCM x) SCM_NORETURN SCM_NOINLINE;
386 static void vm_error_not_a_bytevector (const char *subr, SCM x) SCM_NORETURN SCM_NOINLINE;
387 static void vm_error_not_a_struct (const char *subr, SCM x) SCM_NORETURN SCM_NOINLINE;
388 static void vm_error_no_values (void) SCM_NORETURN SCM_NOINLINE;
389 static void vm_error_not_enough_values (void) SCM_NORETURN SCM_NOINLINE;
390 static void vm_error_wrong_number_of_values (scm_t_uint32 expected) SCM_NORETURN SCM_NOINLINE;
391 static void vm_error_continuation_not_rewindable (SCM cont) SCM_NORETURN SCM_NOINLINE;
392 static void vm_error_bad_wide_string_length (size_t len) SCM_NORETURN SCM_NOINLINE;
393
394 static void
395 vm_error (const char *msg, SCM arg)
396 {
397 scm_throw (sym_vm_error,
398 scm_list_3 (sym_vm_run, scm_from_latin1_string (msg),
399 SCM_UNBNDP (arg) ? SCM_EOL : scm_list_1 (arg)));
400 abort(); /* not reached */
401 }
402
403 static void
404 vm_error_bad_instruction (scm_t_uint32 inst)
405 {
406 vm_error ("VM: Bad instruction: ~s", scm_from_uint32 (inst));
407 }
408
409 static void
410 vm_error_unbound (SCM proc, SCM sym)
411 {
412 scm_error_scm (scm_misc_error_key, proc,
413 scm_from_latin1_string ("Unbound variable: ~s"),
414 scm_list_1 (sym), SCM_BOOL_F);
415 }
416
417 static void
418 vm_error_unbound_fluid (SCM proc, SCM fluid)
419 {
420 scm_error_scm (scm_misc_error_key, proc,
421 scm_from_latin1_string ("Unbound fluid: ~s"),
422 scm_list_1 (fluid), SCM_BOOL_F);
423 }
424
425 static void
426 vm_error_not_a_variable (const char *func_name, SCM x)
427 {
428 scm_error (scm_arg_type_key, func_name, "Not a variable: ~S",
429 scm_list_1 (x), scm_list_1 (x));
430 }
431
432 static void
433 vm_error_apply_to_non_list (SCM x)
434 {
435 scm_error (scm_arg_type_key, "apply", "Apply to non-list: ~S",
436 scm_list_1 (x), scm_list_1 (x));
437 }
438
439 static void
440 vm_error_kwargs_length_not_even (SCM proc)
441 {
442 scm_error_scm (sym_keyword_argument_error, proc,
443 scm_from_latin1_string ("Odd length of keyword argument list"),
444 SCM_EOL, SCM_BOOL_F);
445 }
446
447 static void
448 vm_error_kwargs_invalid_keyword (SCM proc, SCM obj)
449 {
450 scm_error_scm (sym_keyword_argument_error, proc,
451 scm_from_latin1_string ("Invalid keyword"),
452 SCM_EOL, scm_list_1 (obj));
453 }
454
455 static void
456 vm_error_kwargs_unrecognized_keyword (SCM proc, SCM kw)
457 {
458 scm_error_scm (sym_keyword_argument_error, proc,
459 scm_from_latin1_string ("Unrecognized keyword"),
460 SCM_EOL, scm_list_1 (kw));
461 }
462
463 static void
464 vm_error_too_many_args (int nargs)
465 {
466 vm_error ("VM: Too many arguments", scm_from_int (nargs));
467 }
468
469 static void
470 vm_error_wrong_num_args (SCM proc)
471 {
472 scm_wrong_num_args (proc);
473 }
474
475 static void
476 vm_error_wrong_type_apply (SCM proc)
477 {
478 scm_error (scm_arg_type_key, NULL, "Wrong type to apply: ~S",
479 scm_list_1 (proc), scm_list_1 (proc));
480 }
481
482 static void
483 vm_error_stack_underflow (void)
484 {
485 vm_error ("VM: Stack underflow", SCM_UNDEFINED);
486 }
487
488 static void
489 vm_error_improper_list (SCM x)
490 {
491 vm_error ("Expected a proper list, but got object with tail ~s", x);
492 }
493
494 static void
495 vm_error_not_a_pair (const char *subr, SCM x)
496 {
497 scm_wrong_type_arg_msg (subr, 1, x, "pair");
498 }
499
500 static void
501 vm_error_not_a_bytevector (const char *subr, SCM x)
502 {
503 scm_wrong_type_arg_msg (subr, 1, x, "bytevector");
504 }
505
506 static void
507 vm_error_not_a_struct (const char *subr, SCM x)
508 {
509 scm_wrong_type_arg_msg (subr, 1, x, "struct");
510 }
511
512 static void
513 vm_error_no_values (void)
514 {
515 vm_error ("Zero values returned to single-valued continuation",
516 SCM_UNDEFINED);
517 }
518
519 static void
520 vm_error_not_enough_values (void)
521 {
522 vm_error ("Too few values returned to continuation", SCM_UNDEFINED);
523 }
524
525 static void
526 vm_error_wrong_number_of_values (scm_t_uint32 expected)
527 {
528 vm_error ("Wrong number of values returned to continuation (expected ~a)",
529 scm_from_uint32 (expected));
530 }
531
532 static void
533 vm_error_continuation_not_rewindable (SCM cont)
534 {
535 vm_error ("Unrewindable partial continuation", cont);
536 }
537
538 static void
539 vm_error_bad_wide_string_length (size_t len)
540 {
541 vm_error ("VM: Bad wide string length: ~S", scm_from_size_t (len));
542 }
543
544
545 \f
546
547 static SCM vm_boot_continuation;
548 static SCM vm_builtin_apply;
549 static SCM vm_builtin_values;
550 static SCM vm_builtin_abort_to_prompt;
551 static SCM vm_builtin_call_with_values;
552 static SCM vm_builtin_call_with_current_continuation;
553
554 static const scm_t_uint32 vm_boot_continuation_code[] = {
555 SCM_PACK_OP_24 (halt, 0)
556 };
557
558 static const scm_t_uint32 vm_builtin_apply_code[] = {
559 SCM_PACK_OP_24 (assert_nargs_ge, 3),
560 SCM_PACK_OP_24 (tail_apply, 0), /* proc in r1, args from r2 */
561 };
562
563 static const scm_t_uint32 vm_builtin_values_code[] = {
564 SCM_PACK_OP_24 (return_values, 0) /* vals from r1 */
565 };
566
567 static const scm_t_uint32 vm_builtin_abort_to_prompt_code[] = {
568 SCM_PACK_OP_24 (assert_nargs_ge, 2),
569 SCM_PACK_OP_24 (abort, 0), /* tag in r1, vals from r2 */
570 /* FIXME: Partial continuation should capture caller regs. */
571 SCM_PACK_OP_24 (return_values, 0) /* vals from r1 */
572 };
573
574 static const scm_t_uint32 vm_builtin_call_with_values_code[] = {
575 SCM_PACK_OP_24 (assert_nargs_ee, 3),
576 SCM_PACK_OP_24 (alloc_frame, 7),
577 SCM_PACK_OP_12_12 (mov, 6, 1),
578 SCM_PACK_OP_24 (call, 6), SCM_PACK_OP_ARG_8_24 (0, 1),
579 SCM_PACK_OP_12_12 (mov, 0, 2),
580 SCM_PACK_OP_24 (tail_call_shuffle, 7)
581 };
582
583 static const scm_t_uint32 vm_builtin_call_with_current_continuation_code[] = {
584 SCM_PACK_OP_24 (assert_nargs_ee, 2),
585 SCM_PACK_OP_24 (call_cc, 0)
586 };
587
588
589 static SCM
590 scm_vm_builtin_ref (unsigned idx)
591 {
592 switch (idx)
593 {
594 #define INDEX_TO_NAME(builtin, BUILTIN, req, opt, rest) \
595 case SCM_VM_BUILTIN_##BUILTIN: return vm_builtin_##builtin;
596 FOR_EACH_VM_BUILTIN(INDEX_TO_NAME)
597 #undef INDEX_TO_NAME
598 default: abort();
599 }
600 }
601
602 SCM scm_sym_apply;
603 static SCM scm_sym_values;
604 static SCM scm_sym_abort_to_prompt;
605 static SCM scm_sym_call_with_values;
606 static SCM scm_sym_call_with_current_continuation;
607
608 SCM
609 scm_vm_builtin_name_to_index (SCM name)
610 #define FUNC_NAME "builtin-name->index"
611 {
612 SCM_VALIDATE_SYMBOL (1, name);
613
614 #define NAME_TO_INDEX(builtin, BUILTIN, req, opt, rest) \
615 if (scm_is_eq (name, scm_sym_##builtin)) \
616 return scm_from_uint (SCM_VM_BUILTIN_##BUILTIN);
617 FOR_EACH_VM_BUILTIN(NAME_TO_INDEX)
618 #undef NAME_TO_INDEX
619
620 return SCM_BOOL_F;
621 }
622 #undef FUNC_NAME
623
624 SCM
625 scm_vm_builtin_index_to_name (SCM index)
626 #define FUNC_NAME "builtin-index->name"
627 {
628 unsigned idx;
629
630 SCM_VALIDATE_UINT_COPY (1, index, idx);
631
632 switch (idx)
633 {
634 #define INDEX_TO_NAME(builtin, BUILTIN, req, opt, rest) \
635 case SCM_VM_BUILTIN_##BUILTIN: return scm_sym_##builtin;
636 FOR_EACH_VM_BUILTIN(INDEX_TO_NAME)
637 #undef INDEX_TO_NAME
638 default: return SCM_BOOL_F;
639 }
640 }
641 #undef FUNC_NAME
642
643 static void
644 scm_init_vm_builtins (void)
645 {
646 scm_c_define_gsubr ("builtin-name->index", 1, 0, 0,
647 scm_vm_builtin_name_to_index);
648 scm_c_define_gsubr ("builtin-index->name", 1, 0, 0,
649 scm_vm_builtin_index_to_name);
650 }
651
652 SCM
653 scm_i_call_with_current_continuation (SCM proc)
654 {
655 return scm_call_1 (vm_builtin_call_with_current_continuation, proc);
656 }
657
658 \f
659 /*
660 * VM
661 */
662
663 /* Hard stack limit is 512M words: 2 gigabytes on 32-bit machines, 4 on
664 64-bit machines. */
665 static const size_t hard_max_stack_size = 512 * 1024 * 1024;
666
667 /* Initial stack size: 4 or 8 kB. */
668 static const size_t initial_stack_size = 1024;
669
670 /* Default soft stack limit is 1M words (4 or 8 megabytes). */
671 static size_t default_max_stack_size = 1024 * 1024;
672
673 static void
674 initialize_default_stack_size (void)
675 {
676 int size = scm_getenv_int ("GUILE_STACK_SIZE", (int) default_max_stack_size);
677 if (size >= initial_stack_size && (size_t) size < ((size_t) -1) / sizeof(SCM))
678 default_max_stack_size = size;
679 }
680
681 static void vm_expand_stack (struct scm_vm *vp) SCM_NOINLINE;
682 #define VM_NAME vm_regular_engine
683 #define VM_USE_HOOKS 0
684 #define FUNC_NAME "vm-regular-engine"
685 #include "vm-engine.c"
686 #undef FUNC_NAME
687 #undef VM_USE_HOOKS
688 #undef VM_NAME
689
690 #define VM_NAME vm_debug_engine
691 #define VM_USE_HOOKS 1
692 #define FUNC_NAME "vm-debug-engine"
693 #include "vm-engine.c"
694 #undef FUNC_NAME
695 #undef VM_USE_HOOKS
696 #undef VM_NAME
697
698 typedef SCM (*scm_t_vm_engine) (scm_i_thread *current_thread, struct scm_vm *vp,
699 scm_i_jmp_buf *registers, int resume);
700
701 static const scm_t_vm_engine vm_engines[SCM_VM_NUM_ENGINES] =
702 { vm_regular_engine, vm_debug_engine };
703
704 static SCM*
705 allocate_stack (size_t size)
706 #define FUNC_NAME "make_vm"
707 {
708 void *ret;
709
710 if (size >= ((size_t) -1) / sizeof (SCM))
711 abort ();
712
713 size *= sizeof (SCM);
714
715 #if HAVE_SYS_MMAN_H
716 ret = mmap (NULL, size, PROT_READ | PROT_WRITE,
717 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
718 if (ret == MAP_FAILED)
719 SCM_SYSERROR;
720 #else
721 ret = malloc (size);
722 if (!ret)
723 SCM_SYSERROR;
724 #endif
725
726 return (SCM *) ret;
727 }
728 #undef FUNC_NAME
729
730 static void
731 free_stack (SCM *stack, size_t size)
732 {
733 size *= sizeof (SCM);
734
735 #if HAVE_SYS_MMAN_H
736 munmap (stack, size);
737 #else
738 free (stack);
739 #endif
740 }
741
742 static SCM*
743 expand_stack (SCM *old_stack, size_t old_size, size_t new_size)
744 #define FUNC_NAME "expand_stack"
745 {
746 #if defined MREMAP_MAYMOVE
747 void *new_stack;
748
749 if (new_size >= ((size_t) -1) / sizeof (SCM))
750 abort ();
751
752 old_size *= sizeof (SCM);
753 new_size *= sizeof (SCM);
754
755 new_stack = mremap (old_stack, old_size, new_size, MREMAP_MAYMOVE);
756 if (new_stack == MAP_FAILED)
757 SCM_SYSERROR;
758
759 return (SCM *) new_stack;
760 #else
761 SCM *new_stack;
762
763 new_stack = allocate_stack (new_size);
764 memcpy (new_stack, old_stack, old_size * sizeof (SCM));
765 free_stack (old_stack, old_size);
766
767 return new_stack;
768 #endif
769 }
770 #undef FUNC_NAME
771
772 static struct scm_vm *
773 make_vm (void)
774 #define FUNC_NAME "make_vm"
775 {
776 int i;
777 struct scm_vm *vp;
778
779 vp = scm_gc_malloc (sizeof (struct scm_vm), "vm");
780
781 vp->stack_size = initial_stack_size;
782 vp->stack_base = allocate_stack (vp->stack_size);
783 vp->stack_limit = vp->stack_base + vp->stack_size;
784 vp->max_stack_size = default_max_stack_size;
785 vp->ip = NULL;
786 vp->sp = vp->stack_base - 1;
787 vp->fp = NULL;
788 vp->engine = vm_default_engine;
789 vp->trace_level = 0;
790 for (i = 0; i < SCM_VM_NUM_HOOKS; i++)
791 vp->hooks[i] = SCM_BOOL_F;
792
793 return vp;
794 }
795 #undef FUNC_NAME
796
797 /* Mark the VM stack region between its base and its current top. */
798 struct GC_ms_entry *
799 scm_i_vm_mark_stack (struct scm_vm *vp, struct GC_ms_entry *mark_stack_ptr,
800 struct GC_ms_entry *mark_stack_limit)
801 {
802 SCM *sp, *fp;
803
804 for (fp = vp->fp, sp = vp->sp; fp; fp = SCM_FRAME_DYNAMIC_LINK (fp))
805 {
806 for (; sp >= &SCM_FRAME_LOCAL (fp, 0); sp--)
807 {
808 SCM elt = *sp;
809 if (SCM_NIMP (elt))
810 mark_stack_ptr = GC_MARK_AND_PUSH ((GC_word *) elt,
811 mark_stack_ptr, mark_stack_limit,
812 NULL);
813 }
814 sp = SCM_FRAME_PREVIOUS_SP (fp);
815 }
816
817 return mark_stack_ptr;
818 }
819
820 /* Free the VM stack, as this thread is exiting. */
821 void
822 scm_i_vm_free_stack (struct scm_vm *vp)
823 {
824 free_stack (vp->stack_base, vp->stack_size);
825 vp->stack_base = vp->stack_limit = NULL;
826 vp->stack_size = 0;
827 }
828
829 static void
830 vm_expand_stack (struct scm_vm *vp)
831 {
832 scm_t_ptrdiff stack_size = vp->sp + 1 - vp->stack_base;
833
834 if (stack_size > hard_max_stack_size)
835 {
836 /* We have expanded the soft limit to the point that we reached a
837 hard limit. There is nothing sensible to do. */
838 fprintf (stderr, "Hard stack size limit (%zu words) reached; aborting.\n",
839 hard_max_stack_size);
840 abort ();
841 }
842
843 if (stack_size > vp->stack_size)
844 {
845 SCM *old_stack;
846 size_t new_size;
847 scm_t_ptrdiff reloc;
848
849 new_size = vp->stack_size;
850 while (new_size < stack_size)
851 new_size *= 2;
852 old_stack = vp->stack_base;
853 vp->stack_base = expand_stack (old_stack, vp->stack_size, new_size);
854 vp->stack_size = new_size;
855 vp->stack_limit = vp->stack_base + new_size;
856 reloc = vp->stack_base - old_stack;
857
858 if (reloc)
859 {
860 SCM *fp;
861 vp->fp += reloc;
862 vp->sp += reloc;
863 fp = vp->fp;
864 while (fp)
865 {
866 SCM *next_fp = SCM_FRAME_DYNAMIC_LINK (fp);
867 if (next_fp)
868 {
869 next_fp += reloc;
870 SCM_FRAME_SET_DYNAMIC_LINK (fp, next_fp);
871 }
872 fp = next_fp;
873 }
874 }
875 }
876
877 if (stack_size >= vp->max_stack_size)
878 {
879 /* Expand the soft limit by 256K entries to give us space to
880 handle the error. */
881 vp->max_stack_size += 256 * 1024;
882
883 /* If it's still not big enough... it's quite improbable, but go
884 ahead and set to the full available stack size. */
885 if (vp->max_stack_size < stack_size)
886 vp->max_stack_size = vp->stack_size;
887
888 /* But don't exceed the hard maximum. */
889 if (vp->max_stack_size > hard_max_stack_size)
890 vp->max_stack_size = hard_max_stack_size;
891
892 /* Finally, reset the limit, to catch further overflows. */
893 vp->stack_limit = vp->stack_base + vp->max_stack_size;
894
895 vm_error ("VM: Stack overflow", SCM_UNDEFINED);
896 }
897
898 /* Otherwise continue, with the new enlarged stack. */
899 }
900
901 static struct scm_vm *
902 thread_vm (scm_i_thread *t)
903 {
904 if (SCM_UNLIKELY (!t->vp))
905 t->vp = make_vm ();
906
907 return t->vp;
908 }
909
910 struct scm_vm *
911 scm_the_vm (void)
912 {
913 return thread_vm (SCM_I_CURRENT_THREAD);
914 }
915
916 SCM
917 scm_call_n (SCM proc, SCM *argv, size_t nargs)
918 {
919 scm_i_thread *thread;
920 struct scm_vm *vp;
921 SCM *base;
922 ptrdiff_t base_frame_size;
923 /* Cached variables. */
924 scm_i_jmp_buf registers; /* used for prompts */
925 size_t i;
926
927 thread = SCM_I_CURRENT_THREAD;
928 vp = thread_vm (thread);
929
930 SCM_CHECK_STACK;
931
932 /* Check that we have enough space: 3 words for the boot
933 continuation, 3 + nargs for the procedure application, and 3 for
934 setting up a new frame. */
935 base_frame_size = 3 + 3 + nargs + 3;
936 vp->sp += base_frame_size;
937 if (vp->sp >= vp->stack_limit)
938 vm_expand_stack (vp);
939 base = vp->sp + 1 - base_frame_size;
940
941 /* Since it's possible to receive the arguments on the stack itself,
942 shuffle up the arguments first. */
943 for (i = nargs; i > 0; i--)
944 base[6 + i - 1] = argv[i - 1];
945
946 /* Push the boot continuation, which calls PROC and returns its
947 result(s). */
948 base[0] = SCM_PACK (vp->fp); /* dynamic link */
949 base[1] = SCM_PACK (vp->ip); /* ra */
950 base[2] = vm_boot_continuation;
951 vp->fp = &base[2];
952 vp->ip = (scm_t_uint32 *) vm_boot_continuation_code;
953
954 /* The pending call to PROC. */
955 base[3] = SCM_PACK (vp->fp); /* dynamic link */
956 base[4] = SCM_PACK (vp->ip); /* ra */
957 base[5] = proc;
958 vp->fp = &base[5];
959 vp->sp = &SCM_FRAME_LOCAL (vp->fp, nargs);
960
961 {
962 int resume = SCM_I_SETJMP (registers);
963
964 if (SCM_UNLIKELY (resume))
965 /* Non-local return. */
966 vm_dispatch_abort_hook (vp);
967
968 return vm_engines[vp->engine](thread, vp, &registers, resume);
969 }
970 }
971
972 /* Scheme interface */
973
974 #define VM_DEFINE_HOOK(n) \
975 { \
976 struct scm_vm *vp; \
977 vp = scm_the_vm (); \
978 if (scm_is_false (vp->hooks[n])) \
979 vp->hooks[n] = scm_make_hook (SCM_I_MAKINUM (1)); \
980 return vp->hooks[n]; \
981 }
982
983 SCM_DEFINE (scm_vm_apply_hook, "vm-apply-hook", 0, 0, 0,
984 (void),
985 "")
986 #define FUNC_NAME s_scm_vm_apply_hook
987 {
988 VM_DEFINE_HOOK (SCM_VM_APPLY_HOOK);
989 }
990 #undef FUNC_NAME
991
992 SCM_DEFINE (scm_vm_push_continuation_hook, "vm-push-continuation-hook", 0, 0, 0,
993 (void),
994 "")
995 #define FUNC_NAME s_scm_vm_push_continuation_hook
996 {
997 VM_DEFINE_HOOK (SCM_VM_PUSH_CONTINUATION_HOOK);
998 }
999 #undef FUNC_NAME
1000
1001 SCM_DEFINE (scm_vm_pop_continuation_hook, "vm-pop-continuation-hook", 0, 0, 0,
1002 (void),
1003 "")
1004 #define FUNC_NAME s_scm_vm_pop_continuation_hook
1005 {
1006 VM_DEFINE_HOOK (SCM_VM_POP_CONTINUATION_HOOK);
1007 }
1008 #undef FUNC_NAME
1009
1010 SCM_DEFINE (scm_vm_next_hook, "vm-next-hook", 0, 0, 0,
1011 (void),
1012 "")
1013 #define FUNC_NAME s_scm_vm_next_hook
1014 {
1015 VM_DEFINE_HOOK (SCM_VM_NEXT_HOOK);
1016 }
1017 #undef FUNC_NAME
1018
1019 SCM_DEFINE (scm_vm_abort_continuation_hook, "vm-abort-continuation-hook", 0, 0, 0,
1020 (void),
1021 "")
1022 #define FUNC_NAME s_scm_vm_abort_continuation_hook
1023 {
1024 VM_DEFINE_HOOK (SCM_VM_ABORT_CONTINUATION_HOOK);
1025 }
1026 #undef FUNC_NAME
1027
1028 SCM_DEFINE (scm_vm_restore_continuation_hook, "vm-restore-continuation-hook", 0, 0, 0,
1029 (void),
1030 "")
1031 #define FUNC_NAME s_scm_vm_restore_continuation_hook
1032 {
1033 VM_DEFINE_HOOK (SCM_VM_RESTORE_CONTINUATION_HOOK);
1034 }
1035 #undef FUNC_NAME
1036
1037 SCM_DEFINE (scm_vm_trace_level, "vm-trace-level", 0, 0, 0,
1038 (void),
1039 "")
1040 #define FUNC_NAME s_scm_vm_trace_level
1041 {
1042 return scm_from_int (scm_the_vm ()->trace_level);
1043 }
1044 #undef FUNC_NAME
1045
1046 SCM_DEFINE (scm_set_vm_trace_level_x, "set-vm-trace-level!", 1, 0, 0,
1047 (SCM level),
1048 "")
1049 #define FUNC_NAME s_scm_set_vm_trace_level_x
1050 {
1051 scm_the_vm ()->trace_level = scm_to_int (level);
1052 return SCM_UNSPECIFIED;
1053 }
1054 #undef FUNC_NAME
1055
1056 \f
1057 /*
1058 * VM engines
1059 */
1060
1061 static int
1062 symbol_to_vm_engine (SCM engine, const char *FUNC_NAME)
1063 {
1064 if (scm_is_eq (engine, sym_regular))
1065 return SCM_VM_REGULAR_ENGINE;
1066 else if (scm_is_eq (engine, sym_debug))
1067 return SCM_VM_DEBUG_ENGINE;
1068 else
1069 SCM_MISC_ERROR ("Unknown VM engine: ~a", scm_list_1 (engine));
1070 }
1071
1072 static SCM
1073 vm_engine_to_symbol (int engine, const char *FUNC_NAME)
1074 {
1075 switch (engine)
1076 {
1077 case SCM_VM_REGULAR_ENGINE:
1078 return sym_regular;
1079 case SCM_VM_DEBUG_ENGINE:
1080 return sym_debug;
1081 default:
1082 /* ? */
1083 SCM_MISC_ERROR ("Unknown VM engine: ~a",
1084 scm_list_1 (scm_from_int (engine)));
1085 }
1086 }
1087
1088 SCM_DEFINE (scm_vm_engine, "vm-engine", 0, 0, 0,
1089 (void),
1090 "")
1091 #define FUNC_NAME s_scm_vm_engine
1092 {
1093 return vm_engine_to_symbol (scm_the_vm ()->engine, FUNC_NAME);
1094 }
1095 #undef FUNC_NAME
1096
1097 void
1098 scm_c_set_vm_engine_x (int engine)
1099 #define FUNC_NAME "set-vm-engine!"
1100 {
1101 if (engine < 0 || engine >= SCM_VM_NUM_ENGINES)
1102 SCM_MISC_ERROR ("Unknown VM engine: ~a",
1103 scm_list_1 (scm_from_int (engine)));
1104
1105 scm_the_vm ()->engine = engine;
1106 }
1107 #undef FUNC_NAME
1108
1109 SCM_DEFINE (scm_set_vm_engine_x, "set-vm-engine!", 1, 0, 0,
1110 (SCM engine),
1111 "")
1112 #define FUNC_NAME s_scm_set_vm_engine_x
1113 {
1114 scm_c_set_vm_engine_x (symbol_to_vm_engine (engine, FUNC_NAME));
1115 return SCM_UNSPECIFIED;
1116 }
1117 #undef FUNC_NAME
1118
1119 void
1120 scm_c_set_default_vm_engine_x (int engine)
1121 #define FUNC_NAME "set-default-vm-engine!"
1122 {
1123 if (engine < 0 || engine >= SCM_VM_NUM_ENGINES)
1124 SCM_MISC_ERROR ("Unknown VM engine: ~a",
1125 scm_list_1 (scm_from_int (engine)));
1126
1127 vm_default_engine = engine;
1128 }
1129 #undef FUNC_NAME
1130
1131 SCM_DEFINE (scm_set_default_vm_engine_x, "set-default-vm-engine!", 1, 0, 0,
1132 (SCM engine),
1133 "")
1134 #define FUNC_NAME s_scm_set_default_vm_engine_x
1135 {
1136 scm_c_set_default_vm_engine_x (symbol_to_vm_engine (engine, FUNC_NAME));
1137 return SCM_UNSPECIFIED;
1138 }
1139 #undef FUNC_NAME
1140
1141 /* FIXME: This function makes no sense, but we keep it to make sure we
1142 have a way of switching to the debug or regular VM. */
1143 SCM_DEFINE (scm_call_with_vm, "call-with-vm", 1, 0, 1,
1144 (SCM proc, SCM args),
1145 "Apply @var{proc} to @var{args} in a dynamic extent in which\n"
1146 "@var{vm} is the current VM.")
1147 #define FUNC_NAME s_scm_call_with_vm
1148 {
1149 return scm_apply_0 (proc, args);
1150 }
1151 #undef FUNC_NAME
1152
1153 \f
1154 /*
1155 * Initialize
1156 */
1157
1158 SCM
1159 scm_load_compiled_with_vm (SCM file)
1160 {
1161 return scm_call_0 (scm_load_thunk_from_file (file));
1162 }
1163
1164
1165 void
1166 scm_init_vm_builtin_properties (void)
1167 {
1168 /* FIXME: Seems hacky to do this here, but oh well :/ */
1169 scm_sym_apply = scm_from_utf8_symbol ("apply");
1170 scm_sym_values = scm_from_utf8_symbol ("values");
1171 scm_sym_abort_to_prompt = scm_from_utf8_symbol ("abort-to-prompt");
1172 scm_sym_call_with_values = scm_from_utf8_symbol ("call-with-values");
1173 scm_sym_call_with_current_continuation =
1174 scm_from_utf8_symbol ("call-with-current-continuation");
1175
1176 #define INIT_BUILTIN(builtin, BUILTIN, req, opt, rest) \
1177 scm_set_procedure_property_x (vm_builtin_##builtin, scm_sym_name, \
1178 scm_sym_##builtin); \
1179 scm_set_procedure_minimum_arity_x (vm_builtin_##builtin, \
1180 SCM_I_MAKINUM (req), \
1181 SCM_I_MAKINUM (opt), \
1182 scm_from_bool (rest));
1183 FOR_EACH_VM_BUILTIN (INIT_BUILTIN);
1184 #undef INIT_BUILTIN
1185 }
1186
1187 void
1188 scm_bootstrap_vm (void)
1189 {
1190 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
1191 "scm_init_vm",
1192 (scm_t_extension_init_func)scm_init_vm, NULL);
1193 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
1194 "scm_init_vm_builtins",
1195 (scm_t_extension_init_func)scm_init_vm_builtins,
1196 NULL);
1197
1198 initialize_default_stack_size ();
1199
1200 sym_vm_run = scm_from_latin1_symbol ("vm-run");
1201 sym_vm_error = scm_from_latin1_symbol ("vm-error");
1202 sym_keyword_argument_error = scm_from_latin1_symbol ("keyword-argument-error");
1203 sym_regular = scm_from_latin1_symbol ("regular");
1204 sym_debug = scm_from_latin1_symbol ("debug");
1205
1206 vm_boot_continuation = scm_i_make_program (vm_boot_continuation_code);
1207 SCM_SET_CELL_WORD_0 (vm_boot_continuation,
1208 (SCM_CELL_WORD_0 (vm_boot_continuation)
1209 | SCM_F_PROGRAM_IS_BOOT));
1210
1211 #define DEFINE_BUILTIN(builtin, BUILTIN, req, opt, rest) \
1212 vm_builtin_##builtin = scm_i_make_program (vm_builtin_##builtin##_code);
1213 FOR_EACH_VM_BUILTIN (DEFINE_BUILTIN);
1214 #undef DEFINE_BUILTIN
1215 }
1216
1217 void
1218 scm_init_vm (void)
1219 {
1220 #ifndef SCM_MAGIC_SNARFER
1221 #include "libguile/vm.x"
1222 #endif
1223 }
1224
1225 /*
1226 Local Variables:
1227 c-file-style: "gnu"
1228 End:
1229 */