tc7 tags for vm-related data
[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_instruction_pointer, "frame-instruction-pointer", 1, 0, 0,
207 (SCM frame),
208 "")
209 #define FUNC_NAME s_scm_frame_instruction_pointer
210 {
211 const struct scm_objcode *c_objcode;
212
213 SCM_VALIDATE_VM_FRAME (1, frame);
214
215 c_objcode = SCM_PROGRAM_DATA (scm_frame_procedure (frame));
216 return scm_from_ulong ((unsigned long)
217 (SCM_VM_FRAME_IP (frame)
218 - SCM_C_OBJCODE_BASE (c_objcode)));
219 }
220 #undef FUNC_NAME
221
222 SCM_DEFINE (scm_frame_return_address, "frame-return-address", 1, 0, 0,
223 (SCM frame),
224 "")
225 #define FUNC_NAME s_scm_frame_return_address
226 {
227 SCM_VALIDATE_VM_FRAME (1, frame);
228 return scm_from_ulong ((unsigned long)
229 (SCM_FRAME_RETURN_ADDRESS
230 (SCM_VM_FRAME_FP (frame))));
231 }
232 #undef FUNC_NAME
233
234 SCM_DEFINE (scm_frame_mv_return_address, "frame-mv-return-address", 1, 0, 0,
235 (SCM frame),
236 "")
237 #define FUNC_NAME s_scm_frame_mv_return_address
238 {
239 SCM_VALIDATE_VM_FRAME (1, frame);
240 return scm_from_ulong ((unsigned long)
241 (SCM_FRAME_MV_RETURN_ADDRESS
242 (SCM_VM_FRAME_FP (frame))));
243 }
244 #undef FUNC_NAME
245
246 SCM_DEFINE (scm_frame_dynamic_link, "frame-dynamic-link", 1, 0, 0,
247 (SCM frame),
248 "")
249 #define FUNC_NAME s_scm_frame_dynamic_link
250 {
251 SCM_VALIDATE_VM_FRAME (1, frame);
252 /* fixme: munge fp if holder is a continuation */
253 return scm_from_ulong
254 ((unsigned long)
255 RELOC (frame,
256 SCM_FRAME_DYNAMIC_LINK (SCM_VM_FRAME_FP (frame))));
257 }
258 #undef FUNC_NAME
259
260 SCM_DEFINE (scm_frame_previous, "frame-previous", 1, 0, 0,
261 (SCM frame),
262 "")
263 #define FUNC_NAME s_scm_frame_previous
264 {
265 SCM *this_fp, *new_fp, *new_sp;
266
267 SCM_VALIDATE_VM_FRAME (1, frame);
268
269 again:
270 this_fp = SCM_VM_FRAME_FP (frame);
271 new_fp = SCM_FRAME_DYNAMIC_LINK (this_fp);
272 if (new_fp)
273 { new_fp = RELOC (frame, new_fp);
274 new_sp = SCM_FRAME_LOWER_ADDRESS (this_fp) - 1;
275 frame = scm_c_make_frame (SCM_VM_FRAME_STACK_HOLDER (frame),
276 new_fp, new_sp,
277 SCM_FRAME_RETURN_ADDRESS (this_fp),
278 SCM_VM_FRAME_OFFSET (frame));
279 if (SCM_PROGRAM_IS_BOOT (scm_frame_procedure (frame)))
280 goto again;
281 else
282 return frame;
283 }
284 else
285 return SCM_BOOL_F;
286 }
287 #undef FUNC_NAME
288
289 \f
290 void
291 scm_init_frames (void)
292 {
293 #ifndef SCM_MAGIC_SNARFER
294 #include "libguile/frames.x"
295 #endif
296 }
297
298 /*
299 Local Variables:
300 c-file-style: "gnu"
301 End:
302 */