Debian Codegraph @

gcc/m2/gm2-libs-iso/M2RTS.mod

raw
1 (* M2RTS.mod implements access to the exception handlers.
2
3 Copyright (C) 2010-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 isTerminating,
51 isHalting,
52 Initialized,
53 CallExit : BOOLEAN ;
54
55
56 (*
57 ConstructModules - resolve dependencies and then call each
58 module constructor in turn.
59 *)
60
61 PROCEDURE ConstructModules (applicationmodule, libname: ADDRESS;
62 overrideliborder: ADDRESS;
63 argc: INTEGER; argv, envp: ADDRESS) ;
64 BEGIN
65 M2Dependent.ConstructModules (applicationmodule, libname,
66 overrideliborder,
67 argc, argv, envp)
68 END ConstructModules ;
69
70
71 (*
72 DeconstructModules - resolve dependencies and then call each
73 module constructor in turn.
74 *)
75
76 PROCEDURE DeconstructModules (applicationmodule, libname: ADDRESS;
77 argc: INTEGER; argv, envp: ADDRESS) ;
78 BEGIN
79 M2Dependent.DeconstructModules (applicationmodule, libname,
80 argc, argv, envp)
81 END DeconstructModules ;
82
83
84 (*
85 RegisterModule - adds module name to the list of outstanding
86 modules which need to have their dependencies
87 explored to determine initialization order.
88 *)
89
90 PROCEDURE RegisterModule (name, libname: ADDRESS;
91 init, fini: ArgCVEnvP;
92 dependencies: PROC) ;
93 BEGIN
94 M2Dependent.RegisterModule (name, libname, init, fini, dependencies)
95 END RegisterModule ;
96
97
98 (*
99 RequestDependant - used to specify that modulename is dependant upon
100 module dependantmodule.
101 *)
102
103 PROCEDURE RequestDependant (modulename, libname,
104 dependantmodule, dependantlibname: ADDRESS) ;
105 BEGIN
106 M2Dependent.RequestDependant (modulename, libname,
107 dependantmodule, dependantlibname)
108 END RequestDependant ;
109
110
111 (*
112 ExecuteTerminationProcedures - calls each installed termination procedure
113 in reverse order.
114 *)
115
116 PROCEDURE ExecuteTerminationProcedures ;
117 BEGIN
118 M2Dependent.ExecuteTerminationProcedures
119 END ExecuteTerminationProcedures ;
120
121
122 (*
123 ExecuteInitialProcedures - executes the initial procedures installed by
124 InstallInitialProcedure.
125 *)
126
127 PROCEDURE ExecuteInitialProcedures ;
128 BEGIN
129 M2Dependent.ExecuteInitialProcedures
130 END ExecuteInitialProcedures ;
131
132
133 (*
134 InstallTerminationProcedure - installs a procedure, p, which will
135 be called when the procedure
136 ExecuteTerminationProcedures
137 is invoked. It returns TRUE if the
138 procedure is installed.
139 *)
140
141 PROCEDURE InstallTerminationProcedure (p: PROC) : BOOLEAN ;
142 BEGIN
143 RETURN M2Dependent.InstallTerminationProcedure (p)
144 END InstallTerminationProcedure ;
145
146
147 (*
148 InstallInitialProcedure - installs a procedure to be executed just
149 before the BEGIN code section of the
150 main program module.
151 *)
152
153 PROCEDURE InstallInitialProcedure (p: PROC) : BOOLEAN ;
154 BEGIN
155 RETURN M2Dependent.InstallInitialProcedure (p)
156 END InstallInitialProcedure ;
157
158
159 (*
160 HALT - terminate the current program. The procedure
161 ExecuteTerminationProcedures
162 is called before the program is stopped. The parameter
163 exitcode is optional. If the parameter is not supplied
164 HALT will call libc 'abort', otherwise it will exit with
165 the code supplied. Supplying a parameter to HALT has the
166 same effect as calling ExitOnHalt with the same code and
167 then calling HALT with no parameter.
168 *)
169
170 PROCEDURE HALT ([exitcode: INTEGER = -1]) <* noreturn *> ;
171 BEGIN
172 IF exitcode#-1
173 THEN
174 CallExit := TRUE ;
175 ExitValue := exitcode
176 END ;
177 IF isHalting
178 THEN
179 (* double HALT found *)
180 exit(-1)
181 ELSE
182 isHalting := TRUE ;
183 ExecuteTerminationProcedures ;
184 END ;
185 IF CallExit
186 THEN
187 exit(ExitValue)
188 ELSE
189 abort
190 END
191 END HALT ;
192
193
194 (*
195 Terminate - provides compatibility for pim. It call exit with
196 the exitcode provided in a prior call to ExitOnHalt
197 (or zero if ExitOnHalt was never called). It does
198 not call ExecuteTerminationProcedures.
199 *)
200
201 PROCEDURE Terminate <* noreturn *> ;
202 BEGIN
203 exit (ExitValue)
204 END Terminate ;
205
206
207 (*
208 ErrorString - writes a string to stderr.
209 *)
210
211 PROCEDURE ErrorString (a: ARRAY OF CHAR) ;
212 VAR
213 n: INTEGER ;
214 BEGIN
215 n := write (stderrFd, ADR (a), StrLen (a))
216 END ErrorString ;
217
218
219 (*
220 ErrorStringC - writes a string to stderr.
221 *)
222
223 PROCEDURE ErrorStringC (str: ADDRESS) ;
224 VAR
225 len: INTEGER ;
226 BEGIN
227 len := write (stderrFd, str, strlen (str))
228 END ErrorStringC ;
229
230
231 (*
232 ErrorMessage - emits an error message to stderr and then calls exit (1).
233 *)
234
235 PROCEDURE ErrorMessage (message: ARRAY OF CHAR;
236 filename: ARRAY OF CHAR;
237 line: CARDINAL;
238 function: ARRAY OF CHAR) <* noreturn *> ;
239 VAR
240 buffer: ARRAY [0..10] OF CHAR ;
241 BEGIN
242 ErrorString (filename) ; ErrorString(':') ;
243 CardToStr (line, 0, buffer) ;
244 ErrorString (buffer) ; ErrorString(':') ;
245 IF NOT StrEqual (function, '')
246 THEN
247 ErrorString ('in ') ;
248 ErrorString (function) ;
249 ErrorString (' has caused ') ;
250 END ;
251 ErrorString (message) ;
252 buffer[0] := nl ; buffer[1] := nul ;
253 ErrorString (buffer) ;
254 exit (1)
255 END ErrorMessage ;
256
257
258 (*
259 ErrorMessageC - emits an error message to stderr and then calls exit (1).
260 *)
261
262 PROCEDURE ErrorMessageC (message, filename: ADDRESS;
263 line: CARDINAL;
264 function: ADDRESS) <* noreturn *> ;
265 VAR
266 buffer: ARRAY [0..10] OF CHAR ;
267 BEGIN
268 ErrorStringC (filename) ; ErrorString (':') ;
269 CardToStr (line, 0, buffer) ;
270 ErrorString (buffer) ; ErrorString(':') ;
271 IF strlen (function) > 0
272 THEN
273 ErrorString ('in ') ;
274 ErrorStringC (function) ;
275 ErrorString (' has caused ') ;
276 END ;
277 ErrorStringC (message) ;
278 buffer[0] := nl ; buffer[1] := nul ;
279 ErrorString (buffer) ;
280 exit (1)
281 END ErrorMessageC ;
282
283
284 (*
285 HaltC - provides a more user friendly version of HALT, which takes
286 four parameters to aid debugging. It writes an error message
287 to stderr and calls exit (1).
288 *)
289
290 PROCEDURE HaltC (description, filename, function: ADDRESS; line: CARDINAL) ;
291 BEGIN
292 ErrorMessageC (description, filename, line, function)
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) ;
303 BEGIN
304 ErrorMessage (description, filename, line, function)
305 END Halt ;
306
307
308 (*
309 IsTerminating - Returns true if any coroutine has started program termination
310 and false otherwise.
311 *)
312
313 PROCEDURE IsTerminating () : BOOLEAN ;
314 BEGIN
315 RETURN isTerminating
316 END IsTerminating ;
317
318
319 (*
320 HasHalted - Returns true if a call to HALT has been made and false
321 otherwise.
322 *)
323
324 PROCEDURE HasHalted () : BOOLEAN ;
325 BEGIN
326 RETURN isHalting
327 END HasHalted ;
328
329
330 (*
331 ErrorCharStar -
332 *)
333
334 PROCEDURE ErrorCharStar (a: ADDRESS) ;
335 VAR
336 p: POINTER TO CHAR ;
337 n: INTEGER ;
338 BEGIN
339 p := a ;
340 n := 0 ;
341 WHILE (p#NIL) AND (p^#nul) DO
342 INC(n) ;
343 INC(p)
344 END ;
345 IF n>0
346 THEN
347 n := write(2, a, n)
348 END
349 END ErrorCharStar ;
350
351
352 (*
353 ErrorMessageColumn - emits an error message to the stderr
354 *)
355
356 PROCEDURE ErrorMessageColumn (filename, scope, message: ADDRESS;
357 line, column: CARDINAL) ;
358 VAR
359 LineNo: ARRAY [0..10] OF CHAR ;
360 BEGIN
361 ErrorCharStar(filename) ; ErrorString(':') ;
362 CardToStr(line, 0, LineNo) ;
363 ErrorString(LineNo) ; ErrorString(':') ;
364 CardToStr(column, 0, LineNo) ;
365 ErrorString(LineNo) ; ErrorString(':') ;
366 ErrorCharStar(scope) ; ErrorString(':') ;
367 ErrorCharStar(message) ;
368 LineNo[0] := nl ; LineNo[1] := nul ;
369 ErrorString(LineNo) ;
370 exit(1)
371 END ErrorMessageColumn ;
372
373
374 (*
375 ExitOnHalt - if HALT is executed then call exit with the exit code, e.
376 *)
377
378 PROCEDURE ExitOnHalt (e: INTEGER) ;
379 BEGIN
380 ExitValue := e ;
381 CallExit := TRUE
382 END ExitOnHalt ;
383
384
385 (*
386 Length - returns the length of a string, a. This is called whenever
387 the user calls LENGTH and the parameter cannot be calculated
388 at compile time.
389 *)
390
391 PROCEDURE Length (a: ARRAY OF CHAR) : CARDINAL ;
392 VAR
393 l, h: CARDINAL ;
394 BEGIN
395 l := 0 ;
396 h := HIGH(a) ;
397 WHILE (l<=h) AND (a[l]#nul) DO
398 INC(l)
399 END ;
400 RETURN( l )
401 END Length ;
402
403
404 (*
405 The following are the runtime exception handler routines.
406 *)
407
408 PROCEDURE AssignmentException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
409 BEGIN
410 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
411 filename, line, column, scope, message)
412 END AssignmentException ;
413
414
415 PROCEDURE ReturnException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
416 BEGIN
417 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
418 filename, line, column, scope, message)
419 END ReturnException ;
420
421
422 PROCEDURE IncException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
423 BEGIN
424 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
425 filename, line, column, scope, message)
426 END IncException ;
427
428
429 PROCEDURE DecException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
430 BEGIN
431 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
432 filename, line, column, scope, message)
433 END DecException ;
434
435
436 PROCEDURE InclException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
437 BEGIN
438 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
439 filename, line, column, scope, message)
440 END InclException ;
441
442
443 PROCEDURE ExclException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
444 BEGIN
445 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
446 filename, line, column, scope, message)
447 END ExclException ;
448
449
450 PROCEDURE ShiftException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
451 BEGIN
452 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
453 filename, line, column, scope, message)
454 END ShiftException ;
455
456
457 PROCEDURE RotateException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
458 BEGIN
459 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
460 filename, line, column, scope, message)
461 END RotateException ;
462
463
464 PROCEDURE StaticArraySubscriptException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
465 BEGIN
466 RTExceptions.Raise(ORD (M2EXCEPTION.indexException),
467 filename, line, column, scope, message)
468 END StaticArraySubscriptException ;
469
470
471 PROCEDURE DynamicArraySubscriptException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
472 BEGIN
473 RTExceptions.Raise (ORD (M2EXCEPTION.indexException),
474 filename, line, column, scope, message)
475 END DynamicArraySubscriptException ;
476
477
478 PROCEDURE ForLoopBeginException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
479 BEGIN
480 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
481 filename, line, column, scope, message)
482 END ForLoopBeginException ;
483
484
485 PROCEDURE ForLoopToException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
486 BEGIN
487 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
488 filename, line, column, scope, message)
489 END ForLoopToException ;
490
491
492 PROCEDURE ForLoopEndException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
493 BEGIN
494 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
495 filename, line, column, scope, message)
496 END ForLoopEndException ;
497
498
499 PROCEDURE PointerNilException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
500 BEGIN
501 RTExceptions.Raise (ORD (M2EXCEPTION.invalidLocation),
502 filename, line, column, scope, message)
503 END PointerNilException ;
504
505
506 PROCEDURE NoReturnException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
507 BEGIN
508 RTExceptions.Raise (ORD (M2EXCEPTION.functionException),
509 filename, line, column, scope, message)
510 END NoReturnException ;
511
512
513 PROCEDURE CaseException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
514 BEGIN
515 RTExceptions.Raise (ORD (M2EXCEPTION.caseSelectException),
516 filename, line, column, scope, message)
517 END CaseException ;
518
519
520 PROCEDURE WholeNonPosDivException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
521 BEGIN
522 RTExceptions.Raise (ORD (M2EXCEPTION.wholeDivException),
523 filename, line, column, scope, message)
524 END WholeNonPosDivException ;
525
526
527 PROCEDURE WholeNonPosModException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
528 BEGIN
529 RTExceptions.Raise (ORD (M2EXCEPTION.wholeDivException),
530 filename, line, column, scope, message)
531 END WholeNonPosModException ;
532
533
534 PROCEDURE WholeZeroDivException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
535 BEGIN
536 RTExceptions.Raise (ORD (M2EXCEPTION.wholeDivException),
537 filename, line, column, scope, message)
538 END WholeZeroDivException ;
539
540
541 PROCEDURE WholeZeroRemException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
542 BEGIN
543 RTExceptions.Raise (ORD (M2EXCEPTION.wholeDivException),
544 filename, line, column, scope, message)
545 END WholeZeroRemException ;
546
547
548 PROCEDURE WholeValueException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
549 BEGIN
550 RTExceptions.Raise (ORD (M2EXCEPTION.wholeValueException),
551 filename, line, column, scope, message)
552 END WholeValueException ;
553
554
555 PROCEDURE RealValueException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
556 BEGIN
557 RTExceptions.Raise (ORD (M2EXCEPTION.realValueException),
558 filename, line, column, scope, message)
559 END RealValueException ;
560
561
562 PROCEDURE ParameterException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
563 BEGIN
564 RTExceptions.Raise (ORD (M2EXCEPTION.rangeException),
565 filename, line, column, scope, message)
566 END ParameterException ;
567
568
569 PROCEDURE NoException (filename: ADDRESS; line, column: CARDINAL; scope, message: ADDRESS) ;
570 BEGIN
571 RTExceptions.Raise (ORD (M2EXCEPTION.exException),
572 filename, line, column, scope, message)
573 END NoException ;
574
575
576 (*
577 Init -
578 *)
579
580 PROCEDURE Init ;
581 BEGIN
582 ExitValue := 0 ;
583 isHalting := FALSE ;
584 CallExit := FALSE ; (* default by calling abort *)
585 isTerminating := FALSE
586 END Init ;
587
588
589 (*
590 CheckInitialized - checks to see if this module has been initialized
591 and if it has not it calls Init. We need this
592 approach as this module is called by module ctors
593 before we reach main.
594 *)
595
596 PROCEDURE CheckInitialized ;
597 BEGIN
598 IF NOT Initialized
599 THEN
600 Initialized := TRUE ;
601 Init
602 END
603 END CheckInitialized ;
604
605
606 BEGIN
607 (* Initialized := FALSE ; is achieved though setting the bss section to zero. *)
608 CheckInitialized
609 END M2RTS.