rts-heap.c
1 //! @file rts-heap.c
2 //! @author J. Marcel van der Veer
3
4 //! @section Copyright
5 //!
6 //! This file is part of Algol68G - an Algol 68 compiler-interpreter.
7 //! Copyright 2001-2025 J. Marcel van der Veer [algol68g@xs4all.nl].
8
9 //! @section License
10 //!
11 //! This program is free software; you can redistribute it and/or modify it
12 //! under the terms of the GNU General Public License as published by the
13 //! Free Software Foundation; either version 3 of the License, or
14 //! (at your option) any later version.
15 //!
16 //! This program is distributed in the hope that it will be useful, but
17 //! WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 //! or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 //! more details. You should have received a copy of the GNU General Public
20 //! License along with this program. If not, see [http://www.gnu.org/licenses/].
21
22 //! @section Synopsis
23 //!
24 //! Generator and garbage collector routines.
25
26 // The generator allocates space in stack or heap and initialises dynamically sized objects.
27 //
28 // A mark-and-gc garbage collector defragments the heap. When called, it walks
29 // the stack frames and marks the heap space that is still active. This marking
30 // process is called "colouring" here since we "pour paint" into the heap.
31 // The active blocks are then joined, the non-active blocks are forgotten.
32 //
33 // When colouring the heap, "cookies" are placed in objects as to find circular
34 // references.
35 //
36 // Algol68G introduces several anonymous tags in the symbol tables that save
37 // temporary REF or ROW results, so that they do not get prematurely swept.
38 //
39 // The genie is not smart enough to handle every heap clog, e.g. when copying
40 // STOWED objects. This seems not very elegant, but garbage collectors in general
41 // cannot solve all core management problems. To avoid many of the "unforeseen"
42 // heap clogs, we try to keep heap occupation low by garbage collecting
43 // occasionally, before it fills up completely. If this automatic mechanism does
44 // not help, one can always invoke the garbage collector by calling "gc heap"
45 // from Algol 68 source text.
46 //
47 // Mark-and-collect is simple but since it walks recursive structures, it could
48 // exhaust the C-stack (segment violation). A rough check is in place.
49 //
50 // For dynamically sized objects, first bounds are evaluated (right first, then down).
51 // The object is generated keeping track of the bound-count.
52 //
53 // ...
54 // [#1]
55 // STRUCT
56 // (
57 // [#2]
58 // STRUCT
59 // (
60 // [#3] A a, b, ...
61 // )
62 // , Advance bound-count here, max is #3
63 // [#4] B a, b, ...
64 // )
65 // , Advance bound-count here, max is #4
66 // [#5] C a, b, ...
67 // ...
68 //
69 // Bound-count is maximised when generator_stowed is entered recursively.
70 // Bound-count is advanced when completing a STRUCTURED_FIELD.
71 // Note that A68G will not extend stack frames. Thus only 'static' LOC generators
72 // are in the stack, and 'dynamic' LOC generators go into the heap. These local
73 // REFs in the heap get local scope however, and A68G's approach differs from the
74 // CDC ALGOL 68 approach that put all generators in the heap.
75 // Note that part of memory is called 'COMMON'. This is meant for future extension
76 // where a68g would need to point to external objects. The adressing scheme is that
77 // of a HEAP pointer - handle pointer + offset.
78
79 #include "a68g.h"
80 #include "a68g-genie.h"
81 #include "a68g-frames.h"
82 #include "a68g-prelude.h"
83 #include "a68g-parser.h"
84
85 #define DEF_NODE(p) (NEXT_NEXT (NODE (TAX (p))))
86
87 //! @brief PROC VOID gc heap
88
89 void genie_gc_heap (NODE_T * p)
90 {
91 gc_heap (p, A68G_FP);
92 }
93
94 //! @brief PROC VOID preemptive gc heap
95
96 void genie_preemptive_gc_heap (NODE_T * p)
97 {
98 if (A68G_GC (preemptive)) {
99 gc_heap (p, A68G_FP);
100 }
101 }
102
103 //! @brief INT blocks
104
105 void genie_block (NODE_T * p)
106 {
107 PUSH_VALUE (p, 0, A68G_INT);
108 }
109
110 //! @brief INT garbage collections
111
112 void genie_garbage_collections (NODE_T * p)
113 {
114 PUSH_VALUE (p, A68G_GC (sweeps), A68G_INT);
115 }
116
117 //! @brief INT garbage refused
118
119 void genie_garbage_refused (NODE_T * p)
120 {
121 PUSH_VALUE (p, A68G_GC (refused), A68G_INT);
122 }
123
124 //! @brief LONG INT garbage freed
125
126 void genie_garbage_freed (NODE_T * p)
127 {
128 PUSH_VALUE (p, A68G_GC (total), A68G_INT);
129 }
130
131 //! @brief REAL garbage seconds
132
133 void genie_garbage_seconds (NODE_T * p)
134 {
135 // Note that this timing is a rough cut.
136 PUSH_VALUE (p, A68G_GC (seconds), A68G_REAL);
137 }
138
139 //! @brief Size available for an object in the heap.
140
141 unt heap_available (void)
142 {
143 return A68G (heap_size) - A68G_HP;
144 }
145
146 //! @brief Initialise heap management.
147
148 void genie_init_heap (NODE_T * p)
149 {
150 (void) p;
151 if (A68G_HEAP == NO_BYTE) {
152 diagnostic (A68G_RUNTIME_ERROR, TOP_NODE (&A68G_JOB), ERROR_OUT_OF_CORE);
153 exit_genie (TOP_NODE (&A68G_JOB), A68G_RUNTIME_ERROR);
154 }
155 if (A68G_HANDLES == NO_BYTE) {
156 diagnostic (A68G_RUNTIME_ERROR, TOP_NODE (&A68G_JOB), ERROR_OUT_OF_CORE);
157 exit_genie (TOP_NODE (&A68G_JOB), A68G_RUNTIME_ERROR);
158 }
159 A68G_GC (seconds) = 0;
160 A68G_GC (total) = 0;
161 A68G_GC (sweeps) = 0;
162 A68G_GC (refused) = 0;
163 A68G_GC (preemptive) = A68G_FALSE;
164 ABEND (A68G (fixed_heap_pointer) >= (A68G (heap_size) - MIN_MEM_SIZE), ERROR_OUT_OF_CORE, __func__);
165 A68G_HP = A68G (fixed_heap_pointer);
166 A68G (heap_is_fluid) = A68G_FALSE;
167 // Assign handle space.
168 A68G_HANDLE *z = (A68G_HANDLE *) A68G_HANDLES;
169 A68G_GC (available_handles) = z;
170 A68G_GC (busy_handles) = NO_HANDLE;
171 int N = (unt) A68G (handle_pool_size) / SIZE_ALIGNED (A68G_HANDLE);
172 A68G_GC (free_handles) = N;
173 A68G_GC (max_handles) = N;
174 for (int k = 0; k < N; k++) {
175 STATUS (&(z[k])) = NULL_MASK;
176 POINTER (&(z[k])) = NO_BYTE;
177 SIZE (&(z[k])) = 0;
178 NEXT (&z[k]) = (k == N - 1 ? NO_HANDLE : &z[k + 1]);
179 PREVIOUS (&z[k]) = (k == 0 ? NO_HANDLE : &z[k - 1]);
180 }
181 }
182
183 //! @brief Whether mode must be coloured.
184
185 BOOL_T moid_needs_colouring (MOID_T * m)
186 {
187 if (IS_REF (m)) {
188 return A68G_TRUE;
189 } else if (IS (m, PROC_SYMBOL)) {
190 return A68G_TRUE;
191 } else if (IS_FLEX (m) || IS_ROW (m)) {
192 return A68G_TRUE;
193 } else if (IS_STRUCT (m) || IS_UNION (m)) {
194 for (PACK_T *p = PACK (m); p != NO_PACK; FORWARD (p)) {
195 if (moid_needs_colouring (MOID (p))) {
196 return A68G_TRUE;
197 }
198 }
199 return A68G_FALSE;
200 } else if (m == M_SIMPLIN || m == M_SIMPLOUT) {
201 return A68G_TRUE;
202 } else {
203 return A68G_FALSE;
204 }
205 }
206
207 //! @brief Colour all elements of a row.
208
209 void colour_row_elements (A68G_REF * z, MOID_T * m)
210 {
211 A68G_ARRAY *arr; A68G_TUPLE *tup;
212 GET_DESCRIPTOR (arr, tup, z);
213 if (get_row_size (tup, DIM (arr)) == 0) {
214 // Empty rows have a ghost elements.
215 BYTE_T *elem = ADDRESS (&ARRAY (arr));
216 colour_object (&elem[0], SUB (m));
217 } else {
218 // The multi-dimensional garbage collector.
219 BYTE_T *elem = ADDRESS (&ARRAY (arr));
220 BOOL_T done = A68G_FALSE;
221 initialise_internal_index (tup, DIM (arr));
222 while (!done) {
223 ADDR_T index = calculate_internal_index (tup, DIM (arr));
224 ADDR_T addr = ROW_ELEMENT (arr, index);
225 colour_object (&elem[addr], SUB (m));
226 done = increment_internal_index (tup, DIM (arr));
227 }
228 }
229 }
230
231 //! @brief Colour an (active) object.
232
233 void colour_object (BYTE_T * item, MOID_T * m)
234 {
235 if (item == NO_BYTE || m == NO_MOID) {
236 return;
237 }
238 if (!moid_needs_colouring (m)) {
239 return;
240 }
241 // Deeply recursive objects might exhaust the stack.
242 LOW_STACK_ALERT (NO_NODE);
243 if (IS_REF (m)) {
244 // REF AMODE colour pointer and object to which it refers.
245 A68G_REF *z = (A68G_REF *) item;
246 if (INITIALISED (z) && IS_IN_HEAP (z)) {
247 if (STATUS_TEST (REF_HANDLE (z), COOKIE_MASK)) {
248 return;
249 }
250 STATUS_SET (REF_HANDLE (z), (COOKIE_MASK | COLOUR_MASK));
251 if (!IS_NIL (*z)) {
252 colour_object (ADDRESS (z), SUB (m));
253 }
254 // STATUS_CLEAR (REF_HANDLE (z), COOKIE_MASK);.
255 }
256 } else if (IS_FLEXETY_ROW (m)) {
257 // Claim the descriptor and the row itself.
258 A68G_REF *z = (A68G_REF *) item;
259 if (INITIALISED (z) && IS_IN_HEAP (z)) {
260 if (STATUS_TEST (REF_HANDLE (z), COOKIE_MASK)) {
261 return;
262 }
263 // An array is ALWAYS in the heap.
264 STATUS_SET (REF_HANDLE (z), (COOKIE_MASK | COLOUR_MASK));
265 A68G_ARRAY *arr; A68G_TUPLE *tup;
266 GET_DESCRIPTOR (arr, tup, z);
267 if (REF_HANDLE (&(ARRAY (arr))) != NO_HANDLE) {
268 // Assume its initialisation.
269 MOID_T *n = DEFLEX (m);
270 STATUS_SET (REF_HANDLE (&(ARRAY (arr))), COLOUR_MASK);
271 if (moid_needs_colouring (SUB (n))) {
272 colour_row_elements (z, n);
273 }
274 }
275 // STATUS_CLEAR (REF_HANDLE (z), COOKIE_MASK);.
276 (void) tup;
277 }
278 } else if (IS_STRUCT (m)) {
279 // STRUCTures - colour fields.
280 for (PACK_T *p = PACK (m); p != NO_PACK; FORWARD (p)) {
281 colour_object (&item[OFFSET (p)], MOID (p));
282 }
283 } else if (IS_UNION (m)) {
284 // UNIONs - a united object may contain a value that needs colouring.
285 A68G_UNION *z = (A68G_UNION *) item;
286 if (INITIALISED (z)) {
287 MOID_T *united_moid = (MOID_T *) VALUE (z);
288 colour_object (&item[A68G_UNION_SIZE], united_moid);
289 }
290 } else if (IS (m, PROC_SYMBOL)) {
291 // PROCs - save a locale and the objects it points to.
292 A68G_PROCEDURE *z = (A68G_PROCEDURE *) item;
293 if (INITIALISED (z) && LOCALE (z) != NO_HANDLE && !(STATUS_TEST (LOCALE (z), COOKIE_MASK))) {
294 BYTE_T *u = POINTER (LOCALE (z));
295 STATUS_SET (LOCALE (z), (COOKIE_MASK | COLOUR_MASK));
296 for (PACK_T *s = PACK (MOID (z)); s != NO_PACK; FORWARD (s)) {
297 if (VALUE ((A68G_BOOL *) & u[0]) == A68G_TRUE) {
298 colour_object (&u[SIZE (M_BOOL)], MOID (s));
299 }
300 u = &(u[SIZE (M_BOOL) + SIZE (MOID (s))]);
301 }
302 // STATUS_CLEAR (LOCALE (z), COOKIE_MASK);.
303 }
304 } else if (m == M_SOUND) {
305 // Claim the data of a SOUND object, that is in the heap.
306 A68G_SOUND *w = (A68G_SOUND *) item;
307 if (INITIALISED (w)) {
308 STATUS_SET (REF_HANDLE (&(DATA (w))), (COOKIE_MASK | COLOUR_MASK));
309 }
310 } else if (m == M_SIMPLIN || m == M_SIMPLOUT) {
311 A68G_UNION *z = (A68G_UNION *) item;
312 if (INITIALISED (z)) {
313 MOID_T *united_moid = (MOID_T *) VALUE (z);
314 colour_object (&item[A68G_UNION_SIZE], united_moid);
315 }
316 }
317 }
318
319 //! @brief Colour active objects in the heap.
320
321 void colour_heap (ADDR_T fp)
322 {
323 while (fp != 0) {
324 NODE_T *p = FRAME_TREE (fp);
325 TABLE_T *t = TABLE (p);
326 if (t != NO_TABLE) {
327 for (TAG_T *q = IDENTIFIERS (t); q != NO_TAG; FORWARD (q)) {
328 colour_object (FRAME_LOCAL (fp, OFFSET (q)), MOID (q));
329 }
330 for (TAG_T *q = ANONYMOUS (t); q != NO_TAG; FORWARD (q)) {
331 colour_object (FRAME_LOCAL (fp, OFFSET (q)), MOID (q));
332 }
333 }
334 fp = FRAME_DYNAMIC_LINK (fp);
335 }
336 }
337
338 //! @brief Join all active blocks in the heap.
339
340 void defragment_heap (void)
341 {
342 A68G_HANDLE *z;
343 // Free handles.
344 z = A68G_GC (busy_handles);
345 while (z != NO_HANDLE) {
346 if (!(STATUS_TEST (z, COLOUR_MASK)) && !(STATUS_TEST (z, BLOCK_GC_MASK))) {
347 A68G_HANDLE *y = NEXT (z);
348 if (PREVIOUS (z) == NO_HANDLE) {
349 A68G_GC (busy_handles) = NEXT (z);
350 } else {
351 NEXT (PREVIOUS (z)) = NEXT (z);
352 }
353 if (NEXT (z) != NO_HANDLE) {
354 PREVIOUS (NEXT (z)) = PREVIOUS (z);
355 }
356 NEXT (z) = A68G_GC (available_handles);
357 PREVIOUS (z) = NO_HANDLE;
358 if (NEXT (z) != NO_HANDLE) {
359 PREVIOUS (NEXT (z)) = z;
360 }
361 A68G_GC (available_handles) = z;
362 STATUS_CLEAR (z, ALLOCATED_MASK);
363 A68G_GC (freed) += SIZE (z);
364 A68G_GC (free_handles)++;
365 z = y;
366 } else {
367 FORWARD (z);
368 }
369 }
370 // There can be no uncoloured allocated handle.
371 for (z = A68G_GC (busy_handles); z != NO_HANDLE; FORWARD (z)) {
372 ABEND (!(STATUS_TEST (z, COLOUR_MASK)) && !(STATUS_TEST (z, BLOCK_GC_MASK)), ERROR_INTERNAL_CONSISTENCY, __func__);
373 }
374 // Defragment the heap.
375 A68G_HP = A68G (fixed_heap_pointer);
376 for (z = A68G_GC (busy_handles); z != NO_HANDLE && NEXT (z) != NO_HANDLE; FORWARD (z)) {
377 ;
378 }
379 for (; z != NO_HANDLE; BACKWARD (z)) {
380 BYTE_T *dst = HEAP_ADDRESS (A68G_HP);
381 if (dst != POINTER (z)) {
382 MOVE (dst, POINTER (z), (unt) SIZE (z));
383 }
384 STATUS_CLEAR (z, (COLOUR_MASK | COOKIE_MASK));
385 POINTER (z) = dst;
386 A68G_HP += (SIZE (z));
387 ABEND (A68G_HP % A68G_ALIGNMENT != 0, ERROR_ALIGNMENT, __func__);
388 }
389 }
390
391 //! @brief Clean up garbage and defragment the heap.
392
393 void gc_heap (NODE_T * p, ADDR_T fp)
394 {
395 // Must start with fp = current frame_pointer.
396 A68G_HANDLE *z;
397 REAL_T t0, t1;
398 #if defined (BUILD_PARALLEL_CLAUSE)
399 if (OTHER_THREAD (FRAME_THREAD_ID (A68G_FP), A68G_PAR (main_thread_id))) {
400 A68G_GC (refused)++;
401 return;
402 }
403 #endif
404 if (STATUS_TEST (p, BLOCK_GC_MASK)) {
405 A68G_GC (refused)++;
406 return;
407 }
408 if (OPTION_CONSERVATIVE_GC (&A68G_JOB) && (A68G_GC (sema) > 0)) {
409 A68G_GC (refused)++;
410 return;
411 }
412 // Take no risk when intermediate results are on the stack.
413 if (OPTION_CONSERVATIVE_GC (&A68G_JOB) && (A68G_SP != A68G (stack_start))) {
414 A68G_GC (refused)++;
415 return;
416 }
417 // Give it a whirl then.
418 t0 = seconds ();
419 // Unfree handles are subject to inspection.
420 // Release them all before colouring.
421 for (z = A68G_GC (busy_handles); z != NO_HANDLE; FORWARD (z)) {
422 STATUS_CLEAR (z, (COLOUR_MASK | COOKIE_MASK));
423 }
424 // Pour paint into the heap to reveal active objects.
425 colour_heap (fp);
426 // Start freeing and compacting.
427 A68G_GC (freed) = 0;
428 defragment_heap ();
429 // Stats and logging.
430 A68G_GC (total) += A68G_GC (freed);
431 A68G_GC (sweeps)++;
432 A68G_GC (preemptive) = A68G_FALSE;
433 t1 = seconds ();
434 // C optimiser can make last digit differ, so next condition is
435 // needed to determine a positive time difference
436 if ((t1 - t0) > ((REAL_T) A68G (clock_res) / 2.0)) {
437 A68G_GC (seconds) += (t1 - t0);
438 } else {
439 A68G_GC (seconds) += ((REAL_T) A68G (clock_res) / 2.0);
440 }
441 // Call the event handler.
442 genie_call_event_routine (p, M_PROC_VOID, &A68G (on_gc_event), A68G_SP, A68G_FP);
443 }
444
445 //! @brief Yield a handle that will point to a block in the heap.
446
447 A68G_HANDLE *give_handle (NODE_T * p, MOID_T * a68m)
448 {
449 if (A68G_GC (available_handles) != NO_HANDLE) {
450 A68G_HANDLE *x = A68G_GC (available_handles);
451 A68G_GC (available_handles) = NEXT (x);
452 if (A68G_GC (available_handles) != NO_HANDLE) {
453 PREVIOUS (A68G_GC (available_handles)) = NO_HANDLE;
454 }
455 STATUS (x) = ALLOCATED_MASK;
456 POINTER (x) = NO_BYTE;
457 SIZE (x) = 0;
458 MOID (x) = a68m;
459 NEXT (x) = A68G_GC (busy_handles);
460 PREVIOUS (x) = NO_HANDLE;
461 if (NEXT (x) != NO_HANDLE) {
462 PREVIOUS (NEXT (x)) = x;
463 }
464 A68G_GC (busy_handles) = x;
465 A68G_GC (free_handles)--;
466 return x;
467 } else {
468 // Do not auto-GC!.
469 diagnostic (A68G_RUNTIME_ERROR, p, ERROR_OUT_OF_CORE);
470 exit_genie (p, A68G_RUNTIME_ERROR);
471 }
472 return NO_HANDLE;
473 }
474
475 //! @brief Give a block of heap for an object of indicated mode.
476
477 A68G_REF heap_generator (NODE_T * p, MOID_T * mode, int size)
478 {
479 ABEND (size < 0, ERROR_INVALID_SIZE, __func__);
480 size = A68G_ALIGN (size);
481 if (heap_available () >= size) {
482 A68G_REF z;
483 STATUS (&z) = (STATUS_MASK_T) (INIT_MASK | IN_HEAP_MASK);
484 OFFSET (&z) = 0;
485 A68G_HANDLE *x = give_handle (p, mode);
486 SIZE (x) = size;
487 POINTER (x) = HEAP_ADDRESS (A68G_HP);
488 FILL (POINTER (x), 0, size);
489 REF_SCOPE (&z) = PRIMAL_SCOPE;
490 REF_HANDLE (&z) = x;
491 ABEND (((long) ADDRESS (&z)) % A68G_ALIGNMENT != 0, ERROR_ALIGNMENT, __func__);
492 A68G_HP += size;
493 REAL_T _f_ = (REAL_T) A68G_HP / (REAL_T) A68G (heap_size);
494 REAL_T _g_ = (REAL_T) (A68G_GC (max_handles) - A68G_GC (free_handles)) / (REAL_T) A68G_GC (max_handles);
495 if (_f_ > DEFAULT_PREEMPTIVE || _g_ > DEFAULT_PREEMPTIVE) {
496 A68G_GC (preemptive) = A68G_TRUE;
497 }
498 return z;
499 } else {
500 // Do not auto-GC!.
501 diagnostic (A68G_RUNTIME_ERROR, p, ERROR_OUT_OF_CORE);
502 exit_genie (p, A68G_RUNTIME_ERROR);
503 return nil_ref;
504 }
505 }
506
507 //! @brief Give a block of heap for an object of indicated mode.
508
509 A68G_REF heap_generator_2 (NODE_T * p, MOID_T * mode, int len, int size)
510 {
511 if (len == 0 || size == 0) {
512 return heap_generator (p, mode, 0);
513 } else if (ABS (size) < (2 * GIGABYTE) / ABS (len)) {
514 return heap_generator (p, mode, len * size);
515 } else {
516 diagnostic (A68G_RUNTIME_ERROR, p, ERROR_OUT_OF_CORE);
517 exit_genie (p, A68G_RUNTIME_ERROR);
518 }
519 return nil_ref;
520 }
521
522 //! @brief Give a block of heap for an object of indicated mode.
523
524 A68G_REF heap_generator_3 (NODE_T * p, MOID_T * mode, int len1, int len2, int size)
525 {
526 if (len1 == 0 || len2 == 0) {
527 return heap_generator (p, mode, 0);
528 } else if (ABS (len2) < (2 * GIGABYTE) / ABS (len1)) {
529 return heap_generator_2 (p, mode, len1 * len2, size);
530 } else {
531 diagnostic (A68G_RUNTIME_ERROR, p, ERROR_OUT_OF_CORE);
532 exit_genie (p, A68G_RUNTIME_ERROR);
533 }
534 return nil_ref;
535 }
536
537 // Following implements the generator.
538
539 //! @brief Whether a moid needs work in allocation.
540
541 BOOL_T mode_needs_allocation (MOID_T * m)
542 {
543 if (IS_UNION (m)) {
544 return A68G_FALSE;
545 } else {
546 return HAS_ROWS (m);
547 }
548 }
549
550 //! @brief Prepare bounds.
551
552 void genie_compute_bounds (NODE_T * p)
553 {
554 for (; p != NO_NODE; FORWARD (p)) {
555 if (IS (p, BOUNDS_LIST)) {
556 genie_compute_bounds (SUB (p));
557 } else if (IS (p, BOUND)) {
558 genie_compute_bounds (SUB (p));
559 } else if (IS (p, UNIT)) {
560 if (NEXT (p) != NO_NODE && (is_one_of (NEXT (p), COLON_SYMBOL, DOTDOT_SYMBOL, STOP))) {
561 GENIE_UNIT (p);
562 p = NEXT_NEXT (p);
563 } else {
564 // Default lower bound.
565 PUSH_VALUE (p, 1, A68G_INT);
566 }
567 GENIE_UNIT (p);
568 }
569 }
570 }
571
572 //! @brief Prepare bounds for a row.
573
574 void genie_generator_bounds (NODE_T * p)
575 {
576 LOW_STACK_ALERT (p);
577 for (; p != NO_NODE; FORWARD (p)) {
578 if (IS (p, BOUNDS)) {
579 genie_compute_bounds (SUB (p));
580 } else if (IS (p, INDICANT) && IS_LITERALLY (p, "STRING")) {
581 return;
582 } else if (IS (p, INDICANT)) {
583 if (TAX (p) != NO_TAG && HAS_ROWS (MOID (TAX (p)))) {
584 // Continue from definition at MODE A = ....
585 genie_generator_bounds (DEF_NODE (p));
586 }
587 } else if (IS (p, DECLARER) && !mode_needs_allocation (MOID (p))) {
588 return;
589 } else {
590 genie_generator_bounds (SUB (p));
591 }
592 }
593 }
594
595 //! @brief Allocate a structure.
596
597 void genie_generator_field (NODE_T * p, BYTE_T ** faddr, NODE_T ** decl, ADDR_T * cur_sp, ADDR_T * top_sp)
598 {
599 for (; p != NO_NODE; FORWARD (p)) {
600 if (IS (p, STRUCTURED_FIELD)) {
601 genie_generator_field (SUB (p), faddr, decl, cur_sp, top_sp);
602 }
603 if (IS (p, DECLARER)) {
604 (*decl) = SUB (p);
605 FORWARD (p);
606 }
607 if (IS (p, FIELD_IDENTIFIER)) {
608 MOID_T *fmoid = MOID (*decl);
609 if (HAS_ROWS (fmoid) && ISNT (fmoid, UNION_SYMBOL)) {
610 ADDR_T pop_sp = *cur_sp;
611 genie_generator_stowed (*decl, *faddr, NO_REF, cur_sp);
612 *top_sp = *cur_sp;
613 *cur_sp = pop_sp;
614 }
615 (*faddr) += SIZE (fmoid);
616 }
617 }
618 }
619
620 //! @brief Allocate a structure.
621
622 void genie_generator_struct (NODE_T * p, BYTE_T ** faddr, ADDR_T * cur_sp)
623 {
624 for (; p != NO_NODE; FORWARD (p)) {
625 if (IS (p, STRUCTURED_FIELD_LIST)) {
626 genie_generator_struct (SUB (p), faddr, cur_sp);
627 } else if (IS (p, STRUCTURED_FIELD)) {
628 NODE_T *decl = NO_NODE;
629 ADDR_T top_sp = *cur_sp;
630 genie_generator_field (SUB (p), faddr, &decl, cur_sp, &top_sp);
631 *cur_sp = top_sp;
632 }
633 }
634 }
635
636 //! @brief Allocate a stowed object.
637
638 void genie_generator_stowed (NODE_T * p, BYTE_T * addr, NODE_T ** decl, ADDR_T * cur_sp)
639 {
640 if (p == NO_NODE) {
641 return;
642 } else if (IS (p, INDICANT) && IS_LITERALLY (p, "STRING")) {
643 // The standard prelude definition is hard coded here.
644 *((A68G_REF *) addr) = empty_string (p);
645 return;
646 } else if (IS (p, INDICANT) && TAX (p) != NO_TAG) {
647 // Continue from definition at MODE A = ..
648 genie_generator_stowed (DEF_NODE (p), addr, decl, cur_sp);
649 return;
650 } else if (IS (p, DECLARER) && mode_needs_allocation (MOID (p))) {
651 genie_generator_stowed (SUB (p), addr, decl, cur_sp);
652 return;
653 } else if (IS_STRUCT (p)) {
654 BYTE_T *faddr = addr;
655 genie_generator_struct (SUB_NEXT (p), &faddr, cur_sp);
656 return;
657 } else if (IS_FLEX (p)) {
658 genie_generator_stowed (NEXT (p), addr, decl, cur_sp);
659 return;
660 } else if (IS (p, BOUNDS)) {
661 A68G_REF desc;
662 MOID_T *rmod = MOID (p), *smod = MOID (NEXT (p));
663 BYTE_T *bounds = STACK_ADDRESS (*cur_sp);
664 int dim = DIM (DEFLEX (rmod)), esiz = SIZE (smod), rsiz = 1;
665 BOOL_T alloc_sub = A68G_FALSE, alloc_str = A68G_FALSE;
666 NODE_T *in = SUB_NEXT (p);
667 if (IS (in, INDICANT) && IS_LITERALLY (in, "STRING")) {
668 alloc_str = A68G_TRUE;
669 alloc_sub = A68G_FALSE;
670 } else {
671 alloc_sub = mode_needs_allocation (smod);
672 alloc_str = A68G_FALSE;
673 }
674 desc = heap_generator (p, rmod, DESCRIPTOR_SIZE (dim));
675 A68G_ARRAY *arr; A68G_TUPLE *tup;
676 GET_DESCRIPTOR (arr, tup, &desc);
677 for (int k = 0; k < dim; k++) {
678 CHECK_INIT (p, INITIALISED ((A68G_INT *) bounds), M_INT);
679 LWB (&tup[k]) = VALUE ((A68G_INT *) bounds);
680 bounds += SIZE (M_INT);
681 CHECK_INIT (p, INITIALISED ((A68G_INT *) bounds), M_INT);
682 UPB (&tup[k]) = VALUE ((A68G_INT *) bounds);
683 bounds += SIZE (M_INT);
684 SPAN (&tup[k]) = rsiz;
685 SHIFT (&tup[k]) = LWB (&tup[k]) * SPAN (&tup[k]);
686 rsiz *= ROW_SIZE (&tup[k]);
687 }
688 DIM (arr) = dim;
689 MOID (arr) = smod;
690 ELEM_SIZE (arr) = esiz;
691 SLICE_OFFSET (arr) = 0;
692 FIELD_OFFSET (arr) = 0;
693 (*cur_sp) += (dim * 2 * SIZE (M_INT));
694 // Generate a new row. Note that STRING is handled explicitly since
695 // it has implicit bounds
696 if (rsiz == 0) {
697 // Generate a ghost element.
698 ADDR_T top_sp = *cur_sp;
699 ARRAY (arr) = heap_generator (p, rmod, esiz);
700 BYTE_T *elem = ADDRESS (&(ARRAY (arr)));
701 if (alloc_sub) {
702 genie_generator_stowed (NEXT (p), &(elem[0]), NO_REF, cur_sp);
703 top_sp = *cur_sp;
704 } else if (alloc_str) {
705 *(A68G_REF *) elem = empty_string (p);
706 }
707 (*cur_sp) = top_sp;
708 } else {
709 ADDR_T pop_sp = *cur_sp, top_sp = *cur_sp;
710 ARRAY (arr) = heap_generator_2 (p, rmod, rsiz, esiz);
711 BYTE_T *elem = ADDRESS (&(ARRAY (arr)));
712 for (int k = 0; k < rsiz; k++) {
713 if (alloc_sub) {
714 (*cur_sp) = pop_sp;
715 genie_generator_stowed (NEXT (p), &(elem[k * esiz]), NO_REF, cur_sp);
716 top_sp = *cur_sp;
717 } else if (alloc_str) {
718 *(A68G_REF *) (&(elem[k * esiz])) = empty_string (p);
719 }
720 }
721 (*cur_sp) = top_sp;
722 }
723 *(A68G_REF *) addr = desc;
724 return;
725 }
726 }
727
728 //! @brief Generate space and push a REF.
729
730 void genie_generator_internal (NODE_T * p, MOID_T * ref_mode, TAG_T * tag, LEAP_T leap, ADDR_T sp)
731 {
732 // Set up a REF MODE object, either in the stack or in the heap.
733 MOID_T *mode = SUB (ref_mode);
734 A68G_REF name = nil_ref;
735 if (leap == LOC_SYMBOL) {
736 STATUS (&name) = (STATUS_MASK_T) (INIT_MASK | IN_FRAME_MASK);
737 REF_HANDLE (&name) = (A68G_HANDLE *) & nil_handle;
738 OFFSET (&name) = A68G_FP + FRAME_INFO_SIZE + OFFSET (tag);
739 REF_SCOPE (&name) = A68G_FP;
740 } else if (leap == -LOC_SYMBOL && NON_LOCAL (p) != NO_TABLE) {
741 name = heap_generator (p, mode, SIZE (mode));
742 ADDR_T lev;
743 FOLLOW_SL (lev, LEVEL (NON_LOCAL (p)));
744 REF_SCOPE (&name) = lev;
745 } else if (leap == -LOC_SYMBOL) {
746 name = heap_generator (p, mode, SIZE (mode));
747 REF_SCOPE (&name) = A68G_FP;
748 } else if (leap == HEAP_SYMBOL || leap == -HEAP_SYMBOL) {
749 name = heap_generator (p, mode, SIZE (mode));
750 REF_SCOPE (&name) = PRIMAL_SCOPE;
751 } else if (leap == NEW_SYMBOL || leap == -NEW_SYMBOL) {
752 name = heap_generator (p, mode, SIZE (mode));
753 REF_SCOPE (&name) = PRIMAL_SCOPE;
754 } else {
755 ABEND (A68G_TRUE, ERROR_INTERNAL_CONSISTENCY, __func__);
756 }
757 if (HAS_ROWS (mode)) {
758 ADDR_T cur_sp = sp;
759 genie_generator_stowed (p, ADDRESS (&name), NO_REF, &cur_sp);
760 }
761 PUSH_REF (p, name);
762 }
763
764 //! @brief Push a name refering to allocated space.
765
766 PROP_T genie_generator (NODE_T * p)
767 {
768 ADDR_T pop_sp = A68G_SP;
769 if (NEXT_SUB (p) != NO_NODE) {
770 genie_generator_bounds (NEXT_SUB (p));
771 }
772 genie_generator_internal (NEXT_SUB (p), MOID (p), TAX (p), -ATTRIBUTE (SUB (p)), pop_sp);
773 A68G_REF z;
774 POP_REF (p, &z);
775 A68G_SP = pop_sp;
776 PUSH_REF (p, z);
777 PROP_T self;
778 UNIT (&self) = genie_generator;
779 SOURCE (&self) = p;
780 return self;
781 }
782
783 // Control of C heap
784
785 //! @brief Discard_heap.
786
787 void discard_heap (void)
788 {
789 a68g_free (A68G_HEAP);
790 A68G (fixed_heap_pointer) = 0;
791 A68G (temp_heap_pointer) = 0;
792 }
© 2002-2025 J.M. van der Veer (jmvdveer@xs4all.nl)
|