Debian Codegraph @

gcc/m2/gm2-compiler/M2StackAddress.mod

raw
1 (* M2StackAddress.mod provides a generic stack for ADDRESS sized objects.
2
3 Copyright (C) 2001-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. *)
21
22 IMPLEMENTATION MODULE M2StackAddress ;
23
24 FROM Storage IMPORT ALLOCATE, DEALLOCATE ;
25 FROM M2Error IMPORT InternalError ;
26 FROM M2Debug IMPORT Assert ;
27
28 CONST
29 MaxBucket = 10 ;
30
31 TYPE
32 StackBucket = POINTER TO RECORD
33 bucket: ARRAY [0..MaxBucket-1] OF ADDRESS ;
34 items : CARDINAL ;
35 prev : StackBucket ;
36 END ;
37
38 StackOfAddress = POINTER TO RECORD
39 tail: StackBucket ;
40 END ;
41
42
43 (*
44 InitStackAddress - creates and returns a new stack.
45 *)
46
47 PROCEDURE InitStackAddress () : StackOfAddress ;
48 VAR
49 s: StackOfAddress ;
50 BEGIN
51 NEW (s) ;
52 WITH s^ DO
53 tail := NIL
54 END ;
55 RETURN s
56 END InitStackAddress ;
57
58
59 (*
60 KillBucket - destroys a StackBucket and returns, NIL.
61 *)
62
63 PROCEDURE KillBucket (b: StackBucket) : StackBucket ;
64 BEGIN
65 IF b # NIL
66 THEN
67 b := KillBucket (b^.prev) ;
68 DISPOSE (b)
69 END ;
70 RETURN NIL
71 END KillBucket ;
72
73
74 (*
75 KillStackAddress - destroys a stack, returning NIL.
76 *)
77
78 PROCEDURE KillStackAddress (s: StackOfAddress) : StackOfAddress ;
79 BEGIN
80 IF s#NIL
81 THEN
82 s^.tail := KillBucket (s^.tail) ;
83 DISPOSE (s)
84 END ;
85 RETURN NIL
86 END KillStackAddress ;
87
88
89 (*
90 InitBucket - returns an empty StackBucket.
91 *)
92
93 PROCEDURE InitBucket (l: StackBucket) : StackBucket ;
94 VAR
95 b: StackBucket ;
96 BEGIN
97 NEW(b) ;
98 WITH b^ DO
99 items := 0 ;
100 prev := l
101 END ;
102 RETURN( b )
103 END InitBucket ;
104
105
106 (*
107 PushAddress - pushes a word, w, onto, s.
108 *)
109
110 PROCEDURE PushAddress (s: StackOfAddress; w: ADDRESS) ;
111 BEGIN
112 IF s=NIL
113 THEN
114 InternalError ('stack has not been initialized')
115 ELSE
116 WITH s^ DO
117 IF (tail=NIL) OR (tail^.items=MaxBucket)
118 THEN
119 tail := InitBucket(tail)
120 END ;
121 WITH tail^ DO
122 IF items<MaxBucket
123 THEN
124 bucket[items] := w ;
125 INC(items)
126 END
127 END
128 END
129 END
130 END PushAddress ;
131
132
133 (*
134 PopAddress - pops an element from stack, s.
135 *)
136
137 PROCEDURE PopAddress (s: StackOfAddress) : ADDRESS ;
138 VAR
139 b: StackBucket ;
140 BEGIN
141 IF s=NIL
142 THEN
143 InternalError ('stack has not been initialized')
144 ELSE
145 IF s^.tail=NIL
146 THEN
147 InternalError ('stack underflow')
148 ELSE
149 IF s^.tail^.items=0
150 THEN
151 b := s^.tail ;
152 IF b=NIL
153 THEN
154 InternalError ('stack underflow')
155 ELSE
156 s^.tail := b^.prev
157 END ;
158 DISPOSE(b)
159 END ;
160 IF s^.tail = NIL
161 THEN
162 InternalError ('stack underflow')
163 ELSE
164 WITH s^.tail^ DO
165 DEC(items) ;
166 RETURN( bucket[items] )
167 END
168 END
169 END
170 END
171 END PopAddress ;
172
173
174 (*
175 IsEmptyAddress - returns TRUE if stack, s, is empty.
176 *)
177
178 PROCEDURE IsEmptyAddress (s: StackOfAddress) : BOOLEAN ;
179 BEGIN
180 RETURN( (s=NIL) OR (s^.tail=NIL) )
181 END IsEmptyAddress ;
182
183
184 (*
185 PeepAddress - returns the element at, n, items below in the stack.
186 Top of stack can be seen via Peep(s, 1)
187 *)
188
189 PROCEDURE PeepAddress (s: StackOfAddress; n: CARDINAL) : ADDRESS ;
190 VAR
191 b: StackBucket ;
192 BEGIN
193 IF s^.tail=NIL
194 THEN
195 InternalError ('stack underflow')
196 ELSE
197 IF s^.tail^.items=0
198 THEN
199 b := s^.tail ;
200 IF b=NIL
201 THEN
202 InternalError ('stack underflow')
203 ELSE
204 s^.tail := b^.prev
205 END ;
206 DISPOSE(b)
207 END ;
208 b := s^.tail ;
209 WHILE n>=1 DO
210 IF b=NIL
211 THEN
212 InternalError ('stack underflow')
213 ELSIF b^.items>=n
214 THEN
215 RETURN( b^.bucket[b^.items-n] )
216 ELSE
217 Assert(b^.items<n) ;
218 DEC(n, b^.items) ;
219 b := b^.prev
220 END
221 END ;
222 InternalError ('stack underflow')
223 END
224 END PeepAddress ;
225
226
227 (*
228 ReduceAddress - reduce the stack by n elements.
229 *)
230
231 PROCEDURE ReduceAddress (s: StackOfAddress; n: CARDINAL) ;
232 VAR
233 b: StackBucket ;
234 BEGIN
235 IF s^.tail=NIL
236 THEN
237 InternalError ('stack underflow')
238 ELSE
239 IF s^.tail^.items=0
240 THEN
241 b := s^.tail ;
242 IF b=NIL
243 THEN
244 InternalError ('stack underflow')
245 ELSE
246 s^.tail := b^.prev
247 END ;
248 DISPOSE(b)
249 END ;
250 LOOP
251 IF s^.tail=NIL
252 THEN
253 InternalError ('stack underflow')
254 ELSIF s^.tail^.items>=n
255 THEN
256 DEC( s^.tail^.items, n) ;
257 RETURN (* all done exit *)
258 ELSE
259 b := s^.tail ;
260 DEC(n, b^.items) ;
261 s^.tail := s^.tail^.prev ;
262 DISPOSE(b)
263 END
264 END
265 END
266 END ReduceAddress ;
267
268
269 (*
270 NoOfItemsInStackAddress - returns the number of items held in the stack, s.
271 *)
272
273 PROCEDURE NoOfItemsInStackAddress (s: StackOfAddress) : CARDINAL ;
274 VAR
275 b: StackBucket ;
276 n: CARDINAL ;
277 BEGIN
278 IF IsEmptyAddress(s)
279 THEN
280 RETURN( 0 )
281 ELSE
282 n := 0 ;
283 b := s^.tail ;
284 WHILE b#NIL DO
285 INC (n, b^.items) ;
286 b := b^.prev
287 END ;
288 RETURN( n )
289 END
290 END NoOfItemsInStackAddress ;
291
292
293 END M2StackAddress.