1 /* Copyright (C) 2001,2008,2009,2010 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * 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.
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
20 /* This file is included in vm_engine.c */
27 VM_DEFINE_INSTRUCTION (0, nop
, "nop", 0, 0, 0)
32 VM_DEFINE_INSTRUCTION (1, halt
, "halt", 0, 0, 0)
35 nvalues
= SCM_I_INUM (*sp
--);
44 finish_args
= scm_values (finish_args
);
48 #ifdef VM_ENABLE_STACK_NULLING
52 /* Restore registers */
53 sp
= SCM_FRAME_LOWER_ADDRESS (fp
) - 1;
54 /* Setting the ip here doesn't actually affect control flow, as the calling
55 code will restore its own registers, but it does help when walking the
57 ip
= SCM_FRAME_RETURN_ADDRESS (fp
);
58 fp
= SCM_FRAME_DYNAMIC_LINK (fp
);
59 NULLSTACK (old_sp
- sp
);
65 VM_DEFINE_INSTRUCTION (2, break, "break", 0, 0, 0)
71 VM_DEFINE_INSTRUCTION (3, drop
, "drop", 0, 1, 0)
77 VM_DEFINE_INSTRUCTION (4, dup
, "dup", 0, 0, 1)
89 VM_DEFINE_INSTRUCTION (5, void, "void", 0, 0, 1)
91 PUSH (SCM_UNSPECIFIED
);
95 VM_DEFINE_INSTRUCTION (6, make_true
, "make-true", 0, 0, 1)
101 VM_DEFINE_INSTRUCTION (7, make_false
, "make-false", 0, 0, 1)
107 VM_DEFINE_INSTRUCTION (8, make_nil
, "make-nil", 0, 0, 1)
109 PUSH (SCM_ELISP_NIL
);
113 VM_DEFINE_INSTRUCTION (9, make_eol
, "make-eol", 0, 0, 1)
119 VM_DEFINE_INSTRUCTION (10, make_int8
, "make-int8", 1, 0, 1)
121 PUSH (SCM_I_MAKINUM ((signed char) FETCH ()));
125 VM_DEFINE_INSTRUCTION (11, make_int8_0
, "make-int8:0", 0, 0, 1)
131 VM_DEFINE_INSTRUCTION (12, make_int8_1
, "make-int8:1", 0, 0, 1)
133 PUSH (SCM_I_MAKINUM (1));
137 VM_DEFINE_INSTRUCTION (13, make_int16
, "make-int16", 2, 0, 1)
141 PUSH (SCM_I_MAKINUM ((signed short) (h
<< 8) + l
));
145 VM_DEFINE_INSTRUCTION (14, make_int64
, "make-int64", 8, 0, 1)
149 v
<<= 8; v
+= FETCH ();
150 v
<<= 8; v
+= FETCH ();
151 v
<<= 8; v
+= FETCH ();
152 v
<<= 8; v
+= FETCH ();
153 v
<<= 8; v
+= FETCH ();
154 v
<<= 8; v
+= FETCH ();
155 v
<<= 8; v
+= FETCH ();
156 PUSH (scm_from_int64 ((scm_t_int64
) v
));
160 VM_DEFINE_INSTRUCTION (15, make_uint64
, "make-uint64", 8, 0, 1)
164 v
<<= 8; v
+= FETCH ();
165 v
<<= 8; v
+= FETCH ();
166 v
<<= 8; v
+= FETCH ();
167 v
<<= 8; v
+= FETCH ();
168 v
<<= 8; v
+= FETCH ();
169 v
<<= 8; v
+= FETCH ();
170 v
<<= 8; v
+= FETCH ();
171 PUSH (scm_from_uint64 (v
));
175 VM_DEFINE_INSTRUCTION (16, make_char8
, "make-char8", 1, 0, 1)
180 PUSH (SCM_MAKE_CHAR (v
));
181 /* Don't simplify this to PUSH (SCM_MAKE_CHAR (FETCH ())). The
182 contents of SCM_MAKE_CHAR may be evaluated more than once,
183 resulting in a double fetch. */
187 VM_DEFINE_INSTRUCTION (17, make_char32
, "make-char32", 4, 0, 1)
191 v
<<= 8; v
+= FETCH ();
192 v
<<= 8; v
+= FETCH ();
193 v
<<= 8; v
+= FETCH ();
194 PUSH (SCM_MAKE_CHAR (v
));
200 VM_DEFINE_INSTRUCTION (18, list
, "list", 2, -1, 1)
202 unsigned h
= FETCH ();
203 unsigned l
= FETCH ();
204 unsigned len
= ((h
<< 8) + l
);
209 VM_DEFINE_INSTRUCTION (19, vector
, "vector", 2, -1, 1)
211 unsigned h
= FETCH ();
212 unsigned l
= FETCH ();
213 unsigned len
= ((h
<< 8) + l
);
219 vect
= scm_make_vector (scm_from_uint (len
), SCM_BOOL_F
);
220 memcpy (SCM_I_VECTOR_WELTS(vect
), sp
, sizeof(SCM
) * len
);
232 #define OBJECT_REF(i) objects[i]
233 #define OBJECT_SET(i,o) objects[i] = o
235 #define LOCAL_REF(i) SCM_FRAME_VARIABLE (fp, i)
236 #define LOCAL_SET(i,o) SCM_FRAME_VARIABLE (fp, i) = o
238 /* For the variable operations, we _must_ obviously avoid function calls to
239 `scm_variable_ref ()', `scm_variable_bound_p ()' and friends which do
240 nothing more than the corresponding macros. */
241 #define VARIABLE_REF(v) SCM_VARIABLE_REF (v)
242 #define VARIABLE_SET(v,o) SCM_VARIABLE_SET (v, o)
243 #define VARIABLE_BOUNDP(v) (VARIABLE_REF (v) != SCM_UNDEFINED)
245 #define FREE_VARIABLE_REF(i) SCM_PROGRAM_FREE_VARIABLE_REF (program, i)
249 VM_DEFINE_INSTRUCTION (20, object_ref
, "object-ref", 1, 0, 1)
251 register unsigned objnum
= FETCH ();
252 CHECK_OBJECT (objnum
);
253 PUSH (OBJECT_REF (objnum
));
257 /* FIXME: necessary? elt 255 of the vector could be a vector... */
258 VM_DEFINE_INSTRUCTION (21, long_object_ref
, "long-object-ref", 2, 0, 1)
260 unsigned int objnum
= FETCH ();
263 CHECK_OBJECT (objnum
);
264 PUSH (OBJECT_REF (objnum
));
268 VM_DEFINE_INSTRUCTION (22, local_ref
, "local-ref", 1, 0, 1)
270 PUSH (LOCAL_REF (FETCH ()));
275 VM_DEFINE_INSTRUCTION (23, long_local_ref
, "long-local-ref", 2, 0, 1)
277 unsigned int i
= FETCH ();
280 PUSH (LOCAL_REF (i
));
285 VM_DEFINE_INSTRUCTION (24, local_bound
, "local-bound?", 1, 0, 1)
287 if (LOCAL_REF (FETCH ()) == SCM_UNDEFINED
)
294 VM_DEFINE_INSTRUCTION (25, long_local_bound
, "long-local-bound?", 2, 0, 1)
296 unsigned int i
= FETCH ();
299 if (LOCAL_REF (i
) == SCM_UNDEFINED
)
306 VM_DEFINE_INSTRUCTION (26, variable_ref
, "variable-ref", 0, 1, 1)
310 if (SCM_UNLIKELY (!VARIABLE_BOUNDP (x
)))
314 /* Attempt to provide the variable name in the error message. */
315 var_name
= scm_module_reverse_lookup (scm_current_module (), x
);
316 finish_args
= scm_list_1 (scm_is_true (var_name
) ? var_name
: x
);
317 goto vm_error_unbound
;
321 SCM o
= VARIABLE_REF (x
);
328 VM_DEFINE_INSTRUCTION (27, variable_bound
, "variable-bound?", 0, 1, 1)
330 if (VARIABLE_BOUNDP (*sp
))
337 VM_DEFINE_INSTRUCTION (28, toplevel_ref
, "toplevel-ref", 1, 0, 1)
339 unsigned objnum
= FETCH ();
341 CHECK_OBJECT (objnum
);
342 what
= OBJECT_REF (objnum
);
344 if (!SCM_VARIABLEP (what
))
347 what
= resolve_variable (what
, scm_program_module (program
));
348 if (!VARIABLE_BOUNDP (what
))
350 finish_args
= scm_list_1 (what
);
351 goto vm_error_unbound
;
353 OBJECT_SET (objnum
, what
);
356 PUSH (VARIABLE_REF (what
));
360 VM_DEFINE_INSTRUCTION (29, long_toplevel_ref
, "long-toplevel-ref", 2, 0, 1)
363 unsigned int objnum
= FETCH ();
366 CHECK_OBJECT (objnum
);
367 what
= OBJECT_REF (objnum
);
369 if (!SCM_VARIABLEP (what
))
372 what
= resolve_variable (what
, scm_program_module (program
));
373 if (!VARIABLE_BOUNDP (what
))
375 finish_args
= scm_list_1 (what
);
376 goto vm_error_unbound
;
378 OBJECT_SET (objnum
, what
);
381 PUSH (VARIABLE_REF (what
));
387 VM_DEFINE_INSTRUCTION (30, local_set
, "local-set", 1, 1, 0)
389 LOCAL_SET (FETCH (), *sp
);
394 VM_DEFINE_INSTRUCTION (31, long_local_set
, "long-local-set", 2, 1, 0)
396 unsigned int i
= FETCH ();
404 VM_DEFINE_INSTRUCTION (32, variable_set
, "variable-set", 0, 2, 0)
406 VARIABLE_SET (sp
[0], sp
[-1]);
411 VM_DEFINE_INSTRUCTION (33, toplevel_set
, "toplevel-set", 1, 1, 0)
413 unsigned objnum
= FETCH ();
415 CHECK_OBJECT (objnum
);
416 what
= OBJECT_REF (objnum
);
418 if (!SCM_VARIABLEP (what
))
421 what
= resolve_variable (what
, scm_program_module (program
));
422 OBJECT_SET (objnum
, what
);
425 VARIABLE_SET (what
, *sp
);
430 VM_DEFINE_INSTRUCTION (34, long_toplevel_set
, "long-toplevel-set", 2, 1, 0)
433 unsigned int objnum
= FETCH ();
436 CHECK_OBJECT (objnum
);
437 what
= OBJECT_REF (objnum
);
439 if (!SCM_VARIABLEP (what
))
442 what
= resolve_variable (what
, scm_program_module (program
));
443 OBJECT_SET (objnum
, what
);
446 VARIABLE_SET (what
, *sp
);
456 /* offset must be at least 24 bits wide, and signed */
457 #define FETCH_OFFSET(offset) \
459 offset = FETCH () << 16; \
460 offset += FETCH () << 8; \
461 offset += FETCH (); \
462 offset -= (offset & (1<<23)) << 1; \
467 scm_t_int32 offset; \
468 FETCH_OFFSET (offset); \
472 VM_HANDLE_INTERRUPTS; \
478 VM_DEFINE_INSTRUCTION (35, br
, "br", 3, 0, 0)
481 FETCH_OFFSET (offset
);
484 VM_HANDLE_INTERRUPTS
;
488 VM_DEFINE_INSTRUCTION (36, br_if
, "br-if", 3, 0, 0)
490 BR (scm_is_true (*sp
));
493 VM_DEFINE_INSTRUCTION (37, br_if_not
, "br-if-not", 3, 0, 0)
495 BR (scm_is_false (*sp
));
498 VM_DEFINE_INSTRUCTION (38, br_if_eq
, "br-if-eq", 3, 0, 0)
500 sp
--; /* underflow? */
501 BR (scm_is_eq (sp
[0], sp
[1]));
504 VM_DEFINE_INSTRUCTION (39, br_if_not_eq
, "br-if-not-eq", 3, 0, 0)
506 sp
--; /* underflow? */
507 BR (!scm_is_eq (sp
[0], sp
[1]));
510 VM_DEFINE_INSTRUCTION (40, br_if_null
, "br-if-null", 3, 0, 0)
512 BR (scm_is_null (*sp
));
515 VM_DEFINE_INSTRUCTION (41, br_if_not_null
, "br-if-not-null", 3, 0, 0)
517 BR (!scm_is_null (*sp
));
525 VM_DEFINE_INSTRUCTION (42, br_if_nargs_ne
, "br-if-nargs-ne", 5, 0, 0)
531 FETCH_OFFSET (offset
);
532 if (sp
- (fp
- 1) != n
)
537 VM_DEFINE_INSTRUCTION (43, br_if_nargs_lt
, "br-if-nargs-lt", 5, 0, 0)
543 FETCH_OFFSET (offset
);
544 if (sp
- (fp
- 1) < n
)
549 VM_DEFINE_INSTRUCTION (44, br_if_nargs_gt
, "br-if-nargs-gt", 5, 0, 0)
556 FETCH_OFFSET (offset
);
557 if (sp
- (fp
- 1) > n
)
562 VM_DEFINE_INSTRUCTION (45, assert_nargs_ee
, "assert-nargs-ee", 2, 0, 0)
567 if (sp
- (fp
- 1) != n
)
568 goto vm_error_wrong_num_args
;
572 VM_DEFINE_INSTRUCTION (46, assert_nargs_ge
, "assert-nargs-ge", 2, 0, 0)
577 if (sp
- (fp
- 1) < n
)
578 goto vm_error_wrong_num_args
;
582 VM_DEFINE_INSTRUCTION (47, bind_optionals
, "bind-optionals", 2, -1, -1)
587 while (sp
- (fp
- 1) < n
)
588 PUSH (SCM_UNDEFINED
);
592 VM_DEFINE_INSTRUCTION (48, bind_optionals_shuffle
, "bind-optionals/shuffle", 6, -1, -1)
595 scm_t_ptrdiff nreq
, nreq_and_opt
, ntotal
;
596 nreq
= FETCH () << 8;
598 nreq_and_opt
= FETCH () << 8;
599 nreq_and_opt
+= FETCH ();
600 ntotal
= FETCH () << 8;
603 /* look in optionals for first keyword or last positional */
604 /* starting after the last required positional arg */
606 while (/* while we have args */
608 /* and we still have positionals to fill */
609 && walk
- fp
< nreq_and_opt
610 /* and we haven't reached a keyword yet */
611 && !scm_is_keyword (*walk
))
612 /* bind this optional arg (by leaving it in place) */
614 /* now shuffle up, from walk to ntotal */
616 scm_t_ptrdiff nshuf
= sp
- walk
+ 1, i
;
617 sp
= (fp
- 1) + ntotal
+ nshuf
;
619 for (i
= 0; i
< nshuf
; i
++)
620 sp
[-i
] = walk
[nshuf
-i
-1];
622 /* and fill optionals & keyword args with SCM_UNDEFINED */
623 while (walk
<= (fp
- 1) + ntotal
)
624 *walk
++ = SCM_UNDEFINED
;
629 /* Flags that determine whether other keywords are allowed, and whether a
630 rest argument is expected. These values must match those used by the
631 glil->assembly compiler. */
632 #define F_ALLOW_OTHER_KEYS 1
635 VM_DEFINE_INSTRUCTION (49, bind_kwargs
, "bind-kwargs", 5, 0, 0)
639 int kw_and_rest_flags
;
643 /* XXX: We don't actually use NKW. */
646 kw_and_rest_flags
= FETCH ();
648 if (!(kw_and_rest_flags
& F_REST
)
649 && ((sp
- (fp
- 1) - nkw
) % 2))
650 goto vm_error_kwargs_length_not_even
;
653 kw
= OBJECT_REF (idx
);
655 /* Switch NKW to be a negative index below SP. */
656 for (nkw
= -(sp
- (fp
- 1) - nkw
) + 1; nkw
< 0; nkw
++)
660 if (scm_is_keyword (sp
[nkw
]))
662 for (walk
= kw
; scm_is_pair (walk
); walk
= SCM_CDR (walk
))
664 if (scm_is_eq (SCM_CAAR (walk
), sp
[nkw
]))
666 SCM si
= SCM_CDAR (walk
);
667 LOCAL_SET (SCM_I_INUMP (si
) ? SCM_I_INUM (si
) : scm_to_long (si
),
672 if (!(kw_and_rest_flags
& F_ALLOW_OTHER_KEYS
) && !scm_is_pair (walk
))
673 goto vm_error_kwargs_unrecognized_keyword
;
677 else if (!(kw_and_rest_flags
& F_REST
))
678 goto vm_error_kwargs_invalid_keyword
;
684 #undef F_ALLOW_OTHER_KEYS
688 VM_DEFINE_INSTRUCTION (50, push_rest
, "push-rest", 2, -1, -1)
694 while (sp
- (fp
- 1) > n
)
695 /* No need to check for underflow. */
696 CONS (rest
, *sp
--, rest
);
701 VM_DEFINE_INSTRUCTION (51, bind_rest
, "bind-rest", 4, -1, -1)
710 while (sp
- (fp
- 1) > n
)
711 /* No need to check for underflow. */
712 CONS (rest
, *sp
--, rest
);
717 VM_DEFINE_INSTRUCTION (52, reserve_locals
, "reserve-locals", 2, -1, -1)
730 *++old_sp
= SCM_UNDEFINED
;
733 NULLSTACK (old_sp
- sp
);
738 VM_DEFINE_INSTRUCTION (53, new_frame
, "new-frame", 0, 0, 3)
740 /* NB: if you change this, see frames.c:vm-frame-num-locals */
741 /* and frames.h, vm-engine.c, etc of course */
742 PUSH ((SCM
)fp
); /* dynamic link */
748 VM_DEFINE_INSTRUCTION (54, call
, "call", 1, -1, 1)
753 program
= sp
[-nargs
];
755 VM_HANDLE_INTERRUPTS
;
757 if (SCM_UNLIKELY (!SCM_PROGRAM_P (program
)))
759 if (SCM_STRUCTP (program
) && SCM_STRUCT_APPLICABLE_P (program
))
761 sp
[-nargs
] = SCM_STRUCT_PROCEDURE (program
);
764 else if (SCM_NIMP (program
) && SCM_TYP7 (program
) == scm_tc7_smob
765 && SCM_SMOB_APPLICABLE_P (program
))
768 sp
[-nargs
] = scm_i_smob_apply_trampoline (program
);
772 goto vm_error_wrong_type_apply
;
777 ASSERT (SCM_FRAME_RETURN_ADDRESS (fp
) == 0);
778 ASSERT (SCM_FRAME_MV_RETURN_ADDRESS (fp
) == 0);
779 SCM_FRAME_SET_RETURN_ADDRESS (fp
, ip
);
780 SCM_FRAME_SET_MV_RETURN_ADDRESS (fp
, 0);
781 ip
= SCM_C_OBJCODE_BASE (bp
);
787 VM_DEFINE_INSTRUCTION (55, tail_call
, "tail-call", 1, -1, 1)
792 program
= sp
[-nargs
];
794 VM_HANDLE_INTERRUPTS
;
796 if (SCM_UNLIKELY (!SCM_PROGRAM_P (program
)))
798 if (SCM_STRUCTP (program
) && SCM_STRUCT_APPLICABLE_P (program
))
800 sp
[-nargs
] = SCM_STRUCT_PROCEDURE (program
);
803 else if (SCM_NIMP (program
) && SCM_TYP7 (program
) == scm_tc7_smob
804 && SCM_SMOB_APPLICABLE_P (program
))
807 sp
[-nargs
] = scm_i_smob_apply_trampoline (program
);
811 goto vm_error_wrong_type_apply
;
816 #ifdef VM_ENABLE_STACK_NULLING
823 /* switch programs */
825 /* shuffle down the program and the arguments */
826 for (i
= -1, sp
= sp
- nargs
+ 1; i
< nargs
; i
++)
827 SCM_FRAME_STACK_ADDRESS (fp
)[i
] = sp
[i
];
831 NULLSTACK (old_sp
- sp
);
833 ip
= SCM_C_OBJCODE_BASE (bp
);
841 VM_DEFINE_INSTRUCTION (56, subr_call
, "subr-call", 1, -1, -1)
848 subr
= SCM_FOREIGN_POINTER (foreign
, void);
850 VM_HANDLE_INTERRUPTS
;
862 ret
= subr (sp
[-1], sp
[0]);
865 ret
= subr (sp
[-2], sp
[-1], sp
[0]);
868 ret
= subr (sp
[-3], sp
[-2], sp
[-1], sp
[0]);
871 ret
= subr (sp
[-4], sp
[-3], sp
[-2], sp
[-1], sp
[0]);
874 ret
= subr (sp
[-5], sp
[-4], sp
[-3], sp
[-2], sp
[-1], sp
[0]);
877 ret
= subr (sp
[-6], sp
[-5], sp
[-4], sp
[-3], sp
[-2], sp
[-1], sp
[0]);
880 ret
= subr (sp
[-7], sp
[-6], sp
[-5], sp
[-4], sp
[-3], sp
[-2], sp
[-1], sp
[0]);
883 ret
= subr (sp
[-8], sp
[-7], sp
[-6], sp
[-5], sp
[-4], sp
[-3], sp
[-2], sp
[-1], sp
[0]);
886 ret
= subr (sp
[-9], sp
[-8], sp
[-7], sp
[-6], sp
[-5], sp
[-4], sp
[-3], sp
[-2], sp
[-1], sp
[0]);
892 NULLSTACK_FOR_NONLOCAL_EXIT ();
894 if (SCM_UNLIKELY (SCM_VALUESP (ret
)))
896 /* multiple values returned to continuation */
897 ret
= scm_struct_ref (ret
, SCM_INUM0
);
898 nvalues
= scm_ilength (ret
);
899 PUSH_LIST (ret
, scm_is_null
);
900 goto vm_return_values
;
909 VM_DEFINE_INSTRUCTION (57, smob_call
, "smob-call", 1, -1, -1)
916 subr
= SCM_SMOB_DESCRIPTOR (smob
).apply
;
918 VM_HANDLE_INTERRUPTS
;
927 ret
= subr (smob
, sp
[0]);
930 ret
= subr (smob
, sp
[-1], sp
[0]);
933 ret
= subr (smob
, sp
[-2], sp
[-1], sp
[0]);
939 NULLSTACK_FOR_NONLOCAL_EXIT ();
941 if (SCM_UNLIKELY (SCM_VALUESP (ret
)))
943 /* multiple values returned to continuation */
944 ret
= scm_struct_ref (ret
, SCM_INUM0
);
945 nvalues
= scm_ilength (ret
);
946 PUSH_LIST (ret
, scm_is_null
);
947 goto vm_return_values
;
956 VM_DEFINE_INSTRUCTION (58, foreign_call
, "foreign-call", 1, -1, -1)
962 VM_HANDLE_INTERRUPTS
;
965 ret
= scm_i_foreign_call (foreign
, sp
- nargs
+ 1);
967 NULLSTACK_FOR_NONLOCAL_EXIT ();
969 if (SCM_UNLIKELY (SCM_VALUESP (ret
)))
971 /* multiple values returned to continuation */
972 ret
= scm_struct_ref (ret
, SCM_INUM0
);
973 nvalues
= scm_ilength (ret
);
974 PUSH_LIST (ret
, scm_is_null
);
975 goto vm_return_values
;
984 VM_DEFINE_INSTRUCTION (89, continuation_call
, "continuation-call", 0, -1, 0)
990 scm_i_check_continuation (contregs
);
991 vm_return_to_continuation (scm_i_contregs_vm (contregs
),
992 scm_i_contregs_vm_cont (contregs
),
994 scm_i_reinstate_continuation (contregs
);
1000 VM_DEFINE_INSTRUCTION (94, partial_cont_call
, "partial-cont-call", 0, -1, 0)
1002 SCM vmcont
, intwinds
, prevwinds
;
1006 if (SCM_UNLIKELY (!SCM_VM_CONT_REWINDABLE_P (vmcont
)))
1007 { finish_args
= vmcont
;
1008 goto vm_error_continuation_not_rewindable
;
1010 prevwinds
= scm_i_dynwinds ();
1011 vm_reinstate_partial_continuation (vm
, vmcont
, intwinds
, sp
+ 1 - fp
, fp
,
1014 /* Rewind prompt jmpbuffers, if any. */
1016 SCM winds
= scm_i_dynwinds ();
1017 for (; !scm_is_eq (winds
, prevwinds
); winds
= scm_cdr (winds
))
1018 if (SCM_PROMPT_P (scm_car (winds
)) && SCM_PROMPT_SETJMP (scm_car (winds
)))
1023 program
= SCM_FRAME_PROGRAM (fp
);
1028 VM_DEFINE_INSTRUCTION (59, tail_call_nargs
, "tail-call/nargs", 0, 0, 1)
1032 nargs
= scm_to_int (x
);
1033 /* FIXME: should truncate values? */
1037 VM_DEFINE_INSTRUCTION (60, call_nargs
, "call/nargs", 0, 0, 1)
1041 nargs
= scm_to_int (x
);
1042 /* FIXME: should truncate values? */
1046 VM_DEFINE_INSTRUCTION (61, mv_call
, "mv-call", 4, -1, 1)
1052 FETCH_OFFSET (offset
);
1056 program
= sp
[-nargs
];
1058 VM_HANDLE_INTERRUPTS
;
1060 if (SCM_UNLIKELY (!SCM_PROGRAM_P (program
)))
1062 if (SCM_STRUCTP (program
) && SCM_STRUCT_APPLICABLE_P (program
))
1064 sp
[-nargs
] = SCM_STRUCT_PROCEDURE (program
);
1067 else if (SCM_NIMP (program
) && SCM_TYP7 (program
) == scm_tc7_smob
1068 && SCM_SMOB_APPLICABLE_P (program
))
1071 sp
[-nargs
] = scm_i_smob_apply_trampoline (program
);
1075 goto vm_error_wrong_type_apply
;
1079 fp
= sp
- nargs
+ 1;
1080 ASSERT (SCM_FRAME_RETURN_ADDRESS (fp
) == 0);
1081 ASSERT (SCM_FRAME_MV_RETURN_ADDRESS (fp
) == 0);
1082 SCM_FRAME_SET_RETURN_ADDRESS (fp
, ip
);
1083 SCM_FRAME_SET_MV_RETURN_ADDRESS (fp
, mvra
);
1084 ip
= SCM_C_OBJCODE_BASE (bp
);
1090 VM_DEFINE_INSTRUCTION (62, apply
, "apply", 1, -1, 1)
1097 ASSERT (nargs
>= 2);
1099 len
= scm_ilength (ls
);
1100 if (SCM_UNLIKELY (len
< 0))
1103 goto vm_error_apply_to_non_list
;
1106 PUSH_LIST (ls
, SCM_NULL_OR_NIL_P
);
1112 VM_DEFINE_INSTRUCTION (63, tail_apply
, "tail-apply", 1, -1, 1)
1119 ASSERT (nargs
>= 2);
1121 len
= scm_ilength (ls
);
1122 if (SCM_UNLIKELY (len
< 0))
1125 goto vm_error_apply_to_non_list
;
1128 PUSH_LIST (ls
, SCM_NULL_OR_NIL_P
);
1134 VM_DEFINE_INSTRUCTION (64, call_cc
, "call/cc", 0, 1, 1)
1137 SCM proc
, vm_cont
, cont
;
1140 vm_cont
= scm_i_vm_capture_stack (vp
->stack_base
, fp
, sp
, ip
, NULL
, 0);
1141 cont
= scm_i_make_continuation (&first
, vm
, vm_cont
);
1144 PUSH ((SCM
)fp
); /* dynamic link */
1145 PUSH (0); /* mvra */
1154 /* otherwise, the vm continuation was reinstated, and
1155 scm_i_vm_return_to_continuation pushed on one value. So pull our regs
1156 back down from the vp, and march on to the next instruction. */
1158 program
= SCM_FRAME_PROGRAM (fp
);
1164 VM_DEFINE_INSTRUCTION (65, tail_call_cc
, "tail-call/cc", 0, 1, 1)
1167 SCM proc
, vm_cont
, cont
;
1170 /* In contrast to call/cc, tail-call/cc captures the continuation without the
1172 vm_cont
= scm_i_vm_capture_stack (vp
->stack_base
,
1173 SCM_FRAME_DYNAMIC_LINK (fp
),
1174 SCM_FRAME_LOWER_ADDRESS (fp
) - 1,
1175 SCM_FRAME_RETURN_ADDRESS (fp
),
1176 SCM_FRAME_MV_RETURN_ADDRESS (fp
),
1178 cont
= scm_i_make_continuation (&first
, vm
, vm_cont
);
1188 /* Otherwise, cache regs and NEXT, as above. Invoking the continuation
1189 does a return from the frame, either to the RA or MVRA. */
1191 program
= SCM_FRAME_PROGRAM (fp
);
1197 VM_DEFINE_INSTRUCTION (66, return, "return", 0, 1, 1)
1203 VM_HANDLE_INTERRUPTS
;
1210 #ifdef VM_ENABLE_STACK_NULLING
1214 /* Restore registers */
1215 sp
= SCM_FRAME_LOWER_ADDRESS (fp
);
1216 ip
= SCM_FRAME_RETURN_ADDRESS (fp
);
1217 fp
= SCM_FRAME_DYNAMIC_LINK (fp
);
1219 #ifdef VM_ENABLE_STACK_NULLING
1220 NULLSTACK (old_sp
- sp
);
1223 /* Set return value (sp is already pushed) */
1227 /* Restore the last program */
1228 program
= SCM_FRAME_PROGRAM (fp
);
1234 VM_DEFINE_INSTRUCTION (67, return_values
, "return/values", 1, -1, -1)
1236 /* nvalues declared at top level, because for some reason gcc seems to think
1237 that perhaps it might be used without declaration. Fooey to that, I say. */
1241 RETURN_HOOK (nvalues
);
1243 VM_HANDLE_INTERRUPTS
;
1245 if (nvalues
!= 1 && SCM_FRAME_MV_RETURN_ADDRESS (fp
))
1247 /* A multiply-valued continuation */
1248 SCM
*vals
= sp
- nvalues
;
1250 /* Restore registers */
1251 sp
= SCM_FRAME_LOWER_ADDRESS (fp
) - 1;
1252 ip
= SCM_FRAME_MV_RETURN_ADDRESS (fp
);
1253 fp
= SCM_FRAME_DYNAMIC_LINK (fp
);
1255 /* Push return values, and the number of values */
1256 for (i
= 0; i
< nvalues
; i
++)
1258 *++sp
= SCM_I_MAKINUM (nvalues
);
1260 /* Finally null the end of the stack */
1261 NULLSTACK (vals
+ nvalues
- sp
);
1263 else if (nvalues
>= 1)
1265 /* Multiple values for a single-valued continuation -- here's where I
1266 break with guile tradition and try and do something sensible. (Also,
1267 this block handles the single-valued return to an mv
1269 SCM
*vals
= sp
- nvalues
;
1270 /* Restore registers */
1271 sp
= SCM_FRAME_LOWER_ADDRESS (fp
) - 1;
1272 ip
= SCM_FRAME_RETURN_ADDRESS (fp
);
1273 fp
= SCM_FRAME_DYNAMIC_LINK (fp
);
1275 /* Push first value */
1278 /* Finally null the end of the stack */
1279 NULLSTACK (vals
+ nvalues
- sp
);
1282 goto vm_error_no_values
;
1284 /* Restore the last program */
1285 program
= SCM_FRAME_PROGRAM (fp
);
1291 VM_DEFINE_INSTRUCTION (68, return_values_star
, "return/values*", 1, -1, -1)
1296 ASSERT (nvalues
>= 1);
1300 while (scm_is_pair (l
))
1306 if (SCM_UNLIKELY (!SCM_NULL_OR_NIL_P (l
))) {
1307 finish_args
= scm_list_1 (l
);
1308 goto vm_error_improper_list
;
1311 goto vm_return_values
;
1314 VM_DEFINE_INSTRUCTION (88, return_nvalues
, "return/nvalues", 0, 1, -1)
1318 nvalues
= scm_to_int (n
);
1319 ASSERT (nvalues
>= 0);
1320 goto vm_return_values
;
1323 VM_DEFINE_INSTRUCTION (69, truncate_values
, "truncate-values", 2, -1, -1)
1328 nvalues
= scm_to_int (x
);
1335 if (nvalues
< nbinds
)
1336 goto vm_error_not_enough_values
;
1339 POP_LIST (nvalues
- nbinds
);
1341 DROPN (nvalues
- nbinds
);
1346 VM_DEFINE_INSTRUCTION (70, box
, "box", 1, 1, 0)
1351 LOCAL_SET (FETCH (), scm_cell (scm_tc7_variable
, SCM_UNPACK (val
)));
1356 (let ((a *undef*) (b *undef*) ...)
1357 (set! a (lambda () (b ...)))
1360 VM_DEFINE_INSTRUCTION (71, empty_box
, "empty-box", 1, 0, 0)
1363 LOCAL_SET (FETCH (),
1364 scm_cell (scm_tc7_variable
, SCM_UNPACK (SCM_UNDEFINED
)));
1368 VM_DEFINE_INSTRUCTION (72, local_boxed_ref
, "local-boxed-ref", 1, 0, 1)
1370 SCM v
= LOCAL_REF (FETCH ());
1371 ASSERT_BOUND_VARIABLE (v
);
1372 PUSH (VARIABLE_REF (v
));
1376 VM_DEFINE_INSTRUCTION (73, local_boxed_set
, "local-boxed-set", 1, 1, 0)
1379 v
= LOCAL_REF (FETCH ());
1381 ASSERT_VARIABLE (v
);
1382 VARIABLE_SET (v
, val
);
1386 VM_DEFINE_INSTRUCTION (74, free_ref
, "free-ref", 1, 0, 1)
1388 scm_t_uint8 idx
= FETCH ();
1390 CHECK_FREE_VARIABLE (idx
);
1391 PUSH (FREE_VARIABLE_REF (idx
));
1395 /* no free-set -- if a var is assigned, it should be in a box */
1397 VM_DEFINE_INSTRUCTION (75, free_boxed_ref
, "free-boxed-ref", 1, 0, 1)
1400 scm_t_uint8 idx
= FETCH ();
1401 CHECK_FREE_VARIABLE (idx
);
1402 v
= FREE_VARIABLE_REF (idx
);
1403 ASSERT_BOUND_VARIABLE (v
);
1404 PUSH (VARIABLE_REF (v
));
1408 VM_DEFINE_INSTRUCTION (76, free_boxed_set
, "free-boxed-set", 1, 1, 0)
1411 scm_t_uint8 idx
= FETCH ();
1413 CHECK_FREE_VARIABLE (idx
);
1414 v
= FREE_VARIABLE_REF (idx
);
1415 ASSERT_BOUND_VARIABLE (v
);
1416 VARIABLE_SET (v
, val
);
1420 VM_DEFINE_INSTRUCTION (77, make_closure
, "make-closure", 2, -1, 1)
1429 closure
= scm_words (scm_tc7_program
| (len
<<16), len
+ 3);
1430 SCM_SET_CELL_OBJECT_1 (closure
, SCM_PROGRAM_OBJCODE (sp
[-len
]));
1431 SCM_SET_CELL_OBJECT_2 (closure
, SCM_PROGRAM_OBJTABLE (sp
[-len
]));
1433 for (n
= 0; n
< len
; n
++)
1434 SCM_PROGRAM_FREE_VARIABLE_SET (closure
, n
, sp
[-len
+ 1 + n
]);
1439 VM_DEFINE_INSTRUCTION (78, make_variable
, "make-variable", 0, 0, 1)
1442 /* fixme underflow */
1443 PUSH (scm_cell (scm_tc7_variable
, SCM_UNPACK (SCM_UNDEFINED
)));
1447 VM_DEFINE_INSTRUCTION (79, fix_closure
, "fix-closure", 2, -1, 0)
1450 unsigned int i
= FETCH ();
1454 /* FIXME CHECK_LOCAL (i) */
1456 /* FIXME ASSERT_PROGRAM (x); */
1457 len
= SCM_PROGRAM_NUM_FREE_VARIABLES (x
);
1458 for (n
= 0; n
< len
; n
++)
1459 SCM_PROGRAM_FREE_VARIABLE_SET (x
, n
, sp
[-len
+ 1 + n
]);
1464 VM_DEFINE_INSTRUCTION (80, define
, "define", 0, 0, 2)
1470 VARIABLE_SET (scm_sym2var (sym
, scm_current_module_lookup_closure (),
1476 VM_DEFINE_INSTRUCTION (81, make_keyword
, "make-keyword", 0, 1, 1)
1480 *sp
= scm_symbol_to_keyword (*sp
);
1484 VM_DEFINE_INSTRUCTION (82, make_symbol
, "make-symbol", 0, 1, 1)
1488 *sp
= scm_string_to_symbol (*sp
);
1492 VM_DEFINE_INSTRUCTION (83, prompt
, "prompt", 4, 2, 0)
1495 scm_t_uint8 escape_only_p
;
1498 escape_only_p
= FETCH ();
1499 FETCH_OFFSET (offset
);
1503 /* Push the prompt onto the dynamic stack. */
1504 prompt
= scm_c_make_prompt (k
, fp
, sp
, ip
+ offset
, escape_only_p
, vm_cookie
,
1506 scm_i_set_dynwinds (scm_cons (prompt
, SCM_PROMPT_DYNWINDS (prompt
)));
1507 if (SCM_PROMPT_SETJMP (prompt
))
1509 /* The prompt exited nonlocally. Cache the regs back from the vp, and go
1512 Note, at this point, we must assume that any variable local to
1513 vm_engine that can be assigned *has* been assigned. So we need to pull
1514 all our state back from the ip/fp/sp.
1517 program
= SCM_FRAME_PROGRAM (fp
);
1522 /* Otherwise setjmp returned for the first time, so we go to execute the
1527 VM_DEFINE_INSTRUCTION (85, wind
, "wind", 0, 2, 0)
1533 /* Push wind and unwind procedures onto the dynamic stack. Note that neither
1534 are actually called; the compiler should emit calls to wind and unwind for
1535 the normal dynamic-wind control flow. */
1536 if (SCM_UNLIKELY (scm_is_false (scm_thunk_p (wind
))))
1539 goto vm_error_not_a_thunk
;
1541 if (SCM_UNLIKELY (scm_is_false (scm_thunk_p (unwind
))))
1543 finish_args
= unwind
;
1544 goto vm_error_not_a_thunk
;
1546 scm_i_set_dynwinds (scm_cons (scm_cons (wind
, unwind
), scm_i_dynwinds ()));
1550 VM_DEFINE_INSTRUCTION (86, abort
, "abort", 1, -1, -1)
1552 unsigned n
= FETCH ();
1554 if (sp
- n
- 2 <= SCM_FRAME_UPPER_ADDRESS (fp
))
1555 goto vm_error_stack_underflow
;
1556 vm_abort (vm
, n
, vm_cookie
);
1557 /* vm_abort should not return */
1561 VM_DEFINE_INSTRUCTION (87, unwind
, "unwind", 0, 0, 0)
1563 /* A normal exit from the dynamic extent of an expression. Pop the top entry
1564 off of the dynamic stack. */
1565 scm_i_set_dynwinds (scm_cdr (scm_i_dynwinds ()));
1569 VM_DEFINE_INSTRUCTION (90, wind_fluids
, "wind-fluids", 1, -1, 0)
1571 unsigned n
= FETCH ();
1574 if (sp
- 2*n
< SCM_FRAME_UPPER_ADDRESS (fp
))
1575 goto vm_error_stack_underflow
;
1578 wf
= scm_i_make_with_fluids (n
, sp
+ 1 - 2*n
, sp
+ 1 - n
);
1579 scm_i_swap_with_fluids (wf
, dynstate
);
1580 scm_i_set_dynwinds (scm_cons (wf
, scm_i_dynwinds ()));
1584 VM_DEFINE_INSTRUCTION (91, unwind_fluids
, "unwind-fluids", 0, 0, 0)
1587 wf
= scm_car (scm_i_dynwinds ());
1588 scm_i_set_dynwinds (scm_cdr (scm_i_dynwinds ()));
1589 scm_i_swap_with_fluids (wf
, dynstate
);
1593 VM_DEFINE_INSTRUCTION (92, fluid_ref
, "fluid-ref", 0, 1, 1)
1599 fluids
= SCM_I_DYNAMIC_STATE_FLUIDS (dynstate
);
1600 if (SCM_UNLIKELY (!SCM_FLUID_P (*sp
))
1601 || ((num
= SCM_I_FLUID_NUM (*sp
)) >= SCM_SIMPLE_VECTOR_LENGTH (fluids
)))
1603 /* Punt dynstate expansion and error handling to the C proc. */
1605 *sp
= scm_fluid_ref (*sp
);
1608 *sp
= SCM_SIMPLE_VECTOR_REF (fluids
, num
);
1613 VM_DEFINE_INSTRUCTION (93, fluid_set
, "fluid-set", 0, 2, 0)
1616 SCM val
, fluid
, fluids
;
1620 fluids
= SCM_I_DYNAMIC_STATE_FLUIDS (dynstate
);
1621 if (SCM_UNLIKELY (!SCM_FLUID_P (fluid
))
1622 || ((num
= SCM_I_FLUID_NUM (fluid
)) >= SCM_SIMPLE_VECTOR_LENGTH (fluids
)))
1624 /* Punt dynstate expansion and error handling to the C proc. */
1626 scm_fluid_set_x (fluid
, val
);
1629 SCM_SIMPLE_VECTOR_SET (fluids
, num
, val
);
1634 VM_DEFINE_INSTRUCTION (95, assert_nargs_ee_locals
, "assert-nargs-ee/locals", 1, 0, 0)
1639 /* nargs = n & 0x7, nlocs = nargs + (n >> 3) */
1642 if (SCM_UNLIKELY (sp
- (fp
- 1) != (n
& 0x7)))
1643 goto vm_error_wrong_num_args
;
1649 *++old_sp
= SCM_UNDEFINED
;
1656 (defun renumber-ops ()
1657 "start from top of buffer and renumber 'VM_DEFINE_FOO (\n' sequences"
1660 (let ((counter -1)) (goto-char (point-min))
1661 (while (re-search-forward "^VM_DEFINE_[^ ]+ (\\([^,]+\\)," (point-max) t)
1663 (number-to-string (setq counter (1+ counter)))