control.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* Copyright (C) 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. #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. #define PROMPT_ESCAPE_P(p) \
  28. (SCM_DYNSTACK_TAG_FLAGS (SCM_DYNSTACK_TAG (p)) \
  29. & SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY)
  30. /* Only to be called if the SCM_I_SETJMP returns 1 */
  31. SCM
  32. scm_i_prompt_pop_abort_args_x (SCM vm)
  33. {
  34. size_t i, n;
  35. SCM vals = SCM_EOL;
  36. n = scm_to_size_t (SCM_VM_DATA (vm)->sp[0]);
  37. for (i = 0; i < n; i++)
  38. vals = scm_cons (SCM_VM_DATA (vm)->sp[-(i + 1)], vals);
  39. /* The abort did reset the VM's registers, but then these values
  40. were pushed on; so we need to pop them ourselves. */
  41. SCM_VM_DATA (vm)->sp -= n + 1;
  42. /* FIXME NULLSTACK */
  43. return vals;
  44. }
  45. #ifdef WORDS_BIGENDIAN
  46. #define OBJCODE_HEADER(main,meta) 0, 0, 0, main, 0, 0, 0, meta+8
  47. #define META_HEADER(meta) 0, 0, 0, meta, 0, 0, 0, 0
  48. #else
  49. #define OBJCODE_HEADER(main,meta) main, 0, 0, 0, meta+8, 0, 0, 0
  50. #define META_HEADER(meta) meta, 0, 0, 0, 0, 0, 0, 0
  51. #endif
  52. #define OBJCODE_TAG SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_STATIC, 0)
  53. #if defined (SCM_ALIGNED)
  54. #define SCM_DECLARE_STATIC_ALIGNED_ARRAY(type, sym) \
  55. static const type sym[]
  56. #define SCM_STATIC_ALIGNED_ARRAY(alignment, type, sym) \
  57. static SCM_ALIGNED (alignment) const type sym[]
  58. #define SCM_STATIC_OBJCODE(sym) \
  59. SCM_DECLARE_STATIC_ALIGNED_ARRAY (scm_t_uint8, sym##__bytecode); \
  60. SCM_STATIC_ALIGNED_ARRAY (8, scm_t_cell, sym##__cells) = { \
  61. { SCM_PACK (OBJCODE_TAG), SCM_PACK (sym##__bytecode) }, \
  62. { SCM_BOOL_F, SCM_PACK (0) } \
  63. }; \
  64. static const SCM sym = SCM_PACK (sym##__cells); \
  65. SCM_STATIC_ALIGNED_ARRAY (8, scm_t_uint8, sym##__bytecode)
  66. #else
  67. #define SCM_STATIC_OBJCODE(sym) \
  68. static SCM sym; \
  69. static scm_t_uint8 *sym##_bytecode; \
  70. SCM_SNARF_INIT(sym##_bytecode = scm_gc_malloc_pointerless (sizeof(sym##_bytecode__unaligned), "partial continuation stub"); \
  71. memcpy (sym##_bytecode, sym##_bytecode__unaligned, sizeof(sym##_bytecode__unaligned));) \
  72. SCM_SNARF_INIT(sym = scm_double_cell (OBJCODE_TAG, \
  73. (scm_t_bits)sym##_bytecode, \
  74. SCM_UNPACK (SCM_BOOL_F), \
  75. 0);) \
  76. static const scm_t_uint8 sym##_bytecode__unaligned[]
  77. #endif
  78. SCM_STATIC_OBJCODE (cont_objcode) = {
  79. /* Like in continuations.c, but with partial-cont-call. */
  80. OBJCODE_HEADER (8, 19),
  81. /* leave args on the stack */
  82. /* 0 */ scm_op_object_ref, 0, /* push scm_vm_cont object */
  83. /* 2 */ scm_op_partial_cont_call, /* and go! */
  84. /* 3 */ scm_op_nop,
  85. /* 4 */ scm_op_nop, scm_op_nop, scm_op_nop, scm_op_nop, /* pad to 8 bytes */
  86. /* 8 */
  87. /* We could put some meta-info to say that this proc is a continuation. Not sure
  88. how to do that, though. */
  89. META_HEADER (19),
  90. /* 0 */ scm_op_make_eol, /* bindings */
  91. /* 1 */ scm_op_make_eol, /* sources */
  92. /* 2 */ scm_op_make_int8, 0, scm_op_make_int8, 3, /* arity: from ip 0 to ip 3 */
  93. /* 6 */ scm_op_make_int8_0, /* the arity is 0 required args */
  94. /* 7 */ scm_op_make_int8_0, /* 0 optionals */
  95. /* 8 */ scm_op_make_true, /* and a rest arg */
  96. /* 9 */ scm_op_list, 0, 5, /* make a list of those 5 vals */
  97. /* 12 */ scm_op_list, 0, 1, /* and the arities will be a list of that one list */
  98. /* 15 */ scm_op_list, 0, 3, /* pack bindings, sources, and arities into list */
  99. /* 18 */ scm_op_return /* and return */
  100. /* 19 */
  101. };
  102. static SCM
  103. reify_partial_continuation (SCM vm,
  104. SCM *saved_fp, SCM *saved_sp, scm_t_uint8 *saved_ip,
  105. scm_i_jmp_buf *saved_registers,
  106. scm_t_dynstack *dynstack,
  107. scm_i_jmp_buf *current_registers)
  108. {
  109. SCM vm_cont, ret;
  110. scm_t_uint32 flags;
  111. flags = SCM_F_VM_CONT_PARTIAL;
  112. /* If we are aborting to a prompt that has the same registers as those
  113. of the abort, it means there are no intervening C frames on the
  114. stack, and so the continuation can be relocated elsewhere on the
  115. stack: it is rewindable. */
  116. if (saved_registers && saved_registers == current_registers)
  117. flags |= SCM_F_VM_CONT_REWINDABLE;
  118. /* Since non-escape continuations should begin with a thunk application, the
  119. first bit of the stack should be a frame, with the saved fp equal to the fp
  120. that was current when the prompt was made. */
  121. if ((SCM*)SCM_UNPACK (saved_sp[1]) != saved_fp)
  122. abort ();
  123. /* Capture from the top of the thunk application frame up to the end. Set an
  124. MVRA only, as the post-abort code is in an MV context. */
  125. vm_cont = scm_i_vm_capture_stack (saved_sp + 4,
  126. SCM_VM_DATA (vm)->fp,
  127. SCM_VM_DATA (vm)->sp,
  128. NULL,
  129. SCM_VM_DATA (vm)->ip,
  130. dynstack,
  131. flags);
  132. ret = scm_make_program (cont_objcode,
  133. scm_c_make_vector (1, vm_cont),
  134. SCM_BOOL_F);
  135. SCM_SET_CELL_WORD_0 (ret,
  136. SCM_CELL_WORD_0 (ret) | SCM_F_PROGRAM_IS_PARTIAL_CONTINUATION);
  137. return ret;
  138. }
  139. void
  140. scm_c_abort (SCM vm, SCM tag, size_t n, SCM *argv,
  141. scm_i_jmp_buf *current_registers)
  142. {
  143. SCM cont;
  144. scm_t_dynstack *dynstack = &SCM_I_CURRENT_THREAD->dynstack;
  145. scm_t_bits *prompt;
  146. scm_t_dynstack_prompt_flags flags;
  147. SCM *fp, *sp;
  148. scm_t_uint8 *ip;
  149. scm_i_jmp_buf *registers;
  150. size_t i;
  151. prompt = scm_dynstack_find_prompt (dynstack, tag,
  152. &flags, &fp, &sp, &ip, &registers);
  153. if (!prompt)
  154. scm_misc_error ("abort", "Abort to unknown prompt", scm_list_1 (tag));
  155. /* Only reify if the continuation referenced in the handler. */
  156. if (flags & SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY)
  157. cont = SCM_BOOL_F;
  158. else
  159. {
  160. scm_t_dynstack *captured;
  161. captured = scm_dynstack_capture (dynstack, SCM_DYNSTACK_NEXT (prompt));
  162. cont = reify_partial_continuation (vm, fp, sp, ip, registers, captured,
  163. current_registers);
  164. }
  165. /* Unwind. */
  166. scm_dynstack_unwind (dynstack, prompt);
  167. /* Unwinding may have changed the current thread's VM, so use the
  168. new one. */
  169. vm = scm_the_vm ();
  170. /* Restore VM regs */
  171. SCM_VM_DATA (vm)->fp = fp;
  172. SCM_VM_DATA (vm)->sp = sp;
  173. SCM_VM_DATA (vm)->ip = ip;
  174. /* Since we're jumping down, we should always have enough space. */
  175. if (SCM_VM_DATA (vm)->sp + n + 1 >= SCM_VM_DATA (vm)->stack_limit)
  176. abort ();
  177. /* Push vals */
  178. *(++(SCM_VM_DATA (vm)->sp)) = cont;
  179. for (i = 0; i < n; i++)
  180. *(++(SCM_VM_DATA (vm)->sp)) = argv[i];
  181. *(++(SCM_VM_DATA (vm)->sp)) = scm_from_size_t (n+1); /* +1 for continuation */
  182. /* Jump! */
  183. SCM_I_LONGJMP (*registers, 1);
  184. /* Shouldn't get here */
  185. abort ();
  186. }
  187. SCM_DEFINE (scm_abort_to_prompt_star, "abort-to-prompt*", 2, 0, 0,
  188. (SCM tag, SCM args),
  189. "Abort to the nearest prompt with tag @var{tag}, yielding the\n"
  190. "values in the list, @var{args}.")
  191. #define FUNC_NAME s_scm_abort_to_prompt_star
  192. {
  193. SCM *argv;
  194. size_t i;
  195. long n;
  196. SCM_VALIDATE_LIST_COPYLEN (SCM_ARG2, args, n);
  197. argv = alloca (sizeof (SCM)*n);
  198. for (i = 0; i < n; i++, args = scm_cdr (args))
  199. argv[i] = scm_car (args);
  200. scm_c_abort (scm_the_vm (), tag, n, argv, NULL);
  201. /* Oh, what, you're still here? The abort must have been reinstated. Actually,
  202. that's quite impossible, given that we're already in C-land here, so...
  203. abort! */
  204. abort ();
  205. }
  206. #undef FUNC_NAME
  207. void
  208. scm_init_control (void)
  209. {
  210. #include "libguile/control.x"
  211. }
  212. /*
  213. Local Variables:
  214. c-file-style: "gnu"
  215. End:
  216. */