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