comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: Interfacing Ada to C
Date: 1997/05/30
Date: 1997-05-30T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023680003005972336090001@news.ni.net> (raw)
In-Reply-To: m3afle72cl.fsf@zaphod.enst.fr


In article <m3afle72cl.fsf@zaphod.enst.fr>, Samuel Tardieu <sam@ada.eu.org>
wrote:

>  type My_Natural is range 0 .. 15;
>  for My_Natural'Size use 4;
>
>  type R is record
>    A, B : My_Natural;
>  end record;
>  pragma Pack (R);
>  for R'Size use 8;
>
>If you do:
>
>  My_R : aliased constant R := (1, 2);
>
>then My_R will be represented in memory by a single byte containing
>00010010 (or 00100001, depending on the endianness).

I'm not sure that the statement "depending on the endianness" is correct. 
Isn't the compiler free to put A and B wherever it chooses?  You have no
guarantee that A comes later or earlier than B, no matter what the
endianess is.

All pragma Pack does is minimize the gaps between the components; it has
nothing to say about the location of the components within the enclosing
record (I think).

Which is why I wouldn't recommend it for interfacing to another language. 
A record representation clause should be used to explictly state the
location of each and every component, so it's absolutely unambiguous what
the layout is.  

for R use
   record
      A at 0 range 0 .. 3;
      B at 0 range 4 .. 7;
   end record;

for R'Size use 8;

for R'Bit_Order use Low_Bit_First;

This puts A on the right (least significant) end of the record, no matter
what the endianess is.

I don't use pragma Pack except on an array of Booleans, because the only
time that pragma Pack makes a guarantee about representation is on an array
whose component size is 1.

Sadly, there's no equivalent to Bit_Order for arrays.  What I would like to
be able to do is specify that the first bit (0) occupies the least
significant location of the storage element, ie

type Bit_Array is array (Natural range 0 .. 7) of Boolean;
pragma Pack (Bit_Array);

type Byte is range 0 .. 255;
for Byte'Size use 8;

function To_Byte is
   new Unchecked_Conversion (Bit_Array, Byte);

Bits : constant Bit_Array := (0 => True, others => False);

Bits_As_Byte : constant Byte := To_Byte (Bits);

On a little endian machine, Bits_As_Byte has the value 1.

On a big endian machine, Bits_As_Byte has the value 128.

It would be cool if there were an attribute analogous to Bit_Order for arrays:

for Bit_Array'Index_Order use Low_Order_First;

That way bit 0 would occupy the least significant bit.  Oh well, maybe Ada 0X.

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




  reply	other threads:[~1997-05-30  0:00 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-05-29  0:00 Interfacing Ada to C Rune Wemberg
1997-05-29  0:00 ` Samuel Tardieu
1997-05-30  0:00   ` Matthew Heaney [this message]
1997-05-31  0:00     ` Robert A Duff
1997-06-07  0:00       ` Robert Dewar
1997-05-30  0:00   ` Robert A Duff
1997-05-29  0:00 ` Robert Dewar
1997-05-30  0:00 ` Robert A Duff
  -- strict thread matches above, loose matches on Subject: below --
1999-06-14  0:00 Drew
1999-06-14  0:00 Drew
1999-06-14  0:00 ` Steve Quinlan
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox