call-with-prompt always compiles to CPS $prompt
[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"
cee1d22c
AW
27#include "libguile/objcodes.h"
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
cee1d22c
AW
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
5556c175 68#define OBJCODE_TAG SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_STATIC, 0)
cee1d22c 69
5556c175 70#if defined (SCM_ALIGNED)
1c05a2a1 71#define SCM_DECLARE_STATIC_ALIGNED_ARRAY(type, sym) \
cee1d22c 72static const type sym[]
1c05a2a1 73#define SCM_STATIC_ALIGNED_ARRAY(alignment, type, sym) \
cee1d22c 74static SCM_ALIGNED (alignment) const type sym[]
cee1d22c
AW
75#define SCM_STATIC_OBJCODE(sym) \
76 SCM_DECLARE_STATIC_ALIGNED_ARRAY (scm_t_uint8, sym##__bytecode); \
77 SCM_STATIC_ALIGNED_ARRAY (8, scm_t_cell, sym##__cells) = { \
5556c175 78 { SCM_PACK (OBJCODE_TAG), SCM_PACK (sym##__bytecode) }, \
cee1d22c
AW
79 { SCM_BOOL_F, SCM_PACK (0) } \
80 }; \
81 static const SCM sym = SCM_PACK (sym##__cells); \
82 SCM_STATIC_ALIGNED_ARRAY (8, scm_t_uint8, sym##__bytecode)
5556c175
AW
83#else
84#define SCM_STATIC_OBJCODE(sym) \
85static SCM sym; \
86static scm_t_uint8 *sym##_bytecode; \
87SCM_SNARF_INIT(sym##_bytecode = scm_gc_malloc_pointerless (sizeof(sym##_bytecode__unaligned), "partial continuation stub"); \
88 memcpy (sym##_bytecode, sym##_bytecode__unaligned, sizeof(sym##_bytecode__unaligned));) \
89SCM_SNARF_INIT(sym = scm_double_cell (OBJCODE_TAG, \
90 (scm_t_bits)sym##_bytecode, \
91 SCM_UNPACK (SCM_BOOL_F), \
92 0);) \
93static const scm_t_uint8 sym##_bytecode__unaligned[]
94#endif
95
cee1d22c 96
cee1d22c
AW
97SCM_STATIC_OBJCODE (cont_objcode) = {
98 /* Like in continuations.c, but with partial-cont-call. */
99 OBJCODE_HEADER (8, 19),
100 /* leave args on the stack */
101 /* 0 */ scm_op_object_ref, 0, /* push scm_vm_cont object */
9ede013f
AW
102 /* 2 */ scm_op_partial_cont_call, /* and go! */
103 /* 3 */ scm_op_nop,
104 /* 4 */ scm_op_nop, scm_op_nop, scm_op_nop, scm_op_nop, /* pad to 8 bytes */
cee1d22c
AW
105 /* 8 */
106
107 /* We could put some meta-info to say that this proc is a continuation. Not sure
108 how to do that, though. */
109 META_HEADER (19),
110 /* 0 */ scm_op_make_eol, /* bindings */
111 /* 1 */ scm_op_make_eol, /* sources */
9ede013f 112 /* 2 */ scm_op_make_int8, 0, scm_op_make_int8, 3, /* arity: from ip 0 to ip 3 */
cee1d22c
AW
113 /* 6 */ scm_op_make_int8_0, /* the arity is 0 required args */
114 /* 7 */ scm_op_make_int8_0, /* 0 optionals */
115 /* 8 */ scm_op_make_true, /* and a rest arg */
116 /* 9 */ scm_op_list, 0, 5, /* make a list of those 5 vals */
117 /* 12 */ scm_op_list, 0, 1, /* and the arities will be a list of that one list */
118 /* 15 */ scm_op_list, 0, 3, /* pack bindings, sources, and arities into list */
119 /* 18 */ scm_op_return /* and return */
120 /* 19 */
121};
122
123
124static SCM
9d381ba4
AW
125reify_partial_continuation (SCM vm,
126 SCM *saved_fp, SCM *saved_sp, scm_t_uint8 *saved_ip,
127 scm_i_jmp_buf *saved_registers,
9ede013f 128 scm_t_dynstack *dynstack,
9d381ba4 129 scm_i_jmp_buf *current_registers)
cee1d22c 130{
9ede013f 131 SCM vm_cont, ret;
cee1d22c
AW
132 scm_t_uint32 flags;
133
cee1d22c 134 flags = SCM_F_VM_CONT_PARTIAL;
9d381ba4
AW
135 /* If we are aborting to a prompt that has the same registers as those
136 of the abort, it means there are no intervening C frames on the
137 stack, and so the continuation can be relocated elsewhere on the
138 stack: it is rewindable. */
139 if (saved_registers && saved_registers == current_registers)
cee1d22c
AW
140 flags |= SCM_F_VM_CONT_REWINDABLE;
141
6d804376
AW
142 /* Since non-escape continuations should begin with a thunk application, the
143 first bit of the stack should be a frame, with the saved fp equal to the fp
144 that was current when the prompt was made. */
9d381ba4 145 if ((SCM*)SCM_UNPACK (saved_sp[1]) != saved_fp)
6d804376
AW
146 abort ();
147
148 /* Capture from the top of the thunk application frame up to the end. Set an
149 MVRA only, as the post-abort code is in an MV context. */
9d381ba4 150 vm_cont = scm_i_vm_capture_stack (saved_sp + 4,
cee1d22c
AW
151 SCM_VM_DATA (vm)->fp,
152 SCM_VM_DATA (vm)->sp,
6d804376
AW
153 NULL,
154 SCM_VM_DATA (vm)->ip,
9ede013f 155 dynstack,
cee1d22c
AW
156 flags);
157
158 ret = scm_make_program (cont_objcode,
9ede013f 159 scm_c_make_vector (1, vm_cont),
cee1d22c
AW
160 SCM_BOOL_F);
161 SCM_SET_CELL_WORD_0 (ret,
2150e9a8 162 SCM_CELL_WORD_0 (ret) | SCM_F_PROGRAM_IS_PARTIAL_CONTINUATION);
cee1d22c
AW
163 return ret;
164}
165
d2964315 166void
9d381ba4
AW
167scm_c_abort (SCM vm, SCM tag, size_t n, SCM *argv,
168 scm_i_jmp_buf *current_registers)
eaefabee 169{
9ede013f
AW
170 SCM cont;
171 scm_t_dynstack *dynstack = &SCM_I_CURRENT_THREAD->dynstack;
172 scm_t_bits *prompt;
9ede013f 173 scm_t_dynstack_prompt_flags flags;
9d381ba4
AW
174 SCM *fp, *sp;
175 scm_t_uint8 *ip;
176 scm_i_jmp_buf *registers;
eaefabee
AW
177 size_t i;
178
9d381ba4
AW
179 prompt = scm_dynstack_find_prompt (dynstack, tag,
180 &flags, &fp, &sp, &ip, &registers);
9ede013f
AW
181
182 if (!prompt)
9f074518 183 scm_misc_error ("abort", "Abort to unknown prompt", scm_list_1 (tag));
eaefabee 184
9ede013f
AW
185 /* Only reify if the continuation referenced in the handler. */
186 if (flags & SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY)
187 cont = SCM_BOOL_F;
188 else
189 {
190 scm_t_dynstack *captured;
cee1d22c 191
9ede013f 192 captured = scm_dynstack_capture (dynstack, SCM_DYNSTACK_NEXT (prompt));
9d381ba4
AW
193 cont = reify_partial_continuation (vm, fp, sp, ip, registers, captured,
194 current_registers);
9ede013f 195 }
8684029d 196
9ede013f
AW
197 /* Unwind. */
198 scm_dynstack_unwind (dynstack, prompt);
eaefabee 199
8684029d
LC
200 /* Unwinding may have changed the current thread's VM, so use the
201 new one. */
202 vm = scm_the_vm ();
203
eaefabee 204 /* Restore VM regs */
9d381ba4
AW
205 SCM_VM_DATA (vm)->fp = fp;
206 SCM_VM_DATA (vm)->sp = sp;
207 SCM_VM_DATA (vm)->ip = ip;
eaefabee 208
9ede013f 209 /* Since we're jumping down, we should always have enough space. */
eaefabee
AW
210 if (SCM_VM_DATA (vm)->sp + n + 1 >= SCM_VM_DATA (vm)->stack_limit)
211 abort ();
212
213 /* Push vals */
cee1d22c 214 *(++(SCM_VM_DATA (vm)->sp)) = cont;
eaefabee
AW
215 for (i = 0; i < n; i++)
216 *(++(SCM_VM_DATA (vm)->sp)) = argv[i];
217 *(++(SCM_VM_DATA (vm)->sp)) = scm_from_size_t (n+1); /* +1 for continuation */
218
219 /* Jump! */
9d381ba4 220 SCM_I_LONGJMP (*registers, 1);
eaefabee
AW
221
222 /* Shouldn't get here */
223 abort ();
224}
adaf86ec 225
38504994
AW
226SCM_DEFINE (scm_abort_to_prompt_star, "abort-to-prompt*", 2, 0, 0,
227 (SCM tag, SCM args),
228 "Abort to the nearest prompt with tag @var{tag}, yielding the\n"
229 "values in the list, @var{args}.")
230#define FUNC_NAME s_scm_abort_to_prompt_star
b9c100d0 231{
747022e4 232 SCM *argv;
2b14df4b
AW
233 size_t i;
234 long n;
747022e4
AW
235
236 SCM_VALIDATE_LIST_COPYLEN (SCM_ARG2, args, n);
237 argv = alloca (sizeof (SCM)*n);
238 for (i = 0; i < n; i++, args = scm_cdr (args))
239 argv[i] = scm_car (args);
240
9d381ba4 241 scm_c_abort (scm_the_vm (), tag, n, argv, NULL);
747022e4 242
2d026f04
AW
243 /* Oh, what, you're still here? The abort must have been reinstated. Actually,
244 that's quite impossible, given that we're already in C-land here, so...
245 abort! */
246
247 abort ();
b9c100d0 248}
747022e4 249#undef FUNC_NAME
b9c100d0 250
3ccee391
AW
251void
252scm_init_control (void)
b9c100d0 253{
69c96006 254#include "libguile/control.x"
b9c100d0
AW
255}
256
257/*
258 Local Variables:
259 c-file-style: "gnu"
260 End:
261*/