This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [gfortran] Fix NULL reference types.
Paul Brook wrote:
[ Sorry for replying this late - I went to bed shortly after hitting
"Send" on my last message ]
Taking a modified version of your example above:
subroutine sub1(a, b)
if (a > 0) b = log(a)
end
Is it acceptable to turn this into:
subroutine sub2(a,b)
c = b
if (a > 0) c = log(a)
b = c
end
"call sub2(0, 0)" is clearly illegal.
Is "call sub1(0, 0)" also illegal?
A subprogram is legal if it is possible to write a legal program using it.
What I'm trying to say is that "call-by-reference" might seem to make it
always legal to call:
subroutine sub1(a, b)
intent(in) :: a
intent(out) :: b
if (a > 0) b = log(a)
end
but it isn't in case you include it in a program as follows:
program test
real a, b
a = -1.0
call sub1(a, b)
print*, b
end
Now, given that this is illegal, an adventurous Fortran compiler
engineer (e.g., X maintaining gfortran fifteen years after you have
abandoned it), might turn:
subroutine sub1(a, b)
intent(in) :: a
intent(out) :: b
if (a > 0) b = log(a)
end
into:
subroutine sub1(a, b)
intent(in) :: a
intent(out) :: b
real c
if (a > 0) c = log(a)
b = c
end
which, given the "intent(out)" on b, is within limits of the Standard.
Note that c has no valid value unless a>0.
Suddenly, the program that worked under the assumption that calling a
subroutine with an intent(out) dummy argument using a defined variable
as actual argument, which works when using call-by-reference, now
actually fails when an optimization is put into place that's fully
within the realm of the Fortran standard.
Hope this helps,
--
Toon Moene - mailto:toon@moene.indiv.nluug.nl - phoneto: +31 346 214290
Saturnushof 14, 3738 XG Maartensdijk, The Netherlands
Maintainer, GNU Fortran 77: http://gcc.gnu.org/onlinedocs/g77_news.html
A maintainer of GNU Fortran 95: http://gcc.gnu.org/fortran/