eval-elisp uses primitive-eval
[bpt/guile.git] / libguile / vm-engine.c
CommitLineData
e6eb2467 1/* Copyright (C) 2001, 2009 Free Software Foundation, Inc.
a98cef7e 2 *
560b9c25 3 * This library is free software; you can redistribute it and/or
53befeb7
NJ
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
a98cef7e 7 *
53befeb7
NJ
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
560b9c25
AW
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
a98cef7e 12 *
560b9c25
AW
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
53befeb7
NJ
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
560b9c25 17 */
a98cef7e 18
6d14383e
AW
19/* This file is included in vm.c multiple times */
20
21#if (VM_ENGINE == SCM_VM_REGULAR_ENGINE)
22#define VM_USE_HOOKS 0 /* Various hooks */
23#define VM_USE_CLOCK 0 /* Bogoclock */
6d14383e 24#define VM_CHECK_OBJECT 1 /* Check object table */
57ab0671 25#define VM_CHECK_FREE_VARIABLES 1 /* Check free variable access */
e06e857c 26#define VM_PUSH_DEBUG_FRAMES 0 /* Push frames onto the evaluator debug stack */
6d14383e
AW
27#elif (VM_ENGINE == SCM_VM_DEBUG_ENGINE)
28#define VM_USE_HOOKS 1
29#define VM_USE_CLOCK 1
6d14383e 30#define VM_CHECK_OBJECT 1
57ab0671 31#define VM_CHECK_FREE_VARIABLES 1
e06e857c 32#define VM_PUSH_DEBUG_FRAMES 1
6d14383e
AW
33#else
34#error unknown debug engine VM_ENGINE
35#endif
a98cef7e 36
83495480 37#include "vm-engine.h"
a98cef7e 38
238e7a11 39
a98cef7e 40static SCM
6d14383e 41VM_NAME (struct scm_vm *vp, SCM program, SCM *argv, int nargs)
a98cef7e 42{
17e90c5e
KN
43 /* VM registers */
44 register scm_byte_t *ip IP_REG; /* instruction pointer */
45 register SCM *sp SP_REG; /* stack pointer */
46 register SCM *fp FP_REG; /* frame pointer */
a98cef7e 47
d608d68d 48 /* Cache variables */
53e28ed9 49 struct scm_objcode *bp = NULL; /* program base pointer */
57ab0671
AW
50 SCM *free_vars = NULL; /* free variables */
51 size_t free_vars_count = 0; /* length of FREE_VARS */
17e90c5e 52 SCM *objects = NULL; /* constant objects */
2fda0242 53 size_t object_count = 0; /* length of OBJECTS */
3d5ee0cd
KN
54 SCM *stack_base = vp->stack_base; /* stack base address */
55 SCM *stack_limit = vp->stack_limit; /* stack limit address */
a98cef7e 56
d608d68d 57 /* Internal variables */
ef24c01b 58 int nvalues = 0;
3d5ee0cd 59 long start_time = scm_c_get_internal_run_time ();
e06e857c
AW
60 SCM finish_args; /* used both for returns: both in error
61 and normal situations */
17e90c5e 62#if VM_USE_HOOKS
6d14383e 63 SCM hook_args = SCM_EOL;
a98cef7e 64#endif
17d1b4bf 65
53e28ed9
AW
66#ifdef HAVE_LABELS_AS_VALUES
67 static void **jump_table = NULL;
e06e857c 68#endif
53e28ed9 69
e06e857c
AW
70#if VM_PUSH_DEBUG_FRAMES
71 scm_t_debug_frame debug;
72 scm_t_debug_info debug_vect_body;
73 debug.status = SCM_VOIDFRAME;
74#endif
75
76#ifdef HAVE_LABELS_AS_VALUES
53e28ed9
AW
77 if (SCM_UNLIKELY (!jump_table))
78 {
79 int i;
f775e51b 80 jump_table = malloc (SCM_VM_NUM_INSTRUCTIONS * sizeof(void*));
53e28ed9
AW
81 for (i = 0; i < SCM_VM_NUM_INSTRUCTIONS; i++)
82 jump_table[i] = &&vm_error_bad_instruction;
83#define VM_INSTRUCTION_TO_LABEL 1
aeeff258
AW
84#include <libguile/vm-expand.h>
85#include <libguile/vm-i-system.i>
86#include <libguile/vm-i-scheme.i>
87#include <libguile/vm-i-loader.i>
53e28ed9
AW
88#undef VM_INSTRUCTION_TO_LABEL
89 }
90#endif
91
3d5ee0cd
KN
92 /* Initialization */
93 {
499a4c07
KN
94 SCM prog = program;
95
96 /* Boot program */
6d14383e 97 program = vm_make_boot_program (nargs);
a98cef7e 98
e06e857c
AW
99#if VM_PUSH_DEBUG_FRAMES
100 debug.prev = scm_i_last_debug_frame ();
2f9769b6
AW
101 debug.status = SCM_APPLYFRAME;
102 debug.vect = &debug_vect_body;
103 debug.vect[0].a.proc = program; /* the boot program */
104 debug.vect[0].a.args = SCM_EOL;
105 scm_i_set_last_debug_frame (&debug);
e06e857c
AW
106#endif
107
3d5ee0cd
KN
108 /* Initial frame */
109 CACHE_REGISTER ();
03e6c165
AW
110 PUSH ((SCM)fp); /* dynamic link */
111 PUSH (0); /* ra */
112 PUSH (0); /* mvra */
499a4c07 113 CACHE_PROGRAM ();
3616e9e9 114 PUSH (program);
03e6c165
AW
115 fp = sp + 1;
116 INIT_FRAME ();
b7946e9e
AW
117 /* MV-call frame, function & arguments */
118 PUSH ((SCM)fp); /* dynamic link */
119 PUSH (0); /* ra */
120 PUSH (0); /* mvra */
3616e9e9 121 PUSH (prog);
6d14383e
AW
122 if (SCM_UNLIKELY (sp + nargs >= stack_limit))
123 goto vm_error_too_many_args;
124 while (nargs--)
125 PUSH (*argv++);
3d5ee0cd 126 }
a98cef7e
KN
127
128 /* Let's go! */
17e90c5e 129 BOOT_HOOK ();
53e28ed9 130 NEXT;
a98cef7e
KN
131
132#ifndef HAVE_LABELS_AS_VALUES
17e90c5e 133 vm_start:
53e28ed9 134 switch ((*ip++) & SCM_VM_INSTRUCTION_MASK) {
a98cef7e
KN
135#endif
136
83495480
AW
137#include "vm-expand.h"
138#include "vm-i-system.c"
139#include "vm-i-scheme.c"
140#include "vm-i-loader.c"
a98cef7e
KN
141
142#ifndef HAVE_LABELS_AS_VALUES
53e28ed9
AW
143 default:
144 goto vm_error_bad_instruction;
a98cef7e
KN
145 }
146#endif
147
e06e857c
AW
148
149 vm_done:
150 SYNC_ALL ();
151#if VM_PUSH_DEBUG_FRAMES
2f9769b6 152 scm_i_set_last_debug_frame (debug.prev);
e06e857c
AW
153#endif
154 return finish_args;
155
17e90c5e
KN
156 /* Errors */
157 {
e06e857c
AW
158 SCM err_msg;
159
53e28ed9 160 vm_error_bad_instruction:
7ea9a0a7 161 err_msg = scm_from_locale_string ("VM: Bad instruction: ~s");
da8b4747 162 finish_args = scm_list_1 (scm_from_uchar (ip[-1]));
53e28ed9
AW
163 goto vm_error;
164
17e90c5e 165 vm_error_unbound:
7ea9a0a7 166 err_msg = scm_from_locale_string ("VM: Unbound variable: ~s");
17e90c5e
KN
167 goto vm_error;
168
4c9ad01d 169 vm_error_wrong_type_arg:
fa19602c 170 err_msg = scm_from_locale_string ("VM: Wrong type argument");
e06e857c 171 finish_args = SCM_EOL;
4c9ad01d
KN
172 goto vm_error;
173
6d14383e
AW
174 vm_error_too_many_args:
175 err_msg = scm_from_locale_string ("VM: Too many arguments");
da8b4747 176 finish_args = scm_list_1 (scm_from_int (nargs));
6d14383e
AW
177 goto vm_error;
178
17e90c5e 179 vm_error_wrong_num_args:
9a8cc8e7 180 /* nargs and program are valid */
0570c3f1 181 SYNC_ALL ();
9a8cc8e7
AW
182 scm_wrong_num_args (program);
183 /* shouldn't get here */
17e90c5e
KN
184 goto vm_error;
185
186 vm_error_wrong_type_apply:
7ea9a0a7
AW
187 SYNC_ALL ();
188 scm_error (scm_misc_error_key, FUNC_NAME, "Wrong type to apply: ~S",
189 scm_list_1 (program), SCM_BOOL_F);
17e90c5e
KN
190 goto vm_error;
191
ac02b386 192 vm_error_stack_overflow:
fa19602c 193 err_msg = scm_from_locale_string ("VM: Stack overflow");
e06e857c 194 finish_args = SCM_EOL;
17e90c5e 195 goto vm_error;
17e90c5e 196
ac02b386 197 vm_error_stack_underflow:
fa19602c 198 err_msg = scm_from_locale_string ("VM: Stack underflow");
e06e857c 199 finish_args = SCM_EOL;
17e90c5e
KN
200 goto vm_error;
201
1f40459f 202 vm_error_improper_list:
7ea9a0a7 203 err_msg = scm_from_locale_string ("Expected a proper list, but got object with tail ~s");
1f40459f
AW
204 goto vm_error;
205
5e390de6
AW
206 vm_error_not_a_pair:
207 SYNC_ALL ();
e06e857c 208 scm_wrong_type_arg_msg (FUNC_NAME, 1, finish_args, "pair");
5e390de6
AW
209 /* shouldn't get here */
210 goto vm_error;
211
e6eb2467
AW
212 vm_error_not_a_bytevector:
213 SYNC_ALL ();
214 scm_wrong_type_arg_msg (FUNC_NAME, 1, finish_args, "bytevector");
215 /* shouldn't get here */
216 goto vm_error;
217
a222b0fa 218 vm_error_no_values:
7ea9a0a7 219 err_msg = scm_from_locale_string ("Zero values returned to single-valued continuation");
e06e857c 220 finish_args = SCM_EOL;
a222b0fa
AW
221 goto vm_error;
222
d51406fe 223 vm_error_not_enough_values:
7ea9a0a7 224 err_msg = scm_from_locale_string ("Too few values returned to continuation");
e06e857c 225 finish_args = SCM_EOL;
d51406fe
AW
226 goto vm_error;
227
94ff26b9
AW
228 vm_error_bad_wide_string_length:
229 err_msg = scm_from_locale_string ("VM: Bad wide string length: ~S");
230 goto vm_error;
231
ac02b386
KN
232#if VM_CHECK_IP
233 vm_error_invalid_address:
fa19602c 234 err_msg = scm_from_locale_string ("VM: Invalid program address");
e06e857c 235 finish_args = SCM_EOL;
17e90c5e 236 goto vm_error;
ac02b386
KN
237#endif
238
0b5f0e49
LC
239#if VM_CHECK_OBJECT
240 vm_error_object:
241 err_msg = scm_from_locale_string ("VM: Invalid object table access");
e06e857c 242 finish_args = SCM_EOL;
0b5f0e49
LC
243 goto vm_error;
244#endif
245
57ab0671
AW
246#if VM_CHECK_FREE_VARIABLES
247 vm_error_free_variable:
248 err_msg = scm_from_locale_string ("VM: Invalid free variable access");
8d90b356
AW
249 finish_args = SCM_EOL;
250 goto vm_error;
251#endif
252
17e90c5e
KN
253 vm_error:
254 SYNC_ALL ();
a52b2d3d 255
da8b4747
LC
256 scm_ithrow (sym_vm_error, scm_list_3 (sym_vm_run, err_msg, finish_args),
257 1);
17e90c5e
KN
258 }
259
a98cef7e
KN
260 abort (); /* never reached */
261}
6d14383e
AW
262
263#undef VM_USE_HOOKS
264#undef VM_USE_CLOCK
6d14383e 265#undef VM_CHECK_OBJECT
57ab0671 266#undef VM_CHECK_FREE_VARIABLE
e06e857c 267#undef VM_PUSH_DEBUG_FRAMES
17e90c5e
KN
268
269/*
270 Local Variables:
271 c-file-style: "gnu"
272 End:
273*/