Debian Codegraph @

gcc/m2/mc-boot/GStdIO.cc

raw
1 /* do not edit automatically generated by mc from StdIO. */
2 /* StdIO.mod provides general Read and Write procedures.
3
4 Copyright (C) 2001-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 "config.h"
29 #include "system.h"
30 #include <stdbool.h>
31 # if !defined (PROC_D)
32 # define PROC_D
33 typedef void (*PROC_t) (void);
34 typedef struct { PROC_t proc; } PROC;
35 # endif
36
37 # include "Gmcrts.h"
38 #define _StdIO_C
39
40 #include "GStdIO.h"
41 # include "GIO.h"
42 # include "GM2RTS.h"
43
44 typedef struct StdIO_ProcWrite_p StdIO_ProcWrite;
45
46 typedef struct StdIO_ProcRead_p StdIO_ProcRead;
47
48 # define MaxStack 40
49 typedef struct StdIO__T1_a StdIO__T1;
50
51 typedef struct StdIO__T2_a StdIO__T2;
52
53 struct StdIO__T1_a { StdIO_ProcWrite array[MaxStack+1]; };
54 struct StdIO__T2_a { StdIO_ProcRead array[MaxStack+1]; };
55 static StdIO__T1 StackW;
56 static unsigned int StackWPtr;
57 static StdIO__T2 StackR;
58 static unsigned int StackRPtr;
59
60 /*
61 Read - is the generic procedure that all higher application layers
62 should use to receive a character.
63 */
64
65 extern "C" void StdIO_Read (char *ch);
66
67 /*
68 Write - is the generic procedure that all higher application layers
69 should use to emit a character.
70 */
71
72 extern "C" void StdIO_Write (char ch);
73
74 /*
75 PushOutput - pushes the current Write procedure onto a stack,
76 any future references to Write will actually invoke
77 procedure, p.
78 */
79
80 extern "C" void StdIO_PushOutput (StdIO_ProcWrite p);
81
82 /*
83 PopOutput - restores Write to use the previous output procedure.
84 */
85
86 extern "C" void StdIO_PopOutput (void);
87
88 /*
89 GetCurrentOutput - returns the current output procedure.
90 */
91
92 extern "C" StdIO_ProcWrite StdIO_GetCurrentOutput (void);
93
94 /*
95 PushInput - pushes the current Read procedure onto a stack,
96 any future references to Read will actually invoke
97 procedure, p.
98 */
99
100 extern "C" void StdIO_PushInput (StdIO_ProcRead p);
101
102 /*
103 PopInput - restores Write to use the previous output procedure.
104 */
105
106 extern "C" void StdIO_PopInput (void);
107
108 /*
109 GetCurrentInput - returns the current input procedure.
110 */
111
112 extern "C" StdIO_ProcRead StdIO_GetCurrentInput (void);
113
114
115 /*
116 Read - is the generic procedure that all higher application layers
117 should use to receive a character.
118 */
119
120 extern "C" void StdIO_Read (char *ch)
121 {
122 (*StackR.array[StackRPtr].proc) (ch);
123 }
124
125
126 /*
127 Write - is the generic procedure that all higher application layers
128 should use to emit a character.
129 */
130
131 extern "C" void StdIO_Write (char ch)
132 {
133 (*StackW.array[StackWPtr].proc) (ch);
134 }
135
136
137 /*
138 PushOutput - pushes the current Write procedure onto a stack,
139 any future references to Write will actually invoke
140 procedure, p.
141 */
142
143 extern "C" void StdIO_PushOutput (StdIO_ProcWrite p)
144 {
145 if (StackWPtr == MaxStack)
146 {
147 M2RTS_HALT (-1);
148 __builtin_unreachable ();
149 }
150 else
151 {
152 StackWPtr += 1;
153 StackW.array[StackWPtr] = p;
154 }
155 }
156
157
158 /*
159 PopOutput - restores Write to use the previous output procedure.
160 */
161
162 extern "C" void StdIO_PopOutput (void)
163 {
164 if (StackWPtr == 1)
165 {
166 M2RTS_HALT (-1);
167 __builtin_unreachable ();
168 }
169 else
170 {
171 StackWPtr -= 1;
172 }
173 }
174
175
176 /*
177 GetCurrentOutput - returns the current output procedure.
178 */
179
180 extern "C" StdIO_ProcWrite StdIO_GetCurrentOutput (void)
181 {
182 if (StackWPtr > 0)
183 {
184 return StackW.array[StackWPtr];
185 }
186 else
187 {
188 M2RTS_HALT (-1);
189 __builtin_unreachable ();
190 }
191 ReturnException ("../../gcc/m2/gm2-libs/StdIO.def", 25, 1);
192 __builtin_unreachable ();
193 }
194
195
196 /*
197 PushInput - pushes the current Read procedure onto a stack,
198 any future references to Read will actually invoke
199 procedure, p.
200 */
201
202 extern "C" void StdIO_PushInput (StdIO_ProcRead p)
203 {
204 if (StackRPtr == MaxStack)
205 {
206 M2RTS_HALT (-1);
207 __builtin_unreachable ();
208 }
209 else
210 {
211 StackRPtr += 1;
212 StackR.array[StackRPtr] = p;
213 }
214 }
215
216
217 /*
218 PopInput - restores Write to use the previous output procedure.
219 */
220
221 extern "C" void StdIO_PopInput (void)
222 {
223 if (StackRPtr == 1)
224 {
225 M2RTS_HALT (-1);
226 __builtin_unreachable ();
227 }
228 else
229 {
230 StackRPtr -= 1;
231 }
232 }
233
234
235 /*
236 GetCurrentInput - returns the current input procedure.
237 */
238
239 extern "C" StdIO_ProcRead StdIO_GetCurrentInput (void)
240 {
241 if (StackRPtr > 0)
242 {
243 return StackR.array[StackRPtr];
244 }
245 else
246 {
247 M2RTS_HALT (-1);
248 __builtin_unreachable ();
249 }
250 ReturnException ("../../gcc/m2/gm2-libs/StdIO.def", 25, 1);
251 __builtin_unreachable ();
252 }
253
254 extern "C" void _M2_StdIO_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
255 {
256 StackWPtr = 0;
257 StackRPtr = 0;
258 StdIO_PushOutput ((StdIO_ProcWrite) {(StdIO_ProcWrite_t) IO_Write});
259 StdIO_PushInput ((StdIO_ProcRead) {(StdIO_ProcRead_t) IO_Read});
260 }
261
262 extern "C" void _M2_StdIO_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
263 {
264 }