continuations.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /* Copyright (C) 1995,1996,1998,2000,2001,2004, 2006, 2008, 2009, 2010, 2011, 2012, 2013, 2014 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. {
  135. scm_gc_after_nonlocal_exit ();
  136. return SCM_UNDEFINED;
  137. }
  138. }
  139. #undef FUNC_NAME
  140. int
  141. scm_i_continuation_to_frame (SCM continuation, struct scm_frame *frame)
  142. {
  143. SCM contregs;
  144. scm_t_contregs *cont;
  145. contregs = SCM_PROGRAM_FREE_VARIABLE_REF (continuation, 0);
  146. cont = SCM_CONTREGS (contregs);
  147. if (scm_is_true (cont->vm_cont))
  148. {
  149. struct scm_vm_cont *data = SCM_VM_CONT_DATA (cont->vm_cont);
  150. union scm_vm_stack_element *stack_top;
  151. /* FIXME vm_cont should hold fp/sp offsets */
  152. stack_top = data->stack_bottom + data->stack_size;
  153. frame->stack_holder = data;
  154. frame->fp_offset = stack_top - (data->fp + data->reloc);
  155. frame->sp_offset = data->stack_size;
  156. frame->ip = data->ra;
  157. return 1;
  158. }
  159. else
  160. return 0;
  161. }
  162. struct scm_vm *
  163. scm_i_contregs_vp (SCM contregs)
  164. {
  165. return SCM_CONTREGS (contregs)->vp;
  166. }
  167. SCM
  168. scm_i_contregs_vm_cont (SCM contregs)
  169. {
  170. return SCM_CONTREGS (contregs)->vm_cont;
  171. }
  172. /* {Apply}
  173. */
  174. /* Invoking a continuation proceeds as follows:
  175. *
  176. * - the stack is made large enough for the called continuation
  177. * - the old windchain is unwound down to the branching point
  178. * - the continuation stack is copied into place
  179. * - the windchain is rewound up to the continuation's context
  180. * - the continuation is invoked via longjmp (or setcontext)
  181. *
  182. * This order is important so that unwind and rewind handlers are run
  183. * with their correct stack.
  184. */
  185. static void scm_dynthrow (SCM);
  186. /* Grow the stack by a fixed amount to provide space to copy in the
  187. * continuation. Possibly this function has to be called several times
  188. * recursively before enough space is available. Make sure the compiler does
  189. * not optimize the growth array away by storing it's address into a global
  190. * variable.
  191. */
  192. static scm_t_bits scm_i_dummy;
  193. static void
  194. grow_stack (SCM cont)
  195. {
  196. scm_t_bits growth[100];
  197. scm_i_dummy = (scm_t_bits) growth;
  198. scm_dynthrow (cont);
  199. }
  200. /* Copy the continuation stack into the current stack. Calling functions from
  201. * within this function is safe, since only stack frames below this function's
  202. * own frame are overwritten. Thus, memcpy can be used for best performance.
  203. */
  204. static void
  205. copy_stack_and_call (scm_t_contregs *continuation,
  206. SCM_STACKITEM * dst)
  207. {
  208. scm_t_dynstack *dynstack;
  209. scm_t_bits *joint;
  210. scm_i_thread *thread = SCM_I_CURRENT_THREAD;
  211. dynstack = SCM_VM_CONT_DATA (continuation->vm_cont)->dynstack;
  212. joint = scm_dynstack_unwind_fork (&thread->dynstack, dynstack);
  213. memcpy (dst, continuation->stack,
  214. sizeof (SCM_STACKITEM) * continuation->num_stack_items);
  215. #ifdef __ia64__
  216. thread->pending_rbs_continuation = continuation;
  217. #endif
  218. scm_dynstack_wind (&thread->dynstack, joint);
  219. SCM_I_LONGJMP (continuation->jmpbuf, 1);
  220. }
  221. #ifdef __ia64__
  222. void
  223. scm_ia64_longjmp (scm_i_jmp_buf *JB, int VAL)
  224. {
  225. scm_i_thread *t = SCM_I_CURRENT_THREAD;
  226. if (t->pending_rbs_continuation)
  227. {
  228. memcpy (t->register_backing_store_base,
  229. t->pending_rbs_continuation->backing_store,
  230. t->pending_rbs_continuation->backing_store_size);
  231. t->pending_rbs_continuation = NULL;
  232. }
  233. setcontext (&JB->ctx);
  234. }
  235. #endif
  236. /* Call grow_stack until the stack space is large enough, then, as the current
  237. * stack frame might get overwritten, let copy_stack_and_call perform the
  238. * actual copying and continuation calling.
  239. */
  240. static void
  241. scm_dynthrow (SCM cont)
  242. {
  243. scm_i_thread *thread = SCM_I_CURRENT_THREAD;
  244. scm_t_contregs *continuation = SCM_CONTREGS (cont);
  245. SCM_STACKITEM *dst = thread->continuation_base;
  246. SCM_STACKITEM stack_top_element;
  247. if (thread->critical_section_level)
  248. {
  249. fprintf (stderr, "continuation invoked from within critical section.\n");
  250. abort ();
  251. }
  252. #if SCM_STACK_GROWS_UP
  253. if (dst + continuation->num_stack_items >= &stack_top_element)
  254. grow_stack (cont);
  255. #else
  256. dst -= continuation->num_stack_items;
  257. if (dst <= &stack_top_element)
  258. grow_stack (cont);
  259. #endif /* def SCM_STACK_GROWS_UP */
  260. SCM_FLUSH_REGISTER_WINDOWS;
  261. copy_stack_and_call (continuation, dst);
  262. }
  263. void
  264. scm_i_check_continuation (SCM cont)
  265. {
  266. scm_i_thread *thread = SCM_I_CURRENT_THREAD;
  267. scm_t_contregs *continuation = SCM_CONTREGS (cont);
  268. if (!scm_is_eq (continuation->root, thread->continuation_root))
  269. scm_misc_error
  270. ("%continuation-call",
  271. "invoking continuation would cross continuation barrier: ~A",
  272. scm_list_1 (cont));
  273. }
  274. void
  275. scm_i_reinstate_continuation (SCM cont)
  276. {
  277. scm_dynthrow (cont);
  278. }
  279. SCM
  280. scm_i_with_continuation_barrier (scm_t_catch_body body,
  281. void *body_data,
  282. scm_t_catch_handler handler,
  283. void *handler_data,
  284. scm_t_catch_handler pre_unwind_handler,
  285. void *pre_unwind_handler_data)
  286. {
  287. SCM_STACKITEM stack_item;
  288. scm_i_thread *thread = SCM_I_CURRENT_THREAD;
  289. SCM old_controot;
  290. SCM_STACKITEM *old_contbase;
  291. SCM result;
  292. /* Establish a fresh continuation root.
  293. */
  294. old_controot = thread->continuation_root;
  295. old_contbase = thread->continuation_base;
  296. thread->continuation_root = scm_cons (thread->handle, old_controot);
  297. thread->continuation_base = &stack_item;
  298. /* Call FUNC inside a catch all. This is now guaranteed to return
  299. directly and exactly once.
  300. */
  301. result = scm_c_catch (SCM_BOOL_T,
  302. body, body_data,
  303. handler, handler_data,
  304. pre_unwind_handler, pre_unwind_handler_data);
  305. /* Return to old continuation root.
  306. */
  307. thread->continuation_base = old_contbase;
  308. thread->continuation_root = old_controot;
  309. return result;
  310. }
  311. static int
  312. should_print_backtrace (SCM tag, SCM stack)
  313. {
  314. return SCM_BACKTRACE_P
  315. && scm_is_true (stack)
  316. && scm_initialized_p
  317. /* It's generally not useful to print backtraces for errors reading
  318. or expanding code in these fallback catch statements. */
  319. && !scm_is_eq (tag, scm_from_latin1_symbol ("read-error"))
  320. && !scm_is_eq (tag, scm_from_latin1_symbol ("syntax-error"));
  321. }
  322. static void
  323. print_exception_and_backtrace (SCM port, SCM tag, SCM args)
  324. {
  325. SCM stack, frame;
  326. /* We get here via a throw to a catch-all. In that case there is the
  327. throw frame active, and this catch closure, so narrow by two
  328. frames. */
  329. stack = scm_make_stack (SCM_BOOL_T, scm_list_1 (scm_from_int (2)));
  330. frame = scm_is_true (stack) ? scm_stack_ref (stack, SCM_INUM0) : SCM_BOOL_F;
  331. if (should_print_backtrace (tag, stack))
  332. {
  333. scm_puts_unlocked ("Backtrace:\n", port);
  334. scm_display_backtrace_with_highlights (stack, port,
  335. SCM_BOOL_F, SCM_BOOL_F,
  336. SCM_EOL);
  337. scm_newline (port);
  338. }
  339. scm_print_exception (port, frame, tag, args);
  340. }
  341. struct c_data {
  342. void *(*func) (void *);
  343. void *data;
  344. void *result;
  345. };
  346. static SCM
  347. c_body (void *d)
  348. {
  349. struct c_data *data = (struct c_data *)d;
  350. data->result = data->func (data->data);
  351. return SCM_UNSPECIFIED;
  352. }
  353. static SCM
  354. c_handler (void *d, SCM tag, SCM args)
  355. {
  356. struct c_data *data;
  357. /* If TAG is `quit', exit() the process. */
  358. if (scm_is_eq (tag, scm_from_latin1_symbol ("quit")))
  359. exit (scm_exit_status (args));
  360. data = (struct c_data *)d;
  361. data->result = NULL;
  362. return SCM_UNSPECIFIED;
  363. }
  364. static SCM
  365. pre_unwind_handler (void *error_port, SCM tag, SCM args)
  366. {
  367. /* Print the exception unless TAG is `quit'. */
  368. if (!scm_is_eq (tag, scm_from_latin1_symbol ("quit")))
  369. print_exception_and_backtrace (SCM_PACK_POINTER (error_port), tag, args);
  370. return SCM_UNSPECIFIED;
  371. }
  372. void *
  373. scm_c_with_continuation_barrier (void *(*func) (void *), void *data)
  374. {
  375. struct c_data c_data;
  376. c_data.func = func;
  377. c_data.data = data;
  378. scm_i_with_continuation_barrier (c_body, &c_data,
  379. c_handler, &c_data,
  380. pre_unwind_handler,
  381. SCM_UNPACK_POINTER (scm_current_error_port ()));
  382. return c_data.result;
  383. }
  384. struct scm_data {
  385. SCM proc;
  386. };
  387. static SCM
  388. scm_body (void *d)
  389. {
  390. struct scm_data *data = (struct scm_data *)d;
  391. return scm_call_0 (data->proc);
  392. }
  393. static SCM
  394. scm_handler (void *d, SCM tag, SCM args)
  395. {
  396. /* Print a message. Note that if TAG is `quit', this will exit() the
  397. process. */
  398. scm_handle_by_message_noexit (NULL, tag, args);
  399. return SCM_BOOL_F;
  400. }
  401. SCM_DEFINE (scm_with_continuation_barrier, "with-continuation-barrier", 1,0,0,
  402. (SCM proc),
  403. "Call @var{proc} and return its result. Do not allow the invocation of\n"
  404. "continuations that would leave or enter the dynamic extent of the call\n"
  405. "to @code{with-continuation-barrier}. Such an attempt causes an error\n"
  406. "to be signaled.\n"
  407. "\n"
  408. "Throws (such as errors) that are not caught from within @var{proc} are\n"
  409. "caught by @code{with-continuation-barrier}. In that case, a short\n"
  410. "message is printed to the current error port and @code{#f} is returned.\n"
  411. "\n"
  412. "Thus, @code{with-continuation-barrier} returns exactly once.\n")
  413. #define FUNC_NAME s_scm_with_continuation_barrier
  414. {
  415. struct scm_data scm_data;
  416. scm_data.proc = proc;
  417. return scm_i_with_continuation_barrier (scm_body, &scm_data,
  418. scm_handler, &scm_data,
  419. pre_unwind_handler,
  420. SCM_UNPACK_POINTER (scm_current_error_port ()));
  421. }
  422. #undef FUNC_NAME
  423. void
  424. scm_init_continuations ()
  425. {
  426. tc16_continuation = scm_make_smob_type ("continuation", 0);
  427. scm_set_smob_print (tc16_continuation, continuation_print);
  428. #include "libguile/continuations.x"
  429. }
  430. /*
  431. Local Variables:
  432. c-file-style: "gnu"
  433. End:
  434. */