From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,642c983bc89db880 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!news.glorb.com!newscon02.news.prodigy.net!prodigy.net!newsfeed-00.mathworks.com!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: pragma Pack vs. Convention C, portability issue? Date: Wed, 09 Jan 2008 17:12:35 -0500 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <3f729fc9-708f-49a5-82a5-b2d82038a47c@s12g2000prg.googlegroups.com> NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls6.std.com 1199916757 27474 192.74.137.71 (9 Jan 2008 22:12:37 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Wed, 9 Jan 2008 22:12:37 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.3 (irix) Cancel-Lock: sha1:A9lfdlNeJgLPkkO6o6ekPb2jIxk= Xref: g2news1.google.com comp.lang.ada:19302 Date: 2008-01-09T17:12:35-05:00 List-Id: Adam Beneschan writes: > On Jan 9, 12:40 am, okellogg 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