1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--              S Y S T E M . E X C E P T I O N S . M A C H I N E           --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 2013-2019, 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 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.                                     --
17--                                                                          --
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.               --
21--                                                                          --
22-- You should have received a copy of the GNU General Public License and    --
23-- a copy of the GCC Runtime Library Exception along with this program;     --
24-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
25-- <http://www.gnu.org/licenses/>.                                          --
26--                                                                          --
27-- GNAT was originally developed  by the GNAT team at  New York University. --
28-- Extensive contributions were provided by Ada Core Technologies Inc.      --
29--                                                                          --
30------------------------------------------------------------------------------
31
32--  Declaration of the machine exception and some associated facilities. The
33--  machine exception is the object that is propagated by low level routines
34--  and that contains the Ada exception occurrence.
35
36--  This is the version using the GCC EH mechanism
37
38with Ada.Unchecked_Conversion;
39with Ada.Exceptions;
40
41package System.Exceptions.Machine is
42   pragma Preelaborate;
43
44   ------------------------------------------------
45   -- Entities to interface with the GCC runtime --
46   ------------------------------------------------
47
48   --  These come from "C++ ABI for Itanium: Exception handling", which is
49   --  the reference for GCC.
50
51   --  Return codes from the GCC runtime functions used to propagate
52   --  an exception.
53
54   type Unwind_Reason_Code is
55     (URC_NO_REASON,
56      URC_FOREIGN_EXCEPTION_CAUGHT,
57      URC_PHASE2_ERROR,
58      URC_PHASE1_ERROR,
59      URC_NORMAL_STOP,
60      URC_END_OF_STACK,
61      URC_HANDLER_FOUND,
62      URC_INSTALL_CONTEXT,
63      URC_CONTINUE_UNWIND);
64
65   pragma Unreferenced
66     (URC_NO_REASON,
67      URC_FOREIGN_EXCEPTION_CAUGHT,
68      URC_PHASE2_ERROR,
69      URC_PHASE1_ERROR,
70      URC_NORMAL_STOP,
71      URC_END_OF_STACK,
72      URC_HANDLER_FOUND,
73      URC_INSTALL_CONTEXT,
74      URC_CONTINUE_UNWIND);
75
76   pragma Convention (C, Unwind_Reason_Code);
77
78   --  Mandatory common header for any exception object handled by the
79   --  GCC unwinding runtime.
80
81   type Exception_Class is mod 2 ** 64;
82
83   GNAT_Exception_Class : constant Exception_Class := 16#474e552d41646100#;
84   --  "GNU-Ada\0"
85
86   type Unwind_Word is mod 2 ** System.Word_Size;
87   for Unwind_Word'Size use System.Word_Size;
88   --  Map the corresponding C type used in Unwind_Exception below
89
90   type Unwind_Exception is record
91      Class    : Exception_Class;
92      Cleanup  : System.Address;
93      Private1 : Unwind_Word;
94      Private2 : Unwind_Word;
95
96      --  Usual exception structure has only two private fields, but the SEH
97      --  one has six. To avoid making this file more complex, we use six
98      --  fields on all platforms, wasting a few bytes on some.
99
100      Private3 : Unwind_Word;
101      Private4 : Unwind_Word;
102      Private5 : Unwind_Word;
103      Private6 : Unwind_Word;
104   end record;
105   pragma Convention (C, Unwind_Exception);
106   --  Map the GCC struct used for exception handling
107
108   for Unwind_Exception'Alignment use Standard'Maximum_Alignment;
109   --  The C++ ABI mandates the common exception header to be at least
110   --  doubleword aligned, and the libGCC implementation actually makes it
111   --  maximally aligned (see unwind.h). See additional comments on the
112   --  alignment below.
113
114   --  There is a subtle issue with the common header alignment, since the C
115   --  version is aligned on BIGGEST_ALIGNMENT, the Ada version is aligned on
116   --  Standard'Maximum_Alignment, and those two values don't quite represent
117   --  the same concepts and so may be decoupled someday. One typical reason
118   --  is that BIGGEST_ALIGNMENT may be larger than what the underlying system
119   --  allocator guarantees, and there are extra costs involved in allocating
120   --  objects aligned to such factors.
121
122   --  To deal with the potential alignment differences between the C and Ada
123   --  representations, the Ada part of the whole structure is only accessed
124   --  by the personality routine through accessors. Ada specific fields are
125   --  thus always accessed through consistent layout, and we expect the
126   --  actual alignment to always be large enough to avoid traps from the C
127   --  accesses to the common header. Besides, accessors alleviate the need
128   --  for a C struct whole counterpart, both painful and error-prone to
129   --  maintain anyway.
130
131   type GCC_Exception_Access is access all Unwind_Exception;
132   --  Pointer to a GCC exception
133
134   procedure Unwind_DeleteException (Excp : not null GCC_Exception_Access);
135   pragma Import (C, Unwind_DeleteException, "_Unwind_DeleteException");
136   --  Procedure to free any GCC exception
137
138   --------------------------------------------------------------
139   -- GNAT Specific Entities To Deal With The GCC EH Circuitry --
140   --------------------------------------------------------------
141
142   --  A GNAT exception object to be dealt with by the personality routine
143   --  called by the GCC unwinding runtime.
144
145   type GNAT_GCC_Exception is record
146      Header : Unwind_Exception;
147      --  ABI Exception header first
148
149      Occurrence : aliased Ada.Exceptions.Exception_Occurrence;
150      --  The Ada occurrence
151   end record;
152
153   pragma Convention (C, GNAT_GCC_Exception);
154
155   type GNAT_GCC_Exception_Access is access all GNAT_GCC_Exception;
156
157   function To_GCC_Exception is new
158     Ada.Unchecked_Conversion (System.Address, GCC_Exception_Access);
159
160   function To_GNAT_GCC_Exception is new
161     Ada.Unchecked_Conversion
162       (GCC_Exception_Access, GNAT_GCC_Exception_Access);
163
164   function New_Occurrence return GNAT_GCC_Exception_Access;
165   --  Allocate and initialize a machine occurrence
166
167end System.Exceptions.Machine;
168