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