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.c twice */
44 #include "vm-engine.h"
48 vm_run (SCM vm
, SCM program
, SCM args
)
49 #define FUNC_NAME "vm-engine"
52 register scm_byte_t
*ip IP_REG
; /* instruction pointer */
53 register SCM
*sp SP_REG
; /* stack pointer */
54 register SCM
*fp FP_REG
; /* frame pointer */
57 struct scm_vm
*vp
= SCM_VM_DATA (vm
); /* VM data pointer */
58 struct scm_objcode
*bp
= NULL
; /* program base pointer */
59 SCM external
= SCM_EOL
; /* external environment */
60 SCM
*objects
= NULL
; /* constant objects */
61 size_t object_count
= 0; /* length of OBJECTS */
62 SCM
*stack_base
= vp
->stack_base
; /* stack base address */
63 SCM
*stack_limit
= vp
->stack_limit
; /* stack limit address */
65 /* Internal variables */
68 long start_time
= scm_c_get_internal_run_time ();
69 // SCM dynwinds = SCM_EOL;
73 SCM hook_args
= SCM_LIST1 (vm
);
75 struct vm_unwind_data wind_data
;
77 #ifdef HAVE_LABELS_AS_VALUES
78 static void **jump_table
= NULL
;
80 if (SCM_UNLIKELY (!jump_table
))
83 jump_table
= scm_gc_malloc (SCM_VM_NUM_INSTRUCTIONS
* sizeof(void*),
85 for (i
= 0; i
< SCM_VM_NUM_INSTRUCTIONS
; i
++)
86 jump_table
[i
] = &&vm_error_bad_instruction
;
87 #define VM_INSTRUCTION_TO_LABEL 1
88 #include "vm-expand.h"
89 #include "vm-i-system.i"
90 #include "vm-i-scheme.i"
91 #include "vm-i-loader.i"
92 #undef VM_INSTRUCTION_TO_LABEL
96 /* dynwind ended in the halt instruction */
97 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE
);
99 wind_data
.sp
= vp
->sp
;
100 wind_data
.fp
= vp
->fp
;
101 scm_dynwind_unwind_handler (vm_reset_stack
, &wind_data
, 0);
103 /* could do this if we reified all vm stacks -- for now, don't bother changing
105 if (scm_fluid_ref (scm_the_vm_fluid) != vm)
106 scm_dynwind_fluid (scm_the_vm_fluid, vm);
114 program
= vm_make_boot_program (scm_ilength (args
));
122 /* Initial arguments */
124 for (; !SCM_NULLP (args
); args
= SCM_CDR (args
))
125 PUSH (SCM_CAR (args
));
132 #ifndef HAVE_LABELS_AS_VALUES
134 switch ((*ip
++) & SCM_VM_INSTRUCTION_MASK
) {
137 #include "vm-expand.h"
138 #include "vm-i-system.c"
139 #include "vm-i-scheme.c"
140 #include "vm-i-loader.c"
142 #ifndef HAVE_LABELS_AS_VALUES
144 goto vm_error_bad_instruction
;
150 vm_error_bad_instruction
:
151 err_msg
= scm_from_locale_string ("VM: Bad instruction: ~A");
152 err_args
= SCM_LIST1 (scm_from_uchar (ip
[-1]));
156 err_msg
= scm_from_locale_string ("VM: Unbound variable: ~A");
159 vm_error_wrong_type_arg
:
160 err_msg
= scm_from_locale_string ("VM: Wrong type argument");
164 vm_error_wrong_num_args
:
165 /* nargs and program are valid */
167 scm_wrong_num_args (program
);
168 /* shouldn't get here */
171 vm_error_wrong_type_apply
:
172 err_msg
= scm_from_locale_string ("VM: Wrong type to apply: ~S "
174 err_args
= SCM_LIST2 (program
,
175 SCM_I_MAKINUM (ip
- bp
->base
));
178 vm_error_stack_overflow
:
179 err_msg
= scm_from_locale_string ("VM: Stack overflow");
183 vm_error_stack_underflow
:
184 err_msg
= scm_from_locale_string ("VM: Stack underflow");
188 vm_error_improper_list
:
189 err_msg
= scm_from_locale_string ("VM: Attempt to unroll an improper list: tail is ~A");
194 scm_wrong_type_arg_msg (FUNC_NAME
, 1, err_args
, "pair");
195 /* shouldn't get here */
199 err_msg
= scm_from_locale_string ("VM: 0-valued return");
203 vm_error_not_enough_values
:
204 err_msg
= scm_from_locale_string ("VM: Not enough values for mv-bind");
208 vm_error_no_such_module
:
209 err_msg
= scm_from_locale_string ("VM: No such module: ~A");
213 vm_error_invalid_address
:
214 err_msg
= scm_from_locale_string ("VM: Invalid program address");
219 #if VM_CHECK_EXTERNAL
221 err_msg
= scm_from_locale_string ("VM: Invalid external access");
228 err_msg
= scm_from_locale_string ("VM: Invalid object table access");
236 scm_ithrow (sym_vm_error
, SCM_LIST3 (sym_vm_run
, err_msg
, err_args
), 1);
239 abort (); /* never reached */