Write docstrings into RTL ELF images
[bpt/guile.git] / libguile / programs.c
CommitLineData
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 33static SCM write_program = SCM_BOOL_F;
17e90c5e 34
53e28ed9 35SCM_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
72SCM_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
102SCM_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
e65f80af
AW
114SCM
115scm_i_rtl_program_name (SCM program)
510ca126 116{
e65f80af
AW
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);
510ca126
AW
124}
125
2fb924f6
AW
126void
127scm_i_program_print (SCM program, SCM port, scm_print_state *pstate)
e6fea618 128{
0ba8bb71
AW
129 static int print_error = 0;
130
5c8cefe5 131 if (scm_is_false (write_program) && scm_module_system_booted_p)
eb2bc00f
AW
132 write_program = scm_c_private_variable ("system vm program",
133 "write-program");
e6fea618 134
1d1cae0e
AW
135 if (SCM_PROGRAM_IS_CONTINUATION (program))
136 {
137 /* twingliness */
0607ebbf 138 scm_puts_unlocked ("#<continuation ", port);
76e38162 139 scm_uintprint (SCM_UNPACK (program), 16, port);
0607ebbf 140 scm_putc_unlocked ('>', port);
1d1cae0e 141 }
5c606217 142 else if (SCM_PROGRAM_IS_PARTIAL_CONTINUATION (program))
2150e9a8
AW
143 {
144 /* twingliness */
0607ebbf 145 scm_puts_unlocked ("#<partial-continuation ", port);
2150e9a8 146 scm_uintprint (SCM_UNPACK (program), 16, port);
0607ebbf 147 scm_putc_unlocked ('>', port);
2150e9a8 148 }
1d1cae0e 149 else if (scm_is_false (write_program) || print_error)
2fb924f6 150 {
e65f80af
AW
151 if (SCM_RTL_PROGRAM_P (program))
152 {
153 scm_puts_unlocked ("#<rtl-program ", port);
154 scm_uintprint (SCM_UNPACK (program), 16, port);
155 scm_putc_unlocked (' ', port);
156 scm_uintprint ((scm_t_uintptr) SCM_RTL_PROGRAM_CODE (program), 16, port);
157 scm_putc_unlocked ('>', port);
158 }
159 else
160 {
161 scm_puts_unlocked ("#<program ", port);
162 scm_uintprint (SCM_UNPACK (program), 16, port);
163 scm_putc_unlocked ('>', port);
164 }
2fb924f6
AW
165 }
166 else
167 {
168 print_error = 1;
169 scm_call_2 (SCM_VARIABLE_REF (write_program), program, port);
170 print_error = 0;
171 }
e6fea618
AW
172}
173
17e90c5e
KN
174\f
175/*
176 * Scheme interface
177 */
178
179SCM_DEFINE (scm_program_p, "program?", 1, 0, 0,
180 (SCM obj),
181 "")
182#define FUNC_NAME s_scm_program_p
183{
5c8cefe5 184 return scm_from_bool (SCM_PROGRAM_P (obj));
17e90c5e
KN
185}
186#undef FUNC_NAME
187
510ca126
AW
188SCM_DEFINE (scm_rtl_program_p, "rtl-program?", 1, 0, 0,
189 (SCM obj),
190 "")
191#define FUNC_NAME s_scm_rtl_program_p
192{
193 return scm_from_bool (SCM_RTL_PROGRAM_P (obj));
194}
195#undef FUNC_NAME
196
ac99cb0c
KN
197SCM_DEFINE (scm_program_base, "program-base", 1, 0, 0,
198 (SCM program),
199 "")
200#define FUNC_NAME s_scm_program_base
201{
3dbbe28d
LC
202 const struct scm_objcode *c_objcode;
203
ac99cb0c
KN
204 SCM_VALIDATE_PROGRAM (1, program);
205
3dbbe28d 206 c_objcode = SCM_PROGRAM_DATA (program);
3d27ef4b 207 return scm_from_unsigned_integer ((scm_t_bits) SCM_C_OBJCODE_BASE (c_objcode));
ac99cb0c
KN
208}
209#undef FUNC_NAME
210
53e28ed9
AW
211SCM_DEFINE (scm_program_objects, "program-objects", 1, 0, 0,
212 (SCM program),
213 "")
214#define FUNC_NAME s_scm_program_objects
215{
216 SCM_VALIDATE_PROGRAM (1, program);
217 return SCM_PROGRAM_OBJTABLE (program);
218}
219#undef FUNC_NAME
220
221SCM_DEFINE (scm_program_module, "program-module", 1, 0, 0,
222 (SCM program),
223 "")
224#define FUNC_NAME s_scm_program_module
225{
7884975a 226 SCM objs, mod;
53e28ed9
AW
227 SCM_VALIDATE_PROGRAM (1, program);
228 objs = SCM_PROGRAM_OBJTABLE (program);
7884975a
AW
229 /* If a program is the result of compiling GLIL to assembly, then if
230 it has an objtable, the first entry will be a module. But some
231 programs are hand-coded trampolines, like boot programs and
232 primitives and the like. So if a program happens to have a
233 non-module in the first slot of the objtable, assume that it is
234 such a trampoline, and just return #f for the module. */
235 mod = scm_is_true (objs) ? scm_c_vector_ref (objs, 0) : SCM_BOOL_F;
236 return SCM_MODULEP (mod) ? mod : SCM_BOOL_F;
53e28ed9
AW
237}
238#undef FUNC_NAME
239
ac99cb0c
KN
240SCM_DEFINE (scm_program_meta, "program-meta", 1, 0, 0,
241 (SCM program),
242 "")
243#define FUNC_NAME s_scm_program_meta
17e90c5e 244{
ac47d5f6
AW
245 SCM metaobj;
246
17e90c5e 247 SCM_VALIDATE_PROGRAM (1, program);
ac47d5f6
AW
248
249 metaobj = scm_objcode_meta (SCM_PROGRAM_OBJCODE (program));
250 if (scm_is_true (metaobj))
31a26df2
AW
251 return scm_make_program (metaobj, SCM_PROGRAM_OBJTABLE (program),
252 SCM_BOOL_F);
ac47d5f6
AW
253 else
254 return SCM_BOOL_F;
17e90c5e
KN
255}
256#undef FUNC_NAME
257
e311f5fa
AW
258SCM_DEFINE (scm_program_bindings, "program-bindings", 1, 0, 0,
259 (SCM program),
260 "")
261#define FUNC_NAME s_scm_program_bindings
9a9f6487 262{
e311f5fa
AW
263 SCM meta;
264
265 SCM_VALIDATE_PROGRAM (1, program);
9a9f6487 266
53e28ed9 267 meta = scm_program_meta (program);
2fda0242 268 if (scm_is_false (meta))
9a9f6487 269 return SCM_BOOL_F;
e311f5fa
AW
270
271 return scm_car (scm_call_0 (meta));
272}
273#undef FUNC_NAME
274
275SCM_DEFINE (scm_program_sources, "program-sources", 1, 0, 0,
276 (SCM program),
277 "")
278#define FUNC_NAME s_scm_program_sources
279{
028e3d06 280 SCM meta, sources, ret, filename;
e311f5fa
AW
281
282 SCM_VALIDATE_PROGRAM (1, program);
283
284 meta = scm_program_meta (program);
9a9f6487 285 if (scm_is_false (meta))
e311f5fa
AW
286 return SCM_EOL;
287
028e3d06
AW
288 filename = SCM_BOOL_F;
289 ret = SCM_EOL;
290 for (sources = scm_cadr (scm_call_0 (meta)); !scm_is_null (sources);
291 sources = scm_cdr (sources))
292 {
293 SCM x = scm_car (sources);
294 if (scm_is_pair (x))
295 {
296 if (scm_is_number (scm_car (x)))
297 {
298 SCM addr = scm_car (x);
299 ret = scm_acons (addr, scm_cons (filename, scm_cdr (x)),
300 ret);
301 }
302 else if (scm_is_eq (scm_car (x), scm_sym_filename))
303 filename = scm_cdr (x);
304 }
305 }
306 return scm_reverse_x (ret, SCM_UNDEFINED);
e311f5fa
AW
307}
308#undef FUNC_NAME
309
6c6a4439
AW
310SCM_DEFINE (scm_program_arities, "program-arities", 1, 0, 0,
311 (SCM program),
312 "")
313#define FUNC_NAME s_scm_program_arities
314{
315 SCM meta;
316
317 SCM_VALIDATE_PROGRAM (1, program);
318
319 meta = scm_program_meta (program);
320 if (scm_is_false (meta))
321 return SCM_BOOL_F;
322
323 return scm_caddr (scm_call_0 (meta));
324}
325#undef FUNC_NAME
326
07e424b7
AW
327SCM
328scm_i_program_properties (SCM program)
329#define FUNC_NAME "%program-properties"
e311f5fa
AW
330{
331 SCM meta;
332
333 SCM_VALIDATE_PROGRAM (1, program);
334
335 meta = scm_program_meta (program);
336 if (scm_is_false (meta))
337 return SCM_EOL;
338
6c6a4439 339 return scm_cdddr (scm_call_0 (meta));
e311f5fa
AW
340}
341#undef FUNC_NAME
342
b262b74b
AW
343static SCM
344program_source (SCM program, size_t ip, SCM sources)
345{
346 SCM source = SCM_BOOL_F;
347
348 while (!scm_is_null (sources)
349 && scm_to_size_t (scm_caar (sources)) <= ip)
350 {
351 source = scm_car (sources);
352 sources = scm_cdr (sources);
353 }
354
355 return source; /* (addr . (filename . (line . column))) */
356}
357
358SCM_DEFINE (scm_program_source, "program-source", 2, 1, 0,
359 (SCM program, SCM ip, SCM sources),
028e3d06
AW
360 "")
361#define FUNC_NAME s_scm_program_source
362{
363 SCM_VALIDATE_PROGRAM (1, program);
b262b74b
AW
364 if (SCM_UNBNDP (sources))
365 sources = scm_program_sources (program);
366 return program_source (program, scm_to_size_t (ip), sources);
028e3d06
AW
367}
368#undef FUNC_NAME
369
e311f5fa
AW
370extern SCM
371scm_c_program_source (SCM program, size_t ip)
372{
b262b74b 373 return program_source (program, ip, scm_program_sources (program));
9a9f6487
AW
374}
375
6f16379e 376SCM_DEFINE (scm_program_num_free_variables, "program-num-free-variables", 1, 0, 0,
17e90c5e
KN
377 (SCM program),
378 "")
6f16379e
AW
379#define FUNC_NAME s_scm_program_num_free_variables
380{
381 SCM_VALIDATE_PROGRAM (1, program);
382 return scm_from_ulong (SCM_PROGRAM_NUM_FREE_VARIABLES (program));
383}
384#undef FUNC_NAME
385
386SCM_DEFINE (scm_program_free_variable_ref, "program-free-variable-ref", 2, 0, 0,
387 (SCM program, SCM i),
388 "")
389#define FUNC_NAME s_scm_program_free_variable_ref
390{
391 unsigned long idx;
392 SCM_VALIDATE_PROGRAM (1, program);
393 SCM_VALIDATE_ULONG_COPY (2, i, idx);
394 if (idx >= SCM_PROGRAM_NUM_FREE_VARIABLES (program))
395 SCM_OUT_OF_RANGE (2, i);
396 return SCM_PROGRAM_FREE_VARIABLE_REF (program, idx);
397}
398#undef FUNC_NAME
399
400SCM_DEFINE (scm_program_free_variable_set_x, "program-free-variable-set!", 3, 0, 0,
401 (SCM program, SCM i, SCM x),
402 "")
403#define FUNC_NAME s_scm_program_free_variable_set_x
62082959 404{
6f16379e 405 unsigned long idx;
62082959 406 SCM_VALIDATE_PROGRAM (1, program);
6f16379e
AW
407 SCM_VALIDATE_ULONG_COPY (2, i, idx);
408 if (idx >= SCM_PROGRAM_NUM_FREE_VARIABLES (program))
409 SCM_OUT_OF_RANGE (2, i);
410 SCM_PROGRAM_FREE_VARIABLE_SET (program, idx, x);
411 return SCM_UNSPECIFIED;
62082959
LC
412}
413#undef FUNC_NAME
414
53e28ed9 415SCM_DEFINE (scm_program_objcode, "program-objcode", 1, 0, 0,
17e90c5e 416 (SCM program),
53e28ed9
AW
417 "Return a @var{program}'s object code.")
418#define FUNC_NAME s_scm_program_objcode
17e90c5e
KN
419{
420 SCM_VALIDATE_PROGRAM (1, program);
fa19602c 421
53e28ed9 422 return SCM_PROGRAM_OBJCODE (program);
17e90c5e
KN
423}
424#undef FUNC_NAME
425
cb2ce548
AW
426/* procedure-minimum-arity support. */
427static void
428parse_arity (SCM arity, int *req, int *opt, int *rest)
56164a5a 429{
cb2ce548 430 SCM x = scm_cddr (arity);
56164a5a 431
56164a5a
AW
432 if (scm_is_pair (x))
433 {
434 *req = scm_to_int (scm_car (x));
435 x = scm_cdr (x);
436 if (scm_is_pair (x))
437 {
438 *opt = scm_to_int (scm_car (x));
439 x = scm_cdr (x);
440 if (scm_is_pair (x))
441 *rest = scm_is_true (scm_car (x));
442 else
443 *rest = 0;
444 }
445 else
446 *opt = *rest = 0;
447 }
448 else
449 *req = *opt = *rest = 0;
cb2ce548
AW
450}
451
eb2bc00f
AW
452static int
453scm_i_rtl_program_minimum_arity (SCM program, int *req, int *opt, int *rest)
454{
455 static SCM rtl_program_minimum_arity = SCM_BOOL_F;
456 SCM l;
457
458 if (scm_is_false (rtl_program_minimum_arity) && scm_module_system_booted_p)
459 rtl_program_minimum_arity =
460 scm_c_private_variable ("system vm debug",
461 "rtl-program-minimum-arity");
462
463 l = scm_call_1 (scm_variable_ref (rtl_program_minimum_arity), program);
464 if (scm_is_false (l))
465 return 0;
466
467 *req = scm_to_int (scm_car (l));
468 *opt = scm_to_int (scm_cadr (l));
469 *rest = scm_is_true (scm_caddr (l));
470
471 return 1;
472}
473
cb2ce548
AW
474int
475scm_i_program_arity (SCM program, int *req, int *opt, int *rest)
476{
477 SCM arities;
478
eb2bc00f
AW
479 if (SCM_RTL_PROGRAM_P (program))
480 return scm_i_rtl_program_minimum_arity (program, req, opt, rest);
481
cb2ce548
AW
482 arities = scm_program_arities (program);
483 if (!scm_is_pair (arities))
484 return 0;
485
486 parse_arity (scm_car (arities), req, opt, rest);
487 arities = scm_cdr (arities);
488
489 for (; scm_is_pair (arities); arities = scm_cdr (arities))
490 {
491 int thisreq, thisopt, thisrest;
492
493 parse_arity (scm_car (arities), &thisreq, &thisopt, &thisrest);
494
495 if (thisreq < *req
496 || (thisreq == *req
497 && ((thisrest && (!*rest || thisopt > *opt))
498 || (!thisrest && !*rest && thisopt > *opt))))
499 {
500 *req = thisreq;
501 *opt = thisopt;
502 *rest = thisrest;
503 }
504 }
505
56164a5a
AW
506 return 1;
507}
fa19602c 508
17e90c5e 509\f
56164a5a 510
17e90c5e 511void
07e56b27 512scm_bootstrap_programs (void)
17e90c5e 513{
44602b08
AW
514 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
515 "scm_init_programs",
60ae5ca2 516 (scm_t_extension_init_func)scm_init_programs, NULL);
07e56b27 517}
17e90c5e 518
07e56b27
AW
519void
520scm_init_programs (void)
521{
17e90c5e 522#ifndef SCM_MAGIC_SNARFER
aeeff258 523#include "libguile/programs.x"
17e90c5e
KN
524#endif
525}
526
527/*
528 Local Variables:
529 c-file-style: "gnu"
530 End:
531*/