Commit | Line | Data |
---|---|---|
a6029b97 | 1 | /* Copyright (C) 2001, 2009, 2010 Free Software Foundation, Inc. |
a98cef7e | 2 | * |
560b9c25 | 3 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
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. | |
a98cef7e | 7 | * |
53befeb7 NJ |
8 | * This library is distributed in the hope that it will be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
560b9c25 AW |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | * Lesser General Public License for more details. | |
a98cef7e | 12 | * |
560b9c25 AW |
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 | |
53befeb7 NJ |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
16 | * 02110-1301 USA | |
560b9c25 | 17 | */ |
a98cef7e | 18 | |
13c47753 AW |
19 | #if HAVE_CONFIG_H |
20 | # include <config.h> | |
21 | #endif | |
22 | ||
da8b4747 | 23 | #include <stdlib.h> |
6d14383e | 24 | #include <alloca.h> |
daccfef4 | 25 | #include <alignof.h> |
17e90c5e | 26 | #include <string.h> |
e3eb628d | 27 | |
1c44468d | 28 | #include "libguile/bdw-gc.h" |
e3eb628d LC |
29 | #include <gc/gc_mark.h> |
30 | ||
560b9c25 | 31 | #include "_scm.h" |
adaf86ec | 32 | #include "control.h" |
ac99cb0c | 33 | #include "frames.h" |
17e90c5e | 34 | #include "instructions.h" |
8f5cfc81 | 35 | #include "objcodes.h" |
ac99cb0c | 36 | #include "programs.h" |
a98cef7e KN |
37 | #include "vm.h" |
38 | ||
a98cef7e KN |
39 | /* I sometimes use this for debugging. */ |
40 | #define vm_puts(OBJ) \ | |
41 | { \ | |
22bcbe8c AW |
42 | scm_display (OBJ, scm_current_error_port ()); \ |
43 | scm_newline (scm_current_error_port ()); \ | |
a98cef7e KN |
44 | } |
45 | ||
11ea1aba AW |
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 | |
49 | ||
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 | |
616167fc | 53 | will ensure that assertions are enabled. Slows down the VM by about 30%. */ |
747a1635 | 54 | /* NB! If you enable this, search for NULLING in throw.c */ |
616167fc | 55 | /* #define VM_ENABLE_STACK_NULLING */ |
11ea1aba | 56 | |
53e28ed9 AW |
57 | /* #define VM_ENABLE_PARANOID_ASSERTIONS */ |
58 | ||
11ea1aba AW |
59 | #if defined (VM_ENABLE_STACK_NULLING) && !defined (VM_ENABLE_ASSERTIONS) |
60 | #define VM_ENABLE_ASSERTIONS | |
61 | #endif | |
62 | ||
e3eb628d LC |
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 | |
66 | for a discussion. */ | |
67 | #define VM_ENABLE_PRECISE_STACK_GC_SCAN | |
68 | ||
f1046e6b LC |
69 | /* Size in SCM objects of the stack reserve. The reserve is used to run |
70 | exception handling code in case of a VM stack overflow. */ | |
71 | #define VM_STACK_RESERVE_SIZE 512 | |
72 | ||
e3eb628d | 73 | |
a98cef7e | 74 | \f |
a98cef7e KN |
75 | /* |
76 | * VM Continuation | |
77 | */ | |
78 | ||
6f3b0cc2 AW |
79 | void |
80 | scm_i_vm_cont_print (SCM x, SCM port, scm_print_state *pstate) | |
81 | { | |
82 | scm_puts ("#<vm-continuation ", port); | |
83 | scm_uintprint (SCM_UNPACK (x), 16, port); | |
84 | scm_puts (">", port); | |
85 | } | |
17e90c5e | 86 | |
d8873dfe AW |
87 | /* In theory, a number of vm instances can be active in the call trace, and we |
88 | only want to reify the continuations of those in the current continuation | |
89 | root. I don't see a nice way to do this -- ideally it would involve dynwinds, | |
90 | and previous values of the *the-vm* fluid within the current continuation | |
91 | root. But we don't have access to continuation roots in the dynwind stack. | |
92 | So, just punt for now, we just capture the continuation for the current VM. | |
93 | ||
94 | While I'm on the topic, ideally we could avoid copying the C stack if the | |
95 | continuation root is inside VM code, and call/cc was invoked within that same | |
96 | call to vm_run; but that's currently not implemented. | |
97 | */ | |
cee1d22c AW |
98 | SCM |
99 | scm_i_vm_capture_stack (SCM *stack_base, SCM *fp, SCM *sp, scm_t_uint8 *ra, | |
100 | scm_t_uint8 *mvra, scm_t_uint32 flags) | |
a98cef7e | 101 | { |
d8873dfe AW |
102 | struct scm_vm_cont *p; |
103 | ||
104 | p = scm_gc_malloc (sizeof (*p), "capture_vm_cont"); | |
105 | p->stack_size = sp - stack_base + 1; | |
d8eeb67c LC |
106 | p->stack_base = scm_gc_malloc (p->stack_size * sizeof (SCM), |
107 | "capture_vm_cont"); | |
d8873dfe AW |
108 | #if defined(VM_ENABLE_STACK_NULLING) && 0 |
109 | /* Tail continuations leave their frame on the stack for subsequent | |
110 | application, but don't capture the frame -- so there are some elements on | |
111 | the stack then, and this check doesn't work, so disable it for now. */ | |
112 | if (sp >= vp->stack_base) | |
66db076a AW |
113 | if (!vp->sp[0] || vp->sp[1]) |
114 | abort (); | |
11ea1aba AW |
115 | memset (p->stack_base, 0, p->stack_size * sizeof (SCM)); |
116 | #endif | |
d8873dfe AW |
117 | p->ra = ra; |
118 | p->mvra = mvra; | |
119 | p->sp = sp; | |
120 | p->fp = fp; | |
121 | memcpy (p->stack_base, stack_base, (sp + 1 - stack_base) * sizeof (SCM)); | |
122 | p->reloc = p->stack_base - stack_base; | |
cee1d22c | 123 | p->flags = flags; |
6f3b0cc2 | 124 | return scm_cell (scm_tc7_vm_cont, (scm_t_bits)p); |
a98cef7e KN |
125 | } |
126 | ||
127 | static void | |
d8873dfe | 128 | vm_return_to_continuation (SCM vm, SCM cont, size_t n, SCM *argv) |
a98cef7e | 129 | { |
d8873dfe AW |
130 | struct scm_vm *vp; |
131 | struct scm_vm_cont *cp; | |
132 | SCM *argv_copy; | |
133 | ||
134 | argv_copy = alloca (n * sizeof(SCM)); | |
135 | memcpy (argv_copy, argv, n * sizeof(SCM)); | |
136 | ||
137 | vp = SCM_VM_DATA (vm); | |
138 | cp = SCM_VM_CONT_DATA (cont); | |
139 | ||
140 | if (n == 0 && !cp->mvra) | |
141 | scm_misc_error (NULL, "Too few values returned to continuation", | |
142 | SCM_EOL); | |
143 | ||
144 | if (vp->stack_size < cp->stack_size + n + 1) | |
29366989 AW |
145 | scm_misc_error ("vm-engine", "not enough space to reinstate continuation", |
146 | scm_list_2 (vm, cont)); | |
147 | ||
11ea1aba AW |
148 | #ifdef VM_ENABLE_STACK_NULLING |
149 | { | |
d8873dfe | 150 | scm_t_ptrdiff nzero = (vp->sp - cp->sp); |
11ea1aba | 151 | if (nzero > 0) |
d8873dfe | 152 | memset (vp->stack_base + cp->stack_size, 0, nzero * sizeof (SCM)); |
66db076a AW |
153 | /* actually nzero should always be negative, because vm_reset_stack will |
154 | unwind the stack to some point *below* this continuation */ | |
11ea1aba AW |
155 | } |
156 | #endif | |
d8873dfe AW |
157 | vp->sp = cp->sp; |
158 | vp->fp = cp->fp; | |
159 | memcpy (vp->stack_base, cp->stack_base, cp->stack_size * sizeof (SCM)); | |
bfffd258 | 160 | |
d8873dfe AW |
161 | if (n == 1 || !cp->mvra) |
162 | { | |
163 | vp->ip = cp->ra; | |
164 | vp->sp++; | |
165 | *vp->sp = argv_copy[0]; | |
166 | } | |
167 | else | |
168 | { | |
169 | size_t i; | |
170 | for (i = 0; i < n; i++) | |
171 | { | |
172 | vp->sp++; | |
173 | *vp->sp = argv_copy[i]; | |
174 | } | |
175 | vp->sp++; | |
176 | *vp->sp = scm_from_size_t (n); | |
177 | vp->ip = cp->mvra; | |
178 | } | |
179 | } | |
bfffd258 | 180 | |
bfffd258 | 181 | SCM |
269479e3 | 182 | scm_i_vm_capture_continuation (SCM vm) |
bfffd258 | 183 | { |
d8873dfe | 184 | struct scm_vm *vp = SCM_VM_DATA (vm); |
cee1d22c | 185 | return scm_i_vm_capture_stack (vp->stack_base, vp->fp, vp->sp, vp->ip, NULL, 0); |
a98cef7e KN |
186 | } |
187 | ||
b1b942b7 | 188 | static void |
7656f194 | 189 | vm_dispatch_hook (SCM vm, int hook_num) |
b1b942b7 | 190 | { |
7656f194 AW |
191 | struct scm_vm *vp; |
192 | SCM hook; | |
b3567435 | 193 | struct scm_frame c_frame; |
405a79ca | 194 | scm_t_aligned_cell frame; |
b3567435 | 195 | SCM args[1]; |
893fb8d0 | 196 | int saved_trace_level; |
b1b942b7 | 197 | |
7656f194 AW |
198 | vp = SCM_VM_DATA (vm); |
199 | hook = vp->hooks[hook_num]; | |
b1b942b7 | 200 | |
7656f194 AW |
201 | if (SCM_LIKELY (scm_is_false (hook)) |
202 | || scm_is_null (SCM_HOOK_PROCEDURES (hook))) | |
203 | return; | |
b3567435 | 204 | |
893fb8d0 AW |
205 | saved_trace_level = vp->trace_level; |
206 | vp->trace_level = 0; | |
b3567435 LC |
207 | |
208 | /* Allocate a frame object on the stack. This is more efficient than calling | |
209 | `scm_c_make_frame ()' to allocate on the heap, but it forces hooks to not | |
210 | capture frame objects. | |
211 | ||
212 | At the same time, procedures such as `frame-procedure' make sense only | |
213 | while the stack frame represented by the frame object is visible, so it | |
214 | seems reasonable to limit the lifetime of frame objects. */ | |
215 | ||
216 | c_frame.stack_holder = vm; | |
217 | c_frame.fp = vp->fp; | |
218 | c_frame.sp = vp->sp; | |
219 | c_frame.ip = vp->ip; | |
220 | c_frame.offset = 0; | |
b79ba0b0 LC |
221 | frame.cell.word_0 = SCM_PACK (scm_tc7_frame); |
222 | frame.cell.word_1 = PTR2SCM (&c_frame); | |
b3567435 LC |
223 | args[0] = PTR2SCM (&frame); |
224 | ||
225 | scm_c_run_hookn (hook, args, 1); | |
226 | ||
893fb8d0 | 227 | vp->trace_level = saved_trace_level; |
b1b942b7 AW |
228 | } |
229 | ||
cee1d22c | 230 | static void vm_abort (SCM vm, size_t n, scm_t_int64 cookie) SCM_NORETURN; |
4f66bcde | 231 | static void |
cee1d22c | 232 | vm_abort (SCM vm, size_t n, scm_t_int64 vm_cookie) |
4f66bcde | 233 | { |
eaefabee | 234 | size_t i; |
2d026f04 AW |
235 | ssize_t tail_len; |
236 | SCM tag, tail, *argv; | |
eaefabee | 237 | |
2d026f04 AW |
238 | /* FIXME: VM_ENABLE_STACK_NULLING */ |
239 | tail = *(SCM_VM_DATA (vm)->sp--); | |
240 | /* NULLSTACK (1) */ | |
241 | tail_len = scm_ilength (tail); | |
242 | if (tail_len < 0) | |
29366989 AW |
243 | scm_misc_error ("vm-engine", "tail values to abort should be a list", |
244 | scm_list_1 (tail)); | |
245 | ||
eaefabee | 246 | tag = SCM_VM_DATA (vm)->sp[-n]; |
2d026f04 | 247 | argv = alloca ((n + tail_len) * sizeof (SCM)); |
eaefabee AW |
248 | for (i = 0; i < n; i++) |
249 | argv[i] = SCM_VM_DATA (vm)->sp[-(n-1-i)]; | |
2d026f04 AW |
250 | for (; i < n + tail_len; i++, tail = scm_cdr (tail)) |
251 | argv[i] = scm_car (tail); | |
252 | /* NULLSTACK (n + 1) */ | |
eaefabee AW |
253 | SCM_VM_DATA (vm)->sp -= n + 1; |
254 | ||
cee1d22c AW |
255 | scm_c_abort (vm, tag, n + tail_len, argv, vm_cookie); |
256 | } | |
257 | ||
258 | static void | |
07801437 | 259 | vm_reinstate_partial_continuation (SCM vm, SCM cont, SCM intwinds, |
adbdfd6d | 260 | size_t n, SCM *argv, scm_t_int64 vm_cookie) |
cee1d22c | 261 | { |
07801437 AW |
262 | struct scm_vm *vp; |
263 | struct scm_vm_cont *cp; | |
264 | SCM *argv_copy, *base; | |
265 | size_t i; | |
266 | ||
267 | argv_copy = alloca (n * sizeof(SCM)); | |
268 | memcpy (argv_copy, argv, n * sizeof(SCM)); | |
269 | ||
270 | vp = SCM_VM_DATA (vm); | |
271 | cp = SCM_VM_CONT_DATA (cont); | |
272 | base = SCM_FRAME_UPPER_ADDRESS (vp->fp) + 1; | |
273 | ||
274 | #define RELOC(scm_p) (scm_p + cp->reloc + (base - cp->stack_base)) | |
275 | ||
276 | if ((base - vp->stack_base) + cp->stack_size + n + 1 > vp->stack_size) | |
29366989 AW |
277 | scm_misc_error ("vm-engine", |
278 | "not enough space to instate partial continuation", | |
279 | scm_list_2 (vm, cont)); | |
07801437 AW |
280 | |
281 | memcpy (base, cp->stack_base, cp->stack_size * sizeof (SCM)); | |
282 | ||
283 | /* now relocate frame pointers */ | |
284 | { | |
285 | SCM *fp; | |
286 | for (fp = RELOC (cp->fp); | |
287 | SCM_FRAME_LOWER_ADDRESS (fp) > base; | |
288 | fp = SCM_FRAME_DYNAMIC_LINK (fp)) | |
289 | SCM_FRAME_SET_DYNAMIC_LINK (fp, RELOC (SCM_FRAME_DYNAMIC_LINK (fp))); | |
290 | } | |
291 | ||
292 | vp->sp = base - 1 + cp->stack_size; | |
293 | vp->fp = RELOC (cp->fp); | |
294 | vp->ip = cp->mvra; | |
295 | ||
07801437 AW |
296 | /* now push args. ip is in a MV context. */ |
297 | for (i = 0; i < n; i++) | |
298 | { | |
299 | vp->sp++; | |
300 | *vp->sp = argv_copy[i]; | |
301 | } | |
302 | vp->sp++; | |
303 | *vp->sp = scm_from_size_t (n); | |
9a1c6f1f | 304 | |
adbdfd6d AW |
305 | /* Finally, rewind the dynamic state. |
306 | ||
307 | We have to treat prompts specially, because we could be rewinding the | |
308 | dynamic state from a different thread, or just a different position on the | |
309 | C and/or VM stack -- so we need to reset the jump buffers so that an abort | |
310 | comes back here, with appropriately adjusted sp and fp registers. */ | |
9a1c6f1f AW |
311 | { |
312 | long delta = 0; | |
313 | SCM newwinds = scm_i_dynwinds (); | |
314 | for (; scm_is_pair (intwinds); intwinds = scm_cdr (intwinds), delta--) | |
adbdfd6d AW |
315 | { |
316 | SCM x = scm_car (intwinds); | |
317 | if (SCM_PROMPT_P (x)) | |
318 | /* the jmpbuf will be reset by our caller */ | |
319 | x = scm_c_make_prompt (SCM_PROMPT_TAG (x), | |
320 | RELOC (SCM_PROMPT_REGISTERS (x)->fp), | |
321 | RELOC (SCM_PROMPT_REGISTERS (x)->sp), | |
322 | SCM_PROMPT_REGISTERS (x)->ip, | |
323 | SCM_PROMPT_ESCAPE_P (x), | |
324 | vm_cookie, | |
325 | newwinds); | |
326 | newwinds = scm_cons (x, newwinds); | |
327 | } | |
9a1c6f1f AW |
328 | scm_dowinds (newwinds, delta); |
329 | } | |
adbdfd6d | 330 | #undef RELOC |
4f66bcde AW |
331 | } |
332 | ||
333 | \f | |
17e90c5e KN |
334 | /* |
335 | * VM Internal functions | |
336 | */ | |
337 | ||
0404c97d AW |
338 | /* Unfortunately we can't snarf these: snarfed things are only loaded up from |
339 | (system vm vm), which might not be loaded before an error happens. */ | |
340 | static SCM sym_vm_run, sym_vm_error, sym_keyword_argument_error, sym_debug; | |
17e90c5e | 341 | |
6f3b0cc2 AW |
342 | void |
343 | scm_i_vm_print (SCM x, SCM port, scm_print_state *pstate) | |
344 | { | |
0a935b2a LC |
345 | const struct scm_vm *vm; |
346 | ||
347 | vm = SCM_VM_DATA (x); | |
348 | ||
6f3b0cc2 | 349 | scm_puts ("#<vm ", port); |
0a935b2a LC |
350 | switch (vm->engine) |
351 | { | |
352 | case SCM_VM_REGULAR_ENGINE: | |
353 | scm_puts ("regular-engine ", port); | |
354 | break; | |
355 | ||
356 | case SCM_VM_DEBUG_ENGINE: | |
357 | scm_puts ("debug-engine ", port); | |
358 | break; | |
359 | ||
360 | default: | |
361 | scm_puts ("unknown-engine ", port); | |
362 | } | |
6f3b0cc2 AW |
363 | scm_uintprint (SCM_UNPACK (x), 16, port); |
364 | scm_puts (">", port); | |
365 | } | |
366 | ||
2fda0242 | 367 | static SCM |
d2d7acd5 | 368 | really_make_boot_program (long nargs) |
2fda0242 | 369 | { |
5bd047ce | 370 | SCM u8vec; |
97fcf583 AW |
371 | scm_t_uint8 text[] = { scm_op_mv_call, 0, 0, 0, 1, |
372 | scm_op_make_int8_1, scm_op_halt }; | |
28b119ee | 373 | struct scm_objcode *bp; |
3b9e095b | 374 | SCM ret; |
5bd047ce | 375 | |
53e28ed9 | 376 | if (SCM_UNLIKELY (nargs > 255 || nargs < 0)) |
29366989 AW |
377 | scm_misc_error ("vm-engine", "too many args when making boot procedure", |
378 | scm_list_1 (scm_from_long (nargs))); | |
379 | ||
28b119ee AW |
380 | text[1] = (scm_t_uint8)nargs; |
381 | ||
d7e7a02a | 382 | bp = scm_malloc (sizeof (struct scm_objcode) + sizeof (text)); |
3dbbe28d | 383 | memcpy (SCM_C_OBJCODE_BASE (bp), text, sizeof (text)); |
28b119ee AW |
384 | bp->len = sizeof(text); |
385 | bp->metalen = 0; | |
28b119ee | 386 | |
7055591c AW |
387 | u8vec = scm_c_take_bytevector ((scm_t_int8*)bp, |
388 | sizeof (struct scm_objcode) + sizeof (text)); | |
5bd047ce | 389 | ret = scm_make_program (scm_bytecode_to_objcode (u8vec), |
20d47c39 | 390 | SCM_BOOL_F, SCM_BOOL_F); |
ba20f78a | 391 | SCM_SET_CELL_WORD_0 (ret, SCM_CELL_WORD_0 (ret) | SCM_F_PROGRAM_IS_BOOT); |
5bd047ce | 392 | |
3b9e095b | 393 | return ret; |
2fda0242 | 394 | } |
d2d7acd5 AW |
395 | #define NUM_BOOT_PROGS 8 |
396 | static SCM | |
397 | vm_make_boot_program (long nargs) | |
398 | { | |
399 | static SCM programs[NUM_BOOT_PROGS] = { 0, }; | |
400 | ||
401 | if (SCM_UNLIKELY (!programs[0])) | |
402 | { | |
403 | int i; | |
404 | for (i = 0; i < NUM_BOOT_PROGS; i++) | |
f39448c5 | 405 | programs[i] = really_make_boot_program (i); |
d2d7acd5 AW |
406 | } |
407 | ||
408 | if (SCM_LIKELY (nargs < NUM_BOOT_PROGS)) | |
409 | return programs[nargs]; | |
410 | else | |
411 | return really_make_boot_program (nargs); | |
412 | } | |
2fda0242 | 413 | |
a98cef7e KN |
414 | \f |
415 | /* | |
416 | * VM | |
417 | */ | |
418 | ||
b7393ea1 AW |
419 | static SCM |
420 | resolve_variable (SCM what, SCM program_module) | |
421 | { | |
9bd48cb1 | 422 | if (SCM_LIKELY (scm_is_symbol (what))) |
b7393ea1 AW |
423 | { |
424 | if (SCM_LIKELY (scm_module_system_booted_p | |
425 | && scm_is_true (program_module))) | |
426 | /* might longjmp */ | |
427 | return scm_module_lookup (program_module, what); | |
428 | else | |
429 | { | |
430 | SCM v = scm_sym2var (what, SCM_BOOL_F, SCM_BOOL_F); | |
431 | if (scm_is_false (v)) | |
432 | scm_misc_error (NULL, "unbound variable: ~S", scm_list_1 (what)); | |
433 | else | |
434 | return v; | |
435 | } | |
436 | } | |
437 | else | |
438 | { | |
439 | SCM mod; | |
440 | /* compilation of @ or @@ | |
441 | `what' is a three-element list: (MODNAME SYM INTERFACE?) | |
442 | INTERFACE? is #t if we compiled @ or #f if we compiled @@ | |
443 | */ | |
444 | mod = scm_resolve_module (SCM_CAR (what)); | |
445 | if (scm_is_true (SCM_CADDR (what))) | |
446 | mod = scm_module_public_interface (mod); | |
5c8cefe5 | 447 | if (scm_is_false (mod)) |
b7393ea1 AW |
448 | scm_misc_error (NULL, "no such module: ~S", |
449 | scm_list_1 (SCM_CAR (what))); | |
450 | /* might longjmp */ | |
451 | return scm_module_lookup (mod, SCM_CADR (what)); | |
452 | } | |
453 | } | |
454 | ||
51e9ba2f | 455 | #define VM_DEFAULT_STACK_SIZE (64 * 1024) |
17e90c5e | 456 | |
17e90c5e | 457 | #define VM_NAME vm_regular_engine |
6d14383e AW |
458 | #define FUNC_NAME "vm-regular-engine" |
459 | #define VM_ENGINE SCM_VM_REGULAR_ENGINE | |
83495480 | 460 | #include "vm-engine.c" |
17e90c5e | 461 | #undef VM_NAME |
6d14383e | 462 | #undef FUNC_NAME |
17e90c5e | 463 | #undef VM_ENGINE |
17e90c5e KN |
464 | |
465 | #define VM_NAME vm_debug_engine | |
6d14383e AW |
466 | #define FUNC_NAME "vm-debug-engine" |
467 | #define VM_ENGINE SCM_VM_DEBUG_ENGINE | |
83495480 | 468 | #include "vm-engine.c" |
17e90c5e | 469 | #undef VM_NAME |
6d14383e | 470 | #undef FUNC_NAME |
17e90c5e KN |
471 | #undef VM_ENGINE |
472 | ||
6d14383e AW |
473 | static const scm_t_vm_engine vm_engines[] = |
474 | { vm_regular_engine, vm_debug_engine }; | |
475 | ||
e3eb628d LC |
476 | #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN |
477 | ||
478 | /* The GC "kind" for the VM stack. */ | |
479 | static int vm_stack_gc_kind; | |
480 | ||
481 | #endif | |
482 | ||
a98cef7e | 483 | static SCM |
17e90c5e KN |
484 | make_vm (void) |
485 | #define FUNC_NAME "make_vm" | |
a98cef7e | 486 | { |
17e90c5e | 487 | int i; |
7f991c7d | 488 | struct scm_vm *vp; |
747a1635 | 489 | |
7f991c7d | 490 | vp = scm_gc_malloc (sizeof (struct scm_vm), "vm"); |
d8eeb67c | 491 | |
3d5ee0cd | 492 | vp->stack_size = VM_DEFAULT_STACK_SIZE; |
e3eb628d LC |
493 | |
494 | #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN | |
4168aa46 TTN |
495 | vp->stack_base = (SCM *) |
496 | GC_generic_malloc (vp->stack_size * sizeof (SCM), vm_stack_gc_kind); | |
e3eb628d LC |
497 | |
498 | /* Keep a pointer to VP so that `vm_stack_mark ()' can know what the stack | |
499 | top is. */ | |
500 | *vp->stack_base = PTR2SCM (vp); | |
501 | vp->stack_base++; | |
502 | vp->stack_size--; | |
503 | #else | |
d8eeb67c LC |
504 | vp->stack_base = scm_gc_malloc (vp->stack_size * sizeof (SCM), |
505 | "stack-base"); | |
e3eb628d LC |
506 | #endif |
507 | ||
2bbe1533 AW |
508 | #ifdef VM_ENABLE_STACK_NULLING |
509 | memset (vp->stack_base, 0, vp->stack_size * sizeof (SCM)); | |
510 | #endif | |
f1046e6b | 511 | vp->stack_limit = vp->stack_base + vp->stack_size - VM_STACK_RESERVE_SIZE; |
3616e9e9 KN |
512 | vp->ip = NULL; |
513 | vp->sp = vp->stack_base - 1; | |
514 | vp->fp = NULL; | |
6d14383e | 515 | vp->engine = SCM_VM_DEBUG_ENGINE; |
3d5ee0cd | 516 | vp->options = SCM_EOL; |
7656f194 | 517 | vp->trace_level = 0; |
17e90c5e | 518 | for (i = 0; i < SCM_VM_NUM_HOOKS; i++) |
3d5ee0cd | 519 | vp->hooks[i] = SCM_BOOL_F; |
2d026f04 | 520 | vp->cookie = 0; |
6f3b0cc2 | 521 | return scm_cell (scm_tc7_vm, (scm_t_bits)vp); |
a98cef7e | 522 | } |
17e90c5e | 523 | #undef FUNC_NAME |
a98cef7e | 524 | |
e3eb628d LC |
525 | #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN |
526 | ||
527 | /* Mark the VM stack region between its base and its current top. */ | |
528 | static struct GC_ms_entry * | |
529 | vm_stack_mark (GC_word *addr, struct GC_ms_entry *mark_stack_ptr, | |
530 | struct GC_ms_entry *mark_stack_limit, GC_word env) | |
531 | { | |
532 | GC_word *word; | |
533 | const struct scm_vm *vm; | |
534 | ||
535 | /* The first word of the VM stack should contain a pointer to the | |
536 | corresponding VM. */ | |
537 | vm = * ((struct scm_vm **) addr); | |
538 | ||
8071c490 | 539 | if (vm == NULL |
f1046e6b | 540 | || (SCM *) addr != vm->stack_base - 1) |
e3eb628d LC |
541 | /* ADDR must be a pointer to a free-list element, which we must ignore |
542 | (see warning in <gc/gc_mark.h>). */ | |
543 | return mark_stack_ptr; | |
544 | ||
e3eb628d LC |
545 | for (word = (GC_word *) vm->stack_base; word <= (GC_word *) vm->sp; word++) |
546 | mark_stack_ptr = GC_MARK_AND_PUSH ((* (GC_word **) word), | |
547 | mark_stack_ptr, mark_stack_limit, | |
548 | NULL); | |
549 | ||
550 | return mark_stack_ptr; | |
551 | } | |
552 | ||
553 | #endif /* VM_ENABLE_PRECISE_STACK_GC_SCAN */ | |
554 | ||
555 | ||
6d14383e | 556 | SCM |
4abef68f | 557 | scm_c_vm_run (SCM vm, SCM program, SCM *argv, int nargs) |
6d14383e | 558 | { |
4abef68f | 559 | struct scm_vm *vp = SCM_VM_DATA (vm); |
7656f194 | 560 | return vm_engines[vp->engine](vm, program, argv, nargs); |
6d14383e AW |
561 | } |
562 | ||
6f3b0cc2 AW |
563 | SCM_DEFINE (scm_vm_apply, "vm-apply", 3, 0, 0, |
564 | (SCM vm, SCM program, SCM args), | |
565 | "") | |
566 | #define FUNC_NAME s_scm_vm_apply | |
a98cef7e | 567 | { |
6d14383e AW |
568 | SCM *argv; |
569 | int i, nargs; | |
570 | ||
571 | SCM_VALIDATE_VM (1, vm); | |
67e2d80a | 572 | SCM_VALIDATE_PROC (2, program); |
6d14383e AW |
573 | |
574 | nargs = scm_ilength (args); | |
575 | if (SCM_UNLIKELY (nargs < 0)) | |
576 | scm_wrong_type_arg_msg (FUNC_NAME, 3, args, "list"); | |
577 | ||
578 | argv = alloca(nargs * sizeof(SCM)); | |
579 | for (i = 0; i < nargs; i++) | |
580 | { | |
581 | argv[i] = SCM_CAR (args); | |
582 | args = SCM_CDR (args); | |
583 | } | |
584 | ||
4abef68f | 585 | return scm_c_vm_run (vm, program, argv, nargs); |
a98cef7e | 586 | } |
17e90c5e | 587 | #undef FUNC_NAME |
a98cef7e KN |
588 | |
589 | /* Scheme interface */ | |
590 | ||
591 | SCM_DEFINE (scm_vm_version, "vm-version", 0, 0, 0, | |
17e90c5e KN |
592 | (void), |
593 | "") | |
a98cef7e KN |
594 | #define FUNC_NAME s_scm_vm_version |
595 | { | |
d3518113 | 596 | return scm_from_locale_string (PACKAGE_VERSION); |
a98cef7e KN |
597 | } |
598 | #undef FUNC_NAME | |
599 | ||
499a4c07 | 600 | SCM_DEFINE (scm_the_vm, "the-vm", 0, 0, 0, |
fcd4901b | 601 | (void), |
499a4c07 KN |
602 | "") |
603 | #define FUNC_NAME s_scm_the_vm | |
604 | { | |
2bbe1533 | 605 | scm_i_thread *t = SCM_I_CURRENT_THREAD; |
f63ea2ce | 606 | |
8b22ed7a | 607 | if (SCM_UNLIKELY (scm_is_false ((t->vm)))) |
2bbe1533 | 608 | t->vm = make_vm (); |
f63ea2ce | 609 | |
2bbe1533 | 610 | return t->vm; |
499a4c07 KN |
611 | } |
612 | #undef FUNC_NAME | |
613 | ||
614 | ||
a98cef7e KN |
615 | SCM_DEFINE (scm_vm_p, "vm?", 1, 0, 0, |
616 | (SCM obj), | |
17e90c5e | 617 | "") |
a98cef7e KN |
618 | #define FUNC_NAME s_scm_vm_p |
619 | { | |
9bd48cb1 | 620 | return scm_from_bool (SCM_VM_P (obj)); |
a98cef7e KN |
621 | } |
622 | #undef FUNC_NAME | |
623 | ||
624 | SCM_DEFINE (scm_make_vm, "make-vm", 0, 0, 0, | |
17e90c5e KN |
625 | (void), |
626 | "") | |
627 | #define FUNC_NAME s_scm_make_vm, | |
a98cef7e | 628 | { |
17e90c5e | 629 | return make_vm (); |
a98cef7e KN |
630 | } |
631 | #undef FUNC_NAME | |
632 | ||
17e90c5e | 633 | SCM_DEFINE (scm_vm_ip, "vm:ip", 1, 0, 0, |
a98cef7e | 634 | (SCM vm), |
17e90c5e KN |
635 | "") |
636 | #define FUNC_NAME s_scm_vm_ip | |
a98cef7e KN |
637 | { |
638 | SCM_VALIDATE_VM (1, vm); | |
f41cb00c | 639 | return scm_from_ulong ((unsigned long) SCM_VM_DATA (vm)->ip); |
a98cef7e KN |
640 | } |
641 | #undef FUNC_NAME | |
642 | ||
643 | SCM_DEFINE (scm_vm_sp, "vm:sp", 1, 0, 0, | |
644 | (SCM vm), | |
17e90c5e | 645 | "") |
a98cef7e KN |
646 | #define FUNC_NAME s_scm_vm_sp |
647 | { | |
648 | SCM_VALIDATE_VM (1, vm); | |
f41cb00c | 649 | return scm_from_ulong ((unsigned long) SCM_VM_DATA (vm)->sp); |
a98cef7e KN |
650 | } |
651 | #undef FUNC_NAME | |
652 | ||
653 | SCM_DEFINE (scm_vm_fp, "vm:fp", 1, 0, 0, | |
654 | (SCM vm), | |
17e90c5e | 655 | "") |
a98cef7e KN |
656 | #define FUNC_NAME s_scm_vm_fp |
657 | { | |
658 | SCM_VALIDATE_VM (1, vm); | |
f41cb00c | 659 | return scm_from_ulong ((unsigned long) SCM_VM_DATA (vm)->fp); |
a98cef7e KN |
660 | } |
661 | #undef FUNC_NAME | |
662 | ||
17e90c5e KN |
663 | #define VM_DEFINE_HOOK(n) \ |
664 | { \ | |
3d5ee0cd | 665 | struct scm_vm *vp; \ |
17e90c5e | 666 | SCM_VALIDATE_VM (1, vm); \ |
3d5ee0cd | 667 | vp = SCM_VM_DATA (vm); \ |
8b22ed7a | 668 | if (scm_is_false (vp->hooks[n])) \ |
238e7a11 | 669 | vp->hooks[n] = scm_make_hook (SCM_I_MAKINUM (1)); \ |
3d5ee0cd | 670 | return vp->hooks[n]; \ |
17e90c5e KN |
671 | } |
672 | ||
c45d4d77 | 673 | SCM_DEFINE (scm_vm_apply_hook, "vm-apply-hook", 1, 0, 0, |
17e90c5e KN |
674 | (SCM vm), |
675 | "") | |
c45d4d77 | 676 | #define FUNC_NAME s_scm_vm_apply_hook |
a98cef7e | 677 | { |
c45d4d77 | 678 | VM_DEFINE_HOOK (SCM_VM_APPLY_HOOK); |
a98cef7e KN |
679 | } |
680 | #undef FUNC_NAME | |
681 | ||
c45d4d77 | 682 | SCM_DEFINE (scm_vm_push_continuation_hook, "vm-push-continuation-hook", 1, 0, 0, |
17e90c5e KN |
683 | (SCM vm), |
684 | "") | |
c45d4d77 | 685 | #define FUNC_NAME s_scm_vm_push_continuation_hook |
a98cef7e | 686 | { |
c45d4d77 | 687 | VM_DEFINE_HOOK (SCM_VM_PUSH_CONTINUATION_HOOK); |
a98cef7e KN |
688 | } |
689 | #undef FUNC_NAME | |
690 | ||
c45d4d77 | 691 | SCM_DEFINE (scm_vm_pop_continuation_hook, "vm-pop-continuation-hook", 1, 0, 0, |
a98cef7e | 692 | (SCM vm), |
17e90c5e | 693 | "") |
c45d4d77 | 694 | #define FUNC_NAME s_scm_vm_pop_continuation_hook |
a98cef7e | 695 | { |
c45d4d77 | 696 | VM_DEFINE_HOOK (SCM_VM_POP_CONTINUATION_HOOK); |
a98cef7e KN |
697 | } |
698 | #undef FUNC_NAME | |
699 | ||
c45d4d77 | 700 | SCM_DEFINE (scm_vm_next_hook, "vm-next-hook", 1, 0, 0, |
a98cef7e | 701 | (SCM vm), |
17e90c5e | 702 | "") |
c45d4d77 | 703 | #define FUNC_NAME s_scm_vm_next_hook |
a98cef7e | 704 | { |
c45d4d77 | 705 | VM_DEFINE_HOOK (SCM_VM_NEXT_HOOK); |
a98cef7e KN |
706 | } |
707 | #undef FUNC_NAME | |
f3120251 AW |
708 | |
709 | SCM_DEFINE (scm_vm_abort_continuation_hook, "vm-abort-continuation-hook", 1, 0, 0, | |
710 | (SCM vm), | |
711 | "") | |
712 | #define FUNC_NAME s_scm_vm_abort_continuation_hook | |
713 | { | |
714 | VM_DEFINE_HOOK (SCM_VM_ABORT_CONTINUATION_HOOK); | |
715 | } | |
716 | #undef FUNC_NAME | |
717 | ||
718 | SCM_DEFINE (scm_vm_restore_continuation_hook, "vm-restore-continuation-hook", 1, 0, 0, | |
719 | (SCM vm), | |
720 | "") | |
721 | #define FUNC_NAME s_scm_vm_restore_continuation_hook | |
722 | { | |
723 | VM_DEFINE_HOOK (SCM_VM_RESTORE_CONTINUATION_HOOK); | |
724 | } | |
725 | #undef FUNC_NAME | |
a98cef7e | 726 | |
17e90c5e KN |
727 | SCM_DEFINE (scm_vm_option, "vm-option", 2, 0, 0, |
728 | (SCM vm, SCM key), | |
729 | "") | |
730 | #define FUNC_NAME s_scm_vm_option | |
a98cef7e KN |
731 | { |
732 | SCM_VALIDATE_VM (1, vm); | |
17e90c5e | 733 | return scm_assq_ref (SCM_VM_DATA (vm)->options, key); |
a98cef7e KN |
734 | } |
735 | #undef FUNC_NAME | |
736 | ||
17e90c5e KN |
737 | SCM_DEFINE (scm_set_vm_option_x, "set-vm-option!", 3, 0, 0, |
738 | (SCM vm, SCM key, SCM val), | |
739 | "") | |
740 | #define FUNC_NAME s_scm_set_vm_option_x | |
a98cef7e KN |
741 | { |
742 | SCM_VALIDATE_VM (1, vm); | |
17e90c5e KN |
743 | SCM_VM_DATA (vm)->options |
744 | = scm_assq_set_x (SCM_VM_DATA (vm)->options, key, val); | |
745 | return SCM_UNSPECIFIED; | |
a98cef7e KN |
746 | } |
747 | #undef FUNC_NAME | |
748 | ||
7656f194 | 749 | SCM_DEFINE (scm_vm_trace_level, "vm-trace-level", 1, 0, 0, |
17e90c5e KN |
750 | (SCM vm), |
751 | "") | |
7656f194 | 752 | #define FUNC_NAME s_scm_vm_trace_level |
a98cef7e | 753 | { |
a98cef7e | 754 | SCM_VALIDATE_VM (1, vm); |
7656f194 AW |
755 | return scm_from_int (SCM_VM_DATA (vm)->trace_level); |
756 | } | |
757 | #undef FUNC_NAME | |
758 | ||
759 | SCM_DEFINE (scm_set_vm_trace_level_x, "set-vm-trace-level!", 2, 0, 0, | |
760 | (SCM vm, SCM level), | |
761 | "") | |
762 | #define FUNC_NAME s_scm_set_vm_trace_level_x | |
763 | { | |
764 | SCM_VALIDATE_VM (1, vm); | |
765 | SCM_VM_DATA (vm)->trace_level = scm_to_int (level); | |
766 | return SCM_UNSPECIFIED; | |
a98cef7e KN |
767 | } |
768 | #undef FUNC_NAME | |
769 | ||
770 | \f | |
771 | /* | |
17e90c5e | 772 | * Initialize |
a98cef7e KN |
773 | */ |
774 | ||
07e56b27 AW |
775 | SCM scm_load_compiled_with_vm (SCM file) |
776 | { | |
53e28ed9 | 777 | SCM program = scm_make_program (scm_load_objcode (file), |
20d47c39 | 778 | SCM_BOOL_F, SCM_BOOL_F); |
07e56b27 | 779 | |
4abef68f | 780 | return scm_c_vm_run (scm_the_vm (), program, NULL, 0); |
07e56b27 AW |
781 | } |
782 | ||
17e90c5e | 783 | void |
07e56b27 | 784 | scm_bootstrap_vm (void) |
17e90c5e | 785 | { |
44602b08 AW |
786 | scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION, |
787 | "scm_init_vm", | |
60ae5ca2 AW |
788 | (scm_t_extension_init_func)scm_init_vm, NULL); |
789 | ||
35ac7852 AW |
790 | sym_vm_run = scm_from_locale_symbol ("vm-run"); |
791 | sym_vm_error = scm_from_locale_symbol ("vm-error"); | |
792 | sym_keyword_argument_error = scm_from_locale_symbol ("keyword-argument-error"); | |
793 | sym_debug = scm_from_locale_symbol ("debug"); | |
0404c97d | 794 | |
e3eb628d LC |
795 | #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN |
796 | vm_stack_gc_kind = | |
797 | GC_new_kind (GC_new_free_list (), | |
798 | GC_MAKE_PROC (GC_new_proc (vm_stack_mark), 0), | |
799 | 0, 1); | |
800 | ||
801 | #endif | |
07e56b27 AW |
802 | } |
803 | ||
804 | void | |
805 | scm_init_vm (void) | |
806 | { | |
17e90c5e | 807 | #ifndef SCM_MAGIC_SNARFER |
aeeff258 | 808 | #include "libguile/vm.x" |
17e90c5e | 809 | #endif |
a98cef7e | 810 | } |
17e90c5e KN |
811 | |
812 | /* | |
813 | Local Variables: | |
814 | c-file-style: "gnu" | |
815 | End: | |
816 | */ |