Commit | Line | Data |
---|---|---|
31a26df2 | 1 | /* Copyright (C) 2001, 2009, 2010 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" |
17e90c5e | 25 | #include "instructions.h" |
8e367074 | 26 | #include "modules.h" |
17e90c5e | 27 | #include "programs.h" |
6f6f0dac LC |
28 | #include "procprop.h" /* scm_sym_name */ |
29 | #include "srcprop.h" /* scm_sym_filename */ | |
17e90c5e KN |
30 | #include "vm.h" |
31 | ||
32 | \f | |
e6fea618 | 33 | static SCM write_program = SCM_BOOL_F; |
17e90c5e | 34 | |
53e28ed9 | 35 | SCM_DEFINE (scm_make_program, "make-program", 1, 2, 0, |
57ab0671 | 36 | (SCM objcode, SCM objtable, SCM free_variables), |
53e28ed9 AW |
37 | "") |
38 | #define FUNC_NAME s_scm_make_program | |
17e90c5e | 39 | { |
53e28ed9 AW |
40 | SCM_VALIDATE_OBJCODE (1, objcode); |
41 | if (SCM_UNLIKELY (SCM_UNBNDP (objtable))) | |
42 | objtable = SCM_BOOL_F; | |
43 | else if (scm_is_true (objtable)) | |
44 | SCM_VALIDATE_VECTOR (2, objtable); | |
20d47c39 | 45 | |
6f16379e AW |
46 | if (SCM_UNBNDP (free_variables) || scm_is_false (free_variables)) |
47 | { | |
48 | SCM ret = scm_words (scm_tc7_program, 3); | |
49 | SCM_SET_CELL_OBJECT_1 (ret, objcode); | |
50 | SCM_SET_CELL_OBJECT_2 (ret, objtable); | |
51 | return ret; | |
52 | } | |
53 | else | |
54 | { | |
55 | size_t i, len; | |
56 | SCM ret; | |
57 | SCM_VALIDATE_VECTOR (3, free_variables); | |
58 | len = scm_c_vector_length (free_variables); | |
59 | if (SCM_UNLIKELY (len >> 16)) | |
60 | SCM_OUT_OF_RANGE (3, free_variables); | |
61 | ret = scm_words (scm_tc7_program | (len<<16), 3 + len); | |
62 | SCM_SET_CELL_OBJECT_1 (ret, objcode); | |
63 | SCM_SET_CELL_OBJECT_2 (ret, objtable); | |
64 | for (i = 0; i < len; i++) | |
65 | SCM_SET_CELL_OBJECT (ret, 3+i, | |
66 | SCM_SIMPLE_VECTOR_REF (free_variables, i)); | |
67 | return ret; | |
68 | } | |
17e90c5e KN |
69 | } |
70 | #undef FUNC_NAME | |
71 | ||
2fb924f6 AW |
72 | void |
73 | scm_i_program_print (SCM program, SCM port, scm_print_state *pstate) | |
e6fea618 | 74 | { |
0ba8bb71 AW |
75 | static int print_error = 0; |
76 | ||
5c8cefe5 | 77 | if (scm_is_false (write_program) && scm_module_system_booted_p) |
e6fea618 AW |
78 | write_program = scm_module_local_variable |
79 | (scm_c_resolve_module ("system vm program"), | |
80 | scm_from_locale_symbol ("write-program")); | |
81 | ||
1d1cae0e AW |
82 | if (SCM_PROGRAM_IS_CONTINUATION (program)) |
83 | { | |
84 | /* twingliness */ | |
85 | scm_puts ("#<continuation ", port); | |
76e38162 | 86 | scm_uintprint (SCM_UNPACK (program), 16, port); |
1d1cae0e AW |
87 | scm_putc ('>', port); |
88 | } | |
89 | else if (scm_is_false (write_program) || print_error) | |
2fb924f6 AW |
90 | { |
91 | scm_puts ("#<program ", port); | |
76e38162 | 92 | scm_uintprint (SCM_UNPACK (program), 16, port); |
2fb924f6 AW |
93 | scm_putc ('>', port); |
94 | } | |
95 | else | |
96 | { | |
97 | print_error = 1; | |
98 | scm_call_2 (SCM_VARIABLE_REF (write_program), program, port); | |
99 | print_error = 0; | |
100 | } | |
e6fea618 AW |
101 | } |
102 | ||
17e90c5e KN |
103 | \f |
104 | /* | |
105 | * Scheme interface | |
106 | */ | |
107 | ||
108 | SCM_DEFINE (scm_program_p, "program?", 1, 0, 0, | |
109 | (SCM obj), | |
110 | "") | |
111 | #define FUNC_NAME s_scm_program_p | |
112 | { | |
5c8cefe5 | 113 | return scm_from_bool (SCM_PROGRAM_P (obj)); |
17e90c5e KN |
114 | } |
115 | #undef FUNC_NAME | |
116 | ||
ac99cb0c KN |
117 | SCM_DEFINE (scm_program_base, "program-base", 1, 0, 0, |
118 | (SCM program), | |
119 | "") | |
120 | #define FUNC_NAME s_scm_program_base | |
121 | { | |
3dbbe28d LC |
122 | const struct scm_objcode *c_objcode; |
123 | ||
ac99cb0c KN |
124 | SCM_VALIDATE_PROGRAM (1, program); |
125 | ||
3dbbe28d LC |
126 | c_objcode = SCM_PROGRAM_DATA (program); |
127 | return scm_from_ulong ((unsigned long) SCM_C_OBJCODE_BASE (c_objcode)); | |
ac99cb0c KN |
128 | } |
129 | #undef FUNC_NAME | |
130 | ||
53e28ed9 AW |
131 | SCM_DEFINE (scm_program_objects, "program-objects", 1, 0, 0, |
132 | (SCM program), | |
133 | "") | |
134 | #define FUNC_NAME s_scm_program_objects | |
135 | { | |
136 | SCM_VALIDATE_PROGRAM (1, program); | |
137 | return SCM_PROGRAM_OBJTABLE (program); | |
138 | } | |
139 | #undef FUNC_NAME | |
140 | ||
141 | SCM_DEFINE (scm_program_module, "program-module", 1, 0, 0, | |
142 | (SCM program), | |
143 | "") | |
144 | #define FUNC_NAME s_scm_program_module | |
145 | { | |
146 | SCM objs; | |
147 | SCM_VALIDATE_PROGRAM (1, program); | |
148 | objs = SCM_PROGRAM_OBJTABLE (program); | |
149 | return scm_is_true (objs) ? scm_c_vector_ref (objs, 0) : SCM_BOOL_F; | |
150 | } | |
151 | #undef FUNC_NAME | |
152 | ||
ac99cb0c KN |
153 | SCM_DEFINE (scm_program_meta, "program-meta", 1, 0, 0, |
154 | (SCM program), | |
155 | "") | |
156 | #define FUNC_NAME s_scm_program_meta | |
17e90c5e | 157 | { |
ac47d5f6 AW |
158 | SCM metaobj; |
159 | ||
17e90c5e | 160 | SCM_VALIDATE_PROGRAM (1, program); |
ac47d5f6 AW |
161 | |
162 | metaobj = scm_objcode_meta (SCM_PROGRAM_OBJCODE (program)); | |
163 | if (scm_is_true (metaobj)) | |
31a26df2 AW |
164 | return scm_make_program (metaobj, SCM_PROGRAM_OBJTABLE (program), |
165 | SCM_BOOL_F); | |
ac47d5f6 AW |
166 | else |
167 | return SCM_BOOL_F; | |
17e90c5e KN |
168 | } |
169 | #undef FUNC_NAME | |
170 | ||
e311f5fa AW |
171 | SCM_DEFINE (scm_program_bindings, "program-bindings", 1, 0, 0, |
172 | (SCM program), | |
173 | "") | |
174 | #define FUNC_NAME s_scm_program_bindings | |
9a9f6487 | 175 | { |
e311f5fa AW |
176 | SCM meta; |
177 | ||
178 | SCM_VALIDATE_PROGRAM (1, program); | |
9a9f6487 | 179 | |
53e28ed9 | 180 | meta = scm_program_meta (program); |
2fda0242 | 181 | if (scm_is_false (meta)) |
9a9f6487 | 182 | return SCM_BOOL_F; |
e311f5fa AW |
183 | |
184 | return scm_car (scm_call_0 (meta)); | |
185 | } | |
186 | #undef FUNC_NAME | |
187 | ||
188 | SCM_DEFINE (scm_program_sources, "program-sources", 1, 0, 0, | |
189 | (SCM program), | |
190 | "") | |
191 | #define FUNC_NAME s_scm_program_sources | |
192 | { | |
028e3d06 | 193 | SCM meta, sources, ret, filename; |
e311f5fa AW |
194 | |
195 | SCM_VALIDATE_PROGRAM (1, program); | |
196 | ||
197 | meta = scm_program_meta (program); | |
9a9f6487 | 198 | if (scm_is_false (meta)) |
e311f5fa AW |
199 | return SCM_EOL; |
200 | ||
028e3d06 AW |
201 | filename = SCM_BOOL_F; |
202 | ret = SCM_EOL; | |
203 | for (sources = scm_cadr (scm_call_0 (meta)); !scm_is_null (sources); | |
204 | sources = scm_cdr (sources)) | |
205 | { | |
206 | SCM x = scm_car (sources); | |
207 | if (scm_is_pair (x)) | |
208 | { | |
209 | if (scm_is_number (scm_car (x))) | |
210 | { | |
211 | SCM addr = scm_car (x); | |
212 | ret = scm_acons (addr, scm_cons (filename, scm_cdr (x)), | |
213 | ret); | |
214 | } | |
215 | else if (scm_is_eq (scm_car (x), scm_sym_filename)) | |
216 | filename = scm_cdr (x); | |
217 | } | |
218 | } | |
219 | return scm_reverse_x (ret, SCM_UNDEFINED); | |
e311f5fa AW |
220 | } |
221 | #undef FUNC_NAME | |
222 | ||
6c6a4439 AW |
223 | SCM_DEFINE (scm_program_arities, "program-arities", 1, 0, 0, |
224 | (SCM program), | |
225 | "") | |
226 | #define FUNC_NAME s_scm_program_arities | |
227 | { | |
228 | SCM meta; | |
229 | ||
230 | SCM_VALIDATE_PROGRAM (1, program); | |
231 | ||
232 | meta = scm_program_meta (program); | |
233 | if (scm_is_false (meta)) | |
234 | return SCM_BOOL_F; | |
235 | ||
236 | return scm_caddr (scm_call_0 (meta)); | |
237 | } | |
238 | #undef FUNC_NAME | |
239 | ||
e311f5fa AW |
240 | SCM_DEFINE (scm_program_properties, "program-properties", 1, 0, 0, |
241 | (SCM program), | |
242 | "") | |
243 | #define FUNC_NAME s_scm_program_properties | |
244 | { | |
245 | SCM meta; | |
246 | ||
247 | SCM_VALIDATE_PROGRAM (1, program); | |
248 | ||
249 | meta = scm_program_meta (program); | |
250 | if (scm_is_false (meta)) | |
251 | return SCM_EOL; | |
252 | ||
6c6a4439 | 253 | return scm_cdddr (scm_call_0 (meta)); |
e311f5fa AW |
254 | } |
255 | #undef FUNC_NAME | |
256 | ||
257 | SCM_DEFINE (scm_program_name, "program-name", 1, 0, 0, | |
258 | (SCM program), | |
259 | "") | |
260 | #define FUNC_NAME s_scm_program_name | |
261 | { | |
262 | SCM_VALIDATE_PROGRAM (1, program); | |
263 | return scm_assq_ref (scm_program_properties (program), scm_sym_name); | |
264 | } | |
265 | #undef FUNC_NAME | |
266 | ||
028e3d06 AW |
267 | SCM_DEFINE (scm_program_source, "program-source", 2, 0, 0, |
268 | (SCM program, SCM ip), | |
269 | "") | |
270 | #define FUNC_NAME s_scm_program_source | |
271 | { | |
272 | SCM_VALIDATE_PROGRAM (1, program); | |
273 | return scm_c_program_source (program, scm_to_size_t (ip)); | |
274 | } | |
275 | #undef FUNC_NAME | |
276 | ||
e311f5fa AW |
277 | extern SCM |
278 | scm_c_program_source (SCM program, size_t ip) | |
279 | { | |
028e3d06 | 280 | SCM sources, source = SCM_BOOL_F; |
9a9f6487 | 281 | |
028e3d06 AW |
282 | for (sources = scm_program_sources (program); |
283 | !scm_is_null (sources) | |
284 | && scm_to_size_t (scm_caar (sources)) <= ip; | |
285 | sources = scm_cdr (sources)) | |
286 | source = scm_car (sources); | |
287 | ||
288 | return source; /* (addr . (filename . (line . column))) */ | |
9a9f6487 AW |
289 | } |
290 | ||
6f16379e | 291 | SCM_DEFINE (scm_program_num_free_variables, "program-num-free-variables", 1, 0, 0, |
17e90c5e KN |
292 | (SCM program), |
293 | "") | |
6f16379e AW |
294 | #define FUNC_NAME s_scm_program_num_free_variables |
295 | { | |
296 | SCM_VALIDATE_PROGRAM (1, program); | |
297 | return scm_from_ulong (SCM_PROGRAM_NUM_FREE_VARIABLES (program)); | |
298 | } | |
299 | #undef FUNC_NAME | |
300 | ||
301 | SCM_DEFINE (scm_program_free_variable_ref, "program-free-variable-ref", 2, 0, 0, | |
302 | (SCM program, SCM i), | |
303 | "") | |
304 | #define FUNC_NAME s_scm_program_free_variable_ref | |
305 | { | |
306 | unsigned long idx; | |
307 | SCM_VALIDATE_PROGRAM (1, program); | |
308 | SCM_VALIDATE_ULONG_COPY (2, i, idx); | |
309 | if (idx >= SCM_PROGRAM_NUM_FREE_VARIABLES (program)) | |
310 | SCM_OUT_OF_RANGE (2, i); | |
311 | return SCM_PROGRAM_FREE_VARIABLE_REF (program, idx); | |
312 | } | |
313 | #undef FUNC_NAME | |
314 | ||
315 | SCM_DEFINE (scm_program_free_variable_set_x, "program-free-variable-set!", 3, 0, 0, | |
316 | (SCM program, SCM i, SCM x), | |
317 | "") | |
318 | #define FUNC_NAME s_scm_program_free_variable_set_x | |
62082959 | 319 | { |
6f16379e | 320 | unsigned long idx; |
62082959 | 321 | SCM_VALIDATE_PROGRAM (1, program); |
6f16379e AW |
322 | SCM_VALIDATE_ULONG_COPY (2, i, idx); |
323 | if (idx >= SCM_PROGRAM_NUM_FREE_VARIABLES (program)) | |
324 | SCM_OUT_OF_RANGE (2, i); | |
325 | SCM_PROGRAM_FREE_VARIABLE_SET (program, idx, x); | |
326 | return SCM_UNSPECIFIED; | |
62082959 LC |
327 | } |
328 | #undef FUNC_NAME | |
329 | ||
53e28ed9 | 330 | SCM_DEFINE (scm_program_objcode, "program-objcode", 1, 0, 0, |
17e90c5e | 331 | (SCM program), |
53e28ed9 AW |
332 | "Return a @var{program}'s object code.") |
333 | #define FUNC_NAME s_scm_program_objcode | |
17e90c5e KN |
334 | { |
335 | SCM_VALIDATE_PROGRAM (1, program); | |
fa19602c | 336 | |
53e28ed9 | 337 | return SCM_PROGRAM_OBJCODE (program); |
17e90c5e KN |
338 | } |
339 | #undef FUNC_NAME | |
340 | ||
56164a5a AW |
341 | /* This one is a shim to pre-case-lambda internal interfaces. Avoid it if you |
342 | can -- use program-arguments or the like. */ | |
343 | static SCM sym_arglist; | |
344 | int | |
345 | scm_i_program_arity (SCM program, int *req, int *opt, int *rest) | |
346 | { | |
347 | SCM arities, x; | |
348 | ||
349 | arities = scm_program_arities (program); | |
350 | if (!scm_is_pair (arities)) | |
351 | return 0; | |
352 | /* take the last arglist, it will be least specific */ | |
353 | while (scm_is_pair (scm_cdr (arities))) | |
354 | arities = scm_cdr (arities); | |
df435c83 | 355 | x = scm_cddar (arities); |
56164a5a AW |
356 | if (scm_is_pair (x)) |
357 | { | |
358 | *req = scm_to_int (scm_car (x)); | |
359 | x = scm_cdr (x); | |
360 | if (scm_is_pair (x)) | |
361 | { | |
362 | *opt = scm_to_int (scm_car (x)); | |
363 | x = scm_cdr (x); | |
364 | if (scm_is_pair (x)) | |
365 | *rest = scm_is_true (scm_car (x)); | |
366 | else | |
367 | *rest = 0; | |
368 | } | |
369 | else | |
370 | *opt = *rest = 0; | |
371 | } | |
372 | else | |
373 | *req = *opt = *rest = 0; | |
374 | ||
375 | return 1; | |
376 | } | |
fa19602c | 377 | |
17e90c5e | 378 | \f |
56164a5a | 379 | |
17e90c5e | 380 | void |
07e56b27 | 381 | scm_bootstrap_programs (void) |
17e90c5e | 382 | { |
56164a5a AW |
383 | /* arglist can't be snarfed, because snarfage is only loaded when (system vm |
384 | program) is loaded. perhaps static-alloc will fix this. */ | |
385 | sym_arglist = scm_from_locale_symbol ("arglist"); | |
60ae5ca2 AW |
386 | scm_c_register_extension ("libguile", "scm_init_programs", |
387 | (scm_t_extension_init_func)scm_init_programs, NULL); | |
07e56b27 | 388 | } |
17e90c5e | 389 | |
07e56b27 AW |
390 | void |
391 | scm_init_programs (void) | |
392 | { | |
17e90c5e | 393 | #ifndef SCM_MAGIC_SNARFER |
aeeff258 | 394 | #include "libguile/programs.x" |
17e90c5e KN |
395 | #endif |
396 | } | |
397 | ||
398 | /* | |
399 | Local Variables: | |
400 | c-file-style: "gnu" | |
401 | End: | |
402 | */ |