|
From: okellogg on 9 Jan 2008 03:40 -- 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;
From: Adam Beneschan on 9 Jan 2008 11:06 On Jan 9, 12:40 am, okellogg <okell...(a)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
From: Robert A Duff on 9 Jan 2008 17:12 Adam Beneschan <adam(a)irvine.com> writes: > On Jan 9, 12:40 am, okellogg <okell...(a)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
From: okellogg on 10 Jan 2008 00:53 On Jan 9, 5:06 pm, Adam Beneschan <a...(a)irvine.com> wrote: > > for Perhaps_Packed'Component_Size use 2; > Yes, this is what I've been overlooking. Many thanks Oliver
From: Randy Brukardt on 10 Jan 2008 23:15
"Robert A Duff" <bobduff(a)shell01.TheWorld.com> wrote in message news:wcctzlmk5rg.fsf(a)shell01.TheWorld.com... .... > Are there any Ada implementations that don't support the SP annex? Yes. But we do support most of the representation IA. (But *not* the packing IA). Randy. |