fix scm_protects deprecation warning
[bpt/guile.git] / libguile / frames.c
1 /* Copyright (C) 2001, 2009, 2010, 2011 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_DEFINE (scm_frame_source, "frame-source", 1, 0, 0,
96 (SCM frame),
97 "")
98 #define FUNC_NAME s_scm_frame_source
99 {
100 SCM_VALIDATE_VM_FRAME (1, frame);
101
102 return scm_program_source (scm_frame_procedure (frame),
103 scm_frame_instruction_pointer (frame),
104 SCM_UNDEFINED);
105 }
106 #undef FUNC_NAME
107
108 /* The number of locals would be a simple thing to compute, if it weren't for
109 the presence of not-yet-active frames on the stack. So we have a cheap
110 heuristic to detect not-yet-active frames, and skip over them. Perhaps we
111 should represent them more usefully.
112 */
113 SCM_DEFINE (scm_frame_num_locals, "frame-num-locals", 1, 0, 0,
114 (SCM frame),
115 "")
116 #define FUNC_NAME s_scm_frame_num_locals
117 {
118 SCM *sp, *p;
119 unsigned int n = 0;
120
121 SCM_VALIDATE_VM_FRAME (1, frame);
122
123 sp = SCM_VM_FRAME_SP (frame);
124 p = SCM_FRAME_STACK_ADDRESS (SCM_VM_FRAME_FP (frame));
125 while (p <= sp)
126 {
127 if (SCM_UNPACK (p[0]) == 0)
128 /* skip over not-yet-active frame */
129 p += 3;
130 else
131 {
132 p++;
133 n++;
134 }
135 }
136 return scm_from_uint (n);
137 }
138 #undef FUNC_NAME
139
140 /* Need same not-yet-active frame logic here as in frame-num-locals */
141 SCM_DEFINE (scm_frame_local_ref, "frame-local-ref", 2, 0, 0,
142 (SCM frame, SCM index),
143 "")
144 #define FUNC_NAME s_scm_frame_local_ref
145 {
146 SCM *sp, *p;
147 unsigned int n = 0;
148 unsigned int i;
149
150 SCM_VALIDATE_VM_FRAME (1, frame);
151 SCM_VALIDATE_UINT_COPY (2, index, i);
152
153 sp = SCM_VM_FRAME_SP (frame);
154 p = SCM_FRAME_STACK_ADDRESS (SCM_VM_FRAME_FP (frame));
155 while (p <= sp)
156 {
157 if (SCM_UNPACK (p[0]) == 0)
158 /* skip over not-yet-active frame */
159 p += 3;
160 else if (n == i)
161 return *p;
162 else
163 {
164 p++;
165 n++;
166 }
167 }
168 SCM_OUT_OF_RANGE (SCM_ARG2, index);
169 }
170 #undef FUNC_NAME
171
172 /* Need same not-yet-active frame logic here as in frame-num-locals */
173 SCM_DEFINE (scm_frame_local_set_x, "frame-local-set!", 3, 0, 0,
174 (SCM frame, SCM index, SCM val),
175 "")
176 #define FUNC_NAME s_scm_frame_local_set_x
177 {
178 SCM *sp, *p;
179 unsigned int n = 0;
180 unsigned int i;
181
182 SCM_VALIDATE_VM_FRAME (1, frame);
183 SCM_VALIDATE_UINT_COPY (2, index, i);
184
185 sp = SCM_VM_FRAME_SP (frame);
186 p = SCM_FRAME_STACK_ADDRESS (SCM_VM_FRAME_FP (frame));
187 while (p <= sp)
188 {
189 if (SCM_UNPACK (p[0]) == 0)
190 /* skip over not-yet-active frame */
191 p += 3;
192 else if (n == i)
193 {
194 *p = val;
195 return SCM_UNSPECIFIED;
196 }
197 else
198 {
199 p++;
200 n++;
201 }
202 }
203 SCM_OUT_OF_RANGE (SCM_ARG2, index);
204 }
205 #undef FUNC_NAME
206
207 SCM_DEFINE (scm_frame_address, "frame-address", 1, 0, 0,
208 (SCM frame),
209 "Return the frame pointer for @var{frame}.")
210 #define FUNC_NAME s_scm_frame_address
211 {
212 SCM_VALIDATE_VM_FRAME (1, frame);
213 return scm_from_unsigned_integer ((scm_t_bits) SCM_VM_FRAME_FP (frame));
214 }
215 #undef FUNC_NAME
216
217 SCM_DEFINE (scm_frame_stack_pointer, "frame-stack-pointer", 1, 0, 0,
218 (SCM frame),
219 "")
220 #define FUNC_NAME s_scm_frame_stack_pointer
221 {
222 SCM_VALIDATE_VM_FRAME (1, frame);
223
224 return scm_from_unsigned_integer ((scm_t_bits) SCM_VM_FRAME_SP (frame));
225 }
226 #undef FUNC_NAME
227
228 SCM_DEFINE (scm_frame_instruction_pointer, "frame-instruction-pointer", 1, 0, 0,
229 (SCM frame),
230 "")
231 #define FUNC_NAME s_scm_frame_instruction_pointer
232 {
233 const struct scm_objcode *c_objcode;
234
235 SCM_VALIDATE_VM_FRAME (1, frame);
236
237 c_objcode = SCM_PROGRAM_DATA (scm_frame_procedure (frame));
238 return scm_from_unsigned_integer ((SCM_VM_FRAME_IP (frame)
239 - SCM_C_OBJCODE_BASE (c_objcode)));
240 }
241 #undef FUNC_NAME
242
243 SCM_DEFINE (scm_frame_return_address, "frame-return-address", 1, 0, 0,
244 (SCM frame),
245 "")
246 #define FUNC_NAME s_scm_frame_return_address
247 {
248 SCM_VALIDATE_VM_FRAME (1, frame);
249 return scm_from_unsigned_integer ((scm_t_bits)
250 (SCM_FRAME_RETURN_ADDRESS
251 (SCM_VM_FRAME_FP (frame))));
252 }
253 #undef FUNC_NAME
254
255 SCM_DEFINE (scm_frame_mv_return_address, "frame-mv-return-address", 1, 0, 0,
256 (SCM frame),
257 "")
258 #define FUNC_NAME s_scm_frame_mv_return_address
259 {
260 SCM_VALIDATE_VM_FRAME (1, frame);
261 return scm_from_unsigned_integer ((scm_t_bits)
262 (SCM_FRAME_MV_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_ulong
275 ((unsigned long)
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
288 SCM_VALIDATE_VM_FRAME (1, frame);
289
290 again:
291 this_fp = SCM_VM_FRAME_FP (frame);
292 new_fp = SCM_FRAME_DYNAMIC_LINK (this_fp);
293 if (new_fp)
294 { new_fp = RELOC (frame, new_fp);
295 new_sp = SCM_FRAME_LOWER_ADDRESS (this_fp) - 1;
296 frame = scm_c_make_frame (SCM_VM_FRAME_STACK_HOLDER (frame),
297 new_fp, new_sp,
298 SCM_FRAME_RETURN_ADDRESS (this_fp),
299 SCM_VM_FRAME_OFFSET (frame));
300 if (SCM_PROGRAM_IS_BOOT (scm_frame_procedure (frame)))
301 goto again;
302 else
303 return frame;
304 }
305 else
306 return SCM_BOOL_F;
307 }
308 #undef FUNC_NAME
309
310 \f
311 void
312 scm_init_frames (void)
313 {
314 #ifndef SCM_MAGIC_SNARFER
315 #include "libguile/frames.x"
316 #endif
317 }
318
319 /*
320 Local Variables:
321 c-file-style: "gnu"
322 End:
323 */