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