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