dynstack.c 14 KB

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