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
);
46 if (SCM_UNBNDP (free_variables
) || scm_is_false (free_variables
))
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
);
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
));
73 scm_i_program_print (SCM program
, SCM port
, scm_print_state
*pstate
)
75 static int print_error
= 0;
77 if (scm_is_false (write_program
) && scm_module_system_booted_p
)
78 write_program
= scm_module_local_variable
79 (scm_c_resolve_module ("system vm program"),
80 scm_from_locale_symbol ("write-program"));
82 if (SCM_PROGRAM_IS_CONTINUATION (program
))
85 scm_puts ("#<continuation ", port
);
86 scm_uintprint (SCM_UNPACK (program
), 16, port
);
89 else if (scm_is_false (write_program
) || print_error
)
91 scm_puts ("#<program ", port
);
92 scm_uintprint (SCM_UNPACK (program
), 16, port
);
98 scm_call_2 (SCM_VARIABLE_REF (write_program
), program
, port
);
108 SCM_DEFINE (scm_program_p
, "program?", 1, 0, 0,
111 #define FUNC_NAME s_scm_program_p
113 return scm_from_bool (SCM_PROGRAM_P (obj
));
117 SCM_DEFINE (scm_program_base
, "program-base", 1, 0, 0,
120 #define FUNC_NAME s_scm_program_base
122 const struct scm_objcode
*c_objcode
;
124 SCM_VALIDATE_PROGRAM (1, program
);
126 c_objcode
= SCM_PROGRAM_DATA (program
);
127 return scm_from_ulong ((unsigned long) SCM_C_OBJCODE_BASE (c_objcode
));
131 SCM_DEFINE (scm_program_objects
, "program-objects", 1, 0, 0,
134 #define FUNC_NAME s_scm_program_objects
136 SCM_VALIDATE_PROGRAM (1, program
);
137 return SCM_PROGRAM_OBJTABLE (program
);
141 SCM_DEFINE (scm_program_module
, "program-module", 1, 0, 0,
144 #define FUNC_NAME s_scm_program_module
147 SCM_VALIDATE_PROGRAM (1, program
);
148 objs
= SCM_PROGRAM_OBJTABLE (program
);
149 return scm_is_true (objs
) ? scm_c_vector_ref (objs
, 0) : SCM_BOOL_F
;
153 SCM_DEFINE (scm_program_meta
, "program-meta", 1, 0, 0,
156 #define FUNC_NAME s_scm_program_meta
160 SCM_VALIDATE_PROGRAM (1, program
);
162 metaobj
= scm_objcode_meta (SCM_PROGRAM_OBJCODE (program
));
163 if (scm_is_true (metaobj
))
164 return scm_make_program (metaobj
, SCM_PROGRAM_OBJTABLE (program
),
171 SCM_DEFINE (scm_program_bindings
, "program-bindings", 1, 0, 0,
174 #define FUNC_NAME s_scm_program_bindings
178 SCM_VALIDATE_PROGRAM (1, program
);
180 meta
= scm_program_meta (program
);
181 if (scm_is_false (meta
))
184 return scm_car (scm_call_0 (meta
));
188 SCM_DEFINE (scm_program_sources
, "program-sources", 1, 0, 0,
191 #define FUNC_NAME s_scm_program_sources
193 SCM meta
, sources
, ret
, filename
;
195 SCM_VALIDATE_PROGRAM (1, program
);
197 meta
= scm_program_meta (program
);
198 if (scm_is_false (meta
))
201 filename
= SCM_BOOL_F
;
203 for (sources
= scm_cadr (scm_call_0 (meta
)); !scm_is_null (sources
);
204 sources
= scm_cdr (sources
))
206 SCM x
= scm_car (sources
);
209 if (scm_is_number (scm_car (x
)))
211 SCM addr
= scm_car (x
);
212 ret
= scm_acons (addr
, scm_cons (filename
, scm_cdr (x
)),
215 else if (scm_is_eq (scm_car (x
), scm_sym_filename
))
216 filename
= scm_cdr (x
);
219 return scm_reverse_x (ret
, SCM_UNDEFINED
);
223 SCM_DEFINE (scm_program_arities
, "program-arities", 1, 0, 0,
226 #define FUNC_NAME s_scm_program_arities
230 SCM_VALIDATE_PROGRAM (1, program
);
232 meta
= scm_program_meta (program
);
233 if (scm_is_false (meta
))
236 return scm_caddr (scm_call_0 (meta
));
240 SCM_DEFINE (scm_program_properties
, "program-properties", 1, 0, 0,
243 #define FUNC_NAME s_scm_program_properties
247 SCM_VALIDATE_PROGRAM (1, program
);
249 meta
= scm_program_meta (program
);
250 if (scm_is_false (meta
))
253 return scm_cdddr (scm_call_0 (meta
));
257 SCM_DEFINE (scm_program_name
, "program-name", 1, 0, 0,
260 #define FUNC_NAME s_scm_program_name
262 SCM_VALIDATE_PROGRAM (1, program
);
263 return scm_assq_ref (scm_program_properties (program
), scm_sym_name
);
267 SCM_DEFINE (scm_program_source
, "program-source", 2, 0, 0,
268 (SCM program
, SCM ip
),
270 #define FUNC_NAME s_scm_program_source
272 SCM_VALIDATE_PROGRAM (1, program
);
273 return scm_c_program_source (program
, scm_to_size_t (ip
));
278 scm_c_program_source (SCM program
, size_t ip
)
280 SCM sources
, source
= SCM_BOOL_F
;
282 for (sources
= scm_program_sources (program
);
283 !scm_is_null (sources
)
284 && scm_to_size_t (scm_caar (sources
)) <= ip
;
285 sources
= scm_cdr (sources
))
286 source
= scm_car (sources
);
288 return source
; /* (addr . (filename . (line . column))) */
291 SCM_DEFINE (scm_program_num_free_variables
, "program-num-free-variables", 1, 0, 0,
294 #define FUNC_NAME s_scm_program_num_free_variables
296 SCM_VALIDATE_PROGRAM (1, program
);
297 return scm_from_ulong (SCM_PROGRAM_NUM_FREE_VARIABLES (program
));
301 SCM_DEFINE (scm_program_free_variable_ref
, "program-free-variable-ref", 2, 0, 0,
302 (SCM program
, SCM i
),
304 #define FUNC_NAME s_scm_program_free_variable_ref
307 SCM_VALIDATE_PROGRAM (1, program
);
308 SCM_VALIDATE_ULONG_COPY (2, i
, idx
);
309 if (idx
>= SCM_PROGRAM_NUM_FREE_VARIABLES (program
))
310 SCM_OUT_OF_RANGE (2, i
);
311 return SCM_PROGRAM_FREE_VARIABLE_REF (program
, idx
);
315 SCM_DEFINE (scm_program_free_variable_set_x
, "program-free-variable-set!", 3, 0, 0,
316 (SCM program
, SCM i
, SCM x
),
318 #define FUNC_NAME s_scm_program_free_variable_set_x
321 SCM_VALIDATE_PROGRAM (1, program
);
322 SCM_VALIDATE_ULONG_COPY (2, i
, idx
);
323 if (idx
>= SCM_PROGRAM_NUM_FREE_VARIABLES (program
))
324 SCM_OUT_OF_RANGE (2, i
);
325 SCM_PROGRAM_FREE_VARIABLE_SET (program
, idx
, x
);
326 return SCM_UNSPECIFIED
;
330 SCM_DEFINE (scm_program_objcode
, "program-objcode", 1, 0, 0,
332 "Return a @var{program}'s object code.")
333 #define FUNC_NAME s_scm_program_objcode
335 SCM_VALIDATE_PROGRAM (1, program
);
337 return SCM_PROGRAM_OBJCODE (program
);
341 /* This one is a shim to pre-case-lambda internal interfaces. Avoid it if you
342 can -- use program-arguments or the like. */
343 static SCM sym_arglist
;
345 scm_i_program_arity (SCM program
, int *req
, int *opt
, int *rest
)
349 arities
= scm_program_arities (program
);
350 if (!scm_is_pair (arities
))
352 /* take the last arglist, it will be least specific */
353 while (scm_is_pair (scm_cdr (arities
)))
354 arities
= scm_cdr (arities
);
355 x
= scm_cddar (arities
);
358 *req
= scm_to_int (scm_car (x
));
362 *opt
= scm_to_int (scm_car (x
));
365 *rest
= scm_is_true (scm_car (x
));
373 *req
= *opt
= *rest
= 0;
381 scm_bootstrap_programs (void)
383 /* arglist can't be snarfed, because snarfage is only loaded when (system vm
384 program) is loaded. perhaps static-alloc will fix this. */
385 sym_arglist
= scm_from_locale_symbol ("arglist");
386 scm_c_register_extension ("libguile", "scm_init_programs",
387 (scm_t_extension_init_func
)scm_init_programs
, NULL
);
391 scm_init_programs (void)
393 #ifndef SCM_MAGIC_SNARFER
394 #include "libguile/programs.x"