1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--               S Y S T E M . S T O R A G E _ E L E M E N T S              --
6--                                                                          --
7--                                 B o d y                                  --
8--                                                                          --
9--          Copyright (C) 1992-2013, 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
32pragma Compiler_Unit_Warning;
33
34with Ada.Unchecked_Conversion;
35
36package body System.Storage_Elements is
37
38   pragma Suppress (All_Checks);
39
40   --  Conversion to/from address
41
42   --  Note qualification below of To_Address to avoid ambiguities on VMS
43
44   function To_Address is
45     new Ada.Unchecked_Conversion (Storage_Offset, Address);
46   function To_Offset  is
47     new Ada.Unchecked_Conversion (Address, Storage_Offset);
48
49   --  Conversion to/from integers
50
51   --  These functions must be place first because they are inlined_always
52   --  and are used and inlined in other subprograms defined in this unit.
53
54   ----------------
55   -- To_Address --
56   ----------------
57
58   function To_Address (Value : Integer_Address) return Address is
59   begin
60      return Address (Value);
61   end To_Address;
62
63   ----------------
64   -- To_Integer --
65   ----------------
66
67   function To_Integer (Value : Address) return Integer_Address is
68   begin
69      return Integer_Address (Value);
70   end To_Integer;
71
72   --  Address arithmetic
73
74   ---------
75   -- "+" --
76   ---------
77
78   function "+" (Left : Address; Right : Storage_Offset) return Address is
79   begin
80      return Storage_Elements.To_Address
81        (To_Integer (Left) + To_Integer (To_Address (Right)));
82   end "+";
83
84   function "+" (Left : Storage_Offset; Right : Address) return Address is
85   begin
86      return Storage_Elements.To_Address
87        (To_Integer (To_Address (Left)) + To_Integer (Right));
88   end "+";
89
90   ---------
91   -- "-" --
92   ---------
93
94   function "-" (Left : Address; Right : Storage_Offset) return Address is
95   begin
96      return Storage_Elements.To_Address
97        (To_Integer (Left) - To_Integer (To_Address (Right)));
98   end "-";
99
100   function "-" (Left, Right : Address) return Storage_Offset is
101   begin
102      return To_Offset (Storage_Elements.To_Address
103                         (To_Integer (Left) - To_Integer (Right)));
104   end "-";
105
106   -----------
107   -- "mod" --
108   -----------
109
110   function "mod"
111     (Left  : Address;
112      Right : Storage_Offset) return Storage_Offset
113   is
114   begin
115      if Right > 0 then
116         return Storage_Offset
117           (To_Integer (Left) mod Integer_Address (Right));
118
119         --  The negative case makes no sense since it is a case of a mod where
120         --  the left argument is unsigned and the right argument is signed. In
121         --  accordance with the (spirit of the) permission of RM 13.7.1(16),
122         --  we raise CE, and also include the zero case here. Yes, the RM says
123         --  PE, but this really is so obviously more like a constraint error.
124
125      else
126         raise Constraint_Error;
127      end if;
128   end "mod";
129
130end System.Storage_Elements;
131