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