comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Gem 39 - compiler specific?
Date: Wed, 2 Sep 2009 17:12:20 -0700 (PDT)
Date: 2009-09-02T17:12:20-07:00	[thread overview]
Message-ID: <40f0d9bb-8d7f-46d1-9397-5b7570d1cd0b@13g2000prl.googlegroups.com> (raw)
In-Reply-To: 9e0bbbcd-260f-48ed-8043-d6280c633e85@h3g2000yqa.googlegroups.com

On Sep 2, 1:38 pm, Maciej Sobczak <see.my.homep...@gmail.com> wrote:
> Consider:
>
> http://www.adacore.com/2008/06/09/gem-39/
>
> The example code performs Unchecked_Conversion between addresses of
> two different array types.
> As far as I understand, there is no standard provision for arrays to
> occupy contiguous memory space (and not even for the alignment of
> their components) and such a conversion relies heavily on the
> assumption that arrays can be "overlaid" by plain address
> reinterpretation.
>
> Still, this technique is quite attractive. What is the chance (in
> practice) to hit the compiler that does not get it "right"?

There are a number of portability problems with this gem ("uncut
gem"?).  All are mentioned or hinted at in the comments section of the
page you reference.

(1) It uses 'Object_Size which is not defined by the Ada language.

(2) It uses Unchecked_Conversion to convert between System.Address and
an access type.  As mentioned both in the comments and in Randy's
post, this is sloppy and really ought to use
System.Access_To_Address_Conversions (or, better, use
Unchecked_Conversion on the entire array as Randy also suggested).

(3) In the definition of Buffer:

    type Buffer is array (1..32) of Interfaces.Unsigned_16;

there is no guarantee that there won't be gaps between elements of the
array, which would screw the whole thing up.  (This is unlikely for a
byte-addressable machine, perhaps, but not all machines are like that;
Irvine Compiler's Ada compiler supports a target whose
System.Storage_Unit is 32, and on that machine, the compiler would put
each array element in a separate 32-bit word unless other
representation clauses tell it not to.)

I think it's necessary to add:

    for Buffer'Component_Size use 16;
    pragma Pack (Buffer);

to ensure that each array element takes up exactly 16 bits and there
are no extra gaps.  (If the Implementation Advice is followed (13.3
(71-73)), the compiler should reject the program if it cannot support
this, as might be the case on a machine with a 36-bit word; but that's
a darn sight better than accepting the program and letting it not work
right.)  I'm not 100% clear on whether the Pack is necessary.

With all those issues, I'd say this gem needs a bit of grinding.

                                      -- Adam



      parent reply	other threads:[~2009-09-03  0:12 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-02 20:38 Gem 39 - compiler specific? Maciej Sobczak
2009-09-02 23:20 ` Randy Brukardt
2009-09-03  7:26   ` Dmitry A. Kazakov
2009-09-03 15:26     ` Adam Beneschan
2009-09-03 16:38       ` Dmitry A. Kazakov
2009-09-03 17:27         ` Adam Beneschan
2009-09-03 20:26           ` Dmitry A. Kazakov
2009-09-03 22:06             ` Randy Brukardt
2009-09-04  7:29               ` Dmitry A. Kazakov
2009-09-04 12:07                 ` Maciej Sobczak
2009-09-04 13:06                   ` Dmitry A. Kazakov
2009-09-04 17:18                     ` Dmitry A. Kazakov
2009-09-04 20:34                     ` Maciej Sobczak
2009-09-04 22:41                   ` sjw
2009-09-05 20:45                     ` Maciej Sobczak
2009-09-06  6:54                       ` sjw
2009-09-03 21:58           ` Randy Brukardt
2009-09-04 17:26           ` Robert A Duff
2009-09-03 21:53     ` Randy Brukardt
2009-09-03  0:12 ` Adam Beneschan [this message]
replies disabled

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