1 /* Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013 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 /* 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
) == 3 * sizeof (SCM
));
32 verify (offsetof (struct scm_vm_frame
, dynamic_link
) == 0);
35 #define RELOC(frame, val) \
36 (((SCM *) (val)) + SCM_VM_FRAME_OFFSET (frame))
39 scm_c_make_frame (SCM stack_holder
, SCM
*fp
, SCM
*sp
,
40 scm_t_uint8
*ip
, scm_t_ptrdiff offset
)
42 struct scm_frame
*p
= scm_gc_malloc (sizeof (struct scm_frame
),
44 p
->stack_holder
= stack_holder
;
49 return scm_cell (scm_tc7_frame
, (scm_t_bits
)p
);
53 scm_i_frame_print (SCM frame
, SCM port
, scm_print_state
*pstate
)
55 scm_puts_unlocked ("#<frame ", port
);
56 scm_uintprint (SCM_UNPACK (frame
), 16, port
);
57 scm_putc_unlocked (' ', port
);
58 scm_write (scm_frame_procedure (frame
), port
);
59 /* don't write args, they can get us into trouble. */
60 scm_puts_unlocked (">", port
);
64 /* Scheme interface */
66 SCM_DEFINE (scm_frame_p
, "frame?", 1, 0, 0,
69 #define FUNC_NAME s_scm_frame_p
71 return scm_from_bool (SCM_VM_FRAME_P (obj
));
75 SCM_DEFINE (scm_frame_procedure
, "frame-procedure", 1, 0, 0,
78 #define FUNC_NAME s_scm_frame_procedure
80 SCM_VALIDATE_VM_FRAME (1, frame
);
81 return SCM_FRAME_PROGRAM (SCM_VM_FRAME_FP (frame
));
85 SCM_DEFINE (scm_frame_arguments
, "frame-arguments", 1, 0, 0,
88 #define FUNC_NAME s_scm_frame_arguments
90 static SCM var
= SCM_BOOL_F
;
92 SCM_VALIDATE_VM_FRAME (1, frame
);
94 if (scm_is_false (var
))
95 var
= scm_c_module_lookup (scm_c_resolve_module ("system vm frame"),
98 return scm_call_1 (SCM_VARIABLE_REF (var
), frame
);
102 SCM_DEFINE (scm_frame_source
, "frame-source", 1, 0, 0,
105 #define FUNC_NAME s_scm_frame_source
107 SCM_VALIDATE_VM_FRAME (1, frame
);
109 return scm_find_source_for_addr (scm_frame_instruction_pointer (frame
));
113 SCM_DEFINE (scm_frame_num_locals
, "frame-num-locals", 1, 0, 0,
116 #define FUNC_NAME s_scm_frame_num_locals
120 SCM_VALIDATE_VM_FRAME (1, frame
);
122 fp
= SCM_VM_FRAME_FP (frame
);
123 sp
= SCM_VM_FRAME_SP (frame
);
125 return scm_from_ptrdiff_t (SCM_FRAME_NUM_LOCALS (fp
, sp
));
129 SCM_DEFINE (scm_frame_local_ref
, "frame-local-ref", 2, 0, 0,
130 (SCM frame
, SCM index
),
132 #define FUNC_NAME s_scm_frame_local_ref
137 SCM_VALIDATE_VM_FRAME (1, frame
);
138 SCM_VALIDATE_UINT_COPY (2, index
, i
);
140 fp
= SCM_VM_FRAME_FP (frame
);
141 sp
= SCM_VM_FRAME_SP (frame
);
143 if (i
< SCM_FRAME_NUM_LOCALS (fp
, sp
))
144 return SCM_FRAME_LOCAL (fp
, i
);
146 SCM_OUT_OF_RANGE (SCM_ARG2
, index
);
150 /* Need same not-yet-active frame logic here as in frame-num-locals */
151 SCM_DEFINE (scm_frame_local_set_x
, "frame-local-set!", 3, 0, 0,
152 (SCM frame
, SCM index
, SCM val
),
154 #define FUNC_NAME s_scm_frame_local_set_x
159 SCM_VALIDATE_VM_FRAME (1, frame
);
160 SCM_VALIDATE_UINT_COPY (2, index
, i
);
162 fp
= SCM_VM_FRAME_FP (frame
);
163 sp
= SCM_VM_FRAME_SP (frame
);
165 if (i
< SCM_FRAME_NUM_LOCALS (fp
, sp
))
167 SCM_FRAME_LOCAL (fp
, i
) = val
;
168 return SCM_UNSPECIFIED
;
171 SCM_OUT_OF_RANGE (SCM_ARG2
, index
);
175 SCM_DEFINE (scm_frame_address
, "frame-address", 1, 0, 0,
177 "Return the frame pointer for @var{frame}.")
178 #define FUNC_NAME s_scm_frame_address
180 SCM_VALIDATE_VM_FRAME (1, frame
);
181 return scm_from_uintptr_t ((scm_t_uintptr
) SCM_VM_FRAME_FP (frame
));
185 SCM_DEFINE (scm_frame_stack_pointer
, "frame-stack-pointer", 1, 0, 0,
188 #define FUNC_NAME s_scm_frame_stack_pointer
190 SCM_VALIDATE_VM_FRAME (1, frame
);
192 return scm_from_uintptr_t ((scm_t_uintptr
) SCM_VM_FRAME_SP (frame
));
196 SCM_DEFINE (scm_frame_instruction_pointer
, "frame-instruction-pointer", 1, 0, 0,
199 #define FUNC_NAME s_scm_frame_instruction_pointer
201 SCM_VALIDATE_VM_FRAME (1, frame
);
203 return scm_from_uintptr_t ((scm_t_uintptr
) SCM_VM_FRAME_IP (frame
));
207 SCM_DEFINE (scm_frame_return_address
, "frame-return-address", 1, 0, 0,
210 #define FUNC_NAME s_scm_frame_return_address
212 SCM_VALIDATE_VM_FRAME (1, frame
);
213 return scm_from_uintptr_t ((scm_t_uintptr
) (SCM_FRAME_RETURN_ADDRESS
214 (SCM_VM_FRAME_FP (frame
))));
218 SCM_DEFINE (scm_frame_dynamic_link
, "frame-dynamic-link", 1, 0, 0,
221 #define FUNC_NAME s_scm_frame_dynamic_link
223 SCM_VALIDATE_VM_FRAME (1, frame
);
224 /* fixme: munge fp if holder is a continuation */
225 return scm_from_uintptr_t
228 SCM_FRAME_DYNAMIC_LINK (SCM_VM_FRAME_FP (frame
))));
232 SCM_DEFINE (scm_frame_previous
, "frame-previous", 1, 0, 0,
235 #define FUNC_NAME s_scm_frame_previous
237 SCM
*this_fp
, *new_fp
, *new_sp
;
240 SCM_VALIDATE_VM_FRAME (1, frame
);
243 this_fp
= SCM_VM_FRAME_FP (frame
);
244 new_fp
= SCM_FRAME_DYNAMIC_LINK (this_fp
);
247 new_fp
= RELOC (frame
, new_fp
);
248 new_sp
= SCM_FRAME_PREVIOUS_SP (this_fp
);
249 frame
= scm_c_make_frame (SCM_VM_FRAME_STACK_HOLDER (frame
),
251 SCM_FRAME_RETURN_ADDRESS (this_fp
),
252 SCM_VM_FRAME_OFFSET (frame
));
253 proc
= scm_frame_procedure (frame
);
255 if (SCM_RTL_PROGRAM_P (proc
) && SCM_PROGRAM_IS_BOOT (proc
))
267 scm_init_frames (void)
269 #ifndef SCM_MAGIC_SNARFER
270 #include "libguile/frames.x"