1 /* Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013 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
27 #include "procprop.h" /* scm_sym_name */
31 static SCM write_program
= SCM_BOOL_F
;
33 SCM_DEFINE (scm_rtl_program_code
, "rtl-program-code", 1, 0, 0,
36 #define FUNC_NAME s_scm_rtl_program_code
38 SCM_VALIDATE_PROGRAM (1, program
);
40 return scm_from_uintptr_t ((scm_t_uintptr
) SCM_PROGRAM_CODE (program
));
45 scm_i_program_name (SCM program
)
47 static SCM rtl_program_name
= SCM_BOOL_F
;
49 if (SCM_PRIMITIVE_P (program
))
50 return SCM_SUBR_NAME (program
);
52 if (scm_is_false (rtl_program_name
) && scm_module_system_booted_p
)
54 scm_c_private_variable ("system vm program", "rtl-program-name");
56 return scm_call_1 (scm_variable_ref (rtl_program_name
), program
);
60 scm_i_program_documentation (SCM program
)
62 static SCM rtl_program_documentation
= SCM_BOOL_F
;
64 if (SCM_PRIMITIVE_P (program
))
67 if (scm_is_false (rtl_program_documentation
) && scm_module_system_booted_p
)
68 rtl_program_documentation
=
69 scm_c_private_variable ("system vm program",
70 "rtl-program-documentation");
72 return scm_call_1 (scm_variable_ref (rtl_program_documentation
), program
);
76 scm_i_program_properties (SCM program
)
78 static SCM rtl_program_properties
= SCM_BOOL_F
;
80 if (SCM_PRIMITIVE_P (program
))
82 SCM name
= scm_i_program_name (program
);
83 if (scm_is_false (name
))
85 return scm_acons (scm_sym_name
, name
, SCM_EOL
);
88 if (scm_is_false (rtl_program_properties
) && scm_module_system_booted_p
)
89 rtl_program_properties
=
90 scm_c_private_variable ("system vm program", "rtl-program-properties");
92 return scm_call_1 (scm_variable_ref (rtl_program_properties
), program
);
96 scm_i_program_print (SCM program
, SCM port
, scm_print_state
*pstate
)
98 static int print_error
= 0;
100 if (scm_is_false (write_program
) && scm_module_system_booted_p
)
101 write_program
= scm_c_private_variable ("system vm program",
104 if (SCM_PROGRAM_IS_CONTINUATION (program
))
107 scm_puts_unlocked ("#<continuation ", port
);
108 scm_uintprint (SCM_UNPACK (program
), 16, port
);
109 scm_putc_unlocked ('>', port
);
111 else if (SCM_PROGRAM_IS_PARTIAL_CONTINUATION (program
))
114 scm_puts_unlocked ("#<partial-continuation ", port
);
115 scm_uintprint (SCM_UNPACK (program
), 16, port
);
116 scm_putc_unlocked ('>', port
);
118 else if (scm_is_false (write_program
) || print_error
)
120 scm_puts_unlocked ("#<rtl-program ", port
);
121 scm_uintprint (SCM_UNPACK (program
), 16, port
);
122 scm_putc_unlocked (' ', port
);
123 scm_uintprint ((scm_t_uintptr
) SCM_PROGRAM_CODE (program
), 16, port
);
124 scm_putc_unlocked ('>', port
);
129 scm_call_2 (SCM_VARIABLE_REF (write_program
), program
, port
);
139 SCM_DEFINE (scm_program_p
, "program?", 1, 0, 0,
142 #define FUNC_NAME s_scm_program_p
144 return scm_from_bool (SCM_PROGRAM_P (obj
));
148 SCM_DEFINE (scm_primitive_p
, "primitive?", 1, 0, 0,
151 #define FUNC_NAME s_scm_primitive_p
153 return scm_from_bool (SCM_PRIMITIVE_P (obj
));
157 SCM_DEFINE (scm_primitive_call_ip
, "primitive-call-ip", 1, 0, 0,
160 #define FUNC_NAME s_scm_primitive_p
162 SCM_MAKE_VALIDATE (1, prim
, PRIMITIVE_P
);
164 return scm_from_uintptr_t (scm_i_primitive_call_ip (prim
));
169 scm_find_source_for_addr (SCM ip
)
171 static SCM source_for_addr
= SCM_BOOL_F
;
173 if (scm_is_false (source_for_addr
)) {
174 if (!scm_module_system_booted_p
)
178 scm_c_private_variable ("system vm program", "source-for-addr");
181 return scm_call_1 (scm_variable_ref (source_for_addr
), ip
);
185 scm_program_source (SCM program
, SCM ip
, SCM sources
)
187 static SCM program_source
= SCM_BOOL_F
;
189 if (scm_is_false (program_source
)) {
190 if (!scm_module_system_booted_p
)
194 scm_c_private_variable ("system vm program", "program-source");
197 if (SCM_UNBNDP (sources
))
198 return scm_call_2 (scm_variable_ref (program_source
), program
, ip
);
200 return scm_call_3 (scm_variable_ref (program_source
), program
, ip
, sources
);
203 SCM_DEFINE (scm_program_num_free_variables
, "program-num-free-variables", 1, 0, 0,
206 #define FUNC_NAME s_scm_program_num_free_variables
208 SCM_VALIDATE_PROGRAM (1, program
);
210 return scm_from_ulong (SCM_PROGRAM_NUM_FREE_VARIABLES (program
));
214 SCM_DEFINE (scm_program_free_variable_ref
, "program-free-variable-ref", 2, 0, 0,
215 (SCM program
, SCM i
),
217 #define FUNC_NAME s_scm_program_free_variable_ref
221 SCM_VALIDATE_PROGRAM (1, program
);
222 SCM_VALIDATE_ULONG_COPY (2, i
, idx
);
223 if (idx
>= SCM_PROGRAM_NUM_FREE_VARIABLES (program
))
224 SCM_OUT_OF_RANGE (2, i
);
225 return SCM_PROGRAM_FREE_VARIABLE_REF (program
, idx
);
229 SCM_DEFINE (scm_program_free_variable_set_x
, "program-free-variable-set!", 3, 0, 0,
230 (SCM program
, SCM i
, SCM x
),
232 #define FUNC_NAME s_scm_program_free_variable_set_x
236 SCM_VALIDATE_PROGRAM (1, program
);
237 SCM_VALIDATE_ULONG_COPY (2, i
, idx
);
238 if (idx
>= SCM_PROGRAM_NUM_FREE_VARIABLES (program
))
239 SCM_OUT_OF_RANGE (2, i
);
240 SCM_PROGRAM_FREE_VARIABLE_SET (program
, idx
, x
);
241 return SCM_UNSPECIFIED
;
246 scm_i_program_arity (SCM program
, int *req
, int *opt
, int *rest
)
248 static SCM rtl_program_minimum_arity
= SCM_BOOL_F
;
251 if (SCM_PRIMITIVE_P (program
))
252 return scm_i_primitive_arity (program
, req
, opt
, rest
);
254 if (SCM_PROGRAM_IS_FOREIGN (program
))
255 return scm_i_foreign_arity (program
, req
, opt
, rest
);
257 if (SCM_PROGRAM_IS_CONTINUATION (program
)
258 || SCM_PROGRAM_IS_PARTIAL_CONTINUATION (program
))
265 if (scm_is_false (rtl_program_minimum_arity
) && scm_module_system_booted_p
)
266 rtl_program_minimum_arity
=
267 scm_c_private_variable ("system vm program",
268 "rtl-program-minimum-arity");
270 l
= scm_call_1 (scm_variable_ref (rtl_program_minimum_arity
), program
);
271 if (scm_is_false (l
))
274 *req
= scm_to_int (scm_car (l
));
275 *opt
= scm_to_int (scm_cadr (l
));
276 *rest
= scm_is_true (scm_caddr (l
));
284 scm_bootstrap_programs (void)
286 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION
,
288 (scm_t_extension_init_func
)scm_init_programs
, NULL
);
292 scm_init_programs (void)
294 #ifndef SCM_MAGIC_SNARFER
295 #include "libguile/programs.x"