recompiling with compile environments, fluid languages, cleanups
[bpt/guile.git] / libguile / vm-i-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 ();
a222b0fa 60 nvalues = SCM_I_INUM (*sp--);
11ea1aba 61 NULLSTACK (1);
a222b0fa
AW
62 if (nvalues == 1)
63 POP (ret);
64 else
65 {
66 POP_LIST (nvalues);
67 POP (ret);
877ffa3f 68 SYNC_REGISTER ();
a222b0fa
AW
69 ret = scm_values (ret);
70 }
71
1dc8f851 72 {
11ea1aba
AW
73 ASSERT (sp == stack_base);
74 ASSERT (stack_base == SCM_FRAME_UPPER_ADDRESS (fp) - 1);
1dc8f851
AW
75
76 /* Restore registers */
77 sp = SCM_FRAME_LOWER_ADDRESS (fp) - 1;
78 ip = NULL;
79 fp = SCM_FRAME_DYNAMIC_LINK (fp);
11ea1aba 80 NULLSTACK (stack_base - sp);
1dc8f851 81 }
17e90c5e 82 SYNC_ALL ();
17d1b4bf 83 scm_dynwind_end ();
17e90c5e 84 return ret;
a98cef7e
KN
85}
86
7a0d0cee
KN
87VM_DEFINE_INSTRUCTION (break, "break", 0, 0, 0)
88{
89 BREAK_HOOK ();
90 NEXT;
91}
92
499a4c07 93VM_DEFINE_INSTRUCTION (drop, "drop", 0, 0, 0)
a98cef7e 94{
17e90c5e 95 DROP ();
a98cef7e
KN
96 NEXT;
97}
98
cb4cca12
KN
99VM_DEFINE_INSTRUCTION (mark, "mark", 0, 0, 1)
100{
101 PUSH (SCM_UNDEFINED);
102 NEXT;
103}
104
46cd9a34 105VM_DEFINE_INSTRUCTION (dup, "dup", 0, 0, 1)
26403690 106{
f349065e
KN
107 SCM x = *sp;
108 PUSH (x);
26403690
KN
109 NEXT;
110}
111
17e90c5e
KN
112\f
113/*
114 * Object creation
115 */
a98cef7e 116
46cd9a34 117VM_DEFINE_INSTRUCTION (void, "void", 0, 0, 1)
a98cef7e 118{
17e90c5e 119 PUSH (SCM_UNSPECIFIED);
a98cef7e
KN
120 NEXT;
121}
122
46cd9a34 123VM_DEFINE_INSTRUCTION (make_true, "make-true", 0, 0, 1)
a98cef7e 124{
17e90c5e 125 PUSH (SCM_BOOL_T);
a98cef7e
KN
126 NEXT;
127}
128
46cd9a34 129VM_DEFINE_INSTRUCTION (make_false, "make-false", 0, 0, 1)
a98cef7e 130{
17e90c5e 131 PUSH (SCM_BOOL_F);
a98cef7e
KN
132 NEXT;
133}
134
46cd9a34 135VM_DEFINE_INSTRUCTION (make_eol, "make-eol", 0, 0, 1)
a98cef7e 136{
17e90c5e 137 PUSH (SCM_EOL);
a98cef7e
KN
138 NEXT;
139}
140
46cd9a34 141VM_DEFINE_INSTRUCTION (make_int8, "make-int8", 1, 0, 1)
a98cef7e 142{
2d80426a 143 PUSH (SCM_I_MAKINUM ((signed char) FETCH ()));
a98cef7e
KN
144 NEXT;
145}
146
46cd9a34 147VM_DEFINE_INSTRUCTION (make_int8_0, "make-int8:0", 0, 0, 1)
a98cef7e 148{
238e7a11 149 PUSH (SCM_INUM0);
a98cef7e
KN
150 NEXT;
151}
152
46cd9a34 153VM_DEFINE_INSTRUCTION (make_int8_1, "make-int8:1", 0, 0, 1)
a98cef7e 154{
238e7a11 155 PUSH (SCM_I_MAKINUM (1));
a98cef7e
KN
156 NEXT;
157}
158
46cd9a34 159VM_DEFINE_INSTRUCTION (make_int16, "make-int16", 2, 0, 1)
a98cef7e 160{
ea9b4b29
KN
161 int h = FETCH ();
162 int l = FETCH ();
2d80426a 163 PUSH (SCM_I_MAKINUM ((signed short) (h << 8) + l));
a98cef7e
KN
164 NEXT;
165}
166
46cd9a34 167VM_DEFINE_INSTRUCTION (make_char8, "make-char8", 1, 0, 1)
a98cef7e 168{
17e90c5e 169 PUSH (SCM_MAKE_CHAR (FETCH ()));
a98cef7e
KN
170 NEXT;
171}
172
23b587b0 173VM_DEFINE_INSTRUCTION (list, "list", 2, -1, 1)
cb4cca12 174{
23b587b0
LC
175 unsigned h = FETCH ();
176 unsigned l = FETCH ();
177 unsigned len = ((h << 8) + l);
178 POP_LIST (len);
cb4cca12
KN
179 NEXT;
180}
181
23b587b0 182VM_DEFINE_INSTRUCTION (vector, "vector", 2, -1, 1)
cb4cca12 183{
23b587b0
LC
184 unsigned h = FETCH ();
185 unsigned l = FETCH ();
186 unsigned len = ((h << 8) + l);
187 POP_LIST (len);
877ffa3f 188 SYNC_REGISTER ();
cb4cca12
KN
189 *sp = scm_vector (*sp);
190 NEXT;
191}
192
193VM_DEFINE_INSTRUCTION (list_mark, "list-mark", 0, 0, 0)
194{
195 POP_LIST_MARK ();
196 NEXT;
197}
198
2bd859c8
AW
199VM_DEFINE_INSTRUCTION (cons_mark, "cons-mark", 0, 0, 0)
200{
201 POP_CONS_MARK ();
202 NEXT;
203}
204
cb4cca12
KN
205VM_DEFINE_INSTRUCTION (vector_mark, "vector-mark", 0, 0, 0)
206{
207 POP_LIST_MARK ();
877ffa3f 208 SYNC_REGISTER ();
cb4cca12
KN
209 *sp = scm_vector (*sp);
210 NEXT;
211}
212
213VM_DEFINE_INSTRUCTION (list_break, "list-break", 0, 0, 0)
214{
215 SCM l;
216 POP (l);
1f40459f 217 PUSH_LIST (l);
cb4cca12
KN
218 NEXT;
219}
220
a98cef7e
KN
221\f
222/*
17e90c5e 223 * Variable access
a98cef7e
KN
224 */
225
17e90c5e
KN
226#define OBJECT_REF(i) objects[i]
227#define OBJECT_SET(i,o) objects[i] = o
a98cef7e 228
af988bbf
KN
229#define LOCAL_REF(i) SCM_FRAME_VARIABLE (fp, i)
230#define LOCAL_SET(i,o) SCM_FRAME_VARIABLE (fp, i) = o
a98cef7e 231
2d80426a
LC
232/* For the variable operations, we _must_ obviously avoid function calls to
233 `scm_variable_ref ()', `scm_variable_bound_p ()' and friends which do
234 nothing more than the corresponding macros. */
235#define VARIABLE_REF(v) SCM_VARIABLE_REF (v)
236#define VARIABLE_SET(v,o) SCM_VARIABLE_SET (v, o)
237#define VARIABLE_BOUNDP(v) (VARIABLE_REF (v) != SCM_UNDEFINED)
a98cef7e 238
17e90c5e 239/* ref */
a98cef7e 240
46cd9a34 241VM_DEFINE_INSTRUCTION (object_ref, "object-ref", 1, 0, 1)
a98cef7e 242{
a52b2d3d 243 register unsigned objnum = FETCH ();
0b5f0e49
LC
244 CHECK_OBJECT (objnum);
245 PUSH (OBJECT_REF (objnum));
17e90c5e 246 NEXT;
a98cef7e
KN
247}
248
46cd9a34 249VM_DEFINE_INSTRUCTION (local_ref, "local-ref", 1, 0, 1)
a98cef7e 250{
17e90c5e
KN
251 PUSH (LOCAL_REF (FETCH ()));
252 NEXT;
a98cef7e
KN
253}
254
46cd9a34 255VM_DEFINE_INSTRUCTION (external_ref, "external-ref", 1, 0, 1)
a98cef7e 256{
17e90c5e
KN
257 unsigned int i;
258 SCM e = external;
259 for (i = FETCH (); i; i--)
2a63758b
KN
260 {
261 CHECK_EXTERNAL(e);
262 e = SCM_CDR (e);
263 }
264 CHECK_EXTERNAL(e);
17e90c5e 265 PUSH (SCM_CAR (e));
a98cef7e
KN
266 NEXT;
267}
268
46cd9a34 269VM_DEFINE_INSTRUCTION (variable_ref, "variable-ref", 0, 0, 1)
a98cef7e 270{
17e90c5e 271 SCM x = *sp;
238e7a11 272
2d80426a 273 if (!VARIABLE_BOUNDP (x))
17e90c5e 274 {
238e7a11
LC
275 err_args = SCM_LIST1 (x);
276 /* Was: err_args = SCM_LIST1 (SCM_CAR (x)); */
17e90c5e
KN
277 goto vm_error_unbound;
278 }
238e7a11
LC
279 else
280 {
2d80426a 281 SCM o = VARIABLE_REF (x);
238e7a11
LC
282 *sp = o;
283 }
284
a98cef7e
KN
285 NEXT;
286}
287
9cc649b8
AW
288VM_DEFINE_INSTRUCTION (late_variable_ref, "late-variable-ref", 1, 0, 1)
289{
6297d229 290 unsigned objnum = FETCH ();
fd358575 291 SCM what;
9cc649b8 292 CHECK_OBJECT (objnum);
fd358575 293 what = OBJECT_REF (objnum);
9cc649b8 294
fd358575 295 if (!SCM_VARIABLEP (what))
9cc649b8 296 {
d0168f3d 297 SYNC_REGISTER ();
fd358575 298 if (SCM_LIKELY (SCM_SYMBOLP (what)))
3aabb7b7 299 {
fd358575
AW
300 if (SCM_LIKELY (scm_module_system_booted_p
301 && scm_is_true (bp->module)))
302 /* might longjmp */
303 what = scm_module_lookup (bp->module, what);
304 else
305 what = scm_sym2var (what, SCM_BOOL_F, SCM_BOOL_F);
3aabb7b7
AW
306 }
307 else
308 {
fd358575
AW
309 SCM mod;
310 /* compilation of @ or @@
311 `what' is a three-element list: (MODNAME SYM INTERFACE?)
312 INTERFACE? is #t if we compiled @ or #f if we compiled @@
313 */
314 mod = scm_resolve_module (SCM_CAR (what));
315 if (scm_is_true (SCM_CADDR (what)))
316 mod = scm_module_public_interface (mod);
317 if (SCM_FALSEP (mod))
318 {
319 err_args = SCM_LIST1 (mod);
320 goto vm_error_no_such_module;
321 }
322 /* might longjmp */
323 what = scm_module_lookup (mod, SCM_CADR (what));
3aabb7b7
AW
324 }
325
fd358575 326 if (!VARIABLE_BOUNDP (what))
9cc649b8 327 {
fd358575 328 err_args = SCM_LIST1 (what);
9cc649b8
AW
329 goto vm_error_unbound;
330 }
3aabb7b7 331
fd358575 332 OBJECT_SET (objnum, what);
9cc649b8
AW
333 }
334
fd358575 335 PUSH (VARIABLE_REF (what));
9cc649b8
AW
336 NEXT;
337}
338
17e90c5e
KN
339/* set */
340
46cd9a34 341VM_DEFINE_INSTRUCTION (local_set, "local-set", 1, 1, 0)
a98cef7e 342{
17e90c5e
KN
343 LOCAL_SET (FETCH (), *sp);
344 DROP ();
a98cef7e
KN
345 NEXT;
346}
347
46cd9a34 348VM_DEFINE_INSTRUCTION (external_set, "external-set", 1, 1, 0)
a98cef7e 349{
17e90c5e
KN
350 unsigned int i;
351 SCM e = external;
352 for (i = FETCH (); i; i--)
ac02b386
KN
353 {
354 CHECK_EXTERNAL(e);
355 e = SCM_CDR (e);
356 }
357 CHECK_EXTERNAL(e);
17e90c5e
KN
358 SCM_SETCAR (e, *sp);
359 DROP ();
a98cef7e
KN
360 NEXT;
361}
362
46cd9a34 363VM_DEFINE_INSTRUCTION (variable_set, "variable-set", 0, 1, 0)
a98cef7e 364{
2d80426a 365 VARIABLE_SET (sp[0], sp[-1]);
11ea1aba 366 DROPN (2);
a98cef7e
KN
367 NEXT;
368}
369
9cc649b8
AW
370VM_DEFINE_INSTRUCTION (late_variable_set, "late-variable-set", 1, 1, 0)
371{
6297d229 372 unsigned objnum = FETCH ();
fd358575 373 SCM what;
9cc649b8 374 CHECK_OBJECT (objnum);
fd358575 375 what = OBJECT_REF (objnum);
9cc649b8 376
fd358575 377 if (!SCM_VARIABLEP (what))
9cc649b8 378 {
6287726a 379 SYNC_BEFORE_GC ();
fd358575 380 if (SCM_LIKELY (SCM_SYMBOLP (what)))
3aabb7b7 381 {
fd358575
AW
382 if (SCM_LIKELY (scm_module_system_booted_p
383 && scm_is_true (bp->module)))
384 /* might longjmp */
385 what = scm_module_lookup (bp->module, what);
386 else
387 what = scm_sym2var (what, SCM_BOOL_F, SCM_BOOL_F);
3aabb7b7
AW
388 }
389 else
390 {
fd358575
AW
391 SCM mod;
392 /* compilation of @ or @@
393 `what' is a three-element list: (MODNAME SYM INTERFACE?)
394 INTERFACE? is #t if we compiled @ or #f if we compiled @@
395 */
396 mod = scm_resolve_module (SCM_CAR (what));
397 if (scm_is_true (SCM_CADDR (what)))
398 mod = scm_module_public_interface (mod);
399 if (SCM_FALSEP (mod))
400 {
401 err_args = SCM_LIST1 (what);
402 goto vm_error_no_such_module;
403 }
404 /* might longjmp */
405 what = scm_module_lookup (mod, SCM_CADR (what));
3aabb7b7
AW
406 }
407
fd358575 408 OBJECT_SET (objnum, what);
9cc649b8
AW
409 }
410
fd358575 411 VARIABLE_SET (what, *sp);
9cc649b8
AW
412 DROP ();
413 NEXT;
414}
415
3de80ed5
AW
416VM_DEFINE_INSTRUCTION (externals, "externals", 0, 0, 1)
417{
418 PUSH (external);
419 NEXT;
420}
421
a98cef7e
KN
422\f
423/*
424 * branch and jump
425 */
426
efbd5892
AW
427/* offset must be a signed short!!! */
428#define FETCH_OFFSET(offset) \
17e90c5e 429{ \
41f248a8
KN
430 int h = FETCH (); \
431 int l = FETCH (); \
efbd5892
AW
432 offset = (h << 8) + l; \
433}
434
435#define BR(p) \
436{ \
437 signed short offset; \
438 FETCH_OFFSET (offset); \
17e90c5e
KN
439 if (p) \
440 ip += offset; \
11ea1aba 441 NULLSTACK (1); \
17e90c5e
KN
442 DROP (); \
443 NEXT; \
444}
445
41f248a8
KN
446VM_DEFINE_INSTRUCTION (br, "br", 2, 0, 0)
447{
448 int h = FETCH ();
449 int l = FETCH ();
450 ip += (signed short) (h << 8) + l;
451 NEXT;
452}
453
454VM_DEFINE_INSTRUCTION (br_if, "br-if", 2, 0, 0)
a98cef7e 455{
17e90c5e 456 BR (!SCM_FALSEP (*sp));
a98cef7e
KN
457}
458
41f248a8 459VM_DEFINE_INSTRUCTION (br_if_not, "br-if-not", 2, 0, 0)
a98cef7e 460{
17e90c5e 461 BR (SCM_FALSEP (*sp));
a98cef7e
KN
462}
463
41f248a8 464VM_DEFINE_INSTRUCTION (br_if_eq, "br-if-eq", 2, 0, 0)
a98cef7e 465{
17e90c5e 466 BR (SCM_EQ_P (sp[0], sp--[1]));
a98cef7e
KN
467}
468
41f248a8 469VM_DEFINE_INSTRUCTION (br_if_not_eq, "br-if-not-eq", 2, 0, 0)
a98cef7e 470{
17e90c5e
KN
471 BR (!SCM_EQ_P (sp[0], sp--[1]));
472}
473
41f248a8 474VM_DEFINE_INSTRUCTION (br_if_null, "br-if-null", 2, 0, 0)
17e90c5e
KN
475{
476 BR (SCM_NULLP (*sp));
477}
478
41f248a8 479VM_DEFINE_INSTRUCTION (br_if_not_null, "br-if-not-null", 2, 0, 0)
17e90c5e
KN
480{
481 BR (!SCM_NULLP (*sp));
a98cef7e
KN
482}
483
a98cef7e
KN
484\f
485/*
486 * Subprogram call
487 */
488
46cd9a34 489VM_DEFINE_INSTRUCTION (make_closure, "make-closure", 0, 1, 1)
a98cef7e 490{
3d5ee0cd
KN
491 SYNC_BEFORE_GC ();
492 *sp = scm_c_make_closure (*sp, external);
17e90c5e 493 NEXT;
a98cef7e
KN
494}
495
46cd9a34 496VM_DEFINE_INSTRUCTION (call, "call", 1, -1, 1)
a98cef7e 497{
3616e9e9 498 SCM x;
17e90c5e 499 nargs = FETCH ();
a98cef7e
KN
500
501 vm_call:
c8b9df71
KN
502 x = sp[-nargs];
503
a98cef7e
KN
504 /*
505 * Subprogram call
506 */
3616e9e9 507 if (SCM_PROGRAM_P (x))
a98cef7e 508 {
3616e9e9 509 program = x;
499a4c07 510 CACHE_PROGRAM ();
17e90c5e
KN
511 INIT_ARGS ();
512 NEW_FRAME ();
17e90c5e
KN
513 ENTER_HOOK ();
514 APPLY_HOOK ();
a98cef7e
KN
515 NEXT;
516 }
d507b25f
AW
517#ifdef ENABLE_TRAMPOLINE
518 /* Seems to slow down the fibo test, dunno why */
a98cef7e 519 /*
659b4611
AW
520 * Subr call
521 */
522 switch (nargs)
523 {
524 case 0:
525 {
526 scm_t_trampoline_0 call = scm_trampoline_0 (x);
527 if (call)
528 {
529 SYNC_ALL ();
530 *sp = call (x);
531 NEXT;
532 }
533 break;
534 }
535 case 1:
536 {
537 scm_t_trampoline_1 call = scm_trampoline_1 (x);
538 if (call)
539 {
540 SCM arg1;
541 POP (arg1);
542 SYNC_ALL ();
543 *sp = call (x, arg1);
544 NEXT;
545 }
546 break;
547 }
548 case 2:
549 {
550 scm_t_trampoline_2 call = scm_trampoline_2 (x);
551 if (call)
552 {
553 SCM arg1, arg2;
554 POP (arg2);
555 POP (arg1);
556 SYNC_ALL ();
557 *sp = call (x, arg1, arg2);
558 NEXT;
559 }
560 break;
561 }
562 }
d507b25f 563#endif
659b4611
AW
564 /*
565 * Other interpreted or compiled call
a98cef7e 566 */
3616e9e9 567 if (!SCM_FALSEP (scm_procedure_p (x)))
a98cef7e 568 {
f41cb00c
LC
569 /* At this point, the stack contains the procedure and each one of its
570 arguments. */
17e90c5e 571 POP_LIST (nargs);
1865ad56 572 SYNC_REGISTER ();
887ce75a
AW
573 /* keep args on stack so they are marked */
574 sp[-1] = scm_apply (x, sp[0], SCM_EOL);
66db076a 575 NULLSTACK_FOR_NONLOCAL_EXIT ();
76282387 576 /* FIXME what if SCM_VALUESP(*sp) */
887ce75a 577 DROP ();
17e90c5e 578 NEXT;
a98cef7e
KN
579 }
580 /*
581 * Continuation call
582 */
3616e9e9 583 if (SCM_VM_CONT_P (x))
a98cef7e 584 {
fcd4901b 585 program = x;
f03c31db 586 vm_call_continuation:
a98cef7e 587 /* Check the number of arguments */
f03c31db 588 /* FIXME multiple args */
382693fe 589 if (nargs != 1)
fcd4901b 590 scm_wrong_num_args (program);
a98cef7e
KN
591
592 /* Reinstate the continuation */
17e90c5e 593 EXIT_HOOK ();
fcd4901b 594 reinstate_vm_cont (vp, program);
3d5ee0cd 595 CACHE_REGISTER ();
af988bbf 596 program = SCM_FRAME_PROGRAM (fp);
3616e9e9 597 CACHE_PROGRAM ();
a98cef7e
KN
598 NEXT;
599 }
600
66292535 601 program = x;
17e90c5e 602 goto vm_error_wrong_type_apply;
a98cef7e
KN
603}
604
f03c31db 605VM_DEFINE_INSTRUCTION (goto_args, "goto/args", 1, -1, 1)
a98cef7e 606{
f41cb00c 607 register SCM x;
17e90c5e 608 nargs = FETCH ();
f03c31db 609 vm_goto_args:
3616e9e9 610 x = sp[-nargs];
17e90c5e 611
28a2f57b 612 SYNC_REGISTER ();
17e90c5e 613 SCM_TICK; /* allow interrupt here */
a98cef7e
KN
614
615 /*
17e90c5e 616 * Tail recursive call
a98cef7e 617 */
17e90c5e 618 if (SCM_EQ_P (x, program))
a98cef7e 619 {
f21dfea6 620 int i;
17e90c5e
KN
621
622 /* Move arguments */
f21dfea6
KN
623 INIT_ARGS ();
624 sp -= bp->nargs - 1;
625 for (i = 0; i < bp->nargs; i++)
626 LOCAL_SET (i, sp[i]);
f41cb00c
LC
627
628 /* Drop the first argument and the program itself. */
629 sp -= 2;
5e390de6
AW
630 NULLSTACK (bp->nargs + 1);
631
632 /* Freshen the externals */
633 external = bp->external;
634 for (i = 0; i < bp->nexts; i++)
635 CONS (external, SCM_UNDEFINED, external);
636 SCM_FRAME_DATA_ADDRESS (fp)[0] = external;
a98cef7e 637
f21dfea6 638 /* Call itself */
17e90c5e 639 ip = bp->base;
17e90c5e 640 APPLY_HOOK ();
a98cef7e
KN
641 NEXT;
642 }
28106f54 643
17e90c5e 644 /*
28106f54 645 * Tail call, but not to self -- reuse the frame, keeping the ra and dl
17e90c5e 646 */
3616e9e9 647 if (SCM_PROGRAM_P (x))
17e90c5e 648 {
28106f54
AW
649 SCM *data, *tail_args, *dl;
650 int i;
da320011 651 scm_byte_t *ra, *mvra;
11ea1aba
AW
652#ifdef VM_ENABLE_STACK_NULLING
653 SCM *old_sp;
654#endif
28106f54 655
17e90c5e 656 EXIT_HOOK ();
28106f54
AW
657
658 /* save registers */
659 tail_args = stack_base + 2;
660 ra = SCM_FRAME_RETURN_ADDRESS (fp);
da320011 661 mvra = SCM_FRAME_MV_RETURN_ADDRESS (fp);
28106f54
AW
662 dl = SCM_FRAME_DYNAMIC_LINK (fp);
663
664 /* switch programs */
11ea1aba 665 program = x;
28106f54
AW
666 CACHE_PROGRAM ();
667 INIT_ARGS ();
11ea1aba
AW
668 /* delay updating the frame so that if INIT_ARGS has to cons up a rest
669 arg, going into GC, the stack still makes sense */
670 fp[-1] = program;
28106f54
AW
671 nargs = bp->nargs;
672
11ea1aba
AW
673#ifdef VM_ENABLE_STACK_NULLING
674 old_sp = sp;
675 CHECK_STACK_LEAK ();
676#endif
677
28106f54
AW
678 /* new registers -- logically this would be better later, but let's make
679 sure we have space for the locals now */
680 data = SCM_FRAME_DATA_ADDRESS (fp);
681 ip = bp->base;
da320011 682 stack_base = data + 4;
28106f54
AW
683 sp = stack_base;
684 CHECK_OVERFLOW ();
685
686 /* copy args, bottom-up */
687 for (i = 0; i < nargs; i++)
688 fp[i] = tail_args[i];
689
11ea1aba
AW
690 NULLSTACK (old_sp - sp);
691
28106f54
AW
692 /* init locals */
693 for (i = bp->nlocs; i; i--)
694 data[-i] = SCM_UNDEFINED;
695
28106f54 696 /* Set frame data */
da320011
AW
697 data[4] = (SCM)ra;
698 data[3] = (SCM)mvra;
28106f54
AW
699 data[2] = (SCM)dl;
700 data[1] = SCM_BOOL_F;
11ea1aba
AW
701
702 /* Postpone initializing external vars, because if the CONS causes a GC,
703 we want the stack marker to see the data array formatted as expected. */
704 data[0] = SCM_UNDEFINED;
705 external = bp->external;
706 for (i = 0; i < bp->nexts; i++)
707 CONS (external, SCM_UNDEFINED, external);
28106f54 708 data[0] = external;
11ea1aba 709
28106f54
AW
710 ENTER_HOOK ();
711 APPLY_HOOK ();
712 NEXT;
17e90c5e 713 }
d507b25f
AW
714#ifdef ENABLE_TRAMPOLINE
715 /* This seems to actually slow down the fibo test -- dunno why */
a98cef7e 716 /*
659b4611
AW
717 * Subr call
718 */
719 switch (nargs)
720 {
721 case 0:
722 {
723 scm_t_trampoline_0 call = scm_trampoline_0 (x);
724 if (call)
725 {
726 SYNC_ALL ();
727 *sp = call (x);
728 goto vm_return;
729 }
730 break;
731 }
732 case 1:
733 {
734 scm_t_trampoline_1 call = scm_trampoline_1 (x);
735 if (call)
736 {
737 SCM arg1;
738 POP (arg1);
739 SYNC_ALL ();
740 *sp = call (x, arg1);
741 goto vm_return;
742 }
743 break;
744 }
745 case 2:
746 {
747 scm_t_trampoline_2 call = scm_trampoline_2 (x);
748 if (call)
749 {
750 SCM arg1, arg2;
751 POP (arg2);
752 POP (arg1);
753 SYNC_ALL ();
754 *sp = call (x, arg1, arg2);
755 goto vm_return;
756 }
757 break;
758 }
759 }
d507b25f 760#endif
659b4611
AW
761
762 /*
763 * Other interpreted or compiled call
a98cef7e 764 */
3616e9e9 765 if (!SCM_FALSEP (scm_procedure_p (x)))
a98cef7e 766 {
17e90c5e 767 POP_LIST (nargs);
1865ad56 768 SYNC_REGISTER ();
887ce75a 769 sp[-1] = scm_apply (x, sp[0], SCM_EOL);
66db076a 770 NULLSTACK_FOR_NONLOCAL_EXIT ();
887ce75a 771 DROP ();
76282387 772 /* FIXME what if SCM_VALUESP(*sp) */
a98cef7e
KN
773 goto vm_return;
774 }
fcd4901b
AW
775
776 program = x;
777
a98cef7e
KN
778 /*
779 * Continuation call
780 */
fcd4901b 781 if (SCM_VM_CONT_P (program))
f03c31db 782 goto vm_call_continuation;
a98cef7e 783
17e90c5e
KN
784 goto vm_error_wrong_type_apply;
785}
786
efbd5892
AW
787VM_DEFINE_INSTRUCTION (goto_nargs, "goto/nargs", 0, 0, 1)
788{
789 SCM x;
790 POP (x);
791 nargs = scm_to_int (x);
d51406fe 792 /* FIXME: should truncate values? */
efbd5892
AW
793 goto vm_goto_args;
794}
795
796VM_DEFINE_INSTRUCTION (call_nargs, "call/nargs", 0, 0, 1)
797{
798 SCM x;
799 POP (x);
800 nargs = scm_to_int (x);
d51406fe 801 /* FIXME: should truncate values? */
efbd5892
AW
802 goto vm_call;
803}
804
805VM_DEFINE_INSTRUCTION (mv_call, "mv-call", 3, -1, 1)
a222b0fa
AW
806{
807 SCM x;
efbd5892 808 signed short offset;
a222b0fa
AW
809
810 nargs = FETCH ();
efbd5892 811 FETCH_OFFSET (offset);
a222b0fa
AW
812
813 x = sp[-nargs];
814
815 /*
816 * Subprogram call
817 */
818 if (SCM_PROGRAM_P (x))
819 {
820 program = x;
821 CACHE_PROGRAM ();
822 INIT_ARGS ();
823 NEW_FRAME ();
824 SCM_FRAME_DATA_ADDRESS (fp)[3] = (SCM)(SCM_FRAME_RETURN_ADDRESS (fp) + offset);
825 ENTER_HOOK ();
826 APPLY_HOOK ();
827 NEXT;
828 }
829 /*
830 * Other interpreted or compiled call
831 */
832 if (!SCM_FALSEP (scm_procedure_p (x)))
833 {
834 /* At this point, the stack contains the procedure and each one of its
835 arguments. */
a222b0fa 836 POP_LIST (nargs);
a222b0fa 837 SYNC_REGISTER ();
887ce75a 838 sp[-1] = scm_apply (x, sp[0], SCM_EOL);
66db076a 839 NULLSTACK_FOR_NONLOCAL_EXIT ();
887ce75a 840 DROP ();
a222b0fa
AW
841 if (SCM_VALUESP (*sp))
842 {
843 SCM values, len;
844 POP (values);
845 values = scm_struct_ref (values, SCM_INUM0);
846 len = scm_length (values);
1f40459f 847 PUSH_LIST (values);
a222b0fa
AW
848 PUSH (len);
849 ip += offset;
850 }
851 NEXT;
852 }
853 /*
854 * Continuation call
855 */
856 if (SCM_VM_CONT_P (x))
857 {
858 program = x;
859 goto vm_call_continuation;
860 }
861
862 program = x;
863 goto vm_error_wrong_type_apply;
864}
865
3616e9e9
KN
866VM_DEFINE_INSTRUCTION (apply, "apply", 1, -1, 1)
867{
c8b9df71
KN
868 int len;
869 SCM ls;
870 POP (ls);
871
872 nargs = FETCH ();
9a8cc8e7 873 ASSERT (nargs >= 2);
c8b9df71
KN
874
875 len = scm_ilength (ls);
876 if (len < 0)
877 goto vm_error_wrong_type_arg;
878
1f40459f 879 PUSH_LIST (ls);
c8b9df71
KN
880
881 nargs += len - 2;
882 goto vm_call;
3616e9e9
KN
883}
884
f03c31db
AW
885VM_DEFINE_INSTRUCTION (goto_apply, "goto/apply", 1, -1, 1)
886{
887 int len;
888 SCM ls;
889 POP (ls);
890
891 nargs = FETCH ();
9a8cc8e7 892 ASSERT (nargs >= 2);
f03c31db
AW
893
894 len = scm_ilength (ls);
895 if (len < 0)
896 goto vm_error_wrong_type_arg;
897
1f40459f 898 PUSH_LIST (ls);
f03c31db
AW
899
900 nargs += len - 2;
901 goto vm_goto_args;
902}
903
76282387 904VM_DEFINE_INSTRUCTION (call_cc, "call/cc", 0, 1, 1)
17e90c5e 905{
76282387
AW
906 int first;
907 SCM proc, cont;
908 POP (proc);
909 SYNC_ALL ();
910 cont = scm_make_continuation (&first);
911 if (first)
912 {
913 PUSH (proc);
914 PUSH (cont);
915 nargs = 1;
916 goto vm_call;
917 }
11ea1aba
AW
918 ASSERT (sp == vp->sp);
919 ASSERT (fp == vp->fp);
76282387
AW
920 else if (SCM_VALUESP (cont))
921 {
922 /* multiple values returned to continuation */
923 SCM values;
924 values = scm_struct_ref (cont, SCM_INUM0);
925 if (SCM_NULLP (values))
9a8cc8e7 926 goto vm_error_no_values;
76282387
AW
927 /* non-tail context does not accept multiple values? */
928 PUSH (SCM_CAR (values));
929 NEXT;
930 }
931 else
932 {
933 PUSH (cont);
934 NEXT;
935 }
a98cef7e
KN
936}
937
76282387 938VM_DEFINE_INSTRUCTION (goto_cc, "goto/cc", 0, 1, 1)
f03c31db 939{
76282387
AW
940 int first;
941 SCM proc, cont;
942 POP (proc);
943 SYNC_ALL ();
944 cont = scm_make_continuation (&first);
66db076a
AW
945 ASSERT (sp == vp->sp);
946 ASSERT (fp == vp->fp);
76282387
AW
947 if (first)
948 {
949 PUSH (proc);
950 PUSH (cont);
951 nargs = 1;
952 goto vm_goto_args;
953 }
954 else if (SCM_VALUESP (cont))
955 {
956 /* multiple values returned to continuation */
957 SCM values;
958 values = scm_struct_ref (cont, SCM_INUM0);
959 nvalues = scm_ilength (values);
1f40459f 960 PUSH_LIST (values);
76282387
AW
961 goto vm_return_values;
962 }
963 else
964 {
965 PUSH (cont);
966 goto vm_return;
967 }
f03c31db
AW
968}
969
46cd9a34 970VM_DEFINE_INSTRUCTION (return, "return", 0, 0, 1)
a98cef7e 971{
a98cef7e 972 vm_return:
17e90c5e
KN
973 EXIT_HOOK ();
974 RETURN_HOOK ();
f13c269b
AW
975 {
976 SCM ret, *data;
977 data = SCM_FRAME_DATA_ADDRESS (fp);
978
979 POP (ret);
11ea1aba
AW
980 ASSERT (sp == stack_base);
981 ASSERT (stack_base == data + 4);
f13c269b
AW
982
983 /* Restore registers */
984 sp = SCM_FRAME_LOWER_ADDRESS (fp);
da320011 985 ip = SCM_FRAME_BYTE_CAST (data[4]);
f13c269b 986 fp = SCM_FRAME_STACK_CAST (data[2]);
11ea1aba
AW
987 {
988#ifdef VM_ENABLE_STACK_NULLING
989 int nullcount = stack_base - sp;
990#endif
991 stack_base = SCM_FRAME_UPPER_ADDRESS (fp) - 1;
992 NULLSTACK (nullcount);
993 }
f13c269b
AW
994
995 /* Set return value (sp is already pushed) */
996 *sp = ret;
997 }
17e90c5e 998
15df3447 999 /* Restore the last program */
af988bbf 1000 program = SCM_FRAME_PROGRAM (fp);
499a4c07 1001 CACHE_PROGRAM ();
af988bbf 1002 CACHE_EXTERNAL ();
7e4760e4 1003 CHECK_IP ();
a98cef7e
KN
1004 NEXT;
1005}
17e90c5e 1006
a222b0fa
AW
1007VM_DEFINE_INSTRUCTION (return_values, "return/values", 1, -1, -1)
1008{
ef24c01b
AW
1009 /* nvalues declared at top level, because for some reason gcc seems to think
1010 that perhaps it might be used without declaration. Fooey to that, I say. */
1011 SCM *data;
1012
1013 nvalues = FETCH ();
1014 vm_return_values:
a222b0fa
AW
1015 EXIT_HOOK ();
1016 RETURN_HOOK ();
ef24c01b
AW
1017
1018 data = SCM_FRAME_DATA_ADDRESS (fp);
11ea1aba 1019 ASSERT (stack_base == data + 4);
a222b0fa 1020
ef24c01b
AW
1021 /* data[3] is the mv return address */
1022 if (nvalues != 1 && data[3])
1023 {
1024 int i;
1025 /* Restore registers */
1026 sp = SCM_FRAME_LOWER_ADDRESS (fp) - 1;
1027 ip = SCM_FRAME_BYTE_CAST (data[3]); /* multiple value ra */
1028 fp = SCM_FRAME_STACK_CAST (data[2]);
a222b0fa 1029
ef24c01b
AW
1030 /* Push return values, and the number of values */
1031 for (i = 0; i < nvalues; i++)
1032 *++sp = stack_base[1+i];
1033 *++sp = SCM_I_MAKINUM (nvalues);
a222b0fa 1034
ef24c01b 1035 /* Finally set new stack_base */
11ea1aba 1036 NULLSTACK (stack_base - sp + nvalues + 1);
ef24c01b
AW
1037 stack_base = SCM_FRAME_UPPER_ADDRESS (fp) - 1;
1038 }
1039 else if (nvalues >= 1)
1040 {
1041 /* Multiple values for a single-valued continuation -- here's where I
1042 break with guile tradition and try and do something sensible. (Also,
1043 this block handles the single-valued return to an mv
1044 continuation.) */
1045 /* Restore registers */
1046 sp = SCM_FRAME_LOWER_ADDRESS (fp) - 1;
1047 ip = SCM_FRAME_BYTE_CAST (data[4]); /* single value ra */
1048 fp = SCM_FRAME_STACK_CAST (data[2]);
a222b0fa 1049
ef24c01b
AW
1050 /* Push first value */
1051 *++sp = stack_base[1];
a222b0fa 1052
ef24c01b 1053 /* Finally set new stack_base */
11ea1aba 1054 NULLSTACK (stack_base - sp);
ef24c01b
AW
1055 stack_base = SCM_FRAME_UPPER_ADDRESS (fp) - 1;
1056 }
1057 else
1058 goto vm_error_no_values;
a222b0fa
AW
1059
1060 /* Restore the last program */
1061 program = SCM_FRAME_PROGRAM (fp);
1062 CACHE_PROGRAM ();
1063 CACHE_EXTERNAL ();
1064 CHECK_IP ();
1065 NEXT;
1066}
1067
ef24c01b
AW
1068VM_DEFINE_INSTRUCTION (return_values_star, "return/values*", 1, -1, -1)
1069{
1070 SCM l;
1071
1072 nvalues = FETCH ();
11ea1aba 1073 ASSERT (nvalues >= 1);
ef24c01b
AW
1074
1075 nvalues--;
1076 POP (l);
1077 while (SCM_CONSP (l))
1078 {
1079 PUSH (SCM_CAR (l));
1080 l = SCM_CDR (l);
1081 nvalues++;
1082 }
1083
1084 goto vm_return_values;
1085}
1086
d51406fe
AW
1087VM_DEFINE_INSTRUCTION (truncate_values, "truncate-values", 2, -1, -1)
1088{
1089 SCM x;
1090 int nbinds, rest;
1091 POP (x);
1092 nvalues = scm_to_int (x);
1093 nbinds = FETCH ();
1094 rest = FETCH ();
1095
1096 if (rest)
1097 nbinds--;
1098
1099 if (nvalues < nbinds)
1100 goto vm_error_not_enough_values;
1101
1102 if (rest)
1103 POP_LIST (nvalues - nbinds);
1104 else
1105 DROPN (nvalues - nbinds);
1106
1107 NEXT;
1108}
1109
17e90c5e
KN
1110/*
1111 Local Variables:
1112 c-file-style: "gnu"
1113 End:
1114*/