vm.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964
  1. /* Copyright 2001,2009-2015,2017-2020,2022-2023
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #if HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <alignof.h>
  19. #include <alloca.h>
  20. #include <errno.h>
  21. #include <math.h>
  22. #include <stdint.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #ifdef HAVE_SYS_MMAN_H
  28. #include <sys/mman.h>
  29. #endif
  30. #include "alist.h"
  31. #include "async.h"
  32. #include "atomic.h"
  33. #include "atomics-internal.h"
  34. #include "bdw-gc.h"
  35. #include "cache-internal.h"
  36. #include "continuations.h"
  37. #include "control.h"
  38. #include "dynwind.h"
  39. #include "eval.h"
  40. #include "extensions.h"
  41. #include "foreign.h"
  42. #include "frames.h"
  43. #include "gc-inline.h"
  44. #include "gsubr.h"
  45. #include "hooks.h"
  46. #include "instructions.h"
  47. #include "intrinsics.h"
  48. #include "jit.h"
  49. #include "keywords.h"
  50. #include "list.h"
  51. #include "loader.h"
  52. #include "modules.h"
  53. #include "numbers.h"
  54. #include "pairs.h"
  55. #include "ports.h"
  56. #include "procprop.h"
  57. #include "programs.h"
  58. #include "simpos.h"
  59. #include "smob.h"
  60. #include "stackchk.h"
  61. #include "symbols.h"
  62. #include "values.h"
  63. #include "vectors.h"
  64. #include "version.h"
  65. #include "vm-builtins.h"
  66. #include "vm.h"
  67. #include <gc/gc_mark.h>
  68. #if (defined __GNUC__)
  69. # define SCM_NOINLINE __attribute__ ((__noinline__))
  70. #else
  71. # define SCM_NOINLINE /* noinline */
  72. #endif
  73. static int vm_default_engine = SCM_VM_REGULAR_ENGINE;
  74. /* Unfortunately we can't snarf these: snarfed things are only loaded up from
  75. (system vm vm), which might not be loaded before an error happens. */
  76. static SCM sym_keyword_argument_error;
  77. static SCM sym_regular;
  78. static SCM sym_debug;
  79. /* The page size. */
  80. static size_t page_size;
  81. /* The VM has a number of internal assertions that shouldn't normally be
  82. necessary, but might be if you think you found a bug in the VM. */
  83. /* #define VM_ENABLE_ASSERTIONS */
  84. static void vm_expand_stack (struct scm_vm *vp,
  85. union scm_vm_stack_element *new_sp) SCM_NOINLINE;
  86. /* RESTORE is for the case where we know we have done a PUSH of equal or
  87. greater stack size in the past. Otherwise PUSH is the thing, which
  88. may expand the stack. */
  89. enum vm_increase_sp_kind { VM_SP_PUSH, VM_SP_RESTORE };
  90. static inline void
  91. vm_increase_sp (struct scm_vm *vp, union scm_vm_stack_element *new_sp,
  92. enum vm_increase_sp_kind kind)
  93. {
  94. if (kind == VM_SP_PUSH && new_sp < vp->stack_limit)
  95. vm_expand_stack (vp, new_sp);
  96. else
  97. vp->sp = new_sp;
  98. }
  99. static inline void
  100. vm_push_sp (struct scm_vm *vp, union scm_vm_stack_element *new_sp)
  101. {
  102. vm_increase_sp (vp, new_sp, VM_SP_PUSH);
  103. }
  104. static inline void
  105. vm_restore_sp (struct scm_vm *vp, union scm_vm_stack_element *new_sp)
  106. {
  107. vm_increase_sp (vp, new_sp, VM_SP_RESTORE);
  108. }
  109. /*
  110. * VM Continuation
  111. */
  112. void
  113. scm_i_vm_cont_print (SCM x, SCM port, scm_print_state *pstate)
  114. {
  115. scm_puts ("#<vm-continuation ", port);
  116. scm_uintprint (SCM_UNPACK (x), 16, port);
  117. scm_puts (">", port);
  118. }
  119. int
  120. scm_i_vm_cont_to_frame (SCM cont, struct scm_frame *frame)
  121. {
  122. struct scm_vm_cont *data = SCM_VM_CONT_DATA (cont);
  123. frame->stack_holder = data;
  124. frame->fp_offset = data->fp_offset;
  125. frame->sp_offset = data->stack_size;
  126. frame->ip = data->vra;
  127. return 1;
  128. }
  129. /* Ideally we could avoid copying the C stack if the continuation root
  130. is inside VM code, and call/cc was invoked within that same call to
  131. vm_run. That's currently not implemented. */
  132. static SCM
  133. capture_stack (union scm_vm_stack_element *stack_top,
  134. union scm_vm_stack_element *fp,
  135. union scm_vm_stack_element *sp,
  136. uint32_t *vra,
  137. uint8_t *mra,
  138. scm_t_dynstack *dynstack, uint32_t flags)
  139. {
  140. struct scm_vm_cont *p;
  141. size_t stack_size;
  142. stack_size = stack_top - sp;
  143. /* Allocate the 'scm_vm_cont' struct and the stack at once. That way,
  144. keeping a pointer to 'p->stack_bottom' around won't retain it.
  145. See <https://bugs.gnu.org/59021>. */
  146. p = scm_gc_malloc (sizeof (*p) + stack_size * sizeof (*p->stack_bottom),
  147. "capture_vm_cont");
  148. p->stack_size = stack_size;
  149. p->stack_bottom = (void *) ((char *) p + sizeof (*p));
  150. p->vra = vra;
  151. p->mra = mra;
  152. p->fp_offset = stack_top - fp;
  153. memcpy (p->stack_bottom, sp, p->stack_size * sizeof (*p->stack_bottom));
  154. p->dynstack = dynstack;
  155. p->flags = flags;
  156. return scm_cell (scm_tc7_vm_cont, (scm_t_bits) p);
  157. }
  158. SCM
  159. scm_i_capture_current_stack (void)
  160. {
  161. scm_thread *thread;
  162. struct scm_vm *vp;
  163. thread = SCM_I_CURRENT_THREAD;
  164. vp = &thread->vm;
  165. return capture_stack (vp->stack_top, vp->fp, vp->sp, vp->ip, NULL,
  166. scm_dynstack_capture_all (&thread->dynstack),
  167. 0);
  168. }
  169. #define FOR_EACH_HOOK(M) \
  170. M(apply) \
  171. M(return) \
  172. M(next) \
  173. M(abort)
  174. static void
  175. vm_hook_compute_enabled (scm_thread *thread, SCM hook, uint8_t *enabled)
  176. {
  177. if (thread->vm.trace_level <= 0
  178. || thread->vm.engine == SCM_VM_REGULAR_ENGINE
  179. || scm_is_false (hook)
  180. || scm_is_true (scm_hook_empty_p (hook)))
  181. *enabled = 0;
  182. else
  183. *enabled = 1;
  184. }
  185. static void
  186. vm_recompute_disable_mcode (scm_thread *thread)
  187. {
  188. #if ENABLE_JIT
  189. /* FIXME: Some of this logic works for ahead-of-time compilation
  190. too. */
  191. uint8_t was_disabled = thread->vm.disable_mcode;
  192. thread->vm.disable_mcode = 0;
  193. #define DISABLE_MCODE_IF_HOOK_ENABLED(h) \
  194. if (thread->vm.h##_hook_enabled) \
  195. thread->vm.disable_mcode = 1;
  196. FOR_EACH_HOOK (DISABLE_MCODE_IF_HOOK_ENABLED);
  197. #undef DISABLE_MCODE_IF_HOOK_ENABLED
  198. if (thread->vm.disable_mcode && !was_disabled)
  199. scm_jit_clear_mcode_return_addresses (thread);
  200. #endif
  201. }
  202. static int
  203. set_vm_trace_level (scm_thread *thread, int level)
  204. {
  205. int old_level;
  206. struct scm_vm *vp = &thread->vm;
  207. old_level = vp->trace_level;
  208. vp->trace_level = level;
  209. vp->disable_mcode = 0;
  210. #define RESET_LEVEL(h) \
  211. vm_hook_compute_enabled (thread, vp->h##_hook, &vp->h##_hook_enabled);
  212. FOR_EACH_HOOK (RESET_LEVEL);
  213. #undef RESET_LEVEL
  214. vm_recompute_disable_mcode (thread);
  215. return old_level;
  216. }
  217. /* Return the first integer greater than or equal to LEN such that
  218. LEN % ALIGN == 0. Return LEN if ALIGN is zero. */
  219. #define ROUND_UP(len, align) \
  220. ((align) ? (((len) - 1UL) | ((align) - 1UL)) + 1UL : (len))
  221. static void
  222. invoke_hook (scm_thread *thread, SCM hook)
  223. {
  224. struct scm_vm *vp = &thread->vm;
  225. struct scm_frame c_frame;
  226. scm_t_cell *frame;
  227. SCM scm_frame;
  228. int saved_trace_level;
  229. uint8_t saved_compare_result;
  230. if (scm_is_false (hook) || scm_is_null (SCM_HOOK_PROCEDURES (hook)))
  231. return;
  232. saved_trace_level = set_vm_trace_level (thread, 0);
  233. saved_compare_result = vp->compare_result;
  234. /* Allocate a frame object on the stack. This is more efficient than calling
  235. `scm_c_make_frame ()' to allocate on the heap, but it forces hooks to not
  236. capture frame objects.
  237. At the same time, procedures such as `frame-procedure' make sense only
  238. while the stack frame represented by the frame object is visible, so it
  239. seems reasonable to limit the lifetime of frame objects. */
  240. c_frame.stack_holder = vp;
  241. c_frame.fp_offset = vp->stack_top - vp->fp;
  242. c_frame.sp_offset = vp->stack_top - vp->sp;
  243. c_frame.ip = vp->ip;
  244. /* Arrange for FRAME to be 8-byte aligned, like any other cell. */
  245. frame = alloca (sizeof (*frame) + 8);
  246. frame = (scm_t_cell *) ROUND_UP ((uintptr_t) frame, 8UL);
  247. frame->word_0 = SCM_PACK (scm_tc7_frame | (SCM_VM_FRAME_KIND_VM << 8));
  248. frame->word_1 = SCM_PACK_POINTER (&c_frame);
  249. scm_frame = SCM_PACK_POINTER (frame);
  250. scm_c_run_hookn (hook, &scm_frame, 1);
  251. vp->compare_result = saved_compare_result;
  252. set_vm_trace_level (thread, saved_trace_level);
  253. }
  254. #define DEFINE_INVOKE_HOOK(h) \
  255. static void \
  256. invoke_##h##_hook (scm_thread *thread) SCM_NOINLINE; \
  257. static void \
  258. invoke_##h##_hook (scm_thread *thread) \
  259. { \
  260. if (thread->vm.h##_hook_enabled) \
  261. return invoke_hook (thread, thread->vm.h##_hook); \
  262. }
  263. FOR_EACH_HOOK (DEFINE_INVOKE_HOOK)
  264. #undef DEFINE_INVOKE_HOOK
  265. /*
  266. * VM Error Handling
  267. */
  268. static void vm_error_bad_instruction (uint32_t inst) SCM_NORETURN SCM_NOINLINE;
  269. static void
  270. vm_error_bad_instruction (uint32_t inst)
  271. {
  272. fprintf (stderr, "VM: Bad instruction: %x\n", inst);
  273. abort ();
  274. }
  275. static SCM vm_boot_continuation;
  276. #define DECLARE_BUILTIN(builtin, BUILTIN, req, opt, rest) \
  277. static SCM vm_builtin_##builtin; \
  278. static uint32_t *vm_builtin_##builtin##_code;
  279. FOR_EACH_VM_BUILTIN (DECLARE_BUILTIN)
  280. #undef DECLARE_BUILTIN
  281. static const uint32_t vm_boot_continuation_code[] = {
  282. SCM_PACK_OP_24 (halt, 0)
  283. };
  284. int
  285. scm_i_vm_is_boot_continuation_code (uint32_t *ip)
  286. {
  287. return ip == vm_boot_continuation_code;
  288. }
  289. SCM
  290. scm_vm_builtin_ref (unsigned idx)
  291. {
  292. switch (idx)
  293. {
  294. #define INDEX_TO_NAME(builtin, BUILTIN, req, opt, rest) \
  295. case SCM_VM_BUILTIN_##BUILTIN: return vm_builtin_##builtin;
  296. FOR_EACH_VM_BUILTIN(INDEX_TO_NAME)
  297. #undef INDEX_TO_NAME
  298. default: abort();
  299. }
  300. }
  301. SCM scm_sym_apply;
  302. static SCM scm_sym_values;
  303. static SCM scm_sym_abort_to_prompt;
  304. static SCM scm_sym_call_with_values;
  305. static SCM scm_sym_call_with_current_continuation;
  306. SCM
  307. scm_vm_builtin_name_to_index (SCM name)
  308. #define FUNC_NAME "builtin-name->index"
  309. {
  310. SCM_VALIDATE_SYMBOL (1, name);
  311. #define NAME_TO_INDEX(builtin, BUILTIN, req, opt, rest) \
  312. if (scm_is_eq (name, scm_sym_##builtin)) \
  313. return scm_from_uint (SCM_VM_BUILTIN_##BUILTIN);
  314. FOR_EACH_VM_BUILTIN(NAME_TO_INDEX)
  315. #undef NAME_TO_INDEX
  316. return SCM_BOOL_F;
  317. }
  318. #undef FUNC_NAME
  319. SCM
  320. scm_vm_builtin_index_to_name (SCM index)
  321. #define FUNC_NAME "builtin-index->name"
  322. {
  323. unsigned idx;
  324. SCM_VALIDATE_UINT_COPY (1, index, idx);
  325. switch (idx)
  326. {
  327. #define INDEX_TO_NAME(builtin, BUILTIN, req, opt, rest) \
  328. case SCM_VM_BUILTIN_##BUILTIN: return scm_sym_##builtin;
  329. FOR_EACH_VM_BUILTIN(INDEX_TO_NAME)
  330. #undef INDEX_TO_NAME
  331. default: return SCM_BOOL_F;
  332. }
  333. }
  334. #undef FUNC_NAME
  335. static void
  336. scm_init_vm_builtins (void)
  337. {
  338. scm_c_define_gsubr ("builtin-name->index", 1, 0, 0,
  339. scm_vm_builtin_name_to_index);
  340. scm_c_define_gsubr ("builtin-index->name", 1, 0, 0,
  341. scm_vm_builtin_index_to_name);
  342. }
  343. static uint32_t*
  344. instrumented_code (const uint32_t *code, size_t byte_size)
  345. {
  346. uint32_t *ret, *write;
  347. ret = scm_i_alloc_primitive_code_with_instrumentation (byte_size / 4, &write);
  348. memcpy (write, code, byte_size);
  349. return ret;
  350. }
  351. static void
  352. define_vm_builtins (void)
  353. {
  354. const uint32_t apply_code[] = {
  355. SCM_PACK_OP_24 (assert_nargs_ge, 3),
  356. SCM_PACK_OP_12_12 (shuffle_down, 1, 0),
  357. SCM_PACK_OP_24 (expand_apply_argument, 0),
  358. SCM_PACK_OP_24 (tail_call, 0),
  359. };
  360. const uint32_t values_code[] = {
  361. SCM_PACK_OP_12_12 (shuffle_down, 1, 0),
  362. SCM_PACK_OP_24 (return_values, 0)
  363. };
  364. const uint32_t abort_to_prompt_code[] = {
  365. SCM_PACK_OP_24 (assert_nargs_ge, 2),
  366. SCM_PACK_OP_24 (abort, 0), /* tag in r1, vals from r2 */
  367. /* FIXME: Partial continuation should capture caller regs. */
  368. SCM_PACK_OP_24 (return_values, 0) /* vals from r0 */
  369. };
  370. const uint32_t call_with_values_code[] = {
  371. SCM_PACK_OP_24 (assert_nargs_ee, 3),
  372. SCM_PACK_OP_24 (alloc_frame, 6),
  373. SCM_PACK_OP_12_12 (mov, 0, 4),
  374. SCM_PACK_OP_12_12 (mov, 4, 3),
  375. SCM_PACK_OP_24 (call, 5), SCM_PACK_OP_ARG_8_24 (0, 1),
  376. SCM_PACK_OP_24 (long_fmov, 0), SCM_PACK_OP_ARG_8_24 (0, 1),
  377. SCM_PACK_OP_12_12 (shuffle_down, 5, 1),
  378. SCM_PACK_OP_24 (tail_call, 0)
  379. };
  380. const uint32_t call_with_current_continuation_code[] = {
  381. SCM_PACK_OP_24 (assert_nargs_ee, 2),
  382. SCM_PACK_OP_12_12 (mov, 1, 0),
  383. SCM_PACK_OP_24 (capture_continuation, 0),
  384. SCM_PACK_OP_24 (tail_call, 0)
  385. };
  386. /* This one isn't exactly a builtin but we still handle it here. */
  387. const uint32_t handle_interrupt_code[] = {
  388. SCM_PACK_OP_24 (alloc_frame, 4),
  389. SCM_PACK_OP_12_12 (mov, 0, 3),
  390. SCM_PACK_OP_24 (call, 3), SCM_PACK_OP_ARG_8_24 (0, 1),
  391. SCM_PACK_OP_24 (return_from_interrupt, 0)
  392. };
  393. #define DEFINE_BUILTIN(builtin, BUILTIN, req, opt, rest) \
  394. { \
  395. size_t sz = sizeof (builtin##_code); \
  396. vm_builtin_##builtin##_code = instrumented_code (builtin##_code, sz); \
  397. vm_builtin_##builtin = \
  398. scm_cell (scm_tc7_program | SCM_F_PROGRAM_IS_PRIMITIVE, \
  399. (scm_t_bits)vm_builtin_##builtin##_code); \
  400. }
  401. FOR_EACH_VM_BUILTIN (DEFINE_BUILTIN);
  402. #undef INDEX_TO_NAME
  403. scm_vm_intrinsics.handle_interrupt_code =
  404. instrumented_code (handle_interrupt_code, sizeof (handle_interrupt_code));
  405. }
  406. SCM
  407. scm_i_call_with_current_continuation (SCM proc)
  408. {
  409. return scm_call_1 (vm_builtin_call_with_current_continuation, proc);
  410. }
  411. /*
  412. * VM
  413. */
  414. #define VM_NAME vm_regular_engine
  415. #define VM_USE_HOOKS 0
  416. #define FUNC_NAME "vm-regular-engine"
  417. #include "vm-engine.c"
  418. #undef FUNC_NAME
  419. #undef VM_USE_HOOKS
  420. #undef VM_NAME
  421. #define VM_NAME vm_debug_engine
  422. #define VM_USE_HOOKS 1
  423. #define FUNC_NAME "vm-debug-engine"
  424. #include "vm-engine.c"
  425. #undef FUNC_NAME
  426. #undef VM_USE_HOOKS
  427. #undef VM_NAME
  428. typedef SCM (*scm_t_vm_engine) (scm_thread *current_thread);
  429. static const scm_t_vm_engine vm_engines[SCM_VM_NUM_ENGINES] =
  430. { vm_regular_engine, vm_debug_engine };
  431. static union scm_vm_stack_element*
  432. allocate_stack (size_t size)
  433. {
  434. void *ret;
  435. if (size >= ((size_t) -1) / sizeof (union scm_vm_stack_element))
  436. abort ();
  437. size *= sizeof (union scm_vm_stack_element);
  438. #if HAVE_SYS_MMAN_H
  439. ret = mmap (NULL, size, PROT_READ | PROT_WRITE,
  440. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  441. if (ret == NULL)
  442. /* Shouldn't happen. */
  443. abort ();
  444. if (ret == MAP_FAILED)
  445. ret = NULL;
  446. #else
  447. ret = malloc (size);
  448. #endif
  449. if (!ret)
  450. perror ("allocate_stack failed");
  451. return (union scm_vm_stack_element *) ret;
  452. }
  453. static void
  454. free_stack (union scm_vm_stack_element *stack, size_t size)
  455. {
  456. size *= sizeof (*stack);
  457. #if HAVE_SYS_MMAN_H
  458. munmap (stack, size);
  459. #else
  460. free (stack);
  461. #endif
  462. }
  463. /* Ideally what we would like is an mremap or a realloc that grows at
  464. the bottom, not the top. Oh well; mmap and memcpy are fast enough,
  465. considering that they run very infrequently. */
  466. static union scm_vm_stack_element*
  467. expand_stack (union scm_vm_stack_element *old_bottom, size_t old_size,
  468. size_t new_size)
  469. #define FUNC_NAME "expand_stack"
  470. {
  471. union scm_vm_stack_element *new_bottom;
  472. size_t extension_size;
  473. if (new_size >= ((size_t) -1) / sizeof (union scm_vm_stack_element))
  474. abort ();
  475. if (new_size <= old_size)
  476. abort ();
  477. extension_size = new_size - old_size;
  478. if ((size_t)old_bottom < extension_size * sizeof (union scm_vm_stack_element))
  479. abort ();
  480. new_bottom = allocate_stack (new_size);
  481. if (!new_bottom)
  482. return NULL;
  483. memcpy (new_bottom + extension_size,
  484. old_bottom,
  485. old_size * sizeof (union scm_vm_stack_element));
  486. free_stack (old_bottom, old_size);
  487. return new_bottom;
  488. }
  489. #undef FUNC_NAME
  490. void
  491. scm_i_vm_prepare_stack (struct scm_vm *vp)
  492. {
  493. /* Not racey, as this will be run the first time a thread enters
  494. Guile. */
  495. if (page_size == 0)
  496. {
  497. page_size = getpagesize ();
  498. /* page_size should be a power of two. */
  499. if (page_size & (page_size - 1))
  500. abort ();
  501. }
  502. vp->stack_size = page_size / sizeof (union scm_vm_stack_element);
  503. vp->stack_bottom = allocate_stack (vp->stack_size);
  504. if (!vp->stack_bottom)
  505. /* As in expand_stack, we don't have any way to throw an exception
  506. if we can't allocate one measely page -- there's no stack to
  507. handle it. For now, abort. */
  508. abort ();
  509. vp->stack_top = vp->stack_bottom + vp->stack_size;
  510. vp->stack_limit = vp->stack_bottom;
  511. vp->overflow_handler_stack = SCM_EOL;
  512. vp->ip = NULL;
  513. vp->sp = vp->stack_top;
  514. vp->fp = vp->stack_top;
  515. vp->compare_result = SCM_F_COMPARE_NONE;
  516. vp->engine = vm_default_engine;
  517. vp->trace_level = 0;
  518. #define INIT_HOOK(h) vp->h##_hook = SCM_BOOL_F;
  519. FOR_EACH_HOOK (INIT_HOOK)
  520. #undef INIT_HOOK
  521. }
  522. static void
  523. return_unused_stack_to_os (struct scm_vm *vp)
  524. {
  525. #if HAVE_SYS_MMAN_H
  526. uintptr_t lo = (uintptr_t) vp->stack_bottom;
  527. uintptr_t hi = (uintptr_t) vp->sp;
  528. lo &= ~(page_size - 1U); /* round down */
  529. hi &= ~(page_size - 1U); /* round down */
  530. /* Return these pages to the OS. The next time they are paged in,
  531. they will be zeroed. */
  532. if (lo < hi)
  533. {
  534. int ret = 0;
  535. do
  536. ret = madvise ((void *) lo, hi - lo, MADV_DONTNEED);
  537. while (ret && errno == EAGAIN);
  538. /* If the OS doesn't implement 'madvise' (as is currently the case
  539. for GNU/Hurd), don't warn the user since there's nothing they
  540. can do about it. */
  541. if (ret && errno != ENOSYS)
  542. perror ("madvise failed");
  543. }
  544. #endif
  545. }
  546. #define SLOT_MAP_CACHE_SIZE 32U
  547. struct slot_map_cache_entry
  548. {
  549. uint32_t *ip;
  550. const uint8_t *map;
  551. };
  552. struct slot_map_cache
  553. {
  554. struct slot_map_cache_entry entries[SLOT_MAP_CACHE_SIZE];
  555. };
  556. static const uint8_t *
  557. find_slot_map (uint32_t *ip, struct slot_map_cache *cache)
  558. {
  559. /* The lower two bits should be zero. FIXME: Use a better hash
  560. function; we don't expose scm_raw_hashq currently. */
  561. size_t slot = (((uintptr_t) ip) >> 2) % SLOT_MAP_CACHE_SIZE;
  562. const uint8_t *map;
  563. if (cache->entries[slot].ip == ip)
  564. map = cache->entries[slot].map;
  565. else
  566. {
  567. map = scm_find_slot_map_unlocked (ip);
  568. cache->entries[slot].ip = ip;
  569. cache->entries[slot].map = map;
  570. }
  571. return map;
  572. }
  573. enum slot_desc
  574. {
  575. SLOT_DESC_DEAD = 0,
  576. SLOT_DESC_LIVE_RAW = 1,
  577. SLOT_DESC_LIVE_GC = 2,
  578. SLOT_DESC_UNUSED = 3
  579. };
  580. /* Mark the active VM stack region. */
  581. struct GC_ms_entry *
  582. scm_i_vm_mark_stack (struct scm_vm *vp, struct GC_ms_entry *mark_stack_ptr,
  583. struct GC_ms_entry *mark_stack_limit)
  584. {
  585. union scm_vm_stack_element *sp, *fp;
  586. /* The first frame will be marked conservatively (without a slot map).
  587. This is because GC can happen at any point within the hottest
  588. activation, due to multiple threads or per-instruction hooks, and
  589. providing slot maps for all points in a program would take a
  590. prohibitive amount of space. */
  591. const uint8_t *slot_map = NULL;
  592. void *upper = (void *) GC_greatest_plausible_heap_addr;
  593. void *lower = (void *) GC_least_plausible_heap_addr;
  594. struct slot_map_cache cache;
  595. memset (&cache, 0, sizeof (cache));
  596. for (fp = vp->fp, sp = vp->sp;
  597. fp < vp->stack_top;
  598. fp = SCM_FRAME_DYNAMIC_LINK (fp))
  599. {
  600. ptrdiff_t nlocals = SCM_FRAME_NUM_LOCALS (fp, sp);
  601. size_t slot = nlocals - 1;
  602. for (slot = nlocals - 1; sp < fp; sp++, slot--)
  603. {
  604. enum slot_desc desc = SLOT_DESC_LIVE_GC;
  605. if (slot_map)
  606. desc = (slot_map[slot / 4U] >> ((slot % 4U) * 2)) & 3U;
  607. switch (desc)
  608. {
  609. case SLOT_DESC_LIVE_RAW:
  610. break;
  611. case SLOT_DESC_UNUSED:
  612. case SLOT_DESC_LIVE_GC:
  613. if (SCM_NIMP (sp->as_scm) &&
  614. sp->as_ptr >= lower && sp->as_ptr <= upper)
  615. mark_stack_ptr = GC_mark_and_push (sp->as_ptr,
  616. mark_stack_ptr,
  617. mark_stack_limit,
  618. NULL);
  619. break;
  620. case SLOT_DESC_DEAD:
  621. /* This value may become dead as a result of GC,
  622. so we can't just leave it on the stack. */
  623. sp->as_scm = SCM_UNSPECIFIED;
  624. break;
  625. }
  626. }
  627. sp = SCM_FRAME_PREVIOUS_SP (fp);
  628. /* Inner frames may have a dead slots map for precise marking.
  629. Note that there may be other reasons to not have a dead slots
  630. map, e.g. if all of the frame's slots below the callee frame
  631. are live. */
  632. slot_map = find_slot_map (SCM_FRAME_VIRTUAL_RETURN_ADDRESS (fp), &cache);
  633. }
  634. return_unused_stack_to_os (vp);
  635. return mark_stack_ptr;
  636. }
  637. /* Free the VM stack, as this thread is exiting. */
  638. void
  639. scm_i_vm_free_stack (struct scm_vm *vp)
  640. {
  641. free_stack (vp->stack_bottom, vp->stack_size);
  642. /* Not strictly necessary, but good to avoid confusion when debugging
  643. thread-related GC issues. */
  644. memset (vp, 0, sizeof (*vp));
  645. }
  646. struct vm_expand_stack_data
  647. {
  648. struct scm_vm *vp;
  649. size_t stack_size;
  650. union scm_vm_stack_element *new_sp;
  651. };
  652. static void *
  653. vm_expand_stack_inner (void *data_ptr)
  654. {
  655. struct vm_expand_stack_data *data = data_ptr;
  656. struct scm_vm *vp = data->vp;
  657. union scm_vm_stack_element *old_top, *new_bottom;
  658. size_t new_size;
  659. ptrdiff_t reloc;
  660. old_top = vp->stack_top;
  661. new_size = vp->stack_size;
  662. while (new_size < data->stack_size)
  663. new_size *= 2;
  664. new_bottom = expand_stack (vp->stack_bottom, vp->stack_size, new_size);
  665. if (!new_bottom)
  666. return NULL;
  667. vp->stack_bottom = new_bottom;
  668. vp->stack_size = new_size;
  669. vp->stack_top = vp->stack_bottom + new_size;
  670. vp->stack_limit = vp->stack_bottom;
  671. reloc = vp->stack_top - old_top;
  672. if (vp->fp)
  673. vp->fp += reloc;
  674. data->new_sp += reloc;
  675. return new_bottom;
  676. }
  677. static ptrdiff_t
  678. current_overflow_size (struct scm_vm *vp)
  679. {
  680. if (scm_is_pair (vp->overflow_handler_stack))
  681. return scm_to_ptrdiff_t (scm_caar (vp->overflow_handler_stack));
  682. return -1;
  683. }
  684. static int
  685. should_handle_stack_overflow (struct scm_vm *vp, ptrdiff_t stack_size)
  686. {
  687. ptrdiff_t overflow_size = current_overflow_size (vp);
  688. return overflow_size >= 0 && stack_size >= overflow_size;
  689. }
  690. static void
  691. reset_stack_limit (struct scm_vm *vp)
  692. {
  693. if (should_handle_stack_overflow (vp, vp->stack_size))
  694. vp->stack_limit = vp->stack_top - current_overflow_size (vp);
  695. else
  696. vp->stack_limit = vp->stack_bottom;
  697. }
  698. struct overflow_handler_data
  699. {
  700. struct scm_vm *vp;
  701. SCM overflow_handler_stack;
  702. };
  703. static void
  704. wind_overflow_handler (void *ptr)
  705. {
  706. struct overflow_handler_data *data = ptr;
  707. data->vp->overflow_handler_stack = data->overflow_handler_stack;
  708. reset_stack_limit (data->vp);
  709. }
  710. static void
  711. unwind_overflow_handler (void *ptr)
  712. {
  713. struct overflow_handler_data *data = ptr;
  714. data->vp->overflow_handler_stack = scm_cdr (data->overflow_handler_stack);
  715. reset_stack_limit (data->vp);
  716. }
  717. static void
  718. vm_expand_stack (struct scm_vm *vp, union scm_vm_stack_element *new_sp)
  719. {
  720. ptrdiff_t stack_size = vp->stack_top - new_sp;
  721. if (stack_size > vp->stack_size)
  722. {
  723. struct vm_expand_stack_data data;
  724. data.vp = vp;
  725. data.stack_size = stack_size;
  726. data.new_sp = new_sp;
  727. if (!GC_call_with_alloc_lock (vm_expand_stack_inner, &data))
  728. /* Throw an unwind-only exception. */
  729. scm_report_stack_overflow ();
  730. new_sp = data.new_sp;
  731. }
  732. vp->sp = new_sp;
  733. if (should_handle_stack_overflow (vp, stack_size))
  734. {
  735. SCM more_stack, new_limit;
  736. struct overflow_handler_data data;
  737. data.vp = vp;
  738. data.overflow_handler_stack = vp->overflow_handler_stack;
  739. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  740. scm_dynwind_rewind_handler (unwind_overflow_handler, &data,
  741. SCM_F_WIND_EXPLICITLY);
  742. scm_dynwind_unwind_handler (wind_overflow_handler, &data,
  743. SCM_F_WIND_EXPLICITLY);
  744. /* Call the overflow handler. */
  745. more_stack = scm_call_0 (scm_cdar (data.overflow_handler_stack));
  746. /* If the overflow handler returns, its return value should be an
  747. integral number of words from the outer stack limit to transfer
  748. to the inner limit. */
  749. if (scm_to_ptrdiff_t (more_stack) <= 0)
  750. scm_out_of_range (NULL, more_stack);
  751. new_limit = scm_sum (scm_caar (data.overflow_handler_stack), more_stack);
  752. if (scm_is_pair (scm_cdr (data.overflow_handler_stack)))
  753. new_limit = scm_min (new_limit,
  754. scm_caadr (data.overflow_handler_stack));
  755. /* Ensure the new limit is in range. */
  756. scm_to_ptrdiff_t (new_limit);
  757. /* Increase the limit that we will restore. */
  758. scm_set_car_x (scm_car (data.overflow_handler_stack), new_limit);
  759. scm_dynwind_end ();
  760. /* Recurse. */
  761. return vm_expand_stack (vp, new_sp);
  762. }
  763. }
  764. static uint32_t
  765. frame_locals_count (scm_thread *thread)
  766. {
  767. return SCM_FRAME_NUM_LOCALS (thread->vm.fp, thread->vm.sp);
  768. }
  769. static void
  770. thread_expand_stack (scm_thread *thread, union scm_vm_stack_element *new_sp)
  771. {
  772. vm_expand_stack (&thread->vm, new_sp);
  773. }
  774. /* This duplicates the inlined "ALLOC_FRAME" macro from vm-engine.c, but
  775. it seems to be necessary for perf; the inlined version avoids the
  776. needs to flush IP in the common case. */
  777. static void
  778. alloc_frame (scm_thread *thread, uint32_t nlocals)
  779. {
  780. union scm_vm_stack_element *sp = thread->vm.fp - nlocals;
  781. if (SCM_UNLIKELY (sp < thread->vm.stack_limit))
  782. thread_expand_stack (thread, sp);
  783. else
  784. thread->vm.sp = sp;
  785. }
  786. static uint32_t
  787. compute_kwargs_npositional (scm_thread *thread, uint32_t nreq, uint32_t nopt)
  788. {
  789. uint32_t npositional, nargs;
  790. nargs = frame_locals_count (thread);
  791. /* look in optionals for first keyword or last positional */
  792. /* starting after the last required positional arg */
  793. npositional = nreq;
  794. while (/* while we have args */
  795. npositional < nargs
  796. /* and we still have positionals to fill */
  797. && npositional < nreq + nopt
  798. /* and we haven't reached a keyword yet */
  799. && !scm_is_keyword (SCM_FRAME_LOCAL (thread->vm.fp, npositional)))
  800. /* bind this optional arg (by leaving it in place) */
  801. npositional++;
  802. return npositional;
  803. }
  804. static void
  805. bind_kwargs (scm_thread *thread, uint32_t npositional, uint32_t nlocals,
  806. SCM kwargs, uint8_t strict, uint8_t allow_other_keys)
  807. {
  808. uint32_t nargs, nkw, n;
  809. union scm_vm_stack_element *fp;
  810. nargs = frame_locals_count (thread);
  811. nkw = nargs - npositional;
  812. /* shuffle non-positional arguments above nlocals */
  813. alloc_frame (thread, nlocals + nkw);
  814. fp = thread->vm.fp;
  815. n = nkw;
  816. while (n--)
  817. SCM_FRAME_LOCAL (fp, nlocals + n) = SCM_FRAME_LOCAL (fp, npositional + n);
  818. /* Fill optionals & keyword args with SCM_UNDEFINED */
  819. n = npositional;
  820. while (n < nlocals)
  821. SCM_FRAME_LOCAL (fp, n++) = SCM_UNDEFINED;
  822. /* Now bind keywords, in the order given. */
  823. for (n = 0; n < nkw; n++)
  824. {
  825. SCM kw = SCM_FRAME_LOCAL (fp, nlocals + n);
  826. if (scm_is_keyword (kw))
  827. {
  828. SCM walk;
  829. for (walk = kwargs; scm_is_pair (walk); walk = SCM_CDR (walk))
  830. if (scm_is_eq (SCM_CAAR (walk), kw))
  831. {
  832. SCM si = SCM_CDAR (walk);
  833. if (n + 1 < nkw)
  834. SCM_FRAME_LOCAL (fp, scm_to_uint32 (si)) =
  835. SCM_FRAME_LOCAL (fp, nlocals + n + 1);
  836. else
  837. scm_error_scm (sym_keyword_argument_error, SCM_BOOL_F,
  838. scm_from_latin1_string
  839. ("Keyword argument has no value"),
  840. SCM_EOL, scm_list_1 (kw));
  841. break;
  842. }
  843. if (!allow_other_keys && !scm_is_pair (walk))
  844. scm_error_scm (sym_keyword_argument_error, SCM_BOOL_F,
  845. scm_from_latin1_string ("Unrecognized keyword"),
  846. SCM_EOL, scm_list_1 (kw));
  847. n++;
  848. }
  849. else if (strict)
  850. {
  851. scm_error_scm (sym_keyword_argument_error, SCM_BOOL_F,
  852. scm_from_latin1_string ("Invalid keyword"),
  853. SCM_EOL, scm_list_1 (kw));
  854. }
  855. else
  856. {
  857. /* Ignore this argument. It might get consed onto a rest list. */
  858. }
  859. }
  860. }
  861. static SCM
  862. cons_rest (scm_thread *thread, uint32_t base)
  863. {
  864. SCM rest = SCM_EOL;
  865. uint32_t n = frame_locals_count (thread) - base;
  866. while (n--)
  867. rest = scm_inline_cons (thread, SCM_FRAME_LOCAL (thread->vm.fp, base + n),
  868. rest);
  869. return rest;
  870. }
  871. static void
  872. push_interrupt_frame (scm_thread *thread, uint8_t *mra)
  873. {
  874. union scm_vm_stack_element *old_fp, *new_fp;
  875. size_t frame_overhead = 3;
  876. size_t old_frame_size = frame_locals_count (thread);
  877. SCM proc = scm_i_async_pop (thread);
  878. #if ENABLE_JIT
  879. if (!mra)
  880. mra = scm_jit_return_to_interpreter_trampoline;
  881. #endif
  882. /* Reserve space for frame and callee. */
  883. alloc_frame (thread, old_frame_size + frame_overhead + 1);
  884. old_fp = thread->vm.fp;
  885. new_fp = SCM_FRAME_SLOT (old_fp, old_frame_size + frame_overhead - 1);
  886. SCM_FRAME_SET_DYNAMIC_LINK (new_fp, old_fp);
  887. /* Arrange to return to the same handle-interrupts opcode to handle
  888. any additional interrupts. */
  889. SCM_FRAME_SET_VIRTUAL_RETURN_ADDRESS (new_fp, thread->vm.ip);
  890. SCM_FRAME_SET_MACHINE_RETURN_ADDRESS (new_fp, mra);
  891. SCM_FRAME_LOCAL (new_fp, 0) = proc;
  892. thread->vm.fp = new_fp;
  893. }
  894. struct return_to_continuation_data
  895. {
  896. struct scm_vm_cont *cp;
  897. struct scm_vm *vp;
  898. };
  899. /* Called with the GC lock to prevent the stack marker from traversing a
  900. stack in an inconsistent state. */
  901. static void *
  902. vm_return_to_continuation_inner (void *data_ptr)
  903. {
  904. struct return_to_continuation_data *data = data_ptr;
  905. struct scm_vm *vp = data->vp;
  906. struct scm_vm_cont *cp = data->cp;
  907. /* We know that there is enough space for the continuation, because we
  908. captured it in the past. However there may have been an expansion
  909. since the capture, so we may have to re-link the frame
  910. pointers. */
  911. memcpy (vp->stack_top - cp->stack_size,
  912. cp->stack_bottom,
  913. cp->stack_size * sizeof (*cp->stack_bottom));
  914. vp->fp = vp->stack_top - cp->fp_offset;
  915. vm_restore_sp (vp, vp->stack_top - cp->stack_size);
  916. return NULL;
  917. }
  918. static void reinstate_continuation_x (scm_thread *thread, SCM cont) SCM_NORETURN;
  919. static void
  920. reinstate_continuation_x (scm_thread *thread, SCM cont)
  921. {
  922. scm_t_contregs *continuation = scm_i_contregs (cont);
  923. struct scm_vm *vp = &thread->vm;
  924. struct scm_vm_cont *cp;
  925. size_t n, i, frame_overhead = 3;
  926. union scm_vm_stack_element *argv;
  927. struct return_to_continuation_data data;
  928. if (!scm_is_eq (continuation->root, thread->continuation_root))
  929. scm_misc_error
  930. ("%continuation-call",
  931. "invoking continuation would cross continuation barrier: ~A",
  932. scm_list_1 (cont));
  933. n = frame_locals_count (thread) - 1;
  934. argv = alloca (n * sizeof (*argv));
  935. memcpy (argv, vp->sp, n * sizeof (*argv));
  936. cp = SCM_VM_CONT_DATA (continuation->vm_cont);
  937. data.cp = cp;
  938. data.vp = vp;
  939. GC_call_with_alloc_lock (vm_return_to_continuation_inner, &data);
  940. /* Now we have the continuation properly copied over. We just need to
  941. copy on an empty frame and the return values, as the continuation
  942. expects. */
  943. vm_push_sp (vp, vp->sp - frame_overhead - n);
  944. for (i = 0; i < frame_overhead; i++)
  945. vp->sp[n+i].as_scm = SCM_BOOL_F;
  946. memcpy(vp->sp, argv, n * sizeof (union scm_vm_stack_element));
  947. vp->ip = cp->vra;
  948. scm_i_reinstate_continuation (cont, cp->mra);
  949. }
  950. static SCM
  951. capture_continuation (scm_thread *thread)
  952. {
  953. struct scm_vm *vp = &thread->vm;
  954. void *mra = SCM_FRAME_MACHINE_RETURN_ADDRESS (vp->fp);
  955. #if ENABLE_JIT
  956. if (mra == scm_jit_return_to_interpreter_trampoline)
  957. mra = NULL;
  958. #endif
  959. SCM vm_cont = capture_stack (vp->stack_top,
  960. SCM_FRAME_DYNAMIC_LINK (vp->fp),
  961. SCM_FRAME_PREVIOUS_SP (vp->fp),
  962. SCM_FRAME_VIRTUAL_RETURN_ADDRESS (vp->fp),
  963. mra,
  964. scm_dynstack_capture_all (&thread->dynstack),
  965. 0);
  966. return scm_i_make_continuation (thread, vm_cont);
  967. }
  968. struct compose_continuation_data
  969. {
  970. struct scm_vm *vp;
  971. struct scm_vm_cont *cp;
  972. };
  973. static void *
  974. compose_continuation_inner (void *data_ptr)
  975. {
  976. struct compose_continuation_data *data = data_ptr;
  977. struct scm_vm *vp = data->vp;
  978. struct scm_vm_cont *cp = data->cp;
  979. memcpy (vp->fp - cp->stack_size,
  980. cp->stack_bottom,
  981. cp->stack_size * sizeof (*cp->stack_bottom));
  982. vp->fp -= cp->fp_offset;
  983. vp->ip = cp->vra;
  984. return cp->mra;
  985. }
  986. static uint8_t*
  987. compose_continuation (scm_thread *thread, SCM cont)
  988. {
  989. struct scm_vm *vp = &thread->vm;
  990. size_t nargs;
  991. struct compose_continuation_data data;
  992. struct scm_vm_cont *cp;
  993. union scm_vm_stack_element *args;
  994. ptrdiff_t old_fp_offset;
  995. uint8_t *mra;
  996. if (SCM_UNLIKELY (! SCM_VM_CONT_REWINDABLE_P (cont)))
  997. scm_wrong_type_arg_msg (NULL, 0, cont, "resumable continuation");
  998. #if ENABLE_JIT
  999. if (!SCM_FRAME_MACHINE_RETURN_ADDRESS (vp->fp))
  1000. SCM_FRAME_SET_MACHINE_RETURN_ADDRESS
  1001. (vp->fp, scm_jit_return_to_interpreter_trampoline);
  1002. #endif
  1003. nargs = frame_locals_count (thread) - 1;
  1004. args = alloca (nargs * sizeof (*args));
  1005. memcpy (args, vp->sp, nargs * sizeof (*args));
  1006. cp = SCM_VM_CONT_DATA (cont);
  1007. old_fp_offset = vp->stack_top - vp->fp;
  1008. vm_push_sp (vp, vp->fp - (cp->stack_size + nargs));
  1009. data.vp = vp;
  1010. data.cp = cp;
  1011. mra = GC_call_with_alloc_lock (compose_continuation_inner, &data);
  1012. /* The resumed continuation will expect ARGS on the stack as if from a
  1013. multiple-value return. */
  1014. memcpy (vp->sp, args, nargs * sizeof (*args));
  1015. /* The prompt captured a slice of the dynamic stack. Here we wind
  1016. those entries onto the current thread's stack. We also have to
  1017. relocate any prompts that we see along the way. */
  1018. {
  1019. scm_t_bits *walk;
  1020. for (walk = SCM_DYNSTACK_FIRST (cp->dynstack);
  1021. SCM_DYNSTACK_TAG (walk);
  1022. walk = SCM_DYNSTACK_NEXT (walk))
  1023. {
  1024. scm_t_bits tag = SCM_DYNSTACK_TAG (walk);
  1025. if (SCM_DYNSTACK_TAG_TYPE (tag) == SCM_DYNSTACK_TYPE_PROMPT)
  1026. scm_dynstack_wind_prompt (&thread->dynstack, walk, old_fp_offset,
  1027. thread->vm.registers);
  1028. else
  1029. scm_dynstack_wind_1 (&thread->dynstack, walk);
  1030. }
  1031. }
  1032. return mra;
  1033. }
  1034. static void
  1035. expand_apply_argument (scm_thread *thread)
  1036. {
  1037. SCM x = thread->vm.sp[0].as_scm;
  1038. int len = scm_ilength (x);
  1039. if (SCM_UNLIKELY (len < 0))
  1040. scm_error (scm_arg_type_key, "apply", "Apply to non-list: ~S",
  1041. scm_list_1 (x), scm_list_1 (x));
  1042. alloc_frame (thread, frame_locals_count (thread) - 1 + len);
  1043. while (len--)
  1044. {
  1045. thread->vm.sp[len].as_scm = SCM_CAR (x);
  1046. x = SCM_CDR (x);
  1047. }
  1048. }
  1049. /* This is here to avoid putting the code for "alloc-frame" in subr
  1050. calls. */
  1051. static void
  1052. unpack_values_object (scm_thread *thread, SCM obj)
  1053. {
  1054. size_t n, nvals = scm_i_nvalues (obj);
  1055. alloc_frame (thread, nvals);
  1056. for (n = 0; n < nvals; n++)
  1057. SCM_FRAME_LOCAL (thread->vm.fp, n) = scm_i_value_ref (obj, n);
  1058. }
  1059. static void
  1060. foreign_call (scm_thread *thread, SCM cif, SCM pointer)
  1061. {
  1062. SCM ret;
  1063. int err = 0;
  1064. ret = scm_i_foreign_call (cif, pointer, &err, thread->vm.sp);
  1065. alloc_frame (thread, 2);
  1066. SCM_FRAME_LOCAL (thread->vm.fp, 0) = ret;
  1067. SCM_FRAME_LOCAL (thread->vm.fp, 1) = scm_from_int (err);
  1068. }
  1069. static SCM
  1070. capture_delimited_continuation (struct scm_vm *vp,
  1071. union scm_vm_stack_element *saved_fp,
  1072. uint8_t *saved_mra,
  1073. jmp_buf *saved_registers,
  1074. scm_t_dynstack *dynstack,
  1075. jmp_buf *current_registers)
  1076. {
  1077. SCM vm_cont;
  1078. uint32_t flags;
  1079. union scm_vm_stack_element *base_fp;
  1080. flags = SCM_F_VM_CONT_PARTIAL;
  1081. /* If we are aborting to a prompt that has the same registers as those
  1082. of the abort, it means there are no intervening C frames on the
  1083. stack, and so the continuation can be relocated elsewhere on the
  1084. stack: it is rewindable. */
  1085. if (saved_registers && saved_registers == current_registers)
  1086. flags |= SCM_F_VM_CONT_REWINDABLE;
  1087. /* Walk the stack until we find the first frame newer than saved_fp.
  1088. We will save the stack until that frame. It used to be that we
  1089. could determine the stack base in O(1) time, but that's no longer
  1090. the case, since the thunk application doesn't occur where the
  1091. prompt is saved. */
  1092. for (base_fp = vp->fp;
  1093. SCM_FRAME_DYNAMIC_LINK (base_fp) < saved_fp;
  1094. base_fp = SCM_FRAME_DYNAMIC_LINK (base_fp));
  1095. if (SCM_FRAME_DYNAMIC_LINK (base_fp) != saved_fp)
  1096. abort();
  1097. scm_dynstack_relocate_prompts (dynstack, vp->stack_top - base_fp);
  1098. /* Capture from the base_fp to the top thunk application frame. Don't
  1099. capture values from the most recent frame, as they are the abort
  1100. args. */
  1101. vm_cont = capture_stack (base_fp, vp->fp, vp->fp, vp->ip,
  1102. saved_mra, dynstack, flags);
  1103. return scm_i_make_composable_continuation (vm_cont);
  1104. }
  1105. void
  1106. scm_i_vm_abort (SCM *tag_and_argv, size_t n)
  1107. {
  1108. scm_call_n (vm_builtin_abort_to_prompt, tag_and_argv, n);
  1109. /* Unreachable. */
  1110. abort ();
  1111. }
  1112. /* The same as scm_i_vm_abort(), but possibly called in response to
  1113. resource allocation failures, so we might not be able to make a
  1114. call, as that might require stack expansion. Grrr. */
  1115. void
  1116. scm_i_vm_emergency_abort (SCM *tag_and_argv, size_t n)
  1117. {
  1118. scm_thread *thread = SCM_I_CURRENT_THREAD;
  1119. struct scm_vm *vp = &thread->vm;
  1120. scm_t_dynstack *dynstack = &thread->dynstack;
  1121. SCM tag, cont;
  1122. size_t nargs;
  1123. scm_t_bits *prompt;
  1124. scm_t_dynstack_prompt_flags flags;
  1125. ptrdiff_t fp_offset, sp_offset;
  1126. union scm_vm_stack_element *fp, *sp;
  1127. SCM *argv;
  1128. uint32_t *vra;
  1129. uint8_t *mra;
  1130. jmp_buf *registers;
  1131. tag = tag_and_argv[0];
  1132. argv = tag_and_argv + 1;
  1133. nargs = n - 1;
  1134. prompt = scm_dynstack_find_prompt (dynstack, tag,
  1135. &flags, &fp_offset, &sp_offset,
  1136. &vra, &mra, &registers);
  1137. if (!prompt)
  1138. {
  1139. fprintf (stderr, "guile: fatal: emergency abort to unknown prompt\n");
  1140. abort ();
  1141. }
  1142. if (!(flags & SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY))
  1143. {
  1144. fprintf (stderr, "guile: fatal: emergency abort to non-linear prompt\n");
  1145. abort ();
  1146. }
  1147. cont = SCM_BOOL_F;
  1148. /* Unwind. */
  1149. scm_dynstack_unwind (dynstack, prompt);
  1150. fp = vp->stack_top - fp_offset;
  1151. sp = vp->stack_top - sp_offset;
  1152. /* Restore FP first so that a concurrent 'scm_i_vm_mark_stack' does
  1153. not overwrite the 'abort' arguments assigned below (see
  1154. <https://bugs.gnu.org/28211>). */
  1155. vp->fp = fp;
  1156. /* Continuation gets nargs+1 values: the one more is for the cont. */
  1157. sp = sp - nargs - 1;
  1158. /* Shuffle abort arguments down to the prompt continuation. We have
  1159. to be jumping to an older part of the stack. */
  1160. if (sp < vp->sp)
  1161. abort ();
  1162. sp[nargs].as_scm = cont;
  1163. while (nargs--)
  1164. sp[nargs].as_scm = *argv++;
  1165. /* Restore VM regs */
  1166. vp->sp = sp;
  1167. vp->ip = vra;
  1168. /* Jump! */
  1169. vp->mra_after_abort = mra;
  1170. longjmp (*registers, 1);
  1171. }
  1172. static uint8_t *
  1173. abort_to_prompt (scm_thread *thread, uint8_t *saved_mra)
  1174. {
  1175. struct scm_vm *vp = &thread->vm;
  1176. scm_t_dynstack *dynstack = &thread->dynstack;
  1177. SCM tag, cont;
  1178. size_t nargs;
  1179. scm_t_bits *prompt;
  1180. scm_t_dynstack_prompt_flags flags;
  1181. ptrdiff_t fp_offset, sp_offset;
  1182. union scm_vm_stack_element *fp, *sp;
  1183. uint32_t *vra;
  1184. uint8_t *mra;
  1185. jmp_buf *registers;
  1186. tag = SCM_FRAME_LOCAL (vp->fp, 1);
  1187. nargs = frame_locals_count (thread) - 2;
  1188. prompt = scm_dynstack_find_prompt (dynstack, tag,
  1189. &flags, &fp_offset, &sp_offset,
  1190. &vra, &mra, &registers);
  1191. if (!prompt)
  1192. scm_misc_error ("abort", "Abort to unknown prompt", scm_list_1 (tag));
  1193. fp = vp->stack_top - fp_offset;
  1194. /* Only reify if the continuation referenced in the handler. */
  1195. if (flags & SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY)
  1196. cont = SCM_BOOL_F;
  1197. else
  1198. {
  1199. scm_t_dynstack *captured;
  1200. captured = scm_dynstack_capture (dynstack, SCM_DYNSTACK_NEXT (prompt));
  1201. cont = capture_delimited_continuation (vp, fp, saved_mra, registers,
  1202. captured, thread->vm.registers);
  1203. }
  1204. /* Unwind. */
  1205. scm_dynstack_unwind (dynstack, prompt);
  1206. /* Recompute FP, as scm_dynstack_unwind may have expanded the stack. */
  1207. fp = vp->stack_top - fp_offset;
  1208. sp = vp->stack_top - sp_offset;
  1209. /* Continuation gets nargs+1 values: the one more is for the cont. */
  1210. sp = sp - nargs - 1;
  1211. /* Restore FP first so that a concurrent 'scm_i_vm_mark_stack' does
  1212. not overwrite the 'abort' arguments assigned below (see
  1213. <https://bugs.gnu.org/28211>). */
  1214. vp->fp = fp;
  1215. /* Shuffle abort arguments down to the prompt continuation. We have
  1216. to be jumping to an older part of the stack. */
  1217. if (sp < vp->sp)
  1218. abort ();
  1219. sp[nargs].as_scm = cont;
  1220. while (nargs--)
  1221. sp[nargs] = vp->sp[nargs];
  1222. /* Restore VM regs */
  1223. vp->sp = sp;
  1224. vp->ip = vra;
  1225. /* If there are intervening C frames, then jump over them, making a
  1226. nonlocal exit. Otherwise fall through and let the VM pick up where
  1227. it left off. */
  1228. if (thread->vm.registers != registers)
  1229. {
  1230. vp->mra_after_abort = mra;
  1231. longjmp (*registers, 1);
  1232. }
  1233. return mra;
  1234. }
  1235. static uint32_t *
  1236. get_callee_vcode (scm_thread *thread)
  1237. {
  1238. struct scm_vm *vp = &thread->vm;
  1239. SCM proc = SCM_FRAME_LOCAL (vp->fp, 0);
  1240. if (SCM_LIKELY (SCM_PROGRAM_P (proc)))
  1241. return SCM_PROGRAM_CODE (proc);
  1242. while (SCM_STRUCTP (proc) && SCM_STRUCT_APPLICABLE_P (proc))
  1243. {
  1244. proc = SCM_STRUCT_PROCEDURE (proc);
  1245. SCM_FRAME_LOCAL (vp->fp, 0) = proc;
  1246. if (SCM_PROGRAM_P (proc))
  1247. return SCM_PROGRAM_CODE (proc);
  1248. }
  1249. if (SCM_HAS_TYP7 (proc, scm_tc7_smob) && SCM_SMOB_APPLICABLE_P (proc))
  1250. {
  1251. uint32_t n = frame_locals_count (thread);
  1252. alloc_frame (thread, n + 1);
  1253. /* Although we could make VM modifications to avoid this shuffle,
  1254. it's easier to piggy-back on the subr arg parsing machinery.
  1255. Hopefully applicable smobs will go away in the mid-term. */
  1256. while (n--)
  1257. SCM_FRAME_LOCAL (vp->fp, n + 1) = SCM_FRAME_LOCAL (vp->fp, n);
  1258. proc = SCM_SMOB_DESCRIPTOR (proc).apply_trampoline;
  1259. SCM_FRAME_LOCAL (vp->fp, 0) = proc;
  1260. return SCM_PROGRAM_CODE (proc);
  1261. }
  1262. vp->ip = SCM_FRAME_VIRTUAL_RETURN_ADDRESS (vp->fp);
  1263. scm_error (scm_arg_type_key, NULL, "Wrong type to apply: ~S",
  1264. scm_list_1 (proc), scm_list_1 (proc));
  1265. }
  1266. SCM
  1267. scm_call_n (SCM proc, SCM *argv, size_t nargs)
  1268. {
  1269. scm_thread *thread;
  1270. struct scm_vm *vp;
  1271. union scm_vm_stack_element *return_fp, *call_fp;
  1272. /* Since nargs can only describe the length of a valid argv array in
  1273. elements and each element is at least 4 bytes, nargs will not be
  1274. greater than INTMAX/2 and therefore we don't have to check for
  1275. overflow here or below. */
  1276. size_t return_nlocals = 0, call_nlocals = nargs + 1, frame_size = 3;
  1277. ptrdiff_t stack_reserve_words;
  1278. size_t i;
  1279. thread = SCM_I_CURRENT_THREAD;
  1280. vp = &thread->vm;
  1281. SCM_CHECK_STACK;
  1282. /* It's not valid for argv to point into the stack already. */
  1283. if ((void *) argv < (void *) vp->stack_top &&
  1284. (void *) argv >= (void *) vp->sp)
  1285. abort();
  1286. /* Check that we have enough space for the two stack frames: the
  1287. innermost one that makes the call, and its continuation which
  1288. receives the resulting value(s) and returns from the engine
  1289. call. */
  1290. stack_reserve_words = call_nlocals + frame_size + return_nlocals + frame_size;
  1291. vm_push_sp (vp, vp->sp - stack_reserve_words);
  1292. call_fp = vp->sp + call_nlocals;
  1293. return_fp = call_fp + frame_size + return_nlocals;
  1294. SCM_FRAME_SET_VIRTUAL_RETURN_ADDRESS (return_fp, vp->ip);
  1295. SCM_FRAME_SET_MACHINE_RETURN_ADDRESS (return_fp, 0);
  1296. SCM_FRAME_SET_DYNAMIC_LINK (return_fp, vp->fp);
  1297. vp->ip = (uint32_t *) vm_boot_continuation_code;
  1298. SCM_FRAME_SET_VIRTUAL_RETURN_ADDRESS (call_fp, vp->ip);
  1299. SCM_FRAME_SET_MACHINE_RETURN_ADDRESS (call_fp, 0);
  1300. SCM_FRAME_SET_DYNAMIC_LINK (call_fp, return_fp);
  1301. SCM_FRAME_LOCAL (call_fp, 0) = proc;
  1302. for (i = 0; i < nargs; i++)
  1303. SCM_FRAME_LOCAL (call_fp, i + 1) = argv[i];
  1304. vp->fp = call_fp;
  1305. {
  1306. jmp_buf registers;
  1307. int resume;
  1308. jmp_buf *prev_registers = thread->vm.registers;
  1309. SCM ret;
  1310. resume = setjmp (registers);
  1311. thread->vm.registers = &registers;
  1312. if (SCM_UNLIKELY (resume))
  1313. {
  1314. uint8_t *mcode = vp->mra_after_abort;
  1315. scm_gc_after_nonlocal_exit ();
  1316. /* Non-local return. */
  1317. if (vp->abort_hook_enabled)
  1318. invoke_abort_hook (thread);
  1319. #if ENABLE_JIT
  1320. if (mcode && !vp->disable_mcode)
  1321. scm_jit_enter_mcode (thread, mcode);
  1322. #endif
  1323. }
  1324. else
  1325. vp->ip = get_callee_vcode (thread);
  1326. ret = vm_engines[vp->engine](thread);
  1327. thread->vm.registers = prev_registers;
  1328. return ret;
  1329. }
  1330. }
  1331. /* Scheme interface */
  1332. #define VM_ADD_HOOK(h, f) \
  1333. { \
  1334. scm_thread *t = SCM_I_CURRENT_THREAD; \
  1335. SCM hook = t->vm.h##_hook; \
  1336. if (scm_is_false (hook)) \
  1337. hook = t->vm.h##_hook = scm_make_hook (SCM_I_MAKINUM (1)); \
  1338. scm_add_hook_x (hook, f, SCM_UNDEFINED); \
  1339. vm_hook_compute_enabled (t, hook, &t->vm.h##_hook_enabled); \
  1340. vm_recompute_disable_mcode (t); \
  1341. return SCM_UNSPECIFIED; \
  1342. }
  1343. #define VM_REMOVE_HOOK(h, f) \
  1344. { \
  1345. scm_thread *t = SCM_I_CURRENT_THREAD; \
  1346. SCM hook = t->vm.h##_hook; \
  1347. if (scm_is_true (hook)) \
  1348. scm_remove_hook_x (hook, f); \
  1349. vm_hook_compute_enabled (t, hook, &t->vm.h##_hook_enabled); \
  1350. vm_recompute_disable_mcode (t); \
  1351. return SCM_UNSPECIFIED; \
  1352. }
  1353. SCM_DEFINE (scm_vm_add_apply_hook_x, "vm-add-apply-hook!", 1, 0, 0,
  1354. (SCM f),
  1355. "")
  1356. #define FUNC_NAME s_scm_vm_add_apply_hook_x
  1357. {
  1358. VM_ADD_HOOK (apply, f);
  1359. }
  1360. #undef FUNC_NAME
  1361. SCM_DEFINE (scm_vm_remove_apply_hook_x, "vm-remove-apply-hook!", 1, 0, 0,
  1362. (SCM f),
  1363. "")
  1364. #define FUNC_NAME s_scm_vm_remove_apply_hook_x
  1365. {
  1366. VM_REMOVE_HOOK (apply, f);
  1367. }
  1368. #undef FUNC_NAME
  1369. SCM_DEFINE (scm_vm_add_return_hook_x, "vm-add-return-hook!", 1, 0, 0,
  1370. (SCM f),
  1371. "")
  1372. #define FUNC_NAME s_scm_vm_add_return_hook_x
  1373. {
  1374. VM_ADD_HOOK (return, f);
  1375. }
  1376. #undef FUNC_NAME
  1377. SCM_DEFINE (scm_vm_remove_return_hook_x, "vm-remove-return-hook!", 1, 0, 0,
  1378. (SCM f),
  1379. "")
  1380. #define FUNC_NAME s_scm_vm_remove_return_hook_x
  1381. {
  1382. VM_REMOVE_HOOK (return, f);
  1383. }
  1384. #undef FUNC_NAME
  1385. SCM_DEFINE (scm_vm_add_next_hook_x, "vm-add-next-hook!", 1, 0, 0,
  1386. (SCM f),
  1387. "")
  1388. #define FUNC_NAME s_scm_vm_add_next_hook_x
  1389. {
  1390. VM_ADD_HOOK (next, f);
  1391. }
  1392. #undef FUNC_NAME
  1393. SCM_DEFINE (scm_vm_remove_next_hook_x, "vm-remove-next-hook!", 1, 0, 0,
  1394. (SCM f),
  1395. "")
  1396. #define FUNC_NAME s_scm_vm_remove_next_hook_x
  1397. {
  1398. VM_REMOVE_HOOK (next, f);
  1399. }
  1400. #undef FUNC_NAME
  1401. SCM_DEFINE (scm_vm_add_abort_hook_x, "vm-add-abort-hook!", 1, 0, 0,
  1402. (SCM f),
  1403. "")
  1404. #define FUNC_NAME s_scm_vm_add_abort_hook_x
  1405. {
  1406. VM_ADD_HOOK (abort, f);
  1407. }
  1408. #undef FUNC_NAME
  1409. SCM_DEFINE (scm_vm_remove_abort_hook_x, "vm-remove-abort-hook!", 1, 0, 0,
  1410. (SCM f),
  1411. "")
  1412. #define FUNC_NAME s_scm_vm_remove_abort_hook_x
  1413. {
  1414. VM_REMOVE_HOOK (abort, f);
  1415. }
  1416. #undef FUNC_NAME
  1417. SCM_DEFINE (scm_vm_trace_level, "vm-trace-level", 0, 0, 0,
  1418. (void),
  1419. "")
  1420. #define FUNC_NAME s_scm_vm_trace_level
  1421. {
  1422. return scm_from_int (SCM_I_CURRENT_THREAD->vm.trace_level);
  1423. }
  1424. #undef FUNC_NAME
  1425. SCM_DEFINE (scm_set_vm_trace_level_x, "set-vm-trace-level!", 1, 0, 0,
  1426. (SCM level),
  1427. "")
  1428. #define FUNC_NAME s_scm_set_vm_trace_level_x
  1429. {
  1430. scm_thread *thread = SCM_I_CURRENT_THREAD;
  1431. return scm_from_int (set_vm_trace_level (thread, scm_to_int (level)));
  1432. }
  1433. #undef FUNC_NAME
  1434. /*
  1435. * VM engines
  1436. */
  1437. static int
  1438. symbol_to_vm_engine (SCM engine, const char *FUNC_NAME)
  1439. {
  1440. if (scm_is_eq (engine, sym_regular))
  1441. return SCM_VM_REGULAR_ENGINE;
  1442. else if (scm_is_eq (engine, sym_debug))
  1443. return SCM_VM_DEBUG_ENGINE;
  1444. else
  1445. SCM_MISC_ERROR ("Unknown VM engine: ~a", scm_list_1 (engine));
  1446. }
  1447. static SCM
  1448. vm_engine_to_symbol (int engine, const char *FUNC_NAME)
  1449. {
  1450. switch (engine)
  1451. {
  1452. case SCM_VM_REGULAR_ENGINE:
  1453. return sym_regular;
  1454. case SCM_VM_DEBUG_ENGINE:
  1455. return sym_debug;
  1456. default:
  1457. /* ? */
  1458. SCM_MISC_ERROR ("Unknown VM engine: ~a",
  1459. scm_list_1 (scm_from_int (engine)));
  1460. }
  1461. }
  1462. SCM_DEFINE (scm_vm_engine, "vm-engine", 0, 0, 0,
  1463. (void),
  1464. "")
  1465. #define FUNC_NAME s_scm_vm_engine
  1466. {
  1467. return vm_engine_to_symbol (SCM_I_CURRENT_THREAD->vm.engine, FUNC_NAME);
  1468. }
  1469. #undef FUNC_NAME
  1470. void
  1471. scm_c_set_vm_engine_x (int engine)
  1472. #define FUNC_NAME "set-vm-engine!"
  1473. {
  1474. scm_thread *thread = SCM_I_CURRENT_THREAD;
  1475. if (engine < 0 || engine >= SCM_VM_NUM_ENGINES)
  1476. SCM_MISC_ERROR ("Unknown VM engine: ~a",
  1477. scm_list_1 (scm_from_int (engine)));
  1478. thread->vm.engine = engine;
  1479. /* Trigger update of the various hook_enabled flags. */
  1480. set_vm_trace_level (thread, thread->vm.trace_level);
  1481. }
  1482. #undef FUNC_NAME
  1483. SCM_DEFINE (scm_set_vm_engine_x, "set-vm-engine!", 1, 0, 0,
  1484. (SCM engine),
  1485. "")
  1486. #define FUNC_NAME s_scm_set_vm_engine_x
  1487. {
  1488. scm_c_set_vm_engine_x (symbol_to_vm_engine (engine, FUNC_NAME));
  1489. return SCM_UNSPECIFIED;
  1490. }
  1491. #undef FUNC_NAME
  1492. void
  1493. scm_c_set_default_vm_engine_x (int engine)
  1494. #define FUNC_NAME "set-default-vm-engine!"
  1495. {
  1496. if (engine < 0 || engine >= SCM_VM_NUM_ENGINES)
  1497. SCM_MISC_ERROR ("Unknown VM engine: ~a",
  1498. scm_list_1 (scm_from_int (engine)));
  1499. vm_default_engine = engine;
  1500. }
  1501. #undef FUNC_NAME
  1502. SCM_DEFINE (scm_set_default_vm_engine_x, "set-default-vm-engine!", 1, 0, 0,
  1503. (SCM engine),
  1504. "")
  1505. #define FUNC_NAME s_scm_set_default_vm_engine_x
  1506. {
  1507. scm_c_set_default_vm_engine_x (symbol_to_vm_engine (engine, FUNC_NAME));
  1508. return SCM_UNSPECIFIED;
  1509. }
  1510. #undef FUNC_NAME
  1511. /* FIXME: This function makes no sense, but we keep it to make sure we
  1512. have a way of switching to the debug or regular VM. */
  1513. SCM_DEFINE (scm_call_with_vm, "call-with-vm", 1, 0, 1,
  1514. (SCM proc, SCM args),
  1515. "Apply @var{proc} to @var{args} in a dynamic extent in which\n"
  1516. "@var{vm} is the current VM.")
  1517. #define FUNC_NAME s_scm_call_with_vm
  1518. {
  1519. return scm_apply_0 (proc, args);
  1520. }
  1521. #undef FUNC_NAME
  1522. SCM_DEFINE (scm_call_with_stack_overflow_handler,
  1523. "call-with-stack-overflow-handler", 3, 0, 0,
  1524. (SCM limit, SCM thunk, SCM handler),
  1525. "Call @var{thunk} in an environment in which the stack limit has\n"
  1526. "been reduced to @var{limit} additional words. If the limit is\n"
  1527. "reached, @var{handler} (a thunk) will be invoked in the dynamic\n"
  1528. "environment of the error. For the extent of the call to\n"
  1529. "@var{handler}, the stack limit and handler are restored to the\n"
  1530. "values that were in place when\n"
  1531. "@code{call-with-stack-overflow-handler} was called.")
  1532. #define FUNC_NAME s_scm_call_with_stack_overflow_handler
  1533. {
  1534. struct scm_thread *t = SCM_I_CURRENT_THREAD;
  1535. ptrdiff_t c_limit, stack_size;
  1536. struct overflow_handler_data data;
  1537. SCM new_limit, ret;
  1538. stack_size = t->vm.stack_top - t->vm.sp;
  1539. c_limit = scm_to_ptrdiff_t (limit);
  1540. if (c_limit <= 0)
  1541. scm_out_of_range (FUNC_NAME, limit);
  1542. new_limit = scm_sum (scm_from_ptrdiff_t (stack_size), limit);
  1543. if (scm_is_pair (t->vm.overflow_handler_stack))
  1544. new_limit = scm_min (new_limit, scm_caar (t->vm.overflow_handler_stack));
  1545. /* Hacky check that the current stack depth plus the limit is within
  1546. the range of a ptrdiff_t. */
  1547. scm_to_ptrdiff_t (new_limit);
  1548. data.vp = &t->vm;
  1549. data.overflow_handler_stack =
  1550. scm_acons (limit, handler, t->vm.overflow_handler_stack);
  1551. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  1552. scm_dynwind_rewind_handler (wind_overflow_handler, &data,
  1553. SCM_F_WIND_EXPLICITLY);
  1554. scm_dynwind_unwind_handler (unwind_overflow_handler, &data,
  1555. SCM_F_WIND_EXPLICITLY);
  1556. ret = scm_call_0 (thunk);
  1557. scm_dynwind_end ();
  1558. return ret;
  1559. }
  1560. #undef FUNC_NAME
  1561. /*
  1562. * Initialize
  1563. */
  1564. SCM
  1565. scm_load_compiled_with_vm (SCM file)
  1566. {
  1567. return scm_call_0 (scm_load_thunk_from_file (file));
  1568. }
  1569. void
  1570. scm_init_vm_builtin_properties (void)
  1571. {
  1572. /* FIXME: Seems hacky to do this here, but oh well :/ */
  1573. scm_sym_apply = scm_from_utf8_symbol ("apply");
  1574. scm_sym_values = scm_from_utf8_symbol ("values");
  1575. scm_sym_abort_to_prompt = scm_from_utf8_symbol ("abort-to-prompt");
  1576. scm_sym_call_with_values = scm_from_utf8_symbol ("call-with-values");
  1577. scm_sym_call_with_current_continuation =
  1578. scm_from_utf8_symbol ("call-with-current-continuation");
  1579. #define INIT_BUILTIN(builtin, BUILTIN, req, opt, rest) \
  1580. scm_set_procedure_property_x (vm_builtin_##builtin, scm_sym_name, \
  1581. scm_sym_##builtin);
  1582. FOR_EACH_VM_BUILTIN (INIT_BUILTIN);
  1583. #undef INIT_BUILTIN
  1584. }
  1585. void
  1586. scm_bootstrap_vm (void)
  1587. {
  1588. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  1589. "scm_init_vm",
  1590. (scm_t_extension_init_func)scm_init_vm, NULL);
  1591. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  1592. "scm_init_vm_builtins",
  1593. (scm_t_extension_init_func)scm_init_vm_builtins,
  1594. NULL);
  1595. scm_vm_intrinsics.expand_stack = thread_expand_stack;
  1596. scm_vm_intrinsics.cons_rest = cons_rest;
  1597. scm_vm_intrinsics.compute_kwargs_npositional = compute_kwargs_npositional;
  1598. scm_vm_intrinsics.bind_kwargs = bind_kwargs;
  1599. scm_vm_intrinsics.push_interrupt_frame = push_interrupt_frame;
  1600. scm_vm_intrinsics.reinstate_continuation_x = reinstate_continuation_x;
  1601. scm_vm_intrinsics.capture_continuation = capture_continuation;
  1602. scm_vm_intrinsics.compose_continuation = compose_continuation;
  1603. scm_vm_intrinsics.expand_apply_argument = expand_apply_argument;
  1604. scm_vm_intrinsics.abort_to_prompt = abort_to_prompt;
  1605. scm_vm_intrinsics.get_callee_vcode = get_callee_vcode;
  1606. scm_vm_intrinsics.unpack_values_object = unpack_values_object;
  1607. scm_vm_intrinsics.foreign_call = foreign_call;
  1608. sym_keyword_argument_error = scm_from_latin1_symbol ("keyword-argument-error");
  1609. sym_regular = scm_from_latin1_symbol ("regular");
  1610. sym_debug = scm_from_latin1_symbol ("debug");
  1611. vm_boot_continuation = scm_i_make_program (vm_boot_continuation_code);
  1612. SCM_SET_CELL_WORD_0 (vm_boot_continuation,
  1613. (SCM_CELL_WORD_0 (vm_boot_continuation)
  1614. | SCM_F_PROGRAM_IS_BOOT));
  1615. define_vm_builtins ();
  1616. }
  1617. void
  1618. scm_init_vm (void)
  1619. {
  1620. #ifndef SCM_MAGIC_SNARFER
  1621. #include "vm.x"
  1622. #endif
  1623. }