control.c 9.1 KB

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