comp.lang.ada
 help / color / mirror / Atom feed
* Size of objects in records with an array.
@ 2006-10-24 17:48 Richard Charts
  2006-10-24 17:56 ` Robert A Duff
  0 siblings, 1 reply; 5+ messages in thread
From: Richard Charts @ 2006-10-24 17:48 UTC (permalink / raw)


So I'm having more fun with records in Ada, particularly ones that must
be of an exact form.
I have a 32 bit data item I'd like to access the individual bits for.
My initial way was the 1st blurb below.  It works but reality slapped
me in the face when I realized I needed to access the bits either like
an array or with some way to loop through the bits.
So I tried the bottom way, but it says the size is incorrect.  Is there
a way to make this work, or am I stuck looping and bit shifting a 32
bit item?
Thanks.
-------------------------------------------------

    type Data_Msg is
    record
        A429    :   Interfaces.Unsigned_8;
        Bit9    :   Boolean;
        Bit10    :   Boolean;
        Bit11    :   Boolean;
        Bit12    :   Boolean;
        Bit13    :   Boolean;
        Bit14    :   Boolean;
        Bit15    :   Boolean;
        Bit16    :   Boolean;
        Bit17    :   Boolean;
        Bit18    :   Boolean;
        Bit19    :   Boolean;
        Bit20    :   Boolean;
        Bit21    :   Boolean;
        Bit22    :   Boolean;
        Bit23    :   Boolean;
        Bit24    :   Boolean;
        Bit25    :   Boolean;
        Bit26    :   Boolean;
        Bit27    :   Boolean;
        Bit28    :   Boolean;
        Bit29    :   Boolean;
        Bit30    :   Boolean;
        Bit31    :   Boolean;
        Bit32    :   Boolean;
    end record;

    for Data_Msg use
    record
        A429    at 0 range 0..7;
        Bit9    at 1 range 0..0;
        Bit10   at 1 range 1..1;
        Bit11   at 1 range 2..2;
        Bit12   at 1 range 3..3;
        Bit13   at 1 range 4..4;
        Bit14   at 1 range 5..5;
        Bit15   at 1 range 6..6;
        Bit16   at 1 range 7..7;
        Bit17   at 2 range 0..0;
        Bit18   at 2 range 1..1;
        Bit19   at 2 range 2..2;
        Bit20   at 2 range 3..3;
        Bit21   at 2 range 4..4;
        Bit22   at 2 range 5..5;
        Bit23   at 2 range 6..6;
        Bit24   at 2 range 7..7;
        Bit25   at 3 range 0..0;
        Bit26   at 3 range 1..1;
        Bit27   at 3 range 2..2;
        Bit28   at 3 range 3..3;
        Bit29   at 3 range 4..4;
        Bit30   at 3 range 5..5;
        Bit31   at 3 range 6..6;
        Bit32   at 3 range 7..7;
    end record;

    for Data_Msg'size use (8 * 4);

--------------------------------------------------
But I want to use this:
--------------------------------------------------
    type Bit_Array is array ( Natural range 8..31)
        of boolean;

    type Data_Msg is
    record
        A429    :   Interfaces.Unsigned_8;
        Bit_Arr    :  Bit_Array := (others => false);
    end record;

    for Data_Msg use
    record
        A429    at 0 range 0..7;
        Bit_Arr    at 1 range 0..23;
    end record;
    
    for Data_Msg'size use (8 * 4);




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Size of objects in records with an array.
  2006-10-24 17:48 Size of objects in records with an array Richard Charts
@ 2006-10-24 17:56 ` Robert A Duff
  2006-10-24 18:06   ` Richard Charts
  0 siblings, 1 reply; 5+ messages in thread
From: Robert A Duff @ 2006-10-24 17:56 UTC (permalink / raw)


"Richard Charts" <richard.charts@gmail.com> writes:

>     type Bit_Array is array ( Natural range 8..31)
>         of boolean;

    for Bit_Array'Component_Size use 1;

- Bob



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Size of objects in records with an array.
  2006-10-24 17:56 ` Robert A Duff
@ 2006-10-24 18:06   ` Richard Charts
  2006-10-24 21:02     ` Adam Beneschan
  2006-10-26 23:53     ` Robert A Duff
  0 siblings, 2 replies; 5+ messages in thread
From: Richard Charts @ 2006-10-24 18:06 UTC (permalink / raw)



Robert A Duff wrote:
> "Richard Charts" <richard.charts@gmail.com> writes:
>
> >     type Bit_Array is array ( Natural range 8..31)
> >         of boolean;
>
>     for Bit_Array'Component_Size use 1;
>
> - Bob

Bah!
So obvious.
Thank you very much.
Out of curiousity would the following lines be equivalent?
I know both compile.

for Bit_Array'Component_Size use 1;

for Bit_Array'Size use 24;




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Size of objects in records with an array.
  2006-10-24 18:06   ` Richard Charts
@ 2006-10-24 21:02     ` Adam Beneschan
  2006-10-26 23:53     ` Robert A Duff
  1 sibling, 0 replies; 5+ messages in thread
From: Adam Beneschan @ 2006-10-24 21:02 UTC (permalink / raw)


Richard Charts wrote:

> Bah!
> So obvious.
> Thank you very much.
> Out of curiousity would the following lines be equivalent?
> I know both compile.
>
> for Bit_Array'Component_Size use 1;
>
> for Bit_Array'Size use 24;

This is implementation-dependent.  However, if the latter clause
compiles and *doesn't* cause the 'Component_Size to be changed to 1, I
think there has to be a bug in the compiler.  It's possible that the
'Size clause may not compile.

RM 13.3(53) says, "A Size clause on a composite subtype should not
affect the internal layout of components."

and the AARM says, in that regard,

"On most machines, arrays don't contain gaps between elementary
components; if the Component_Size is greater than the Size of the
component subtype, the extra bits are generally considered part of each
component, rather than gaps between components."

and

 "For an array, any extra bits stored for each elementary component
will generally be part of the component".

That means that if the normal implementation of a Boolean array is to
put one Boolean in each 8-bit byte, then each component is an 8-bit
object, and having the 'Size clause change the 'Component_Size to 1
would thus affect the internal layout of the components and thus go
against the RM.  However, this is open to interpretation (translation:
I may be all wet), and it's all under Implementation Advice anyway, so
implementations can allow the 'Size clause if they wish.

But I'd use the 'Component_Size clause instead of 'Size.  The former
should work, the latter may be rejected.

                         -- Adam




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Size of objects in records with an array.
  2006-10-24 18:06   ` Richard Charts
  2006-10-24 21:02     ` Adam Beneschan
@ 2006-10-26 23:53     ` Robert A Duff
  1 sibling, 0 replies; 5+ messages in thread
From: Robert A Duff @ 2006-10-26 23:53 UTC (permalink / raw)


"Richard Charts" <richard.charts@gmail.com> writes:

> Out of curiousity would the following lines be equivalent?
> I know both compile.
>
> for Bit_Array'Component_Size use 1;
>
> for Bit_Array'Size use 24;

I'm not sure both are supposed to compile, but if they do,
then they should both force the component size to be 1 bit.

- Bob



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2006-10-26 23:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-10-24 17:48 Size of objects in records with an array Richard Charts
2006-10-24 17:56 ` Robert A Duff
2006-10-24 18:06   ` Richard Charts
2006-10-24 21:02     ` Adam Beneschan
2006-10-26 23:53     ` Robert A Duff

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