Removed a few more deprecated function calls; documented closures.
[bpt/guile.git] / src / vm_system.c
CommitLineData
8f5cfc81 1/* Copyright (C) 2001 Free Software Foundation, Inc.
a98cef7e
KN
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41
42/* This file is included in vm_engine.c */
43
a98cef7e
KN
44\f
45/*
46 * Basic operations
47 */
48
17e90c5e 49/* This must be the first instruction! */
46cd9a34 50VM_DEFINE_INSTRUCTION (nop, "nop", 0, 0, 0)
a98cef7e
KN
51{
52 NEXT;
53}
54
46cd9a34 55VM_DEFINE_INSTRUCTION (halt, "halt", 0, 0, 0)
a98cef7e 56{
a6df585a 57 SCM ret;
3d5ee0cd 58 vp->time += scm_c_get_internal_run_time () - start_time;
17e90c5e 59 HALT_HOOK ();
a6df585a 60 POP (ret);
17e90c5e
KN
61 FREE_FRAME ();
62 SYNC_ALL ();
63 return ret;
a98cef7e
KN
64}
65
7a0d0cee
KN
66VM_DEFINE_INSTRUCTION (break, "break", 0, 0, 0)
67{
68 BREAK_HOOK ();
69 NEXT;
70}
71
499a4c07 72VM_DEFINE_INSTRUCTION (drop, "drop", 0, 0, 0)
a98cef7e 73{
17e90c5e 74 DROP ();
a98cef7e
KN
75 NEXT;
76}
77
cb4cca12
KN
78VM_DEFINE_INSTRUCTION (mark, "mark", 0, 0, 1)
79{
80 PUSH (SCM_UNDEFINED);
81 NEXT;
82}
83
46cd9a34 84VM_DEFINE_INSTRUCTION (dup, "dup", 0, 0, 1)
26403690 85{
f349065e
KN
86 SCM x = *sp;
87 PUSH (x);
26403690
KN
88 NEXT;
89}
90
17e90c5e
KN
91\f
92/*
93 * Object creation
94 */
a98cef7e 95
46cd9a34 96VM_DEFINE_INSTRUCTION (void, "void", 0, 0, 1)
a98cef7e 97{
17e90c5e 98 PUSH (SCM_UNSPECIFIED);
a98cef7e
KN
99 NEXT;
100}
101
46cd9a34 102VM_DEFINE_INSTRUCTION (make_true, "make-true", 0, 0, 1)
a98cef7e 103{
17e90c5e 104 PUSH (SCM_BOOL_T);
a98cef7e
KN
105 NEXT;
106}
107
46cd9a34 108VM_DEFINE_INSTRUCTION (make_false, "make-false", 0, 0, 1)
a98cef7e 109{
17e90c5e 110 PUSH (SCM_BOOL_F);
a98cef7e
KN
111 NEXT;
112}
113
46cd9a34 114VM_DEFINE_INSTRUCTION (make_eol, "make-eol", 0, 0, 1)
a98cef7e 115{
17e90c5e 116 PUSH (SCM_EOL);
a98cef7e
KN
117 NEXT;
118}
119
46cd9a34 120VM_DEFINE_INSTRUCTION (make_int8, "make-int8", 1, 0, 1)
a98cef7e 121{
2d80426a 122 PUSH (SCM_I_MAKINUM ((signed char) FETCH ()));
a98cef7e
KN
123 NEXT;
124}
125
46cd9a34 126VM_DEFINE_INSTRUCTION (make_int8_0, "make-int8:0", 0, 0, 1)
a98cef7e 127{
238e7a11 128 PUSH (SCM_INUM0);
a98cef7e
KN
129 NEXT;
130}
131
46cd9a34 132VM_DEFINE_INSTRUCTION (make_int8_1, "make-int8:1", 0, 0, 1)
a98cef7e 133{
238e7a11 134 PUSH (SCM_I_MAKINUM (1));
a98cef7e
KN
135 NEXT;
136}
137
46cd9a34 138VM_DEFINE_INSTRUCTION (make_int16, "make-int16", 2, 0, 1)
a98cef7e 139{
ea9b4b29
KN
140 int h = FETCH ();
141 int l = FETCH ();
2d80426a 142 PUSH (SCM_I_MAKINUM ((signed short) (h << 8) + l));
a98cef7e
KN
143 NEXT;
144}
145
46cd9a34 146VM_DEFINE_INSTRUCTION (make_char8, "make-char8", 1, 0, 1)
a98cef7e 147{
17e90c5e 148 PUSH (SCM_MAKE_CHAR (FETCH ()));
a98cef7e
KN
149 NEXT;
150}
151
cb4cca12
KN
152VM_DEFINE_INSTRUCTION (list, "list", 1, -1, 1)
153{
154 int n = FETCH ();
155 POP_LIST (n);
156 NEXT;
157}
158
159VM_DEFINE_INSTRUCTION (vector, "vector", 1, -1, 1)
160{
161 int n = FETCH ();
162 POP_LIST (n);
163 *sp = scm_vector (*sp);
164 NEXT;
165}
166
167VM_DEFINE_INSTRUCTION (list_mark, "list-mark", 0, 0, 0)
168{
169 POP_LIST_MARK ();
170 NEXT;
171}
172
173VM_DEFINE_INSTRUCTION (vector_mark, "vector-mark", 0, 0, 0)
174{
175 POP_LIST_MARK ();
176 *sp = scm_vector (*sp);
177 NEXT;
178}
179
180VM_DEFINE_INSTRUCTION (list_break, "list-break", 0, 0, 0)
181{
182 SCM l;
183 POP (l);
184 for (; !SCM_NULLP (l); l = SCM_CDR (l))
185 PUSH (SCM_CAR (l));
186 NEXT;
187}
188
a98cef7e
KN
189\f
190/*
17e90c5e 191 * Variable access
a98cef7e
KN
192 */
193
17e90c5e
KN
194#define OBJECT_REF(i) objects[i]
195#define OBJECT_SET(i,o) objects[i] = o
a98cef7e 196
af988bbf
KN
197#define LOCAL_REF(i) SCM_FRAME_VARIABLE (fp, i)
198#define LOCAL_SET(i,o) SCM_FRAME_VARIABLE (fp, i) = o
a98cef7e 199
2d80426a
LC
200/* For the variable operations, we _must_ obviously avoid function calls to
201 `scm_variable_ref ()', `scm_variable_bound_p ()' and friends which do
202 nothing more than the corresponding macros. */
203#define VARIABLE_REF(v) SCM_VARIABLE_REF (v)
204#define VARIABLE_SET(v,o) SCM_VARIABLE_SET (v, o)
205#define VARIABLE_BOUNDP(v) (VARIABLE_REF (v) != SCM_UNDEFINED)
a98cef7e 206
17e90c5e 207/* ref */
a98cef7e 208
46cd9a34 209VM_DEFINE_INSTRUCTION (object_ref, "object-ref", 1, 0, 1)
a98cef7e 210{
17e90c5e
KN
211 PUSH (OBJECT_REF (FETCH ()));
212 NEXT;
a98cef7e
KN
213}
214
46cd9a34 215VM_DEFINE_INSTRUCTION (local_ref, "local-ref", 1, 0, 1)
a98cef7e 216{
17e90c5e
KN
217 PUSH (LOCAL_REF (FETCH ()));
218 NEXT;
a98cef7e
KN
219}
220
46cd9a34 221VM_DEFINE_INSTRUCTION (external_ref, "external-ref", 1, 0, 1)
a98cef7e 222{
17e90c5e
KN
223 unsigned int i;
224 SCM e = external;
225 for (i = FETCH (); i; i--)
2a63758b
KN
226 {
227 CHECK_EXTERNAL(e);
228 e = SCM_CDR (e);
229 }
230 CHECK_EXTERNAL(e);
17e90c5e 231 PUSH (SCM_CAR (e));
a98cef7e
KN
232 NEXT;
233}
234
46cd9a34 235VM_DEFINE_INSTRUCTION (variable_ref, "variable-ref", 0, 0, 1)
a98cef7e 236{
17e90c5e 237 SCM x = *sp;
238e7a11 238
2d80426a 239 if (!VARIABLE_BOUNDP (x))
17e90c5e 240 {
238e7a11
LC
241 err_args = SCM_LIST1 (x);
242 /* Was: err_args = SCM_LIST1 (SCM_CAR (x)); */
17e90c5e
KN
243 goto vm_error_unbound;
244 }
238e7a11
LC
245 else
246 {
2d80426a 247 SCM o = VARIABLE_REF (x);
238e7a11
LC
248 *sp = o;
249 }
250
a98cef7e
KN
251 NEXT;
252}
253
17e90c5e
KN
254/* set */
255
46cd9a34 256VM_DEFINE_INSTRUCTION (local_set, "local-set", 1, 1, 0)
a98cef7e 257{
17e90c5e
KN
258 LOCAL_SET (FETCH (), *sp);
259 DROP ();
a98cef7e
KN
260 NEXT;
261}
262
46cd9a34 263VM_DEFINE_INSTRUCTION (external_set, "external-set", 1, 1, 0)
a98cef7e 264{
17e90c5e
KN
265 unsigned int i;
266 SCM e = external;
267 for (i = FETCH (); i; i--)
ac02b386
KN
268 {
269 CHECK_EXTERNAL(e);
270 e = SCM_CDR (e);
271 }
272 CHECK_EXTERNAL(e);
17e90c5e
KN
273 SCM_SETCAR (e, *sp);
274 DROP ();
a98cef7e
KN
275 NEXT;
276}
277
46cd9a34 278VM_DEFINE_INSTRUCTION (variable_set, "variable-set", 0, 1, 0)
a98cef7e 279{
2d80426a 280 VARIABLE_SET (sp[0], sp[-1]);
3616e9e9
KN
281 scm_set_object_property_x (sp[-1], scm_sym_name, SCM_CAR (sp[0]));
282 sp -= 2;
a98cef7e
KN
283 NEXT;
284}
285
286\f
287/*
288 * branch and jump
289 */
290
17e90c5e
KN
291#define BR(p) \
292{ \
41f248a8
KN
293 int h = FETCH (); \
294 int l = FETCH (); \
295 signed short offset = (h << 8) + l; \
17e90c5e
KN
296 if (p) \
297 ip += offset; \
298 DROP (); \
299 NEXT; \
300}
301
41f248a8
KN
302VM_DEFINE_INSTRUCTION (br, "br", 2, 0, 0)
303{
304 int h = FETCH ();
305 int l = FETCH ();
306 ip += (signed short) (h << 8) + l;
307 NEXT;
308}
309
310VM_DEFINE_INSTRUCTION (br_if, "br-if", 2, 0, 0)
a98cef7e 311{
17e90c5e 312 BR (!SCM_FALSEP (*sp));
a98cef7e
KN
313}
314
41f248a8 315VM_DEFINE_INSTRUCTION (br_if_not, "br-if-not", 2, 0, 0)
a98cef7e 316{
17e90c5e 317 BR (SCM_FALSEP (*sp));
a98cef7e
KN
318}
319
41f248a8 320VM_DEFINE_INSTRUCTION (br_if_eq, "br-if-eq", 2, 0, 0)
a98cef7e 321{
17e90c5e 322 BR (SCM_EQ_P (sp[0], sp--[1]));
a98cef7e
KN
323}
324
41f248a8 325VM_DEFINE_INSTRUCTION (br_if_not_eq, "br-if-not-eq", 2, 0, 0)
a98cef7e 326{
17e90c5e
KN
327 BR (!SCM_EQ_P (sp[0], sp--[1]));
328}
329
41f248a8 330VM_DEFINE_INSTRUCTION (br_if_null, "br-if-null", 2, 0, 0)
17e90c5e
KN
331{
332 BR (SCM_NULLP (*sp));
333}
334
41f248a8 335VM_DEFINE_INSTRUCTION (br_if_not_null, "br-if-not-null", 2, 0, 0)
17e90c5e
KN
336{
337 BR (!SCM_NULLP (*sp));
a98cef7e
KN
338}
339
a98cef7e
KN
340\f
341/*
342 * Subprogram call
343 */
344
46cd9a34 345VM_DEFINE_INSTRUCTION (make_closure, "make-closure", 0, 1, 1)
a98cef7e 346{
3d5ee0cd
KN
347 SYNC_BEFORE_GC ();
348 *sp = scm_c_make_closure (*sp, external);
17e90c5e 349 NEXT;
a98cef7e
KN
350}
351
46cd9a34 352VM_DEFINE_INSTRUCTION (call, "call", 1, -1, 1)
a98cef7e 353{
3616e9e9 354 SCM x;
17e90c5e 355 nargs = FETCH ();
a98cef7e
KN
356
357 vm_call:
c8b9df71
KN
358 x = sp[-nargs];
359
a98cef7e
KN
360 /*
361 * Subprogram call
362 */
3616e9e9 363 if (SCM_PROGRAM_P (x))
a98cef7e 364 {
3616e9e9 365 program = x;
3d5ee0cd 366 vm_call_program:
499a4c07 367 CACHE_PROGRAM ();
17e90c5e
KN
368 INIT_ARGS ();
369 NEW_FRAME ();
17e90c5e
KN
370 ENTER_HOOK ();
371 APPLY_HOOK ();
a98cef7e
KN
372 NEXT;
373 }
374 /*
375 * Function call
376 */
3616e9e9 377 if (!SCM_FALSEP (scm_procedure_p (x)))
a98cef7e 378 {
f41cb00c
LC
379 /* At this point, the stack contains the procedure and each one of its
380 arguments. */
f21dfea6 381 SCM args;
f41cb00c 382
135b32ee 383#if 1
17e90c5e 384 POP_LIST (nargs);
135b32ee
LC
385#else
386 /* Experimental: Build the arglist on the VM stack. XXX */
387 POP_LIST_ON_STACK (nargs);
388#endif
f21dfea6
KN
389 POP (args);
390 *sp = scm_apply (x, args, SCM_EOL);
17e90c5e 391 NEXT;
a98cef7e
KN
392 }
393 /*
394 * Continuation call
395 */
3616e9e9 396 if (SCM_VM_CONT_P (x))
a98cef7e
KN
397 {
398 vm_call_cc:
399 /* Check the number of arguments */
382693fe 400 if (nargs != 1)
3616e9e9 401 scm_wrong_num_args (x);
a98cef7e
KN
402
403 /* Reinstate the continuation */
17e90c5e 404 EXIT_HOOK ();
3616e9e9 405 reinstate_vm_cont (vp, x);
3d5ee0cd 406 CACHE_REGISTER ();
af988bbf 407 program = SCM_FRAME_PROGRAM (fp);
3616e9e9 408 CACHE_PROGRAM ();
a98cef7e
KN
409 NEXT;
410 }
411
66292535 412 program = x;
17e90c5e 413 goto vm_error_wrong_type_apply;
a98cef7e
KN
414}
415
46cd9a34 416VM_DEFINE_INSTRUCTION (tail_call, "tail-call", 1, -1, 1)
a98cef7e 417{
f41cb00c 418 register SCM x;
17e90c5e 419 nargs = FETCH ();
3616e9e9 420 x = sp[-nargs];
17e90c5e
KN
421
422 SCM_TICK; /* allow interrupt here */
a98cef7e
KN
423
424 /*
17e90c5e 425 * Tail recursive call
a98cef7e 426 */
17e90c5e 427 if (SCM_EQ_P (x, program))
a98cef7e 428 {
f21dfea6 429 int i;
17e90c5e
KN
430
431 /* Move arguments */
f21dfea6
KN
432 INIT_ARGS ();
433 sp -= bp->nargs - 1;
434 for (i = 0; i < bp->nargs; i++)
435 LOCAL_SET (i, sp[i]);
f41cb00c
LC
436
437 /* Drop the first argument and the program itself. */
438 sp -= 2;
a98cef7e 439
f21dfea6 440 /* Call itself */
17e90c5e 441 ip = bp->base;
17e90c5e 442 APPLY_HOOK ();
a98cef7e
KN
443 NEXT;
444 }
17e90c5e
KN
445 /*
446 * Proper tail call
447 */
3616e9e9 448 if (SCM_PROGRAM_P (x))
17e90c5e 449 {
17e90c5e
KN
450 EXIT_HOOK ();
451 FREE_FRAME ();
3616e9e9 452 program = x;
3d5ee0cd 453 goto vm_call_program;
17e90c5e 454 }
a98cef7e
KN
455 /*
456 * Function call
457 */
3616e9e9 458 if (!SCM_FALSEP (scm_procedure_p (x)))
a98cef7e 459 {
f21dfea6 460 SCM args;
17e90c5e 461 POP_LIST (nargs);
f21dfea6
KN
462 POP (args);
463 *sp = scm_apply (x, args, SCM_EOL);
a98cef7e
KN
464 goto vm_return;
465 }
466 /*
467 * Continuation call
468 */
3616e9e9 469 if (SCM_VM_CONT_P (x))
a98cef7e
KN
470 goto vm_call_cc;
471
66292535 472 program = x;
17e90c5e
KN
473 goto vm_error_wrong_type_apply;
474}
475
3616e9e9
KN
476VM_DEFINE_INSTRUCTION (apply, "apply", 1, -1, 1)
477{
c8b9df71
KN
478 int len;
479 SCM ls;
480 POP (ls);
481
482 nargs = FETCH ();
483 if (nargs < 2)
484 goto vm_error_wrong_num_args;
485
486 len = scm_ilength (ls);
487 if (len < 0)
488 goto vm_error_wrong_type_arg;
489
490 for (; !SCM_NULLP (ls); ls = SCM_CDR (ls))
491 PUSH (SCM_CAR (ls));
492
493 nargs += len - 2;
494 goto vm_call;
3616e9e9
KN
495}
496
46cd9a34 497VM_DEFINE_INSTRUCTION (call_cc, "call/cc", 1, 1, 1)
17e90c5e 498{
3d5ee0cd
KN
499 SYNC_BEFORE_GC ();
500 PUSH (capture_vm_cont (vp));
17e90c5e
KN
501 POP (program);
502 nargs = 1;
503 goto vm_call;
a98cef7e
KN
504}
505
46cd9a34 506VM_DEFINE_INSTRUCTION (return, "return", 0, 0, 1)
a98cef7e 507{
a98cef7e 508 vm_return:
17e90c5e
KN
509 EXIT_HOOK ();
510 RETURN_HOOK ();
511 FREE_FRAME ();
512
15df3447 513 /* Restore the last program */
af988bbf 514 program = SCM_FRAME_PROGRAM (fp);
499a4c07 515 CACHE_PROGRAM ();
af988bbf 516 CACHE_EXTERNAL ();
a98cef7e
KN
517 NEXT;
518}
17e90c5e 519
17e90c5e
KN
520/*
521 Local Variables:
522 c-file-style: "gnu"
523 End:
524*/