dynstack.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /* Copyright (C) 2012 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 *) ((top)[1]))
  30. #define PROMPT_SP(top) ((SCM *) ((top)[2]))
  31. #define PROMPT_IP(top) ((scm_t_uint8 *) ((top)[3]))
  32. #define PROMPT_JMPBUF(top) ((scm_i_jmp_buf *) ((top)[4]))
  33. #define WINDER_WORDS 2
  34. #define WINDER_PROC(top) ((scm_t_guard) ((top)[0]))
  35. #define WINDER_DATA(top) ((void *) ((top)[1]))
  36. #define DYNWIND_WORDS 2
  37. #define DYNWIND_ENTER(top) (SCM_PACK ((top)[0]))
  38. #define DYNWIND_LEAVE(top) (SCM_PACK ((top)[1]))
  39. #define WITH_FLUIDS_FLUIDS(top) ((SCM*)((top) + 1))
  40. #define WITH_FLUIDS_VALUES(top) ((SCM*)((top)[0]))
  41. static void
  42. copy_scm_t_bits (scm_t_bits *dst, scm_t_bits *src, size_t n)
  43. {
  44. size_t i;
  45. for (i = 0; i < n; i++)
  46. dst[i] = src[i];
  47. }
  48. static void
  49. copy_scm (SCM *dst, SCM *src, size_t n)
  50. {
  51. size_t i;
  52. for (i = 0; i < n; i++)
  53. dst[i] = src[i];
  54. }
  55. static void
  56. clear_scm_t_bits (scm_t_bits *items, size_t n)
  57. {
  58. size_t i;
  59. for (i = 0; i < n; i++)
  60. items[i] = 0;
  61. }
  62. /* Ensure space for N additional words. */
  63. static void
  64. dynstack_ensure_space (scm_t_dynstack *dynstack, size_t n)
  65. {
  66. size_t capacity = SCM_DYNSTACK_CAPACITY (dynstack);
  67. size_t height = SCM_DYNSTACK_HEIGHT (dynstack);
  68. n += SCM_DYNSTACK_HEADER_LEN;
  69. if (capacity < height + n)
  70. {
  71. scm_t_bits *new_base;
  72. while (capacity < height + n)
  73. capacity = (capacity < 4) ? 8 : (capacity * 2);
  74. new_base = scm_gc_malloc (capacity * sizeof(scm_t_bits), "dynstack");
  75. copy_scm_t_bits (new_base, dynstack->base, height);
  76. clear_scm_t_bits (dynstack->base, height);
  77. dynstack->base = new_base;
  78. dynstack->top = new_base + height;
  79. dynstack->limit = new_base + capacity;
  80. }
  81. }
  82. static inline scm_t_bits *
  83. push_dynstack_entry_unchecked (scm_t_dynstack *dynstack,
  84. scm_t_dynstack_item_type type,
  85. scm_t_bits flags, size_t len)
  86. {
  87. scm_t_bits *ret = dynstack->top;
  88. SCM_DYNSTACK_SET_TAG (dynstack->top, SCM_MAKE_DYNSTACK_TAG (type, flags, len));
  89. dynstack->top += SCM_DYNSTACK_HEADER_LEN + len;
  90. SCM_DYNSTACK_SET_PREV_OFFSET (dynstack->top, SCM_DYNSTACK_HEADER_LEN + len);
  91. return ret;
  92. }
  93. static inline scm_t_bits *
  94. push_dynstack_entry (scm_t_dynstack *dynstack,
  95. scm_t_dynstack_item_type type,
  96. scm_t_bits flags, size_t len)
  97. {
  98. if (SCM_UNLIKELY (!SCM_DYNSTACK_HAS_SPACE (dynstack, len)))
  99. dynstack_ensure_space (dynstack, len);
  100. return push_dynstack_entry_unchecked (dynstack, type, flags, len);
  101. }
  102. void
  103. scm_dynstack_push_frame (scm_t_dynstack *dynstack,
  104. scm_t_dynstack_frame_flags flags)
  105. {
  106. push_dynstack_entry (dynstack, SCM_DYNSTACK_TYPE_FRAME, flags, 0);
  107. }
  108. void
  109. scm_dynstack_push_rewinder (scm_t_dynstack *dynstack,
  110. scm_t_dynstack_winder_flags flags,
  111. scm_t_guard proc, void *data)
  112. {
  113. scm_t_bits *words;
  114. words = push_dynstack_entry (dynstack, SCM_DYNSTACK_TYPE_REWINDER, flags, 2);
  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, 2);
  125. words[0] = (scm_t_bits) proc;
  126. words[1] = (scm_t_bits) data;
  127. }
  128. /* The fluids are stored on the stack. However, the values have to be
  129. stored on the heap, so that all continuations that capture this
  130. dynamic scope capture the same bindings. */
  131. void
  132. scm_dynstack_push_fluids (scm_t_dynstack *dynstack, size_t n,
  133. SCM *fluids, SCM *values, SCM dynamic_state)
  134. {
  135. scm_t_bits *words;
  136. SCM *heap_values;
  137. n = scm_prepare_fluids (n, fluids, values);
  138. heap_values = scm_gc_malloc (n * sizeof (scm_t_bits), "with-fluids");
  139. copy_scm (heap_values, values, n);
  140. words = push_dynstack_entry (dynstack, SCM_DYNSTACK_TYPE_WITH_FLUIDS,
  141. 0, n + 1);
  142. words[0] = (scm_t_bits) heap_values;
  143. copy_scm (WITH_FLUIDS_FLUIDS (words), fluids, n);
  144. /* Go ahead and swap them. */
  145. scm_swap_fluids (n, WITH_FLUIDS_FLUIDS (words), WITH_FLUIDS_VALUES (words),
  146. 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 *fp, SCM *sp, scm_t_uint8 *ip,
  153. 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;
  160. words[2] = (scm_t_bits) sp;
  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, 2);
  169. words[0] = SCM_UNPACK (enter);
  170. words[1] = SCM_UNPACK (leave);
  171. }
  172. static inline scm_t_bits
  173. dynstack_pop (scm_t_dynstack *dynstack, scm_t_bits **words)
  174. {
  175. scm_t_bits *prev = SCM_DYNSTACK_PREV (dynstack->top);
  176. scm_t_bits tag;
  177. if (SCM_UNLIKELY (!prev))
  178. abort ();
  179. SCM_DYNSTACK_SET_PREV_OFFSET (dynstack->top, 0);
  180. dynstack->top = prev;
  181. tag = SCM_DYNSTACK_TAG (dynstack->top);
  182. SCM_DYNSTACK_SET_TAG (dynstack->top, 0);
  183. *words = dynstack->top;
  184. return tag;
  185. }
  186. void
  187. scm_dynstack_pop (scm_t_dynstack *dynstack)
  188. {
  189. scm_t_bits tag, *words;
  190. tag = dynstack_pop (dynstack, &words);
  191. clear_scm_t_bits (words, SCM_DYNSTACK_TAG_LEN (tag));
  192. }
  193. scm_t_dynstack *
  194. scm_dynstack_capture_all (scm_t_dynstack *dynstack)
  195. {
  196. return scm_dynstack_capture (dynstack, SCM_DYNSTACK_FIRST (dynstack));
  197. }
  198. scm_t_dynstack *
  199. scm_dynstack_capture (scm_t_dynstack *dynstack, scm_t_bits *item)
  200. {
  201. char *mem;
  202. scm_t_dynstack *ret;
  203. size_t len;
  204. assert (item >= SCM_DYNSTACK_FIRST (dynstack));
  205. assert (item <= dynstack->top);
  206. len = dynstack->top - item + SCM_DYNSTACK_HEADER_LEN;
  207. mem = scm_gc_malloc (sizeof (*ret) + len * sizeof(scm_t_bits), "dynstack");
  208. ret = (scm_t_dynstack *) mem;
  209. ret->base = (scm_t_bits *) (mem + sizeof (*ret));
  210. ret->limit = ret->base + len;
  211. ret->top = ret->base + len;
  212. copy_scm_t_bits (ret->base, item - SCM_DYNSTACK_HEADER_LEN, len);
  213. SCM_DYNSTACK_SET_PREV_OFFSET (SCM_DYNSTACK_FIRST (ret), 0);
  214. return ret;
  215. }
  216. void
  217. scm_dynstack_wind_1 (scm_t_dynstack *dynstack, scm_t_bits *item)
  218. {
  219. scm_t_bits tag = SCM_DYNSTACK_TAG (item);
  220. scm_t_dynstack_item_type type = SCM_DYNSTACK_TAG_TYPE (tag);
  221. scm_t_bits flags = SCM_DYNSTACK_TAG_FLAGS (tag);
  222. size_t len = SCM_DYNSTACK_TAG_LEN (tag);
  223. switch (type)
  224. {
  225. case SCM_DYNSTACK_TYPE_FRAME:
  226. if (!(flags & SCM_F_DYNSTACK_FRAME_REWINDABLE))
  227. scm_misc_error ("scm_dynstack_wind_1",
  228. "cannot invoke continuation from this context",
  229. SCM_EOL);
  230. break;
  231. case SCM_DYNSTACK_TYPE_UNWINDER:
  232. break;
  233. case SCM_DYNSTACK_TYPE_REWINDER:
  234. WINDER_PROC (item) (WINDER_DATA (item));
  235. break;
  236. case SCM_DYNSTACK_TYPE_WITH_FLUIDS:
  237. scm_swap_fluids (len - 1, WITH_FLUIDS_FLUIDS (item),
  238. WITH_FLUIDS_VALUES (item),
  239. SCM_I_CURRENT_THREAD->dynamic_state);
  240. break;
  241. case SCM_DYNSTACK_TYPE_PROMPT:
  242. /* see vm_reinstate_partial_continuation */
  243. break;
  244. case SCM_DYNSTACK_TYPE_DYNWIND:
  245. scm_call_0 (DYNWIND_ENTER (item));
  246. break;
  247. case SCM_DYNSTACK_TYPE_NONE:
  248. default:
  249. abort ();
  250. }
  251. {
  252. scm_t_bits *words = push_dynstack_entry (dynstack, type, flags, len);
  253. copy_scm_t_bits (words, item, len);
  254. }
  255. }
  256. scm_t_bits
  257. scm_dynstack_unwind_1 (scm_t_dynstack *dynstack)
  258. {
  259. scm_t_bits tag;
  260. scm_t_bits *words;
  261. scm_t_dynstack_item_type type;
  262. size_t len;
  263. tag = dynstack_pop (dynstack, &words);
  264. type = SCM_DYNSTACK_TAG_TYPE (tag);
  265. len = SCM_DYNSTACK_TAG_LEN (tag);
  266. switch (type)
  267. {
  268. case SCM_DYNSTACK_TYPE_FRAME:
  269. break;
  270. case SCM_DYNSTACK_TYPE_UNWINDER:
  271. WINDER_PROC (words) (WINDER_DATA (words));
  272. clear_scm_t_bits (words, WINDER_WORDS);
  273. break;
  274. case SCM_DYNSTACK_TYPE_REWINDER:
  275. clear_scm_t_bits (words, WINDER_WORDS);
  276. break;
  277. case SCM_DYNSTACK_TYPE_WITH_FLUIDS:
  278. scm_swap_fluids (len - 1, WITH_FLUIDS_FLUIDS (words),
  279. WITH_FLUIDS_VALUES (words),
  280. SCM_I_CURRENT_THREAD->dynamic_state);
  281. clear_scm_t_bits (words, len);
  282. break;
  283. case SCM_DYNSTACK_TYPE_PROMPT:
  284. /* we could invalidate the prompt */
  285. clear_scm_t_bits (words, PROMPT_WORDS);
  286. break;
  287. case SCM_DYNSTACK_TYPE_DYNWIND:
  288. {
  289. SCM proc = DYNWIND_LEAVE (words);
  290. clear_scm_t_bits (words, DYNWIND_WORDS);
  291. scm_call_0 (proc);
  292. }
  293. break;
  294. case SCM_DYNSTACK_TYPE_NONE:
  295. default:
  296. abort ();
  297. }
  298. return tag;
  299. }
  300. void
  301. scm_dynstack_wind (scm_t_dynstack *dynstack, scm_t_bits *item)
  302. {
  303. for (; SCM_DYNSTACK_TAG (item); item = SCM_DYNSTACK_NEXT (item))
  304. scm_dynstack_wind_1 (dynstack, item);
  305. }
  306. void
  307. scm_dynstack_unwind (scm_t_dynstack *dynstack, scm_t_bits *base)
  308. {
  309. while (dynstack->top > base)
  310. scm_dynstack_unwind_1 (dynstack);
  311. }
  312. static int
  313. same_entries (scm_t_bits *walk_a, scm_t_bits *next_a,
  314. scm_t_bits *walk_b, scm_t_bits *next_b)
  315. {
  316. if (SCM_DYNSTACK_TAG (walk_a) != SCM_DYNSTACK_TAG (walk_b))
  317. return 0;
  318. if (next_a - walk_a != next_b - walk_b)
  319. return 0;
  320. assert (SCM_DYNSTACK_PREV_OFFSET (next_a) == next_a - walk_a);
  321. assert (SCM_DYNSTACK_PREV_OFFSET (next_b) == next_b - walk_b);
  322. while (walk_a != next_a)
  323. if (*(walk_a++) != *(walk_b++))
  324. return 0;
  325. return 1;
  326. }
  327. static ptrdiff_t
  328. shared_prefix_length (scm_t_dynstack *a, scm_t_dynstack *b)
  329. {
  330. scm_t_bits *walk_a, *next_a, *walk_b, *next_b;
  331. walk_a = SCM_DYNSTACK_FIRST (a);
  332. walk_b = SCM_DYNSTACK_FIRST (b);
  333. next_a = SCM_DYNSTACK_NEXT (walk_a);
  334. next_b = SCM_DYNSTACK_NEXT (walk_b);
  335. while (next_a && next_b && same_entries (walk_a, next_a, walk_b, next_b))
  336. {
  337. walk_a = next_a;
  338. walk_b = next_b;
  339. next_a = SCM_DYNSTACK_NEXT (walk_a);
  340. next_b = SCM_DYNSTACK_NEXT (walk_b);
  341. }
  342. return walk_a - a->base;
  343. }
  344. scm_t_bits *
  345. scm_dynstack_unwind_fork (scm_t_dynstack *dynstack, scm_t_dynstack *branch)
  346. {
  347. ptrdiff_t join_height;
  348. join_height = shared_prefix_length (dynstack, branch);
  349. scm_dynstack_unwind (dynstack, dynstack->base + join_height);
  350. return branch->base + join_height;
  351. }
  352. scm_t_bits*
  353. scm_dynstack_find_prompt (scm_t_dynstack *dynstack, SCM key,
  354. scm_t_dynstack_prompt_flags *flags,
  355. SCM **fp, SCM **sp, scm_t_uint8 **ip,
  356. scm_i_jmp_buf **registers)
  357. {
  358. scm_t_bits *walk;
  359. for (walk = SCM_DYNSTACK_PREV (dynstack->top); walk;
  360. walk = SCM_DYNSTACK_PREV (walk))
  361. {
  362. scm_t_bits tag = SCM_DYNSTACK_TAG (walk);
  363. if (SCM_DYNSTACK_TAG_TYPE (tag) == SCM_DYNSTACK_TYPE_PROMPT
  364. && scm_is_eq (PROMPT_KEY (walk), key))
  365. {
  366. if (flags)
  367. *flags = SCM_DYNSTACK_TAG_FLAGS (tag);
  368. if (fp)
  369. *fp = PROMPT_FP (walk);
  370. if (sp)
  371. *sp = PROMPT_SP (walk);
  372. if (ip)
  373. *ip = PROMPT_IP (walk);
  374. if (registers)
  375. *registers = PROMPT_JMPBUF (walk);
  376. return walk;
  377. }
  378. }
  379. return NULL;
  380. }
  381. void
  382. scm_dynstack_wind_prompt (scm_t_dynstack *dynstack, scm_t_bits *item,
  383. scm_t_ptrdiff reloc, scm_i_jmp_buf *registers)
  384. {
  385. scm_t_bits tag = SCM_DYNSTACK_TAG (item);
  386. if (SCM_DYNSTACK_TAG_TYPE (tag) != SCM_DYNSTACK_TYPE_PROMPT)
  387. abort ();
  388. scm_dynstack_push_prompt (dynstack,
  389. SCM_DYNSTACK_TAG_FLAGS (tag),
  390. PROMPT_KEY (item),
  391. PROMPT_FP (item) + reloc,
  392. PROMPT_SP (item) + reloc,
  393. PROMPT_IP (item),
  394. registers);
  395. }
  396. void
  397. scm_dynstack_unwind_frame (scm_t_dynstack *dynstack)
  398. {
  399. /* Unwind up to and including the next frame entry. */
  400. while (1)
  401. {
  402. scm_t_bits tag, *words;
  403. tag = dynstack_pop (dynstack, &words);
  404. switch (SCM_DYNSTACK_TAG_TYPE (tag))
  405. {
  406. case SCM_DYNSTACK_TYPE_FRAME:
  407. return;
  408. case SCM_DYNSTACK_TYPE_REWINDER:
  409. clear_scm_t_bits (words, WINDER_WORDS);
  410. continue;
  411. case SCM_DYNSTACK_TYPE_UNWINDER:
  412. {
  413. scm_t_guard proc = WINDER_PROC (words);
  414. void *data = WINDER_DATA (words);
  415. clear_scm_t_bits (words, WINDER_WORDS);
  416. if (SCM_DYNSTACK_TAG_FLAGS (tag) & SCM_F_DYNSTACK_WINDER_EXPLICIT)
  417. proc (data);
  418. continue;
  419. }
  420. default:
  421. /* We should only see winders. */
  422. abort ();
  423. }
  424. }
  425. }
  426. /* This function must not allocate. */
  427. void
  428. scm_dynstack_unwind_fluids (scm_t_dynstack *dynstack, SCM dynamic_state)
  429. {
  430. scm_t_bits tag, *words;
  431. size_t len;
  432. tag = dynstack_pop (dynstack, &words);
  433. len = SCM_DYNSTACK_TAG_LEN (tag);
  434. assert (SCM_DYNSTACK_TAG_TYPE (tag) == SCM_DYNSTACK_TYPE_WITH_FLUIDS);
  435. assert (len >= 1);
  436. scm_swap_fluids (len - 1, WITH_FLUIDS_FLUIDS (words),
  437. WITH_FLUIDS_VALUES (words), dynamic_state);
  438. clear_scm_t_bits (words, len);
  439. }
  440. /*
  441. Local Variables:
  442. c-file-style: "gnu"
  443. End:
  444. */