Debian Codegraph @

src/h/stacks.h

raw symbols SCIP
1 /* -*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=8 shiftwidth=4 expandtab: */
3
4 /*
5 * Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya.
6 * Copyright (c) 1990, Giuseppe Attardi.
7 * Copyright (c) 2000, Juan Jose Garcia-Ripoll
8 *
9 * See file 'LICENSE' for the copyright details.
10 *
11 */
12
13 /* stacks.h -- Bind/Jump/Frame stacks. */
14
15 #ifndef ECL_STACKS_H
16 #define ECL_STACKS_H
17
18 #include <ecl/ecl_atomics.h>
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 /***********
25 * C STACK
26 ***********/
27
28 #ifdef ECL_DOWN_STACK
29 #define ecl_cs_check(env,var) \
30 if (ecl_unlikely((char*)(&var) <= (env)->c_stack.limit)) ecl_cs_overflow()
31 #else
32 #define ecl_cs_check(env,var) \
33 if (ecl_unlikely((char*)(&var) >= (env)->c_stack.limit)) ecl_cs_overflow()
34 #endif
35
36 /*********************************************************
37 * INTERRUPT SAFE STACK MANIPULATIONS
38 *
39 * The requirement for interruptible threads puts major
40 * restrictions on the implementation of stack push/pop and unwind
41 * routines. There are two principle requirements to be fulfilled:
42 * The code which is executed during the interrupt must not
43 * overwrite stack values (1) and it must be able to safely call stack
44 * unwind functions (2).
45 * The first requirement can be met in two distinct ways:
46 * - Ordering the manipulations of the stack pointer and of the
47 * stack itself in the right manner. This means, when pushing in
48 * the stack first increase the stack pointer and then write the
49 * value and the other way round for popping from the stack. This
50 * method has the drawback that it requires insertions of
51 * memory barriers on modern processors, possibly impacting
52 * performance.
53 * - Avoid overwriting stack values in the interrupt signal
54 * handler. This can be achieved by either increasing the stack
55 * pointer temporarily during the execution of the interrupt code
56 * or by saving/restoring the topmost stack value. However due to
57 * the second requirement, this simple method is sufficient only
58 * for the arguments stack.
59 * The second requirement requires the stack to be in a consistent
60 * state during the interrupt. The easiest solution would be to
61 * disable interrupts during stack manipulations. Because of the
62 * mprotect()-mechanism for fast interrupt dispatch, which does not
63 * allow writes in the thread-local environment while interrupts
64 * are disabled, this is unfortunately not possible. The solution
65 * adopted for this case (bindings and frame stack) is to push a
66 * dummy tag/symbol in the stack before any other manipulations are
67 * done. This dummy tag/symbol will then be ignored while
68 * unwinding.
69 */
70
71 /**************
72 * BIND STACK
73 **************/
74
75 typedef struct ecl_bds_frame {
76 cl_object symbol; /* symbol */
77 cl_object value; /* previous value of the symbol */
78 } *ecl_bds_ptr;
79
80 #define ecl_bds_check(env) \
81 (ecl_unlikely(env->bds_stack.top >= env->bds_stack.limit)? (ecl_bds_overflow(),1) : 0)
82
83 #define ECL_MISSING_SPECIAL_BINDING (~((cl_index)0))
84
85 extern ECL_API ecl_bds_ptr ecl_bds_overflow(void);
86 extern ECL_API void ecl_bds_bind(cl_env_ptr env, cl_object symbol, cl_object v);
87 extern ECL_API void ecl_bds_push(cl_env_ptr env, cl_object symbol);
88 extern ECL_API void ecl_bds_unwind1(cl_env_ptr env);
89 extern ECL_API void ecl_bds_unwind_n(cl_env_ptr env, int n);
90 #ifdef ECL_THREADS
91 extern ECL_API cl_object ecl_bds_read(cl_env_ptr env, cl_object s);
92 extern ECL_API cl_object *ecl_bds_ref(cl_env_ptr env, cl_object s);
93 extern ECL_API cl_object ecl_bds_set(cl_env_ptr env, cl_object s, cl_object v);
94 # define ECL_SYM_VAL(env,s) (ecl_bds_read(env,s))
95 # define ECL_SET(s,v) ((s)->symbol.value=(v))
96 # define ECL_SETQ(env,s,v) (*ecl_bds_ref(env,s)=(v))
97 #else
98 # define ECL_SYM_VAL(env,s) ((s)->symbol.value)
99 # define ECL_SET(s,v) ((s)->symbol.value=(v))
100 # define ECL_SETQ(env,s,v) ((s)->symbol.value=(v))
101 #endif
102
103 #ifdef ECL_THREADS
104 static inline void
105 ecl_bds_bind_inl(cl_env_ptr env, cl_object s, cl_object v)
106 {
107 ecl_bds_ptr slot;
108 cl_object *location;
109 const cl_index index = s->symbol.binding;
110 if (index >= env->bds_stack.tl_bindings_size) {
111 ecl_bds_bind(env,s,v);
112 } else {
113 location = env->bds_stack.tl_bindings + index;
114 slot = env->bds_stack.top+1;
115 if (slot >= env->bds_stack.limit) slot = ecl_bds_overflow();
116 /* First, we push a dummy symbol in the stack to
117 * prevent segfaults when we are interrupted with a
118 * call to ecl_bds_unwind. */
119 slot->symbol = ECL_DUMMY_TAG;
120 AO_nop_full();
121 ++env->bds_stack.top;
122 /* Then we disable interrupts to ensure that
123 * ecl_bds_unwind doesn't overwrite the symbol with
124 * some random value. */
125 ecl_disable_interrupts_env(env);
126 slot->symbol = s;
127 slot->value = *location;
128 *location = v;
129 ecl_enable_interrupts_env(env);
130 }
131 }
132
133 static inline void
134 ecl_bds_push_inl(cl_env_ptr env, cl_object s)
135 {
136 ecl_bds_ptr slot;
137 cl_object *location;
138 const cl_index index = s->symbol.binding;
139 if (index >= env->bds_stack.tl_bindings_size) {
140 ecl_bds_push(env, s);
141 } else {
142 location = env->bds_stack.tl_bindings + index;
143 slot = env->bds_stack.top+1;
144 if (slot >= env->bds_stack.limit) slot = ecl_bds_overflow();
145 slot->symbol = ECL_DUMMY_TAG;
146 AO_nop_full();
147 ++env->bds_stack.top;
148 ecl_disable_interrupts_env(env);
149 slot->symbol = s;
150 slot->value = *location;
151 if (*location == ECL_NO_TL_BINDING) *location = s->symbol.value;
152 ecl_enable_interrupts_env(env);
153 }
154 }
155
156 static inline void
157 ecl_bds_unwind1_inl(cl_env_ptr env)
158 {
159 cl_object s = env->bds_stack.top->symbol;
160 cl_object *location = env->bds_stack.tl_bindings + s->symbol.binding;
161 *location = env->bds_stack.top->value;
162 --env->bds_stack.top;
163 }
164
165 static inline cl_object
166 ecl_bds_read_inl(cl_env_ptr env, cl_object s)
167 {
168 cl_index index = s->symbol.binding;
169 if (index < env->bds_stack.tl_bindings_size) {
170 cl_object x = env->bds_stack.tl_bindings[index];
171 if (x != ECL_NO_TL_BINDING) return x;
172 }
173 return s->symbol.value;
174 }
175 static inline cl_object *
176 ecl_bds_ref_inl(cl_env_ptr env, cl_object s)
177 {
178 cl_index index = s->symbol.binding;
179 if (index < env->bds_stack.tl_bindings_size) {
180 cl_object *location = env->bds_stack.tl_bindings + index;
181 if (*location != ECL_NO_TL_BINDING) return location;
182 }
183 return &s->symbol.value;
184 }
185
186 # define ecl_bds_set(env,s,v) (*ecl_bds_ref_inl(env,s)=(v))
187 # define ecl_bds_read ecl_bds_read_inl
188
189 #else /* ECL_THREADS */
190 static inline void
191 ecl_bds_bind_inl(cl_env_ptr env, cl_object s, cl_object v)
192 {
193 ecl_bds_ptr slot;
194 slot = ++env->bds_stack.top;
195 if (slot >= env->bds_stack.limit) slot = ecl_bds_overflow();
196 ecl_disable_interrupts_env(env);
197 slot->symbol = s;
198 slot->value = s->symbol.value;
199 s->symbol.value = v;
200 ecl_enable_interrupts_env(env);
201 }
202
203 static inline void
204 ecl_bds_push_inl(cl_env_ptr env, cl_object s)
205 {
206 ecl_bds_ptr slot;
207 slot = ++env->bds_stack.top;
208 if (slot >= env->bds_stack.limit) slot = ecl_bds_overflow();
209 ecl_disable_interrupts_env(env);
210 slot->symbol = s;
211 slot->value = s->symbol.value;
212 ecl_enable_interrupts_env(env);
213 }
214
215 static inline void
216 ecl_bds_unwind1_inl(cl_env_ptr env)
217 {
218 cl_object s = env->bds_stack.top->symbol;
219 s->symbol.value = env->bds_stack.top->value;
220 --env->bds_stack.top;
221 }
222 #endif /* ECL_THREADS */
223
224 #define ecl_bds_bind ecl_bds_bind_inl
225 #define ecl_bds_push ecl_bds_push_inl
226 #define ecl_bds_unwind1 ecl_bds_unwind1_inl
227
228 /****************************
229 * INVOCATION HISTORY STACK
230 ****************************/
231
232 typedef struct ecl_ihs_frame {
233 struct ecl_ihs_frame *next;
234 cl_object function;
235 cl_object lex_env;
236 cl_index index;
237 cl_index bds;
238 } *ecl_ihs_ptr;
239
240 #define ecl_ihs_push(env,rec,fun,lisp_env) do { \
241 const cl_env_ptr __the_env = (env); \
242 ecl_ihs_ptr const r = (ecl_ihs_ptr const)(rec); \
243 r->next=__the_env->ihs_stack.top; \
244 r->function=(fun); \
245 r->lex_env=(lisp_env); \
246 r->index=__the_env->ihs_stack.top->index+1; \
247 r->bds=__the_env->bds_stack.top - __the_env->bds_stack.org; \
248 __the_env->ihs_stack.top = r; \
249 } while(0)
250
251 #define ecl_ihs_pop(env) do { \
252 const cl_env_ptr __the_env = (env); \
253 ecl_ihs_ptr r = __the_env->ihs_stack.top; \
254 if (r) __the_env->ihs_stack.top = r->next; \
255 } while(0)
256
257 /***************
258 * FRAME STACK
259 ***************/
260 /* Frames signal points in the code to which we can at any time jump.
261 * Frames are established, for instance, by CATCH, BLOCK, TAGBODY,
262 * LAMBDA, UNWIND-PROTECT, etc.
263 *
264 * Frames are established by ecl_frs_push(). For each call to ecl_frs_push()
265 * there must be a corresponding ecl_frs_pop(). More precisely, since our
266 * frame mechanism relies on the C stack and on the setjmp/longjmp
267 * functions, any function that creates a frame must also destroy it
268 * with ecl_frs_pop() before returning.
269 *
270 * Frames are identified by a value frs_val. This can be either a
271 * unique identifier, created for each CATCH, BLOCK, etc, or a common
272 * one ECL_PROTECT_TAG, used by UNWIND-PROTECT forms. The first type
273 * of frames can be target of a search ecl_frs_sch() and thus one can jump
274 * to them. The second type of frames are like barriers designed to
275 * intercept the jumps to the outer frames and are called
276 * automatically by the function unwind() whenever it jumps to a frame
277 * which is beyond one of these barriers.
278 */
279
280 typedef struct ecl_frame {
281 jmp_buf frs_jmpbuf;
282 cl_object frs_val;
283 ecl_ihs_ptr frs_ihs;
284 cl_index frs_bds_ndx;
285 cl_index frs_run_ndx;
286 } *ecl_frame_ptr;
287
288 extern ECL_API ecl_frame_ptr _ecl_frs_push(cl_env_ptr);
289 #define ecl_frs_push(env,val) \
290 ecl_frame_ptr __frame = _ecl_frs_push(env); \
291 ecl_disable_interrupts_env(env); \
292 __frame->frs_val = val; \
293 int __ecl_frs_push_result = ecl_setjmp(__frame->frs_jmpbuf); \
294 ecl_enable_interrupts_env(env)
295
296 #define ecl_frs_pop(env) ((env)->frs_stack.top--)
297 #define ecl_frs_pop_n(env,n) ((env)->frs_stack.top-=n)
298
299 /*******************
300 * ARGUMENTS STACK
301 *******************
302 * Here we define how we handle the incoming arguments for a
303 * function. Our calling conventions specify that at most
304 * ECL_C_ARGUMENTS_LIMIT are pushed onto the C stack. If the function
305 * receives more than this number of arguments it will keep a copy of
306 * _all_ those arguments _plus_ the remaining ones in the lisp
307 * stack. The caller is responsible for storing and removing such
308 * values.
309 *
310 * Given this structure, we need our own object for handling variable
311 * argument list, ecl_va_list. This object joins the C data type for
312 * handling vararg lists and a pointer to the lisp stack, in case the
313 * arguments were passed there.
314 *
315 * Note that keeping a direct reference to the lisp stack effectively
316 * locks it in memory, preventing the block from being garbage
317 * collected if the stack grows -- at least until all references are
318 * eliminated --. This is something we have to live with and which
319 * is somehow unavoidable, given that function arguments have to be
320 * stored somewhere.
321 */
322
323 #define ecl_va_start(a,p,n,k) { \
324 a[0].narg = (n)-(k); \
325 va_start(a[0].args,p); \
326 a[0].sp = ((n) <= ECL_C_ARGUMENTS_LIMIT)? 0 : _ecl_va_sp(k); }
327 #define ecl_va_arg(a) \
328 (a[0].narg--,(a[0].sp? *(a[0].sp++) : va_arg(a[0].args,cl_object)))
329 #define ecl_va_copy(dest,orig) { \
330 dest[0].narg = orig[0].narg; \
331 dest[0].sp = orig[0].sp; \
332 va_copy(dest[0].args,orig[0].args); \
333 }
334 #define ecl_va_end(a) \
335 va_end(a[0].args)
336
337 /***********************
338 * RETURN VALUES STACK
339 ***********************/
340
341 #define ecl_nth_value(env,n) ((env)->values[n])
342 #define ecl_nvalues(env) ((env)->nvalues)
343 #define ecl_return0(env) \
344 do { (env)->nvalues = 0; return ECL_NIL; } while (0)
345 #define ecl_return1(env,x) \
346 do { (env)->nvalues = 1; return (x); } while (0)
347 #define ecl_return2(env,x,y) \
348 do { \
349 cl_env_ptr __ecl_env = (env); \
350 cl_object __aux1 = (x), __aux2=(y); \
351 __ecl_env->values[1] = __aux2; \
352 __ecl_env->nvalues = 2; return __aux1; \
353 } while (0)
354 #define ecl_return3(env,x,y,z) \
355 do { \
356 cl_env_ptr __ecl_env = (env); \
357 cl_object __aux1=(x), __aux2=(y), __aux3=(z); \
358 __ecl_env->values[1] = __aux2; \
359 __ecl_env->values[2] = __aux3; \
360 __ecl_env->nvalues = 3; return __aux1; \
361 } while (0)
362
363 /*************
364 * LISP STACK
365 *************/
366
367 static inline void
368 ecl_data_stack_push(cl_env_ptr env, cl_object o) {
369 cl_object *new_top = env->run_stack.top;
370 if (ecl_unlikely(new_top >= env->run_stack.limit)) {
371 new_top = ecl_data_stack_grow(env);
372 }
373 env->run_stack.top = new_top+1;
374 *new_top = (o);
375 }
376
377 static inline void
378 ecl_data_stack_push_n(cl_env_ptr env, cl_index n) {
379 cl_object *new_top = env->run_stack.top;
380 while (ecl_unlikely((env->run_stack.limit - new_top) <= n)) {
381 new_top = ecl_data_stack_grow(env);
382 }
383 env->run_stack.top = new_top + n;
384 }
385
386 static inline cl_object
387 ecl_data_stack_pop_unsafe(cl_env_ptr env)
388 {
389 return *(--((env)->run_stack.top));
390 }
391
392 static inline void
393 ecl_data_stack_pop_n_unsafe(cl_env_ptr env, cl_index n)
394 {
395 env->run_stack.top -= n;
396 }
397
398 static inline cl_index
399 ecl_data_stack_index(cl_env_ptr env) {
400 return (env)->run_stack.top - (env)->run_stack.org;
401 }
402
403 static inline void
404 ecl_data_stack_set_index(cl_env_ptr env, cl_index ndx)
405 {
406 env->run_stack.top = env->run_stack.org + (ndx);
407 }
408
409 #define ECL_STACK_REF(env,n) ((env)->run_stack.top[n])
410 #define ECL_STACK_INDEX(env) ecl_data_stack_index(env)
411 #define ECL_STACK_UNWIND(env,ndx) ecl_data_stack_set_index(env,ndx)
412 #define ECL_STACK_PUSH_N(env,n) ecl_data_stack_push_n(env,n)
413 #define ECL_STACK_PUSH(env,o) ecl_data_stack_push(env,o)
414 #define ECL_STACK_POP_UNSAFE(env) ecl_data_stack_pop_unsafe(env)
415 #define ECL_STACK_POP_N_UNSAFE(env,o) ecl_data_stack_pop_n_unsafe(env,o)
416
417 #define ECL_STACK_FRAME_REF(f,ndx) \
418 ((f)->frame.env->run_stack.org[(f)->frame.base+(ndx)])
419 #define ECL_STACK_FRAME_SET(f,ndx,o) \
420 do { ECL_STACK_FRAME_REF(f,ndx) = (o); } while(0)
421
422 #define ECL_STACK_FRAME_PTR(f) \
423 ((f)->frame.env->run_stack.org+(f)->frame.base)
424 #define ECL_STACK_FRAME_TOP(f) \
425 ((f)->frame.env->run_stack.org+(f)->frame.sp)
426
427 #define ECL_STACK_FRAME_COPY(dest,orig) do { \
428 cl_object __dst = (dest); \
429 cl_object __src = (orig); \
430 cl_index __size = __src->frame.size; \
431 ecl_stack_frame_open(__src->frame.env, __dst, __size); \
432 memcpy(ECL_STACK_FRAME_PTR(__dst), \
433 ECL_STACK_FRAME_PTR(__src), \
434 __size * sizeof(cl_object)); \
435 } while (0);
436
437 /*********************************
438 * HIGH LEVEL CONTROL STRUCTURES *
439 *********************************/
440
441 #define ECL_UNWIND_PROTECT_BEGIN(the_env) do { \
442 bool __unwinding; ecl_frame_ptr __next_fr; \
443 const cl_env_ptr __the_env = (the_env); \
444 cl_index __nr; \
445 ecl_frs_push(__the_env,ECL_PROTECT_TAG); \
446 if (__ecl_frs_push_result) { \
447 __unwinding=1; __next_fr=__the_env->frs_stack.nlj_fr; \
448 } else {
449
450 #define ECL_UNWIND_PROTECT_EXIT \
451 __unwinding=0; } \
452 ecl_frs_pop(__the_env); \
453 __nr = ecl_data_stack_push_values(__the_env);
454
455 #define ECL_UNWIND_PROTECT_END \
456 ecl_data_stack_pop_values(__the_env,__nr); \
457 if (__unwinding) ecl_unwind(__the_env,__next_fr); } while(0)
458
459 /* unwind-protect variant which disables interrupts during cleanup */
460 #define ECL_UNWIND_PROTECT_THREAD_SAFE_EXIT \
461 __unwinding=0; } \
462 ecl_bds_bind(__the_env,ECL_INTERRUPTS_ENABLED,ECL_NIL); \
463 ecl_frs_pop(__the_env); \
464 __nr = ecl_data_stack_push_values(__the_env);
465
466 #define ECL_UNWIND_PROTECT_THREAD_SAFE_END \
467 ecl_data_stack_pop_values(__the_env,__nr); \
468 ecl_bds_unwind1(__the_env); \
469 ecl_check_pending_interrupts(__the_env); \
470 if (__unwinding) ecl_unwind(__the_env,__next_fr); } while(0)
471
472 #define ECL_NEW_FRAME_ID(env) ecl_make_fixnum(env->frs_stack.frame_id++)
473
474 #define ECL_BLOCK_BEGIN(the_env,id) do { \
475 const cl_object __id = ECL_NEW_FRAME_ID(the_env); \
476 const cl_env_ptr __the_env = (the_env); \
477 ecl_frs_push(__the_env,__id); \
478 if (__ecl_frs_push_result == 0)
479
480 #define ECL_BLOCK_END \
481 ecl_frs_pop(__the_env); } while(0)
482
483 #define ECL_CATCH_BEGIN(the_env,tag) do { \
484 const cl_env_ptr __the_env = (the_env); \
485 ecl_frs_push(__the_env,tag); \
486 if (__ecl_frs_push_result == 0) {
487
488 #define ECL_CATCH_END } \
489 ecl_frs_pop(__the_env); } while (0)
490
491 #define ECL_RESTART_CASE_BEGIN(the_env, names) do { \
492 const cl_env_ptr __the_env = (the_env); \
493 const cl_object __ecl_tag = ecl_list1(names); \
494 ecl_bds_bind(__the_env, ECL_RESTART_CLUSTERS, \
495 si_bind_simple_restarts(__ecl_tag, names)); \
496 ecl_frs_push(__the_env,__ecl_tag); \
497 if (__ecl_frs_push_result == 0) {
498
499 #define ECL_RESTART_CASE(code, args) \
500 } else if (__the_env->values[0] == ecl_make_fixnum(code)) { \
501 const cl_object args = __the_env->values[1];
502
503 #define ECL_RESTART_CASE_END } \
504 ecl_frs_pop(__the_env); \
505 ecl_bds_unwind1(__the_env); \
506 } while (0)
507
508 #define ECL_HANDLER_CASE_BEGIN(the_env, names) do { \
509 const cl_env_ptr __the_env = (the_env); \
510 const cl_object __ecl_tag = ecl_list1(names); \
511 ecl_bds_bind(__the_env, ECL_HANDLER_CLUSTERS, \
512 si_bind_simple_handlers(__ecl_tag, names)); \
513 ecl_frs_push(__the_env,__ecl_tag); \
514 if (__ecl_frs_push_result == 0) {
515
516 #define ECL_HANDLER_CASE(code, args) \
517 } else if (__the_env->values[0] == ecl_make_fixnum(code)) { \
518 const cl_object args = __the_env->values[1];
519
520 #define ECL_HANDLER_CASE_END } \
521 ecl_frs_pop(__the_env); \
522 ecl_bds_unwind1(__the_env); \
523 } while (0)
524
525 #if defined(_MSC_VER)
526 # define ECL_CATCH_ALL_BEGIN(the_env) do { \
527 const cl_env_ptr __the_env = (the_env); \
528 _try { \
529 const cl_env_ptr __the_env = (the_env); \
530 ecl_frs_push(__the_env,ECL_PROTECT_TAG); \
531 if (__ecl_frs_push_result == 0) {
532 # define ECL_CATCH_ALL_IF_CAUGHT } else {
533 # define ECL_CATCH_ALL_END }} \
534 _except(_ecl_w32_exception_filter(GetExceptionInformation())) \
535 { (void)0; } \
536 ecl_frs_pop(__the_env); } while(0)
537 #else
538 # define ECL_CATCH_ALL_BEGIN(the_env) do { \
539 const cl_env_ptr __the_env = (the_env); \
540 ecl_frs_push(__the_env,ECL_PROTECT_TAG); \
541 if (__ecl_frs_push_result == 0) {
542 # define ECL_CATCH_ALL_IF_CAUGHT } else {
543 # define ECL_CATCH_ALL_END } \
544 ecl_frs_pop(__the_env); } while(0)
545 #endif
546
547
548 #ifdef __cplusplus
549 }
550 #endif
551
552 #endif /* ECL_STACKS_H */