xspack.py is used to generate libgnat/s-pack* files.

gcc/ada/ChangeLog:

        * xspack.py: New
        * s-pack.ads.tmpl: New.
        * s-pack.adb.tmpl: New.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/s-pack.adb.tmpl | 254 ++++++++++++++++++++++++++++++++++++++++
 gcc/ada/s-pack.ads.tmpl |  79 +++++++++++++
 gcc/ada/xspack.py       |  82 +++++++++++++
 3 files changed, 415 insertions(+)
 create mode 100644 gcc/ada/s-pack.adb.tmpl
 create mode 100644 gcc/ada/s-pack.ads.tmpl
 create mode 100644 gcc/ada/xspack.py

diff --git a/gcc/ada/s-pack.adb.tmpl b/gcc/ada/s-pack.adb.tmpl
new file mode 100644
index 00000000000..69da423016a
--- /dev/null
+++ b/gcc/ada/s-pack.adb.tmpl
@@ -0,0 +1,254 @@
+------------------------------------------------------------------------------
+--                                                                          --
+--                         GNAT RUN-TIME COMPONENTS                         --
+--                                                                          --
+--                       S Y S T E M . P A C K _ @ @                        --
+--                                                                          --
+--                                 B o d y                                  --
+--                                                                          --
+--          Copyright (C) 1992-2024, 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 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.      --
+--                                                                          --
+------------------------------------------------------------------------------
+
+with System.Storage_Elements;
+with System.Unsigned_Types;
+
+package body System.Pack_@@ is
+
+   subtype Bit_Order is System.Bit_Order;
+   Reverse_Bit_Order : constant Bit_Order :=
+     Bit_Order'Val (1 - Bit_Order'Pos (System.Default_Bit_Order));
+
+   subtype Ofs is System.Storage_Elements.Storage_Offset;
+   subtype Uns is System.Unsigned_Types.Unsigned;
+   subtype N07 is System.Unsigned_Types.Unsigned range 0 .. 7;
+
+   use type System.Storage_Elements.Storage_Offset;
+   use type System.Unsigned_Types.Unsigned;
+
+   type Cluster is record
+      E0, E1, E2, E3, E4, E5, E6, E7 : Bits_@@;
+   end record;
+
+   for Cluster use record
+      E0 at 0 range 0 * Bits .. 0 * Bits + Bits - 1;
+      E1 at 0 range 1 * Bits .. 1 * Bits + Bits - 1;
+      E2 at 0 range 2 * Bits .. 2 * Bits + Bits - 1;
+      E3 at 0 range 3 * Bits .. 3 * Bits + Bits - 1;
+      E4 at 0 range 4 * Bits .. 4 * Bits + Bits - 1;
+      E5 at 0 range 5 * Bits .. 5 * Bits + Bits - 1;
+      E6 at 0 range 6 * Bits .. 6 * Bits + Bits - 1;
+      E7 at 0 range 7 * Bits .. 7 * Bits + Bits - 1;
+   end record;
+
+   for Cluster'Size use Bits * 8;
+
+   for Cluster'Alignment use Integer'Min (Standard'Maximum_Alignment,
+     1 +
+     1 * Boolean'Pos (Bits mod 2 = 0) +
+     2 * Boolean'Pos (Bits mod 4 = 0));
+   --  Use maximum possible alignment, given the bit field size, since this
+   --  will result in the most efficient code possible for the field.
+
+   type Cluster_Ref is access Cluster;
+
+   type Rev_Cluster is new Cluster
+     with Bit_Order            => Reverse_Bit_Order,
+          Scalar_Storage_Order => Reverse_Bit_Order;
+   type Rev_Cluster_Ref is access Rev_Cluster;
+
+@even
+   --  The following declarations are for the case where the address
+   --  passed to GetU_@@ or SetU_@@ is not guaranteed to be aligned.
+   --  These routines are used when the packed array is itself a
+   --  component of a packed record, and therefore may not be aligned.
+
+   type ClusterU is new Cluster;
+   for ClusterU'Alignment use 1;
+
+   type ClusterU_Ref is access ClusterU;
+
+   type Rev_ClusterU is new ClusterU
+     with Bit_Order            => Reverse_Bit_Order,
+          Scalar_Storage_Order => Reverse_Bit_Order;
+   type Rev_ClusterU_Ref is access Rev_ClusterU;
+
+@/even
+   ------------
+   -- Get_@@ --
+   ------------
+
+   function Get_@@
+     (Arr     : System.Address;
+      N       : Natural;
+      Rev_SSO : Boolean) return Bits_@@
+   is
+      A  : constant System.Address := Arr + Bits * Ofs (Uns (N) / 8);
+      C  : Cluster_Ref     with Address => A'Address, Import;
+      RC : Rev_Cluster_Ref with Address => A'Address, Import;
+   begin
+      return
+         (if Rev_SSO then
+            (case N07 (Uns (N) mod 8) is
+               when 0 => RC.E0,
+               when 1 => RC.E1,
+               when 2 => RC.E2,
+               when 3 => RC.E3,
+               when 4 => RC.E4,
+               when 5 => RC.E5,
+               when 6 => RC.E6,
+               when 7 => RC.E7)
+
+         else
+            (case N07 (Uns (N) mod 8) is
+               when 0 => C.E0,
+               when 1 => C.E1,
+               when 2 => C.E2,
+               when 3 => C.E3,
+               when 4 => C.E4,
+               when 5 => C.E5,
+               when 6 => C.E6,
+               when 7 => C.E7)
+         );
+   end Get_@@;
+
+@even
+   -------------
+   -- GetU_@@ --
+   -------------
+
+   function GetU_@@
+     (Arr     : System.Address;
+      N       : Natural;
+      Rev_SSO : Boolean) return Bits_@@
+   is
+      A  : constant System.Address := Arr + Bits * Ofs (Uns (N) / 8);
+      C  : ClusterU_Ref     with Address => A'Address, Import;
+      RC : Rev_ClusterU_Ref with Address => A'Address, Import;
+   begin
+      return
+         (if Rev_SSO then
+            (case N07 (Uns (N) mod 8) is
+               when 0 => RC.E0,
+               when 1 => RC.E1,
+               when 2 => RC.E2,
+               when 3 => RC.E3,
+               when 4 => RC.E4,
+               when 5 => RC.E5,
+               when 6 => RC.E6,
+               when 7 => RC.E7)
+
+         else
+            (case N07 (Uns (N) mod 8) is
+               when 0 => C.E0,
+               when 1 => C.E1,
+               when 2 => C.E2,
+               when 3 => C.E3,
+               when 4 => C.E4,
+               when 5 => C.E5,
+               when 6 => C.E6,
+               when 7 => C.E7)
+         );
+   end GetU_@@;
+
+@/even
+   ------------
+   -- Set_@@ --
+   ------------
+
+   procedure Set_@@
+     (Arr     : System.Address;
+      N       : Natural;
+      E       : Bits_@@;
+      Rev_SSO : Boolean)
+   is
+      A  : constant System.Address := Arr + Bits * Ofs (Uns (N) / 8);
+      C  : Cluster_Ref     with Address => A'Address, Import;
+      RC : Rev_Cluster_Ref with Address => A'Address, Import;
+   begin
+      if Rev_SSO then
+         case N07 (Uns (N) mod 8) is
+            when 0 => RC.E0 := E;
+            when 1 => RC.E1 := E;
+            when 2 => RC.E2 := E;
+            when 3 => RC.E3 := E;
+            when 4 => RC.E4 := E;
+            when 5 => RC.E5 := E;
+            when 6 => RC.E6 := E;
+            when 7 => RC.E7 := E;
+         end case;
+      else
+         case N07 (Uns (N) mod 8) is
+            when 0 => C.E0 := E;
+            when 1 => C.E1 := E;
+            when 2 => C.E2 := E;
+            when 3 => C.E3 := E;
+            when 4 => C.E4 := E;
+            when 5 => C.E5 := E;
+            when 6 => C.E6 := E;
+            when 7 => C.E7 := E;
+         end case;
+      end if;
+   end Set_@@;
+
+@even
+   -------------
+   -- SetU_@@ --
+   -------------
+
+   procedure SetU_@@
+     (Arr     : System.Address;
+      N       : Natural;
+      E       : Bits_@@;
+      Rev_SSO : Boolean)
+   is
+      A  : constant System.Address := Arr + Bits * Ofs (Uns (N) / 8);
+      C  : ClusterU_Ref     with Address => A'Address, Import;
+      RC : Rev_ClusterU_Ref with Address => A'Address, Import;
+   begin
+      if Rev_SSO then
+         case N07 (Uns (N) mod 8) is
+            when 0 => RC.E0 := E;
+            when 1 => RC.E1 := E;
+            when 2 => RC.E2 := E;
+            when 3 => RC.E3 := E;
+            when 4 => RC.E4 := E;
+            when 5 => RC.E5 := E;
+            when 6 => RC.E6 := E;
+            when 7 => RC.E7 := E;
+         end case;
+      else
+         case N07 (Uns (N) mod 8) is
+            when 0 => C.E0 := E;
+            when 1 => C.E1 := E;
+            when 2 => C.E2 := E;
+            when 3 => C.E3 := E;
+            when 4 => C.E4 := E;
+            when 5 => C.E5 := E;
+            when 6 => C.E6 := E;
+            when 7 => C.E7 := E;
+         end case;
+      end if;
+   end SetU_@@;
+
+@/even
+end System.Pack_@@;
diff --git a/gcc/ada/s-pack.ads.tmpl b/gcc/ada/s-pack.ads.tmpl
new file mode 100644
index 00000000000..f5f1784e478
--- /dev/null
+++ b/gcc/ada/s-pack.ads.tmpl
@@ -0,0 +1,79 @@
+------------------------------------------------------------------------------
+--                                                                          --
+--                         GNAT RUN-TIME COMPONENTS                         --
+--                                                                          --
+--                       S Y S T E M . P A C K _ @ @                        --
+--                                                                          --
+--                                 S p e c                                  --
+--                                                                          --
+--          Copyright (C) 1992-2024, 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 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.      --
+--                                                                          --
+------------------------------------------------------------------------------
+
+--  Handling of packed arrays with Component_Size = @
+
+package System.Pack_@@ is
+   pragma Preelaborate;
+
+   Bits : constant := @;
+
+   type Bits_@@ is mod 2 ** Bits;
+   for Bits_@@'Size use Bits;
+
+   --  In all subprograms below, Rev_SSO is set True if the array has the
+   --  non-default scalar storage order.
+
+   function Get_@@
+     (Arr     : System.Address;
+      N       : Natural;
+      Rev_SSO : Boolean) return Bits_@@ with Inline;
+   --  Arr is the address of the packed array, N is the zero-based
+   --  subscript. This element is extracted and returned.
+
+   procedure Set_@@
+     (Arr     : System.Address;
+      N       : Natural;
+      E       : Bits_@@;
+      Rev_SSO : Boolean) with Inline;
+   --  Arr is the address of the packed array, N is the zero-based
+   --  subscript. This element is set to the given value.
+
+@even
+   function GetU_@@
+     (Arr     : System.Address;
+      N       : Natural;
+      Rev_SSO : Boolean) return Bits_@@ with Inline;
+   --  Arr is the address of the packed array, N is the zero-based
+   --  subscript. This element is extracted and returned. This version
+   --  is used when Arr may represent an unaligned address.
+
+   procedure SetU_@@
+     (Arr     : System.Address;
+      N       : Natural;
+      E       : Bits_@@;
+      Rev_SSO : Boolean) with Inline;
+   --  Arr is the address of the packed array, N is the zero-based
+   --  subscript. This element is set to the given value. This version
+   --  is used when Arr may represent an unaligned address
+
+@/even
+end System.Pack_@@;
diff --git a/gcc/ada/xspack.py b/gcc/ada/xspack.py
new file mode 100644
index 00000000000..a3af62d1d3a
--- /dev/null
+++ b/gcc/ada/xspack.py
@@ -0,0 +1,82 @@
+#! /usr/bin/env python
+
+#                                                                          #
+#                         GNAT COMPILER COMPONENTS                         #
+#                                                                          #
+#                               X S P A C K                                #
+#                                                                          #
+#           Copyright (C) 2001-2024, 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 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.      #
+
+# xspack
+
+# Generate s-pack*.{ads,adb} from templates s-pack.ads.tmpl and s-pack.adb.tmpl
+
+tmpl_base = "s-pack.ad%c.tmpl"
+
+
+def read_template(part):
+    return open(tmpl_base % part).readlines()
+
+
+def output(pkg, bits, part):
+    global tmpl
+
+    bits_str_pad = "%02d" % bits
+    if bits > 99:
+        bits_str_dbl = "1 %d %d" % ((bits / 10) % 10, bits % 10)
+    else:
+        bits_str_dbl = "%d %d  " % (bits / 10, bits % 10)
+    bits_str = "%d" % bits
+
+    out = open(pkg % (bits, part), 'w')
+    skip = False
+
+    for line in tmpl[part]:
+        if line.find('@even') == 0 and bits % 2 == 1:
+            skip = True
+        if line.find('@/even') == 0:
+            skip = False
+        if line[0] != '@' and not skip:
+            line = line.replace('@@', bits_str_pad)
+            line = line.replace('@ @  ', bits_str_dbl)
+            line = line.replace('@', bits_str)
+            out.write(line)
+
+
+parts = ['s', 'b']
+tmpl = {}
+
+for part in parts:
+    tmpl[part] = read_template(part)
+
+for bits in range(1, 128):
+    if bits & (bits - 1) == 0:
+        # Power of two: no package generated
+        continue
+
+    if bits > 99:
+        pkg_base = "s-pack%3d.ad%c"
+    else:
+        pkg_base = "s-pack%02d.ad%c"
+
+    for part in parts:
+        output(pkg_base, bits, part)
-- 
2.43.0

Reply via email to