Commit | Line | Data |
---|---|---|
510ca126 | 1 | /* Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013 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 | ||
510ca126 AW |
72 | SCM_DEFINE (scm_make_rtl_program, "make-rtl-program", 1, 2, 0, |
73 | (SCM bytevector, SCM byte_offset, SCM free_variables), | |
74 | "") | |
75 | #define FUNC_NAME s_scm_make_rtl_program | |
76 | { | |
77 | scm_t_uint8 *code; | |
78 | scm_t_uint32 offset; | |
79 | ||
80 | if (!scm_is_bytevector (bytevector)) | |
81 | scm_wrong_type_arg (FUNC_NAME, 1, bytevector); | |
82 | if (SCM_UNBNDP (byte_offset)) | |
83 | offset = 0; | |
84 | else | |
85 | { | |
86 | offset = scm_to_uint32 (byte_offset); | |
87 | if (offset > SCM_BYTEVECTOR_LENGTH (bytevector)) | |
88 | SCM_OUT_OF_RANGE (2, byte_offset); | |
89 | } | |
90 | ||
91 | code = (scm_t_uint8*) SCM_BYTEVECTOR_CONTENTS (bytevector) + offset; | |
92 | if (((scm_t_uintptr) code) % 4) | |
93 | SCM_OUT_OF_RANGE (2, byte_offset); | |
94 | ||
95 | if (SCM_UNBNDP (free_variables) || scm_is_false (free_variables)) | |
96 | return scm_cell (scm_tc7_rtl_program, (scm_t_bits) code); | |
97 | else | |
98 | abort (); | |
99 | } | |
100 | #undef FUNC_NAME | |
101 | ||
102 | SCM_DEFINE (scm_rtl_program_code, "rtl-program-code", 1, 0, 0, | |
103 | (SCM program), | |
104 | "") | |
105 | #define FUNC_NAME s_scm_rtl_program_code | |
106 | { | |
107 | SCM_VALIDATE_RTL_PROGRAM (1, program); | |
108 | ||
72b82b0f | 109 | return scm_from_uintptr_t ((scm_t_uintptr) SCM_RTL_PROGRAM_CODE (program)); |
510ca126 AW |
110 | } |
111 | #undef FUNC_NAME | |
112 | ||
e65f80af AW |
113 | SCM |
114 | scm_i_rtl_program_name (SCM program) | |
510ca126 | 115 | { |
e65f80af AW |
116 | static SCM rtl_program_name = SCM_BOOL_F; |
117 | ||
27337b63 AW |
118 | if (SCM_PRIMITIVE_P (program)) |
119 | return SCM_SUBR_NAME (program); | |
120 | ||
e65f80af AW |
121 | if (scm_is_false (rtl_program_name) && scm_module_system_booted_p) |
122 | rtl_program_name = | |
123 | scm_c_private_variable ("system vm program", "rtl-program-name"); | |
124 | ||
125 | return scm_call_1 (scm_variable_ref (rtl_program_name), program); | |
510ca126 AW |
126 | } |
127 | ||
bf8328ec AW |
128 | SCM |
129 | scm_i_rtl_program_documentation (SCM program) | |
130 | { | |
131 | static SCM rtl_program_documentation = SCM_BOOL_F; | |
132 | ||
27337b63 AW |
133 | if (SCM_PRIMITIVE_P (program)) |
134 | return SCM_BOOL_F; | |
135 | ||
bf8328ec AW |
136 | if (scm_is_false (rtl_program_documentation) && scm_module_system_booted_p) |
137 | rtl_program_documentation = | |
138 | scm_c_private_variable ("system vm program", | |
139 | "rtl-program-documentation"); | |
140 | ||
141 | return scm_call_1 (scm_variable_ref (rtl_program_documentation), program); | |
142 | } | |
143 | ||
c4c098e3 AW |
144 | SCM |
145 | scm_i_rtl_program_properties (SCM program) | |
146 | { | |
147 | static SCM rtl_program_properties = SCM_BOOL_F; | |
148 | ||
27337b63 AW |
149 | if (SCM_PRIMITIVE_P (program)) |
150 | { | |
151 | SCM name = scm_i_rtl_program_name (program); | |
152 | if (scm_is_false (name)) | |
153 | return SCM_EOL; | |
154 | return scm_acons (scm_sym_name, name, SCM_EOL); | |
155 | } | |
156 | ||
c4c098e3 AW |
157 | if (scm_is_false (rtl_program_properties) && scm_module_system_booted_p) |
158 | rtl_program_properties = | |
159 | scm_c_private_variable ("system vm program", "rtl-program-properties"); | |
160 | ||
161 | return scm_call_1 (scm_variable_ref (rtl_program_properties), program); | |
162 | } | |
163 | ||
2fb924f6 AW |
164 | void |
165 | scm_i_program_print (SCM program, SCM port, scm_print_state *pstate) | |
e6fea618 | 166 | { |
0ba8bb71 AW |
167 | static int print_error = 0; |
168 | ||
5c8cefe5 | 169 | if (scm_is_false (write_program) && scm_module_system_booted_p) |
eb2bc00f AW |
170 | write_program = scm_c_private_variable ("system vm program", |
171 | "write-program"); | |
e6fea618 | 172 | |
1d1cae0e AW |
173 | if (SCM_PROGRAM_IS_CONTINUATION (program)) |
174 | { | |
175 | /* twingliness */ | |
0607ebbf | 176 | scm_puts_unlocked ("#<continuation ", port); |
76e38162 | 177 | scm_uintprint (SCM_UNPACK (program), 16, port); |
0607ebbf | 178 | scm_putc_unlocked ('>', port); |
1d1cae0e | 179 | } |
5c606217 | 180 | else if (SCM_PROGRAM_IS_PARTIAL_CONTINUATION (program)) |
2150e9a8 AW |
181 | { |
182 | /* twingliness */ | |
0607ebbf | 183 | scm_puts_unlocked ("#<partial-continuation ", port); |
2150e9a8 | 184 | scm_uintprint (SCM_UNPACK (program), 16, port); |
0607ebbf | 185 | scm_putc_unlocked ('>', port); |
2150e9a8 | 186 | } |
1d1cae0e | 187 | else if (scm_is_false (write_program) || print_error) |
2fb924f6 | 188 | { |
e65f80af AW |
189 | if (SCM_RTL_PROGRAM_P (program)) |
190 | { | |
191 | scm_puts_unlocked ("#<rtl-program ", port); | |
192 | scm_uintprint (SCM_UNPACK (program), 16, port); | |
193 | scm_putc_unlocked (' ', port); | |
194 | scm_uintprint ((scm_t_uintptr) SCM_RTL_PROGRAM_CODE (program), 16, port); | |
195 | scm_putc_unlocked ('>', port); | |
196 | } | |
197 | else | |
198 | { | |
199 | scm_puts_unlocked ("#<program ", port); | |
200 | scm_uintprint (SCM_UNPACK (program), 16, port); | |
201 | scm_putc_unlocked ('>', port); | |
202 | } | |
2fb924f6 AW |
203 | } |
204 | else | |
205 | { | |
206 | print_error = 1; | |
207 | scm_call_2 (SCM_VARIABLE_REF (write_program), program, port); | |
208 | print_error = 0; | |
209 | } | |
e6fea618 AW |
210 | } |
211 | ||
17e90c5e KN |
212 | \f |
213 | /* | |
214 | * Scheme interface | |
215 | */ | |
216 | ||
217 | SCM_DEFINE (scm_program_p, "program?", 1, 0, 0, | |
218 | (SCM obj), | |
219 | "") | |
220 | #define FUNC_NAME s_scm_program_p | |
221 | { | |
5c8cefe5 | 222 | return scm_from_bool (SCM_PROGRAM_P (obj)); |
17e90c5e KN |
223 | } |
224 | #undef FUNC_NAME | |
225 | ||
510ca126 AW |
226 | SCM_DEFINE (scm_rtl_program_p, "rtl-program?", 1, 0, 0, |
227 | (SCM obj), | |
228 | "") | |
229 | #define FUNC_NAME s_scm_rtl_program_p | |
230 | { | |
231 | return scm_from_bool (SCM_RTL_PROGRAM_P (obj)); | |
232 | } | |
233 | #undef FUNC_NAME | |
234 | ||
27337b63 AW |
235 | SCM_DEFINE (scm_primitive_p, "primitive?", 1, 0, 0, |
236 | (SCM obj), | |
237 | "") | |
238 | #define FUNC_NAME s_scm_primitive_p | |
239 | { | |
240 | return scm_from_bool (SCM_PRIMITIVE_P (obj)); | |
241 | } | |
242 | #undef FUNC_NAME | |
243 | ||
244 | SCM_DEFINE (scm_primitive_call_ip, "primitive-call-ip", 1, 0, 0, | |
245 | (SCM prim), | |
246 | "") | |
247 | #define FUNC_NAME s_scm_primitive_p | |
248 | { | |
249 | SCM_MAKE_VALIDATE (1, prim, PRIMITIVE_P); | |
250 | ||
251 | return scm_from_int (scm_i_primitive_call_ip (prim)); | |
252 | } | |
253 | #undef FUNC_NAME | |
254 | ||
ac99cb0c KN |
255 | SCM_DEFINE (scm_program_base, "program-base", 1, 0, 0, |
256 | (SCM program), | |
257 | "") | |
258 | #define FUNC_NAME s_scm_program_base | |
259 | { | |
3dbbe28d LC |
260 | const struct scm_objcode *c_objcode; |
261 | ||
ac99cb0c KN |
262 | SCM_VALIDATE_PROGRAM (1, program); |
263 | ||
3dbbe28d | 264 | c_objcode = SCM_PROGRAM_DATA (program); |
3d27ef4b | 265 | return scm_from_unsigned_integer ((scm_t_bits) SCM_C_OBJCODE_BASE (c_objcode)); |
ac99cb0c KN |
266 | } |
267 | #undef FUNC_NAME | |
268 | ||
53e28ed9 AW |
269 | SCM_DEFINE (scm_program_objects, "program-objects", 1, 0, 0, |
270 | (SCM program), | |
271 | "") | |
272 | #define FUNC_NAME s_scm_program_objects | |
273 | { | |
274 | SCM_VALIDATE_PROGRAM (1, program); | |
275 | return SCM_PROGRAM_OBJTABLE (program); | |
276 | } | |
277 | #undef FUNC_NAME | |
278 | ||
279 | SCM_DEFINE (scm_program_module, "program-module", 1, 0, 0, | |
280 | (SCM program), | |
281 | "") | |
282 | #define FUNC_NAME s_scm_program_module | |
283 | { | |
7884975a | 284 | SCM objs, mod; |
53e28ed9 AW |
285 | SCM_VALIDATE_PROGRAM (1, program); |
286 | objs = SCM_PROGRAM_OBJTABLE (program); | |
7884975a AW |
287 | /* If a program is the result of compiling GLIL to assembly, then if |
288 | it has an objtable, the first entry will be a module. But some | |
289 | programs are hand-coded trampolines, like boot programs and | |
290 | primitives and the like. So if a program happens to have a | |
291 | non-module in the first slot of the objtable, assume that it is | |
292 | such a trampoline, and just return #f for the module. */ | |
293 | mod = scm_is_true (objs) ? scm_c_vector_ref (objs, 0) : SCM_BOOL_F; | |
294 | return SCM_MODULEP (mod) ? mod : SCM_BOOL_F; | |
53e28ed9 AW |
295 | } |
296 | #undef FUNC_NAME | |
297 | ||
ac99cb0c KN |
298 | SCM_DEFINE (scm_program_meta, "program-meta", 1, 0, 0, |
299 | (SCM program), | |
300 | "") | |
301 | #define FUNC_NAME s_scm_program_meta | |
17e90c5e | 302 | { |
ac47d5f6 AW |
303 | SCM metaobj; |
304 | ||
17e90c5e | 305 | SCM_VALIDATE_PROGRAM (1, program); |
ac47d5f6 AW |
306 | |
307 | metaobj = scm_objcode_meta (SCM_PROGRAM_OBJCODE (program)); | |
308 | if (scm_is_true (metaobj)) | |
31a26df2 AW |
309 | return scm_make_program (metaobj, SCM_PROGRAM_OBJTABLE (program), |
310 | SCM_BOOL_F); | |
ac47d5f6 AW |
311 | else |
312 | return SCM_BOOL_F; | |
17e90c5e KN |
313 | } |
314 | #undef FUNC_NAME | |
315 | ||
e311f5fa AW |
316 | SCM_DEFINE (scm_program_bindings, "program-bindings", 1, 0, 0, |
317 | (SCM program), | |
318 | "") | |
319 | #define FUNC_NAME s_scm_program_bindings | |
9a9f6487 | 320 | { |
e311f5fa AW |
321 | SCM meta; |
322 | ||
323 | SCM_VALIDATE_PROGRAM (1, program); | |
9a9f6487 | 324 | |
53e28ed9 | 325 | meta = scm_program_meta (program); |
2fda0242 | 326 | if (scm_is_false (meta)) |
9a9f6487 | 327 | return SCM_BOOL_F; |
e311f5fa AW |
328 | |
329 | return scm_car (scm_call_0 (meta)); | |
330 | } | |
331 | #undef FUNC_NAME | |
332 | ||
7c540297 | 333 | SCM_DEFINE (scm_program_sources, "%program-sources", 1, 0, 0, |
e311f5fa AW |
334 | (SCM program), |
335 | "") | |
336 | #define FUNC_NAME s_scm_program_sources | |
337 | { | |
028e3d06 | 338 | SCM meta, sources, ret, filename; |
e311f5fa AW |
339 | |
340 | SCM_VALIDATE_PROGRAM (1, program); | |
341 | ||
342 | meta = scm_program_meta (program); | |
9a9f6487 | 343 | if (scm_is_false (meta)) |
e311f5fa AW |
344 | return SCM_EOL; |
345 | ||
028e3d06 AW |
346 | filename = SCM_BOOL_F; |
347 | ret = SCM_EOL; | |
348 | for (sources = scm_cadr (scm_call_0 (meta)); !scm_is_null (sources); | |
349 | sources = scm_cdr (sources)) | |
350 | { | |
351 | SCM x = scm_car (sources); | |
352 | if (scm_is_pair (x)) | |
353 | { | |
354 | if (scm_is_number (scm_car (x))) | |
355 | { | |
356 | SCM addr = scm_car (x); | |
357 | ret = scm_acons (addr, scm_cons (filename, scm_cdr (x)), | |
358 | ret); | |
359 | } | |
360 | else if (scm_is_eq (scm_car (x), scm_sym_filename)) | |
361 | filename = scm_cdr (x); | |
362 | } | |
363 | } | |
364 | return scm_reverse_x (ret, SCM_UNDEFINED); | |
e311f5fa AW |
365 | } |
366 | #undef FUNC_NAME | |
367 | ||
6c6a4439 AW |
368 | SCM_DEFINE (scm_program_arities, "program-arities", 1, 0, 0, |
369 | (SCM program), | |
370 | "") | |
371 | #define FUNC_NAME s_scm_program_arities | |
372 | { | |
373 | SCM meta; | |
374 | ||
375 | SCM_VALIDATE_PROGRAM (1, program); | |
376 | ||
377 | meta = scm_program_meta (program); | |
378 | if (scm_is_false (meta)) | |
379 | return SCM_BOOL_F; | |
380 | ||
381 | return scm_caddr (scm_call_0 (meta)); | |
382 | } | |
383 | #undef FUNC_NAME | |
384 | ||
07e424b7 AW |
385 | SCM |
386 | scm_i_program_properties (SCM program) | |
387 | #define FUNC_NAME "%program-properties" | |
e311f5fa AW |
388 | { |
389 | SCM meta; | |
390 | ||
391 | SCM_VALIDATE_PROGRAM (1, program); | |
392 | ||
393 | meta = scm_program_meta (program); | |
394 | if (scm_is_false (meta)) | |
395 | return SCM_EOL; | |
396 | ||
6c6a4439 | 397 | return scm_cdddr (scm_call_0 (meta)); |
e311f5fa AW |
398 | } |
399 | #undef FUNC_NAME | |
400 | ||
7c540297 AW |
401 | SCM |
402 | scm_program_source (SCM program, SCM ip, SCM sources) | |
b262b74b | 403 | { |
7c540297 | 404 | static SCM program_source = SCM_BOOL_F; |
b262b74b | 405 | |
7c540297 AW |
406 | if (scm_is_false (program_source)) { |
407 | if (!scm_module_system_booted_p) | |
408 | return SCM_BOOL_F; | |
409 | ||
410 | program_source = | |
411 | scm_c_private_variable ("system vm program", "program-source"); | |
412 | } | |
b262b74b | 413 | |
b262b74b | 414 | if (SCM_UNBNDP (sources)) |
7c540297 AW |
415 | return scm_call_2 (scm_variable_ref (program_source), program, ip); |
416 | else | |
417 | return scm_call_3 (scm_variable_ref (program_source), program, ip, sources); | |
028e3d06 | 418 | } |
028e3d06 | 419 | |
6f16379e | 420 | SCM_DEFINE (scm_program_num_free_variables, "program-num-free-variables", 1, 0, 0, |
17e90c5e KN |
421 | (SCM program), |
422 | "") | |
6f16379e AW |
423 | #define FUNC_NAME s_scm_program_num_free_variables |
424 | { | |
ee0a2b51 AW |
425 | if (SCM_RTL_PROGRAM_P (program)) { |
426 | return scm_from_ulong (SCM_RTL_PROGRAM_NUM_FREE_VARIABLES (program)); | |
427 | } | |
428 | ||
6f16379e AW |
429 | SCM_VALIDATE_PROGRAM (1, program); |
430 | return scm_from_ulong (SCM_PROGRAM_NUM_FREE_VARIABLES (program)); | |
431 | } | |
432 | #undef FUNC_NAME | |
433 | ||
434 | SCM_DEFINE (scm_program_free_variable_ref, "program-free-variable-ref", 2, 0, 0, | |
435 | (SCM program, SCM i), | |
436 | "") | |
437 | #define FUNC_NAME s_scm_program_free_variable_ref | |
438 | { | |
439 | unsigned long idx; | |
ee0a2b51 AW |
440 | |
441 | if (SCM_RTL_PROGRAM_P (program)) { | |
442 | SCM_VALIDATE_ULONG_COPY (2, i, idx); | |
443 | if (idx >= SCM_RTL_PROGRAM_NUM_FREE_VARIABLES (program)) | |
444 | SCM_OUT_OF_RANGE (2, i); | |
445 | return SCM_RTL_PROGRAM_FREE_VARIABLE_REF (program, idx); | |
446 | } | |
447 | ||
6f16379e AW |
448 | SCM_VALIDATE_PROGRAM (1, program); |
449 | SCM_VALIDATE_ULONG_COPY (2, i, idx); | |
450 | if (idx >= SCM_PROGRAM_NUM_FREE_VARIABLES (program)) | |
451 | SCM_OUT_OF_RANGE (2, i); | |
452 | return SCM_PROGRAM_FREE_VARIABLE_REF (program, idx); | |
453 | } | |
454 | #undef FUNC_NAME | |
455 | ||
456 | SCM_DEFINE (scm_program_free_variable_set_x, "program-free-variable-set!", 3, 0, 0, | |
457 | (SCM program, SCM i, SCM x), | |
458 | "") | |
459 | #define FUNC_NAME s_scm_program_free_variable_set_x | |
62082959 | 460 | { |
6f16379e | 461 | unsigned long idx; |
ee0a2b51 AW |
462 | |
463 | if (SCM_RTL_PROGRAM_P (program)) { | |
464 | SCM_VALIDATE_ULONG_COPY (2, i, idx); | |
465 | if (idx >= SCM_RTL_PROGRAM_NUM_FREE_VARIABLES (program)) | |
466 | SCM_OUT_OF_RANGE (2, i); | |
467 | SCM_RTL_PROGRAM_FREE_VARIABLE_SET (program, idx, x); | |
468 | return SCM_UNSPECIFIED; | |
469 | } | |
470 | ||
62082959 | 471 | SCM_VALIDATE_PROGRAM (1, program); |
6f16379e AW |
472 | SCM_VALIDATE_ULONG_COPY (2, i, idx); |
473 | if (idx >= SCM_PROGRAM_NUM_FREE_VARIABLES (program)) | |
474 | SCM_OUT_OF_RANGE (2, i); | |
475 | SCM_PROGRAM_FREE_VARIABLE_SET (program, idx, x); | |
476 | return SCM_UNSPECIFIED; | |
62082959 LC |
477 | } |
478 | #undef FUNC_NAME | |
479 | ||
53e28ed9 | 480 | SCM_DEFINE (scm_program_objcode, "program-objcode", 1, 0, 0, |
17e90c5e | 481 | (SCM program), |
53e28ed9 AW |
482 | "Return a @var{program}'s object code.") |
483 | #define FUNC_NAME s_scm_program_objcode | |
17e90c5e KN |
484 | { |
485 | SCM_VALIDATE_PROGRAM (1, program); | |
fa19602c | 486 | |
53e28ed9 | 487 | return SCM_PROGRAM_OBJCODE (program); |
17e90c5e KN |
488 | } |
489 | #undef FUNC_NAME | |
490 | ||
cb2ce548 AW |
491 | /* procedure-minimum-arity support. */ |
492 | static void | |
493 | parse_arity (SCM arity, int *req, int *opt, int *rest) | |
56164a5a | 494 | { |
cb2ce548 | 495 | SCM x = scm_cddr (arity); |
56164a5a | 496 | |
56164a5a AW |
497 | if (scm_is_pair (x)) |
498 | { | |
499 | *req = scm_to_int (scm_car (x)); | |
500 | x = scm_cdr (x); | |
501 | if (scm_is_pair (x)) | |
502 | { | |
503 | *opt = scm_to_int (scm_car (x)); | |
504 | x = scm_cdr (x); | |
505 | if (scm_is_pair (x)) | |
506 | *rest = scm_is_true (scm_car (x)); | |
507 | else | |
508 | *rest = 0; | |
509 | } | |
510 | else | |
511 | *opt = *rest = 0; | |
512 | } | |
513 | else | |
514 | *req = *opt = *rest = 0; | |
cb2ce548 AW |
515 | } |
516 | ||
eb2bc00f AW |
517 | static int |
518 | scm_i_rtl_program_minimum_arity (SCM program, int *req, int *opt, int *rest) | |
519 | { | |
520 | static SCM rtl_program_minimum_arity = SCM_BOOL_F; | |
521 | SCM l; | |
522 | ||
27337b63 AW |
523 | if (SCM_PRIMITIVE_P (program)) |
524 | return scm_i_primitive_arity (program, req, opt, rest); | |
525 | ||
b0ca878c AW |
526 | if (SCM_PROGRAM_IS_FOREIGN (program)) |
527 | return scm_i_foreign_arity (program, req, opt, rest); | |
528 | ||
d76de871 AW |
529 | if (SCM_PROGRAM_IS_CONTINUATION (program) |
530 | || SCM_PROGRAM_IS_PARTIAL_CONTINUATION (program)) | |
d691ac20 AW |
531 | { |
532 | *req = *opt = 0; | |
533 | *rest = 1; | |
534 | return 1; | |
535 | } | |
536 | ||
eb2bc00f AW |
537 | if (scm_is_false (rtl_program_minimum_arity) && scm_module_system_booted_p) |
538 | rtl_program_minimum_arity = | |
081cf910 | 539 | scm_c_private_variable ("system vm program", |
eb2bc00f AW |
540 | "rtl-program-minimum-arity"); |
541 | ||
542 | l = scm_call_1 (scm_variable_ref (rtl_program_minimum_arity), program); | |
543 | if (scm_is_false (l)) | |
544 | return 0; | |
545 | ||
546 | *req = scm_to_int (scm_car (l)); | |
547 | *opt = scm_to_int (scm_cadr (l)); | |
548 | *rest = scm_is_true (scm_caddr (l)); | |
549 | ||
550 | return 1; | |
551 | } | |
552 | ||
cb2ce548 AW |
553 | int |
554 | scm_i_program_arity (SCM program, int *req, int *opt, int *rest) | |
555 | { | |
556 | SCM arities; | |
557 | ||
eb2bc00f AW |
558 | if (SCM_RTL_PROGRAM_P (program)) |
559 | return scm_i_rtl_program_minimum_arity (program, req, opt, rest); | |
560 | ||
cb2ce548 AW |
561 | arities = scm_program_arities (program); |
562 | if (!scm_is_pair (arities)) | |
563 | return 0; | |
564 | ||
565 | parse_arity (scm_car (arities), req, opt, rest); | |
566 | arities = scm_cdr (arities); | |
567 | ||
568 | for (; scm_is_pair (arities); arities = scm_cdr (arities)) | |
569 | { | |
570 | int thisreq, thisopt, thisrest; | |
571 | ||
572 | parse_arity (scm_car (arities), &thisreq, &thisopt, &thisrest); | |
573 | ||
574 | if (thisreq < *req | |
575 | || (thisreq == *req | |
576 | && ((thisrest && (!*rest || thisopt > *opt)) | |
577 | || (!thisrest && !*rest && thisopt > *opt)))) | |
578 | { | |
579 | *req = thisreq; | |
580 | *opt = thisopt; | |
581 | *rest = thisrest; | |
582 | } | |
583 | } | |
584 | ||
56164a5a AW |
585 | return 1; |
586 | } | |
fa19602c | 587 | |
17e90c5e | 588 | \f |
56164a5a | 589 | |
17e90c5e | 590 | void |
07e56b27 | 591 | scm_bootstrap_programs (void) |
17e90c5e | 592 | { |
44602b08 AW |
593 | scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION, |
594 | "scm_init_programs", | |
60ae5ca2 | 595 | (scm_t_extension_init_func)scm_init_programs, NULL); |
07e56b27 | 596 | } |
17e90c5e | 597 | |
07e56b27 AW |
598 | void |
599 | scm_init_programs (void) | |
600 | { | |
17e90c5e | 601 | #ifndef SCM_MAGIC_SNARFER |
aeeff258 | 602 | #include "libguile/programs.x" |
17e90c5e KN |
603 | #endif |
604 | } | |
605 | ||
606 | /* | |
607 | Local Variables: | |
608 | c-file-style: "gnu" | |
609 | End: | |
610 | */ |