vm.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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_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 = SCM_PACK_POINTER (&c_frame);
  196. args[0] = SCM_PACK_POINTER (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) \
  238. (((SCM *) (scm_p)) + cp->reloc + (base - cp->stack_base))
  239. if ((base - vp->stack_base) + cp->stack_size + n + 1 > vp->stack_size)
  240. scm_misc_error ("vm-engine",
  241. "not enough space to instate partial continuation",
  242. scm_list_2 (vm, cont));
  243. memcpy (base, cp->stack_base, cp->stack_size * sizeof (SCM));
  244. /* now relocate frame pointers */
  245. {
  246. SCM *fp;
  247. for (fp = RELOC (cp->fp);
  248. SCM_FRAME_LOWER_ADDRESS (fp) > base;
  249. fp = SCM_FRAME_DYNAMIC_LINK (fp))
  250. SCM_FRAME_SET_DYNAMIC_LINK (fp, RELOC (SCM_FRAME_DYNAMIC_LINK (fp)));
  251. }
  252. vp->sp = base - 1 + cp->stack_size;
  253. vp->fp = RELOC (cp->fp);
  254. vp->ip = cp->mvra;
  255. /* now push args. ip is in a MV context. */
  256. for (i = 0; i < n; i++)
  257. {
  258. vp->sp++;
  259. *vp->sp = argv_copy[i];
  260. }
  261. vp->sp++;
  262. *vp->sp = scm_from_size_t (n);
  263. /* Finally, rewind the dynamic state.
  264. We have to treat prompts specially, because we could be rewinding the
  265. dynamic state from a different thread, or just a different position on the
  266. C and/or VM stack -- so we need to reset the jump buffers so that an abort
  267. comes back here, with appropriately adjusted sp and fp registers. */
  268. {
  269. long delta = 0;
  270. SCM newwinds = scm_i_dynwinds ();
  271. for (; scm_is_pair (intwinds); intwinds = scm_cdr (intwinds), delta--)
  272. {
  273. SCM x = scm_car (intwinds);
  274. if (SCM_PROMPT_P (x))
  275. /* the jmpbuf will be reset by our caller */
  276. x = scm_c_make_prompt (SCM_PROMPT_TAG (x),
  277. RELOC (SCM_PROMPT_REGISTERS (x)->fp),
  278. RELOC (SCM_PROMPT_REGISTERS (x)->sp),
  279. SCM_PROMPT_REGISTERS (x)->ip,
  280. SCM_PROMPT_ESCAPE_P (x),
  281. vm_cookie,
  282. newwinds);
  283. newwinds = scm_cons (x, newwinds);
  284. }
  285. scm_dowinds (newwinds, delta);
  286. }
  287. #undef RELOC
  288. }
  289. /*
  290. * VM Internal functions
  291. */
  292. void
  293. scm_i_vm_print (SCM x, SCM port, scm_print_state *pstate)
  294. {
  295. const struct scm_vm *vm;
  296. vm = SCM_VM_DATA (x);
  297. scm_puts_unlocked ("#<vm ", port);
  298. switch (vm->engine)
  299. {
  300. case SCM_VM_REGULAR_ENGINE:
  301. scm_puts_unlocked ("regular-engine ", port);
  302. break;
  303. case SCM_VM_DEBUG_ENGINE:
  304. scm_puts_unlocked ("debug-engine ", port);
  305. break;
  306. default:
  307. scm_puts_unlocked ("unknown-engine ", port);
  308. }
  309. scm_uintprint (SCM_UNPACK (x), 16, port);
  310. scm_puts_unlocked (">", port);
  311. }
  312. static SCM
  313. really_make_boot_program (long nargs)
  314. {
  315. SCM u8vec;
  316. scm_t_uint8 text[] = { scm_op_mv_call, 0, 0, 0, 1,
  317. scm_op_make_int8_1, scm_op_halt };
  318. struct scm_objcode *bp;
  319. SCM ret;
  320. if (SCM_UNLIKELY (nargs > 255 || nargs < 0))
  321. scm_misc_error ("vm-engine", "too many args when making boot procedure",
  322. scm_list_1 (scm_from_long (nargs)));
  323. text[1] = (scm_t_uint8)nargs;
  324. bp = scm_gc_malloc_pointerless (sizeof (struct scm_objcode) + sizeof (text),
  325. "boot-program");
  326. memcpy (SCM_C_OBJCODE_BASE (bp), text, sizeof (text));
  327. bp->len = sizeof(text);
  328. bp->metalen = 0;
  329. u8vec = scm_c_take_gc_bytevector ((scm_t_int8*)bp,
  330. sizeof (struct scm_objcode) + sizeof (text),
  331. SCM_BOOL_F);
  332. ret = scm_make_program (scm_bytecode_to_native_objcode (u8vec),
  333. SCM_BOOL_F, SCM_BOOL_F);
  334. SCM_SET_CELL_WORD_0 (ret, SCM_CELL_WORD_0 (ret) | SCM_F_PROGRAM_IS_BOOT);
  335. return ret;
  336. }
  337. #define NUM_BOOT_PROGS 8
  338. static SCM
  339. vm_make_boot_program (long nargs)
  340. {
  341. static SCM programs[NUM_BOOT_PROGS] = { SCM_BOOL_F, };
  342. if (SCM_UNLIKELY (scm_is_false (programs[0])))
  343. {
  344. int i;
  345. for (i = 0; i < NUM_BOOT_PROGS; i++)
  346. programs[i] = really_make_boot_program (i);
  347. }
  348. if (SCM_LIKELY (nargs < NUM_BOOT_PROGS))
  349. return programs[nargs];
  350. else
  351. return really_make_boot_program (nargs);
  352. }
  353. /*
  354. * VM
  355. */
  356. static SCM
  357. resolve_variable (SCM what, SCM program_module)
  358. {
  359. if (SCM_LIKELY (scm_is_symbol (what)))
  360. {
  361. if (SCM_LIKELY (scm_module_system_booted_p
  362. && scm_is_true (program_module)))
  363. /* might longjmp */
  364. return scm_module_lookup (program_module, what);
  365. else
  366. {
  367. SCM v = scm_sym2var (what, SCM_BOOL_F, SCM_BOOL_F);
  368. if (scm_is_false (v))
  369. scm_misc_error (NULL, "unbound variable: ~S", scm_list_1 (what));
  370. else
  371. return v;
  372. }
  373. }
  374. else
  375. {
  376. SCM mod;
  377. /* compilation of @ or @@
  378. `what' is a three-element list: (MODNAME SYM INTERFACE?)
  379. INTERFACE? is #t if we compiled @ or #f if we compiled @@
  380. */
  381. mod = scm_resolve_module (SCM_CAR (what));
  382. if (scm_is_true (SCM_CADDR (what)))
  383. mod = scm_module_public_interface (mod);
  384. if (scm_is_false (mod))
  385. scm_misc_error (NULL, "no such module: ~S",
  386. scm_list_1 (SCM_CAR (what)));
  387. /* might longjmp */
  388. return scm_module_lookup (mod, SCM_CADR (what));
  389. }
  390. }
  391. #define VM_DEFAULT_STACK_SIZE (64 * 1024)
  392. #define VM_NAME vm_regular_engine
  393. #define FUNC_NAME "vm-regular-engine"
  394. #define VM_ENGINE SCM_VM_REGULAR_ENGINE
  395. #include "vm-engine.c"
  396. #undef VM_NAME
  397. #undef FUNC_NAME
  398. #undef VM_ENGINE
  399. #define VM_NAME vm_debug_engine
  400. #define FUNC_NAME "vm-debug-engine"
  401. #define VM_ENGINE SCM_VM_DEBUG_ENGINE
  402. #include "vm-engine.c"
  403. #undef VM_NAME
  404. #undef FUNC_NAME
  405. #undef VM_ENGINE
  406. static const scm_t_vm_engine vm_engines[] =
  407. { vm_regular_engine, vm_debug_engine };
  408. #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN
  409. /* The GC "kind" for the VM stack. */
  410. static int vm_stack_gc_kind;
  411. #endif
  412. static SCM
  413. make_vm (void)
  414. #define FUNC_NAME "make_vm"
  415. {
  416. int i;
  417. struct scm_vm *vp;
  418. vp = scm_gc_malloc (sizeof (struct scm_vm), "vm");
  419. vp->stack_size = VM_DEFAULT_STACK_SIZE;
  420. #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN
  421. vp->stack_base = (SCM *)
  422. GC_generic_malloc (vp->stack_size * sizeof (SCM), vm_stack_gc_kind);
  423. /* Keep a pointer to VP so that `vm_stack_mark ()' can know what the stack
  424. top is. */
  425. *vp->stack_base = SCM_PACK_POINTER (vp);
  426. vp->stack_base++;
  427. vp->stack_size--;
  428. #else
  429. vp->stack_base = scm_gc_malloc (vp->stack_size * sizeof (SCM),
  430. "stack-base");
  431. #endif
  432. #ifdef VM_ENABLE_STACK_NULLING
  433. memset (vp->stack_base, 0, vp->stack_size * sizeof (SCM));
  434. #endif
  435. vp->stack_limit = vp->stack_base + vp->stack_size - VM_STACK_RESERVE_SIZE;
  436. vp->ip = NULL;
  437. vp->sp = vp->stack_base - 1;
  438. vp->fp = NULL;
  439. vp->engine = vm_default_engine;
  440. vp->trace_level = 0;
  441. for (i = 0; i < SCM_VM_NUM_HOOKS; i++)
  442. vp->hooks[i] = SCM_BOOL_F;
  443. vp->cookie = 0;
  444. return scm_cell (scm_tc7_vm, (scm_t_bits)vp);
  445. }
  446. #undef FUNC_NAME
  447. #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN
  448. /* Mark the VM stack region between its base and its current top. */
  449. static struct GC_ms_entry *
  450. vm_stack_mark (GC_word *addr, struct GC_ms_entry *mark_stack_ptr,
  451. struct GC_ms_entry *mark_stack_limit, GC_word env)
  452. {
  453. GC_word *word;
  454. const struct scm_vm *vm;
  455. /* The first word of the VM stack should contain a pointer to the
  456. corresponding VM. */
  457. vm = * ((struct scm_vm **) addr);
  458. if (vm == NULL
  459. || (SCM *) addr != vm->stack_base - 1)
  460. /* ADDR must be a pointer to a free-list element, which we must ignore
  461. (see warning in <gc/gc_mark.h>). */
  462. return mark_stack_ptr;
  463. for (word = (GC_word *) vm->stack_base; word <= (GC_word *) vm->sp; word++)
  464. mark_stack_ptr = GC_MARK_AND_PUSH ((* (GC_word **) word),
  465. mark_stack_ptr, mark_stack_limit,
  466. NULL);
  467. return mark_stack_ptr;
  468. }
  469. #endif /* VM_ENABLE_PRECISE_STACK_GC_SCAN */
  470. SCM
  471. scm_c_vm_run (SCM vm, SCM program, SCM *argv, int nargs)
  472. {
  473. struct scm_vm *vp = SCM_VM_DATA (vm);
  474. SCM_CHECK_STACK;
  475. return vm_engines[vp->engine](vm, program, argv, nargs);
  476. }
  477. /* Scheme interface */
  478. SCM_DEFINE (scm_the_vm, "the-vm", 0, 0, 0,
  479. (void),
  480. "Return the current thread's VM.")
  481. #define FUNC_NAME s_scm_the_vm
  482. {
  483. scm_i_thread *t = SCM_I_CURRENT_THREAD;
  484. if (SCM_UNLIKELY (scm_is_false (t->vm)))
  485. t->vm = make_vm ();
  486. return t->vm;
  487. }
  488. #undef FUNC_NAME
  489. SCM_DEFINE (scm_vm_p, "vm?", 1, 0, 0,
  490. (SCM obj),
  491. "")
  492. #define FUNC_NAME s_scm_vm_p
  493. {
  494. return scm_from_bool (SCM_VM_P (obj));
  495. }
  496. #undef FUNC_NAME
  497. SCM_DEFINE (scm_make_vm, "make-vm", 0, 0, 0,
  498. (void),
  499. "")
  500. #define FUNC_NAME s_scm_make_vm,
  501. {
  502. return make_vm ();
  503. }
  504. #undef FUNC_NAME
  505. SCM_DEFINE (scm_vm_ip, "vm:ip", 1, 0, 0,
  506. (SCM vm),
  507. "")
  508. #define FUNC_NAME s_scm_vm_ip
  509. {
  510. SCM_VALIDATE_VM (1, vm);
  511. return scm_from_unsigned_integer ((scm_t_bits) SCM_VM_DATA (vm)->ip);
  512. }
  513. #undef FUNC_NAME
  514. SCM_DEFINE (scm_vm_sp, "vm:sp", 1, 0, 0,
  515. (SCM vm),
  516. "")
  517. #define FUNC_NAME s_scm_vm_sp
  518. {
  519. SCM_VALIDATE_VM (1, vm);
  520. return scm_from_unsigned_integer ((scm_t_bits) SCM_VM_DATA (vm)->sp);
  521. }
  522. #undef FUNC_NAME
  523. SCM_DEFINE (scm_vm_fp, "vm:fp", 1, 0, 0,
  524. (SCM vm),
  525. "")
  526. #define FUNC_NAME s_scm_vm_fp
  527. {
  528. SCM_VALIDATE_VM (1, vm);
  529. return scm_from_unsigned_integer ((scm_t_bits) SCM_VM_DATA (vm)->fp);
  530. }
  531. #undef FUNC_NAME
  532. #define VM_DEFINE_HOOK(n) \
  533. { \
  534. struct scm_vm *vp; \
  535. SCM_VALIDATE_VM (1, vm); \
  536. vp = SCM_VM_DATA (vm); \
  537. if (scm_is_false (vp->hooks[n])) \
  538. vp->hooks[n] = scm_make_hook (SCM_I_MAKINUM (1)); \
  539. return vp->hooks[n]; \
  540. }
  541. SCM_DEFINE (scm_vm_apply_hook, "vm-apply-hook", 1, 0, 0,
  542. (SCM vm),
  543. "")
  544. #define FUNC_NAME s_scm_vm_apply_hook
  545. {
  546. VM_DEFINE_HOOK (SCM_VM_APPLY_HOOK);
  547. }
  548. #undef FUNC_NAME
  549. SCM_DEFINE (scm_vm_push_continuation_hook, "vm-push-continuation-hook", 1, 0, 0,
  550. (SCM vm),
  551. "")
  552. #define FUNC_NAME s_scm_vm_push_continuation_hook
  553. {
  554. VM_DEFINE_HOOK (SCM_VM_PUSH_CONTINUATION_HOOK);
  555. }
  556. #undef FUNC_NAME
  557. SCM_DEFINE (scm_vm_pop_continuation_hook, "vm-pop-continuation-hook", 1, 0, 0,
  558. (SCM vm),
  559. "")
  560. #define FUNC_NAME s_scm_vm_pop_continuation_hook
  561. {
  562. VM_DEFINE_HOOK (SCM_VM_POP_CONTINUATION_HOOK);
  563. }
  564. #undef FUNC_NAME
  565. SCM_DEFINE (scm_vm_next_hook, "vm-next-hook", 1, 0, 0,
  566. (SCM vm),
  567. "")
  568. #define FUNC_NAME s_scm_vm_next_hook
  569. {
  570. VM_DEFINE_HOOK (SCM_VM_NEXT_HOOK);
  571. }
  572. #undef FUNC_NAME
  573. SCM_DEFINE (scm_vm_abort_continuation_hook, "vm-abort-continuation-hook", 1, 0, 0,
  574. (SCM vm),
  575. "")
  576. #define FUNC_NAME s_scm_vm_abort_continuation_hook
  577. {
  578. VM_DEFINE_HOOK (SCM_VM_ABORT_CONTINUATION_HOOK);
  579. }
  580. #undef FUNC_NAME
  581. SCM_DEFINE (scm_vm_restore_continuation_hook, "vm-restore-continuation-hook", 1, 0, 0,
  582. (SCM vm),
  583. "")
  584. #define FUNC_NAME s_scm_vm_restore_continuation_hook
  585. {
  586. VM_DEFINE_HOOK (SCM_VM_RESTORE_CONTINUATION_HOOK);
  587. }
  588. #undef FUNC_NAME
  589. SCM_DEFINE (scm_vm_trace_level, "vm-trace-level", 1, 0, 0,
  590. (SCM vm),
  591. "")
  592. #define FUNC_NAME s_scm_vm_trace_level
  593. {
  594. SCM_VALIDATE_VM (1, vm);
  595. return scm_from_int (SCM_VM_DATA (vm)->trace_level);
  596. }
  597. #undef FUNC_NAME
  598. SCM_DEFINE (scm_set_vm_trace_level_x, "set-vm-trace-level!", 2, 0, 0,
  599. (SCM vm, SCM level),
  600. "")
  601. #define FUNC_NAME s_scm_set_vm_trace_level_x
  602. {
  603. SCM_VALIDATE_VM (1, vm);
  604. SCM_VM_DATA (vm)->trace_level = scm_to_int (level);
  605. return SCM_UNSPECIFIED;
  606. }
  607. #undef FUNC_NAME
  608. /*
  609. * VM engines
  610. */
  611. static int
  612. symbol_to_vm_engine (SCM engine, const char *FUNC_NAME)
  613. {
  614. if (scm_is_eq (engine, sym_regular))
  615. return SCM_VM_REGULAR_ENGINE;
  616. else if (scm_is_eq (engine, sym_debug))
  617. return SCM_VM_DEBUG_ENGINE;
  618. else
  619. SCM_MISC_ERROR ("Unknown VM engine: ~a", scm_list_1 (engine));
  620. }
  621. static SCM
  622. vm_engine_to_symbol (int engine, const char *FUNC_NAME)
  623. {
  624. switch (engine)
  625. {
  626. case SCM_VM_REGULAR_ENGINE:
  627. return sym_regular;
  628. case SCM_VM_DEBUG_ENGINE:
  629. return sym_debug;
  630. default:
  631. /* ? */
  632. SCM_MISC_ERROR ("Unknown VM engine: ~a",
  633. scm_list_1 (scm_from_int (engine)));
  634. }
  635. }
  636. SCM_DEFINE (scm_vm_engine, "vm-engine", 1, 0, 0,
  637. (SCM vm),
  638. "")
  639. #define FUNC_NAME s_scm_vm_engine
  640. {
  641. SCM_VALIDATE_VM (1, vm);
  642. return vm_engine_to_symbol (SCM_VM_DATA (vm)->engine, FUNC_NAME);
  643. }
  644. #undef FUNC_NAME
  645. void
  646. scm_c_set_vm_engine_x (SCM vm, int engine)
  647. #define FUNC_NAME "set-vm-engine!"
  648. {
  649. SCM_VALIDATE_VM (1, vm);
  650. if (engine < 0 || engine >= SCM_VM_NUM_ENGINES)
  651. SCM_MISC_ERROR ("Unknown VM engine: ~a",
  652. scm_list_1 (scm_from_int (engine)));
  653. SCM_VM_DATA (vm)->engine = engine;
  654. }
  655. #undef FUNC_NAME
  656. SCM_DEFINE (scm_set_vm_engine_x, "set-vm-engine!", 2, 0, 0,
  657. (SCM vm, SCM engine),
  658. "")
  659. #define FUNC_NAME s_scm_set_vm_engine_x
  660. {
  661. scm_c_set_vm_engine_x (vm, symbol_to_vm_engine (engine, FUNC_NAME));
  662. return SCM_UNSPECIFIED;
  663. }
  664. #undef FUNC_NAME
  665. void
  666. scm_c_set_default_vm_engine_x (int engine)
  667. #define FUNC_NAME "set-default-vm-engine!"
  668. {
  669. if (engine < 0 || engine >= SCM_VM_NUM_ENGINES)
  670. SCM_MISC_ERROR ("Unknown VM engine: ~a",
  671. scm_list_1 (scm_from_int (engine)));
  672. vm_default_engine = engine;
  673. }
  674. #undef FUNC_NAME
  675. SCM_DEFINE (scm_set_default_vm_engine_x, "set-default-vm-engine!", 1, 0, 0,
  676. (SCM engine),
  677. "")
  678. #define FUNC_NAME s_scm_set_default_vm_engine_x
  679. {
  680. scm_c_set_default_vm_engine_x (symbol_to_vm_engine (engine, FUNC_NAME));
  681. return SCM_UNSPECIFIED;
  682. }
  683. #undef FUNC_NAME
  684. static void reinstate_vm (SCM vm)
  685. {
  686. scm_i_thread *t = SCM_I_CURRENT_THREAD;
  687. t->vm = vm;
  688. }
  689. SCM_DEFINE (scm_call_with_vm, "call-with-vm", 2, 0, 1,
  690. (SCM vm, SCM proc, SCM args),
  691. "Apply @var{proc} to @var{args} in a dynamic extent in which\n"
  692. "@var{vm} is the current VM.\n\n"
  693. "As an implementation restriction, if @var{vm} is not the same\n"
  694. "as the current thread's VM, continuations captured within the\n"
  695. "call to @var{proc} may not be reinstated once control leaves\n"
  696. "@var{proc}.")
  697. #define FUNC_NAME s_scm_call_with_vm
  698. {
  699. SCM prev_vm, ret;
  700. SCM *argv;
  701. int i, nargs;
  702. scm_t_wind_flags flags;
  703. scm_i_thread *t = SCM_I_CURRENT_THREAD;
  704. SCM_VALIDATE_VM (1, vm);
  705. SCM_VALIDATE_PROC (2, proc);
  706. nargs = scm_ilength (args);
  707. if (SCM_UNLIKELY (nargs < 0))
  708. scm_wrong_type_arg_msg (FUNC_NAME, 3, args, "list");
  709. argv = alloca (nargs * sizeof(SCM));
  710. for (i = 0; i < nargs; i++)
  711. {
  712. argv[i] = SCM_CAR (args);
  713. args = SCM_CDR (args);
  714. }
  715. prev_vm = t->vm;
  716. /* Reentry can happen via invokation of a saved continuation, but
  717. continuations only save the state of the VM that they are in at
  718. capture-time, which might be different from this one. So, in the
  719. case that the VMs are different, set up a non-rewindable frame to
  720. prevent reinstating an incomplete continuation. */
  721. flags = scm_is_eq (prev_vm, vm) ? 0 : SCM_F_WIND_EXPLICITLY;
  722. if (flags)
  723. {
  724. scm_dynwind_begin (0);
  725. scm_dynwind_unwind_handler_with_scm (reinstate_vm, prev_vm, flags);
  726. t->vm = vm;
  727. }
  728. ret = scm_c_vm_run (vm, proc, argv, nargs);
  729. if (flags)
  730. scm_dynwind_end ();
  731. return ret;
  732. }
  733. #undef FUNC_NAME
  734. /*
  735. * Initialize
  736. */
  737. SCM scm_load_compiled_with_vm (SCM file)
  738. {
  739. SCM program = scm_make_program (scm_load_objcode (file),
  740. SCM_BOOL_F, SCM_BOOL_F);
  741. return scm_c_vm_run (scm_the_vm (), program, NULL, 0);
  742. }
  743. void
  744. scm_bootstrap_vm (void)
  745. {
  746. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  747. "scm_init_vm",
  748. (scm_t_extension_init_func)scm_init_vm, NULL);
  749. sym_vm_run = scm_from_latin1_symbol ("vm-run");
  750. sym_vm_error = scm_from_latin1_symbol ("vm-error");
  751. sym_keyword_argument_error = scm_from_latin1_symbol ("keyword-argument-error");
  752. sym_regular = scm_from_latin1_symbol ("regular");
  753. sym_debug = scm_from_latin1_symbol ("debug");
  754. #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN
  755. vm_stack_gc_kind =
  756. GC_new_kind (GC_new_free_list (),
  757. GC_MAKE_PROC (GC_new_proc (vm_stack_mark), 0),
  758. 0, 1);
  759. #endif
  760. }
  761. void
  762. scm_init_vm (void)
  763. {
  764. #ifndef SCM_MAGIC_SNARFER
  765. #include "libguile/vm.x"
  766. #endif
  767. }
  768. /*
  769. Local Variables:
  770. c-file-style: "gnu"
  771. End:
  772. */