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