Merge commit 'origin/master' into vm
[bpt/guile.git] / libguile / programs.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 "instructions.h"
49 #include "modules.h"
50 #include "programs.h"
51 #include "vm.h"
52
53 \f
54 scm_t_bits scm_tc16_program;
55
56 static SCM zero_vector;
57 static SCM write_program = SCM_BOOL_F;
58
59 SCM
60 scm_c_make_program (void *addr, size_t size, SCM holder)
61 #define FUNC_NAME "scm_c_make_program"
62 {
63 struct scm_program *p = scm_gc_malloc (sizeof (struct scm_program),
64 "program");
65 p->size = size;
66 p->nargs = 0;
67 p->nrest = 0;
68 p->nlocs = 0;
69 p->nexts = 0;
70 p->meta = SCM_BOOL_F;
71 p->objs = zero_vector;
72 p->external = SCM_EOL;
73 p->holder = holder;
74 p->module = scm_current_module ();
75
76 /* If nobody holds bytecode's address, then allocate a new memory */
77 if (SCM_FALSEP (holder))
78 {
79 p->base = scm_gc_malloc (size, "program-base");
80 memcpy (p->base, addr, size);
81 }
82 else
83 p->base = addr;
84
85 SCM_RETURN_NEWSMOB (scm_tc16_program, p);
86 }
87 #undef FUNC_NAME
88
89 SCM
90 scm_c_make_closure (SCM program, SCM external)
91 {
92 SCM prog = scm_c_make_program (0, 0, program);
93 if (!SCM_PROGRAM_P (program))
94 abort ();
95 *SCM_PROGRAM_DATA (prog) = *SCM_PROGRAM_DATA (program);
96 SCM_PROGRAM_DATA (prog)->external = external;
97 return prog;
98 }
99
100 static SCM
101 program_mark (SCM obj)
102 {
103 struct scm_program *p = SCM_PROGRAM_DATA (obj);
104 scm_gc_mark (p->meta);
105 scm_gc_mark (p->objs);
106 scm_gc_mark (p->external);
107 scm_gc_mark (p->module);
108 return p->holder;
109 }
110
111 static scm_sizet
112 program_free (SCM obj)
113 {
114 struct scm_program *p = SCM_PROGRAM_DATA (obj);
115 scm_sizet size = (sizeof (struct scm_program));
116
117 if (SCM_FALSEP (p->holder))
118 scm_gc_free (p->base, p->size, "program-base");
119
120 scm_gc_free (p, size, "program");
121
122 return 0;
123 }
124
125 static SCM
126 program_apply (SCM program, SCM args)
127 {
128 return scm_vm_apply (scm_the_vm (), program, args);
129 }
130
131 static int
132 program_print (SCM program, SCM port, scm_print_state *pstate)
133 {
134 static int print_error = 0;
135
136 if (SCM_FALSEP (write_program) && scm_module_system_booted_p)
137 write_program = scm_module_local_variable
138 (scm_c_resolve_module ("system vm program"),
139 scm_from_locale_symbol ("write-program"));
140
141 if (SCM_FALSEP (write_program) || print_error)
142 return scm_smob_print (program, port, pstate);
143
144 print_error = 1;
145 scm_call_2 (SCM_VARIABLE_REF (write_program), program, port);
146 print_error = 0;
147 return 1;
148 }
149
150 \f
151 /*
152 * Scheme interface
153 */
154
155 SCM_DEFINE (scm_program_p, "program?", 1, 0, 0,
156 (SCM obj),
157 "")
158 #define FUNC_NAME s_scm_program_p
159 {
160 return SCM_BOOL (SCM_PROGRAM_P (obj));
161 }
162 #undef FUNC_NAME
163
164 SCM_DEFINE (scm_program_base, "program-base", 1, 0, 0,
165 (SCM program),
166 "")
167 #define FUNC_NAME s_scm_program_base
168 {
169 SCM_VALIDATE_PROGRAM (1, program);
170
171 return scm_from_ulong ((unsigned long) SCM_PROGRAM_DATA (program)->base);
172 }
173 #undef FUNC_NAME
174
175 SCM_DEFINE (scm_program_arity, "program-arity", 1, 0, 0,
176 (SCM program),
177 "")
178 #define FUNC_NAME s_scm_program_arity
179 {
180 struct scm_program *p;
181
182 SCM_VALIDATE_PROGRAM (1, program);
183
184 p = SCM_PROGRAM_DATA (program);
185 return SCM_LIST4 (SCM_I_MAKINUM (p->nargs),
186 SCM_I_MAKINUM (p->nrest),
187 SCM_I_MAKINUM (p->nlocs),
188 SCM_I_MAKINUM (p->nexts));
189 }
190 #undef FUNC_NAME
191
192 SCM_DEFINE (scm_program_meta, "program-meta", 1, 0, 0,
193 (SCM program),
194 "")
195 #define FUNC_NAME s_scm_program_meta
196 {
197 SCM_VALIDATE_PROGRAM (1, program);
198 return SCM_PROGRAM_DATA (program)->meta;
199 }
200 #undef FUNC_NAME
201
202 SCM_DEFINE (scm_program_objects, "program-objects", 1, 0, 0,
203 (SCM program),
204 "")
205 #define FUNC_NAME s_scm_program_objects
206 {
207 SCM_VALIDATE_PROGRAM (1, program);
208 return SCM_PROGRAM_DATA (program)->objs;
209 }
210 #undef FUNC_NAME
211
212 SCM_DEFINE (scm_program_module, "program-module", 1, 0, 0,
213 (SCM program),
214 "")
215 #define FUNC_NAME s_scm_program_module
216 {
217 SCM_VALIDATE_PROGRAM (1, program);
218 return SCM_PROGRAM_DATA (program)->module;
219 }
220 #undef FUNC_NAME
221
222 SCM_DEFINE (scm_program_external, "program-external", 1, 0, 0,
223 (SCM program),
224 "")
225 #define FUNC_NAME s_scm_program_external
226 {
227 SCM_VALIDATE_PROGRAM (1, program);
228 return SCM_PROGRAM_DATA (program)->external;
229 }
230 #undef FUNC_NAME
231
232 SCM_DEFINE (scm_program_external_set_x, "program-external-set!", 2, 0, 0,
233 (SCM program, SCM external),
234 "Modify the list of closure variables of @var{program} (for "
235 "debugging purposes).")
236 #define FUNC_NAME s_scm_program_external_set_x
237 {
238 SCM_VALIDATE_PROGRAM (1, program);
239 SCM_VALIDATE_LIST (2, external);
240 SCM_PROGRAM_DATA (program)->external = external;
241 return SCM_UNSPECIFIED;
242 }
243 #undef FUNC_NAME
244
245 SCM_DEFINE (scm_program_bytecode, "program-bytecode", 1, 0, 0,
246 (SCM program),
247 "Return a u8vector containing @var{program}'s bytecode.")
248 #define FUNC_NAME s_scm_program_bytecode
249 {
250 size_t size;
251 scm_t_uint8 *c_bytecode;
252
253 SCM_VALIDATE_PROGRAM (1, program);
254
255 size = SCM_PROGRAM_DATA (program)->size;
256 c_bytecode = malloc (size);
257 if (!c_bytecode)
258 return SCM_BOOL_F;
259
260 memcpy (c_bytecode, SCM_PROGRAM_DATA (program)->base, size);
261
262 return scm_take_u8vector (c_bytecode, size);
263 }
264 #undef FUNC_NAME
265
266
267 \f
268 void
269 scm_bootstrap_programs (void)
270 {
271 zero_vector = scm_permanent_object (scm_c_make_vector (0, SCM_BOOL_F));
272
273 scm_tc16_program = scm_make_smob_type ("program", 0);
274 scm_set_smob_mark (scm_tc16_program, program_mark);
275 scm_set_smob_free (scm_tc16_program, program_free);
276 scm_set_smob_apply (scm_tc16_program, program_apply, 0, 0, 1);
277 scm_set_smob_print (scm_tc16_program, program_print);
278 }
279
280 void
281 scm_init_programs (void)
282 {
283 scm_bootstrap_vm ();
284
285 #ifndef SCM_MAGIC_SNARFER
286 #include "programs.x"
287 #endif
288 }
289
290 /*
291 Local Variables:
292 c-file-style: "gnu"
293 End:
294 */