comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: pragma Pack vs. Convention C, portability issue?
Date: Wed, 9 Jan 2008 08:06:54 -0800 (PST)
Date: 2008-01-09T08:06:54-08:00	[thread overview]
Message-ID: <3f729fc9-708f-49a5-82a5-b2d82038a47c@s12g2000prg.googlegroups.com> (raw)
In-Reply-To: a244cc70-8735-468d-a0ad-b2b659b6d58e@v67g2000hse.googlegroups.com

On Jan 9, 12:40 am, okellogg <okell...@freenet.de> wrote:
> -- File: main.adb
> -- Can we portably rely on pragma Pack taking precedence
> -- over Convention C?
> with Text_IO;
>
> procedure Main is
>
>    type C_Represented_Enum is (Zero, One, Two, Three);
>    pragma Convention (C, C_Represented_Enum);
>    -- This would be 32 bits on a 32 bit architecture
>
>    type Perhaps_Packed is array (1 .. 4) of C_Represented_Enum;
>    pragma Pack (Perhaps_Packed);
>    -- This could be either 8 bits if the compiler lets pragma Pack
>    -- take precedence over Convention C, or 4 * 32 = 128 bits
>    -- otherwise.
>
> begin
>    Text_IO.Put_Line
>      ("Perhaps_Packed'Size is" &
>       Natural'Image (Perhaps_Packed'Size));
> end Main;

In general, you can't rely on the Pack pragma to be portable at all;
implementations are free to ignore it if they choose, or to choose
whatever representation they think is best, without rejecting your
program.  In your example, it's possible for Perhaps_Pack'Size to be
32 if the compiler decides to make each element 8 bits.

The Implementation Advice for the Pack pragma says that "the
implementation should try to minimize storage allocated to objects of
the type, possibly at the expense of speed of accessing components,
subject to reasonable complexity in addressing calculations".  Of
course, this is just "advice", and an Ada implementation doesn't need
to follow it.  If it does follow the Advice, then I believe the
compiler should normally make each component two bits, but the Advice
is "flexible" enough that the compiler could make the components 8
bits or 16 bits or something else if it chooses.

If you really need the components to be two bits each, rather than
letting the compiler use its "judgment" between minimizing storage
size and reasonable speed, you're better off using a Component_Size
clause:

   for Perhaps_Packed'Component_Size use 2;

Assuming that the "word size" is divisible by 2 (and I haven't seen
any computers with a 19-bit word size for quite some time now :), then
the implementation should implement this with no gaps between the 2-
bit components, which means that the array would be 8 bits.  If for
some reason it doesn't support this, it will reject the program.
Note, though, that 13.3(72) says that an implementation doesn't need
to support Component_Sizes that are less than the size of the
component subtype.

In any case, to answer a question I think you're asking:

The Component_Size of an array *may* be less than the Size of the
component subtypes.  Thus, if your Convention pragma makes
C_Represented_Enum'Size equal to 32, this does not *prevent* the
compiler from making the Component_Size of the array type 2 (whether
with a Component_Size clause or a Pack pragma).  Convention does not
take "precedence" over Pack (or a Component_Size clause), the way you
asked it.  Whether the compiler actually makes the component size 2 or
not is implementation-dependent, but I think most compilers would.
They're definitely allowed to.

Hope this helps,

                                -- Adam







  reply	other threads:[~2008-01-09 16:06 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-09  8:40 pragma Pack vs. Convention C, portability issue? okellogg
2008-01-09 16:06 ` Adam Beneschan [this message]
2008-01-09 22:12   ` Robert A Duff
2008-01-11  4:15     ` Randy Brukardt
2008-01-11 19:17       ` Randy Brukardt
2008-01-11  4:15     ` Randy Brukardt
2008-01-11  4:15     ` Randy Brukardt
2008-01-10  5:53   ` okellogg
2008-01-11  4:20 ` Randy Brukardt
2008-01-11 19:53   ` (see below)
2008-01-12  0:35     ` Adam Beneschan
2008-01-12  4:58     ` Randy Brukardt
2008-01-11 22:46   ` Robert A Duff
replies disabled

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