scm_rtl_op_* -> scm_op_*
[bpt/guile.git] / libguile / control.c
CommitLineData
38504994 1/* Copyright (C) 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
b9c100d0
AW
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
19#if HAVE_CONFIG_H
20# include <config.h>
21#endif
22
cdd47ec7
LC
23#include <alloca.h>
24
b9c100d0
AW
25#include "libguile/_scm.h"
26#include "libguile/control.h"
d76de871 27#include "libguile/programs.h"
cee1d22c 28#include "libguile/instructions.h"
adaf86ec 29#include "libguile/vm.h"
b9c100d0
AW
30
31\f
32
9ede013f
AW
33#define PROMPT_ESCAPE_P(p) \
34 (SCM_DYNSTACK_TAG_FLAGS (SCM_DYNSTACK_TAG (p)) \
35 & SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY)
3ccee391 36
9ede013f 37\f
adaf86ec 38
9ede013f 39
9ede013f 40/* Only to be called if the SCM_I_SETJMP returns 1 */
b8af64db 41SCM
572eef50 42scm_i_prompt_pop_abort_args_x (SCM vm)
b8af64db
AW
43{
44 size_t i, n;
45 SCM vals = SCM_EOL;
46
572eef50 47 n = scm_to_size_t (SCM_VM_DATA (vm)->sp[0]);
b8af64db 48 for (i = 0; i < n; i++)
572eef50 49 vals = scm_cons (SCM_VM_DATA (vm)->sp[-(i + 1)], vals);
b8af64db
AW
50
51 /* The abort did reset the VM's registers, but then these values
52 were pushed on; so we need to pop them ourselves. */
572eef50 53 SCM_VM_DATA (vm)->sp -= n + 1;
b8af64db
AW
54 /* FIXME NULLSTACK */
55
56 return vals;
57}
58
59
d76de871
AW
60static const scm_t_uint32 compose_continuation_code[] =
61 {
3fe96dd8 62 SCM_PACK_RTL_24 (scm_op_compose_continuation, 0)
d76de871 63 };
cee1d22c 64
5556c175 65
d76de871
AW
66static SCM
67make_partial_continuation (SCM vm_cont)
68{
69 scm_t_bits nfree = 1;
70 scm_t_bits flags = SCM_F_PROGRAM_IS_PARTIAL_CONTINUATION;
71 SCM ret;
cee1d22c 72
e0755cd1 73 ret = scm_words (scm_tc7_program | (nfree << 16) | flags, nfree + 2);
d76de871 74 SCM_SET_CELL_WORD_1 (ret, compose_continuation_code);
d798a895 75 SCM_PROGRAM_FREE_VARIABLE_SET (ret, 0, vm_cont);
cee1d22c 76
d76de871
AW
77 return ret;
78}
cee1d22c
AW
79
80static SCM
9d381ba4 81reify_partial_continuation (SCM vm,
9121d9f1
AW
82 SCM *saved_fp,
83 SCM *saved_sp,
84 scm_t_uint32 *saved_ip,
9d381ba4 85 scm_i_jmp_buf *saved_registers,
9ede013f 86 scm_t_dynstack *dynstack,
9d381ba4 87 scm_i_jmp_buf *current_registers)
cee1d22c 88{
d76de871 89 SCM vm_cont;
cee1d22c 90 scm_t_uint32 flags;
88e5cade 91 SCM *bottom_fp;
cee1d22c 92
cee1d22c 93 flags = SCM_F_VM_CONT_PARTIAL;
9d381ba4
AW
94 /* If we are aborting to a prompt that has the same registers as those
95 of the abort, it means there are no intervening C frames on the
96 stack, and so the continuation can be relocated elsewhere on the
97 stack: it is rewindable. */
98 if (saved_registers && saved_registers == current_registers)
cee1d22c
AW
99 flags |= SCM_F_VM_CONT_REWINDABLE;
100
88e5cade
AW
101 /* Walk the stack down until we find the first frame after saved_fp.
102 We will save the stack down to that frame. It used to be that we
103 could determine the stack bottom in O(1) time, but that's no longer
104 the case, since the thunk application doesn't occur where the
105 prompt is saved. */
106 for (bottom_fp = SCM_VM_DATA (vm)->fp;
107 SCM_FRAME_DYNAMIC_LINK (bottom_fp) > saved_fp;
108 bottom_fp = SCM_FRAME_DYNAMIC_LINK (bottom_fp));
109
110 if (SCM_FRAME_DYNAMIC_LINK (bottom_fp) != saved_fp)
111 abort();
6d804376 112
840ec334 113 /* Capture from the top of the thunk application frame up to the end. */
b636cdb0 114 vm_cont = scm_i_vm_capture_stack (&SCM_FRAME_LOCAL (bottom_fp, 0),
cee1d22c
AW
115 SCM_VM_DATA (vm)->fp,
116 SCM_VM_DATA (vm)->sp,
6d804376 117 SCM_VM_DATA (vm)->ip,
9ede013f 118 dynstack,
cee1d22c
AW
119 flags);
120
d76de871 121 return make_partial_continuation (vm_cont);
cee1d22c
AW
122}
123
d2964315 124void
9d381ba4
AW
125scm_c_abort (SCM vm, SCM tag, size_t n, SCM *argv,
126 scm_i_jmp_buf *current_registers)
eaefabee 127{
9ede013f
AW
128 SCM cont;
129 scm_t_dynstack *dynstack = &SCM_I_CURRENT_THREAD->dynstack;
130 scm_t_bits *prompt;
9ede013f 131 scm_t_dynstack_prompt_flags flags;
9d381ba4 132 SCM *fp, *sp;
9121d9f1 133 scm_t_uint32 *ip;
9d381ba4 134 scm_i_jmp_buf *registers;
eaefabee
AW
135 size_t i;
136
9d381ba4
AW
137 prompt = scm_dynstack_find_prompt (dynstack, tag,
138 &flags, &fp, &sp, &ip, &registers);
9ede013f
AW
139
140 if (!prompt)
9f074518 141 scm_misc_error ("abort", "Abort to unknown prompt", scm_list_1 (tag));
eaefabee 142
9ede013f
AW
143 /* Only reify if the continuation referenced in the handler. */
144 if (flags & SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY)
145 cont = SCM_BOOL_F;
146 else
147 {
148 scm_t_dynstack *captured;
cee1d22c 149
9ede013f 150 captured = scm_dynstack_capture (dynstack, SCM_DYNSTACK_NEXT (prompt));
9d381ba4
AW
151 cont = reify_partial_continuation (vm, fp, sp, ip, registers, captured,
152 current_registers);
9ede013f 153 }
8684029d 154
9ede013f
AW
155 /* Unwind. */
156 scm_dynstack_unwind (dynstack, prompt);
eaefabee 157
8684029d
LC
158 /* Unwinding may have changed the current thread's VM, so use the
159 new one. */
160 vm = scm_the_vm ();
161
eaefabee 162 /* Restore VM regs */
9d381ba4
AW
163 SCM_VM_DATA (vm)->fp = fp;
164 SCM_VM_DATA (vm)->sp = sp;
165 SCM_VM_DATA (vm)->ip = ip;
eaefabee 166
9ede013f 167 /* Since we're jumping down, we should always have enough space. */
eaefabee
AW
168 if (SCM_VM_DATA (vm)->sp + n + 1 >= SCM_VM_DATA (vm)->stack_limit)
169 abort ();
170
171 /* Push vals */
cee1d22c 172 *(++(SCM_VM_DATA (vm)->sp)) = cont;
eaefabee
AW
173 for (i = 0; i < n; i++)
174 *(++(SCM_VM_DATA (vm)->sp)) = argv[i];
c6cd692f
AW
175 if (flags & SCM_F_DYNSTACK_PROMPT_PUSH_NARGS)
176 *(++(SCM_VM_DATA (vm)->sp)) = scm_from_size_t (n+1); /* +1 for continuation */
eaefabee
AW
177
178 /* Jump! */
9d381ba4 179 SCM_I_LONGJMP (*registers, 1);
eaefabee
AW
180
181 /* Shouldn't get here */
182 abort ();
183}
adaf86ec 184
38504994
AW
185SCM_DEFINE (scm_abort_to_prompt_star, "abort-to-prompt*", 2, 0, 0,
186 (SCM tag, SCM args),
187 "Abort to the nearest prompt with tag @var{tag}, yielding the\n"
188 "values in the list, @var{args}.")
189#define FUNC_NAME s_scm_abort_to_prompt_star
b9c100d0 190{
747022e4 191 SCM *argv;
2b14df4b
AW
192 size_t i;
193 long n;
747022e4
AW
194
195 SCM_VALIDATE_LIST_COPYLEN (SCM_ARG2, args, n);
196 argv = alloca (sizeof (SCM)*n);
197 for (i = 0; i < n; i++, args = scm_cdr (args))
198 argv[i] = scm_car (args);
199
9d381ba4 200 scm_c_abort (scm_the_vm (), tag, n, argv, NULL);
747022e4 201
2d026f04
AW
202 /* Oh, what, you're still here? The abort must have been reinstated. Actually,
203 that's quite impossible, given that we're already in C-land here, so...
204 abort! */
205
206 abort ();
b9c100d0 207}
747022e4 208#undef FUNC_NAME
b9c100d0 209
3ccee391
AW
210void
211scm_init_control (void)
b9c100d0 212{
69c96006 213#include "libguile/control.x"
b9c100d0
AW
214}
215
216/*
217 Local Variables:
218 c-file-style: "gnu"
219 End:
220*/