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