make pre-inst-guile use pre-inst-guile-env
[bpt/guile.git] / src / 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
17e90c5e 42/* This file is included in vm.c twice */
a98cef7e
KN
43
44#include "vm_engine.h"
45
238e7a11 46
a98cef7e 47static SCM
41f248a8 48vm_run (SCM vm, SCM program, SCM args)
a98cef7e
KN
49#define FUNC_NAME "vm-engine"
50{
17e90c5e
KN
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 */
a98cef7e 55
d608d68d 56 /* Cache variables */
3d5ee0cd 57 struct scm_vm *vp = SCM_VM_DATA (vm); /* VM data pointer */
17e90c5e 58 struct scm_program *bp = NULL; /* program base pointer */
41f248a8 59 SCM external = SCM_EOL; /* external environment */
17e90c5e 60 SCM *objects = NULL; /* constant objects */
a52b2d3d 61 scm_t_array_handle objects_handle; /* handle of the OBJECTS array */
0b5f0e49 62 size_t object_count; /* length of OBJECTS */
3d5ee0cd
KN
63 SCM *stack_base = vp->stack_base; /* stack base address */
64 SCM *stack_limit = vp->stack_limit; /* stack limit address */
a98cef7e 65
d608d68d 66 /* Internal variables */
17e90c5e 67 int nargs = 0;
3d5ee0cd 68 long start_time = scm_c_get_internal_run_time ();
17e90c5e
KN
69 // SCM dynwinds = SCM_EOL;
70 SCM err_msg;
71 SCM err_args;
72#if VM_USE_HOOKS
a98cef7e
KN
73 SCM hook_args = SCM_LIST1 (vm);
74#endif
17d1b4bf
AW
75 struct vm_unwind_data wind_data;
76
77 /* dynwind ended in the halt instruction */
78 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
79 wind_data.vp = vp;
80 wind_data.sp = vp->sp;
81 wind_data.fp = vp->fp;
82 wind_data.this_frame = vp->this_frame;
83 scm_dynwind_unwind_handler (vm_reset_stack, &wind_data, 0);
84
a98cef7e 85
17e90c5e 86#ifdef HAVE_LABELS_AS_VALUES
2d80426a 87 /* Jump table */
4b482259 88 static void *jump_table[] = {
17e90c5e 89#define VM_INSTRUCTION_TO_LABEL 1
77c04abe
KN
90#include "vm_expand.h"
91#include "vm_system.i"
92#include "vm_scheme.i"
17e90c5e 93#include "vm_loader.i"
77c04abe 94#undef VM_INSTRUCTION_TO_LABEL
4b482259 95 };
17e90c5e 96#endif
a98cef7e 97
3d5ee0cd
KN
98 /* Initialization */
99 {
499a4c07
KN
100 SCM prog = program;
101
102 /* Boot program */
3d5ee0cd 103 scm_byte_t bytes[3] = {scm_op_call, 0, scm_op_halt};
ac99cb0c 104 bytes[1] = scm_ilength (args); /* FIXME: argument overflow */
29711eb9 105 program = scm_c_make_program (bytes, 3, SCM_BOOL_F);
a98cef7e 106
3d5ee0cd
KN
107 /* Initial frame */
108 CACHE_REGISTER ();
499a4c07 109 CACHE_PROGRAM ();
3616e9e9 110 PUSH (program);
3d5ee0cd 111 NEW_FRAME ();
17e90c5e 112
3d5ee0cd 113 /* Initial arguments */
3616e9e9 114 PUSH (prog);
3d5ee0cd
KN
115 for (; !SCM_NULLP (args); args = SCM_CDR (args))
116 PUSH (SCM_CAR (args));
3d5ee0cd 117 }
a98cef7e
KN
118
119 /* Let's go! */
17e90c5e 120 BOOT_HOOK ();
a98cef7e
KN
121
122#ifndef HAVE_LABELS_AS_VALUES
17e90c5e
KN
123 vm_start:
124 switch (*ip++) {
a98cef7e
KN
125#endif
126
77c04abe 127#include "vm_expand.h"
a98cef7e
KN
128#include "vm_system.c"
129#include "vm_scheme.c"
17e90c5e 130#include "vm_loader.c"
a98cef7e
KN
131
132#ifndef HAVE_LABELS_AS_VALUES
133 }
134#endif
135
17e90c5e
KN
136 /* Errors */
137 {
138 vm_error_unbound:
fa19602c 139 err_msg = scm_from_locale_string ("VM: Unbound variable: ~A");
17e90c5e
KN
140 goto vm_error;
141
4c9ad01d 142 vm_error_wrong_type_arg:
fa19602c 143 err_msg = scm_from_locale_string ("VM: Wrong type argument");
4c9ad01d
KN
144 err_args = SCM_EOL;
145 goto vm_error;
146
17e90c5e 147 vm_error_wrong_num_args:
fa19602c 148 err_msg = scm_from_locale_string ("VM: Wrong number of arguments");
17e90c5e
KN
149 err_args = SCM_EOL;
150 goto vm_error;
151
152 vm_error_wrong_type_apply:
0b5f0e49
LC
153 err_msg = scm_from_locale_string ("VM: Wrong type to apply: ~S "
154 "[IP offset: ~a]");
155 err_args = SCM_LIST2 (program,
156 SCM_I_MAKINUM (ip - bp->base));
17e90c5e
KN
157 goto vm_error;
158
ac02b386 159 vm_error_stack_overflow:
fa19602c 160 err_msg = scm_from_locale_string ("VM: Stack overflow");
17e90c5e
KN
161 err_args = SCM_EOL;
162 goto vm_error;
17e90c5e 163
ac02b386 164 vm_error_stack_underflow:
fa19602c 165 err_msg = scm_from_locale_string ("VM: Stack underflow");
17e90c5e
KN
166 err_args = SCM_EOL;
167 goto vm_error;
168
ac02b386
KN
169#if VM_CHECK_IP
170 vm_error_invalid_address:
fa19602c 171 err_msg = scm_from_locale_string ("VM: Invalid program address");
17e90c5e
KN
172 err_args = SCM_EOL;
173 goto vm_error;
ac02b386
KN
174#endif
175
176#if VM_CHECK_EXTERNAL
177 vm_error_external:
fa19602c 178 err_msg = scm_from_locale_string ("VM: Invalid external access");
ac02b386
KN
179 err_args = SCM_EOL;
180 goto vm_error;
181#endif
17e90c5e 182
0b5f0e49
LC
183#if VM_CHECK_OBJECT
184 vm_error_object:
185 err_msg = scm_from_locale_string ("VM: Invalid object table access");
186 err_args = SCM_EOL;
187 goto vm_error;
188#endif
189
17e90c5e
KN
190 vm_error:
191 SYNC_ALL ();
a52b2d3d
LC
192 if (objects)
193 scm_array_handle_release (&objects_handle);
194
ac99cb0c 195 scm_ithrow (sym_vm_error, SCM_LIST3 (sym_vm_run, err_msg, err_args), 1);
17e90c5e
KN
196 }
197
a98cef7e
KN
198 abort (); /* never reached */
199}
200#undef FUNC_NAME
17e90c5e
KN
201
202/*
203 Local Variables:
204 c-file-style: "gnu"
205 End:
206*/