control.c 6.1 KB

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