Merge commit 'origin/master' into vm
[bpt/guile.git] / libguile / frames.c
1 /* Copyright (C) 2001 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41
42 #if HAVE_CONFIG_H
43 # include <config.h>
44 #endif
45
46 #include <string.h>
47 #include "vm-bootstrap.h"
48 #include "frames.h"
49
50 \f
51 scm_t_bits scm_tc16_vm_frame;
52
53 #define RELOC(frame, val) (val + SCM_VM_FRAME_OFFSET (frame))
54
55 SCM
56 scm_c_make_vm_frame (SCM stack_holder, SCM *fp, SCM *sp,
57 scm_byte_t *ip, scm_t_ptrdiff offset)
58 {
59 struct scm_vm_frame *p = scm_gc_malloc (sizeof (struct scm_vm_frame),
60 "vmframe");
61 p->stack_holder = stack_holder;
62 p->fp = fp;
63 p->sp = sp;
64 p->ip = ip;
65 p->offset = offset;
66 SCM_RETURN_NEWSMOB (scm_tc16_vm_frame, p);
67 }
68
69 static SCM
70 vm_frame_mark (SCM obj)
71 {
72 return SCM_VM_FRAME_STACK_HOLDER (obj);
73 }
74
75 static scm_sizet
76 vm_frame_free (SCM obj)
77 {
78 struct scm_vm_frame *p = SCM_VM_FRAME_DATA (obj);
79 scm_gc_free (p, sizeof(struct scm_vm_frame), "vmframe");
80 return 0;
81 }
82
83 /* Scheme interface */
84
85 SCM_DEFINE (scm_vm_frame_p, "vm-frame?", 1, 0, 0,
86 (SCM obj),
87 "")
88 #define FUNC_NAME s_scm_vm_frame_p
89 {
90 return SCM_BOOL (SCM_VM_FRAME_P (obj));
91 }
92 #undef FUNC_NAME
93
94 SCM_DEFINE (scm_vm_frame_program, "vm-frame-program", 1, 0, 0,
95 (SCM frame),
96 "")
97 #define FUNC_NAME s_scm_vm_frame_program
98 {
99 SCM_VALIDATE_VM_FRAME (1, frame);
100 return SCM_FRAME_PROGRAM (SCM_VM_FRAME_FP (frame));
101 }
102 #undef FUNC_NAME
103
104 SCM_DEFINE (scm_vm_frame_arguments, "vm-frame-arguments", 1, 0, 0,
105 (SCM frame),
106 "")
107 #define FUNC_NAME s_scm_vm_frame_arguments
108 {
109 SCM *fp;
110 int i;
111 struct scm_program *bp;
112 SCM ret;
113
114 SCM_VALIDATE_VM_FRAME (1, frame);
115
116 fp = SCM_VM_FRAME_FP (frame);
117 bp = SCM_PROGRAM_DATA (SCM_FRAME_PROGRAM (fp));
118
119 if (!bp->nargs)
120 return SCM_EOL;
121 else if (bp->nrest)
122 ret = fp[bp->nargs - 1];
123 else
124 ret = scm_cons (fp[bp->nargs - 1], SCM_EOL);
125
126 for (i = bp->nargs - 2; i >= 0; i--)
127 ret = scm_cons (fp[i], ret);
128
129 return ret;
130 }
131 #undef FUNC_NAME
132
133 SCM_DEFINE (scm_vm_frame_source, "vm-frame-source", 1, 0, 0,
134 (SCM frame),
135 "")
136 #define FUNC_NAME s_scm_vm_frame_source
137 {
138 SCM *fp;
139 struct scm_program *bp;
140
141 SCM_VALIDATE_VM_FRAME (1, frame);
142
143 fp = SCM_VM_FRAME_FP (frame);
144 bp = SCM_PROGRAM_DATA (SCM_FRAME_PROGRAM (fp));
145
146 return scm_c_program_source (bp, SCM_VM_FRAME_IP (frame) - bp->base);
147 }
148 #undef FUNC_NAME
149
150 SCM_DEFINE (scm_vm_frame_local_ref, "vm-frame-local-ref", 2, 0, 0,
151 (SCM frame, SCM index),
152 "")
153 #define FUNC_NAME s_scm_vm_frame_local_ref
154 {
155 SCM *fp;
156 unsigned int i;
157 struct scm_program *bp;
158
159 SCM_VALIDATE_VM_FRAME (1, frame);
160
161 fp = SCM_VM_FRAME_FP (frame);
162 bp = SCM_PROGRAM_DATA (SCM_FRAME_PROGRAM (fp));
163
164 SCM_VALIDATE_UINT_COPY (2, index, i);
165 SCM_ASSERT_RANGE (2, index, i < bp->nargs + bp->nlocs);
166
167 return SCM_FRAME_VARIABLE (fp, i);
168 }
169 #undef FUNC_NAME
170
171 SCM_DEFINE (scm_vm_frame_local_set_x, "vm-frame-local-set!", 3, 0, 0,
172 (SCM frame, SCM index, SCM val),
173 "")
174 #define FUNC_NAME s_scm_vm_frame_local_set_x
175 {
176 SCM *fp;
177 unsigned int i;
178 struct scm_program *bp;
179
180 SCM_VALIDATE_VM_FRAME (1, frame);
181
182 fp = SCM_VM_FRAME_FP (frame);
183 bp = SCM_PROGRAM_DATA (SCM_FRAME_PROGRAM (fp));
184
185 SCM_VALIDATE_UINT_COPY (2, index, i);
186 SCM_ASSERT_RANGE (2, index, i < bp->nargs + bp->nlocs);
187
188 SCM_FRAME_VARIABLE (fp, i) = val;
189
190 return SCM_UNSPECIFIED;
191 }
192 #undef FUNC_NAME
193
194 SCM_DEFINE (scm_vm_frame_return_address, "vm-frame-return-address", 1, 0, 0,
195 (SCM frame),
196 "")
197 #define FUNC_NAME s_scm_vm_frame_return_address
198 {
199 SCM_VALIDATE_VM_FRAME (1, frame);
200 return scm_from_ulong ((unsigned long)
201 (SCM_FRAME_RETURN_ADDRESS
202 (SCM_VM_FRAME_FP (frame))));
203 }
204 #undef FUNC_NAME
205
206 SCM_DEFINE (scm_vm_frame_mv_return_address, "vm-frame-mv-return-address", 1, 0, 0,
207 (SCM frame),
208 "")
209 #define FUNC_NAME s_scm_vm_frame_mv_return_address
210 {
211 SCM_VALIDATE_VM_FRAME (1, frame);
212 return scm_from_ulong ((unsigned long)
213 (SCM_FRAME_MV_RETURN_ADDRESS
214 (SCM_VM_FRAME_FP (frame))));
215 }
216 #undef FUNC_NAME
217
218 SCM_DEFINE (scm_vm_frame_dynamic_link, "vm-frame-dynamic-link", 1, 0, 0,
219 (SCM frame),
220 "")
221 #define FUNC_NAME s_scm_vm_frame_dynamic_link
222 {
223 SCM_VALIDATE_VM_FRAME (1, frame);
224 /* fixme: munge fp if holder is a continuation */
225 return scm_from_ulong
226 ((unsigned long)
227 RELOC (frame,
228 SCM_FRAME_DYNAMIC_LINK (SCM_VM_FRAME_FP (frame))));
229 }
230 #undef FUNC_NAME
231
232 SCM_DEFINE (scm_vm_frame_external_link, "vm-frame-external-link", 1, 0, 0,
233 (SCM frame),
234 "")
235 #define FUNC_NAME s_scm_vm_frame_external_link
236 {
237 SCM_VALIDATE_VM_FRAME (1, frame);
238 return SCM_FRAME_EXTERNAL_LINK (SCM_VM_FRAME_FP (frame));
239 }
240 #undef FUNC_NAME
241
242 SCM_DEFINE (scm_vm_frame_stack, "vm-frame-stack", 1, 0, 0,
243 (SCM frame),
244 "")
245 #define FUNC_NAME s_scm_vm_frame_stack
246 {
247 SCM *top, *bottom, ret = SCM_EOL;
248
249 SCM_VALIDATE_VM_FRAME (1, frame);
250
251 top = SCM_VM_FRAME_SP (frame);
252 bottom = SCM_FRAME_UPPER_ADDRESS (SCM_VM_FRAME_FP (frame));
253 while (bottom <= top)
254 ret = scm_cons (*bottom++, ret);
255
256 return ret;
257 }
258 #undef FUNC_NAME
259
260 extern SCM
261 scm_c_vm_frame_prev (SCM frame)
262 {
263 SCM *this_fp, *new_fp, *new_sp;
264 this_fp = SCM_VM_FRAME_FP (frame);
265 new_fp = SCM_FRAME_DYNAMIC_LINK (this_fp);
266 if (new_fp)
267 { new_fp = RELOC (frame, new_fp);
268 new_sp = SCM_FRAME_LOWER_ADDRESS (this_fp) - 1;
269 return scm_c_make_vm_frame (SCM_VM_FRAME_STACK_HOLDER (frame),
270 new_fp, new_sp,
271 SCM_FRAME_RETURN_ADDRESS (this_fp),
272 SCM_VM_FRAME_OFFSET (frame));
273 }
274 else
275 return SCM_BOOL_F;
276 }
277
278 \f
279 void
280 scm_bootstrap_frames (void)
281 {
282 scm_tc16_vm_frame = scm_make_smob_type ("vm-frame", 0);
283 scm_set_smob_mark (scm_tc16_vm_frame, vm_frame_mark);
284 scm_set_smob_free (scm_tc16_vm_frame, vm_frame_free);
285 }
286
287 void
288 scm_init_frames (void)
289 {
290 scm_bootstrap_vm ();
291
292 #ifndef SCM_MAGIC_SNARFER
293 #include "frames.x"
294 #endif
295 }
296
297 /*
298 Local Variables:
299 c-file-style: "gnu"
300 End:
301 */