comp.lang.ada
 help / color / mirror / Atom feed
From: Martin Krischik <krischik@users.sourceforge.net>
Subject: Re: range checking
Date: Fri, 04 Jul 2003 06:52:59 +0200
Date: 2003-07-04T06:52:59+02:00	[thread overview]
Message-ID: <1124504.87IONMPbAv@linux1.krischik.com> (raw)
In-Reply-To: Pine.SOL.4.44.0307021627430.15367-100000@tank.cs.uiuc.edu

Dinakar Dhurjati wrote:

> Hi,
> 
> I am a researcher in compilers/security.
> 
> I am looking to adapt Ada's subrange idea for eliminating some array
> bounds checks for a different language. I am curious as to how the thing
> works in common implementations. Any pointers welcome.
> 
> Specifically
> 
> (1) Are there any compilers which try to infer the subranges themselves
> Or the subranges have to be given by the programmer  ?

The compiler can deduct the range on assignment:

x : String (0 .. 10);
y : String := x;

Once y has x assigned to it the range is frozen and cannot be changed. The
size does not need to be known at compile time:

procedure x (Size : Integer)
is
    x : String (0 .. Size);
begin
    null;
end x;

> (2) If a variable is declared is of type some subrange, then every
> assignment to that variable needs to be checked for correctness -- i.e. if
> the assigned value is with in the subrange, right ? Are these checks done
> statically or at runtime ? Are there any compilers which try to reduce the
> amount of runtime checks that need to be done ?

The optimizer usualy analyzes the assignment and removed unneded checks. So
in x := x + 1; only the upper bound need to be checked.

The optimizer usualy will also make a static checks and issue a warning.
i.E.:

x : Interger := Integer'Last;
x := x + 1;

might raise a warning at compile time. However the language standart does
not enforce the existance of an optimizer.

With Regards

Martin

-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com




  reply	other threads:[~2003-07-04  4:52 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-07-02 22:07 range checking Dinakar Dhurjati
2003-07-04  4:52 ` Martin Krischik [this message]
2003-07-12 18:55 ` Nick Roberts
replies disabled

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