This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Ada] Endianness of Ada.Sequential_IO length information


For unconstrained types, Ada.Sequential_IO needs to encode length information
prior to actual object representation. This length information is now stored
with the same endianness as the object itself, which means that the same
representation is now generated, independent of platform endianness, if the
Scalar_Storage_Order aspect is specified for the actual type.

The following unit test must produce the indicated output:
$ gnatmake -q endian_io
$ endian_io
out_le: 8 0 0 0 0 0 0 0 123 0 0 0 200 1 0 0
out_be: 0 0 0 0 0 0 0 8 0 0 0 123 0 0 1 200

with System; use System;
with Ada.Text_IO; use Ada.Text_IO;

with Ada.Exceptions;
with Ada.Sequential_IO;
with Ada.Streams.Stream_IO;

procedure Endian_IO is

   package Int_IO is new Ada.Sequential_IO (Integer);
   pragma Unreferenced (Int_IO);
   --  Check that Sequential_IO can be instantiated for a non-composite type

   type Arr is array (Integer range <>) of Integer;
   package Arr_IO is new Ada.Sequential_IO (Arr);
   F : Arr_IO.File_Type;

   type Arr_BE is new Arr
      with Scalar_Storage_Order => High_Order_First;
   package Arr_BE_IO is new Ada.Sequential_IO (Arr_BE);
   F_BE : Arr_BE_IO.File_Type;

   type Arr_LE is new Arr
      with Scalar_Storage_Order => Low_Order_First;
   package Arr_LE_IO is new Ada.Sequential_IO (Arr_LE);
   F_LE : Arr_LE_IO.File_Type;

   Val    : constant Arr := (1 => 123, 2 => 456);
   Val_BE : constant Arr_BE := Arr_BE (Val);
   Val_LE : constant Arr_LE := Arr_LE (Val);

   Rval    : Arr (1 .. 2);

   use Arr_IO, Arr_BE_IO, Arr_LE_IO;

   DBO : System.Bit_Order renames System.Default_Bit_Order;

   procedure Dump (FN : String) is
      use Ada.Streams, Ada.Streams.Stream_IO;

      F : Ada.Streams.Stream_IO.File_Type;
      SEA : Stream_Element_Array (1 .. 32);
      Last : Stream_Element_Offset;
   begin
      Open (F, In_File, FN);
      Read (F, SEA, Last);
      Put (FN & ":");
      for J in SEA'First .. Last loop
         Put (SEA (J)'Img);
      end loop;
      New_Line;
      Close (F);
   end Dump;

begin
   Create (F, Out_File, "out");
   Write (F, Val);
   Close (F);

   Create (F_BE, Out_File, "out_be");
   Write (F_BE, Val_BE);
   Close (F_BE);

   Create (F_LE, Out_File, "out_le");
   Write (F_LE, Val_LE);
   Close (F_LE);

   Open (F, In_File, (if DBO = High_Order_First
                        then "out_be"
                        else "out_le"));
   begin
      Read (F, Rval);
   exception
      when E : others =>
         Rval := (others => 666);
         Put_Line ("exception raised: "
           & Ada.Exceptions.Exception_Information (E));
   end;
   Close (F);

   if Rval /= Val then
      Put_Line ("FAIL re-reading " & DBO'Img);
   end if;

   Dump ("out_le");
   Dump ("out_be");
end Endian_IO;

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

2013-10-10  Thomas Quinot  <quinot@adacore.com>

	* sem_attr.adb (Analyse_Attribute, case
	Attribute_Scalar_Storage_Order): a 'Scalar_Storage_Order attribute
	reference for a generic type is permitted in GNAT runtime mode.
	* a-sequio.adb (Read, Write): Use the endianness of the actual
	type to encode length information written to the file.

Attachment: difs
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]