programs have their own tc7 now
[bpt/guile.git] / libguile / programs.c
CommitLineData
20d47c39 1/* Copyright (C) 2001, 2009 Free Software Foundation, Inc.
17e90c5e 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.
17e90c5e 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.
17e90c5e 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 */
17e90c5e 18
13c47753
AW
19#if HAVE_CONFIG_H
20# include <config.h>
21#endif
22
17e90c5e 23#include <string.h>
560b9c25 24#include "_scm.h"
83495480 25#include "vm-bootstrap.h"
17e90c5e 26#include "instructions.h"
8e367074 27#include "modules.h"
17e90c5e 28#include "programs.h"
e311f5fa 29#include "procprop.h" // scm_sym_name
028e3d06 30#include "srcprop.h" // scm_sym_filename
17e90c5e
KN
31#include "vm.h"
32
33\f
e6fea618 34static SCM write_program = SCM_BOOL_F;
17e90c5e 35
53e28ed9 36SCM_DEFINE (scm_make_program, "make-program", 1, 2, 0,
57ab0671 37 (SCM objcode, SCM objtable, SCM free_variables),
53e28ed9
AW
38 "")
39#define FUNC_NAME s_scm_make_program
17e90c5e 40{
53e28ed9
AW
41 SCM_VALIDATE_OBJCODE (1, objcode);
42 if (SCM_UNLIKELY (SCM_UNBNDP (objtable)))
43 objtable = SCM_BOOL_F;
44 else if (scm_is_true (objtable))
45 SCM_VALIDATE_VECTOR (2, objtable);
57ab0671
AW
46 if (SCM_UNLIKELY (SCM_UNBNDP (free_variables)))
47 free_variables = SCM_BOOL_F;
48 else if (free_variables != SCM_BOOL_F)
49 SCM_VALIDATE_VECTOR (3, free_variables);
20d47c39 50
2fb924f6
AW
51 return scm_double_cell (scm_tc7_program, (scm_t_bits)objcode,
52 (scm_t_bits)objtable, (scm_t_bits)free_variables);
17e90c5e
KN
53}
54#undef FUNC_NAME
55
2fb924f6
AW
56void
57scm_i_program_print (SCM program, SCM port, scm_print_state *pstate)
e6fea618 58{
0ba8bb71
AW
59 static int print_error = 0;
60
28106f54 61 if (SCM_FALSEP (write_program) && scm_module_system_booted_p)
e6fea618
AW
62 write_program = scm_module_local_variable
63 (scm_c_resolve_module ("system vm program"),
64 scm_from_locale_symbol ("write-program"));
65
0ba8bb71 66 if (SCM_FALSEP (write_program) || print_error)
2fb924f6
AW
67 {
68 scm_puts ("#<program ", port);
69 scm_uintprint (SCM_CELL_WORD_1 (program), 16, port);
70 scm_putc ('>', port);
71 }
72 else
73 {
74 print_error = 1;
75 scm_call_2 (SCM_VARIABLE_REF (write_program), program, port);
76 print_error = 0;
77 }
e6fea618
AW
78}
79
17e90c5e
KN
80\f
81/*
82 * Scheme interface
83 */
84
85SCM_DEFINE (scm_program_p, "program?", 1, 0, 0,
86 (SCM obj),
87 "")
88#define FUNC_NAME s_scm_program_p
89{
90 return SCM_BOOL (SCM_PROGRAM_P (obj));
91}
92#undef FUNC_NAME
93
ac99cb0c
KN
94SCM_DEFINE (scm_program_base, "program-base", 1, 0, 0,
95 (SCM program),
96 "")
97#define FUNC_NAME s_scm_program_base
98{
99 SCM_VALIDATE_PROGRAM (1, program);
100
f41cb00c 101 return scm_from_ulong ((unsigned long) SCM_PROGRAM_DATA (program)->base);
ac99cb0c
KN
102}
103#undef FUNC_NAME
104
17e90c5e
KN
105SCM_DEFINE (scm_program_arity, "program-arity", 1, 0, 0,
106 (SCM program),
107 "")
108#define FUNC_NAME s_scm_program_arity
ac99cb0c 109{
53e28ed9 110 struct scm_objcode *p;
ac99cb0c
KN
111
112 SCM_VALIDATE_PROGRAM (1, program);
113
114 p = SCM_PROGRAM_DATA (program);
20d47c39 115 return scm_list_3 (SCM_I_MAKINUM (p->nargs),
da8b4747 116 SCM_I_MAKINUM (p->nrest),
20d47c39 117 SCM_I_MAKINUM (p->nlocs));
ac99cb0c
KN
118}
119#undef FUNC_NAME
120
53e28ed9
AW
121SCM_DEFINE (scm_program_objects, "program-objects", 1, 0, 0,
122 (SCM program),
123 "")
124#define FUNC_NAME s_scm_program_objects
125{
126 SCM_VALIDATE_PROGRAM (1, program);
127 return SCM_PROGRAM_OBJTABLE (program);
128}
129#undef FUNC_NAME
130
131SCM_DEFINE (scm_program_module, "program-module", 1, 0, 0,
132 (SCM program),
133 "")
134#define FUNC_NAME s_scm_program_module
135{
136 SCM objs;
137 SCM_VALIDATE_PROGRAM (1, program);
138 objs = SCM_PROGRAM_OBJTABLE (program);
139 return scm_is_true (objs) ? scm_c_vector_ref (objs, 0) : SCM_BOOL_F;
140}
141#undef FUNC_NAME
142
ac99cb0c
KN
143SCM_DEFINE (scm_program_meta, "program-meta", 1, 0, 0,
144 (SCM program),
145 "")
146#define FUNC_NAME s_scm_program_meta
17e90c5e 147{
ac47d5f6
AW
148 SCM metaobj;
149
17e90c5e 150 SCM_VALIDATE_PROGRAM (1, program);
ac47d5f6
AW
151
152 metaobj = scm_objcode_meta (SCM_PROGRAM_OBJCODE (program));
153 if (scm_is_true (metaobj))
20d47c39 154 return scm_make_program (metaobj, SCM_BOOL_F, SCM_BOOL_F);
ac47d5f6
AW
155 else
156 return SCM_BOOL_F;
17e90c5e
KN
157}
158#undef FUNC_NAME
159
e311f5fa
AW
160SCM_DEFINE (scm_program_bindings, "program-bindings", 1, 0, 0,
161 (SCM program),
162 "")
163#define FUNC_NAME s_scm_program_bindings
9a9f6487 164{
e311f5fa
AW
165 SCM meta;
166
167 SCM_VALIDATE_PROGRAM (1, program);
9a9f6487 168
53e28ed9 169 meta = scm_program_meta (program);
2fda0242 170 if (scm_is_false (meta))
9a9f6487 171 return SCM_BOOL_F;
e311f5fa
AW
172
173 return scm_car (scm_call_0 (meta));
174}
175#undef FUNC_NAME
176
177SCM_DEFINE (scm_program_sources, "program-sources", 1, 0, 0,
178 (SCM program),
179 "")
180#define FUNC_NAME s_scm_program_sources
181{
028e3d06 182 SCM meta, sources, ret, filename;
e311f5fa
AW
183
184 SCM_VALIDATE_PROGRAM (1, program);
185
186 meta = scm_program_meta (program);
9a9f6487 187 if (scm_is_false (meta))
e311f5fa
AW
188 return SCM_EOL;
189
028e3d06
AW
190 filename = SCM_BOOL_F;
191 ret = SCM_EOL;
192 for (sources = scm_cadr (scm_call_0 (meta)); !scm_is_null (sources);
193 sources = scm_cdr (sources))
194 {
195 SCM x = scm_car (sources);
196 if (scm_is_pair (x))
197 {
198 if (scm_is_number (scm_car (x)))
199 {
200 SCM addr = scm_car (x);
201 ret = scm_acons (addr, scm_cons (filename, scm_cdr (x)),
202 ret);
203 }
204 else if (scm_is_eq (scm_car (x), scm_sym_filename))
205 filename = scm_cdr (x);
206 }
207 }
208 return scm_reverse_x (ret, SCM_UNDEFINED);
e311f5fa
AW
209}
210#undef FUNC_NAME
211
212SCM_DEFINE (scm_program_properties, "program-properties", 1, 0, 0,
213 (SCM program),
214 "")
215#define FUNC_NAME s_scm_program_properties
216{
217 SCM meta;
218
219 SCM_VALIDATE_PROGRAM (1, program);
220
221 meta = scm_program_meta (program);
222 if (scm_is_false (meta))
223 return SCM_EOL;
224
225 return scm_cddr (scm_call_0 (meta));
226}
227#undef FUNC_NAME
228
229SCM_DEFINE (scm_program_name, "program-name", 1, 0, 0,
230 (SCM program),
231 "")
232#define FUNC_NAME s_scm_program_name
233{
234 SCM_VALIDATE_PROGRAM (1, program);
235 return scm_assq_ref (scm_program_properties (program), scm_sym_name);
236}
237#undef FUNC_NAME
238
028e3d06
AW
239SCM_DEFINE (scm_program_source, "program-source", 2, 0, 0,
240 (SCM program, SCM ip),
241 "")
242#define FUNC_NAME s_scm_program_source
243{
244 SCM_VALIDATE_PROGRAM (1, program);
245 return scm_c_program_source (program, scm_to_size_t (ip));
246}
247#undef FUNC_NAME
248
e311f5fa
AW
249extern SCM
250scm_c_program_source (SCM program, size_t ip)
251{
028e3d06 252 SCM sources, source = SCM_BOOL_F;
9a9f6487 253
028e3d06
AW
254 for (sources = scm_program_sources (program);
255 !scm_is_null (sources)
256 && scm_to_size_t (scm_caar (sources)) <= ip;
257 sources = scm_cdr (sources))
258 source = scm_car (sources);
259
260 return source; /* (addr . (filename . (line . column))) */
9a9f6487
AW
261}
262
57ab0671 263SCM_DEFINE (scm_program_free_variables, "program-free-variables", 1, 0, 0,
17e90c5e
KN
264 (SCM program),
265 "")
57ab0671 266#define FUNC_NAME s_scm_program_free_variables
62082959
LC
267{
268 SCM_VALIDATE_PROGRAM (1, program);
57ab0671 269 return SCM_PROGRAM_FREE_VARIABLES (program);
62082959
LC
270}
271#undef FUNC_NAME
272
53e28ed9 273SCM_DEFINE (scm_program_objcode, "program-objcode", 1, 0, 0,
17e90c5e 274 (SCM program),
53e28ed9
AW
275 "Return a @var{program}'s object code.")
276#define FUNC_NAME s_scm_program_objcode
17e90c5e
KN
277{
278 SCM_VALIDATE_PROGRAM (1, program);
fa19602c 279
53e28ed9 280 return SCM_PROGRAM_OBJCODE (program);
17e90c5e
KN
281}
282#undef FUNC_NAME
283
fa19602c 284
17e90c5e
KN
285\f
286void
07e56b27 287scm_bootstrap_programs (void)
17e90c5e 288{
60ae5ca2
AW
289 scm_c_register_extension ("libguile", "scm_init_programs",
290 (scm_t_extension_init_func)scm_init_programs, NULL);
07e56b27 291}
17e90c5e 292
07e56b27
AW
293void
294scm_init_programs (void)
295{
296 scm_bootstrap_vm ();
297
17e90c5e 298#ifndef SCM_MAGIC_SNARFER
aeeff258 299#include "libguile/programs.x"
17e90c5e
KN
300#endif
301}
302
303/*
304 Local Variables:
305 c-file-style: "gnu"
306 End:
307*/