From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,1a6e940e9297b109 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!d4g2000prg.googlegroups.com!not-for-mail From: Maciej Sobczak Newsgroups: comp.lang.ada Subject: Re: Curiosity about rage checking Date: Thu, 7 Feb 2008 00:45:19 -0800 (PST) Organization: http://groups.google.com Message-ID: References: <29e89783-8802-474e-b3c7-9721407ce42e@s13g2000prd.googlegroups.com> NNTP-Posting-Host: 128.141.54.94 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1202373919 1367 127.0.0.1 (7 Feb 2008 08:45:19 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 7 Feb 2008 08:45:19 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: d4g2000prg.googlegroups.com; posting-host=128.141.54.94; posting-account=bMuEOQoAAACUUr_ghL3RBIi5neBZ5w_S User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:19724 Date: 2008-02-07T00:45:19-08:00 List-Id: On 7 Lut, 09:17, "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