Fix reading and writing arities into DWARF.
[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 return scm_from_uintptr_t ((scm_t_uintptr) SCM_RTL_PROGRAM_CODE (program));
110 }
111 #undef FUNC_NAME
112
113 SCM
114 scm_i_rtl_program_name (SCM program)
115 {
116 static SCM rtl_program_name = SCM_BOOL_F;
117
118 if (SCM_PRIMITIVE_P (program))
119 return SCM_SUBR_NAME (program);
120
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);
126 }
127
128 SCM
129 scm_i_rtl_program_documentation (SCM program)
130 {
131 static SCM rtl_program_documentation = SCM_BOOL_F;
132
133 if (SCM_PRIMITIVE_P (program))
134 return SCM_BOOL_F;
135
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
144 SCM
145 scm_i_rtl_program_properties (SCM program)
146 {
147 static SCM rtl_program_properties = SCM_BOOL_F;
148
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
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
164 void
165 scm_i_program_print (SCM program, SCM port, scm_print_state *pstate)
166 {
167 static int print_error = 0;
168
169 if (scm_is_false (write_program) && scm_module_system_booted_p)
170 write_program = scm_c_private_variable ("system vm program",
171 "write-program");
172
173 if (SCM_PROGRAM_IS_CONTINUATION (program))
174 {
175 /* twingliness */
176 scm_puts_unlocked ("#<continuation ", port);
177 scm_uintprint (SCM_UNPACK (program), 16, port);
178 scm_putc_unlocked ('>', port);
179 }
180 else if (SCM_PROGRAM_IS_PARTIAL_CONTINUATION (program))
181 {
182 /* twingliness */
183 scm_puts_unlocked ("#<partial-continuation ", port);
184 scm_uintprint (SCM_UNPACK (program), 16, port);
185 scm_putc_unlocked ('>', port);
186 }
187 else if (scm_is_false (write_program) || print_error)
188 {
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 }
203 }
204 else
205 {
206 print_error = 1;
207 scm_call_2 (SCM_VARIABLE_REF (write_program), program, port);
208 print_error = 0;
209 }
210 }
211
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 {
222 return scm_from_bool (SCM_PROGRAM_P (obj));
223 }
224 #undef FUNC_NAME
225
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
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_uintptr_t (scm_i_primitive_call_ip (prim));
252 }
253 #undef FUNC_NAME
254
255 SCM_DEFINE (scm_program_base, "program-base", 1, 0, 0,
256 (SCM program),
257 "")
258 #define FUNC_NAME s_scm_program_base
259 {
260 const struct scm_objcode *c_objcode;
261
262 SCM_VALIDATE_PROGRAM (1, program);
263
264 c_objcode = SCM_PROGRAM_DATA (program);
265 return scm_from_unsigned_integer ((scm_t_bits) SCM_C_OBJCODE_BASE (c_objcode));
266 }
267 #undef FUNC_NAME
268
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 {
284 SCM objs, mod;
285 SCM_VALIDATE_PROGRAM (1, program);
286 objs = SCM_PROGRAM_OBJTABLE (program);
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;
295 }
296 #undef FUNC_NAME
297
298 SCM_DEFINE (scm_program_meta, "program-meta", 1, 0, 0,
299 (SCM program),
300 "")
301 #define FUNC_NAME s_scm_program_meta
302 {
303 SCM metaobj;
304
305 SCM_VALIDATE_PROGRAM (1, program);
306
307 metaobj = scm_objcode_meta (SCM_PROGRAM_OBJCODE (program));
308 if (scm_is_true (metaobj))
309 return scm_make_program (metaobj, SCM_PROGRAM_OBJTABLE (program),
310 SCM_BOOL_F);
311 else
312 return SCM_BOOL_F;
313 }
314 #undef FUNC_NAME
315
316 SCM_DEFINE (scm_program_bindings, "program-bindings", 1, 0, 0,
317 (SCM program),
318 "")
319 #define FUNC_NAME s_scm_program_bindings
320 {
321 SCM meta;
322
323 SCM_VALIDATE_PROGRAM (1, program);
324
325 meta = scm_program_meta (program);
326 if (scm_is_false (meta))
327 return SCM_BOOL_F;
328
329 return scm_car (scm_call_0 (meta));
330 }
331 #undef FUNC_NAME
332
333 SCM_DEFINE (scm_program_sources, "%program-sources", 1, 0, 0,
334 (SCM program),
335 "")
336 #define FUNC_NAME s_scm_program_sources
337 {
338 SCM meta, sources, ret, filename;
339
340 SCM_VALIDATE_PROGRAM (1, program);
341
342 meta = scm_program_meta (program);
343 if (scm_is_false (meta))
344 return SCM_EOL;
345
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);
365 }
366 #undef FUNC_NAME
367
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
385 SCM
386 scm_i_program_properties (SCM program)
387 #define FUNC_NAME "%program-properties"
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
397 return scm_cdddr (scm_call_0 (meta));
398 }
399 #undef FUNC_NAME
400
401 SCM
402 scm_find_source_for_addr (SCM ip)
403 {
404 static SCM source_for_addr = SCM_BOOL_F;
405
406 if (scm_is_false (source_for_addr)) {
407 if (!scm_module_system_booted_p)
408 return SCM_BOOL_F;
409
410 source_for_addr =
411 scm_c_private_variable ("system vm program", "source-for-addr");
412 }
413
414 return scm_call_1 (scm_variable_ref (source_for_addr), ip);
415 }
416
417 SCM
418 scm_program_source (SCM program, SCM ip, SCM sources)
419 {
420 static SCM program_source = SCM_BOOL_F;
421
422 if (scm_is_false (program_source)) {
423 if (!scm_module_system_booted_p)
424 return SCM_BOOL_F;
425
426 program_source =
427 scm_c_private_variable ("system vm program", "program-source");
428 }
429
430 if (SCM_UNBNDP (sources))
431 return scm_call_2 (scm_variable_ref (program_source), program, ip);
432 else
433 return scm_call_3 (scm_variable_ref (program_source), program, ip, sources);
434 }
435
436 SCM_DEFINE (scm_program_num_free_variables, "program-num-free-variables", 1, 0, 0,
437 (SCM program),
438 "")
439 #define FUNC_NAME s_scm_program_num_free_variables
440 {
441 if (SCM_RTL_PROGRAM_P (program)) {
442 return scm_from_ulong (SCM_RTL_PROGRAM_NUM_FREE_VARIABLES (program));
443 }
444
445 SCM_VALIDATE_PROGRAM (1, program);
446 return scm_from_ulong (SCM_PROGRAM_NUM_FREE_VARIABLES (program));
447 }
448 #undef FUNC_NAME
449
450 SCM_DEFINE (scm_program_free_variable_ref, "program-free-variable-ref", 2, 0, 0,
451 (SCM program, SCM i),
452 "")
453 #define FUNC_NAME s_scm_program_free_variable_ref
454 {
455 unsigned long idx;
456
457 if (SCM_RTL_PROGRAM_P (program)) {
458 SCM_VALIDATE_ULONG_COPY (2, i, idx);
459 if (idx >= SCM_RTL_PROGRAM_NUM_FREE_VARIABLES (program))
460 SCM_OUT_OF_RANGE (2, i);
461 return SCM_RTL_PROGRAM_FREE_VARIABLE_REF (program, idx);
462 }
463
464 SCM_VALIDATE_PROGRAM (1, program);
465 SCM_VALIDATE_ULONG_COPY (2, i, idx);
466 if (idx >= SCM_PROGRAM_NUM_FREE_VARIABLES (program))
467 SCM_OUT_OF_RANGE (2, i);
468 return SCM_PROGRAM_FREE_VARIABLE_REF (program, idx);
469 }
470 #undef FUNC_NAME
471
472 SCM_DEFINE (scm_program_free_variable_set_x, "program-free-variable-set!", 3, 0, 0,
473 (SCM program, SCM i, SCM x),
474 "")
475 #define FUNC_NAME s_scm_program_free_variable_set_x
476 {
477 unsigned long idx;
478
479 if (SCM_RTL_PROGRAM_P (program)) {
480 SCM_VALIDATE_ULONG_COPY (2, i, idx);
481 if (idx >= SCM_RTL_PROGRAM_NUM_FREE_VARIABLES (program))
482 SCM_OUT_OF_RANGE (2, i);
483 SCM_RTL_PROGRAM_FREE_VARIABLE_SET (program, idx, x);
484 return SCM_UNSPECIFIED;
485 }
486
487 SCM_VALIDATE_PROGRAM (1, program);
488 SCM_VALIDATE_ULONG_COPY (2, i, idx);
489 if (idx >= SCM_PROGRAM_NUM_FREE_VARIABLES (program))
490 SCM_OUT_OF_RANGE (2, i);
491 SCM_PROGRAM_FREE_VARIABLE_SET (program, idx, x);
492 return SCM_UNSPECIFIED;
493 }
494 #undef FUNC_NAME
495
496 SCM_DEFINE (scm_program_objcode, "program-objcode", 1, 0, 0,
497 (SCM program),
498 "Return a @var{program}'s object code.")
499 #define FUNC_NAME s_scm_program_objcode
500 {
501 SCM_VALIDATE_PROGRAM (1, program);
502
503 return SCM_PROGRAM_OBJCODE (program);
504 }
505 #undef FUNC_NAME
506
507 /* procedure-minimum-arity support. */
508 static void
509 parse_arity (SCM arity, int *req, int *opt, int *rest)
510 {
511 SCM x = scm_cddr (arity);
512
513 if (scm_is_pair (x))
514 {
515 *req = scm_to_int (scm_car (x));
516 x = scm_cdr (x);
517 if (scm_is_pair (x))
518 {
519 *opt = scm_to_int (scm_car (x));
520 x = scm_cdr (x);
521 if (scm_is_pair (x))
522 *rest = scm_is_true (scm_car (x));
523 else
524 *rest = 0;
525 }
526 else
527 *opt = *rest = 0;
528 }
529 else
530 *req = *opt = *rest = 0;
531 }
532
533 static int
534 scm_i_rtl_program_minimum_arity (SCM program, int *req, int *opt, int *rest)
535 {
536 static SCM rtl_program_minimum_arity = SCM_BOOL_F;
537 SCM l;
538
539 if (SCM_PRIMITIVE_P (program))
540 return scm_i_primitive_arity (program, req, opt, rest);
541
542 if (SCM_PROGRAM_IS_FOREIGN (program))
543 return scm_i_foreign_arity (program, req, opt, rest);
544
545 if (SCM_PROGRAM_IS_CONTINUATION (program)
546 || SCM_PROGRAM_IS_PARTIAL_CONTINUATION (program))
547 {
548 *req = *opt = 0;
549 *rest = 1;
550 return 1;
551 }
552
553 if (scm_is_false (rtl_program_minimum_arity) && scm_module_system_booted_p)
554 rtl_program_minimum_arity =
555 scm_c_private_variable ("system vm program",
556 "rtl-program-minimum-arity");
557
558 l = scm_call_1 (scm_variable_ref (rtl_program_minimum_arity), program);
559 if (scm_is_false (l))
560 return 0;
561
562 *req = scm_to_int (scm_car (l));
563 *opt = scm_to_int (scm_cadr (l));
564 *rest = scm_is_true (scm_caddr (l));
565
566 return 1;
567 }
568
569 int
570 scm_i_program_arity (SCM program, int *req, int *opt, int *rest)
571 {
572 SCM arities;
573
574 if (SCM_RTL_PROGRAM_P (program))
575 return scm_i_rtl_program_minimum_arity (program, req, opt, rest);
576
577 arities = scm_program_arities (program);
578 if (!scm_is_pair (arities))
579 return 0;
580
581 parse_arity (scm_car (arities), req, opt, rest);
582 arities = scm_cdr (arities);
583
584 for (; scm_is_pair (arities); arities = scm_cdr (arities))
585 {
586 int thisreq, thisopt, thisrest;
587
588 parse_arity (scm_car (arities), &thisreq, &thisopt, &thisrest);
589
590 if (thisreq < *req
591 || (thisreq == *req
592 && ((thisrest && (!*rest || thisopt > *opt))
593 || (!thisrest && !*rest && thisopt > *opt))))
594 {
595 *req = thisreq;
596 *opt = thisopt;
597 *rest = thisrest;
598 }
599 }
600
601 return 1;
602 }
603
604 \f
605
606 void
607 scm_bootstrap_programs (void)
608 {
609 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
610 "scm_init_programs",
611 (scm_t_extension_init_func)scm_init_programs, NULL);
612 }
613
614 void
615 scm_init_programs (void)
616 {
617 #ifndef SCM_MAGIC_SNARFER
618 #include "libguile/programs.x"
619 #endif
620 }
621
622 /*
623 Local Variables:
624 c-file-style: "gnu"
625 End:
626 */