Fix thread-unsafe lazy initializations.
[bpt/guile.git] / libguile / frames.c
1 /* Copyright (C) 2001, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
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.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
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
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
17 */
18
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include "_scm.h"
26 #include "frames.h"
27 #include <verify.h>
28
29 /* Make sure assumptions on the layout of `struct scm_vm_frame' hold. */
30 verify (sizeof (SCM) == sizeof (SCM *));
31 verify (sizeof (struct scm_vm_frame) == 5 * sizeof (SCM));
32 verify (offsetof (struct scm_vm_frame, dynamic_link) == 0);
33
34 \f
35 #define RELOC(frame, val) \
36 (((SCM *) (val)) + SCM_VM_FRAME_OFFSET (frame))
37
38 SCM
39 scm_c_make_frame (SCM stack_holder, SCM *fp, SCM *sp,
40 scm_t_uint8 *ip, scm_t_ptrdiff offset)
41 {
42 struct scm_frame *p = scm_gc_malloc (sizeof (struct scm_frame),
43 "vmframe");
44 p->stack_holder = stack_holder;
45 p->fp = fp;
46 p->sp = sp;
47 p->ip = ip;
48 p->offset = offset;
49 return scm_cell (scm_tc7_frame, (scm_t_bits)p);
50 }
51
52 void
53 scm_i_frame_print (SCM frame, SCM port, scm_print_state *pstate)
54 {
55 scm_puts ("#<frame ", port);
56 scm_uintprint (SCM_UNPACK (frame), 16, port);
57 scm_putc (' ', port);
58 scm_write (scm_frame_procedure (frame), port);
59 /* don't write args, they can get us into trouble. */
60 scm_puts (">", port);
61 }
62
63 \f
64 /* Scheme interface */
65
66 SCM_DEFINE (scm_frame_p, "frame?", 1, 0, 0,
67 (SCM obj),
68 "")
69 #define FUNC_NAME s_scm_frame_p
70 {
71 return scm_from_bool (SCM_VM_FRAME_P (obj));
72 }
73 #undef FUNC_NAME
74
75 SCM_DEFINE (scm_frame_procedure, "frame-procedure", 1, 0, 0,
76 (SCM frame),
77 "")
78 #define FUNC_NAME s_scm_frame_procedure
79 {
80 SCM_VALIDATE_VM_FRAME (1, frame);
81 return SCM_FRAME_PROGRAM (SCM_VM_FRAME_FP (frame));
82 }
83 #undef FUNC_NAME
84
85 static SCM frame_arguments_var;
86
87 static void
88 init_frame_arguments_var (void)
89 {
90 frame_arguments_var
91 = scm_c_private_lookup ("system vm frame", "frame-arguments");
92 }
93
94 SCM_DEFINE (scm_frame_arguments, "frame-arguments", 1, 0, 0,
95 (SCM frame),
96 "")
97 #define FUNC_NAME s_scm_frame_arguments
98 {
99 static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
100 scm_i_pthread_once (&once, init_frame_arguments_var);
101
102 SCM_VALIDATE_VM_FRAME (1, frame);
103
104 return scm_call_1 (scm_variable_ref (frame_arguments_var), frame);
105 }
106 #undef FUNC_NAME
107
108 SCM_DEFINE (scm_frame_source, "frame-source", 1, 0, 0,
109 (SCM frame),
110 "")
111 #define FUNC_NAME s_scm_frame_source
112 {
113 SCM proc;
114
115 SCM_VALIDATE_VM_FRAME (1, frame);
116
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;
125 }
126 #undef FUNC_NAME
127
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.
132 */
133 SCM_DEFINE (scm_frame_num_locals, "frame-num-locals", 1, 0, 0,
134 (SCM frame),
135 "")
136 #define FUNC_NAME s_scm_frame_num_locals
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 {
147 if (SCM_UNPACK (p[0]) == 0)
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
160 /* Need same not-yet-active frame logic here as in frame-num-locals */
161 SCM_DEFINE (scm_frame_local_ref, "frame-local-ref", 2, 0, 0,
162 (SCM frame, SCM index),
163 "")
164 #define FUNC_NAME s_scm_frame_local_ref
165 {
166 SCM *sp, *p;
167 unsigned int n = 0;
168 unsigned int i;
169
170 SCM_VALIDATE_VM_FRAME (1, frame);
171 SCM_VALIDATE_UINT_COPY (2, index, i);
172
173 sp = SCM_VM_FRAME_SP (frame);
174 p = SCM_FRAME_STACK_ADDRESS (SCM_VM_FRAME_FP (frame));
175 while (p <= sp)
176 {
177 if (SCM_UNPACK (p[0]) == 0)
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);
189 }
190 #undef FUNC_NAME
191
192 /* Need same not-yet-active frame logic here as in frame-num-locals */
193 SCM_DEFINE (scm_frame_local_set_x, "frame-local-set!", 3, 0, 0,
194 (SCM frame, SCM index, SCM val),
195 "")
196 #define FUNC_NAME s_scm_frame_local_set_x
197 {
198 SCM *sp, *p;
199 unsigned int n = 0;
200 unsigned int i;
201
202 SCM_VALIDATE_VM_FRAME (1, frame);
203 SCM_VALIDATE_UINT_COPY (2, index, i);
204
205 sp = SCM_VM_FRAME_SP (frame);
206 p = SCM_FRAME_STACK_ADDRESS (SCM_VM_FRAME_FP (frame));
207 while (p <= sp)
208 {
209 if (SCM_UNPACK (p[0]) == 0)
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
226
227 SCM_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);
233 return scm_from_unsigned_integer ((scm_t_bits) SCM_VM_FRAME_FP (frame));
234 }
235 #undef FUNC_NAME
236
237 SCM_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
244 return scm_from_unsigned_integer ((scm_t_bits) SCM_VM_FRAME_SP (frame));
245 }
246 #undef FUNC_NAME
247
248 SCM_DEFINE (scm_frame_instruction_pointer, "frame-instruction-pointer", 1, 0, 0,
249 (SCM frame),
250 "")
251 #define FUNC_NAME s_scm_frame_instruction_pointer
252 {
253 SCM program;
254 const struct scm_objcode *c_objcode;
255
256 SCM_VALIDATE_VM_FRAME (1, frame);
257 program = scm_frame_procedure (frame);
258
259 if (!SCM_PROGRAM_P (program))
260 return SCM_INUM0;
261
262 c_objcode = SCM_PROGRAM_DATA (program);
263 return scm_from_unsigned_integer ((SCM_VM_FRAME_IP (frame)
264 - SCM_C_OBJCODE_BASE (c_objcode)));
265 }
266 #undef FUNC_NAME
267
268 SCM_DEFINE (scm_frame_return_address, "frame-return-address", 1, 0, 0,
269 (SCM frame),
270 "")
271 #define FUNC_NAME s_scm_frame_return_address
272 {
273 SCM_VALIDATE_VM_FRAME (1, frame);
274 return scm_from_unsigned_integer ((scm_t_bits)
275 (SCM_FRAME_RETURN_ADDRESS
276 (SCM_VM_FRAME_FP (frame))));
277 }
278 #undef FUNC_NAME
279
280 SCM_DEFINE (scm_frame_mv_return_address, "frame-mv-return-address", 1, 0, 0,
281 (SCM frame),
282 "")
283 #define FUNC_NAME s_scm_frame_mv_return_address
284 {
285 SCM_VALIDATE_VM_FRAME (1, frame);
286 return scm_from_unsigned_integer ((scm_t_bits)
287 (SCM_FRAME_MV_RETURN_ADDRESS
288 (SCM_VM_FRAME_FP (frame))));
289 }
290 #undef FUNC_NAME
291
292 SCM_DEFINE (scm_frame_dynamic_link, "frame-dynamic-link", 1, 0, 0,
293 (SCM frame),
294 "")
295 #define FUNC_NAME s_scm_frame_dynamic_link
296 {
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))));
303 }
304 #undef FUNC_NAME
305
306 SCM_DEFINE (scm_frame_previous, "frame-previous", 1, 0, 0,
307 (SCM frame),
308 "")
309 #define FUNC_NAME s_scm_frame_previous
310 {
311 SCM *this_fp, *new_fp, *new_sp;
312 SCM proc;
313
314 SCM_VALIDATE_VM_FRAME (1, frame);
315
316 again:
317 this_fp = SCM_VM_FRAME_FP (frame);
318 new_fp = SCM_FRAME_DYNAMIC_LINK (this_fp);
319 if (new_fp)
320 {
321 new_fp = RELOC (frame, new_fp);
322 new_sp = SCM_FRAME_LOWER_ADDRESS (this_fp) - 1;
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));
327 proc = scm_frame_procedure (frame);
328
329 if (SCM_PROGRAM_P (proc) && SCM_PROGRAM_IS_BOOT (proc))
330 goto again;
331 else
332 return frame;
333 }
334 else
335 return SCM_BOOL_F;
336 }
337 #undef FUNC_NAME
338
339 \f
340 void
341 scm_init_frames (void)
342 {
343 #ifndef SCM_MAGIC_SNARFER
344 #include "libguile/frames.x"
345 #endif
346 }
347
348 /*
349 Local Variables:
350 c-file-style: "gnu"
351 End:
352 */