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