1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
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 --
9 -- Copyright (C) 1992-2013, Free Software Foundation, Inc. --
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 3, 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. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 -- This is the version using the GCC EH mechanism
34 with Ada
.Unchecked_Conversion
;
35 with Ada
.Unchecked_Deallocation
;
37 with System
.Storage_Elements
; use System
.Storage_Elements
;
38 with System
.Exceptions
.Machine
; use System
.Exceptions
.Machine
;
40 separate (Ada
.Exceptions
)
41 package body Exception_Propagation
is
45 Foreign_Exception
: aliased System
.Standard_Library
.Exception_Data
;
46 pragma Import
(Ada
, Foreign_Exception
,
47 "system__exceptions__foreign_exception");
48 -- Id for foreign exceptions
50 --------------------------------------------------------------
51 -- GNAT Specific Entities To Deal With The GCC EH Circuitry --
52 --------------------------------------------------------------
54 procedure GNAT_GCC_Exception_Cleanup
55 (Reason
: Unwind_Reason_Code
;
56 Excep
: not null GNAT_GCC_Exception_Access
);
57 pragma Convention
(C
, GNAT_GCC_Exception_Cleanup
);
58 -- Procedure called when a GNAT GCC exception is free.
60 procedure Propagate_GCC_Exception
61 (GCC_Exception
: not null GCC_Exception_Access
);
62 pragma No_Return
(Propagate_GCC_Exception
);
63 -- Propagate a GCC exception
65 procedure Reraise_GCC_Exception
66 (GCC_Exception
: not null GCC_Exception_Access
);
67 pragma No_Return
(Reraise_GCC_Exception
);
68 pragma Export
(C
, Reraise_GCC_Exception
, "__gnat_reraise_zcx");
69 -- Called to implement raise without exception, ie reraise. Called
70 -- directly from gigi.
72 function Setup_Current_Excep
73 (GCC_Exception
: not null GCC_Exception_Access
) return EOA
;
74 pragma Export
(C
, Setup_Current_Excep
, "__gnat_setup_current_excep");
75 -- Write Get_Current_Excep.all from GCC_Exception. Called by the
76 -- personality routine.
78 procedure Unhandled_Except_Handler
79 (GCC_Exception
: not null GCC_Exception_Access
);
80 pragma No_Return
(Unhandled_Except_Handler
);
81 pragma Export
(C
, Unhandled_Except_Handler
,
82 "__gnat_unhandled_except_handler");
83 -- Called for handle unhandled exceptions, ie the last chance handler
84 -- on platforms (such as SEH) that never returns after throwing an
85 -- exception. Called directly by gigi.
87 function CleanupUnwind_Handler
88 (UW_Version
: Integer;
89 UW_Phases
: Unwind_Action
;
90 UW_Eclass
: Exception_Class
;
91 UW_Exception
: not null GCC_Exception_Access
;
92 UW_Context
: System
.Address
;
93 UW_Argument
: System
.Address
) return Unwind_Reason_Code
;
94 pragma Import
(C
, CleanupUnwind_Handler
,
95 "__gnat_cleanupunwind_handler");
96 -- Hook called at each step of the forced unwinding we perform to trigger
97 -- cleanups found during the propagation of an unhandled exception.
99 -- GCC runtime functions used. These are C non-void functions, actually,
100 -- but we ignore the return values. See raise.c as to why we are using
101 -- __gnat stubs for these.
103 procedure Unwind_RaiseException
104 (UW_Exception
: not null GCC_Exception_Access
);
105 pragma Import
(C
, Unwind_RaiseException
, "__gnat_Unwind_RaiseException");
107 procedure Unwind_ForcedUnwind
108 (UW_Exception
: not null GCC_Exception_Access
;
109 UW_Handler
: System
.Address
;
110 UW_Argument
: System
.Address
);
111 pragma Import
(C
, Unwind_ForcedUnwind
, "__gnat_Unwind_ForcedUnwind");
113 procedure Set_Exception_Parameter
115 GCC_Exception
: not null GCC_Exception_Access
);
117 (C
, Set_Exception_Parameter
, "__gnat_set_exception_parameter");
118 -- Called inserted by gigi to initialize the exception parameter
120 procedure Set_Foreign_Occurrence
(Excep
: EOA
; Mo
: System
.Address
);
121 -- Utility routine to initialize occurrence Excep from a foreign exception
122 -- whose machine occurrence is Mo. The message is empty, the backtrace
123 -- is empty too and the exception identity is Foreign_Exception.
125 -- Hooks called when entering/leaving an exception handler for a given
126 -- occurrence, aimed at handling the stack of active occurrences. The
127 -- calls are generated by gigi in tree_transform/N_Exception_Handler.
129 procedure Begin_Handler
(GCC_Exception
: not null GCC_Exception_Access
);
130 pragma Export
(C
, Begin_Handler
, "__gnat_begin_handler");
132 procedure End_Handler
(GCC_Exception
: GCC_Exception_Access
);
133 pragma Export
(C
, End_Handler
, "__gnat_end_handler");
135 --------------------------------------------------------------------
136 -- Accessors to Basic Components of a GNAT Exception Data Pointer --
137 --------------------------------------------------------------------
139 -- As of today, these are only used by the C implementation of the GCC
140 -- propagation personality routine to avoid having to rely on a C
141 -- counterpart of the whole exception_data structure, which is both
142 -- painful and error prone. These subprograms could be moved to a more
143 -- widely visible location if need be.
145 function Is_Handled_By_Others
(E
: Exception_Data_Ptr
) return Boolean;
146 pragma Export
(C
, Is_Handled_By_Others
, "__gnat_is_handled_by_others");
147 pragma Warnings
(Off
, Is_Handled_By_Others
);
149 function Language_For
(E
: Exception_Data_Ptr
) return Character;
150 pragma Export
(C
, Language_For
, "__gnat_language_for");
152 function Foreign_Data_For
(E
: Exception_Data_Ptr
) return Address
;
153 pragma Export
(C
, Foreign_Data_For
, "__gnat_foreign_data_for");
155 function EID_For
(GNAT_Exception
: not null GNAT_GCC_Exception_Access
)
157 pragma Export
(C
, EID_For
, "__gnat_eid_for");
159 ---------------------------------------------------------------------------
160 -- Objects to materialize "others" and "all others" in the GCC EH tables --
161 ---------------------------------------------------------------------------
163 -- Currently, these only have their address taken and compared so there is
164 -- no real point having whole exception data blocks allocated. Note that
165 -- there are corresponding declarations in gigi (trans.c) which must be
166 -- kept properly synchronized.
168 Others_Value
: constant Character := 'O';
169 pragma Export
(C
, Others_Value
, "__gnat_others_value");
171 All_Others_Value
: constant Character := 'A';
172 pragma Export
(C
, All_Others_Value
, "__gnat_all_others_value");
174 Unhandled_Others_Value
: constant Character := 'U';
175 pragma Export
(C
, Unhandled_Others_Value
, "__gnat_unhandled_others_value");
176 -- Special choice (emitted by gigi) to catch and notify unhandled
177 -- exceptions on targets which always handle exceptions (such as SEH).
178 -- The handler will simply call Unhandled_Except_Handler.
180 -------------------------
181 -- Allocate_Occurrence --
182 -------------------------
184 function Allocate_Occurrence
return EOA
is
185 Res
: GNAT_GCC_Exception_Access
;
188 Res
:= New_Occurrence
;
189 Res
.Header
.Cleanup
:= GNAT_GCC_Exception_Cleanup
'Address;
190 Res
.Occurrence
.Machine_Occurrence
:= Res
.all'Address;
192 return Res
.Occurrence
'Access;
193 end Allocate_Occurrence
;
195 --------------------------------
196 -- GNAT_GCC_Exception_Cleanup --
197 --------------------------------
199 procedure GNAT_GCC_Exception_Cleanup
200 (Reason
: Unwind_Reason_Code
;
201 Excep
: not null GNAT_GCC_Exception_Access
)
203 pragma Unreferenced
(Reason
);
205 procedure Free
is new Unchecked_Deallocation
206 (GNAT_GCC_Exception
, GNAT_GCC_Exception_Access
);
208 Copy
: GNAT_GCC_Exception_Access
:= Excep
;
211 -- Simply free the memory
214 end GNAT_GCC_Exception_Cleanup
;
216 ----------------------------
217 -- Set_Foreign_Occurrence --
218 ----------------------------
220 procedure Set_Foreign_Occurrence
(Excep
: EOA
; Mo
: System
.Address
) is
223 Id
=> Foreign_Exception
'Access,
224 Machine_Occurrence
=> Mo
,
227 Exception_Raised
=> True,
228 Pid
=> Local_Partition_ID
,
231 end Set_Foreign_Occurrence
;
233 -------------------------
234 -- Setup_Current_Excep --
235 -------------------------
237 function Setup_Current_Excep
238 (GCC_Exception
: not null GCC_Exception_Access
) return EOA
240 Excep
: constant EOA
:= Get_Current_Excep
.all;
243 -- Setup the exception occurrence
245 if GCC_Exception
.Class
= GNAT_Exception_Class
then
247 -- From the GCC exception
250 GNAT_Occurrence
: constant GNAT_GCC_Exception_Access
:=
251 To_GNAT_GCC_Exception
(GCC_Exception
);
253 Excep
.all := GNAT_Occurrence
.Occurrence
;
254 return GNAT_Occurrence
.Occurrence
'Access;
260 Set_Foreign_Occurrence
(Excep
, GCC_Exception
.all'Address);
264 end Setup_Current_Excep
;
270 procedure Begin_Handler
(GCC_Exception
: not null GCC_Exception_Access
) is
271 pragma Unreferenced
(GCC_Exception
);
280 procedure End_Handler
(GCC_Exception
: GCC_Exception_Access
) is
282 if GCC_Exception
/= null then
284 -- The exception might have been reraised, in this case the cleanup
285 -- mustn't be called.
287 Unwind_DeleteException
(GCC_Exception
);
291 -----------------------------
292 -- Reraise_GCC_Exception --
293 -----------------------------
295 procedure Reraise_GCC_Exception
296 (GCC_Exception
: not null GCC_Exception_Access
)
299 -- Simply propagate it
301 Propagate_GCC_Exception
(GCC_Exception
);
302 end Reraise_GCC_Exception
;
304 -----------------------------
305 -- Propagate_GCC_Exception --
306 -----------------------------
308 -- Call Unwind_RaiseException to actually throw, taking care of handling
309 -- the two phase scheme it implements.
311 procedure Propagate_GCC_Exception
312 (GCC_Exception
: not null GCC_Exception_Access
)
317 -- Perform a standard raise first. If a regular handler is found, it
318 -- will be entered after all the intermediate cleanups have run. If
319 -- there is no regular handler, it will return.
321 Unwind_RaiseException
(GCC_Exception
);
323 -- If we get here we know the exception is not handled, as otherwise
324 -- Unwind_RaiseException arranges for the handler to be entered. Take
325 -- the necessary steps to enable the debugger to gain control while the
326 -- stack is still intact.
328 Excep
:= Setup_Current_Excep
(GCC_Exception
);
329 Notify_Unhandled_Exception
(Excep
);
331 -- Now, un a forced unwind to trigger cleanups. Control should not
332 -- resume there, if there are cleanups and in any cases as the
333 -- unwinding hook calls Unhandled_Exception_Terminate when end of
338 CleanupUnwind_Handler
'Address,
339 System
.Null_Address
);
341 -- We get here in case of error. The debugger has been notified before
342 -- the second step above.
344 Unhandled_Except_Handler
(GCC_Exception
);
345 end Propagate_GCC_Exception
;
347 -------------------------
348 -- Propagate_Exception --
349 -------------------------
351 procedure Propagate_Exception
(Excep
: EOA
) is
353 Propagate_GCC_Exception
(To_GCC_Exception
(Excep
.Machine_Occurrence
));
354 end Propagate_Exception
;
356 -----------------------------
357 -- Set_Exception_Parameter --
358 -----------------------------
360 procedure Set_Exception_Parameter
362 GCC_Exception
: not null GCC_Exception_Access
)
365 -- Setup the exception occurrence
367 if GCC_Exception
.Class
= GNAT_Exception_Class
then
369 -- From the GCC exception
372 GNAT_Occurrence
: constant GNAT_GCC_Exception_Access
:=
373 To_GNAT_GCC_Exception
(GCC_Exception
);
375 Save_Occurrence
(Excep
.all, GNAT_Occurrence
.Occurrence
);
381 Set_Foreign_Occurrence
(Excep
, GCC_Exception
.all'Address);
383 end Set_Exception_Parameter
;
385 ------------------------------
386 -- Unhandled_Except_Handler --
387 ------------------------------
389 procedure Unhandled_Except_Handler
390 (GCC_Exception
: not null GCC_Exception_Access
)
394 Excep
:= Setup_Current_Excep
(GCC_Exception
);
395 Unhandled_Exception_Terminate
(Excep
);
396 end Unhandled_Except_Handler
;
403 (GNAT_Exception
: not null GNAT_GCC_Exception_Access
) return Exception_Id
406 return GNAT_Exception
.Occurrence
.Id
;
409 ----------------------
410 -- Foreign_Data_For --
411 ----------------------
413 function Foreign_Data_For
414 (E
: SSL
.Exception_Data_Ptr
) return Address
417 return E
.Foreign_Data
;
418 end Foreign_Data_For
;
420 --------------------------
421 -- Is_Handled_By_Others --
422 --------------------------
424 function Is_Handled_By_Others
(E
: SSL
.Exception_Data_Ptr
) return Boolean is
426 return not E
.all.Not_Handled_By_Others
;
427 end Is_Handled_By_Others
;
433 function Language_For
(E
: SSL
.Exception_Data_Ptr
) return Character is
438 end Exception_Propagation
;