Commit | Line | Data |
---|---|---|
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 | 34 | static SCM write_program = SCM_BOOL_F; |
17e90c5e | 35 | |
53e28ed9 | 36 | SCM_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 |
56 | void |
57 | scm_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 | ||
85 | SCM_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 |
94 | SCM_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 | ||
53e28ed9 AW |
105 | SCM_DEFINE (scm_program_objects, "program-objects", 1, 0, 0, |
106 | (SCM program), | |
107 | "") | |
108 | #define FUNC_NAME s_scm_program_objects | |
109 | { | |
110 | SCM_VALIDATE_PROGRAM (1, program); | |
111 | return SCM_PROGRAM_OBJTABLE (program); | |
112 | } | |
113 | #undef FUNC_NAME | |
114 | ||
115 | SCM_DEFINE (scm_program_module, "program-module", 1, 0, 0, | |
116 | (SCM program), | |
117 | "") | |
118 | #define FUNC_NAME s_scm_program_module | |
119 | { | |
120 | SCM objs; | |
121 | SCM_VALIDATE_PROGRAM (1, program); | |
122 | objs = SCM_PROGRAM_OBJTABLE (program); | |
123 | return scm_is_true (objs) ? scm_c_vector_ref (objs, 0) : SCM_BOOL_F; | |
124 | } | |
125 | #undef FUNC_NAME | |
126 | ||
ac99cb0c KN |
127 | SCM_DEFINE (scm_program_meta, "program-meta", 1, 0, 0, |
128 | (SCM program), | |
129 | "") | |
130 | #define FUNC_NAME s_scm_program_meta | |
17e90c5e | 131 | { |
ac47d5f6 AW |
132 | SCM metaobj; |
133 | ||
17e90c5e | 134 | SCM_VALIDATE_PROGRAM (1, program); |
ac47d5f6 AW |
135 | |
136 | metaobj = scm_objcode_meta (SCM_PROGRAM_OBJCODE (program)); | |
137 | if (scm_is_true (metaobj)) | |
20d47c39 | 138 | return scm_make_program (metaobj, SCM_BOOL_F, SCM_BOOL_F); |
ac47d5f6 AW |
139 | else |
140 | return SCM_BOOL_F; | |
17e90c5e KN |
141 | } |
142 | #undef FUNC_NAME | |
143 | ||
e311f5fa AW |
144 | SCM_DEFINE (scm_program_bindings, "program-bindings", 1, 0, 0, |
145 | (SCM program), | |
146 | "") | |
147 | #define FUNC_NAME s_scm_program_bindings | |
9a9f6487 | 148 | { |
e311f5fa AW |
149 | SCM meta; |
150 | ||
151 | SCM_VALIDATE_PROGRAM (1, program); | |
9a9f6487 | 152 | |
53e28ed9 | 153 | meta = scm_program_meta (program); |
2fda0242 | 154 | if (scm_is_false (meta)) |
9a9f6487 | 155 | return SCM_BOOL_F; |
e311f5fa AW |
156 | |
157 | return scm_car (scm_call_0 (meta)); | |
158 | } | |
159 | #undef FUNC_NAME | |
160 | ||
161 | SCM_DEFINE (scm_program_sources, "program-sources", 1, 0, 0, | |
162 | (SCM program), | |
163 | "") | |
164 | #define FUNC_NAME s_scm_program_sources | |
165 | { | |
028e3d06 | 166 | SCM meta, sources, ret, filename; |
e311f5fa AW |
167 | |
168 | SCM_VALIDATE_PROGRAM (1, program); | |
169 | ||
170 | meta = scm_program_meta (program); | |
9a9f6487 | 171 | if (scm_is_false (meta)) |
e311f5fa AW |
172 | return SCM_EOL; |
173 | ||
028e3d06 AW |
174 | filename = SCM_BOOL_F; |
175 | ret = SCM_EOL; | |
176 | for (sources = scm_cadr (scm_call_0 (meta)); !scm_is_null (sources); | |
177 | sources = scm_cdr (sources)) | |
178 | { | |
179 | SCM x = scm_car (sources); | |
180 | if (scm_is_pair (x)) | |
181 | { | |
182 | if (scm_is_number (scm_car (x))) | |
183 | { | |
184 | SCM addr = scm_car (x); | |
185 | ret = scm_acons (addr, scm_cons (filename, scm_cdr (x)), | |
186 | ret); | |
187 | } | |
188 | else if (scm_is_eq (scm_car (x), scm_sym_filename)) | |
189 | filename = scm_cdr (x); | |
190 | } | |
191 | } | |
192 | return scm_reverse_x (ret, SCM_UNDEFINED); | |
e311f5fa AW |
193 | } |
194 | #undef FUNC_NAME | |
195 | ||
6c6a4439 AW |
196 | SCM_DEFINE (scm_program_arities, "program-arities", 1, 0, 0, |
197 | (SCM program), | |
198 | "") | |
199 | #define FUNC_NAME s_scm_program_arities | |
200 | { | |
201 | SCM meta; | |
202 | ||
203 | SCM_VALIDATE_PROGRAM (1, program); | |
204 | ||
205 | meta = scm_program_meta (program); | |
206 | if (scm_is_false (meta)) | |
207 | return SCM_BOOL_F; | |
208 | ||
209 | return scm_caddr (scm_call_0 (meta)); | |
210 | } | |
211 | #undef FUNC_NAME | |
212 | ||
e311f5fa AW |
213 | SCM_DEFINE (scm_program_properties, "program-properties", 1, 0, 0, |
214 | (SCM program), | |
215 | "") | |
216 | #define FUNC_NAME s_scm_program_properties | |
217 | { | |
218 | SCM meta; | |
219 | ||
220 | SCM_VALIDATE_PROGRAM (1, program); | |
221 | ||
222 | meta = scm_program_meta (program); | |
223 | if (scm_is_false (meta)) | |
224 | return SCM_EOL; | |
225 | ||
6c6a4439 | 226 | return scm_cdddr (scm_call_0 (meta)); |
e311f5fa AW |
227 | } |
228 | #undef FUNC_NAME | |
229 | ||
230 | SCM_DEFINE (scm_program_name, "program-name", 1, 0, 0, | |
231 | (SCM program), | |
232 | "") | |
233 | #define FUNC_NAME s_scm_program_name | |
234 | { | |
235 | SCM_VALIDATE_PROGRAM (1, program); | |
236 | return scm_assq_ref (scm_program_properties (program), scm_sym_name); | |
237 | } | |
238 | #undef FUNC_NAME | |
239 | ||
028e3d06 AW |
240 | SCM_DEFINE (scm_program_source, "program-source", 2, 0, 0, |
241 | (SCM program, SCM ip), | |
242 | "") | |
243 | #define FUNC_NAME s_scm_program_source | |
244 | { | |
245 | SCM_VALIDATE_PROGRAM (1, program); | |
246 | return scm_c_program_source (program, scm_to_size_t (ip)); | |
247 | } | |
248 | #undef FUNC_NAME | |
249 | ||
e311f5fa AW |
250 | extern SCM |
251 | scm_c_program_source (SCM program, size_t ip) | |
252 | { | |
028e3d06 | 253 | SCM sources, source = SCM_BOOL_F; |
9a9f6487 | 254 | |
028e3d06 AW |
255 | for (sources = scm_program_sources (program); |
256 | !scm_is_null (sources) | |
257 | && scm_to_size_t (scm_caar (sources)) <= ip; | |
258 | sources = scm_cdr (sources)) | |
259 | source = scm_car (sources); | |
260 | ||
261 | return source; /* (addr . (filename . (line . column))) */ | |
9a9f6487 AW |
262 | } |
263 | ||
57ab0671 | 264 | SCM_DEFINE (scm_program_free_variables, "program-free-variables", 1, 0, 0, |
17e90c5e KN |
265 | (SCM program), |
266 | "") | |
57ab0671 | 267 | #define FUNC_NAME s_scm_program_free_variables |
62082959 LC |
268 | { |
269 | SCM_VALIDATE_PROGRAM (1, program); | |
57ab0671 | 270 | return SCM_PROGRAM_FREE_VARIABLES (program); |
62082959 LC |
271 | } |
272 | #undef FUNC_NAME | |
273 | ||
53e28ed9 | 274 | SCM_DEFINE (scm_program_objcode, "program-objcode", 1, 0, 0, |
17e90c5e | 275 | (SCM program), |
53e28ed9 AW |
276 | "Return a @var{program}'s object code.") |
277 | #define FUNC_NAME s_scm_program_objcode | |
17e90c5e KN |
278 | { |
279 | SCM_VALIDATE_PROGRAM (1, program); | |
fa19602c | 280 | |
53e28ed9 | 281 | return SCM_PROGRAM_OBJCODE (program); |
17e90c5e KN |
282 | } |
283 | #undef FUNC_NAME | |
284 | ||
56164a5a AW |
285 | /* This one is a shim to pre-case-lambda internal interfaces. Avoid it if you |
286 | can -- use program-arguments or the like. */ | |
287 | static SCM sym_arglist; | |
288 | int | |
289 | scm_i_program_arity (SCM program, int *req, int *opt, int *rest) | |
290 | { | |
291 | SCM arities, x; | |
292 | ||
293 | arities = scm_program_arities (program); | |
294 | if (!scm_is_pair (arities)) | |
295 | return 0; | |
296 | /* take the last arglist, it will be least specific */ | |
297 | while (scm_is_pair (scm_cdr (arities))) | |
298 | arities = scm_cdr (arities); | |
df435c83 | 299 | x = scm_cddar (arities); |
56164a5a AW |
300 | if (scm_is_pair (x)) |
301 | { | |
302 | *req = scm_to_int (scm_car (x)); | |
303 | x = scm_cdr (x); | |
304 | if (scm_is_pair (x)) | |
305 | { | |
306 | *opt = scm_to_int (scm_car (x)); | |
307 | x = scm_cdr (x); | |
308 | if (scm_is_pair (x)) | |
309 | *rest = scm_is_true (scm_car (x)); | |
310 | else | |
311 | *rest = 0; | |
312 | } | |
313 | else | |
314 | *opt = *rest = 0; | |
315 | } | |
316 | else | |
317 | *req = *opt = *rest = 0; | |
318 | ||
319 | return 1; | |
320 | } | |
fa19602c | 321 | |
17e90c5e | 322 | \f |
56164a5a | 323 | |
17e90c5e | 324 | void |
07e56b27 | 325 | scm_bootstrap_programs (void) |
17e90c5e | 326 | { |
56164a5a AW |
327 | /* arglist can't be snarfed, because snarfage is only loaded when (system vm |
328 | program) is loaded. perhaps static-alloc will fix this. */ | |
329 | sym_arglist = scm_from_locale_symbol ("arglist"); | |
60ae5ca2 AW |
330 | scm_c_register_extension ("libguile", "scm_init_programs", |
331 | (scm_t_extension_init_func)scm_init_programs, NULL); | |
07e56b27 | 332 | } |
17e90c5e | 333 | |
07e56b27 AW |
334 | void |
335 | scm_init_programs (void) | |
336 | { | |
337 | scm_bootstrap_vm (); | |
338 | ||
17e90c5e | 339 | #ifndef SCM_MAGIC_SNARFER |
aeeff258 | 340 | #include "libguile/programs.x" |
17e90c5e KN |
341 | #endif |
342 | } | |
343 | ||
344 | /* | |
345 | Local Variables: | |
346 | c-file-style: "gnu" | |
347 | End: | |
348 | */ |