continuations.c 14 KB

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