continuations.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /* Copyright (C) 1995,1996,1998,2000,2001,2004, 2006, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2017 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 "libguile/_scm.h"
  22. #include <assert.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include "libguile/async.h"
  26. #include "libguile/debug.h"
  27. #include "libguile/stackchk.h"
  28. #include "libguile/smob.h"
  29. #include "libguile/ports.h"
  30. #include "libguile/dynstack.h"
  31. #include "libguile/eval.h"
  32. #include "libguile/vm.h"
  33. #include "libguile/instructions.h"
  34. #include "libguile/validate.h"
  35. #include "libguile/continuations.h"
  36. static scm_t_bits tc16_continuation;
  37. #define SCM_CONTREGSP(x) SCM_TYP16_PREDICATE (tc16_continuation, x)
  38. #define SCM_CONTREGS(x) ((scm_t_contregs *) SCM_SMOB_DATA_1 (x))
  39. #define SCM_CONTINUATION_LENGTH(x) (SCM_CONTREGS (x)->num_stack_items)
  40. #define SCM_SET_CONTINUATION_LENGTH(x, n)\
  41. (SCM_CONTREGS (x)->num_stack_items = (n))
  42. #define SCM_JMPBUF(x) ((SCM_CONTREGS (x))->jmpbuf)
  43. #define SCM_CONTINUATION_ROOT(x) ((SCM_CONTREGS (x))->root)
  44. #define SCM_DFRAME(x) ((SCM_CONTREGS (x))->dframe)
  45. /* scm_i_make_continuation will return a procedure whose code will
  46. reinstate the continuation. Here, as in gsubr.c, we define the form
  47. of that trampoline function.
  48. */
  49. static const scm_t_uint32 continuation_stub_code[] =
  50. {
  51. SCM_PACK_OP_24 (continuation_call, 0)
  52. };
  53. static SCM
  54. make_continuation_trampoline (SCM contregs)
  55. {
  56. SCM ret;
  57. scm_t_bits nfree = 1;
  58. scm_t_bits flags = SCM_F_PROGRAM_IS_CONTINUATION;
  59. ret = scm_words (scm_tc7_program | (nfree << 16) | flags, nfree + 2);
  60. SCM_SET_CELL_WORD_1 (ret, continuation_stub_code);
  61. SCM_PROGRAM_FREE_VARIABLE_SET (ret, 0, contregs);
  62. return ret;
  63. }
  64. /* {Continuations}
  65. */
  66. static int
  67. continuation_print (SCM obj, SCM port, scm_print_state *state SCM_UNUSED)
  68. {
  69. scm_t_contregs *continuation = SCM_CONTREGS (obj);
  70. scm_puts ("#<continuation ", port);
  71. scm_intprint (continuation->num_stack_items, 10, port);
  72. scm_puts (" @ ", port);
  73. scm_uintprint (SCM_SMOB_DATA_1 (obj), 16, port);
  74. scm_putc ('>', port);
  75. return 1;
  76. }
  77. /* James Clark came up with this neat one instruction fix for
  78. * continuations on the SPARC. It flushes the register windows so
  79. * that all the state of the process is contained in the stack.
  80. */
  81. #if defined (sparc) || defined (__sparc__) || defined (__sparc)
  82. # define SCM_FLUSH_REGISTER_WINDOWS asm("ta 3")
  83. #else
  84. # define SCM_FLUSH_REGISTER_WINDOWS /* empty */
  85. #endif
  86. /* this may return more than once: the first time with the escape
  87. procedure, then subsequently with SCM_UNDEFINED (the vals already having been
  88. placed on the VM stack). */
  89. #define FUNC_NAME "scm_i_make_continuation"
  90. SCM
  91. scm_i_make_continuation (int *first, struct scm_vm *vp, SCM vm_cont)
  92. {
  93. scm_i_thread *thread = SCM_I_CURRENT_THREAD;
  94. SCM cont;
  95. scm_t_contregs *continuation;
  96. long stack_size;
  97. const void *saved_cookie;
  98. SCM_STACKITEM * src;
  99. SCM_FLUSH_REGISTER_WINDOWS;
  100. stack_size = scm_stack_size (thread->continuation_base);
  101. continuation = scm_gc_malloc (sizeof (scm_t_contregs)
  102. + (stack_size - 1) * sizeof (SCM_STACKITEM),
  103. "continuation");
  104. continuation->num_stack_items = stack_size;
  105. continuation->root = thread->continuation_root;
  106. src = thread->continuation_base;
  107. #if ! SCM_STACK_GROWS_UP
  108. src -= stack_size;
  109. #endif
  110. continuation->offset = continuation->stack - src;
  111. memcpy (continuation->stack, src, sizeof (SCM_STACKITEM) * stack_size);
  112. continuation->vp = vp;
  113. continuation->vm_cont = vm_cont;
  114. saved_cookie = vp->resumable_prompt_cookie;
  115. SCM_NEWSMOB (cont, tc16_continuation, continuation);
  116. *first = !SCM_I_SETJMP (continuation->jmpbuf);
  117. if (*first)
  118. {
  119. #ifdef __ia64__
  120. continuation->backing_store_size =
  121. (char *) scm_ia64_ar_bsp(&continuation->jmpbuf.ctx)
  122. -
  123. (char *) thread->register_backing_store_base;
  124. continuation->backing_store = NULL;
  125. continuation->backing_store =
  126. scm_gc_malloc (continuation->backing_store_size,
  127. "continuation backing store");
  128. memcpy (continuation->backing_store,
  129. (void *) thread->register_backing_store_base,
  130. continuation->backing_store_size);
  131. #endif /* __ia64__ */
  132. return make_continuation_trampoline (cont);
  133. }
  134. else
  135. {
  136. vp->resumable_prompt_cookie = saved_cookie;
  137. scm_gc_after_nonlocal_exit ();
  138. return SCM_UNDEFINED;
  139. }
  140. }
  141. #undef FUNC_NAME
  142. int
  143. scm_i_continuation_to_frame (SCM continuation, struct scm_frame *frame)
  144. {
  145. SCM contregs;
  146. scm_t_contregs *cont;
  147. contregs = SCM_PROGRAM_FREE_VARIABLE_REF (continuation, 0);
  148. cont = SCM_CONTREGS (contregs);
  149. if (scm_is_true (cont->vm_cont))
  150. {
  151. struct scm_vm_cont *data = SCM_VM_CONT_DATA (cont->vm_cont);
  152. frame->stack_holder = data;
  153. frame->fp_offset = data->fp_offset;
  154. frame->sp_offset = data->stack_size;
  155. frame->ip = data->ra;
  156. return 1;
  157. }
  158. else
  159. return 0;
  160. }
  161. struct scm_vm *
  162. scm_i_contregs_vp (SCM contregs)
  163. {
  164. return SCM_CONTREGS (contregs)->vp;
  165. }
  166. SCM
  167. scm_i_contregs_vm_cont (SCM contregs)
  168. {
  169. return SCM_CONTREGS (contregs)->vm_cont;
  170. }
  171. /* {Apply}
  172. */
  173. /* Invoking a continuation proceeds as follows:
  174. *
  175. * - the stack is made large enough for the called continuation
  176. * - the old windchain is unwound down to the branching point
  177. * - the continuation stack is copied into place
  178. * - the windchain is rewound up to the continuation's context
  179. * - the continuation is invoked via longjmp (or setcontext)
  180. *
  181. * This order is important so that unwind and rewind handlers are run
  182. * with their correct stack.
  183. */
  184. static void scm_dynthrow (SCM);
  185. /* Grow the stack by a fixed amount to provide space to copy in the
  186. * continuation. Possibly this function has to be called several times
  187. * recursively before enough space is available. Make sure the compiler does
  188. * not optimize the growth array away by storing it's address into a global
  189. * variable.
  190. */
  191. static scm_t_bits scm_i_dummy;
  192. static void
  193. grow_stack (SCM cont)
  194. {
  195. scm_t_bits growth[100];
  196. scm_i_dummy = (scm_t_bits) growth;
  197. scm_dynthrow (cont);
  198. }
  199. /* Copy the continuation stack into the current stack. Calling functions from
  200. * within this function is safe, since only stack frames below this function's
  201. * own frame are overwritten. Thus, memcpy can be used for best performance.
  202. */
  203. static void
  204. copy_stack_and_call (scm_t_contregs *continuation,
  205. SCM_STACKITEM * dst)
  206. {
  207. scm_t_dynstack *dynstack;
  208. scm_t_bits *joint;
  209. scm_i_thread *thread = SCM_I_CURRENT_THREAD;
  210. dynstack = SCM_VM_CONT_DATA (continuation->vm_cont)->dynstack;
  211. joint = scm_dynstack_unwind_fork (&thread->dynstack, dynstack);
  212. memcpy (dst, continuation->stack,
  213. sizeof (SCM_STACKITEM) * continuation->num_stack_items);
  214. #ifdef __ia64__
  215. thread->pending_rbs_continuation = continuation;
  216. #endif
  217. scm_dynstack_wind (&thread->dynstack, joint);
  218. SCM_I_LONGJMP (continuation->jmpbuf, 1);
  219. }
  220. #ifdef __ia64__
  221. void
  222. scm_ia64_longjmp (scm_i_jmp_buf *JB, int VAL)
  223. {
  224. scm_i_thread *t = SCM_I_CURRENT_THREAD;
  225. if (t->pending_rbs_continuation)
  226. {
  227. memcpy (t->register_backing_store_base,
  228. t->pending_rbs_continuation->backing_store,
  229. t->pending_rbs_continuation->backing_store_size);
  230. t->pending_rbs_continuation = NULL;
  231. }
  232. setcontext (&JB->ctx);
  233. }
  234. #endif
  235. /* Call grow_stack until the stack space is large enough, then, as the current
  236. * stack frame might get overwritten, let copy_stack_and_call perform the
  237. * actual copying and continuation calling.
  238. */
  239. static void
  240. scm_dynthrow (SCM cont)
  241. {
  242. scm_i_thread *thread = SCM_I_CURRENT_THREAD;
  243. scm_t_contregs *continuation = SCM_CONTREGS (cont);
  244. SCM_STACKITEM *dst = thread->continuation_base;
  245. SCM_STACKITEM stack_top_element;
  246. #if SCM_STACK_GROWS_UP
  247. if (dst + continuation->num_stack_items >= &stack_top_element)
  248. grow_stack (cont);
  249. #else
  250. dst -= continuation->num_stack_items;
  251. if (dst <= &stack_top_element)
  252. grow_stack (cont);
  253. #endif /* def SCM_STACK_GROWS_UP */
  254. SCM_FLUSH_REGISTER_WINDOWS;
  255. copy_stack_and_call (continuation, dst);
  256. }
  257. void
  258. scm_i_check_continuation (SCM cont)
  259. {
  260. scm_i_thread *thread = SCM_I_CURRENT_THREAD;
  261. scm_t_contregs *continuation = SCM_CONTREGS (cont);
  262. if (!scm_is_eq (continuation->root, thread->continuation_root))
  263. scm_misc_error
  264. ("%continuation-call",
  265. "invoking continuation would cross continuation barrier: ~A",
  266. scm_list_1 (cont));
  267. }
  268. void
  269. scm_i_reinstate_continuation (SCM cont)
  270. {
  271. scm_dynthrow (cont);
  272. }
  273. SCM
  274. scm_i_with_continuation_barrier (scm_t_catch_body body,
  275. void *body_data,
  276. scm_t_catch_handler handler,
  277. void *handler_data,
  278. scm_t_catch_handler pre_unwind_handler,
  279. void *pre_unwind_handler_data)
  280. {
  281. SCM_STACKITEM stack_item;
  282. scm_i_thread *thread = SCM_I_CURRENT_THREAD;
  283. SCM old_controot;
  284. SCM_STACKITEM *old_contbase;
  285. SCM result;
  286. /* Establish a fresh continuation root.
  287. */
  288. old_controot = thread->continuation_root;
  289. old_contbase = thread->continuation_base;
  290. thread->continuation_root = scm_cons (thread->handle, old_controot);
  291. thread->continuation_base = &stack_item;
  292. /* Call FUNC inside a catch all. This is now guaranteed to return
  293. directly and exactly once.
  294. */
  295. result = scm_c_catch (SCM_BOOL_T,
  296. body, body_data,
  297. handler, handler_data,
  298. pre_unwind_handler, pre_unwind_handler_data);
  299. /* Return to old continuation root.
  300. */
  301. thread->continuation_base = old_contbase;
  302. thread->continuation_root = old_controot;
  303. return result;
  304. }
  305. static int
  306. should_print_backtrace (SCM tag, SCM stack)
  307. {
  308. return SCM_BACKTRACE_P
  309. && scm_is_true (stack)
  310. && scm_initialized_p
  311. /* It's generally not useful to print backtraces for errors reading
  312. or expanding code in these fallback catch statements. */
  313. && !scm_is_eq (tag, scm_from_latin1_symbol ("read-error"))
  314. && !scm_is_eq (tag, scm_from_latin1_symbol ("syntax-error"));
  315. }
  316. static void
  317. print_exception_and_backtrace (SCM port, SCM tag, SCM args)
  318. {
  319. SCM stack, frame;
  320. /* We get here via a throw to a catch-all. In that case there is the
  321. throw frame active, and this catch closure, so narrow by two
  322. frames. */
  323. stack = scm_make_stack (SCM_BOOL_T, scm_list_1 (scm_from_int (2)));
  324. frame = scm_is_true (stack) ? scm_stack_ref (stack, SCM_INUM0) : SCM_BOOL_F;
  325. if (should_print_backtrace (tag, stack))
  326. {
  327. scm_puts ("Backtrace:\n", port);
  328. scm_display_backtrace_with_highlights (stack, port,
  329. SCM_BOOL_F, SCM_BOOL_F,
  330. SCM_EOL);
  331. scm_newline (port);
  332. }
  333. scm_print_exception (port, frame, tag, args);
  334. }
  335. struct c_data {
  336. void *(*func) (void *);
  337. void *data;
  338. void *result;
  339. };
  340. static SCM
  341. c_body (void *d)
  342. {
  343. struct c_data *data = (struct c_data *)d;
  344. data->result = data->func (data->data);
  345. return SCM_UNSPECIFIED;
  346. }
  347. static SCM
  348. c_handler (void *d, SCM tag, SCM args)
  349. {
  350. struct c_data *data;
  351. /* If TAG is `quit', exit() the process. */
  352. if (scm_is_eq (tag, scm_from_latin1_symbol ("quit")))
  353. exit (scm_exit_status (args));
  354. data = (struct c_data *)d;
  355. data->result = NULL;
  356. return SCM_UNSPECIFIED;
  357. }
  358. static SCM
  359. pre_unwind_handler (void *error_port, SCM tag, SCM args)
  360. {
  361. /* Print the exception unless TAG is `quit'. */
  362. if (!scm_is_eq (tag, scm_from_latin1_symbol ("quit")))
  363. print_exception_and_backtrace (SCM_PACK_POINTER (error_port), tag, args);
  364. return SCM_UNSPECIFIED;
  365. }
  366. void *
  367. scm_c_with_continuation_barrier (void *(*func) (void *), void *data)
  368. {
  369. struct c_data c_data;
  370. c_data.func = func;
  371. c_data.data = data;
  372. scm_i_with_continuation_barrier (c_body, &c_data,
  373. c_handler, &c_data,
  374. pre_unwind_handler,
  375. SCM_UNPACK_POINTER (scm_current_error_port ()));
  376. return c_data.result;
  377. }
  378. struct scm_data {
  379. SCM proc;
  380. };
  381. static SCM
  382. scm_body (void *d)
  383. {
  384. struct scm_data *data = (struct scm_data *)d;
  385. return scm_call_0 (data->proc);
  386. }
  387. static SCM
  388. scm_handler (void *d, SCM tag, SCM args)
  389. {
  390. /* Print a message. Note that if TAG is `quit', this will exit() the
  391. process. */
  392. scm_handle_by_message_noexit (NULL, tag, args);
  393. return SCM_BOOL_F;
  394. }
  395. SCM_DEFINE (scm_with_continuation_barrier, "with-continuation-barrier", 1,0,0,
  396. (SCM proc),
  397. "Call @var{proc} and return its result. Do not allow the invocation of\n"
  398. "continuations that would leave or enter the dynamic extent of the call\n"
  399. "to @code{with-continuation-barrier}. Such an attempt causes an error\n"
  400. "to be signaled.\n"
  401. "\n"
  402. "Throws (such as errors) that are not caught from within @var{proc} are\n"
  403. "caught by @code{with-continuation-barrier}. In that case, a short\n"
  404. "message is printed to the current error port and @code{#f} is returned.\n"
  405. "\n"
  406. "Thus, @code{with-continuation-barrier} returns exactly once.\n")
  407. #define FUNC_NAME s_scm_with_continuation_barrier
  408. {
  409. struct scm_data scm_data;
  410. scm_data.proc = proc;
  411. return scm_i_with_continuation_barrier (scm_body, &scm_data,
  412. scm_handler, &scm_data,
  413. pre_unwind_handler,
  414. SCM_UNPACK_POINTER (scm_current_error_port ()));
  415. }
  416. #undef FUNC_NAME
  417. void
  418. scm_init_continuations ()
  419. {
  420. tc16_continuation = scm_make_smob_type ("continuation", 0);
  421. scm_set_smob_print (tc16_continuation, continuation_print);
  422. #include "libguile/continuations.x"
  423. }
  424. /*
  425. Local Variables:
  426. c-file-style: "gnu"
  427. End:
  428. */