dynstack.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /* Copyright (C) 2012, 2013 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <assert.h>
  22. #include "libguile/_scm.h"
  23. #include "libguile/control.h"
  24. #include "libguile/eval.h"
  25. #include "libguile/fluids.h"
  26. #include "libguile/dynstack.h"
  27. #define PROMPT_WORDS 5
  28. #define PROMPT_KEY(top) (SCM_PACK ((top)[0]))
  29. #define PROMPT_FP(top) ((scm_t_ptrdiff) ((top)[1]))
  30. #define SET_PROMPT_FP(top, fp) do { top[1] = (scm_t_bits)(fp); } while (0)
  31. #define PROMPT_SP(top) ((scm_t_ptrdiff) ((top)[2]))
  32. #define SET_PROMPT_SP(top, sp) do { top[2] = (scm_t_bits)(sp); } while (0)
  33. #define PROMPT_IP(top) ((scm_t_uint32 *) ((top)[3]))
  34. #define PROMPT_JMPBUF(top) ((scm_i_jmp_buf *) ((top)[4]))
  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. scm_t_ptrdiff fp_offset, scm_t_ptrdiff sp_offset,
  153. scm_t_uint32 *ip, scm_i_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) ip;
  162. words[4] = (scm_t_bits) registers;
  163. }
  164. void
  165. scm_dynstack_push_dynwind (scm_t_dynstack *dynstack, SCM enter, SCM leave)
  166. {
  167. scm_t_bits *words;
  168. words = push_dynstack_entry (dynstack, SCM_DYNSTACK_TYPE_DYNWIND, 0,
  169. DYNWIND_WORDS);
  170. words[0] = SCM_UNPACK (enter);
  171. words[1] = SCM_UNPACK (leave);
  172. }
  173. static inline scm_t_bits
  174. dynstack_pop (scm_t_dynstack *dynstack, scm_t_bits **words)
  175. {
  176. scm_t_bits *prev = SCM_DYNSTACK_PREV (dynstack->top);
  177. scm_t_bits tag;
  178. if (SCM_UNLIKELY (!prev))
  179. abort ();
  180. SCM_DYNSTACK_SET_PREV_OFFSET (dynstack->top, 0);
  181. dynstack->top = prev;
  182. tag = SCM_DYNSTACK_TAG (dynstack->top);
  183. SCM_DYNSTACK_SET_TAG (dynstack->top, 0);
  184. *words = dynstack->top;
  185. return tag;
  186. }
  187. void
  188. scm_dynstack_push_dynamic_state (scm_t_dynstack *dynstack, SCM state,
  189. scm_t_dynamic_state *dynamic_state)
  190. {
  191. scm_t_bits *words;
  192. SCM state_box;
  193. if (SCM_UNLIKELY (scm_is_false (scm_dynamic_state_p (state))))
  194. scm_wrong_type_arg ("with-dynamic-state", 0, state);
  195. state_box = scm_make_variable (scm_set_current_dynamic_state (state));
  196. words = push_dynstack_entry (dynstack, SCM_DYNSTACK_TYPE_DYNAMIC_STATE, 0,
  197. DYNAMIC_STATE_WORDS);
  198. words[0] = SCM_UNPACK (state_box);
  199. }
  200. void
  201. scm_dynstack_pop (scm_t_dynstack *dynstack)
  202. {
  203. scm_t_bits tag, *words;
  204. tag = dynstack_pop (dynstack, &words);
  205. clear_scm_t_bits (words, SCM_DYNSTACK_TAG_LEN (tag));
  206. }
  207. scm_t_dynstack *
  208. scm_dynstack_capture_all (scm_t_dynstack *dynstack)
  209. {
  210. return scm_dynstack_capture (dynstack, SCM_DYNSTACK_FIRST (dynstack));
  211. }
  212. scm_t_dynstack *
  213. scm_dynstack_capture (scm_t_dynstack *dynstack, scm_t_bits *item)
  214. {
  215. char *mem;
  216. scm_t_dynstack *ret;
  217. size_t len;
  218. assert (item >= SCM_DYNSTACK_FIRST (dynstack));
  219. assert (item <= dynstack->top);
  220. len = dynstack->top - item + SCM_DYNSTACK_HEADER_LEN;
  221. mem = scm_gc_malloc (sizeof (*ret) + len * sizeof(scm_t_bits), "dynstack");
  222. ret = (scm_t_dynstack *) mem;
  223. ret->base = (scm_t_bits *) (mem + sizeof (*ret));
  224. ret->limit = ret->base + len;
  225. ret->top = ret->base + len;
  226. copy_scm_t_bits (ret->base, item - SCM_DYNSTACK_HEADER_LEN, len);
  227. SCM_DYNSTACK_SET_PREV_OFFSET (SCM_DYNSTACK_FIRST (ret), 0);
  228. return ret;
  229. }
  230. void
  231. scm_dynstack_relocate_prompts (scm_t_dynstack *dynstack, scm_t_ptrdiff base)
  232. {
  233. scm_t_bits *walk;
  234. /* Relocate prompts. */
  235. for (walk = dynstack->top; walk; walk = SCM_DYNSTACK_PREV (walk))
  236. {
  237. scm_t_bits tag = SCM_DYNSTACK_TAG (walk);
  238. if (SCM_DYNSTACK_TAG_TYPE (tag) == SCM_DYNSTACK_TYPE_PROMPT)
  239. {
  240. SET_PROMPT_FP (walk, PROMPT_FP (walk) - base);
  241. SET_PROMPT_SP (walk, PROMPT_SP (walk) - base);
  242. }
  243. }
  244. }
  245. void
  246. scm_dynstack_wind_1 (scm_t_dynstack *dynstack, scm_t_bits *item)
  247. {
  248. scm_t_bits tag = SCM_DYNSTACK_TAG (item);
  249. scm_t_dynstack_item_type type = SCM_DYNSTACK_TAG_TYPE (tag);
  250. scm_t_bits flags = SCM_DYNSTACK_TAG_FLAGS (tag);
  251. size_t len = SCM_DYNSTACK_TAG_LEN (tag);
  252. switch (type)
  253. {
  254. case SCM_DYNSTACK_TYPE_FRAME:
  255. if (!(flags & SCM_F_DYNSTACK_FRAME_REWINDABLE))
  256. scm_misc_error ("scm_dynstack_wind_1",
  257. "cannot invoke continuation from this context",
  258. SCM_EOL);
  259. break;
  260. case SCM_DYNSTACK_TYPE_UNWINDER:
  261. break;
  262. case SCM_DYNSTACK_TYPE_REWINDER:
  263. WINDER_PROC (item) (WINDER_DATA (item));
  264. break;
  265. case SCM_DYNSTACK_TYPE_WITH_FLUID:
  266. scm_swap_fluid (WITH_FLUID_FLUID (item),
  267. WITH_FLUID_VALUE_BOX (item),
  268. SCM_I_CURRENT_THREAD->dynamic_state);
  269. break;
  270. case SCM_DYNSTACK_TYPE_PROMPT:
  271. /* see vm_reinstate_partial_continuation */
  272. break;
  273. case SCM_DYNSTACK_TYPE_DYNWIND:
  274. scm_call_0 (DYNWIND_ENTER (item));
  275. break;
  276. case SCM_DYNSTACK_TYPE_DYNAMIC_STATE:
  277. scm_variable_set_x (DYNAMIC_STATE_STATE_BOX (item),
  278. scm_set_current_dynamic_state
  279. (scm_variable_ref (DYNAMIC_STATE_STATE_BOX (item))));
  280. break;
  281. case SCM_DYNSTACK_TYPE_NONE:
  282. default:
  283. abort ();
  284. }
  285. {
  286. scm_t_bits *words = push_dynstack_entry (dynstack, type, flags, len);
  287. copy_scm_t_bits (words, item, len);
  288. }
  289. }
  290. scm_t_bits
  291. scm_dynstack_unwind_1 (scm_t_dynstack *dynstack)
  292. {
  293. scm_t_bits tag;
  294. scm_t_bits *words;
  295. scm_t_dynstack_item_type type;
  296. tag = dynstack_pop (dynstack, &words);
  297. type = SCM_DYNSTACK_TAG_TYPE (tag);
  298. switch (type)
  299. {
  300. case SCM_DYNSTACK_TYPE_FRAME:
  301. break;
  302. case SCM_DYNSTACK_TYPE_UNWINDER:
  303. WINDER_PROC (words) (WINDER_DATA (words));
  304. clear_scm_t_bits (words, WINDER_WORDS);
  305. break;
  306. case SCM_DYNSTACK_TYPE_REWINDER:
  307. clear_scm_t_bits (words, WINDER_WORDS);
  308. break;
  309. case SCM_DYNSTACK_TYPE_WITH_FLUID:
  310. scm_swap_fluid (WITH_FLUID_FLUID (words),
  311. WITH_FLUID_VALUE_BOX (words),
  312. SCM_I_CURRENT_THREAD->dynamic_state);
  313. clear_scm_t_bits (words, WITH_FLUID_WORDS);
  314. break;
  315. case SCM_DYNSTACK_TYPE_PROMPT:
  316. /* we could invalidate the prompt */
  317. clear_scm_t_bits (words, PROMPT_WORDS);
  318. break;
  319. case SCM_DYNSTACK_TYPE_DYNWIND:
  320. {
  321. SCM proc = DYNWIND_LEAVE (words);
  322. clear_scm_t_bits (words, DYNWIND_WORDS);
  323. scm_call_0 (proc);
  324. }
  325. break;
  326. case SCM_DYNSTACK_TYPE_DYNAMIC_STATE:
  327. scm_variable_set_x (DYNAMIC_STATE_STATE_BOX (words),
  328. scm_set_current_dynamic_state
  329. (scm_variable_ref (DYNAMIC_STATE_STATE_BOX (words))));
  330. clear_scm_t_bits (words, DYNAMIC_STATE_WORDS);
  331. break;
  332. case SCM_DYNSTACK_TYPE_NONE:
  333. default:
  334. abort ();
  335. }
  336. return tag;
  337. }
  338. void
  339. scm_dynstack_wind (scm_t_dynstack *dynstack, scm_t_bits *item)
  340. {
  341. for (; SCM_DYNSTACK_TAG (item); item = SCM_DYNSTACK_NEXT (item))
  342. scm_dynstack_wind_1 (dynstack, item);
  343. }
  344. void
  345. scm_dynstack_unwind (scm_t_dynstack *dynstack, scm_t_bits *base)
  346. {
  347. while (dynstack->top > base)
  348. scm_dynstack_unwind_1 (dynstack);
  349. }
  350. static int
  351. same_entries (scm_t_bits *walk_a, scm_t_bits *next_a,
  352. scm_t_bits *walk_b, scm_t_bits *next_b)
  353. {
  354. if (SCM_DYNSTACK_TAG (walk_a) != SCM_DYNSTACK_TAG (walk_b))
  355. return 0;
  356. if (next_a - walk_a != next_b - walk_b)
  357. return 0;
  358. assert (SCM_DYNSTACK_PREV_OFFSET (next_a) == next_a - walk_a);
  359. assert (SCM_DYNSTACK_PREV_OFFSET (next_b) == next_b - walk_b);
  360. while (walk_a != next_a)
  361. if (*(walk_a++) != *(walk_b++))
  362. return 0;
  363. return 1;
  364. }
  365. static ptrdiff_t
  366. shared_prefix_length (scm_t_dynstack *a, scm_t_dynstack *b)
  367. {
  368. scm_t_bits *walk_a, *next_a, *walk_b, *next_b;
  369. walk_a = SCM_DYNSTACK_FIRST (a);
  370. walk_b = SCM_DYNSTACK_FIRST (b);
  371. next_a = SCM_DYNSTACK_NEXT (walk_a);
  372. next_b = SCM_DYNSTACK_NEXT (walk_b);
  373. while (next_a && next_b && same_entries (walk_a, next_a, walk_b, next_b))
  374. {
  375. walk_a = next_a;
  376. walk_b = next_b;
  377. next_a = SCM_DYNSTACK_NEXT (walk_a);
  378. next_b = SCM_DYNSTACK_NEXT (walk_b);
  379. }
  380. return walk_a - a->base;
  381. }
  382. scm_t_bits *
  383. scm_dynstack_unwind_fork (scm_t_dynstack *dynstack, scm_t_dynstack *branch)
  384. {
  385. ptrdiff_t join_height;
  386. join_height = shared_prefix_length (dynstack, branch);
  387. scm_dynstack_unwind (dynstack, dynstack->base + join_height);
  388. return branch->base + join_height;
  389. }
  390. scm_t_bits*
  391. scm_dynstack_find_prompt (scm_t_dynstack *dynstack, SCM key,
  392. scm_t_dynstack_prompt_flags *flags,
  393. scm_t_ptrdiff *fp_offset, scm_t_ptrdiff *sp_offset,
  394. scm_t_uint32 **ip, scm_i_jmp_buf **registers)
  395. {
  396. scm_t_bits *walk;
  397. for (walk = SCM_DYNSTACK_PREV (dynstack->top); walk;
  398. walk = SCM_DYNSTACK_PREV (walk))
  399. {
  400. scm_t_bits tag = SCM_DYNSTACK_TAG (walk);
  401. if (SCM_DYNSTACK_TAG_TYPE (tag) == SCM_DYNSTACK_TYPE_PROMPT
  402. && scm_is_eq (PROMPT_KEY (walk), key))
  403. {
  404. if (flags)
  405. *flags = SCM_DYNSTACK_TAG_FLAGS (tag);
  406. if (fp_offset)
  407. *fp_offset = PROMPT_FP (walk);
  408. if (sp_offset)
  409. *sp_offset = PROMPT_SP (walk);
  410. if (ip)
  411. *ip = PROMPT_IP (walk);
  412. if (registers)
  413. *registers = PROMPT_JMPBUF (walk);
  414. return walk;
  415. }
  416. }
  417. return NULL;
  418. }
  419. SCM
  420. scm_dynstack_find_old_fluid_value (scm_t_dynstack *dynstack, SCM fluid,
  421. size_t depth, SCM dflt)
  422. {
  423. scm_t_bits *walk;
  424. for (walk = SCM_DYNSTACK_PREV (dynstack->top); walk;
  425. walk = SCM_DYNSTACK_PREV (walk))
  426. {
  427. scm_t_bits tag = SCM_DYNSTACK_TAG (walk);
  428. switch (SCM_DYNSTACK_TAG_TYPE (tag))
  429. {
  430. case SCM_DYNSTACK_TYPE_WITH_FLUID:
  431. {
  432. if (scm_is_eq (WITH_FLUID_FLUID (walk), fluid))
  433. {
  434. if (depth == 0)
  435. return SCM_VARIABLE_REF (WITH_FLUID_VALUE_BOX (walk));
  436. else
  437. depth--;
  438. }
  439. break;
  440. }
  441. case SCM_DYNSTACK_TYPE_DYNAMIC_STATE:
  442. {
  443. SCM state, val;
  444. /* The previous dynamic state may or may not have
  445. established a binding for this fluid. */
  446. state = scm_variable_ref (DYNAMIC_STATE_STATE_BOX (walk));
  447. val = scm_dynamic_state_ref (state, fluid, SCM_UNDEFINED);
  448. if (!SCM_UNBNDP (val))
  449. {
  450. if (depth == 0)
  451. return val;
  452. else
  453. depth--;
  454. }
  455. break;
  456. }
  457. default:
  458. break;
  459. }
  460. }
  461. return dflt;
  462. }
  463. void
  464. scm_dynstack_wind_prompt (scm_t_dynstack *dynstack, scm_t_bits *item,
  465. scm_t_ptrdiff base_fp_offset,
  466. scm_i_jmp_buf *registers)
  467. {
  468. scm_t_bits tag = SCM_DYNSTACK_TAG (item);
  469. if (SCM_DYNSTACK_TAG_TYPE (tag) != SCM_DYNSTACK_TYPE_PROMPT)
  470. abort ();
  471. scm_dynstack_push_prompt (dynstack,
  472. SCM_DYNSTACK_TAG_FLAGS (tag),
  473. PROMPT_KEY (item),
  474. PROMPT_FP (item) + base_fp_offset,
  475. PROMPT_SP (item) + base_fp_offset,
  476. PROMPT_IP (item),
  477. registers);
  478. }
  479. void
  480. scm_dynstack_unwind_frame (scm_t_dynstack *dynstack)
  481. {
  482. /* Unwind up to and including the next frame entry. */
  483. while (1)
  484. {
  485. scm_t_bits tag, *words;
  486. tag = dynstack_pop (dynstack, &words);
  487. switch (SCM_DYNSTACK_TAG_TYPE (tag))
  488. {
  489. case SCM_DYNSTACK_TYPE_FRAME:
  490. return;
  491. case SCM_DYNSTACK_TYPE_REWINDER:
  492. clear_scm_t_bits (words, WINDER_WORDS);
  493. continue;
  494. case SCM_DYNSTACK_TYPE_UNWINDER:
  495. {
  496. scm_t_guard proc = WINDER_PROC (words);
  497. void *data = WINDER_DATA (words);
  498. clear_scm_t_bits (words, WINDER_WORDS);
  499. if (SCM_DYNSTACK_TAG_FLAGS (tag) & SCM_F_DYNSTACK_WINDER_EXPLICIT)
  500. proc (data);
  501. continue;
  502. }
  503. default:
  504. /* We should only see winders. */
  505. abort ();
  506. }
  507. }
  508. }
  509. /* This function must not allocate. */
  510. void
  511. scm_dynstack_unwind_fluid (scm_t_dynstack *dynstack,
  512. scm_t_dynamic_state *dynamic_state)
  513. {
  514. scm_t_bits tag, *words;
  515. size_t len;
  516. tag = dynstack_pop (dynstack, &words);
  517. len = SCM_DYNSTACK_TAG_LEN (tag);
  518. assert (SCM_DYNSTACK_TAG_TYPE (tag) == SCM_DYNSTACK_TYPE_WITH_FLUID);
  519. assert (len == WITH_FLUID_WORDS);
  520. scm_swap_fluid (WITH_FLUID_FLUID (words), WITH_FLUID_VALUE_BOX (words),
  521. dynamic_state);
  522. clear_scm_t_bits (words, len);
  523. }
  524. void
  525. scm_dynstack_unwind_dynamic_state (scm_t_dynstack *dynstack,
  526. scm_t_dynamic_state *dynamic_state)
  527. {
  528. scm_t_bits tag, *words;
  529. size_t len;
  530. tag = dynstack_pop (dynstack, &words);
  531. len = SCM_DYNSTACK_TAG_LEN (tag);
  532. assert (SCM_DYNSTACK_TAG_TYPE (tag) == SCM_DYNSTACK_TYPE_DYNAMIC_STATE);
  533. assert (len == DYNAMIC_STATE_WORDS);
  534. scm_variable_set_x (DYNAMIC_STATE_STATE_BOX (words),
  535. scm_set_current_dynamic_state
  536. (scm_variable_ref (DYNAMIC_STATE_STATE_BOX (words))));
  537. clear_scm_t_bits (words, len);
  538. }
  539. /*
  540. Local Variables:
  541. c-file-style: "gnu"
  542. End:
  543. */