Enumeration representation clause surprise.
  Home FAQ Contact Sign in
comp.lang.ada only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.ada Profile…
 Up
Enumeration representation clause surprise.         


Author: Markus Schoepflin
Date: Jun 11, 2008 04:38

Hello,

please consider the following code snippet:

type ENUM_T is (ONE, TWO);
for ENUM_T use (ONE => 1, TWO => 2);

type RECORD_T is record
A1 : ENUM_T;
A2 : ENUM_T;
end record;
for RECORD_T use record
A1 at 0 range 0 .. 0; -- (*)
A2 at 1 range 0 .. 7;
end record;
for RECORD_T'Size use 2 * 8;

FOO : RECORD_T;

I was _very_ surprised to discover that when executing

FOO.A1 := TWO;
FOO.A2 := TWO;

the binary content of FOO looks like this:

00000010 00000001
Show full article (1.28Kb)
41 Comments
Re: Enumeration representation clause surprise.         


Author: christoph.grein
Date: Jun 11, 2008 05:30

What you request for A1 seems impossible: Store value 2 = 2#01# in one
bit.

An illustrating example:

type My_Int is range 10_000 .. 10_001;

type My_Record is record
I: My_Int;
end record;

for My_Record use record
I at 0 range 0 .. 0;
end record;

This is allowed. My_Record holds only two values, so one bit is
enough. The compiler uses a so-called biased representation with
10_000 as bias, which need not be stored.

Hope this helps.
no comments
Re: Enumeration representation clause surprise.         


Author: Markus Schoepflin
Date: Jun 11, 2008 05:56

christoph.grein@eurocopter.com wrote:
> What you request for A1 seems impossible: Store value 2 = 2#01# in one
> bit.

This is exactly my point. I would have expected a compile time error for
this impossible request.

Representation clauses are (amongst other) meant to specify specific binary
layouts for interfacing with the external world. Or am I mistaken here?
Show full article (1.04Kb)
no comments
Re: Enumeration representation clause surprise.         


Author: christoph.grein
Date: Jun 11, 2008 06:08

Hm, I gather you have detected a nice way to shoot yourself in the
foot :-)
no comments
Re: Enumeration representation clause surprise.         


Author: Samuel Tardieu
Date: Jun 11, 2008 06:28

Markus> This is exactly my point. I would have expected a compile time error
Markus> for this impossible request.

Markus> Representation clauses are (amongst other) meant to specify specific
Markus> binary layouts for interfacing with the external world. Or am I
Markus> mistaken here?

I can't find anything in RM chapter 13 which either forbids or allows
such a behaviour. I will submit a patch for GNAT which gives a new
warning for this case:

11. A1 at 0 range 0 .. 0; -- (*)
|
>>> warning: component representation will be biased

Sam
no comments
Re: Enumeration representation clause surprise.         


Author: Markus Schoepflin
Date: Jun 11, 2008 06:48

Samuel Tardieu wrote:
> Markus> This is exactly my point. I would have expected a compile time error
> Markus> for this impossible request.
>
> Markus> Representation clauses are (amongst other) meant to specify specific
> Markus> binary layouts for interfacing with the external world. Or am I
> Markus> mistaken here?
>
> I can't find anything in RM chapter 13 which either forbids or allows
> such a behaviour. I will submit a patch for GNAT which gives a new
> warning for this case:
>
> 11. A1 at 0 range 0 .. 0; -- (*)
> |
>>>> warning: component representation will be biased
>
> Sam

That would be extremely helpful!
Show full article (0.69Kb)
no comments
Re: Enumeration representation clause surprise.         


Author: Samuel Tardieu
Date: Jun 11, 2008 06:56

Sam> I will submit a patch for GNAT which gives a new warning for this
Sam> case

Markus> That would be extremely helpful!

Done, at http://gcc.gnu.org/ml/gcc-patches/2008-06/msg00680.html
no comments
Re: Enumeration representation clause surprise.         


Author: Adam Beneschan
Date: Jun 11, 2008 07:58

On Jun 11, 6:28 am, Samuel Tardieu rfc1149.net> wrote:
> Markus> This is exactly my point. I would have expected a compile time error
> Markus> for this impossible request.
>
> Markus> Representation clauses are (amongst other) meant to specify specific
> Markus> binary layouts for interfacing with the external world. Or am I
> Markus> mistaken here?
>
> I can't find anything in RM chapter 13 which either forbids or allows
> such a behaviour. I will submit a patch for GNAT which gives a new
> warning for this case:
>
> 11. A1 at 0 range 0 .. 0; -- (*)
> |
>>>> warning: component representation will be biased
Show full article (1.31Kb)
no comments
Re: Enumeration representation clause surprise.         


Author: Markus Schoepflin
Date: Jun 11, 2008 08:23

Adam Beneschan wrote:
> On Jun 11, 6:28 am, Samuel Tardieu rfc1149.net> wrote:
>> Markus> This is exactly my point. I would have expected a compile time error
>> Markus> for this impossible request.
>>
>> Markus> Representation clauses are (amongst other) meant to specify specific
>> Markus> binary layouts for interfacing with the external world. Or am I
>> Markus> mistaken here?
>>
>> I can't find anything in RM chapter 13 which either forbids or allows
>> such a behaviour. I will submit a patch for GNAT which gives a new
>> warning for this case:
>>
>> 11. A1 at 0 range 0 .. 0; -- (*)
>> |
>>>>> warning: component representation will be biased
>
> This seems odd to me. Having GNAT select a biased component in
> Christoph's example makes some sense, since a compiler can choose any
> representation it likes. But in Markus' case, he specifically asked ...
Show full article (1.51Kb)
no comments
Re: Enumeration representation clause surprise.         


Author: Samuel Tardieu
Date: Jun 11, 2008 08:56

Adam> This seems odd to me. Having GNAT select a biased component in
Adam> Christoph's example makes some sense, since a compiler can choose any
Adam> representation it likes. But in Markus' case, he specifically asked
Adam> for a certain representation for the enumeration types---should GNAT
Adam> take it upon itself to change that, and display a warning that doesn't
Adam> make it clear that it's disrespecting his request to have the
Adam> enumeration represented a certain way?
Adam>
Adam> I dunno... maybe this would be acceptable to some, and apparently
Adam> Markus thinks it's OK, but ... it's just not what I would expect ...
Adam> I guess it's a subjective thing.

I would prefer an error indeed, but I can't seem to find a clause in
chapter 13 which says that in records with representation clauses the
representation given for objects when created independently the
enumeration representation clause must be honored.

Anyway, the patch I submitted will probably be evaluated by AdaCore
language lawyers. If an error must be signalled, then the warning will
be transformed into one.

Sam
no comments
1 2 3 4 5