Remove GOOPS random state
[bpt/guile.git] / libguile / programs.c
CommitLineData
de0233af 1/* Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013, 2014 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"
8e367074 25#include "modules.h"
17e90c5e 26#include "programs.h"
6f6f0dac 27#include "procprop.h" /* scm_sym_name */
17e90c5e
KN
28#include "vm.h"
29
30\f
e6fea618 31static SCM write_program = SCM_BOOL_F;
17e90c5e 32
d1100525 33SCM_DEFINE (scm_program_code, "program-code", 1, 0, 0,
510ca126
AW
34 (SCM program),
35 "")
d1100525 36#define FUNC_NAME s_scm_program_code
510ca126 37{
d798a895 38 SCM_VALIDATE_PROGRAM (1, program);
510ca126 39
d798a895 40 return scm_from_uintptr_t ((scm_t_uintptr) SCM_PROGRAM_CODE (program));
510ca126
AW
41}
42#undef FUNC_NAME
43
e65f80af 44SCM
80797145 45scm_i_program_name (SCM program)
510ca126 46{
2e12c1a2 47 static SCM program_name = SCM_BOOL_F;
e65f80af 48
27337b63
AW
49 if (SCM_PRIMITIVE_P (program))
50 return SCM_SUBR_NAME (program);
51
2e12c1a2
AW
52 if (scm_is_false (program_name) && scm_module_system_booted_p)
53 program_name =
54 scm_c_private_variable ("system vm program", "program-name");
e65f80af 55
2e12c1a2 56 return scm_call_1 (scm_variable_ref (program_name), program);
510ca126
AW
57}
58
bf8328ec 59SCM
80797145 60scm_i_program_documentation (SCM program)
bf8328ec 61{
2e12c1a2 62 static SCM program_documentation = SCM_BOOL_F;
bf8328ec 63
27337b63
AW
64 if (SCM_PRIMITIVE_P (program))
65 return SCM_BOOL_F;
66
2e12c1a2
AW
67 if (scm_is_false (program_documentation) && scm_module_system_booted_p)
68 program_documentation =
69 scm_c_private_variable ("system vm program", "program-documentation");
bf8328ec 70
2e12c1a2 71 return scm_call_1 (scm_variable_ref (program_documentation), program);
bf8328ec
AW
72}
73
c4c098e3 74SCM
80797145 75scm_i_program_properties (SCM program)
c4c098e3 76{
6b9470bf 77 static SCM program_properties = SCM_BOOL_F;
c4c098e3 78
27337b63
AW
79 if (SCM_PRIMITIVE_P (program))
80 {
80797145 81 SCM name = scm_i_program_name (program);
27337b63
AW
82 if (scm_is_false (name))
83 return SCM_EOL;
84 return scm_acons (scm_sym_name, name, SCM_EOL);
85 }
86
6b9470bf
AW
87 if (scm_is_false (program_properties) && scm_module_system_booted_p)
88 program_properties =
89 scm_c_private_variable ("system vm program", "program-properties");
c4c098e3 90
6b9470bf 91 return scm_call_1 (scm_variable_ref (program_properties), program);
c4c098e3
AW
92}
93
2fb924f6
AW
94void
95scm_i_program_print (SCM program, SCM port, scm_print_state *pstate)
e6fea618 96{
0ba8bb71
AW
97 static int print_error = 0;
98
5c8cefe5 99 if (scm_is_false (write_program) && scm_module_system_booted_p)
eb2bc00f
AW
100 write_program = scm_c_private_variable ("system vm program",
101 "write-program");
e6fea618 102
1d1cae0e
AW
103 if (SCM_PROGRAM_IS_CONTINUATION (program))
104 {
105 /* twingliness */
0607ebbf 106 scm_puts_unlocked ("#<continuation ", port);
76e38162 107 scm_uintprint (SCM_UNPACK (program), 16, port);
0607ebbf 108 scm_putc_unlocked ('>', port);
1d1cae0e 109 }
5c606217 110 else if (SCM_PROGRAM_IS_PARTIAL_CONTINUATION (program))
2150e9a8
AW
111 {
112 /* twingliness */
0607ebbf 113 scm_puts_unlocked ("#<partial-continuation ", port);
2150e9a8 114 scm_uintprint (SCM_UNPACK (program), 16, port);
0607ebbf 115 scm_putc_unlocked ('>', port);
2150e9a8 116 }
1d1cae0e 117 else if (scm_is_false (write_program) || print_error)
2fb924f6 118 {
2b4d21d5 119 scm_puts_unlocked ("#<program ", port);
1c33be99
AW
120 scm_uintprint (SCM_UNPACK (program), 16, port);
121 scm_putc_unlocked (' ', port);
d798a895 122 scm_uintprint ((scm_t_uintptr) SCM_PROGRAM_CODE (program), 16, port);
1c33be99 123 scm_putc_unlocked ('>', port);
2fb924f6
AW
124 }
125 else
126 {
127 print_error = 1;
128 scm_call_2 (SCM_VARIABLE_REF (write_program), program, port);
129 print_error = 0;
130 }
e6fea618
AW
131}
132
17e90c5e
KN
133\f
134/*
135 * Scheme interface
136 */
137
0bd1e9c6 138SCM_DEFINE (scm_program_p, "program?", 1, 0, 0,
510ca126
AW
139 (SCM obj),
140 "")
0bd1e9c6 141#define FUNC_NAME s_scm_program_p
510ca126 142{
d798a895 143 return scm_from_bool (SCM_PROGRAM_P (obj));
510ca126
AW
144}
145#undef FUNC_NAME
146
27337b63
AW
147SCM_DEFINE (scm_primitive_p, "primitive?", 1, 0, 0,
148 (SCM obj),
149 "")
150#define FUNC_NAME s_scm_primitive_p
151{
152 return scm_from_bool (SCM_PRIMITIVE_P (obj));
153}
154#undef FUNC_NAME
155
156SCM_DEFINE (scm_primitive_call_ip, "primitive-call-ip", 1, 0, 0,
157 (SCM prim),
158 "")
159#define FUNC_NAME s_scm_primitive_p
160{
161 SCM_MAKE_VALIDATE (1, prim, PRIMITIVE_P);
162
0e3a59f7 163 return scm_from_uintptr_t (scm_i_primitive_call_ip (prim));
27337b63
AW
164}
165#undef FUNC_NAME
166
581a4eb8
AW
167SCM
168scm_find_source_for_addr (SCM ip)
169{
170 static SCM source_for_addr = SCM_BOOL_F;
171
172 if (scm_is_false (source_for_addr)) {
173 if (!scm_module_system_booted_p)
174 return SCM_BOOL_F;
175
176 source_for_addr =
177 scm_c_private_variable ("system vm program", "source-for-addr");
178 }
179
180 return scm_call_1 (scm_variable_ref (source_for_addr), ip);
181}
182
de0233af
AW
183SCM
184scm_program_address_range (SCM program)
185{
186 static SCM program_address_range = SCM_BOOL_F;
187
188 if (scm_is_false (program_address_range) && scm_module_system_booted_p)
189 program_address_range =
190 scm_c_private_variable ("system vm program", "program-address-range");
191
192 return scm_call_1 (scm_variable_ref (program_address_range), program);
193}
194
6f16379e 195SCM_DEFINE (scm_program_num_free_variables, "program-num-free-variables", 1, 0, 0,
17e90c5e
KN
196 (SCM program),
197 "")
6f16379e
AW
198#define FUNC_NAME s_scm_program_num_free_variables
199{
d798a895 200 SCM_VALIDATE_PROGRAM (1, program);
ee0a2b51 201
d798a895 202 return scm_from_ulong (SCM_PROGRAM_NUM_FREE_VARIABLES (program));
6f16379e
AW
203}
204#undef FUNC_NAME
205
206SCM_DEFINE (scm_program_free_variable_ref, "program-free-variable-ref", 2, 0, 0,
207 (SCM program, SCM i),
208 "")
209#define FUNC_NAME s_scm_program_free_variable_ref
210{
211 unsigned long idx;
ee0a2b51 212
d798a895 213 SCM_VALIDATE_PROGRAM (1, program);
6f16379e 214 SCM_VALIDATE_ULONG_COPY (2, i, idx);
d798a895 215 if (idx >= SCM_PROGRAM_NUM_FREE_VARIABLES (program))
6f16379e 216 SCM_OUT_OF_RANGE (2, i);
d798a895 217 return SCM_PROGRAM_FREE_VARIABLE_REF (program, idx);
6f16379e
AW
218}
219#undef FUNC_NAME
220
221SCM_DEFINE (scm_program_free_variable_set_x, "program-free-variable-set!", 3, 0, 0,
222 (SCM program, SCM i, SCM x),
223 "")
224#define FUNC_NAME s_scm_program_free_variable_set_x
62082959 225{
6f16379e 226 unsigned long idx;
ee0a2b51 227
d798a895 228 SCM_VALIDATE_PROGRAM (1, program);
6f16379e 229 SCM_VALIDATE_ULONG_COPY (2, i, idx);
d798a895 230 if (idx >= SCM_PROGRAM_NUM_FREE_VARIABLES (program))
6f16379e 231 SCM_OUT_OF_RANGE (2, i);
d798a895 232 SCM_PROGRAM_FREE_VARIABLE_SET (program, idx, x);
6f16379e 233 return SCM_UNSPECIFIED;
62082959
LC
234}
235#undef FUNC_NAME
236
1c33be99
AW
237int
238scm_i_program_arity (SCM program, int *req, int *opt, int *rest)
eb2bc00f 239{
34cf09cc 240 static SCM program_minimum_arity = SCM_BOOL_F;
eb2bc00f
AW
241 SCM l;
242
27337b63
AW
243 if (SCM_PRIMITIVE_P (program))
244 return scm_i_primitive_arity (program, req, opt, rest);
245
b0ca878c
AW
246 if (SCM_PROGRAM_IS_FOREIGN (program))
247 return scm_i_foreign_arity (program, req, opt, rest);
248
d76de871
AW
249 if (SCM_PROGRAM_IS_CONTINUATION (program)
250 || SCM_PROGRAM_IS_PARTIAL_CONTINUATION (program))
d691ac20
AW
251 {
252 *req = *opt = 0;
253 *rest = 1;
254 return 1;
255 }
256
34cf09cc
AW
257 if (scm_is_false (program_minimum_arity) && scm_module_system_booted_p)
258 program_minimum_arity =
259 scm_c_private_variable ("system vm program", "program-minimum-arity");
eb2bc00f 260
34cf09cc 261 l = scm_call_1 (scm_variable_ref (program_minimum_arity), program);
eb2bc00f
AW
262 if (scm_is_false (l))
263 return 0;
264
265 *req = scm_to_int (scm_car (l));
266 *opt = scm_to_int (scm_cadr (l));
267 *rest = scm_is_true (scm_caddr (l));
268
269 return 1;
270}
271
17e90c5e 272\f
56164a5a 273
17e90c5e 274void
07e56b27 275scm_bootstrap_programs (void)
17e90c5e 276{
44602b08
AW
277 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
278 "scm_init_programs",
60ae5ca2 279 (scm_t_extension_init_func)scm_init_programs, NULL);
07e56b27 280}
17e90c5e 281
07e56b27
AW
282void
283scm_init_programs (void)
284{
17e90c5e 285#ifndef SCM_MAGIC_SNARFER
aeeff258 286#include "libguile/programs.x"
17e90c5e
KN
287#endif
288}
289
290/*
291 Local Variables:
292 c-file-style: "gnu"
293 End:
294*/