vm.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  1. /* Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2018 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 <unistd.h>
  27. #ifdef HAVE_SYS_MMAN_H
  28. #include <sys/mman.h>
  29. #endif
  30. #include "libguile/bdw-gc.h"
  31. #include <gc/gc_mark.h>
  32. #include "libguile/_scm.h"
  33. #include "libguile/atomic.h"
  34. #include "libguile/atomics-internal.h"
  35. #include "libguile/cache-internal.h"
  36. #include "libguile/control.h"
  37. #include "libguile/frames.h"
  38. #include "libguile/gc-inline.h"
  39. #include "libguile/instructions.h"
  40. #include "libguile/loader.h"
  41. #include "libguile/programs.h"
  42. #include "libguile/simpos.h"
  43. #include "libguile/vm.h"
  44. #include "libguile/vm-builtins.h"
  45. static int vm_default_engine = SCM_VM_REGULAR_ENGINE;
  46. /* Unfortunately we can't snarf these: snarfed things are only loaded up from
  47. (system vm vm), which might not be loaded before an error happens. */
  48. static SCM sym_vm_run;
  49. static SCM sym_vm_error;
  50. static SCM sym_keyword_argument_error;
  51. static SCM sym_regular;
  52. static SCM sym_debug;
  53. /* The page size. */
  54. static size_t page_size;
  55. /* The VM has a number of internal assertions that shouldn't normally be
  56. necessary, but might be if you think you found a bug in the VM. */
  57. /* #define VM_ENABLE_ASSERTIONS */
  58. static void vm_expand_stack (struct scm_vm *vp,
  59. union scm_vm_stack_element *new_sp) SCM_NOINLINE;
  60. /* RESTORE is for the case where we know we have done a PUSH of equal or
  61. greater stack size in the past. Otherwise PUSH is the thing, which
  62. may expand the stack. */
  63. enum vm_increase_sp_kind { VM_SP_PUSH, VM_SP_RESTORE };
  64. static inline void
  65. vm_increase_sp (struct scm_vm *vp, union scm_vm_stack_element *new_sp,
  66. enum vm_increase_sp_kind kind)
  67. {
  68. if (new_sp >= vp->sp_min_since_gc)
  69. {
  70. vp->sp = new_sp;
  71. return;
  72. }
  73. if (kind == VM_SP_PUSH && new_sp < vp->stack_limit)
  74. vm_expand_stack (vp, new_sp);
  75. else
  76. vp->sp_min_since_gc = vp->sp = new_sp;
  77. }
  78. static inline void
  79. vm_push_sp (struct scm_vm *vp, union scm_vm_stack_element *new_sp)
  80. {
  81. vm_increase_sp (vp, new_sp, VM_SP_PUSH);
  82. }
  83. static inline void
  84. vm_restore_sp (struct scm_vm *vp, union scm_vm_stack_element *new_sp)
  85. {
  86. vm_increase_sp (vp, new_sp, VM_SP_RESTORE);
  87. }
  88. /*
  89. * VM Continuation
  90. */
  91. void
  92. scm_i_vm_cont_print (SCM x, SCM port, scm_print_state *pstate)
  93. {
  94. scm_puts ("#<vm-continuation ", port);
  95. scm_uintprint (SCM_UNPACK (x), 16, port);
  96. scm_puts (">", port);
  97. }
  98. int
  99. scm_i_vm_cont_to_frame (SCM cont, struct scm_frame *frame)
  100. {
  101. struct scm_vm_cont *data = SCM_VM_CONT_DATA (cont);
  102. frame->stack_holder = data;
  103. frame->fp_offset = data->fp_offset;
  104. frame->sp_offset = data->stack_size;
  105. frame->ip = data->ra;
  106. return 1;
  107. }
  108. /* Ideally we could avoid copying the C stack if the continuation root
  109. is inside VM code, and call/cc was invoked within that same call to
  110. vm_run. That's currently not implemented. */
  111. SCM
  112. scm_i_vm_capture_stack (union scm_vm_stack_element *stack_top,
  113. union scm_vm_stack_element *fp,
  114. union scm_vm_stack_element *sp, scm_t_uint32 *ra,
  115. scm_t_dynstack *dynstack, scm_t_uint32 flags)
  116. {
  117. struct scm_vm_cont *p;
  118. p = scm_gc_malloc (sizeof (*p), "capture_vm_cont");
  119. p->stack_size = stack_top - sp;
  120. p->stack_bottom = scm_gc_malloc (p->stack_size * sizeof (*p->stack_bottom),
  121. "capture_vm_cont");
  122. p->ra = ra;
  123. p->fp_offset = stack_top - fp;
  124. memcpy (p->stack_bottom, sp, p->stack_size * sizeof (*p->stack_bottom));
  125. p->dynstack = dynstack;
  126. p->flags = flags;
  127. return scm_cell (scm_tc7_vm_cont, (scm_t_bits) p);
  128. }
  129. struct return_to_continuation_data
  130. {
  131. struct scm_vm_cont *cp;
  132. struct scm_vm *vp;
  133. };
  134. /* Called with the GC lock to prevent the stack marker from traversing a
  135. stack in an inconsistent state. */
  136. static void *
  137. vm_return_to_continuation_inner (void *data_ptr)
  138. {
  139. struct return_to_continuation_data *data = data_ptr;
  140. struct scm_vm *vp = data->vp;
  141. struct scm_vm_cont *cp = data->cp;
  142. /* We know that there is enough space for the continuation, because we
  143. captured it in the past. However there may have been an expansion
  144. since the capture, so we may have to re-link the frame
  145. pointers. */
  146. memcpy (vp->stack_top - cp->stack_size,
  147. cp->stack_bottom,
  148. cp->stack_size * sizeof (*cp->stack_bottom));
  149. vp->fp = vp->stack_top - cp->fp_offset;
  150. vm_restore_sp (vp, vp->stack_top - cp->stack_size);
  151. return NULL;
  152. }
  153. static void
  154. vm_return_to_continuation (struct scm_vm *vp, SCM cont, size_t n,
  155. union scm_vm_stack_element *argv)
  156. {
  157. struct scm_vm_cont *cp;
  158. union scm_vm_stack_element *argv_copy;
  159. struct return_to_continuation_data data;
  160. argv_copy = alloca (n * sizeof (*argv));
  161. memcpy (argv_copy, argv, n * sizeof (*argv));
  162. cp = SCM_VM_CONT_DATA (cont);
  163. data.cp = cp;
  164. data.vp = vp;
  165. GC_call_with_alloc_lock (vm_return_to_continuation_inner, &data);
  166. /* Now we have the continuation properly copied over. We just need to
  167. copy on an empty frame and the return values, as the continuation
  168. expects. */
  169. vm_push_sp (vp, vp->sp - 3 - n);
  170. vp->sp[n+2].as_scm = SCM_BOOL_F;
  171. vp->sp[n+1].as_scm = SCM_BOOL_F;
  172. vp->sp[n].as_scm = SCM_BOOL_F;
  173. memcpy(vp->sp, argv_copy, n * sizeof (union scm_vm_stack_element));
  174. vp->ip = cp->ra;
  175. }
  176. static struct scm_vm * thread_vm (scm_i_thread *t);
  177. SCM
  178. scm_i_capture_current_stack (void)
  179. {
  180. scm_i_thread *thread;
  181. struct scm_vm *vp;
  182. thread = SCM_I_CURRENT_THREAD;
  183. vp = thread_vm (thread);
  184. return scm_i_vm_capture_stack (vp->stack_top, vp->fp, vp->sp, vp->ip,
  185. scm_dynstack_capture_all (&thread->dynstack),
  186. 0);
  187. }
  188. static void vm_dispatch_apply_hook (struct scm_vm *vp) SCM_NOINLINE;
  189. static void vm_dispatch_push_continuation_hook (struct scm_vm *vp) SCM_NOINLINE;
  190. static void vm_dispatch_pop_continuation_hook
  191. (struct scm_vm *vp, union scm_vm_stack_element *old_fp) SCM_NOINLINE;
  192. static void vm_dispatch_next_hook (struct scm_vm *vp) SCM_NOINLINE;
  193. static void vm_dispatch_abort_hook (struct scm_vm *vp) SCM_NOINLINE;
  194. static void
  195. vm_dispatch_hook (struct scm_vm *vp, int hook_num,
  196. union scm_vm_stack_element *argv, int n)
  197. {
  198. SCM hook;
  199. struct scm_frame c_frame;
  200. scm_t_cell *frame;
  201. int saved_trace_level;
  202. hook = vp->hooks[hook_num];
  203. if (SCM_LIKELY (scm_is_false (hook))
  204. || scm_is_null (SCM_HOOK_PROCEDURES (hook)))
  205. return;
  206. saved_trace_level = vp->trace_level;
  207. vp->trace_level = 0;
  208. /* Allocate a frame object on the stack. This is more efficient than calling
  209. `scm_c_make_frame ()' to allocate on the heap, but it forces hooks to not
  210. capture frame objects.
  211. At the same time, procedures such as `frame-procedure' make sense only
  212. while the stack frame represented by the frame object is visible, so it
  213. seems reasonable to limit the lifetime of frame objects. */
  214. c_frame.stack_holder = vp;
  215. c_frame.fp_offset = vp->stack_top - vp->fp;
  216. c_frame.sp_offset = vp->stack_top - vp->sp;
  217. c_frame.ip = vp->ip;
  218. /* Arrange for FRAME to be 8-byte aligned, like any other cell. */
  219. frame = alloca (sizeof (*frame) + 8);
  220. frame = (scm_t_cell *) ROUND_UP ((scm_t_uintptr) frame, 8UL);
  221. frame->word_0 = SCM_PACK (scm_tc7_frame | (SCM_VM_FRAME_KIND_VM << 8));
  222. frame->word_1 = SCM_PACK_POINTER (&c_frame);
  223. if (n == 0)
  224. {
  225. SCM args[1];
  226. args[0] = SCM_PACK_POINTER (frame);
  227. scm_c_run_hookn (hook, args, 1);
  228. }
  229. else if (n == 1)
  230. {
  231. SCM args[2];
  232. args[0] = SCM_PACK_POINTER (frame);
  233. args[1] = argv[0].as_scm;
  234. scm_c_run_hookn (hook, args, 2);
  235. }
  236. else
  237. {
  238. SCM args = SCM_EOL;
  239. int i;
  240. for (i = 0; i < n; i++)
  241. args = scm_cons (argv[i].as_scm, args);
  242. scm_c_run_hook (hook, scm_cons (SCM_PACK_POINTER (frame), args));
  243. }
  244. vp->trace_level = saved_trace_level;
  245. }
  246. static void
  247. vm_dispatch_apply_hook (struct scm_vm *vp)
  248. {
  249. return vm_dispatch_hook (vp, SCM_VM_APPLY_HOOK, NULL, 0);
  250. }
  251. static void vm_dispatch_push_continuation_hook (struct scm_vm *vp)
  252. {
  253. return vm_dispatch_hook (vp, SCM_VM_PUSH_CONTINUATION_HOOK, NULL, 0);
  254. }
  255. static void vm_dispatch_pop_continuation_hook (struct scm_vm *vp,
  256. union scm_vm_stack_element *old_fp)
  257. {
  258. return vm_dispatch_hook (vp, SCM_VM_POP_CONTINUATION_HOOK,
  259. vp->sp, SCM_FRAME_NUM_LOCALS (old_fp, vp->sp) - 1);
  260. }
  261. static void vm_dispatch_next_hook (struct scm_vm *vp)
  262. {
  263. return vm_dispatch_hook (vp, SCM_VM_NEXT_HOOK, NULL, 0);
  264. }
  265. static void vm_dispatch_abort_hook (struct scm_vm *vp)
  266. {
  267. return vm_dispatch_hook (vp, SCM_VM_ABORT_CONTINUATION_HOOK,
  268. vp->sp, SCM_FRAME_NUM_LOCALS (vp->fp, vp->sp) - 1);
  269. }
  270. static void
  271. vm_abort (struct scm_vm *vp, SCM tag, size_t nargs,
  272. scm_i_jmp_buf *current_registers) SCM_NORETURN;
  273. static void
  274. vm_abort (struct scm_vm *vp, SCM tag, size_t nargs,
  275. scm_i_jmp_buf *current_registers)
  276. {
  277. size_t i;
  278. SCM *argv;
  279. argv = alloca (nargs * sizeof (SCM));
  280. for (i = 0; i < nargs; i++)
  281. argv[i] = vp->sp[nargs - i - 1].as_scm;
  282. vp->sp = vp->fp;
  283. scm_c_abort (vp, tag, nargs, argv, current_registers);
  284. }
  285. struct vm_reinstate_partial_continuation_data
  286. {
  287. struct scm_vm *vp;
  288. struct scm_vm_cont *cp;
  289. };
  290. static void *
  291. vm_reinstate_partial_continuation_inner (void *data_ptr)
  292. {
  293. struct vm_reinstate_partial_continuation_data *data = data_ptr;
  294. struct scm_vm *vp = data->vp;
  295. struct scm_vm_cont *cp = data->cp;
  296. memcpy (vp->fp - cp->stack_size,
  297. cp->stack_bottom,
  298. cp->stack_size * sizeof (*cp->stack_bottom));
  299. vp->fp -= cp->fp_offset;
  300. vp->ip = cp->ra;
  301. return NULL;
  302. }
  303. static void
  304. vm_reinstate_partial_continuation (struct scm_vm *vp, SCM cont, size_t nargs,
  305. scm_t_dynstack *dynstack,
  306. scm_i_jmp_buf *registers)
  307. {
  308. struct vm_reinstate_partial_continuation_data data;
  309. struct scm_vm_cont *cp;
  310. union scm_vm_stack_element *args;
  311. scm_t_ptrdiff old_fp_offset;
  312. args = alloca (nargs * sizeof (*args));
  313. memcpy (args, vp->sp, nargs * sizeof (*args));
  314. cp = SCM_VM_CONT_DATA (cont);
  315. old_fp_offset = vp->stack_top - vp->fp;
  316. vm_push_sp (vp, vp->fp - (cp->stack_size + nargs + 1));
  317. data.vp = vp;
  318. data.cp = cp;
  319. GC_call_with_alloc_lock (vm_reinstate_partial_continuation_inner, &data);
  320. /* The resume continuation will expect ARGS on the stack as if from a
  321. multiple-value return. Fill in the closure slot with #f, and copy
  322. the arguments into place. */
  323. vp->sp[nargs].as_scm = SCM_BOOL_F;
  324. memcpy (vp->sp, args, nargs * sizeof (*args));
  325. /* The prompt captured a slice of the dynamic stack. Here we wind
  326. those entries onto the current thread's stack. We also have to
  327. relocate any prompts that we see along the way. */
  328. {
  329. scm_t_bits *walk;
  330. for (walk = SCM_DYNSTACK_FIRST (cp->dynstack);
  331. SCM_DYNSTACK_TAG (walk);
  332. walk = SCM_DYNSTACK_NEXT (walk))
  333. {
  334. scm_t_bits tag = SCM_DYNSTACK_TAG (walk);
  335. if (SCM_DYNSTACK_TAG_TYPE (tag) == SCM_DYNSTACK_TYPE_PROMPT)
  336. scm_dynstack_wind_prompt (dynstack, walk, old_fp_offset, registers);
  337. else
  338. scm_dynstack_wind_1 (dynstack, walk);
  339. }
  340. }
  341. }
  342. /*
  343. * VM Error Handling
  344. */
  345. static void vm_error (const char *msg, SCM arg) SCM_NORETURN;
  346. static void vm_error_bad_instruction (scm_t_uint32 inst) SCM_NORETURN SCM_NOINLINE;
  347. static void vm_error_unbound (SCM sym) SCM_NORETURN SCM_NOINLINE;
  348. static void vm_error_not_a_variable (const char *func_name, SCM x) SCM_NORETURN SCM_NOINLINE;
  349. static void vm_error_apply_to_non_list (SCM x) SCM_NORETURN SCM_NOINLINE;
  350. static void vm_error_kwargs_missing_value (SCM proc, SCM kw) SCM_NORETURN SCM_NOINLINE;
  351. static void vm_error_kwargs_invalid_keyword (SCM proc, SCM obj) SCM_NORETURN SCM_NOINLINE;
  352. static void vm_error_kwargs_unrecognized_keyword (SCM proc, SCM kw) SCM_NORETURN SCM_NOINLINE;
  353. static void vm_error_wrong_num_args (SCM proc) SCM_NORETURN SCM_NOINLINE;
  354. static void vm_error_wrong_type_apply (SCM proc) SCM_NORETURN SCM_NOINLINE;
  355. static void vm_error_not_a_char (const char *subr, SCM x) SCM_NORETURN SCM_NOINLINE;
  356. static void vm_error_not_a_pair (const char *subr, SCM x) SCM_NORETURN SCM_NOINLINE;
  357. static void vm_error_not_a_mutable_pair (const char *subr, SCM x) SCM_NORETURN SCM_NOINLINE;
  358. static void vm_error_not_a_string (const char *subr, SCM x) SCM_NORETURN SCM_NOINLINE;
  359. static void vm_error_not_a_atomic_box (const char *subr, SCM x) SCM_NORETURN SCM_NOINLINE;
  360. static void vm_error_not_a_bytevector (const char *subr, SCM x) SCM_NORETURN SCM_NOINLINE;
  361. static void vm_error_not_a_mutable_bytevector (const char *subr, SCM v) SCM_NORETURN SCM_NOINLINE;
  362. static void vm_error_not_a_struct (const char *subr, SCM x) SCM_NORETURN SCM_NOINLINE;
  363. static void vm_error_not_a_vector (const char *subr, SCM v) SCM_NORETURN SCM_NOINLINE;
  364. static void vm_error_not_a_mutable_vector (const char *subr, SCM v) SCM_NORETURN SCM_NOINLINE;
  365. static void vm_error_out_of_range_uint64 (const char *subr, scm_t_uint64 idx) SCM_NORETURN SCM_NOINLINE;
  366. static void vm_error_out_of_range_int64 (const char *subr, scm_t_int64 idx) SCM_NORETURN SCM_NOINLINE;
  367. static void vm_error_no_values (void) SCM_NORETURN SCM_NOINLINE;
  368. static void vm_error_not_enough_values (void) SCM_NORETURN SCM_NOINLINE;
  369. static void vm_error_wrong_number_of_values (scm_t_uint32 expected) SCM_NORETURN SCM_NOINLINE;
  370. static void vm_error_continuation_not_rewindable (SCM cont) SCM_NORETURN SCM_NOINLINE;
  371. static void
  372. vm_error (const char *msg, SCM arg)
  373. {
  374. scm_throw (sym_vm_error,
  375. scm_list_3 (sym_vm_run, scm_from_latin1_string (msg),
  376. SCM_UNBNDP (arg) ? SCM_EOL : scm_list_1 (arg)));
  377. abort(); /* not reached */
  378. }
  379. static void
  380. vm_error_bad_instruction (scm_t_uint32 inst)
  381. {
  382. vm_error ("VM: Bad instruction: ~s", scm_from_uint32 (inst));
  383. }
  384. static void
  385. vm_error_unbound (SCM sym)
  386. {
  387. scm_error_scm (scm_misc_error_key, SCM_BOOL_F,
  388. scm_from_latin1_string ("Unbound variable: ~s"),
  389. scm_list_1 (sym), SCM_BOOL_F);
  390. }
  391. static void
  392. vm_error_not_a_variable (const char *func_name, SCM x)
  393. {
  394. scm_error (scm_arg_type_key, func_name, "Not a variable: ~S",
  395. scm_list_1 (x), scm_list_1 (x));
  396. }
  397. static void
  398. vm_error_apply_to_non_list (SCM x)
  399. {
  400. scm_error (scm_arg_type_key, "apply", "Apply to non-list: ~S",
  401. scm_list_1 (x), scm_list_1 (x));
  402. }
  403. static void
  404. vm_error_kwargs_missing_value (SCM proc, SCM kw)
  405. {
  406. scm_error_scm (sym_keyword_argument_error, proc,
  407. scm_from_latin1_string ("Keyword argument has no value"),
  408. SCM_EOL, scm_list_1 (kw));
  409. }
  410. static void
  411. vm_error_kwargs_invalid_keyword (SCM proc, SCM obj)
  412. {
  413. scm_error_scm (sym_keyword_argument_error, proc,
  414. scm_from_latin1_string ("Invalid keyword"),
  415. SCM_EOL, scm_list_1 (obj));
  416. }
  417. static void
  418. vm_error_kwargs_unrecognized_keyword (SCM proc, SCM kw)
  419. {
  420. scm_error_scm (sym_keyword_argument_error, proc,
  421. scm_from_latin1_string ("Unrecognized keyword"),
  422. SCM_EOL, scm_list_1 (kw));
  423. }
  424. static void
  425. vm_error_wrong_num_args (SCM proc)
  426. {
  427. scm_wrong_num_args (proc);
  428. }
  429. static void
  430. vm_error_wrong_type_apply (SCM proc)
  431. {
  432. scm_error (scm_arg_type_key, NULL, "Wrong type to apply: ~S",
  433. scm_list_1 (proc), scm_list_1 (proc));
  434. }
  435. static void
  436. vm_error_not_a_char (const char *subr, SCM x)
  437. {
  438. scm_wrong_type_arg_msg (subr, 1, x, "char");
  439. }
  440. static void
  441. vm_error_not_a_pair (const char *subr, SCM x)
  442. {
  443. scm_wrong_type_arg_msg (subr, 1, x, "pair");
  444. }
  445. static void
  446. vm_error_not_a_mutable_pair (const char *subr, SCM x)
  447. {
  448. scm_wrong_type_arg_msg (subr, 1, x, "mutable pair");
  449. }
  450. static void
  451. vm_error_not_a_string (const char *subr, SCM x)
  452. {
  453. scm_wrong_type_arg_msg (subr, 1, x, "string");
  454. }
  455. static void
  456. vm_error_not_a_atomic_box (const char *subr, SCM x)
  457. {
  458. scm_wrong_type_arg_msg (subr, 1, x, "atomic box");
  459. }
  460. static void
  461. vm_error_not_a_bytevector (const char *subr, SCM x)
  462. {
  463. scm_wrong_type_arg_msg (subr, 1, x, "bytevector");
  464. }
  465. static void
  466. vm_error_not_a_mutable_bytevector (const char *subr, SCM x)
  467. {
  468. scm_wrong_type_arg_msg (subr, 1, x, "mutable bytevector");
  469. }
  470. static void
  471. vm_error_not_a_struct (const char *subr, SCM x)
  472. {
  473. scm_wrong_type_arg_msg (subr, 1, x, "struct");
  474. }
  475. static void
  476. vm_error_not_a_vector (const char *subr, SCM x)
  477. {
  478. scm_wrong_type_arg_msg (subr, 1, x, "vector");
  479. }
  480. static void
  481. vm_error_not_a_mutable_vector (const char *subr, SCM x)
  482. {
  483. scm_wrong_type_arg_msg (subr, 1, x, "mutable vector");
  484. }
  485. static void
  486. vm_error_out_of_range_uint64 (const char *subr, scm_t_uint64 idx)
  487. {
  488. scm_out_of_range (subr, scm_from_uint64 (idx));
  489. }
  490. static void
  491. vm_error_out_of_range_int64 (const char *subr, scm_t_int64 idx)
  492. {
  493. scm_out_of_range (subr, scm_from_int64 (idx));
  494. }
  495. static void
  496. vm_error_no_values (void)
  497. {
  498. vm_error ("Zero values returned to single-valued continuation",
  499. SCM_UNDEFINED);
  500. }
  501. static void
  502. vm_error_not_enough_values (void)
  503. {
  504. vm_error ("Too few values returned to continuation", SCM_UNDEFINED);
  505. }
  506. static void
  507. vm_error_wrong_number_of_values (scm_t_uint32 expected)
  508. {
  509. vm_error ("Wrong number of values returned to continuation (expected ~a)",
  510. scm_from_uint32 (expected));
  511. }
  512. static void
  513. vm_error_continuation_not_rewindable (SCM cont)
  514. {
  515. vm_error ("Unrewindable partial continuation", cont);
  516. }
  517. static SCM vm_boot_continuation;
  518. static SCM vm_builtin_apply;
  519. static SCM vm_builtin_values;
  520. static SCM vm_builtin_abort_to_prompt;
  521. static SCM vm_builtin_call_with_values;
  522. static SCM vm_builtin_call_with_current_continuation;
  523. static const scm_t_uint32 vm_boot_continuation_code[] = {
  524. SCM_PACK_OP_24 (halt, 0)
  525. };
  526. static const scm_t_uint32 vm_apply_non_program_code[] = {
  527. SCM_PACK_OP_24 (apply_non_program, 0)
  528. };
  529. static const scm_t_uint32 vm_builtin_apply_code[] = {
  530. SCM_PACK_OP_24 (assert_nargs_ge, 3),
  531. SCM_PACK_OP_24 (tail_apply, 0), /* proc in r1, args from r2 */
  532. };
  533. static const scm_t_uint32 vm_builtin_values_code[] = {
  534. SCM_PACK_OP_24 (return_values, 0) /* vals from r1 */
  535. };
  536. static const scm_t_uint32 vm_builtin_abort_to_prompt_code[] = {
  537. SCM_PACK_OP_24 (assert_nargs_ge, 2),
  538. SCM_PACK_OP_24 (abort, 0), /* tag in r1, vals from r2 */
  539. /* FIXME: Partial continuation should capture caller regs. */
  540. SCM_PACK_OP_24 (return_values, 0) /* vals from r1 */
  541. };
  542. static const scm_t_uint32 vm_builtin_call_with_values_code[] = {
  543. SCM_PACK_OP_24 (assert_nargs_ee, 3),
  544. SCM_PACK_OP_24 (alloc_frame, 7),
  545. SCM_PACK_OP_12_12 (mov, 0, 5),
  546. SCM_PACK_OP_24 (call, 6), SCM_PACK_OP_ARG_8_24 (0, 1),
  547. SCM_PACK_OP_24 (long_fmov, 0), SCM_PACK_OP_ARG_8_24 (0, 2),
  548. SCM_PACK_OP_24 (tail_call_shuffle, 7)
  549. };
  550. static const scm_t_uint32 vm_builtin_call_with_current_continuation_code[] = {
  551. SCM_PACK_OP_24 (assert_nargs_ee, 2),
  552. SCM_PACK_OP_24 (call_cc, 0)
  553. };
  554. static const scm_t_uint32 vm_handle_interrupt_code[] = {
  555. SCM_PACK_OP_24 (alloc_frame, 3),
  556. SCM_PACK_OP_12_12 (mov, 0, 2),
  557. SCM_PACK_OP_24 (call, 2), SCM_PACK_OP_ARG_8_24 (0, 1),
  558. SCM_PACK_OP_24 (return_from_interrupt, 0)
  559. };
  560. int
  561. scm_i_vm_is_boot_continuation_code (scm_t_uint32 *ip)
  562. {
  563. return ip == vm_boot_continuation_code;
  564. }
  565. static SCM
  566. scm_vm_builtin_ref (unsigned idx)
  567. {
  568. switch (idx)
  569. {
  570. #define INDEX_TO_NAME(builtin, BUILTIN, req, opt, rest) \
  571. case SCM_VM_BUILTIN_##BUILTIN: return vm_builtin_##builtin;
  572. FOR_EACH_VM_BUILTIN(INDEX_TO_NAME)
  573. #undef INDEX_TO_NAME
  574. default: abort();
  575. }
  576. }
  577. SCM scm_sym_apply;
  578. static SCM scm_sym_values;
  579. static SCM scm_sym_abort_to_prompt;
  580. static SCM scm_sym_call_with_values;
  581. static SCM scm_sym_call_with_current_continuation;
  582. SCM
  583. scm_vm_builtin_name_to_index (SCM name)
  584. #define FUNC_NAME "builtin-name->index"
  585. {
  586. SCM_VALIDATE_SYMBOL (1, name);
  587. #define NAME_TO_INDEX(builtin, BUILTIN, req, opt, rest) \
  588. if (scm_is_eq (name, scm_sym_##builtin)) \
  589. return scm_from_uint (SCM_VM_BUILTIN_##BUILTIN);
  590. FOR_EACH_VM_BUILTIN(NAME_TO_INDEX)
  591. #undef NAME_TO_INDEX
  592. return SCM_BOOL_F;
  593. }
  594. #undef FUNC_NAME
  595. SCM
  596. scm_vm_builtin_index_to_name (SCM index)
  597. #define FUNC_NAME "builtin-index->name"
  598. {
  599. unsigned idx;
  600. SCM_VALIDATE_UINT_COPY (1, index, idx);
  601. switch (idx)
  602. {
  603. #define INDEX_TO_NAME(builtin, BUILTIN, req, opt, rest) \
  604. case SCM_VM_BUILTIN_##BUILTIN: return scm_sym_##builtin;
  605. FOR_EACH_VM_BUILTIN(INDEX_TO_NAME)
  606. #undef INDEX_TO_NAME
  607. default: return SCM_BOOL_F;
  608. }
  609. }
  610. #undef FUNC_NAME
  611. static void
  612. scm_init_vm_builtins (void)
  613. {
  614. scm_c_define_gsubr ("builtin-name->index", 1, 0, 0,
  615. scm_vm_builtin_name_to_index);
  616. scm_c_define_gsubr ("builtin-index->name", 1, 0, 0,
  617. scm_vm_builtin_index_to_name);
  618. }
  619. SCM
  620. scm_i_call_with_current_continuation (SCM proc)
  621. {
  622. return scm_call_1 (vm_builtin_call_with_current_continuation, proc);
  623. }
  624. /*
  625. * VM
  626. */
  627. #define VM_NAME vm_regular_engine
  628. #define VM_USE_HOOKS 0
  629. #define FUNC_NAME "vm-regular-engine"
  630. #include "vm-engine.c"
  631. #undef FUNC_NAME
  632. #undef VM_USE_HOOKS
  633. #undef VM_NAME
  634. #define VM_NAME vm_debug_engine
  635. #define VM_USE_HOOKS 1
  636. #define FUNC_NAME "vm-debug-engine"
  637. #include "vm-engine.c"
  638. #undef FUNC_NAME
  639. #undef VM_USE_HOOKS
  640. #undef VM_NAME
  641. typedef SCM (*scm_t_vm_engine) (scm_i_thread *current_thread, struct scm_vm *vp,
  642. scm_i_jmp_buf *registers, int resume);
  643. static const scm_t_vm_engine vm_engines[SCM_VM_NUM_ENGINES] =
  644. { vm_regular_engine, vm_debug_engine };
  645. static union scm_vm_stack_element*
  646. allocate_stack (size_t size)
  647. {
  648. void *ret;
  649. if (size >= ((size_t) -1) / sizeof (union scm_vm_stack_element))
  650. abort ();
  651. size *= sizeof (union scm_vm_stack_element);
  652. #if HAVE_SYS_MMAN_H
  653. ret = mmap (NULL, size, PROT_READ | PROT_WRITE,
  654. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  655. if (ret == NULL)
  656. /* Shouldn't happen. */
  657. abort ();
  658. if (ret == MAP_FAILED)
  659. ret = NULL;
  660. #else
  661. ret = malloc (size);
  662. #endif
  663. if (!ret)
  664. perror ("allocate_stack failed");
  665. return (union scm_vm_stack_element *) ret;
  666. }
  667. static void
  668. free_stack (union scm_vm_stack_element *stack, size_t size)
  669. {
  670. size *= sizeof (*stack);
  671. #if HAVE_SYS_MMAN_H
  672. munmap (stack, size);
  673. #else
  674. free (stack);
  675. #endif
  676. }
  677. /* Ideally what we would like is an mremap or a realloc that grows at
  678. the bottom, not the top. Oh well; mmap and memcpy are fast enough,
  679. considering that they run very infrequently. */
  680. static union scm_vm_stack_element*
  681. expand_stack (union scm_vm_stack_element *old_bottom, size_t old_size,
  682. size_t new_size)
  683. #define FUNC_NAME "expand_stack"
  684. {
  685. union scm_vm_stack_element *new_bottom;
  686. size_t extension_size;
  687. if (new_size >= ((size_t) -1) / sizeof (union scm_vm_stack_element))
  688. abort ();
  689. if (new_size <= old_size)
  690. abort ();
  691. extension_size = new_size - old_size;
  692. if ((size_t)old_bottom < extension_size * sizeof (union scm_vm_stack_element))
  693. abort ();
  694. new_bottom = allocate_stack (new_size);
  695. if (!new_bottom)
  696. return NULL;
  697. memcpy (new_bottom + extension_size,
  698. old_bottom,
  699. old_size * sizeof (union scm_vm_stack_element));
  700. free_stack (old_bottom, old_size);
  701. return new_bottom;
  702. }
  703. #undef FUNC_NAME
  704. static struct scm_vm *
  705. make_vm (void)
  706. #define FUNC_NAME "make_vm"
  707. {
  708. int i;
  709. struct scm_vm *vp;
  710. vp = scm_gc_malloc (sizeof (struct scm_vm), "vm");
  711. vp->stack_size = page_size / sizeof (union scm_vm_stack_element);
  712. vp->stack_bottom = allocate_stack (vp->stack_size);
  713. if (!vp->stack_bottom)
  714. /* As in expand_stack, we don't have any way to throw an exception
  715. if we can't allocate one measely page -- there's no stack to
  716. handle it. For now, abort. */
  717. abort ();
  718. vp->stack_top = vp->stack_bottom + vp->stack_size;
  719. vp->stack_limit = vp->stack_bottom;
  720. vp->overflow_handler_stack = SCM_EOL;
  721. vp->ip = NULL;
  722. vp->sp = vp->stack_top;
  723. vp->sp_min_since_gc = vp->sp;
  724. vp->fp = vp->stack_top;
  725. vp->engine = vm_default_engine;
  726. vp->trace_level = 0;
  727. for (i = 0; i < SCM_VM_NUM_HOOKS; i++)
  728. vp->hooks[i] = SCM_BOOL_F;
  729. return vp;
  730. }
  731. #undef FUNC_NAME
  732. static void
  733. return_unused_stack_to_os (struct scm_vm *vp)
  734. {
  735. #if HAVE_SYS_MMAN_H
  736. scm_t_uintptr lo = (scm_t_uintptr) vp->stack_bottom;
  737. scm_t_uintptr hi = (scm_t_uintptr) vp->sp;
  738. /* The second condition is needed to protect against wrap-around. */
  739. if (vp->sp_min_since_gc >= vp->stack_bottom && vp->sp >= vp->sp_min_since_gc)
  740. lo = (scm_t_uintptr) vp->sp_min_since_gc;
  741. lo &= ~(page_size - 1U); /* round down */
  742. hi &= ~(page_size - 1U); /* round down */
  743. /* Return these pages to the OS. The next time they are paged in,
  744. they will be zeroed. */
  745. if (lo < hi)
  746. {
  747. int ret = 0;
  748. do
  749. ret = madvise ((void *) lo, hi - lo, MADV_DONTNEED);
  750. while (ret && errno == EAGAIN);
  751. /* If the OS doesn't implement 'madvise' (as is currently the case
  752. for GNU/Hurd), don't warn the user since there's nothing they
  753. can do about it. */
  754. if (ret && errno != ENOSYS)
  755. perror ("madvise failed");
  756. }
  757. vp->sp_min_since_gc = vp->sp;
  758. #endif
  759. }
  760. #define SLOT_MAP_CACHE_SIZE 32U
  761. struct slot_map_cache_entry
  762. {
  763. scm_t_uint32 *ip;
  764. const scm_t_uint8 *map;
  765. };
  766. struct slot_map_cache
  767. {
  768. struct slot_map_cache_entry entries[SLOT_MAP_CACHE_SIZE];
  769. };
  770. static const scm_t_uint8 *
  771. find_slot_map (scm_t_uint32 *ip, struct slot_map_cache *cache)
  772. {
  773. /* The lower two bits should be zero. FIXME: Use a better hash
  774. function; we don't expose scm_raw_hashq currently. */
  775. size_t slot = (((scm_t_uintptr) ip) >> 2) % SLOT_MAP_CACHE_SIZE;
  776. const scm_t_uint8 *map;
  777. if (cache->entries[slot].ip == ip)
  778. map = cache->entries[slot].map;
  779. else
  780. {
  781. map = scm_find_slot_map_unlocked (ip);
  782. cache->entries[slot].ip = ip;
  783. cache->entries[slot].map = map;
  784. }
  785. return map;
  786. }
  787. enum slot_desc
  788. {
  789. SLOT_DESC_DEAD = 0,
  790. SLOT_DESC_LIVE_RAW = 1,
  791. SLOT_DESC_LIVE_SCM = 2,
  792. SLOT_DESC_UNUSED = 3
  793. };
  794. /* Mark the active VM stack region. */
  795. struct GC_ms_entry *
  796. scm_i_vm_mark_stack (struct scm_vm *vp, struct GC_ms_entry *mark_stack_ptr,
  797. struct GC_ms_entry *mark_stack_limit)
  798. {
  799. union scm_vm_stack_element *sp, *fp;
  800. /* The first frame will be marked conservatively (without a slot map).
  801. This is because GC can happen at any point within the hottest
  802. activation, due to multiple threads or per-instruction hooks, and
  803. providing slot maps for all points in a program would take a
  804. prohibitive amount of space. */
  805. const scm_t_uint8 *slot_map = NULL;
  806. void *upper = (void *) GC_greatest_plausible_heap_addr;
  807. void *lower = (void *) GC_least_plausible_heap_addr;
  808. struct slot_map_cache cache;
  809. memset (&cache, 0, sizeof (cache));
  810. for (fp = vp->fp, sp = vp->sp;
  811. fp < vp->stack_top;
  812. fp = SCM_FRAME_DYNAMIC_LINK (fp))
  813. {
  814. scm_t_ptrdiff nlocals = SCM_FRAME_NUM_LOCALS (fp, sp);
  815. size_t slot = nlocals - 1;
  816. for (slot = nlocals - 1; sp < fp; sp++, slot--)
  817. {
  818. enum slot_desc desc = SLOT_DESC_LIVE_SCM;
  819. if (slot_map)
  820. desc = (slot_map[slot / 4U] >> ((slot % 4U) * 2)) & 3U;
  821. switch (desc)
  822. {
  823. case SLOT_DESC_LIVE_RAW:
  824. break;
  825. case SLOT_DESC_UNUSED:
  826. case SLOT_DESC_LIVE_SCM:
  827. if (SCM_NIMP (sp->as_scm) &&
  828. sp->as_ptr >= lower && sp->as_ptr <= upper)
  829. mark_stack_ptr = GC_mark_and_push (sp->as_ptr,
  830. mark_stack_ptr,
  831. mark_stack_limit,
  832. NULL);
  833. break;
  834. case SLOT_DESC_DEAD:
  835. /* This value may become dead as a result of GC,
  836. so we can't just leave it on the stack. */
  837. sp->as_scm = SCM_UNSPECIFIED;
  838. break;
  839. }
  840. }
  841. sp = SCM_FRAME_PREVIOUS_SP (fp);
  842. /* Inner frames may have a dead slots map for precise marking.
  843. Note that there may be other reasons to not have a dead slots
  844. map, e.g. if all of the frame's slots below the callee frame
  845. are live. */
  846. slot_map = find_slot_map (SCM_FRAME_RETURN_ADDRESS (fp), &cache);
  847. }
  848. return_unused_stack_to_os (vp);
  849. return mark_stack_ptr;
  850. }
  851. /* Free the VM stack, as this thread is exiting. */
  852. void
  853. scm_i_vm_free_stack (struct scm_vm *vp)
  854. {
  855. free_stack (vp->stack_bottom, vp->stack_size);
  856. vp->stack_bottom = vp->stack_top = vp->stack_limit = NULL;
  857. vp->stack_size = 0;
  858. }
  859. struct vm_expand_stack_data
  860. {
  861. struct scm_vm *vp;
  862. size_t stack_size;
  863. union scm_vm_stack_element *new_sp;
  864. };
  865. static void *
  866. vm_expand_stack_inner (void *data_ptr)
  867. {
  868. struct vm_expand_stack_data *data = data_ptr;
  869. struct scm_vm *vp = data->vp;
  870. union scm_vm_stack_element *old_top, *new_bottom;
  871. size_t new_size;
  872. scm_t_ptrdiff reloc;
  873. old_top = vp->stack_top;
  874. new_size = vp->stack_size;
  875. while (new_size < data->stack_size)
  876. new_size *= 2;
  877. new_bottom = expand_stack (vp->stack_bottom, vp->stack_size, new_size);
  878. if (!new_bottom)
  879. return NULL;
  880. vp->stack_bottom = new_bottom;
  881. vp->stack_size = new_size;
  882. vp->stack_top = vp->stack_bottom + new_size;
  883. vp->stack_limit = vp->stack_bottom;
  884. reloc = vp->stack_top - old_top;
  885. if (vp->fp)
  886. vp->fp += reloc;
  887. data->new_sp += reloc;
  888. return new_bottom;
  889. }
  890. static scm_t_ptrdiff
  891. current_overflow_size (struct scm_vm *vp)
  892. {
  893. if (scm_is_pair (vp->overflow_handler_stack))
  894. return scm_to_ptrdiff_t (scm_caar (vp->overflow_handler_stack));
  895. return -1;
  896. }
  897. static int
  898. should_handle_stack_overflow (struct scm_vm *vp, scm_t_ptrdiff stack_size)
  899. {
  900. scm_t_ptrdiff overflow_size = current_overflow_size (vp);
  901. return overflow_size >= 0 && stack_size >= overflow_size;
  902. }
  903. static void
  904. reset_stack_limit (struct scm_vm *vp)
  905. {
  906. if (should_handle_stack_overflow (vp, vp->stack_size))
  907. vp->stack_limit = vp->stack_top - current_overflow_size (vp);
  908. else
  909. vp->stack_limit = vp->stack_bottom;
  910. }
  911. struct overflow_handler_data
  912. {
  913. struct scm_vm *vp;
  914. SCM overflow_handler_stack;
  915. };
  916. static void
  917. wind_overflow_handler (void *ptr)
  918. {
  919. struct overflow_handler_data *data = ptr;
  920. data->vp->overflow_handler_stack = data->overflow_handler_stack;
  921. reset_stack_limit (data->vp);
  922. }
  923. static void
  924. unwind_overflow_handler (void *ptr)
  925. {
  926. struct overflow_handler_data *data = ptr;
  927. data->vp->overflow_handler_stack = scm_cdr (data->overflow_handler_stack);
  928. reset_stack_limit (data->vp);
  929. }
  930. static void
  931. vm_expand_stack (struct scm_vm *vp, union scm_vm_stack_element *new_sp)
  932. {
  933. scm_t_ptrdiff stack_size = vp->stack_top - new_sp;
  934. if (stack_size > vp->stack_size)
  935. {
  936. struct vm_expand_stack_data data;
  937. data.vp = vp;
  938. data.stack_size = stack_size;
  939. data.new_sp = new_sp;
  940. if (!GC_call_with_alloc_lock (vm_expand_stack_inner, &data))
  941. /* Throw an unwind-only exception. */
  942. scm_report_stack_overflow ();
  943. new_sp = data.new_sp;
  944. }
  945. vp->sp_min_since_gc = vp->sp = new_sp;
  946. if (should_handle_stack_overflow (vp, stack_size))
  947. {
  948. SCM more_stack, new_limit;
  949. struct overflow_handler_data data;
  950. data.vp = vp;
  951. data.overflow_handler_stack = vp->overflow_handler_stack;
  952. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  953. scm_dynwind_rewind_handler (unwind_overflow_handler, &data,
  954. SCM_F_WIND_EXPLICITLY);
  955. scm_dynwind_unwind_handler (wind_overflow_handler, &data,
  956. SCM_F_WIND_EXPLICITLY);
  957. /* Call the overflow handler. */
  958. more_stack = scm_call_0 (scm_cdar (data.overflow_handler_stack));
  959. /* If the overflow handler returns, its return value should be an
  960. integral number of words from the outer stack limit to transfer
  961. to the inner limit. */
  962. if (scm_to_ptrdiff_t (more_stack) <= 0)
  963. scm_out_of_range (NULL, more_stack);
  964. new_limit = scm_sum (scm_caar (data.overflow_handler_stack), more_stack);
  965. if (scm_is_pair (scm_cdr (data.overflow_handler_stack)))
  966. new_limit = scm_min (new_limit,
  967. scm_caadr (data.overflow_handler_stack));
  968. /* Ensure the new limit is in range. */
  969. scm_to_ptrdiff_t (new_limit);
  970. /* Increase the limit that we will restore. */
  971. scm_set_car_x (scm_car (data.overflow_handler_stack), new_limit);
  972. scm_dynwind_end ();
  973. /* Recurse. */
  974. return vm_expand_stack (vp, new_sp);
  975. }
  976. }
  977. static struct scm_vm *
  978. thread_vm (scm_i_thread *t)
  979. {
  980. if (SCM_UNLIKELY (!t->vp))
  981. t->vp = make_vm ();
  982. return t->vp;
  983. }
  984. struct scm_vm *
  985. scm_the_vm (void)
  986. {
  987. return thread_vm (SCM_I_CURRENT_THREAD);
  988. }
  989. SCM
  990. scm_call_n (SCM proc, SCM *argv, size_t nargs)
  991. {
  992. scm_i_thread *thread;
  993. struct scm_vm *vp;
  994. union scm_vm_stack_element *return_fp, *call_fp;
  995. /* Since nargs can only describe the length of a valid argv array in
  996. elements and each element is at least 4 bytes, nargs will not be
  997. greater than INTMAX/2 and therefore we don't have to check for
  998. overflow here or below. */
  999. size_t return_nlocals = 1, call_nlocals = nargs + 1, frame_size = 2;
  1000. scm_t_ptrdiff stack_reserve_words;
  1001. size_t i;
  1002. thread = SCM_I_CURRENT_THREAD;
  1003. vp = thread_vm (thread);
  1004. SCM_CHECK_STACK;
  1005. /* It's not valid for argv to point into the stack already. */
  1006. if ((void *) argv < (void *) vp->stack_top &&
  1007. (void *) argv >= (void *) vp->sp)
  1008. abort();
  1009. /* Check that we have enough space for the two stack frames: the
  1010. innermost one that makes the call, and its continuation which
  1011. receives the resulting value(s) and returns from the engine
  1012. call. */
  1013. stack_reserve_words = call_nlocals + frame_size + return_nlocals + frame_size;
  1014. vm_push_sp (vp, vp->sp - stack_reserve_words);
  1015. call_fp = vp->sp + call_nlocals;
  1016. return_fp = call_fp + frame_size + return_nlocals;
  1017. SCM_FRAME_SET_RETURN_ADDRESS (return_fp, vp->ip);
  1018. SCM_FRAME_SET_DYNAMIC_LINK (return_fp, vp->fp);
  1019. SCM_FRAME_LOCAL (return_fp, 0) = vm_boot_continuation;
  1020. vp->ip = (scm_t_uint32 *) vm_boot_continuation_code;
  1021. vp->fp = call_fp;
  1022. SCM_FRAME_SET_RETURN_ADDRESS (call_fp, vp->ip);
  1023. SCM_FRAME_SET_DYNAMIC_LINK (call_fp, return_fp);
  1024. SCM_FRAME_LOCAL (call_fp, 0) = proc;
  1025. for (i = 0; i < nargs; i++)
  1026. SCM_FRAME_LOCAL (call_fp, i + 1) = argv[i];
  1027. {
  1028. scm_i_jmp_buf registers;
  1029. int resume;
  1030. const void *prev_cookie = vp->resumable_prompt_cookie;
  1031. SCM ret;
  1032. resume = SCM_I_SETJMP (registers);
  1033. if (SCM_UNLIKELY (resume))
  1034. {
  1035. scm_gc_after_nonlocal_exit ();
  1036. /* Non-local return. */
  1037. vm_dispatch_abort_hook (vp);
  1038. }
  1039. vp->resumable_prompt_cookie = &registers;
  1040. ret = vm_engines[vp->engine](thread, vp, &registers, resume);
  1041. vp->resumable_prompt_cookie = prev_cookie;
  1042. return ret;
  1043. }
  1044. }
  1045. /* Scheme interface */
  1046. #define VM_DEFINE_HOOK(n) \
  1047. { \
  1048. struct scm_vm *vp; \
  1049. vp = scm_the_vm (); \
  1050. if (scm_is_false (vp->hooks[n])) \
  1051. vp->hooks[n] = scm_make_hook (SCM_I_MAKINUM (1)); \
  1052. return vp->hooks[n]; \
  1053. }
  1054. SCM_DEFINE (scm_vm_apply_hook, "vm-apply-hook", 0, 0, 0,
  1055. (void),
  1056. "")
  1057. #define FUNC_NAME s_scm_vm_apply_hook
  1058. {
  1059. VM_DEFINE_HOOK (SCM_VM_APPLY_HOOK);
  1060. }
  1061. #undef FUNC_NAME
  1062. SCM_DEFINE (scm_vm_push_continuation_hook, "vm-push-continuation-hook", 0, 0, 0,
  1063. (void),
  1064. "")
  1065. #define FUNC_NAME s_scm_vm_push_continuation_hook
  1066. {
  1067. VM_DEFINE_HOOK (SCM_VM_PUSH_CONTINUATION_HOOK);
  1068. }
  1069. #undef FUNC_NAME
  1070. SCM_DEFINE (scm_vm_pop_continuation_hook, "vm-pop-continuation-hook", 0, 0, 0,
  1071. (void),
  1072. "")
  1073. #define FUNC_NAME s_scm_vm_pop_continuation_hook
  1074. {
  1075. VM_DEFINE_HOOK (SCM_VM_POP_CONTINUATION_HOOK);
  1076. }
  1077. #undef FUNC_NAME
  1078. SCM_DEFINE (scm_vm_next_hook, "vm-next-hook", 0, 0, 0,
  1079. (void),
  1080. "")
  1081. #define FUNC_NAME s_scm_vm_next_hook
  1082. {
  1083. VM_DEFINE_HOOK (SCM_VM_NEXT_HOOK);
  1084. }
  1085. #undef FUNC_NAME
  1086. SCM_DEFINE (scm_vm_abort_continuation_hook, "vm-abort-continuation-hook", 0, 0, 0,
  1087. (void),
  1088. "")
  1089. #define FUNC_NAME s_scm_vm_abort_continuation_hook
  1090. {
  1091. VM_DEFINE_HOOK (SCM_VM_ABORT_CONTINUATION_HOOK);
  1092. }
  1093. #undef FUNC_NAME
  1094. SCM_DEFINE (scm_vm_trace_level, "vm-trace-level", 0, 0, 0,
  1095. (void),
  1096. "")
  1097. #define FUNC_NAME s_scm_vm_trace_level
  1098. {
  1099. return scm_from_int (scm_the_vm ()->trace_level);
  1100. }
  1101. #undef FUNC_NAME
  1102. SCM_DEFINE (scm_set_vm_trace_level_x, "set-vm-trace-level!", 1, 0, 0,
  1103. (SCM level),
  1104. "")
  1105. #define FUNC_NAME s_scm_set_vm_trace_level_x
  1106. {
  1107. scm_the_vm ()->trace_level = scm_to_int (level);
  1108. return SCM_UNSPECIFIED;
  1109. }
  1110. #undef FUNC_NAME
  1111. /*
  1112. * VM engines
  1113. */
  1114. static int
  1115. symbol_to_vm_engine (SCM engine, const char *FUNC_NAME)
  1116. {
  1117. if (scm_is_eq (engine, sym_regular))
  1118. return SCM_VM_REGULAR_ENGINE;
  1119. else if (scm_is_eq (engine, sym_debug))
  1120. return SCM_VM_DEBUG_ENGINE;
  1121. else
  1122. SCM_MISC_ERROR ("Unknown VM engine: ~a", scm_list_1 (engine));
  1123. }
  1124. static SCM
  1125. vm_engine_to_symbol (int engine, const char *FUNC_NAME)
  1126. {
  1127. switch (engine)
  1128. {
  1129. case SCM_VM_REGULAR_ENGINE:
  1130. return sym_regular;
  1131. case SCM_VM_DEBUG_ENGINE:
  1132. return sym_debug;
  1133. default:
  1134. /* ? */
  1135. SCM_MISC_ERROR ("Unknown VM engine: ~a",
  1136. scm_list_1 (scm_from_int (engine)));
  1137. }
  1138. }
  1139. SCM_DEFINE (scm_vm_engine, "vm-engine", 0, 0, 0,
  1140. (void),
  1141. "")
  1142. #define FUNC_NAME s_scm_vm_engine
  1143. {
  1144. return vm_engine_to_symbol (scm_the_vm ()->engine, FUNC_NAME);
  1145. }
  1146. #undef FUNC_NAME
  1147. void
  1148. scm_c_set_vm_engine_x (int engine)
  1149. #define FUNC_NAME "set-vm-engine!"
  1150. {
  1151. if (engine < 0 || engine >= SCM_VM_NUM_ENGINES)
  1152. SCM_MISC_ERROR ("Unknown VM engine: ~a",
  1153. scm_list_1 (scm_from_int (engine)));
  1154. scm_the_vm ()->engine = engine;
  1155. }
  1156. #undef FUNC_NAME
  1157. SCM_DEFINE (scm_set_vm_engine_x, "set-vm-engine!", 1, 0, 0,
  1158. (SCM engine),
  1159. "")
  1160. #define FUNC_NAME s_scm_set_vm_engine_x
  1161. {
  1162. scm_c_set_vm_engine_x (symbol_to_vm_engine (engine, FUNC_NAME));
  1163. return SCM_UNSPECIFIED;
  1164. }
  1165. #undef FUNC_NAME
  1166. void
  1167. scm_c_set_default_vm_engine_x (int engine)
  1168. #define FUNC_NAME "set-default-vm-engine!"
  1169. {
  1170. if (engine < 0 || engine >= SCM_VM_NUM_ENGINES)
  1171. SCM_MISC_ERROR ("Unknown VM engine: ~a",
  1172. scm_list_1 (scm_from_int (engine)));
  1173. vm_default_engine = engine;
  1174. }
  1175. #undef FUNC_NAME
  1176. SCM_DEFINE (scm_set_default_vm_engine_x, "set-default-vm-engine!", 1, 0, 0,
  1177. (SCM engine),
  1178. "")
  1179. #define FUNC_NAME s_scm_set_default_vm_engine_x
  1180. {
  1181. scm_c_set_default_vm_engine_x (symbol_to_vm_engine (engine, FUNC_NAME));
  1182. return SCM_UNSPECIFIED;
  1183. }
  1184. #undef FUNC_NAME
  1185. /* FIXME: This function makes no sense, but we keep it to make sure we
  1186. have a way of switching to the debug or regular VM. */
  1187. SCM_DEFINE (scm_call_with_vm, "call-with-vm", 1, 0, 1,
  1188. (SCM proc, SCM args),
  1189. "Apply @var{proc} to @var{args} in a dynamic extent in which\n"
  1190. "@var{vm} is the current VM.")
  1191. #define FUNC_NAME s_scm_call_with_vm
  1192. {
  1193. return scm_apply_0 (proc, args);
  1194. }
  1195. #undef FUNC_NAME
  1196. SCM_DEFINE (scm_call_with_stack_overflow_handler,
  1197. "call-with-stack-overflow-handler", 3, 0, 0,
  1198. (SCM limit, SCM thunk, SCM handler),
  1199. "Call @var{thunk} in an environment in which the stack limit has\n"
  1200. "been reduced to @var{limit} additional words. If the limit is\n"
  1201. "reached, @var{handler} (a thunk) will be invoked in the dynamic\n"
  1202. "environment of the error. For the extent of the call to\n"
  1203. "@var{handler}, the stack limit and handler are restored to the\n"
  1204. "values that were in place when\n"
  1205. "@code{call-with-stack-overflow-handler} was called.")
  1206. #define FUNC_NAME s_scm_call_with_stack_overflow_handler
  1207. {
  1208. struct scm_vm *vp;
  1209. scm_t_ptrdiff c_limit, stack_size;
  1210. struct overflow_handler_data data;
  1211. SCM new_limit, ret;
  1212. vp = scm_the_vm ();
  1213. stack_size = vp->stack_top - vp->sp;
  1214. c_limit = scm_to_ptrdiff_t (limit);
  1215. if (c_limit <= 0)
  1216. scm_out_of_range (FUNC_NAME, limit);
  1217. new_limit = scm_sum (scm_from_ptrdiff_t (stack_size), limit);
  1218. if (scm_is_pair (vp->overflow_handler_stack))
  1219. new_limit = scm_min (new_limit, scm_caar (vp->overflow_handler_stack));
  1220. /* Hacky check that the current stack depth plus the limit is within
  1221. the range of a ptrdiff_t. */
  1222. scm_to_ptrdiff_t (new_limit);
  1223. data.vp = vp;
  1224. data.overflow_handler_stack =
  1225. scm_acons (limit, handler, vp->overflow_handler_stack);
  1226. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  1227. scm_dynwind_rewind_handler (wind_overflow_handler, &data,
  1228. SCM_F_WIND_EXPLICITLY);
  1229. scm_dynwind_unwind_handler (unwind_overflow_handler, &data,
  1230. SCM_F_WIND_EXPLICITLY);
  1231. /* Reset vp->sp_min_since_gc so that the VM checks actually
  1232. trigger. */
  1233. return_unused_stack_to_os (vp);
  1234. ret = scm_call_0 (thunk);
  1235. scm_dynwind_end ();
  1236. return ret;
  1237. }
  1238. #undef FUNC_NAME
  1239. /*
  1240. * Initialize
  1241. */
  1242. SCM
  1243. scm_load_compiled_with_vm (SCM file)
  1244. {
  1245. return scm_call_0 (scm_load_thunk_from_file (file));
  1246. }
  1247. void
  1248. scm_init_vm_builtin_properties (void)
  1249. {
  1250. /* FIXME: Seems hacky to do this here, but oh well :/ */
  1251. scm_sym_apply = scm_from_utf8_symbol ("apply");
  1252. scm_sym_values = scm_from_utf8_symbol ("values");
  1253. scm_sym_abort_to_prompt = scm_from_utf8_symbol ("abort-to-prompt");
  1254. scm_sym_call_with_values = scm_from_utf8_symbol ("call-with-values");
  1255. scm_sym_call_with_current_continuation =
  1256. scm_from_utf8_symbol ("call-with-current-continuation");
  1257. #define INIT_BUILTIN(builtin, BUILTIN, req, opt, rest) \
  1258. scm_set_procedure_property_x (vm_builtin_##builtin, scm_sym_name, \
  1259. scm_sym_##builtin); \
  1260. scm_set_procedure_minimum_arity_x (vm_builtin_##builtin, \
  1261. SCM_I_MAKINUM (req), \
  1262. SCM_I_MAKINUM (opt), \
  1263. scm_from_bool (rest));
  1264. FOR_EACH_VM_BUILTIN (INIT_BUILTIN);
  1265. #undef INIT_BUILTIN
  1266. }
  1267. void
  1268. scm_bootstrap_vm (void)
  1269. {
  1270. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  1271. "scm_init_vm",
  1272. (scm_t_extension_init_func)scm_init_vm, NULL);
  1273. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  1274. "scm_init_vm_builtins",
  1275. (scm_t_extension_init_func)scm_init_vm_builtins,
  1276. NULL);
  1277. page_size = getpagesize ();
  1278. /* page_size should be a power of two. */
  1279. if (page_size & (page_size - 1))
  1280. abort ();
  1281. sym_vm_run = scm_from_latin1_symbol ("vm-run");
  1282. sym_vm_error = scm_from_latin1_symbol ("vm-error");
  1283. sym_keyword_argument_error = scm_from_latin1_symbol ("keyword-argument-error");
  1284. sym_regular = scm_from_latin1_symbol ("regular");
  1285. sym_debug = scm_from_latin1_symbol ("debug");
  1286. vm_boot_continuation = scm_i_make_program (vm_boot_continuation_code);
  1287. SCM_SET_CELL_WORD_0 (vm_boot_continuation,
  1288. (SCM_CELL_WORD_0 (vm_boot_continuation)
  1289. | SCM_F_PROGRAM_IS_BOOT));
  1290. #define DEFINE_BUILTIN(builtin, BUILTIN, req, opt, rest) \
  1291. vm_builtin_##builtin = scm_i_make_program (vm_builtin_##builtin##_code);
  1292. FOR_EACH_VM_BUILTIN (DEFINE_BUILTIN);
  1293. #undef DEFINE_BUILTIN
  1294. }
  1295. void
  1296. scm_init_vm (void)
  1297. {
  1298. #ifndef SCM_MAGIC_SNARFER
  1299. #include "libguile/vm.x"
  1300. #endif
  1301. }
  1302. /*
  1303. Local Variables:
  1304. c-file-style: "gnu"
  1305. End:
  1306. */