Debian Codegraph @

gcc/m2/mc-boot/GIndexing.cc

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