A From:	SMTP%"RELAY-INFO-VAX@CRVAX.SRI.COM" 27-MAY-1993 13:59:04.05  To:	EVERHART CC:	9 Subj:	Re: Q: Detecting number of arguments in VAX Fortran    X-Newsgroups: comp.os.vms . From: lionel@quark.enet.dec.com (Steve Lionel)< Subject: Re: Q: Detecting number of arguments in VAX Fortran7 Message-Id: <1993May27.015018.22479@dbased.nuo.dec.com> 4 Sender: news@dbased.nuo.dec.com (USENET News System)+ Organization: Digital Equipment Corporation # Date: Thu, 27 May 1993 01:54:13 GMT 
 Lines: 122 To: Info-VAX@kl.sri.com  X-Gateway-Source-Info: USENET     K There's a lot of confusing, misleading, incomplete and sometimes just wrong H information floating around this newsgroup on this subject.  Some of it,@ I'm sorry to say, came from me, and I really should know better!  H So, here is all I know about the subject, including what happens on AXP.  F First, I will repeat what I said before.  Both the DEC Fortran LRM andB the Fortran-77 standard say that the number, type and order of theC actual arguments (the ones in your CALL or function reference) must B match those of the "dummy" (formal) arguments in the subroutine orE function you are calling.  This applies to calls to Fortran routines; > when you call non-Fortran routines, different rules may apply.  E Now that I've gotten that out of the way, it is generally possible to F write a Fortran routine for OpenVMS such that it can, with predictableB results, be called with fewer actuals than dummys or with selectedA arguments "omitted" (by passing an immediate value of zero in the E argument position).  The exceptions to this are dummy arguments which J are type CHARACTER or which are adjustable arrays (whose bounds are passedJ either in other arguments or in COMMON); for these two cases, the compilerH will generate routine prologue code that fetches the argument descriptorC (or address) and will typically give you an access violation if you   omitted that argument in a call.  L Ok, you promise not to omit CHARACTER or adjustable array arguments.  What'sH next is that there are two separate questions a routine may want to ask.  I'll deal with these separately.  N The first is "how many arguments was I called with?"  On VAX, this informationD is supplied in the first byte pointed to by register AP (R12) when aG routine is called.  The VAX compiler does not currently provide a means H of getting at this value directly.  What most people do is write a shortG MACRO function, such as was posted earlier, to get the "saved AP" value E from the stack frame when the MACRO routine is called and extract the F byte.  There is a catch, though.  The compiled Fortran code is free toG use AP/R12 as a general register, so there's no guarantee that when you F call your MACRO routine that the value of AP at that time will be whatF it was when the Fortran routine was called.  However, the VAX compilerC currently reuses AP only in a main program or a routine that has no E declared dummy arguments.  Furthermore, I think it's safe to say that ? the VAX compiler won't ever change its behavior in this regard.   H On AXP, it's more complicated.  There is an argument count, but it isn'tG something you can reliably get by calling a MACRO routine.   But relax, K we didn't leave you hanging.  The AXP compiler has an IARGCOUNT() intrinsic C that will reliably return the argument count.  I'll very likely add - this to the VAX compiler in a future version.   H The second question is "was a particular argument passed by the caller?"D An argument can be omitted in two ways.  First, the argument list isB short, and second, the argument list is not short but the argumentG address in the argument list is zero.   If you've used one of the above C methods to determine that the argument position is present, you can D reliably test %LOC(arg) for zero to see if the argument was omitted.F If you use this method, there's nothing else you need do; the compilerD will give you a free VOLATILE declaration for the argument and won'tB do any optimizations that will touch the argument except when yourA code does so.  It is important to use conditional code to prevent B unwanted accesses to an argument which may be omitted and not rely@ on order of expression evaluation.  For example, I saw a program which did something like this:  0 		IF ((%LOC(ARG) .NE. 0) .AND. (ARG .NE. 0)) ...  E This happened to work on a VAX system but not on an AXP system as the C AXP compiler chose to evaluate the second subexpresion first, as it . was free to do.  It's safer to do it this way:   		IF (%LOC(ARG) .NE. 0) THEN 	 	  IF (ARG .NE. 0) ...  K Now comes the confusing part, to which I have contributed mightily.  I have B been telling people that with V6 of the VAX compiler that you MUSTD use /ASSUME=DUMMY_ALIASES if your routine can be called with omittedC arguments.  This isn't strictly true.  In particular, it's not true G if you've used the %LOC method to determine if the argument was passed. A Where it IS necessary is if the routine uses some OTHER method of B determining whether or not the argument was passed, such as solelyF relying on the argument count or by looking at the value of some otherD argument.  In this case, the V6 compiler may choose to perform a newI optimization in which it makes a local copy of the argument in a register E so that it can eliminate two memory references each time the argument F is used.  It does this by putting code in the routine prologue, beforeF the first statement, to do the copy.  If the argument was omitted, youE might well get an ACCVIO as the code tries to fetch from address zero I (or whatever happens to be at that position in the actual argument list.) J You can disable this optimization by compiling with /ASSUME=DUMMY_ALIASES,D which tells the compiler that it must assume that any dummy argumentC may be aliased (share storage with) some other argument or a COMMON I variable.  The V5 compiler made this assumption all the time, even though D the Fortran-77 standard prohibits such aliasing.  An example of this	 would be:    	CALL SUB (A,A)  	... 	SUBROUTINE SUB (X,Y)   D This assumption prevents many useful optimizations, and most FortranD programs don't have such aliasing, so the V6 compiler was changed toF assume NODUMMY_ALIASES and the qualifier was created so that you couldG tell the compiler that you might have aliasing.  As I noted before, you / can put this qualifier in an OPTIONS statement.   F The AXP compiler does the same optimization, but in a different way soF that a problem is less likely to occur.  Still, it's safest to use theD qualifier if you're NOT using %LOC or VOLATILE on the argument name.E (Oh yeah - on AXP, some arguments are passed in registers, but if you B use %LOC, the compiler "homes" the argument list to memory so that it works as it does on a VAX.)  D So...  If your Fortran routines use %LOC to test whether an argumentA is present, V6 shouldn't break your application.  If you use some D other method, you'll need to use /ASSUME=DUMMY_ALIASES.  I'll try to@ get this all in the documentation and/or release notes in future	 versions.   9 Steve Lionel                    lionel@quark.enet.dec.com  SDT Languages Group  Digital Equipment Corporation  110 Spit Brook Road  Nashua, NH 03062  