1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/* Copyright (C) 2001 Free Software Foundation, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <string.h>
#include "_scm.h"
#include "vm-bootstrap.h"
#include "instructions.h"
#include "modules.h"
#include "programs.h"
#include "procprop.h" // scm_sym_name
#include "srcprop.h" // scm_sym_filename
#include "vm.h"
scm_t_bits scm_tc16_program;
static SCM write_program = SCM_BOOL_F;
SCM_DEFINE (scm_make_program, "make-program", 1, 2, 0,
(SCM objcode, SCM objtable, SCM external),
"")
#define FUNC_NAME s_scm_make_program
{
SCM_VALIDATE_OBJCODE (1, objcode);
if (SCM_UNLIKELY (SCM_UNBNDP (objtable)))
objtable = SCM_BOOL_F;
else if (scm_is_true (objtable))
SCM_VALIDATE_VECTOR (2, objtable);
if (SCM_UNLIKELY (SCM_UNBNDP (external)))
external = SCM_EOL;
else
/* FIXME: currently this test is quite expensive (can be 2-3% of total
execution time in programs that make many closures). We could remove it,
yes, but we'd get much better gains if we used some other method, like
just capturing the variables that we need instead of all heap-allocated
variables. Dunno. Keeping the check for now, as it's a user-callable
function, and inlining the op in the vm's make-closure operation. */
SCM_VALIDATE_LIST (3, external);
SCM_RETURN_NEWSMOB3 (scm_tc16_program, objcode, objtable, external);
}
#undef FUNC_NAME
static SCM
program_mark (SCM obj)
{
if (scm_is_true (SCM_PROGRAM_OBJTABLE (obj)))
scm_gc_mark (SCM_PROGRAM_OBJTABLE (obj));
if (!scm_is_null (SCM_PROGRAM_EXTERNALS (obj)))
scm_gc_mark (SCM_PROGRAM_EXTERNALS (obj));
return SCM_PROGRAM_OBJCODE (obj);
}
static SCM
program_apply (SCM program, SCM args)
{
return scm_vm_apply (scm_the_vm (), program, args);
}
static SCM
program_apply_0 (SCM program)
{
return scm_c_vm_run (scm_the_vm (), program, NULL, 0);
}
static SCM
program_apply_1 (SCM program, SCM a)
{
return scm_c_vm_run (scm_the_vm (), program, &a, 1);
}
static SCM
program_apply_2 (SCM program, SCM a, SCM b)
{
SCM args[2];
args[0] = a;
args[1] = b;
return scm_c_vm_run (scm_the_vm (), program, args, 2);
}
static int
program_print (SCM program, SCM port, scm_print_state *pstate)
{
static int print_error = 0;
if (SCM_FALSEP (write_program) && scm_module_system_booted_p)
write_program = scm_module_local_variable
(scm_c_resolve_module ("system vm program"),
scm_from_locale_symbol ("write-program"));
if (SCM_FALSEP (write_program) || print_error)
return scm_smob_print (program, port, pstate);
print_error = 1;
scm_call_2 (SCM_VARIABLE_REF (write_program), program, port);
print_error = 0;
return 1;
}
/*
* Scheme interface
*/
SCM_DEFINE (scm_program_p, "program?", 1, 0, 0,
(SCM obj),
"")
#define FUNC_NAME s_scm_program_p
{
return SCM_BOOL (SCM_PROGRAM_P (obj));
}
#undef FUNC_NAME
SCM_DEFINE (scm_program_base, "program-base", 1, 0, 0,
(SCM program),
"")
#define FUNC_NAME s_scm_program_base
{
SCM_VALIDATE_PROGRAM (1, program);
return scm_from_ulong ((unsigned long) SCM_PROGRAM_DATA (program)->base);
}
#undef FUNC_NAME
SCM_DEFINE (scm_program_arity, "program-arity", 1, 0, 0,
(SCM program),
"")
#define FUNC_NAME s_scm_program_arity
{
struct scm_objcode *p;
SCM_VALIDATE_PROGRAM (1, program);
p = SCM_PROGRAM_DATA (program);
return scm_list_4 (SCM_I_MAKINUM (p->nargs),
SCM_I_MAKINUM (p->nrest),
SCM_I_MAKINUM (p->nlocs),
SCM_I_MAKINUM (p->nexts));
}
#undef FUNC_NAME
SCM_DEFINE (scm_program_objects, "program-objects", 1, 0, 0,
(SCM program),
"")
#define FUNC_NAME s_scm_program_objects
{
SCM_VALIDATE_PROGRAM (1, program);
return SCM_PROGRAM_OBJTABLE (program);
}
#undef FUNC_NAME
SCM_DEFINE (scm_program_module, "program-module", 1, 0, 0,
(SCM program),
"")
#define FUNC_NAME s_scm_program_module
{
SCM objs;
SCM_VALIDATE_PROGRAM (1, program);
objs = SCM_PROGRAM_OBJTABLE (program);
return scm_is_true (objs) ? scm_c_vector_ref (objs, 0) : SCM_BOOL_F;
}
#undef FUNC_NAME
SCM_DEFINE (scm_program_meta, "program-meta", 1, 0, 0,
(SCM program),
"")
#define FUNC_NAME s_scm_program_meta
{
SCM metaobj;
SCM_VALIDATE_PROGRAM (1, program);
metaobj = scm_objcode_meta (SCM_PROGRAM_OBJCODE (program));
if (scm_is_true (metaobj))
return scm_make_program (metaobj, SCM_BOOL_F, SCM_EOL);
else
return SCM_BOOL_F;
}
#undef FUNC_NAME
SCM_DEFINE (scm_program_bindings, "program-bindings", 1, 0, 0,
(SCM program),
"")
#define FUNC_NAME s_scm_program_bindings
{
SCM meta;
SCM_VALIDATE_PROGRAM (1, program);
meta = scm_program_meta (program);
if (scm_is_false (meta))
return SCM_BOOL_F;
return scm_car (scm_call_0 (meta));
}
#undef FUNC_NAME
SCM_DEFINE (scm_program_sources, "program-sources", 1, 0, 0,
(SCM program),
"")
#define FUNC_NAME s_scm_program_sources
{
SCM meta, sources, ret, filename;
SCM_VALIDATE_PROGRAM (1, program);
meta = scm_program_meta (program);
if (scm_is_false (meta))
return SCM_EOL;
filename = SCM_BOOL_F;
ret = SCM_EOL;
for (sources = scm_cadr (scm_call_0 (meta)); !scm_is_null (sources);
sources = scm_cdr (sources))
{
SCM x = scm_car (sources);
if (scm_is_pair (x))
{
if (scm_is_number (scm_car (x)))
{
SCM addr = scm_car (x);
ret = scm_acons (addr, scm_cons (filename, scm_cdr (x)),
ret);
}
else if (scm_is_eq (scm_car (x), scm_sym_filename))
filename = scm_cdr (x);
}
}
return scm_reverse_x (ret, SCM_UNDEFINED);
}
#undef FUNC_NAME
SCM_DEFINE (scm_program_properties, "program-properties", 1, 0, 0,
(SCM program),
"")
#define FUNC_NAME s_scm_program_properties
{
SCM meta;
SCM_VALIDATE_PROGRAM (1, program);
meta = scm_program_meta (program);
if (scm_is_false (meta))
return SCM_EOL;
return scm_cddr (scm_call_0 (meta));
}
#undef FUNC_NAME
SCM_DEFINE (scm_program_name, "program-name", 1, 0, 0,
(SCM program),
"")
#define FUNC_NAME s_scm_program_name
{
SCM_VALIDATE_PROGRAM (1, program);
return scm_assq_ref (scm_program_properties (program), scm_sym_name);
}
#undef FUNC_NAME
SCM_DEFINE (scm_program_source, "program-source", 2, 0, 0,
(SCM program, SCM ip),
"")
#define FUNC_NAME s_scm_program_source
{
SCM_VALIDATE_PROGRAM (1, program);
return scm_c_program_source (program, scm_to_size_t (ip));
}
#undef FUNC_NAME
extern SCM
scm_c_program_source (SCM program, size_t ip)
{
SCM sources, source = SCM_BOOL_F;
for (sources = scm_program_sources (program);
!scm_is_null (sources)
&& scm_to_size_t (scm_caar (sources)) <= ip;
sources = scm_cdr (sources))
source = scm_car (sources);
return source; /* (addr . (filename . (line . column))) */
}
SCM_DEFINE (scm_program_external, "program-external", 1, 0, 0,
(SCM program),
"")
#define FUNC_NAME s_scm_program_external
{
SCM_VALIDATE_PROGRAM (1, program);
return SCM_PROGRAM_EXTERNALS (program);
}
#undef FUNC_NAME
SCM_DEFINE (scm_program_external_set_x, "program-external-set!", 2, 0, 0,
(SCM program, SCM external),
"Modify the list of closure variables of @var{program} (for "
"debugging purposes).")
#define FUNC_NAME s_scm_program_external_set_x
{
SCM_VALIDATE_PROGRAM (1, program);
SCM_VALIDATE_LIST (2, external);
SCM_PROGRAM_EXTERNALS (program) = external;
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
SCM_DEFINE (scm_program_objcode, "program-objcode", 1, 0, 0,
(SCM program),
"Return a @var{program}'s object code.")
#define FUNC_NAME s_scm_program_objcode
{
SCM_VALIDATE_PROGRAM (1, program);
return SCM_PROGRAM_OBJCODE (program);
}
#undef FUNC_NAME
void
scm_bootstrap_programs (void)
{
scm_tc16_program = scm_make_smob_type ("program", 0);
scm_set_smob_mark (scm_tc16_program, program_mark);
scm_set_smob_apply (scm_tc16_program, program_apply, 0, 0, 1);
scm_smobs[SCM_TC2SMOBNUM (scm_tc16_program)].apply_0 = program_apply_0;
scm_smobs[SCM_TC2SMOBNUM (scm_tc16_program)].apply_1 = program_apply_1;
scm_smobs[SCM_TC2SMOBNUM (scm_tc16_program)].apply_2 = program_apply_2;
scm_set_smob_print (scm_tc16_program, program_print);
scm_c_register_extension ("libguile", "scm_init_programs",
(scm_t_extension_init_func)scm_init_programs, NULL);
}
void
scm_init_programs (void)
{
scm_bootstrap_vm ();
#ifndef SCM_MAGIC_SNARFER
#include "libguile/programs.x"
#endif
}
/*
Local Variables:
c-file-style: "gnu"
End:
*/
|