annotate gcc/ada/libgnat/g-bytswa.ads @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT RUN-TIME COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- G N A T . B Y T E _ S W A P P I N G --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- S p e c --
kono
parents:
diff changeset
8 -- --
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
9 -- Copyright (C) 2006-2018, AdaCore --
111
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
29 -- --
kono
parents:
diff changeset
30 ------------------------------------------------------------------------------
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 -- Simple routines for swapping the bytes of 16-, 32-, and 64-bit objects
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 -- The generic functions should be instantiated with types that are of a size
kono
parents:
diff changeset
35 -- in bytes corresponding to the name of the generic. For example, a 2-byte
kono
parents:
diff changeset
36 -- integer type would be compatible with Swapped2, 4-byte integer with
kono
parents:
diff changeset
37 -- Swapped4, and so on. Failure to do so will result in a warning when
kono
parents:
diff changeset
38 -- compiling the instantiation; this warning should be heeded. Ignoring this
kono
parents:
diff changeset
39 -- warning can result in unexpected results.
kono
parents:
diff changeset
40
kono
parents:
diff changeset
41 -- An example of proper usage follows:
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 -- declare
kono
parents:
diff changeset
44 -- type Short_Integer is range -32768 .. 32767;
kono
parents:
diff changeset
45 -- for Short_Integer'Size use 16; -- for confirmation
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 -- X : Short_Integer := 16#7FFF#;
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 -- function Swapped is new Byte_Swapping.Swapped2 (Short_Integer);
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 -- begin
kono
parents:
diff changeset
52 -- Put_Line (X'Img);
kono
parents:
diff changeset
53 -- X := Swapped (X);
kono
parents:
diff changeset
54 -- Put_Line (X'Img);
kono
parents:
diff changeset
55 -- end;
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 -- Note that the generic actual types need not be scalars, but must be
kono
parents:
diff changeset
58 -- 'definite' types. They can, for example, be constrained subtypes of
kono
parents:
diff changeset
59 -- unconstrained array types as long as the size is correct. For instance,
kono
parents:
diff changeset
60 -- a subtype of String with length of 4 would be compatible with the
kono
parents:
diff changeset
61 -- Swapped4 generic:
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 -- declare
kono
parents:
diff changeset
64 -- subtype String4 is String (1 .. 4);
kono
parents:
diff changeset
65 -- function Swapped is new Byte_Swapping.Swapped4 (String4);
kono
parents:
diff changeset
66 -- S : String4 := "ABCD";
kono
parents:
diff changeset
67 -- for S'Alignment use 4;
kono
parents:
diff changeset
68 -- begin
kono
parents:
diff changeset
69 -- Put_Line (S);
kono
parents:
diff changeset
70 -- S := Swapped (S);
kono
parents:
diff changeset
71 -- Put_Line (S);
kono
parents:
diff changeset
72 -- end;
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 -- Similarly, a constrained array type is also acceptable:
kono
parents:
diff changeset
75
kono
parents:
diff changeset
76 -- declare
kono
parents:
diff changeset
77 -- type Mask is array (0 .. 15) of Boolean;
kono
parents:
diff changeset
78 -- for Mask'Alignment use 2;
kono
parents:
diff changeset
79 -- for Mask'Component_Size use Boolean'Size;
kono
parents:
diff changeset
80 -- X : Mask := (0 .. 7 => True, others => False);
kono
parents:
diff changeset
81 -- function Swapped is new Byte_Swapping.Swapped2 (Mask);
kono
parents:
diff changeset
82 -- begin
kono
parents:
diff changeset
83 -- ...
kono
parents:
diff changeset
84 -- X := Swapped (X);
kono
parents:
diff changeset
85 -- ...
kono
parents:
diff changeset
86 -- end;
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 -- A properly-sized record type will also be acceptable, and so forth
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 -- However, as described, a size mismatch must be avoided. In the following we
kono
parents:
diff changeset
91 -- instantiate one of the generics with a type that is too large. The result
kono
parents:
diff changeset
92 -- of the function call is undefined, such that assignment to an object can
kono
parents:
diff changeset
93 -- result in garbage values.
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 -- Wrong: declare
kono
parents:
diff changeset
96 -- subtype String16 is String (1 .. 16);
kono
parents:
diff changeset
97
kono
parents:
diff changeset
98 -- function Swapped is new Byte_Swapping.Swapped8 (String16);
kono
parents:
diff changeset
99 -- -- Instantiation generates a compiler warning about
kono
parents:
diff changeset
100 -- -- mismatched sizes
kono
parents:
diff changeset
101
kono
parents:
diff changeset
102 -- S : String16;
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 -- begin
kono
parents:
diff changeset
105 -- S := "ABCDEFGHDEADBEEF";
kono
parents:
diff changeset
106 --
kono
parents:
diff changeset
107 -- Put_Line (S);
kono
parents:
diff changeset
108 --
kono
parents:
diff changeset
109 -- -- the following assignment results in garbage in S after the
kono
parents:
diff changeset
110 -- -- first 8 bytes
kono
parents:
diff changeset
111 --
kono
parents:
diff changeset
112 -- S := Swapped (S);
kono
parents:
diff changeset
113 --
kono
parents:
diff changeset
114 -- Put_Line (S);
kono
parents:
diff changeset
115 -- end Wrong;
kono
parents:
diff changeset
116
kono
parents:
diff changeset
117 -- When the size of the type is larger than 8 bytes, the use of the non-
kono
parents:
diff changeset
118 -- generic procedures is an alternative because no function result is
kono
parents:
diff changeset
119 -- involved; manipulation of the object is direct.
kono
parents:
diff changeset
120
kono
parents:
diff changeset
121 -- The procedures are passed the address of an object to manipulate. They will
kono
parents:
diff changeset
122 -- swap the first N bytes of that object corresponding to the name of the
kono
parents:
diff changeset
123 -- procedure. For example:
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 -- declare
kono
parents:
diff changeset
126 -- S2 : String := "AB";
kono
parents:
diff changeset
127 -- for S2'Alignment use 2;
kono
parents:
diff changeset
128 -- S4 : String := "ABCD";
kono
parents:
diff changeset
129 -- for S4'Alignment use 4;
kono
parents:
diff changeset
130 -- S8 : String := "ABCDEFGH";
kono
parents:
diff changeset
131 -- for S8'Alignment use 8;
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 -- begin
kono
parents:
diff changeset
134 -- Swap2 (S2'Address);
kono
parents:
diff changeset
135 -- Put_Line (S2);
kono
parents:
diff changeset
136
kono
parents:
diff changeset
137 -- Swap4 (S4'Address);
kono
parents:
diff changeset
138 -- Put_Line (S4);
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 -- Swap8 (S8'Address);
kono
parents:
diff changeset
141 -- Put_Line (S8);
kono
parents:
diff changeset
142 -- end;
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 -- If an object of a type larger than N is passed, the remaining bytes of the
kono
parents:
diff changeset
145 -- object are undisturbed. For example:
kono
parents:
diff changeset
146
kono
parents:
diff changeset
147 -- declare
kono
parents:
diff changeset
148 -- subtype String16 is String (1 .. 16);
kono
parents:
diff changeset
149
kono
parents:
diff changeset
150 -- S : String16;
kono
parents:
diff changeset
151 -- for S'Alignment use 8;
kono
parents:
diff changeset
152
kono
parents:
diff changeset
153 -- begin
kono
parents:
diff changeset
154 -- S := "ABCDEFGHDEADBEEF";
kono
parents:
diff changeset
155 -- Put_Line (S);
kono
parents:
diff changeset
156 -- Swap8 (S'Address);
kono
parents:
diff changeset
157 -- Put_Line (S);
kono
parents:
diff changeset
158 -- end;
kono
parents:
diff changeset
159
kono
parents:
diff changeset
160 with System;
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 package GNAT.Byte_Swapping is
kono
parents:
diff changeset
163 pragma Pure;
kono
parents:
diff changeset
164
kono
parents:
diff changeset
165 -- NB: all the routines in this package treat the application objects as
kono
parents:
diff changeset
166 -- unsigned (modular) types of a size in bytes corresponding to the routine
kono
parents:
diff changeset
167 -- name. For example, the generic function Swapped2 manipulates the object
kono
parents:
diff changeset
168 -- passed to the formal parameter Input as a value of an unsigned type that
kono
parents:
diff changeset
169 -- is 2 bytes long. Therefore clients are responsible for the compatibility
kono
parents:
diff changeset
170 -- of application types manipulated by these routines and these modular
kono
parents:
diff changeset
171 -- types, in terms of both size and alignment. This requirement applies to
kono
parents:
diff changeset
172 -- the generic actual type passed to the generic formal type Item in the
kono
parents:
diff changeset
173 -- generic functions, as well as to the type of the object implicitly
kono
parents:
diff changeset
174 -- designated by the address passed to the non-generic procedures. Use of
kono
parents:
diff changeset
175 -- incompatible types can result in implementation- defined effects.
kono
parents:
diff changeset
176
kono
parents:
diff changeset
177 generic
kono
parents:
diff changeset
178 type Item is limited private;
kono
parents:
diff changeset
179 function Swapped2 (Input : Item) return Item;
kono
parents:
diff changeset
180 -- Return the 2-byte value of Input with the bytes swapped
kono
parents:
diff changeset
181
kono
parents:
diff changeset
182 generic
kono
parents:
diff changeset
183 type Item is limited private;
kono
parents:
diff changeset
184 function Swapped4 (Input : Item) return Item;
kono
parents:
diff changeset
185 -- Return the 4-byte value of Input with the bytes swapped
kono
parents:
diff changeset
186
kono
parents:
diff changeset
187 generic
kono
parents:
diff changeset
188 type Item is limited private;
kono
parents:
diff changeset
189 function Swapped8 (Input : Item) return Item;
kono
parents:
diff changeset
190 -- Return the 8-byte value of Input with the bytes swapped
kono
parents:
diff changeset
191
kono
parents:
diff changeset
192 procedure Swap2 (Location : System.Address);
kono
parents:
diff changeset
193 -- Swap the first 2 bytes of the object starting at the address specified
kono
parents:
diff changeset
194 -- by Location.
kono
parents:
diff changeset
195
kono
parents:
diff changeset
196 procedure Swap4 (Location : System.Address);
kono
parents:
diff changeset
197 -- Swap the first 4 bytes of the object starting at the address specified
kono
parents:
diff changeset
198 -- by Location.
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 procedure Swap8 (Location : System.Address);
kono
parents:
diff changeset
201 -- Swap the first 8 bytes of the object starting at the address specified
kono
parents:
diff changeset
202 -- by Location.
kono
parents:
diff changeset
203
kono
parents:
diff changeset
204 pragma Inline (Swap2, Swap4, Swap8, Swapped2, Swapped4, Swapped8);
kono
parents:
diff changeset
205
kono
parents:
diff changeset
206 end GNAT.Byte_Swapping;