diff options
author | Tristan Gingold <gingold@adacore.com> | 2013-10-17 13:47:37 +0000 |
---|---|---|
committer | Arnaud Charlet <charlet@gcc.gnu.org> | 2013-10-17 15:47:37 +0200 |
commit | c2cd3032e67390915044effcfe7d0ffd2c6fccbd (patch) | |
tree | 1e83e328ae9c573fe82380b9609587724b52aa51 | |
parent | ee6decab8f5307ff86d00894b3bbd9219aa22944 (diff) | |
download | gcc-c2cd3032e67390915044effcfe7d0ffd2c6fccbd.zip gcc-c2cd3032e67390915044effcfe7d0ffd2c6fccbd.tar.gz gcc-c2cd3032e67390915044effcfe7d0ffd2c6fccbd.tar.bz2 |
g-cppexc.adb, [...]: New files.
2013-10-17 Tristan Gingold <gingold@adacore.com>
* g-cppexc.adb, g-cppexc.ads: New files.
* gcc-interface/Makefile.in: Add g-cppexc when building zcx runtimes.
From-SVN: r203757
-rw-r--r-- | gcc/ada/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/ada/g-cppexc.adb | 139 | ||||
-rw-r--r-- | gcc/ada/g-cppexc.ads | 48 | ||||
-rw-r--r-- | gcc/ada/gcc-interface/Makefile.in | 7 |
4 files changed, 196 insertions, 3 deletions
diff --git a/gcc/ada/ChangeLog b/gcc/ada/ChangeLog index 7777a8a..1dc9b05 100644 --- a/gcc/ada/ChangeLog +++ b/gcc/ada/ChangeLog @@ -1,3 +1,8 @@ +2013-10-17 Tristan Gingold <gingold@adacore.com> + + * g-cppexc.adb, g-cppexc.ads: New files. + * gcc-interface/Makefile.in: Add g-cppexc when building zcx runtimes. + 2013-10-17 Thomas Quinot <quinot@adacore.com> * exp_ch7.adb: Minor reformatting. diff --git a/gcc/ada/g-cppexc.adb b/gcc/ada/g-cppexc.adb new file mode 100644 index 0000000..d89cf0c --- /dev/null +++ b/gcc/ada/g-cppexc.adb @@ -0,0 +1,139 @@ +------------------------------------------------------------------------------ +-- -- +-- GNAT RUN-TIME COMPONENTS -- +-- -- +-- G N A T . C P P _ E X C E P T I O N S -- +-- -- +-- B o d y -- +-- -- +-- Copyright (C) 2013, AdaCore -- +-- -- +-- GNAT is free software; you can redistribute it and/or modify it under -- +-- terms of the GNU General Public License as published by the Free Soft- -- +-- ware Foundation; either version 3, or (at your option) any later ver- -- +-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- +-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- +-- or FITNESS FOR A PARTICULAR PURPOSE. -- +-- -- +-- As a special exception under Section 7 of GPL version 3, you are granted -- +-- additional permissions described in the GCC Runtime Library Exception, -- +-- version 3.1, as published by the Free Software Foundation. -- +-- -- +-- You should have received a copy of the GNU General Public License and -- +-- a copy of the GCC Runtime Library Exception along with this program; -- +-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- +-- <http://www.gnu.org/licenses/>. -- +-- -- +-- GNAT was originally developed by the GNAT team at New York University. -- +-- Extensive contributions were provided by Ada Core Technologies Inc. -- +-- -- +------------------------------------------------------------------------------ + +with System; +with System.Storage_Elements; +with Interfaces.C; use Interfaces.C; +with Ada.Unchecked_Conversion; +with System.Standard_Library; use System.Standard_Library; + +package body GNAT.CPP_Exceptions is + + -- Note: all functions prefixed by __cxa are part of the c++ ABI for + -- exception handling. As they are provided by the c++ library, there + -- must be no dependencies on it in the compiled code of this unit, but + -- there can be dependencies in instances. This is required to be able + -- to build the shared library without the c++ library. + + function To_Exception_Data_Ptr is new + Ada.Unchecked_Conversion + (Exception_Id, Exception_Data_Ptr); + -- Convert an Exception_Id to its non-private type. This is used to get + -- the RTTI of a C++ exception + + function Get_Exception_Machine_Occurrence + (X : Exception_Occurrence) return System.Address; + pragma Import (Ada, Get_Exception_Machine_Occurrence, + "__gnat_get_exception_machine_occurrence"); + -- Imported function (from Ada.Exceptions) that returns the machine + -- occurrence from an exception occurrence. + + ------------------------- + -- Raise_Cpp_Exception -- + ------------------------- + + procedure Raise_Cpp_Exception (Id : Exception_Id; Value : T) + is + Id_Data : constant Exception_Data_Ptr := To_Exception_Data_Ptr (Id); + -- Get a non-private view on the exception + + type T_Acc is access all T; + pragma Convention (C, T_Acc); + -- Access type to the object compatible with C + + Occ : T_Acc; + -- The occurrence to propagate + + function cxa_allocate_exception (Size : size_t) return T_Acc; + pragma Import (C, cxa_allocate_exception, "__cxa_allocate_exception"); + -- The C++ function to allocate an occurrence + + procedure cxa_throw (Obj : T_Acc; Tinfo : System.Address; + Dest : System.Address); + pragma Import (C, cxa_throw, "__cxa_throw"); + pragma No_Return (cxa_throw); + -- The C++ function to raise an exception + begin + -- Check the exception was imported from C++ + + if Id_Data.Lang /= 'C' then + raise Constraint_Error; + end if; + + -- Allocate the C++ occurrence + + Occ := cxa_allocate_exception (T'Size / System.Storage_Unit); + + -- Set the object + + Occ.all := Value; + + -- Throw the exception + + cxa_throw (Occ, Id_Data.Foreign_Data, System.Null_Address); + end Raise_Cpp_Exception; + + ---------------- + -- Get_Object -- + ---------------- + + function Get_Object (X : Exception_Occurrence) return T + is + use System; + use System.Storage_Elements; + + Unwind_Exception_Size : Natural; + pragma Import (C, Unwind_Exception_Size, "__gnat_unwind_exception_size"); + -- Size in bytes of _Unwind_Exception + + Exception_Addr : constant Address := + Get_Exception_Machine_Occurrence (X); + -- Machine occurrence of X + + begin + -- Check the machine occurrence exists + + if Exception_Addr = Null_Address then + raise Constraint_Error; + end if; + + declare + -- Import the object from the occurrence + Result : T; + pragma Import (Ada, Result); + for Result'Address use + Exception_Addr + Storage_Offset (Unwind_Exception_Size); + begin + -- And return it + return Result; + end; + end Get_Object; +end GNAT.CPP_Exceptions; diff --git a/gcc/ada/g-cppexc.ads b/gcc/ada/g-cppexc.ads new file mode 100644 index 0000000..60105e6f --- /dev/null +++ b/gcc/ada/g-cppexc.ads @@ -0,0 +1,48 @@ +------------------------------------------------------------------------------ +-- -- +-- GNAT RUN-TIME COMPONENTS -- +-- -- +-- G N A T . C P P _ E X C E P T I O N S -- +-- -- +-- S p e c -- +-- -- +-- Copyright (C) 2013, AdaCore -- +-- -- +-- GNAT is free software; you can redistribute it and/or modify it under -- +-- terms of the GNU General Public License as published by the Free Soft- -- +-- ware Foundation; either version 3, or (at your option) any later ver- -- +-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- +-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- +-- or FITNESS FOR A PARTICULAR PURPOSE. -- +-- -- +-- As a special exception under Section 7 of GPL version 3, you are granted -- +-- additional permissions described in the GCC Runtime Library Exception, -- +-- version 3.1, as published by the Free Software Foundation. -- +-- -- +-- You should have received a copy of the GNU General Public License and -- +-- a copy of the GCC Runtime Library Exception along with this program; -- +-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- +-- <http://www.gnu.org/licenses/>. -- +-- -- +-- GNAT was originally developed by the GNAT team at New York University. -- +-- Extensive contributions were provided by Ada Core Technologies Inc. -- +-- -- +------------------------------------------------------------------------------ + +-- This package provides an interface for raising and handling C++ exceptions + +with Ada.Exceptions; use Ada.Exceptions; + +package GNAT.CPP_Exceptions is + generic + type T is private; + procedure Raise_Cpp_Exception (Id : Exception_Id; Value : T); + -- Raise a C++ exception identified by Id. Associate Value with this + -- occurrence. Id must refer to an exception that has the Cpp convention. + + generic + type T is private; + function Get_Object (X : Exception_Occurrence) return T; + -- Extract the object associated with X. The exception of the occurrence + -- X must have a Cpp Convention. +end GNAT.CPP_Exceptions; diff --git a/gcc/ada/gcc-interface/Makefile.in b/gcc/ada/gcc-interface/Makefile.in index f047372..a5ca90d 100644 --- a/gcc/ada/gcc-interface/Makefile.in +++ b/gcc/ada/gcc-interface/Makefile.in @@ -2307,9 +2307,10 @@ ifeq ($(strip $(filter-out arm nucleus%,$(target_cpu) $(target_os))),) GNATRTL_SOCKETS_OBJS = endif -ifneq ($(EH_MECHANISM),) - LIBGNAT_TARGET_PAIRS += a-exexpr.adb<a-exexpr$(EH_MECHANISM).adb - EXTRA_LIBGNAT_OBJS+=raise$(EH_MECHANISM).o +ifeq ($(EH_MECHANISM),-gcc) + LIBGNAT_TARGET_PAIRS += a-exexpr.adb<a-exexpr-gcc.adb + EXTRA_LIBGNAT_OBJS+=raise-gcc.o + EXTRA_GNATRTL_NONTASKING_OBJS+=g-cppexc.o endif # Use the Ada 2005 version of Ada.Exceptions by default, unless specified |