summaryrefslogtreecommitdiff
path: root/gcc/ada/s-scaval.adb
blob: 7e386a0375eb1958bc9a318b4938ba9131f00c29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
------------------------------------------------------------------------------
--                                                                          --
--                          GNAT RUN-TIME COMPONENTS                        --
--                                                                          --
--                  S Y S T E M . S C A L A R _ V A L U E S                 --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--          Copyright (C) 2003-2007, Free Software Foundation, Inc.         --
--                                                                          --
-- 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 2,  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.  See the GNU General Public License --
-- for  more details.  You should have  received  a copy of the GNU General --
-- Public License  distributed with GNAT;  see file COPYING.  If not, write --
-- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
-- Boston, MA 02110-1301, USA.                                              --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable  to  be --
-- covered  by the  GNU  General  Public  License.  This exception does not --
-- however invalidate  any other reasons why  the executable file  might be --
-- covered by the  GNU Public License.                                      --
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Unchecked_Conversion;

package body System.Scalar_Values is

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize (Mode1 : Character; Mode2 : Character) is
      C1 : Character := Mode1;
      C2 : Character := Mode2;

      procedure Get_Env_Value_Ptr (Name, Length, Ptr : Address);
      pragma Import (C, Get_Env_Value_Ptr, "__gnat_getenv");

      subtype String2 is String (1 .. 2);
      type String2_Ptr is access all String2;

      Env_Value_Ptr    : aliased String2_Ptr;
      Env_Value_Length : aliased Integer;

      EV_Val : aliased constant String :=
                 "GNAT_INIT_SCALARS" & ASCII.NUL;

      B : Byte1;

      EFloat : constant Boolean := Long_Long_Float'Size > Long_Float'Size;
      --  Set True if we are on an x86 with 96-bit floats for extended

      AFloat : constant Boolean :=
                 Long_Float'Size = 48 and Long_Long_Float'Size = 48;
      --  Set True if we are on an AAMP with 48-bit extended floating point

      type ByteLF is array (0 .. 7 - 2 * Boolean'Pos (AFloat)) of Byte1;

      for ByteLF'Component_Size use 8;

      --  Type used to hold Long_Float values on all targets and to initialize
      --  48-bit Long_Float values used on AAMP. On AAMP, this type is 6 bytes.
      --  On other targets the type is 8 bytes, and type Byte8 is used for
      --  values that are then converted to ByteLF.

      pragma Warnings (Off);
      function To_ByteLF is new Ada.Unchecked_Conversion (Byte8, ByteLF);
      pragma Warnings (On);

      type ByteLLF is
        array (0 .. 7 + 4 * Boolean'Pos (EFloat) - 2 * Boolean'Pos (AFloat))
          of Byte1;

      for ByteLLF'Component_Size use 8;

      --  Type used to initialize Long_Long_Float values used on x86 and
      --  any other target with the same 80-bit floating-point values that
      --  GCC always stores in 96-bits. Note that we are assuming Intel
      --  format little-endian addressing for this type. On non-Intel
      --  architectures, this is the same length as Byte8 and holds
      --  a Long_Float value.

      --  The following variables are used to initialize the float values
      --  by overlay. We can't assign directly to the float values, since
      --  we may be assigning signalling Nan's that will cause a trap if
      --  loaded into a floating-point register.

      IV_Isf : aliased Byte4;     -- Initialize short float
      IV_Ifl : aliased Byte4;     -- Initialize float
      IV_Ilf : aliased ByteLF;    -- Initialize long float
      IV_Ill : aliased ByteLLF;   -- Initialize long long float

      for IV_Isf'Address use IS_Isf'Address;
      for IV_Ifl'Address use IS_Ifl'Address;
      for IV_Ilf'Address use IS_Ilf'Address;
      for IV_Ill'Address use IS_Ill'Address;

      --  The following pragmas are used to suppress initialization

      pragma Import (Ada, IV_Isf);
      pragma Import (Ada, IV_Ifl);
      pragma Import (Ada, IV_Ilf);
      pragma Import (Ada, IV_Ill);

   begin
      --  Acquire environment variable value if necessary

      if C1 = 'E' and then C2 = 'V' then
         Get_Env_Value_Ptr
           (EV_Val'Address, Env_Value_Length'Address, Env_Value_Ptr'Address);

         --  Ignore if length is not 2

         if Env_Value_Length /= 2 then
            C1 := 'I';
            C2 := 'N';

         --  Length is 2, see if it is a valid value

         else
            --  Acquire two characters and fold to upper case

            C1 := Env_Value_Ptr (1);
            C2 := Env_Value_Ptr (2);

            if C1 in 'a' .. 'z' then
               C1 := Character'Val (Character'Pos (C1) - 32);
            end if;

            if C2 in 'a' .. 'z' then
               C2 := Character'Val (Character'Pos (C2) - 32);
            end if;

            --  IN/LO/HI are ok values

            if (C1 = 'I' and then C2 = 'N')
                  or else
               (C1 = 'L' and then C2 = 'O')
                  or else
               (C1 = 'H' and then C2 = 'I')
            then
               null;

            --  Try for valid hex digits

            elsif (C1 in '0' .. '9' or else C1 in 'A' .. 'Z')
                     or else
                  (C2 in '0' .. '9' or else C2 in 'A' .. 'Z')
            then
               null;

            --  Otherwise environment value is bad, ignore and use IN (invalid)

            else
               C1 := 'I';
               C2 := 'N';
            end if;
         end if;
      end if;

      --  IN (invalid value)

      if C1 = 'I' and then C2 = 'N' then
         IS_Is1 := 16#80#;
         IS_Is2 := 16#8000#;
         IS_Is4 := 16#8000_0000#;
         IS_Is8 := 16#8000_0000_0000_0000#;

         IS_Iu1 := 16#FF#;
         IS_Iu2 := 16#FFFF#;
         IS_Iu4 := 16#FFFF_FFFF#;
         IS_Iu8 := 16#FFFF_FFFF_FFFF_FFFF#;

         IS_Iz1 := 16#00#;
         IS_Iz2 := 16#0000#;
         IS_Iz4 := 16#0000_0000#;
         IS_Iz8 := 16#0000_0000_0000_0000#;

         if AFloat then
            IV_Isf := 16#FFFF_FF00#;
            IV_Ifl := 16#FFFF_FF00#;
            IV_Ilf := (0, 16#FF#, 16#FF#, 16#FF#, 16#FF#, 16#FF#);

         else
            IV_Isf := IS_Iu4;
            IV_Ifl := IS_Iu4;
            IV_Ilf := To_ByteLF (IS_Iu8);
         end if;

         if EFloat then
            IV_Ill := (0, 0, 0, 0, 0, 0, 0, 16#C0#, 16#FF#, 16#FF#, 0, 0);
         end if;

      --  LO (Low values)

      elsif C1 = 'L' and then C2 = 'O' then
         IS_Is1 := 16#80#;
         IS_Is2 := 16#8000#;
         IS_Is4 := 16#8000_0000#;
         IS_Is8 := 16#8000_0000_0000_0000#;

         IS_Iu1 := 16#00#;
         IS_Iu2 := 16#0000#;
         IS_Iu4 := 16#0000_0000#;
         IS_Iu8 := 16#0000_0000_0000_0000#;

         IS_Iz1 := 16#00#;
         IS_Iz2 := 16#0000#;
         IS_Iz4 := 16#0000_0000#;
         IS_Iz8 := 16#0000_0000_0000_0000#;

         if AFloat then
            IV_Isf := 16#0000_0001#;
            IV_Ifl := 16#0000_0001#;
            IV_Ilf := (1, 0, 0, 0, 0, 0);

         else
            IV_Isf := 16#FF80_0000#;
            IV_Ifl := 16#FF80_0000#;
            IV_Ilf := To_ByteLF (16#FFF0_0000_0000_0000#);
         end if;

         if EFloat then
            IV_Ill := (0, 0, 0, 0, 0, 0, 0, 16#80#, 16#FF#, 16#FF#, 0, 0);
         end if;

      --  HI (High values)

      elsif C1 = 'H' and then C2 = 'I' then
         IS_Is1 := 16#7F#;
         IS_Is2 := 16#7FFF#;
         IS_Is4 := 16#7FFF_FFFF#;
         IS_Is8 := 16#7FFF_FFFF_FFFF_FFFF#;

         IS_Iu1 := 16#FF#;
         IS_Iu2 := 16#FFFF#;
         IS_Iu4 := 16#FFFF_FFFF#;
         IS_Iu8 := 16#FFFF_FFFF_FFFF_FFFF#;

         IS_Iz1 := 16#FF#;
         IS_Iz2 := 16#FFFF#;
         IS_Iz4 := 16#FFFF_FFFF#;
         IS_Iz8 := 16#FFFF_FFFF_FFFF_FFFF#;

         if AFloat then
            IV_Isf := 16#7FFF_FFFF#;
            IV_Ifl := 16#7FFF_FFFF#;
            IV_Ilf := (16#FF#, 16#FF#, 16#FF#, 16#FF#, 16#FF#, 16#7F#);

         else
            IV_Isf := 16#7F80_0000#;
            IV_Ifl := 16#7F80_0000#;
            IV_Ilf := To_ByteLF (16#7FF0_0000_0000_0000#);
         end if;

         if EFloat then
            IV_Ill := (0, 0, 0, 0, 0, 0, 0, 16#80#, 16#FF#, 16#7F#, 0, 0);
         end if;

      --  -Shh (hex byte)

      else
         --  Convert the two hex digits (we know they are valid here)

         if C1 in '0' .. '9' then
            B := Character'Pos (C1) - Character'Pos ('0');
         else
            B := Character'Pos (C1) - (Character'Pos ('A') - 10);
         end if;

         if C2 in '0' .. '9' then
            B := B * 16 + Character'Pos (C2) - Character'Pos ('0');
         else
            B := B * 16 + Character'Pos (C2) - (Character'Pos ('A') - 10);
         end if;

         --  Initialize data values from the hex value

         IS_Is1 := B;
         IS_Is2 := 2**8  * Byte2 (IS_Is1) + Byte2 (IS_Is1);
         IS_Is4 := 2**16 * Byte4 (IS_Is2) + Byte4 (IS_Is2);
         IS_Is8 := 2**32 * Byte8 (IS_Is4) + Byte8 (IS_Is4);

         IS_Iu1 := IS_Is1;
         IS_Iu2 := IS_Is2;
         IS_Iu4 := IS_Is4;
         IS_Iu8 := IS_Is8;

         IS_Iz1 := IS_Is1;
         IS_Iz2 := IS_Is2;
         IS_Iz4 := IS_Is4;
         IS_Iz8 := IS_Is8;

         IV_Isf := IS_Is4;
         IV_Ifl := IS_Is4;

         if AFloat then
            IV_Ill := (B, B, B, B, B, B);
         else
            IV_Ilf := To_ByteLF (IS_Is8);
         end if;

         if EFloat then
            IV_Ill := (B, B, B, B, B, B, B, B, B, B, B, B);
         end if;
      end if;

      --  If no separate Long_Long_Float, then use Long_Float value as
      --  Long_Long_Float initial value.

      if not EFloat then
         declare
            pragma Warnings (Off);  -- why???
            function To_ByteLLF is
              new Ada.Unchecked_Conversion (ByteLF, ByteLLF);
            pragma Warnings (On);
         begin
            IV_Ill := To_ByteLLF (IV_Ilf);
         end;
      end if;
   end Initialize;

end System.Scalar_Values;