Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / libguile / frames.c
1 /* Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
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
23 #include <stdlib.h>
24 #include <string.h>
25 #include "_scm.h"
26 #include "frames.h"
27 #include "vm.h"
28 #include <verify.h>
29
30 /* Make sure assumptions on the layout of `struct scm_vm_frame' hold. */
31 verify (sizeof (SCM) == sizeof (SCM *));
32 verify (sizeof (struct scm_vm_frame) == 3 * sizeof (SCM));
33 verify (offsetof (struct scm_vm_frame, dynamic_link) == 0);
34
35 \f
36 #define RELOC(frame, val) \
37 (((SCM *) (val)) + SCM_VM_FRAME_OFFSET (frame))
38
39 SCM
40 scm_c_make_frame (enum scm_vm_frame_kind frame_kind, void *stack_holder,
41 scm_t_ptrdiff fp_offset, scm_t_ptrdiff sp_offset,
42 scm_t_uint32 *ip)
43 {
44 struct scm_frame *p = scm_gc_malloc (sizeof (struct scm_frame),
45 "vmframe");
46 p->stack_holder = stack_holder;
47 p->fp_offset = fp_offset;
48 p->sp_offset = sp_offset;
49 p->ip = ip;
50 return scm_cell (scm_tc7_frame | (frame_kind << 8), (scm_t_bits)p);
51 }
52
53 void
54 scm_i_frame_print (SCM frame, SCM port, scm_print_state *pstate)
55 {
56 scm_puts_unlocked ("#<frame ", port);
57 scm_uintprint (SCM_UNPACK (frame), 16, port);
58 scm_putc_unlocked (' ', port);
59 scm_write (scm_frame_procedure (frame), port);
60 /* don't write args, they can get us into trouble. */
61 scm_puts_unlocked (">", port);
62 }
63
64 SCM*
65 scm_i_frame_stack_base (SCM frame)
66 #define FUNC_NAME "frame-stack-base"
67 {
68 void *stack_holder;
69
70 SCM_VALIDATE_VM_FRAME (1, frame);
71
72 stack_holder = SCM_VM_FRAME_STACK_HOLDER (frame);
73
74 switch (SCM_VM_FRAME_KIND (frame))
75 {
76 case SCM_VM_FRAME_KIND_CONT:
77 return ((struct scm_vm_cont *) stack_holder)->stack_base;
78
79 case SCM_VM_FRAME_KIND_VM:
80 return ((struct scm_vm *) stack_holder)->stack_base;
81
82 default:
83 abort ();
84 }
85 }
86 #undef FUNC_NAME
87
88 scm_t_ptrdiff
89 scm_i_frame_offset (SCM frame)
90 #define FUNC_NAME "frame-offset"
91 {
92 void *stack_holder;
93
94 SCM_VALIDATE_VM_FRAME (1, frame);
95
96 stack_holder = SCM_VM_FRAME_STACK_HOLDER (frame);
97
98 switch (SCM_VM_FRAME_KIND (frame))
99 {
100 case SCM_VM_FRAME_KIND_CONT:
101 return ((struct scm_vm_cont *) stack_holder)->reloc;
102
103 case SCM_VM_FRAME_KIND_VM:
104 return 0;
105
106 default:
107 abort ();
108 }
109 }
110 #undef FUNC_NAME
111
112 \f
113 /* Scheme interface */
114
115 SCM_DEFINE (scm_frame_p, "frame?", 1, 0, 0,
116 (SCM obj),
117 "")
118 #define FUNC_NAME s_scm_frame_p
119 {
120 return scm_from_bool (SCM_VM_FRAME_P (obj));
121 }
122 #undef FUNC_NAME
123
124 SCM_DEFINE (scm_frame_procedure, "frame-procedure", 1, 0, 0,
125 (SCM frame),
126 "")
127 #define FUNC_NAME s_scm_frame_procedure
128 {
129 SCM_VALIDATE_VM_FRAME (1, frame);
130 return SCM_FRAME_PROGRAM (SCM_VM_FRAME_FP (frame));
131 }
132 #undef FUNC_NAME
133
134 SCM_DEFINE (scm_frame_arguments, "frame-arguments", 1, 0, 0,
135 (SCM frame),
136 "")
137 #define FUNC_NAME s_scm_frame_arguments
138 {
139 static SCM var = SCM_BOOL_F;
140
141 SCM_VALIDATE_VM_FRAME (1, frame);
142
143 if (scm_is_false (var))
144 var = scm_c_module_lookup (scm_c_resolve_module ("system vm frame"),
145 "frame-arguments");
146
147 return scm_call_1 (SCM_VARIABLE_REF (var), frame);
148 }
149 #undef FUNC_NAME
150
151 SCM_DEFINE (scm_frame_source, "frame-source", 1, 0, 0,
152 (SCM frame),
153 "")
154 #define FUNC_NAME s_scm_frame_source
155 {
156 SCM_VALIDATE_VM_FRAME (1, frame);
157
158 return scm_find_source_for_addr (scm_frame_instruction_pointer (frame));
159 }
160 #undef FUNC_NAME
161
162 SCM_DEFINE (scm_frame_num_locals, "frame-num-locals", 1, 0, 0,
163 (SCM frame),
164 "")
165 #define FUNC_NAME s_scm_frame_num_locals
166 {
167 SCM *fp, *sp;
168
169 SCM_VALIDATE_VM_FRAME (1, frame);
170
171 fp = SCM_VM_FRAME_FP (frame);
172 sp = SCM_VM_FRAME_SP (frame);
173
174 return scm_from_ptrdiff_t (SCM_FRAME_NUM_LOCALS (fp, sp));
175 }
176 #undef FUNC_NAME
177
178 SCM_DEFINE (scm_frame_local_ref, "frame-local-ref", 2, 0, 0,
179 (SCM frame, SCM index),
180 "")
181 #define FUNC_NAME s_scm_frame_local_ref
182 {
183 SCM *fp, *sp;
184 unsigned int i;
185
186 SCM_VALIDATE_VM_FRAME (1, frame);
187 SCM_VALIDATE_UINT_COPY (2, index, i);
188
189 fp = SCM_VM_FRAME_FP (frame);
190 sp = SCM_VM_FRAME_SP (frame);
191
192 if (i < SCM_FRAME_NUM_LOCALS (fp, sp))
193 return SCM_FRAME_LOCAL (fp, i);
194
195 SCM_OUT_OF_RANGE (SCM_ARG2, index);
196 }
197 #undef FUNC_NAME
198
199 /* Need same not-yet-active frame logic here as in frame-num-locals */
200 SCM_DEFINE (scm_frame_local_set_x, "frame-local-set!", 3, 0, 0,
201 (SCM frame, SCM index, SCM val),
202 "")
203 #define FUNC_NAME s_scm_frame_local_set_x
204 {
205 SCM *fp, *sp;
206 unsigned int i;
207
208 SCM_VALIDATE_VM_FRAME (1, frame);
209 SCM_VALIDATE_UINT_COPY (2, index, i);
210
211 fp = SCM_VM_FRAME_FP (frame);
212 sp = SCM_VM_FRAME_SP (frame);
213
214 if (i < SCM_FRAME_NUM_LOCALS (fp, sp))
215 {
216 SCM_FRAME_LOCAL (fp, i) = val;
217 return SCM_UNSPECIFIED;
218 }
219
220 SCM_OUT_OF_RANGE (SCM_ARG2, index);
221 }
222 #undef FUNC_NAME
223
224 SCM_DEFINE (scm_frame_address, "frame-address", 1, 0, 0,
225 (SCM frame),
226 "Return the frame pointer for @var{frame}.")
227 #define FUNC_NAME s_scm_frame_address
228 {
229 SCM_VALIDATE_VM_FRAME (1, frame);
230 return scm_from_uintptr_t ((scm_t_uintptr) SCM_VM_FRAME_FP (frame));
231 }
232 #undef FUNC_NAME
233
234 SCM_DEFINE (scm_frame_stack_pointer, "frame-stack-pointer", 1, 0, 0,
235 (SCM frame),
236 "")
237 #define FUNC_NAME s_scm_frame_stack_pointer
238 {
239 SCM_VALIDATE_VM_FRAME (1, frame);
240
241 return scm_from_uintptr_t ((scm_t_uintptr) SCM_VM_FRAME_SP (frame));
242 }
243 #undef FUNC_NAME
244
245 SCM_DEFINE (scm_frame_instruction_pointer, "frame-instruction-pointer", 1, 0, 0,
246 (SCM frame),
247 "")
248 #define FUNC_NAME s_scm_frame_instruction_pointer
249 {
250 SCM_VALIDATE_VM_FRAME (1, frame);
251
252 return scm_from_uintptr_t ((scm_t_uintptr) SCM_VM_FRAME_IP (frame));
253 }
254 #undef FUNC_NAME
255
256 SCM_DEFINE (scm_frame_return_address, "frame-return-address", 1, 0, 0,
257 (SCM frame),
258 "")
259 #define FUNC_NAME s_scm_frame_return_address
260 {
261 SCM_VALIDATE_VM_FRAME (1, frame);
262 return scm_from_uintptr_t ((scm_t_uintptr) (SCM_FRAME_RETURN_ADDRESS
263 (SCM_VM_FRAME_FP (frame))));
264 }
265 #undef FUNC_NAME
266
267 SCM_DEFINE (scm_frame_dynamic_link, "frame-dynamic-link", 1, 0, 0,
268 (SCM frame),
269 "")
270 #define FUNC_NAME s_scm_frame_dynamic_link
271 {
272 SCM_VALIDATE_VM_FRAME (1, frame);
273 /* fixme: munge fp if holder is a continuation */
274 return scm_from_uintptr_t
275 ((scm_t_uintptr)
276 RELOC (frame,
277 SCM_FRAME_DYNAMIC_LINK (SCM_VM_FRAME_FP (frame))));
278 }
279 #undef FUNC_NAME
280
281 SCM_DEFINE (scm_frame_previous, "frame-previous", 1, 0, 0,
282 (SCM frame),
283 "")
284 #define FUNC_NAME s_scm_frame_previous
285 {
286 SCM *this_fp, *new_fp, *new_sp;
287 SCM proc;
288
289 SCM_VALIDATE_VM_FRAME (1, frame);
290
291 again:
292 this_fp = SCM_VM_FRAME_FP (frame);
293 new_fp = SCM_FRAME_DYNAMIC_LINK (this_fp);
294 if (new_fp)
295 {
296 SCM *stack_base = scm_i_frame_stack_base (frame);
297 new_fp = RELOC (frame, new_fp);
298 new_sp = SCM_FRAME_PREVIOUS_SP (this_fp);
299 frame = scm_c_make_frame (SCM_VM_FRAME_KIND (frame),
300 SCM_VM_FRAME_STACK_HOLDER (frame),
301 new_fp - stack_base, new_sp - stack_base,
302 SCM_FRAME_RETURN_ADDRESS (this_fp));
303 proc = scm_frame_procedure (frame);
304
305 if (SCM_PROGRAM_P (proc) && SCM_PROGRAM_IS_BOOT (proc))
306 goto again;
307 else
308 return frame;
309 }
310 else
311 return SCM_BOOL_F;
312 }
313 #undef FUNC_NAME
314
315 \f
316 void
317 scm_init_frames (void)
318 {
319 #ifndef SCM_MAGIC_SNARFER
320 #include "libguile/frames.x"
321 #endif
322 }
323
324 /*
325 Local Variables:
326 c-file-style: "gnu"
327 End:
328 */