comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <rm.dash-bauhaus@futureapps.de>
Subject: Re: expression function bug or think?
Date: Thu, 16 Jun 2011 23:14:55 +0200
Date: 2011-06-16T23:14:56+02:00	[thread overview]
Message-ID: <4dfa7250$0$7610$9b4e6d93@newsspool1.arcor-online.net> (raw)
In-Reply-To: <m2tybp1pce.fsf@pushface.org>

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;



  reply	other threads:[~2011-06-16 21:14 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
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 [this message]
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