This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Ada subtypes and base types


On Tuesday 14 March 2006 03:16, Waldek Hebisch wrote:
> Jeffrey A Law wrote:
> > On Mon, 2006-02-27 at 20:08 +0100, Waldek Hebisch wrote:
> > 
> > > What do you mean by "abuse"?  TYPE_MAX_VALUE means maximal value
> > > allowed by given type.
> > As long as you're *absolutely* clear that  a variable with a
> > restricted range can never hold a value outside that the
> > restricted range in a conforming program, then I'll back off
> > the "abuse" label and merely call it pointless :-)
> > 
> > The scheme you're using "promoting" to a base type before all
> > arithmetic creates lots of useless type conversions and means
> > that the optimizers can't utilize TYPE_MIN_VALUE/TYPE_MAX_VALUE
> > anyway.  ie, you don't gain anything over keeping that knowledge
> > in the front-end.
> > 
> 
> Pascal arithmetic essentially is untyped: operators take integer
> arguments and are supposed to give mathematically correct result
> (provided all intermediate results are representable in machine
> arithmetic, overflow is treated as user error). OTOH for proper
> type checking front end have to track ranges associated to
> variables. So "useless" type conversions are needed due to
> Pascal standard and backend constraints.
> 
> I think that it is easy for back end to make good use of
> TYPE_MIN_VALUE/TYPE_MAX_VALUE. Namely, consider the assignment
> 
> x := y + z * w;
> 
> where variables y, z and w have values in the interval [0,7] and
> x have values in [0,1000]. Pascal converts the above to the
> following C like code:
> 
> int tmp = (int) y + (int) z * (int) w;
> x = (tmp < 0 || tmp > 1000)? (Range_Check_Error (), 0) : tmp;
>
> I expect VRP to deduce that tmp will have values in [0..56] and
> eliminate range check.

This is much the same in Ada.  However the Ada runtime and compiler
are compiled using a special flag (-gnatp) that turns all checks off.
This is not conformant with Ada semantics.  If you look at what the
front end is generating during a bootstrap, you therefore see it
happily converting between types and base types, and completely ignoring
the possibility of out-of-range values.  Someone inspecting the output
of the front-end during a bootstrap could well wonder why it bothers setting
TYPE_MIN_VALUE/TYPE_MAX_VALUE, and what the point of all the conversions
to and from base types is.  The point is that usually there would be
range checks all over the place as in Waldek's example, but they have
been suppressed.

> Also, it should be clear that in the 
> assigment above artithmetic can be done using any convenient
> precision.
> 
> In principle Pascal front end could deduce more precise types (ranges),
> but that would create some extra type conversions and a lot
> of extra types. Moreover, I assume that VRP can do better job
> at tracking ranges then Pascal front end.

Duncan.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]