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