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
--);
68 ret
= scm_values (ret
);
72 #ifdef THE_GOVERNMENT_IS_AFTER_ME
75 if (stack_base
!= SCM_FRAME_UPPER_ADDRESS (fp
) - 1)
79 /* Restore registers */
80 sp
= SCM_FRAME_LOWER_ADDRESS (fp
) - 1;
82 fp
= SCM_FRAME_DYNAMIC_LINK (fp
);
89 VM_DEFINE_INSTRUCTION (break, "break", 0, 0, 0)
95 VM_DEFINE_INSTRUCTION (drop
, "drop", 0, 0, 0)
101 VM_DEFINE_INSTRUCTION (mark
, "mark", 0, 0, 1)
103 PUSH (SCM_UNDEFINED
);
107 VM_DEFINE_INSTRUCTION (dup
, "dup", 0, 0, 1)
119 VM_DEFINE_INSTRUCTION (void, "void", 0, 0, 1)
121 PUSH (SCM_UNSPECIFIED
);
125 VM_DEFINE_INSTRUCTION (make_true
, "make-true", 0, 0, 1)
131 VM_DEFINE_INSTRUCTION (make_false
, "make-false", 0, 0, 1)
137 VM_DEFINE_INSTRUCTION (make_eol
, "make-eol", 0, 0, 1)
143 VM_DEFINE_INSTRUCTION (make_int8
, "make-int8", 1, 0, 1)
145 PUSH (SCM_I_MAKINUM ((signed char) FETCH ()));
149 VM_DEFINE_INSTRUCTION (make_int8_0
, "make-int8:0", 0, 0, 1)
155 VM_DEFINE_INSTRUCTION (make_int8_1
, "make-int8:1", 0, 0, 1)
157 PUSH (SCM_I_MAKINUM (1));
161 VM_DEFINE_INSTRUCTION (make_int16
, "make-int16", 2, 0, 1)
165 PUSH (SCM_I_MAKINUM ((signed short) (h
<< 8) + l
));
169 VM_DEFINE_INSTRUCTION (make_char8
, "make-char8", 1, 0, 1)
171 PUSH (SCM_MAKE_CHAR (FETCH ()));
175 VM_DEFINE_INSTRUCTION (list
, "list", 2, -1, 1)
177 unsigned h
= FETCH ();
178 unsigned l
= FETCH ();
179 unsigned len
= ((h
<< 8) + l
);
184 VM_DEFINE_INSTRUCTION (vector
, "vector", 2, -1, 1)
186 unsigned h
= FETCH ();
187 unsigned l
= FETCH ();
188 unsigned len
= ((h
<< 8) + l
);
191 *sp
= scm_vector (*sp
);
195 VM_DEFINE_INSTRUCTION (list_mark
, "list-mark", 0, 0, 0)
201 VM_DEFINE_INSTRUCTION (cons_mark
, "cons-mark", 0, 0, 0)
207 VM_DEFINE_INSTRUCTION (vector_mark
, "vector-mark", 0, 0, 0)
211 *sp
= scm_vector (*sp
);
215 VM_DEFINE_INSTRUCTION (list_break
, "list-break", 0, 0, 0)
219 for (; !SCM_NULLP (l
); l
= SCM_CDR (l
))
229 #define OBJECT_REF(i) objects[i]
230 #define OBJECT_SET(i,o) objects[i] = o
232 #define LOCAL_REF(i) SCM_FRAME_VARIABLE (fp, i)
233 #define LOCAL_SET(i,o) SCM_FRAME_VARIABLE (fp, i) = o
235 /* For the variable operations, we _must_ obviously avoid function calls to
236 `scm_variable_ref ()', `scm_variable_bound_p ()' and friends which do
237 nothing more than the corresponding macros. */
238 #define VARIABLE_REF(v) SCM_VARIABLE_REF (v)
239 #define VARIABLE_SET(v,o) SCM_VARIABLE_SET (v, o)
240 #define VARIABLE_BOUNDP(v) (VARIABLE_REF (v) != SCM_UNDEFINED)
244 VM_DEFINE_INSTRUCTION (object_ref
, "object-ref", 1, 0, 1)
246 register unsigned objnum
= FETCH ();
247 CHECK_OBJECT (objnum
);
248 PUSH (OBJECT_REF (objnum
));
252 VM_DEFINE_INSTRUCTION (local_ref
, "local-ref", 1, 0, 1)
254 PUSH (LOCAL_REF (FETCH ()));
258 VM_DEFINE_INSTRUCTION (external_ref
, "external-ref", 1, 0, 1)
262 for (i
= FETCH (); i
; i
--)
272 VM_DEFINE_INSTRUCTION (variable_ref
, "variable-ref", 0, 0, 1)
276 if (!VARIABLE_BOUNDP (x
))
278 err_args
= SCM_LIST1 (x
);
279 /* Was: err_args = SCM_LIST1 (SCM_CAR (x)); */
280 goto vm_error_unbound
;
284 SCM o
= VARIABLE_REF (x
);
291 VM_DEFINE_INSTRUCTION (late_variable_ref
, "late-variable-ref", 1, 0, 1)
293 unsigned objnum
= FETCH ();
295 CHECK_OBJECT (objnum
);
296 what
= OBJECT_REF (objnum
);
298 if (!SCM_VARIABLEP (what
))
301 if (SCM_LIKELY (SCM_SYMBOLP (what
)))
303 if (SCM_LIKELY (scm_module_system_booted_p
304 && scm_is_true (bp
->module
)))
306 what
= scm_module_lookup (bp
->module
, what
);
308 what
= scm_sym2var (what
, SCM_BOOL_F
, SCM_BOOL_F
);
313 /* compilation of @ or @@
314 `what' is a three-element list: (MODNAME SYM INTERFACE?)
315 INTERFACE? is #t if we compiled @ or #f if we compiled @@
317 mod
= scm_resolve_module (SCM_CAR (what
));
318 if (scm_is_true (SCM_CADDR (what
)))
319 mod
= scm_module_public_interface (mod
);
320 if (SCM_FALSEP (mod
))
322 err_args
= SCM_LIST1 (mod
);
323 goto vm_error_no_such_module
;
326 what
= scm_module_lookup (mod
, SCM_CADR (what
));
329 if (!VARIABLE_BOUNDP (what
))
331 err_args
= SCM_LIST1 (what
);
332 goto vm_error_unbound
;
335 OBJECT_SET (objnum
, what
);
338 PUSH (VARIABLE_REF (what
));
344 VM_DEFINE_INSTRUCTION (local_set
, "local-set", 1, 1, 0)
346 LOCAL_SET (FETCH (), *sp
);
351 VM_DEFINE_INSTRUCTION (external_set
, "external-set", 1, 1, 0)
355 for (i
= FETCH (); i
; i
--)
366 VM_DEFINE_INSTRUCTION (variable_set
, "variable-set", 0, 1, 0)
368 VARIABLE_SET (sp
[0], sp
[-1]);
373 VM_DEFINE_INSTRUCTION (late_variable_set
, "late-variable-set", 1, 1, 0)
375 unsigned objnum
= FETCH ();
377 CHECK_OBJECT (objnum
);
378 what
= OBJECT_REF (objnum
);
380 if (!SCM_VARIABLEP (what
))
383 if (SCM_LIKELY (SCM_SYMBOLP (what
)))
385 if (SCM_LIKELY (scm_module_system_booted_p
386 && scm_is_true (bp
->module
)))
388 what
= scm_module_lookup (bp
->module
, what
);
390 what
= scm_sym2var (what
, SCM_BOOL_F
, SCM_BOOL_F
);
395 /* compilation of @ or @@
396 `what' is a three-element list: (MODNAME SYM INTERFACE?)
397 INTERFACE? is #t if we compiled @ or #f if we compiled @@
399 mod
= scm_resolve_module (SCM_CAR (what
));
400 if (scm_is_true (SCM_CADDR (what
)))
401 mod
= scm_module_public_interface (mod
);
402 if (SCM_FALSEP (mod
))
404 err_args
= SCM_LIST1 (what
);
405 goto vm_error_no_such_module
;
408 what
= scm_module_lookup (mod
, SCM_CADR (what
));
411 OBJECT_SET (objnum
, what
);
414 VARIABLE_SET (what
, *sp
);
424 /* offset must be a signed short!!! */
425 #define FETCH_OFFSET(offset) \
429 offset = (h << 8) + l; \
434 signed short offset; \
435 FETCH_OFFSET (offset); \
442 VM_DEFINE_INSTRUCTION (br
, "br", 2, 0, 0)
446 ip
+= (signed short) (h
<< 8) + l
;
450 VM_DEFINE_INSTRUCTION (br_if
, "br-if", 2, 0, 0)
452 BR (!SCM_FALSEP (*sp
));
455 VM_DEFINE_INSTRUCTION (br_if_not
, "br-if-not", 2, 0, 0)
457 BR (SCM_FALSEP (*sp
));
460 VM_DEFINE_INSTRUCTION (br_if_eq
, "br-if-eq", 2, 0, 0)
462 BR (SCM_EQ_P (sp
[0], sp
--[1]));
465 VM_DEFINE_INSTRUCTION (br_if_not_eq
, "br-if-not-eq", 2, 0, 0)
467 BR (!SCM_EQ_P (sp
[0], sp
--[1]));
470 VM_DEFINE_INSTRUCTION (br_if_null
, "br-if-null", 2, 0, 0)
472 BR (SCM_NULLP (*sp
));
475 VM_DEFINE_INSTRUCTION (br_if_not_null
, "br-if-not-null", 2, 0, 0)
477 BR (!SCM_NULLP (*sp
));
485 VM_DEFINE_INSTRUCTION (make_closure
, "make-closure", 0, 1, 1)
488 *sp
= scm_c_make_closure (*sp
, external
);
492 VM_DEFINE_INSTRUCTION (call
, "call", 1, -1, 1)
503 if (SCM_PROGRAM_P (x
))
513 #ifdef ENABLE_TRAMPOLINE
514 /* Seems to slow down the fibo test, dunno why */
522 scm_t_trampoline_0 call
= scm_trampoline_0 (x
);
533 scm_t_trampoline_1 call
= scm_trampoline_1 (x
);
539 *sp
= call (x
, arg1
);
546 scm_t_trampoline_2 call
= scm_trampoline_2 (x
);
553 *sp
= call (x
, arg1
, arg2
);
561 * Other interpreted or compiled call
563 if (!SCM_FALSEP (scm_procedure_p (x
)))
565 /* At this point, the stack contains the procedure and each one of its
569 /* keep args on stack so they are marked */
570 sp
[-1] = scm_apply (x
, sp
[0], SCM_EOL
);
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)
607 SCM_TICK
; /* allow interrupt here */
610 * Tail recursive call
612 if (SCM_EQ_P (x
, program
))
619 for (i
= 0; i
< bp
->nargs
; i
++)
620 LOCAL_SET (i
, sp
[i
]);
622 /* Drop the first argument and the program itself. */
632 * Tail call, but not to self -- reuse the frame, keeping the ra and dl
634 if (SCM_PROGRAM_P (x
))
636 SCM
*data
, *tail_args
, *dl
;
638 scm_byte_t
*ra
, *mvra
;
643 tail_args
= stack_base
+ 2;
644 ra
= SCM_FRAME_RETURN_ADDRESS (fp
);
645 mvra
= SCM_FRAME_MV_RETURN_ADDRESS (fp
);
646 dl
= SCM_FRAME_DYNAMIC_LINK (fp
);
648 /* switch programs */
649 fp
[-1] = program
= x
;
654 /* new registers -- logically this would be better later, but let's make
655 sure we have space for the locals now */
656 data
= SCM_FRAME_DATA_ADDRESS (fp
);
658 stack_base
= data
+ 4;
662 /* copy args, bottom-up */
663 for (i
= 0; i
< nargs
; i
++)
664 fp
[i
] = tail_args
[i
];
667 for (i
= bp
->nlocs
; i
; i
--)
668 data
[-i
] = SCM_UNDEFINED
;
670 /* and the external variables */
671 external
= bp
->external
;
672 for (i
= 0; i
< bp
->nexts
; i
++)
673 CONS (external
, SCM_UNDEFINED
, external
);
679 data
[1] = SCM_BOOL_F
;
685 #ifdef ENABLE_TRAMPOLINE
686 /* This seems to actually slow down the fibo test -- dunno why */
694 scm_t_trampoline_0 call
= scm_trampoline_0 (x
);
705 scm_t_trampoline_1 call
= scm_trampoline_1 (x
);
711 *sp
= call (x
, arg1
);
718 scm_t_trampoline_2 call
= scm_trampoline_2 (x
);
725 *sp
= call (x
, arg1
, arg2
);
734 * Other interpreted or compiled call
736 if (!SCM_FALSEP (scm_procedure_p (x
)))
740 sp
[-1] = scm_apply (x
, sp
[0], SCM_EOL
);
742 /* FIXME what if SCM_VALUESP(*sp) */
751 if (SCM_VM_CONT_P (program
))
752 goto vm_call_continuation
;
754 goto vm_error_wrong_type_apply
;
757 VM_DEFINE_INSTRUCTION (goto_nargs
, "goto/nargs", 0, 0, 1)
761 nargs
= scm_to_int (x
);
762 /* FIXME: should truncate values? */
766 VM_DEFINE_INSTRUCTION (call_nargs
, "call/nargs", 0, 0, 1)
770 nargs
= scm_to_int (x
);
771 /* FIXME: should truncate values? */
775 VM_DEFINE_INSTRUCTION (mv_call
, "mv-call", 3, -1, 1)
781 FETCH_OFFSET (offset
);
788 if (SCM_PROGRAM_P (x
))
794 SCM_FRAME_DATA_ADDRESS (fp
)[3] = (SCM
)(SCM_FRAME_RETURN_ADDRESS (fp
) + offset
);
800 * Other interpreted or compiled call
802 if (!SCM_FALSEP (scm_procedure_p (x
)))
804 /* At this point, the stack contains the procedure and each one of its
808 sp
[-1] = scm_apply (x
, sp
[0], SCM_EOL
);
810 if (SCM_VALUESP (*sp
))
814 values
= scm_struct_ref (values
, SCM_INUM0
);
815 len
= scm_length (values
);
816 for (; !SCM_NULLP (values
); values
= SCM_CDR (values
))
817 PUSH (SCM_CAR (values
));
826 if (SCM_VM_CONT_P (x
))
829 goto vm_call_continuation
;
833 goto vm_error_wrong_type_apply
;
836 VM_DEFINE_INSTRUCTION (apply
, "apply", 1, -1, 1)
845 len
= scm_ilength (ls
);
847 goto vm_error_wrong_type_arg
;
849 for (; !SCM_NULLP (ls
); ls
= SCM_CDR (ls
))
856 VM_DEFINE_INSTRUCTION (goto_apply
, "goto/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 (call_cc
, "call/cc", 0, 1, 1)
882 cont
= scm_make_continuation (&first
);
890 else if (SCM_VALUESP (cont
))
892 /* multiple values returned to continuation */
894 values
= scm_struct_ref (cont
, SCM_INUM0
);
895 if (SCM_NULLP (values
))
896 goto vm_error_no_values
;
897 /* non-tail context does not accept multiple values? */
898 PUSH (SCM_CAR (values
));
908 VM_DEFINE_INSTRUCTION (goto_cc
, "goto/cc", 0, 1, 1)
914 cont
= scm_make_continuation (&first
);
922 else if (SCM_VALUESP (cont
))
924 /* multiple values returned to continuation */
926 values
= scm_struct_ref (cont
, SCM_INUM0
);
927 nvalues
= scm_ilength (values
);
928 for (; !SCM_NULLP (values
); values
= SCM_CDR (values
))
929 PUSH (SCM_CAR (values
));
930 goto vm_return_values
;
939 VM_DEFINE_INSTRUCTION (return, "return", 0, 0, 1)
946 data
= SCM_FRAME_DATA_ADDRESS (fp
);
949 #ifdef THE_GOVERNMENT_IS_AFTER_ME
950 if (sp
!= stack_base
)
952 if (stack_base
!= data
+ 4)
956 /* Restore registers */
957 sp
= SCM_FRAME_LOWER_ADDRESS (fp
);
958 ip
= SCM_FRAME_BYTE_CAST (data
[4]);
959 fp
= SCM_FRAME_STACK_CAST (data
[2]);
960 stack_base
= SCM_FRAME_UPPER_ADDRESS (fp
) - 1;
962 /* Set return value (sp is already pushed) */
966 /* Restore the last program */
967 program
= SCM_FRAME_PROGRAM (fp
);
974 VM_DEFINE_INSTRUCTION (return_values
, "return/values", 1, -1, -1)
976 /* nvalues declared at top level, because for some reason gcc seems to think
977 that perhaps it might be used without declaration. Fooey to that, I say. */
985 data
= SCM_FRAME_DATA_ADDRESS (fp
);
986 #ifdef THE_GOVERNMENT_IS_AFTER_ME
987 if (stack_base
!= data
+ 4)
991 /* data[3] is the mv return address */
992 if (nvalues
!= 1 && data
[3])
995 /* Restore registers */
996 sp
= SCM_FRAME_LOWER_ADDRESS (fp
) - 1;
997 ip
= SCM_FRAME_BYTE_CAST (data
[3]); /* multiple value ra */
998 fp
= SCM_FRAME_STACK_CAST (data
[2]);
1000 /* Push return values, and the number of values */
1001 for (i
= 0; i
< nvalues
; i
++)
1002 *++sp
= stack_base
[1+i
];
1003 *++sp
= SCM_I_MAKINUM (nvalues
);
1005 /* Finally set new stack_base */
1006 stack_base
= SCM_FRAME_UPPER_ADDRESS (fp
) - 1;
1008 else if (nvalues
>= 1)
1010 /* Multiple values for a single-valued continuation -- here's where I
1011 break with guile tradition and try and do something sensible. (Also,
1012 this block handles the single-valued return to an mv
1014 /* Restore registers */
1015 sp
= SCM_FRAME_LOWER_ADDRESS (fp
) - 1;
1016 ip
= SCM_FRAME_BYTE_CAST (data
[4]); /* single value ra */
1017 fp
= SCM_FRAME_STACK_CAST (data
[2]);
1019 /* Push first value */
1020 *++sp
= stack_base
[1];
1022 /* Finally set new stack_base */
1023 stack_base
= SCM_FRAME_UPPER_ADDRESS (fp
) - 1;
1026 goto vm_error_no_values
;
1028 /* Restore the last program */
1029 program
= SCM_FRAME_PROGRAM (fp
);
1036 VM_DEFINE_INSTRUCTION (return_values_star
, "return/values*", 1, -1, -1)
1041 #ifdef THE_GOVERNMENT_IS_AFTER_ME
1048 while (SCM_CONSP (l
))
1055 goto vm_return_values
;
1058 VM_DEFINE_INSTRUCTION (truncate_values
, "truncate-values", 2, -1, -1)
1063 nvalues
= scm_to_int (x
);
1070 if (nvalues
< nbinds
)
1071 goto vm_error_not_enough_values
;
1074 POP_LIST (nvalues
- nbinds
);
1076 DROPN (nvalues
- nbinds
);