comp.lang.ada
 help / color / mirror / Atom feed
* Re: Rules for Representation of Subtypes
@ 1996-09-28  0:00 Robert Dewar
  1996-09-30  0:00 ` Keith Thompson
  0 siblings, 1 reply; 61+ messages in thread
From: Robert Dewar @ 1996-09-28  0:00 UTC (permalink / raw)




Keith said

"(Publicly disputing Robert Dewar is always dangerous.  Fortunately it's
a bounded error; the effect is limited to learning something.)"

Actually Keith has a pretty good track record in such disputes, he is
seldom wrong :-)

Well in fact I don't see a significant dipute here, I agree with everything
Keith said. The one interesting additional piece of information is that

there is another compiler besides Intermetrics that made Natural'Size 31.
I certainly am aware that Alsys makde Natural'Size be 31, and I am
(painfully, because it causes some compatibility problems for some of
our customers) aware that Verdix made Natural'Size 32. Keith for interest
which compiler made natural'Size 31? It's interesting to know, since it
means that the Ada 95 decision is more justified (i.e. we had a chaotic
non-portable situation in Ada 83, and Ada 95 eliminated the non-portability,
but was bound to cause some incompatibilities for certain implementations
when it did so.






^ permalink raw reply	[flat|nested] 61+ messages in thread
* Re: Rules for Representation of Subtypes
@ 1996-10-10  0:00 W. Wesley Groleau (Wes)
  1996-10-10  0:00 ` Robert Dewar
  1996-10-11  0:00 ` Ken Garlington
  0 siblings, 2 replies; 61+ messages in thread
From: W. Wesley Groleau (Wes) @ 1996-10-10  0:00 UTC (permalink / raw)



The opinion was expressed that the generic formals for unchecked_conversion
have to be the same size or at least source smaller than target.  I choose
not to comment on the truth or falsehood of that opinion, but I offer the
behavior of three vendors (Ada-83 except gnat):

Apex on SPARC:  If source is larger than target, the warning message in
the semantic phase is that they are not the same size.  However, you do not
see the warnings unless there are also errors to justify opening the
message window.

Alsys on HP RISC:  NO diagnostic at all if default compiler settings are
used.  In the one case I tested, the code also seemed to work, i.e. the
lost information just happened to not be needed.  Since I did not decide
whether to use the default settings, I haven't bothered to try others.

GNAT: Code rejected when source and target were not the same size.
I've forgotten which was larger.

I haven't used Verdix in 18 months, but if I remember right, it allowed
source to be larger than target WITH a warning.  Similar memory for
VAX Ada, though I haven't used that in five years.  Unfortunately, many
coders suppress warnings because "there are so @$#^$%^*&$ many of them,
they just hide the real problems."

---------------------------------------------------------------------------
W. Wesley Groleau (Wes)                                Office: 219-429-4923
Hughes Defense Communications (MS 10-40)                 Home: 219-471-7206
Fort Wayne,  IN   46808                  (Unix): wwgrol@pseserv3.fw.hac.com
---------------------------------------------------------------------------




^ permalink raw reply	[flat|nested] 61+ messages in thread
* Re: Rules for Representation of Subtypes
@ 1996-10-03  0:00 Franco Mazzanti
  1996-10-03  0:00 ` Robert A Duff
  0 siblings, 1 reply; 61+ messages in thread
From: Franco Mazzanti @ 1996-10-03  0:00 UTC (permalink / raw)



Robert A Duff wrote:

> >generic
> >   type Source(<>) is limited private;
> >   type Target is (<>);
> >function Checked_Scalar_Conversion (S:Source) return Target; 
> >
> >
> >with Unchecked_Conversion;
> >function Checked_Scalar_Conversion (S : Source) return Target is
> >   type My_Rec is record
> >      Scalar : Target;
> 
> That's illegal, since Target has unknown discrims.  But other than that,
> this approach will work.
> 
> But, you don't really need to go to all that trouble.  If you just make
> sure your integer types match the hardware (like -2**31..2**31-1 or
> whatever), then unchecked conv of integers will tend to work just fine.
>  ...
>  ...
> 
> - Bob

illegal? isn't Target required to be just a discrete type [RM 12.5.2(2)]?
If I am wrong, is this a GNAT bug (since it compiles without problem)?

Notice that all this trouble might be needed if I want my program to be
as far as possible implementation-independent, or if I want to look for
same mechanically verifiable (as far as possible implementation-independent)
safe coding guidelines (without going to disallow all unchecked conversions).

Franco




^ permalink raw reply	[flat|nested] 61+ messages in thread
* Re: Rules for Representation of Subtypes
@ 1996-10-02  0:00 Franco Mazzanti
  1996-10-03  0:00 ` Robert A Duff
  0 siblings, 1 reply; 61+ messages in thread
From: Franco Mazzanti @ 1996-10-02  0:00 UTC (permalink / raw)



Larry wrote:
> So can someone give an example of how Z'Valid _might_ be useful
> after an unchecked conversion, as seems to be indicated by the
> Reference Manual.

Bob wrote:
> If you unchecked_convert to a record (which is the usual case), then
> you can usefully use 'Valid on the components.  Be sure that the record
> doesn't contain fancy stuff (details in the RM), but a record containing
> scalar fields and constrained arrays will work. 


Then the following should work too ...

generic
   type Source(<>) is limited private;
   type Target is (<>);
function Checked_Scalar_Conversion (S:Source) return Target; 


with Unchecked_Conversion;
function Checked_Scalar_Conversion (S : Source) return Target is
   type My_Rec is record
      Scalar : Target;
   end record;
   Tmp : My_Rec;
   My_Scalar: Target renames Tmp.Scalar;
   -- small trick: even if Tmp as a whole record becomes abnormal
   -- its scalar component can still be safely checked for validity
   -- without "using" the abnormal record object as a prefix.
   function Unchecked_Cvt is new Unchecked_Conversion (Source, My_Rec);
begin
   if My_Rec'Size /= Source'Size or
        My_rec'Alignment /= Source'Alignment then
      raise Program_Error;
   end if;
   Tmp := Unchecked_Cvt (S);
   if My_Scalar'Valid then
      return My_Scalar;
   else
      raise Program_Error;
   end if;
end Checked_Scalar_Conversion; 


At least, when I tried this with gnat it seemed to work.
The following is a simple test program:

with Unchecked_Conversion;
with Checked_Scalar_Conversion;
procedure Main is
   type T is access Integer;
   for T'Size use 32;
   type My_Scalar is new Integer range 1 .. Integer'Last;
   for My_Scalar'Size use 32;
   type My_Scalar2 is new Integer range 1 .. 10;
   for My_Scalar2'Size use 32;
   function Cvt is new Checked_Scalar_Conversion (T, My_Scalar);
   function Cvt is new Checked_Scalar_Conversion (T, My_Scalar2);
   function Cvt is new Unchecked_Conversion (T, Integer);
   Ptr : T := new Integer;
   I : Integer;
   N : My_Scalar;
   S : My_Scalar2;
begin
   I := Cvt (Ptr);                          -- safely converts
   Text_Io.Put_Line (Integer'Image(I));
   N := Cvt (Ptr);                          -- safely converts
   Text_Io.Put_Line (My_Scalar'Image(N));
   S := Cvt (Ptr);                          -- raises Program_Error
   Text_Io.Put_Line (My_Scalar2'Image(S));
end Main; 

Actually, at a rigorous reading, the behavior has just changed from
"erroneous" [RM 13.9.1(12)]" to "implementation-defined" [RM 13.9 (10,
11)]. And "implementation-defined" may still include the possibility of
"erroneousness". But hopefully this not likely not to be the common case.

Franco 

----------------
Franco Mazzanti    <mazzanti@iei.pi.cnr.it>




^ permalink raw reply	[flat|nested] 61+ messages in thread
* Re: Rules for Representation of Subtypes
@ 1996-09-28  0:00 Robert Dewar
  1996-09-29  0:00 ` Robert A Duff
  0 siblings, 1 reply; 61+ messages in thread
From: Robert Dewar @ 1996-09-28  0:00 UTC (permalink / raw)



Bob Duff said

">BTW: Why doesn't Ada have a 'Storage_Size clause for (non-access) types or
>objects?

It should."


I am completely puzzled, you can specify the size of types and the size of
objects, what on earth woul it mean to specify Storage_Size for an array
(as opposed to specifying the type or object size for the array).






^ permalink raw reply	[flat|nested] 61+ messages in thread
* Re: Rules for Representation of Subtypes
@ 1996-09-28  0:00 Robert Dewar
  1996-09-29  0:00 ` Robert A Duff
  0 siblings, 1 reply; 61+ messages in thread
From: Robert Dewar @ 1996-09-28  0:00 UTC (permalink / raw)




Bob Duff said

"But this is all very obscure.  If you're interfacing to hardware, or to
C, or to something else where the interface is a low-level binary
interface, the best thing to do is make the types match the hardware, or
the C, or whatever it is.  Don't use constraints on the Ada side of the
interface, just because the logical properties would warrant a
constraint.
"

If you are using GNAT, you need not worry about this. We found that so
many users were depending on objects of a subtype being the same as
objects of the base type that it was essential to do this. If you use
a compiler that does NOT have this convention with existing Ada 83
code, our experience is that you will likely run into troubles.

At the very least, I think that a compiler should regard a subtype
object as having  the same size as the base type for convention C.

i.e. the implementatiojn advice in the RM to squeeze things down is
actively undesirable for pragma foreign conventions where this would
not be done by the foriegn language.






^ permalink raw reply	[flat|nested] 61+ messages in thread
* Rules for Representation of Subtypes
@ 1996-09-22  0:00 Matthew Heaney
  1996-09-23  0:00 ` Robert A Duff
  1996-09-23  0:00 ` David C. Hoos, Sr.
  0 siblings, 2 replies; 61+ messages in thread
From: Matthew Heaney @ 1996-09-22  0:00 UTC (permalink / raw)



I've always been curious about the rules for representation of subtypes. 
For example, if I make a declaration like this:

   A_Small_Integer : Integer range 0 .. 255;  -- assume type Integer is 4 bytes

then is an implementation allowed to use fewer than 4 bytes to represent
A_Small_Integer?

Suppose I'm on a Unix system and I do this

   read (fd, A_Small_Integer'Address, 4);

If the representation of A_Small_Integer is only 1 byte, then obviously I'm
in trouble.  Am I required to specify the size of objects?

   A_Small_Integer : Integer range 0 .. 255;
   for A_Small_Integer'Size use 32;                     -- required?

matt
mheaney@ni.net

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
mheaney@ni.net
(818) 985-1271




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

end of thread, other threads:[~1996-10-11  0:00 UTC | newest]

Thread overview: 61+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-09-28  0:00 Rules for Representation of Subtypes Robert Dewar
1996-09-30  0:00 ` Keith Thompson
  -- strict thread matches above, loose matches on Subject: below --
1996-10-10  0:00 W. Wesley Groleau (Wes)
1996-10-10  0:00 ` Robert Dewar
1996-10-11  0:00 ` Ken Garlington
1996-10-03  0:00 Franco Mazzanti
1996-10-03  0:00 ` Robert A Duff
1996-10-02  0:00 Franco Mazzanti
1996-10-03  0:00 ` Robert A Duff
1996-09-28  0:00 Robert Dewar
1996-09-29  0:00 ` Robert A Duff
1996-09-29  0:00   ` Matthew Heaney
1996-09-29  0:00   ` Robert Dewar
1996-09-30  0:00     ` Art Schwarz
1996-09-30  0:00       ` Robert A Duff
1996-10-01  0:00       ` Larry Kilgallen
1996-10-01  0:00         ` Brian R. Hanson
1996-10-01  0:00         ` Robert A Duff
1996-09-28  0:00 Robert Dewar
1996-09-29  0:00 ` Robert A Duff
1996-09-29  0:00   ` Matthew Heaney
1996-09-22  0:00 Matthew Heaney
1996-09-23  0:00 ` Robert A Duff
1996-09-24  0:00   ` Matthew Heaney
1996-09-26  0:00     ` Robert A Duff
1996-09-26  0:00       ` Larry Kilgallen
1996-09-27  0:00         ` Robert A Duff
1996-09-27  0:00           ` Mark A Biggar
1996-09-30  0:00             ` Robert A Duff
1996-10-01  0:00               ` Larry Kilgallen
1996-10-01  0:00                 ` Samuel Tardieu
1996-10-01  0:00                   ` Ken Garlington
1996-10-01  0:00                 ` Robert A Duff
1996-09-28  0:00           ` Larry Kilgallen
1996-09-29  0:00             ` Robert A Duff
1996-09-29  0:00               ` Matthew Heaney
1996-09-30  0:00                 ` Robert A Duff
1996-09-30  0:00                 ` Robert Dewar
1996-09-30  0:00                   ` Matthew Heaney
1996-09-29  0:00               ` Larry Kilgallen
1996-09-29  0:00                 ` Matthew Heaney
1996-09-30  0:00                 ` Robert A Duff
1996-10-01  0:00                   ` Ken Garlington
1996-10-02  0:00                     ` Robert A Duff
1996-10-02  0:00                       ` Ken Garlington
1996-10-06  0:00                   ` Robert Dewar
1996-10-03  0:00             ` Robert Dewar
1996-09-27  0:00       ` Matthew Heaney
1996-09-27  0:00         ` Robert A Duff
1996-09-23  0:00 ` David C. Hoos, Sr.
1996-09-23  0:00   ` Robert A Duff
1996-09-23  0:00   ` Samuel T. Harris
1996-09-26  0:00     ` David C. Hoos, Sr.
1996-09-24  0:00   ` Robert Dewar
1996-09-26  0:00     ` Keith Thompson
1996-09-26  0:00       ` Matthew Heaney
1996-09-27  0:00         ` Robert A Duff
1996-09-27  0:00           ` Robert Dewar
1996-09-27  0:00         ` Robert Dewar
1996-09-27  0:00       ` Robert A Duff
1996-09-24  0:00   ` Robert Dewar

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