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