home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / fortran / 4419 < prev    next >
Encoding:
Internet Message Format  |  1992-11-23  |  7.6 KB

  1. Path: sparky!uunet!usc!cs.utexas.edu!tamsun.tamu.edu!snorkelwacker.mit.edu!ai-lab!life.ai.mit.edu!burley
  2. From: burley@apple-gunkies.gnu.ai.mit.edu (Craig Burley)
  3. Newsgroups: comp.lang.fortran
  4. Subject: ENTRY (was Re: Jumping from nested subroutine to main directly?)
  5. Date: 23 Nov 92 10:51:06
  6. Organization: Free Software Foundation 545 Tech Square Cambridge, MA 02139
  7. Lines: 147
  8. Message-ID: <BURLEY.92Nov23105106@apple-gunkies.gnu.ai.mit.edu>
  9. References: <1992Nov21.015026.17820@netcom.com>
  10.     <DODD.92Nov21071326@mycenae.cchem.berkeley.edu>
  11.     <By2xxC.JAB@mentor.cc.purdue.edu>
  12.     <1992Nov21.222705.20726@monu6.cc.monash.edu.au>
  13. NNTP-Posting-Host: apple-gunkies.gnu.ai.mit.edu
  14. In-reply-to: map@hal.maths.monash.edu.au's message of Sat, 21 Nov 1992 22:27:05 GMT
  15.  
  16. In article <1992Nov21.222705.20726@monu6.cc.monash.edu.au> map@hal.maths.monash.edu.au (Michael Page) writes:
  17.  
  18.    One way to almost do what he wants is to use an ENTRY statement at the 
  19.    point he wants to return to in the main program.  However, I suspect he 
  20.    wants something a bit more `automatic' than that, but I don't think it 
  21.    exists in FORTRAN.  Does it exist in C, Pascal, ..?
  22.  
  23. No, ENTRY is not the solution, because a) it is not allowed in a main
  24. program unit (or BLOCK DATA, for that matter); b) even if it was, you'd
  25. just be activating a new procedure corresponding to the main program
  26. unit at a different starting point, not getting back to the old one; and
  27. c) even if that worked, it essentially would amount to an attempt at
  28. recursion, though you probably don't _want_ recursion in terms of activiting
  29. a new set of local variables.  (I.e. why do you want to go back to MAIN?
  30. To share code between the normal return path and the special path?  To
  31. share variables you don't want to put in COMMON?  Because your system
  32. somehow requires a special action from MAIN's stack frame?)
  33.  
  34. To solve the problem at hand, either use a nonportable construct like a
  35. non-local GOTO (for example, perhaps there's a facility on your system for
  36. accessing, say, C's setjmp/longjmp or PL/1's signalling mechanism from
  37. Fortran), or for portability on any ANSI FORTRAN 77 system, use alternate
  38. returns.  The latter requires that all possible intervening procedures be
  39. subroutines and have at least one alternate-return argument that, when
  40. selected, just causes its caller's alternate-return argument to be selected,
  41. as in the following:
  42.  
  43.     PROGRAM FOO
  44.     ...
  45.     CALL SUBR1(...,*999)
  46.     ...
  47. 999    (cleanup code)
  48.     END
  49.  
  50.     SUBROUTINE SUBR1(...,*)
  51.     ...
  52.     CALL SUBR2(...,*999)
  53.     ...
  54. 999    RETURN 1    (assumes the * is the first such in the dummy arglist)
  55.     END
  56.  
  57.     SUBROUTINE SUBR2(...,*)
  58.     ...
  59.     CALL SUBR3(...,*999)    (SUBR3 omitted for brevity :-)
  60.     ...
  61. C Here's what this subroutine does to "go to" MAIN's 999
  62.     RETURN 1
  63. C Or, more generally, it could do
  64.     GOTO 999
  65.     ...
  66. 999    RETURN 1    (assumes the * is the first such)
  67.     END
  68.  
  69. Of course, not only are adding these alternate-returns and converting
  70. FUNCTIONs to SUBROUTINEs annoying, but if you don't control the source
  71. code for the intervening procedures, it might be impossible.
  72.  
  73.    (Which also prompts me to ask why the ENTRY statement is so frowned upon 
  74.    that it is now deprecated?  Is it superceded by something else?)
  75.  
  76. Well, perhaps among other things I can't think of right now, ENTRY means to do
  77. the following (numbers for identification only):
  78.  
  79. 1.  Share code among different procedures for easier maintenance
  80.  
  81. 2.  Persuade the compiler to produce less code for a given set of procedures,
  82.     unless the compiler happens to "explode" all the entry point anyway (i.e.
  83.     this is a "weak" reason in the sense that the standard says nothing about
  84.     obeying it)
  85.  
  86. 3.  Share non-COMMON data among different procedures
  87.  
  88. Other reasons that are really combinations of the above:
  89.  
  90. A)  Provide more than one name for a given procedure, as in a library
  91.     (really #s 1 and 2, and perhaps 3 as well if it involves SAVEd data)
  92.  
  93. B)  For a number of similar procedures, reduce the need to retype lots of
  94.     identical declarations (really #1)
  95.  
  96. Perhps others can add more reasons (perhaps I can immediately after
  97. posting this :-), but I'll pretend for now that the above list is
  98. definitive.
  99.  
  100. Fortran 90 addresses #3, which is really the only "functional" use for ENTRY
  101. from a programming point of view.  Memory availability has made #2 kind of
  102. less important.  #1 can be solved via Fortran-90's INCLUDE facility, better
  103. program-development tools, and so on.
  104.  
  105. ENTRY is one of many examples of features in languages like Fortran and C
  106. where the feature really is thought of as enabling several different
  107. "primitive" features at once, even though those primitive features might well
  108. be orthagonal to each other.
  109.  
  110. So, when a "compound feature" like ENTRY has one or more of its major
  111. "primitive" features like #3 replicated in what is felt to be a more
  112. maintainable, less error-prone way as is the case with Fortran 90, the
  113. compound feature often becomes deprecated.  This is especially true when
  114. implementation of the compound feature by compiler people is not only
  115. annoying but involves questions like "what does the programmer really mean
  116. to do here? save coding space? that'll cost run time; code more conveniently?
  117. then I can increase code-space usage and generate better code, as long as
  118. I make sure non-COMMON data is shared?".  In GNU Fortran, for example, I
  119. currently (sort of) save code space at the expense of run time (and, perhaps,
  120. optimization as well), but I could fairly easily (with a few days work,
  121. perhaps) reverse that approach.
  122.  
  123. Sometimes, of course, once one of the primitive features is reimplemented
  124. in a better way and that way gets used a lot, then rather than the older
  125. compound feature like ENTRY becoming obsolete/deprecated, it actually takes
  126. on a clearer meaning.
  127.  
  128. E.g., once most Fortran programs use MODULE or other Fortran-90 techniques
  129. to do #3 (share non-COMMON data among procedures), then when ENTRY is seen,
  130. it's pretty certain the programmer intended #1 or #2 -- in this case, still
  131. a difficult choice for the compiler without guidance.  Further, if most
  132. programs use INCLUDE or CASE techniques to achieve #1, then ENTRY will
  133. finally mean pretty much just this: "share code to save code space even
  134. at the expense of slower run time".  At that point, though ENTRY will still
  135. be semantically a compound feature, it'll be practically _more_ useful
  136. and directed in its everyday meaning and use.
  137.  
  138. So, I don't think of ENTRY (and other such features) as a compound feature
  139. finally ready to be put out to pasture so much as a currently inscrutable
  140. compound feature that might someday become, in practice, a fairly direct
  141. way to accomplish a particular goal.
  142.  
  143. Other similar examples of compound features are SAVE (controls, potentially,
  144. several implementation aspects) and C's "static" keyword as applied to local
  145. variables (similar to Fortran's SAVE in a way).  In both cases they have
  146. definite semantic meaning for the running program but also are informally
  147. yet practically used to control placement of variables/arrays in memory,
  148. even though the two things are potentially orthagonal (assuming a
  149. reasonably sophisticated compiler).  For example, one might want an array
  150. to be automatic (not static) in the sense of having one instance per
  151. activation of the owning procedure, but have it be allocated from the heap
  152. instead of the stack due to its size, or allocated in static memory (with
  153. mechanisms to "stack" still-active instances somewhere, like the heap or
  154. the stack) so there's always room for one set aside by the linker and/or
  155. to speed up access to it on, say, a machine that does global access
  156. faster than stack/heap access.
  157.  
  158. Language design is wonderful.
  159. --
  160.  
  161. James Craig Burley, Software Craftsperson    burley@gnu.ai.mit.edu
  162. Member of the League for Programming Freedom (LPF) lpf@uunet.uu.net
  163.