Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.1 6/24/83; site petsd.UUCP
Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxj!houxm!vax135!petsd!joe
From: joe@petsd.UUCP (Joe Orost)
Newsgroups: net.lang
Subject: Re: Constants in subroutine calls
Message-ID: <405@petsd.UUCP>
Date: Tue, 8-Jan-85 08:47:04 EST
Article-I.D.: petsd.405
Posted: Tue Jan  8 08:47:04 1985
Date-Received: Wed, 9-Jan-85 04:09:11 EST
References: <6599@brl-tgr.ARPA> <979@opus.UUCP> <1146@ut-ngp.UUCP> <18397@lanl.ARPA> <472@intelca.UUCP> <5143@rochester.UUCP>
Reply-To: joe@petsd.UUCP (PUT YOUR NAME HERE)
Organization: Perkin-Elmer DSG, Tinton Falls, N.J.
Lines: 58
Summary: 

In article <5143@rochester.UUCP> ken@rochester.UUCP (Ken Yap) writes:
>The problem is not using constants in subroutine calls per se but
>changing a parameter that corresponds to a constant.
>
>BTW I saw a neat way of trapping this sort of error without any
>run-time overhead in the VMS Fortran compiler. Constants are placed in
>a read-only segment and the VM system traps any violation. True, you
>may get an obscure error message if your run-time doesn't understand
>what happened but it is better than carrying on innocently.
>
>Can anybody tell me if there are other compilers out there that also do
>this? If not, is there any reason this is not more widely practiced?
>(Other than lack of memory management hardware, that is.)

I don't know how they got away with that.  Consider the following:

	CALL A(1.0)

	SUBROUTINE A(R)
	CALL B(R)
	END

	SUBROUTINE B(R)
	...
	END

Since scalar arguments are passed by value, the value 1.0 is copied into "R"
on entry to subroutine A.  On exit from A, the compiler can't know if B
changed the value of R (it also doesn't know that the argument is a
constant), so it must copy back the local copy of R into the constant 1.0.
This prevents us from storing the constant in read-only memory.  It seems
that the only way thay can get away with storing passed constants in read-only
memory is to CHECK what was passed on subroutine exit, and skip the update
code if a constant was passed (yick!).

According to the ANSI '77 Fortran standard 15.9.2 (15):

"Actual arguments may be constants, symbolic names of constants, function
references, expressions involving operators, and expressions enclosed in
parenthesis IF AND ONLY IF THE ASSOCIATED DUMMY ARGUMENT IS A VARIABLE THAT
IS NOT DEFINED DURING THE EXECUTION OF THE REFERENCED EXTERNAL PROCEDURE."

This means that it is erroneous to pass a constant to a routine which
changes its dummy argument.

In our compiler, we allow a routine to change a dummy argument that is
associated with an expression without side effects, but changing a constant
or a symbolic constant is taboo.

					regards,
					joe

--
Full-Name:  Joseph M. Orost
UUCP:       ..!{decvax,ucbvax,ihnp4}!vax135!petsd!joe
US Mail:    MS 313; Perkin-Elmer; 106 Apple St; Tinton Falls, NJ 07724
Phone:      (201) 870-5844
Location:   40 19'49" N / 74 04'37" W