1 /* Copyright (C) 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
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.
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.
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
25 #include "libguile/_scm.h"
26 #include "libguile/control.h"
27 #include "libguile/programs.h"
28 #include "libguile/instructions.h"
29 #include "libguile/vm.h"
33 #define PROMPT_ESCAPE_P(p) \
34 (SCM_DYNSTACK_TAG_FLAGS (SCM_DYNSTACK_TAG (p)) \
35 & SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY)
40 /* Only to be called if the SCM_I_SETJMP returns 1 */
42 scm_i_prompt_pop_abort_args_x (struct scm_vm
*vp
)
47 n
= scm_to_size_t (vp
->sp
[0]);
48 for (i
= 0; i
< n
; i
++)
49 vals
= scm_cons (vp
->sp
[-(i
+ 1)], vals
);
51 /* The abort did reset the VM's registers, but then these values
52 were pushed on; so we need to pop them ourselves. */
60 static const scm_t_uint32 compose_continuation_code
[] =
62 SCM_PACK_OP_24 (compose_continuation
, 0)
67 make_partial_continuation (SCM vm_cont
)
70 scm_t_bits flags
= SCM_F_PROGRAM_IS_PARTIAL_CONTINUATION
;
73 ret
= scm_words (scm_tc7_program
| (nfree
<< 16) | flags
, nfree
+ 2);
74 SCM_SET_CELL_WORD_1 (ret
, compose_continuation_code
);
75 SCM_PROGRAM_FREE_VARIABLE_SET (ret
, 0, vm_cont
);
81 reify_partial_continuation (struct scm_vm
*vp
,
84 scm_t_uint32
*saved_ip
,
85 scm_i_jmp_buf
*saved_registers
,
86 scm_t_dynstack
*dynstack
,
87 scm_i_jmp_buf
*current_registers
)
93 flags
= SCM_F_VM_CONT_PARTIAL
;
94 /* If we are aborting to a prompt that has the same registers as those
95 of the abort, it means there are no intervening C frames on the
96 stack, and so the continuation can be relocated elsewhere on the
97 stack: it is rewindable. */
98 if (saved_registers
&& saved_registers
== current_registers
)
99 flags
|= SCM_F_VM_CONT_REWINDABLE
;
101 /* Walk the stack down until we find the first frame after saved_fp.
102 We will save the stack down to that frame. It used to be that we
103 could determine the stack bottom in O(1) time, but that's no longer
104 the case, since the thunk application doesn't occur where the
106 for (bottom_fp
= vp
->fp
;
107 SCM_FRAME_DYNAMIC_LINK (bottom_fp
) > saved_fp
;
108 bottom_fp
= SCM_FRAME_DYNAMIC_LINK (bottom_fp
));
110 if (SCM_FRAME_DYNAMIC_LINK (bottom_fp
) != saved_fp
)
113 /* Capture from the top of the thunk application frame up to the end. */
114 vm_cont
= scm_i_vm_capture_stack (&SCM_FRAME_LOCAL (bottom_fp
, 0),
121 return make_partial_continuation (vm_cont
);
125 scm_c_abort (struct scm_vm
*vp
, SCM tag
, size_t n
, SCM
*argv
,
126 scm_i_jmp_buf
*current_registers
)
129 scm_t_dynstack
*dynstack
= &SCM_I_CURRENT_THREAD
->dynstack
;
131 scm_t_dynstack_prompt_flags flags
;
132 scm_t_ptrdiff fp_offset
, sp_offset
;
135 scm_i_jmp_buf
*registers
;
138 prompt
= scm_dynstack_find_prompt (dynstack
, tag
,
139 &flags
, &fp_offset
, &sp_offset
, &ip
,
143 scm_misc_error ("abort", "Abort to unknown prompt", scm_list_1 (tag
));
145 fp
= vp
->stack_base
+ fp_offset
;
146 sp
= vp
->stack_base
+ sp_offset
;
148 /* Only reify if the continuation referenced in the handler. */
149 if (flags
& SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY
)
153 scm_t_dynstack
*captured
;
155 captured
= scm_dynstack_capture (dynstack
, SCM_DYNSTACK_NEXT (prompt
));
156 cont
= reify_partial_continuation (vp
, fp
, sp
, ip
, registers
, captured
,
161 scm_dynstack_unwind (dynstack
, prompt
);
163 /* Restore VM regs */
168 /* Since we're jumping down, we should always have enough space. */
169 if (vp
->sp
+ n
+ 1 >= vp
->stack_limit
)
173 *(++(vp
->sp
)) = cont
;
174 for (i
= 0; i
< n
; i
++)
175 *(++(vp
->sp
)) = argv
[i
];
176 if (flags
& SCM_F_DYNSTACK_PROMPT_PUSH_NARGS
)
177 *(++(vp
->sp
)) = scm_from_size_t (n
+1); /* +1 for continuation */
180 SCM_I_LONGJMP (*registers
, 1);
182 /* Shouldn't get here */
186 SCM_DEFINE (scm_abort_to_prompt_star
, "abort-to-prompt*", 2, 0, 0,
188 "Abort to the nearest prompt with tag @var{tag}, yielding the\n"
189 "values in the list, @var{args}.")
190 #define FUNC_NAME s_scm_abort_to_prompt_star
196 SCM_VALIDATE_LIST_COPYLEN (SCM_ARG2
, args
, n
);
197 argv
= alloca (sizeof (SCM
)*n
);
198 for (i
= 0; i
< n
; i
++, args
= scm_cdr (args
))
199 argv
[i
] = scm_car (args
);
201 scm_c_abort (scm_the_vm (), tag
, n
, argv
, NULL
);
203 /* Oh, what, you're still here? The abort must have been reinstated. Actually,
204 that's quite impossible, given that we're already in C-land here, so...
212 scm_init_control (void)
214 #include "libguile/control.x"