Commit | Line | Data |
---|---|---|
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" |
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 | 31 | static SCM write_program = SCM_BOOL_F; |
17e90c5e | 32 | |
d1100525 | 33 | SCM_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 | 44 | SCM |
80797145 | 45 | scm_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 | 59 | SCM |
80797145 | 60 | scm_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 | 74 | SCM |
80797145 | 75 | scm_i_program_properties (SCM program) |
c4c098e3 AW |
76 | { |
77 | static SCM rtl_program_properties = SCM_BOOL_F; | |
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 | ||
c4c098e3 AW |
87 | if (scm_is_false (rtl_program_properties) && scm_module_system_booted_p) |
88 | rtl_program_properties = | |
89 | scm_c_private_variable ("system vm program", "rtl-program-properties"); | |
90 | ||
91 | return scm_call_1 (scm_variable_ref (rtl_program_properties), program); | |
92 | } | |
93 | ||
2fb924f6 AW |
94 | void |
95 | scm_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 | 138 | SCM_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 |
147 | SCM_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 | ||
156 | SCM_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 |
167 | SCM |
168 | scm_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 | ||
6f16379e | 183 | SCM_DEFINE (scm_program_num_free_variables, "program-num-free-variables", 1, 0, 0, |
17e90c5e KN |
184 | (SCM program), |
185 | "") | |
6f16379e AW |
186 | #define FUNC_NAME s_scm_program_num_free_variables |
187 | { | |
d798a895 | 188 | SCM_VALIDATE_PROGRAM (1, program); |
ee0a2b51 | 189 | |
d798a895 | 190 | return scm_from_ulong (SCM_PROGRAM_NUM_FREE_VARIABLES (program)); |
6f16379e AW |
191 | } |
192 | #undef FUNC_NAME | |
193 | ||
194 | SCM_DEFINE (scm_program_free_variable_ref, "program-free-variable-ref", 2, 0, 0, | |
195 | (SCM program, SCM i), | |
196 | "") | |
197 | #define FUNC_NAME s_scm_program_free_variable_ref | |
198 | { | |
199 | unsigned long idx; | |
ee0a2b51 | 200 | |
d798a895 | 201 | SCM_VALIDATE_PROGRAM (1, program); |
6f16379e | 202 | SCM_VALIDATE_ULONG_COPY (2, i, idx); |
d798a895 | 203 | if (idx >= SCM_PROGRAM_NUM_FREE_VARIABLES (program)) |
6f16379e | 204 | SCM_OUT_OF_RANGE (2, i); |
d798a895 | 205 | return SCM_PROGRAM_FREE_VARIABLE_REF (program, idx); |
6f16379e AW |
206 | } |
207 | #undef FUNC_NAME | |
208 | ||
209 | SCM_DEFINE (scm_program_free_variable_set_x, "program-free-variable-set!", 3, 0, 0, | |
210 | (SCM program, SCM i, SCM x), | |
211 | "") | |
212 | #define FUNC_NAME s_scm_program_free_variable_set_x | |
62082959 | 213 | { |
6f16379e | 214 | unsigned long idx; |
ee0a2b51 | 215 | |
d798a895 | 216 | SCM_VALIDATE_PROGRAM (1, program); |
6f16379e | 217 | SCM_VALIDATE_ULONG_COPY (2, i, idx); |
d798a895 | 218 | if (idx >= SCM_PROGRAM_NUM_FREE_VARIABLES (program)) |
6f16379e | 219 | SCM_OUT_OF_RANGE (2, i); |
d798a895 | 220 | SCM_PROGRAM_FREE_VARIABLE_SET (program, idx, x); |
6f16379e | 221 | return SCM_UNSPECIFIED; |
62082959 LC |
222 | } |
223 | #undef FUNC_NAME | |
224 | ||
1c33be99 AW |
225 | int |
226 | scm_i_program_arity (SCM program, int *req, int *opt, int *rest) | |
eb2bc00f | 227 | { |
34cf09cc | 228 | static SCM program_minimum_arity = SCM_BOOL_F; |
eb2bc00f AW |
229 | SCM l; |
230 | ||
27337b63 AW |
231 | if (SCM_PRIMITIVE_P (program)) |
232 | return scm_i_primitive_arity (program, req, opt, rest); | |
233 | ||
b0ca878c AW |
234 | if (SCM_PROGRAM_IS_FOREIGN (program)) |
235 | return scm_i_foreign_arity (program, req, opt, rest); | |
236 | ||
d76de871 AW |
237 | if (SCM_PROGRAM_IS_CONTINUATION (program) |
238 | || SCM_PROGRAM_IS_PARTIAL_CONTINUATION (program)) | |
d691ac20 AW |
239 | { |
240 | *req = *opt = 0; | |
241 | *rest = 1; | |
242 | return 1; | |
243 | } | |
244 | ||
34cf09cc AW |
245 | if (scm_is_false (program_minimum_arity) && scm_module_system_booted_p) |
246 | program_minimum_arity = | |
247 | scm_c_private_variable ("system vm program", "program-minimum-arity"); | |
eb2bc00f | 248 | |
34cf09cc | 249 | l = scm_call_1 (scm_variable_ref (program_minimum_arity), program); |
eb2bc00f AW |
250 | if (scm_is_false (l)) |
251 | return 0; | |
252 | ||
253 | *req = scm_to_int (scm_car (l)); | |
254 | *opt = scm_to_int (scm_cadr (l)); | |
255 | *rest = scm_is_true (scm_caddr (l)); | |
256 | ||
257 | return 1; | |
258 | } | |
259 | ||
17e90c5e | 260 | \f |
56164a5a | 261 | |
17e90c5e | 262 | void |
07e56b27 | 263 | scm_bootstrap_programs (void) |
17e90c5e | 264 | { |
44602b08 AW |
265 | scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION, |
266 | "scm_init_programs", | |
60ae5ca2 | 267 | (scm_t_extension_init_func)scm_init_programs, NULL); |
07e56b27 | 268 | } |
17e90c5e | 269 | |
07e56b27 AW |
270 | void |
271 | scm_init_programs (void) | |
272 | { | |
17e90c5e | 273 | #ifndef SCM_MAGIC_SNARFER |
aeeff258 | 274 | #include "libguile/programs.x" |
17e90c5e KN |
275 | #endif |
276 | } | |
277 | ||
278 | /* | |
279 | Local Variables: | |
280 | c-file-style: "gnu" | |
281 | End: | |
282 | */ |