Debian Codegraph @

gcc/m2/pge-boot/GIndexing.cc

raw
1 /* do not edit automatically generated by mc from Indexing. */
2 /* Indexing.mod provides a dynamic indexing mechanism for CARDINAL.
3
4 Copyright (C) 2003-2025 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <[email protected]>.
6
7 This file is part of GNU Modula-2.
8
9 GNU Modula-2 is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GNU Modula-2 is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
22
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
27
28 #include <stdbool.h>
29 # if !defined (PROC_D)
30 # define PROC_D
31 typedef void (*PROC_t) (void);
32 typedef struct { PROC_t proc; } PROC;
33 # endif
34
35 # if !defined (TRUE)
36 # define TRUE (1==1)
37 # endif
38
39 # if !defined (FALSE)
40 # define FALSE (1==0)
41 # endif
42
43 #include <stddef.h>
44 #include <stdlib.h>
45 # include "GStorage.h"
46 # include "Gmcrts.h"
47 #if defined(__cplusplus)
48 # undef NULL
49 # define NULL 0
50 #endif
51 #define _Indexing_C
52
53 #include "GIndexing.h"
54 # include "Glibc.h"
55 # include "GStorage.h"
56 # include "GSYSTEM.h"
57 # include "GM2RTS.h"
58
59 typedef struct Indexing_IndexProcedure_p Indexing_IndexProcedure;
60
61 # define MinSize 128
62 # define DefaultGrowFactor 2
63 typedef struct Indexing__T2_r Indexing__T2;
64
65 typedef void * *Indexing_PtrToAddress;
66
67 typedef unsigned char *Indexing_PtrToByte;
68
69 typedef Indexing__T2 *Indexing_Index__opaque;
70
71 struct Indexing__T2_r {
72 void *ArrayStart;
73 unsigned int ArraySize;
74 unsigned int Used;
75 unsigned int Low;
76 unsigned int High;
77 bool Debug;
78 unsigned int Map;
79 unsigned int GrowFactor;
80 };
81
82
83 /*
84 InitIndexTuned - creates a dynamic array with low indice.
85 The minsize is the initial number of elements the
86 array is allocated and growfactor determines how
87 it will be resized once it becomes full.
88 */
89
90 extern "C" Indexing_Index Indexing_InitIndexTuned (unsigned int low, unsigned int minsize, unsigned int growfactor);
91
92 /*
93 InitIndex - creates and returns an Index.
94 */
95
96 extern "C" Indexing_Index Indexing_InitIndex (unsigned int low);
97
98 /*
99 KillIndex - returns Index to free storage.
100 */
101
102 extern "C" Indexing_Index Indexing_KillIndex (Indexing_Index i);
103
104 /*
105 DebugIndex - turns on debugging within an index.
106 */
107
108 extern "C" Indexing_Index Indexing_DebugIndex (Indexing_Index i);
109
110 /*
111 InBounds - returns TRUE if indice, n, is within the bounds
112 of the dynamic array.
113 */
114
115 extern "C" bool Indexing_InBounds (Indexing_Index i, unsigned int n);
116
117 /*
118 HighIndice - returns the last legally accessible indice of this array.
119 */
120
121 extern "C" unsigned int Indexing_HighIndice (Indexing_Index i);
122
123 /*
124 LowIndice - returns the first legally accessible indice of this array.
125 */
126
127 extern "C" unsigned int Indexing_LowIndice (Indexing_Index i);
128
129 /*
130 PutIndice - places, a, into the dynamic array at position i[n]
131 */
132
133 extern "C" void Indexing_PutIndice (Indexing_Index i, unsigned int n, void * a);
134
135 /*
136 GetIndice - retrieves, element i[n] from the dynamic array.
137 */
138
139 extern "C" void * Indexing_GetIndice (Indexing_Index i, unsigned int n);
140
141 /*
142 IsIndiceInIndex - returns TRUE if, a, is in the index, i.
143 */
144
145 extern "C" bool Indexing_IsIndiceInIndex (Indexing_Index i, void * a);
146
147 /*
148 RemoveIndiceFromIndex - removes, a, from Index, i.
149 */
150
151 extern "C" void Indexing_RemoveIndiceFromIndex (Indexing_Index i, void * a);
152
153 /*
154 DeleteIndice - delete i[j] from the array.
155 */
156
157 extern "C" void Indexing_DeleteIndice (Indexing_Index i, unsigned int j);
158
159 /*
160 IncludeIndiceIntoIndex - if the indice is not in the index, then
161 add it at the end.
162 */
163
164 extern "C" void Indexing_IncludeIndiceIntoIndex (Indexing_Index i, void * a);
165
166 /*
167 ForeachIndiceInIndexDo - for each j indice of i, call procedure p(i[j])
168 */
169
170 extern "C" void Indexing_ForeachIndiceInIndexDo (Indexing_Index i, Indexing_IndexProcedure p);
171
172 /*
173 IsEmpty - return TRUE if the array has no entries it.
174 */
175
176 extern "C" bool Indexing_IsEmpty (Indexing_Index i);
177
178 /*
179 FindIndice - returns the indice containing a.
180 It returns zero if a is not found in array i.
181 */
182
183 extern "C" unsigned int Indexing_FindIndice (Indexing_Index i, void * a);
184
185
186 /*
187 InitIndexTuned - creates a dynamic array with low indice.
188 The minsize is the initial number of elements the
189 array is allocated and growfactor determines how
190 it will be resized once it becomes full.
191 */
192
193 extern "C" Indexing_Index Indexing_InitIndexTuned (unsigned int low, unsigned int minsize, unsigned int growfactor)
194 {
195 Indexing_Index__opaque i;
196
197 Storage_ALLOCATE ((void **) &i, sizeof (Indexing__T2));
198 i->Low = low;
199 i->High = 0;
200 i->ArraySize = minsize*sizeof (void *);
201 Storage_ALLOCATE (&i->ArrayStart, i->ArraySize);
202 i->ArrayStart = libc_memset (i->ArrayStart, 0, static_cast<size_t> (i->ArraySize));
203 i->Debug = false;
204 i->Used = 0;
205 i->Map = (unsigned int) 0;
206 i->GrowFactor = growfactor;
207 return static_cast<Indexing_Index> (i);
208 /* static analysis guarentees a RETURN statement will be used before here. */
209 __builtin_unreachable ();
210 }
211
212
213 /*
214 InitIndex - creates and returns an Index.
215 */
216
217 extern "C" Indexing_Index Indexing_InitIndex (unsigned int low)
218 {
219 return Indexing_InitIndexTuned (low, MinSize, DefaultGrowFactor);
220 /* static analysis guarentees a RETURN statement will be used before here. */
221 __builtin_unreachable ();
222 }
223
224
225 /*
226 KillIndex - returns Index to free storage.
227 */
228
229 extern "C" Indexing_Index Indexing_KillIndex (Indexing_Index i)
230 {
231 Storage_DEALLOCATE (&static_cast<Indexing_Index__opaque> (i)->ArrayStart, static_cast<Indexing_Index__opaque> (i)->ArraySize);
232 Storage_DEALLOCATE ((void **) &i, sizeof (Indexing__T2));
233 return static_cast<Indexing_Index> (NULL);
234 /* static analysis guarentees a RETURN statement will be used before here. */
235 __builtin_unreachable ();
236 }
237
238
239 /*
240 DebugIndex - turns on debugging within an index.
241 */
242
243 extern "C" Indexing_Index Indexing_DebugIndex (Indexing_Index i)
244 {
245 static_cast<Indexing_Index__opaque> (i)->Debug = true;
246 return i;
247 /* static analysis guarentees a RETURN statement will be used before here. */
248 __builtin_unreachable ();
249 }
250
251
252 /*
253 InBounds - returns TRUE if indice, n, is within the bounds
254 of the dynamic array.
255 */
256
257 extern "C" bool Indexing_InBounds (Indexing_Index i, unsigned int n)
258 {
259 if (i == NULL)
260 {
261 M2RTS_HALT (-1);
262 __builtin_unreachable ();
263 }
264 else
265 {
266 return (n >= static_cast<Indexing_Index__opaque> (i)->Low) && (n <= static_cast<Indexing_Index__opaque> (i)->High);
267 }
268 ReturnException ("../../gcc/m2/gm2-libs/Indexing.def", 25, 1);
269 __builtin_unreachable ();
270 }
271
272
273 /*
274 HighIndice - returns the last legally accessible indice of this array.
275 */
276
277 extern "C" unsigned int Indexing_HighIndice (Indexing_Index i)
278 {
279 if (i == NULL)
280 {
281 M2RTS_HALT (-1);
282 __builtin_unreachable ();
283 }
284 else
285 {
286 return static_cast<Indexing_Index__opaque> (i)->High;
287 }
288 ReturnException ("../../gcc/m2/gm2-libs/Indexing.def", 25, 1);
289 __builtin_unreachable ();
290 }
291
292
293 /*
294 LowIndice - returns the first legally accessible indice of this array.
295 */
296
297 extern "C" unsigned int Indexing_LowIndice (Indexing_Index i)
298 {
299 if (i == NULL)
300 {
301 M2RTS_HALT (-1);
302 __builtin_unreachable ();
303 }
304 else
305 {
306 return static_cast<Indexing_Index__opaque> (i)->Low;
307 }
308 ReturnException ("../../gcc/m2/gm2-libs/Indexing.def", 25, 1);
309 __builtin_unreachable ();
310 }
311
312
313 /*
314 PutIndice - places, a, into the dynamic array at position i[n]
315 */
316
317 extern "C" void Indexing_PutIndice (Indexing_Index i, unsigned int n, void * a)
318 {
319 typedef unsigned int * *PutIndice__T1;
320
321 unsigned int oldSize;
322 void * b;
323 PutIndice__T1 p;
324
325 if (! (Indexing_InBounds (i, n)))
326 {
327 /* avoid gcc warning by using compound statement even if not strictly necessary. */
328 if (n < static_cast<Indexing_Index__opaque> (i)->Low)
329 {
330 M2RTS_HALT (-1);
331 __builtin_unreachable ();
332 }
333 else
334 {
335 oldSize = static_cast<Indexing_Index__opaque> (i)->ArraySize;
336 while (((n-static_cast<Indexing_Index__opaque> (i)->Low)*sizeof (void *)) >= static_cast<Indexing_Index__opaque> (i)->ArraySize)
337 {
338 static_cast<Indexing_Index__opaque> (i)->ArraySize = static_cast<Indexing_Index__opaque> (i)->ArraySize*static_cast<Indexing_Index__opaque> (i)->GrowFactor;
339 }
340 if (oldSize != static_cast<Indexing_Index__opaque> (i)->ArraySize)
341 {
342 /*
343 IF Debug
344 THEN
345 printf2('increasing memory hunk from %d to %d
346 ',
347 oldSize, ArraySize)
348 END ;
349 */
350 Storage_REALLOCATE (&static_cast<Indexing_Index__opaque> (i)->ArrayStart, static_cast<Indexing_Index__opaque> (i)->ArraySize);
351 /* and initialize the remainder of the array to NIL */
352 b = static_cast<Indexing_Index__opaque> (i)->ArrayStart;
353 b = reinterpret_cast<void *> (reinterpret_cast<char *> (b)+oldSize);
354 b = libc_memset (b, 0, static_cast<size_t> (static_cast<Indexing_Index__opaque> (i)->ArraySize-oldSize));
355 }
356 static_cast<Indexing_Index__opaque> (i)->High = n;
357 }
358 }
359 b = static_cast<Indexing_Index__opaque> (i)->ArrayStart;
360 b = reinterpret_cast<void *> (reinterpret_cast<char *> (b)+(n-static_cast<Indexing_Index__opaque> (i)->Low)*sizeof (void *));
361 p = static_cast<PutIndice__T1> (b);
362 (*p) = static_cast<unsigned int *> (a);
363 static_cast<Indexing_Index__opaque> (i)->Used += 1;
364 if (static_cast<Indexing_Index__opaque> (i)->Debug)
365 {
366 if (n < 32)
367 {
368 static_cast<Indexing_Index__opaque> (i)->Map |= (1 << (n ));
369 }
370 }
371 }
372
373
374 /*
375 GetIndice - retrieves, element i[n] from the dynamic array.
376 */
377
378 extern "C" void * Indexing_GetIndice (Indexing_Index i, unsigned int n)
379 {
380 Indexing_PtrToByte b;
381 Indexing_PtrToAddress p;
382
383 if (! (Indexing_InBounds (i, n)))
384 {
385 M2RTS_HALT (-1);
386 __builtin_unreachable ();
387 }
388 b = static_cast<Indexing_PtrToByte> (static_cast<Indexing_Index__opaque> (i)->ArrayStart);
389 b += (n-static_cast<Indexing_Index__opaque> (i)->Low)*sizeof (void *);
390 p = (Indexing_PtrToAddress) (b);
391 if (static_cast<Indexing_Index__opaque> (i)->Debug)
392 {
393 if (((n < 32) && (! ((((1 << (n)) & (static_cast<Indexing_Index__opaque> (i)->Map)) != 0)))) && ((*p) != NULL))
394 {
395 M2RTS_HALT (-1);
396 __builtin_unreachable ();
397 }
398 }
399 return (*p);
400 /* static analysis guarentees a RETURN statement will be used before here. */
401 __builtin_unreachable ();
402 }
403
404
405 /*
406 IsIndiceInIndex - returns TRUE if, a, is in the index, i.
407 */
408
409 extern "C" bool Indexing_IsIndiceInIndex (Indexing_Index i, void * a)
410 {
411 unsigned int j;
412 Indexing_PtrToByte b;
413 Indexing_PtrToAddress p;
414
415 j = static_cast<Indexing_Index__opaque> (i)->Low;
416 b = static_cast<Indexing_PtrToByte> (static_cast<Indexing_Index__opaque> (i)->ArrayStart);
417 while (j <= static_cast<Indexing_Index__opaque> (i)->High)
418 {
419 p = (Indexing_PtrToAddress) (b);
420 if ((*p) == a)
421 {
422 return true;
423 }
424 /* we must not INC(p, ..) as p2c gets confused */
425 b += sizeof (void *);
426 j += 1;
427 }
428 return false;
429 /* static analysis guarentees a RETURN statement will be used before here. */
430 __builtin_unreachable ();
431 }
432
433
434 /*
435 RemoveIndiceFromIndex - removes, a, from Index, i.
436 */
437
438 extern "C" void Indexing_RemoveIndiceFromIndex (Indexing_Index i, void * a)
439 {
440 unsigned int j;
441 Indexing_PtrToAddress p;
442 Indexing_PtrToByte b;
443
444 j = static_cast<Indexing_Index__opaque> (i)->Low;
445 b = static_cast<Indexing_PtrToByte> (static_cast<Indexing_Index__opaque> (i)->ArrayStart);
446 while (j <= static_cast<Indexing_Index__opaque> (i)->High)
447 {
448 p = (Indexing_PtrToAddress) (b);
449 b += sizeof (void *);
450 if ((*p) == a)
451 {
452 Indexing_DeleteIndice (i, j);
453 }
454 j += 1;
455 }
456 }
457
458
459 /*
460 DeleteIndice - delete i[j] from the array.
461 */
462
463 extern "C" void Indexing_DeleteIndice (Indexing_Index i, unsigned int j)
464 {
465 Indexing_PtrToAddress p;
466 Indexing_PtrToByte b;
467
468 if (Indexing_InBounds (i, j))
469 {
470 b = static_cast<Indexing_PtrToByte> (static_cast<Indexing_Index__opaque> (i)->ArrayStart);
471 b += sizeof (void *)*(j-static_cast<Indexing_Index__opaque> (i)->Low);
472 p = (Indexing_PtrToAddress) (b);
473 b += sizeof (void *);
474 p = static_cast<Indexing_PtrToAddress> (libc_memmove (reinterpret_cast <void *> (p), reinterpret_cast <void *> (b), static_cast<size_t> ((static_cast<Indexing_Index__opaque> (i)->High-j)*sizeof (void *))));
475 static_cast<Indexing_Index__opaque> (i)->High -= 1;
476 static_cast<Indexing_Index__opaque> (i)->Used -= 1;
477 }
478 else
479 {
480 M2RTS_HALT (-1);
481 __builtin_unreachable ();
482 }
483 }
484
485
486 /*
487 IncludeIndiceIntoIndex - if the indice is not in the index, then
488 add it at the end.
489 */
490
491 extern "C" void Indexing_IncludeIndiceIntoIndex (Indexing_Index i, void * a)
492 {
493 if (! (Indexing_IsIndiceInIndex (i, a)))
494 {
495 /* avoid gcc warning by using compound statement even if not strictly necessary. */
496 if (static_cast<Indexing_Index__opaque> (i)->Used == 0)
497 {
498 Indexing_PutIndice (i, Indexing_LowIndice (i), a);
499 }
500 else
501 {
502 Indexing_PutIndice (i, (Indexing_HighIndice (i))+1, a);
503 }
504 }
505 }
506
507
508 /*
509 ForeachIndiceInIndexDo - for each j indice of i, call procedure p(i[j])
510 */
511
512 extern "C" void Indexing_ForeachIndiceInIndexDo (Indexing_Index i, Indexing_IndexProcedure p)
513 {
514 unsigned int j;
515
516 j = Indexing_LowIndice (i);
517 while (j <= (Indexing_HighIndice (i)))
518 {
519 (*p.proc) (Indexing_GetIndice (i, j));
520 j += 1;
521 }
522 }
523
524
525 /*
526 IsEmpty - return TRUE if the array has no entries it.
527 */
528
529 extern "C" bool Indexing_IsEmpty (Indexing_Index i)
530 {
531 return static_cast<Indexing_Index__opaque> (i)->Used == 0;
532 /* static analysis guarentees a RETURN statement will be used before here. */
533 __builtin_unreachable ();
534 }
535
536
537 /*
538 FindIndice - returns the indice containing a.
539 It returns zero if a is not found in array i.
540 */
541
542 extern "C" unsigned int Indexing_FindIndice (Indexing_Index i, void * a)
543 {
544 unsigned int j;
545 Indexing_PtrToAddress p;
546 Indexing_PtrToByte b;
547
548 j = static_cast<Indexing_Index__opaque> (i)->Low;
549 b = static_cast<Indexing_PtrToByte> (static_cast<Indexing_Index__opaque> (i)->ArrayStart);
550 while (j <= static_cast<Indexing_Index__opaque> (i)->High)
551 {
552 p = (Indexing_PtrToAddress) (b);
553 b += sizeof (void *);
554 if ((*p) == a)
555 {
556 return j;
557 }
558 j += 1;
559 }
560 return 0;
561 /* static analysis guarentees a RETURN statement will be used before here. */
562 __builtin_unreachable ();
563 }
564
565 extern "C" void _M2_Indexing_init (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
566 {
567 }
568
569 extern "C" void _M2_Indexing_fini (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
570 {
571 }