vm.c 24 KB

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