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 PUSH_LIST (l
, SCM_NULLP
);
226 #define OBJECT_REF(i) objects[i]
227 #define OBJECT_SET(i,o) objects[i] = o
229 #define LOCAL_REF(i) SCM_FRAME_VARIABLE (fp, i)
230 #define LOCAL_SET(i,o) SCM_FRAME_VARIABLE (fp, i) = o
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)
241 VM_DEFINE_INSTRUCTION (object_ref
, "object-ref", 1, 0, 1)
243 register unsigned objnum
= FETCH ();
244 CHECK_OBJECT (objnum
);
245 PUSH (OBJECT_REF (objnum
));
249 VM_DEFINE_INSTRUCTION (local_ref
, "local-ref", 1, 0, 1)
251 PUSH (LOCAL_REF (FETCH ()));
255 VM_DEFINE_INSTRUCTION (external_ref
, "external-ref", 1, 0, 1)
259 for (i
= FETCH (); i
; i
--)
269 VM_DEFINE_INSTRUCTION (variable_ref
, "variable-ref", 0, 0, 1)
273 if (!VARIABLE_BOUNDP (x
))
275 err_args
= SCM_LIST1 (x
);
276 /* Was: err_args = SCM_LIST1 (SCM_CAR (x)); */
277 goto vm_error_unbound
;
281 SCM o
= VARIABLE_REF (x
);
288 VM_DEFINE_INSTRUCTION (toplevel_ref
, "toplevel-ref", 1, 0, 1)
290 unsigned objnum
= FETCH ();
292 CHECK_OBJECT (objnum
);
293 what
= OBJECT_REF (objnum
);
295 if (!SCM_VARIABLEP (what
))
298 if (SCM_LIKELY (SCM_SYMBOLP (what
)))
301 if (SCM_LIKELY (scm_module_system_booted_p
302 && scm_is_true ((mod
= scm_program_module (program
)))))
304 what
= scm_module_lookup (mod
, 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 (toplevel_set
, "toplevel-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
)))
384 if (SCM_LIKELY (scm_module_system_booted_p
385 && scm_is_true ((mod
= scm_program_module (program
)))))
387 what
= scm_module_lookup (mod
, what
);
389 what
= scm_sym2var (what
, SCM_BOOL_F
, SCM_BOOL_F
);
394 /* compilation of @ or @@
395 `what' is a three-element list: (MODNAME SYM INTERFACE?)
396 INTERFACE? is #t if we compiled @ or #f if we compiled @@
398 mod
= scm_resolve_module (SCM_CAR (what
));
399 if (scm_is_true (SCM_CADDR (what
)))
400 mod
= scm_module_public_interface (mod
);
401 if (SCM_FALSEP (mod
))
403 err_args
= SCM_LIST1 (what
);
404 goto vm_error_no_such_module
;
407 what
= scm_module_lookup (mod
, SCM_CADR (what
));
410 OBJECT_SET (objnum
, what
);
413 VARIABLE_SET (what
, *sp
);
418 VM_DEFINE_INSTRUCTION (externals
, "externals", 0, 0, 1)
429 /* offset must be a signed short!!! */
430 #define FETCH_OFFSET(offset) \
434 offset = (h << 8) + l; \
439 signed short offset; \
440 FETCH_OFFSET (offset); \
448 VM_DEFINE_INSTRUCTION (br
, "br", 2, 0, 0)
452 ip
+= (signed short) (h
<< 8) + l
;
456 VM_DEFINE_INSTRUCTION (br_if
, "br-if", 2, 0, 0)
458 BR (!SCM_FALSEP (*sp
));
461 VM_DEFINE_INSTRUCTION (br_if_not
, "br-if-not", 2, 0, 0)
463 BR (SCM_FALSEP (*sp
));
466 VM_DEFINE_INSTRUCTION (br_if_eq
, "br-if-eq", 2, 0, 0)
468 BR (SCM_EQ_P (sp
[0], sp
--[1]));
471 VM_DEFINE_INSTRUCTION (br_if_not_eq
, "br-if-not-eq", 2, 0, 0)
473 BR (!SCM_EQ_P (sp
[0], sp
--[1]));
476 VM_DEFINE_INSTRUCTION (br_if_null
, "br-if-null", 2, 0, 0)
478 BR (SCM_NULLP (*sp
));
481 VM_DEFINE_INSTRUCTION (br_if_not_null
, "br-if-not-null", 2, 0, 0)
483 BR (!SCM_NULLP (*sp
));
491 VM_DEFINE_INSTRUCTION (make_closure
, "make-closure", 0, 1, 1)
494 *sp
= scm_c_make_closure (*sp
, external
);
498 VM_DEFINE_INSTRUCTION (call
, "call", 1, -1, 1)
509 if (SCM_PROGRAM_P (x
))
519 #ifdef ENABLE_TRAMPOLINE
520 /* Seems to slow down the fibo test, dunno why */
528 scm_t_trampoline_0 call
= scm_trampoline_0 (x
);
539 scm_t_trampoline_1 call
= scm_trampoline_1 (x
);
545 *sp
= call (x
, arg1
);
552 scm_t_trampoline_2 call
= scm_trampoline_2 (x
);
559 *sp
= call (x
, arg1
, arg2
);
567 * Other interpreted or compiled call
569 if (!SCM_FALSEP (scm_procedure_p (x
)))
571 /* At this point, the stack contains the procedure and each one of its
575 /* keep args on stack so they are marked */
576 sp
[-1] = scm_apply (x
, sp
[0], SCM_EOL
);
577 NULLSTACK_FOR_NONLOCAL_EXIT ();
579 if (SCM_UNLIKELY (SCM_VALUESP (*sp
)))
581 /* truncate values */
584 values
= scm_struct_ref (values
, SCM_INUM0
);
585 if (scm_is_null (values
))
586 goto vm_error_not_enough_values
;
587 PUSH (SCM_CAR (values
));
594 if (SCM_VM_CONT_P (x
))
597 vm_call_continuation
:
598 /* Check the number of arguments */
599 /* FIXME multiple args */
601 scm_wrong_num_args (program
);
603 /* Reinstate the continuation */
605 reinstate_vm_cont (vp
, program
);
607 program
= SCM_FRAME_PROGRAM (fp
);
613 goto vm_error_wrong_type_apply
;
616 VM_DEFINE_INSTRUCTION (goto_args
, "goto/args", 1, -1, 1)
624 SCM_TICK
; /* allow interrupt here */
627 * Tail recursive call
629 if (SCM_EQ_P (x
, program
))
636 for (i
= 0; i
< bp
->nargs
; i
++)
637 LOCAL_SET (i
, sp
[i
]);
639 /* Drop the first argument and the program itself. */
641 NULLSTACK (bp
->nargs
+ 1);
643 /* Freshen the externals */
644 external
= bp
->external
;
645 for (i
= 0; i
< bp
->nexts
; i
++)
646 CONS (external
, SCM_UNDEFINED
, external
);
647 SCM_FRAME_DATA_ADDRESS (fp
)[0] = external
;
656 * Tail call, but not to self -- reuse the frame, keeping the ra and dl
658 if (SCM_PROGRAM_P (x
))
660 SCM
*data
, *tail_args
, *dl
;
662 scm_byte_t
*ra
, *mvra
;
663 #ifdef VM_ENABLE_STACK_NULLING
670 tail_args
= stack_base
+ 2;
671 ra
= SCM_FRAME_RETURN_ADDRESS (fp
);
672 mvra
= SCM_FRAME_MV_RETURN_ADDRESS (fp
);
673 dl
= SCM_FRAME_DYNAMIC_LINK (fp
);
675 /* switch programs */
679 /* delay updating the frame so that if INIT_ARGS has to cons up a rest
680 arg, going into GC, the stack still makes sense */
684 #ifdef VM_ENABLE_STACK_NULLING
689 /* new registers -- logically this would be better later, but let's make
690 sure we have space for the locals now */
691 data
= SCM_FRAME_DATA_ADDRESS (fp
);
693 stack_base
= data
+ 3;
697 /* copy args, bottom-up */
698 for (i
= 0; i
< nargs
; i
++)
699 fp
[i
] = tail_args
[i
];
701 NULLSTACK (old_sp
- sp
);
704 for (i
= bp
->nlocs
; i
; i
--)
705 data
[-i
] = SCM_UNDEFINED
;
712 /* Postpone initializing external vars, because if the CONS causes a GC,
713 we want the stack marker to see the data array formatted as expected. */
714 data
[0] = SCM_UNDEFINED
;
715 external
= bp
->external
;
716 for (i
= 0; i
< bp
->nexts
; i
++)
717 CONS (external
, SCM_UNDEFINED
, external
);
724 #ifdef ENABLE_TRAMPOLINE
725 /* This seems to actually slow down the fibo test -- dunno why */
733 scm_t_trampoline_0 call
= scm_trampoline_0 (x
);
744 scm_t_trampoline_1 call
= scm_trampoline_1 (x
);
750 *sp
= call (x
, arg1
);
757 scm_t_trampoline_2 call
= scm_trampoline_2 (x
);
764 *sp
= call (x
, arg1
, arg2
);
773 * Other interpreted or compiled call
775 if (!SCM_FALSEP (scm_procedure_p (x
)))
779 sp
[-1] = scm_apply (x
, sp
[0], SCM_EOL
);
780 NULLSTACK_FOR_NONLOCAL_EXIT ();
782 if (SCM_UNLIKELY (SCM_VALUESP (*sp
)))
784 /* multiple values returned to continuation */
787 values
= scm_struct_ref (values
, SCM_INUM0
);
788 nvalues
= scm_ilength (values
);
789 PUSH_LIST (values
, SCM_NULLP
);
790 goto vm_return_values
;
800 if (SCM_VM_CONT_P (program
))
801 goto vm_call_continuation
;
803 goto vm_error_wrong_type_apply
;
806 VM_DEFINE_INSTRUCTION (goto_nargs
, "goto/nargs", 0, 0, 1)
810 nargs
= scm_to_int (x
);
811 /* FIXME: should truncate values? */
815 VM_DEFINE_INSTRUCTION (call_nargs
, "call/nargs", 0, 0, 1)
819 nargs
= scm_to_int (x
);
820 /* FIXME: should truncate values? */
824 VM_DEFINE_INSTRUCTION (mv_call
, "mv-call", 3, -1, 1)
830 FETCH_OFFSET (offset
);
837 if (SCM_PROGRAM_P (x
))
843 SCM_FRAME_DATA_ADDRESS (fp
)[2] = (SCM
)(SCM_FRAME_RETURN_ADDRESS (fp
) + offset
);
849 * Other interpreted or compiled call
851 if (!SCM_FALSEP (scm_procedure_p (x
)))
853 /* At this point, the stack contains the procedure and each one of its
857 sp
[-1] = scm_apply (x
, sp
[0], SCM_EOL
);
858 NULLSTACK_FOR_NONLOCAL_EXIT ();
860 if (SCM_VALUESP (*sp
))
864 values
= scm_struct_ref (values
, SCM_INUM0
);
865 len
= scm_length (values
);
866 PUSH_LIST (values
, SCM_NULLP
);
875 if (SCM_VM_CONT_P (x
))
878 goto vm_call_continuation
;
882 goto vm_error_wrong_type_apply
;
885 VM_DEFINE_INSTRUCTION (apply
, "apply", 1, -1, 1)
894 len
= scm_ilength (ls
);
896 goto vm_error_wrong_type_arg
;
898 PUSH_LIST (ls
, SCM_NULL_OR_NIL_P
);
904 VM_DEFINE_INSTRUCTION (goto_apply
, "goto/apply", 1, -1, 1)
913 len
= scm_ilength (ls
);
915 goto vm_error_wrong_type_arg
;
917 PUSH_LIST (ls
, SCM_NULL_OR_NIL_P
);
923 VM_DEFINE_INSTRUCTION (call_cc
, "call/cc", 0, 1, 1)
929 cont
= scm_make_continuation (&first
);
937 ASSERT (sp
== vp
->sp
);
938 ASSERT (fp
== vp
->fp
);
939 else if (SCM_VALUESP (cont
))
941 /* multiple values returned to continuation */
943 values
= scm_struct_ref (cont
, SCM_INUM0
);
944 if (SCM_NULLP (values
))
945 goto vm_error_no_values
;
946 /* non-tail context does not accept multiple values? */
947 PUSH (SCM_CAR (values
));
957 VM_DEFINE_INSTRUCTION (goto_cc
, "goto/cc", 0, 1, 1)
963 cont
= scm_make_continuation (&first
);
964 ASSERT (sp
== vp
->sp
);
965 ASSERT (fp
== vp
->fp
);
973 else if (SCM_VALUESP (cont
))
975 /* multiple values returned to continuation */
977 values
= scm_struct_ref (cont
, SCM_INUM0
);
978 nvalues
= scm_ilength (values
);
979 PUSH_LIST (values
, SCM_NULLP
);
980 goto vm_return_values
;
989 VM_DEFINE_INSTRUCTION (return, "return", 0, 0, 1)
996 data
= SCM_FRAME_DATA_ADDRESS (fp
);
999 ASSERT (sp
== stack_base
);
1000 ASSERT (stack_base
== data
+ 3);
1002 /* Restore registers */
1003 sp
= SCM_FRAME_LOWER_ADDRESS (fp
);
1004 ip
= SCM_FRAME_BYTE_CAST (data
[3]);
1005 fp
= SCM_FRAME_STACK_CAST (data
[1]);
1007 #ifdef VM_ENABLE_STACK_NULLING
1008 int nullcount
= stack_base
- sp
;
1010 stack_base
= SCM_FRAME_UPPER_ADDRESS (fp
) - 1;
1011 NULLSTACK (nullcount
);
1014 /* Set return value (sp is already pushed) */
1018 /* Restore the last program */
1019 program
= SCM_FRAME_PROGRAM (fp
);
1026 VM_DEFINE_INSTRUCTION (return_values
, "return/values", 1, -1, -1)
1028 /* nvalues declared at top level, because for some reason gcc seems to think
1029 that perhaps it might be used without declaration. Fooey to that, I say. */
1037 data
= SCM_FRAME_DATA_ADDRESS (fp
);
1038 ASSERT (stack_base
== data
+ 3);
1040 /* data[2] is the mv return address */
1041 if (nvalues
!= 1 && data
[2])
1044 /* Restore registers */
1045 sp
= SCM_FRAME_LOWER_ADDRESS (fp
) - 1;
1046 ip
= SCM_FRAME_BYTE_CAST (data
[2]); /* multiple value ra */
1047 fp
= SCM_FRAME_STACK_CAST (data
[1]);
1049 /* Push return values, and the number of values */
1050 for (i
= 0; i
< nvalues
; i
++)
1051 *++sp
= stack_base
[1+i
];
1052 *++sp
= SCM_I_MAKINUM (nvalues
);
1054 /* Finally set new stack_base */
1055 NULLSTACK (stack_base
- sp
+ nvalues
+ 1);
1056 stack_base
= SCM_FRAME_UPPER_ADDRESS (fp
) - 1;
1058 else if (nvalues
>= 1)
1060 /* Multiple values for a single-valued continuation -- here's where I
1061 break with guile tradition and try and do something sensible. (Also,
1062 this block handles the single-valued return to an mv
1064 /* Restore registers */
1065 sp
= SCM_FRAME_LOWER_ADDRESS (fp
) - 1;
1066 ip
= SCM_FRAME_BYTE_CAST (data
[3]); /* single value ra */
1067 fp
= SCM_FRAME_STACK_CAST (data
[1]);
1069 /* Push first value */
1070 *++sp
= stack_base
[1];
1072 /* Finally set new stack_base */
1073 NULLSTACK (stack_base
- sp
+ nvalues
+ 1);
1074 stack_base
= SCM_FRAME_UPPER_ADDRESS (fp
) - 1;
1077 goto vm_error_no_values
;
1079 /* Restore the last program */
1080 program
= SCM_FRAME_PROGRAM (fp
);
1087 VM_DEFINE_INSTRUCTION (return_values_star
, "return/values*", 1, -1, -1)
1092 ASSERT (nvalues
>= 1);
1096 while (SCM_CONSP (l
))
1102 if (SCM_UNLIKELY (!SCM_NULL_OR_NIL_P (l
))) {
1103 err_args
= scm_list_1 (l
);
1104 goto vm_error_improper_list
;
1107 goto vm_return_values
;
1110 VM_DEFINE_INSTRUCTION (truncate_values
, "truncate-values", 2, -1, -1)
1115 nvalues
= scm_to_int (x
);
1122 if (nvalues
< nbinds
)
1123 goto vm_error_not_enough_values
;
1126 POP_LIST (nvalues
- nbinds
);
1128 DROPN (nvalues
- nbinds
);