1 /* Copyright (C) 2001, 2009, 2010 Free Software Foundation, Inc.
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.
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.
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
25 #include "instructions.h"
28 #include "procprop.h" /* scm_sym_name */
29 #include "srcprop.h" /* scm_sym_filename */
33 static SCM write_program
= SCM_BOOL_F
;
35 SCM_DEFINE (scm_make_program
, "make-program", 1, 2, 0,
36 (SCM objcode
, SCM objtable
, SCM free_variables
),
38 #define FUNC_NAME s_scm_make_program
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 if (SCM_UNLIKELY (SCM_UNBNDP (free_variables
)))
46 free_variables
= SCM_BOOL_F
;
47 else if (free_variables
!= SCM_BOOL_F
)
48 SCM_VALIDATE_VECTOR (3, free_variables
);
50 return scm_double_cell (scm_tc7_program
, (scm_t_bits
)objcode
,
51 (scm_t_bits
)objtable
, (scm_t_bits
)free_variables
);
56 scm_i_program_print (SCM program
, SCM port
, scm_print_state
*pstate
)
58 static int print_error
= 0;
60 if (scm_is_false (write_program
) && scm_module_system_booted_p
)
61 write_program
= scm_module_local_variable
62 (scm_c_resolve_module ("system vm program"),
63 scm_from_locale_symbol ("write-program"));
65 if (scm_is_false (write_program
) || print_error
)
67 scm_puts ("#<program ", port
);
68 scm_uintprint (SCM_CELL_WORD_1 (program
), 16, port
);
74 scm_call_2 (SCM_VARIABLE_REF (write_program
), program
, port
);
84 SCM_DEFINE (scm_program_p
, "program?", 1, 0, 0,
87 #define FUNC_NAME s_scm_program_p
89 return scm_from_bool (SCM_PROGRAM_P (obj
));
93 SCM_DEFINE (scm_program_base
, "program-base", 1, 0, 0,
96 #define FUNC_NAME s_scm_program_base
98 const struct scm_objcode
*c_objcode
;
100 SCM_VALIDATE_PROGRAM (1, program
);
102 c_objcode
= SCM_PROGRAM_DATA (program
);
103 return scm_from_ulong ((unsigned long) SCM_C_OBJCODE_BASE (c_objcode
));
107 SCM_DEFINE (scm_program_objects
, "program-objects", 1, 0, 0,
110 #define FUNC_NAME s_scm_program_objects
112 SCM_VALIDATE_PROGRAM (1, program
);
113 return SCM_PROGRAM_OBJTABLE (program
);
117 SCM_DEFINE (scm_program_module
, "program-module", 1, 0, 0,
120 #define FUNC_NAME s_scm_program_module
123 SCM_VALIDATE_PROGRAM (1, program
);
124 objs
= SCM_PROGRAM_OBJTABLE (program
);
125 return scm_is_true (objs
) ? scm_c_vector_ref (objs
, 0) : SCM_BOOL_F
;
129 SCM_DEFINE (scm_program_meta
, "program-meta", 1, 0, 0,
132 #define FUNC_NAME s_scm_program_meta
136 SCM_VALIDATE_PROGRAM (1, program
);
138 metaobj
= scm_objcode_meta (SCM_PROGRAM_OBJCODE (program
));
139 if (scm_is_true (metaobj
))
140 return scm_make_program (metaobj
, SCM_PROGRAM_OBJTABLE (program
),
147 SCM_DEFINE (scm_program_bindings
, "program-bindings", 1, 0, 0,
150 #define FUNC_NAME s_scm_program_bindings
154 SCM_VALIDATE_PROGRAM (1, program
);
156 meta
= scm_program_meta (program
);
157 if (scm_is_false (meta
))
160 return scm_car (scm_call_0 (meta
));
164 SCM_DEFINE (scm_program_sources
, "program-sources", 1, 0, 0,
167 #define FUNC_NAME s_scm_program_sources
169 SCM meta
, sources
, ret
, filename
;
171 SCM_VALIDATE_PROGRAM (1, program
);
173 meta
= scm_program_meta (program
);
174 if (scm_is_false (meta
))
177 filename
= SCM_BOOL_F
;
179 for (sources
= scm_cadr (scm_call_0 (meta
)); !scm_is_null (sources
);
180 sources
= scm_cdr (sources
))
182 SCM x
= scm_car (sources
);
185 if (scm_is_number (scm_car (x
)))
187 SCM addr
= scm_car (x
);
188 ret
= scm_acons (addr
, scm_cons (filename
, scm_cdr (x
)),
191 else if (scm_is_eq (scm_car (x
), scm_sym_filename
))
192 filename
= scm_cdr (x
);
195 return scm_reverse_x (ret
, SCM_UNDEFINED
);
199 SCM_DEFINE (scm_program_arities
, "program-arities", 1, 0, 0,
202 #define FUNC_NAME s_scm_program_arities
206 SCM_VALIDATE_PROGRAM (1, program
);
208 meta
= scm_program_meta (program
);
209 if (scm_is_false (meta
))
212 return scm_caddr (scm_call_0 (meta
));
216 SCM_DEFINE (scm_program_properties
, "program-properties", 1, 0, 0,
219 #define FUNC_NAME s_scm_program_properties
223 SCM_VALIDATE_PROGRAM (1, program
);
225 meta
= scm_program_meta (program
);
226 if (scm_is_false (meta
))
229 return scm_cdddr (scm_call_0 (meta
));
233 SCM_DEFINE (scm_program_name
, "program-name", 1, 0, 0,
236 #define FUNC_NAME s_scm_program_name
238 SCM_VALIDATE_PROGRAM (1, program
);
239 return scm_assq_ref (scm_program_properties (program
), scm_sym_name
);
243 SCM_DEFINE (scm_program_source
, "program-source", 2, 0, 0,
244 (SCM program
, SCM ip
),
246 #define FUNC_NAME s_scm_program_source
248 SCM_VALIDATE_PROGRAM (1, program
);
249 return scm_c_program_source (program
, scm_to_size_t (ip
));
254 scm_c_program_source (SCM program
, size_t ip
)
256 SCM sources
, source
= SCM_BOOL_F
;
258 for (sources
= scm_program_sources (program
);
259 !scm_is_null (sources
)
260 && scm_to_size_t (scm_caar (sources
)) <= ip
;
261 sources
= scm_cdr (sources
))
262 source
= scm_car (sources
);
264 return source
; /* (addr . (filename . (line . column))) */
267 SCM_DEFINE (scm_program_free_variables
, "program-free-variables", 1, 0, 0,
270 #define FUNC_NAME s_scm_program_free_variables
272 SCM_VALIDATE_PROGRAM (1, program
);
273 return SCM_PROGRAM_FREE_VARIABLES (program
);
277 SCM_DEFINE (scm_program_objcode
, "program-objcode", 1, 0, 0,
279 "Return a @var{program}'s object code.")
280 #define FUNC_NAME s_scm_program_objcode
282 SCM_VALIDATE_PROGRAM (1, program
);
284 return SCM_PROGRAM_OBJCODE (program
);
288 /* This one is a shim to pre-case-lambda internal interfaces. Avoid it if you
289 can -- use program-arguments or the like. */
290 static SCM sym_arglist
;
292 scm_i_program_arity (SCM program
, int *req
, int *opt
, int *rest
)
296 arities
= scm_program_arities (program
);
297 if (!scm_is_pair (arities
))
299 /* take the last arglist, it will be least specific */
300 while (scm_is_pair (scm_cdr (arities
)))
301 arities
= scm_cdr (arities
);
302 x
= scm_cddar (arities
);
305 *req
= scm_to_int (scm_car (x
));
309 *opt
= scm_to_int (scm_car (x
));
312 *rest
= scm_is_true (scm_car (x
));
320 *req
= *opt
= *rest
= 0;
328 scm_bootstrap_programs (void)
330 /* arglist can't be snarfed, because snarfage is only loaded when (system vm
331 program) is loaded. perhaps static-alloc will fix this. */
332 sym_arglist
= scm_from_locale_symbol ("arglist");
333 scm_c_register_extension ("libguile", "scm_init_programs",
334 (scm_t_extension_init_func
)scm_init_programs
, NULL
);
338 scm_init_programs (void)
340 #ifndef SCM_MAGIC_SNARFER
341 #include "libguile/programs.x"