dynstack.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /* Copyright 2012-2013,2018
  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. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <assert.h>
  19. #include <setjmp.h>
  20. #include "control.h"
  21. #include "eval.h"
  22. #include "fluids.h"
  23. #include "variable.h"
  24. #include "threads.h"
  25. #include "dynstack.h"
  26. #define PROMPT_WORDS 6
  27. #define PROMPT_KEY(top) (SCM_PACK ((top)[0]))
  28. #define PROMPT_FP(top) ((ptrdiff_t) ((top)[1]))
  29. #define SET_PROMPT_FP(top, fp) do { top[1] = (scm_t_bits)(fp); } while (0)
  30. #define PROMPT_SP(top) ((ptrdiff_t) ((top)[2]))
  31. #define SET_PROMPT_SP(top, sp) do { top[2] = (scm_t_bits)(sp); } while (0)
  32. #define PROMPT_VRA(top) ((uint32_t *) ((top)[3]))
  33. #define PROMPT_MRA(top) ((uint8_t *) ((top)[4]))
  34. #define PROMPT_JMPBUF(top) ((jmp_buf *) ((top)[5]))
  35. #define WINDER_WORDS 2
  36. #define WINDER_PROC(top) ((scm_t_guard) ((top)[0]))
  37. #define WINDER_DATA(top) ((void *) ((top)[1]))
  38. #define DYNWIND_WORDS 2
  39. #define DYNWIND_ENTER(top) (SCM_PACK ((top)[0]))
  40. #define DYNWIND_LEAVE(top) (SCM_PACK ((top)[1]))
  41. #define WITH_FLUID_WORDS 2
  42. #define WITH_FLUID_FLUID(top) (SCM_PACK ((top)[0]))
  43. #define WITH_FLUID_VALUE_BOX(top) (SCM_PACK ((top)[1]))
  44. #define DYNAMIC_STATE_WORDS 1
  45. #define DYNAMIC_STATE_STATE_BOX(top) (SCM_PACK ((top)[0]))
  46. static void
  47. copy_scm_t_bits (scm_t_bits *dst, scm_t_bits *src, size_t n)
  48. {
  49. size_t i;
  50. for (i = 0; i < n; i++)
  51. dst[i] = src[i];
  52. }
  53. static void
  54. clear_scm_t_bits (scm_t_bits *items, size_t n)
  55. {
  56. size_t i;
  57. for (i = 0; i < n; i++)
  58. items[i] = 0;
  59. }
  60. /* Ensure space for N additional words. */
  61. static void
  62. dynstack_ensure_space (scm_t_dynstack *dynstack, size_t n)
  63. {
  64. size_t capacity = SCM_DYNSTACK_CAPACITY (dynstack);
  65. size_t height = SCM_DYNSTACK_HEIGHT (dynstack);
  66. n += SCM_DYNSTACK_HEADER_LEN;
  67. if (capacity < height + n)
  68. {
  69. scm_t_bits *new_base;
  70. while (capacity < height + n)
  71. capacity = (capacity < 4) ? 8 : (capacity * 2);
  72. new_base = scm_gc_malloc (capacity * sizeof(scm_t_bits), "dynstack");
  73. copy_scm_t_bits (new_base, dynstack->base, height);
  74. clear_scm_t_bits (dynstack->base, height);
  75. dynstack->base = new_base;
  76. dynstack->top = new_base + height;
  77. dynstack->limit = new_base + capacity;
  78. }
  79. }
  80. static inline scm_t_bits *
  81. push_dynstack_entry_unchecked (scm_t_dynstack *dynstack,
  82. scm_t_dynstack_item_type type,
  83. scm_t_bits flags, size_t len)
  84. {
  85. scm_t_bits *ret = dynstack->top;
  86. SCM_DYNSTACK_SET_TAG (dynstack->top, SCM_MAKE_DYNSTACK_TAG (type, flags, len));
  87. dynstack->top += SCM_DYNSTACK_HEADER_LEN + len;
  88. SCM_DYNSTACK_SET_TAG (dynstack->top, 0);
  89. SCM_DYNSTACK_SET_PREV_OFFSET (dynstack->top, SCM_DYNSTACK_HEADER_LEN + len);
  90. return ret;
  91. }
  92. static inline scm_t_bits *
  93. push_dynstack_entry (scm_t_dynstack *dynstack,
  94. scm_t_dynstack_item_type type,
  95. scm_t_bits flags, size_t len)
  96. {
  97. if (SCM_UNLIKELY (!SCM_DYNSTACK_HAS_SPACE (dynstack, len)))
  98. dynstack_ensure_space (dynstack, len);
  99. return push_dynstack_entry_unchecked (dynstack, type, flags, len);
  100. }
  101. void
  102. scm_dynstack_push_frame (scm_t_dynstack *dynstack,
  103. scm_t_dynstack_frame_flags flags)
  104. {
  105. push_dynstack_entry (dynstack, SCM_DYNSTACK_TYPE_FRAME, flags, 0);
  106. }
  107. void
  108. scm_dynstack_push_rewinder (scm_t_dynstack *dynstack,
  109. scm_t_dynstack_winder_flags flags,
  110. scm_t_guard proc, void *data)
  111. {
  112. scm_t_bits *words;
  113. words = push_dynstack_entry (dynstack, SCM_DYNSTACK_TYPE_REWINDER, flags,
  114. WINDER_WORDS);
  115. words[0] = (scm_t_bits) proc;
  116. words[1] = (scm_t_bits) data;
  117. }
  118. void
  119. scm_dynstack_push_unwinder (scm_t_dynstack *dynstack,
  120. scm_t_dynstack_winder_flags flags,
  121. scm_t_guard proc, void *data)
  122. {
  123. scm_t_bits *words;
  124. words = push_dynstack_entry (dynstack, SCM_DYNSTACK_TYPE_UNWINDER, flags,
  125. WINDER_WORDS);
  126. words[0] = (scm_t_bits) proc;
  127. words[1] = (scm_t_bits) data;
  128. }
  129. /* The fluid is stored on the stack, but the value has to be stored on the heap,
  130. so that all continuations that capture this dynamic scope capture the same
  131. binding. */
  132. void
  133. scm_dynstack_push_fluid (scm_t_dynstack *dynstack, SCM fluid, SCM value,
  134. scm_t_dynamic_state *dynamic_state)
  135. {
  136. scm_t_bits *words;
  137. SCM value_box;
  138. if (SCM_UNLIKELY (!SCM_FLUID_P (fluid)))
  139. scm_wrong_type_arg ("with-fluid*", 0, fluid);
  140. value_box = scm_make_variable (value);
  141. words = push_dynstack_entry (dynstack, SCM_DYNSTACK_TYPE_WITH_FLUID, 0,
  142. WITH_FLUID_WORDS);
  143. words[0] = SCM_UNPACK (fluid);
  144. words[1] = SCM_UNPACK (value_box);
  145. /* Go ahead and swap them. */
  146. scm_swap_fluid (fluid, value_box, dynamic_state);
  147. }
  148. void
  149. scm_dynstack_push_prompt (scm_t_dynstack *dynstack,
  150. scm_t_dynstack_prompt_flags flags,
  151. SCM key,
  152. ptrdiff_t fp_offset, ptrdiff_t sp_offset,
  153. uint32_t *vra, uint8_t *mra, jmp_buf *registers)
  154. {
  155. scm_t_bits *words;
  156. words = push_dynstack_entry (dynstack, SCM_DYNSTACK_TYPE_PROMPT, flags,
  157. PROMPT_WORDS);
  158. words[0] = SCM_UNPACK (key);
  159. words[1] = (scm_t_bits) fp_offset;
  160. words[2] = (scm_t_bits) sp_offset;
  161. words[3] = (scm_t_bits) vra;
  162. words[4] = (scm_t_bits) mra;
  163. words[5] = (scm_t_bits) registers;
  164. }
  165. void
  166. scm_dynstack_push_dynwind (scm_t_dynstack *dynstack, SCM enter, SCM leave)
  167. {
  168. scm_t_bits *words;
  169. words = push_dynstack_entry (dynstack, SCM_DYNSTACK_TYPE_DYNWIND, 0,
  170. DYNWIND_WORDS);
  171. words[0] = SCM_UNPACK (enter);
  172. words[1] = SCM_UNPACK (leave);
  173. }
  174. static inline scm_t_bits
  175. dynstack_pop (scm_t_dynstack *dynstack, scm_t_bits **words)
  176. {
  177. scm_t_bits *prev = SCM_DYNSTACK_PREV (dynstack->top);
  178. scm_t_bits tag;
  179. if (SCM_UNLIKELY (!prev))
  180. abort ();
  181. SCM_DYNSTACK_SET_PREV_OFFSET (dynstack->top, 0);
  182. dynstack->top = prev;
  183. tag = SCM_DYNSTACK_TAG (dynstack->top);
  184. SCM_DYNSTACK_SET_TAG (dynstack->top, 0);
  185. *words = dynstack->top;
  186. return tag;
  187. }
  188. void
  189. scm_dynstack_push_dynamic_state (scm_t_dynstack *dynstack, SCM state,
  190. scm_t_dynamic_state *dynamic_state)
  191. {
  192. scm_t_bits *words;
  193. SCM state_box;
  194. if (SCM_UNLIKELY (scm_is_false (scm_dynamic_state_p (state))))
  195. scm_wrong_type_arg ("with-dynamic-state", 0, state);
  196. state_box = scm_make_variable (scm_set_current_dynamic_state (state));
  197. words = push_dynstack_entry (dynstack, SCM_DYNSTACK_TYPE_DYNAMIC_STATE, 0,
  198. DYNAMIC_STATE_WORDS);
  199. words[0] = SCM_UNPACK (state_box);
  200. }
  201. void
  202. scm_dynstack_pop (scm_t_dynstack *dynstack)
  203. {
  204. scm_t_bits tag, *words;
  205. tag = dynstack_pop (dynstack, &words);
  206. clear_scm_t_bits (words, SCM_DYNSTACK_TAG_LEN (tag));
  207. }
  208. scm_t_dynstack *
  209. scm_dynstack_capture_all (scm_t_dynstack *dynstack)
  210. {
  211. return scm_dynstack_capture (dynstack, SCM_DYNSTACK_FIRST (dynstack));
  212. }
  213. scm_t_dynstack *
  214. scm_dynstack_capture (scm_t_dynstack *dynstack, scm_t_bits *item)
  215. {
  216. char *mem;
  217. scm_t_dynstack *ret;
  218. size_t len;
  219. assert (item >= SCM_DYNSTACK_FIRST (dynstack));
  220. assert (item <= dynstack->top);
  221. len = dynstack->top - item + SCM_DYNSTACK_HEADER_LEN;
  222. mem = scm_gc_malloc (sizeof (*ret) + len * sizeof(scm_t_bits), "dynstack");
  223. ret = (scm_t_dynstack *) mem;
  224. ret->base = (scm_t_bits *) (mem + sizeof (*ret));
  225. ret->limit = ret->base + len;
  226. ret->top = ret->base + len;
  227. copy_scm_t_bits (ret->base, item - SCM_DYNSTACK_HEADER_LEN, len);
  228. SCM_DYNSTACK_SET_PREV_OFFSET (SCM_DYNSTACK_FIRST (ret), 0);
  229. return ret;
  230. }
  231. void
  232. scm_dynstack_relocate_prompts (scm_t_dynstack *dynstack, ptrdiff_t base)
  233. {
  234. scm_t_bits *walk;
  235. /* Relocate prompts. */
  236. for (walk = dynstack->top; walk; walk = SCM_DYNSTACK_PREV (walk))
  237. {
  238. scm_t_bits tag = SCM_DYNSTACK_TAG (walk);
  239. if (SCM_DYNSTACK_TAG_TYPE (tag) == SCM_DYNSTACK_TYPE_PROMPT)
  240. {
  241. SET_PROMPT_FP (walk, PROMPT_FP (walk) - base);
  242. SET_PROMPT_SP (walk, PROMPT_SP (walk) - base);
  243. }
  244. }
  245. }
  246. void
  247. scm_dynstack_wind_1 (scm_t_dynstack *dynstack, scm_t_bits *item)
  248. {
  249. scm_t_bits tag = SCM_DYNSTACK_TAG (item);
  250. scm_t_dynstack_item_type type = SCM_DYNSTACK_TAG_TYPE (tag);
  251. scm_t_bits flags = SCM_DYNSTACK_TAG_FLAGS (tag);
  252. size_t len = SCM_DYNSTACK_TAG_LEN (tag);
  253. switch (type)
  254. {
  255. case SCM_DYNSTACK_TYPE_FRAME:
  256. if (!(flags & SCM_F_DYNSTACK_FRAME_REWINDABLE))
  257. scm_misc_error ("scm_dynstack_wind_1",
  258. "cannot invoke continuation from this context",
  259. SCM_EOL);
  260. break;
  261. case SCM_DYNSTACK_TYPE_UNWINDER:
  262. break;
  263. case SCM_DYNSTACK_TYPE_REWINDER:
  264. WINDER_PROC (item) (WINDER_DATA (item));
  265. break;
  266. case SCM_DYNSTACK_TYPE_WITH_FLUID:
  267. scm_swap_fluid (WITH_FLUID_FLUID (item),
  268. WITH_FLUID_VALUE_BOX (item),
  269. SCM_I_CURRENT_THREAD->dynamic_state);
  270. break;
  271. case SCM_DYNSTACK_TYPE_PROMPT:
  272. /* see vm_reinstate_partial_continuation */
  273. break;
  274. case SCM_DYNSTACK_TYPE_DYNWIND:
  275. scm_call_0 (DYNWIND_ENTER (item));
  276. break;
  277. case SCM_DYNSTACK_TYPE_DYNAMIC_STATE:
  278. scm_variable_set_x (DYNAMIC_STATE_STATE_BOX (item),
  279. scm_set_current_dynamic_state
  280. (scm_variable_ref (DYNAMIC_STATE_STATE_BOX (item))));
  281. break;
  282. case SCM_DYNSTACK_TYPE_NONE:
  283. default:
  284. abort ();
  285. }
  286. {
  287. scm_t_bits *words = push_dynstack_entry (dynstack, type, flags, len);
  288. copy_scm_t_bits (words, item, len);
  289. }
  290. }
  291. scm_t_bits
  292. scm_dynstack_unwind_1 (scm_t_dynstack *dynstack)
  293. {
  294. scm_t_bits tag;
  295. scm_t_bits *words;
  296. scm_t_dynstack_item_type type;
  297. tag = dynstack_pop (dynstack, &words);
  298. type = SCM_DYNSTACK_TAG_TYPE (tag);
  299. switch (type)
  300. {
  301. case SCM_DYNSTACK_TYPE_FRAME:
  302. break;
  303. case SCM_DYNSTACK_TYPE_UNWINDER:
  304. WINDER_PROC (words) (WINDER_DATA (words));
  305. clear_scm_t_bits (words, WINDER_WORDS);
  306. break;
  307. case SCM_DYNSTACK_TYPE_REWINDER:
  308. clear_scm_t_bits (words, WINDER_WORDS);
  309. break;
  310. case SCM_DYNSTACK_TYPE_WITH_FLUID:
  311. scm_swap_fluid (WITH_FLUID_FLUID (words),
  312. WITH_FLUID_VALUE_BOX (words),
  313. SCM_I_CURRENT_THREAD->dynamic_state);
  314. clear_scm_t_bits (words, WITH_FLUID_WORDS);
  315. break;
  316. case SCM_DYNSTACK_TYPE_PROMPT:
  317. /* we could invalidate the prompt */
  318. clear_scm_t_bits (words, PROMPT_WORDS);
  319. break;
  320. case SCM_DYNSTACK_TYPE_DYNWIND:
  321. {
  322. SCM proc = DYNWIND_LEAVE (words);
  323. clear_scm_t_bits (words, DYNWIND_WORDS);
  324. scm_call_0 (proc);
  325. }
  326. break;
  327. case SCM_DYNSTACK_TYPE_DYNAMIC_STATE:
  328. scm_variable_set_x (DYNAMIC_STATE_STATE_BOX (words),
  329. scm_set_current_dynamic_state
  330. (scm_variable_ref (DYNAMIC_STATE_STATE_BOX (words))));
  331. clear_scm_t_bits (words, DYNAMIC_STATE_WORDS);
  332. break;
  333. case SCM_DYNSTACK_TYPE_NONE:
  334. default:
  335. abort ();
  336. }
  337. return tag;
  338. }
  339. void
  340. scm_dynstack_wind (scm_t_dynstack *dynstack, scm_t_bits *item)
  341. {
  342. for (; SCM_DYNSTACK_TAG (item); item = SCM_DYNSTACK_NEXT (item))
  343. scm_dynstack_wind_1 (dynstack, item);
  344. }
  345. void
  346. scm_dynstack_unwind (scm_t_dynstack *dynstack, scm_t_bits *base)
  347. {
  348. while (dynstack->top > base)
  349. scm_dynstack_unwind_1 (dynstack);
  350. }
  351. static int
  352. same_entries (scm_t_bits *walk_a, scm_t_bits *next_a,
  353. scm_t_bits *walk_b, scm_t_bits *next_b)
  354. {
  355. if (SCM_DYNSTACK_TAG (walk_a) != SCM_DYNSTACK_TAG (walk_b))
  356. return 0;
  357. if (next_a - walk_a != next_b - walk_b)
  358. return 0;
  359. assert (SCM_DYNSTACK_PREV_OFFSET (next_a) == next_a - walk_a);
  360. assert (SCM_DYNSTACK_PREV_OFFSET (next_b) == next_b - walk_b);
  361. while (walk_a != next_a)
  362. if (*(walk_a++) != *(walk_b++))
  363. return 0;
  364. return 1;
  365. }
  366. static ptrdiff_t
  367. shared_prefix_length (scm_t_dynstack *a, scm_t_dynstack *b)
  368. {
  369. scm_t_bits *walk_a, *next_a, *walk_b, *next_b;
  370. walk_a = SCM_DYNSTACK_FIRST (a);
  371. walk_b = SCM_DYNSTACK_FIRST (b);
  372. next_a = SCM_DYNSTACK_NEXT (walk_a);
  373. next_b = SCM_DYNSTACK_NEXT (walk_b);
  374. while (next_a && next_b && same_entries (walk_a, next_a, walk_b, next_b))
  375. {
  376. walk_a = next_a;
  377. walk_b = next_b;
  378. next_a = SCM_DYNSTACK_NEXT (walk_a);
  379. next_b = SCM_DYNSTACK_NEXT (walk_b);
  380. }
  381. return walk_a - a->base;
  382. }
  383. scm_t_bits *
  384. scm_dynstack_unwind_fork (scm_t_dynstack *dynstack, scm_t_dynstack *branch)
  385. {
  386. ptrdiff_t join_height;
  387. join_height = shared_prefix_length (dynstack, branch);
  388. scm_dynstack_unwind (dynstack, dynstack->base + join_height);
  389. return branch->base + join_height;
  390. }
  391. scm_t_bits*
  392. scm_dynstack_find_prompt (scm_t_dynstack *dynstack, SCM key,
  393. scm_t_dynstack_prompt_flags *flags,
  394. ptrdiff_t *fp_offset, ptrdiff_t *sp_offset,
  395. uint32_t **vra, uint8_t **mra, jmp_buf **registers)
  396. {
  397. scm_t_bits *walk;
  398. for (walk = SCM_DYNSTACK_PREV (dynstack->top); walk;
  399. walk = SCM_DYNSTACK_PREV (walk))
  400. {
  401. scm_t_bits tag = SCM_DYNSTACK_TAG (walk);
  402. if (SCM_DYNSTACK_TAG_TYPE (tag) == SCM_DYNSTACK_TYPE_PROMPT
  403. && scm_is_eq (PROMPT_KEY (walk), key))
  404. {
  405. if (flags)
  406. *flags = SCM_DYNSTACK_TAG_FLAGS (tag);
  407. if (fp_offset)
  408. *fp_offset = PROMPT_FP (walk);
  409. if (sp_offset)
  410. *sp_offset = PROMPT_SP (walk);
  411. if (vra)
  412. *vra = PROMPT_VRA (walk);
  413. if (mra)
  414. *mra = PROMPT_MRA (walk);
  415. if (registers)
  416. *registers = PROMPT_JMPBUF (walk);
  417. return walk;
  418. }
  419. }
  420. return NULL;
  421. }
  422. SCM
  423. scm_dynstack_find_old_fluid_value (scm_t_dynstack *dynstack, SCM fluid,
  424. size_t depth, SCM dflt)
  425. {
  426. scm_t_bits *walk;
  427. for (walk = SCM_DYNSTACK_PREV (dynstack->top); walk;
  428. walk = SCM_DYNSTACK_PREV (walk))
  429. {
  430. scm_t_bits tag = SCM_DYNSTACK_TAG (walk);
  431. switch (SCM_DYNSTACK_TAG_TYPE (tag))
  432. {
  433. case SCM_DYNSTACK_TYPE_WITH_FLUID:
  434. {
  435. if (scm_is_eq (WITH_FLUID_FLUID (walk), fluid))
  436. {
  437. if (depth == 0)
  438. return SCM_VARIABLE_REF (WITH_FLUID_VALUE_BOX (walk));
  439. else
  440. depth--;
  441. }
  442. break;
  443. }
  444. case SCM_DYNSTACK_TYPE_DYNAMIC_STATE:
  445. {
  446. SCM state, val;
  447. /* The previous dynamic state may or may not have
  448. established a binding for this fluid. */
  449. state = scm_variable_ref (DYNAMIC_STATE_STATE_BOX (walk));
  450. val = scm_dynamic_state_ref (state, fluid, SCM_UNDEFINED);
  451. if (!SCM_UNBNDP (val))
  452. {
  453. if (depth == 0)
  454. return val;
  455. else
  456. depth--;
  457. }
  458. break;
  459. }
  460. default:
  461. break;
  462. }
  463. }
  464. return dflt;
  465. }
  466. void
  467. scm_dynstack_wind_prompt (scm_t_dynstack *dynstack, scm_t_bits *item,
  468. ptrdiff_t base_fp_offset,
  469. jmp_buf *registers)
  470. {
  471. scm_t_bits tag = SCM_DYNSTACK_TAG (item);
  472. if (SCM_DYNSTACK_TAG_TYPE (tag) != SCM_DYNSTACK_TYPE_PROMPT)
  473. abort ();
  474. scm_dynstack_push_prompt (dynstack,
  475. SCM_DYNSTACK_TAG_FLAGS (tag),
  476. PROMPT_KEY (item),
  477. PROMPT_FP (item) + base_fp_offset,
  478. PROMPT_SP (item) + base_fp_offset,
  479. PROMPT_VRA (item),
  480. PROMPT_MRA (item),
  481. registers);
  482. }
  483. void
  484. scm_dynstack_unwind_frame (scm_t_dynstack *dynstack)
  485. {
  486. /* Unwind up to and including the next frame entry. */
  487. while (1)
  488. {
  489. scm_t_bits tag, *words;
  490. tag = dynstack_pop (dynstack, &words);
  491. switch (SCM_DYNSTACK_TAG_TYPE (tag))
  492. {
  493. case SCM_DYNSTACK_TYPE_FRAME:
  494. return;
  495. case SCM_DYNSTACK_TYPE_REWINDER:
  496. clear_scm_t_bits (words, WINDER_WORDS);
  497. continue;
  498. case SCM_DYNSTACK_TYPE_UNWINDER:
  499. {
  500. scm_t_guard proc = WINDER_PROC (words);
  501. void *data = WINDER_DATA (words);
  502. clear_scm_t_bits (words, WINDER_WORDS);
  503. if (SCM_DYNSTACK_TAG_FLAGS (tag) & SCM_F_DYNSTACK_WINDER_EXPLICIT)
  504. proc (data);
  505. continue;
  506. }
  507. default:
  508. /* We should only see winders. */
  509. abort ();
  510. }
  511. }
  512. }
  513. /* This function must not allocate. */
  514. void
  515. scm_dynstack_unwind_fluid (scm_t_dynstack *dynstack,
  516. scm_t_dynamic_state *dynamic_state)
  517. {
  518. scm_t_bits tag, *words;
  519. size_t len;
  520. tag = dynstack_pop (dynstack, &words);
  521. len = SCM_DYNSTACK_TAG_LEN (tag);
  522. assert (SCM_DYNSTACK_TAG_TYPE (tag) == SCM_DYNSTACK_TYPE_WITH_FLUID);
  523. assert (len == WITH_FLUID_WORDS);
  524. scm_swap_fluid (WITH_FLUID_FLUID (words), WITH_FLUID_VALUE_BOX (words),
  525. dynamic_state);
  526. clear_scm_t_bits (words, len);
  527. }
  528. void
  529. scm_dynstack_unwind_dynamic_state (scm_t_dynstack *dynstack,
  530. scm_t_dynamic_state *dynamic_state)
  531. {
  532. scm_t_bits tag, *words;
  533. size_t len;
  534. tag = dynstack_pop (dynstack, &words);
  535. len = SCM_DYNSTACK_TAG_LEN (tag);
  536. assert (SCM_DYNSTACK_TAG_TYPE (tag) == SCM_DYNSTACK_TYPE_DYNAMIC_STATE);
  537. assert (len == DYNAMIC_STATE_WORDS);
  538. scm_variable_set_x (DYNAMIC_STATE_STATE_BOX (words),
  539. scm_set_current_dynamic_state
  540. (scm_variable_ref (DYNAMIC_STATE_STATE_BOX (words))));
  541. clear_scm_t_bits (words, len);
  542. }