rewind the dynamic state when entering a partial continuation
[bpt/guile.git] / libguile / stacks.c
CommitLineData
aa3f6951 1/* A stack holds a frame chain
1d1cae0e 2 * Copyright (C) 1996,1997,2000,2001, 2006, 2007, 2008, 2009, 2010 Free Software Foundation
782d171c 3 *
73be1d9e 4 * This library is free software; you can redistribute it and/or
53befeb7
NJ
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
782d171c 8 *
53befeb7
NJ
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
782d171c 13 *
73be1d9e
MV
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
53befeb7
NJ
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
73be1d9e 18 */
1bbd0b84 19
1bbd0b84 20
782d171c 21\f
dbb605f5
LC
22#ifdef HAVE_CONFIG_H
23# include <config.h>
24#endif
782d171c 25
a0599745
MD
26#include "libguile/_scm.h"
27#include "libguile/eval.h"
28#include "libguile/debug.h"
29#include "libguile/continuations.h"
30#include "libguile/struct.h"
31#include "libguile/macros.h"
32#include "libguile/procprop.h"
33#include "libguile/modules.h"
34#include "libguile/root.h"
35#include "libguile/strings.h"
b1b942b7
AW
36#include "libguile/vm.h" /* to capture vm stacks */
37#include "libguile/frames.h" /* vm frames */
a0599745
MD
38
39#include "libguile/validate.h"
40#include "libguile/stacks.h"
22fc179a
HWN
41#include "libguile/private-options.h"
42
782d171c
MD
43
44\f
aa3f6951 45/* {Stacks}
782d171c 46 *
aa3f6951
AW
47 * The stack is represented as a struct that holds a frame. The frame itself is
48 * linked to the next frame, or #f.
782d171c
MD
49 *
50 * Stacks
51 * Constructor
52 * make-stack
7115d1e4
MD
53 * Selectors
54 * stack-id
782d171c
MD
55 * stack-ref
56 * Inspector
57 * stack-length
aa3f6951 58 */
782d171c
MD
59
60\f
61
aa3f6951 62static SCM stack_id_with_fp (SCM frame, SCM **fp);
782d171c 63
aa3f6951 64/* Count number of debug info frames on a stack, beginning with FRAME.
782d171c 65 */
b1b942b7 66static long
aa3f6951 67stack_depth (SCM frame, SCM *fp)
782d171c 68{
fdcb2b82 69 long n = 0;
aa3f6951
AW
70 /* count frames, skipping boot frames */
71 for (; scm_is_true (frame) && SCM_VM_FRAME_FP (frame) > fp;
93dbc31b
AW
72 frame = scm_frame_previous (frame))
73 ++n;
782d171c
MD
74 return n;
75}
76
c3a6c6f9
MD
77/* Narrow STACK by cutting away stackframes (mutatingly).
78 *
79 * Inner frames (most recent) are cut by advancing the frames pointer.
80 * Outer frames are cut by decreasing the recorded length.
81 *
82 * Cut maximally INNER inner frames and OUTER outer frames using
83 * the keys INNER_KEY and OUTER_KEY.
84 *
85 * Frames are cut away starting at the end points and moving towards
86 * the center of the stack. The key is normally compared to the
87 * operator in application frames. Frames up to and including the key
88 * are cut.
89 *
90 * If INNER_KEY is #t a different scheme is used for inner frames:
91 *
92 * Frames up to but excluding the first source frame originating from
93 * a user module are cut, except for possible application frames
94 * between the user frame and the last system frame previously
95 * encountered.
96 */
97
7115d1e4 98static void
34d19ef6 99narrow_stack (SCM stack, long inner, SCM inner_key, long outer, SCM outer_key)
7115d1e4 100{
aa3f6951
AW
101 unsigned long int len;
102 SCM frame;
7115d1e4 103
aa3f6951
AW
104 len = SCM_STACK_LENGTH (stack);
105 frame = SCM_STACK_FRAME (stack);
106
7115d1e4 107 /* Cut inner part. */
bc36d050 108 if (scm_is_eq (inner_key, SCM_BOOL_T))
c3a6c6f9 109 {
aa3f6951
AW
110 /* Cut specified number of frames. */
111 for (; inner && len; --inner)
112 {
113 len--;
93dbc31b 114 frame = scm_frame_previous (frame);
aa3f6951 115 }
c3a6c6f9
MD
116 }
117 else
c3a6c6f9 118 {
aa3f6951
AW
119 /* Cut until the given procedure is seen. */
120 for (; inner && len ; --inner)
121 {
122 SCM proc = scm_frame_procedure (frame);
123 len--;
93dbc31b 124 frame = scm_frame_previous (frame);
aa3f6951
AW
125 if (scm_is_eq (proc, inner_key))
126 break;
127 }
c3a6c6f9 128 }
aa3f6951
AW
129
130 SCM_SET_STACK_LENGTH (stack, len);
131 SCM_SET_STACK_FRAME (stack, frame);
7115d1e4
MD
132
133 /* Cut outer part. */
aa3f6951
AW
134 for (; outer && len ; --outer)
135 {
136 frame = scm_stack_ref (stack, scm_from_long (len - 1));
137 len--;
138 if (scm_is_eq (scm_frame_procedure (frame), outer_key))
139 break;
140 }
7115d1e4 141
aa3f6951 142 SCM_SET_STACK_LENGTH (stack, len);
7115d1e4
MD
143}
144
782d171c
MD
145\f
146
147/* Stacks
148 */
149
762e289a 150SCM scm_stack_type;
66f45472 151
a1ec6916 152SCM_DEFINE (scm_stack_p, "stack?", 1, 0, 0,
1bbd0b84 153 (SCM obj),
b380b885 154 "Return @code{#t} if @var{obj} is a calling stack.")
1bbd0b84 155#define FUNC_NAME s_scm_stack_p
66f45472 156{
7888309b 157 return scm_from_bool(SCM_STACKP (obj));
66f45472 158}
1bbd0b84 159#undef FUNC_NAME
66f45472 160
af45e3b0
DH
161SCM_DEFINE (scm_make_stack, "make-stack", 1, 0, 1,
162 (SCM obj, SCM args),
67941e3c
MG
163 "Create a new stack. If @var{obj} is @code{#t}, the current\n"
164 "evaluation stack is used for creating the stack frames,\n"
165 "otherwise the frames are taken from @var{obj} (which must be\n"
baffb19f
NJ
166 "either a debug object or a continuation).\n\n"
167 "@var{args} should be a list containing any combination of\n"
168 "integer, procedure and @code{#t} values.\n\n"
169 "These values specify various ways of cutting away uninteresting\n"
170 "stack frames from the top and bottom of the stack that\n"
171 "@code{make-stack} returns. They come in pairs like this:\n"
172 "@code{(@var{inner_cut_1} @var{outer_cut_1} @var{inner_cut_2}\n"
173 "@var{outer_cut_2} @dots{})}.\n\n"
174 "Each @var{inner_cut_N} can be @code{#t}, an integer, or a\n"
175 "procedure. @code{#t} means to cut away all frames up to but\n"
176 "excluding the first user module frame. An integer means to cut\n"
177 "away exactly that number of frames. A procedure means to cut\n"
178 "away all frames up to but excluding the application frame whose\n"
179 "procedure matches the specified one.\n\n"
180 "Each @var{outer_cut_N} can be an integer or a procedure. An\n"
181 "integer means to cut away that number of frames. A procedure\n"
182 "means to cut away frames down to but excluding the application\n"
183 "frame whose procedure matches the specified one.\n\n"
184 "If the @var{outer_cut_N} of the last pair is missing, it is\n"
185 "taken as 0.")
1bbd0b84 186#define FUNC_NAME s_scm_make_stack
782d171c 187{
aa3f6951 188 long n;
1be6b49c 189 int maxp;
aa3f6951 190 SCM frame;
14aa25e4
AW
191 SCM stack;
192 SCM id, *id_fp;
af45e3b0 193 SCM inner_cut, outer_cut;
f6f88e0d
MD
194
195 /* Extract a pointer to the innermost frame of whatever object
196 scm_make_stack was given. */
bc36d050 197 if (scm_is_eq (obj, SCM_BOOL_T))
782d171c 198 {
aa3f6951
AW
199 SCM cont;
200 struct scm_vm_cont *c;
201
269479e3 202 cont = scm_i_vm_capture_continuation (scm_the_vm ());
aa3f6951
AW
203 c = SCM_VM_CONT_DATA (cont);
204
205 frame = scm_c_make_frame (cont, c->fp + c->reloc,
d8873dfe 206 c->sp + c->reloc, c->ra,
aa3f6951 207 c->reloc);
13dcb666 208 }
b1b942b7 209 else if (SCM_VM_FRAME_P (obj))
aa3f6951 210 frame = obj;
13dcb666 211 else if (SCM_CONTINUATIONP (obj))
1d1cae0e 212 frame = scm_i_continuation_to_frame (obj);
13dcb666
DH
213 else
214 {
215 SCM_WRONG_TYPE_ARG (SCM_ARG1, obj);
216 /* not reached */
782d171c
MD
217 }
218
93dbc31b
AW
219 /* FIXME: is this even possible? */
220 if (scm_is_true (frame)
221 && SCM_PROGRAM_IS_BOOT (scm_frame_procedure (frame)))
222 frame = scm_frame_previous (frame);
223
aa3f6951 224 if (scm_is_false (frame))
14aa25e4
AW
225 return SCM_BOOL_F;
226
227 /* Get ID of the stack corresponding to the given frame. */
aa3f6951 228 id = stack_id_with_fp (frame, &id_fp);
14aa25e4 229
f6f88e0d
MD
230 /* Count number of frames. Also get stack id tag and check whether
231 there are more stackframes than we want to record
232 (SCM_BACKTRACE_MAXDEPTH). */
66f45472
MD
233 id = SCM_BOOL_F;
234 maxp = 0;
aa3f6951 235 n = stack_depth (frame, id_fp);
782d171c 236
f6f88e0d 237 /* Make the stack object. */
aa3f6951
AW
238 stack = scm_make_struct (scm_stack_type, SCM_INUM0, SCM_EOL);
239 SCM_SET_STACK_LENGTH (stack, n);
240 SCM_SET_STACK_ID (stack, id);
241 SCM_SET_STACK_FRAME (stack, frame);
242
f6f88e0d 243 /* Narrow the stack according to the arguments given to scm_make_stack. */
af45e3b0 244 SCM_VALIDATE_REST_ARGUMENT (args);
d2e53ed6 245 while (n > 0 && !scm_is_null (args))
f6f88e0d
MD
246 {
247 inner_cut = SCM_CAR (args);
248 args = SCM_CDR (args);
d2e53ed6 249 if (scm_is_null (args))
af45e3b0 250 {
13dcb666 251 outer_cut = SCM_INUM0;
af45e3b0
DH
252 }
253 else
f6f88e0d
MD
254 {
255 outer_cut = SCM_CAR (args);
256 args = SCM_CDR (args);
257 }
f6f88e0d
MD
258
259 narrow_stack (stack,
e11e83f3
MV
260 scm_is_integer (inner_cut) ? scm_to_int (inner_cut) : n,
261 scm_is_integer (inner_cut) ? 0 : inner_cut,
262 scm_is_integer (outer_cut) ? scm_to_int (outer_cut) : n,
263 scm_is_integer (outer_cut) ? 0 : outer_cut);
f6f88e0d 264
aa3f6951 265 n = SCM_STACK_LENGTH (stack);
f6f88e0d
MD
266 }
267
7115d1e4 268 if (n > 0)
b1b942b7 269 return stack;
7115d1e4
MD
270 else
271 return SCM_BOOL_F;
782d171c 272}
1bbd0b84 273#undef FUNC_NAME
782d171c 274
a1ec6916 275SCM_DEFINE (scm_stack_id, "stack-id", 1, 0, 0,
1bbd0b84 276 (SCM stack),
b380b885 277 "Return the identifier given to @var{stack} by @code{start-stack}.")
1bbd0b84 278#define FUNC_NAME s_scm_stack_id
66f45472 279{
aa3f6951 280 SCM frame, *id_fp;
14aa25e4 281
bc36d050 282 if (scm_is_eq (stack, SCM_BOOL_T))
7115d1e4 283 {
14aa25e4 284 struct scm_vm *vp = SCM_VM_DATA (scm_the_vm ());
aa3f6951 285 frame = scm_c_make_frame (scm_the_vm (), vp->fp, vp->sp, vp->ip, 0);
13dcb666 286 }
14aa25e4 287 else if (SCM_VM_FRAME_P (stack))
aa3f6951 288 frame = stack;
13dcb666 289 else if (SCM_CONTINUATIONP (stack))
1d1cae0e 290 frame = scm_i_continuation_to_frame (stack);
13dcb666
DH
291 else
292 {
14aa25e4
AW
293 SCM_WRONG_TYPE_ARG (SCM_ARG1, stack);
294 /* not reached */
13dcb666
DH
295 }
296
aa3f6951 297 return stack_id_with_fp (frame, &id_fp);
66f45472 298}
1bbd0b84 299#undef FUNC_NAME
66f45472 300
14aa25e4 301static SCM
aa3f6951 302stack_id_with_fp (SCM frame, SCM **fp)
14aa25e4 303{
aa3f6951 304 SCM holder = SCM_VM_FRAME_STACK_HOLDER (frame);
14aa25e4
AW
305
306 if (SCM_VM_CONT_P (holder))
307 {
308 *fp = NULL;
309 return SCM_BOOL_F;
310 }
311 else
312 {
313 *fp = NULL;
314 return SCM_BOOL_F;
315 }
316}
317
a1ec6916 318SCM_DEFINE (scm_stack_ref, "stack-ref", 2, 0, 0,
13dcb666
DH
319 (SCM stack, SCM index),
320 "Return the @var{index}'th frame from @var{stack}.")
1bbd0b84 321#define FUNC_NAME s_scm_stack_ref
782d171c 322{
13dcb666 323 unsigned long int c_index;
aa3f6951 324 SCM frame;
13dcb666
DH
325
326 SCM_VALIDATE_STACK (1, stack);
a55c2b68 327 c_index = scm_to_unsigned_integer (index, 0, SCM_STACK_LENGTH(stack)-1);
aa3f6951
AW
328 frame = SCM_STACK_FRAME (stack);
329 while (c_index--)
93dbc31b 330 frame = scm_frame_previous (frame);
aa3f6951 331 return frame;
782d171c 332}
1bbd0b84 333#undef FUNC_NAME
782d171c 334
3b3b36dd 335SCM_DEFINE (scm_stack_length, "stack-length", 1, 0, 0,
67941e3c
MG
336 (SCM stack),
337 "Return the length of @var{stack}.")
1bbd0b84 338#define FUNC_NAME s_scm_stack_length
782d171c 339{
34d19ef6 340 SCM_VALIDATE_STACK (1, stack);
aa3f6951 341 return scm_from_long (SCM_STACK_LENGTH (stack));
782d171c 342}
1bbd0b84 343#undef FUNC_NAME
782d171c
MD
344
345\f
346
347void
348scm_init_stacks ()
349{
f39448c5
AW
350 scm_stack_type = scm_make_vtable (scm_from_locale_string (SCM_STACK_LAYOUT),
351 SCM_UNDEFINED);
cc95e00a
MV
352 scm_set_struct_vtable_name_x (scm_stack_type,
353 scm_from_locale_symbol ("stack"));
a0599745 354#include "libguile/stacks.x"
782d171c 355}
89e00824
ML
356
357/*
358 Local Variables:
359 c-file-style: "gnu"
360 End:
361*/