public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/62309] New: -fno-automatic with -finit-local prevents initialization of automatics in recursive functions
@ 2014-08-29 20:08 fritzoreese at gmail dot com
  2014-08-29 20:10 ` [Bug fortran/62309] " fritzoreese at gmail dot com
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: fritzoreese at gmail dot com @ 2014-08-29 20:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62309

            Bug ID: 62309
           Summary: -fno-automatic with -finit-local prevents
                    initialization of automatics in recursive functions
           Product: gcc
           Version: 4.8.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fritzoreese at gmail dot com

It seems with gcc-4.8.3 -fno-automatic prevents initializers from being applied
to automatic variables. The following does not behave as I would expect it to
with (linux-x86-64):

      function f (x)
        implicit none
        integer f, x
        integer a ! should be SAVEd from -fno-automatic
        a = a + x ! should increment by y every time
        f = a
        return
      endfunction

      recursive function g (x)
        implicit none
        integer g, x
        integer b ! should be automatic from recursive
        b = b + x ! should be set to y every time
        g = b
        return
      endfunction

      implicit none
      integer f, g

      ! Should return static value of a; accumulates x
      print *, f(3) ! -> 3, ok
      print *, f(4) ! -> 7, ok
      print *, f(2) ! -> 2, ok

      ! Should return automatic value of c; equal to y each time
      print *, g(3) ! -> garbage, expected 3
      print *, g(4) ! -> garbage, expected 4
      print *, g(2) ! -> garbage, expected 2

      end
$ gfortran -fno-automatic -finit-local-zero auto_test.f
$ ./a.out
           3
           7
           9
       32770
       32771
       32769
$

According to gfortran's manual page, -fno-automatic should "Treat each program
unit (except those marked as RECURSIVE) as if the "SAVE" statement were
specified for every local variable [...]". As far as I can tell,
-finit-local-zero should still initialize automatic variables in RECURSIVE
functions.


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

* [Bug fortran/62309] -fno-automatic with -finit-local prevents initialization of automatics in recursive functions
  2014-08-29 20:08 [Bug fortran/62309] New: -fno-automatic with -finit-local prevents initialization of automatics in recursive functions fritzoreese at gmail dot com
@ 2014-08-29 20:10 ` fritzoreese at gmail dot com
  2014-08-31 22:29 ` burnus at gcc dot gnu.org
  2014-08-31 22:31 ` burnus at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: fritzoreese at gmail dot com @ 2014-08-29 20:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62309

Fritz Reese <fritzoreese at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |fritzoreese at gmail dot com

--- Comment #1 from Fritz Reese <fritzoreese at gmail dot com> ---
Created attachment 33418
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=33418&action=edit
Patch and testcase

I believe this is a simple fix; to actually follow the specification set forth
in the man page, don't treat symbols in a RECURSIVE namespace as if they are
saved in resolve.c (apply_default_init_local):

2014-08-29  Fritz Reese  <Reese-Fritz@zai.com>

    * resolve.c (apply_default_init_local): Don't treat variables in
    RECURSIVE units as saved.


diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
index 43eb240..a428633 100644
--- a/gcc/fortran/resolve.c
+++ b/gcc/fortran/resolve.c
@@ -11104,6 +11104,7 @@ apply_default_init_local (gfc_symbol *sym)
      result variable, which are also nonstatic.  */
   if (sym->attr.save || sym->ns->save_all
       || (gfc_option.flag_max_stack_var_size == 0 && !sym->attr.result
+          && !sym->ns->proc_name->attr.recursive
          && (!sym->attr.dimension || !is_non_constant_shape_array (sym))))
     {
       /* Don't clobber an existing initializer!  */
diff --git a/gcc/testsuite/gfortran.dg/auto_save_2.f90
b/gcc/testsuite/gfortran.
new file mode 100644
index 0000000..0d39d48
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/auto_save_2.f90
@@ -0,0 +1,52 @@
+! { dg-do run }
+! { dg-options "-fno-automatic -finit-local-zero" }
+!
+! Make sure variables are saved with -fno-automatic except in
+! functions marked RECURSIVE, and that they are still initialized with
+! -finit-local-zero.
+!
+
+function f (x)
+implicit none
+  integer f, x
+  integer a ! should be SAVEd
+  a = a + x ! should increment by y every time
+  f = a
+  return
+endfunction
+
+recursive function g (x)
+implicit none
+  integer g, x
+  integer b ! should be automatic
+  b = b + x ! should be set to y every time
+  g = b
+  return
+endfunction
+
+implicit none
+integer f, g
+
+! Should return static value of a; accumulates y
+if ( f(3) .ne. 3 ) then
+  call abort ()
+endif
+if ( f(4) .ne. 7 ) then
+  call abort ()
+endif
+if ( f(2) .ne. 9 ) then
+  call abort ()
+endif
+
+! Should return automatic value of a; equal to y each time
+if ( g(3) .ne. 3 ) then
+  call abort ()
+endif
+if ( g(4) .ne. 4 ) then
+  call abort ()
+endif
+if ( g(2) .ne. 2 ) then
+  call abort ()
+endif
+
+end


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

* [Bug fortran/62309] -fno-automatic with -finit-local prevents initialization of automatics in recursive functions
  2014-08-29 20:08 [Bug fortran/62309] New: -fno-automatic with -finit-local prevents initialization of automatics in recursive functions fritzoreese at gmail dot com
  2014-08-29 20:10 ` [Bug fortran/62309] " fritzoreese at gmail dot com
@ 2014-08-31 22:29 ` burnus at gcc dot gnu.org
  2014-08-31 22:31 ` burnus at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: burnus at gcc dot gnu.org @ 2014-08-31 22:29 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62309

--- Comment #2 from Tobias Burnus <burnus at gcc dot gnu.org> ---
Author: burnus
Date: Sun Aug 31 22:28:30 2014
New Revision: 214771

URL: https://gcc.gnu.org/viewcvs?rev=214771&root=gcc&view=rev
Log:
2014-08-31  Fritz Reese  <Reese-Fritz@zai.com>

        PR fortran/62309
        * resolve.c (apply_default_init_local): Don't treat variables
        in RECURSIVE procedures as saved.

2014-08-31  Fritz Reese  <Reese-Fritz@zai.com>
            Tobias Burnus  <burnus@net-b.de>

        PR fortran/62309
        * gcc/testsuite/gfortran.dg/auto_save_2.f90: New.


Added:
    trunk/gcc/testsuite/gfortran.dg/auto_save_2.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/resolve.c
    trunk/gcc/testsuite/ChangeLog


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

* [Bug fortran/62309] -fno-automatic with -finit-local prevents initialization of automatics in recursive functions
  2014-08-29 20:08 [Bug fortran/62309] New: -fno-automatic with -finit-local prevents initialization of automatics in recursive functions fritzoreese at gmail dot com
  2014-08-29 20:10 ` [Bug fortran/62309] " fritzoreese at gmail dot com
  2014-08-31 22:29 ` burnus at gcc dot gnu.org
@ 2014-08-31 22:31 ` burnus at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: burnus at gcc dot gnu.org @ 2014-08-31 22:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62309

Tobias Burnus <burnus at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |burnus at gcc dot gnu.org
         Resolution|---                         |FIXED

--- Comment #3 from Tobias Burnus <burnus at gcc dot gnu.org> ---
FIXED on the trunk (GCC 5, which will be released in spring 2015).


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

end of thread, other threads:[~2014-08-31 22:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-29 20:08 [Bug fortran/62309] New: -fno-automatic with -finit-local prevents initialization of automatics in recursive functions fritzoreese at gmail dot com
2014-08-29 20:10 ` [Bug fortran/62309] " fritzoreese at gmail dot com
2014-08-31 22:29 ` burnus at gcc dot gnu.org
2014-08-31 22:31 ` burnus at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).