comp.lang.ada
 help / color / mirror / Atom feed
* setting 'Size and unchecked conversion safe here?
@ 2005-02-11 18:52 Marius Amado Alves
  2005-02-11 19:08 ` Martin Krischik
                   ` (2 more replies)
  0 siblings, 3 replies; 17+ messages in thread
From: Marius Amado Alves @ 2005-02-11 18:52 UTC (permalink / raw)
  To: comp.lang.ada

I need to store access-to-subprogram values in a 64-bit modular type. I 
write

    for My_Access_To_Subprogram_Type'Size use 64;

and GNAT warns me about 32 bits being unused. But Unchecked_Conversion 
between the access and the modular works fine. Now, is it working fine 
by chance or by language definition?

(I know how I can write it differently, but if the above is safe I'd 
rather leave it as it is.)

Thanks a lot.

(This happens in both Mac OS X and Windows XP.)




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

* Re: setting 'Size and unchecked conversion safe here?
  2005-02-11 18:52 setting 'Size and unchecked conversion safe here? Marius Amado Alves
@ 2005-02-11 19:08 ` Martin Krischik
  2005-02-11 20:34 ` tmoran
  2005-02-11 23:38 ` Robert A Duff
  2 siblings, 0 replies; 17+ messages in thread
From: Martin Krischik @ 2005-02-11 19:08 UTC (permalink / raw)


Marius Amado Alves wrote:

> I need to store access-to-subprogram values in a 64-bit modular type. I
> write
> 
>     for My_Access_To_Subprogram_Type'Size use 64;
> 
> and GNAT warns me about 32 bits being unused. But Unchecked_Conversion
> between the access and the modular works fine. Now, is it working fine
> by chance or by language definition?

In which case you probably have a 32 bit GNAT installed. Otherwise GNAT
would use all 64 bit and won't say a word.

> (I know how I can write it differently, but if the above is safe I'd
> rather leave it as it is.)

I don't know. The upper 32 bit are undefined - maybe not that good.

Martin

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




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

* Re: setting 'Size and unchecked conversion safe here?
  2005-02-11 18:52 setting 'Size and unchecked conversion safe here? Marius Amado Alves
  2005-02-11 19:08 ` Martin Krischik
@ 2005-02-11 20:34 ` tmoran
  2005-02-11 22:37   ` Marius Amado Alves
  2005-02-11 23:38 ` Robert A Duff
  2 siblings, 1 reply; 17+ messages in thread
From: tmoran @ 2005-02-11 20:34 UTC (permalink / raw)


>    for My_Access_To_Subprogram_Type'Size use 64;
>
>and GNAT warns me about 32 bits being unused. But Unchecked_Conversion
>between the access and the modular works fine. Now, is it working fine
>by chance or by language definition?
   ARM 13.9(5 .. 11) says
"If all of the following are true,
 ...
 S'Size = Target'Size
 ...
 Otherwise, the effect is implementation defined; ..."



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

* Re: setting 'Size and unchecked conversion safe here?
  2005-02-11 20:34 ` tmoran
@ 2005-02-11 22:37   ` Marius Amado Alves
  2005-02-11 23:49     ` tmoran
  2005-02-12  6:55     ` Martin Krischik
  0 siblings, 2 replies; 17+ messages in thread
From: Marius Amado Alves @ 2005-02-11 22:37 UTC (permalink / raw)
  To: tmoran; +Cc: comp.lang.ada

>    ARM 13.9(5 .. 11) says
> "If all of the following are true,
>  ...
>  S'Size = Target'Size

I know. This happens, because I forced the access type to be 64-bit, 
same as the modular. What I'm not sure about is whether the unused 32 
bits are harmless or not (by definition). Thanks.




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

* Re: setting 'Size and unchecked conversion safe here?
  2005-02-11 18:52 setting 'Size and unchecked conversion safe here? Marius Amado Alves
  2005-02-11 19:08 ` Martin Krischik
  2005-02-11 20:34 ` tmoran
@ 2005-02-11 23:38 ` Robert A Duff
  2005-02-12 12:46   ` Marius Amado Alves
  2 siblings, 1 reply; 17+ messages in thread
From: Robert A Duff @ 2005-02-11 23:38 UTC (permalink / raw)


Marius Amado Alves <amado.alves@netcabo.pt> writes:

> I need to store access-to-subprogram values in a 64-bit modular type.

Why do you want to do that?

>... I
> write
> 
>     for My_Access_To_Subprogram_Type'Size use 64;
> 
> and GNAT warns me about 32 bits being unused. But Unchecked_Conversion
> between the access and the modular works fine. Now, is it working fine
> by chance or by language definition?

The language certainly does not define this to work.  It might work on
some (all?) implementations.

What do you mean by "works fine"?  Values convert from one to the other
and back, and you get what you started with?

- Bob



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

* Re: setting 'Size and unchecked conversion safe here?
  2005-02-11 22:37   ` Marius Amado Alves
@ 2005-02-11 23:49     ` tmoran
  2005-02-12 13:10       ` Marius Amado Alves
  2005-02-12  6:55     ` Martin Krischik
  1 sibling, 1 reply; 17+ messages in thread
From: tmoran @ 2005-02-11 23:49 UTC (permalink / raw)


> What I'm not sure about is whether the unused 32
> bits are harmless or not (by definition). Thanks.
  An Unchecked_Conversion of dissimilar sizes is clearly unsafe, simply
because it's implementation defined how, or if, it works.
  But your problem appears to be the warning on
    for My_Access_To_Subprogram_Type'Size use 64;
ARM 13.3(42-43) recommends that a compiler should support this.
Presumably you don't care how the compiler places the 32 useful bits in
the 64 bit slot, so long as you get out what you put in.  So another
compiler might do it differently, but your program would still work
correctly.  So presumably the Gnat warning should be taken in the spirit
of "I notice you did this weird thing - are you sure you really meant to?"



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

* Re: setting 'Size and unchecked conversion safe here?
  2005-02-11 22:37   ` Marius Amado Alves
  2005-02-11 23:49     ` tmoran
@ 2005-02-12  6:55     ` Martin Krischik
  2005-02-12 13:30       ` Marius Amado Alves
  1 sibling, 1 reply; 17+ messages in thread
From: Martin Krischik @ 2005-02-12  6:55 UTC (permalink / raw)


Marius Amado Alves wrote:

>>    ARM 13.9(5 .. 11) says
>> "If all of the following are true,
>>  ...
>>  S'Size = Target'Size
> 
> I know. This happens, because I forced the access type to be 64-bit,
> same as the modular. What I'm not sure about is whether the unused 32
> bits are harmless or not (by definition). Thanks.

There are not harmless since there is no requironment to set them to
16#0000000#.

In order to help you we have to know why it is that the access is 32 bit and
the modular 64. I can only guess it is for some compatiblity reason - but
what do you need to keep compatible?

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




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

* Re: setting 'Size and unchecked conversion safe here?
  2005-02-11 23:38 ` Robert A Duff
@ 2005-02-12 12:46   ` Marius Amado Alves
  2005-02-12 18:05     ` tmoran
  2005-02-12 19:14     ` Robert A Duff
  0 siblings, 2 replies; 17+ messages in thread
From: Marius Amado Alves @ 2005-02-12 12:46 UTC (permalink / raw)
  To: Robert A Duff; +Cc: comp.lang.ada


On 11 Feb 2005, at 23:38, Robert A Duff wrote:

> Marius Amado Alves <amado.alves@netcabo.pt> writes:
>
>> I need to store access-to-subprogram values in a 64-bit modular type.
>
> Why do you want to do that?

That's the container I have to store small values. It's in a database 
system called Mneson. Mneson is based on a graph. Vertices are 
represented thusly:

    type Vertex is record
       Tip : Natural_16;
       Cue : Modular_64;
    end record;
    for Vertex'Size use 80;
    pragma Pack (Vertex);

Small values are stored directly in the Cue. Mneson is my baby, so I 
can change it if necessary, but as I said I'd rather leave it that way.

> What do you mean by "works fine"?  Values convert from one to the other
> and back, and you get what you started with?

"Works fine" means the referenced subprograms are correctly reaccessed. 
But I don't know if the unused bits stay the same. That's what I'd like 
to know. Or if there is some means to ensure that.




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

* Re: setting 'Size and unchecked conversion safe here?
  2005-02-11 23:49     ` tmoran
@ 2005-02-12 13:10       ` Marius Amado Alves
  0 siblings, 0 replies; 17+ messages in thread
From: Marius Amado Alves @ 2005-02-12 13:10 UTC (permalink / raw)
  To: tmoran; +Cc: comp.lang.ada


On 11 Feb 2005, at 23:49, tmoran@acm.org wrote:

>> What I'm not sure about is whether the unused 32
>> bits are harmless or not (by definition). Thanks.
>   An Unchecked_Conversion of dissimilar sizes is clearly unsafe, simply
> because it's implementation defined how, or if, it works.

Strictly they have the same size, because I said so with Size clauses.

>   But your problem appears to be the warning on
>     for My_Access_To_Subprogram_Type'Size use 64;
> ARM 13.3(42-43) recommends that a compiler should support this.
> Presumably you don't care how the compiler places the 32 useful bits in
> the 64 bit slot, so long as you get out what you put in.

Actually I'd be happier if the unused bits could be forced to have a 
specified value, say all zeros, or all ones. To ensure a one-to-one 
mapping between the original access values and their representation 
with the extra bits. Would pragma Normalize_Scalars do the trick? I'd 
rather avoid it, because it has partition-wide effect, which is 
overkill for the case at hand, and so might even interfere with other 
parts of the program.

>   So another
> compiler might do it differently, but your program would still work
> correctly.

Excelent. That was my expectation. Thanks a lot.




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

* Re: setting 'Size and unchecked conversion safe here?
  2005-02-12  6:55     ` Martin Krischik
@ 2005-02-12 13:30       ` Marius Amado Alves
  2005-02-12 14:57         ` Martin Krischik
  0 siblings, 1 reply; 17+ messages in thread
From: Marius Amado Alves @ 2005-02-12 13:30 UTC (permalink / raw)
  To: martin; +Cc: comp.lang.ada


On 12 Feb 2005, at 06:55, Martin Krischik wrote:

> Marius Amado Alves wrote:
>
>>>    ARM 13.9(5 .. 11) says
>>> "If all of the following are true,
>>>  ...
>>>  S'Size = Target'Size
>>
>> I know. This happens, because I forced the access type to be 64-bit,
>> same as the modular. What I'm not sure about is whether the unused 32
>> bits are harmless or not (by definition). Thanks.
>
> There are not harmless since there is no requironment to set them to
> 16#0000000#.

If there were such a requirement is there a simple way to specify it?

> In order to help you we have to know why it is that the access is 32 
> bit and
> the modular 64. I can only guess it is for some compatiblity reason - 
> but
> what do you need to keep compatible?

The access type is 32 bits because the compiler says so.
The modular 64 is my choice to represent small values (integers, 
floats, tiny strings, access values) in the vertices of a Mneson graph:

    type Vertex is record
       Tip : Natural_16;
       Cue : Modular_64;
    end record;
    for Vertex'Size use 80;
    pragma Pack (Vertex);

I know this might be incompatible with some unusual architectures but 
I'm prepared to deal with that when and if the time comes.




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

* Re: setting 'Size and unchecked conversion safe here?
  2005-02-12 13:30       ` Marius Amado Alves
@ 2005-02-12 14:57         ` Martin Krischik
  2005-02-12 16:03           ` Marius Amado Alves
  2005-02-12 16:04           ` Marius Amado Alves
  0 siblings, 2 replies; 17+ messages in thread
From: Martin Krischik @ 2005-02-12 14:57 UTC (permalink / raw)


Marius Amado Alves wrote:

> 
> On 12 Feb 2005, at 06:55, Martin Krischik wrote:
> 
>> Marius Amado Alves wrote:
>>
>>>>    ARM 13.9(5 .. 11) says
>>>> "If all of the following are true,
>>>>  ...
>>>>  S'Size = Target'Size
>>>
>>> I know. This happens, because I forced the access type to be 64-bit,
>>> same as the modular. What I'm not sure about is whether the unused 32
>>> bits are harmless or not (by definition). Thanks.
>>
>> There are not harmless since there is no requironment to set them to
>> 16#0000000#.

> If there were such a requirement is there a simple way to specify it?

A combination of checked and unchecked convertions. First an unchecked
convertion to an access compatible modular type:

http://en.wikibooks.org/wiki/Programming:Ada:Types:access#where_is_void.2A.3F

then a checked convertion to the 64 bit type.
 
>> In order to help you we have to know why it is that the access is 32
>> bit and
>> the modular 64. I can only guess it is for some compatiblity reason -
>> but
>> what do you need to keep compatible?
> 
> The access type is 32 bits because the compiler says so.
> The modular 64 is my choice to represent small values (integers,
> floats, tiny strings, access values) in the vertices of a Mneson graph:
> 
>     type Vertex is record
>        Tip : Natural_16;
>        Cue : Modular_64;
>     end record;
>     for Vertex'Size use 80;
>     pragma Pack (Vertex);
> 
> I know this might be incompatible with some unusual architectures but
> I'm prepared to deal with that when and if the time comes.

I would use a variant record:

http://en.wikibooks.org/wiki/Programming:Ada:Types:record#Variant_Record

But that's is just me - and may not be suitable for your database.

Martin

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




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

* Re: setting 'Size and unchecked conversion safe here?
  2005-02-12 14:57         ` Martin Krischik
@ 2005-02-12 16:03           ` Marius Amado Alves
  2005-02-12 16:04           ` Marius Amado Alves
  1 sibling, 0 replies; 17+ messages in thread
From: Marius Amado Alves @ 2005-02-12 16:03 UTC (permalink / raw)
  To: martin; +Cc: comp.lang.ada

> I would use a variant record...

I though of that, but was afraid it would make exportation to C harder. 
I want to be able to export to C easily.




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

* Re: setting 'Size and unchecked conversion safe here?
  2005-02-12 14:57         ` Martin Krischik
  2005-02-12 16:03           ` Marius Amado Alves
@ 2005-02-12 16:04           ` Marius Amado Alves
  1 sibling, 0 replies; 17+ messages in thread
From: Marius Amado Alves @ 2005-02-12 16:04 UTC (permalink / raw)
  To: martin; +Cc: comp.lang.ada

>>>
>>> There are not harmless since there is no requironment to set them to
>>> 16#0000000#.
>
>> If there were such a requirement is there a simple way to specify it?
>
> A combination of checked and unchecked convertions. First an unchecked
> convertion to an access compatible modular type:
>
> http://en.wikibooks.org/wiki/Programming:Ada:Types: 
> access#where_is_void.2A.3F
>
> then a checked convertion to the 64 bit type.

I'll check :-)
Thanks.




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

* Re: setting 'Size and unchecked conversion safe here?
  2005-02-12 12:46   ` Marius Amado Alves
@ 2005-02-12 18:05     ` tmoran
  2005-02-12 18:29       ` Marius Amado Alves
  2005-02-12 19:14     ` Robert A Duff
  1 sibling, 1 reply; 17+ messages in thread
From: tmoran @ 2005-02-12 18:05 UTC (permalink / raw)


> >> I need to store access-to-subprogram values in a 64-bit modular type.
> That's the container I have to store small values. It's in a database system
    That database is not persistent from one program execution to the
next, surely.  Is there any reason to believe an access to subprogram P in
one execution has the same 32 bit representation as an access to P in
another execution?



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

* Re: setting 'Size and unchecked conversion safe here?
  2005-02-12 18:05     ` tmoran
@ 2005-02-12 18:29       ` Marius Amado Alves
  0 siblings, 0 replies; 17+ messages in thread
From: Marius Amado Alves @ 2005-02-12 18:29 UTC (permalink / raw)
  To: tmoran; +Cc: comp.lang.ada

>>>> I need to store access-to-subprogram values in a 64-bit modular 
>>>> type.
>> That's the container I have to store small values. It's in a database 
>> system
>     That database is not persistent from one program execution to the
> next, surely.  Is there any reason to believe an access to subprogram 
> P in
> one execution has the same 32 bit representation as an access to P in
> another execution?

No, of course not. The access-to-subprogram values are redefined upon 
each restart of the system. It's a long story. Basically my PhD. Due in 
2006 or 2007. An interim paper will be published in 2005. I'll take the 
liberty to announce it here.




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

* Re: setting 'Size and unchecked conversion safe here?
  2005-02-12 12:46   ` Marius Amado Alves
  2005-02-12 18:05     ` tmoran
@ 2005-02-12 19:14     ` Robert A Duff
  2005-02-12 19:48       ` Marius Amado Alves
  1 sibling, 1 reply; 17+ messages in thread
From: Robert A Duff @ 2005-02-12 19:14 UTC (permalink / raw)


Marius Amado Alves <amado.alves@netcabo.pt> writes:

> On 11 Feb 2005, at 23:38, Robert A Duff wrote:
> 
> > Marius Amado Alves <amado.alves@netcabo.pt> writes:
> >
> >> I need to store access-to-subprogram values in a 64-bit modular type.
> >
> > Why do you want to do that?
> 
> That's the container I have to store small values. It's in a database
> system called Mneson. Mneson is based on a graph. Vertices are
> represented thusly:
> 
>     type Vertex is record
>        Tip : Natural_16;
>        Cue : Modular_64;
>     end record;
>     for Vertex'Size use 80;
>     pragma Pack (Vertex);
> 
> Small values are stored directly in the Cue. Mneson is my baby, so I can
> change it if necessary, but as I said I'd rather leave it that way.
> 
> > What do you mean by "works fine"?  Values convert from one to the other
> > and back, and you get what you started with?
> 
> "Works fine" means the referenced subprograms are correctly
> reaccessed. But I don't know if the unused bits stay the same. That's
> what I'd like to know. Or if there is some means to ensure that.

If the unused bits are ignored, then you don't care whether they stay
the same.  I suspect it will work on most compilers (within a single run
of the program), but there's no guarantee.

Why not do somthing like this:

    X: Acc_To_Subp;
    type Modular_32 is mod 2**32;
    function Cast is new Unchecked_Conversion(Acc_To_Subp, Modular_32);
    V: constant Vertex := (Tip => 0, Cue => Modular_64(Cast(X)));

?  Of course, this has to be different on a 64-bit machine,
if Acc_To_Subp is 64 bits.  But on a 32-bit machine, it seems
like a shame to waste 32 bits in every acc-to-subp value
(I mean, when they're *not* stored in a Vertex).

I presume that are many access-to-subprogram types, normally declared
*after* type Vertex.  If there's just one (or a small fixed number), you
could use a variant record.  See Unchecked_Union for C compatibility.

- Bob



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

* Re: setting 'Size and unchecked conversion safe here?
  2005-02-12 19:14     ` Robert A Duff
@ 2005-02-12 19:48       ` Marius Amado Alves
  0 siblings, 0 replies; 17+ messages in thread
From: Marius Amado Alves @ 2005-02-12 19:48 UTC (permalink / raw)
  To: Robert A Duff; +Cc: comp.lang.ada

"A combination of checked and unchecked convertions. First an unchecked
convertion to an access compatible modular type." (Martin)

>     X: Acc_To_Subp;
>     type Modular_32 is mod 2**32;
>     function Cast is new Unchecked_Conversion(Acc_To_Subp, Modular_32);
>     V: constant Vertex := (Tip => 0, Cue => Modular_64(Cast(X)));
(Bob)

This is a great idea. Bob's example made me understand Martin's 
suggestion. I think I'll do this. Thanks a lot.

> ... you
> could use a variant record.  See Unchecked_Union for C compatibility.

I'll check. Thanks.




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

end of thread, other threads:[~2005-02-12 19:48 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-02-11 18:52 setting 'Size and unchecked conversion safe here? Marius Amado Alves
2005-02-11 19:08 ` Martin Krischik
2005-02-11 20:34 ` tmoran
2005-02-11 22:37   ` Marius Amado Alves
2005-02-11 23:49     ` tmoran
2005-02-12 13:10       ` Marius Amado Alves
2005-02-12  6:55     ` Martin Krischik
2005-02-12 13:30       ` Marius Amado Alves
2005-02-12 14:57         ` Martin Krischik
2005-02-12 16:03           ` Marius Amado Alves
2005-02-12 16:04           ` Marius Amado Alves
2005-02-11 23:38 ` Robert A Duff
2005-02-12 12:46   ` Marius Amado Alves
2005-02-12 18:05     ` tmoran
2005-02-12 18:29       ` Marius Amado Alves
2005-02-12 19:14     ` Robert A Duff
2005-02-12 19:48       ` Marius Amado Alves

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