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