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