Commit | Line | Data |
---|---|---|
44d97054 | 1 | /* Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013, 2014 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" |
89b235af | 27 | #include "vm.h" |
0fc9040f LC |
28 | #include <verify.h> |
29 | ||
30 | /* Make sure assumptions on the layout of `struct scm_vm_frame' hold. */ | |
31 | verify (sizeof (SCM) == sizeof (SCM *)); | |
b636cdb0 | 32 | verify (sizeof (struct scm_vm_frame) == 3 * sizeof (SCM)); |
0fc9040f | 33 | verify (offsetof (struct scm_vm_frame, dynamic_link) == 0); |
ac99cb0c KN |
34 | |
35 | \f | |
ac99cb0c KN |
36 | |
37 | SCM | |
8de051da | 38 | scm_c_make_frame (enum scm_vm_frame_kind kind, const struct scm_frame *frame) |
ac99cb0c | 39 | { |
aa3f6951 AW |
40 | struct scm_frame *p = scm_gc_malloc (sizeof (struct scm_frame), |
41 | "vmframe"); | |
8de051da AW |
42 | p->stack_holder = frame->stack_holder; |
43 | p->fp_offset = frame->fp_offset; | |
44 | p->sp_offset = frame->sp_offset; | |
45 | p->ip = frame->ip; | |
46 | return scm_cell (scm_tc7_frame | (kind << 8), (scm_t_bits)p); | |
ac99cb0c KN |
47 | } |
48 | ||
6f3b0cc2 AW |
49 | void |
50 | scm_i_frame_print (SCM frame, SCM port, scm_print_state *pstate) | |
2f9769b6 | 51 | { |
0607ebbf | 52 | scm_puts_unlocked ("#<frame ", port); |
2f9769b6 | 53 | scm_uintprint (SCM_UNPACK (frame), 16, port); |
0607ebbf | 54 | scm_putc_unlocked (' ', port); |
aa3f6951 | 55 | scm_write (scm_frame_procedure (frame), port); |
2f9769b6 | 56 | /* don't write args, they can get us into trouble. */ |
0607ebbf | 57 | scm_puts_unlocked (">", port); |
2f9769b6 AW |
58 | } |
59 | ||
44d97054 | 60 | static SCM* |
3b14dd2f | 61 | frame_stack_base (enum scm_vm_frame_kind kind, const struct scm_frame *frame) |
89b235af | 62 | { |
44d97054 | 63 | switch (kind) |
5515edc5 AW |
64 | { |
65 | case SCM_VM_FRAME_KIND_CONT: | |
44d97054 | 66 | return ((struct scm_vm_cont *) frame->stack_holder)->stack_base; |
89b235af | 67 | |
5515edc5 | 68 | case SCM_VM_FRAME_KIND_VM: |
44d97054 | 69 | return ((struct scm_vm *) frame->stack_holder)->stack_base; |
5515edc5 AW |
70 | |
71 | default: | |
72 | abort (); | |
73 | } | |
89b235af | 74 | } |
44d97054 AW |
75 | |
76 | static scm_t_ptrdiff | |
3b14dd2f | 77 | frame_offset (enum scm_vm_frame_kind kind, const struct scm_frame *frame) |
44d97054 AW |
78 | { |
79 | switch (kind) | |
80 | { | |
81 | case SCM_VM_FRAME_KIND_CONT: | |
82 | return ((struct scm_vm_cont *) frame->stack_holder)->reloc; | |
83 | ||
84 | case SCM_VM_FRAME_KIND_VM: | |
85 | return 0; | |
86 | ||
87 | default: | |
88 | abort (); | |
89 | } | |
90 | } | |
91 | ||
92 | SCM* | |
93 | scm_i_frame_stack_base (SCM frame) | |
94 | #define FUNC_NAME "frame-stack-base" | |
95 | { | |
96 | SCM_VALIDATE_VM_FRAME (1, frame); | |
97 | ||
98 | return frame_stack_base (SCM_VM_FRAME_KIND (frame), | |
99 | SCM_VM_FRAME_DATA (frame)); | |
100 | } | |
89b235af AW |
101 | #undef FUNC_NAME |
102 | ||
89b235af AW |
103 | scm_t_ptrdiff |
104 | scm_i_frame_offset (SCM frame) | |
105 | #define FUNC_NAME "frame-offset" | |
106 | { | |
89b235af AW |
107 | SCM_VALIDATE_VM_FRAME (1, frame); |
108 | ||
44d97054 AW |
109 | return frame_offset (SCM_VM_FRAME_KIND (frame), |
110 | SCM_VM_FRAME_DATA (frame)); | |
5515edc5 | 111 | |
89b235af AW |
112 | } |
113 | #undef FUNC_NAME | |
114 | ||
3d94d862 | 115 | \f |
ac99cb0c KN |
116 | /* Scheme interface */ |
117 | ||
aa3f6951 | 118 | SCM_DEFINE (scm_frame_p, "frame?", 1, 0, 0, |
ac99cb0c KN |
119 | (SCM obj), |
120 | "") | |
aa3f6951 | 121 | #define FUNC_NAME s_scm_frame_p |
ac99cb0c | 122 | { |
5c8cefe5 | 123 | return scm_from_bool (SCM_VM_FRAME_P (obj)); |
b1b942b7 AW |
124 | } |
125 | #undef FUNC_NAME | |
126 | ||
3b14dd2f AW |
127 | /* Retrieve the local in slot 0, which may or may not actually be a |
128 | procedure, and may or may not actually be the procedure being | |
129 | applied. If you want the procedure, look it up from the IP. */ | |
130 | SCM | |
131 | scm_c_frame_closure (enum scm_vm_frame_kind kind, const struct scm_frame *frame) | |
132 | { | |
deb2df53 AW |
133 | SCM *fp, *sp; |
134 | ||
135 | fp = frame_stack_base (kind, frame) + frame->fp_offset; | |
136 | sp = frame_stack_base (kind, frame) + frame->sp_offset; | |
3b14dd2f | 137 | |
deb2df53 AW |
138 | if (SCM_FRAME_NUM_LOCALS (fp, sp) > 0) |
139 | return SCM_FRAME_LOCAL (fp, 0); | |
140 | ||
141 | return SCM_BOOL_F; | |
3b14dd2f AW |
142 | } |
143 | ||
aa3f6951 | 144 | SCM_DEFINE (scm_frame_procedure, "frame-procedure", 1, 0, 0, |
b1b942b7 AW |
145 | (SCM frame), |
146 | "") | |
aa3f6951 | 147 | #define FUNC_NAME s_scm_frame_procedure |
b1b942b7 AW |
148 | { |
149 | SCM_VALIDATE_VM_FRAME (1, frame); | |
3b14dd2f AW |
150 | |
151 | /* FIXME: Retrieve procedure from address? */ | |
152 | return scm_c_frame_closure (SCM_VM_FRAME_KIND (frame), | |
153 | SCM_VM_FRAME_DATA (frame)); | |
b1b942b7 AW |
154 | } |
155 | #undef FUNC_NAME | |
156 | ||
60617d81 MW |
157 | static SCM frame_arguments_var; |
158 | ||
159 | static void | |
160 | init_frame_arguments_var (void) | |
161 | { | |
162 | frame_arguments_var | |
163 | = scm_c_private_lookup ("system vm frame", "frame-arguments"); | |
164 | } | |
165 | ||
aa3f6951 AW |
166 | SCM_DEFINE (scm_frame_arguments, "frame-arguments", 1, 0, 0, |
167 | (SCM frame), | |
168 | "") | |
169 | #define FUNC_NAME s_scm_frame_arguments | |
b1b942b7 | 170 | { |
60617d81 MW |
171 | static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT; |
172 | scm_i_pthread_once (&once, init_frame_arguments_var); | |
b1b942b7 | 173 | |
60617d81 | 174 | SCM_VALIDATE_VM_FRAME (1, frame); |
b1b942b7 | 175 | |
60617d81 | 176 | return scm_call_1 (scm_variable_ref (frame_arguments_var), frame); |
ac99cb0c KN |
177 | } |
178 | #undef FUNC_NAME | |
179 | ||
48192761 AW |
180 | static SCM frame_call_representation_var; |
181 | ||
182 | static void | |
183 | init_frame_call_representation_var (void) | |
184 | { | |
185 | frame_call_representation_var | |
186 | = scm_c_private_lookup ("system vm frame", "frame-call-representation"); | |
187 | } | |
188 | ||
189 | SCM scm_frame_call_representation (SCM frame) | |
190 | #define FUNC_NAME "frame-call-representation" | |
191 | { | |
192 | static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT; | |
193 | scm_i_pthread_once (&once, init_frame_call_representation_var); | |
194 | ||
195 | SCM_VALIDATE_VM_FRAME (1, frame); | |
196 | ||
197 | return scm_call_1 (scm_variable_ref (frame_call_representation_var), frame); | |
198 | } | |
199 | #undef FUNC_NAME | |
200 | ||
423fca76 AW |
201 | SCM_DEFINE (scm_frame_source, "frame-source", 1, 0, 0, |
202 | (SCM frame), | |
203 | "") | |
204 | #define FUNC_NAME s_scm_frame_source | |
ac99cb0c | 205 | { |
423fca76 | 206 | SCM_VALIDATE_VM_FRAME (1, frame); |
b1b942b7 | 207 | |
581a4eb8 | 208 | return scm_find_source_for_addr (scm_frame_instruction_pointer (frame)); |
ac99cb0c | 209 | } |
423fca76 | 210 | #undef FUNC_NAME |
ac99cb0c | 211 | |
aa3f6951 | 212 | SCM_DEFINE (scm_frame_num_locals, "frame-num-locals", 1, 0, 0, |
6c6a4439 AW |
213 | (SCM frame), |
214 | "") | |
aa3f6951 | 215 | #define FUNC_NAME s_scm_frame_num_locals |
6c6a4439 | 216 | { |
b636cdb0 | 217 | SCM *fp, *sp; |
6c6a4439 AW |
218 | |
219 | SCM_VALIDATE_VM_FRAME (1, frame); | |
220 | ||
b636cdb0 | 221 | fp = SCM_VM_FRAME_FP (frame); |
510ca126 | 222 | sp = SCM_VM_FRAME_SP (frame); |
510ca126 | 223 | |
b636cdb0 | 224 | return scm_from_ptrdiff_t (SCM_FRAME_NUM_LOCALS (fp, sp)); |
6c6a4439 AW |
225 | } |
226 | #undef FUNC_NAME | |
227 | ||
aa3f6951 | 228 | SCM_DEFINE (scm_frame_local_ref, "frame-local-ref", 2, 0, 0, |
af988bbf | 229 | (SCM frame, SCM index), |
ac99cb0c | 230 | "") |
aa3f6951 | 231 | #define FUNC_NAME s_scm_frame_local_ref |
ac99cb0c | 232 | { |
b636cdb0 | 233 | SCM *fp, *sp; |
b1b942b7 | 234 | unsigned int i; |
b1b942b7 | 235 | |
6c6a4439 | 236 | SCM_VALIDATE_VM_FRAME (1, frame); |
b1b942b7 | 237 | SCM_VALIDATE_UINT_COPY (2, index, i); |
b1b942b7 | 238 | |
b636cdb0 | 239 | fp = SCM_VM_FRAME_FP (frame); |
6c6a4439 | 240 | sp = SCM_VM_FRAME_SP (frame); |
f8085163 | 241 | |
b636cdb0 AW |
242 | if (i < SCM_FRAME_NUM_LOCALS (fp, sp)) |
243 | return SCM_FRAME_LOCAL (fp, i); | |
f8085163 | 244 | |
6c6a4439 | 245 | SCM_OUT_OF_RANGE (SCM_ARG2, index); |
af988bbf KN |
246 | } |
247 | #undef FUNC_NAME | |
ac99cb0c | 248 | |
aa3f6951 AW |
249 | /* Need same not-yet-active frame logic here as in frame-num-locals */ |
250 | SCM_DEFINE (scm_frame_local_set_x, "frame-local-set!", 3, 0, 0, | |
af988bbf KN |
251 | (SCM frame, SCM index, SCM val), |
252 | "") | |
aa3f6951 | 253 | #define FUNC_NAME s_scm_frame_local_set_x |
af988bbf | 254 | { |
b636cdb0 | 255 | SCM *fp, *sp; |
b1b942b7 | 256 | unsigned int i; |
b1b942b7 | 257 | |
6c6a4439 | 258 | SCM_VALIDATE_VM_FRAME (1, frame); |
b1b942b7 | 259 | SCM_VALIDATE_UINT_COPY (2, index, i); |
b1b942b7 | 260 | |
b636cdb0 | 261 | fp = SCM_VM_FRAME_FP (frame); |
6c6a4439 | 262 | sp = SCM_VM_FRAME_SP (frame); |
f8085163 | 263 | |
b636cdb0 | 264 | if (i < SCM_FRAME_NUM_LOCALS (fp, sp)) |
6c6a4439 | 265 | { |
b636cdb0 | 266 | SCM_FRAME_LOCAL (fp, i) = val; |
f8085163 | 267 | return SCM_UNSPECIFIED; |
6c6a4439 | 268 | } |
f8085163 | 269 | |
6c6a4439 AW |
270 | SCM_OUT_OF_RANGE (SCM_ARG2, index); |
271 | } | |
272 | #undef FUNC_NAME | |
b1b942b7 | 273 | |
2e30f398 AW |
274 | SCM_DEFINE (scm_frame_address, "frame-address", 1, 0, 0, |
275 | (SCM frame), | |
276 | "Return the frame pointer for @var{frame}.") | |
277 | #define FUNC_NAME s_scm_frame_address | |
278 | { | |
279 | SCM_VALIDATE_VM_FRAME (1, frame); | |
7c080187 | 280 | return scm_from_ptrdiff_t (SCM_VM_FRAME_FP_OFFSET (frame)); |
2e30f398 AW |
281 | } |
282 | #undef FUNC_NAME | |
283 | ||
542f975e AW |
284 | SCM_DEFINE (scm_frame_stack_pointer, "frame-stack-pointer", 1, 0, 0, |
285 | (SCM frame), | |
286 | "") | |
287 | #define FUNC_NAME s_scm_frame_stack_pointer | |
288 | { | |
289 | SCM_VALIDATE_VM_FRAME (1, frame); | |
290 | ||
7c080187 | 291 | return scm_from_ptrdiff_t (SCM_VM_FRAME_SP_OFFSET (frame)); |
542f975e AW |
292 | } |
293 | #undef FUNC_NAME | |
294 | ||
aa3f6951 | 295 | SCM_DEFINE (scm_frame_instruction_pointer, "frame-instruction-pointer", 1, 0, 0, |
6c6a4439 AW |
296 | (SCM frame), |
297 | "") | |
aa3f6951 | 298 | #define FUNC_NAME s_scm_frame_instruction_pointer |
6c6a4439 AW |
299 | { |
300 | SCM_VALIDATE_VM_FRAME (1, frame); | |
67b699cc | 301 | |
581a4eb8 | 302 | return scm_from_uintptr_t ((scm_t_uintptr) SCM_VM_FRAME_IP (frame)); |
ac99cb0c KN |
303 | } |
304 | #undef FUNC_NAME | |
305 | ||
aa3f6951 | 306 | SCM_DEFINE (scm_frame_return_address, "frame-return-address", 1, 0, 0, |
ac99cb0c KN |
307 | (SCM frame), |
308 | "") | |
aa3f6951 | 309 | #define FUNC_NAME s_scm_frame_return_address |
ac99cb0c | 310 | { |
b1b942b7 | 311 | SCM_VALIDATE_VM_FRAME (1, frame); |
72b82b0f AW |
312 | return scm_from_uintptr_t ((scm_t_uintptr) (SCM_FRAME_RETURN_ADDRESS |
313 | (SCM_VM_FRAME_FP (frame)))); | |
ac99cb0c KN |
314 | } |
315 | #undef FUNC_NAME | |
316 | ||
44d97054 AW |
317 | #define RELOC(kind, frame, val) \ |
318 | (((SCM *) (val)) + frame_offset (kind, frame)) | |
319 | ||
aa3f6951 | 320 | SCM_DEFINE (scm_frame_dynamic_link, "frame-dynamic-link", 1, 0, 0, |
ac99cb0c KN |
321 | (SCM frame), |
322 | "") | |
aa3f6951 | 323 | #define FUNC_NAME s_scm_frame_dynamic_link |
ac99cb0c | 324 | { |
b1b942b7 AW |
325 | SCM_VALIDATE_VM_FRAME (1, frame); |
326 | /* fixme: munge fp if holder is a continuation */ | |
72b82b0f AW |
327 | return scm_from_uintptr_t |
328 | ((scm_t_uintptr) | |
44d97054 | 329 | RELOC (SCM_VM_FRAME_KIND (frame), SCM_VM_FRAME_DATA (frame), |
b1b942b7 | 330 | SCM_FRAME_DYNAMIC_LINK (SCM_VM_FRAME_FP (frame)))); |
ac99cb0c KN |
331 | } |
332 | #undef FUNC_NAME | |
333 | ||
44d97054 AW |
334 | int |
335 | scm_c_frame_previous (enum scm_vm_frame_kind kind, struct scm_frame *frame) | |
b1b942b7 AW |
336 | { |
337 | SCM *this_fp, *new_fp, *new_sp; | |
deb2df53 | 338 | SCM *stack_base = frame_stack_base (kind, frame); |
93dbc31b | 339 | |
93dbc31b | 340 | again: |
deb2df53 AW |
341 | this_fp = frame->fp_offset + stack_base; |
342 | ||
343 | if (this_fp == stack_base) | |
344 | return 0; | |
345 | ||
b1b942b7 | 346 | new_fp = SCM_FRAME_DYNAMIC_LINK (this_fp); |
deb2df53 AW |
347 | |
348 | if (!new_fp) | |
349 | return 0; | |
350 | ||
351 | new_fp = RELOC (kind, frame, new_fp); | |
352 | ||
353 | if (new_fp < stack_base) | |
44d97054 | 354 | return 0; |
deb2df53 AW |
355 | |
356 | new_sp = SCM_FRAME_PREVIOUS_SP (this_fp); | |
357 | frame->fp_offset = new_fp - stack_base; | |
358 | frame->sp_offset = new_sp - stack_base; | |
359 | frame->ip = SCM_FRAME_RETURN_ADDRESS (this_fp); | |
360 | ||
361 | { | |
362 | SCM proc = scm_c_frame_closure (kind, frame); | |
363 | if (SCM_PROGRAM_P (proc) && SCM_PROGRAM_IS_BOOT (proc)) | |
364 | goto again; | |
365 | } | |
366 | ||
367 | return 1; | |
44d97054 AW |
368 | } |
369 | ||
370 | SCM_DEFINE (scm_frame_previous, "frame-previous", 1, 0, 0, | |
371 | (SCM frame), | |
372 | "") | |
373 | #define FUNC_NAME s_scm_frame_previous | |
374 | { | |
375 | enum scm_vm_frame_kind kind; | |
376 | struct scm_frame tmp; | |
377 | ||
378 | SCM_VALIDATE_VM_FRAME (1, frame); | |
379 | ||
380 | kind = SCM_VM_FRAME_KIND (frame); | |
381 | memcpy (&tmp, SCM_VM_FRAME_DATA (frame), sizeof tmp); | |
382 | ||
383 | if (!scm_c_frame_previous (SCM_VM_FRAME_KIND (frame), &tmp)) | |
b1b942b7 | 384 | return SCM_BOOL_F; |
44d97054 | 385 | |
8de051da | 386 | return scm_c_make_frame (kind, &tmp); |
b1b942b7 | 387 | } |
93dbc31b | 388 | #undef FUNC_NAME |
b1b942b7 | 389 | |
ac99cb0c | 390 | \f |
07e56b27 AW |
391 | void |
392 | scm_init_frames (void) | |
393 | { | |
ac99cb0c | 394 | #ifndef SCM_MAGIC_SNARFER |
aeeff258 | 395 | #include "libguile/frames.x" |
ac99cb0c KN |
396 | #endif |
397 | } | |
398 | ||
399 | /* | |
400 | Local Variables: | |
401 | c-file-style: "gnu" | |
402 | End: | |
403 | */ |