Zero-offset branches are backward branches; fix "br" backward branches
[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
60617d81
MW
134static SCM frame_arguments_var;
135
136static void
137init_frame_arguments_var (void)
138{
139 frame_arguments_var
140 = scm_c_private_lookup ("system vm frame", "frame-arguments");
141}
142
aa3f6951
AW
143SCM_DEFINE (scm_frame_arguments, "frame-arguments", 1, 0, 0,
144 (SCM frame),
145 "")
146#define FUNC_NAME s_scm_frame_arguments
b1b942b7 147{
60617d81
MW
148 static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
149 scm_i_pthread_once (&once, init_frame_arguments_var);
b1b942b7 150
60617d81 151 SCM_VALIDATE_VM_FRAME (1, frame);
b1b942b7 152
60617d81 153 return scm_call_1 (scm_variable_ref (frame_arguments_var), frame);
ac99cb0c
KN
154}
155#undef FUNC_NAME
156
423fca76
AW
157SCM_DEFINE (scm_frame_source, "frame-source", 1, 0, 0,
158 (SCM frame),
159 "")
160#define FUNC_NAME s_scm_frame_source
ac99cb0c 161{
423fca76 162 SCM_VALIDATE_VM_FRAME (1, frame);
b1b942b7 163
581a4eb8 164 return scm_find_source_for_addr (scm_frame_instruction_pointer (frame));
ac99cb0c 165}
423fca76 166#undef FUNC_NAME
ac99cb0c 167
aa3f6951 168SCM_DEFINE (scm_frame_num_locals, "frame-num-locals", 1, 0, 0,
6c6a4439
AW
169 (SCM frame),
170 "")
aa3f6951 171#define FUNC_NAME s_scm_frame_num_locals
6c6a4439 172{
b636cdb0 173 SCM *fp, *sp;
6c6a4439
AW
174
175 SCM_VALIDATE_VM_FRAME (1, frame);
176
b636cdb0 177 fp = SCM_VM_FRAME_FP (frame);
510ca126 178 sp = SCM_VM_FRAME_SP (frame);
510ca126 179
b636cdb0 180 return scm_from_ptrdiff_t (SCM_FRAME_NUM_LOCALS (fp, sp));
6c6a4439
AW
181}
182#undef FUNC_NAME
183
aa3f6951 184SCM_DEFINE (scm_frame_local_ref, "frame-local-ref", 2, 0, 0,
af988bbf 185 (SCM frame, SCM index),
ac99cb0c 186 "")
aa3f6951 187#define FUNC_NAME s_scm_frame_local_ref
ac99cb0c 188{
b636cdb0 189 SCM *fp, *sp;
b1b942b7 190 unsigned int i;
b1b942b7 191
6c6a4439 192 SCM_VALIDATE_VM_FRAME (1, frame);
b1b942b7 193 SCM_VALIDATE_UINT_COPY (2, index, i);
b1b942b7 194
b636cdb0 195 fp = SCM_VM_FRAME_FP (frame);
6c6a4439 196 sp = SCM_VM_FRAME_SP (frame);
f8085163 197
b636cdb0
AW
198 if (i < SCM_FRAME_NUM_LOCALS (fp, sp))
199 return SCM_FRAME_LOCAL (fp, i);
f8085163 200
6c6a4439 201 SCM_OUT_OF_RANGE (SCM_ARG2, index);
af988bbf
KN
202}
203#undef FUNC_NAME
ac99cb0c 204
aa3f6951
AW
205/* Need same not-yet-active frame logic here as in frame-num-locals */
206SCM_DEFINE (scm_frame_local_set_x, "frame-local-set!", 3, 0, 0,
af988bbf
KN
207 (SCM frame, SCM index, SCM val),
208 "")
aa3f6951 209#define FUNC_NAME s_scm_frame_local_set_x
af988bbf 210{
b636cdb0 211 SCM *fp, *sp;
b1b942b7 212 unsigned int i;
b1b942b7 213
6c6a4439 214 SCM_VALIDATE_VM_FRAME (1, frame);
b1b942b7 215 SCM_VALIDATE_UINT_COPY (2, index, i);
b1b942b7 216
b636cdb0 217 fp = SCM_VM_FRAME_FP (frame);
6c6a4439 218 sp = SCM_VM_FRAME_SP (frame);
f8085163 219
b636cdb0 220 if (i < SCM_FRAME_NUM_LOCALS (fp, sp))
6c6a4439 221 {
b636cdb0 222 SCM_FRAME_LOCAL (fp, i) = val;
f8085163 223 return SCM_UNSPECIFIED;
6c6a4439 224 }
f8085163 225
6c6a4439
AW
226 SCM_OUT_OF_RANGE (SCM_ARG2, index);
227}
228#undef FUNC_NAME
b1b942b7 229
2e30f398
AW
230SCM_DEFINE (scm_frame_address, "frame-address", 1, 0, 0,
231 (SCM frame),
232 "Return the frame pointer for @var{frame}.")
233#define FUNC_NAME s_scm_frame_address
234{
235 SCM_VALIDATE_VM_FRAME (1, frame);
72b82b0f 236 return scm_from_uintptr_t ((scm_t_uintptr) SCM_VM_FRAME_FP (frame));
2e30f398
AW
237}
238#undef FUNC_NAME
239
542f975e
AW
240SCM_DEFINE (scm_frame_stack_pointer, "frame-stack-pointer", 1, 0, 0,
241 (SCM frame),
242 "")
243#define FUNC_NAME s_scm_frame_stack_pointer
244{
245 SCM_VALIDATE_VM_FRAME (1, frame);
246
72b82b0f 247 return scm_from_uintptr_t ((scm_t_uintptr) SCM_VM_FRAME_SP (frame));
542f975e
AW
248}
249#undef FUNC_NAME
250
aa3f6951 251SCM_DEFINE (scm_frame_instruction_pointer, "frame-instruction-pointer", 1, 0, 0,
6c6a4439
AW
252 (SCM frame),
253 "")
aa3f6951 254#define FUNC_NAME s_scm_frame_instruction_pointer
6c6a4439
AW
255{
256 SCM_VALIDATE_VM_FRAME (1, frame);
67b699cc 257
581a4eb8 258 return scm_from_uintptr_t ((scm_t_uintptr) SCM_VM_FRAME_IP (frame));
ac99cb0c
KN
259}
260#undef FUNC_NAME
261
aa3f6951 262SCM_DEFINE (scm_frame_return_address, "frame-return-address", 1, 0, 0,
ac99cb0c
KN
263 (SCM frame),
264 "")
aa3f6951 265#define FUNC_NAME s_scm_frame_return_address
ac99cb0c 266{
b1b942b7 267 SCM_VALIDATE_VM_FRAME (1, frame);
72b82b0f
AW
268 return scm_from_uintptr_t ((scm_t_uintptr) (SCM_FRAME_RETURN_ADDRESS
269 (SCM_VM_FRAME_FP (frame))));
ac99cb0c
KN
270}
271#undef FUNC_NAME
272
aa3f6951 273SCM_DEFINE (scm_frame_dynamic_link, "frame-dynamic-link", 1, 0, 0,
ac99cb0c
KN
274 (SCM frame),
275 "")
aa3f6951 276#define FUNC_NAME s_scm_frame_dynamic_link
ac99cb0c 277{
b1b942b7
AW
278 SCM_VALIDATE_VM_FRAME (1, frame);
279 /* fixme: munge fp if holder is a continuation */
72b82b0f
AW
280 return scm_from_uintptr_t
281 ((scm_t_uintptr)
b1b942b7
AW
282 RELOC (frame,
283 SCM_FRAME_DYNAMIC_LINK (SCM_VM_FRAME_FP (frame))));
ac99cb0c
KN
284}
285#undef FUNC_NAME
286
93dbc31b
AW
287SCM_DEFINE (scm_frame_previous, "frame-previous", 1, 0, 0,
288 (SCM frame),
289 "")
290#define FUNC_NAME s_scm_frame_previous
b1b942b7
AW
291{
292 SCM *this_fp, *new_fp, *new_sp;
da874e54 293 SCM proc;
93dbc31b
AW
294
295 SCM_VALIDATE_VM_FRAME (1, frame);
296
297 again:
b1b942b7
AW
298 this_fp = SCM_VM_FRAME_FP (frame);
299 new_fp = SCM_FRAME_DYNAMIC_LINK (this_fp);
300 if (new_fp)
da874e54 301 {
89b235af 302 SCM *stack_base = scm_i_frame_stack_base (frame);
da874e54 303 new_fp = RELOC (frame, new_fp);
b636cdb0 304 new_sp = SCM_FRAME_PREVIOUS_SP (this_fp);
050a40db
AW
305 frame = scm_c_make_frame (SCM_VM_FRAME_KIND (frame),
306 SCM_VM_FRAME_STACK_HOLDER (frame),
89b235af
AW
307 new_fp - stack_base, new_sp - stack_base,
308 SCM_FRAME_RETURN_ADDRESS (this_fp));
da874e54
AW
309 proc = scm_frame_procedure (frame);
310
d798a895 311 if (SCM_PROGRAM_P (proc) && SCM_PROGRAM_IS_BOOT (proc))
93dbc31b
AW
312 goto again;
313 else
314 return frame;
b1b942b7
AW
315 }
316 else
317 return SCM_BOOL_F;
318}
93dbc31b 319#undef FUNC_NAME
b1b942b7 320
ac99cb0c 321\f
07e56b27
AW
322void
323scm_init_frames (void)
324{
ac99cb0c 325#ifndef SCM_MAGIC_SNARFER
aeeff258 326#include "libguile/frames.x"
ac99cb0c
KN
327#endif
328}
329
330/*
331 Local Variables:
332 c-file-style: "gnu"
333 End:
334*/