Patchwork [Ada] Move GNAT.Byte_Swapping to System

login
register
mail settings
Submitter Arnaud Charlet
Date July 12, 2012, 10:30 a.m.
Message ID <20120712103038.GA4797@adacore.com>
Download mbox | patch
Permalink /patch/170631/
State New
Headers show

Comments

Arnaud Charlet - July 12, 2012, 10:30 a.m.
This change moves the GNAT runtime package GNAT.Byte_Swapping under System
(with a renaming under GNAT for compatibility) so that the unit can be
used from expanded code at a later point.

No visible behaviour change (simple code reorganization), no test.

Tested on x86_64-pc-linux-gnu, committed on trunk

2012-07-12  Thomas Quinot  <quinot@adacore.com>

	* g-bytswa.adb, g-bytswa.ads, s-bytswa.adb, s-bytswa.ads, Makefile.rtl:
	Move GNAT.Byte_Swapping to System (with a renaming under GNAT)
	so that it is usable in expanded code.

Patch

Index: g-bytswa.adb
===================================================================
--- g-bytswa.adb	(revision 189431)
+++ g-bytswa.adb	(working copy)
@@ -2,11 +2,11 @@ 
 --                                                                          --
 --                         GNAT RUN-TIME COMPONENTS                         --
 --                                                                          --
---                    G N A T . B Y T E _ S W A P P I N G                   --
+--                     G N A T . B Y T E _ S W A P P I N G                  --
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---                     Copyright (C) 2006-2012, AdaCore                     --
+--                     Copyright (C) 1995-2012, 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- --
@@ -29,98 +29,8 @@ 
 --                                                                          --
 ------------------------------------------------------------------------------
 
---  This is a general implementation that uses GCC intrinsics to take
---  advantage of any machine-specific instructions.
+--  This package does not require a body, since it is a package renaming. We
+--  provide a dummy file containing a No_Body pragma so that previous versions
+--  of the body (which did exist) will not interfere.
 
-with Ada.Unchecked_Conversion; use Ada;
-
-package body GNAT.Byte_Swapping is
-
-   type U16 is mod 2**16;
-   type U32 is mod 2**32;
-   type U64 is mod 2**64;
-
-   function Bswap_16 (X : U16) return U16 is (X / 256 or X * 256);
-   --  The above is an idiom recognized by GCC
-
-   function Bswap_32 (X : U32) return U32;
-   pragma Import (Intrinsic, Bswap_32, "__builtin_bswap32");
-
-   function Bswap_64 (X : U64) return U64;
-   pragma Import (Intrinsic, Bswap_64, "__builtin_bswap64");
-
-   --------------
-   -- Swapped2 --
-   --------------
-
-   function Swapped2 (Input : Item) return Item is
-      function As_U16 is new Unchecked_Conversion (Item, U16);
-      function As_Item is new Unchecked_Conversion (U16, Item);
-
-      function Bswap_16 (X : U16) return U16 is (X / 256 or X * 256);
-      --  ??? Need to have function local here to allow inlining
-      pragma Compile_Time_Error (Item'Max_Size_In_Storage_Elements /= 2,
-        "storage size must be 2 bytes");
-   begin
-      return As_Item (Bswap_16 (As_U16 (Input)));
-   end Swapped2;
-
-   --------------
-   -- Swapped4 --
-   --------------
-
-   function Swapped4 (Input : Item) return Item is
-      function As_U32 is new Unchecked_Conversion (Item, U32);
-      function As_Item is new Unchecked_Conversion (U32, Item);
-      pragma Compile_Time_Error (Item'Max_Size_In_Storage_Elements /= 4,
-        "storage size must be 4 bytes");
-   begin
-      return As_Item (Bswap_32 (As_U32 (Input)));
-   end Swapped4;
-
-   --------------
-   -- Swapped8 --
-   --------------
-
-   function Swapped8 (Input : Item) return Item is
-      function As_U64 is new Unchecked_Conversion (Item, U64);
-      function As_Item is new Unchecked_Conversion (U64, Item);
-      pragma Compile_Time_Error (Item'Max_Size_In_Storage_Elements /= 8,
-        "storage size must be 8 bytes");
-   begin
-      return As_Item (Bswap_64 (As_U64 (Input)));
-   end Swapped8;
-
-   -----------
-   -- Swap2 --
-   -----------
-
-   procedure Swap2 (Location : System.Address) is
-      X : U16;
-      for X'Address use Location;
-   begin
-      X := Bswap_16 (X);
-   end Swap2;
-
-   -----------
-   -- Swap4 --
-   -----------
-
-   procedure Swap4 (Location : System.Address) is
-      X : U32;
-      for X'Address use Location;
-   begin
-      X := Bswap_32 (X);
-   end Swap4;
-
-   -----------
-   -- Swap8 --
-   -----------
-
-   procedure Swap8 (Location : System.Address) is
-      X : U64;
-      for X'Address use Location;
-   begin
-      X := Bswap_64 (X);
-   end Swap8;
-end GNAT.Byte_Swapping;
+pragma No_Body;
Index: g-bytswa.ads
===================================================================
--- g-bytswa.ads	(revision 189431)
+++ g-bytswa.ads	(working copy)
@@ -2,11 +2,11 @@ 
 --                                                                          --
 --                         GNAT RUN-TIME COMPONENTS                         --
 --                                                                          --
---                    G N A T . B Y T E _ S W A P P I N G                   --
+--                     G N A T . B Y T E _ S W A P P I N G                  --
 --                                                                          --
 --                                 S p e c                                  --
 --                                                                          --
---                     Copyright (C) 2006-2011, AdaCore                     --
+--                     Copyright (C) 2006-2012, 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- --
@@ -31,176 +31,8 @@ 
 
 --  Simple routines for swapping the bytes of 16-, 32-, and 64-bit objects
 
---  The generic functions should be instantiated with types that are of a size
---  in bytes corresponding to the name of the generic. For example, a 2-byte
---  integer type would be compatible with Swapped2, 4-byte integer with
---  Swapped4, and so on. Failure to do so will result in a warning when
---  compiling the instantiation; this warning should be heeded. Ignoring this
---  warning can result in unexpected results.
+--  See file s-bytswa.ads for full documentation of the interface
 
---  An example of proper usage follows:
+with System.Byte_Swapping;
 
---     declare
---        type Short_Integer is range -32768 .. 32767;
---        for Short_Integer'Size use 16; -- for confirmation
-
---        X : Short_Integer := 16#7FFF#;
-
---        function Swapped is new Byte_Swapping.Swapped2 (Short_Integer);
-
---     begin
---        Put_Line (X'Img);
---        X := Swapped (X);
---        Put_Line (X'Img);
---     end;
-
---  Note that the generic actual types need not be scalars, but must be
---  'definite' types. They can, for example, be constrained subtypes of
---  unconstrained array types as long as the size is correct. For instance,
---  a subtype of String with length of 4 would be compatible with the
---  Swapped4 generic:
-
---     declare
---        subtype String4 is String (1 .. 4);
---        function Swapped is new Byte_Swapping.Swapped4 (String4);
---        S : String4 := "ABCD";
---        for S'Alignment use 4;
---     begin
---        Put_Line (S);
---        S := Swapped (S);
---        Put_Line (S);
---     end;
-
---  Similarly, a constrained array type is also acceptable:
-
---     declare
---        type Mask is array (0 .. 15) of Boolean;
---        for Mask'Alignment use 2;
---        for Mask'Component_Size use Boolean'Size;
---        X : Mask := (0 .. 7 => True, others => False);
---        function Swapped is new Byte_Swapping.Swapped2 (Mask);
---     begin
---        ...
---        X := Swapped (X);
---        ...
---     end;
-
---  A properly-sized record type will also be acceptable, and so forth
-
---  However, as described, a size mismatch must be avoided. In the following we
---  instantiate one of the generics with a type that is too large. The result
---  of the function call is undefined, such that assignment to an object can
---  result in garbage values.
-
---     Wrong: declare
---        subtype String16 is String (1 .. 16);
-
---        function Swapped is new Byte_Swapping.Swapped8 (String16);
---        --  Instantiation generates a compiler warning about
---        --  mismatched sizes
-
---        S : String16;
-
---     begin
---        S := "ABCDEFGHDEADBEEF";
---
---        Put_Line (S);
---
---        --  the following assignment results in garbage in S after the
---        --  first 8 bytes
---
---        S := Swapped (S);
---
---        Put_Line (S);
---     end Wrong;
-
---  When the size of the type is larger than 8 bytes, the use of the non-
---  generic procedures is an alternative because no function result is
---  involved; manipulation of the object is direct.
-
---  The procedures are passed the address of an object to manipulate. They will
---  swap the first N bytes of that object corresponding to the name of the
---  procedure.  For example:
-
---     declare
---        S2 : String := "AB";
---        for S2'Alignment use 2;
---        S4 : String := "ABCD";
---        for S4'Alignment use 4;
---        S8 : String := "ABCDEFGH";
---        for S8'Alignment use 8;
-
---     begin
---        Swap2 (S2'Address);
---        Put_Line (S2);
-
---        Swap4 (S4'Address);
---        Put_Line (S4);
-
---        Swap8 (S8'Address);
---        Put_Line (S8);
---     end;
-
---  If an object of a type larger than N is passed, the remaining bytes of the
---  object are undisturbed. For example:
-
---     declare
---        subtype String16 is String (1 .. 16);
-
---        S : String16;
---        for S'Alignment use 8;
-
---     begin
---        S  := "ABCDEFGHDEADBEEF";
---        Put_Line (S);
---        Swap8 (S'Address);
---        Put_Line (S);
---     end;
-
-with System;
-
-package GNAT.Byte_Swapping is
-   pragma Pure;
-
-   --  NB: all the routines in this package treat the application objects as
-   --  unsigned (modular) types of a size in bytes corresponding to the routine
-   --  name. For example, the generic function Swapped2 manipulates the object
-   --  passed to the formal parameter Input as a value of an unsigned type that
-   --  is 2 bytes long. Therefore clients are responsible for the compatibility
-   --  of application types manipulated by these routines and these modular
-   --  types, in terms of both size and alignment. This requirement applies to
-   --  the generic actual type passed to the generic formal type Item in the
-   --  generic functions, as well as to the type of the object implicitly
-   --  designated by the address passed to the non-generic procedures. Use of
-   --  incompatible types can result in implementation- defined effects.
-
-   generic
-      type Item is limited private;
-   function Swapped2 (Input : Item) return Item;
-   --  Return the 2-byte value of Input with the bytes swapped
-
-   generic
-      type Item is limited private;
-   function Swapped4 (Input : Item) return Item;
-   --  Return the 4-byte value of Input with the bytes swapped
-
-   generic
-      type Item is limited private;
-   function Swapped8 (Input : Item) return Item;
-   --  Return the 8-byte value of Input with the bytes swapped
-
-   procedure Swap2 (Location : System.Address);
-   --  Swap the first 2 bytes of the object starting at the address specified
-   --  by Location.
-
-   procedure Swap4 (Location : System.Address);
-   --  Swap the first 4 bytes of the object starting at the address specified
-   --  by Location.
-
-   procedure Swap8 (Location : System.Address);
-   --  Swap the first 8 bytes of the object starting at the address specified
-   --  by Location.
-
-   pragma Inline (Swap2, Swap4, Swap8, Swapped2, Swapped4, Swapped8);
-
-end GNAT.Byte_Swapping;
+package GNAT.Byte_Swapping renames System.Byte_Swapping;
Index: s-bytswa.adb
===================================================================
--- s-bytswa.adb	(revision 0)
+++ s-bytswa.adb	(revision 0)
@@ -0,0 +1,127 @@ 
+------------------------------------------------------------------------------
+--                                                                          --
+--                         GNAT RUN-TIME COMPONENTS                         --
+--                                                                          --
+--                  S Y S T E M . B Y T E _ S W A P P I N G                 --
+--                                                                          --
+--                                 B o d y                                  --
+--                                                                          --
+--                     Copyright (C) 2006-2012, 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 is a general implementation that uses GCC intrinsics to take
+--  advantage of any machine-specific instructions.
+
+with Ada.Unchecked_Conversion; use Ada;
+
+package body System.Byte_Swapping is
+
+   type U16 is mod 2**16;
+   type U32 is mod 2**32;
+   type U64 is mod 2**64;
+
+   function Bswap_16 (X : U16) return U16;
+   pragma Import (Intrinsic, Bswap_16, "__builtin_bswap16");
+
+   function Bswap_32 (X : U32) return U32;
+   pragma Import (Intrinsic, Bswap_32, "__builtin_bswap32");
+
+   function Bswap_64 (X : U64) return U64;
+   pragma Import (Intrinsic, Bswap_64, "__builtin_bswap64");
+
+   --------------
+   -- Swapped2 --
+   --------------
+
+   function Swapped2 (Input : Item) return Item is
+      function As_U16 is new Unchecked_Conversion (Item, U16);
+      function As_Item is new Unchecked_Conversion (U16, Item);
+
+      function Bswap_16 (X : U16) return U16 is (X / 256 or X * 256);
+      --  ??? Need to have function local here to allow inlining
+      pragma Compile_Time_Error (Item'Max_Size_In_Storage_Elements /= 2,
+        "storage size must be 2 bytes");
+   begin
+      return As_Item (Bswap_16 (As_U16 (Input)));
+   end Swapped2;
+
+   --------------
+   -- Swapped4 --
+   --------------
+
+   function Swapped4 (Input : Item) return Item is
+      function As_U32 is new Unchecked_Conversion (Item, U32);
+      function As_Item is new Unchecked_Conversion (U32, Item);
+      pragma Compile_Time_Error (Item'Max_Size_In_Storage_Elements /= 4,
+        "storage size must be 4 bytes");
+   begin
+      return As_Item (Bswap_32 (As_U32 (Input)));
+   end Swapped4;
+
+   --------------
+   -- Swapped8 --
+   --------------
+
+   function Swapped8 (Input : Item) return Item is
+      function As_U64 is new Unchecked_Conversion (Item, U64);
+      function As_Item is new Unchecked_Conversion (U64, Item);
+      pragma Compile_Time_Error (Item'Max_Size_In_Storage_Elements /= 8,
+        "storage size must be 8 bytes");
+   begin
+      return As_Item (Bswap_64 (As_U64 (Input)));
+   end Swapped8;
+
+   -----------
+   -- Swap2 --
+   -----------
+
+   procedure Swap2 (Location : System.Address) is
+      X : U16;
+      for X'Address use Location;
+   begin
+      X := Bswap_16 (X);
+   end Swap2;
+
+   -----------
+   -- Swap4 --
+   -----------
+
+   procedure Swap4 (Location : System.Address) is
+      X : U32;
+      for X'Address use Location;
+   begin
+      X := Bswap_32 (X);
+   end Swap4;
+
+   -----------
+   -- Swap8 --
+   -----------
+
+   procedure Swap8 (Location : System.Address) is
+      X : U64;
+      for X'Address use Location;
+   begin
+      X := Bswap_64 (X);
+   end Swap8;
+
+end System.Byte_Swapping;
Index: s-bytswa.ads
===================================================================
--- s-bytswa.ads	(revision 0)
+++ s-bytswa.ads	(revision 0)
@@ -0,0 +1,206 @@ 
+------------------------------------------------------------------------------
+--                                                                          --
+--                         GNAT RUN-TIME COMPONENTS                         --
+--                                                                          --
+--                  S Y S T E M . B Y T E _ S W A P P I N G                 --
+--                                                                          --
+--                                 S p e c                                  --
+--                                                                          --
+--                     Copyright (C) 2006-2012, 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.      --
+--                                                                          --
+------------------------------------------------------------------------------
+
+--  Simple routines for swapping the bytes of 16-, 32-, and 64-bit objects
+
+--  The generic functions should be instantiated with types that are of a size
+--  in bytes corresponding to the name of the generic. For example, a 2-byte
+--  integer type would be compatible with Swapped2, 4-byte integer with
+--  Swapped4, and so on. Failure to do so will result in a warning when
+--  compiling the instantiation; this warning should be heeded. Ignoring this
+--  warning can result in unexpected results.
+
+--  An example of proper usage follows:
+
+--     declare
+--        type Short_Integer is range -32768 .. 32767;
+--        for Short_Integer'Size use 16; -- for confirmation
+
+--        X : Short_Integer := 16#7FFF#;
+
+--        function Swapped is new Byte_Swapping.Swapped2 (Short_Integer);
+
+--     begin
+--        Put_Line (X'Img);
+--        X := Swapped (X);
+--        Put_Line (X'Img);
+--     end;
+
+--  Note that the generic actual types need not be scalars, but must be
+--  'definite' types. They can, for example, be constrained subtypes of
+--  unconstrained array types as long as the size is correct. For instance,
+--  a subtype of String with length of 4 would be compatible with the
+--  Swapped4 generic:
+
+--     declare
+--        subtype String4 is String (1 .. 4);
+--        function Swapped is new Byte_Swapping.Swapped4 (String4);
+--        S : String4 := "ABCD";
+--        for S'Alignment use 4;
+--     begin
+--        Put_Line (S);
+--        S := Swapped (S);
+--        Put_Line (S);
+--     end;
+
+--  Similarly, a constrained array type is also acceptable:
+
+--     declare
+--        type Mask is array (0 .. 15) of Boolean;
+--        for Mask'Alignment use 2;
+--        for Mask'Component_Size use Boolean'Size;
+--        X : Mask := (0 .. 7 => True, others => False);
+--        function Swapped is new Byte_Swapping.Swapped2 (Mask);
+--     begin
+--        ...
+--        X := Swapped (X);
+--        ...
+--     end;
+
+--  A properly-sized record type will also be acceptable, and so forth
+
+--  However, as described, a size mismatch must be avoided. In the following we
+--  instantiate one of the generics with a type that is too large. The result
+--  of the function call is undefined, such that assignment to an object can
+--  result in garbage values.
+
+--     Wrong: declare
+--        subtype String16 is String (1 .. 16);
+
+--        function Swapped is new Byte_Swapping.Swapped8 (String16);
+--        --  Instantiation generates a compiler warning about
+--        --  mismatched sizes
+
+--        S : String16;
+
+--     begin
+--        S := "ABCDEFGHDEADBEEF";
+--
+--        Put_Line (S);
+--
+--        --  the following assignment results in garbage in S after the
+--        --  first 8 bytes
+--
+--        S := Swapped (S);
+--
+--        Put_Line (S);
+--     end Wrong;
+
+--  When the size of the type is larger than 8 bytes, the use of the non-
+--  generic procedures is an alternative because no function result is
+--  involved; manipulation of the object is direct.
+
+--  The procedures are passed the address of an object to manipulate. They will
+--  swap the first N bytes of that object corresponding to the name of the
+--  procedure.  For example:
+
+--     declare
+--        S2 : String := "AB";
+--        for S2'Alignment use 2;
+--        S4 : String := "ABCD";
+--        for S4'Alignment use 4;
+--        S8 : String := "ABCDEFGH";
+--        for S8'Alignment use 8;
+
+--     begin
+--        Swap2 (S2'Address);
+--        Put_Line (S2);
+
+--        Swap4 (S4'Address);
+--        Put_Line (S4);
+
+--        Swap8 (S8'Address);
+--        Put_Line (S8);
+--     end;
+
+--  If an object of a type larger than N is passed, the remaining bytes of the
+--  object are undisturbed. For example:
+
+--     declare
+--        subtype String16 is String (1 .. 16);
+
+--        S : String16;
+--        for S'Alignment use 8;
+
+--     begin
+--        S  := "ABCDEFGHDEADBEEF";
+--        Put_Line (S);
+--        Swap8 (S'Address);
+--        Put_Line (S);
+--     end;
+
+with System;
+
+package System.Byte_Swapping is
+   pragma Pure;
+
+   --  NB: all the routines in this package treat the application objects as
+   --  unsigned (modular) types of a size in bytes corresponding to the routine
+   --  name. For example, the generic function Swapped2 manipulates the object
+   --  passed to the formal parameter Input as a value of an unsigned type that
+   --  is 2 bytes long. Therefore clients are responsible for the compatibility
+   --  of application types manipulated by these routines and these modular
+   --  types, in terms of both size and alignment. This requirement applies to
+   --  the generic actual type passed to the generic formal type Item in the
+   --  generic functions, as well as to the type of the object implicitly
+   --  designated by the address passed to the non-generic procedures. Use of
+   --  incompatible types can result in implementation- defined effects.
+
+   generic
+      type Item is limited private;
+   function Swapped2 (Input : Item) return Item;
+   --  Return the 2-byte value of Input with the bytes swapped
+
+   generic
+      type Item is limited private;
+   function Swapped4 (Input : Item) return Item;
+   --  Return the 4-byte value of Input with the bytes swapped
+
+   generic
+      type Item is limited private;
+   function Swapped8 (Input : Item) return Item;
+   --  Return the 8-byte value of Input with the bytes swapped
+
+   procedure Swap2 (Location : System.Address);
+   --  Swap the first 2 bytes of the object starting at the address specified
+   --  by Location.
+
+   procedure Swap4 (Location : System.Address);
+   --  Swap the first 4 bytes of the object starting at the address specified
+   --  by Location.
+
+   procedure Swap8 (Location : System.Address);
+   --  Swap the first 8 bytes of the object starting at the address specified
+   --  by Location.
+
+   pragma Inline (Swap2, Swap4, Swap8, Swapped2, Swapped4, Swapped8);
+
+end System.Byte_Swapping;
Index: Makefile.rtl
===================================================================
--- Makefile.rtl	(revision 189431)
+++ Makefile.rtl	(working copy)
@@ -483,6 +483,7 @@ 
   s-auxdec$(objext) \
   s-bitops$(objext) \
   s-boarop$(objext) \
+  s-bytswa$(objext) \
   s-carsi8$(objext) \
   s-carun8$(objext) \
   s-casi16$(objext) \