vm.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. /* Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #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. #include "private-gc.h" /* scm_getenv_int */
  36. static int vm_default_engine = SCM_VM_REGULAR_ENGINE;
  37. /* Unfortunately we can't snarf these: snarfed things are only loaded up from
  38. (system vm vm), which might not be loaded before an error happens. */
  39. static SCM sym_vm_run;
  40. static SCM sym_vm_error;
  41. static SCM sym_keyword_argument_error;
  42. static SCM sym_regular;
  43. static SCM sym_debug;
  44. /* The VM has a number of internal assertions that shouldn't normally be
  45. necessary, but might be if you think you found a bug in the VM. */
  46. #define VM_ENABLE_ASSERTIONS
  47. /* We can add a mode that ensures that all stack items above the stack pointer
  48. are NULL. This is useful for checking the internal consistency of the VM's
  49. assumptions and its operators, but isn't necessary for normal operation. It
  50. will ensure that assertions are enabled. Slows down the VM by about 30%. */
  51. /* NB! If you enable this, search for NULLING in throw.c */
  52. /* #define VM_ENABLE_STACK_NULLING */
  53. /* #define VM_ENABLE_PARANOID_ASSERTIONS */
  54. #if defined (VM_ENABLE_STACK_NULLING) && !defined (VM_ENABLE_ASSERTIONS)
  55. #define VM_ENABLE_ASSERTIONS
  56. #endif
  57. /* When defined, arrange so that the GC doesn't scan the VM stack beyond its
  58. current SP. This should help avoid excess data retention. See
  59. http://thread.gmane.org/gmane.comp.programming.garbage-collection.boehmgc/3001
  60. for a discussion. */
  61. #define VM_ENABLE_PRECISE_STACK_GC_SCAN
  62. /* Size in SCM objects of the stack reserve. The reserve is used to run
  63. exception handling code in case of a VM stack overflow. */
  64. #define VM_STACK_RESERVE_SIZE 512
  65. /*
  66. * VM Continuation
  67. */
  68. void
  69. scm_i_vm_cont_print (SCM x, SCM port, scm_print_state *pstate)
  70. {
  71. scm_puts_unlocked ("#<vm-continuation ", port);
  72. scm_uintprint (SCM_UNPACK (x), 16, port);
  73. scm_puts_unlocked (">", port);
  74. }
  75. /* In theory, a number of vm instances can be active in the call trace, and we
  76. only want to reify the continuations of those in the current continuation
  77. root. I don't see a nice way to do this -- ideally it would involve dynwinds,
  78. and previous values of the *the-vm* fluid within the current continuation
  79. root. But we don't have access to continuation roots in the dynwind stack.
  80. So, just punt for now, we just capture the continuation for the current VM.
  81. While I'm on the topic, ideally we could avoid copying the C stack if the
  82. continuation root is inside VM code, and call/cc was invoked within that same
  83. call to vm_run; but that's currently not implemented.
  84. */
  85. SCM
  86. scm_i_vm_capture_stack (SCM *stack_base, SCM *fp, SCM *sp, scm_t_uint8 *ra,
  87. scm_t_uint8 *mvra, scm_t_dynstack *dynstack,
  88. scm_t_uint32 flags)
  89. {
  90. struct scm_vm_cont *p;
  91. p = scm_gc_malloc (sizeof (*p), "capture_vm_cont");
  92. p->stack_size = sp - stack_base + 1;
  93. p->stack_base = scm_gc_malloc (p->stack_size * sizeof (SCM),
  94. "capture_vm_cont");
  95. #if defined(VM_ENABLE_STACK_NULLING) && 0
  96. /* Tail continuations leave their frame on the stack for subsequent
  97. application, but don't capture the frame -- so there are some elements on
  98. the stack then, and this check doesn't work, so disable it for now. */
  99. if (sp >= vp->stack_base)
  100. if (!vp->sp[0] || vp->sp[1])
  101. abort ();
  102. memset (p->stack_base, 0, p->stack_size * sizeof (SCM));
  103. #endif
  104. p->ra = ra;
  105. p->mvra = mvra;
  106. p->sp = sp;
  107. p->fp = fp;
  108. memcpy (p->stack_base, stack_base, (sp + 1 - stack_base) * sizeof (SCM));
  109. p->reloc = p->stack_base - stack_base;
  110. p->dynstack = dynstack;
  111. p->flags = flags;
  112. return scm_cell (scm_tc7_vm_cont, (scm_t_bits)p);
  113. }
  114. static void
  115. vm_return_to_continuation (SCM vm, SCM cont, size_t n, SCM *argv)
  116. {
  117. struct scm_vm *vp;
  118. struct scm_vm_cont *cp;
  119. SCM *argv_copy;
  120. argv_copy = alloca (n * sizeof(SCM));
  121. memcpy (argv_copy, argv, n * sizeof(SCM));
  122. vp = SCM_VM_DATA (vm);
  123. cp = SCM_VM_CONT_DATA (cont);
  124. if (n == 0 && !cp->mvra)
  125. scm_misc_error (NULL, "Too few values returned to continuation",
  126. SCM_EOL);
  127. if (vp->stack_size < cp->stack_size + n + 1)
  128. scm_misc_error ("vm-engine", "not enough space to reinstate continuation",
  129. scm_list_2 (vm, cont));
  130. #ifdef VM_ENABLE_STACK_NULLING
  131. {
  132. scm_t_ptrdiff nzero = (vp->sp - cp->sp);
  133. if (nzero > 0)
  134. memset (vp->stack_base + cp->stack_size, 0, nzero * sizeof (SCM));
  135. /* actually nzero should always be negative, because vm_reset_stack will
  136. unwind the stack to some point *below* this continuation */
  137. }
  138. #endif
  139. vp->sp = cp->sp;
  140. vp->fp = cp->fp;
  141. memcpy (vp->stack_base, cp->stack_base, cp->stack_size * sizeof (SCM));
  142. if (n == 1 || !cp->mvra)
  143. {
  144. vp->ip = cp->ra;
  145. vp->sp++;
  146. *vp->sp = argv_copy[0];
  147. }
  148. else
  149. {
  150. size_t i;
  151. for (i = 0; i < n; i++)
  152. {
  153. vp->sp++;
  154. *vp->sp = argv_copy[i];
  155. }
  156. vp->sp++;
  157. *vp->sp = scm_from_size_t (n);
  158. vp->ip = cp->mvra;
  159. }
  160. }
  161. SCM
  162. scm_i_capture_current_stack (void)
  163. {
  164. scm_i_thread *thread;
  165. SCM vm;
  166. struct scm_vm *vp;
  167. thread = SCM_I_CURRENT_THREAD;
  168. vm = scm_the_vm ();
  169. vp = SCM_VM_DATA (vm);
  170. return scm_i_vm_capture_stack (vp->stack_base, vp->fp, vp->sp, vp->ip, NULL,
  171. scm_dynstack_capture_all (&thread->dynstack),
  172. 0);
  173. }
  174. static void vm_dispatch_hook (SCM vm, int hook_num,
  175. SCM *argv, int n) SCM_NOINLINE;
  176. static void
  177. vm_dispatch_hook (SCM vm, int hook_num, SCM *argv, int n)
  178. {
  179. struct scm_vm *vp;
  180. SCM hook;
  181. struct scm_frame c_frame;
  182. scm_t_cell *frame;
  183. int saved_trace_level;
  184. vp = SCM_VM_DATA (vm);
  185. hook = vp->hooks[hook_num];
  186. if (SCM_LIKELY (scm_is_false (hook))
  187. || scm_is_null (SCM_HOOK_PROCEDURES (hook)))
  188. return;
  189. saved_trace_level = vp->trace_level;
  190. vp->trace_level = 0;
  191. /* Allocate a frame object on the stack. This is more efficient than calling
  192. `scm_c_make_frame ()' to allocate on the heap, but it forces hooks to not
  193. capture frame objects.
  194. At the same time, procedures such as `frame-procedure' make sense only
  195. while the stack frame represented by the frame object is visible, so it
  196. seems reasonable to limit the lifetime of frame objects. */
  197. c_frame.stack_holder = vm;
  198. c_frame.fp = vp->fp;
  199. c_frame.sp = vp->sp;
  200. c_frame.ip = vp->ip;
  201. c_frame.offset = 0;
  202. /* Arrange for FRAME to be 8-byte aligned, like any other cell. */
  203. frame = alloca (sizeof (*frame) + 8);
  204. frame = (scm_t_cell *) ROUND_UP ((scm_t_uintptr) frame, 8UL);
  205. frame->word_0 = SCM_PACK (scm_tc7_frame);
  206. frame->word_1 = SCM_PACK_POINTER (&c_frame);
  207. if (n == 0)
  208. {
  209. SCM args[1];
  210. args[0] = SCM_PACK_POINTER (frame);
  211. scm_c_run_hookn (hook, args, 1);
  212. }
  213. else if (n == 1)
  214. {
  215. SCM args[2];
  216. args[0] = SCM_PACK_POINTER (frame);
  217. args[1] = argv[0];
  218. scm_c_run_hookn (hook, args, 2);
  219. }
  220. else
  221. {
  222. SCM args = SCM_EOL;
  223. while (n--)
  224. args = scm_cons (argv[n], args);
  225. scm_c_run_hook (hook, scm_cons (SCM_PACK_POINTER (frame), args));
  226. }
  227. vp->trace_level = saved_trace_level;
  228. }
  229. static void
  230. vm_abort (SCM vm, size_t n, scm_i_jmp_buf *current_registers) SCM_NORETURN;
  231. static void
  232. vm_abort (SCM vm, size_t n, scm_i_jmp_buf *current_registers)
  233. {
  234. size_t i;
  235. ssize_t tail_len;
  236. SCM tag, tail, *argv;
  237. /* FIXME: VM_ENABLE_STACK_NULLING */
  238. tail = *(SCM_VM_DATA (vm)->sp--);
  239. /* NULLSTACK (1) */
  240. tail_len = scm_ilength (tail);
  241. if (tail_len < 0)
  242. scm_misc_error ("vm-engine", "tail values to abort should be a list",
  243. scm_list_1 (tail));
  244. tag = SCM_VM_DATA (vm)->sp[-n];
  245. argv = alloca ((n + tail_len) * sizeof (SCM));
  246. for (i = 0; i < n; i++)
  247. argv[i] = SCM_VM_DATA (vm)->sp[-(n-1-i)];
  248. for (; i < n + tail_len; i++, tail = scm_cdr (tail))
  249. argv[i] = scm_car (tail);
  250. /* NULLSTACK (n + 1) */
  251. SCM_VM_DATA (vm)->sp -= n + 1;
  252. scm_c_abort (vm, tag, n + tail_len, argv, current_registers);
  253. }
  254. static void
  255. vm_reinstate_partial_continuation (SCM vm, SCM cont, size_t n, SCM *argv,
  256. scm_t_dynstack *dynstack,
  257. scm_i_jmp_buf *registers)
  258. {
  259. struct scm_vm *vp;
  260. struct scm_vm_cont *cp;
  261. SCM *argv_copy, *base;
  262. scm_t_ptrdiff reloc;
  263. size_t i;
  264. argv_copy = alloca (n * sizeof(SCM));
  265. memcpy (argv_copy, argv, n * sizeof(SCM));
  266. vp = SCM_VM_DATA (vm);
  267. cp = SCM_VM_CONT_DATA (cont);
  268. base = SCM_FRAME_UPPER_ADDRESS (vp->fp) + 1;
  269. reloc = cp->reloc + (base - cp->stack_base);
  270. #define RELOC(scm_p) \
  271. (((SCM *) (scm_p)) + reloc)
  272. if ((base - vp->stack_base) + cp->stack_size + n + 1 > vp->stack_size)
  273. scm_misc_error ("vm-engine",
  274. "not enough space to instate partial continuation",
  275. scm_list_2 (vm, cont));
  276. memcpy (base, cp->stack_base, cp->stack_size * sizeof (SCM));
  277. /* now relocate frame pointers */
  278. {
  279. SCM *fp;
  280. for (fp = RELOC (cp->fp);
  281. SCM_FRAME_LOWER_ADDRESS (fp) > base;
  282. fp = SCM_FRAME_DYNAMIC_LINK (fp))
  283. SCM_FRAME_SET_DYNAMIC_LINK (fp, RELOC (SCM_FRAME_DYNAMIC_LINK (fp)));
  284. }
  285. vp->sp = base - 1 + cp->stack_size;
  286. vp->fp = RELOC (cp->fp);
  287. vp->ip = cp->mvra;
  288. /* now push args. ip is in a MV context. */
  289. for (i = 0; i < n; i++)
  290. {
  291. vp->sp++;
  292. *vp->sp = argv_copy[i];
  293. }
  294. vp->sp++;
  295. *vp->sp = scm_from_size_t (n);
  296. /* The prompt captured a slice of the dynamic stack. Here we wind
  297. those entries onto the current thread's stack. We also have to
  298. relocate any prompts that we see along the way. */
  299. {
  300. scm_t_bits *walk;
  301. for (walk = SCM_DYNSTACK_FIRST (cp->dynstack);
  302. SCM_DYNSTACK_TAG (walk);
  303. walk = SCM_DYNSTACK_NEXT (walk))
  304. {
  305. scm_t_bits tag = SCM_DYNSTACK_TAG (walk);
  306. if (SCM_DYNSTACK_TAG_TYPE (tag) == SCM_DYNSTACK_TYPE_PROMPT)
  307. scm_dynstack_wind_prompt (dynstack, walk, reloc, registers);
  308. else
  309. scm_dynstack_wind_1 (dynstack, walk);
  310. }
  311. }
  312. #undef RELOC
  313. }
  314. /*
  315. * VM Internal functions
  316. */
  317. void
  318. scm_i_vm_print (SCM x, SCM port, scm_print_state *pstate)
  319. {
  320. const struct scm_vm *vm;
  321. vm = SCM_VM_DATA (x);
  322. scm_puts_unlocked ("#<vm ", port);
  323. switch (vm->engine)
  324. {
  325. case SCM_VM_REGULAR_ENGINE:
  326. scm_puts_unlocked ("regular-engine ", port);
  327. break;
  328. case SCM_VM_DEBUG_ENGINE:
  329. scm_puts_unlocked ("debug-engine ", port);
  330. break;
  331. default:
  332. scm_puts_unlocked ("unknown-engine ", port);
  333. }
  334. scm_uintprint (SCM_UNPACK (x), 16, port);
  335. scm_puts_unlocked (">", port);
  336. }
  337. /*
  338. * VM Error Handling
  339. */
  340. static void vm_error (const char *msg, SCM arg) SCM_NORETURN;
  341. static void vm_error_bad_instruction (scm_t_uint32 inst) SCM_NORETURN SCM_NOINLINE;
  342. static void vm_error_unbound (SCM proc, SCM sym) SCM_NORETURN SCM_NOINLINE;
  343. static void vm_error_unbound_fluid (SCM proc, SCM fluid) SCM_NORETURN SCM_NOINLINE;
  344. static void vm_error_not_a_variable (const char *func_name, SCM x) SCM_NORETURN SCM_NOINLINE;
  345. static void vm_error_apply_to_non_list (SCM x) SCM_NORETURN SCM_NOINLINE;
  346. static void vm_error_kwargs_length_not_even (SCM proc) SCM_NORETURN SCM_NOINLINE;
  347. static void vm_error_kwargs_invalid_keyword (SCM proc, SCM obj) SCM_NORETURN SCM_NOINLINE;
  348. static void vm_error_kwargs_unrecognized_keyword (SCM proc, SCM kw) SCM_NORETURN SCM_NOINLINE;
  349. static void vm_error_too_many_args (int nargs) SCM_NORETURN SCM_NOINLINE;
  350. static void vm_error_wrong_num_args (SCM proc) SCM_NORETURN SCM_NOINLINE;
  351. static void vm_error_wrong_type_apply (SCM proc) SCM_NORETURN SCM_NOINLINE;
  352. static void vm_error_stack_overflow (struct scm_vm *vp) SCM_NORETURN SCM_NOINLINE;
  353. static void vm_error_stack_underflow (void) SCM_NORETURN SCM_NOINLINE;
  354. static void vm_error_improper_list (SCM x) SCM_NORETURN SCM_NOINLINE;
  355. static void vm_error_not_a_pair (const char *subr, SCM x) SCM_NORETURN SCM_NOINLINE;
  356. static void vm_error_not_a_bytevector (const char *subr, SCM x) SCM_NORETURN SCM_NOINLINE;
  357. static void vm_error_not_a_struct (const char *subr, SCM x) SCM_NORETURN SCM_NOINLINE;
  358. static void vm_error_no_values (void) SCM_NORETURN SCM_NOINLINE;
  359. static void vm_error_not_enough_values (void) SCM_NORETURN SCM_NOINLINE;
  360. static void vm_error_continuation_not_rewindable (SCM cont) SCM_NORETURN SCM_NOINLINE;
  361. static void vm_error_bad_wide_string_length (size_t len) SCM_NORETURN SCM_NOINLINE;
  362. static void
  363. vm_error (const char *msg, SCM arg)
  364. {
  365. scm_throw (sym_vm_error,
  366. scm_list_3 (sym_vm_run, scm_from_latin1_string (msg),
  367. SCM_UNBNDP (arg) ? SCM_EOL : scm_list_1 (arg)));
  368. abort(); /* not reached */
  369. }
  370. static void
  371. vm_error_bad_instruction (scm_t_uint32 inst)
  372. {
  373. vm_error ("VM: Bad instruction: ~s", scm_from_uint32 (inst));
  374. }
  375. static void
  376. vm_error_unbound (SCM proc, SCM sym)
  377. {
  378. scm_error_scm (scm_misc_error_key, proc,
  379. scm_from_latin1_string ("Unbound variable: ~s"),
  380. scm_list_1 (sym), SCM_BOOL_F);
  381. }
  382. static void
  383. vm_error_unbound_fluid (SCM proc, SCM fluid)
  384. {
  385. scm_error_scm (scm_misc_error_key, proc,
  386. scm_from_latin1_string ("Unbound fluid: ~s"),
  387. scm_list_1 (fluid), SCM_BOOL_F);
  388. }
  389. static void
  390. vm_error_not_a_variable (const char *func_name, SCM x)
  391. {
  392. scm_error (scm_arg_type_key, func_name, "Not a variable: ~S",
  393. scm_list_1 (x), scm_list_1 (x));
  394. }
  395. static void
  396. vm_error_apply_to_non_list (SCM x)
  397. {
  398. scm_error (scm_arg_type_key, "apply", "Apply to non-list: ~S",
  399. scm_list_1 (x), scm_list_1 (x));
  400. }
  401. static void
  402. vm_error_kwargs_length_not_even (SCM proc)
  403. {
  404. scm_error_scm (sym_keyword_argument_error, proc,
  405. scm_from_latin1_string ("Odd length of keyword argument list"),
  406. SCM_EOL, SCM_BOOL_F);
  407. }
  408. static void
  409. vm_error_kwargs_invalid_keyword (SCM proc, SCM obj)
  410. {
  411. scm_error_scm (sym_keyword_argument_error, proc,
  412. scm_from_latin1_string ("Invalid keyword"),
  413. SCM_EOL, scm_list_1 (obj));
  414. }
  415. static void
  416. vm_error_kwargs_unrecognized_keyword (SCM proc, SCM kw)
  417. {
  418. scm_error_scm (sym_keyword_argument_error, proc,
  419. scm_from_latin1_string ("Unrecognized keyword"),
  420. SCM_EOL, scm_list_1 (kw));
  421. }
  422. static void
  423. vm_error_too_many_args (int nargs)
  424. {
  425. vm_error ("VM: Too many arguments", scm_from_int (nargs));
  426. }
  427. static void
  428. vm_error_wrong_num_args (SCM proc)
  429. {
  430. scm_wrong_num_args (proc);
  431. }
  432. static void
  433. vm_error_wrong_type_apply (SCM proc)
  434. {
  435. scm_error (scm_arg_type_key, NULL, "Wrong type to apply: ~S",
  436. scm_list_1 (proc), scm_list_1 (proc));
  437. }
  438. static void
  439. vm_error_stack_overflow (struct scm_vm *vp)
  440. {
  441. if (vp->stack_limit < vp->stack_base + vp->stack_size)
  442. /* There are VM_STACK_RESERVE_SIZE bytes left. Make them available so
  443. that `throw' below can run on this VM. */
  444. vp->stack_limit = vp->stack_base + vp->stack_size;
  445. else
  446. /* There is no space left on the stack. FIXME: Do something more
  447. sensible here! */
  448. abort ();
  449. vm_error ("VM: Stack overflow", SCM_UNDEFINED);
  450. }
  451. static void
  452. vm_error_stack_underflow (void)
  453. {
  454. vm_error ("VM: Stack underflow", SCM_UNDEFINED);
  455. }
  456. static void
  457. vm_error_improper_list (SCM x)
  458. {
  459. vm_error ("Expected a proper list, but got object with tail ~s", x);
  460. }
  461. static void
  462. vm_error_not_a_pair (const char *subr, SCM x)
  463. {
  464. scm_wrong_type_arg_msg (subr, 1, x, "pair");
  465. }
  466. static void
  467. vm_error_not_a_bytevector (const char *subr, SCM x)
  468. {
  469. scm_wrong_type_arg_msg (subr, 1, x, "bytevector");
  470. }
  471. static void
  472. vm_error_not_a_struct (const char *subr, SCM x)
  473. {
  474. scm_wrong_type_arg_msg (subr, 1, x, "struct");
  475. }
  476. static void
  477. vm_error_no_values (void)
  478. {
  479. vm_error ("Zero values returned to single-valued continuation",
  480. SCM_UNDEFINED);
  481. }
  482. static void
  483. vm_error_not_enough_values (void)
  484. {
  485. vm_error ("Too few values returned to continuation", SCM_UNDEFINED);
  486. }
  487. static void
  488. vm_error_continuation_not_rewindable (SCM cont)
  489. {
  490. vm_error ("Unrewindable partial continuation", cont);
  491. }
  492. static void
  493. vm_error_bad_wide_string_length (size_t len)
  494. {
  495. vm_error ("VM: Bad wide string length: ~S", scm_from_size_t (len));
  496. }
  497. static SCM boot_continuation;
  498. static SCM rtl_boot_continuation;
  499. static SCM rtl_apply;
  500. static SCM rtl_values;
  501. static const scm_t_uint32 rtl_boot_continuation_code[] = {
  502. SCM_PACK_RTL_24 (scm_rtl_op_halt, 0)
  503. };
  504. static const scm_t_uint32 rtl_apply_code[] = {
  505. SCM_PACK_RTL_24 (scm_rtl_op_tail_apply, 0) /* proc in r1, args from r2, nargs set */
  506. };
  507. static const scm_t_uint32 rtl_values_code[] = {
  508. SCM_PACK_RTL_24 (scm_rtl_op_return_values, 0) /* vals from r1 */
  509. };
  510. /*
  511. * VM
  512. */
  513. static SCM
  514. resolve_variable (SCM what, SCM module)
  515. {
  516. if (SCM_LIKELY (scm_is_symbol (what)))
  517. {
  518. if (scm_is_true (module))
  519. return scm_module_lookup (module, what);
  520. else
  521. return scm_module_lookup (scm_the_root_module (), what);
  522. }
  523. else
  524. {
  525. SCM modname, sym, public;
  526. modname = SCM_CAR (what);
  527. sym = SCM_CADR (what);
  528. public = SCM_CADDR (what);
  529. if (scm_is_true (public))
  530. return scm_public_lookup (modname, sym);
  531. else
  532. return scm_private_lookup (modname, sym);
  533. }
  534. }
  535. #define VM_MIN_STACK_SIZE (1024)
  536. #define VM_DEFAULT_STACK_SIZE (64 * 1024)
  537. static size_t vm_stack_size = VM_DEFAULT_STACK_SIZE;
  538. static void
  539. initialize_default_stack_size (void)
  540. {
  541. int size = scm_getenv_int ("GUILE_STACK_SIZE", vm_stack_size);
  542. if (size >= VM_MIN_STACK_SIZE)
  543. vm_stack_size = size;
  544. }
  545. #define VM_NAME vm_regular_engine
  546. #define RTL_VM_NAME rtl_vm_regular_engine
  547. #define FUNC_NAME "vm-regular-engine"
  548. #define VM_ENGINE SCM_VM_REGULAR_ENGINE
  549. #include "vm-engine.c"
  550. #undef VM_NAME
  551. #undef RTL_VM_NAME
  552. #undef FUNC_NAME
  553. #undef VM_ENGINE
  554. #define VM_NAME vm_debug_engine
  555. #define RTL_VM_NAME rtl_vm_debug_engine
  556. #define FUNC_NAME "vm-debug-engine"
  557. #define VM_ENGINE SCM_VM_DEBUG_ENGINE
  558. #include "vm-engine.c"
  559. #undef VM_NAME
  560. #undef RTL_VM_NAME
  561. #undef FUNC_NAME
  562. #undef VM_ENGINE
  563. static const scm_t_vm_engine vm_engines[] =
  564. { vm_regular_engine, vm_debug_engine };
  565. #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN
  566. /* The GC "kind" for the VM stack. */
  567. static int vm_stack_gc_kind;
  568. #endif
  569. static SCM
  570. make_vm (void)
  571. #define FUNC_NAME "make_vm"
  572. {
  573. int i;
  574. struct scm_vm *vp;
  575. vp = scm_gc_malloc (sizeof (struct scm_vm), "vm");
  576. vp->stack_size= vm_stack_size;
  577. #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN
  578. vp->stack_base = (SCM *)
  579. GC_generic_malloc (vp->stack_size * sizeof (SCM), vm_stack_gc_kind);
  580. /* Keep a pointer to VP so that `vm_stack_mark ()' can know what the stack
  581. top is. */
  582. *vp->stack_base = SCM_PACK_POINTER (vp);
  583. vp->stack_base++;
  584. vp->stack_size--;
  585. #else
  586. vp->stack_base = scm_gc_malloc (vp->stack_size * sizeof (SCM),
  587. "stack-base");
  588. #endif
  589. #ifdef VM_ENABLE_STACK_NULLING
  590. memset (vp->stack_base, 0, vp->stack_size * sizeof (SCM));
  591. #endif
  592. vp->stack_limit = vp->stack_base + vp->stack_size - VM_STACK_RESERVE_SIZE;
  593. vp->ip = NULL;
  594. vp->sp = vp->stack_base - 1;
  595. vp->fp = NULL;
  596. vp->engine = vm_default_engine;
  597. vp->trace_level = 0;
  598. for (i = 0; i < SCM_VM_NUM_HOOKS; i++)
  599. vp->hooks[i] = SCM_BOOL_F;
  600. return scm_cell (scm_tc7_vm, (scm_t_bits)vp);
  601. }
  602. #undef FUNC_NAME
  603. #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN
  604. /* Mark the VM stack region between its base and its current top. */
  605. static struct GC_ms_entry *
  606. vm_stack_mark (GC_word *addr, struct GC_ms_entry *mark_stack_ptr,
  607. struct GC_ms_entry *mark_stack_limit, GC_word env)
  608. {
  609. GC_word *word;
  610. const struct scm_vm *vm;
  611. /* The first word of the VM stack should contain a pointer to the
  612. corresponding VM. */
  613. vm = * ((struct scm_vm **) addr);
  614. if (vm == NULL
  615. || (SCM *) addr != vm->stack_base - 1)
  616. /* ADDR must be a pointer to a free-list element, which we must ignore
  617. (see warning in <gc/gc_mark.h>). */
  618. return mark_stack_ptr;
  619. for (word = (GC_word *) vm->stack_base; word <= (GC_word *) vm->sp; word++)
  620. mark_stack_ptr = GC_MARK_AND_PUSH ((* (GC_word **) word),
  621. mark_stack_ptr, mark_stack_limit,
  622. NULL);
  623. return mark_stack_ptr;
  624. }
  625. #endif /* VM_ENABLE_PRECISE_STACK_GC_SCAN */
  626. SCM
  627. scm_c_vm_run (SCM vm, SCM program, SCM *argv, int nargs)
  628. {
  629. struct scm_vm *vp = SCM_VM_DATA (vm);
  630. SCM_CHECK_STACK;
  631. return vm_engines[vp->engine](vm, program, argv, nargs);
  632. }
  633. /* Scheme interface */
  634. SCM_DEFINE (scm_the_vm, "the-vm", 0, 0, 0,
  635. (void),
  636. "Return the current thread's VM.")
  637. #define FUNC_NAME s_scm_the_vm
  638. {
  639. scm_i_thread *t = SCM_I_CURRENT_THREAD;
  640. if (SCM_UNLIKELY (scm_is_false (t->vm)))
  641. t->vm = make_vm ();
  642. return t->vm;
  643. }
  644. #undef FUNC_NAME
  645. SCM_DEFINE (scm_vm_p, "vm?", 1, 0, 0,
  646. (SCM obj),
  647. "")
  648. #define FUNC_NAME s_scm_vm_p
  649. {
  650. return scm_from_bool (SCM_VM_P (obj));
  651. }
  652. #undef FUNC_NAME
  653. SCM_DEFINE (scm_make_vm, "make-vm", 0, 0, 0,
  654. (void),
  655. "")
  656. #define FUNC_NAME s_scm_make_vm,
  657. {
  658. return make_vm ();
  659. }
  660. #undef FUNC_NAME
  661. SCM_DEFINE (scm_vm_ip, "vm:ip", 1, 0, 0,
  662. (SCM vm),
  663. "")
  664. #define FUNC_NAME s_scm_vm_ip
  665. {
  666. SCM_VALIDATE_VM (1, vm);
  667. return scm_from_unsigned_integer ((scm_t_bits) SCM_VM_DATA (vm)->ip);
  668. }
  669. #undef FUNC_NAME
  670. SCM_DEFINE (scm_vm_sp, "vm:sp", 1, 0, 0,
  671. (SCM vm),
  672. "")
  673. #define FUNC_NAME s_scm_vm_sp
  674. {
  675. SCM_VALIDATE_VM (1, vm);
  676. return scm_from_unsigned_integer ((scm_t_bits) SCM_VM_DATA (vm)->sp);
  677. }
  678. #undef FUNC_NAME
  679. SCM_DEFINE (scm_vm_fp, "vm:fp", 1, 0, 0,
  680. (SCM vm),
  681. "")
  682. #define FUNC_NAME s_scm_vm_fp
  683. {
  684. SCM_VALIDATE_VM (1, vm);
  685. return scm_from_unsigned_integer ((scm_t_bits) SCM_VM_DATA (vm)->fp);
  686. }
  687. #undef FUNC_NAME
  688. #define VM_DEFINE_HOOK(n) \
  689. { \
  690. struct scm_vm *vp; \
  691. SCM_VALIDATE_VM (1, vm); \
  692. vp = SCM_VM_DATA (vm); \
  693. if (scm_is_false (vp->hooks[n])) \
  694. vp->hooks[n] = scm_make_hook (SCM_I_MAKINUM (1)); \
  695. return vp->hooks[n]; \
  696. }
  697. SCM_DEFINE (scm_vm_apply_hook, "vm-apply-hook", 1, 0, 0,
  698. (SCM vm),
  699. "")
  700. #define FUNC_NAME s_scm_vm_apply_hook
  701. {
  702. VM_DEFINE_HOOK (SCM_VM_APPLY_HOOK);
  703. }
  704. #undef FUNC_NAME
  705. SCM_DEFINE (scm_vm_push_continuation_hook, "vm-push-continuation-hook", 1, 0, 0,
  706. (SCM vm),
  707. "")
  708. #define FUNC_NAME s_scm_vm_push_continuation_hook
  709. {
  710. VM_DEFINE_HOOK (SCM_VM_PUSH_CONTINUATION_HOOK);
  711. }
  712. #undef FUNC_NAME
  713. SCM_DEFINE (scm_vm_pop_continuation_hook, "vm-pop-continuation-hook", 1, 0, 0,
  714. (SCM vm),
  715. "")
  716. #define FUNC_NAME s_scm_vm_pop_continuation_hook
  717. {
  718. VM_DEFINE_HOOK (SCM_VM_POP_CONTINUATION_HOOK);
  719. }
  720. #undef FUNC_NAME
  721. SCM_DEFINE (scm_vm_next_hook, "vm-next-hook", 1, 0, 0,
  722. (SCM vm),
  723. "")
  724. #define FUNC_NAME s_scm_vm_next_hook
  725. {
  726. VM_DEFINE_HOOK (SCM_VM_NEXT_HOOK);
  727. }
  728. #undef FUNC_NAME
  729. SCM_DEFINE (scm_vm_abort_continuation_hook, "vm-abort-continuation-hook", 1, 0, 0,
  730. (SCM vm),
  731. "")
  732. #define FUNC_NAME s_scm_vm_abort_continuation_hook
  733. {
  734. VM_DEFINE_HOOK (SCM_VM_ABORT_CONTINUATION_HOOK);
  735. }
  736. #undef FUNC_NAME
  737. SCM_DEFINE (scm_vm_restore_continuation_hook, "vm-restore-continuation-hook", 1, 0, 0,
  738. (SCM vm),
  739. "")
  740. #define FUNC_NAME s_scm_vm_restore_continuation_hook
  741. {
  742. VM_DEFINE_HOOK (SCM_VM_RESTORE_CONTINUATION_HOOK);
  743. }
  744. #undef FUNC_NAME
  745. SCM_DEFINE (scm_vm_trace_level, "vm-trace-level", 1, 0, 0,
  746. (SCM vm),
  747. "")
  748. #define FUNC_NAME s_scm_vm_trace_level
  749. {
  750. SCM_VALIDATE_VM (1, vm);
  751. return scm_from_int (SCM_VM_DATA (vm)->trace_level);
  752. }
  753. #undef FUNC_NAME
  754. SCM_DEFINE (scm_set_vm_trace_level_x, "set-vm-trace-level!", 2, 0, 0,
  755. (SCM vm, SCM level),
  756. "")
  757. #define FUNC_NAME s_scm_set_vm_trace_level_x
  758. {
  759. SCM_VALIDATE_VM (1, vm);
  760. SCM_VM_DATA (vm)->trace_level = scm_to_int (level);
  761. return SCM_UNSPECIFIED;
  762. }
  763. #undef FUNC_NAME
  764. /*
  765. * VM engines
  766. */
  767. static int
  768. symbol_to_vm_engine (SCM engine, const char *FUNC_NAME)
  769. {
  770. if (scm_is_eq (engine, sym_regular))
  771. return SCM_VM_REGULAR_ENGINE;
  772. else if (scm_is_eq (engine, sym_debug))
  773. return SCM_VM_DEBUG_ENGINE;
  774. else
  775. SCM_MISC_ERROR ("Unknown VM engine: ~a", scm_list_1 (engine));
  776. }
  777. static SCM
  778. vm_engine_to_symbol (int engine, const char *FUNC_NAME)
  779. {
  780. switch (engine)
  781. {
  782. case SCM_VM_REGULAR_ENGINE:
  783. return sym_regular;
  784. case SCM_VM_DEBUG_ENGINE:
  785. return sym_debug;
  786. default:
  787. /* ? */
  788. SCM_MISC_ERROR ("Unknown VM engine: ~a",
  789. scm_list_1 (scm_from_int (engine)));
  790. }
  791. }
  792. SCM_DEFINE (scm_vm_engine, "vm-engine", 1, 0, 0,
  793. (SCM vm),
  794. "")
  795. #define FUNC_NAME s_scm_vm_engine
  796. {
  797. SCM_VALIDATE_VM (1, vm);
  798. return vm_engine_to_symbol (SCM_VM_DATA (vm)->engine, FUNC_NAME);
  799. }
  800. #undef FUNC_NAME
  801. void
  802. scm_c_set_vm_engine_x (SCM vm, int engine)
  803. #define FUNC_NAME "set-vm-engine!"
  804. {
  805. SCM_VALIDATE_VM (1, vm);
  806. if (engine < 0 || engine >= SCM_VM_NUM_ENGINES)
  807. SCM_MISC_ERROR ("Unknown VM engine: ~a",
  808. scm_list_1 (scm_from_int (engine)));
  809. SCM_VM_DATA (vm)->engine = engine;
  810. }
  811. #undef FUNC_NAME
  812. SCM_DEFINE (scm_set_vm_engine_x, "set-vm-engine!", 2, 0, 0,
  813. (SCM vm, SCM engine),
  814. "")
  815. #define FUNC_NAME s_scm_set_vm_engine_x
  816. {
  817. scm_c_set_vm_engine_x (vm, symbol_to_vm_engine (engine, FUNC_NAME));
  818. return SCM_UNSPECIFIED;
  819. }
  820. #undef FUNC_NAME
  821. void
  822. scm_c_set_default_vm_engine_x (int engine)
  823. #define FUNC_NAME "set-default-vm-engine!"
  824. {
  825. if (engine < 0 || engine >= SCM_VM_NUM_ENGINES)
  826. SCM_MISC_ERROR ("Unknown VM engine: ~a",
  827. scm_list_1 (scm_from_int (engine)));
  828. vm_default_engine = engine;
  829. }
  830. #undef FUNC_NAME
  831. SCM_DEFINE (scm_set_default_vm_engine_x, "set-default-vm-engine!", 1, 0, 0,
  832. (SCM engine),
  833. "")
  834. #define FUNC_NAME s_scm_set_default_vm_engine_x
  835. {
  836. scm_c_set_default_vm_engine_x (symbol_to_vm_engine (engine, FUNC_NAME));
  837. return SCM_UNSPECIFIED;
  838. }
  839. #undef FUNC_NAME
  840. static void reinstate_vm (SCM vm)
  841. {
  842. scm_i_thread *t = SCM_I_CURRENT_THREAD;
  843. t->vm = vm;
  844. }
  845. SCM_DEFINE (scm_call_with_vm, "call-with-vm", 2, 0, 1,
  846. (SCM vm, SCM proc, SCM args),
  847. "Apply @var{proc} to @var{args} in a dynamic extent in which\n"
  848. "@var{vm} is the current VM.\n\n"
  849. "As an implementation restriction, if @var{vm} is not the same\n"
  850. "as the current thread's VM, continuations captured within the\n"
  851. "call to @var{proc} may not be reinstated once control leaves\n"
  852. "@var{proc}.")
  853. #define FUNC_NAME s_scm_call_with_vm
  854. {
  855. SCM prev_vm, ret;
  856. SCM *argv;
  857. int i, nargs;
  858. scm_t_wind_flags flags;
  859. scm_i_thread *t = SCM_I_CURRENT_THREAD;
  860. SCM_VALIDATE_VM (1, vm);
  861. SCM_VALIDATE_PROC (2, proc);
  862. nargs = scm_ilength (args);
  863. if (SCM_UNLIKELY (nargs < 0))
  864. scm_wrong_type_arg_msg (FUNC_NAME, 3, args, "list");
  865. argv = alloca (nargs * sizeof(SCM));
  866. for (i = 0; i < nargs; i++)
  867. {
  868. argv[i] = SCM_CAR (args);
  869. args = SCM_CDR (args);
  870. }
  871. prev_vm = t->vm;
  872. /* Reentry can happen via invokation of a saved continuation, but
  873. continuations only save the state of the VM that they are in at
  874. capture-time, which might be different from this one. So, in the
  875. case that the VMs are different, set up a non-rewindable frame to
  876. prevent reinstating an incomplete continuation. */
  877. flags = scm_is_eq (prev_vm, vm) ? 0 : SCM_F_WIND_EXPLICITLY;
  878. if (flags)
  879. {
  880. scm_dynwind_begin (0);
  881. scm_dynwind_unwind_handler_with_scm (reinstate_vm, prev_vm, flags);
  882. t->vm = vm;
  883. }
  884. ret = scm_c_vm_run (vm, proc, argv, nargs);
  885. if (flags)
  886. scm_dynwind_end ();
  887. return ret;
  888. }
  889. #undef FUNC_NAME
  890. /*
  891. * Initialize
  892. */
  893. SCM scm_load_compiled_with_vm (SCM file)
  894. {
  895. SCM program = scm_load_thunk_from_file (file);
  896. return scm_c_vm_run (scm_the_vm (), program, NULL, 0);
  897. }
  898. static SCM
  899. make_boot_program (void)
  900. {
  901. struct scm_objcode *bp;
  902. size_t bp_size;
  903. SCM u8vec, ret;
  904. const scm_t_uint8 text[] = {
  905. scm_op_make_int8_1,
  906. scm_op_halt
  907. };
  908. bp_size = sizeof (struct scm_objcode) + sizeof (text);
  909. bp = scm_gc_malloc_pointerless (bp_size, "boot-program");
  910. memcpy (SCM_C_OBJCODE_BASE (bp), text, sizeof (text));
  911. bp->len = sizeof(text);
  912. bp->metalen = 0;
  913. u8vec = scm_c_take_gc_bytevector ((scm_t_int8*)bp, bp_size, SCM_BOOL_F);
  914. ret = scm_make_program (scm_bytecode_to_objcode (u8vec, SCM_UNDEFINED),
  915. SCM_BOOL_F, SCM_BOOL_F);
  916. SCM_SET_CELL_WORD_0 (ret, (SCM_CELL_WORD_0 (ret) | SCM_F_PROGRAM_IS_BOOT));
  917. return ret;
  918. }
  919. void
  920. scm_bootstrap_vm (void)
  921. {
  922. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  923. "scm_init_vm",
  924. (scm_t_extension_init_func)scm_init_vm, NULL);
  925. initialize_default_stack_size ();
  926. sym_vm_run = scm_from_latin1_symbol ("vm-run");
  927. sym_vm_error = scm_from_latin1_symbol ("vm-error");
  928. sym_keyword_argument_error = scm_from_latin1_symbol ("keyword-argument-error");
  929. sym_regular = scm_from_latin1_symbol ("regular");
  930. sym_debug = scm_from_latin1_symbol ("debug");
  931. boot_continuation = make_boot_program ();
  932. #ifdef VM_ENABLE_PRECISE_STACK_GC_SCAN
  933. vm_stack_gc_kind =
  934. GC_new_kind (GC_new_free_list (),
  935. GC_MAKE_PROC (GC_new_proc (vm_stack_mark), 0),
  936. 0, 1);
  937. #endif
  938. }
  939. void
  940. scm_init_vm (void)
  941. {
  942. #ifndef SCM_MAGIC_SNARFER
  943. #include "libguile/vm.x"
  944. #endif
  945. rtl_boot_continuation = scm_i_make_rtl_program (rtl_boot_continuation_code);
  946. rtl_apply = scm_i_make_rtl_program (rtl_apply_code);
  947. rtl_values = scm_i_make_rtl_program (rtl_values_code);
  948. }
  949. /*
  950. Local Variables:
  951. c-file-style: "gnu"
  952. End:
  953. */