procedure-documentation works on RTL procedures
[bpt/guile.git] / libguile / programs.c
1 /* Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
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.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
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
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
17 */
18
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <string.h>
24 #include "_scm.h"
25 #include "instructions.h"
26 #include "modules.h"
27 #include "programs.h"
28 #include "procprop.h" /* scm_sym_name */
29 #include "srcprop.h" /* scm_sym_filename */
30 #include "vm.h"
31
32 \f
33 static SCM write_program = SCM_BOOL_F;
34
35 SCM_DEFINE (scm_make_program, "make-program", 1, 2, 0,
36 (SCM objcode, SCM objtable, SCM free_variables),
37 "")
38 #define FUNC_NAME s_scm_make_program
39 {
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);
45
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 }
69 }
70 #undef FUNC_NAME
71
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
109 /* FIXME: we need scm_from_uintptr (). */
110 return scm_from_size_t ((size_t) SCM_RTL_PROGRAM_CODE (program));
111 }
112 #undef FUNC_NAME
113
114 SCM
115 scm_i_rtl_program_name (SCM program)
116 {
117 static SCM rtl_program_name = SCM_BOOL_F;
118
119 if (scm_is_false (rtl_program_name) && scm_module_system_booted_p)
120 rtl_program_name =
121 scm_c_private_variable ("system vm program", "rtl-program-name");
122
123 return scm_call_1 (scm_variable_ref (rtl_program_name), program);
124 }
125
126 SCM
127 scm_i_rtl_program_documentation (SCM program)
128 {
129 static SCM rtl_program_documentation = SCM_BOOL_F;
130
131 if (scm_is_false (rtl_program_documentation) && scm_module_system_booted_p)
132 rtl_program_documentation =
133 scm_c_private_variable ("system vm program",
134 "rtl-program-documentation");
135
136 return scm_call_1 (scm_variable_ref (rtl_program_documentation), program);
137 }
138
139 void
140 scm_i_program_print (SCM program, SCM port, scm_print_state *pstate)
141 {
142 static int print_error = 0;
143
144 if (scm_is_false (write_program) && scm_module_system_booted_p)
145 write_program = scm_c_private_variable ("system vm program",
146 "write-program");
147
148 if (SCM_PROGRAM_IS_CONTINUATION (program))
149 {
150 /* twingliness */
151 scm_puts_unlocked ("#<continuation ", port);
152 scm_uintprint (SCM_UNPACK (program), 16, port);
153 scm_putc_unlocked ('>', port);
154 }
155 else if (SCM_PROGRAM_IS_PARTIAL_CONTINUATION (program))
156 {
157 /* twingliness */
158 scm_puts_unlocked ("#<partial-continuation ", port);
159 scm_uintprint (SCM_UNPACK (program), 16, port);
160 scm_putc_unlocked ('>', port);
161 }
162 else if (scm_is_false (write_program) || print_error)
163 {
164 if (SCM_RTL_PROGRAM_P (program))
165 {
166 scm_puts_unlocked ("#<rtl-program ", port);
167 scm_uintprint (SCM_UNPACK (program), 16, port);
168 scm_putc_unlocked (' ', port);
169 scm_uintprint ((scm_t_uintptr) SCM_RTL_PROGRAM_CODE (program), 16, port);
170 scm_putc_unlocked ('>', port);
171 }
172 else
173 {
174 scm_puts_unlocked ("#<program ", port);
175 scm_uintprint (SCM_UNPACK (program), 16, port);
176 scm_putc_unlocked ('>', port);
177 }
178 }
179 else
180 {
181 print_error = 1;
182 scm_call_2 (SCM_VARIABLE_REF (write_program), program, port);
183 print_error = 0;
184 }
185 }
186
187 \f
188 /*
189 * Scheme interface
190 */
191
192 SCM_DEFINE (scm_program_p, "program?", 1, 0, 0,
193 (SCM obj),
194 "")
195 #define FUNC_NAME s_scm_program_p
196 {
197 return scm_from_bool (SCM_PROGRAM_P (obj));
198 }
199 #undef FUNC_NAME
200
201 SCM_DEFINE (scm_rtl_program_p, "rtl-program?", 1, 0, 0,
202 (SCM obj),
203 "")
204 #define FUNC_NAME s_scm_rtl_program_p
205 {
206 return scm_from_bool (SCM_RTL_PROGRAM_P (obj));
207 }
208 #undef FUNC_NAME
209
210 SCM_DEFINE (scm_program_base, "program-base", 1, 0, 0,
211 (SCM program),
212 "")
213 #define FUNC_NAME s_scm_program_base
214 {
215 const struct scm_objcode *c_objcode;
216
217 SCM_VALIDATE_PROGRAM (1, program);
218
219 c_objcode = SCM_PROGRAM_DATA (program);
220 return scm_from_unsigned_integer ((scm_t_bits) SCM_C_OBJCODE_BASE (c_objcode));
221 }
222 #undef FUNC_NAME
223
224 SCM_DEFINE (scm_program_objects, "program-objects", 1, 0, 0,
225 (SCM program),
226 "")
227 #define FUNC_NAME s_scm_program_objects
228 {
229 SCM_VALIDATE_PROGRAM (1, program);
230 return SCM_PROGRAM_OBJTABLE (program);
231 }
232 #undef FUNC_NAME
233
234 SCM_DEFINE (scm_program_module, "program-module", 1, 0, 0,
235 (SCM program),
236 "")
237 #define FUNC_NAME s_scm_program_module
238 {
239 SCM objs, mod;
240 SCM_VALIDATE_PROGRAM (1, program);
241 objs = SCM_PROGRAM_OBJTABLE (program);
242 /* If a program is the result of compiling GLIL to assembly, then if
243 it has an objtable, the first entry will be a module. But some
244 programs are hand-coded trampolines, like boot programs and
245 primitives and the like. So if a program happens to have a
246 non-module in the first slot of the objtable, assume that it is
247 such a trampoline, and just return #f for the module. */
248 mod = scm_is_true (objs) ? scm_c_vector_ref (objs, 0) : SCM_BOOL_F;
249 return SCM_MODULEP (mod) ? mod : SCM_BOOL_F;
250 }
251 #undef FUNC_NAME
252
253 SCM_DEFINE (scm_program_meta, "program-meta", 1, 0, 0,
254 (SCM program),
255 "")
256 #define FUNC_NAME s_scm_program_meta
257 {
258 SCM metaobj;
259
260 SCM_VALIDATE_PROGRAM (1, program);
261
262 metaobj = scm_objcode_meta (SCM_PROGRAM_OBJCODE (program));
263 if (scm_is_true (metaobj))
264 return scm_make_program (metaobj, SCM_PROGRAM_OBJTABLE (program),
265 SCM_BOOL_F);
266 else
267 return SCM_BOOL_F;
268 }
269 #undef FUNC_NAME
270
271 SCM_DEFINE (scm_program_bindings, "program-bindings", 1, 0, 0,
272 (SCM program),
273 "")
274 #define FUNC_NAME s_scm_program_bindings
275 {
276 SCM meta;
277
278 SCM_VALIDATE_PROGRAM (1, program);
279
280 meta = scm_program_meta (program);
281 if (scm_is_false (meta))
282 return SCM_BOOL_F;
283
284 return scm_car (scm_call_0 (meta));
285 }
286 #undef FUNC_NAME
287
288 SCM_DEFINE (scm_program_sources, "program-sources", 1, 0, 0,
289 (SCM program),
290 "")
291 #define FUNC_NAME s_scm_program_sources
292 {
293 SCM meta, sources, ret, filename;
294
295 SCM_VALIDATE_PROGRAM (1, program);
296
297 meta = scm_program_meta (program);
298 if (scm_is_false (meta))
299 return SCM_EOL;
300
301 filename = SCM_BOOL_F;
302 ret = SCM_EOL;
303 for (sources = scm_cadr (scm_call_0 (meta)); !scm_is_null (sources);
304 sources = scm_cdr (sources))
305 {
306 SCM x = scm_car (sources);
307 if (scm_is_pair (x))
308 {
309 if (scm_is_number (scm_car (x)))
310 {
311 SCM addr = scm_car (x);
312 ret = scm_acons (addr, scm_cons (filename, scm_cdr (x)),
313 ret);
314 }
315 else if (scm_is_eq (scm_car (x), scm_sym_filename))
316 filename = scm_cdr (x);
317 }
318 }
319 return scm_reverse_x (ret, SCM_UNDEFINED);
320 }
321 #undef FUNC_NAME
322
323 SCM_DEFINE (scm_program_arities, "program-arities", 1, 0, 0,
324 (SCM program),
325 "")
326 #define FUNC_NAME s_scm_program_arities
327 {
328 SCM meta;
329
330 SCM_VALIDATE_PROGRAM (1, program);
331
332 meta = scm_program_meta (program);
333 if (scm_is_false (meta))
334 return SCM_BOOL_F;
335
336 return scm_caddr (scm_call_0 (meta));
337 }
338 #undef FUNC_NAME
339
340 SCM
341 scm_i_program_properties (SCM program)
342 #define FUNC_NAME "%program-properties"
343 {
344 SCM meta;
345
346 SCM_VALIDATE_PROGRAM (1, program);
347
348 meta = scm_program_meta (program);
349 if (scm_is_false (meta))
350 return SCM_EOL;
351
352 return scm_cdddr (scm_call_0 (meta));
353 }
354 #undef FUNC_NAME
355
356 static SCM
357 program_source (SCM program, size_t ip, SCM sources)
358 {
359 SCM source = SCM_BOOL_F;
360
361 while (!scm_is_null (sources)
362 && scm_to_size_t (scm_caar (sources)) <= ip)
363 {
364 source = scm_car (sources);
365 sources = scm_cdr (sources);
366 }
367
368 return source; /* (addr . (filename . (line . column))) */
369 }
370
371 SCM_DEFINE (scm_program_source, "program-source", 2, 1, 0,
372 (SCM program, SCM ip, SCM sources),
373 "")
374 #define FUNC_NAME s_scm_program_source
375 {
376 SCM_VALIDATE_PROGRAM (1, program);
377 if (SCM_UNBNDP (sources))
378 sources = scm_program_sources (program);
379 return program_source (program, scm_to_size_t (ip), sources);
380 }
381 #undef FUNC_NAME
382
383 extern SCM
384 scm_c_program_source (SCM program, size_t ip)
385 {
386 return program_source (program, ip, scm_program_sources (program));
387 }
388
389 SCM_DEFINE (scm_program_num_free_variables, "program-num-free-variables", 1, 0, 0,
390 (SCM program),
391 "")
392 #define FUNC_NAME s_scm_program_num_free_variables
393 {
394 SCM_VALIDATE_PROGRAM (1, program);
395 return scm_from_ulong (SCM_PROGRAM_NUM_FREE_VARIABLES (program));
396 }
397 #undef FUNC_NAME
398
399 SCM_DEFINE (scm_program_free_variable_ref, "program-free-variable-ref", 2, 0, 0,
400 (SCM program, SCM i),
401 "")
402 #define FUNC_NAME s_scm_program_free_variable_ref
403 {
404 unsigned long idx;
405 SCM_VALIDATE_PROGRAM (1, program);
406 SCM_VALIDATE_ULONG_COPY (2, i, idx);
407 if (idx >= SCM_PROGRAM_NUM_FREE_VARIABLES (program))
408 SCM_OUT_OF_RANGE (2, i);
409 return SCM_PROGRAM_FREE_VARIABLE_REF (program, idx);
410 }
411 #undef FUNC_NAME
412
413 SCM_DEFINE (scm_program_free_variable_set_x, "program-free-variable-set!", 3, 0, 0,
414 (SCM program, SCM i, SCM x),
415 "")
416 #define FUNC_NAME s_scm_program_free_variable_set_x
417 {
418 unsigned long idx;
419 SCM_VALIDATE_PROGRAM (1, program);
420 SCM_VALIDATE_ULONG_COPY (2, i, idx);
421 if (idx >= SCM_PROGRAM_NUM_FREE_VARIABLES (program))
422 SCM_OUT_OF_RANGE (2, i);
423 SCM_PROGRAM_FREE_VARIABLE_SET (program, idx, x);
424 return SCM_UNSPECIFIED;
425 }
426 #undef FUNC_NAME
427
428 SCM_DEFINE (scm_program_objcode, "program-objcode", 1, 0, 0,
429 (SCM program),
430 "Return a @var{program}'s object code.")
431 #define FUNC_NAME s_scm_program_objcode
432 {
433 SCM_VALIDATE_PROGRAM (1, program);
434
435 return SCM_PROGRAM_OBJCODE (program);
436 }
437 #undef FUNC_NAME
438
439 /* procedure-minimum-arity support. */
440 static void
441 parse_arity (SCM arity, int *req, int *opt, int *rest)
442 {
443 SCM x = scm_cddr (arity);
444
445 if (scm_is_pair (x))
446 {
447 *req = scm_to_int (scm_car (x));
448 x = scm_cdr (x);
449 if (scm_is_pair (x))
450 {
451 *opt = scm_to_int (scm_car (x));
452 x = scm_cdr (x);
453 if (scm_is_pair (x))
454 *rest = scm_is_true (scm_car (x));
455 else
456 *rest = 0;
457 }
458 else
459 *opt = *rest = 0;
460 }
461 else
462 *req = *opt = *rest = 0;
463 }
464
465 static int
466 scm_i_rtl_program_minimum_arity (SCM program, int *req, int *opt, int *rest)
467 {
468 static SCM rtl_program_minimum_arity = SCM_BOOL_F;
469 SCM l;
470
471 if (scm_is_false (rtl_program_minimum_arity) && scm_module_system_booted_p)
472 rtl_program_minimum_arity =
473 scm_c_private_variable ("system vm debug",
474 "rtl-program-minimum-arity");
475
476 l = scm_call_1 (scm_variable_ref (rtl_program_minimum_arity), program);
477 if (scm_is_false (l))
478 return 0;
479
480 *req = scm_to_int (scm_car (l));
481 *opt = scm_to_int (scm_cadr (l));
482 *rest = scm_is_true (scm_caddr (l));
483
484 return 1;
485 }
486
487 int
488 scm_i_program_arity (SCM program, int *req, int *opt, int *rest)
489 {
490 SCM arities;
491
492 if (SCM_RTL_PROGRAM_P (program))
493 return scm_i_rtl_program_minimum_arity (program, req, opt, rest);
494
495 arities = scm_program_arities (program);
496 if (!scm_is_pair (arities))
497 return 0;
498
499 parse_arity (scm_car (arities), req, opt, rest);
500 arities = scm_cdr (arities);
501
502 for (; scm_is_pair (arities); arities = scm_cdr (arities))
503 {
504 int thisreq, thisopt, thisrest;
505
506 parse_arity (scm_car (arities), &thisreq, &thisopt, &thisrest);
507
508 if (thisreq < *req
509 || (thisreq == *req
510 && ((thisrest && (!*rest || thisopt > *opt))
511 || (!thisrest && !*rest && thisopt > *opt))))
512 {
513 *req = thisreq;
514 *opt = thisopt;
515 *rest = thisrest;
516 }
517 }
518
519 return 1;
520 }
521
522 \f
523
524 void
525 scm_bootstrap_programs (void)
526 {
527 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
528 "scm_init_programs",
529 (scm_t_extension_init_func)scm_init_programs, NULL);
530 }
531
532 void
533 scm_init_programs (void)
534 {
535 #ifndef SCM_MAGIC_SNARFER
536 #include "libguile/programs.x"
537 #endif
538 }
539
540 /*
541 Local Variables:
542 c-file-style: "gnu"
543 End:
544 */