Fix thread-unsafe lazy initializations.
[bpt/guile.git] / libguile / frames.c
CommitLineData
0fc9040f 1/* Copyright (C) 2001, 2009, 2010, 2011, 2012 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 *));
31verify (sizeof (struct scm_vm_frame) == 5 * sizeof (SCM));
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{
aa3f6951 55 scm_puts ("#<frame ", port);
2f9769b6
AW
56 scm_uintprint (SCM_UNPACK (frame), 16, port);
57 scm_putc (' ', port);
aa3f6951 58 scm_write (scm_frame_procedure (frame), port);
2f9769b6
AW
59 /* don't write args, they can get us into trouble. */
60 scm_puts (">", 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
60617d81
MW
85static SCM frame_arguments_var;
86
87static void
88init_frame_arguments_var (void)
89{
90 frame_arguments_var
91 = scm_c_private_lookup ("system vm frame", "frame-arguments");
92}
93
aa3f6951
AW
94SCM_DEFINE (scm_frame_arguments, "frame-arguments", 1, 0, 0,
95 (SCM frame),
96 "")
97#define FUNC_NAME s_scm_frame_arguments
b1b942b7 98{
60617d81
MW
99 static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
100 scm_i_pthread_once (&once, init_frame_arguments_var);
b1b942b7 101
60617d81 102 SCM_VALIDATE_VM_FRAME (1, frame);
b1b942b7 103
60617d81 104 return scm_call_1 (scm_variable_ref (frame_arguments_var), frame);
ac99cb0c
KN
105}
106#undef FUNC_NAME
107
423fca76
AW
108SCM_DEFINE (scm_frame_source, "frame-source", 1, 0, 0,
109 (SCM frame),
110 "")
111#define FUNC_NAME s_scm_frame_source
ac99cb0c 112{
da874e54
AW
113 SCM proc;
114
423fca76 115 SCM_VALIDATE_VM_FRAME (1, frame);
b1b942b7 116
da874e54
AW
117 proc = scm_frame_procedure (frame);
118
119 if (SCM_PROGRAM_P (proc))
120 return scm_program_source (scm_frame_procedure (frame),
121 scm_frame_instruction_pointer (frame),
122 SCM_UNDEFINED);
123
124 return SCM_BOOL_F;
ac99cb0c 125}
423fca76 126#undef FUNC_NAME
ac99cb0c 127
6c6a4439
AW
128/* The number of locals would be a simple thing to compute, if it weren't for
129 the presence of not-yet-active frames on the stack. So we have a cheap
130 heuristic to detect not-yet-active frames, and skip over them. Perhaps we
131 should represent them more usefully.
aa3f6951
AW
132*/
133SCM_DEFINE (scm_frame_num_locals, "frame-num-locals", 1, 0, 0,
6c6a4439
AW
134 (SCM frame),
135 "")
aa3f6951 136#define FUNC_NAME s_scm_frame_num_locals
6c6a4439
AW
137{
138 SCM *sp, *p;
139 unsigned int n = 0;
140
141 SCM_VALIDATE_VM_FRAME (1, frame);
142
143 sp = SCM_VM_FRAME_SP (frame);
144 p = SCM_FRAME_STACK_ADDRESS (SCM_VM_FRAME_FP (frame));
145 while (p <= sp)
146 {
b2b33168 147 if (SCM_UNPACK (p[0]) == 0)
6c6a4439
AW
148 /* skip over not-yet-active frame */
149 p += 3;
150 else
151 {
152 p++;
153 n++;
154 }
155 }
156 return scm_from_uint (n);
157}
158#undef FUNC_NAME
159
aa3f6951
AW
160/* Need same not-yet-active frame logic here as in frame-num-locals */
161SCM_DEFINE (scm_frame_local_ref, "frame-local-ref", 2, 0, 0,
af988bbf 162 (SCM frame, SCM index),
ac99cb0c 163 "")
aa3f6951 164#define FUNC_NAME s_scm_frame_local_ref
ac99cb0c 165{
6c6a4439
AW
166 SCM *sp, *p;
167 unsigned int n = 0;
b1b942b7 168 unsigned int i;
b1b942b7 169
6c6a4439 170 SCM_VALIDATE_VM_FRAME (1, frame);
b1b942b7 171 SCM_VALIDATE_UINT_COPY (2, index, i);
b1b942b7 172
6c6a4439
AW
173 sp = SCM_VM_FRAME_SP (frame);
174 p = SCM_FRAME_STACK_ADDRESS (SCM_VM_FRAME_FP (frame));
175 while (p <= sp)
176 {
b2b33168 177 if (SCM_UNPACK (p[0]) == 0)
6c6a4439
AW
178 /* skip over not-yet-active frame */
179 p += 3;
180 else if (n == i)
181 return *p;
182 else
183 {
184 p++;
185 n++;
186 }
187 }
188 SCM_OUT_OF_RANGE (SCM_ARG2, index);
af988bbf
KN
189}
190#undef FUNC_NAME
ac99cb0c 191
aa3f6951
AW
192/* Need same not-yet-active frame logic here as in frame-num-locals */
193SCM_DEFINE (scm_frame_local_set_x, "frame-local-set!", 3, 0, 0,
af988bbf
KN
194 (SCM frame, SCM index, SCM val),
195 "")
aa3f6951 196#define FUNC_NAME s_scm_frame_local_set_x
af988bbf 197{
6c6a4439
AW
198 SCM *sp, *p;
199 unsigned int n = 0;
b1b942b7 200 unsigned int i;
b1b942b7 201
6c6a4439 202 SCM_VALIDATE_VM_FRAME (1, frame);
b1b942b7 203 SCM_VALIDATE_UINT_COPY (2, index, i);
b1b942b7 204
6c6a4439
AW
205 sp = SCM_VM_FRAME_SP (frame);
206 p = SCM_FRAME_STACK_ADDRESS (SCM_VM_FRAME_FP (frame));
207 while (p <= sp)
208 {
b2b33168 209 if (SCM_UNPACK (p[0]) == 0)
6c6a4439
AW
210 /* skip over not-yet-active frame */
211 p += 3;
212 else if (n == i)
213 {
214 *p = val;
215 return SCM_UNSPECIFIED;
216 }
217 else
218 {
219 p++;
220 n++;
221 }
222 }
223 SCM_OUT_OF_RANGE (SCM_ARG2, index);
224}
225#undef FUNC_NAME
b1b942b7 226
2e30f398
AW
227SCM_DEFINE (scm_frame_address, "frame-address", 1, 0, 0,
228 (SCM frame),
229 "Return the frame pointer for @var{frame}.")
230#define FUNC_NAME s_scm_frame_address
231{
232 SCM_VALIDATE_VM_FRAME (1, frame);
3d27ef4b 233 return scm_from_unsigned_integer ((scm_t_bits) SCM_VM_FRAME_FP (frame));
2e30f398
AW
234}
235#undef FUNC_NAME
236
542f975e
AW
237SCM_DEFINE (scm_frame_stack_pointer, "frame-stack-pointer", 1, 0, 0,
238 (SCM frame),
239 "")
240#define FUNC_NAME s_scm_frame_stack_pointer
241{
242 SCM_VALIDATE_VM_FRAME (1, frame);
243
3d27ef4b 244 return scm_from_unsigned_integer ((scm_t_bits) SCM_VM_FRAME_SP (frame));
542f975e
AW
245}
246#undef FUNC_NAME
247
aa3f6951 248SCM_DEFINE (scm_frame_instruction_pointer, "frame-instruction-pointer", 1, 0, 0,
6c6a4439
AW
249 (SCM frame),
250 "")
aa3f6951 251#define FUNC_NAME s_scm_frame_instruction_pointer
6c6a4439 252{
67b699cc 253 SCM program;
3dbbe28d
LC
254 const struct scm_objcode *c_objcode;
255
6c6a4439 256 SCM_VALIDATE_VM_FRAME (1, frame);
67b699cc 257 program = scm_frame_procedure (frame);
3dbbe28d 258
67b699cc
AW
259 if (!SCM_PROGRAM_P (program))
260 return SCM_INUM0;
261
262 c_objcode = SCM_PROGRAM_DATA (program);
3d27ef4b
AW
263 return scm_from_unsigned_integer ((SCM_VM_FRAME_IP (frame)
264 - SCM_C_OBJCODE_BASE (c_objcode)));
ac99cb0c
KN
265}
266#undef FUNC_NAME
267
aa3f6951 268SCM_DEFINE (scm_frame_return_address, "frame-return-address", 1, 0, 0,
ac99cb0c
KN
269 (SCM frame),
270 "")
aa3f6951 271#define FUNC_NAME s_scm_frame_return_address
ac99cb0c 272{
b1b942b7 273 SCM_VALIDATE_VM_FRAME (1, frame);
3d27ef4b
AW
274 return scm_from_unsigned_integer ((scm_t_bits)
275 (SCM_FRAME_RETURN_ADDRESS
276 (SCM_VM_FRAME_FP (frame))));
ac99cb0c
KN
277}
278#undef FUNC_NAME
279
aa3f6951 280SCM_DEFINE (scm_frame_mv_return_address, "frame-mv-return-address", 1, 0, 0,
da320011
AW
281 (SCM frame),
282 "")
aa3f6951 283#define FUNC_NAME s_scm_frame_mv_return_address
da320011 284{
b1b942b7 285 SCM_VALIDATE_VM_FRAME (1, frame);
3d27ef4b
AW
286 return scm_from_unsigned_integer ((scm_t_bits)
287 (SCM_FRAME_MV_RETURN_ADDRESS
288 (SCM_VM_FRAME_FP (frame))));
da320011
AW
289}
290#undef FUNC_NAME
291
aa3f6951 292SCM_DEFINE (scm_frame_dynamic_link, "frame-dynamic-link", 1, 0, 0,
ac99cb0c
KN
293 (SCM frame),
294 "")
aa3f6951 295#define FUNC_NAME s_scm_frame_dynamic_link
ac99cb0c 296{
b1b942b7
AW
297 SCM_VALIDATE_VM_FRAME (1, frame);
298 /* fixme: munge fp if holder is a continuation */
299 return scm_from_ulong
300 ((unsigned long)
301 RELOC (frame,
302 SCM_FRAME_DYNAMIC_LINK (SCM_VM_FRAME_FP (frame))));
ac99cb0c
KN
303}
304#undef FUNC_NAME
305
93dbc31b
AW
306SCM_DEFINE (scm_frame_previous, "frame-previous", 1, 0, 0,
307 (SCM frame),
308 "")
309#define FUNC_NAME s_scm_frame_previous
b1b942b7
AW
310{
311 SCM *this_fp, *new_fp, *new_sp;
da874e54 312 SCM proc;
93dbc31b
AW
313
314 SCM_VALIDATE_VM_FRAME (1, frame);
315
316 again:
b1b942b7
AW
317 this_fp = SCM_VM_FRAME_FP (frame);
318 new_fp = SCM_FRAME_DYNAMIC_LINK (this_fp);
319 if (new_fp)
da874e54
AW
320 {
321 new_fp = RELOC (frame, new_fp);
b1b942b7 322 new_sp = SCM_FRAME_LOWER_ADDRESS (this_fp) - 1;
93dbc31b
AW
323 frame = scm_c_make_frame (SCM_VM_FRAME_STACK_HOLDER (frame),
324 new_fp, new_sp,
325 SCM_FRAME_RETURN_ADDRESS (this_fp),
326 SCM_VM_FRAME_OFFSET (frame));
da874e54
AW
327 proc = scm_frame_procedure (frame);
328
329 if (SCM_PROGRAM_P (proc) && SCM_PROGRAM_IS_BOOT (proc))
93dbc31b
AW
330 goto again;
331 else
332 return frame;
b1b942b7
AW
333 }
334 else
335 return SCM_BOOL_F;
336}
93dbc31b 337#undef FUNC_NAME
b1b942b7 338
ac99cb0c 339\f
07e56b27
AW
340void
341scm_init_frames (void)
342{
ac99cb0c 343#ifndef SCM_MAGIC_SNARFER
aeeff258 344#include "libguile/frames.x"
ac99cb0c
KN
345#endif
346}
347
348/*
349 Local Variables:
350 c-file-style: "gnu"
351 End:
352*/