Commit | Line | Data |
---|---|---|
423fca76 | 1 | /* Copyright (C) 2001, 2009, 2010, 2011 Free Software Foundation, Inc. |
ac99cb0c | 2 | * |
560b9c25 | 3 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
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. | |
ac99cb0c | 7 | * |
53befeb7 NJ |
8 | * This library is distributed in the hope that it will be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
560b9c25 AW |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | * Lesser General Public License for more details. | |
ac99cb0c | 12 | * |
560b9c25 AW |
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 | |
53befeb7 NJ |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
16 | * 02110-1301 USA | |
560b9c25 | 17 | */ |
ac99cb0c | 18 | |
13c47753 AW |
19 | #if HAVE_CONFIG_H |
20 | # include <config.h> | |
21 | #endif | |
22 | ||
da8b4747 | 23 | #include <stdlib.h> |
ac99cb0c | 24 | #include <string.h> |
560b9c25 | 25 | #include "_scm.h" |
ac99cb0c KN |
26 | #include "frames.h" |
27 | ||
28 | \f | |
b1b942b7 | 29 | #define RELOC(frame, val) (val + SCM_VM_FRAME_OFFSET (frame)) |
ac99cb0c KN |
30 | |
31 | SCM | |
aa3f6951 AW |
32 | scm_c_make_frame (SCM stack_holder, SCM *fp, SCM *sp, |
33 | scm_t_uint8 *ip, scm_t_ptrdiff offset) | |
ac99cb0c | 34 | { |
aa3f6951 AW |
35 | struct scm_frame *p = scm_gc_malloc (sizeof (struct scm_frame), |
36 | "vmframe"); | |
b1b942b7 AW |
37 | p->stack_holder = stack_holder; |
38 | p->fp = fp; | |
39 | p->sp = sp; | |
40 | p->ip = ip; | |
41 | p->offset = offset; | |
6f3b0cc2 | 42 | return scm_cell (scm_tc7_frame, (scm_t_bits)p); |
ac99cb0c KN |
43 | } |
44 | ||
6f3b0cc2 AW |
45 | void |
46 | scm_i_frame_print (SCM frame, SCM port, scm_print_state *pstate) | |
2f9769b6 | 47 | { |
aa3f6951 | 48 | scm_puts ("#<frame ", port); |
2f9769b6 AW |
49 | scm_uintprint (SCM_UNPACK (frame), 16, port); |
50 | scm_putc (' ', port); | |
aa3f6951 | 51 | scm_write (scm_frame_procedure (frame), port); |
2f9769b6 AW |
52 | /* don't write args, they can get us into trouble. */ |
53 | scm_puts (">", port); | |
2f9769b6 AW |
54 | } |
55 | ||
3d94d862 | 56 | \f |
ac99cb0c KN |
57 | /* Scheme interface */ |
58 | ||
aa3f6951 | 59 | SCM_DEFINE (scm_frame_p, "frame?", 1, 0, 0, |
ac99cb0c KN |
60 | (SCM obj), |
61 | "") | |
aa3f6951 | 62 | #define FUNC_NAME s_scm_frame_p |
ac99cb0c | 63 | { |
5c8cefe5 | 64 | return scm_from_bool (SCM_VM_FRAME_P (obj)); |
b1b942b7 AW |
65 | } |
66 | #undef FUNC_NAME | |
67 | ||
aa3f6951 | 68 | SCM_DEFINE (scm_frame_procedure, "frame-procedure", 1, 0, 0, |
b1b942b7 AW |
69 | (SCM frame), |
70 | "") | |
aa3f6951 | 71 | #define FUNC_NAME s_scm_frame_procedure |
b1b942b7 AW |
72 | { |
73 | SCM_VALIDATE_VM_FRAME (1, frame); | |
74 | return SCM_FRAME_PROGRAM (SCM_VM_FRAME_FP (frame)); | |
75 | } | |
76 | #undef FUNC_NAME | |
77 | ||
aa3f6951 AW |
78 | SCM_DEFINE (scm_frame_arguments, "frame-arguments", 1, 0, 0, |
79 | (SCM frame), | |
80 | "") | |
81 | #define FUNC_NAME s_scm_frame_arguments | |
b1b942b7 | 82 | { |
6c6a4439 | 83 | static SCM var = SCM_BOOL_F; |
b1b942b7 AW |
84 | |
85 | SCM_VALIDATE_VM_FRAME (1, frame); | |
86 | ||
6c6a4439 AW |
87 | if (scm_is_false (var)) |
88 | var = scm_c_module_lookup (scm_c_resolve_module ("system vm frame"), | |
aa3f6951 | 89 | "frame-arguments"); |
b1b942b7 | 90 | |
6c6a4439 | 91 | return scm_call_1 (SCM_VARIABLE_REF (var), frame); |
ac99cb0c KN |
92 | } |
93 | #undef FUNC_NAME | |
94 | ||
423fca76 AW |
95 | SCM_DEFINE (scm_frame_source, "frame-source", 1, 0, 0, |
96 | (SCM frame), | |
97 | "") | |
98 | #define FUNC_NAME s_scm_frame_source | |
ac99cb0c | 99 | { |
423fca76 | 100 | SCM_VALIDATE_VM_FRAME (1, frame); |
b1b942b7 | 101 | |
423fca76 AW |
102 | return scm_program_source (scm_frame_procedure (frame), |
103 | scm_frame_instruction_pointer (frame), | |
104 | SCM_UNDEFINED); | |
ac99cb0c | 105 | } |
423fca76 | 106 | #undef FUNC_NAME |
ac99cb0c | 107 | |
6c6a4439 AW |
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. | |
aa3f6951 AW |
112 | */ |
113 | SCM_DEFINE (scm_frame_num_locals, "frame-num-locals", 1, 0, 0, | |
6c6a4439 AW |
114 | (SCM frame), |
115 | "") | |
aa3f6951 | 116 | #define FUNC_NAME s_scm_frame_num_locals |
6c6a4439 AW |
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 | { | |
b2b33168 | 127 | if (SCM_UNPACK (p[0]) == 0) |
6c6a4439 AW |
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 | ||
aa3f6951 AW |
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, | |
af988bbf | 142 | (SCM frame, SCM index), |
ac99cb0c | 143 | "") |
aa3f6951 | 144 | #define FUNC_NAME s_scm_frame_local_ref |
ac99cb0c | 145 | { |
6c6a4439 AW |
146 | SCM *sp, *p; |
147 | unsigned int n = 0; | |
b1b942b7 | 148 | unsigned int i; |
b1b942b7 | 149 | |
6c6a4439 | 150 | SCM_VALIDATE_VM_FRAME (1, frame); |
b1b942b7 | 151 | SCM_VALIDATE_UINT_COPY (2, index, i); |
b1b942b7 | 152 | |
6c6a4439 AW |
153 | sp = SCM_VM_FRAME_SP (frame); |
154 | p = SCM_FRAME_STACK_ADDRESS (SCM_VM_FRAME_FP (frame)); | |
155 | while (p <= sp) | |
156 | { | |
b2b33168 | 157 | if (SCM_UNPACK (p[0]) == 0) |
6c6a4439 AW |
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); | |
af988bbf KN |
169 | } |
170 | #undef FUNC_NAME | |
ac99cb0c | 171 | |
aa3f6951 AW |
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, | |
af988bbf KN |
174 | (SCM frame, SCM index, SCM val), |
175 | "") | |
aa3f6951 | 176 | #define FUNC_NAME s_scm_frame_local_set_x |
af988bbf | 177 | { |
6c6a4439 AW |
178 | SCM *sp, *p; |
179 | unsigned int n = 0; | |
b1b942b7 | 180 | unsigned int i; |
b1b942b7 | 181 | |
6c6a4439 | 182 | SCM_VALIDATE_VM_FRAME (1, frame); |
b1b942b7 | 183 | SCM_VALIDATE_UINT_COPY (2, index, i); |
b1b942b7 | 184 | |
6c6a4439 AW |
185 | sp = SCM_VM_FRAME_SP (frame); |
186 | p = SCM_FRAME_STACK_ADDRESS (SCM_VM_FRAME_FP (frame)); | |
187 | while (p <= sp) | |
188 | { | |
b2b33168 | 189 | if (SCM_UNPACK (p[0]) == 0) |
6c6a4439 AW |
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 | |
b1b942b7 | 206 | |
2e30f398 AW |
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); | |
3d27ef4b | 213 | return scm_from_unsigned_integer ((scm_t_bits) SCM_VM_FRAME_FP (frame)); |
2e30f398 AW |
214 | } |
215 | #undef FUNC_NAME | |
216 | ||
542f975e AW |
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 | ||
3d27ef4b | 224 | return scm_from_unsigned_integer ((scm_t_bits) SCM_VM_FRAME_SP (frame)); |
542f975e AW |
225 | } |
226 | #undef FUNC_NAME | |
227 | ||
aa3f6951 | 228 | SCM_DEFINE (scm_frame_instruction_pointer, "frame-instruction-pointer", 1, 0, 0, |
6c6a4439 AW |
229 | (SCM frame), |
230 | "") | |
aa3f6951 | 231 | #define FUNC_NAME s_scm_frame_instruction_pointer |
6c6a4439 | 232 | { |
3dbbe28d LC |
233 | const struct scm_objcode *c_objcode; |
234 | ||
6c6a4439 | 235 | SCM_VALIDATE_VM_FRAME (1, frame); |
3dbbe28d LC |
236 | |
237 | c_objcode = SCM_PROGRAM_DATA (scm_frame_procedure (frame)); | |
3d27ef4b AW |
238 | return scm_from_unsigned_integer ((SCM_VM_FRAME_IP (frame) |
239 | - SCM_C_OBJCODE_BASE (c_objcode))); | |
ac99cb0c KN |
240 | } |
241 | #undef FUNC_NAME | |
242 | ||
aa3f6951 | 243 | SCM_DEFINE (scm_frame_return_address, "frame-return-address", 1, 0, 0, |
ac99cb0c KN |
244 | (SCM frame), |
245 | "") | |
aa3f6951 | 246 | #define FUNC_NAME s_scm_frame_return_address |
ac99cb0c | 247 | { |
b1b942b7 | 248 | SCM_VALIDATE_VM_FRAME (1, frame); |
3d27ef4b AW |
249 | return scm_from_unsigned_integer ((scm_t_bits) |
250 | (SCM_FRAME_RETURN_ADDRESS | |
251 | (SCM_VM_FRAME_FP (frame)))); | |
ac99cb0c KN |
252 | } |
253 | #undef FUNC_NAME | |
254 | ||
aa3f6951 | 255 | SCM_DEFINE (scm_frame_mv_return_address, "frame-mv-return-address", 1, 0, 0, |
da320011 AW |
256 | (SCM frame), |
257 | "") | |
aa3f6951 | 258 | #define FUNC_NAME s_scm_frame_mv_return_address |
da320011 | 259 | { |
b1b942b7 | 260 | SCM_VALIDATE_VM_FRAME (1, frame); |
3d27ef4b AW |
261 | return scm_from_unsigned_integer ((scm_t_bits) |
262 | (SCM_FRAME_MV_RETURN_ADDRESS | |
263 | (SCM_VM_FRAME_FP (frame)))); | |
da320011 AW |
264 | } |
265 | #undef FUNC_NAME | |
266 | ||
aa3f6951 | 267 | SCM_DEFINE (scm_frame_dynamic_link, "frame-dynamic-link", 1, 0, 0, |
ac99cb0c KN |
268 | (SCM frame), |
269 | "") | |
aa3f6951 | 270 | #define FUNC_NAME s_scm_frame_dynamic_link |
ac99cb0c | 271 | { |
b1b942b7 AW |
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)))); | |
ac99cb0c KN |
278 | } |
279 | #undef FUNC_NAME | |
280 | ||
93dbc31b AW |
281 | SCM_DEFINE (scm_frame_previous, "frame-previous", 1, 0, 0, |
282 | (SCM frame), | |
283 | "") | |
284 | #define FUNC_NAME s_scm_frame_previous | |
b1b942b7 AW |
285 | { |
286 | SCM *this_fp, *new_fp, *new_sp; | |
93dbc31b AW |
287 | |
288 | SCM_VALIDATE_VM_FRAME (1, frame); | |
289 | ||
290 | again: | |
b1b942b7 AW |
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; | |
93dbc31b AW |
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; | |
b1b942b7 AW |
304 | } |
305 | else | |
306 | return SCM_BOOL_F; | |
307 | } | |
93dbc31b | 308 | #undef FUNC_NAME |
b1b942b7 | 309 | |
ac99cb0c | 310 | \f |
07e56b27 AW |
311 | void |
312 | scm_init_frames (void) | |
313 | { | |
ac99cb0c | 314 | #ifndef SCM_MAGIC_SNARFER |
aeeff258 | 315 | #include "libguile/frames.x" |
ac99cb0c KN |
316 | #endif |
317 | } | |
318 | ||
319 | /* | |
320 | Local Variables: | |
321 | c-file-style: "gnu" | |
322 | End: | |
323 | */ |