Commit | Line | Data |
---|---|---|
a6029b97 | 1 | /* Copyright (C) 2001, 2009, 2010 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 | ||
7abb7efd AW |
95 | SCM |
96 | scm_frame_source (SCM frame) | |
ac99cb0c | 97 | { |
7abb7efd | 98 | static SCM var = SCM_BOOL_F; |
b1b942b7 | 99 | |
7abb7efd AW |
100 | if (scm_is_false (var)) |
101 | var = scm_c_module_lookup (scm_c_resolve_module ("system vm frame"), | |
102 | "frame-source"); | |
b1b942b7 | 103 | |
7abb7efd | 104 | return scm_call_1 (SCM_VARIABLE_REF (var), frame); |
ac99cb0c | 105 | } |
ac99cb0c | 106 | |
6c6a4439 AW |
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. | |
aa3f6951 AW |
111 | */ |
112 | SCM_DEFINE (scm_frame_num_locals, "frame-num-locals", 1, 0, 0, | |
6c6a4439 AW |
113 | (SCM frame), |
114 | "") | |
aa3f6951 | 115 | #define FUNC_NAME s_scm_frame_num_locals |
6c6a4439 AW |
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 | ||
aa3f6951 AW |
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, | |
af988bbf | 141 | (SCM frame, SCM index), |
ac99cb0c | 142 | "") |
aa3f6951 | 143 | #define FUNC_NAME s_scm_frame_local_ref |
ac99cb0c | 144 | { |
6c6a4439 AW |
145 | SCM *sp, *p; |
146 | unsigned int n = 0; | |
b1b942b7 | 147 | unsigned int i; |
b1b942b7 | 148 | |
6c6a4439 | 149 | SCM_VALIDATE_VM_FRAME (1, frame); |
b1b942b7 | 150 | SCM_VALIDATE_UINT_COPY (2, index, i); |
b1b942b7 | 151 | |
6c6a4439 AW |
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); | |
af988bbf KN |
168 | } |
169 | #undef FUNC_NAME | |
ac99cb0c | 170 | |
aa3f6951 AW |
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, | |
af988bbf KN |
173 | (SCM frame, SCM index, SCM val), |
174 | "") | |
aa3f6951 | 175 | #define FUNC_NAME s_scm_frame_local_set_x |
af988bbf | 176 | { |
6c6a4439 AW |
177 | SCM *sp, *p; |
178 | unsigned int n = 0; | |
b1b942b7 | 179 | unsigned int i; |
b1b942b7 | 180 | |
6c6a4439 | 181 | SCM_VALIDATE_VM_FRAME (1, frame); |
b1b942b7 | 182 | SCM_VALIDATE_UINT_COPY (2, index, i); |
b1b942b7 | 183 | |
6c6a4439 AW |
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 | |
b1b942b7 | 205 | |
2e30f398 AW |
206 | SCM_DEFINE (scm_frame_address, "frame-address", 1, 0, 0, |
207 | (SCM frame), | |
208 | "Return the frame pointer for @var{frame}.") | |
209 | #define FUNC_NAME s_scm_frame_address | |
210 | { | |
211 | SCM_VALIDATE_VM_FRAME (1, frame); | |
212 | return scm_from_ulong ((unsigned long) SCM_VM_FRAME_FP (frame)); | |
213 | } | |
214 | #undef FUNC_NAME | |
215 | ||
aa3f6951 | 216 | SCM_DEFINE (scm_frame_instruction_pointer, "frame-instruction-pointer", 1, 0, 0, |
6c6a4439 AW |
217 | (SCM frame), |
218 | "") | |
aa3f6951 | 219 | #define FUNC_NAME s_scm_frame_instruction_pointer |
6c6a4439 | 220 | { |
3dbbe28d LC |
221 | const struct scm_objcode *c_objcode; |
222 | ||
6c6a4439 | 223 | SCM_VALIDATE_VM_FRAME (1, frame); |
3dbbe28d LC |
224 | |
225 | c_objcode = SCM_PROGRAM_DATA (scm_frame_procedure (frame)); | |
6c6a4439 AW |
226 | return scm_from_ulong ((unsigned long) |
227 | (SCM_VM_FRAME_IP (frame) | |
3dbbe28d | 228 | - SCM_C_OBJCODE_BASE (c_objcode))); |
ac99cb0c KN |
229 | } |
230 | #undef FUNC_NAME | |
231 | ||
aa3f6951 | 232 | SCM_DEFINE (scm_frame_return_address, "frame-return-address", 1, 0, 0, |
ac99cb0c KN |
233 | (SCM frame), |
234 | "") | |
aa3f6951 | 235 | #define FUNC_NAME s_scm_frame_return_address |
ac99cb0c | 236 | { |
b1b942b7 | 237 | SCM_VALIDATE_VM_FRAME (1, frame); |
b6368dbb LC |
238 | return scm_from_ulong ((unsigned long) |
239 | (SCM_FRAME_RETURN_ADDRESS | |
b1b942b7 | 240 | (SCM_VM_FRAME_FP (frame)))); |
ac99cb0c KN |
241 | } |
242 | #undef FUNC_NAME | |
243 | ||
aa3f6951 | 244 | SCM_DEFINE (scm_frame_mv_return_address, "frame-mv-return-address", 1, 0, 0, |
da320011 AW |
245 | (SCM frame), |
246 | "") | |
aa3f6951 | 247 | #define FUNC_NAME s_scm_frame_mv_return_address |
da320011 | 248 | { |
b1b942b7 | 249 | SCM_VALIDATE_VM_FRAME (1, frame); |
da320011 AW |
250 | return scm_from_ulong ((unsigned long) |
251 | (SCM_FRAME_MV_RETURN_ADDRESS | |
b1b942b7 | 252 | (SCM_VM_FRAME_FP (frame)))); |
da320011 AW |
253 | } |
254 | #undef FUNC_NAME | |
255 | ||
aa3f6951 | 256 | SCM_DEFINE (scm_frame_dynamic_link, "frame-dynamic-link", 1, 0, 0, |
ac99cb0c KN |
257 | (SCM frame), |
258 | "") | |
aa3f6951 | 259 | #define FUNC_NAME s_scm_frame_dynamic_link |
ac99cb0c | 260 | { |
b1b942b7 AW |
261 | SCM_VALIDATE_VM_FRAME (1, frame); |
262 | /* fixme: munge fp if holder is a continuation */ | |
263 | return scm_from_ulong | |
264 | ((unsigned long) | |
265 | RELOC (frame, | |
266 | SCM_FRAME_DYNAMIC_LINK (SCM_VM_FRAME_FP (frame)))); | |
ac99cb0c KN |
267 | } |
268 | #undef FUNC_NAME | |
269 | ||
93dbc31b AW |
270 | SCM_DEFINE (scm_frame_previous, "frame-previous", 1, 0, 0, |
271 | (SCM frame), | |
272 | "") | |
273 | #define FUNC_NAME s_scm_frame_previous | |
b1b942b7 AW |
274 | { |
275 | SCM *this_fp, *new_fp, *new_sp; | |
93dbc31b AW |
276 | |
277 | SCM_VALIDATE_VM_FRAME (1, frame); | |
278 | ||
279 | again: | |
b1b942b7 AW |
280 | this_fp = SCM_VM_FRAME_FP (frame); |
281 | new_fp = SCM_FRAME_DYNAMIC_LINK (this_fp); | |
282 | if (new_fp) | |
283 | { new_fp = RELOC (frame, new_fp); | |
284 | new_sp = SCM_FRAME_LOWER_ADDRESS (this_fp) - 1; | |
93dbc31b AW |
285 | frame = scm_c_make_frame (SCM_VM_FRAME_STACK_HOLDER (frame), |
286 | new_fp, new_sp, | |
287 | SCM_FRAME_RETURN_ADDRESS (this_fp), | |
288 | SCM_VM_FRAME_OFFSET (frame)); | |
289 | if (SCM_PROGRAM_IS_BOOT (scm_frame_procedure (frame))) | |
290 | goto again; | |
291 | else | |
292 | return frame; | |
b1b942b7 AW |
293 | } |
294 | else | |
295 | return SCM_BOOL_F; | |
296 | } | |
93dbc31b | 297 | #undef FUNC_NAME |
b1b942b7 | 298 | |
ac99cb0c | 299 | \f |
07e56b27 AW |
300 | void |
301 | scm_init_frames (void) | |
302 | { | |
ac99cb0c | 303 | #ifndef SCM_MAGIC_SNARFER |
aeeff258 | 304 | #include "libguile/frames.x" |
ac99cb0c KN |
305 | #endif |
306 | } | |
307 | ||
308 | /* | |
309 | Local Variables: | |
310 | c-file-style: "gnu" | |
311 | End: | |
312 | */ |