Debian Codegraph @

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