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