comp.lang.ada
 help / color / mirror / Atom feed
* accessing record element (question to the pros)
@ 2006-02-24 15:03 Norbert Caspari
  2006-02-24 15:27 ` Peter Hermann
                   ` (4 more replies)
  0 siblings, 5 replies; 11+ messages in thread
From: Norbert Caspari @ 2006-02-24 15:03 UTC (permalink / raw)


I'm new to Ada programming and until now I wrote just some small pieces of 
code to try out new things and to educate myself. In this example I try to 
build a Union Record, where I can access a single Byte either as a small 
positive Integer value or as single Bits. I don't understand, why there is 
a compiler error in line 56 and why it is forbidden in this case to assign 
just a simple positive Integer value. If I comment this line out, the rest 
of the code will compile without errors. Please remember, this is just some 
experimental code with no special sense and no meaningful variable names.

Could somebody please help me to correct my code, showing me how to access 
a numeric value to the variable Yyy.

Thank you very much for your help!

Best regards, Norbert

----------------------------------------------------------
GNAT 3.15p  (20020523) Copyright 1992-2002 Free Software Foundation, Inc.

Compiling: j:/source/uniontest4.adb (source file time stamp: 2006-02-24 
14:43:50)

     1. with Ada.Text_Io;
     2. use Ada.Text_Io;
     3. 
     4. procedure Uniontest4 is
     5. 
     6.    type Bit_T is
     7.          (Off,
     8.           On);
     9. 
    10.    for Bit_T use (
    11.       Off => 0,
    12.       On  => 1);
    13. 
    14.    type Bit8 is array (0 .. 7) of Bit_T;
    15. 
    16.    pragma Pack(Bit8);
    17.    for Bit8'Size use 8;
    18. 
    19.    type Uint8 is mod 2**8;
    20. 
    21.    for Uint8'Size use 8;
    22. 
    23.    type Form is
    24.          (Bytemode,
    25.           Bitmode);
    26. 
    27.    type Bfeld (Ftyp: Form) is
    28.    record
    29.       case Ftyp is
    30.          when Bitmode =>
    31.             Bit: Bit8;
    32.          when Bytemode =>
    33.             Byte : Uint8;
    34.       end case;
    35.    end record;
    36. 
    37.    pragma Pack(Bfeld);
    38. 
    39.    for Bfeld use record
    40.       at mod 1;
    41.       Bit at 0 range 0..7;
    42.       Byte at 0 range 0..7;
    43.    end record;
    44. 
    45.    type Bfeld_Bitmode is new Bfeld
    46.          (Bitmode);
    47.    type Bfeld_Bytemode is new Bfeld
    48.          (Bytemode);
    49. 
    50.    Xxx : Bfeld_Bitmode;
    51.    Yyy : Bfeld_Bytemode;
    52. 
    53. begin
    54. 
    55.    Xxx.Bit(2) := On;
    56.    Yyy := 20;
                  |
        >>> expected type "Bfeld_Bytemode" defined at line 47
        >>> found type universal integer

    57. 
    58.    for K in 0 .. 7
    59.          loop
    60.       Put(Bit_T'Image(Xxx.Bit(K)));
    61.    end loop;
    62.    New_Line;
    63. 
    64. end Uniontest4;
    65. 

 65 lines: 2 errors




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

* Re: accessing record element (question to the pros)
  2006-02-24 15:03 accessing record element (question to the pros) Norbert Caspari
@ 2006-02-24 15:27 ` Peter Hermann
  2006-02-24 17:07 ` Norbert Caspari
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 11+ messages in thread
From: Peter Hermann @ 2006-02-24 15:27 UTC (permalink / raw)


Norbert Caspari <nnc@gmx.li> wrote:
> I'm new to Ada programming and until now I wrote just some small pieces of 
> code to try out new things and to educate myself. In this example I try to 
> build a Union Record, where I can access a single Byte either as a small 
> positive Integer value or as single Bits. I don't understand, why there is 
> a compiler error in line 56 and why it is forbidden in this case to assign 
> just a simple positive Integer value. If I comment this line out, the rest 
> of the code will compile without errors. Please remember, this is just some 
> experimental code with no special sense and no meaningful variable names.
> 
> Could somebody please help me to correct my code, showing me how to access 

how to assign ?

> a numeric value to the variable Yyy.
> 
> Thank you very much for your help!
> 
> Best regards, Norbert
> 
> ----------------------------------------------------------
> GNAT 3.15p  (20020523) Copyright 1992-2002 Free Software Foundation, Inc.
> 
> Compiling: j:/source/uniontest4.adb (source file time stamp: 2006-02-24 
> 14:43:50)
> 
>      1. with Ada.Text_Io;
>      2. use Ada.Text_Io;
>      3. 
>      4. procedure Uniontest4 is
>      5. 
>      6.    type Bit_T is
>      7.          (Off,
>      8.           On);

may be cumbersome to replace 0 by off   etc

why not boolean?

>      9. 
>     10.    for Bit_T use (
>     11.       Off => 0,
>     12.       On  => 1);
>     13. 
>     14.    type Bit8 is array (0 .. 7) of Bit_T;
>     15. 
>     16.    pragma Pack(Bit8);
>     17.    for Bit8'Size use 8;
>     18. 
>     19.    type Uint8 is mod 2**8;
>     20. 
>     21.    for Uint8'Size use 8;
>     22. 
>     23.    type Form is
>     24.          (Bytemode,
>     25.           Bitmode);
>     26. 
>     27.    type Bfeld (Ftyp: Form) is
>     28.    record
>     29.       case Ftyp is
>     30.          when Bitmode =>
>     31.             Bit: Bit8;
>     32.          when Bytemode =>
>     33.             Byte : Uint8;
>     34.       end case;
>     35.    end record;
>     36. 
>     37.    pragma Pack(Bfeld);
>     38. 
>     39.    for Bfeld use record
>     40.       at mod 1;
>     41.       Bit at 0 range 0..7;
>     42.       Byte at 0 range 0..7;
>     43.    end record;
>     44. 
>     45.    type Bfeld_Bitmode is new Bfeld
>     46.          (Bitmode);
>     47.    type Bfeld_Bytemode is new Bfeld
>     48.          (Bytemode);

with these declarations you have lost the advantage of the
discriminated record bfeld

>     49. 
>     50.    Xxx : Bfeld_Bitmode;
>     51.    Yyy : Bfeld_Bytemode;
>     52. 
>     53. begin
>     54. 
>     55.    Xxx.Bit(2) := On;
>     56.    Yyy := 20;

yyy.byte := 20;

>                   |
>         >>> expected type "Bfeld_Bytemode" defined at line 47
>         >>> found type universal integer
> 
>     57. 
>     58.    for K in 0 .. 7
>     59.          loop
>     60.       Put(Bit_T'Image(Xxx.Bit(K)));
>     61.    end loop;
>     62.    New_Line;
>     63. 
>     64. end Uniontest4;
>     65. 
> 
>  65 lines: 2 errors
> 

-- 
--Peter.Hermann@ihr.uni-stuttgart.de                (+49)0711-685-87244
--Nobelstr.19 Raum 0.030, D-70569 Stuttgart IHR Hoechstleistungsrechnen
--http://www.ihr.uni-stuttgart.de/                   Fax  0711-89238279




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

* Re: accessing record element (question to the pros)
  2006-02-24 15:03 accessing record element (question to the pros) Norbert Caspari
  2006-02-24 15:27 ` Peter Hermann
@ 2006-02-24 17:07 ` Norbert Caspari
  2006-02-24 17:08 ` Alex R. Mosteo
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 11+ messages in thread
From: Norbert Caspari @ 2006-02-24 17:07 UTC (permalink / raw)


I found the solution, sorry for bothering you.

Of course it must be written

yyy.byte:= 20;

Sometimes I can't see the wood for the trees ;-)

Best regards, Norbert




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

* Re: accessing record element (question to the pros)
  2006-02-24 15:03 accessing record element (question to the pros) Norbert Caspari
  2006-02-24 15:27 ` Peter Hermann
  2006-02-24 17:07 ` Norbert Caspari
@ 2006-02-24 17:08 ` Alex R. Mosteo
  2006-02-24 18:02 ` Jeffrey R. Carter
  2006-02-24 18:32 ` accessing record element (question to the pros) Martin Krischik
  4 siblings, 0 replies; 11+ messages in thread
From: Alex R. Mosteo @ 2006-02-24 17:08 UTC (permalink / raw)


Norbert Caspari wrote:
> (...) In this example I try to build a Union Record, where I can
> access a single Byte either as a small positive Integer value or as
> single Bits. (...)

You don't do this in Ada with variant records. I would elaborate but I'm 
just leaving the office. Take a look at Unchecked_Conversion and the 
'Address attribute for a start.



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

* Re: accessing record element (question to the pros)
  2006-02-24 15:03 accessing record element (question to the pros) Norbert Caspari
                   ` (2 preceding siblings ...)
  2006-02-24 17:08 ` Alex R. Mosteo
@ 2006-02-24 18:02 ` Jeffrey R. Carter
  2006-02-24 19:04   ` Simon Wright
  2006-02-24 18:32 ` accessing record element (question to the pros) Martin Krischik
  4 siblings, 1 reply; 11+ messages in thread
From: Jeffrey R. Carter @ 2006-02-24 18:02 UTC (permalink / raw)


Norbert Caspari wrote:

> Could somebody please help me to correct my code, showing me how to access 
> a numeric value to the variable Yyy.

You can't, because Yyy is not of a numeric type.

However, in general, you can't do what you seem to be trying to do. Variant 
records do not allow unchecked type conversion; the language rules prohibit it. 
That's what Ada.Unchecked_Conversion is for.

For the specific problem of accessing bits in an unsigned integer, you can use 
the logical operators on modular types: and, or, Shift_Right, and so on. For a 
signed integer, you can unchecked convert to an array of Boolean, but then you 
have a portability problem: is the LSB in 'First or 'Last? This is not defined 
by the language.

-- 
Jeff Carter
"I've got to stay here, but there's no reason
why you folks shouldn't go out into the lobby
until this thing blows over."
Horse Feathers
50



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

* Re: accessing record element (question to the pros)
  2006-02-24 15:03 accessing record element (question to the pros) Norbert Caspari
                   ` (3 preceding siblings ...)
  2006-02-24 18:02 ` Jeffrey R. Carter
@ 2006-02-24 18:32 ` Martin Krischik
  2006-02-28 20:02   ` Matthew Heaney
  4 siblings, 1 reply; 11+ messages in thread
From: Martin Krischik @ 2006-02-24 18:32 UTC (permalink / raw)


Norbert Caspari wrote:

> n this example I try to
> build a Union Record, where I can access a single Byte either as a small
> positive Integer value or as single Bits.

In Pascal you needed this construct but Ada has Unchecked_Conversion to
perform this task.

In Ada a variant record is checked for consistency and you can't do it
because its inconsistence.

Suggested readings:

http://en.wikibooks.org/wiki/Ada_Programming/Subtypes#Converting_data
http://en.wikibooks.org/wiki/Ada_Programming/Types/record#Variant_record

And if you still want to do it: The newest GNAT has pragma Unchecked_Union.
You find a description in the texts above.

Martin
-- 
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com



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

* Re: accessing record element (question to the pros)
  2006-02-24 18:02 ` Jeffrey R. Carter
@ 2006-02-24 19:04   ` Simon Wright
  2006-02-24 21:46     ` Randy Brukardt
  0 siblings, 1 reply; 11+ messages in thread
From: Simon Wright @ 2006-02-24 19:04 UTC (permalink / raw)


"Jeffrey R. Carter" <spam@spam.com> writes:

> However, in general, you can't do what you seem to be trying to
> do. Variant records do not allow unchecked type conversion; the
> language rules prohibit it. That's what Ada.Unchecked_Conversion is
> for.

GNAT (certainly in recent versions) has pragma Unchecked_Union
(http://gcc.gnu.org/onlinedocs/gcc-4.0.2/gnat_rm/Pragma-Unchecked_005fUnion.html#Pragma-Unchecked_005fUnion).

NB, compiler-specific.



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

* Re: accessing record element (question to the pros)
  2006-02-24 19:04   ` Simon Wright
@ 2006-02-24 21:46     ` Randy Brukardt
  2006-02-26 11:49       ` many thanks to all the people who give me useful hints! Norbert Caspari
  0 siblings, 1 reply; 11+ messages in thread
From: Randy Brukardt @ 2006-02-24 21:46 UTC (permalink / raw)


"Simon Wright" <simon@pushface.org> wrote in message
news:m2oe0w7f3c.fsf@grendel.local...
> "Jeffrey R. Carter" <spam@spam.com> writes:
>
> > However, in general, you can't do what you seem to be trying to
> > do. Variant records do not allow unchecked type conversion; the
> > language rules prohibit it. That's what Ada.Unchecked_Conversion is
> > for.
>
> GNAT (certainly in recent versions) has pragma Unchecked_Union
>
(http://gcc.gnu.org/onlinedocs/gcc-4.0.2/gnat_rm/Pragma-Unchecked_005fUnion.
html#Pragma-Unchecked_005fUnion).
>
> NB, compiler-specific.

It is present in a number of Ada 95 compilers (at least the Ada
Magic-derived ones as well as GNAT), and is part of Ada 2005 (see
http://www.adaic.com/standards/05rm/html/RM-B-3-3.html). So it's not really
compiler-specific.

OTOH, Unchecked_Union is *only* intended for interfacing to C. In
particular, using it to get the effect of Unchecked_Conversion is strongly
discouraged. Indeed, any such use is defined to be erroneous (see the Note
at the end of B.3.3 - there's no explicit rule because it follows from the
rules for check suppression). It might work, but a compiler could raise an
exception or reformat your hard disk as well...

If you want this to work reliably, use Unchecked_Conversion. I don't endorse
tricks with 'Address, either, because they can confuse optimizers and are
just too tricky to understand for the maintenance programmers to follow. Ada
has a special dispensation that Unchecked_Conversion doesn't have to make a
copy (unlike other functions), so there's really no good reason for using
tricky overlays. If Unchecked_Conversion is too slow, lean on your compiler
vendor!!

                         Randy.





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

* many thanks to all the people who give me useful hints!
  2006-02-24 21:46     ` Randy Brukardt
@ 2006-02-26 11:49       ` Norbert Caspari
  2006-02-28  0:33         ` Randy Brukardt
  0 siblings, 1 reply; 11+ messages in thread
From: Norbert Caspari @ 2006-02-26 11:49 UTC (permalink / raw)


Thank you very much to all the people, who gave me some more useful tips 
and hints about Ada programming. Especially the information about the 
unchecked conversion, unchecked union and about the overlay conversion are 
particular interesting for me. Don't worry, I'm familiar with assembler 
programming and I have particular imagination about the code, the compiler 
will produce from my sources.

Especially the hint about the Wikibook is very useful for me. Thanks a lot 
for giving me this advice!

If somebody could mention a book about Ada, especially for embedded system 
programming, I would be very happy.

Thanks a lot for you kind help!

Best regards, Norbert.

PS: please excuse my horrible english, it's not my native language ;-)




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

* Re: many thanks to all the people who give me useful hints!
  2006-02-26 11:49       ` many thanks to all the people who give me useful hints! Norbert Caspari
@ 2006-02-28  0:33         ` Randy Brukardt
  0 siblings, 0 replies; 11+ messages in thread
From: Randy Brukardt @ 2006-02-28  0:33 UTC (permalink / raw)


"Norbert Caspari" <nnc@gmx.li> wrote in message
news:dts4kg$fre$03$1@news.t-online.com...
...
> If somebody could mention a book about Ada, especially for embedded system
> programming, I would be very happy.

See the newly updated list of textbooks on AdaIC for suggestions:

   http://www.adaic.org/learn/textbook.html

                 Randy Brukardt, Technical Webmaster adaic.org/.com





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

* Re: accessing record element (question to the pros)
  2006-02-24 18:32 ` accessing record element (question to the pros) Martin Krischik
@ 2006-02-28 20:02   ` Matthew Heaney
  0 siblings, 0 replies; 11+ messages in thread
From: Matthew Heaney @ 2006-02-28 20:02 UTC (permalink / raw)


Martin Krischik wrote:

> And if you still want to do it: The newest GNAT has pragma Unchecked_Union.
> You find a description in the texts above.

Yes, but realize that that pragma is now officially part of Ada 2005.
It's true the GNAT has it, but I don't think even GNAT supports what's
described in RM05.  (But I could be wrong.  All compilers differ in how
much of Ada 2005 they support today.)




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

end of thread, other threads:[~2006-02-28 20:02 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-02-24 15:03 accessing record element (question to the pros) Norbert Caspari
2006-02-24 15:27 ` Peter Hermann
2006-02-24 17:07 ` Norbert Caspari
2006-02-24 17:08 ` Alex R. Mosteo
2006-02-24 18:02 ` Jeffrey R. Carter
2006-02-24 19:04   ` Simon Wright
2006-02-24 21:46     ` Randy Brukardt
2006-02-26 11:49       ` many thanks to all the people who give me useful hints! Norbert Caspari
2006-02-28  0:33         ` Randy Brukardt
2006-02-24 18:32 ` accessing record element (question to the pros) Martin Krischik
2006-02-28 20:02   ` Matthew Heaney

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