1 /* Copyright (C) 2001, 2009, 2010, 2011 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
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.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
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
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 #define RELOC(frame, val) (val + SCM_VM_FRAME_OFFSET (frame))
32 scm_c_make_frame (SCM stack_holder
, SCM
*fp
, SCM
*sp
,
33 scm_t_uint8
*ip
, scm_t_ptrdiff offset
)
35 struct scm_frame
*p
= scm_gc_malloc (sizeof (struct scm_frame
),
37 p
->stack_holder
= stack_holder
;
42 return scm_cell (scm_tc7_frame
, (scm_t_bits
)p
);
46 scm_i_frame_print (SCM frame
, SCM port
, scm_print_state
*pstate
)
48 scm_puts_unlocked ("#<frame ", port
);
49 scm_uintprint (SCM_UNPACK (frame
), 16, port
);
50 scm_putc_unlocked (' ', port
);
51 scm_write (scm_frame_procedure (frame
), port
);
52 /* don't write args, they can get us into trouble. */
53 scm_puts_unlocked (">", port
);
57 /* Scheme interface */
59 SCM_DEFINE (scm_frame_p
, "frame?", 1, 0, 0,
62 #define FUNC_NAME s_scm_frame_p
64 return scm_from_bool (SCM_VM_FRAME_P (obj
));
68 SCM_DEFINE (scm_frame_procedure
, "frame-procedure", 1, 0, 0,
71 #define FUNC_NAME s_scm_frame_procedure
73 SCM_VALIDATE_VM_FRAME (1, frame
);
74 return SCM_FRAME_PROGRAM (SCM_VM_FRAME_FP (frame
));
78 SCM_DEFINE (scm_frame_arguments
, "frame-arguments", 1, 0, 0,
81 #define FUNC_NAME s_scm_frame_arguments
83 static SCM var
= SCM_BOOL_F
;
85 SCM_VALIDATE_VM_FRAME (1, frame
);
87 if (scm_is_false (var
))
88 var
= scm_c_module_lookup (scm_c_resolve_module ("system vm frame"),
91 return scm_call_1 (SCM_VARIABLE_REF (var
), frame
);
95 SCM_DEFINE (scm_frame_source
, "frame-source", 1, 0, 0,
98 #define FUNC_NAME s_scm_frame_source
100 SCM_VALIDATE_VM_FRAME (1, frame
);
102 return scm_program_source (scm_frame_procedure (frame
),
103 scm_frame_instruction_pointer (frame
),
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.
113 SCM_DEFINE (scm_frame_num_locals
, "frame-num-locals", 1, 0, 0,
116 #define FUNC_NAME s_scm_frame_num_locals
121 SCM_VALIDATE_VM_FRAME (1, frame
);
123 sp
= SCM_VM_FRAME_SP (frame
);
124 p
= SCM_FRAME_STACK_ADDRESS (SCM_VM_FRAME_FP (frame
));
127 if (SCM_UNPACK (p
[0]) == 0)
128 /* skip over not-yet-active frame */
136 return scm_from_uint (n
);
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,
142 (SCM frame
, SCM index
),
144 #define FUNC_NAME s_scm_frame_local_ref
150 SCM_VALIDATE_VM_FRAME (1, frame
);
151 SCM_VALIDATE_UINT_COPY (2, index
, i
);
153 sp
= SCM_VM_FRAME_SP (frame
);
154 p
= SCM_FRAME_STACK_ADDRESS (SCM_VM_FRAME_FP (frame
));
157 if (SCM_UNPACK (p
[0]) == 0)
158 /* skip over not-yet-active frame */
168 SCM_OUT_OF_RANGE (SCM_ARG2
, index
);
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,
174 (SCM frame
, SCM index
, SCM val
),
176 #define FUNC_NAME s_scm_frame_local_set_x
182 SCM_VALIDATE_VM_FRAME (1, frame
);
183 SCM_VALIDATE_UINT_COPY (2, index
, i
);
185 sp
= SCM_VM_FRAME_SP (frame
);
186 p
= SCM_FRAME_STACK_ADDRESS (SCM_VM_FRAME_FP (frame
));
189 if (SCM_UNPACK (p
[0]) == 0)
190 /* skip over not-yet-active frame */
195 return SCM_UNSPECIFIED
;
203 SCM_OUT_OF_RANGE (SCM_ARG2
, index
);
207 SCM_DEFINE (scm_frame_address
, "frame-address", 1, 0, 0,
209 "Return the frame pointer for @var{frame}.")
210 #define FUNC_NAME s_scm_frame_address
212 SCM_VALIDATE_VM_FRAME (1, frame
);
213 return scm_from_unsigned_integer ((scm_t_bits
) SCM_VM_FRAME_FP (frame
));
217 SCM_DEFINE (scm_frame_stack_pointer
, "frame-stack-pointer", 1, 0, 0,
220 #define FUNC_NAME s_scm_frame_stack_pointer
222 SCM_VALIDATE_VM_FRAME (1, frame
);
224 return scm_from_unsigned_integer ((scm_t_bits
) SCM_VM_FRAME_SP (frame
));
228 SCM_DEFINE (scm_frame_instruction_pointer
, "frame-instruction-pointer", 1, 0, 0,
231 #define FUNC_NAME s_scm_frame_instruction_pointer
233 const struct scm_objcode
*c_objcode
;
235 SCM_VALIDATE_VM_FRAME (1, frame
);
237 c_objcode
= SCM_PROGRAM_DATA (scm_frame_procedure (frame
));
238 return scm_from_unsigned_integer ((SCM_VM_FRAME_IP (frame
)
239 - SCM_C_OBJCODE_BASE (c_objcode
)));
243 SCM_DEFINE (scm_frame_return_address
, "frame-return-address", 1, 0, 0,
246 #define FUNC_NAME s_scm_frame_return_address
248 SCM_VALIDATE_VM_FRAME (1, frame
);
249 return scm_from_unsigned_integer ((scm_t_bits
)
250 (SCM_FRAME_RETURN_ADDRESS
251 (SCM_VM_FRAME_FP (frame
))));
255 SCM_DEFINE (scm_frame_mv_return_address
, "frame-mv-return-address", 1, 0, 0,
258 #define FUNC_NAME s_scm_frame_mv_return_address
260 SCM_VALIDATE_VM_FRAME (1, frame
);
261 return scm_from_unsigned_integer ((scm_t_bits
)
262 (SCM_FRAME_MV_RETURN_ADDRESS
263 (SCM_VM_FRAME_FP (frame
))));
267 SCM_DEFINE (scm_frame_dynamic_link
, "frame-dynamic-link", 1, 0, 0,
270 #define FUNC_NAME s_scm_frame_dynamic_link
272 SCM_VALIDATE_VM_FRAME (1, frame
);
273 /* fixme: munge fp if holder is a continuation */
274 return scm_from_ulong
277 SCM_FRAME_DYNAMIC_LINK (SCM_VM_FRAME_FP (frame
))));
281 SCM_DEFINE (scm_frame_previous
, "frame-previous", 1, 0, 0,
284 #define FUNC_NAME s_scm_frame_previous
286 SCM
*this_fp
, *new_fp
, *new_sp
;
288 SCM_VALIDATE_VM_FRAME (1, frame
);
291 this_fp
= SCM_VM_FRAME_FP (frame
);
292 new_fp
= SCM_FRAME_DYNAMIC_LINK (this_fp
);
294 { new_fp
= RELOC (frame
, new_fp
);
295 new_sp
= SCM_FRAME_LOWER_ADDRESS (this_fp
) - 1;
296 frame
= scm_c_make_frame (SCM_VM_FRAME_STACK_HOLDER (frame
),
298 SCM_FRAME_RETURN_ADDRESS (this_fp
),
299 SCM_VM_FRAME_OFFSET (frame
));
300 if (SCM_PROGRAM_IS_BOOT (scm_frame_procedure (frame
)))
312 scm_init_frames (void)
314 #ifndef SCM_MAGIC_SNARFER
315 #include "libguile/frames.x"