Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / gcc / ada / a-exexpr-gcc.adb
blob22f057d18a4e72598db25586b1760168e7cbe41d
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- A D A . E X C E P T I O N S . E X C E P T I O N _ P R O P A G A T I O N --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2005, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 -- This is the version using the GCC EH mechanism
36 with Ada.Unchecked_Conversion;
37 with Ada.Unchecked_Deallocation;
39 with System.Storage_Elements; use System.Storage_Elements;
41 separate (Ada.Exceptions)
42 package body Exception_Propagation is
44 ------------------------------------------------
45 -- Entities to interface with the GCC runtime --
46 ------------------------------------------------
48 -- These come from "C++ ABI for Itanium: Exception handling", which is
49 -- the reference for GCC. They are used only when we are relying on
50 -- back-end tables for exception propagation, which in turn is currenly
51 -- only the case for Zero_Cost_Exceptions in GNAT5.
53 -- Return codes from the GCC runtime functions used to propagate
54 -- an exception.
56 type Unwind_Reason_Code is
57 (URC_NO_REASON,
58 URC_FOREIGN_EXCEPTION_CAUGHT,
59 URC_PHASE2_ERROR,
60 URC_PHASE1_ERROR,
61 URC_NORMAL_STOP,
62 URC_END_OF_STACK,
63 URC_HANDLER_FOUND,
64 URC_INSTALL_CONTEXT,
65 URC_CONTINUE_UNWIND);
67 pragma Unreferenced
68 (URC_FOREIGN_EXCEPTION_CAUGHT,
69 URC_PHASE2_ERROR,
70 URC_PHASE1_ERROR,
71 URC_NORMAL_STOP,
72 URC_END_OF_STACK,
73 URC_HANDLER_FOUND,
74 URC_INSTALL_CONTEXT,
75 URC_CONTINUE_UNWIND);
77 pragma Convention (C, Unwind_Reason_Code);
79 -- Phase identifiers
81 type Unwind_Action is
82 (UA_SEARCH_PHASE,
83 UA_CLEANUP_PHASE,
84 UA_HANDLER_FRAME,
85 UA_FORCE_UNWIND);
87 for Unwind_Action use
88 (UA_SEARCH_PHASE => 1,
89 UA_CLEANUP_PHASE => 2,
90 UA_HANDLER_FRAME => 4,
91 UA_FORCE_UNWIND => 8);
93 pragma Convention (C, Unwind_Action);
95 -- Mandatory common header for any exception object handled by the
96 -- GCC unwinding runtime.
98 type Exception_Class is mod 2 ** 64;
100 GNAT_Exception_Class : constant Exception_Class := 16#474e552d41646100#;
101 -- "GNU-Ada\0"
103 type Unwind_Word is mod 2 ** System.Word_Size;
104 for Unwind_Word'Size use System.Word_Size;
105 -- Map the corresponding C type used in Unwind_Exception below
107 type Unwind_Exception is record
108 Class : Exception_Class := GNAT_Exception_Class;
109 Cleanup : System.Address := System.Null_Address;
110 Private1 : Unwind_Word;
111 Private2 : Unwind_Word;
112 end record;
113 -- Map the GCC struct used for exception handling
115 for Unwind_Exception'Alignment use Standard'Maximum_Alignment;
116 -- The C++ ABI mandates the common exception header to be at least
117 -- doubleword aligned, and the libGCC implementation actually makes it
118 -- maximally aligned (see unwind.h). See additional comments on the
119 -- alignment below.
121 --------------------------------------------------------------
122 -- GNAT Specific Entities To Deal With The GCC EH Circuitry --
123 --------------------------------------------------------------
125 -- A GNAT exception object to be dealt with by the personality routine
126 -- called by the GCC unwinding runtime.
128 type GNAT_GCC_Exception is record
129 Header : Unwind_Exception;
130 -- ABI Exception header first
132 Id : Exception_Id;
133 -- GNAT Exception identifier. This is filled by Propagate_Exception
134 -- and then used by the personality routine to determine if the context
135 -- it examines contains a handler for the exception beeing propagated.
137 N_Cleanups_To_Trigger : Integer;
138 -- Number of cleanup only frames encountered in SEARCH phase. This is
139 -- initialized to 0 by Propagate_Exception and maintained by the
140 -- personality routine to control a forced unwinding phase triggering
141 -- all the cleanups before calling Unhandled_Exception_Terminate when
142 -- an exception is not handled.
144 Next_Exception : EOA;
145 -- Used to create a linked list of exception occurrences
146 end record;
148 pragma Convention (C, GNAT_GCC_Exception);
150 -- There is a subtle issue with the common header alignment, since the C
151 -- version is aligned on BIGGEST_ALIGNMENT, the Ada version is aligned on
152 -- Standard'Maximum_Alignment, and those two values don't quite represent
153 -- the same concepts and so may be decoupled someday. One typical reason
154 -- is that BIGGEST_ALIGNMENT may be larger than what the underlying system
155 -- allocator guarantees, and there are extra costs involved in allocating
156 -- objects aligned to such factors.
158 -- To deal with the potential alignment differences between the C and Ada
159 -- representations, the Ada part of the whole structure is only accessed
160 -- by the personality routine through the accessors declared below. Ada
161 -- specific fields are thus always accessed through consistent layout, and
162 -- we expect the actual alignment to always be large enough to avoid traps
163 -- from the C accesses to the common header. Besides, accessors aleviate
164 -- the need for a C struct whole conterpart, both painful and errorprone
165 -- to maintain anyway.
167 type GNAT_GCC_Exception_Access is access all GNAT_GCC_Exception;
169 function To_GNAT_GCC_Exception is new
170 Unchecked_Conversion (System.Address, GNAT_GCC_Exception_Access);
172 procedure Free is new Unchecked_Deallocation
173 (GNAT_GCC_Exception, GNAT_GCC_Exception_Access);
175 procedure Free is new Unchecked_Deallocation
176 (Exception_Occurrence, EOA);
178 function CleanupUnwind_Handler
179 (UW_Version : Integer;
180 UW_Phases : Unwind_Action;
181 UW_Eclass : Exception_Class;
182 UW_Exception : access GNAT_GCC_Exception;
183 UW_Context : System.Address;
184 UW_Argument : System.Address) return Unwind_Reason_Code;
185 -- Hook called at each step of the forced unwinding we perform to
186 -- trigger cleanups found during the propagation of an unhandled
187 -- exception.
189 -- GCC runtime functions used. These are C non-void functions, actually,
190 -- but we ignore the return values. See raise.c as to why we are using
191 -- __gnat stubs for these.
193 procedure Unwind_RaiseException
194 (UW_Exception : access GNAT_GCC_Exception);
195 pragma Import (C, Unwind_RaiseException, "__gnat_Unwind_RaiseException");
197 procedure Unwind_ForcedUnwind
198 (UW_Exception : access GNAT_GCC_Exception;
199 UW_Handler : System.Address;
200 UW_Argument : System.Address);
201 pragma Import (C, Unwind_ForcedUnwind, "__gnat_Unwind_ForcedUnwind");
203 ------------------------------------------------------------------
204 -- Occurrence Stack Management Facilities for the GCC-EH Scheme --
205 ------------------------------------------------------------------
207 function Remove
208 (Top : EOA;
209 Excep : GNAT_GCC_Exception_Access) return Boolean;
210 -- Remove Excep from the stack starting at Top.
211 -- Return True if Excep was found and removed, false otherwise.
213 -- Hooks called when entering/leaving an exception handler for a given
214 -- occurrence, aimed at handling the stack of active occurrences. The
215 -- calls are generated by gigi in tree_transform/N_Exception_Handler.
217 procedure Begin_Handler (GCC_Exception : GNAT_GCC_Exception_Access);
218 pragma Export (C, Begin_Handler, "__gnat_begin_handler");
220 procedure End_Handler (GCC_Exception : GNAT_GCC_Exception_Access);
221 pragma Export (C, End_Handler, "__gnat_end_handler");
223 Setup_Key : constant := 16#DEAD#;
224 -- To handle the case of a task "transferring" an exception occurrence to
225 -- another task, for instance via Exceptional_Complete_Rendezvous, we need
226 -- to be able to identify occurrences which have been Setup and not yet
227 -- Propagated. We hijack one of the common header fields for that purpose,
228 -- setting it to a special key value during the setup process, clearing it
229 -- at the very beginning of the propagation phase, and expecting it never
230 -- to be reset to the special value later on. A 16-bit value is used rather
231 -- than a 32-bit value for static compatibility with 16-bit targets such as
232 -- AAMP (where type Unwind_Word will be 16 bits).
234 function Is_Setup_And_Not_Propagated (E : EOA) return Boolean;
236 procedure Set_Setup_And_Not_Propagated (E : EOA);
237 procedure Clear_Setup_And_Not_Propagated (E : EOA);
239 procedure Save_Occurrence_And_Private
240 (Target : out Exception_Occurrence;
241 Source : Exception_Occurrence);
242 -- Copy all the components of Source to Target as well as the
243 -- Private_Data pointer.
245 ------------------------------------------------------------
246 -- Accessors to basic components of a GNAT exception data --
247 ------------------------------------------------------------
249 -- As of today, these are only used by the C implementation of the
250 -- GCC propagation personality routine to avoid having to rely on a C
251 -- counterpart of the whole exception_data structure, which is both
252 -- painful and error prone. These subprograms could be moved to a
253 -- more widely visible location if need be.
255 function Is_Handled_By_Others (E : Exception_Data_Ptr) return Boolean;
256 pragma Export (C, Is_Handled_By_Others, "__gnat_is_handled_by_others");
258 function Language_For (E : Exception_Data_Ptr) return Character;
259 pragma Export (C, Language_For, "__gnat_language_for");
261 function Import_Code_For (E : Exception_Data_Ptr) return Exception_Code;
262 pragma Export (C, Import_Code_For, "__gnat_import_code_for");
264 function EID_For (GNAT_Exception : GNAT_GCC_Exception_Access)
265 return Exception_Id;
266 pragma Export (C, EID_For, "__gnat_eid_for");
268 procedure Adjust_N_Cleanups_For
269 (GNAT_Exception : GNAT_GCC_Exception_Access;
270 Adjustment : Integer);
271 pragma Export (C, Adjust_N_Cleanups_For, "__gnat_adjust_n_cleanups_for");
273 ---------------------------------------------------------------------------
274 -- Objects to materialize "others" and "all others" in the GCC EH tables --
275 ---------------------------------------------------------------------------
277 -- Currently, these only have their address taken and compared so there is
278 -- no real point having whole exception data blocks allocated. In any case
279 -- the types should match what gigi and the personality routine expect.
280 -- The initial value is an arbitrary value that will not exceed the range
281 -- of Integer on 16-bit targets (such as AAMP).
283 Others_Value : constant Integer := 16#7FFF#;
284 pragma Export (C, Others_Value, "__gnat_others_value");
286 All_Others_Value : constant Integer := 16#7FFF#;
287 pragma Export (C, All_Others_Value, "__gnat_all_others_value");
289 ------------
290 -- Remove --
291 ------------
293 function Remove
294 (Top : EOA;
295 Excep : GNAT_GCC_Exception_Access) return Boolean
297 Prev : GNAT_GCC_Exception_Access := null;
298 Iter : EOA := Top;
299 GCC_Exception : GNAT_GCC_Exception_Access;
301 begin
302 -- Pop stack
304 loop
305 pragma Assert (Iter.Private_Data /= System.Null_Address);
307 GCC_Exception := To_GNAT_GCC_Exception (Iter.Private_Data);
309 if GCC_Exception = Excep then
310 if Prev = null then
312 -- Special case for the top of the stack: shift the contents
313 -- of the next item to the top, since top is at a fixed
314 -- location and can't be changed.
316 Iter := GCC_Exception.Next_Exception;
318 if Iter = null then
320 -- Stack is now empty
322 Top.Private_Data := System.Null_Address;
324 else
325 Save_Occurrence_And_Private (Top.all, Iter.all);
326 Free (Iter);
327 end if;
329 else
330 Prev.Next_Exception := GCC_Exception.Next_Exception;
331 Free (Iter);
332 end if;
334 Free (GCC_Exception);
336 return True;
337 end if;
339 exit when GCC_Exception.Next_Exception = null;
341 Prev := GCC_Exception;
342 Iter := GCC_Exception.Next_Exception;
343 end loop;
345 return False;
346 end Remove;
348 ---------------------------
349 -- CleanupUnwind_Handler --
350 ---------------------------
352 function CleanupUnwind_Handler
353 (UW_Version : Integer;
354 UW_Phases : Unwind_Action;
355 UW_Eclass : Exception_Class;
356 UW_Exception : access GNAT_GCC_Exception;
357 UW_Context : System.Address;
358 UW_Argument : System.Address) return Unwind_Reason_Code
360 pragma Unreferenced
361 (UW_Version, UW_Phases, UW_Eclass, UW_Context, UW_Argument);
363 begin
364 -- Terminate as soon as we know there is nothing more to run. The
365 -- count is maintained by the personality routine.
367 if UW_Exception.N_Cleanups_To_Trigger = 0 then
368 Unhandled_Exception_Terminate;
369 end if;
371 -- We know there is at least one cleanup further up. Return so that it
372 -- is searched and entered, after which Unwind_Resume will be called
373 -- and this hook will gain control (with an updated count) again.
375 return URC_NO_REASON;
376 end CleanupUnwind_Handler;
378 ---------------------------------
379 -- Is_Setup_And_Not_Propagated --
380 ---------------------------------
382 function Is_Setup_And_Not_Propagated (E : EOA) return Boolean is
383 GCC_E : constant GNAT_GCC_Exception_Access :=
384 To_GNAT_GCC_Exception (E.Private_Data);
385 begin
386 return GCC_E /= null and then GCC_E.Header.Private1 = Setup_Key;
387 end Is_Setup_And_Not_Propagated;
389 ------------------------------------
390 -- Clear_Setup_And_Not_Propagated --
391 ------------------------------------
393 procedure Clear_Setup_And_Not_Propagated (E : EOA) is
394 GCC_E : constant GNAT_GCC_Exception_Access :=
395 To_GNAT_GCC_Exception (E.Private_Data);
396 begin
397 pragma Assert (GCC_E /= null);
398 GCC_E.Header.Private1 := 0;
399 end Clear_Setup_And_Not_Propagated;
401 ----------------------------------
402 -- Set_Setup_And_Not_Propagated --
403 ----------------------------------
405 procedure Set_Setup_And_Not_Propagated (E : EOA) is
406 GCC_E : constant GNAT_GCC_Exception_Access :=
407 To_GNAT_GCC_Exception (E.Private_Data);
408 begin
409 pragma Assert (GCC_E /= null);
410 GCC_E.Header.Private1 := Setup_Key;
411 end Set_Setup_And_Not_Propagated;
413 --------------------------------
414 -- Save_Occurrence_And_Private --
415 --------------------------------
417 procedure Save_Occurrence_And_Private
418 (Target : out Exception_Occurrence;
419 Source : Exception_Occurrence)
421 begin
422 Save_Occurrence_No_Private (Target, Source);
423 Target.Private_Data := Source.Private_Data;
424 end Save_Occurrence_And_Private;
426 ---------------------
427 -- Setup_Exception --
428 ---------------------
430 -- In the GCC-EH implementation of the propagation scheme, this
431 -- subprogram should be understood as: Setup the exception occurrence
432 -- stack headed at Current for a forthcoming raise of Excep.
434 procedure Setup_Exception
435 (Excep : EOA;
436 Current : EOA;
437 Reraised : Boolean := False)
439 Top : constant EOA := Current;
440 Next : EOA;
441 GCC_Exception : GNAT_GCC_Exception_Access;
443 begin
444 -- The exception Excep is soon to be propagated, and the
445 -- storage used for that will be the occurrence statically allocated
446 -- for the current thread. This storage might currently be used for a
447 -- still active occurrence, so we need to push it on the thread's
448 -- occurrence stack (headed at that static occurrence) before it gets
449 -- clobbered.
451 -- What we do here is to trigger this push when need be, and allocate a
452 -- Private_Data block for the forthcoming Propagation.
454 -- Some tasking rendez-vous attempts lead to an occurrence transfer
455 -- from the server to the client (see Exceptional_Complete_Rendezvous).
456 -- In those cases Setup is called twice for the very same occurrence
457 -- before it gets propagated: once from the server, because this is
458 -- where the occurrence contents is elaborated and known, and then
459 -- once from the client when it detects the case and actually raises
460 -- the exception in its own context.
462 -- The Is_Setup_And_Not_Propagated predicate tells us when we are in
463 -- the second call to Setup for a Transferred occurrence, and there is
464 -- nothing to be done here in this situation. This predicate cannot be
465 -- True if we are dealing with a Reraise, and we may even be called
466 -- with a raw uninitialized Excep occurrence in this case so we should
467 -- not check anyway. Observe the front-end expansion for a "raise;" to
468 -- see that happening. We get a local occurrence and a direct call to
469 -- Save_Occurrence without the intermediate init-proc call.
471 if not Reraised and then Is_Setup_And_Not_Propagated (Excep) then
472 return;
473 end if;
475 -- Allocate what will be the Private_Data block for the exception
476 -- to be propagated.
478 GCC_Exception := new GNAT_GCC_Exception;
480 -- If the Top of the occurrence stack is not currently used for an
481 -- active exception (the stack is empty) we just need to setup the
482 -- Private_Data pointer.
484 -- Otherwise, we also need to shift the contents of the Top of the
485 -- stack in a freshly allocated entry and link everything together.
487 if Top.Private_Data /= System.Null_Address then
488 Next := new Exception_Occurrence;
489 Save_Occurrence_And_Private (Next.all, Top.all);
491 GCC_Exception.Next_Exception := Next;
492 Top.Private_Data := GCC_Exception.all'Address;
493 end if;
495 Top.Private_Data := GCC_Exception.all'Address;
497 Set_Setup_And_Not_Propagated (Top);
498 end Setup_Exception;
500 -------------------
501 -- Begin_Handler --
502 -------------------
504 procedure Begin_Handler (GCC_Exception : GNAT_GCC_Exception_Access) is
505 pragma Unreferenced (GCC_Exception);
507 begin
508 -- Every necessary operation related to the occurrence stack has
509 -- already been performed by Propagate_Exception. This hook remains for
510 -- potential future necessity in optimizing the overall scheme, as well
511 -- a useful debugging tool.
513 null;
514 end Begin_Handler;
516 -----------------
517 -- End_Handler --
518 -----------------
520 procedure End_Handler (GCC_Exception : GNAT_GCC_Exception_Access) is
521 Removed : Boolean;
522 begin
523 Removed := Remove (Get_Current_Excep.all, GCC_Exception);
524 pragma Assert (Removed);
525 end End_Handler;
527 -------------------------
528 -- Propagate_Exception --
529 -------------------------
531 -- Build an object suitable for the libgcc processing and call
532 -- Unwind_RaiseException to actually throw, taking care of handling
533 -- the two phase scheme it implements.
535 procedure Propagate_Exception (From_Signal_Handler : Boolean) is
536 pragma Unreferenced (From_Signal_Handler);
538 Excep : constant EOA := Get_Current_Excep.all;
539 GCC_Exception : GNAT_GCC_Exception_Access;
541 begin
542 pragma Assert (Excep.Private_Data /= System.Null_Address);
544 -- Retrieve the Private_Data for this occurrence and set the useful
545 -- flags for the personality routine, which will be called for each
546 -- frame via Unwind_RaiseException below.
548 GCC_Exception := To_GNAT_GCC_Exception (Excep.Private_Data);
550 Clear_Setup_And_Not_Propagated (Excep);
552 GCC_Exception.Id := Excep.Id;
553 GCC_Exception.N_Cleanups_To_Trigger := 0;
555 -- Compute the backtrace for this occurrence if the corresponding
556 -- binder option has been set. Call_Chain takes care of the reraise
557 -- case.
559 -- ??? Using Call_Chain here means we are going to walk up the stack
560 -- once only for backtracing purposes before doing it again for the
561 -- propagation per se.
563 -- The first inspection is much lighter, though, as it only requires
564 -- partial unwinding of each frame. Additionally, although we could use
565 -- the personality routine to record the addresses while propagating,
566 -- this method has two drawbacks:
568 -- 1) the trace is incomplete if the exception is handled since we
569 -- don't walk past the frame with the handler,
571 -- and
573 -- 2) we would miss the frames for which our personality routine is not
574 -- called, e.g. if C or C++ calls are on the way.
576 Call_Chain (Excep);
578 -- Perform a standard raise first. If a regular handler is found, it
579 -- will be entered after all the intermediate cleanups have run. If
580 -- there is no regular handler, control will get back to after the
581 -- call, with N_Cleanups_To_Trigger set to the number of frames with
582 -- cleanups found on the way up, and none of these already run.
584 Unwind_RaiseException (GCC_Exception);
586 -- If we get here we know the exception is not handled, as otherwise
587 -- Unwind_RaiseException arranges for the handler to be entered. Take
588 -- the necessary steps to enable the debugger to gain control while the
589 -- stack is still intact.
591 Notify_Unhandled_Exception;
593 -- Now, if cleanups have been found, run a forced unwind to trigger
594 -- them. Control should not resume there, as the unwinding hook calls
595 -- Unhandled_Exception_Terminate as soon as the last cleanup has been
596 -- triggered.
598 if GCC_Exception.N_Cleanups_To_Trigger /= 0 then
599 Unwind_ForcedUnwind (GCC_Exception,
600 CleanupUnwind_Handler'Address,
601 System.Null_Address);
602 end if;
604 -- We get here when there is no handler or cleanup to be run at all.
605 -- The debugger has been notified before the second step above.
607 Unhandled_Exception_Terminate;
608 end Propagate_Exception;
610 ---------------------------
611 -- Adjust_N_Cleanups_For --
612 ---------------------------
614 procedure Adjust_N_Cleanups_For
615 (GNAT_Exception : GNAT_GCC_Exception_Access;
616 Adjustment : Integer)
618 begin
619 GNAT_Exception.N_Cleanups_To_Trigger :=
620 GNAT_Exception.N_Cleanups_To_Trigger + Adjustment;
621 end Adjust_N_Cleanups_For;
623 -------------
624 -- EID_For --
625 -------------
627 function EID_For
628 (GNAT_Exception : GNAT_GCC_Exception_Access) return Exception_Id
630 begin
631 return GNAT_Exception.Id;
632 end EID_For;
634 ---------------------
635 -- Import_Code_For --
636 ---------------------
638 function Import_Code_For
639 (E : SSL.Exception_Data_Ptr) return Exception_Code
641 begin
642 return E.all.Import_Code;
643 end Import_Code_For;
645 --------------------------
646 -- Is_Handled_By_Others --
647 --------------------------
649 function Is_Handled_By_Others (E : SSL.Exception_Data_Ptr) return Boolean is
650 begin
651 return not E.all.Not_Handled_By_Others;
652 end Is_Handled_By_Others;
654 ------------------
655 -- Language_For --
656 ------------------
658 function Language_For (E : SSL.Exception_Data_Ptr) return Character is
659 begin
660 return E.all.Lang;
661 end Language_For;
663 -----------
664 -- Notes --
665 -----------
667 -- The current model implemented for the stack of occurrences is a
668 -- simplification of previous attempts, which all prooved to be flawed or
669 -- would have needed significant additional circuitry to be made to work
670 -- correctly.
672 -- We now represent every propagation by a new entry on the stack, which
673 -- means that an exception occurrence may appear more than once (e.g. when
674 -- it is reraised during the course of its own handler).
676 -- This may seem overcostly compared to the C++ model as implemented in
677 -- the g++ v3 libstd. This is actually understandable when one considers
678 -- the extra variations of possible run-time configurations induced by the
679 -- freedom offered by the Save_Occurrence/Reraise_Occurrence public
680 -- interface.
682 -- The basic point is that arranging for an occurrence to always appear at
683 -- most once on the stack requires a way to determine if a given occurence
684 -- is already there, which is not as easy as it might seem.
686 -- An attempt was made to use the Private_Data pointer for this purpose.
687 -- It did not work because:
689 -- 1) The Private_Data has to be saved by Save_Occurrence to be usable
690 -- as a key in case of a later reraise,
692 -- 2) There is no easy way to synchronize End_Handler for an occurrence
693 -- and the data attached to potential copies, so these copies may end
694 -- up pointing to stale data. Moreover ...
696 -- 3) The same address may be reused for different occurrences, which
697 -- defeats the idea of using it as a key.
699 -- The example below illustrates:
701 -- Saved_CE : Exception_Occurrence;
703 -- begin
704 -- raise Constraint_Error;
705 -- exception
706 -- when CE: others =>
707 -- Save_Occurrence (Saved_CE, CE); <= Saved_CE.PDA = CE.PDA
708 -- end;
710 -- <= Saved_CE.PDA is stale (!)
712 -- begin
713 -- raise Program_Error; <= Saved_CE.PDA = PE.PDA (!!)
714 -- exception
715 -- when others =>
716 -- Reraise_Occurrence (Saved_CE);
717 -- end;
719 -- Not releasing the Private_Data via End_Handler could be an option,
720 -- but making this to work while still avoiding memory leaks is far
721 -- from trivial.
723 -- The current scheme has the advantage of beeing simple, and induces
724 -- extra costs only in reraise cases which is acceptable.
726 end Exception_Propagation;