Merge commit 'origin/master' into vm
[bpt/guile.git] / libguile / vm-engine.c
1 /* Copyright (C) 2001 Free Software Foundation, Inc.
2 *
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)
6 * any later version.
7 *
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.
12 *
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
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
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.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
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.
37 *
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. */
41
42 /* This file is included in vm.c twice */
43
44 #include "vm-engine.h"
45
46
47 static SCM
48 vm_run (SCM vm, SCM program, SCM args)
49 #define FUNC_NAME "vm-engine"
50 {
51 /* VM registers */
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 */
55
56 /* Cache variables */
57 struct scm_vm *vp = SCM_VM_DATA (vm); /* VM data pointer */
58 struct scm_program *bp = NULL; /* program base pointer */
59 SCM external = SCM_EOL; /* external environment */
60 SCM *objects = NULL; /* constant objects */
61 scm_t_array_handle objects_handle; /* handle of the OBJECTS array */
62 size_t object_count; /* length of OBJECTS */
63 SCM *stack_base = vp->stack_base; /* stack base address */
64 SCM *stack_limit = vp->stack_limit; /* stack limit address */
65
66 /* Internal variables */
67 int nargs = 0;
68 int nvalues = 0;
69 long start_time = scm_c_get_internal_run_time ();
70 // SCM dynwinds = SCM_EOL;
71 SCM err_msg;
72 SCM err_args;
73 #if VM_USE_HOOKS
74 SCM hook_args = SCM_LIST1 (vm);
75 #endif
76 struct vm_unwind_data wind_data;
77
78 /* dynwind ended in the halt instruction */
79 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
80 wind_data.vp = vp;
81 wind_data.sp = vp->sp;
82 wind_data.fp = vp->fp;
83 scm_dynwind_unwind_handler (vm_reset_stack, &wind_data, 0);
84
85 /* could do this if we reified all vm stacks -- for now, don't bother changing
86 *the-vm*
87 if (scm_fluid_ref (scm_the_vm_fluid) != vm)
88 scm_dynwind_fluid (scm_the_vm_fluid, vm);
89 */
90
91 #ifdef HAVE_LABELS_AS_VALUES
92 /* Jump table */
93 static void *jump_table[] = {
94 #define VM_INSTRUCTION_TO_LABEL 1
95 #include "vm-expand.h"
96 #include "vm-i-system.i"
97 #include "vm-i-scheme.i"
98 #include "vm-i-loader.i"
99 #undef VM_INSTRUCTION_TO_LABEL
100 };
101 #endif
102
103 /* Initialization */
104 {
105 SCM prog = program;
106
107 /* Boot program */
108 scm_byte_t bytes[6] = {scm_op_mv_call, 0, 0, 1, scm_op_make_int8_1, scm_op_halt};
109 bytes[1] = scm_ilength (args); /* FIXME: argument overflow */
110 program = scm_c_make_program (bytes, 6, SCM_BOOL_F);
111
112 /* Initial frame */
113 CACHE_REGISTER ();
114 CACHE_PROGRAM ();
115 PUSH (program);
116 NEW_FRAME ();
117
118 /* Initial arguments */
119 PUSH (prog);
120 for (; !SCM_NULLP (args); args = SCM_CDR (args))
121 PUSH (SCM_CAR (args));
122 }
123
124 /* Let's go! */
125 BOOT_HOOK ();
126
127 #ifndef HAVE_LABELS_AS_VALUES
128 vm_start:
129 switch (*ip++) {
130 #endif
131
132 #include "vm-expand.h"
133 #include "vm-i-system.c"
134 #include "vm-i-scheme.c"
135 #include "vm-i-loader.c"
136
137 #ifndef HAVE_LABELS_AS_VALUES
138 }
139 #endif
140
141 /* Errors */
142 {
143 vm_error_unbound:
144 err_msg = scm_from_locale_string ("VM: Unbound variable: ~A");
145 goto vm_error;
146
147 vm_error_wrong_type_arg:
148 err_msg = scm_from_locale_string ("VM: Wrong type argument");
149 err_args = SCM_EOL;
150 goto vm_error;
151
152 vm_error_wrong_num_args:
153 /* nargs and program are valid */
154 SYNC_ALL ();
155 if (objects)
156 scm_array_handle_release (&objects_handle);
157 scm_wrong_num_args (program);
158 /* shouldn't get here */
159 goto vm_error;
160
161 vm_error_wrong_type_apply:
162 err_msg = scm_from_locale_string ("VM: Wrong type to apply: ~S "
163 "[IP offset: ~a]");
164 err_args = SCM_LIST2 (program,
165 SCM_I_MAKINUM (ip - bp->base));
166 goto vm_error;
167
168 vm_error_stack_overflow:
169 err_msg = scm_from_locale_string ("VM: Stack overflow");
170 err_args = SCM_EOL;
171 goto vm_error;
172
173 vm_error_stack_underflow:
174 err_msg = scm_from_locale_string ("VM: Stack underflow");
175 err_args = SCM_EOL;
176 goto vm_error;
177
178 vm_error_improper_list:
179 err_msg = scm_from_locale_string ("VM: Attempt to unroll an improper list: tail is ~A");
180 goto vm_error;
181
182 vm_error_not_a_pair:
183 SYNC_ALL ();
184 scm_wrong_type_arg_msg (FUNC_NAME, 1, err_args, "pair");
185 /* shouldn't get here */
186 goto vm_error;
187
188 vm_error_no_values:
189 err_msg = scm_from_locale_string ("VM: 0-valued return");
190 err_args = SCM_EOL;
191 goto vm_error;
192
193 vm_error_not_enough_values:
194 err_msg = scm_from_locale_string ("VM: Not enough values for mv-bind");
195 err_args = SCM_EOL;
196 goto vm_error;
197
198 vm_error_no_such_module:
199 err_msg = scm_from_locale_string ("VM: No such module: ~A");
200 goto vm_error;
201
202 #if VM_CHECK_IP
203 vm_error_invalid_address:
204 err_msg = scm_from_locale_string ("VM: Invalid program address");
205 err_args = SCM_EOL;
206 goto vm_error;
207 #endif
208
209 #if VM_CHECK_EXTERNAL
210 vm_error_external:
211 err_msg = scm_from_locale_string ("VM: Invalid external access");
212 err_args = SCM_EOL;
213 goto vm_error;
214 #endif
215
216 #if VM_CHECK_OBJECT
217 vm_error_object:
218 err_msg = scm_from_locale_string ("VM: Invalid object table access");
219 err_args = SCM_EOL;
220 goto vm_error;
221 #endif
222
223 vm_error:
224 SYNC_ALL ();
225 if (objects)
226 scm_array_handle_release (&objects_handle);
227
228 scm_ithrow (sym_vm_error, SCM_LIST3 (sym_vm_run, err_msg, err_args), 1);
229 }
230
231 abort (); /* never reached */
232 }
233 #undef FUNC_NAME
234
235 /*
236 Local Variables:
237 c-file-style: "gnu"
238 End:
239 */