1 /* Copyright (C) 2001 Free Software Foundation, Inc.
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)
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.
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
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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. */
42 /* This file is included in vm_engine.c */
48 #define VM_USE_HOOKS 1 /* Various hooks */
49 #define VM_USE_CLOCK 1 /* Bogoclock */
50 #define VM_CHECK_EXTERNAL 1 /* Check external link */
51 #define VM_CHECK_OBJECT 1 /* Check object table */
58 /* Register optimization. [ stolen from librep/src/lispmach.h,v 1.3 ]
60 Some compilers underestimate the use of the local variables representing
61 the abstract machine registers, and don't put them in hardware registers,
62 which slows down the interpreter considerably.
63 For GCC, I have hand-assigned hardware registers for several architectures.
68 #define IP_REG asm("$16")
69 #define SP_REG asm("$17")
70 #define FP_REG asm("$18")
73 #define IP_REG asm("%l0")
74 #define SP_REG asm("%l1")
75 #define FP_REG asm("%l2")
79 #define IP_REG asm("r9")
80 #define SP_REG asm("r10")
81 #define FP_REG asm("r11")
83 #define IP_REG asm("$9")
84 #define SP_REG asm("$10")
85 #define FP_REG asm("$11")
89 #define IP_REG asm("%esi")
90 #define SP_REG asm("%edi")
93 #if defined(PPC) || defined(_POWER) || defined(_IBMR2)
94 #define IP_REG asm("26")
95 #define SP_REG asm("27")
96 #define FP_REG asm("28")
99 #define IP_REG asm("%r18")
100 #define SP_REG asm("%r17")
101 #define FP_REG asm("%r16")
104 #define IP_REG asm("a5")
105 #define SP_REG asm("a4")
109 #define IP_REG asm("r9")
110 #define SP_REG asm("r8")
111 #define FP_REG asm("r7")
130 #define CACHE_REGISTER() \
137 #define SYNC_REGISTER() \
146 do { if (ip < bp->base || ip - bp->base > bp->size) abort (); } while (0)
151 /* Get a local copy of the program's "object table" (i.e. the vector of
152 external bindings that are referenced by the program), initialized by
154 /* XXX: We could instead use the "simple vector macros", thus not having to
155 call `scm_vector_writable_elements ()' and the likes. */
156 #define CACHE_PROGRAM() \
160 if (bp != SCM_PROGRAM_DATA (program)) { \
161 bp = SCM_PROGRAM_DATA (program); \
162 /* Was: objects = SCM_VELTS (bp->objs); */ \
165 scm_array_handle_release (&objects_handle); \
167 objects = scm_vector_writable_elements (bp->objs, &objects_handle, \
168 &object_count, &_vincr); \
172 #define SYNC_BEFORE_GC() \
187 #undef CHECK_EXTERNAL
188 #if VM_CHECK_EXTERNAL
189 #define CHECK_EXTERNAL(e) \
190 do { if (!SCM_CONSP (e)) goto vm_error_external; } while (0)
192 #define CHECK_EXTERNAL(e)
195 /* Accesses to a program's object table. */
197 #define CHECK_OBJECT(_num) \
198 do { if ((_num) >= object_count) goto vm_error_object; } while (0)
200 #define CHECK_OBJECT(_num)
210 #define RUN_HOOK(h) \
212 if (!SCM_FALSEP (vp->hooks[h])) \
215 vm_heapify_frames (vm); \
216 scm_c_run_hook (vp->hooks[h], hook_args); \
224 #define BOOT_HOOK() RUN_HOOK (SCM_VM_BOOT_HOOK)
225 #define HALT_HOOK() RUN_HOOK (SCM_VM_HALT_HOOK)
226 #define NEXT_HOOK() RUN_HOOK (SCM_VM_NEXT_HOOK)
227 #define BREAK_HOOK() RUN_HOOK (SCM_VM_BREAK_HOOK)
228 #define ENTER_HOOK() RUN_HOOK (SCM_VM_ENTER_HOOK)
229 #define APPLY_HOOK() RUN_HOOK (SCM_VM_APPLY_HOOK)
230 #define EXIT_HOOK() RUN_HOOK (SCM_VM_EXIT_HOOK)
231 #define RETURN_HOOK() RUN_HOOK (SCM_VM_RETURN_HOOK)
238 #define CHECK_OVERFLOW() \
239 if (sp > stack_limit) \
240 goto vm_error_stack_overflow
242 #define CHECK_UNDERFLOW() \
243 if (sp < stack_base) \
244 goto vm_error_stack_underflow;
246 #define PUSH(x) do { sp++; CHECK_OVERFLOW (); *sp = x; } while (0)
247 #define DROP() do { sp--; CHECK_UNDERFLOW (); } while (0)
248 #define DROPN(_n) do { sp -= (_n); CHECK_UNDERFLOW (); } while (0)
249 #define POP(x) do { x = *sp; DROP (); } while (0)
251 /* A fast CONS. This has to be fast since its used, for instance, by
252 POP_LIST when fetching a function's argument list. Note: `scm_cell' is an
253 inlined function in Guile 1.7. Unfortunately, it calls
254 `scm_gc_for_newcell ()' which is _not_ inlined and allocated cells on the
256 #define CONS(x,y,z) \
259 x = scm_cell (SCM_UNPACK (y), SCM_UNPACK (z)); \
262 /* Pop the N objects on top of the stack and push a list that contains
264 #define POP_LIST(n) \
270 for (i = n; i; i--) \
271 CONS (l, sp[i], l); \
276 /* Below is a (slightly broken) experiment to avoid calling `scm_cell' and to
277 allocate cells on the stack. This is a significant improvement for
278 programs which call a lot of procedures, since the procedure call
279 mechanism uses POP_LIST which normally uses `scm_cons'.
281 What it does is that it creates a list whose cells are allocated on the
282 VM's stack instead of being allocated on the heap via `scm_cell'. This is
283 much faster. However, if the callee does something like:
286 (set! the-args args))
288 then terrible things may happen since the list of arguments may be
289 overwritten later on. */
292 /* Awful hack that aligns PTR so that it can be considered as a non-immediate
294 #define ALIGN_AS_NON_IMMEDIATE(_ptr) \
296 if ((scm_t_bits)(_ptr) & 6) \
300 _incr = (scm_t_bits)(_ptr) & 6; \
301 _incr = (~_incr) & 7; \
306 #define POP_LIST_ON_STACK(n) \
317 SCM *list_head, *list; \
319 list_head = sp + 1; \
320 ALIGN_AS_NON_IMMEDIATE (list_head); \
324 for (i = 1; i <= n; i++) \
326 /* The cell's car and cdr. */ \
328 *(list + 1) = PTR2SCM (list + 2); \
332 /* The last pair's cdr is '(). */ \
335 /* Push the SCM object that points */ \
336 /* to the first cell. */ \
337 PUSH (PTR2SCM (list_head)); \
342 /* end of the experiment */
345 #define POP_LIST_MARK() \
350 while (!SCM_UNBNDP (o)) \
360 * Instruction operation
363 #define FETCH() (*ip++)
364 #define FETCH_LENGTH(len) do { ip = vm_fetch_length (ip, &len); } while (0)
368 #define CLOCK(n) vp->clock += n
374 #ifdef HAVE_LABELS_AS_VALUES
375 #define NEXT_JUMP() goto *jump_table[FETCH ()]
377 #define NEXT_JUMP() goto vm_start
392 #define INIT_ARGS() \
396 int n = nargs - (bp->nargs - 1); \
398 goto vm_error_wrong_num_args; \
403 if (nargs != bp->nargs) \
404 goto vm_error_wrong_num_args; \
408 /* See frames.h for the layout of stack frames */
409 /* When this is called, bp points to the new program data,
410 and the arguments are already on the stack */
411 #define NEW_FRAME() \
415 scm_byte_t *ra = ip; \
417 /* Save old registers */ \
421 /* New registers */ \
422 fp = sp - bp->nargs + 1; \
423 data = SCM_FRAME_DATA_ADDRESS (fp); \
429 /* Init local variables */ \
430 for (i=bp->nlocs; i; i--) \
431 data[-i] = SCM_UNDEFINED; \
433 /* Create external variables */ \
434 external = bp->external; \
435 for (i = 0; i < bp->nexts; i++) \
436 CONS (external, SCM_UNDEFINED, external); \
438 /* Set frame data */ \
441 data[1] = SCM_BOOL_F; \
442 data[0] = external; \
445 #define FREE_FRAME() \
449 SCM *p = fp + bp->nargs + bp->nlocs; \
451 /* Restore pointers */ \
452 ip = SCM_FRAME_BYTE_CAST (p[3]); \
453 fp = SCM_FRAME_STACK_CAST (p[2]); \
455 if (!SCM_FALSEP (p[1])) \
457 /* Unlink the heap stack */ \
458 vp->this_frame = p[1]; \
462 /* Move stack items */ \
464 sp = SCM_FRAME_LOWER_ADDRESS (last_fp); \
465 while (p <= last_sp) \
470 SCM_FRAME_UPPER_ADDRESS (fp) - 1 \
474 #define CACHE_EXTERNAL() external = fp[bp->nargs + bp->nlocs]