comp.lang.ada
 help / color / mirror / Atom feed
* expression function bug or think?
@ 2011-06-16 11:00 georg bauhaus
  2011-06-16 11:27 ` AdaMagica
                   ` (4 more replies)
  0 siblings, 5 replies; 20+ messages in thread
From: georg bauhaus @ 2011-06-16 11:00 UTC (permalink / raw)


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.)

Maybe there is a hint in 
http://gcc.gnu.org/ml/gcc-patches/2010-10/txt00134.txt
of
http://gcc.gnu.org/ml/gcc-patches/2010-10/msg00579.html

Is this a bug?



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: expression function bug or think?
  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
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 20+ messages in thread
From: AdaMagica @ 2011-06-16 11:27 UTC (permalink / raw)


Looks like a bug?




^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: expression function bug or think?
  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
  2011-06-17  3:56   ` Yannick Duchêne (Hibou57)
  2011-06-16 16:16 ` Anh Vo
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 20+ messages in thread
From: Adam Beneschan @ 2011-06-16 15:56 UTC (permalink / raw)


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




^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: expression function bug or think?
  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
@ 2011-06-16 16:16 ` Anh Vo
  2011-06-16 18:56 ` Bill Findlay
  2011-06-16 20:31 ` Florian Weimer
  4 siblings, 0 replies; 20+ messages in thread
From: Anh Vo @ 2011-06-16 16:16 UTC (permalink / raw)


It works fine on Windows.



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: expression function bug or think?
  2011-06-16 11:00 expression function bug or think? georg bauhaus
                   ` (2 preceding siblings ...)
  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:31 ` Florian Weimer
  4 siblings, 1 reply; 20+ messages in thread
From: Bill Findlay @ 2011-06-16 18:56 UTC (permalink / raw)


On 16/06/2011 12:00, in article
678085105329914667.504682rmhost.bauhaus-maps.arcor.de@news.arcor.de, "georg
bauhaus" <rmhost.bauhaus@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.

Strange. On Snow Leopard 10.6.7, I compile and run:

> /Users/wf/: gnatmake -gnat12 -gnatl run_fib.adb
> gcc -c -gnat12 -gnatl run_fib.adb
> 
> GNAT GPL 2011 (20110419)
> Copyright 1992-2011, Free Software Foundation, Inc.
> 
> 
> Compiling: run_fib.adb (source file time stamp: 2011-06-16 18:50:24)
> 
>      1. with Functions;
>      2. with Ada.Text_IO;
>      3.
>      4. procedure Run_Fib is
>      5.     N : Natural;
>      6. begin
>      7.     if Functions.Fib(0) /= 0 then
>      8.          raise Program_Error;
>      9.     end if;
>     10.     N := Functions.Fib(10);
>     11.     Ada.Text_IO.Put_Line ("fib(10) =" & Natural'Image(N));
>     12. end Run_Fib;
> 
>  12 lines: No errors
> gcc -c -gnat12 -gnatl functions.ads
> 
> GNAT GPL 2011 (20110419)
> Copyright 1992-2011, Free Software Foundation, Inc.
> 
> 
> Compiling: functions.ads (source file time stamp: 2011-06-16 18:51:28)
> 
>      1. package Functions is
>      2.    function Fib (N : Natural) return Natural is
>      3.        (case N is
>      4.         when 0 => 0,
>      5.         when 1 => 1,
>      6.         when others => Fib(N-1) + Fib(N-2));  -- L.6
>      7. end Functions;
> 
>  7 lines: No errors
> gnatbind -x run_fib.ali
> gnatlink run_fib.ali
> 
> 
> /Users/wf/: ./run_fib
> fib(10) = 55

The -gnatp option makes no difference.


-- 
Bill Findlay
with blueyonder.co.uk;
use  surname & forename;





^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: expression function bug or think?
  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
  0 siblings, 2 replies; 20+ messages in thread
From: Simon Wright @ 2011-06-16 19:18 UTC (permalink / raw)


... here, I find the same as Georg; and like you I am on SL 10.6.7!
Xcode 3.2.6.

With GCC 4.6.0, with -gnatp the code compiles and works fine; without
it, I get a compiler bomb.



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: expression function bug or think?
  2011-06-16 19:18   ` Simon Wright
@ 2011-06-16 20:03     ` Georg Bauhaus
  2011-06-16 20:03     ` Bill Findlay
  1 sibling, 0 replies; 20+ messages in thread
From: Georg Bauhaus @ 2011-06-16 20:03 UTC (permalink / raw)


On 6/16/11 9:18 PM, Simon Wright wrote:
> ... here, I find the same as Georg; and like you I am on SL 10.6.7!
> Xcode 3.2.6.

Same here.

> With GCC 4.6.0, with -gnatp the code compiles and works fine; without
> it, I get a compiler bomb.

This is what I get, too, with GCC 4.7.0 built on Debian GNU/Linux
from recent FSF sources:

  $ gnatchop -gnat12 -r -w ../fib.ada && gnatmake -gnatl -gnat12 functions.ads -cargs -v
splitting ../fib.ada into:
    functions.ads
    run_fib.adb
gcc -c -gnatl -gnat12 -v functions.ads
Using built-in specs.
COLLECT_GCC=/usr/local//bin/gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/i686-pc-linux-gnu/4.7.0/lto-wrapper
Target: i686-pc-linux-gnu
Configured with: /opt/src/configure --enable-languages=c,ada --disable-nls
Thread model: posix
gcc version 4.7.0 20110610 (experimental) (GCC)
COLLECT_GCC_OPTIONS='-gnatea' '-c' '-gnatl' '-gnat12' '-v' '-gnatez' '-mtune=generic' '-march=pentiumpro'
  /usr/local/libexec/gcc/i686-pc-linux-gnu/4.7.0/gnat1 -quiet -dumpbase functions.ads -auxbase functions -gnatez -gnatea -gnatl -gnat12 -gnatez -mtune=generic -march=pentiumpro functions.ads -o /tmp/cclE8jaZ.s

GNAT 4.7.0 20110610 (experimental)
Copyright 1992-2011, Free Software Foundation, Inc.
+===========================GNAT BUG DETECTED==============================+
| 4.7.0 20110610 (experimental) (i686-pc-linux-gnu) Assert_Failure sinfo.adb:2947|
| Error detected at fib.ada:6:21                                           |
| Please submit a bug report; see http://gcc.gnu.org/bugs.html.            |
| Use a subject line meaningful to you and us to track the bug.            |
| Include the entire contents of this bug box in the report.               |
| Include the exact gcc or gnatmake command that you entered.              |
| Also include sources listed below in gnatchop format                     |
| (concatenated together with no headers between files).                   |
+==========================================================================+

Please include these source files with error report
Note that list may not be accurate in some cases,
so please double check that the problem can still
be reproduced with the set of files listed.
Consider also -gnatd.n switch (see debug.adb).

functions.ads




raised SYSTEM.ASSERTIONS.ASSERT_FAILURE : namet.adb:655
gnatmake: "functions.ads" compilation error
CHROOT-thymian-bauhaus $



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: expression function bug or think?
  2011-06-16 19:18   ` Simon Wright
  2011-06-16 20:03     ` Georg Bauhaus
@ 2011-06-16 20:03     ` Bill Findlay
  1 sibling, 0 replies; 20+ messages in thread
From: Bill Findlay @ 2011-06-16 20:03 UTC (permalink / raw)


On 16/06/2011 20:18, in article m2y6111t71.fsf@pushface.org, "Simon Wright"
<simon@pushface.org> wrote:

> ... here, I find the same as Georg; and like you I am on SL 10.6.7!

Curiouser and curiouser!

> Xcode 3.2.6.
> 
> With GCC 4.6.0, with -gnatp the code compiles and works fine; without
> it, I get a compiler bomb.

I don't use Xcode. Perhaps that has some bearing, though I don't see what.

-- 
Bill Findlay
with blueyonder.co.uk;
use  surname & forename;





^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: expression function bug or think?
  2011-06-16 11:00 expression function bug or think? georg bauhaus
                   ` (3 preceding siblings ...)
  2011-06-16 18:56 ` Bill Findlay
@ 2011-06-16 20:31 ` Florian Weimer
  2011-06-16 20:41   ` Simon Wright
                     ` (2 more replies)
  4 siblings, 3 replies; 20+ messages in thread
From: Florian Weimer @ 2011-06-16 20:31 UTC (permalink / raw)


* georg bauhaus:

> 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.

Out of curiosity, can you show us the expanded Ada code, using -gnatG?



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: expression function bug or think?
  2011-06-16 20:31 ` Florian Weimer
@ 2011-06-16 20:41   ` Simon Wright
  2011-06-16 21:14     ` Georg Bauhaus
                       ` (2 more replies)
  2011-06-16 21:04   ` Georg Bauhaus
  2011-06-17  8:55   ` Georg Bauhaus
  2 siblings, 3 replies; 20+ messages in thread
From: Simon Wright @ 2011-06-16 20:41 UTC (permalink / raw)


Florian Weimer <fw@deneb.enyo.de> writes:

> Out of curiosity, can you show us the expanded Ada code, using -gnatG?

GNAT GPL 2011, with -gnatp:

functions_E : short_integer := 0;

package functions is
   function functions__fib (n : natural) return natural;

   function functions__fib (n : natural) return natural is
   begin
      return
         do
            T2s : natural;
            case n is
               when 0 =>
                  T2s := 0;
               when 1 =>
                  T2s := 1;
               when others =>
                  T2s := functions__fib (n - 1) + functions__fib (n -
                    2);
            end case;
         in T2s end
      ;
   end functions__fib;
end functions;

GNAT GPL 2011, without -gnatp:

functions_E : short_integer := 0;

package functions is
   function functions__fib (n : natural) return natural;

   function functions__fib (n : natural) return natural is
   begin
      return
         do
            T2s : natural;[constraint_error when
              not (n - 1 >= 0)
              "range check failed"][constraint_error when
              not (n - 2 >= 0)
              "range check failed"][constraint_error when
              not (interfaces__unsigned_32!(n) <= 16#7FFF_FFFF#)
              "invalid data"]
            case n is
               when 0 =>
                  T2s := 0;
               when 1 =>
                  T2s := 1;
               when others =>
                  T2s := functions__fib (n - 1) + functions__fib (n -
                    2);
            end case;
         in T2s end
      ;
   end functions__fib;
end functions;



Pretty conclusive, I think!



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: expression function bug or think?
  2011-06-16 20:31 ` Florian Weimer
  2011-06-16 20:41   ` Simon Wright
@ 2011-06-16 21:04   ` Georg Bauhaus
  2011-06-17  8:55   ` Georg Bauhaus
  2 siblings, 0 replies; 20+ messages in thread
From: Georg Bauhaus @ 2011-06-16 21:04 UTC (permalink / raw)


On 6/16/11 10:31 PM, Florian Weimer wrote:
> * georg bauhaus:
>
>> 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.
>
> Out of curiosity, can you show us the expanded Ada code, using -gnatG?

Coming in a moment ...




^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: expression function bug or think?
  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
  2 siblings, 0 replies; 20+ messages in thread
From: Georg Bauhaus @ 2011-06-16 21:14 UTC (permalink / raw)


On 6/16/11 10:41 PM, Simon Wright wrote:
> Florian Weimer<fw@deneb.enyo.de>  writes:
>
>> Out of curiosity, can you show us the expanded Ada code, using -gnatG?
>
> GNAT GPL 2011, with -gnatp:
>
> functions_E : short_integer := 0;
>
> package functions is
>     function functions__fib (n : natural) return natural;
>
>     function functions__fib (n : natural) return natural is
>     begin
>        return
>           do
>              T2s : natural;
>              case n is
>                 when 0 =>
>                    T2s := 0;

>     function functions__fib (n : natural) return natural is
>     begin
>        return
>           do
>              T2s : natural;[constraint_error when
>                not (n - 1>= 0)
>                "range check failed"][constraint_error when

> Pretty conclusive, I think!


This seems to confirm what I have found in the assembly listing.
(The very first call in Run_Fib with N = 0 raises Constraint_Error.)
The case expression seems to cause the effect.

For comparison, a case statement in a function body (see below)
does not generate a "subtract 1" instruction (which at least
matches the not (n - 1>= 0) shown above).

Assembly for _functions__fib, assume called with N = 0:

         movl    %edi, -20(%rbp)
         movl    -20(%rbp), %eax    ; N in EAX
         subl    $1, %eax          ; -1   <-----------------
         testl   %eax, %eax        ;  sets sign flag  SF
         jns     L2                ; jump if SF not set
         leaq    LC0(%rip), %rax   ;  range check otherwise ...
         movl    $6, %esi
         movq    %rax, %rdi
         call    ___gnat_rcheck_12

Assembly for function with case statement:

         movl    %edi, -20(%rbp)
         movl    -20(%rbp), %eax
                                   ; no SUB here, no -1
         testl   %eax, %eax
         jns     L13
         leaq    LC0(%rip), %rax
         movl    $26, %esi
         movq    %rax, %rdi
         call    ___gnat_rcheck_06


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));
function Fib2 (N : Natural) return Natural;
end Functions;

with Functions;
with Ada.Text_IO;

procedure Run_Fib is
     N : Natural;
begin
     if Functions.Fib2(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;

package body Functions is
function Fib2 (N : Natural) return Natural is
begin
     case N is
     when 0 => return 0;
     when 1 => return 1;
     when others => return Fib2(N-1) + Fib2(N-2);
     end case;
end Fib2;
end Functions;



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: expression function bug or think?
  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
  2 siblings, 0 replies; 20+ messages in thread
From: Bill Findlay @ 2011-06-16 23:14 UTC (permalink / raw)


On 16/06/2011 21:41, in article m2tybp1pce.fsf@pushface.org, "Simon Wright"
<simon@pushface.org> wrote:

> GNAT GPL 2011, without -gnatp:
> 
> functions_E : short_integer := 0;
> 
> package functions is
>    function functions__fib (n : natural) return natural;
> 
>    function functions__fib (n : natural) return natural is
>    begin
>       return
>          do
>             T2s : natural;[constraint_error when
>               not (n - 1 >= 0)
>               "range check failed"][constraint_error when
>               not (n - 2 >= 0)
>               "range check failed"][constraint_error when
>               not (interfaces__unsigned_32!(n) <= 16#7FFF_FFFF#)
>               "invalid data"]
>             case n is
>                when 0 =>
>                   T2s := 0;
>                when 1 =>
>                   T2s := 1;
>                when others =>
>                   T2s := functions__fib (n - 1) + functions__fib (n -
>                     2);
>             end case;
>          in T2s end
>       ;
>    end functions__fib;
> end functions;
> 
> 
> 
> Pretty conclusive, I think!

So it would seem, but without -gnatp I get exactly the same result as you do
with -gnatp:

> /Users/wf: gnatmake  -gnat12 -gnatl -gnatG run_fib.adb >nop.txt
> gcc -c -gnat12 -gnatl -gnatG run_fib.adb
> gcc -c -gnat12 -gnatl -gnatG functions.ads
> gnatbind -x run_fib.ali
> gnatlink run_fib.ali
> 
> /Users/wf: cat nop.txt
> 
> GNAT GPL 2011 (20110419)
> Copyright 1992-2011, Free Software Foundation, Inc.
> Source recreated from tree for Run_Fib (body)
> ---------------------------------------------
> 
> ....
> 
> GNAT GPL 2011 (20110419)
> Copyright 1992-2011, Free Software Foundation, Inc.
> Source recreated from tree for Functions (spec)
> -----------------------------------------------
> 
> functions_E : short_integer := 0;
> 
> package functions is
>    function functions__fib (n : natural) return natural;
> 
>    function functions__fib (n : natural) return natural is
>    begin
>       return
>          do
>             T2s : natural;
>             case n is
>                when 0 =>
>                   T2s := 0;
>                when 1 =>
>                   T2s := 1;
>                when others =>
>                   T2s := functions__fib (n - 1) + functions__fib (n -
>                     2);
>             end case;
>          in T2s end
>       ;
>    end functions__fib;
> end functions;
> 

?

-- 
Bill Findlay
with blueyonder.co.uk;
use  surname & forename;





^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: expression function bug or think?
  2011-06-16 15:56 ` Adam Beneschan
@ 2011-06-17  3:56   ` Yannick Duchêne (Hibou57)
  0 siblings, 0 replies; 20+ messages in thread
From: Yannick Duchêne (Hibou57) @ 2011-06-17  3:56 UTC (permalink / raw)


Le Thu, 16 Jun 2011 17:56:32 +0200, Adam Beneschan <adam@irvine.com> a  
écrit:
> 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.

There is no formal proofs the applied transformations are valid ?


-- 
“Syntactic sugar causes cancer of the semi-colons.”  [Epigrams on  
Programming — Alan J. — P. Yale University]
“Structured Programming supports the law of the excluded muddle.” [Idem]
Java: Write once, Never revisit



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: expression function bug or think?
  2011-06-16 20:31 ` Florian Weimer
  2011-06-16 20:41   ` Simon Wright
  2011-06-16 21:04   ` Georg Bauhaus
@ 2011-06-17  8:55   ` Georg Bauhaus
  2011-06-17  9:10     ` Georg Bauhaus
  2 siblings, 1 reply; 20+ messages in thread
From: Georg Bauhaus @ 2011-06-17  8:55 UTC (permalink / raw)


On 6/16/11 10:31 PM, Florian Weimer wrote:
> * georg bauhaus:
>
>> 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.
>
> Out of curiosity, can you show us the expanded Ada code, using -gnatG?

GNAT GPL 2011 x86-linux produces Constraint_Error, too.
With Debian/GNU 5.0.8, running on a virtual machine:

$ ./run_fib

raised CONSTRAINT_ERROR : fib.ada:6 range check failed
$ gcc --version|head -1
gcc (GCC) 4.5.3 20110419 for GNAT GPL 2011 (20110419)


Whereas, with -gnatp, I get

$ gnatchop -gnat2012 -r -w fib.ada && gnatmake -gnat2012 -gnatp run_fib
splitting fib.ada into:
    functions.ads
    run_fib.adb
    functions.adb
gcc -c -gnat2012 -gnatp run_fib.adb
gcc -c -gnat2012 -gnatp functions.adb
gnatbind -x run_fib.ali
gnatlink run_fib.ali
$ ./run_fib
fib(10) = 55


Without -gnatp, comparing case expressions and case statements:

GNAT GPL 2011 (20110419)
Copyright 1992-2011, Free Software Foundation, Inc.
Source recreated from tree for Run_Fib (body)
---------------------------------------------

with ada;
pragma source_reference (9, "fib.ada");
with functions;
with ada.ada__text_io;
with system.system__img_int;
with system.system__concat_2;

procedure run_fib is
    n : natural;
begin
    if functions.functions__fib (0) /= 0 then
       [program_error "explicit raise"]
    end if;
    n := functions.functions__fib (10);
    B1b : declare
    begin
       subtype run_fib__B1b__TS2bS is string (1 .. 11);
       S2b : string (1 .. 11);
       P3b : natural;
       $system__img_int__image_integer (integer(n), S2b, P3b);
       R4b : constant natural := P3b;
       [subtype run_fib__B1b__T5b is integer range 1 .. R4b]
       [subtype run_fib__B1b__T6b is string (run_fib__B1b__T5b)]
       reference run_fib__B1b__T6b
       L9b : constant integer := S2b (1 .. R4b)'length;
       L11b : constant integer := 9 + L9b;
       subtype run_fib__B1b__TS12bS is string (1 .. 1 + (L11b - 1));
       S12b : string (1 .. 1 + (L11b - 1));
       $system__concat_2__str_concat_2 (S12b, "fib(10) =", S2b (1 .. R4b));
       ada.ada__text_io.ada__text_io__put_line__2 (S12b);
    end B1b;
    return;
end run_fib;



Compiling: run_fib.adb (source file time stamp: 2011-06-17 08:49:11)

         pragma Source_Reference (000009, "fib.ada");
      9.
     10. with Functions;
     11. with Ada.Text_IO;
     12.
     13. procedure Run_Fib is
     14.     N : Natural;
     15. begin
     16.     if Functions.Fib(0) /= 0 then
     17.          raise Program_Error;
     18.     end if;
     19.     N := Functions.Fib(10);
     20.     Ada.Text_IO.Put_Line ("fib(10) =" & Natural'Image(N));
     21. end Run_Fib;

  14 lines: No errors

GNAT GPL 2011 (20110419)
Copyright 1992-2011, Free Software Foundation, Inc.
Source recreated from tree for Functions (body)
-----------------------------------------------

pragma source_reference (22, "fib.ada");
with interfaces;

package body functions is

    function functions__fib2 (n : natural) return natural is
    begin
       [constraint_error when
         not (interfaces__unsigned_32!(n) <= 16#7FFF_FFFF#)
         "invalid data"]
       case n is
          when 0 =>
             return 0;
          when 1 =>
             return 1;
          when others =>
             [constraint_error when
               not (n - 1 >= 0)
               "range check failed"]
             [constraint_error when
               not (n - 2 >= 0)
               "range check failed"]
             return natural(functions__fib2 (n - 1) + functions__fib2 (n -
               2));
       end case;
    end functions__fib2;
begin
    null;
end functions;

Source recreated from tree for Functions (spec)
-----------------------------------------------

pragma source_reference (1, "fib.ada");
with interfaces;
functions_E : short_integer := 0;

package functions is
    function functions__fib (n : natural) return natural;

    function functions__fib (n : natural) return natural is
    begin
       return
          do
             T2s : natural;[constraint_error when
               not (n - 1 >= 0)
               "range check failed"][constraint_error when
               not (n - 2 >= 0)
               "range check failed"][constraint_error when
               not (interfaces__unsigned_32!(n) <= 16#7FFF_FFFF#)
               "invalid data"]
             case n is
                when 0 =>
                   T2s := 0;
                when 1 =>
                   T2s := 1;
                when others =>
                   T2s := functions__fib (n - 1) + functions__fib (n -
                     2);
             end case;
          in T2s end
       ;
    end functions__fib;

    function functions__fib2 (n : natural) return natural;
end functions;



Compiling: functions.adb (source file time stamp: 2011-06-17 08:49:11)

         pragma Source_Reference (000022, "fib.ada");
     22.
     23. package body Functions is
     24. function Fib2 (N : Natural) return Natural is
     25. begin
     26.     case N is
     27.     when 0 => return 0;
     28.     when 1 => return 1;
     29.     when others => return Fib2(N-1) + Fib2(N-2);
     30.     end case;
     31. end Fib2;
     32. end Functions;

Compiling: functions.ads (source file time stamp: 2011-06-17 08:49:11)

         pragma Source_Reference (000001, "fib.ada");
      1. package Functions is
      2. function Fib (N : Natural) return Natural is
      3.     (case N is
      4.      when 0 => 0,
      5.      when 1 => 1,
      6.      when others => Fib(N-1) + Fib(N-2));
      7. function Fib2 (N : Natural) return Natural;
      8. end Functions;

  12 lines: No errors



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: expression function bug or think?
  2011-06-17  8:55   ` Georg Bauhaus
@ 2011-06-17  9:10     ` Georg Bauhaus
  0 siblings, 0 replies; 20+ messages in thread
From: Georg Bauhaus @ 2011-06-17  9:10 UTC (permalink / raw)


On 6/17/11 10:55 AM, Georg Bauhaus wrote:
>           when others =>
>              [constraint_error when
>                not (n - 1 >= 0)
>                "range check failed"]
>              [constraint_error when
>                not (n - 2 >= 0)
>                "range check failed"]
>              return natural(functions__fib2 (n - 1) + functions__fib2 (n -
>                2));

This looks like a suitable place for the range checks.
Maybe they are being been move upwards for case statements
the way Adam has described?



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: expression function bug or think?
  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
  2 siblings, 1 reply; 20+ messages in thread
From: Martin @ 2011-06-17  9:34 UTC (permalink / raw)


On Jun 16, 9:41 pm, Simon Wright <si...@pushface.org> wrote:
[snip]
> package functions is
>    function functions__fib (n : natural) return natural;

Thanks for this - this confirms something I'd been told wasn't the
case - when compiling GNAT, non-standard identifiers _are_ created and
allowed (in this case a name with a double underscore)!

-- Martin



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: expression function bug or think?
  2011-06-17  9:34     ` Martin
@ 2011-06-17 10:33       ` Simon Wright
  2011-06-17 10:39         ` Martin
  0 siblings, 1 reply; 20+ messages in thread
From: Simon Wright @ 2011-06-17 10:33 UTC (permalink / raw)


Martin <martin.dowie@btopenworld.com> writes:

> On Jun 16, 9:41 pm, Simon Wright <si...@pushface.org> wrote:
> [snip]
>> package functions is
>>    function functions__fib (n : natural) return natural;
>
> Thanks for this - this confirms something I'd been told wasn't the
> case - when compiling GNAT, non-standard identifiers _are_ created and
> allowed (in this case a name with a double underscore)!

I think this only happens when GNAT recreates the processed source to
show you what it did.



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: expression function bug or think?
  2011-06-17 10:33       ` Simon Wright
@ 2011-06-17 10:39         ` Martin
  2011-06-17 11:09           ` Niklas Holsti
  0 siblings, 1 reply; 20+ messages in thread
From: Martin @ 2011-06-17 10:39 UTC (permalink / raw)


On Jun 17, 11:33 am, Simon Wright <si...@pushface.org> wrote:
> Martin <martin.do...@btopenworld.com> writes:
> > On Jun 16, 9:41 pm, Simon Wright <si...@pushface.org> wrote:
> > [snip]
> >> package functions is
> >>    function functions__fib (n : natural) return natural;
>
> > Thanks for this - this confirms something I'd been told wasn't the
> > case - when compiling GNAT, non-standard identifiers _are_ created and
> > allowed (in this case a name with a double underscore)!
>
> I think this only happens when GNAT recreates the processed source to
> show you what it did.

Ahhh! Thanks...wonder what the internal reference looks like...or is
there one at all and it just creates these names when this option is
selected? If I had a 28 hour day, I might find out!

-- Martin



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: expression function bug or think?
  2011-06-17 10:39         ` Martin
@ 2011-06-17 11:09           ` Niklas Holsti
  0 siblings, 0 replies; 20+ messages in thread
From: Niklas Holsti @ 2011-06-17 11:09 UTC (permalink / raw)


Martin wrote:
> On Jun 17, 11:33 am, Simon Wright <si...@pushface.org> wrote:
>> Martin <martin.do...@btopenworld.com> writes:
>>> On Jun 16, 9:41 pm, Simon Wright <si...@pushface.org> wrote:
>>> [snip]
>>>> package functions is
>>>>    function functions__fib (n : natural) return natural;
>>> Thanks for this - this confirms something I'd been told wasn't the
>>> case - when compiling GNAT, non-standard identifiers _are_ created and
>>> allowed (in this case a name with a double underscore)!
>> I think this only happens when GNAT recreates the processed source to
>> show you what it did.
> 
> Ahhh! Thanks...wonder what the internal reference looks like...or is
> there one at all and it just creates these names when this option is
> selected? If I had a 28 hour day, I might find out!

I'm not sure what your question really is, but if it is about the way 
GNAT generates link names for Ada entities, this is explained in the 
GNAT User's Guide. Quote (from the version for GCC 4.3.0):

"25.2 The External Symbol Naming Scheme of GNAT

In order to interpret the output from GNAT, when using tools that are 
originally intended for use with other languages, it is useful to 
understand the conventions used to generate link names from the Ada 
entity names. All link names are in all lowercase letters. With the 
exception of library procedure names, the mechanism used is simply to 
use the full expanded Ada name with dots replaced by double underscores. 
For example, suppose we have the following package spec:

package QRS is
    MN : Integer;
end QRS;

The variable MN has a full expanded Ada name of QRS.MN, so the 
corresponding link name is qrs__mn."

The name "functions__fib" in the recreated source, above, follows this rule.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .



^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2011-06-17 11:09 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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