control.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/programs.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 (struct scm_vm *vp)
  33. {
  34. size_t i, n;
  35. SCM vals = SCM_EOL;
  36. n = scm_to_size_t (vp->sp[0]);
  37. for (i = 0; i < n; i++)
  38. vals = scm_cons (vp->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. vp->sp -= n + 1;
  42. /* FIXME NULLSTACK */
  43. return vals;
  44. }
  45. static const scm_t_uint32 compose_continuation_code[] =
  46. {
  47. SCM_PACK_OP_24 (compose_continuation, 0)
  48. };
  49. static SCM
  50. make_partial_continuation (SCM vm_cont)
  51. {
  52. scm_t_bits nfree = 1;
  53. scm_t_bits flags = SCM_F_PROGRAM_IS_PARTIAL_CONTINUATION;
  54. SCM ret;
  55. ret = scm_words (scm_tc7_program | (nfree << 16) | flags, nfree + 2);
  56. SCM_SET_CELL_WORD_1 (ret, compose_continuation_code);
  57. SCM_PROGRAM_FREE_VARIABLE_SET (ret, 0, vm_cont);
  58. return ret;
  59. }
  60. static SCM
  61. reify_partial_continuation (struct scm_vm *vp,
  62. SCM *saved_fp,
  63. SCM *saved_sp,
  64. scm_t_uint32 *saved_ip,
  65. scm_i_jmp_buf *saved_registers,
  66. scm_t_dynstack *dynstack,
  67. scm_i_jmp_buf *current_registers)
  68. {
  69. SCM vm_cont;
  70. scm_t_uint32 flags;
  71. SCM *bottom_fp;
  72. flags = SCM_F_VM_CONT_PARTIAL;
  73. /* If we are aborting to a prompt that has the same registers as those
  74. of the abort, it means there are no intervening C frames on the
  75. stack, and so the continuation can be relocated elsewhere on the
  76. stack: it is rewindable. */
  77. if (saved_registers && saved_registers == current_registers)
  78. flags |= SCM_F_VM_CONT_REWINDABLE;
  79. /* Walk the stack down until we find the first frame after saved_fp.
  80. We will save the stack down to that frame. It used to be that we
  81. could determine the stack bottom in O(1) time, but that's no longer
  82. the case, since the thunk application doesn't occur where the
  83. prompt is saved. */
  84. for (bottom_fp = vp->fp;
  85. SCM_FRAME_DYNAMIC_LINK (bottom_fp) > saved_fp;
  86. bottom_fp = SCM_FRAME_DYNAMIC_LINK (bottom_fp));
  87. if (SCM_FRAME_DYNAMIC_LINK (bottom_fp) != saved_fp)
  88. abort();
  89. /* Capture from the top of the thunk application frame up to the end. */
  90. vm_cont = scm_i_vm_capture_stack (&SCM_FRAME_LOCAL (bottom_fp, 0),
  91. vp->fp,
  92. vp->sp,
  93. vp->ip,
  94. dynstack,
  95. flags);
  96. return make_partial_continuation (vm_cont);
  97. }
  98. void
  99. scm_c_abort (struct scm_vm *vp, SCM tag, size_t n, SCM *argv,
  100. scm_i_jmp_buf *current_registers)
  101. {
  102. SCM cont;
  103. scm_t_dynstack *dynstack = &SCM_I_CURRENT_THREAD->dynstack;
  104. scm_t_bits *prompt;
  105. scm_t_dynstack_prompt_flags flags;
  106. scm_t_ptrdiff fp_offset, sp_offset;
  107. SCM *fp, *sp;
  108. scm_t_uint32 *ip;
  109. scm_i_jmp_buf *registers;
  110. size_t i;
  111. prompt = scm_dynstack_find_prompt (dynstack, tag,
  112. &flags, &fp_offset, &sp_offset, &ip,
  113. &registers);
  114. if (!prompt)
  115. scm_misc_error ("abort", "Abort to unknown prompt", scm_list_1 (tag));
  116. fp = vp->stack_base + fp_offset;
  117. sp = vp->stack_base + sp_offset;
  118. /* Only reify if the continuation referenced in the handler. */
  119. if (flags & SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY)
  120. cont = SCM_BOOL_F;
  121. else
  122. {
  123. scm_t_dynstack *captured;
  124. captured = scm_dynstack_capture (dynstack, SCM_DYNSTACK_NEXT (prompt));
  125. cont = reify_partial_continuation (vp, fp, sp, ip, registers, captured,
  126. current_registers);
  127. }
  128. /* Unwind. */
  129. scm_dynstack_unwind (dynstack, prompt);
  130. /* Restore VM regs */
  131. vp->fp = fp;
  132. vp->sp = sp;
  133. vp->ip = ip;
  134. /* Since we're jumping down, we should always have enough space. */
  135. if (vp->sp + n + 1 >= vp->stack_limit)
  136. abort ();
  137. /* Push vals */
  138. *(++(vp->sp)) = cont;
  139. for (i = 0; i < n; i++)
  140. *(++(vp->sp)) = argv[i];
  141. if (flags & SCM_F_DYNSTACK_PROMPT_PUSH_NARGS)
  142. *(++(vp->sp)) = scm_from_size_t (n+1); /* +1 for continuation */
  143. /* Jump! */
  144. SCM_I_LONGJMP (*registers, 1);
  145. /* Shouldn't get here */
  146. abort ();
  147. }
  148. SCM_DEFINE (scm_abort_to_prompt_star, "abort-to-prompt*", 2, 0, 0,
  149. (SCM tag, SCM args),
  150. "Abort to the nearest prompt with tag @var{tag}, yielding the\n"
  151. "values in the list, @var{args}.")
  152. #define FUNC_NAME s_scm_abort_to_prompt_star
  153. {
  154. SCM *argv;
  155. size_t i;
  156. long n;
  157. SCM_VALIDATE_LIST_COPYLEN (SCM_ARG2, args, n);
  158. argv = alloca (sizeof (SCM)*n);
  159. for (i = 0; i < n; i++, args = scm_cdr (args))
  160. argv[i] = scm_car (args);
  161. scm_c_abort (scm_the_vm (), tag, n, argv, NULL);
  162. /* Oh, what, you're still here? The abort must have been reinstated. Actually,
  163. that's quite impossible, given that we're already in C-land here, so...
  164. abort! */
  165. abort ();
  166. }
  167. #undef FUNC_NAME
  168. void
  169. scm_init_control (void)
  170. {
  171. #include "libguile/control.x"
  172. }
  173. /*
  174. Local Variables:
  175. c-file-style: "gnu"
  176. End:
  177. */