Debian Codegraph @

gcc/m2/pge-boot/GRTExceptions.cc

raw
1 /* do not edit automatically generated by mc from RTExceptions. */
2 /* RTExceptions.mod runtime exception handler routines.
3
4 Copyright (C) 2008-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 <stdbool.h>
29 # if !defined (PROC_D)
30 # define PROC_D
31 typedef void (*PROC_t) (void);
32 typedef struct { PROC_t proc; } PROC;
33 # endif
34
35 # if !defined (FALSE)
36 # define FALSE (1==0)
37 # endif
38
39 #include <stddef.h>
40 #include <string.h>
41 #include <limits.h>
42 #include <stdlib.h>
43 # include "GStorage.h"
44 # include "Gmcrts.h"
45 #include <unistd.h>
46 #ifndef __cplusplus
47 extern void throw (unsigned int);
48 #endif
49 #if defined(__cplusplus)
50 # undef NULL
51 # define NULL 0
52 #endif
53 #define _RTExceptions_C
54
55 #include "GRTExceptions.h"
56 # include "GASCII.h"
57 # include "GStrLib.h"
58 # include "GStorage.h"
59 # include "GSYSTEM.h"
60 # include "Glibc.h"
61 # include "GM2RTS.h"
62 # include "GSysExceptions.h"
63 # include "GM2EXCEPTION.h"
64
65 typedef struct RTExceptions_ProcedureHandler_p RTExceptions_ProcedureHandler;
66
67 # define MaxBuffer 4096
68 typedef struct RTExceptions__T1_r RTExceptions__T1;
69
70 typedef char *RTExceptions_PtrToChar;
71
72 typedef struct RTExceptions__T2_a RTExceptions__T2;
73
74 typedef struct RTExceptions__T3_r RTExceptions__T3;
75
76 typedef RTExceptions__T3 *RTExceptions_Handler;
77
78 typedef RTExceptions__T1 *RTExceptions_EHBlock__opaque;
79
80 struct RTExceptions__T2_a { char array[MaxBuffer+1]; };
81 struct RTExceptions__T1_r {
82 RTExceptions__T2 buffer;
83 unsigned int number;
84 RTExceptions_Handler handlers;
85 RTExceptions_EHBlock__opaque right;
86 };
87
88 struct RTExceptions__T3_r {
89 RTExceptions_ProcedureHandler p;
90 unsigned int n;
91 RTExceptions_Handler right;
92 RTExceptions_Handler left;
93 RTExceptions_Handler stack;
94 };
95
96 static bool inException;
97 static RTExceptions_Handler freeHandler;
98 static RTExceptions_EHBlock__opaque freeEHB;
99 static RTExceptions_EHBlock__opaque currentEHB;
100 static void * currentSource;
101
102 /*
103 Raise - invoke the exception handler associated with, number,
104 in the active EHBlock. It keeps a record of the number
105 and message in the EHBlock for later use.
106 */
107
108 extern "C" void RTExceptions_Raise (unsigned int number, void * file, unsigned int line, unsigned int column, void * function, void * message) __attribute__ ((noreturn));
109
110 /*
111 SetExceptionBlock - sets, source, as the active EHB.
112 */
113
114 extern "C" void RTExceptions_SetExceptionBlock (RTExceptions_EHBlock source);
115
116 /*
117 GetExceptionBlock - returns the active EHB.
118 */
119
120 extern "C" RTExceptions_EHBlock RTExceptions_GetExceptionBlock (void);
121
122 /*
123 GetTextBuffer - returns the address of the EHB buffer.
124 */
125
126 extern "C" void * RTExceptions_GetTextBuffer (RTExceptions_EHBlock e);
127
128 /*
129 GetTextBufferSize - return the size of the EHB text buffer.
130 */
131
132 extern "C" unsigned int RTExceptions_GetTextBufferSize (RTExceptions_EHBlock e);
133
134 /*
135 GetNumber - return the exception number associated with,
136 source.
137 */
138
139 extern "C" unsigned int RTExceptions_GetNumber (RTExceptions_EHBlock source);
140
141 /*
142 InitExceptionBlock - creates and returns a new exception block.
143 */
144
145 extern "C" RTExceptions_EHBlock RTExceptions_InitExceptionBlock (void);
146
147 /*
148 KillExceptionBlock - destroys the EHB, e, and all its handlers.
149 */
150
151 extern "C" RTExceptions_EHBlock RTExceptions_KillExceptionBlock (RTExceptions_EHBlock e);
152
153 /*
154 PushHandler - install a handler in EHB, e.
155 */
156
157 extern "C" void RTExceptions_PushHandler (RTExceptions_EHBlock e, unsigned int number, RTExceptions_ProcedureHandler p);
158
159 /*
160 PopHandler - removes the handler associated with, number, from
161 EHB, e.
162 */
163
164 extern "C" void RTExceptions_PopHandler (RTExceptions_EHBlock e, unsigned int number);
165
166 /*
167 DefaultErrorCatch - displays the current error message in
168 the current exception block and then
169 calls HALT.
170 */
171
172 extern "C" void RTExceptions_DefaultErrorCatch (void);
173
174 /*
175 BaseExceptionsThrow - configures the Modula-2 exceptions to call
176 THROW which in turn can be caught by an
177 exception block. If this is not called then
178 a Modula-2 exception will simply call an
179 error message routine and then HALT.
180 */
181
182 extern "C" void RTExceptions_BaseExceptionsThrow (void);
183
184 /*
185 IsInExceptionState - returns TRUE if the program is currently
186 in the exception state.
187 */
188
189 extern "C" bool RTExceptions_IsInExceptionState (void);
190
191 /*
192 SetExceptionState - returns the current exception state and
193 then sets the current exception state to,
194 to.
195 */
196
197 extern "C" bool RTExceptions_SetExceptionState (bool to);
198
199 /*
200 SwitchExceptionState - assigns, from, with the current exception
201 state and then assigns the current exception
202 to, to.
203 */
204
205 extern "C" void RTExceptions_SwitchExceptionState (bool *from, bool to);
206
207 /*
208 GetBaseExceptionBlock - returns the initial language exception block
209 created.
210 */
211
212 extern "C" RTExceptions_EHBlock RTExceptions_GetBaseExceptionBlock (void);
213
214 /*
215 SetExceptionSource - sets the current exception source to, source.
216 */
217
218 extern "C" void RTExceptions_SetExceptionSource (void * source);
219
220 /*
221 GetExceptionSource - returns the current exception source.
222 */
223
224 extern "C" void * RTExceptions_GetExceptionSource (void);
225
226 /*
227 ErrorString - writes a string to stderr.
228 */
229
230 static void ErrorString (const char *a_, unsigned int _a_high);
231
232 /*
233 findHandler -
234 */
235
236 static RTExceptions_Handler findHandler (RTExceptions_EHBlock__opaque e, unsigned int number);
237
238 /*
239 InvokeHandler - invokes the associated handler for the current
240 exception in the active EHB.
241 */
242
243 static void InvokeHandler (void) __attribute__ ((noreturn));
244
245 /*
246 DoThrow - throw the exception number in the exception block.
247 */
248
249 static void DoThrow (void);
250
251 /*
252 addChar - adds, ch, to the current exception handler text buffer
253 at index, i. The index in then incremented.
254 */
255
256 static void addChar (char ch, unsigned int *i);
257
258 /*
259 stripPath - returns the filename from the path.
260 */
261
262 static void * stripPath (void * s);
263
264 /*
265 addFile - adds the filename determined by, s, however it strips
266 any preceeding path.
267 */
268
269 static void addFile (void * s, unsigned int *i);
270
271 /*
272 addStr - adds a C string from address, s, into the current
273 handler text buffer.
274 */
275
276 static void addStr (void * s, unsigned int *i);
277
278 /*
279 addNum - adds a number, n, to the current handler
280 text buffer.
281 */
282
283 static void addNum (unsigned int n, unsigned int *i);
284
285 /*
286 New - returns a new EHBlock.
287 */
288
289 static RTExceptions_EHBlock__opaque New (void);
290
291 /*
292 NewHandler - returns a new handler.
293 */
294
295 static RTExceptions_Handler NewHandler (void);
296
297 /*
298 KillHandler - returns, NIL, and places, h, onto the free list.
299 */
300
301 static RTExceptions_Handler KillHandler (RTExceptions_Handler h);
302
303 /*
304 KillHandlers - kills all handlers in the list.
305 */
306
307 static RTExceptions_Handler KillHandlers (RTExceptions_Handler h);
308
309 /*
310 InitHandler -
311 */
312
313 static RTExceptions_Handler InitHandler (RTExceptions_Handler h, RTExceptions_Handler l, RTExceptions_Handler r, RTExceptions_Handler s, unsigned int number, RTExceptions_ProcedureHandler proc);
314
315 /*
316 SubHandler -
317 */
318
319 static void SubHandler (RTExceptions_Handler h);
320
321 /*
322 AddHandler - add, e, to the end of the list of handlers.
323 */
324
325 static void AddHandler (RTExceptions_EHBlock__opaque e, RTExceptions_Handler h);
326
327 /*
328 indexf - raise an index out of bounds exception.
329 */
330
331 static void indexf (void * a);
332
333 /*
334 range - raise an assignment out of range exception.
335 */
336
337 static void range (void * a);
338
339 /*
340 casef - raise a case selector out of range exception.
341 */
342
343 static void casef (void * a);
344
345 /*
346 invalidloc - raise an invalid location exception.
347 */
348
349 static void invalidloc (void * a);
350
351 /*
352 function - raise a ... function ... exception. --fixme-- what does this exception catch?
353 */
354
355 static void function (void * a);
356
357 /*
358 wholevalue - raise an illegal whole value exception.
359 */
360
361 static void wholevalue (void * a);
362
363 /*
364 wholediv - raise a division by zero exception.
365 */
366
367 static void wholediv (void * a);
368
369 /*
370 realvalue - raise an illegal real value exception.
371 */
372
373 static void realvalue (void * a);
374
375 /*
376 realdiv - raise a division by zero in a real number exception.
377 */
378
379 static void realdiv (void * a);
380
381 /*
382 complexvalue - raise an illegal complex value exception.
383 */
384
385 static void complexvalue (void * a);
386
387 /*
388 complexdiv - raise a division by zero in a complex number exception.
389 */
390
391 static void complexdiv (void * a);
392
393 /*
394 protection - raise a protection exception.
395 */
396
397 static void protection (void * a);
398
399 /*
400 systemf - raise a system exception.
401 */
402
403 static void systemf (void * a);
404
405 /*
406 coroutine - raise a coroutine exception.
407 */
408
409 static void coroutine (void * a);
410
411 /*
412 exception - raise a exception exception.
413 */
414
415 static void exception (void * a);
416
417 /*
418 Init - initialises this module.
419 */
420
421 static void Init (void);
422
423 /*
424 TidyUp - deallocate memory used by this module.
425 */
426
427 static void TidyUp (void);
428
429
430 /*
431 ErrorString - writes a string to stderr.
432 */
433
434 static void ErrorString (const char *a_, unsigned int _a_high)
435 {
436 int n;
437 char a[_a_high+1];
438
439 /* make a local copy of each unbounded array. */
440 memcpy (a, a_, _a_high+1);
441
442 n = static_cast<int> (libc_write (2, const_cast<void*> (static_cast<const void*>(a)), static_cast<size_t> (StrLib_StrLen ((const char *) a, _a_high))));
443 }
444
445
446 /*
447 findHandler -
448 */
449
450 static RTExceptions_Handler findHandler (RTExceptions_EHBlock__opaque e, unsigned int number)
451 {
452 RTExceptions_Handler h;
453
454 h = e->handlers->right;
455 while ((h != e->handlers) && (number != h->n))
456 {
457 h = h->right;
458 }
459 if (h == e->handlers)
460 {
461 return NULL;
462 }
463 else
464 {
465 return h;
466 }
467 /* static analysis guarentees a RETURN statement will be used before here. */
468 __builtin_unreachable ();
469 }
470
471
472 /*
473 InvokeHandler - invokes the associated handler for the current
474 exception in the active EHB.
475 */
476
477 static void InvokeHandler (void)
478 {
479 RTExceptions_Handler h;
480
481 h = findHandler (currentEHB, currentEHB->number);
482 if (h == NULL)
483 {
484 throw (RTExceptions_GetNumber (RTExceptions_GetExceptionBlock ()));
485 }
486 else
487 {
488 (*h->p.proc) ();
489 M2RTS_HALT (-1);
490 __builtin_unreachable ();
491 }
492 }
493
494
495 /*
496 DoThrow - throw the exception number in the exception block.
497 */
498
499 static void DoThrow (void)
500 {
501 throw (RTExceptions_GetNumber (RTExceptions_GetExceptionBlock ()));
502 }
503
504
505 /*
506 addChar - adds, ch, to the current exception handler text buffer
507 at index, i. The index in then incremented.
508 */
509
510 static void addChar (char ch, unsigned int *i)
511 {
512 if (((*i) <= MaxBuffer) && (currentEHB != NULL))
513 {
514 currentEHB->buffer.array[(*i)] = ch;
515 (*i) += 1;
516 }
517 }
518
519
520 /*
521 stripPath - returns the filename from the path.
522 */
523
524 static void * stripPath (void * s)
525 {
526 RTExceptions_PtrToChar f;
527 RTExceptions_PtrToChar p;
528
529 p = static_cast<RTExceptions_PtrToChar> (s);
530 f = static_cast<RTExceptions_PtrToChar> (s);
531 while ((*p) != ASCII_nul)
532 {
533 if ((*p) == '/')
534 {
535 p += 1;
536 f = p;
537 }
538 else
539 {
540 p += 1;
541 }
542 }
543 return static_cast<void *> (f);
544 /* static analysis guarentees a RETURN statement will be used before here. */
545 __builtin_unreachable ();
546 }
547
548
549 /*
550 addFile - adds the filename determined by, s, however it strips
551 any preceeding path.
552 */
553
554 static void addFile (void * s, unsigned int *i)
555 {
556 RTExceptions_PtrToChar p;
557
558 p = static_cast<RTExceptions_PtrToChar> (stripPath (s));
559 while ((p != NULL) && ((*p) != ASCII_nul))
560 {
561 addChar ((*p), i);
562 p += 1;
563 }
564 }
565
566
567 /*
568 addStr - adds a C string from address, s, into the current
569 handler text buffer.
570 */
571
572 static void addStr (void * s, unsigned int *i)
573 {
574 RTExceptions_PtrToChar p;
575
576 p = static_cast<RTExceptions_PtrToChar> (s);
577 while ((p != NULL) && ((*p) != ASCII_nul))
578 {
579 addChar ((*p), i);
580 p += 1;
581 }
582 }
583
584
585 /*
586 addNum - adds a number, n, to the current handler
587 text buffer.
588 */
589
590 static void addNum (unsigned int n, unsigned int *i)
591 {
592 if (n < 10)
593 {
594 addChar ( ((char) ((n % 10)+ ((unsigned int) ('0')))), i);
595 }
596 else
597 {
598 addNum (n / 10, i);
599 addNum (n % 10, i);
600 }
601 }
602
603
604 /*
605 New - returns a new EHBlock.
606 */
607
608 static RTExceptions_EHBlock__opaque New (void)
609 {
610 RTExceptions_EHBlock__opaque e;
611
612 if (freeEHB == NULL)
613 {
614 Storage_ALLOCATE ((void **) &e, sizeof (RTExceptions__T1));
615 }
616 else
617 {
618 e = freeEHB;
619 freeEHB = freeEHB->right;
620 }
621 return e;
622 /* static analysis guarentees a RETURN statement will be used before here. */
623 __builtin_unreachable ();
624 }
625
626
627 /*
628 NewHandler - returns a new handler.
629 */
630
631 static RTExceptions_Handler NewHandler (void)
632 {
633 RTExceptions_Handler h;
634
635 if (freeHandler == NULL)
636 {
637 Storage_ALLOCATE ((void **) &h, sizeof (RTExceptions__T3));
638 }
639 else
640 {
641 h = freeHandler;
642 freeHandler = freeHandler->right;
643 }
644 return h;
645 /* static analysis guarentees a RETURN statement will be used before here. */
646 __builtin_unreachable ();
647 }
648
649
650 /*
651 KillHandler - returns, NIL, and places, h, onto the free list.
652 */
653
654 static RTExceptions_Handler KillHandler (RTExceptions_Handler h)
655 {
656 h->right = freeHandler;
657 freeHandler = h;
658 return NULL;
659 /* static analysis guarentees a RETURN statement will be used before here. */
660 __builtin_unreachable ();
661 }
662
663
664 /*
665 KillHandlers - kills all handlers in the list.
666 */
667
668 static RTExceptions_Handler KillHandlers (RTExceptions_Handler h)
669 {
670 h->left->right = freeHandler;
671 freeHandler = h;
672 return NULL;
673 /* static analysis guarentees a RETURN statement will be used before here. */
674 __builtin_unreachable ();
675 }
676
677
678 /*
679 InitHandler -
680 */
681
682 static RTExceptions_Handler InitHandler (RTExceptions_Handler h, RTExceptions_Handler l, RTExceptions_Handler r, RTExceptions_Handler s, unsigned int number, RTExceptions_ProcedureHandler proc)
683 {
684 h->p = proc;
685 h->n = number;
686 h->right = r;
687 h->left = l;
688 h->stack = s;
689 return h;
690 /* static analysis guarentees a RETURN statement will be used before here. */
691 __builtin_unreachable ();
692 }
693
694
695 /*
696 SubHandler -
697 */
698
699 static void SubHandler (RTExceptions_Handler h)
700 {
701 h->right->left = h->left;
702 h->left->right = h->right;
703 }
704
705
706 /*
707 AddHandler - add, e, to the end of the list of handlers.
708 */
709
710 static void AddHandler (RTExceptions_EHBlock__opaque e, RTExceptions_Handler h)
711 {
712 h->right = e->handlers;
713 h->left = e->handlers->left;
714 e->handlers->left->right = h;
715 e->handlers->left = h;
716 }
717
718
719 /*
720 indexf - raise an index out of bounds exception.
721 */
722
723 static void indexf (void * a)
724 {
725 RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_indexException)), const_cast<void*> (static_cast<const void*>("../../gcc/m2/gm2-libs/RTExceptions.mod")), 614, 9, const_cast<void*> (static_cast<const void*>("indexf")), const_cast<void*> (static_cast<const void*>("array index out of bounds")));
726 }
727
728
729 /*
730 range - raise an assignment out of range exception.
731 */
732
733 static void range (void * a)
734 {
735 RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_rangeException)), const_cast<void*> (static_cast<const void*>("../../gcc/m2/gm2-libs/RTExceptions.mod")), 626, 9, const_cast<void*> (static_cast<const void*>("range")), const_cast<void*> (static_cast<const void*>("assignment out of range")));
736 }
737
738
739 /*
740 casef - raise a case selector out of range exception.
741 */
742
743 static void casef (void * a)
744 {
745 RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_caseSelectException)), const_cast<void*> (static_cast<const void*>("../../gcc/m2/gm2-libs/RTExceptions.mod")), 638, 9, const_cast<void*> (static_cast<const void*>("casef")), const_cast<void*> (static_cast<const void*>("case selector out of range")));
746 }
747
748
749 /*
750 invalidloc - raise an invalid location exception.
751 */
752
753 static void invalidloc (void * a)
754 {
755 RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_invalidLocation)), const_cast<void*> (static_cast<const void*>("../../gcc/m2/gm2-libs/RTExceptions.mod")), 650, 9, const_cast<void*> (static_cast<const void*>("invalidloc")), const_cast<void*> (static_cast<const void*>("invalid address referenced")));
756 }
757
758
759 /*
760 function - raise a ... function ... exception. --fixme-- what does this exception catch?
761 */
762
763 static void function (void * a)
764 {
765 RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_functionException)), const_cast<void*> (static_cast<const void*>("../../gcc/m2/gm2-libs/RTExceptions.mod")), 662, 9, const_cast<void*> (static_cast<const void*>("function")), const_cast<void*> (static_cast<const void*>("... function ... "))); /* --fixme-- what has happened ? */
766 }
767
768
769 /*
770 wholevalue - raise an illegal whole value exception.
771 */
772
773 static void wholevalue (void * a)
774 {
775 RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_wholeValueException)), const_cast<void*> (static_cast<const void*>("../../gcc/m2/gm2-libs/RTExceptions.mod")), 674, 9, const_cast<void*> (static_cast<const void*>("wholevalue")), const_cast<void*> (static_cast<const void*>("illegal whole value exception")));
776 }
777
778
779 /*
780 wholediv - raise a division by zero exception.
781 */
782
783 static void wholediv (void * a)
784 {
785 RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_wholeDivException)), const_cast<void*> (static_cast<const void*>("../../gcc/m2/gm2-libs/RTExceptions.mod")), 686, 9, const_cast<void*> (static_cast<const void*>("wholediv")), const_cast<void*> (static_cast<const void*>("illegal whole value exception")));
786 }
787
788
789 /*
790 realvalue - raise an illegal real value exception.
791 */
792
793 static void realvalue (void * a)
794 {
795 RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_realValueException)), const_cast<void*> (static_cast<const void*>("../../gcc/m2/gm2-libs/RTExceptions.mod")), 698, 9, const_cast<void*> (static_cast<const void*>("realvalue")), const_cast<void*> (static_cast<const void*>("illegal real value exception")));
796 }
797
798
799 /*
800 realdiv - raise a division by zero in a real number exception.
801 */
802
803 static void realdiv (void * a)
804 {
805 RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_realDivException)), const_cast<void*> (static_cast<const void*>("../../gcc/m2/gm2-libs/RTExceptions.mod")), 710, 9, const_cast<void*> (static_cast<const void*>("realdiv")), const_cast<void*> (static_cast<const void*>("real number division by zero exception")));
806 }
807
808
809 /*
810 complexvalue - raise an illegal complex value exception.
811 */
812
813 static void complexvalue (void * a)
814 {
815 RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_complexValueException)), const_cast<void*> (static_cast<const void*>("../../gcc/m2/gm2-libs/RTExceptions.mod")), 722, 9, const_cast<void*> (static_cast<const void*>("complexvalue")), const_cast<void*> (static_cast<const void*>("illegal complex value exception")));
816 }
817
818
819 /*
820 complexdiv - raise a division by zero in a complex number exception.
821 */
822
823 static void complexdiv (void * a)
824 {
825 RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_complexDivException)), const_cast<void*> (static_cast<const void*>("../../gcc/m2/gm2-libs/RTExceptions.mod")), 734, 9, const_cast<void*> (static_cast<const void*>("complexdiv")), const_cast<void*> (static_cast<const void*>("complex number division by zero exception")));
826 }
827
828
829 /*
830 protection - raise a protection exception.
831 */
832
833 static void protection (void * a)
834 {
835 RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_protException)), const_cast<void*> (static_cast<const void*>("../../gcc/m2/gm2-libs/RTExceptions.mod")), 746, 9, const_cast<void*> (static_cast<const void*>("protection")), const_cast<void*> (static_cast<const void*>("protection exception")));
836 }
837
838
839 /*
840 systemf - raise a system exception.
841 */
842
843 static void systemf (void * a)
844 {
845 RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_sysException)), const_cast<void*> (static_cast<const void*>("../../gcc/m2/gm2-libs/RTExceptions.mod")), 758, 9, const_cast<void*> (static_cast<const void*>("systemf")), const_cast<void*> (static_cast<const void*>("system exception")));
846 }
847
848
849 /*
850 coroutine - raise a coroutine exception.
851 */
852
853 static void coroutine (void * a)
854 {
855 RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_coException)), const_cast<void*> (static_cast<const void*>("../../gcc/m2/gm2-libs/RTExceptions.mod")), 770, 9, const_cast<void*> (static_cast<const void*>("coroutine")), const_cast<void*> (static_cast<const void*>("coroutine exception")));
856 }
857
858
859 /*
860 exception - raise a exception exception.
861 */
862
863 static void exception (void * a)
864 {
865 RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_exException)), const_cast<void*> (static_cast<const void*>("../../gcc/m2/gm2-libs/RTExceptions.mod")), 782, 9, const_cast<void*> (static_cast<const void*>("exception")), const_cast<void*> (static_cast<const void*>("exception exception")));
866 }
867
868
869 /*
870 Init - initialises this module.
871 */
872
873 static void Init (void)
874 {
875 inException = false;
876 freeHandler = NULL;
877 freeEHB = static_cast<RTExceptions_EHBlock__opaque> (NULL);
878 currentEHB = static_cast<RTExceptions_EHBlock__opaque> (RTExceptions_InitExceptionBlock ());
879 currentSource = NULL;
880 RTExceptions_BaseExceptionsThrow ();
881 SysExceptions_InitExceptionHandlers ((SysExceptions_PROCEXCEPTION) {(SysExceptions_PROCEXCEPTION_t) indexf}, (SysExceptions_PROCEXCEPTION) {(SysExceptions_PROCEXCEPTION_t) range}, (SysExceptions_PROCEXCEPTION) {(SysExceptions_PROCEXCEPTION_t) casef}, (SysExceptions_PROCEXCEPTION) {(SysExceptions_PROCEXCEPTION_t) invalidloc}, (SysExceptions_PROCEXCEPTION) {(SysExceptions_PROCEXCEPTION_t) function}, (SysExceptions_PROCEXCEPTION) {(SysExceptions_PROCEXCEPTION_t) wholevalue}, (SysExceptions_PROCEXCEPTION) {(SysExceptions_PROCEXCEPTION_t) wholediv}, (SysExceptions_PROCEXCEPTION) {(SysExceptions_PROCEXCEPTION_t) realvalue}, (SysExceptions_PROCEXCEPTION) {(SysExceptions_PROCEXCEPTION_t) realdiv}, (SysExceptions_PROCEXCEPTION) {(SysExceptions_PROCEXCEPTION_t) complexvalue}, (SysExceptions_PROCEXCEPTION) {(SysExceptions_PROCEXCEPTION_t) complexdiv}, (SysExceptions_PROCEXCEPTION) {(SysExceptions_PROCEXCEPTION_t) protection}, (SysExceptions_PROCEXCEPTION) {(SysExceptions_PROCEXCEPTION_t) systemf}, (SysExceptions_PROCEXCEPTION) {(SysExceptions_PROCEXCEPTION_t) coroutine}, (SysExceptions_PROCEXCEPTION) {(SysExceptions_PROCEXCEPTION_t) exception});
882 }
883
884
885 /*
886 TidyUp - deallocate memory used by this module.
887 */
888
889 static void TidyUp (void)
890 {
891 RTExceptions_Handler f;
892 RTExceptions_EHBlock__opaque e;
893
894 if (currentEHB != NULL)
895 {
896 currentEHB = static_cast<RTExceptions_EHBlock__opaque> (RTExceptions_KillExceptionBlock (static_cast<RTExceptions_EHBlock> (currentEHB)));
897 }
898 while (freeHandler != NULL)
899 {
900 f = freeHandler;
901 freeHandler = freeHandler->right;
902 Storage_DEALLOCATE ((void **) &f, sizeof (RTExceptions__T3));
903 }
904 while (freeEHB != NULL)
905 {
906 e = freeEHB;
907 freeEHB = freeEHB->right;
908 Storage_DEALLOCATE ((void **) &e, sizeof (RTExceptions__T1));
909 }
910 }
911
912
913 /*
914 Raise - invoke the exception handler associated with, number,
915 in the active EHBlock. It keeps a record of the number
916 and message in the EHBlock for later use.
917 */
918
919 extern "C" void RTExceptions_Raise (unsigned int number, void * file, unsigned int line, unsigned int column, void * function, void * message)
920 {
921 unsigned int i;
922
923 currentEHB->number = number;
924 i = 0;
925 addFile (file, &i);
926 addChar (':', &i);
927 addNum (line, &i);
928 addChar (':', &i);
929 addNum (column, &i);
930 addChar (':', &i);
931 addChar (' ', &i);
932 addChar ('I', &i);
933 addChar ('n', &i);
934 addChar (' ', &i);
935 addStr (function, &i);
936 addChar (ASCII_nl, &i);
937 addFile (file, &i);
938 addChar (':', &i);
939 addNum (line, &i);
940 addChar (':', &i);
941 addNum (column, &i);
942 addChar (':', &i);
943 addStr (message, &i);
944 addChar (ASCII_nl, &i);
945 addChar (ASCII_nul, &i);
946 InvokeHandler ();
947 }
948
949
950 /*
951 SetExceptionBlock - sets, source, as the active EHB.
952 */
953
954 extern "C" void RTExceptions_SetExceptionBlock (RTExceptions_EHBlock source)
955 {
956 currentEHB = static_cast<RTExceptions_EHBlock__opaque> (source);
957 }
958
959
960 /*
961 GetExceptionBlock - returns the active EHB.
962 */
963
964 extern "C" RTExceptions_EHBlock RTExceptions_GetExceptionBlock (void)
965 {
966 return static_cast<RTExceptions_EHBlock> (currentEHB);
967 /* static analysis guarentees a RETURN statement will be used before here. */
968 __builtin_unreachable ();
969 }
970
971
972 /*
973 GetTextBuffer - returns the address of the EHB buffer.
974 */
975
976 extern "C" void * RTExceptions_GetTextBuffer (RTExceptions_EHBlock e)
977 {
978 return &static_cast<RTExceptions_EHBlock__opaque> (e)->buffer;
979 /* static analysis guarentees a RETURN statement will be used before here. */
980 __builtin_unreachable ();
981 }
982
983
984 /*
985 GetTextBufferSize - return the size of the EHB text buffer.
986 */
987
988 extern "C" unsigned int RTExceptions_GetTextBufferSize (RTExceptions_EHBlock e)
989 {
990 return sizeof (static_cast<RTExceptions_EHBlock__opaque> (e)->buffer);
991 /* static analysis guarentees a RETURN statement will be used before here. */
992 __builtin_unreachable ();
993 }
994
995
996 /*
997 GetNumber - return the exception number associated with,
998 source.
999 */
1000
1001 extern "C" unsigned int RTExceptions_GetNumber (RTExceptions_EHBlock source)
1002 {
1003 return static_cast<RTExceptions_EHBlock__opaque> (source)->number;
1004 /* static analysis guarentees a RETURN statement will be used before here. */
1005 __builtin_unreachable ();
1006 }
1007
1008
1009 /*
1010 InitExceptionBlock - creates and returns a new exception block.
1011 */
1012
1013 extern "C" RTExceptions_EHBlock RTExceptions_InitExceptionBlock (void)
1014 {
1015 RTExceptions_EHBlock__opaque e;
1016
1017 e = New ();
1018 e->number = UINT_MAX;
1019 e->handlers = NewHandler (); /* add the dummy onto the head */
1020 e->handlers->right = e->handlers; /* add the dummy onto the head */
1021 e->handlers->left = e->handlers;
1022 e->right = e;
1023 return static_cast<RTExceptions_EHBlock> (e);
1024 /* static analysis guarentees a RETURN statement will be used before here. */
1025 __builtin_unreachable ();
1026 }
1027
1028
1029 /*
1030 KillExceptionBlock - destroys the EHB, e, and all its handlers.
1031 */
1032
1033 extern "C" RTExceptions_EHBlock RTExceptions_KillExceptionBlock (RTExceptions_EHBlock e)
1034 {
1035 static_cast<RTExceptions_EHBlock__opaque> (e)->handlers = KillHandlers (static_cast<RTExceptions_EHBlock__opaque> (e)->handlers);
1036 static_cast<RTExceptions_EHBlock__opaque> (e)->right = freeEHB;
1037 freeEHB = static_cast<RTExceptions_EHBlock__opaque> (e);
1038 return static_cast<RTExceptions_EHBlock> (NULL);
1039 /* static analysis guarentees a RETURN statement will be used before here. */
1040 __builtin_unreachable ();
1041 }
1042
1043
1044 /*
1045 PushHandler - install a handler in EHB, e.
1046 */
1047
1048 extern "C" void RTExceptions_PushHandler (RTExceptions_EHBlock e, unsigned int number, RTExceptions_ProcedureHandler p)
1049 {
1050 RTExceptions_Handler h;
1051 RTExceptions_Handler i;
1052
1053 h = findHandler (static_cast<RTExceptions_EHBlock__opaque> (e), number);
1054 if (h == NULL)
1055 {
1056 i = InitHandler (NewHandler (), NULL, NULL, NULL, number, p);
1057 }
1058 else
1059 {
1060 /* remove, h, */
1061 SubHandler (h);
1062 /* stack it onto a new handler */
1063 i = InitHandler (NewHandler (), NULL, NULL, h, number, p);
1064 }
1065 /* add new handler */
1066 AddHandler (static_cast<RTExceptions_EHBlock__opaque> (e), i);
1067 }
1068
1069
1070 /*
1071 PopHandler - removes the handler associated with, number, from
1072 EHB, e.
1073 */
1074
1075 extern "C" void RTExceptions_PopHandler (RTExceptions_EHBlock e, unsigned int number)
1076 {
1077 RTExceptions_Handler h;
1078
1079 h = findHandler (static_cast<RTExceptions_EHBlock__opaque> (e), number);
1080 if (h != NULL)
1081 {
1082 /* remove, h, */
1083 SubHandler (h);
1084 if (h->stack != NULL)
1085 {
1086 AddHandler (static_cast<RTExceptions_EHBlock__opaque> (e), h->stack);
1087 }
1088 h = KillHandler (h);
1089 }
1090 }
1091
1092
1093 /*
1094 DefaultErrorCatch - displays the current error message in
1095 the current exception block and then
1096 calls HALT.
1097 */
1098
1099 extern "C" void RTExceptions_DefaultErrorCatch (void)
1100 {
1101 RTExceptions_EHBlock__opaque e;
1102 int n;
1103
1104 e = static_cast<RTExceptions_EHBlock__opaque> (RTExceptions_GetExceptionBlock ());
1105 n = static_cast<int> (libc_write (2, RTExceptions_GetTextBuffer (static_cast<RTExceptions_EHBlock> (e)), libc_strlen (RTExceptions_GetTextBuffer (static_cast<RTExceptions_EHBlock> (e)))));
1106 M2RTS_HALT (-1);
1107 __builtin_unreachable ();
1108 }
1109
1110
1111 /*
1112 BaseExceptionsThrow - configures the Modula-2 exceptions to call
1113 THROW which in turn can be caught by an
1114 exception block. If this is not called then
1115 a Modula-2 exception will simply call an
1116 error message routine and then HALT.
1117 */
1118
1119 extern "C" void RTExceptions_BaseExceptionsThrow (void)
1120 {
1121 M2EXCEPTION_M2Exceptions i;
1122
1123 for (i=M2EXCEPTION_indexException; i<=M2EXCEPTION_exException; i= static_cast<M2EXCEPTION_M2Exceptions>(static_cast<int>(i+1)))
1124 {
1125 RTExceptions_PushHandler (RTExceptions_GetExceptionBlock (), (unsigned int ) (i), (RTExceptions_ProcedureHandler) {(RTExceptions_ProcedureHandler_t) DoThrow});
1126 }
1127 }
1128
1129
1130 /*
1131 IsInExceptionState - returns TRUE if the program is currently
1132 in the exception state.
1133 */
1134
1135 extern "C" bool RTExceptions_IsInExceptionState (void)
1136 {
1137 return inException;
1138 /* static analysis guarentees a RETURN statement will be used before here. */
1139 __builtin_unreachable ();
1140 }
1141
1142
1143 /*
1144 SetExceptionState - returns the current exception state and
1145 then sets the current exception state to,
1146 to.
1147 */
1148
1149 extern "C" bool RTExceptions_SetExceptionState (bool to)
1150 {
1151 bool old;
1152
1153 old = inException;
1154 inException = to;
1155 return old;
1156 /* static analysis guarentees a RETURN statement will be used before here. */
1157 __builtin_unreachable ();
1158 }
1159
1160
1161 /*
1162 SwitchExceptionState - assigns, from, with the current exception
1163 state and then assigns the current exception
1164 to, to.
1165 */
1166
1167 extern "C" void RTExceptions_SwitchExceptionState (bool *from, bool to)
1168 {
1169 (*from) = inException;
1170 inException = to;
1171 }
1172
1173
1174 /*
1175 GetBaseExceptionBlock - returns the initial language exception block
1176 created.
1177 */
1178
1179 extern "C" RTExceptions_EHBlock RTExceptions_GetBaseExceptionBlock (void)
1180 {
1181 if (currentEHB == NULL)
1182 {
1183 M2RTS_Halt ((const char *) "currentEHB has not been initialized yet", 39, (const char *) "../../gcc/m2/gm2-libs/RTExceptions.mod", 38, (const char *) "GetBaseExceptionBlock", 21, 600);
1184 }
1185 else
1186 {
1187 return static_cast<RTExceptions_EHBlock> (currentEHB);
1188 }
1189 ReturnException ("../../gcc/m2/gm2-libs/RTExceptions.def", 25, 1);
1190 __builtin_unreachable ();
1191 }
1192
1193
1194 /*
1195 SetExceptionSource - sets the current exception source to, source.
1196 */
1197
1198 extern "C" void RTExceptions_SetExceptionSource (void * source)
1199 {
1200 currentSource = source;
1201 }
1202
1203
1204 /*
1205 GetExceptionSource - returns the current exception source.
1206 */
1207
1208 extern "C" void * RTExceptions_GetExceptionSource (void)
1209 {
1210 return currentSource;
1211 /* static analysis guarentees a RETURN statement will be used before here. */
1212 __builtin_unreachable ();
1213 }
1214
1215 extern "C" void _M2_RTExceptions_init (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
1216 {
1217 Init ();
1218 }
1219
1220 extern "C" void _M2_RTExceptions_fini (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
1221 {
1222 TidyUp ();
1223 }