Line data Source code
1 : /* do not edit automatically generated by mc from M2StackWord. */
2 : /* M2StackWord.mod provides a generic stack for WORD sized objects.
3 :
4 : Copyright (C) 2001-2026 Free Software Foundation, Inc.
5 : Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
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 : You should have received a copy of the GNU General Public License
20 : along with GNU Modula-2; see the file COPYING3. If not see
21 : <http://www.gnu.org/licenses/>. */
22 :
23 : #include "config.h"
24 : #include "system.h"
25 : #include <stdbool.h>
26 : # if !defined (PROC_D)
27 : # define PROC_D
28 : typedef void (*PROC_t) (void);
29 : typedef struct { PROC_t proc; } PROC;
30 : # endif
31 :
32 : # include "GStorage.h"
33 : # include "Gmcrts.h"
34 : #if defined(__cplusplus)
35 : # undef NULL
36 : # define NULL 0
37 : #endif
38 : #define _M2StackWord_C
39 :
40 : #include "GM2StackWord.h"
41 : # include "GStorage.h"
42 : # include "GM2Error.h"
43 : # include "GM2Debug.h"
44 :
45 : # define MaxBucket 10
46 : typedef struct M2StackWord_BucketWord_r M2StackWord_BucketWord;
47 :
48 : typedef struct M2StackWord_StackDescriptor_r M2StackWord_StackDescriptor;
49 :
50 : typedef struct M2StackWord__T1_a M2StackWord__T1;
51 :
52 : typedef M2StackWord_BucketWord *M2StackWord_StackBucketWord;
53 :
54 : typedef M2StackWord_StackDescriptor *M2StackWord_StackOfWord__opaque;
55 :
56 : struct M2StackWord_StackDescriptor_r {
57 : M2StackWord_StackBucketWord tail;
58 : };
59 :
60 : struct M2StackWord__T1_a { unsigned int array[(MaxBucket-1)+1]; };
61 : struct M2StackWord_BucketWord_r {
62 : M2StackWord__T1 bucket;
63 : unsigned int items;
64 : M2StackWord_StackBucketWord last;
65 : };
66 :
67 :
68 : /*
69 : InitStackWord - creates and returns a new stack.
70 : */
71 :
72 : extern "C" M2StackWord_StackOfWord M2StackWord_InitStackWord (void);
73 :
74 : /*
75 : KillStackWord - destroys a stack, returning NIL.
76 : */
77 :
78 : extern "C" M2StackWord_StackOfWord M2StackWord_KillStackWord (M2StackWord_StackOfWord s);
79 :
80 : /*
81 : PushWord - pushes a word, w, onto, s.
82 : */
83 :
84 : extern "C" void M2StackWord_PushWord (M2StackWord_StackOfWord s, unsigned int w);
85 :
86 : /*
87 : PopWord - pops an element from stack, s.
88 : */
89 :
90 : extern "C" unsigned int M2StackWord_PopWord (M2StackWord_StackOfWord s);
91 :
92 : /*
93 : IsEmptyWord - returns TRUE if stack, s, is empty.
94 : */
95 :
96 : extern "C" bool M2StackWord_IsEmptyWord (M2StackWord_StackOfWord s);
97 :
98 : /*
99 : PeepWord - returns the element at, n, items below in the stack.
100 : Top of stack can be seen via Peep(s, 1)
101 : */
102 :
103 : extern "C" unsigned int M2StackWord_PeepWord (M2StackWord_StackOfWord s, unsigned int n);
104 :
105 : /*
106 : ReduceWord - reduce the stack by n elements.
107 : */
108 :
109 : extern "C" void M2StackWord_ReduceWord (M2StackWord_StackOfWord s, unsigned int n);
110 :
111 : /*
112 : RemoveTop - throw away the top element of the stack.
113 : */
114 :
115 : extern "C" void M2StackWord_RemoveTop (M2StackWord_StackOfWord s);
116 :
117 : /*
118 : NoOfItemsInStackWord - returns the number of items held in the stack, s.
119 : */
120 :
121 : extern "C" unsigned int M2StackWord_NoOfItemsInStackWord (M2StackWord_StackOfWord s);
122 :
123 : /*
124 : KillBucket - destroys a StackBucketWord and returns, NIL.
125 : */
126 :
127 : static M2StackWord_StackBucketWord KillBucket (M2StackWord_StackBucketWord b);
128 :
129 : /*
130 : InitBucket - returns an empty StackBucketWord.
131 : */
132 :
133 : static M2StackWord_StackBucketWord InitBucket (M2StackWord_StackBucketWord l);
134 :
135 :
136 : /*
137 : KillBucket - destroys a StackBucketWord and returns, NIL.
138 : */
139 :
140 1335402 : static M2StackWord_StackBucketWord KillBucket (M2StackWord_StackBucketWord b)
141 : {
142 1335402 : if (b != NULL)
143 : {
144 667701 : b = KillBucket (b->last);
145 667701 : Storage_DEALLOCATE ((void **) &b, sizeof (M2StackWord_BucketWord));
146 : }
147 1335402 : return NULL;
148 : /* static analysis guarentees a RETURN statement will be used before here. */
149 : __builtin_unreachable ();
150 : }
151 :
152 :
153 : /*
154 : InitBucket - returns an empty StackBucketWord.
155 : */
156 :
157 3569047 : static M2StackWord_StackBucketWord InitBucket (M2StackWord_StackBucketWord l)
158 : {
159 3569047 : M2StackWord_StackBucketWord b;
160 :
161 0 : Storage_ALLOCATE ((void **) &b, sizeof (M2StackWord_BucketWord));
162 3569047 : b->items = 0;
163 3569047 : b->last = l;
164 3569047 : return b;
165 : /* static analysis guarentees a RETURN statement will be used before here. */
166 : __builtin_unreachable ();
167 : }
168 :
169 :
170 : /*
171 : InitStackWord - creates and returns a new stack.
172 : */
173 :
174 951789 : extern "C" M2StackWord_StackOfWord M2StackWord_InitStackWord (void)
175 : {
176 951789 : M2StackWord_StackOfWord__opaque s;
177 :
178 951789 : Storage_ALLOCATE ((void **) &s, sizeof (M2StackWord_StackDescriptor));
179 951789 : s->tail = NULL;
180 951789 : return static_cast<M2StackWord_StackOfWord> (s);
181 : /* static analysis guarentees a RETURN statement will be used before here. */
182 : __builtin_unreachable ();
183 : }
184 :
185 :
186 : /*
187 : KillStackWord - destroys a stack, returning NIL.
188 : */
189 :
190 667701 : extern "C" M2StackWord_StackOfWord M2StackWord_KillStackWord (M2StackWord_StackOfWord s)
191 : {
192 667701 : if (s != NULL)
193 : {
194 667701 : static_cast<M2StackWord_StackOfWord__opaque> (s)->tail = KillBucket (static_cast<M2StackWord_StackOfWord__opaque> (s)->tail);
195 667701 : Storage_DEALLOCATE ((void **) &s, sizeof (M2StackWord_StackDescriptor));
196 : }
197 667701 : return static_cast<M2StackWord_StackOfWord> (NULL);
198 : /* static analysis guarentees a RETURN statement will be used before here. */
199 : __builtin_unreachable ();
200 : }
201 :
202 :
203 : /*
204 : PushWord - pushes a word, w, onto, s.
205 : */
206 :
207 212810256 : extern "C" void M2StackWord_PushWord (M2StackWord_StackOfWord s, unsigned int w)
208 : {
209 212810256 : if (s == NULL)
210 : {
211 0 : M2Error_InternalError ((const char *) "stack has not been initialized", 30);
212 : }
213 : else
214 : {
215 212810256 : if ((static_cast<M2StackWord_StackOfWord__opaque> (s)->tail == NULL) || (static_cast<M2StackWord_StackOfWord__opaque> (s)->tail->items == MaxBucket))
216 : {
217 3569047 : static_cast<M2StackWord_StackOfWord__opaque> (s)->tail = InitBucket (static_cast<M2StackWord_StackOfWord__opaque> (s)->tail);
218 : }
219 212810256 : if (static_cast<M2StackWord_StackOfWord__opaque> (s)->tail->items < MaxBucket)
220 : {
221 212810256 : static_cast<M2StackWord_StackOfWord__opaque> (s)->tail->bucket.array[static_cast<M2StackWord_StackOfWord__opaque> (s)->tail->items] = w;
222 212810256 : static_cast<M2StackWord_StackOfWord__opaque> (s)->tail->items += 1;
223 : }
224 : }
225 212810256 : }
226 :
227 :
228 : /*
229 : PopWord - pops an element from stack, s.
230 : */
231 :
232 211438601 : extern "C" unsigned int M2StackWord_PopWord (M2StackWord_StackOfWord s)
233 : {
234 211438601 : M2StackWord_StackBucketWord b;
235 :
236 211438601 : if (s == NULL)
237 : {
238 0 : M2Error_InternalError ((const char *) "stack has not been initialized", 30);
239 : }
240 : else
241 : {
242 211438601 : if (static_cast<M2StackWord_StackOfWord__opaque> (s)->tail == NULL)
243 : {
244 0 : M2Error_InternalError ((const char *) "stack underflow", 15);
245 : }
246 : else
247 : {
248 211438601 : if (static_cast<M2StackWord_StackOfWord__opaque> (s)->tail->items == 0)
249 : {
250 2709386 : b = static_cast<M2StackWord_StackOfWord__opaque> (s)->tail;
251 2709386 : if (b == NULL)
252 : {
253 : M2Error_InternalError ((const char *) "stack underflow", 15);
254 : }
255 : else
256 : {
257 2709386 : static_cast<M2StackWord_StackOfWord__opaque> (s)->tail = b->last;
258 : }
259 2709386 : Storage_DEALLOCATE ((void **) &b, sizeof (M2StackWord_BucketWord));
260 : }
261 211438601 : static_cast<M2StackWord_StackOfWord__opaque> (s)->tail->items -= 1;
262 211438601 : return static_cast<M2StackWord_StackOfWord__opaque> (s)->tail->bucket.array[static_cast<M2StackWord_StackOfWord__opaque> (s)->tail->items];
263 : }
264 : }
265 : ReturnException ("/home/worker/buildworker/tiber-lcov/build/gcc/m2/gm2-compiler/M2StackWord.def", 20, 1);
266 : __builtin_unreachable ();
267 : }
268 :
269 :
270 : /*
271 : IsEmptyWord - returns TRUE if stack, s, is empty.
272 : */
273 :
274 802319 : extern "C" bool M2StackWord_IsEmptyWord (M2StackWord_StackOfWord s)
275 : {
276 802319 : return (s == NULL) || (static_cast<M2StackWord_StackOfWord__opaque> (s)->tail == NULL);
277 : /* static analysis guarentees a RETURN statement will be used before here. */
278 : __builtin_unreachable ();
279 : }
280 :
281 :
282 : /*
283 : PeepWord - returns the element at, n, items below in the stack.
284 : Top of stack can be seen via Peep(s, 1)
285 : */
286 :
287 18453429 : extern "C" unsigned int M2StackWord_PeepWord (M2StackWord_StackOfWord s, unsigned int n)
288 : {
289 18453429 : M2StackWord_StackBucketWord b;
290 :
291 18453429 : if (static_cast<M2StackWord_StackOfWord__opaque> (s)->tail == NULL)
292 : {
293 0 : M2Error_InternalError ((const char *) "stack underflow", 15);
294 : }
295 : else
296 : {
297 18453429 : if (static_cast<M2StackWord_StackOfWord__opaque> (s)->tail->items == 0)
298 : {
299 6 : b = static_cast<M2StackWord_StackOfWord__opaque> (s)->tail;
300 6 : if (b == NULL)
301 : {
302 : M2Error_InternalError ((const char *) "stack underflow", 15);
303 : }
304 : else
305 : {
306 6 : static_cast<M2StackWord_StackOfWord__opaque> (s)->tail = b->last;
307 : }
308 6 : Storage_DEALLOCATE ((void **) &b, sizeof (M2StackWord_BucketWord));
309 : }
310 18453429 : b = static_cast<M2StackWord_StackOfWord__opaque> (s)->tail;
311 18453429 : while (n >= 1)
312 : {
313 18453429 : if (b == NULL)
314 : {
315 0 : M2Error_InternalError ((const char *) "stack underflow", 15);
316 : }
317 18453429 : else if (b->items >= n)
318 : {
319 : /* avoid dangling else. */
320 18453429 : return b->bucket.array[b->items-n];
321 : }
322 : else
323 : {
324 : /* avoid dangling else. */
325 0 : M2Debug_Assert (b->items < n);
326 0 : n -= b->items;
327 0 : b = b->last;
328 : }
329 : }
330 0 : M2Error_InternalError ((const char *) "stack underflow", 15);
331 : }
332 : ReturnException ("/home/worker/buildworker/tiber-lcov/build/gcc/m2/gm2-compiler/M2StackWord.def", 20, 1);
333 : __builtin_unreachable ();
334 : }
335 :
336 :
337 : /*
338 : ReduceWord - reduce the stack by n elements.
339 : */
340 :
341 157852 : extern "C" void M2StackWord_ReduceWord (M2StackWord_StackOfWord s, unsigned int n)
342 : {
343 157852 : M2StackWord_StackBucketWord b;
344 :
345 157852 : if (static_cast<M2StackWord_StackOfWord__opaque> (s)->tail == NULL)
346 : {
347 0 : M2Error_InternalError ((const char *) "stack underflow", 15);
348 : }
349 : else
350 : {
351 157852 : if (static_cast<M2StackWord_StackOfWord__opaque> (s)->tail->items == 0)
352 : {
353 0 : b = static_cast<M2StackWord_StackOfWord__opaque> (s)->tail;
354 0 : if (b == NULL)
355 : {
356 : M2Error_InternalError ((const char *) "stack underflow", 15);
357 : }
358 : else
359 : {
360 0 : static_cast<M2StackWord_StackOfWord__opaque> (s)->tail = b->last;
361 : }
362 0 : Storage_DEALLOCATE ((void **) &b, sizeof (M2StackWord_BucketWord));
363 : }
364 157852 : for (;;)
365 : {
366 157852 : if (static_cast<M2StackWord_StackOfWord__opaque> (s)->tail == NULL)
367 : {
368 0 : M2Error_InternalError ((const char *) "stack underflow", 15);
369 : }
370 157852 : else if (static_cast<M2StackWord_StackOfWord__opaque> (s)->tail->items >= n)
371 : {
372 : /* avoid dangling else. */
373 157852 : static_cast<M2StackWord_StackOfWord__opaque> (s)->tail->items -= n;
374 157852 : return; /* all done exit */
375 : }
376 : else
377 : {
378 : /* avoid dangling else. */
379 0 : b = static_cast<M2StackWord_StackOfWord__opaque> (s)->tail;
380 0 : n -= b->items;
381 0 : static_cast<M2StackWord_StackOfWord__opaque> (s)->tail = static_cast<M2StackWord_StackOfWord__opaque> (s)->tail->last;
382 0 : Storage_DEALLOCATE ((void **) &b, sizeof (M2StackWord_BucketWord));
383 : }
384 : }
385 : }
386 : }
387 :
388 :
389 : /*
390 : RemoveTop - throw away the top element of the stack.
391 : */
392 :
393 157852 : extern "C" void M2StackWord_RemoveTop (M2StackWord_StackOfWord s)
394 : {
395 157852 : M2StackWord_ReduceWord (s, 1);
396 157852 : }
397 :
398 :
399 : /*
400 : NoOfItemsInStackWord - returns the number of items held in the stack, s.
401 : */
402 :
403 240 : extern "C" unsigned int M2StackWord_NoOfItemsInStackWord (M2StackWord_StackOfWord s)
404 : {
405 240 : M2StackWord_StackBucketWord b;
406 240 : unsigned int n;
407 :
408 240 : if (M2StackWord_IsEmptyWord (s))
409 : {
410 : return 0;
411 : }
412 : else
413 : {
414 204 : n = 0;
415 204 : b = static_cast<M2StackWord_StackOfWord__opaque> (s)->tail;
416 408 : while (b != NULL)
417 : {
418 204 : n += b->items;
419 204 : b = b->last;
420 : }
421 : return n;
422 : }
423 : /* static analysis guarentees a RETURN statement will be used before here. */
424 : __builtin_unreachable ();
425 : }
426 :
427 0 : extern "C" void _M2_M2StackWord_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
428 : {
429 0 : }
430 :
431 0 : extern "C" void _M2_M2StackWord_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
432 : {
433 0 : }
|