comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: expression function bug or think?
Date: Thu, 16 Jun 2011 08:56:32 -0700 (PDT)
Date: 2011-06-16T08:56:32-07:00	[thread overview]
Message-ID: <f4d6b6d2-e1c4-434a-97ed-08347bf47152@y27g2000prb.googlegroups.com> (raw)
In-Reply-To: 678085105329914667.504682rmhost.bauhaus-maps.arcor.de@news.arcor.de

On Jun 16, 4:00 am, georg bauhaus <rmhost.bauh...@maps.arcor.de>
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  reminds me of potential for Ariane 5
> gossip—or simply of me being dense.)  When checks are *disabled*
> (-gnatp), the program runs as expected and prints fib(10) = 55.
> When checks are enabled (no -gnatp) I get a constraint error
> on L.6.  Optimization 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
>     (case N is
>      when 0 => 0,
>      when 1 => 1,
>      when others => Fib(N-1) + Fib(N-2));  -- L.6
> end Functions;
>
> with Functions;
> with Ada.Text_IO;
>
> procedure Run_Fib is
>     N : Natural;
> begin
>     if Functions.Fib(0) /= 0 then
>          raise Program_Error;
>     end if;
>     N := Functions.Fib(10);
>     Ada.Text_IO.Put_Line ("fib(10) =" & Natural'Image(N));
> end Run_Fib;
>
> The debugger says that N=0 at L.6. Therefore, my guess is that
> translating in normal Ada mode (implying range checks) breaks
> the instruction sequence for case somewhere.  (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 => should be evaluated, so there should not be any
code to evaluate N-1 or N-2 when N is <= 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




  parent reply	other threads:[~2011-06-16 15:56 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-16 11:00 expression function bug or think? georg bauhaus
2011-06-16 11:27 ` AdaMagica
2011-06-16 15:56 ` Adam Beneschan [this message]
2011-06-17  3:56   ` Yannick Duchêne (Hibou57)
2011-06-16 16:16 ` Anh Vo
2011-06-16 18:56 ` Bill Findlay
2011-06-16 19:18   ` Simon Wright
2011-06-16 20:03     ` Georg Bauhaus
2011-06-16 20:03     ` Bill Findlay
2011-06-16 20:31 ` Florian Weimer
2011-06-16 20:41   ` Simon Wright
2011-06-16 21:14     ` Georg Bauhaus
2011-06-16 23:14     ` Bill Findlay
2011-06-17  9:34     ` Martin
2011-06-17 10:33       ` Simon Wright
2011-06-17 10:39         ` Martin
2011-06-17 11:09           ` Niklas Holsti
2011-06-16 21:04   ` Georg Bauhaus
2011-06-17  8:55   ` Georg Bauhaus
2011-06-17  9:10     ` Georg Bauhaus
replies disabled

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