fix backtraces with compiled boot-9
[bpt/guile.git] / libguile / vm-engine.c
CommitLineData
8f5cfc81 1/* Copyright (C) 2001 Free Software Foundation, Inc.
a98cef7e
KN
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
6d14383e
AW
42/* This file is included in vm.c multiple times */
43
44#if (VM_ENGINE == SCM_VM_REGULAR_ENGINE)
45#define VM_USE_HOOKS 0 /* Various hooks */
46#define VM_USE_CLOCK 0 /* Bogoclock */
47#define VM_CHECK_EXTERNAL 1 /* Check external link */
48#define VM_CHECK_OBJECT 1 /* Check object table */
e06e857c 49#define VM_PUSH_DEBUG_FRAMES 0 /* Push frames onto the evaluator debug stack */
6d14383e
AW
50#elif (VM_ENGINE == SCM_VM_DEBUG_ENGINE)
51#define VM_USE_HOOKS 1
52#define VM_USE_CLOCK 1
53#define VM_CHECK_EXTERNAL 1
54#define VM_CHECK_OBJECT 1
e06e857c 55#define VM_PUSH_DEBUG_FRAMES 1
6d14383e
AW
56#else
57#error unknown debug engine VM_ENGINE
58#endif
a98cef7e 59
83495480 60#include "vm-engine.h"
a98cef7e 61
238e7a11 62
a98cef7e 63static SCM
6d14383e 64VM_NAME (struct scm_vm *vp, SCM program, SCM *argv, int nargs)
a98cef7e 65{
17e90c5e
KN
66 /* VM registers */
67 register scm_byte_t *ip IP_REG; /* instruction pointer */
68 register SCM *sp SP_REG; /* stack pointer */
69 register SCM *fp FP_REG; /* frame pointer */
a98cef7e 70
d608d68d 71 /* Cache variables */
53e28ed9 72 struct scm_objcode *bp = NULL; /* program base pointer */
41f248a8 73 SCM external = SCM_EOL; /* external environment */
17e90c5e 74 SCM *objects = NULL; /* constant objects */
2fda0242 75 size_t object_count = 0; /* length of OBJECTS */
3d5ee0cd
KN
76 SCM *stack_base = vp->stack_base; /* stack base address */
77 SCM *stack_limit = vp->stack_limit; /* stack limit address */
a98cef7e 78
d608d68d 79 /* Internal variables */
ef24c01b 80 int nvalues = 0;
3d5ee0cd 81 long start_time = scm_c_get_internal_run_time ();
e06e857c
AW
82 SCM finish_args; /* used both for returns: both in error
83 and normal situations */
17e90c5e 84#if VM_USE_HOOKS
6d14383e 85 SCM hook_args = SCM_EOL;
a98cef7e 86#endif
17d1b4bf 87
53e28ed9
AW
88#ifdef HAVE_LABELS_AS_VALUES
89 static void **jump_table = NULL;
e06e857c 90#endif
53e28ed9 91
e06e857c
AW
92#if VM_PUSH_DEBUG_FRAMES
93 scm_t_debug_frame debug;
94 scm_t_debug_info debug_vect_body;
95 debug.status = SCM_VOIDFRAME;
96#endif
97
98#ifdef HAVE_LABELS_AS_VALUES
53e28ed9
AW
99 if (SCM_UNLIKELY (!jump_table))
100 {
101 int i;
f775e51b 102 jump_table = malloc (SCM_VM_NUM_INSTRUCTIONS * sizeof(void*));
53e28ed9
AW
103 for (i = 0; i < SCM_VM_NUM_INSTRUCTIONS; i++)
104 jump_table[i] = &&vm_error_bad_instruction;
105#define VM_INSTRUCTION_TO_LABEL 1
aeeff258
AW
106#include <libguile/vm-expand.h>
107#include <libguile/vm-i-system.i>
108#include <libguile/vm-i-scheme.i>
109#include <libguile/vm-i-loader.i>
53e28ed9
AW
110#undef VM_INSTRUCTION_TO_LABEL
111 }
112#endif
113
3d5ee0cd
KN
114 /* Initialization */
115 {
499a4c07
KN
116 SCM prog = program;
117
118 /* Boot program */
6d14383e 119 program = vm_make_boot_program (nargs);
a98cef7e 120
e06e857c
AW
121#if VM_PUSH_DEBUG_FRAMES
122 debug.prev = scm_i_last_debug_frame ();
2f9769b6
AW
123 debug.status = SCM_APPLYFRAME;
124 debug.vect = &debug_vect_body;
125 debug.vect[0].a.proc = program; /* the boot program */
126 debug.vect[0].a.args = SCM_EOL;
127 scm_i_set_last_debug_frame (&debug);
e06e857c
AW
128#endif
129
3d5ee0cd
KN
130 /* Initial frame */
131 CACHE_REGISTER ();
499a4c07 132 CACHE_PROGRAM ();
3616e9e9 133 PUSH (program);
3d5ee0cd 134 NEW_FRAME ();
17e90c5e 135
3d5ee0cd 136 /* Initial arguments */
3616e9e9 137 PUSH (prog);
6d14383e
AW
138 if (SCM_UNLIKELY (sp + nargs >= stack_limit))
139 goto vm_error_too_many_args;
140 while (nargs--)
141 PUSH (*argv++);
3d5ee0cd 142 }
a98cef7e
KN
143
144 /* Let's go! */
17e90c5e 145 BOOT_HOOK ();
53e28ed9 146 NEXT;
a98cef7e
KN
147
148#ifndef HAVE_LABELS_AS_VALUES
17e90c5e 149 vm_start:
53e28ed9 150 switch ((*ip++) & SCM_VM_INSTRUCTION_MASK) {
a98cef7e
KN
151#endif
152
83495480
AW
153#include "vm-expand.h"
154#include "vm-i-system.c"
155#include "vm-i-scheme.c"
156#include "vm-i-loader.c"
a98cef7e
KN
157
158#ifndef HAVE_LABELS_AS_VALUES
53e28ed9
AW
159 default:
160 goto vm_error_bad_instruction;
a98cef7e
KN
161 }
162#endif
163
e06e857c
AW
164
165 vm_done:
166 SYNC_ALL ();
167#if VM_PUSH_DEBUG_FRAMES
2f9769b6 168 scm_i_set_last_debug_frame (debug.prev);
e06e857c
AW
169#endif
170 return finish_args;
171
17e90c5e
KN
172 /* Errors */
173 {
e06e857c
AW
174 SCM err_msg;
175
53e28ed9
AW
176 vm_error_bad_instruction:
177 err_msg = scm_from_locale_string ("VM: Bad instruction: ~A");
da8b4747 178 finish_args = scm_list_1 (scm_from_uchar (ip[-1]));
53e28ed9
AW
179 goto vm_error;
180
17e90c5e 181 vm_error_unbound:
fa19602c 182 err_msg = scm_from_locale_string ("VM: Unbound variable: ~A");
17e90c5e
KN
183 goto vm_error;
184
4c9ad01d 185 vm_error_wrong_type_arg:
fa19602c 186 err_msg = scm_from_locale_string ("VM: Wrong type argument");
e06e857c 187 finish_args = SCM_EOL;
4c9ad01d
KN
188 goto vm_error;
189
6d14383e
AW
190 vm_error_too_many_args:
191 err_msg = scm_from_locale_string ("VM: Too many arguments");
da8b4747 192 finish_args = scm_list_1 (scm_from_int (nargs));
6d14383e
AW
193 goto vm_error;
194
17e90c5e 195 vm_error_wrong_num_args:
9a8cc8e7 196 /* nargs and program are valid */
0570c3f1 197 SYNC_ALL ();
9a8cc8e7
AW
198 scm_wrong_num_args (program);
199 /* shouldn't get here */
17e90c5e
KN
200 goto vm_error;
201
202 vm_error_wrong_type_apply:
0b5f0e49
LC
203 err_msg = scm_from_locale_string ("VM: Wrong type to apply: ~S "
204 "[IP offset: ~a]");
da8b4747
LC
205 finish_args = scm_list_2 (program,
206 SCM_I_MAKINUM (ip - bp->base));
17e90c5e
KN
207 goto vm_error;
208
ac02b386 209 vm_error_stack_overflow:
fa19602c 210 err_msg = scm_from_locale_string ("VM: Stack overflow");
e06e857c 211 finish_args = SCM_EOL;
17e90c5e 212 goto vm_error;
17e90c5e 213
ac02b386 214 vm_error_stack_underflow:
fa19602c 215 err_msg = scm_from_locale_string ("VM: Stack underflow");
e06e857c 216 finish_args = SCM_EOL;
17e90c5e
KN
217 goto vm_error;
218
1f40459f
AW
219 vm_error_improper_list:
220 err_msg = scm_from_locale_string ("VM: Attempt to unroll an improper list: tail is ~A");
221 goto vm_error;
222
5e390de6
AW
223 vm_error_not_a_pair:
224 SYNC_ALL ();
e06e857c 225 scm_wrong_type_arg_msg (FUNC_NAME, 1, finish_args, "pair");
5e390de6
AW
226 /* shouldn't get here */
227 goto vm_error;
228
a222b0fa
AW
229 vm_error_no_values:
230 err_msg = scm_from_locale_string ("VM: 0-valued return");
e06e857c 231 finish_args = SCM_EOL;
a222b0fa
AW
232 goto vm_error;
233
d51406fe
AW
234 vm_error_not_enough_values:
235 err_msg = scm_from_locale_string ("VM: Not enough values for mv-bind");
e06e857c 236 finish_args = SCM_EOL;
d51406fe
AW
237 goto vm_error;
238
fd358575
AW
239 vm_error_no_such_module:
240 err_msg = scm_from_locale_string ("VM: No such module: ~A");
241 goto vm_error;
242
ac02b386
KN
243#if VM_CHECK_IP
244 vm_error_invalid_address:
fa19602c 245 err_msg = scm_from_locale_string ("VM: Invalid program address");
e06e857c 246 finish_args = SCM_EOL;
17e90c5e 247 goto vm_error;
ac02b386
KN
248#endif
249
250#if VM_CHECK_EXTERNAL
251 vm_error_external:
fa19602c 252 err_msg = scm_from_locale_string ("VM: Invalid external access");
e06e857c 253 finish_args = SCM_EOL;
ac02b386
KN
254 goto vm_error;
255#endif
17e90c5e 256
0b5f0e49
LC
257#if VM_CHECK_OBJECT
258 vm_error_object:
259 err_msg = scm_from_locale_string ("VM: Invalid object table access");
e06e857c 260 finish_args = SCM_EOL;
0b5f0e49
LC
261 goto vm_error;
262#endif
263
17e90c5e
KN
264 vm_error:
265 SYNC_ALL ();
a52b2d3d 266
da8b4747
LC
267 scm_ithrow (sym_vm_error, scm_list_3 (sym_vm_run, err_msg, finish_args),
268 1);
17e90c5e
KN
269 }
270
a98cef7e
KN
271 abort (); /* never reached */
272}
6d14383e
AW
273
274#undef VM_USE_HOOKS
275#undef VM_USE_CLOCK
276#undef VM_CHECK_EXTERNAL
277#undef VM_CHECK_OBJECT
e06e857c 278#undef VM_PUSH_DEBUG_FRAMES
17e90c5e
KN
279
280/*
281 Local Variables:
282 c-file-style: "gnu"
283 End:
284*/