comp.lang.ada
 help / color / mirror / Atom feed
* Confused about Attribute Size?
@ 2002-01-22  7:39 Zebylon
  2002-01-22 19:08 ` Patrick Hohmeyer
  2002-01-22 22:56 ` Mark Johnson
  0 siblings, 2 replies; 8+ messages in thread
From: Zebylon @ 2002-01-22  7:39 UTC (permalink / raw)


Hi,

I'm a newbie in Ada but I saw an Ada example that confused me a bit, here it
is:

type My_Type is
    (startbit,
     endbit,
     timeout,
     spare,
     nextbit,
     ......
     ......
     lastbit);

for My_Type use
    (startbit =>0,
     endbit=>1,
     timeout=>2,
     spare=>3,
     nextbit=>4,
     ......
     ......
     lastbit=>15);

for My_Type'Size use 8;

type My_Type_Array is array (My_Type) of Boolean;

What I don't understand is how you can assign the size to 8 bits? How will
this work when
there are 16 bits in the type? What will be the size of the array?
This example was in Ada 83 could this be a clue?

Thanks

/S






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

* Re: Confused about Attribute Size?
@ 2002-01-22  8:00 Christoph Grein
  0 siblings, 0 replies; 8+ messages in thread
From: Christoph Grein @ 2002-01-22  8:00 UTC (permalink / raw)


> 
> type My_Type is
>     (startbit,
>      endbit,
>      timeout,
>      spare,
>      nextbit,
>      ......
>      ......
>      lastbit);

This assigns _values_ (not bit positions) to the enumeration literals that shall 
be used to represent them internally, i.e. startbit has the value (or is another 
name for) 0, lastbit the value 15. However you have no access to the internal 
values (you can't add them e.g. startbit + timeout is illegal).

> for My_Type use
>     (startbit =>0,
>      endbit=>1,
>      timeout=>2,
>      spare=>3,
>      nextbit=>4,
>      ......
>      ......
>      lastbit=>15);
> 
> for My_Type'Size use 8;

Since a value of 15 = 2#00001111# can be represented in 8 bits, there is no 
problem.

> type My_Type_Array is array (My_Type) of Boolean;
> 
> What I don't understand is how you can assign the size to 8 bits? How will
> this work when
> there are 16 bits in the type? What will be the size of the array?
> This example was in Ada 83 could this be a clue?

In Ada95, there is no need to explicitly define this default representation, see 
RM 13.4(8).



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

* Re: Confused about Attribute Size?
  2002-01-22  7:39 Zebylon
@ 2002-01-22 19:08 ` Patrick Hohmeyer
  2002-01-22 22:56 ` Mark Johnson
  1 sibling, 0 replies; 8+ messages in thread
From: Patrick Hohmeyer @ 2002-01-22 19:08 UTC (permalink / raw)


And Zebylon has spoken :

> Hi,
> 
> I'm a newbie in Ada but I saw an Ada example that confused me a bit, here
> it is:
> 
> type My_Type is
>     (startbit,
>      endbit,
>      timeout,
>      spare,
>      nextbit,
>      ......
>      ......
>      lastbit);
> 
> for My_Type use
>     (startbit =>0,
>      endbit=>1,
>      timeout=>2,
>      spare=>3,
>      nextbit=>4,
>      ......
>      ......
>      lastbit=>15);
> 
> for My_Type'Size use 8;
> 
> type My_Type_Array is array (My_Type) of Boolean;
> 
> What I don't understand is how you can assign the size to 8 bits? How will
> this work when
> there are 16 bits in the type? What will be the size of the array?
> This example was in Ada 83 could this be a clue?
> 
> Thanks
> 
> /S

Hint : My_Type is an enumeration type.

Ex : Character is also an enumeration,
there are 256 possible characters,
does this make a character 256 bit ? ;-)

The array will be 16 or 2 bytes
(if you use the pragma pack, what I suspect)
One boolean per possible value of My_Type.

Hint2 : try some general tutorials befor trying to understand chapter 13.

-- 
Patrick Hohmeyer



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

* Re: Confused about Attribute Size?
  2002-01-22  7:39 Zebylon
  2002-01-22 19:08 ` Patrick Hohmeyer
@ 2002-01-22 22:56 ` Mark Johnson
  2002-01-23  2:20   ` Jeffrey Carter
  1 sibling, 1 reply; 8+ messages in thread
From: Mark Johnson @ 2002-01-22 22:56 UTC (permalink / raw)


Zebylon wrote:
> 
> Hi,
> 
> I'm a newbie in Ada but I saw an Ada example that confused me a bit, here it
> is:
> 
See comments after each block of code and the questions at the end....

> type My_Type is
>     (startbit,
>      endbit,
>      timeout,
>      spare,
>      nextbit,
>      ......
>      ......
>      lastbit);
> 
Ok, we define an enumeration of several values.

> for My_Type use
>     (startbit =>0,
>      endbit=>1,
>      timeout=>2,
>      spare=>3,
>      nextbit=>4,
>      ......
>      ......
>      lastbit=>15);
> 
A representation specification that requires startbit to be zero, endbit
to be one, and so on. Assuming the items are sequential, there are 16
possible values, 0 through 15.

> for My_Type'Size use 8;
> 
Another representation specification. In this case, you are asking that
the 16 possible values to be stored in an eight bit byte (or larger).
There are several cases where the compiler can use more than 8 bits to
store My_Type...
 - a record component if the record is NOT packed
 - array components
 - a formal parameter passed to a function or procedure
There are several pages of dense text in the Annotated Ada Reference
Manual in section 13.3 that describes this in far more detail that I
will attempt to describe - especially to a newbie.

> type My_Type_Array is array (My_Type) of Boolean;
> 
An array of 16 boolean values (index is startbit..lastbit). Hmm. Since
you did not specify a representation specification nor pragma Packed for
this data type, the compiler could...
 - generate a 16 bit array (two bytes)
 - generate a 16 byte array (16 bytes)
 - generate a 16 [some easy to address size goes here] array (16 x n
bytes)
Without the representation specification or pragma, "you don't care"
about the size, you care about an efficient implementation.

> What I don't understand is how you can assign the size to 8 bits?
> How will this work when there are 16 bits in the type?
I assume you refer to My_Type'Size (8) and not My_Type_Array'Size
(>=16). Eight bits is more than enough bits to represent 16 values from
0 to 15.

> What will be the size of the array?
Hmm. You didn't specify the size explicitly, therefore the compiler can
pick something "convenient". As I mentioned above, it will likely be
some multiple of 16 bits. As small as 16, not likely, but could as large
or larger than 16 x 64.

> This example was in Ada 83 could this be a clue?
Nope. At this level, this code would be the same in Ada 83 and Ada 95.

--
  --Mark



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

* Re: Confused about Attribute Size?
  2002-01-22 22:56 ` Mark Johnson
@ 2002-01-23  2:20   ` Jeffrey Carter
  2002-01-23 14:56     ` Mark Johnson
  0 siblings, 1 reply; 8+ messages in thread
From: Jeffrey Carter @ 2002-01-23  2:20 UTC (permalink / raw)


Mark Johnson wrote:
> 
> > for My_Type'Size use 8;
> >
> Another representation specification. In this case, you are asking that
> the 16 possible values to be stored in an eight bit byte (or larger).
> There are several cases where the compiler can use more than 8 bits to
> store My_Type...
>  - a record component if the record is NOT packed
>  - array components
>  - a formal parameter passed to a function or procedure
> There are several pages of dense text in the Annotated Ada Reference
> Manual in section 13.3 that describes this in far more detail that I
> will attempt to describe - especially to a newbie.

A 'Size specification on a type instructs the compiler to use that size
for components of packed records and arrays. In most other cases
(records with the layout specified is an exception), the compiler may
use any size it chooses.

-- 
Jeff Carter
"Go and boil your bottoms."
Monty Python & the Holy Grail



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

* Re: Confused about Attribute Size?
  2002-01-23  2:20   ` Jeffrey Carter
@ 2002-01-23 14:56     ` Mark Johnson
  2002-01-23 17:06       ` Jeffrey Carter
  0 siblings, 1 reply; 8+ messages in thread
From: Mark Johnson @ 2002-01-23 14:56 UTC (permalink / raw)


Jeffrey Carter wrote:
> 
> Mark Johnson wrote:
> >
> > > for My_Type'Size use 8;
> > >
> > Another representation specification. In this case, you are asking that
> > the 16 possible values to be stored in an eight bit byte (or larger).
> > There are several cases where the compiler can use more than 8 bits to
> > store My_Type...
> >  - a record component if the record is NOT packed
> >  - array components
> >  - a formal parameter passed to a function or procedure
> > There are several pages of dense text in the Annotated Ada Reference
> > Manual in section 13.3 that describes this in far more detail that I
> > will attempt to describe - especially to a newbie.
> 
> A 'Size specification on a type instructs the compiler to use that size
> for components of packed records and arrays. In most other cases
> (records with the layout specified is an exception), the compiler may
> use any size it chooses.
> 
> --
> Jeff Carter
> "Go and boil your bottoms."
> Monty Python & the Holy Grail

What you state is often true but not required behavior. The annotated
ARM says 13.3 (52.c)...
  Note that "for S2'Size use 5;" requires record components whose
subtype is S2 to be exactly 5 bits if the record type is packed. The
same is NOT TRUE [my emphasis] of array components; their Size MAY [my
emphasis] be rounded up to the nearest factor of word size. [what I
stated]

What you described is often true but not required behavior for an Ada
compiler.

I can also point you to a note in the GNAT 3.14a1 reference manual where
it states...
  GNAT treats packed arrays in one of two ways. If the size of the array
is known at compile time and is less than 64 bits, then internally the
array is represented as a single modular type, of exactly the
appropriate number of bits. [what you stated] If the length is greater
than 63 bits, or is not known at compile time, then the packed array is
represented as an array of bytes, and the length is always a multiple of
8 bits. [what I stated]

As I noted above, it is far more complicated than it appears.
  --Mark



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

* Re: Confused about Attribute Size?
  2002-01-23 14:56     ` Mark Johnson
@ 2002-01-23 17:06       ` Jeffrey Carter
  2002-01-24  4:11         ` Robert Dewar
  0 siblings, 1 reply; 8+ messages in thread
From: Jeffrey Carter @ 2002-01-23 17:06 UTC (permalink / raw)


Mark Johnson wrote:
> 
> Jeffrey Carter wrote:
> >
> > A 'Size specification on a type instructs the compiler to use that size
> > for components of packed records and arrays. In most other cases
> > (records with the layout specified is an exception), the compiler may
> > use any size it chooses.
> 
> What you state is often true but not required behavior. The annotated
> ARM says 13.3 (52.c)...
>   Note that "for S2'Size use 5;" requires record components whose
> subtype is S2 to be exactly 5 bits if the record type is packed. The
> same is NOT TRUE [my emphasis] of array components; their Size MAY [my
> emphasis] be rounded up to the nearest factor of word size. [what I
> stated]

Since a compiler may ignore pragma Pack completely, a 'Size
specification does not require any behavior (except possibly the size of
the Source parameter for an instance of Ada.Unchecked_Conversion). Even
when a compiler packs a record, it may use a larger size than 'Size for
a component or add padding [ARM 13.2(7)].

So a 'Size specification on a type is an instruction to the compiler,
but that instruction may be ignored in most cases.

In practice, most compilers do an excellent job of packing records. Most
compilers also do an excellent job of packing arrays when
'Component_Size is specified for the array type.

-- 
Jeffrey Carter



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

* Re: Confused about Attribute Size?
  2002-01-23 17:06       ` Jeffrey Carter
@ 2002-01-24  4:11         ` Robert Dewar
  0 siblings, 0 replies; 8+ messages in thread
From: Robert Dewar @ 2002-01-24  4:11 UTC (permalink / raw)


Jeffrey Carter <jeffrey.carter@boeing.com> wrote in message news:<3C4EEDAA.10377A9C@boeing.com>...
> Since a compiler may ignore pragma Pack completely

Quite false!

> a 'Size specification does not require any behavior

Quite false!



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

end of thread, other threads:[~2002-01-24  4:11 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-01-22  8:00 Confused about Attribute Size? Christoph Grein
  -- strict thread matches above, loose matches on Subject: below --
2002-01-22  7:39 Zebylon
2002-01-22 19:08 ` Patrick Hohmeyer
2002-01-22 22:56 ` Mark Johnson
2002-01-23  2:20   ` Jeffrey Carter
2002-01-23 14:56     ` Mark Johnson
2002-01-23 17:06       ` Jeffrey Carter
2002-01-24  4:11         ` Robert Dewar

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