1 /* Copyright (C) 2001 Free Software Foundation, Inc.
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)
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.
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
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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. */
42 /* This file is included in vm_engine.c */
49 /* This must be the first instruction! */
50 VM_DEFINE_INSTRUCTION (nop
, "nop", 0, 0, 0)
55 VM_DEFINE_INSTRUCTION (halt
, "halt", 0, 0, 0)
58 vp
->time
+= scm_c_get_internal_run_time () - start_time
;
60 nvalues
= SCM_I_INUM (*sp
--);
69 ret
= scm_values (ret
);
73 ASSERT (sp
== stack_base
);
74 ASSERT (stack_base
== SCM_FRAME_UPPER_ADDRESS (fp
) - 1);
76 /* Restore registers */
77 sp
= SCM_FRAME_LOWER_ADDRESS (fp
) - 1;
79 fp
= SCM_FRAME_DYNAMIC_LINK (fp
);
80 NULLSTACK (stack_base
- sp
);
87 VM_DEFINE_INSTRUCTION (break, "break", 0, 0, 0)
93 VM_DEFINE_INSTRUCTION (drop
, "drop", 0, 0, 0)
99 VM_DEFINE_INSTRUCTION (mark
, "mark", 0, 0, 1)
101 PUSH (SCM_UNDEFINED
);
105 VM_DEFINE_INSTRUCTION (dup
, "dup", 0, 0, 1)
117 VM_DEFINE_INSTRUCTION (void, "void", 0, 0, 1)
119 PUSH (SCM_UNSPECIFIED
);
123 VM_DEFINE_INSTRUCTION (make_true
, "make-true", 0, 0, 1)
129 VM_DEFINE_INSTRUCTION (make_false
, "make-false", 0, 0, 1)
135 VM_DEFINE_INSTRUCTION (make_eol
, "make-eol", 0, 0, 1)
141 VM_DEFINE_INSTRUCTION (make_int8
, "make-int8", 1, 0, 1)
143 PUSH (SCM_I_MAKINUM ((signed char) FETCH ()));
147 VM_DEFINE_INSTRUCTION (make_int8_0
, "make-int8:0", 0, 0, 1)
153 VM_DEFINE_INSTRUCTION (make_int8_1
, "make-int8:1", 0, 0, 1)
155 PUSH (SCM_I_MAKINUM (1));
159 VM_DEFINE_INSTRUCTION (make_int16
, "make-int16", 2, 0, 1)
163 PUSH (SCM_I_MAKINUM ((signed short) (h
<< 8) + l
));
167 VM_DEFINE_INSTRUCTION (make_char8
, "make-char8", 1, 0, 1)
169 PUSH (SCM_MAKE_CHAR (FETCH ()));
173 VM_DEFINE_INSTRUCTION (list
, "list", 2, -1, 1)
175 unsigned h
= FETCH ();
176 unsigned l
= FETCH ();
177 unsigned len
= ((h
<< 8) + l
);
182 VM_DEFINE_INSTRUCTION (vector
, "vector", 2, -1, 1)
184 unsigned h
= FETCH ();
185 unsigned l
= FETCH ();
186 unsigned len
= ((h
<< 8) + l
);
189 *sp
= scm_vector (*sp
);
193 VM_DEFINE_INSTRUCTION (list_mark
, "list-mark", 0, 0, 0)
199 VM_DEFINE_INSTRUCTION (cons_mark
, "cons-mark", 0, 0, 0)
205 VM_DEFINE_INSTRUCTION (vector_mark
, "vector-mark", 0, 0, 0)
209 *sp
= scm_vector (*sp
);
213 VM_DEFINE_INSTRUCTION (list_break
, "list-break", 0, 0, 0)
217 for (; !SCM_NULLP (l
); l
= SCM_CDR (l
))
227 #define OBJECT_REF(i) objects[i]
228 #define OBJECT_SET(i,o) objects[i] = o
230 #define LOCAL_REF(i) SCM_FRAME_VARIABLE (fp, i)
231 #define LOCAL_SET(i,o) SCM_FRAME_VARIABLE (fp, i) = o
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)
242 VM_DEFINE_INSTRUCTION (object_ref
, "object-ref", 1, 0, 1)
244 register unsigned objnum
= FETCH ();
245 CHECK_OBJECT (objnum
);
246 PUSH (OBJECT_REF (objnum
));
250 VM_DEFINE_INSTRUCTION (local_ref
, "local-ref", 1, 0, 1)
252 PUSH (LOCAL_REF (FETCH ()));
256 VM_DEFINE_INSTRUCTION (external_ref
, "external-ref", 1, 0, 1)
260 for (i
= FETCH (); i
; i
--)
270 VM_DEFINE_INSTRUCTION (variable_ref
, "variable-ref", 0, 0, 1)
274 if (!VARIABLE_BOUNDP (x
))
276 err_args
= SCM_LIST1 (x
);
277 /* Was: err_args = SCM_LIST1 (SCM_CAR (x)); */
278 goto vm_error_unbound
;
282 SCM o
= VARIABLE_REF (x
);
289 VM_DEFINE_INSTRUCTION (late_variable_ref
, "late-variable-ref", 1, 0, 1)
291 unsigned objnum
= FETCH ();
293 CHECK_OBJECT (objnum
);
294 what
= OBJECT_REF (objnum
);
296 if (!SCM_VARIABLEP (what
))
299 if (SCM_LIKELY (SCM_SYMBOLP (what
)))
301 if (SCM_LIKELY (scm_module_system_booted_p
302 && scm_is_true (bp
->module
)))
304 what
= scm_module_lookup (bp
->module
, what
);
306 what
= scm_sym2var (what
, SCM_BOOL_F
, SCM_BOOL_F
);
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 @@
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
))
320 err_args
= SCM_LIST1 (mod
);
321 goto vm_error_no_such_module
;
324 what
= scm_module_lookup (mod
, SCM_CADR (what
));
327 if (!VARIABLE_BOUNDP (what
))
329 err_args
= SCM_LIST1 (what
);
330 goto vm_error_unbound
;
333 OBJECT_SET (objnum
, what
);
336 PUSH (VARIABLE_REF (what
));
342 VM_DEFINE_INSTRUCTION (local_set
, "local-set", 1, 1, 0)
344 LOCAL_SET (FETCH (), *sp
);
349 VM_DEFINE_INSTRUCTION (external_set
, "external-set", 1, 1, 0)
353 for (i
= FETCH (); i
; i
--)
364 VM_DEFINE_INSTRUCTION (variable_set
, "variable-set", 0, 1, 0)
366 VARIABLE_SET (sp
[0], sp
[-1]);
371 VM_DEFINE_INSTRUCTION (late_variable_set
, "late-variable-set", 1, 1, 0)
373 unsigned objnum
= FETCH ();
375 CHECK_OBJECT (objnum
);
376 what
= OBJECT_REF (objnum
);
378 if (!SCM_VARIABLEP (what
))
381 if (SCM_LIKELY (SCM_SYMBOLP (what
)))
383 if (SCM_LIKELY (scm_module_system_booted_p
384 && scm_is_true (bp
->module
)))
386 what
= scm_module_lookup (bp
->module
, what
);
388 what
= scm_sym2var (what
, SCM_BOOL_F
, SCM_BOOL_F
);
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 @@
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
))
402 err_args
= SCM_LIST1 (what
);
403 goto vm_error_no_such_module
;
406 what
= scm_module_lookup (mod
, SCM_CADR (what
));
409 OBJECT_SET (objnum
, what
);
412 VARIABLE_SET (what
, *sp
);
422 /* offset must be a signed short!!! */
423 #define FETCH_OFFSET(offset) \
427 offset = (h << 8) + l; \
432 signed short offset; \
433 FETCH_OFFSET (offset); \
441 VM_DEFINE_INSTRUCTION (br
, "br", 2, 0, 0)
445 ip
+= (signed short) (h
<< 8) + l
;
449 VM_DEFINE_INSTRUCTION (br_if
, "br-if", 2, 0, 0)
451 BR (!SCM_FALSEP (*sp
));
454 VM_DEFINE_INSTRUCTION (br_if_not
, "br-if-not", 2, 0, 0)
456 BR (SCM_FALSEP (*sp
));
459 VM_DEFINE_INSTRUCTION (br_if_eq
, "br-if-eq", 2, 0, 0)
461 BR (SCM_EQ_P (sp
[0], sp
--[1]));
464 VM_DEFINE_INSTRUCTION (br_if_not_eq
, "br-if-not-eq", 2, 0, 0)
466 BR (!SCM_EQ_P (sp
[0], sp
--[1]));
469 VM_DEFINE_INSTRUCTION (br_if_null
, "br-if-null", 2, 0, 0)
471 BR (SCM_NULLP (*sp
));
474 VM_DEFINE_INSTRUCTION (br_if_not_null
, "br-if-not-null", 2, 0, 0)
476 BR (!SCM_NULLP (*sp
));
484 VM_DEFINE_INSTRUCTION (make_closure
, "make-closure", 0, 1, 1)
487 *sp
= scm_c_make_closure (*sp
, external
);
491 VM_DEFINE_INSTRUCTION (call
, "call", 1, -1, 1)
502 if (SCM_PROGRAM_P (x
))
512 #ifdef ENABLE_TRAMPOLINE
513 /* Seems to slow down the fibo test, dunno why */
521 scm_t_trampoline_0 call
= scm_trampoline_0 (x
);
532 scm_t_trampoline_1 call
= scm_trampoline_1 (x
);
538 *sp
= call (x
, arg1
);
545 scm_t_trampoline_2 call
= scm_trampoline_2 (x
);
552 *sp
= call (x
, arg1
, arg2
);
560 * Other interpreted or compiled call
562 if (!SCM_FALSEP (scm_procedure_p (x
)))
564 /* At this point, the stack contains the procedure and each one of its
568 /* keep args on stack so they are marked */
569 sp
[-1] = scm_apply (x
, sp
[0], SCM_EOL
);
570 NULLSTACK_FOR_NONLOCAL_EXIT ();
571 /* FIXME what if SCM_VALUESP(*sp) */
578 if (SCM_VM_CONT_P (x
))
581 vm_call_continuation
:
582 /* Check the number of arguments */
583 /* FIXME multiple args */
585 scm_wrong_num_args (program
);
587 /* Reinstate the continuation */
589 reinstate_vm_cont (vp
, program
);
591 program
= SCM_FRAME_PROGRAM (fp
);
597 goto vm_error_wrong_type_apply
;
600 VM_DEFINE_INSTRUCTION (goto_args
, "goto/args", 1, -1, 1)
608 SCM_TICK
; /* allow interrupt here */
611 * Tail recursive call
613 if (SCM_EQ_P (x
, program
))
620 for (i
= 0; i
< bp
->nargs
; i
++)
621 LOCAL_SET (i
, sp
[i
]);
623 /* Drop the first argument and the program itself. */
625 NULLSTACK (bp
->nargs
+ 1)
634 * Tail call, but not to self -- reuse the frame, keeping the ra and dl
636 if (SCM_PROGRAM_P (x
))
638 SCM
*data
, *tail_args
, *dl
;
640 scm_byte_t
*ra
, *mvra
;
641 #ifdef VM_ENABLE_STACK_NULLING
648 tail_args
= stack_base
+ 2;
649 ra
= SCM_FRAME_RETURN_ADDRESS (fp
);
650 mvra
= SCM_FRAME_MV_RETURN_ADDRESS (fp
);
651 dl
= SCM_FRAME_DYNAMIC_LINK (fp
);
653 /* switch programs */
657 /* delay updating the frame so that if INIT_ARGS has to cons up a rest
658 arg, going into GC, the stack still makes sense */
662 #ifdef VM_ENABLE_STACK_NULLING
667 /* new registers -- logically this would be better later, but let's make
668 sure we have space for the locals now */
669 data
= SCM_FRAME_DATA_ADDRESS (fp
);
671 stack_base
= data
+ 4;
675 /* copy args, bottom-up */
676 for (i
= 0; i
< nargs
; i
++)
677 fp
[i
] = tail_args
[i
];
679 NULLSTACK (old_sp
- sp
);
682 for (i
= bp
->nlocs
; i
; i
--)
683 data
[-i
] = SCM_UNDEFINED
;
689 data
[1] = SCM_BOOL_F
;
691 /* Postpone initializing external vars, because if the CONS causes a GC,
692 we want the stack marker to see the data array formatted as expected. */
693 data
[0] = SCM_UNDEFINED
;
694 external
= bp
->external
;
695 for (i
= 0; i
< bp
->nexts
; i
++)
696 CONS (external
, SCM_UNDEFINED
, external
);
703 #ifdef ENABLE_TRAMPOLINE
704 /* This seems to actually slow down the fibo test -- dunno why */
712 scm_t_trampoline_0 call
= scm_trampoline_0 (x
);
723 scm_t_trampoline_1 call
= scm_trampoline_1 (x
);
729 *sp
= call (x
, arg1
);
736 scm_t_trampoline_2 call
= scm_trampoline_2 (x
);
743 *sp
= call (x
, arg1
, arg2
);
752 * Other interpreted or compiled call
754 if (!SCM_FALSEP (scm_procedure_p (x
)))
758 sp
[-1] = scm_apply (x
, sp
[0], SCM_EOL
);
759 NULLSTACK_FOR_NONLOCAL_EXIT ();
761 /* FIXME what if SCM_VALUESP(*sp) */
770 if (SCM_VM_CONT_P (program
))
771 goto vm_call_continuation
;
773 goto vm_error_wrong_type_apply
;
776 VM_DEFINE_INSTRUCTION (goto_nargs
, "goto/nargs", 0, 0, 1)
780 nargs
= scm_to_int (x
);
781 /* FIXME: should truncate values? */
785 VM_DEFINE_INSTRUCTION (call_nargs
, "call/nargs", 0, 0, 1)
789 nargs
= scm_to_int (x
);
790 /* FIXME: should truncate values? */
794 VM_DEFINE_INSTRUCTION (mv_call
, "mv-call", 3, -1, 1)
800 FETCH_OFFSET (offset
);
807 if (SCM_PROGRAM_P (x
))
813 SCM_FRAME_DATA_ADDRESS (fp
)[3] = (SCM
)(SCM_FRAME_RETURN_ADDRESS (fp
) + offset
);
819 * Other interpreted or compiled call
821 if (!SCM_FALSEP (scm_procedure_p (x
)))
823 /* At this point, the stack contains the procedure and each one of its
827 sp
[-1] = scm_apply (x
, sp
[0], SCM_EOL
);
828 NULLSTACK_FOR_NONLOCAL_EXIT ();
830 if (SCM_VALUESP (*sp
))
834 values
= scm_struct_ref (values
, SCM_INUM0
);
835 len
= scm_length (values
);
836 for (; !SCM_NULLP (values
); values
= SCM_CDR (values
))
837 PUSH (SCM_CAR (values
));
846 if (SCM_VM_CONT_P (x
))
849 goto vm_call_continuation
;
853 goto vm_error_wrong_type_apply
;
856 VM_DEFINE_INSTRUCTION (apply
, "apply", 1, -1, 1)
865 len
= scm_ilength (ls
);
867 goto vm_error_wrong_type_arg
;
869 for (; !SCM_NULLP (ls
); ls
= SCM_CDR (ls
))
876 VM_DEFINE_INSTRUCTION (goto_apply
, "goto/apply", 1, -1, 1)
885 len
= scm_ilength (ls
);
887 goto vm_error_wrong_type_arg
;
889 for (; !SCM_NULLP (ls
); ls
= SCM_CDR (ls
))
896 VM_DEFINE_INSTRUCTION (call_cc
, "call/cc", 0, 1, 1)
902 cont
= scm_make_continuation (&first
);
910 ASSERT (sp
== vp
->sp
);
911 ASSERT (fp
== vp
->fp
);
912 else if (SCM_VALUESP (cont
))
914 /* multiple values returned to continuation */
916 values
= scm_struct_ref (cont
, SCM_INUM0
);
917 if (SCM_NULLP (values
))
918 goto vm_error_no_values
;
919 /* non-tail context does not accept multiple values? */
920 PUSH (SCM_CAR (values
));
930 VM_DEFINE_INSTRUCTION (goto_cc
, "goto/cc", 0, 1, 1)
936 cont
= scm_make_continuation (&first
);
937 ASSERT (sp
== vp
->sp
);
938 ASSERT (fp
== vp
->fp
);
946 else if (SCM_VALUESP (cont
))
948 /* multiple values returned to continuation */
950 values
= scm_struct_ref (cont
, SCM_INUM0
);
951 nvalues
= scm_ilength (values
);
952 for (; !SCM_NULLP (values
); values
= SCM_CDR (values
))
953 PUSH (SCM_CAR (values
));
954 goto vm_return_values
;
963 VM_DEFINE_INSTRUCTION (return, "return", 0, 0, 1)
970 data
= SCM_FRAME_DATA_ADDRESS (fp
);
973 ASSERT (sp
== stack_base
);
974 ASSERT (stack_base
== data
+ 4);
976 /* Restore registers */
977 sp
= SCM_FRAME_LOWER_ADDRESS (fp
);
978 ip
= SCM_FRAME_BYTE_CAST (data
[4]);
979 fp
= SCM_FRAME_STACK_CAST (data
[2]);
981 #ifdef VM_ENABLE_STACK_NULLING
982 int nullcount
= stack_base
- sp
;
984 stack_base
= SCM_FRAME_UPPER_ADDRESS (fp
) - 1;
985 NULLSTACK (nullcount
);
988 /* Set return value (sp is already pushed) */
992 /* Restore the last program */
993 program
= SCM_FRAME_PROGRAM (fp
);
1000 VM_DEFINE_INSTRUCTION (return_values
, "return/values", 1, -1, -1)
1002 /* nvalues declared at top level, because for some reason gcc seems to think
1003 that perhaps it might be used without declaration. Fooey to that, I say. */
1011 data
= SCM_FRAME_DATA_ADDRESS (fp
);
1012 ASSERT (stack_base
== data
+ 4);
1014 /* data[3] is the mv return address */
1015 if (nvalues
!= 1 && data
[3])
1018 /* Restore registers */
1019 sp
= SCM_FRAME_LOWER_ADDRESS (fp
) - 1;
1020 ip
= SCM_FRAME_BYTE_CAST (data
[3]); /* multiple value ra */
1021 fp
= SCM_FRAME_STACK_CAST (data
[2]);
1023 /* Push return values, and the number of values */
1024 for (i
= 0; i
< nvalues
; i
++)
1025 *++sp
= stack_base
[1+i
];
1026 *++sp
= SCM_I_MAKINUM (nvalues
);
1028 /* Finally set new stack_base */
1029 NULLSTACK (stack_base
- sp
+ nvalues
+ 1);
1030 stack_base
= SCM_FRAME_UPPER_ADDRESS (fp
) - 1;
1032 else if (nvalues
>= 1)
1034 /* Multiple values for a single-valued continuation -- here's where I
1035 break with guile tradition and try and do something sensible. (Also,
1036 this block handles the single-valued return to an mv
1038 /* Restore registers */
1039 sp
= SCM_FRAME_LOWER_ADDRESS (fp
) - 1;
1040 ip
= SCM_FRAME_BYTE_CAST (data
[4]); /* single value ra */
1041 fp
= SCM_FRAME_STACK_CAST (data
[2]);
1043 /* Push first value */
1044 *++sp
= stack_base
[1];
1046 /* Finally set new stack_base */
1047 NULLSTACK (stack_base
- sp
);
1048 stack_base
= SCM_FRAME_UPPER_ADDRESS (fp
) - 1;
1051 goto vm_error_no_values
;
1053 /* Restore the last program */
1054 program
= SCM_FRAME_PROGRAM (fp
);
1061 VM_DEFINE_INSTRUCTION (return_values_star
, "return/values*", 1, -1, -1)
1066 ASSERT (nvalues
>= 1);
1070 while (SCM_CONSP (l
))
1077 goto vm_return_values
;
1080 VM_DEFINE_INSTRUCTION (truncate_values
, "truncate-values", 2, -1, -1)
1085 nvalues
= scm_to_int (x
);
1092 if (nvalues
< nbinds
)
1093 goto vm_error_not_enough_values
;
1096 POP_LIST (nvalues
- nbinds
);
1098 DROPN (nvalues
- nbinds
);