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