comp.lang.ada
 help / color / mirror / Atom feed
From: Maciej Sobczak <see.my.homepage@gmail.com>
Subject: Re: Curiosity about rage checking
Date: Thu, 7 Feb 2008 00:45:19 -0800 (PST)
Date: 2008-02-07T00:45:19-08:00	[thread overview]
Message-ID: <b654c899-df29-40b9-aaed-3f6b8d9fa94b@d4g2000prg.googlegroups.com> (raw)
In-Reply-To: 29e89783-8802-474e-b3c7-9721407ce42e@s13g2000prd.googlegroups.com

On 7 Lut, 09:17, "framefri...@gmail.com" <framefri...@gmail.com>
wrote:

> Consider the following piece of code
>
> for I in A..B loop                                         --
> <--------------------+
>     Put_Line("Assigning entry for I=" & Integer'Image(I));
> --                        |
>
> --                        |
>     X(I) := 0;    --  <== Access checking for this assignement could
> be moved  here--+
> end loop
>
> As explained in the fancy comment (I hope you are seeing it with a
> fixed-size font :-)

Fixed-size font is one thing, the fixed-length display system is
another. :-)

> the check for
> index overflow in the access to X could be done _outside_ the for
> loop, saving some computational
> effort.

Yes, but see below.

> My question is: is an Ada compiler allowed to move the check outside
> the loop?

It allowed to do so if it can prove that the check will never fail
inside the loop, in which case the programmer cannot tell the
difference.

>   1. The saving is not very important

It is!

>   2. Moving the check outside the loop changes the program behaviour

In which case it should not be done.
If the compiler can prove statically that A and B are withing the
range of  X's index type, then the behaviour does *not* change (all
checks are OK anyway). Otherwise the compiler cannot do any
optimization.

> However, the
> program is buggy anyway

Why? Exceptions are language events that not necessarily indicate
errors in the sense of not meeting the specification of the system.
Progammer might actually *expect* the exception to be risen.

In this context, optimizations can be reasonably applied only when the
compiler can prove that the visible behaviour (except performance)
will not change. [*]

There are, however, some places where it makes sense to optimize with
changing the visible behaviour - for example eliminating the
intermediate object that is used in the assignment of controlled types
makes sense even though it can lead to different behaviour.

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com



  reply	other threads:[~2008-02-07  8:45 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-07  8:17 Curiosity about rage checking framefritti
2008-02-07  8:45 ` Maciej Sobczak [this message]
2008-02-07  9:55 ` Jean-Pierre Rosen
2008-02-07 10:36   ` framefritti
2008-02-07 11:21     ` Dmitry A. Kazakov
2008-02-07 15:57   ` Adam Beneschan
2008-02-07 16:45 ` Robert A Duff
2008-02-07 16:54 ` Adam Beneschan
replies disabled

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