vm.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /* Copyright (C) 2001, 2009, 2010, 2011, 2012 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. #if HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <stdlib.h>
  22. #include <alloca.h>
  23. #include <alignof.h>
  24. #include <string.h>
  25. #include <stdint.h>
  26. #include "libguile/bdw-gc.h"
  27. #include <gc/gc_mark.h>
  28. #include "_scm.h"
  29. #include "control.h"
  30. #include "frames.h"
  31. #include "instructions.h"
  32. #include "objcodes.h"
  33. #include "programs.h"
  34. #include "vm.h"
  35. static int vm_default_engine = SCM_VM_REGULAR_ENGINE;
  36. /* Unfortunately we can't snarf these: snarfed things are only loaded up from
  37. (system vm vm), which might not be loaded before an error happens. */
  38. static SCM sym_vm_run;
  39. static SCM sym_vm_error;
  40. static SCM sym_keyword_argument_error;
  41. static SCM sym_regular;
  42. static SCM sym_debug;
  43. /* The VM has a number of internal assertions that shouldn't normally be
  44. necessary, but might be if you think you found a bug in the VM. */
  45. #define VM_ENABLE_ASSERTIONS
  46. /* We can add a mode that ensures that all stack items above the stack pointer
  47. are NULL. This is useful for checking the internal consistency of the VM's
  48. assumptions and its operators, but isn't necessary for normal operation. It
  49. will ensure that assertions are enabled. Slows down the VM by about 30%. */
  50. /* NB! If you enable this, search for NULLING in throw.c */
  51. /* #define VM_ENABLE_STACK_NULLING */
  52. /* #define VM_ENABLE_PARANOID_ASSERTIONS */
  53. #if defined (VM_ENABLE_STACK_NULLING) && !defined (VM_ENABLE_ASSERTIONS)
  54. #define VM_ENABLE_ASSERTIONS
  55. #endif
  56. /* When defined, arrange so that the GC doesn't scan the VM stack beyond its
  57. current SP. This should help avoid excess data retention. See
  58. http://thread.gmane.org/gmane.comp.programming.garbage-collection.boehmgc/3001
  59. for a discussion. */
  60. #define VM_ENABLE_PRECISE_STACK_GC_SCAN
  61. /* Size in SCM objects of the stack reserve. The reserve is used to run
  62. exception handling code in case of a VM stack overflow. */
  63. #define VM_STACK_RESERVE_SIZE 512
  64. /*
  65. * VM Continuation
  66. */
  67. void
  68. scm_i_vm_cont_print (SCM x, SCM port, scm_print_state *pstate)
  69. {
  70. scm_puts_unlocked ("#<vm-continuation ", port);
  71. scm_uintprint (SCM_UNPACK (x), 16, port);
  72. scm_puts_unlocked (">", port);
  73. }
  74. /* In theory, a number of vm instances can be active in the call trace, and we
  75. only want to reify the continuations of those in the current continuation
  76. root. I don't see a nice way to do this -- ideally it would involve dynwinds,
  77. and previous values of the *the-vm* fluid within the current continuation
  78. root. But we don't have access to continuation roots in the dynwind stack.
  79. So, just punt for now, we just capture the continuation for the current VM.
  80. While I'm on the topic, ideally we could avoid copying the C stack if the
  81. continuation root is inside VM code, and call/cc was invoked within that same
  82. call to vm_run; but that's currently not implemented.
  83. */
  84. SCM
  85. scm_i_vm_capture_stack (SCM *stack_base, SCM *fp, SCM *sp, scm_t_uint8 *ra,
  86. scm_t_uint8 *mvra, scm_t_dynstack *dynstack,
  87. scm_t_uint32 flags)
  88. {
  89. struct scm_vm_cont *p;
  90. p = scm_gc_malloc (sizeof (*p), "capture_vm_cont");
  91. p->stack_size = sp - stack_base + 1;
  92. p->stack_base = scm_gc_malloc (p->stack_size * sizeof (SCM),
  93. "capture_vm_cont");
  94. #if defined(VM_ENABLE_STACK_NULLING) && 0
  95. /* Tail continuations leave their frame on the stack for subsequent
  96. application, but don't capture the frame -- so there are some elements on
  97. the stack then, and this check doesn't work, so disable it for now. */
  98. if (sp >= vp->stack_base)
  99. if (!vp->sp[0] || vp->sp[1])
  100. abort ();
  101. memset (p->stack_base, 0, p->stack_size * sizeof (SCM));
  102. #endif
  103. p->ra = ra;
  104. p->mvra = mvra;
  105. p->sp = sp;
  106. p->fp = fp;
  107. memcpy (p->stack_base, stack_base, (sp + 1 - stack_base) * sizeof (SCM));
  108. p->reloc = p->stack_base - stack_base;
  109. p->dynstack = dynstack;
  110. p->flags = flags;
  111. return scm_cell (scm_tc7_vm_cont, (scm_t_bits)p);
  112. }
  113. static void
  114. vm_return_to_continuation (SCM vm, SCM cont, size_t n, SCM *argv)
  115. {
  116. struct scm_vm *vp;
  117. struct scm_vm_cont *cp;
  118. SCM *argv_copy;
  119. argv_copy = alloca (n * sizeof(SCM));
  120. memcpy (argv_copy, argv, n * sizeof(SCM));
  121. vp = SCM_VM_DATA (vm);
  122. cp = SCM_VM_CONT_DATA (cont);
  123. if (n == 0 && !cp->mvra)
  124. scm_misc_error (NULL, "Too few values returned to continuation",
  125. SCM_EOL);
  126. if (vp->stack_size < cp->stack_size + n + 1)
  127. scm_misc_error ("vm-engine", "not enough space to reinstate continuation",
  128. scm_list_2 (vm, cont));
  129. #ifdef VM_ENABLE_STACK_NULLING
  130. {
  131. scm_t_ptrdiff nzero = (vp->sp - cp->sp);
  132. if (nzero > 0)
  133. memset (vp->stack_base + cp->stack_size, 0, nzero * sizeof (SCM));
  134. /* actually nzero should always be negative, because vm_reset_stack will
  135. unwind the stack to some point *below* this continuation */
  136. }
  137. #endif
  138. vp->sp = cp->sp;
  139. vp->fp = cp->fp;
  140. memcpy (vp->stack_base, cp->stack_base, cp->stack_size * sizeof (SCM));
  141. if (n == 1 || !cp->mvra)
  142. {
  143. vp->ip = cp->ra;
  144. vp->sp++;
  145. *vp->sp = argv_copy[0];
  146. }
  147. else
  148. {
  149. size_t i;
  150. for (i = 0; i < n; i++)
  151. {
  152. vp->sp++;
  153. *vp->sp = argv_copy[i];
  154. }
  155. vp->sp++;
  156. *vp->sp = scm_from_size_t (n);
  157. vp->ip = cp->mvra;
  158. }
  159. }
  160. SCM
  161. scm_i_capture_current_stack (void)
  162. {
  163. scm_i_thread *thread;
  164. SCM vm;
  165. struct scm_vm *vp;
  166. thread = SCM_I_CURRENT_THREAD;
  167. vm = scm_the_vm ();
  168. vp = SCM_VM_DATA (vm);
  169. return scm_i_vm_capture_stack (vp->stack_base, vp->fp, vp->sp, vp->ip, NULL,
  170. scm_dynstack_capture_all (&thread->dynstack),
  171. 0);
  172. }
  173. static void
  174. vm_dispatch_hook (SCM vm, int hook_num)
  175. {
  176. struct scm_vm *vp;
  177. SCM hook;
  178. struct scm_frame c_frame;
  179. scm_t_cell *frame;
  180. SCM args[1];
  181. int saved_trace_level;
  182. vp = SCM_VM_DATA (vm);
  183. hook = vp->hooks[hook_num];
  184. if (SCM_LIKELY (scm_is_false (hook))
  185. || scm_is_null (SCM_HOOK_PROCEDURES (hook)))
  186. return;
  187. saved_trace_level = vp->trace_level;
  188. vp->trace_level = 0;
  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. At the same time, procedures such as `frame-procedure' make sense only
  193. while the stack frame represented by the frame object is visible, so it
  194. seems reasonable to limit the lifetime of frame objects. */
  195. c_frame.stack_holder = vm;
  196. c_frame.fp = vp->fp;
  197. c_frame.sp = vp->sp;
  198. c_frame.ip = vp->ip;
  199. c_frame.offset = 0;
  200. /* Arrange for FRAME to be 8-byte aligned, like any other cell. */
  201. frame = alloca (sizeof (*frame) + 8);
  202. frame = (scm_t_cell *) ROUND_UP ((scm_t_uintptr) frame, 8UL);
  203. frame->word_0 = SCM_PACK (scm_tc7_frame);
  204. frame->word_1 = SCM_PACK_POINTER (&c_frame);
  205. args[0] = SCM_PACK_POINTER (frame);
  206. scm_c_run_hookn (hook, args, 1);
  207. vp->trace_level = saved_trace_level;
  208. }
  209. static void
  210. vm_abort (SCM vm, size_t n, scm_i_jmp_buf *current_registers) SCM_NORETURN;
  211. static void
  212. vm_abort (SCM vm, size_t n, scm_i_jmp_buf *current_registers)
  213. {
  214. size_t i;
  215. ssize_t tail_len;
  216. SCM tag, tail, *argv;
  217. /* FIXME: VM_ENABLE_STACK_NULLING */
  218. tail = *(SCM_VM_DATA (vm)->sp--);
  219. /* NULLSTACK (1) */
  220. tail_len = scm_ilength (tail);
  221. if (tail_len < 0)
  222. scm_misc_error ("vm-engine", "tail values to abort should be a list",
  223. scm_list_1 (tail));
  224. tag = SCM_VM_DATA (vm)->sp[-n];
  225. argv = alloca ((n + tail_len) * sizeof (SCM));
  226. for (i = 0; i < n; i++)
  227. argv[i] = SCM_VM_DATA (vm)->sp[-(n-1-i)];
  228. for (; i < n + tail_len; i++, tail = scm_cdr (tail))
  229. argv[i] = scm_car (tail);
  230. /* NULLSTACK (n + 1) */
  231. SCM_VM_DATA (vm)->sp -= n + 1;
  232. scm_c_abort (vm, tag, n + tail_len, argv, current_registers);
  233. }
  234. static void
  235. vm_reinstate_partial_continuation (SCM vm, SCM cont, size_t n, SCM *argv,
  236. scm_t_dynstack *dynstack,
  237. scm_i_jmp_buf *registers)
  238. {
  239. struct scm_vm *vp;
  240. struct scm_vm_cont *cp;
  241. SCM *argv_copy, *base;
  242. scm_t_ptrdiff reloc;
  243. size_t i;
  244. argv_copy = alloca (n * sizeof(SCM));
  245. memcpy (argv_copy, argv, n * sizeof(SCM));
  246. vp = SCM_VM_DATA (vm);
  247. cp = SCM_VM_CONT_DATA (cont);
  248. base = SCM_FRAME_UPPER_ADDRESS (vp->fp) + 1;
  249. reloc = cp->reloc + (base - cp->stack_base);
  250. #define RELOC(scm_p) \
  251. (((SCM *) (scm_p)) + reloc)
  252. if ((base - vp->stack_base) + cp->stack_size + n + 1 > vp->stack_size)
  253. scm_misc_error ("vm-engine",
  254. "not enough space to instate partial continuation",
  255. scm_list_2 (vm, cont));
  256. memcpy (base, cp->stack_base, cp->stack_size * sizeof (SCM));
  257. /* now relocate frame pointers */
  258. {
  259. SCM *fp;
  260. for (fp = RELOC (cp->fp);
  261. SCM_FRAME_LOWER_ADDRESS (fp) > base;
  262. fp = SCM_FRAME_DYNAMIC_LINK (fp))
  263. SCM_FRAME_SET_DYNAMIC_LINK (fp, RELOC (SCM_FRAME_DYNAMIC_LINK (fp)));
  264. }
  265. vp->sp = base - 1 + cp->stack_size;
  266. vp->fp = RELOC (cp->fp);
  267. vp->ip = cp->mvra;
  268. /* now push args. ip is in a MV context. */
  269. for (i = 0; i < n; i++)
  270. {
  271. vp->sp++;
  272. *vp->sp = argv_copy[i];
  273. }
  274. vp->sp++;
  275. *vp->sp = scm_from_size_t (n);
  276. /* The prompt captured a slice of the dynamic stack. Here we wind
  277. those entries onto the current thread's stack. We also have to
  278. relocate any prompts that we see along the way. */
  279. {
  280. scm_t_bits *walk;
  281. for (walk = SCM_DYNSTACK_FIRST (cp->dynstack);
  282. SCM_DYNSTACK_TAG (walk);
  283. walk = SCM_DYNSTACK_NEXT (walk))
  284. {
  285. scm_t_bits tag = SCM_DYNSTACK_TAG (walk);
  286. if (SCM_DYNSTACK_TAG_TYPE (tag) == SCM_DYNSTACK_TYPE_PROMPT)
  287. scm_dynstack_wind_prompt (dynstack, walk, reloc, registers);
  288. else
  289. scm_dynstack_wind_1 (dynstack, walk);
  290. }
  291. }
  292. #undef RELOC
  293. }
  294. /*
  295. * VM Internal functions
  296. */
  297. void
  298. scm_i_vm_print (SCM x, SCM port, scm_print_state *pstate)
  299. {
  300. const struct scm_vm *vm;
  301. vm = SCM_VM_DATA (x);
  302. scm_puts_unlocked ("#<vm ", port);
  303. switch (vm->engine)
  304. {
  305. case SCM_VM_REGULAR_ENGINE:
  306. scm_puts_unlocked ("regular-engine ", port);
  307. break;
  308. case SCM_VM_DEBUG_ENGINE:
  309. scm_puts_unlocked ("debug-engine ", port);
  310. break;
  311. default:
  312. scm_puts_unlocked ("unknown-engine ", port);
  313. }
  314. scm_uintprint (SCM_UNPACK (x), 16, port);
  315. scm_puts_unlocked (">", port);
  316. }
  317. static SCM
  318. really_make_boot_program (long nargs)
  319. {
  320. SCM u8vec;
  321. scm_t_uint8 text[] = { scm_op_mv_call, 0, 0, 0, 1,
  322. scm_op_make_int8_1, scm_op_halt };
  323. struct scm_objcode *bp;
  324. SCM ret;
  325. if (SCM_UNLIKELY (nargs > 255 || nargs < 0))
  326. scm_misc_error ("vm-engine", "too many args when making boot procedure",
  327. scm_list_1 (scm_from_long (nargs)));
  328. text[1] = (scm_t_uint8)nargs;
  329. bp = scm_gc_malloc_pointerless (sizeof (struct scm_objcode) + sizeof (text),
  330. "boot-program");
  331. memcpy (SCM_C_OBJCODE_BASE (bp), text, sizeof (text));
  332. bp->len = sizeof(text);
  333. bp->metalen = 0;
  334. u8vec = scm_c_take_gc_bytevector ((scm_t_int8*)bp,
  335. sizeof (struct scm_objcode) + sizeof (text),
  336. SCM_BOOL_F);
  337. ret = scm_make_program (scm_bytecode_to_native_objcode (u8vec),
  338. SCM_BOOL_F, SCM_BOOL_F);
  339. SCM_SET_CELL_WORD_0 (ret, SCM_CELL_WORD_0 (ret) | SCM_F_PROGRAM_IS_BOOT);
  340. return ret;
  341. }
  342. #define NUM_BOOT_PROGS 8
  343. static SCM
  344. vm_make_boot_program (long nargs)
  345. {
  346. static SCM programs[NUM_BOOT_PROGS] = { SCM_BOOL_F, };
  347. if (SCM_UNLIKELY (scm_is_false (programs[0])))
  348. {
  349. int i;
  350. for (i = 0; i < NUM_BOOT_PROGS; i++)
  351. programs[i] = really_make_boot_program (i);
  352. }
  353. if (SCM_LIKELY (nargs < NUM_BOOT_PROGS))
  354. return programs[nargs];
  355. else
  356. return really_make_boot_program (nargs);
  357. }
  358. /*
  359. * VM
  360. */
  361. static SCM
  362. resolve_variable (SCM what, SCM program_module)
  363. {
  364. if (SCM_LIKELY (scm_is_symbol (what)))
  365. {
  366. if (SCM_LIKELY (scm_module_system_booted_p
  367. && scm_is_true (program_module)))
  368. /* might longjmp */
  369. return scm_module_lookup (program_module, what);
  370. else
  371. {
  372. SCM v = scm_sym2var (what, SCM_BOOL_F, SCM_BOOL_F);
  373. if (scm_is_false (v))
  374. scm_misc_error (NULL, "unbound variable: ~S", scm_list_1 (what));
  375. else
  376. return v;
  377. }
  378. }
  379. else
  380. {
  381. SCM mod;
  382. /* compilation of @ or @@
  383. `what' is a three-element list: (MODNAME SYM INTERFACE?)
  384. INTERFACE? is #t if we compiled @ or #f if we compiled @@
  385. */
  386. mod = scm_resolve_module (SCM_CAR (what));
  387. if (scm_is_true (SCM_CADDR (what)))
  388. mod = scm_module_public_interface (mod);
  389. if (scm_is_false (mod))
  390. scm_misc_error (NULL, "no such module: ~S",
  391. scm_list_1 (SCM_CAR (what)));
  392. /* might longjmp */
  393. return scm_module_lookup (mod, SCM_CADR (what));
  394. }
  395. }
  396. #define VM_DEFAULT_STACK_SIZE (64 * 1024)
  397. #define VM_NAME vm_regular_engine
  398. #define FUNC_NAME "vm-regular-engine"
  399. #define VM_ENGINE SCM_VM_REGULAR_ENGINE
  400. #include "vm-engine.c"
  401. #undef VM_NAME
  402. #undef FUNC_NAME
  403. #undef VM_ENGINE
  404. #define VM_NAME vm_debug_engine
  405. #define FUNC_NAME "vm-debug-engine"
  406. #define VM_ENGINE SCM_VM_DEBUG_ENGINE
  407. #include "vm-engine.c"
  408. #undef VM_NAME
  409. #undef FUNC_NAME
  410. #undef VM_ENGINE
  411. static const scm_t_vm_engine vm_engines[] =
  412. { vm_regular_engine, vm_debug_engine };
  413. #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN
  414. /* The GC "kind" for the VM stack. */
  415. static int vm_stack_gc_kind;
  416. #endif
  417. static SCM
  418. make_vm (void)
  419. #define FUNC_NAME "make_vm"
  420. {
  421. int i;
  422. struct scm_vm *vp;
  423. vp = scm_gc_malloc (sizeof (struct scm_vm), "vm");
  424. vp->stack_size = VM_DEFAULT_STACK_SIZE;
  425. #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN
  426. vp->stack_base = (SCM *)
  427. GC_generic_malloc (vp->stack_size * sizeof (SCM), vm_stack_gc_kind);
  428. /* Keep a pointer to VP so that `vm_stack_mark ()' can know what the stack
  429. top is. */
  430. *vp->stack_base = SCM_PACK_POINTER (vp);
  431. vp->stack_base++;
  432. vp->stack_size--;
  433. #else
  434. vp->stack_base = scm_gc_malloc (vp->stack_size * sizeof (SCM),
  435. "stack-base");
  436. #endif
  437. #ifdef VM_ENABLE_STACK_NULLING
  438. memset (vp->stack_base, 0, vp->stack_size * sizeof (SCM));
  439. #endif
  440. vp->stack_limit = vp->stack_base + vp->stack_size - VM_STACK_RESERVE_SIZE;
  441. vp->ip = NULL;
  442. vp->sp = vp->stack_base - 1;
  443. vp->fp = NULL;
  444. vp->engine = vm_default_engine;
  445. vp->trace_level = 0;
  446. for (i = 0; i < SCM_VM_NUM_HOOKS; i++)
  447. vp->hooks[i] = SCM_BOOL_F;
  448. return scm_cell (scm_tc7_vm, (scm_t_bits)vp);
  449. }
  450. #undef FUNC_NAME
  451. #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN
  452. /* Mark the VM stack region between its base and its current top. */
  453. static struct GC_ms_entry *
  454. vm_stack_mark (GC_word *addr, struct GC_ms_entry *mark_stack_ptr,
  455. struct GC_ms_entry *mark_stack_limit, GC_word env)
  456. {
  457. GC_word *word;
  458. const struct scm_vm *vm;
  459. /* The first word of the VM stack should contain a pointer to the
  460. corresponding VM. */
  461. vm = * ((struct scm_vm **) addr);
  462. if (vm == NULL
  463. || (SCM *) addr != vm->stack_base - 1)
  464. /* ADDR must be a pointer to a free-list element, which we must ignore
  465. (see warning in <gc/gc_mark.h>). */
  466. return mark_stack_ptr;
  467. for (word = (GC_word *) vm->stack_base; word <= (GC_word *) vm->sp; word++)
  468. mark_stack_ptr = GC_MARK_AND_PUSH ((* (GC_word **) word),
  469. mark_stack_ptr, mark_stack_limit,
  470. NULL);
  471. return mark_stack_ptr;
  472. }
  473. #endif /* VM_ENABLE_PRECISE_STACK_GC_SCAN */
  474. SCM
  475. scm_c_vm_run (SCM vm, SCM program, SCM *argv, int nargs)
  476. {
  477. struct scm_vm *vp = SCM_VM_DATA (vm);
  478. SCM_CHECK_STACK;
  479. return vm_engines[vp->engine](vm, program, argv, nargs);
  480. }
  481. /* Scheme interface */
  482. SCM_DEFINE (scm_the_vm, "the-vm", 0, 0, 0,
  483. (void),
  484. "Return the current thread's VM.")
  485. #define FUNC_NAME s_scm_the_vm
  486. {
  487. scm_i_thread *t = SCM_I_CURRENT_THREAD;
  488. if (SCM_UNLIKELY (scm_is_false (t->vm)))
  489. t->vm = make_vm ();
  490. return t->vm;
  491. }
  492. #undef FUNC_NAME
  493. SCM_DEFINE (scm_vm_p, "vm?", 1, 0, 0,
  494. (SCM obj),
  495. "")
  496. #define FUNC_NAME s_scm_vm_p
  497. {
  498. return scm_from_bool (SCM_VM_P (obj));
  499. }
  500. #undef FUNC_NAME
  501. SCM_DEFINE (scm_make_vm, "make-vm", 0, 0, 0,
  502. (void),
  503. "")
  504. #define FUNC_NAME s_scm_make_vm,
  505. {
  506. return make_vm ();
  507. }
  508. #undef FUNC_NAME
  509. SCM_DEFINE (scm_vm_ip, "vm:ip", 1, 0, 0,
  510. (SCM vm),
  511. "")
  512. #define FUNC_NAME s_scm_vm_ip
  513. {
  514. SCM_VALIDATE_VM (1, vm);
  515. return scm_from_unsigned_integer ((scm_t_bits) SCM_VM_DATA (vm)->ip);
  516. }
  517. #undef FUNC_NAME
  518. SCM_DEFINE (scm_vm_sp, "vm:sp", 1, 0, 0,
  519. (SCM vm),
  520. "")
  521. #define FUNC_NAME s_scm_vm_sp
  522. {
  523. SCM_VALIDATE_VM (1, vm);
  524. return scm_from_unsigned_integer ((scm_t_bits) SCM_VM_DATA (vm)->sp);
  525. }
  526. #undef FUNC_NAME
  527. SCM_DEFINE (scm_vm_fp, "vm:fp", 1, 0, 0,
  528. (SCM vm),
  529. "")
  530. #define FUNC_NAME s_scm_vm_fp
  531. {
  532. SCM_VALIDATE_VM (1, vm);
  533. return scm_from_unsigned_integer ((scm_t_bits) SCM_VM_DATA (vm)->fp);
  534. }
  535. #undef FUNC_NAME
  536. #define VM_DEFINE_HOOK(n) \
  537. { \
  538. struct scm_vm *vp; \
  539. SCM_VALIDATE_VM (1, vm); \
  540. vp = SCM_VM_DATA (vm); \
  541. if (scm_is_false (vp->hooks[n])) \
  542. vp->hooks[n] = scm_make_hook (SCM_I_MAKINUM (1)); \
  543. return vp->hooks[n]; \
  544. }
  545. SCM_DEFINE (scm_vm_apply_hook, "vm-apply-hook", 1, 0, 0,
  546. (SCM vm),
  547. "")
  548. #define FUNC_NAME s_scm_vm_apply_hook
  549. {
  550. VM_DEFINE_HOOK (SCM_VM_APPLY_HOOK);
  551. }
  552. #undef FUNC_NAME
  553. SCM_DEFINE (scm_vm_push_continuation_hook, "vm-push-continuation-hook", 1, 0, 0,
  554. (SCM vm),
  555. "")
  556. #define FUNC_NAME s_scm_vm_push_continuation_hook
  557. {
  558. VM_DEFINE_HOOK (SCM_VM_PUSH_CONTINUATION_HOOK);
  559. }
  560. #undef FUNC_NAME
  561. SCM_DEFINE (scm_vm_pop_continuation_hook, "vm-pop-continuation-hook", 1, 0, 0,
  562. (SCM vm),
  563. "")
  564. #define FUNC_NAME s_scm_vm_pop_continuation_hook
  565. {
  566. VM_DEFINE_HOOK (SCM_VM_POP_CONTINUATION_HOOK);
  567. }
  568. #undef FUNC_NAME
  569. SCM_DEFINE (scm_vm_next_hook, "vm-next-hook", 1, 0, 0,
  570. (SCM vm),
  571. "")
  572. #define FUNC_NAME s_scm_vm_next_hook
  573. {
  574. VM_DEFINE_HOOK (SCM_VM_NEXT_HOOK);
  575. }
  576. #undef FUNC_NAME
  577. SCM_DEFINE (scm_vm_abort_continuation_hook, "vm-abort-continuation-hook", 1, 0, 0,
  578. (SCM vm),
  579. "")
  580. #define FUNC_NAME s_scm_vm_abort_continuation_hook
  581. {
  582. VM_DEFINE_HOOK (SCM_VM_ABORT_CONTINUATION_HOOK);
  583. }
  584. #undef FUNC_NAME
  585. SCM_DEFINE (scm_vm_restore_continuation_hook, "vm-restore-continuation-hook", 1, 0, 0,
  586. (SCM vm),
  587. "")
  588. #define FUNC_NAME s_scm_vm_restore_continuation_hook
  589. {
  590. VM_DEFINE_HOOK (SCM_VM_RESTORE_CONTINUATION_HOOK);
  591. }
  592. #undef FUNC_NAME
  593. SCM_DEFINE (scm_vm_trace_level, "vm-trace-level", 1, 0, 0,
  594. (SCM vm),
  595. "")
  596. #define FUNC_NAME s_scm_vm_trace_level
  597. {
  598. SCM_VALIDATE_VM (1, vm);
  599. return scm_from_int (SCM_VM_DATA (vm)->trace_level);
  600. }
  601. #undef FUNC_NAME
  602. SCM_DEFINE (scm_set_vm_trace_level_x, "set-vm-trace-level!", 2, 0, 0,
  603. (SCM vm, SCM level),
  604. "")
  605. #define FUNC_NAME s_scm_set_vm_trace_level_x
  606. {
  607. SCM_VALIDATE_VM (1, vm);
  608. SCM_VM_DATA (vm)->trace_level = scm_to_int (level);
  609. return SCM_UNSPECIFIED;
  610. }
  611. #undef FUNC_NAME
  612. /*
  613. * VM engines
  614. */
  615. static int
  616. symbol_to_vm_engine (SCM engine, const char *FUNC_NAME)
  617. {
  618. if (scm_is_eq (engine, sym_regular))
  619. return SCM_VM_REGULAR_ENGINE;
  620. else if (scm_is_eq (engine, sym_debug))
  621. return SCM_VM_DEBUG_ENGINE;
  622. else
  623. SCM_MISC_ERROR ("Unknown VM engine: ~a", scm_list_1 (engine));
  624. }
  625. static SCM
  626. vm_engine_to_symbol (int engine, const char *FUNC_NAME)
  627. {
  628. switch (engine)
  629. {
  630. case SCM_VM_REGULAR_ENGINE:
  631. return sym_regular;
  632. case SCM_VM_DEBUG_ENGINE:
  633. return sym_debug;
  634. default:
  635. /* ? */
  636. SCM_MISC_ERROR ("Unknown VM engine: ~a",
  637. scm_list_1 (scm_from_int (engine)));
  638. }
  639. }
  640. SCM_DEFINE (scm_vm_engine, "vm-engine", 1, 0, 0,
  641. (SCM vm),
  642. "")
  643. #define FUNC_NAME s_scm_vm_engine
  644. {
  645. SCM_VALIDATE_VM (1, vm);
  646. return vm_engine_to_symbol (SCM_VM_DATA (vm)->engine, FUNC_NAME);
  647. }
  648. #undef FUNC_NAME
  649. void
  650. scm_c_set_vm_engine_x (SCM vm, int engine)
  651. #define FUNC_NAME "set-vm-engine!"
  652. {
  653. SCM_VALIDATE_VM (1, vm);
  654. if (engine < 0 || engine >= SCM_VM_NUM_ENGINES)
  655. SCM_MISC_ERROR ("Unknown VM engine: ~a",
  656. scm_list_1 (scm_from_int (engine)));
  657. SCM_VM_DATA (vm)->engine = engine;
  658. }
  659. #undef FUNC_NAME
  660. SCM_DEFINE (scm_set_vm_engine_x, "set-vm-engine!", 2, 0, 0,
  661. (SCM vm, SCM engine),
  662. "")
  663. #define FUNC_NAME s_scm_set_vm_engine_x
  664. {
  665. scm_c_set_vm_engine_x (vm, symbol_to_vm_engine (engine, FUNC_NAME));
  666. return SCM_UNSPECIFIED;
  667. }
  668. #undef FUNC_NAME
  669. void
  670. scm_c_set_default_vm_engine_x (int engine)
  671. #define FUNC_NAME "set-default-vm-engine!"
  672. {
  673. if (engine < 0 || engine >= SCM_VM_NUM_ENGINES)
  674. SCM_MISC_ERROR ("Unknown VM engine: ~a",
  675. scm_list_1 (scm_from_int (engine)));
  676. vm_default_engine = engine;
  677. }
  678. #undef FUNC_NAME
  679. SCM_DEFINE (scm_set_default_vm_engine_x, "set-default-vm-engine!", 1, 0, 0,
  680. (SCM engine),
  681. "")
  682. #define FUNC_NAME s_scm_set_default_vm_engine_x
  683. {
  684. scm_c_set_default_vm_engine_x (symbol_to_vm_engine (engine, FUNC_NAME));
  685. return SCM_UNSPECIFIED;
  686. }
  687. #undef FUNC_NAME
  688. static void reinstate_vm (SCM vm)
  689. {
  690. scm_i_thread *t = SCM_I_CURRENT_THREAD;
  691. t->vm = vm;
  692. }
  693. SCM_DEFINE (scm_call_with_vm, "call-with-vm", 2, 0, 1,
  694. (SCM vm, SCM proc, SCM args),
  695. "Apply @var{proc} to @var{args} in a dynamic extent in which\n"
  696. "@var{vm} is the current VM.\n\n"
  697. "As an implementation restriction, if @var{vm} is not the same\n"
  698. "as the current thread's VM, continuations captured within the\n"
  699. "call to @var{proc} may not be reinstated once control leaves\n"
  700. "@var{proc}.")
  701. #define FUNC_NAME s_scm_call_with_vm
  702. {
  703. SCM prev_vm, ret;
  704. SCM *argv;
  705. int i, nargs;
  706. scm_t_wind_flags flags;
  707. scm_i_thread *t = SCM_I_CURRENT_THREAD;
  708. SCM_VALIDATE_VM (1, vm);
  709. SCM_VALIDATE_PROC (2, proc);
  710. nargs = scm_ilength (args);
  711. if (SCM_UNLIKELY (nargs < 0))
  712. scm_wrong_type_arg_msg (FUNC_NAME, 3, args, "list");
  713. argv = alloca (nargs * sizeof(SCM));
  714. for (i = 0; i < nargs; i++)
  715. {
  716. argv[i] = SCM_CAR (args);
  717. args = SCM_CDR (args);
  718. }
  719. prev_vm = t->vm;
  720. /* Reentry can happen via invokation of a saved continuation, but
  721. continuations only save the state of the VM that they are in at
  722. capture-time, which might be different from this one. So, in the
  723. case that the VMs are different, set up a non-rewindable frame to
  724. prevent reinstating an incomplete continuation. */
  725. flags = scm_is_eq (prev_vm, vm) ? 0 : SCM_F_WIND_EXPLICITLY;
  726. if (flags)
  727. {
  728. scm_dynwind_begin (0);
  729. scm_dynwind_unwind_handler_with_scm (reinstate_vm, prev_vm, flags);
  730. t->vm = vm;
  731. }
  732. ret = scm_c_vm_run (vm, proc, argv, nargs);
  733. if (flags)
  734. scm_dynwind_end ();
  735. return ret;
  736. }
  737. #undef FUNC_NAME
  738. /*
  739. * Initialize
  740. */
  741. SCM scm_load_compiled_with_vm (SCM file)
  742. {
  743. SCM program = scm_make_program (scm_load_objcode (file),
  744. SCM_BOOL_F, SCM_BOOL_F);
  745. return scm_c_vm_run (scm_the_vm (), program, NULL, 0);
  746. }
  747. void
  748. scm_bootstrap_vm (void)
  749. {
  750. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  751. "scm_init_vm",
  752. (scm_t_extension_init_func)scm_init_vm, NULL);
  753. sym_vm_run = scm_from_latin1_symbol ("vm-run");
  754. sym_vm_error = scm_from_latin1_symbol ("vm-error");
  755. sym_keyword_argument_error = scm_from_latin1_symbol ("keyword-argument-error");
  756. sym_regular = scm_from_latin1_symbol ("regular");
  757. sym_debug = scm_from_latin1_symbol ("debug");
  758. #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN
  759. vm_stack_gc_kind =
  760. GC_new_kind (GC_new_free_list (),
  761. GC_MAKE_PROC (GC_new_proc (vm_stack_mark), 0),
  762. 0, 1);
  763. #endif
  764. }
  765. void
  766. scm_init_vm (void)
  767. {
  768. #ifndef SCM_MAGIC_SNARFER
  769. #include "libguile/vm.x"
  770. #endif
  771. }
  772. /*
  773. Local Variables:
  774. c-file-style: "gnu"
  775. End:
  776. */