(scm_bit_extract): Use SCM_SRS for signed right shift.
[bpt/guile.git] / libguile / dynwind.c
CommitLineData
4845bbae 1/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2003, 2004 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e
MV
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
0f2d19dd 7 *
73be1d9e
MV
8 * This library is distributed in the hope that it will be useful,
9 * but 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.
0f2d19dd 12 *
73be1d9e
MV
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
1bbd0b84 17
1bbd0b84 18
0f2d19dd
JB
19\f
20
4845bbae
MV
21#include <assert.h>
22
a0599745
MD
23#include "libguile/_scm.h"
24#include "libguile/eval.h"
25#include "libguile/alist.h"
26#include "libguile/fluids.h"
27#include "libguile/ports.h"
28#include "libguile/smob.h"
0f2d19dd 29
a0599745 30#include "libguile/dynwind.h"
0f2d19dd
JB
31\f
32
33/* {Dynamic wind}
b3460a50
MV
34
35 Things that can be on the wind list:
36
4845bbae
MV
37 #<frame>
38 #<winder>
b3460a50
MV
39 (enter-proc . leave-proc) dynamic-wind
40 (tag . jmpbuf) catch
41 (tag . lazy-catch) lazy-catch
42 tag is either a symbol or a boolean
43
b3460a50 44*/
0f2d19dd
JB
45
46
47
3b3b36dd 48SCM_DEFINE (scm_dynamic_wind, "dynamic-wind", 3, 0, 0,
1e6808ea
MG
49 (SCM in_guard, SCM thunk, SCM out_guard),
50 "All three arguments must be 0-argument procedures.\n"
51 "@var{in_guard} is called, then @var{thunk}, then\n"
52 "@var{out_guard}.\n"
53 "\n"
54 "If, any time during the execution of @var{thunk}, the\n"
55 "continuation of the @code{dynamic_wind} expression is escaped\n"
56 "non-locally, @var{out_guard} is called. If the continuation of\n"
57 "the dynamic-wind is re-entered, @var{in_guard} is called. Thus\n"
58 "@var{in_guard} and @var{out_guard} may be called any number of\n"
59 "times.\n"
60 "@lisp\n"
b380b885 61 "(define x 'normal-binding)\n"
1e6808ea 62 "@result{} x\n"
9401323e 63 "(define a-cont (call-with-current-continuation\n"
b380b885
MD
64 " (lambda (escape)\n"
65 " (let ((old-x x))\n"
66 " (dynamic-wind\n"
67 " ;; in-guard:\n"
68 " ;;\n"
1e6808ea
MG
69 " (lambda () (set! x 'special-binding))\n"
70 "\n"
b380b885
MD
71 " ;; thunk\n"
72 " ;;\n"
73 " (lambda () (display x) (newline)\n"
74 " (call-with-current-continuation escape)\n"
75 " (display x) (newline)\n"
1e6808ea
MG
76 " x)\n"
77 "\n"
b380b885
MD
78 " ;; out-guard:\n"
79 " ;;\n"
1e6808ea
MG
80 " (lambda () (set! x old-x)))))))\n"
81 "\n"
9401323e 82 ";; Prints:\n"
b380b885
MD
83 "special-binding\n"
84 ";; Evaluates to:\n"
1e6808ea 85 "@result{} a-cont\n"
b380b885 86 "x\n"
1e6808ea 87 "@result{} normal-binding\n"
b380b885
MD
88 "(a-cont #f)\n"
89 ";; Prints:\n"
90 "special-binding\n"
91 ";; Evaluates to:\n"
1e6808ea 92 "@result{} a-cont ;; the value of the (define a-cont...)\n"
b380b885 93 "x\n"
1e6808ea 94 "@result{} normal-binding\n"
b380b885
MD
95 "a-cont\n"
96 "@result{} special-binding\n"
1e6808ea 97 "@end lisp")
1bbd0b84 98#define FUNC_NAME s_scm_dynamic_wind
0f2d19dd
JB
99{
100 SCM ans;
1e6808ea
MG
101 SCM_ASSERT (SCM_NFALSEP (scm_thunk_p (out_guard)),
102 out_guard,
1bbd0b84 103 SCM_ARG3, FUNC_NAME);
fdc28395 104 scm_call_0 (in_guard);
1e6808ea 105 scm_dynwinds = scm_acons (in_guard, out_guard, scm_dynwinds);
fdc28395 106 ans = scm_call_0 (thunk);
0f2d19dd 107 scm_dynwinds = SCM_CDR (scm_dynwinds);
fdc28395 108 scm_call_0 (out_guard);
0f2d19dd
JB
109 return ans;
110}
1bbd0b84 111#undef FUNC_NAME
0f2d19dd 112
3346a90f 113SCM
92c2555f
MV
114scm_internal_dynamic_wind (scm_t_guard before,
115 scm_t_inner inner,
116 scm_t_guard after,
3346a90f
MD
117 void *inner_data,
118 void *guard_data)
119{
4845bbae
MV
120 SCM ans;
121
0888de4f 122 scm_frame_begin (SCM_F_FRAME_REWINDABLE);
16c5cac2
MV
123 scm_frame_rewind_handler (before, guard_data, SCM_F_WIND_EXPLICITLY);
124 scm_frame_unwind_handler (after, guard_data, SCM_F_WIND_EXPLICITLY);
3346a90f 125 ans = inner (inner_data);
0888de4f 126 scm_frame_end ();
3346a90f
MD
127 return ans;
128}
1cc91f1b 129
4845bbae
MV
130/* Frames and winders. */
131
132static scm_t_bits tc16_frame;
133#define FRAME_P(f) SCM_SMOB_PREDICATE (tc16_frame, (f))
134
135#define FRAME_F_REWINDABLE (1 << 16)
136#define FRAME_REWINDABLE_P(f) (SCM_CELL_WORD_0(f) & FRAME_F_REWINDABLE)
137
138static scm_t_bits tc16_winder;
14578fa4
MV
139#define WINDER_P(w) SCM_SMOB_PREDICATE (tc16_winder, (w))
140#define WINDER_PROC(w) ((void (*)(void *))SCM_CELL_WORD_1 (w))
141#define WINDER_DATA(w) ((void *)SCM_CELL_WORD_2 (w))
4845bbae
MV
142
143#define WINDER_F_EXPLICIT (1 << 16)
144#define WINDER_F_REWIND (1 << 17)
a520e4f0 145#define WINDER_F_MARK (1 << 18)
4845bbae
MV
146#define WINDER_EXPLICIT_P(w) (SCM_CELL_WORD_0(w) & WINDER_F_EXPLICIT)
147#define WINDER_REWIND_P(w) (SCM_CELL_WORD_0(w) & WINDER_F_REWIND)
a520e4f0 148#define WINDER_MARK_P(w) (SCM_CELL_WORD_0(w) & WINDER_F_MARK)
4845bbae
MV
149
150void
0888de4f 151scm_frame_begin (scm_t_frame_flags flags)
4845bbae
MV
152{
153 SCM f;
154 scm_t_bits fl = ((flags&SCM_F_FRAME_REWINDABLE)? FRAME_F_REWINDABLE : 0);
155 SCM_NEWSMOB (f, tc16_frame | fl, 0);
156 scm_dynwinds = scm_cons (f, scm_dynwinds);
157}
158
159void
0888de4f 160scm_frame_end (void)
4845bbae 161{
0888de4f
MV
162 /* Unwind upto and including the next frame entry. We can only
163 encounter #<winder> entries on the way.
4845bbae
MV
164 */
165
0888de4f 166 while (SCM_CONSP (scm_dynwinds))
4845bbae 167 {
0888de4f
MV
168 SCM entry = SCM_CAR (scm_dynwinds);
169 scm_dynwinds = SCM_CDR (scm_dynwinds);
170
171 if (FRAME_P (entry))
172 return;
173
174 assert (WINDER_P (entry));
175 if (!WINDER_REWIND_P (entry) && WINDER_EXPLICIT_P (entry))
176 WINDER_PROC(entry) (WINDER_DATA (entry));
4845bbae
MV
177 }
178
179 assert (0);
180}
181
a520e4f0
MV
182static SCM
183winder_mark (SCM w)
184{
185 if (WINDER_MARK_P (w))
186 return WINDER_DATA (w);
187 return SCM_BOOL_F;
188}
189
4845bbae 190void
16c5cac2
MV
191scm_frame_unwind_handler (void (*proc) (void *), void *data,
192 scm_t_wind_flags flags)
4845bbae
MV
193{
194 SCM w;
a520e4f0 195 scm_t_bits fl = ((flags&SCM_F_WIND_EXPLICITLY)? WINDER_F_EXPLICIT : 0);
4845bbae
MV
196 SCM_NEWSMOB2 (w, tc16_winder | fl,
197 (scm_t_bits) proc, (scm_t_bits) data);
198 scm_dynwinds = scm_cons (w, scm_dynwinds);
199}
200
201void
16c5cac2
MV
202scm_frame_rewind_handler (void (*proc) (void *), void *data,
203 scm_t_wind_flags flags)
4845bbae
MV
204{
205 SCM w;
206 SCM_NEWSMOB2 (w, tc16_winder | WINDER_F_REWIND,
207 (scm_t_bits) proc, (scm_t_bits) data);
208 scm_dynwinds = scm_cons (w, scm_dynwinds);
a520e4f0
MV
209 if (flags & SCM_F_WIND_EXPLICITLY)
210 proc (data);
211}
212
213void
16c5cac2
MV
214scm_frame_unwind_handler_with_scm (void (*proc) (SCM), SCM data,
215 scm_t_wind_flags flags)
a520e4f0
MV
216{
217 SCM w;
218 scm_t_bits fl = ((flags&SCM_F_WIND_EXPLICITLY)? WINDER_F_EXPLICIT : 0);
219 SCM_NEWSMOB2 (w, tc16_winder | fl | WINDER_F_MARK,
220 (scm_t_bits) proc, SCM_UNPACK (data));
221 scm_dynwinds = scm_cons (w, scm_dynwinds);
222}
223
224void
16c5cac2
MV
225scm_frame_rewind_handler_with_scm (void (*proc) (SCM), SCM data,
226 scm_t_wind_flags flags)
a520e4f0
MV
227{
228 SCM w;
229 SCM_NEWSMOB2 (w, tc16_winder | WINDER_F_REWIND | WINDER_F_MARK,
230 (scm_t_bits) proc, SCM_UNPACK (data));
231 scm_dynwinds = scm_cons (w, scm_dynwinds);
232 if (flags & SCM_F_WIND_EXPLICITLY)
4845bbae
MV
233 proc (data);
234}
235
c2654ef0 236#ifdef GUILE_DEBUG
a1ec6916 237SCM_DEFINE (scm_wind_chain, "wind-chain", 0, 0, 0,
1bbd0b84 238 (),
156149ad
MG
239 "Return the current wind chain. The wind chain contains all\n"
240 "information required by @code{dynamic-wind} to call its\n"
241 "argument thunks when entering/exiting its scope.")
1bbd0b84 242#define FUNC_NAME s_scm_wind_chain
c2654ef0
MD
243{
244 return scm_dynwinds;
245}
1bbd0b84 246#undef FUNC_NAME
c2654ef0
MD
247#endif
248
2e171178 249void
904a077d 250scm_swap_bindings (SCM vars, SCM vals)
6778caf9
MD
251{
252 SCM tmp;
253 while (SCM_NIMP (vals))
254 {
904a077d
MV
255 tmp = SCM_VARIABLE_REF (SCM_CAR (vars));
256 SCM_VARIABLE_SET (SCM_CAR (vars), SCM_CAR (vals));
6778caf9 257 SCM_SETCAR (vals, tmp);
904a077d 258 vars = SCM_CDR (vars);
6778caf9
MD
259 vals = SCM_CDR (vals);
260 }
261}
c2654ef0 262
4845bbae 263void
c014a02e 264scm_dowinds (SCM to, long delta)
4845bbae 265{
14578fa4 266 scm_i_dowinds (to, delta, NULL, NULL);
4845bbae
MV
267}
268
269void
14578fa4 270scm_i_dowinds (SCM to, long delta, void (*turn_func) (void *), void *data)
0f2d19dd
JB
271{
272 tail:
4845bbae
MV
273 if (SCM_EQ_P (to, scm_dynwinds))
274 {
275 if (turn_func)
276 turn_func (data);
277 }
1be6b49c 278 else if (delta < 0)
0f2d19dd
JB
279 {
280 SCM wind_elt;
281 SCM wind_key;
282
14578fa4 283 scm_i_dowinds (SCM_CDR (to), 1 + delta, turn_func, data);
0f2d19dd 284 wind_elt = SCM_CAR (to);
4845bbae 285
0f2d19dd
JB
286#if 0
287 if (SCM_INUMP (wind_elt))
288 {
289 scm_cross_dynwind_binding_scope (wind_elt, 0);
290 }
291 else
292#endif
293 {
4845bbae 294 if (FRAME_P (wind_elt))
b3460a50 295 {
4845bbae
MV
296 if (!FRAME_REWINDABLE_P (wind_elt))
297 scm_misc_error ("dowinds",
298 "cannot invoke continuation from this context",
299 SCM_EOL);
300 }
301 else if (WINDER_P (wind_elt))
302 {
303 if (WINDER_REWIND_P (wind_elt))
14578fa4 304 WINDER_PROC (wind_elt) (WINDER_DATA (wind_elt));
4845bbae
MV
305 }
306 else
307 {
308 wind_key = SCM_CAR (wind_elt);
540cc7ab 309 /* key = #t | symbol | thunk | list of variables */
4845bbae
MV
310 if (SCM_NIMP (wind_key))
311 {
312 if (SCM_CONSP (wind_key))
313 {
314 if (SCM_VARIABLEP (SCM_CAR (wind_key)))
315 scm_swap_bindings (wind_key, SCM_CDR (wind_elt));
4845bbae
MV
316 }
317 else if (SCM_TYP3 (wind_key) == scm_tc3_closure)
318 scm_call_0 (wind_key);
904a077d 319 }
b3460a50 320 }
0f2d19dd
JB
321 }
322 scm_dynwinds = to;
323 }
324 else
325 {
0f2d19dd
JB
326 SCM wind_elt;
327 SCM wind_key;
328
0f2d19dd
JB
329 wind_elt = SCM_CAR (scm_dynwinds);
330 scm_dynwinds = SCM_CDR (scm_dynwinds);
4845bbae 331
0f2d19dd
JB
332#if 0
333 if (SCM_INUMP (wind_elt))
334 {
335 scm_cross_dynwind_binding_scope (wind_elt, 0);
336 }
337 else
338#endif
339 {
4845bbae
MV
340 if (FRAME_P (wind_elt))
341 {
342 /* Nothing to do. */
343 }
344 else if (WINDER_P (wind_elt))
b3460a50 345 {
14578fa4
MV
346 if (!WINDER_REWIND_P (wind_elt))
347 WINDER_PROC (wind_elt) (WINDER_DATA (wind_elt));
4845bbae
MV
348 }
349 else
350 {
351 wind_key = SCM_CAR (wind_elt);
352 if (SCM_NIMP (wind_key))
353 {
354 if (SCM_CONSP (wind_key))
355 {
356 if (SCM_VARIABLEP (SCM_CAR (wind_key)))
357 scm_swap_bindings (wind_key, SCM_CDR (wind_elt));
4845bbae
MV
358 }
359 else if (SCM_TYP3 (wind_key) == scm_tc3_closure)
360 scm_call_0 (SCM_CDR (wind_elt));
904a077d 361 }
b3460a50 362 }
0f2d19dd
JB
363 }
364 delta--;
365 goto tail; /* scm_dowinds(to, delta-1); */
366 }
367}
368
0f2d19dd
JB
369void
370scm_init_dynwind ()
0f2d19dd 371{
4845bbae 372 tc16_frame = scm_make_smob_type ("frame", 0);
4845bbae
MV
373
374 tc16_winder = scm_make_smob_type ("winder", 0);
a520e4f0 375 scm_set_smob_mark (tc16_winder, winder_mark);
4845bbae 376
a0599745 377#include "libguile/dynwind.x"
0f2d19dd 378}
89e00824
ML
379
380/*
381 Local Variables:
382 c-file-style: "gnu"
383 End:
384*/