*** empty log message ***
[bpt/guile.git] / src / vm_engine.h
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
42/* This file is included in vm_engine.c */
43
44/*
3d5ee0cd 45 * Options
a98cef7e
KN
46 */
47
ac02b386
KN
48#define VM_USE_HOOKS 1 /* Various hooks */
49#define VM_USE_CLOCK 1 /* Bogoclock */
50#define VM_CHECK_EXTERNAL 1 /* Check external link */
a98cef7e
KN
51
52\f
53/*
17e90c5e 54 * Registers
a98cef7e
KN
55 */
56
17e90c5e 57/* Register optimization. [ stolen from librep/src/lispmach.h,v 1.3 ]
9df03fd0 58
17e90c5e
KN
59 Some compilers underestimate the use of the local variables representing
60 the abstract machine registers, and don't put them in hardware registers,
61 which slows down the interpreter considerably.
62 For GCC, I have hand-assigned hardware registers for several architectures.
63*/
9df03fd0 64
17e90c5e
KN
65#ifdef __GNUC__
66#ifdef __mips__
67#define IP_REG asm("$16")
68#define SP_REG asm("$17")
69#define FP_REG asm("$18")
70#endif
71#ifdef __sparc__
72#define IP_REG asm("%l0")
73#define SP_REG asm("%l1")
74#define FP_REG asm("%l2")
75#endif
76#ifdef __alpha__
77#ifdef __CRAY__
78#define IP_REG asm("r9")
79#define SP_REG asm("r10")
80#define FP_REG asm("r11")
9df03fd0 81#else
17e90c5e
KN
82#define IP_REG asm("$9")
83#define SP_REG asm("$10")
84#define FP_REG asm("$11")
85#endif
86#endif
87#ifdef __i386__
88#define IP_REG asm("%esi")
89#define SP_REG asm("%edi")
90#define FP_REG
91#endif
92#if defined(PPC) || defined(_POWER) || defined(_IBMR2)
93#define IP_REG asm("26")
94#define SP_REG asm("27")
95#define FP_REG asm("28")
96#endif
97#ifdef __hppa__
98#define IP_REG asm("%r18")
99#define SP_REG asm("%r17")
100#define FP_REG asm("%r16")
101#endif
102#ifdef __mc68000__
103#define IP_REG asm("a5")
104#define SP_REG asm("a4")
105#define FP_REG
106#endif
107#ifdef __arm__
108#define IP_REG asm("r9")
109#define SP_REG asm("r8")
110#define FP_REG asm("r7")
111#endif
9df03fd0
KN
112#endif
113
114\f
a98cef7e 115/*
3d5ee0cd 116 * Cache/Sync
a98cef7e
KN
117 */
118
3d5ee0cd 119#define CACHE_REGISTER() \
17e90c5e 120{ \
3d5ee0cd
KN
121 ip = vp->ip; \
122 sp = vp->sp; \
123 fp = vp->fp; \
17e90c5e 124}
a98cef7e 125
3d5ee0cd 126#define SYNC_REGISTER() \
a98cef7e 127{ \
3d5ee0cd
KN
128 vp->ip = ip; \
129 vp->sp = sp; \
130 vp->fp = fp; \
a98cef7e
KN
131}
132
499a4c07 133#define CACHE_PROGRAM() \
a98cef7e 134{ \
3d5ee0cd 135 bp = SCM_PROGRAM_DATA (program); \
41f248a8
KN
136 objects = SCM_VELTS (bp->objs); \
137}
138
3d5ee0cd
KN
139#define SYNC_BEFORE_GC() \
140{ \
141 SYNC_REGISTER (); \
17e90c5e 142}
a98cef7e 143
17e90c5e 144#define SYNC_ALL() \
a98cef7e 145{ \
3d5ee0cd 146 SYNC_REGISTER (); \
a98cef7e
KN
147}
148
a98cef7e 149\f
ac02b386
KN
150/*
151 * Error check
152 */
153
154#undef CHECK_EXTERNAL
155#if VM_CHECK_EXTERNAL
156#define CHECK_EXTERNAL(e) \
157 do { if (!SCM_CONSP (e)) goto vm_error_external; } while (0)
158#else
159#define CHECK_EXTERNAL(e)
160#endif
161
162\f
3d5ee0cd
KN
163/*
164 * Hooks
165 */
166
167#undef RUN_HOOK
168#if VM_USE_HOOKS
169#define RUN_HOOK(h) \
170{ \
ac02b386 171 if (!SCM_FALSEP (vp->hooks[h])) \
3d5ee0cd 172 { \
af988bbf
KN
173 SYNC_REGISTER (); \
174 vm_heapify_frames (vm); \
ac02b386 175 scm_c_run_hook (vp->hooks[h], hook_args); \
af988bbf 176 CACHE_REGISTER (); \
3d5ee0cd
KN
177 } \
178}
179#else
180#define RUN_HOOK(h)
181#endif
182
ac02b386
KN
183#define BOOT_HOOK() RUN_HOOK (SCM_VM_BOOT_HOOK)
184#define HALT_HOOK() RUN_HOOK (SCM_VM_HALT_HOOK)
185#define NEXT_HOOK() RUN_HOOK (SCM_VM_NEXT_HOOK)
186#define ENTER_HOOK() RUN_HOOK (SCM_VM_ENTER_HOOK)
187#define APPLY_HOOK() RUN_HOOK (SCM_VM_APPLY_HOOK)
188#define EXIT_HOOK() RUN_HOOK (SCM_VM_EXIT_HOOK)
189#define RETURN_HOOK() RUN_HOOK (SCM_VM_RETURN_HOOK)
3d5ee0cd
KN
190
191\f
a98cef7e
KN
192/*
193 * Stack operation
194 */
195
17e90c5e 196#define CHECK_OVERFLOW() \
3616e9e9 197 if (sp > stack_limit) \
17e90c5e
KN
198 goto vm_error_stack_overflow
199
200#define CHECK_UNDERFLOW() \
3616e9e9 201 if (sp < stack_base) \
17e90c5e 202 goto vm_error_stack_underflow
a98cef7e 203
3616e9e9
KN
204#define PUSH(x) do { sp++; CHECK_OVERFLOW (); *sp = x; } while (0)
205#define DROP() do { CHECK_UNDERFLOW (); sp--; } while (0)
17e90c5e
KN
206#define POP(x) do { x = *sp; DROP (); } while (0)
207
208#define CONS(x,y,z) \
a98cef7e 209{ \
17e90c5e 210 SCM cell; \
3d5ee0cd 211 SYNC_BEFORE_GC (); \
17e90c5e
KN
212 SCM_NEWCELL (cell); \
213 SCM_SET_CELL_OBJECT_0 (cell, y); \
214 SCM_SET_CELL_OBJECT_1 (cell, z); \
215 x = cell; \
a98cef7e
KN
216}
217
17e90c5e
KN
218#define POP_LIST(n) \
219do { \
220 int i; \
221 SCM l = SCM_EOL; \
3616e9e9
KN
222 sp -= n; \
223 for (i = n; i; i--) \
17e90c5e 224 CONS (l, sp[i], l); \
3616e9e9 225 PUSH (l); \
17e90c5e
KN
226} while (0)
227
cb4cca12
KN
228#define POP_LIST_MARK() \
229do { \
230 SCM o; \
231 SCM l = SCM_EOL; \
232 POP (o); \
233 while (!SCM_UNBNDP (o)) \
234 { \
235 CONS (l, o, l); \
236 POP (o); \
237 } \
238 PUSH (l); \
239} while (0)
240
a98cef7e
KN
241\f
242/*
17e90c5e 243 * Instruction operation
a98cef7e
KN
244 */
245
17e90c5e 246#define FETCH() (*ip++)
17e90c5e
KN
247#define FETCH_LENGTH(len) do { ip = vm_fetch_length (ip, &len); } while (0)
248
249#undef CLOCK
250#if VM_USE_CLOCK
3d5ee0cd 251#define CLOCK(n) vp->clock += n
a98cef7e 252#else
17e90c5e 253#define CLOCK(n)
a98cef7e
KN
254#endif
255
17e90c5e
KN
256#undef NEXT_JUMP
257#ifdef HAVE_LABELS_AS_VALUES
258#define NEXT_JUMP() goto *jump_table[FETCH ()]
259#else
260#define NEXT_JUMP() goto vm_start
261#endif
262
263#define NEXT \
264{ \
265 CLOCK (1); \
17e90c5e
KN
266 NEXT_HOOK (); \
267 NEXT_JUMP (); \
a98cef7e
KN
268}
269
270\f
271/*
ac02b386 272 * Stack frame
17e90c5e
KN
273 */
274
17e90c5e
KN
275#define INIT_ARGS() \
276{ \
277 if (bp->nrest) \
278 { \
5315b862 279 int n = nargs - (bp->nargs - 1); \
17e90c5e
KN
280 if (n < 0) \
281 goto vm_error_wrong_num_args; \
282 POP_LIST (n); \
283 } \
284 else \
285 { \
286 if (nargs != bp->nargs) \
287 goto vm_error_wrong_num_args; \
288 } \
289}
290
ac99cb0c 291/* See frames.h for the layout of stack frames */
3616e9e9
KN
292
293#define NEW_FRAME() \
294{ \
24aa2715 295 int i; \
af988bbf
KN
296 SCM ra = SCM_PACK (ip); \
297 SCM dl = SCM_PACK (fp); \
24aa2715
KN
298 SCM *p = sp + 1; \
299 SCM *q = p + bp->nlocs; \
300 \
301 /* New pointers */ \
3616e9e9 302 ip = bp->base; \
24aa2715 303 fp = p - bp->nargs; \
af988bbf 304 sp = q + 3; \
3616e9e9 305 CHECK_OVERFLOW (); \
24aa2715
KN
306 \
307 /* Init local variables */ \
308 for (; p < q; p++) \
309 *p = SCM_UNDEFINED; \
310 \
311 /* Create external variables */ \
312 external = bp->external; \
313 for (i = 0; i < bp->nexts; i++) \
314 CONS (external, SCM_UNDEFINED, external); \
ac02b386
KN
315 \
316 /* Set frame data */ \
af988bbf
KN
317 p[3] = ra; \
318 p[2] = dl; \
319 p[1] = SCM_BOOL_F; \
24aa2715 320 p[0] = external; \
3616e9e9
KN
321}
322
323#define FREE_FRAME() \
324{ \
46f215f8
KN
325 SCM *last_sp = sp; \
326 SCM *last_fp = fp; \
f21dfea6 327 SCM *p = fp + bp->nargs + bp->nlocs; \
46f215f8
KN
328 \
329 /* Restore pointers */ \
330 ip = SCM_FRAME_BYTE_CAST (p[3]); \
331 fp = SCM_FRAME_STACK_CAST (p[2]); \
332 \
af988bbf 333 if (!SCM_FALSEP (p[1])) \
46f215f8
KN
334 { \
335 /* Unlink the heap stack */ \
336 vp->this_frame = p[1]; \
337 } \
af988bbf 338 else \
46f215f8
KN
339 { \
340 /* Move stack items */ \
341 p += 4; \
342 sp = SCM_FRAME_LOWER_ADDRESS (last_fp); \
343 while (p <= last_sp) \
344 *sp++ = *p++; \
345 sp--; \
346 } \
3616e9e9
KN
347}
348
af988bbf
KN
349#define CACHE_EXTERNAL() external = fp[bp->nargs + bp->nlocs]
350
ac02b386
KN
351\f
352/*
353 * Function support
354 */
355
356#define ARGS1(a1) SCM a1 = sp[0];
357#define ARGS2(a1,a2) SCM a1 = sp[-1], a2 = sp[0]; sp--;
358#define ARGS3(a1,a2,a3) SCM a1 = sp[-2], a2 = sp[-1], a3 = sp[0]; sp -= 2;
359
360#define RETURN(x) do { *sp = x; NEXT; } while (0)
361
17e90c5e
KN
362/*
363 Local Variables:
364 c-file-style: "gnu"
365 End:
366*/