vm-trace only traces execution of its thunk
[bpt/guile.git] / libguile / vm-engine.h
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
KN
18
19/* This file is included in vm_engine.c */
20
a98cef7e
KN
21\f
22/*
17e90c5e 23 * Registers
a98cef7e
KN
24 */
25
17e90c5e 26/* Register optimization. [ stolen from librep/src/lispmach.h,v 1.3 ]
9df03fd0 27
17e90c5e
KN
28 Some compilers underestimate the use of the local variables representing
29 the abstract machine registers, and don't put them in hardware registers,
30 which slows down the interpreter considerably.
31 For GCC, I have hand-assigned hardware registers for several architectures.
32*/
9df03fd0 33
17e90c5e
KN
34#ifdef __GNUC__
35#ifdef __mips__
36#define IP_REG asm("$16")
37#define SP_REG asm("$17")
38#define FP_REG asm("$18")
39#endif
40#ifdef __sparc__
41#define IP_REG asm("%l0")
42#define SP_REG asm("%l1")
43#define FP_REG asm("%l2")
44#endif
45#ifdef __alpha__
46#ifdef __CRAY__
47#define IP_REG asm("r9")
48#define SP_REG asm("r10")
49#define FP_REG asm("r11")
9df03fd0 50#else
17e90c5e
KN
51#define IP_REG asm("$9")
52#define SP_REG asm("$10")
53#define FP_REG asm("$11")
54#endif
55#endif
56#ifdef __i386__
e6eb2467
AW
57/* too few registers! because of register allocation errors with various gcs,
58 just punt on explicit assignments on i386, hoping that the "register"
59 declaration will be sufficient. */
893be93f 60#endif
17e90c5e
KN
61#if defined(PPC) || defined(_POWER) || defined(_IBMR2)
62#define IP_REG asm("26")
63#define SP_REG asm("27")
64#define FP_REG asm("28")
65#endif
66#ifdef __hppa__
67#define IP_REG asm("%r18")
68#define SP_REG asm("%r17")
69#define FP_REG asm("%r16")
70#endif
71#ifdef __mc68000__
72#define IP_REG asm("a5")
73#define SP_REG asm("a4")
74#define FP_REG
75#endif
76#ifdef __arm__
77#define IP_REG asm("r9")
78#define SP_REG asm("r8")
79#define FP_REG asm("r7")
80#endif
9df03fd0
KN
81#endif
82
17d1b4bf
AW
83#ifndef IP_REG
84#define IP_REG
85#endif
86#ifndef SP_REG
87#define SP_REG
88#endif
89#ifndef FP_REG
90#define FP_REG
91#endif
92
9df03fd0 93\f
a98cef7e 94/*
3d5ee0cd 95 * Cache/Sync
a98cef7e
KN
96 */
97
11ea1aba 98#ifdef VM_ENABLE_ASSERTIONS
9a8cc8e7
AW
99# define ASSERT(condition) if (SCM_UNLIKELY (!(condition))) abort()
100#else
101# define ASSERT(condition)
102#endif
103
104
3d5ee0cd 105#define CACHE_REGISTER() \
17e90c5e 106{ \
3d5ee0cd
KN
107 ip = vp->ip; \
108 sp = vp->sp; \
109 fp = vp->fp; \
17e90c5e 110}
a98cef7e 111
3d5ee0cd 112#define SYNC_REGISTER() \
a98cef7e 113{ \
3d5ee0cd
KN
114 vp->ip = ip; \
115 vp->sp = sp; \
116 vp->fp = fp; \
a98cef7e
KN
117}
118
8d90b356
AW
119/* FIXME */
120#define ASSERT_VARIABLE(x) \
121 do { if (!SCM_VARIABLEP (x)) { SYNC_REGISTER (); abort(); } \
122 } while (0)
123#define ASSERT_BOUND_VARIABLE(x) \
124 do { ASSERT_VARIABLE (x); \
125 if (SCM_VARIABLE_REF (x) == SCM_UNDEFINED) \
126 { SYNC_REGISTER (); abort(); } \
127 } while (0)
128
11ea1aba 129#ifdef VM_ENABLE_PARANOID_ASSERTIONS
7e4760e4 130#define CHECK_IP() \
53e28ed9 131 do { if (ip < bp->base || ip - bp->base > bp->len) abort (); } while (0)
28b119ee
AW
132#define ASSERT_ALIGNED_PROCEDURE() \
133 do { if ((scm_t_bits)bp % 8) abort (); } while (0)
a1a482e0
AW
134#define ASSERT_BOUND(x) \
135 do { if ((x) == SCM_UNDEFINED) { SYNC_REGISTER (); abort(); } \
136 } while (0)
7e4760e4
AW
137#else
138#define CHECK_IP()
28b119ee 139#define ASSERT_ALIGNED_PROCEDURE()
a1a482e0 140#define ASSERT_BOUND(x)
7e4760e4
AW
141#endif
142
20d47c39 143/* Cache the object table and free variables. */
a52b2d3d
LC
144#define CACHE_PROGRAM() \
145{ \
e677365c
AW
146 if (bp != SCM_PROGRAM_DATA (program)) { \
147 bp = SCM_PROGRAM_DATA (program); \
28b119ee 148 ASSERT_ALIGNED_PROCEDURE (); \
53e28ed9
AW
149 if (SCM_I_IS_VECTOR (SCM_PROGRAM_OBJTABLE (program))) { \
150 objects = SCM_I_VECTOR_WELTS (SCM_PROGRAM_OBJTABLE (program)); \
151 object_count = SCM_I_VECTOR_LENGTH (SCM_PROGRAM_OBJTABLE (program)); \
2fda0242
AW
152 } else { \
153 objects = NULL; \
154 object_count = 0; \
155 } \
e677365c 156 } \
41f248a8
KN
157}
158
3d5ee0cd
KN
159#define SYNC_BEFORE_GC() \
160{ \
161 SYNC_REGISTER (); \
17e90c5e 162}
a98cef7e 163
17e90c5e 164#define SYNC_ALL() \
a98cef7e 165{ \
3d5ee0cd 166 SYNC_REGISTER (); \
a98cef7e
KN
167}
168
a98cef7e 169\f
ac02b386
KN
170/*
171 * Error check
172 */
173
0b5f0e49
LC
174/* Accesses to a program's object table. */
175#if VM_CHECK_OBJECT
176#define CHECK_OBJECT(_num) \
6d14383e 177 do { if (SCM_UNLIKELY ((_num) >= object_count)) goto vm_error_object; } while (0)
0b5f0e49
LC
178#else
179#define CHECK_OBJECT(_num)
180#endif
181
57ab0671 182#if VM_CHECK_FREE_VARIABLES
6f16379e
AW
183#define CHECK_FREE_VARIABLE(_num) \
184 do { \
185 if (SCM_UNLIKELY ((_num) >= SCM_PROGRAM_NUM_FREE_VARIABLES (program))) \
186 goto vm_error_free_variable; \
187 } while (0)
8d90b356 188#else
57ab0671 189#define CHECK_FREE_VARIABLE(_num)
8d90b356
AW
190#endif
191
ac02b386 192\f
3d5ee0cd
KN
193/*
194 * Hooks
195 */
196
197#undef RUN_HOOK
198#if VM_USE_HOOKS
7656f194
AW
199#define RUN_HOOK(h) \
200 { \
201 if (SCM_UNLIKELY (vp->trace_level > 0)) \
202 { \
203 SYNC_REGISTER (); \
204 vm_dispatch_hook (vm, h); \
205 } \
206 }
3d5ee0cd
KN
207#else
208#define RUN_HOOK(h)
209#endif
210
ac02b386
KN
211#define BOOT_HOOK() RUN_HOOK (SCM_VM_BOOT_HOOK)
212#define HALT_HOOK() RUN_HOOK (SCM_VM_HALT_HOOK)
213#define NEXT_HOOK() RUN_HOOK (SCM_VM_NEXT_HOOK)
7a0d0cee 214#define BREAK_HOOK() RUN_HOOK (SCM_VM_BREAK_HOOK)
ac02b386
KN
215#define ENTER_HOOK() RUN_HOOK (SCM_VM_ENTER_HOOK)
216#define APPLY_HOOK() RUN_HOOK (SCM_VM_APPLY_HOOK)
217#define EXIT_HOOK() RUN_HOOK (SCM_VM_EXIT_HOOK)
218#define RETURN_HOOK() RUN_HOOK (SCM_VM_RETURN_HOOK)
3d5ee0cd 219
e8c37772
AW
220#define VM_HANDLE_INTERRUPTS \
221 SCM_ASYNC_TICK_WITH_CODE (SYNC_REGISTER ())
222
3d5ee0cd 223\f
a98cef7e
KN
224/*
225 * Stack operation
226 */
227
11ea1aba
AW
228#ifdef VM_ENABLE_STACK_NULLING
229# define CHECK_STACK_LEAKN(_n) ASSERT (!sp[_n]);
230# define CHECK_STACK_LEAK() CHECK_STACK_LEAKN(1)
231# define NULLSTACK(_n) { int __x = _n; CHECK_STACK_LEAKN (_n+1); while (__x > 0) sp[__x--] = NULL; }
66db076a
AW
232/* If you have a nonlocal exit in a pre-wind proc while invoking a continuation
233 inside a dynwind (phew!), the stack is fully rewound but vm_reset_stack for
234 that continuation doesn't have a chance to run. It's not important on a
235 semantic level, but it does mess up our stack nulling -- so this macro is to
236 fix that. */
237# define NULLSTACK_FOR_NONLOCAL_EXIT() if (vp->sp > sp) NULLSTACK (vp->sp - sp);
11ea1aba
AW
238#else
239# define CHECK_STACK_LEAKN(_n)
240# define CHECK_STACK_LEAK()
241# define NULLSTACK(_n)
66db076a 242# define NULLSTACK_FOR_NONLOCAL_EXIT()
11ea1aba
AW
243#endif
244
17e90c5e 245#define CHECK_OVERFLOW() \
75d315e1 246 if (sp >= stack_limit) \
17e90c5e
KN
247 goto vm_error_stack_overflow
248
7e4760e4 249#define CHECK_UNDERFLOW() \
6c6a4439 250 if (sp < SCM_FRAME_UPPER_ADDRESS (fp)) \
7e4760e4 251 goto vm_error_stack_underflow;
a98cef7e 252
3616e9e9 253#define PUSH(x) do { sp++; CHECK_OVERFLOW (); *sp = x; } while (0)
11ea1aba
AW
254#define DROP() do { sp--; CHECK_UNDERFLOW (); NULLSTACK (1); } while (0)
255#define DROPN(_n) do { sp -= (_n); CHECK_UNDERFLOW (); NULLSTACK (_n); } while (0)
17e90c5e
KN
256#define POP(x) do { x = *sp; DROP (); } while (0)
257
2d80426a
LC
258/* A fast CONS. This has to be fast since its used, for instance, by
259 POP_LIST when fetching a function's argument list. Note: `scm_cell' is an
260 inlined function in Guile 1.7. Unfortunately, it calls
261 `scm_gc_for_newcell ()' which is _not_ inlined and allocated cells on the
262 heap. XXX */
263#define CONS(x,y,z) \
264{ \
265 SYNC_BEFORE_GC (); \
266 x = scm_cell (SCM_UNPACK (y), SCM_UNPACK (z)); \
a98cef7e
KN
267}
268
f41cb00c
LC
269/* Pop the N objects on top of the stack and push a list that contains
270 them. */
17e90c5e 271#define POP_LIST(n) \
f41cb00c
LC
272do \
273{ \
17e90c5e 274 int i; \
11ea1aba
AW
275 SCM l = SCM_EOL, x; \
276 for (i = n; i; i--) \
277 { \
278 POP (x); \
279 CONS (l, x, l); \
280 } \
3616e9e9 281 PUSH (l); \
17e90c5e
KN
282} while (0)
283
1f40459f 284/* The opposite: push all of the elements in L onto the list. */
fb10a008 285#define PUSH_LIST(l, NILP) \
1f40459f
AW
286do \
287{ \
288 for (; scm_is_pair (l); l = SCM_CDR (l)) \
289 PUSH (SCM_CAR (l)); \
fb10a008 290 if (SCM_UNLIKELY (!NILP (l))) { \
e06e857c 291 finish_args = scm_list_1 (l); \
1f40459f
AW
292 goto vm_error_improper_list; \
293 } \
294} while (0)
295
135b32ee 296\f
cb4cca12
KN
297#define POP_LIST_MARK() \
298do { \
299 SCM o; \
300 SCM l = SCM_EOL; \
301 POP (o); \
302 while (!SCM_UNBNDP (o)) \
303 { \
304 CONS (l, o, l); \
305 POP (o); \
306 } \
307 PUSH (l); \
308} while (0)
309
2bd859c8
AW
310#define POP_CONS_MARK() \
311do { \
312 SCM o, l; \
313 POP (l); \
314 POP (o); \
315 while (!SCM_UNBNDP (o)) \
316 { \
317 CONS (l, o, l); \
318 POP (o); \
319 } \
320 PUSH (l); \
321} while (0)
322
a98cef7e
KN
323\f
324/*
17e90c5e 325 * Instruction operation
a98cef7e
KN
326 */
327
17e90c5e 328#define FETCH() (*ip++)
53e28ed9 329#define FETCH_LENGTH(len) do { len=*ip++; len<<=8; len+=*ip++; len<<=8; len+=*ip++; } while (0)
17e90c5e 330
17e90c5e
KN
331#undef NEXT_JUMP
332#ifdef HAVE_LABELS_AS_VALUES
53e28ed9 333#define NEXT_JUMP() goto *jump_table[FETCH () & SCM_VM_INSTRUCTION_MASK]
17e90c5e
KN
334#else
335#define NEXT_JUMP() goto vm_start
336#endif
337
338#define NEXT \
339{ \
17e90c5e 340 NEXT_HOOK (); \
11ea1aba 341 CHECK_STACK_LEAK (); \
17e90c5e 342 NEXT_JUMP (); \
a98cef7e
KN
343}
344
345\f
ac99cb0c 346/* See frames.h for the layout of stack frames */
2cdb8cdc
AW
347/* When this is called, bp points to the new program data,
348 and the arguments are already on the stack */
03e6c165
AW
349#define DROP_FRAME() \
350 { \
351 sp -= 3; \
352 NULLSTACK (3); \
353 CHECK_UNDERFLOW (); \
354 }
355
356
17e90c5e
KN
357/*
358 Local Variables:
359 c-file-style: "gnu"
360 End:
361*/