From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: pragma Pack vs. Convention C, portability issue?
Date: Wed, 09 Jan 2008 17:12:35 -0500
Date: 2008-01-09T17:12:35-05:00 [thread overview]
Message-ID: <wcctzlmk5rg.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: 3f729fc9-708f-49a5-82a5-b2d82038a47c@s12g2000prg.googlegroups.com
Adam Beneschan <adam@irvine.com> writes:
> 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.
That's true, but for an implementation that claims to support the
Systems Programming Annex, the compiler is required by C.2(2) to
implement tight packing in many circumstances. Without the
Convention(C) above, Perhaps_Packed'Size must be 8 bits.
Are there any Ada implementations that don't support the SP annex?
With the Convention(C), I'm not sure what the right answer is.
I think what you say here:
> 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.
is probably right in that case.
I don't see why it matters, though. If you pass X(1), where X is of
type Perhaps_Packed, to a C function, it will be passed by copy,
so if it's 2 bits, it will get expanded into a 32-bit register
or some such.
If Perhaps_Pack were also Convention(C), then I suppose that would
defeat the packing -- it has to represent the array in a way the C
implementation likes.
- Bob
next prev parent reply other threads:[~2008-01-09 22:12 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
2008-01-09 22:12 ` Robert A Duff [this message]
2008-01-11 4:15 ` Randy Brukardt
2008-01-11 4:15 ` Randy Brukardt
2008-01-11 4:15 ` Randy Brukardt
2008-01-11 19:17 ` 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