Debian Codegraph @

src/c/stacks.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * stacks.d - runtime, binding, history and frame stacks
6 *
7 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
8 * Copyright (c) 1990 Giuseppe Attardi
9 * Copyright (c) 2001 Juan Jose Garcia Ripoll
10 *
11 * See file 'LICENSE' for the copyright details.
12 *
13 */
14
15 #include <ecl/ecl.h>
16 #include <ecl/ecl-inl.h>
17 #include <signal.h>
18 #include <string.h>
19 #ifdef HAVE_SYS_RESOURCE_H
20 # include <sys/time.h>
21 # include <sys/resource.h>
22 #endif
23 #include <ecl/ecl-inl.h>
24 #include <ecl/internal.h>
25 #include <ecl/stack-resize.h>
26
27 /* -- C Stack ---------------------------------------------------------------- */
28 void
29 ecl_cs_init(cl_env_ptr env)
30 {
31 volatile char foo = 0;
32 cl_index margin = ecl_option_values[ECL_OPT_C_STACK_SAFETY_AREA];
33 cl_index new_size = ecl_option_values[ECL_OPT_C_STACK_SIZE];
34 cl_index max_size = new_size;
35 #ifdef GBC_BOEHM
36 struct GC_stack_base base;
37 if (GC_get_stack_base(&base) == GC_SUCCESS)
38 env->c_stack.org = (char*)base.mem_base;
39 else
40 env->c_stack.org = (char*)(&env);
41 #else
42 /* Rough estimate. Not very safe. We assume that cl_boot() is invoked from the
43 * main() routine of the program. */
44 env->c_stack.org = (char*)(&env);
45 #endif
46 #ifdef ECL_CAN_SET_STACK_SIZE
47 {
48 struct rlimit rl;
49 if (!getrlimit(RLIMIT_STACK, &rl)) {
50 if (new_size > rl.rlim_cur) {
51 rl.rlim_cur = (new_size > rl.rlim_max) ? rl.rlim_max : new_size;
52 if (setrlimit(RLIMIT_STACK, &rl))
53 ecl_internal_error("Can't set the size of the C stack");
54 }
55 } else {
56 rl.rlim_cur = new_size;
57 rl.rlim_max = max_size;
58 }
59 if (rl.rlim_cur == 0 || rl.rlim_cur == RLIM_INFINITY || rl.rlim_cur > (cl_index)(-1)) {
60 /* Either getrlimit failed or returned nonsense, either way we don't
61 * know the stack size. Use a default of 1 MB and hope for the best. */
62 new_size = 1048576;
63 max_size = 1048576;
64 } else {
65 new_size = rl.rlim_cur;
66 max_size = rl.rlim_max;
67 }
68 }
69 #endif
70 env->c_stack.limit_size = new_size - 2*margin;
71 env->c_stack.size = new_size;
72 env->c_stack.max_size = max_size;
73 #ifdef ECL_DOWN_STACK
74 env->c_stack.max = env->c_stack.org - new_size;
75 if (&foo > (env->c_stack.org - new_size) + 16) {
76 env->c_stack.limit = (env->c_stack.org - new_size) + (2*margin);
77 if (env->c_stack.limit < env->c_stack.max)
78 env->c_stack.max = env->c_stack.limit;
79 } else {
80 ecl_internal_error("Can't set the size of the C stack: sanity check failed.");
81 }
82 #else
83 env->c_stack.max = env->c_stack.org + new_size;
84 if (&foo < (env->c_stack.org + new_size) - 16) {
85 env->c_stack.limit = (env->c_stack.org + new_size) - (2*margin);
86 if (env->c_stack.limit > env->c_stack.max)
87 env->c_stack.max = env->c_stack.limit;
88 } else {
89 ecl_internal_error("Can't set the size of the C stack: sanity check failed.");
90 }
91 #endif
92 }
93
94 void
95 ecl_cs_set_size(cl_env_ptr env, cl_index new_size)
96 {
97 volatile char foo = 0;
98 cl_index margin = ecl_option_values[ECL_OPT_C_STACK_SAFETY_AREA];
99 if (new_size > env->c_stack.max_size)
100 new_size = env->c_stack.max_size;
101 #ifdef ECL_CAN_SET_STACK_SIZE
102 {
103 struct rlimit rl;
104 if (!getrlimit(RLIMIT_STACK, &rl)) {
105 if (new_size > rl.rlim_cur) {
106 rl.rlim_cur = (new_size > rl.rlim_max) ? rl.rlim_max : new_size;
107 if (setrlimit(RLIMIT_STACK, &rl))
108 ecl_internal_error("Can't set the size of the C stack");
109 }
110 } else {
111 rl.rlim_cur = new_size;
112 }
113 if (rl.rlim_cur == 0 || rl.rlim_cur == RLIM_INFINITY || rl.rlim_cur > (cl_index)(-1)) {
114 /* Either getrlimit failed or returned nonsense, either way we don't know
115 * the stack size. Use a default of 1 MB and hope for the best. */
116 new_size = 1048576;
117 } else {
118 new_size = rl.rlim_cur;
119 }
120 }
121 #endif
122 env->c_stack.limit_size = new_size - 2*margin;
123 env->c_stack.size = new_size;
124 #ifdef ECL_DOWN_STACK
125 env->c_stack.max = env->c_stack.org - new_size;
126 if (&foo > (env->c_stack.org - new_size) + 16) {
127 env->c_stack.limit = (env->c_stack.org - new_size) + (2*margin);
128 if (env->c_stack.limit < env->c_stack.max)
129 env->c_stack.max = env->c_stack.limit;
130 } else {
131 ecl_internal_error("Can't set the size of the C stack: sanity check failed.");
132 }
133 #else
134 env->c_stack.max = env->c_stack.org + new_size;
135 if (&foo < (env->c_stack.org + new_size) - 16) {
136 env->c_stack.limit = (env->c_stack.org + new_size) - (2*margin);
137 if (env->c_stack.limit > env->c_stack.max)
138 env->c_stack.max = env->c_stack.limit;
139 } else {
140 ecl_internal_error("Can't set the size of the C stack: sanity check failed.");
141 }
142 #endif
143 }
144
145 void
146 ecl_cs_overflow(void)
147 {
148 static const char *stack_overflow_msg =
149 "\n;;;\n;;; Stack overflow.\n"
150 ";;; Jumping to the outermost toplevel prompt\n"
151 ";;;\n\n";
152 cl_env_ptr env = ecl_process_env();
153 cl_index margin = ecl_option_values[ECL_OPT_C_STACK_SAFETY_AREA];
154 cl_index size = env->c_stack.size;
155 #ifdef ECL_DOWN_STACK
156 if (env->c_stack.limit > env->c_stack.org - size)
157 env->c_stack.limit -= margin;
158 #else
159 if (env->c_stack.limit < env->c_stack.org + size)
160 env->c_stack.limit += margin;
161 #endif
162 else
163 ecl_internal_error(stack_overflow_msg);
164 if (env->c_stack.max_size == (cl_index)0 || env->c_stack.size < env->c_stack.max_size)
165 CEstack_overflow(@'ext::c-stack', ecl_make_fixnum(size), ECL_T);
166 else
167 CEstack_overflow(@'ext::c-stack', ecl_make_fixnum(size), ECL_NIL);
168 }
169
170 /* -- Data stack ------------------------------------------------------------ */
171
172 static void
173 run_init(cl_env_ptr env)
174 {
175 cl_index size, limit_size, margin;
176 margin = ecl_option_values[ECL_OPT_LISP_STACK_SAFETY_AREA];
177 limit_size = ecl_option_values[ECL_OPT_LISP_STACK_SIZE];
178 size = limit_size + 2 * margin;
179 env->run_stack.org = (cl_object *)ecl_malloc(size * sizeof(cl_object));
180 env->run_stack.top = env->run_stack.org;
181 env->run_stack.limit = &env->run_stack.org[limit_size];
182 env->run_stack.size = size;
183 env->run_stack.limit_size = limit_size;
184 /* A stack always has at least one element. This is assumed by cl__va_start
185 and friends, which take a sp=0 to have no arguments. */
186 *(env->run_stack.top++) = ecl_make_fixnum(0);
187 }
188
189 void
190 ecl_data_stack_set_limit(cl_env_ptr env, cl_index new_lim_size)
191 {
192 cl_index margin = ecl_option_values[ECL_OPT_LISP_STACK_SAFETY_AREA];
193 cl_object *old_org = env->run_stack.org;
194 cl_object *new_org = NULL;
195 cl_index osize = env->run_stack.size;
196 cl_index nsize = new_lim_size + 2*margin;
197 cl_index current_size = env->run_stack.top - old_org;
198 if (current_size > new_lim_size)
199 ecl_internal_error("Cannot shrink frame stack below its minimal element");
200 ECL_STACK_RESIZE_DISABLE_INTERRUPTS(env);
201 new_org = ecl_realloc(old_org,
202 osize * sizeof(*old_org),
203 nsize * sizeof(*old_org));
204 env->run_stack.org = new_org;
205 env->run_stack.top = new_org + current_size;
206 env->run_stack.limit = new_org + new_lim_size;
207 /* Update indexes */
208 env->run_stack.size = nsize;
209 env->run_stack.limit_size = new_lim_size;
210 ECL_STACK_RESIZE_ENABLE_INTERRUPTS(env);
211 }
212
213 cl_object *
214 ecl_data_stack_grow(cl_env_ptr env)
215 {
216 ecl_data_stack_set_limit(env, env->run_stack.limit_size + env->run_stack.limit_size / 2);
217 return env->run_stack.top;
218 }
219
220 cl_index
221 ecl_data_stack_push_values(cl_env_ptr env) {
222 cl_index i = env->nvalues;
223 cl_object *b = env->run_stack.top;
224 cl_object *p = b + i;
225 while (p >= env->run_stack.limit) {
226 b = ecl_data_stack_grow(env);
227 p = b + i;
228 }
229 env->run_stack.top = p;
230 ecl_copy(b, env->values, i * sizeof(cl_object));
231 return i;
232 }
233
234 void
235 ecl_data_stack_pop_values(cl_env_ptr env, cl_index n) {
236 cl_object *p = env->run_stack.top - n;
237 if (ecl_unlikely(p < env->run_stack.org))
238 ecl_internal_error("data stack: stack underflow.");
239 env->nvalues = n;
240 env->run_stack.top = p;
241 ecl_copy(env->values, p, n * sizeof(cl_object));
242 }
243
244 /* A stack frame denotes a slice of the lisp stack [BASE,BASE+SIZE]. Between
245 these two values we maintain a stack pointer SP that shows where we push and
246 pop values when we use the frame. There are two nuances to keep in mind:
247
248 1. When we try to push-extend to the frame, it is possible only if the stack
249 top is aligned with the stack frame end: TOP_INDEX = SP = BASE+SIZE. This is
250 to avoid a situation where we override a newer frame.
251
252 2. When the stack top is aligned with the stack frame end, then push and pop
253 modifies the lisp stack TOP and the frame's SP and SIZE. This ensures that we
254 can use the topmost stack frame as if it were the stack, but also that we can
255 use some inner frames without corrupting it.
256
257 Note that direct stack operations do not update existing frames, so it is
258 still possible to corrupt a stack frame if not carful. -- jd 2025-05-29 */
259
260 cl_object
261 ecl_stack_frame_open(cl_env_ptr env, cl_object f, cl_index size)
262 {
263 cl_object *base = env->run_stack.top;
264 cl_index bindex;
265 if (size) {
266 if ((env->run_stack.limit - base) < size) {
267 ecl_data_stack_set_limit(env, env->run_stack.limit_size + size);
268 base = env->run_stack.top;
269 }
270 }
271 bindex = ECL_STACK_INDEX(env);
272 f->frame.t = t_frame;
273 f->frame.opened = 1;
274 f->frame.base = bindex;
275 f->frame.size = size;
276 f->frame.sp = bindex;
277 f->frame.env = env;
278 env->run_stack.top = (base + size);
279 return f;
280 }
281
282 void
283 ecl_stack_frame_push(cl_object f, cl_object o)
284 {
285 cl_env_ptr the_env = f->frame.env;
286 cl_object *frame_top = ECL_STACK_FRAME_TOP(f);
287 cl_index limit_index = f->frame.base + f->frame.size;
288 if (f->frame.sp < limit_index) {
289 *frame_top = o;
290 f->frame.sp++;
291 } else if (frame_top == the_env->run_stack.top) {
292 f->frame.sp++;
293 f->frame.size++;
294 ECL_STACK_PUSH(the_env, o);
295 } else {
296 ecl_internal_error("ecl_stack_frame_pop: frame overflow.");
297 }
298 }
299
300 cl_object
301 ecl_stack_frame_pop(cl_object f)
302 {
303 cl_env_ptr the_env = f->frame.env;
304 cl_object *frame_top = ECL_STACK_FRAME_TOP(f);
305 if (f->frame.sp <= f->frame.base) {
306 ecl_internal_error("ecl_stack_frame_pop: frame underflow.");
307 } else if (frame_top == the_env->run_stack.top) {
308 f->frame.sp--;
309 f->frame.size--;
310 return ECL_STACK_POP_UNSAFE(the_env);
311 } else {
312 f->frame.sp--;
313 return *ECL_STACK_FRAME_TOP(f);
314 }
315 }
316
317 void
318 ecl_stack_frame_push_values(cl_object f)
319 {
320 cl_env_ptr the_env = f->frame.env;
321 cl_index limit_index = f->frame.base + f->frame.size;
322 cl_index vals_length = the_env->nvalues;
323 cl_index value_index = f->frame.sp + vals_length;
324 cl_object *frame_top = ECL_STACK_FRAME_TOP(f);
325 if (value_index <= limit_index) {
326 ecl_copy(frame_top, the_env->values, vals_length * sizeof(cl_object));
327 f->frame.sp = value_index;
328 } else if (frame_top == the_env->run_stack.top) {
329 f->frame.sp = value_index;
330 f->frame.size = value_index - f->frame.base;
331 ecl_data_stack_push_values(the_env);
332 } else {
333 ecl_internal_error("ecl_stack_frame_push: frame overflow.");
334 }
335 }
336
337 cl_object
338 ecl_stack_frame_pop_values(cl_object f)
339 {
340 cl_env_ptr the_env = f->frame.env;
341 cl_index top_size = f->frame.sp - f->frame.base;
342 cl_index n = top_size % ECL_MULTIPLE_VALUES_LIMIT;
343 cl_object *frame_top = ECL_STACK_FRAME_TOP(f), result;
344 if (frame_top == the_env->run_stack.top) {
345 ecl_data_stack_pop_values(the_env, n);
346 f->frame.sp -= n;
347 f->frame.size -= n;
348 return the_env->values[0];
349 } else {
350 the_env->nvalues = n;
351 the_env->values[0] = result = ECL_NIL;
352 while (n--) {
353 the_env->values[n] = result = ECL_STACK_FRAME_REF(f, n);
354 }
355 f->frame.sp -= n;
356 return result;
357 }
358 }
359
360 void
361 ecl_stack_frame_close(cl_object f)
362 {
363 if (f->frame.opened) {
364 f->frame.opened = 0;
365 ECL_STACK_UNWIND(f->frame.env, f->frame.base);
366 }
367 }
368
369 /* -- Binding stack ---------------------------------------------------------- */
370
371 static void
372 bds_init(cl_env_ptr env)
373 {
374 cl_index size, margin, limit_size;
375 margin = ecl_option_values[ECL_OPT_BIND_STACK_SAFETY_AREA];
376 limit_size = ecl_option_values[ECL_OPT_BIND_STACK_SIZE];
377 size = limit_size + 2 * margin;
378 env->bds_stack.org = (ecl_bds_ptr)ecl_malloc(size * sizeof(*env->bds_stack.org));
379 env->bds_stack.top = env->bds_stack.org-1;
380 env->bds_stack.limit = &env->bds_stack.org[limit_size];
381 env->bds_stack.size = size;
382 env->bds_stack.limit_size = limit_size;
383 }
384
385 ecl_bds_ptr
386 ecl_bds_overflow(void)
387 {
388 static const char *stack_overflow_msg =
389 "\n;;;\n;;; Binding stack overflow.\n"
390 ";;; Jumping to the outermost toplevel prompt\n"
391 ";;;\n\n";
392 cl_env_ptr env = ecl_process_env();
393 cl_index margin = ecl_option_values[ECL_OPT_BIND_STACK_SAFETY_AREA];
394 cl_index size = env->bds_stack.size;
395 ecl_bds_ptr org = env->bds_stack.org;
396 ecl_bds_ptr last = org + size;
397 if (env->bds_stack.limit >= last) {
398 ecl_internal_error(stack_overflow_msg);
399 }
400 env->bds_stack.limit += margin;
401 CEstack_overflow(@'ext::binding-stack', ecl_make_fixnum(size), ECL_T);
402 return env->bds_stack.top;
403 }
404
405 void
406 ecl_bds_unwind(cl_env_ptr env, cl_index new_bds_ndx)
407 {
408 ecl_bds_ptr new_bds_top = env->bds_stack.org + new_bds_ndx;
409 ecl_bds_ptr bds = env->bds_stack.top;
410 for (; bds > new_bds_top; bds--)
411 #ifdef ECL_THREADS
412 ecl_bds_unwind1(env);
413 #else
414 bds->symbol->symbol.value = bds->value;
415 #endif
416 env->bds_stack.top = new_bds_top;
417 }
418
419 void
420 ecl_bds_unwind_n(cl_env_ptr env, int n)
421 {
422 while (n--) ecl_bds_unwind1(env);
423 }
424
425 #ifdef ecl_bds_bind
426 # undef ecl_bds_bind
427 # undef ecl_bds_push
428 # undef ecl_bds_unwind1
429 #endif
430 #ifdef ecl_bds_read
431 # undef ecl_bds_read
432 # undef ecl_bds_set
433 # undef ecl_bds_ref
434 #endif
435
436 #ifdef ECL_THREADS
437 static cl_index
438 ecl_new_binding_index(cl_env_ptr env, cl_object symbol)
439 {
440 cl_object pool;
441 cl_index new_index = symbol->symbol.binding;
442 if (new_index == ECL_MISSING_SPECIAL_BINDING) {
443 pool = ecl_atomic_pop(&cl_core.reused_indices);
444 if (!Null(pool)) {
445 new_index = ecl_fixnum(ECL_CONS_CAR(pool));
446 } else {
447 new_index = ecl_atomic_index_incf(&cl_core.last_var_index);
448 }
449 symbol->symbol.binding = new_index;
450 }
451 return new_index;
452 }
453
454 static cl_index
455 invalid_or_too_large_binding_index(cl_env_ptr env, cl_object s)
456 {
457 cl_index index = s->symbol.binding;
458 if (index == ECL_MISSING_SPECIAL_BINDING) {
459 index = ecl_new_binding_index(env, s);
460 }
461 if (index >= env->bds_stack.tl_bindings_size) {
462 cl_index osize = env->bds_stack.tl_bindings_size;
463 cl_index nsize = cl_core.last_var_index * 1.25;
464 cl_object *old_vector = env->bds_stack.tl_bindings;
465 cl_object *new_vector = ecl_realloc(old_vector,
466 osize*sizeof(cl_object*),
467 nsize*sizeof(cl_object*));
468 while(osize < nsize) {
469 new_vector[osize++] = ECL_NO_TL_BINDING;
470 }
471 env->bds_stack.tl_bindings = new_vector;
472 env->bds_stack.tl_bindings_size = nsize;
473 }
474 return index;
475 }
476 #endif /* ECL_THREADS */
477
478 /*
479 * The following routines must match the inline forms in stacks.h
480 */
481 void
482 ecl_bds_bind(cl_env_ptr env, cl_object s, cl_object v)
483 {
484 #ifdef ECL_THREADS
485 cl_object *location;
486 ecl_bds_ptr slot;
487 cl_index index = s->symbol.binding;
488 if (index >= env->bds_stack.tl_bindings_size) {
489 index = invalid_or_too_large_binding_index(env,s);
490 }
491 location = env->bds_stack.tl_bindings + index;
492 slot = env->bds_stack.top+1;
493 if (slot >= env->bds_stack.limit) slot = ecl_bds_overflow();
494 slot->symbol = ECL_DUMMY_TAG;
495 AO_nop_full();
496 ++env->bds_stack.top;
497 ecl_disable_interrupts_env(env);
498 slot->symbol = s;
499 slot->value = *location;
500 *location = v;
501 ecl_enable_interrupts_env(env);
502 #else
503 ecl_bds_check(env);
504 ecl_bds_ptr slot = ++(env->bds_stack.top);
505 ecl_disable_interrupts_env(env);
506 slot->symbol = s;
507 slot->value = s->symbol.value;
508 s->symbol.value = v;
509 ecl_enable_interrupts_env(env);
510 #endif
511 }
512
513 void
514 ecl_bds_push(cl_env_ptr env, cl_object s)
515 {
516 #ifdef ECL_THREADS
517 cl_object *location;
518 ecl_bds_ptr slot;
519 cl_index index = s->symbol.binding;
520 if (index >= env->bds_stack.tl_bindings_size) {
521 index = invalid_or_too_large_binding_index(env,s);
522 }
523 location = env->bds_stack.tl_bindings + index;
524 slot = env->bds_stack.top+1;
525 if (slot >= env->bds_stack.limit) slot = ecl_bds_overflow();
526 slot->symbol = ECL_DUMMY_TAG;
527 AO_nop_full();
528 ++env->bds_stack.top;
529 ecl_disable_interrupts_env(env);
530 slot->symbol = s;
531 slot->value = *location;
532 if (*location == ECL_NO_TL_BINDING) *location = s->symbol.value;
533 ecl_enable_interrupts_env(env);
534 #else
535 ecl_bds_check(env);
536 ecl_bds_ptr slot = ++(env->bds_stack.top);
537 ecl_disable_interrupts_env(env);
538 slot->symbol = s;
539 slot->value = s->symbol.value;
540 ecl_enable_interrupts_env(env);
541 #endif
542 }
543
544 void
545 ecl_bds_unwind1(cl_env_ptr env)
546 {
547 cl_object s = env->bds_stack.top->symbol;
548 #ifdef ECL_THREADS
549 cl_object *location = env->bds_stack.tl_bindings + s->symbol.binding;
550 *location = env->bds_stack.top->value;
551 #else
552 s->symbol.value = env->bds_stack.top->value;
553 #endif
554 --env->bds_stack.top;
555 }
556
557 #ifdef ECL_THREADS
558 cl_object
559 ecl_bds_read(cl_env_ptr env, cl_object s)
560 {
561 cl_index index = s->symbol.binding;
562 if (index < env->bds_stack.tl_bindings_size) {
563 cl_object x = env->bds_stack.tl_bindings[index];
564 if (x != ECL_NO_TL_BINDING) return x;
565 }
566 return s->symbol.value;
567 }
568
569 cl_object *
570 ecl_bds_ref(cl_env_ptr env, cl_object s)
571 {
572 cl_index index = s->symbol.binding;
573 if (index < env->bds_stack.tl_bindings_size) {
574 cl_object *location = env->bds_stack.tl_bindings + index;
575 if (*location != ECL_NO_TL_BINDING)
576 return location;
577 }
578 return &(s->symbol.value);
579 }
580
581 cl_object
582 ecl_bds_set(cl_env_ptr env, cl_object s, cl_object value)
583 {
584 return *ecl_bds_ref(env, s) = value;
585 }
586 #endif /* ECL_THREADS */
587
588 void
589 ecl_bds_set_limit(cl_env_ptr env, cl_index new_lim_size)
590 {
591 cl_index margin = ecl_option_values[ECL_OPT_BIND_STACK_SAFETY_AREA];
592 ecl_bds_ptr old_org = env->bds_stack.org;
593 ecl_bds_ptr new_org = NULL;
594 cl_index osize = env->bds_stack.size;
595 cl_index nsize = new_lim_size + 2*margin;
596 cl_index current_size = env->bds_stack.top - old_org;
597 if (current_size > new_lim_size)
598 ecl_internal_error("Cannot shrink frame stack below its minimal element");
599 ECL_STACK_RESIZE_DISABLE_INTERRUPTS(env);
600 new_org = ecl_realloc(old_org,
601 osize * sizeof(*old_org),
602 nsize * sizeof(*old_org));
603 env->bds_stack.org = new_org;
604 env->bds_stack.top = new_org + current_size;
605 env->bds_stack.limit = new_org + new_lim_size;
606 /* Update indexes */
607 env->bds_stack.size = nsize;
608 env->bds_stack.limit_size = new_lim_size;
609 ECL_STACK_RESIZE_ENABLE_INTERRUPTS(env);
610 }
611
612 /* -- Invocation stack ------------------------------------------------------- */
613 static void
614 ihs_init(cl_env_ptr env)
615 {
616 static struct ecl_ihs_frame ihs_org = { NULL, NULL, NULL, 0};
617 env->ihs_stack.top = &ihs_org;
618 ihs_org.function = ECL_NIL;
619 ihs_org.lex_env = ECL_NIL;
620 ihs_org.index = 0;
621 }
622
623 /* -- Frame stack ------------------------------------------------------------ */
624
625 static void
626 frs_init(cl_env_ptr env)
627 {
628 cl_index size, margin, limit_size;
629 margin = ecl_option_values[ECL_OPT_FRAME_STACK_SAFETY_AREA];
630 limit_size = ecl_option_values[ECL_OPT_FRAME_STACK_SIZE];
631 size = limit_size + 2 * margin;
632 env->frs_stack.org = (ecl_frame_ptr)ecl_malloc(size * sizeof(*env->frs_stack.org));
633 env->frs_stack.top = env->frs_stack.org-1;
634 env->frs_stack.limit = &env->frs_stack.org[limit_size];
635 env->frs_stack.size = size;
636 env->frs_stack.limit_size = limit_size;
637 }
638
639 void
640 ecl_frs_set_limit(cl_env_ptr env, cl_index new_lim_size)
641 {
642 cl_index margin = ecl_option_values[ECL_OPT_FRAME_STACK_SAFETY_AREA];
643 ecl_frame_ptr old_org = env->frs_stack.org;
644 ecl_frame_ptr new_org = NULL;
645 cl_index osize = env->frs_stack.size;
646 cl_index nsize = new_lim_size + 2*margin;
647 cl_index current_size = env->frs_stack.top - old_org;
648 if (current_size > new_lim_size)
649 ecl_internal_error("Cannot shrink frame stack below its minimal element");
650 ECL_STACK_RESIZE_DISABLE_INTERRUPTS(env);
651 new_org = ecl_realloc(old_org,
652 osize * sizeof(*old_org),
653 nsize * sizeof(*old_org));
654 env->frs_stack.org = new_org;
655 env->frs_stack.top = new_org + current_size;
656 env->frs_stack.limit = new_org + new_lim_size;
657 /* Update indexes. */
658 env->frs_stack.size = nsize;
659 env->frs_stack.limit_size = new_lim_size;
660 ECL_STACK_RESIZE_ENABLE_INTERRUPTS(env);
661 }
662
663 static void
664 frs_overflow(void)
665 {
666 static const char *stack_overflow_msg =
667 "\n;;;\n;;; Frame stack overflow.\n"
668 ";;; Jumping to the outermost toplevel prompt\n"
669 ";;;\n\n";
670 cl_env_ptr env = ecl_process_env();
671 cl_index margin = ecl_option_values[ECL_OPT_FRAME_STACK_SAFETY_AREA];
672 cl_index size = env->frs_stack.size;
673 cl_index limit_size = env->frs_stack.limit_size;
674 ecl_frame_ptr org = env->frs_stack.org;
675 ecl_frame_ptr last = org + size;
676 if (env->frs_stack.limit >= last) {
677 ecl_internal_error(stack_overflow_msg);
678 }
679 env->frs_stack.limit += margin;
680 CEstack_overflow(@'ext::frame-stack', ecl_make_fixnum(limit_size), ECL_T);
681 }
682
683 ecl_frame_ptr
684 _ecl_frs_push(cl_env_ptr env)
685 {
686 /* We store a dummy tag first, to make sure that it is safe to
687 * interrupt this method with a call to ecl_unwind. Otherwise, a
688 * stray ECL_PROTECT_TAG will lead to segfaults. AO_nop_full is
689 * needed to ensure that the CPU doesn't reorder the memory
690 * stores. */
691 ecl_frame_ptr output = env->frs_stack.top+1;
692 if (output >= env->frs_stack.limit) {
693 frs_overflow();
694 output = env->frs_stack.top+1;
695 }
696 output->frs_val = ECL_DUMMY_TAG;
697 AO_nop_full();
698 ++env->frs_stack.top;
699 output->frs_bds_ndx = env->bds_stack.top - env->bds_stack.org;
700 output->frs_run_ndx = ECL_STACK_INDEX(env);
701 output->frs_ihs = env->ihs_stack.top;
702 return output;
703 }
704
705 ecl_frame_ptr
706 frs_sch (cl_object frame_id)
707 {
708 cl_env_ptr env = ecl_process_env();
709 ecl_frame_ptr top;
710 for (top = env->frs_stack.top; top >= env->frs_stack.org; top--)
711 if (top->frs_val == frame_id)
712 return(top);
713 return(NULL);
714 }
715
716 /* -- Initialization -------------------------------------------------------- */
717 cl_object
718 init_stacks(cl_env_ptr the_env)
719 {
720 #ifdef ECL_THREADS
721 if (the_env == cl_core.first_env) {
722 cl_index idx;
723 cl_object *vector = (cl_object *)ecl_malloc(1024*sizeof(cl_object*));
724 for(idx=0; idx<1024; idx++) {
725 vector[idx] = ECL_NO_TL_BINDING;
726 }
727 the_env->bds_stack.tl_bindings_size = 1024;
728 the_env->bds_stack.tl_bindings = vector;
729 }
730 #endif
731 frs_init(the_env);
732 bds_init(the_env);
733 run_init(the_env);
734 ihs_init(the_env);
735 /* FIXME ecl_cs_init must be called from the thread entry point at the
736 beginning to correctly determine the stack base. */
737 #if 0
738 cs_init(the_env);
739 #endif
740 return ECL_NIL;
741 }
742
743 cl_object
744 free_stacks(cl_env_ptr the_env)
745 {
746 #ifdef ECL_THREADS
747 ecl_free(the_env->bds_stack.tl_bindings);
748 the_env->bds_stack.tl_bindings_size = 0;
749 #endif
750 ecl_free(the_env->run_stack.org);
751 ecl_free(the_env->bds_stack.org);
752 ecl_free(the_env->frs_stack.org);
753 return ECL_NIL;
754 }
755
756 /* -- High level interface -------------------------------------------------- */
757
758 void
759 ecl_unwind(cl_env_ptr env, ecl_frame_ptr fr)
760 {
761 env->frs_stack.nlj_fr = fr;
762 ecl_frame_ptr top = env->frs_stack.top;
763 while (top != fr && top->frs_val != ECL_PROTECT_TAG){
764 top->frs_val = ECL_DUMMY_TAG;
765 --top;
766 }
767 env->ihs_stack.top = top->frs_ihs;
768 ecl_bds_unwind(env, top->frs_bds_ndx);
769 ECL_STACK_UNWIND(env, top->frs_run_ndx);
770 env->frs_stack.top = top;
771 ecl_longjmp(env->frs_stack.top->frs_jmpbuf, 1);
772 /* never reached */
773 }
774
775 cl_index
776 ecl_progv(cl_env_ptr env, cl_object vars0, cl_object values0)
777 {
778 cl_object vars = vars0, values = values0;
779 cl_index n = env->bds_stack.top - env->bds_stack.org;
780 for (; LISTP(vars) && LISTP(values); vars = ECL_CONS_CDR(vars)) {
781 if (Null(vars)) {
782 return n;
783 } else {
784 cl_object var = ECL_CONS_CAR(vars);
785 if (!ECL_SYMBOLP(var))
786 FEillegal_variable_name(var);
787 if (ecl_symbol_type(var) & ecl_stp_constant)
788 FEbinding_a_constant(var);
789 if (Null(values)) {
790 ecl_bds_bind(env, var, OBJNULL);
791 } else {
792 ecl_bds_bind(env, var, ECL_CONS_CAR(values));
793 values = ECL_CONS_CDR(values);
794 }
795 }
796 }
797 FEerror("Wrong arguments to special form PROGV. Either~%"
798 "~A~%or~%~A~%are not proper lists",
799 2, vars0, values0);
800 }
801
802 /* -- Bindings stack -------------------------------------------------------- */
803
804 static ecl_bds_ptr
805 get_bds_ptr(cl_object x)
806 {
807 if (ECL_FIXNUMP(x)) {
808 cl_env_ptr env = ecl_process_env();
809 ecl_bds_ptr p = env->bds_stack.org + ecl_fixnum(x);
810 if (env->bds_stack.org <= p && p <= env->bds_stack.top)
811 return(p);
812 }
813 FEerror("~S is an illegal bds index.", 1, x);
814 }
815
816 cl_object
817 si_bds_top()
818 {
819 cl_env_ptr env = ecl_process_env();
820 ecl_return1(env, ecl_make_fixnum(env->bds_stack.top - env->bds_stack.org));
821 }
822
823 cl_object
824 si_bds_var(cl_object arg)
825 {
826 cl_env_ptr env = ecl_process_env();
827 ecl_return1(env, get_bds_ptr(arg)->symbol);
828 }
829
830 cl_object
831 si_bds_val(cl_object arg)
832 {
833 cl_env_ptr env = ecl_process_env();
834 cl_object v = get_bds_ptr(arg)->value;
835 ecl_return1(env, ((v == OBJNULL || v == ECL_NO_TL_BINDING)? ECL_UNBOUND : v));
836 }
837
838 /* -- Frame stack ----------------------------------------------------------- */
839
840 static ecl_frame_ptr
841 get_frame_ptr(cl_object x)
842 {
843 if (ECL_FIXNUMP(x)) {
844 cl_env_ptr env = ecl_process_env();
845 ecl_frame_ptr p = env->frs_stack.org + ecl_fixnum(x);
846 if (env->frs_stack.org <= p && p <= env->frs_stack.top)
847 return p;
848 }
849 FEerror("~S is an illegal frs index.", 1, x);
850 }
851
852 cl_object
853 si_frs_top()
854 {
855 cl_env_ptr env = ecl_process_env();
856 ecl_return1(env, ecl_make_fixnum(env->frs_stack.top - env->frs_stack.org));
857 }
858
859 cl_object
860 si_frs_bds(cl_object arg)
861 {
862 cl_env_ptr env = ecl_process_env();
863 ecl_return1(env, ecl_make_fixnum(get_frame_ptr(arg)->frs_bds_ndx));
864 }
865
866 cl_object
867 si_frs_tag(cl_object arg)
868 {
869 cl_env_ptr env = ecl_process_env();
870 ecl_return1(env, get_frame_ptr(arg)->frs_val);
871 }
872
873 cl_object
874 si_frs_ihs(cl_object arg)
875 {
876 cl_env_ptr env = ecl_process_env();
877 ecl_return1(env, ecl_make_fixnum(get_frame_ptr(arg)->frs_ihs->index));
878 }
879
880 cl_object
881 si_sch_frs_base(cl_object fr, cl_object ihs)
882 {
883 cl_env_ptr env = ecl_process_env();
884 ecl_frame_ptr x;
885 cl_index y = ecl_to_size(ihs);
886 for (x = get_frame_ptr(fr);
887 x <= env->frs_stack.top && x->frs_ihs->index < y;
888 x++);
889 ecl_return1(env, ((x > env->frs_stack.top)
890 ? ECL_NIL
891 : ecl_make_fixnum(x - env->frs_stack.org)));
892 }
893
894 /* -- Invocation stack ------------------------------------------------------ */
895
896 static ecl_ihs_ptr
897 get_ihs_ptr(cl_index n)
898 {
899 cl_env_ptr env = ecl_process_env();
900 ecl_ihs_ptr p = env->ihs_stack.top;
901 if (n > p->index)
902 FEerror("~D is an illegal IHS index.", 1, ecl_make_fixnum(n));
903 while (n < p->index)
904 p = p->next;
905 return p;
906 }
907
908 cl_object
909 si_ihs_top(void)
910 {
911 cl_env_ptr env = ecl_process_env();
912 ecl_return1(env, ecl_make_fixnum(env->ihs_stack.top->index));
913 }
914
915 cl_object
916 si_ihs_prev(cl_object x)
917 {
918 cl_env_ptr env = ecl_process_env();
919 ecl_return1(env, cl_1M(x));
920 }
921
922 cl_object
923 si_ihs_next(cl_object x)
924 {
925 cl_env_ptr env = ecl_process_env();
926 ecl_return1(env, cl_1P(x));
927 }
928
929 cl_object
930 si_ihs_bds(cl_object arg)
931 {
932 cl_env_ptr env = ecl_process_env();
933 ecl_return1(env, ecl_make_fixnum(get_ihs_ptr(ecl_to_size(arg))->bds));
934 }
935
936 cl_object
937 si_ihs_fun(cl_object arg)
938 {
939 cl_env_ptr env = ecl_process_env();
940 ecl_return1(env, get_ihs_ptr(ecl_to_size(arg))->function);
941 }
942
943 cl_object
944 si_ihs_env(cl_object arg)
945 {
946 cl_env_ptr env = ecl_process_env();
947 ecl_return1(env, get_ihs_ptr(ecl_to_size(arg))->lex_env);
948 }
949
950 /* -- General purpose stack implementation ----------------------------------- */
951
952 /* Stacks are based on actually adjustable simple vectors. */
953 cl_object
954 ecl_make_stack(cl_index size)
955 {
956 cl_object x = ecl_malloc(sizeof(struct ecl_vector));
957 x->vector.t = t_vector;
958 x->vector.elttype = ecl_aet_object;
959 x->vector.self.t = NULL;
960 x->vector.displaced = ECL_NIL;
961 x->vector.dim = size;
962 x->vector.fillp = 0;
963 x->vector.flags = ECL_FLAG_ADJUSTABLE | ECL_FLAG_HAS_FILL_POINTER;
964 x->vector.self.t = (cl_object *)ecl_malloc(size * sizeof(cl_object));
965 return x;
966 }
967 void
968 ecl_free_stack(cl_object self)
969 {
970 ecl_free(self->vector.self.t);
971 ecl_free(self);
972 }
973
974 void
975 ecl_stack_resize(cl_object self, cl_index nsize)
976 {
977 cl_index osize = self->vector.dim;
978 self->vector.self.t = (cl_object *)ecl_realloc(self->vector.self.t,
979 osize * sizeof(cl_object),
980 nsize * sizeof(cl_object));
981 self->vector.dim = nsize;
982 }
983
984 void
985 stack_ensure_size(cl_object self, cl_index nsize)
986 {
987 if (nsize >= self->vector.dim) {
988 ecl_stack_resize(self, nsize);
989 }
990 }
991
992 cl_index
993 ecl_stack_index(cl_object self) {
994 return self->vector.fillp;
995 }
996
997 cl_object
998 ecl_stack_push(cl_object self, cl_object elt)
999 {
1000 cl_index fillp = self->vector.fillp;
1001 cl_index dim = self->vector.dim;
1002 if (ecl_unlikely(fillp == dim)) {
1003 cl_index new_dim = dim+dim/2+1;
1004 ecl_stack_resize(self, new_dim);
1005 }
1006 self->vector.self.t[self->vector.fillp++] = elt;
1007 return self;
1008 }
1009
1010 cl_object
1011 ecl_stack_del(cl_object self, cl_object elt)
1012 {
1013 cl_index idx;
1014 cl_index ndx = self->vector.fillp;
1015 cl_object *v = self->vector.self.t;
1016 for(idx = 0; idx < ndx; idx++) {
1017 if (v[idx] == elt) {
1018 do { v[idx] = v[idx+1]; } while (++idx <= ndx);
1019 ecl_stack_popu(self);
1020 break;
1021 }
1022 }
1023 return self;
1024 }
1025
1026 /* Unsafe operations */
1027
1028 cl_object
1029 ecl_stack_popu(cl_object self)
1030 {
1031 cl_object result = self->vector.self.t[--self->vector.fillp];
1032 self->vector.self.t[self->vector.fillp] = ECL_NIL;
1033 return result;
1034 }
1035
1036 /* -- Lisp ops on stacks ---------------------------------------------------- */
1037
1038 cl_object
1039 si_set_limit(cl_object type, cl_object limit)
1040 {
1041 cl_env_ptr env = ecl_process_env();
1042 cl_index margin;
1043 if (type == @'ext::frame-stack') {
1044 cl_index current_size = env->frs_stack.top - env->frs_stack.org;
1045 cl_index request_size = ecl_to_size(limit);
1046 if(current_size > request_size)
1047 FEerror("Cannot shrink frame stack below ~D.", 1, limit);
1048 ecl_frs_set_limit(env, request_size);
1049 } else if (type == @'ext::binding-stack') {
1050 cl_index current_size = env->bds_stack.top - env->bds_stack.org;
1051 cl_index request_size = ecl_to_size(limit);
1052 if(current_size > request_size)
1053 FEerror("Cannot shrink binding stack below ~D.", 1, limit);
1054 ecl_bds_set_limit(env, request_size);
1055 } else if (type == @'ext::lisp-stack') {
1056 cl_index current_size = env->run_stack.top - env->run_stack.org;
1057 cl_index request_size = ecl_to_size(limit);
1058 if(current_size > request_size)
1059 FEerror("Cannot shrink lisp stack below ~D.", 1, limit);
1060 ecl_data_stack_set_limit(env, request_size);
1061 } else if (type == @'ext::c-stack') {
1062 cl_index the_size = ecl_to_size(limit);
1063 margin = ecl_option_values[ECL_OPT_C_STACK_SAFETY_AREA];
1064 ecl_cs_set_size(env, the_size + 2*margin);
1065 } else if (type == @'ext::heap-size') {
1066 /*
1067 * size_t can be larger than cl_index, and ecl_to_size()
1068 * creates a fixnum which is too small for size_t on 32-bit.
1069 */
1070 size_t the_size = (size_t)ecl_to_ulong(limit);
1071 _ecl_set_max_heap_size(the_size);
1072 }
1073
1074 ecl_return1(env, si_get_limit(type));
1075 }
1076
1077 cl_object
1078 si_get_limit(cl_object type)
1079 {
1080 cl_env_ptr env = ecl_process_env();
1081 cl_index output = 0;
1082 if (type == @'ext::frame-stack')
1083 output = env->frs_stack.limit_size;
1084 else if (type == @'ext::binding-stack')
1085 output = env->bds_stack.limit_size;
1086 else if (type == @'ext::lisp-stack')
1087 output = env->run_stack.limit_size;
1088 else if (type == @'ext::c-stack')
1089 output = env->c_stack.limit_size;
1090 else if (type == @'ext::heap-size') {
1091 /* size_t can be larger than cl_index */
1092 ecl_return1(env, ecl_make_unsigned_integer(cl_core.max_heap_size));
1093 }
1094
1095 ecl_return1(env, ecl_make_unsigned_integer(output));
1096 }