comment formatting in ports.c
[bpt/guile.git] / libguile / control.c
CommitLineData
5556c175 1/* Copyright (C) 2010, 2011, 2012 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
3ccee391 33
adaf86ec 34SCM
d2964315 35scm_c_make_prompt (SCM k, SCM *fp, SCM *sp, scm_t_uint8 *abort_ip,
adbdfd6d
AW
36 scm_t_uint8 escape_only_p, scm_t_int64 vm_cookie,
37 SCM winds)
adaf86ec
AW
38{
39 scm_t_bits tag;
adaf86ec
AW
40 struct scm_prompt_registers *regs;
41
42 tag = scm_tc7_prompt;
adaf86ec 43 if (escape_only_p)
211fcbc8 44 tag |= (SCM_F_PROMPT_ESCAPE<<8);
adaf86ec
AW
45
46 regs = scm_gc_malloc_pointerless (sizeof (*regs), "prompt registers");
d2964315
AW
47 regs->fp = fp;
48 regs->sp = sp;
49 regs->ip = abort_ip;
2d026f04 50 regs->cookie = vm_cookie;
adaf86ec 51
adbdfd6d
AW
52 return scm_double_cell (tag, SCM_UNPACK (k), (scm_t_bits)regs,
53 SCM_UNPACK (winds));
adaf86ec
AW
54}
55
b8af64db
AW
56/* Only to be called if the SCM_PROMPT_SETJMP returns 1 */
57SCM
572eef50 58scm_i_prompt_pop_abort_args_x (SCM vm)
b8af64db
AW
59{
60 size_t i, n;
61 SCM vals = SCM_EOL;
62
572eef50 63 n = scm_to_size_t (SCM_VM_DATA (vm)->sp[0]);
b8af64db 64 for (i = 0; i < n; i++)
572eef50 65 vals = scm_cons (SCM_VM_DATA (vm)->sp[-(i + 1)], vals);
b8af64db
AW
66
67 /* The abort did reset the VM's registers, but then these values
68 were pushed on; so we need to pop them ourselves. */
572eef50 69 SCM_VM_DATA (vm)->sp -= n + 1;
b8af64db
AW
70 /* FIXME NULLSTACK */
71
72 return vals;
73}
74
75
cee1d22c
AW
76#ifdef WORDS_BIGENDIAN
77#define OBJCODE_HEADER(main,meta) 0, 0, 0, main, 0, 0, 0, meta+8
78#define META_HEADER(meta) 0, 0, 0, meta, 0, 0, 0, 0
79#else
80#define OBJCODE_HEADER(main,meta) main, 0, 0, 0, meta+8, 0, 0, 0
81#define META_HEADER(meta) meta, 0, 0, 0, 0, 0, 0, 0
82#endif
83
5556c175 84#define OBJCODE_TAG SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_STATIC, 0)
cee1d22c 85
5556c175 86#if defined (SCM_ALIGNED)
1c05a2a1 87#define SCM_DECLARE_STATIC_ALIGNED_ARRAY(type, sym) \
cee1d22c 88static const type sym[]
1c05a2a1 89#define SCM_STATIC_ALIGNED_ARRAY(alignment, type, sym) \
cee1d22c 90static SCM_ALIGNED (alignment) const type sym[]
cee1d22c
AW
91#define SCM_STATIC_OBJCODE(sym) \
92 SCM_DECLARE_STATIC_ALIGNED_ARRAY (scm_t_uint8, sym##__bytecode); \
93 SCM_STATIC_ALIGNED_ARRAY (8, scm_t_cell, sym##__cells) = { \
5556c175 94 { SCM_PACK (OBJCODE_TAG), SCM_PACK (sym##__bytecode) }, \
cee1d22c
AW
95 { SCM_BOOL_F, SCM_PACK (0) } \
96 }; \
97 static const SCM sym = SCM_PACK (sym##__cells); \
98 SCM_STATIC_ALIGNED_ARRAY (8, scm_t_uint8, sym##__bytecode)
5556c175
AW
99#else
100#define SCM_STATIC_OBJCODE(sym) \
101static SCM sym; \
102static scm_t_uint8 *sym##_bytecode; \
103SCM_SNARF_INIT(sym##_bytecode = scm_gc_malloc_pointerless (sizeof(sym##_bytecode__unaligned), "partial continuation stub"); \
104 memcpy (sym##_bytecode, sym##_bytecode__unaligned, sizeof(sym##_bytecode__unaligned));) \
105SCM_SNARF_INIT(sym = scm_double_cell (OBJCODE_TAG, \
106 (scm_t_bits)sym##_bytecode, \
107 SCM_UNPACK (SCM_BOOL_F), \
108 0);) \
109static const scm_t_uint8 sym##_bytecode__unaligned[]
110#endif
111
cee1d22c 112
cee1d22c
AW
113SCM_STATIC_OBJCODE (cont_objcode) = {
114 /* Like in continuations.c, but with partial-cont-call. */
115 OBJCODE_HEADER (8, 19),
116 /* leave args on the stack */
117 /* 0 */ scm_op_object_ref, 0, /* push scm_vm_cont object */
118 /* 2 */ scm_op_object_ref, 1, /* push internal winds */
9a1c6f1f
AW
119 /* 4 */ scm_op_partial_cont_call, /* and go! */
120 /* 5 */ scm_op_nop, scm_op_nop, scm_op_nop, /* pad to 8 bytes */
cee1d22c
AW
121 /* 8 */
122
123 /* We could put some meta-info to say that this proc is a continuation. Not sure
124 how to do that, though. */
125 META_HEADER (19),
126 /* 0 */ scm_op_make_eol, /* bindings */
127 /* 1 */ scm_op_make_eol, /* sources */
9a1c6f1f 128 /* 2 */ scm_op_make_int8, 0, scm_op_make_int8, 5, /* arity: from ip 0 to ip 7 */
cee1d22c
AW
129 /* 6 */ scm_op_make_int8_0, /* the arity is 0 required args */
130 /* 7 */ scm_op_make_int8_0, /* 0 optionals */
131 /* 8 */ scm_op_make_true, /* and a rest arg */
132 /* 9 */ scm_op_list, 0, 5, /* make a list of those 5 vals */
133 /* 12 */ scm_op_list, 0, 1, /* and the arities will be a list of that one list */
134 /* 15 */ scm_op_list, 0, 3, /* pack bindings, sources, and arities into list */
135 /* 18 */ scm_op_return /* and return */
136 /* 19 */
137};
138
139
140static SCM
141reify_partial_continuation (SCM vm, SCM prompt, SCM extwinds,
142 scm_t_int64 cookie)
143{
144 SCM vm_cont, dynwinds, intwinds = SCM_EOL, ret;
145 scm_t_uint32 flags;
146
147 /* No need to reify if the continuation is never referenced in the handler. */
148 if (SCM_PROMPT_ESCAPE_P (prompt))
149 return SCM_BOOL_F;
150
151 dynwinds = scm_i_dynwinds ();
152 while (!scm_is_eq (dynwinds, extwinds))
153 {
154 intwinds = scm_cons (scm_car (dynwinds), intwinds);
155 dynwinds = scm_cdr (dynwinds);
156 }
157
158 flags = SCM_F_VM_CONT_PARTIAL;
159 if (cookie >= 0 && SCM_PROMPT_REGISTERS (prompt)->cookie == cookie)
160 flags |= SCM_F_VM_CONT_REWINDABLE;
161
6d804376
AW
162 /* Since non-escape continuations should begin with a thunk application, the
163 first bit of the stack should be a frame, with the saved fp equal to the fp
164 that was current when the prompt was made. */
b2b33168 165 if ((SCM*)SCM_UNPACK (SCM_PROMPT_REGISTERS (prompt)->sp[1])
6d804376
AW
166 != SCM_PROMPT_REGISTERS (prompt)->fp)
167 abort ();
168
169 /* Capture from the top of the thunk application frame up to the end. Set an
170 MVRA only, as the post-abort code is in an MV context. */
171 vm_cont = scm_i_vm_capture_stack (SCM_PROMPT_REGISTERS (prompt)->sp + 4,
cee1d22c
AW
172 SCM_VM_DATA (vm)->fp,
173 SCM_VM_DATA (vm)->sp,
6d804376
AW
174 NULL,
175 SCM_VM_DATA (vm)->ip,
cee1d22c
AW
176 flags);
177
178 ret = scm_make_program (cont_objcode,
9a1c6f1f 179 scm_vector (scm_list_2 (vm_cont, intwinds)),
cee1d22c
AW
180 SCM_BOOL_F);
181 SCM_SET_CELL_WORD_0 (ret,
2150e9a8 182 SCM_CELL_WORD_0 (ret) | SCM_F_PROGRAM_IS_PARTIAL_CONTINUATION);
cee1d22c
AW
183 return ret;
184}
185
d2964315 186void
cee1d22c 187scm_c_abort (SCM vm, SCM tag, size_t n, SCM *argv, scm_t_int64 cookie)
eaefabee 188{
cee1d22c 189 SCM cont, winds, prompt = SCM_BOOL_F;
eaefabee
AW
190 long delta;
191 size_t i;
192
193 /* Search the wind list for an appropriate prompt.
194 "Waiter, please bring us the wind list." */
195 for (winds = scm_i_dynwinds (), delta = 0;
196 scm_is_pair (winds);
197 winds = SCM_CDR (winds), delta++)
198 {
199 SCM elt = SCM_CAR (winds);
200 if (SCM_PROMPT_P (elt) && scm_is_eq (SCM_PROMPT_TAG (elt), tag))
201 {
202 prompt = elt;
203 break;
204 }
205 }
206
c6a32a2c 207 /* If we didn't find anything, raise an error. */
eaefabee 208 if (scm_is_false (prompt))
9f074518 209 scm_misc_error ("abort", "Abort to unknown prompt", scm_list_1 (tag));
eaefabee 210
cee1d22c
AW
211 cont = reify_partial_continuation (vm, prompt, winds, cookie);
212
eaefabee
AW
213 /* Unwind once more, beyond the prompt. */
214 winds = SCM_CDR (winds), delta++;
8684029d 215
eaefabee
AW
216 /* Unwind */
217 scm_dowinds (winds, delta);
218
8684029d
LC
219 /* Unwinding may have changed the current thread's VM, so use the
220 new one. */
221 vm = scm_the_vm ();
222
eaefabee
AW
223 /* Restore VM regs */
224 SCM_VM_DATA (vm)->fp = SCM_PROMPT_REGISTERS (prompt)->fp;
225 SCM_VM_DATA (vm)->sp = SCM_PROMPT_REGISTERS (prompt)->sp;
226 SCM_VM_DATA (vm)->ip = SCM_PROMPT_REGISTERS (prompt)->ip;
227
228 /* Since we're jumping down, we should always have enough space */
229 if (SCM_VM_DATA (vm)->sp + n + 1 >= SCM_VM_DATA (vm)->stack_limit)
230 abort ();
231
232 /* Push vals */
cee1d22c 233 *(++(SCM_VM_DATA (vm)->sp)) = cont;
eaefabee
AW
234 for (i = 0; i < n; i++)
235 *(++(SCM_VM_DATA (vm)->sp)) = argv[i];
236 *(++(SCM_VM_DATA (vm)->sp)) = scm_from_size_t (n+1); /* +1 for continuation */
237
238 /* Jump! */
239 SCM_I_LONGJMP (SCM_PROMPT_REGISTERS (prompt)->regs, 1);
240
241 /* Shouldn't get here */
242 abort ();
243}
adaf86ec 244
2d026f04 245SCM_DEFINE (scm_at_abort, "@abort", 2, 0, 0, (SCM tag, SCM args),
747022e4 246 "Abort to the nearest prompt with tag @var{tag}.")
2d026f04 247#define FUNC_NAME s_scm_at_abort
b9c100d0 248{
747022e4 249 SCM *argv;
2b14df4b
AW
250 size_t i;
251 long n;
747022e4
AW
252
253 SCM_VALIDATE_LIST_COPYLEN (SCM_ARG2, args, n);
254 argv = alloca (sizeof (SCM)*n);
255 for (i = 0; i < n; i++, args = scm_cdr (args))
256 argv[i] = scm_car (args);
257
cee1d22c 258 scm_c_abort (scm_the_vm (), tag, n, argv, -1);
747022e4 259
2d026f04
AW
260 /* Oh, what, you're still here? The abort must have been reinstated. Actually,
261 that's quite impossible, given that we're already in C-land here, so...
262 abort! */
263
264 abort ();
b9c100d0 265}
747022e4 266#undef FUNC_NAME
b9c100d0 267
bbb2ecd1
AW
268void
269scm_i_prompt_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
270{
0607ebbf 271 scm_puts_unlocked ("#<prompt ", port);
bbb2ecd1 272 scm_intprint (SCM_UNPACK (exp), 16, port);
0607ebbf 273 scm_putc_unlocked ('>', port);
bbb2ecd1
AW
274}
275
3ccee391
AW
276void
277scm_init_control (void)
b9c100d0 278{
69c96006 279#include "libguile/control.x"
b9c100d0
AW
280}
281
282/*
283 Local Variables:
284 c-file-style: "gnu"
285 End:
286*/