Scheme frame objects hold relative stack offsets
[bpt/guile.git] / libguile / frames.c
CommitLineData
f8fb13ef 1/* Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013 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. */
31verify (sizeof (SCM) == sizeof (SCM *));
b636cdb0 32verify (sizeof (struct scm_vm_frame) == 3 * sizeof (SCM));
0fc9040f 33verify (offsetof (struct scm_vm_frame, dynamic_link) == 0);
ac99cb0c
KN
34
35\f
0fc9040f
LC
36#define RELOC(frame, val) \
37 (((SCM *) (val)) + SCM_VM_FRAME_OFFSET (frame))
ac99cb0c
KN
38
39SCM
89b235af
AW
40scm_c_make_frame (SCM stack_holder, scm_t_ptrdiff fp_offset,
41 scm_t_ptrdiff sp_offset, scm_t_uint32 *ip)
ac99cb0c 42{
aa3f6951
AW
43 struct scm_frame *p = scm_gc_malloc (sizeof (struct scm_frame),
44 "vmframe");
b1b942b7 45 p->stack_holder = stack_holder;
89b235af
AW
46 p->fp_offset = fp_offset;
47 p->sp_offset = sp_offset;
b1b942b7 48 p->ip = ip;
6f3b0cc2 49 return scm_cell (scm_tc7_frame, (scm_t_bits)p);
ac99cb0c
KN
50}
51
6f3b0cc2
AW
52void
53scm_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
89b235af
AW
63SCM*
64scm_i_frame_stack_base (SCM frame)
65#define FUNC_NAME "frame-stack-base"
66{
67 SCM stack_holder;
68
69 SCM_VALIDATE_VM_FRAME (1, frame);
70
71 stack_holder = SCM_VM_FRAME_STACK_HOLDER (frame);
72
73 if (SCM_VM_CONT_P (stack_holder))
74 return SCM_VM_CONT_DATA (stack_holder)->stack_base;
75
76 return SCM_VM_DATA (stack_holder)->stack_base;
77}
78#undef FUNC_NAME
79
80
81scm_t_ptrdiff
82scm_i_frame_offset (SCM frame)
83#define FUNC_NAME "frame-offset"
84{
85 SCM stack_holder;
86
87 SCM_VALIDATE_VM_FRAME (1, frame);
88
89 stack_holder = SCM_VM_FRAME_STACK_HOLDER (frame);
90
91 if (SCM_VM_CONT_P (stack_holder))
92 return SCM_VM_CONT_DATA (stack_holder)->reloc;
93
94 return 0;
95}
96#undef FUNC_NAME
97
3d94d862 98\f
ac99cb0c
KN
99/* Scheme interface */
100
aa3f6951 101SCM_DEFINE (scm_frame_p, "frame?", 1, 0, 0,
ac99cb0c
KN
102 (SCM obj),
103 "")
aa3f6951 104#define FUNC_NAME s_scm_frame_p
ac99cb0c 105{
5c8cefe5 106 return scm_from_bool (SCM_VM_FRAME_P (obj));
b1b942b7
AW
107}
108#undef FUNC_NAME
109
aa3f6951 110SCM_DEFINE (scm_frame_procedure, "frame-procedure", 1, 0, 0,
b1b942b7
AW
111 (SCM frame),
112 "")
aa3f6951 113#define FUNC_NAME s_scm_frame_procedure
b1b942b7
AW
114{
115 SCM_VALIDATE_VM_FRAME (1, frame);
116 return SCM_FRAME_PROGRAM (SCM_VM_FRAME_FP (frame));
117}
118#undef FUNC_NAME
119
aa3f6951
AW
120SCM_DEFINE (scm_frame_arguments, "frame-arguments", 1, 0, 0,
121 (SCM frame),
122 "")
123#define FUNC_NAME s_scm_frame_arguments
b1b942b7 124{
6c6a4439 125 static SCM var = SCM_BOOL_F;
b1b942b7
AW
126
127 SCM_VALIDATE_VM_FRAME (1, frame);
128
6c6a4439
AW
129 if (scm_is_false (var))
130 var = scm_c_module_lookup (scm_c_resolve_module ("system vm frame"),
aa3f6951 131 "frame-arguments");
b1b942b7 132
6c6a4439 133 return scm_call_1 (SCM_VARIABLE_REF (var), frame);
ac99cb0c
KN
134}
135#undef FUNC_NAME
136
423fca76
AW
137SCM_DEFINE (scm_frame_source, "frame-source", 1, 0, 0,
138 (SCM frame),
139 "")
140#define FUNC_NAME s_scm_frame_source
ac99cb0c 141{
423fca76 142 SCM_VALIDATE_VM_FRAME (1, frame);
b1b942b7 143
581a4eb8 144 return scm_find_source_for_addr (scm_frame_instruction_pointer (frame));
ac99cb0c 145}
423fca76 146#undef FUNC_NAME
ac99cb0c 147
aa3f6951 148SCM_DEFINE (scm_frame_num_locals, "frame-num-locals", 1, 0, 0,
6c6a4439
AW
149 (SCM frame),
150 "")
aa3f6951 151#define FUNC_NAME s_scm_frame_num_locals
6c6a4439 152{
b636cdb0 153 SCM *fp, *sp;
6c6a4439
AW
154
155 SCM_VALIDATE_VM_FRAME (1, frame);
156
b636cdb0 157 fp = SCM_VM_FRAME_FP (frame);
510ca126 158 sp = SCM_VM_FRAME_SP (frame);
510ca126 159
b636cdb0 160 return scm_from_ptrdiff_t (SCM_FRAME_NUM_LOCALS (fp, sp));
6c6a4439
AW
161}
162#undef FUNC_NAME
163
aa3f6951 164SCM_DEFINE (scm_frame_local_ref, "frame-local-ref", 2, 0, 0,
af988bbf 165 (SCM frame, SCM index),
ac99cb0c 166 "")
aa3f6951 167#define FUNC_NAME s_scm_frame_local_ref
ac99cb0c 168{
b636cdb0 169 SCM *fp, *sp;
b1b942b7 170 unsigned int i;
b1b942b7 171
6c6a4439 172 SCM_VALIDATE_VM_FRAME (1, frame);
b1b942b7 173 SCM_VALIDATE_UINT_COPY (2, index, i);
b1b942b7 174
b636cdb0 175 fp = SCM_VM_FRAME_FP (frame);
6c6a4439 176 sp = SCM_VM_FRAME_SP (frame);
f8085163 177
b636cdb0
AW
178 if (i < SCM_FRAME_NUM_LOCALS (fp, sp))
179 return SCM_FRAME_LOCAL (fp, i);
f8085163 180
6c6a4439 181 SCM_OUT_OF_RANGE (SCM_ARG2, index);
af988bbf
KN
182}
183#undef FUNC_NAME
ac99cb0c 184
aa3f6951
AW
185/* Need same not-yet-active frame logic here as in frame-num-locals */
186SCM_DEFINE (scm_frame_local_set_x, "frame-local-set!", 3, 0, 0,
af988bbf
KN
187 (SCM frame, SCM index, SCM val),
188 "")
aa3f6951 189#define FUNC_NAME s_scm_frame_local_set_x
af988bbf 190{
b636cdb0 191 SCM *fp, *sp;
b1b942b7 192 unsigned int i;
b1b942b7 193
6c6a4439 194 SCM_VALIDATE_VM_FRAME (1, frame);
b1b942b7 195 SCM_VALIDATE_UINT_COPY (2, index, i);
b1b942b7 196
b636cdb0 197 fp = SCM_VM_FRAME_FP (frame);
6c6a4439 198 sp = SCM_VM_FRAME_SP (frame);
f8085163 199
b636cdb0 200 if (i < SCM_FRAME_NUM_LOCALS (fp, sp))
6c6a4439 201 {
b636cdb0 202 SCM_FRAME_LOCAL (fp, i) = val;
f8085163 203 return SCM_UNSPECIFIED;
6c6a4439 204 }
f8085163 205
6c6a4439
AW
206 SCM_OUT_OF_RANGE (SCM_ARG2, index);
207}
208#undef FUNC_NAME
b1b942b7 209
2e30f398
AW
210SCM_DEFINE (scm_frame_address, "frame-address", 1, 0, 0,
211 (SCM frame),
212 "Return the frame pointer for @var{frame}.")
213#define FUNC_NAME s_scm_frame_address
214{
215 SCM_VALIDATE_VM_FRAME (1, frame);
72b82b0f 216 return scm_from_uintptr_t ((scm_t_uintptr) SCM_VM_FRAME_FP (frame));
2e30f398
AW
217}
218#undef FUNC_NAME
219
542f975e
AW
220SCM_DEFINE (scm_frame_stack_pointer, "frame-stack-pointer", 1, 0, 0,
221 (SCM frame),
222 "")
223#define FUNC_NAME s_scm_frame_stack_pointer
224{
225 SCM_VALIDATE_VM_FRAME (1, frame);
226
72b82b0f 227 return scm_from_uintptr_t ((scm_t_uintptr) SCM_VM_FRAME_SP (frame));
542f975e
AW
228}
229#undef FUNC_NAME
230
aa3f6951 231SCM_DEFINE (scm_frame_instruction_pointer, "frame-instruction-pointer", 1, 0, 0,
6c6a4439
AW
232 (SCM frame),
233 "")
aa3f6951 234#define FUNC_NAME s_scm_frame_instruction_pointer
6c6a4439
AW
235{
236 SCM_VALIDATE_VM_FRAME (1, frame);
67b699cc 237
581a4eb8 238 return scm_from_uintptr_t ((scm_t_uintptr) SCM_VM_FRAME_IP (frame));
ac99cb0c
KN
239}
240#undef FUNC_NAME
241
aa3f6951 242SCM_DEFINE (scm_frame_return_address, "frame-return-address", 1, 0, 0,
ac99cb0c
KN
243 (SCM frame),
244 "")
aa3f6951 245#define FUNC_NAME s_scm_frame_return_address
ac99cb0c 246{
b1b942b7 247 SCM_VALIDATE_VM_FRAME (1, frame);
72b82b0f
AW
248 return scm_from_uintptr_t ((scm_t_uintptr) (SCM_FRAME_RETURN_ADDRESS
249 (SCM_VM_FRAME_FP (frame))));
ac99cb0c
KN
250}
251#undef FUNC_NAME
252
aa3f6951 253SCM_DEFINE (scm_frame_dynamic_link, "frame-dynamic-link", 1, 0, 0,
ac99cb0c
KN
254 (SCM frame),
255 "")
aa3f6951 256#define FUNC_NAME s_scm_frame_dynamic_link
ac99cb0c 257{
b1b942b7
AW
258 SCM_VALIDATE_VM_FRAME (1, frame);
259 /* fixme: munge fp if holder is a continuation */
72b82b0f
AW
260 return scm_from_uintptr_t
261 ((scm_t_uintptr)
b1b942b7
AW
262 RELOC (frame,
263 SCM_FRAME_DYNAMIC_LINK (SCM_VM_FRAME_FP (frame))));
ac99cb0c
KN
264}
265#undef FUNC_NAME
266
93dbc31b
AW
267SCM_DEFINE (scm_frame_previous, "frame-previous", 1, 0, 0,
268 (SCM frame),
269 "")
270#define FUNC_NAME s_scm_frame_previous
b1b942b7
AW
271{
272 SCM *this_fp, *new_fp, *new_sp;
da874e54 273 SCM proc;
93dbc31b
AW
274
275 SCM_VALIDATE_VM_FRAME (1, frame);
276
277 again:
b1b942b7
AW
278 this_fp = SCM_VM_FRAME_FP (frame);
279 new_fp = SCM_FRAME_DYNAMIC_LINK (this_fp);
280 if (new_fp)
da874e54 281 {
89b235af 282 SCM *stack_base = scm_i_frame_stack_base (frame);
da874e54 283 new_fp = RELOC (frame, new_fp);
b636cdb0 284 new_sp = SCM_FRAME_PREVIOUS_SP (this_fp);
93dbc31b 285 frame = scm_c_make_frame (SCM_VM_FRAME_STACK_HOLDER (frame),
89b235af
AW
286 new_fp - stack_base, new_sp - stack_base,
287 SCM_FRAME_RETURN_ADDRESS (this_fp));
da874e54
AW
288 proc = scm_frame_procedure (frame);
289
d798a895 290 if (SCM_PROGRAM_P (proc) && SCM_PROGRAM_IS_BOOT (proc))
93dbc31b
AW
291 goto again;
292 else
293 return frame;
b1b942b7
AW
294 }
295 else
296 return SCM_BOOL_F;
297}
93dbc31b 298#undef FUNC_NAME
b1b942b7 299
ac99cb0c 300\f
07e56b27
AW
301void
302scm_init_frames (void)
303{
ac99cb0c 304#ifndef SCM_MAGIC_SNARFER
aeeff258 305#include "libguile/frames.x"
ac99cb0c
KN
306#endif
307}
308
309/*
310 Local Variables:
311 c-file-style: "gnu"
312 End:
313*/