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 | { |
0607ebbf | 55 | scm_puts_unlocked ("#<frame ", port); |
2f9769b6 | 56 | scm_uintprint (SCM_UNPACK (frame), 16, port); |
0607ebbf | 57 | scm_putc_unlocked (' ', port); |
aa3f6951 | 58 | scm_write (scm_frame_procedure (frame), port); |
2f9769b6 | 59 | /* don't write args, they can get us into trouble. */ |
0607ebbf | 60 | scm_puts_unlocked (">", 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 | 131 | { |
510ca126 | 132 | SCM *fp, *sp, *p; |
6c6a4439 AW |
133 | unsigned int n = 0; |
134 | ||
135 | SCM_VALIDATE_VM_FRAME (1, frame); | |
136 | ||
510ca126 AW |
137 | fp = SCM_VM_FRAME_FP (frame); |
138 | sp = SCM_VM_FRAME_SP (frame); | |
139 | p = SCM_FRAME_STACK_ADDRESS (SCM_VM_FRAME_FP (frame)); | |
140 | ||
141 | if (SCM_RTL_PROGRAM_P (fp[-1])) | |
142 | /* The frame size of an RTL program is fixed, except in the case of | |
143 | passing a wrong number of arguments to the program. So we do | |
144 | need to use an SP for determining the number of locals. */ | |
145 | return scm_from_uint32 (sp + 1 - p); | |
146 | ||
6c6a4439 AW |
147 | sp = SCM_VM_FRAME_SP (frame); |
148 | p = SCM_FRAME_STACK_ADDRESS (SCM_VM_FRAME_FP (frame)); | |
149 | while (p <= sp) | |
150 | { | |
b2b33168 | 151 | if (SCM_UNPACK (p[0]) == 0) |
6c6a4439 AW |
152 | /* skip over not-yet-active frame */ |
153 | p += 3; | |
154 | else | |
155 | { | |
156 | p++; | |
157 | n++; | |
158 | } | |
159 | } | |
160 | return scm_from_uint (n); | |
161 | } | |
162 | #undef FUNC_NAME | |
163 | ||
aa3f6951 AW |
164 | /* Need same not-yet-active frame logic here as in frame-num-locals */ |
165 | SCM_DEFINE (scm_frame_local_ref, "frame-local-ref", 2, 0, 0, | |
af988bbf | 166 | (SCM frame, SCM index), |
ac99cb0c | 167 | "") |
aa3f6951 | 168 | #define FUNC_NAME s_scm_frame_local_ref |
ac99cb0c | 169 | { |
6c6a4439 AW |
170 | SCM *sp, *p; |
171 | unsigned int n = 0; | |
b1b942b7 | 172 | unsigned int i; |
b1b942b7 | 173 | |
6c6a4439 | 174 | SCM_VALIDATE_VM_FRAME (1, frame); |
b1b942b7 | 175 | SCM_VALIDATE_UINT_COPY (2, index, i); |
b1b942b7 | 176 | |
6c6a4439 AW |
177 | sp = SCM_VM_FRAME_SP (frame); |
178 | p = SCM_FRAME_STACK_ADDRESS (SCM_VM_FRAME_FP (frame)); | |
179 | while (p <= sp) | |
180 | { | |
b2b33168 | 181 | if (SCM_UNPACK (p[0]) == 0) |
6c6a4439 AW |
182 | /* skip over not-yet-active frame */ |
183 | p += 3; | |
184 | else if (n == i) | |
185 | return *p; | |
186 | else | |
187 | { | |
188 | p++; | |
189 | n++; | |
190 | } | |
191 | } | |
192 | SCM_OUT_OF_RANGE (SCM_ARG2, index); | |
af988bbf KN |
193 | } |
194 | #undef FUNC_NAME | |
ac99cb0c | 195 | |
aa3f6951 AW |
196 | /* Need same not-yet-active frame logic here as in frame-num-locals */ |
197 | SCM_DEFINE (scm_frame_local_set_x, "frame-local-set!", 3, 0, 0, | |
af988bbf KN |
198 | (SCM frame, SCM index, SCM val), |
199 | "") | |
aa3f6951 | 200 | #define FUNC_NAME s_scm_frame_local_set_x |
af988bbf | 201 | { |
6c6a4439 AW |
202 | SCM *sp, *p; |
203 | unsigned int n = 0; | |
b1b942b7 | 204 | unsigned int i; |
b1b942b7 | 205 | |
6c6a4439 | 206 | SCM_VALIDATE_VM_FRAME (1, frame); |
b1b942b7 | 207 | SCM_VALIDATE_UINT_COPY (2, index, i); |
b1b942b7 | 208 | |
6c6a4439 AW |
209 | sp = SCM_VM_FRAME_SP (frame); |
210 | p = SCM_FRAME_STACK_ADDRESS (SCM_VM_FRAME_FP (frame)); | |
211 | while (p <= sp) | |
212 | { | |
b2b33168 | 213 | if (SCM_UNPACK (p[0]) == 0) |
6c6a4439 AW |
214 | /* skip over not-yet-active frame */ |
215 | p += 3; | |
216 | else if (n == i) | |
217 | { | |
218 | *p = val; | |
219 | return SCM_UNSPECIFIED; | |
220 | } | |
221 | else | |
222 | { | |
223 | p++; | |
224 | n++; | |
225 | } | |
226 | } | |
227 | SCM_OUT_OF_RANGE (SCM_ARG2, index); | |
228 | } | |
229 | #undef FUNC_NAME | |
b1b942b7 | 230 | |
2e30f398 AW |
231 | SCM_DEFINE (scm_frame_address, "frame-address", 1, 0, 0, |
232 | (SCM frame), | |
233 | "Return the frame pointer for @var{frame}.") | |
234 | #define FUNC_NAME s_scm_frame_address | |
235 | { | |
236 | SCM_VALIDATE_VM_FRAME (1, frame); | |
3d27ef4b | 237 | return scm_from_unsigned_integer ((scm_t_bits) SCM_VM_FRAME_FP (frame)); |
2e30f398 AW |
238 | } |
239 | #undef FUNC_NAME | |
240 | ||
542f975e AW |
241 | SCM_DEFINE (scm_frame_stack_pointer, "frame-stack-pointer", 1, 0, 0, |
242 | (SCM frame), | |
243 | "") | |
244 | #define FUNC_NAME s_scm_frame_stack_pointer | |
245 | { | |
246 | SCM_VALIDATE_VM_FRAME (1, frame); | |
247 | ||
3d27ef4b | 248 | return scm_from_unsigned_integer ((scm_t_bits) SCM_VM_FRAME_SP (frame)); |
542f975e AW |
249 | } |
250 | #undef FUNC_NAME | |
251 | ||
aa3f6951 | 252 | SCM_DEFINE (scm_frame_instruction_pointer, "frame-instruction-pointer", 1, 0, 0, |
6c6a4439 AW |
253 | (SCM frame), |
254 | "") | |
aa3f6951 | 255 | #define FUNC_NAME s_scm_frame_instruction_pointer |
6c6a4439 | 256 | { |
67b699cc | 257 | SCM program; |
3dbbe28d LC |
258 | const struct scm_objcode *c_objcode; |
259 | ||
6c6a4439 | 260 | SCM_VALIDATE_VM_FRAME (1, frame); |
67b699cc | 261 | program = scm_frame_procedure (frame); |
3dbbe28d | 262 | |
67b699cc AW |
263 | if (!SCM_PROGRAM_P (program)) |
264 | return SCM_INUM0; | |
265 | ||
266 | c_objcode = SCM_PROGRAM_DATA (program); | |
3d27ef4b AW |
267 | return scm_from_unsigned_integer ((SCM_VM_FRAME_IP (frame) |
268 | - SCM_C_OBJCODE_BASE (c_objcode))); | |
ac99cb0c KN |
269 | } |
270 | #undef FUNC_NAME | |
271 | ||
aa3f6951 | 272 | SCM_DEFINE (scm_frame_return_address, "frame-return-address", 1, 0, 0, |
ac99cb0c KN |
273 | (SCM frame), |
274 | "") | |
aa3f6951 | 275 | #define FUNC_NAME s_scm_frame_return_address |
ac99cb0c | 276 | { |
b1b942b7 | 277 | SCM_VALIDATE_VM_FRAME (1, frame); |
3d27ef4b AW |
278 | return scm_from_unsigned_integer ((scm_t_bits) |
279 | (SCM_FRAME_RETURN_ADDRESS | |
280 | (SCM_VM_FRAME_FP (frame)))); | |
ac99cb0c KN |
281 | } |
282 | #undef FUNC_NAME | |
283 | ||
aa3f6951 | 284 | SCM_DEFINE (scm_frame_mv_return_address, "frame-mv-return-address", 1, 0, 0, |
da320011 AW |
285 | (SCM frame), |
286 | "") | |
aa3f6951 | 287 | #define FUNC_NAME s_scm_frame_mv_return_address |
da320011 | 288 | { |
b1b942b7 | 289 | SCM_VALIDATE_VM_FRAME (1, frame); |
3d27ef4b AW |
290 | return scm_from_unsigned_integer ((scm_t_bits) |
291 | (SCM_FRAME_MV_RETURN_ADDRESS | |
292 | (SCM_VM_FRAME_FP (frame)))); | |
da320011 AW |
293 | } |
294 | #undef FUNC_NAME | |
295 | ||
aa3f6951 | 296 | SCM_DEFINE (scm_frame_dynamic_link, "frame-dynamic-link", 1, 0, 0, |
ac99cb0c KN |
297 | (SCM frame), |
298 | "") | |
aa3f6951 | 299 | #define FUNC_NAME s_scm_frame_dynamic_link |
ac99cb0c | 300 | { |
b1b942b7 AW |
301 | SCM_VALIDATE_VM_FRAME (1, frame); |
302 | /* fixme: munge fp if holder is a continuation */ | |
303 | return scm_from_ulong | |
304 | ((unsigned long) | |
305 | RELOC (frame, | |
306 | SCM_FRAME_DYNAMIC_LINK (SCM_VM_FRAME_FP (frame)))); | |
ac99cb0c KN |
307 | } |
308 | #undef FUNC_NAME | |
309 | ||
93dbc31b AW |
310 | SCM_DEFINE (scm_frame_previous, "frame-previous", 1, 0, 0, |
311 | (SCM frame), | |
312 | "") | |
313 | #define FUNC_NAME s_scm_frame_previous | |
b1b942b7 AW |
314 | { |
315 | SCM *this_fp, *new_fp, *new_sp; | |
da874e54 | 316 | SCM proc; |
93dbc31b AW |
317 | |
318 | SCM_VALIDATE_VM_FRAME (1, frame); | |
319 | ||
320 | again: | |
b1b942b7 AW |
321 | this_fp = SCM_VM_FRAME_FP (frame); |
322 | new_fp = SCM_FRAME_DYNAMIC_LINK (this_fp); | |
323 | if (new_fp) | |
da874e54 AW |
324 | { |
325 | new_fp = RELOC (frame, new_fp); | |
b1b942b7 | 326 | new_sp = SCM_FRAME_LOWER_ADDRESS (this_fp) - 1; |
93dbc31b AW |
327 | frame = scm_c_make_frame (SCM_VM_FRAME_STACK_HOLDER (frame), |
328 | new_fp, new_sp, | |
329 | SCM_FRAME_RETURN_ADDRESS (this_fp), | |
330 | SCM_VM_FRAME_OFFSET (frame)); | |
da874e54 AW |
331 | proc = scm_frame_procedure (frame); |
332 | ||
333 | if (SCM_PROGRAM_P (proc) && SCM_PROGRAM_IS_BOOT (proc)) | |
93dbc31b AW |
334 | goto again; |
335 | else | |
336 | return frame; | |
b1b942b7 AW |
337 | } |
338 | else | |
339 | return SCM_BOOL_F; | |
340 | } | |
93dbc31b | 341 | #undef FUNC_NAME |
b1b942b7 | 342 | |
ac99cb0c | 343 | \f |
07e56b27 AW |
344 | void |
345 | scm_init_frames (void) | |
346 | { | |
ac99cb0c | 347 | #ifndef SCM_MAGIC_SNARFER |
aeeff258 | 348 | #include "libguile/frames.x" |
ac99cb0c KN |
349 | #endif |
350 | } | |
351 | ||
352 | /* | |
353 | Local Variables: | |
354 | c-file-style: "gnu" | |
355 | End: | |
356 | */ |