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
;
68 VM_DEFINE_INSTRUCTION (break, "break", 0, 0, 0)
74 VM_DEFINE_INSTRUCTION (drop
, "drop", 0, 0, 0)
80 VM_DEFINE_INSTRUCTION (mark
, "mark", 0, 0, 1)
86 VM_DEFINE_INSTRUCTION (dup
, "dup", 0, 0, 1)
98 VM_DEFINE_INSTRUCTION (void, "void", 0, 0, 1)
100 PUSH (SCM_UNSPECIFIED
);
104 VM_DEFINE_INSTRUCTION (make_true
, "make-true", 0, 0, 1)
110 VM_DEFINE_INSTRUCTION (make_false
, "make-false", 0, 0, 1)
116 VM_DEFINE_INSTRUCTION (make_eol
, "make-eol", 0, 0, 1)
122 VM_DEFINE_INSTRUCTION (make_int8
, "make-int8", 1, 0, 1)
124 PUSH (SCM_I_MAKINUM ((signed char) FETCH ()));
128 VM_DEFINE_INSTRUCTION (make_int8_0
, "make-int8:0", 0, 0, 1)
134 VM_DEFINE_INSTRUCTION (make_int8_1
, "make-int8:1", 0, 0, 1)
136 PUSH (SCM_I_MAKINUM (1));
140 VM_DEFINE_INSTRUCTION (make_int16
, "make-int16", 2, 0, 1)
144 PUSH (SCM_I_MAKINUM ((signed short) (h
<< 8) + l
));
148 VM_DEFINE_INSTRUCTION (make_char8
, "make-char8", 1, 0, 1)
150 PUSH (SCM_MAKE_CHAR (FETCH ()));
154 VM_DEFINE_INSTRUCTION (list
, "list", 2, -1, 1)
156 unsigned h
= FETCH ();
157 unsigned l
= FETCH ();
158 unsigned len
= ((h
<< 8) + l
);
163 VM_DEFINE_INSTRUCTION (vector
, "vector", 2, -1, 1)
165 unsigned h
= FETCH ();
166 unsigned l
= FETCH ();
167 unsigned len
= ((h
<< 8) + l
);
169 *sp
= scm_vector (*sp
);
173 VM_DEFINE_INSTRUCTION (list_mark
, "list-mark", 0, 0, 0)
179 VM_DEFINE_INSTRUCTION (vector_mark
, "vector-mark", 0, 0, 0)
182 *sp
= scm_vector (*sp
);
186 VM_DEFINE_INSTRUCTION (list_break
, "list-break", 0, 0, 0)
190 for (; !SCM_NULLP (l
); l
= SCM_CDR (l
))
200 #define OBJECT_REF(i) objects[i]
201 #define OBJECT_SET(i,o) objects[i] = o
203 #define LOCAL_REF(i) SCM_FRAME_VARIABLE (fp, i)
204 #define LOCAL_SET(i,o) SCM_FRAME_VARIABLE (fp, i) = o
206 /* For the variable operations, we _must_ obviously avoid function calls to
207 `scm_variable_ref ()', `scm_variable_bound_p ()' and friends which do
208 nothing more than the corresponding macros. */
209 #define VARIABLE_REF(v) SCM_VARIABLE_REF (v)
210 #define VARIABLE_SET(v,o) SCM_VARIABLE_SET (v, o)
211 #define VARIABLE_BOUNDP(v) (VARIABLE_REF (v) != SCM_UNDEFINED)
215 VM_DEFINE_INSTRUCTION (object_ref
, "object-ref", 1, 0, 1)
217 register unsigned objnum
= FETCH ();
218 CHECK_OBJECT (objnum
);
219 PUSH (OBJECT_REF (objnum
));
223 VM_DEFINE_INSTRUCTION (local_ref
, "local-ref", 1, 0, 1)
225 PUSH (LOCAL_REF (FETCH ()));
229 VM_DEFINE_INSTRUCTION (external_ref
, "external-ref", 1, 0, 1)
233 for (i
= FETCH (); i
; i
--)
243 VM_DEFINE_INSTRUCTION (variable_ref
, "variable-ref", 0, 0, 1)
247 if (!VARIABLE_BOUNDP (x
))
249 err_args
= SCM_LIST1 (x
);
250 /* Was: err_args = SCM_LIST1 (SCM_CAR (x)); */
251 goto vm_error_unbound
;
255 SCM o
= VARIABLE_REF (x
);
262 VM_DEFINE_INSTRUCTION (late_variable_ref
, "late-variable-ref", 1, 0, 1)
264 unsigned objnum
= FETCH ();
266 CHECK_OBJECT (objnum
);
267 pair_or_var
= OBJECT_REF (objnum
);
269 if (!SCM_VARIABLEP (pair_or_var
))
272 /* either one of these calls might longjmp */
273 SCM mod
= scm_resolve_module (SCM_CAR (pair_or_var
));
274 pair_or_var
= scm_module_lookup (mod
, SCM_CDR (pair_or_var
));
275 OBJECT_SET (objnum
, pair_or_var
);
276 if (!VARIABLE_BOUNDP (pair_or_var
))
278 err_args
= SCM_LIST1 (pair_or_var
);
279 goto vm_error_unbound
;
283 PUSH (VARIABLE_REF (pair_or_var
));
289 VM_DEFINE_INSTRUCTION (local_set
, "local-set", 1, 1, 0)
291 LOCAL_SET (FETCH (), *sp
);
296 VM_DEFINE_INSTRUCTION (external_set
, "external-set", 1, 1, 0)
300 for (i
= FETCH (); i
; i
--)
311 VM_DEFINE_INSTRUCTION (variable_set
, "variable-set", 0, 1, 0)
313 VARIABLE_SET (sp
[0], sp
[-1]);
318 VM_DEFINE_INSTRUCTION (late_variable_set
, "late-variable-set", 1, 1, 0)
320 unsigned objnum
= FETCH ();
322 CHECK_OBJECT (objnum
);
323 pair_or_var
= OBJECT_REF (objnum
);
325 if (!SCM_VARIABLEP (pair_or_var
))
328 SCM mod
= scm_resolve_module (SCM_CAR (pair_or_var
));
329 /* module_lookup might longjmp */
330 pair_or_var
= scm_module_lookup (mod
, SCM_CDR (pair_or_var
));
331 OBJECT_SET (objnum
, pair_or_var
);
334 VARIABLE_SET (pair_or_var
, *sp
);
348 signed short offset = (h << 8) + l; \
355 VM_DEFINE_INSTRUCTION (br
, "br", 2, 0, 0)
359 ip
+= (signed short) (h
<< 8) + l
;
363 VM_DEFINE_INSTRUCTION (br_if
, "br-if", 2, 0, 0)
365 BR (!SCM_FALSEP (*sp
));
368 VM_DEFINE_INSTRUCTION (br_if_not
, "br-if-not", 2, 0, 0)
370 BR (SCM_FALSEP (*sp
));
373 VM_DEFINE_INSTRUCTION (br_if_eq
, "br-if-eq", 2, 0, 0)
375 BR (SCM_EQ_P (sp
[0], sp
--[1]));
378 VM_DEFINE_INSTRUCTION (br_if_not_eq
, "br-if-not-eq", 2, 0, 0)
380 BR (!SCM_EQ_P (sp
[0], sp
--[1]));
383 VM_DEFINE_INSTRUCTION (br_if_null
, "br-if-null", 2, 0, 0)
385 BR (SCM_NULLP (*sp
));
388 VM_DEFINE_INSTRUCTION (br_if_not_null
, "br-if-not-null", 2, 0, 0)
390 BR (!SCM_NULLP (*sp
));
398 VM_DEFINE_INSTRUCTION (make_closure
, "make-closure", 0, 1, 1)
401 *sp
= scm_c_make_closure (*sp
, external
);
405 VM_DEFINE_INSTRUCTION (call
, "call", 1, -1, 1)
416 if (SCM_PROGRAM_P (x
))
427 #ifdef ENABLE_TRAMPOLINE
428 /* Seems to slow down the fibo test, dunno why */
436 scm_t_trampoline_0 call
= scm_trampoline_0 (x
);
447 scm_t_trampoline_1 call
= scm_trampoline_1 (x
);
453 *sp
= call (x
, arg1
);
460 scm_t_trampoline_2 call
= scm_trampoline_2 (x
);
467 *sp
= call (x
, arg1
, arg2
);
475 * Other interpreted or compiled call
477 if (!SCM_FALSEP (scm_procedure_p (x
)))
479 /* At this point, the stack contains the procedure and each one of its
485 *sp
= scm_apply (x
, args
, SCM_EOL
);
491 if (SCM_VM_CONT_P (x
))
494 /* Check the number of arguments */
496 scm_wrong_num_args (x
);
498 /* Reinstate the continuation */
500 reinstate_vm_cont (vp
, x
);
502 program
= SCM_FRAME_PROGRAM (fp
);
508 goto vm_error_wrong_type_apply
;
511 VM_DEFINE_INSTRUCTION (tail_call
, "tail-call", 1, -1, 1)
517 SCM_TICK
; /* allow interrupt here */
520 * Tail recursive call
522 if (SCM_EQ_P (x
, program
))
529 for (i
= 0; i
< bp
->nargs
; i
++)
530 LOCAL_SET (i
, sp
[i
]);
532 /* Drop the first argument and the program itself. */
543 if (SCM_PROGRAM_P (x
))
548 goto vm_call_program
;
550 #ifdef ENABLE_TRAMPOLINE
551 /* This seems to actually slow down the fibo test -- dunno why */
559 scm_t_trampoline_0 call
= scm_trampoline_0 (x
);
570 scm_t_trampoline_1 call
= scm_trampoline_1 (x
);
576 *sp
= call (x
, arg1
);
583 scm_t_trampoline_2 call
= scm_trampoline_2 (x
);
590 *sp
= call (x
, arg1
, arg2
);
599 * Other interpreted or compiled call
601 if (!SCM_FALSEP (scm_procedure_p (x
)))
607 *sp
= scm_apply (x
, args
, SCM_EOL
);
613 if (SCM_VM_CONT_P (x
))
617 goto vm_error_wrong_type_apply
;
620 VM_DEFINE_INSTRUCTION (apply
, "apply", 1, -1, 1)
628 goto vm_error_wrong_num_args
;
630 len
= scm_ilength (ls
);
632 goto vm_error_wrong_type_arg
;
634 for (; !SCM_NULLP (ls
); ls
= SCM_CDR (ls
))
641 VM_DEFINE_INSTRUCTION (call_cc
, "call/cc", 1, 1, 1)
644 PUSH (capture_vm_cont (vp
));
650 VM_DEFINE_INSTRUCTION (return, "return", 0, 0, 1)
657 /* Restore the last program */
658 program
= SCM_FRAME_PROGRAM (fp
);