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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,ed872c72866dab2b X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,CP1252 Path: g2news2.google.com!postnews.google.com!y27g2000prb.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: expression function bug or think? Date: Thu, 16 Jun 2011 08:56:32 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: <678085105329914667.504682rmhost.bauhaus-maps.arcor.de@news.arcor.de> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1308239981 24513 127.0.0.1 (16 Jun 2011 15:59:41 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 16 Jun 2011 15:59:41 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: y27g2000prb.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: ARLUEHNKC X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:20846 Date: 2011-06-16T08:56:32-07:00 List-Id: On Jun 16, 4:00=A0am, georg bauhaus wrote: > When I compile the following program with GNAT GPL 2011 on Mac, > I get confusingly different behavior depending on whether > or not I suppress checks. *Suppressing* the checks results in the > desired > program behavior. (The effect =A0reminds me of potential for Ariane 5 > gossip=97or simply of me being dense.) =A0When checks are *disabled* > (-gnatp), the program runs as expected and prints fib(10) =3D 55. > When checks are enabled (no -gnatp) I get a constraint error > on L.6. =A0Optimization does not affect the result, only -gnatp. > > Is the program correct, at least in theory? > > raised CONSTRAINT_ERROR : fib.ada:6 range check failed > > package Functions is > function Fib (N : Natural) return Natural is > =A0 =A0 (case N is > =A0 =A0 =A0when 0 =3D> 0, > =A0 =A0 =A0when 1 =3D> 1, > =A0 =A0 =A0when others =3D> Fib(N-1) + Fib(N-2)); =A0-- L.6 > end Functions; > > with Functions; > with Ada.Text_IO; > > procedure Run_Fib is > =A0 =A0 N : Natural; > begin > =A0 =A0 if Functions.Fib(0) /=3D 0 then > =A0 =A0 =A0 =A0 =A0raise Program_Error; > =A0 =A0 end if; > =A0 =A0 N :=3D Functions.Fib(10); > =A0 =A0 Ada.Text_IO.Put_Line ("fib(10) =3D" & Natural'Image(N)); > end Run_Fib; > > The debugger says that N=3D0 at L.6. Therefore, my guess is that > translating in normal Ada mode (implying range checks) breaks > the instruction sequence for case somewhere. =A0(I haven't understood > the asm yet, takes time and effort since I don't > read that every day.) My guess is that the code is trying to evaluate N-1 and/or N-2 even when N is 0 or 1, which would cause the Constraint_Error. The program should work; according to 4.5.7(21), at most one of the expressions on the right side of =3D> should be evaluated, so there should not be any code to evaluate N-1 or N-2 when N is <=3D 1. However, this is the kind of thing that's easy to screw up in a compiler. You have a tree representing the conditional expression, and then you try to rearrange the nodes in the tree in order make the code run faster and avoid redundant computations, and if you don't do it right then something that was supposed to be evaluated conditionally gets moved so that it's evaluated before the condition is checked, and then your code blows up. I've had to fix a number of compiler problems of this nature, related to short-circuit expressions. -- Adam