Debian Codegraph @

gcc/m2/gm2-libs/M2RTS.mod

raw
1 (* M2RTS.mod Implements the run time system facilities of Modula-2.
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 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. *)
26
27 IMPLEMENTATION MODULE M2RTS ;
28
29
30 FROM libc IMPORT abort, exit, write, getenv, printf, strlen ;
31 (* FROM Builtins IMPORT strncmp, strcmp ; not available during bootstrap. *)
32 FROM NumberIO IMPORT CardToStr ;
33 FROM StrLib IMPORT StrCopy, StrLen, StrEqual ;
34 FROM SYSTEM IMPORT ADR ;
35 FROM ASCII IMPORT nl, nul ;
36 FROM Storage IMPORT ALLOCATE ;
37
38 IMPORT RTExceptions ;
39 IMPORT M2EXCEPTION ;
40 IMPORT M2Dependent ;
41
42 CONST
43 stderrFd = 2 ;
44
45 TYPE
46 PtrToChar = POINTER TO CHAR ;
47
48 VAR
49 ExitValue : INTEGER ;
50 isHalting,
51 CallExit : BOOLEAN ;
52 Initialized : BOOLEAN ;
53
54
55 (*
56 ConstructModules - resolve dependencies and then call each
57 module constructor in turn.
58 *)
59
60 PROCEDURE ConstructModules (applicationmodule, libname: ADDRESS;
61 overrideliborder: ADDRESS;
62 argc: INTEGER; argv, envp: ADDRESS) ;
63 BEGIN
64 M2Dependent.ConstructModules (applicationmodule, libname,
65 overrideliborder,
66 argc, argv, envp)
67 END ConstructModules ;
68
69
70 (*
71 DeconstructModules - resolve dependencies and then call each
72 module constructor in turn.
73 *)
74
75 PROCEDURE DeconstructModules (applicationmodule, libname: ADDRESS;
76 argc: INTEGER; argv, envp: ADDRESS) ;
77 BEGIN
78 M2Dependent.DeconstructModules (applicationmodule, libname,
79 argc, argv, envp)
80 END DeconstructModules ;
81
82
83 (*
84 RegisterModule - adds module name to the list of outstanding
85 modules which need to have their dependencies
86 explored to determine initialization order.
87 *)
88
89 PROCEDURE RegisterModule (name, libname: ADDRESS;
90 init, fini: ArgCVEnvP;
91 dependencies: PROC) ;
92 BEGIN
93 M2Dependent.RegisterModule (name, libname, init, fini, dependencies)
94 END RegisterModule ;
95
96
97 (*
98 RequestDependant - used to specify that modulename is dependant upon
99 module dependantmodule.
100 *)
101
102 PROCEDURE RequestDependant (modulename, libname,
103 dependantmodule, dependantlibname: ADDRESS) ;
104 BEGIN
105 M2Dependent.RequestDependant (modulename, libname,
106 dependantmodule, dependantlibname)
107 END RequestDependant ;
108
109
110 (*
111 ExecuteTerminationProcedures - calls each installed termination procedure
112 in reverse order.
113 *)
114
115 PROCEDURE ExecuteTerminationProcedures ;
116 BEGIN
117 M2Dependent.ExecuteTerminationProcedures
118 END ExecuteTerminationProcedures ;
119
120
121 (*
122 ExecuteInitialProcedures - executes the initial procedures installed by
123 InstallInitialProcedure.
124 *)
125
126 PROCEDURE ExecuteInitialProcedures ;
127 BEGIN
128 M2Dependent.ExecuteInitialProcedures
129 END ExecuteInitialProcedures ;
130
131
132 (*
133 InstallTerminationProcedure - installs a procedure, p, which will
134 be called when the procedure
135 ExecuteTerminationProcedures
136 is invoked. It returns TRUE if the
137 procedure is installed.
138 *)
139
140 PROCEDURE InstallTerminationProcedure (p: PROC) : BOOLEAN ;
141 BEGIN
142 RETURN M2Dependent.InstallTerminationProcedure (p)
143 END InstallTerminationProcedure ;
144
145
146 (*
147 InstallInitialProcedure - installs a procedure to be executed just
148 before the BEGIN code section of the
149 main program module.
150 *)
151
152 PROCEDURE InstallInitialProcedure (p: PROC) : BOOLEAN ;
153 BEGIN
154 RETURN M2Dependent.InstallInitialProcedure (p)
155 END InstallInitialProcedure ;
156
157
158 (*
159 HALT - terminate the current program. The procedure
160 ExecuteTerminationProcedures
161 is called before the program is stopped. The parameter
162 exitcode is optional. If the parameter is not supplied
163 HALT will call libc 'abort', otherwise it will exit with
164 the code supplied. Supplying a parameter to HALT has the
165 same effect as calling ExitOnHalt with the same code and
166 then calling HALT with no parameter.
167 *)
168
169 PROCEDURE HALT ([exitcode: INTEGER = -1]) <* noreturn *> ;
170 BEGIN
171 IF exitcode#-1
172 THEN
173 CallExit := TRUE ;
174 ExitValue := exitcode
175 END ;
176 IF isHalting
177 THEN
178 (* double HALT found *)
179 exit(-1)
180 ELSE
181 isHalting := TRUE ;
182 ExecuteTerminationProcedures ;
183 END ;
184 IF CallExit
185 THEN
186 exit(ExitValue)
187 ELSE
188 abort
189 END
190 END HALT ;
191
192
193 (*
194 Terminate - provides compatibility for pim. It calls exit with
195 the exitcode provided in a prior call to ExitOnHalt
196 (or zero if ExitOnHalt was never called). It does
197 not call ExecuteTerminationProcedures.
198 *)
199
200 PROCEDURE Terminate <* noreturn *> ;
201 BEGIN
202 exit (ExitValue)
203 END Terminate ;
204
205
206 (*
207 ErrorString - writes a string to stderr.
208 *)
209
210 PROCEDURE ErrorString (a: ARRAY OF CHAR) ;
211 VAR
212 n: INTEGER ;
213 BEGIN
214 n := write (stderrFd, ADR (a), StrLen (a))
215 END ErrorString ;
216
217
218 (*
219 ErrorStringC - writes a string to stderr.
220 *)
221
222 PROCEDURE ErrorStringC (str: ADDRESS) ;
223 VAR
224 len: INTEGER ;
225 BEGIN
226 len := write (stderrFd, str, strlen (str))
227 END ErrorStringC ;
228
229
230 (*
231 ErrorMessage - emits an error message to stderr and then calls exit (1).
232 *)
233
234 PROCEDURE ErrorMessage (message: ARRAY OF CHAR;
235 filename: ARRAY OF CHAR;
236 line: CARDINAL;
237 function: ARRAY OF CHAR) <* noreturn *> ;
238 VAR
239 buffer: ARRAY [0..10] OF CHAR ;
240 BEGIN
241 ErrorString (filename) ; ErrorString(':') ;
242 CardToStr (line, 0, buffer) ;
243 ErrorString (buffer) ; ErrorString(':') ;
244 IF NOT StrEqual (function, '')
245 THEN
246 ErrorString ('in ') ;
247 ErrorString (function) ;
248 ErrorString (' has caused ') ;
249 END ;
250 ErrorString (message) ;
251 buffer[0] := nl ; buffer[1] := nul ;
252 ErrorString (buffer) ;
253 exit (1)
254 END ErrorMessage ;
255
256
257 (*
258 ErrorMessageC - emits an error message to stderr and then calls exit (1).
259 *)
260
261 PROCEDURE ErrorMessageC (message, filename: ADDRESS;
262 line: CARDINAL;
263 function: ADDRESS) <* noreturn *> ;
264 VAR
265 buffer: ARRAY [0..10] OF CHAR ;
266 BEGIN
267 ErrorStringC (filename) ; ErrorString (':') ;
268 CardToStr (line, 0, buffer) ;
269 ErrorString (buffer) ; ErrorString(':') ;
270 IF strlen (function) > 0
271 THEN
272 ErrorString ('in ') ;
273 ErrorStringC (function) ;
274 ErrorString (' has caused ') ;
275 END ;
276 ErrorStringC (message) ;
277 buffer[0] := nl ; buffer[1] := nul ;
278 ErrorString (buffer) ;
279 exit (1)
280 END ErrorMessageC ;
281
282
283 (*
284 HaltC - provides a more user friendly version of HALT, which takes
285 four parameters to aid debugging. It writes an error message
286 to stderr and calls exit (1).
287 *)
288
289 PROCEDURE HaltC (description, filename, function: ADDRESS; line: CARDINAL) <* noreturn *> ;
290 BEGIN
291 ErrorMessageC (description, filename, line, function) ;
292 exit (1)
293 END HaltC ;
294
295
296 (*
297 Halt - provides a more user friendly version of HALT, which takes
298 four parameters to aid debugging. It writes an error message
299 to stderr and calls exit (1).
300 *)
301
302 PROCEDURE Halt (description, filename, function: ARRAY OF CHAR; line: CARDINAL) <* noreturn *> ;
303 BEGIN
304 ErrorMessage (description, filename, line, function) ;
305 exit (1)
306 END Halt ;
307
308
309 (*
310 The following are the runtime exception handler routines.
311 *)
312
313 PROCEDURE AssignmentException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
314 BEGIN
315 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
316 filename, line, column, scope, message)
317 END AssignmentException ;
318
319
320 PROCEDURE ReturnException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
321 BEGIN
322 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
323 filename, line, column, scope, message)
324 END ReturnException ;
325
326
327 PROCEDURE IncException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
328 BEGIN
329 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
330 filename, line, column, scope, message)
331 END IncException ;
332
333
334 PROCEDURE DecException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
335 BEGIN
336 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
337 filename, line, column, scope, message)
338 END DecException ;
339
340
341 PROCEDURE InclException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
342 BEGIN
343 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
344 filename, line, column, scope, message)
345 END InclException ;
346
347
348 PROCEDURE ExclException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
349 BEGIN
350 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
351 filename, line, column, scope, message)
352 END ExclException ;
353
354
355 PROCEDURE ShiftException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
356 BEGIN
357 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
358 filename, line, column, scope, message)
359 END ShiftException ;
360
361
362 PROCEDURE RotateException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
363 BEGIN
364 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
365 filename, line, column, scope, message)
366 END RotateException ;
367
368
369 PROCEDURE StaticArraySubscriptException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
370 BEGIN
371 RTExceptions.Raise (ORD (M2EXCEPTION.indexException),
372 filename, line, column, scope, message)
373 END StaticArraySubscriptException ;
374
375
376 PROCEDURE DynamicArraySubscriptException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
377 BEGIN
378 RTExceptions.Raise (ORD (M2EXCEPTION.indexException),
379 filename, line, column, scope, message)
380 END DynamicArraySubscriptException ;
381
382
383 PROCEDURE ForLoopBeginException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
384 BEGIN
385 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
386 filename, line, column, scope, message)
387 END ForLoopBeginException ;
388
389
390 PROCEDURE ForLoopToException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
391 BEGIN
392 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
393 filename, line, column, scope, message)
394 END ForLoopToException ;
395
396
397 PROCEDURE ForLoopEndException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
398 BEGIN
399 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
400 filename, line, column, scope, message)
401 END ForLoopEndException ;
402
403
404 PROCEDURE PointerNilException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
405 BEGIN
406 RTExceptions.Raise (ORD (M2EXCEPTION.invalidLocation),
407 filename, line, column, scope, message)
408 END PointerNilException ;
409
410
411 PROCEDURE NoReturnException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
412 BEGIN
413 RTExceptions.Raise (ORD (M2EXCEPTION.functionException),
414 filename, line, column, scope, message)
415 END NoReturnException ;
416
417
418 PROCEDURE CaseException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
419 BEGIN
420 RTExceptions.Raise (ORD (M2EXCEPTION.caseSelectException),
421 filename, line, column, scope, message)
422 END CaseException ;
423
424
425 PROCEDURE WholeNonPosDivException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
426 BEGIN
427 RTExceptions.Raise (ORD (M2EXCEPTION.wholeDivException),
428 filename, line, column, scope, message)
429 END WholeNonPosDivException ;
430
431
432 PROCEDURE WholeNonPosModException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
433 BEGIN
434 RTExceptions.Raise (ORD (M2EXCEPTION.wholeDivException),
435 filename, line, column, scope, message)
436 END WholeNonPosModException ;
437
438
439 PROCEDURE WholeZeroDivException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
440 BEGIN
441 RTExceptions.Raise(ORD (M2EXCEPTION.wholeDivException),
442 filename, line, column, scope, message)
443 END WholeZeroDivException ;
444
445
446 PROCEDURE WholeZeroRemException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
447 BEGIN
448 RTExceptions.Raise (ORD (M2EXCEPTION.wholeDivException),
449 filename, line, column, scope, message)
450 END WholeZeroRemException ;
451
452
453 PROCEDURE WholeValueException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
454 BEGIN
455 RTExceptions.Raise (ORD (M2EXCEPTION.wholeValueException),
456 filename, line, column, scope, message)
457 END WholeValueException ;
458
459
460 PROCEDURE RealValueException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
461 BEGIN
462 RTExceptions.Raise (ORD (M2EXCEPTION.realValueException),
463 filename, line, column, scope, message)
464 END RealValueException ;
465
466
467 PROCEDURE ParameterException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
468 BEGIN
469 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
470 filename, line, column, scope, message)
471 END ParameterException ;
472
473
474 PROCEDURE NoException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
475 BEGIN
476 RTExceptions.Raise (ORD (M2EXCEPTION.exException),
477 filename, line, column, scope, message)
478 END NoException ;
479
480
481 (*
482 ExitOnHalt - if HALT is executed then call exit with the exit code, e.
483 *)
484
485 PROCEDURE ExitOnHalt (e: INTEGER) ;
486 BEGIN
487 ExitValue := e ;
488 CallExit := TRUE
489 END ExitOnHalt ;
490
491
492 (*
493 Length - returns the length of a string, a. This is called whenever
494 the user calls LENGTH and the parameter cannot be calculated
495 at compile time.
496 *)
497
498 PROCEDURE Length (a: ARRAY OF CHAR) : CARDINAL ;
499 VAR
500 l, h: CARDINAL ;
501 BEGIN
502 l := 0 ;
503 h := HIGH(a) ;
504 WHILE (l<=h) AND (a[l]#nul) DO
505 INC(l)
506 END ;
507 RETURN( l )
508 END Length ;
509
510
511 (*
512 Init - initialize the initial, terminate procedure lists and booleans.
513 *)
514
515 PROCEDURE Init ;
516 BEGIN
517 ExitValue := 0 ;
518 isHalting := FALSE ;
519 CallExit := FALSE (* default by calling abort *)
520 END Init ;
521
522
523 (*
524 CheckInitialized - checks to see if this module has been initialized
525 and if it has not it calls Init. We need this
526 approach as this module is called by module ctors
527 before we reach main.
528 *)
529
530 PROCEDURE CheckInitialized ;
531 BEGIN
532 IF NOT Initialized
533 THEN
534 Initialized := TRUE ;
535 Init
536 END
537 END CheckInitialized ;
538
539
540 BEGIN
541 CheckInitialized
542 END M2RTS.