control.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* Copyright (C) 2010, 2011, 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. #if HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <alloca.h>
  22. #include "libguile/_scm.h"
  23. #include "libguile/control.h"
  24. #include "libguile/objcodes.h"
  25. #include "libguile/instructions.h"
  26. #include "libguile/vm.h"
  27. SCM
  28. scm_c_make_prompt (SCM k, SCM *fp, SCM *sp, scm_t_uint8 *abort_ip,
  29. scm_t_uint8 escape_only_p, scm_t_int64 vm_cookie,
  30. SCM winds)
  31. {
  32. scm_t_bits tag;
  33. struct scm_prompt_registers *regs;
  34. tag = scm_tc7_prompt;
  35. if (escape_only_p)
  36. tag |= (SCM_F_PROMPT_ESCAPE<<8);
  37. regs = scm_gc_malloc_pointerless (sizeof (*regs), "prompt registers");
  38. regs->fp = fp;
  39. regs->sp = sp;
  40. regs->ip = abort_ip;
  41. regs->cookie = vm_cookie;
  42. return scm_double_cell (tag, SCM_UNPACK (k), (scm_t_bits)regs,
  43. SCM_UNPACK (winds));
  44. }
  45. /* Only to be called if the SCM_PROMPT_SETJMP returns 1 */
  46. SCM
  47. scm_i_prompt_pop_abort_args_x (SCM vm)
  48. {
  49. size_t i, n;
  50. SCM vals = SCM_EOL;
  51. n = scm_to_size_t (SCM_VM_DATA (vm)->sp[0]);
  52. for (i = 0; i < n; i++)
  53. vals = scm_cons (SCM_VM_DATA (vm)->sp[-(i + 1)], vals);
  54. /* The abort did reset the VM's registers, but then these values
  55. were pushed on; so we need to pop them ourselves. */
  56. SCM_VM_DATA (vm)->sp -= n + 1;
  57. /* FIXME NULLSTACK */
  58. return vals;
  59. }
  60. #ifdef WORDS_BIGENDIAN
  61. #define OBJCODE_HEADER(main,meta) 0, 0, 0, main, 0, 0, 0, meta+8
  62. #define META_HEADER(meta) 0, 0, 0, meta, 0, 0, 0, 0
  63. #else
  64. #define OBJCODE_HEADER(main,meta) main, 0, 0, 0, meta+8, 0, 0, 0
  65. #define META_HEADER(meta) meta, 0, 0, 0, 0, 0, 0, 0
  66. #endif
  67. #define OBJCODE_TAG SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_STATIC, 0)
  68. #if defined (SCM_ALIGNED)
  69. #define SCM_DECLARE_STATIC_ALIGNED_ARRAY(type, sym) \
  70. static const type sym[]
  71. #define SCM_STATIC_ALIGNED_ARRAY(alignment, type, sym) \
  72. static SCM_ALIGNED (alignment) const type sym[]
  73. #define SCM_STATIC_OBJCODE(sym) \
  74. SCM_DECLARE_STATIC_ALIGNED_ARRAY (scm_t_uint8, sym##__bytecode); \
  75. SCM_STATIC_ALIGNED_ARRAY (8, scm_t_cell, sym##__cells) = { \
  76. { SCM_PACK (OBJCODE_TAG), SCM_PACK (sym##__bytecode) }, \
  77. { SCM_BOOL_F, SCM_PACK (0) } \
  78. }; \
  79. static const SCM sym = SCM_PACK (sym##__cells); \
  80. SCM_STATIC_ALIGNED_ARRAY (8, scm_t_uint8, sym##__bytecode)
  81. #else
  82. #define SCM_STATIC_OBJCODE(sym) \
  83. static SCM sym; \
  84. static scm_t_uint8 *sym##_bytecode; \
  85. SCM_SNARF_INIT(sym##_bytecode = scm_gc_malloc_pointerless (sizeof(sym##_bytecode__unaligned), "partial continuation stub"); \
  86. memcpy (sym##_bytecode, sym##_bytecode__unaligned, sizeof(sym##_bytecode__unaligned));) \
  87. SCM_SNARF_INIT(sym = scm_double_cell (OBJCODE_TAG, \
  88. (scm_t_bits)sym##_bytecode, \
  89. SCM_UNPACK (SCM_BOOL_F), \
  90. 0);) \
  91. static const scm_t_uint8 sym##_bytecode__unaligned[]
  92. #endif
  93. SCM_STATIC_OBJCODE (cont_objcode) = {
  94. /* Like in continuations.c, but with partial-cont-call. */
  95. OBJCODE_HEADER (8, 19),
  96. /* leave args on the stack */
  97. /* 0 */ scm_op_object_ref, 0, /* push scm_vm_cont object */
  98. /* 2 */ scm_op_object_ref, 1, /* push internal winds */
  99. /* 4 */ scm_op_partial_cont_call, /* and go! */
  100. /* 5 */ scm_op_nop, scm_op_nop, scm_op_nop, /* pad to 8 bytes */
  101. /* 8 */
  102. /* We could put some meta-info to say that this proc is a continuation. Not sure
  103. how to do that, though. */
  104. META_HEADER (19),
  105. /* 0 */ scm_op_make_eol, /* bindings */
  106. /* 1 */ scm_op_make_eol, /* sources */
  107. /* 2 */ scm_op_make_int8, 0, scm_op_make_int8, 5, /* arity: from ip 0 to ip 7 */
  108. /* 6 */ scm_op_make_int8_0, /* the arity is 0 required args */
  109. /* 7 */ scm_op_make_int8_0, /* 0 optionals */
  110. /* 8 */ scm_op_make_true, /* and a rest arg */
  111. /* 9 */ scm_op_list, 0, 5, /* make a list of those 5 vals */
  112. /* 12 */ scm_op_list, 0, 1, /* and the arities will be a list of that one list */
  113. /* 15 */ scm_op_list, 0, 3, /* pack bindings, sources, and arities into list */
  114. /* 18 */ scm_op_return /* and return */
  115. /* 19 */
  116. };
  117. static SCM
  118. reify_partial_continuation (SCM vm, SCM prompt, SCM extwinds,
  119. scm_t_int64 cookie)
  120. {
  121. SCM vm_cont, dynwinds, intwinds = SCM_EOL, ret;
  122. scm_t_uint32 flags;
  123. /* No need to reify if the continuation is never referenced in the handler. */
  124. if (SCM_PROMPT_ESCAPE_P (prompt))
  125. return SCM_BOOL_F;
  126. dynwinds = scm_i_dynwinds ();
  127. while (!scm_is_eq (dynwinds, extwinds))
  128. {
  129. intwinds = scm_cons (scm_car (dynwinds), intwinds);
  130. dynwinds = scm_cdr (dynwinds);
  131. }
  132. flags = SCM_F_VM_CONT_PARTIAL;
  133. if (cookie >= 0 && SCM_PROMPT_REGISTERS (prompt)->cookie == cookie)
  134. flags |= SCM_F_VM_CONT_REWINDABLE;
  135. /* Since non-escape continuations should begin with a thunk application, the
  136. first bit of the stack should be a frame, with the saved fp equal to the fp
  137. that was current when the prompt was made. */
  138. if ((SCM*)SCM_UNPACK (SCM_PROMPT_REGISTERS (prompt)->sp[1])
  139. != SCM_PROMPT_REGISTERS (prompt)->fp)
  140. abort ();
  141. /* Capture from the top of the thunk application frame up to the end. Set an
  142. MVRA only, as the post-abort code is in an MV context. */
  143. vm_cont = scm_i_vm_capture_stack (SCM_PROMPT_REGISTERS (prompt)->sp + 4,
  144. SCM_VM_DATA (vm)->fp,
  145. SCM_VM_DATA (vm)->sp,
  146. NULL,
  147. SCM_VM_DATA (vm)->ip,
  148. flags);
  149. ret = scm_make_program (cont_objcode,
  150. scm_vector (scm_list_2 (vm_cont, intwinds)),
  151. SCM_BOOL_F);
  152. SCM_SET_CELL_WORD_0 (ret,
  153. SCM_CELL_WORD_0 (ret) | SCM_F_PROGRAM_IS_PARTIAL_CONTINUATION);
  154. return ret;
  155. }
  156. void
  157. scm_c_abort (SCM vm, SCM tag, size_t n, SCM *argv, scm_t_int64 cookie)
  158. {
  159. SCM cont, winds, prompt = SCM_BOOL_F;
  160. long delta;
  161. size_t i;
  162. /* Search the wind list for an appropriate prompt.
  163. "Waiter, please bring us the wind list." */
  164. for (winds = scm_i_dynwinds (), delta = 0;
  165. scm_is_pair (winds);
  166. winds = SCM_CDR (winds), delta++)
  167. {
  168. SCM elt = SCM_CAR (winds);
  169. if (SCM_PROMPT_P (elt) && scm_is_eq (SCM_PROMPT_TAG (elt), tag))
  170. {
  171. prompt = elt;
  172. break;
  173. }
  174. }
  175. /* If we didn't find anything, raise an error. */
  176. if (scm_is_false (prompt))
  177. scm_misc_error ("abort", "Abort to unknown prompt", scm_list_1 (tag));
  178. cont = reify_partial_continuation (vm, prompt, winds, cookie);
  179. /* Unwind once more, beyond the prompt. */
  180. winds = SCM_CDR (winds), delta++;
  181. /* Unwind */
  182. scm_dowinds (winds, delta);
  183. /* Unwinding may have changed the current thread's VM, so use the
  184. new one. */
  185. vm = scm_the_vm ();
  186. /* Restore VM regs */
  187. SCM_VM_DATA (vm)->fp = SCM_PROMPT_REGISTERS (prompt)->fp;
  188. SCM_VM_DATA (vm)->sp = SCM_PROMPT_REGISTERS (prompt)->sp;
  189. SCM_VM_DATA (vm)->ip = SCM_PROMPT_REGISTERS (prompt)->ip;
  190. /* Since we're jumping down, we should always have enough space */
  191. if (SCM_VM_DATA (vm)->sp + n + 1 >= SCM_VM_DATA (vm)->stack_limit)
  192. abort ();
  193. /* Push vals */
  194. *(++(SCM_VM_DATA (vm)->sp)) = cont;
  195. for (i = 0; i < n; i++)
  196. *(++(SCM_VM_DATA (vm)->sp)) = argv[i];
  197. *(++(SCM_VM_DATA (vm)->sp)) = scm_from_size_t (n+1); /* +1 for continuation */
  198. /* Jump! */
  199. SCM_I_LONGJMP (SCM_PROMPT_REGISTERS (prompt)->regs, 1);
  200. /* Shouldn't get here */
  201. abort ();
  202. }
  203. SCM_DEFINE (scm_at_abort, "@abort", 2, 0, 0, (SCM tag, SCM args),
  204. "Abort to the nearest prompt with tag @var{tag}.")
  205. #define FUNC_NAME s_scm_at_abort
  206. {
  207. SCM *argv;
  208. size_t i;
  209. long n;
  210. SCM_VALIDATE_LIST_COPYLEN (SCM_ARG2, args, n);
  211. argv = alloca (sizeof (SCM)*n);
  212. for (i = 0; i < n; i++, args = scm_cdr (args))
  213. argv[i] = scm_car (args);
  214. scm_c_abort (scm_the_vm (), tag, n, argv, -1);
  215. /* Oh, what, you're still here? The abort must have been reinstated. Actually,
  216. that's quite impossible, given that we're already in C-land here, so...
  217. abort! */
  218. abort ();
  219. }
  220. #undef FUNC_NAME
  221. void
  222. scm_i_prompt_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  223. {
  224. scm_puts_unlocked ("#<prompt ", port);
  225. scm_intprint (SCM_UNPACK (exp), 16, port);
  226. scm_putc_unlocked ('>', port);
  227. }
  228. void
  229. scm_init_control (void)
  230. {
  231. #include "libguile/control.x"
  232. }
  233. /*
  234. Local Variables:
  235. c-file-style: "gnu"
  236. End:
  237. */