home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / fortran / 2821 < prev    next >
Encoding:
Internet Message Format  |  1992-07-24  |  6.1 KB

  1. Path: sparky!uunet!olivea!mintaka.lcs.mit.edu!ai-lab!life.ai.mit.edu!burley
  2. From: burley@geech.gnu.ai.mit.edu (Craig Burley)
  3. Newsgroups: comp.lang.fortran
  4. Subject: Re: Variable repeat count specifier for FORMAT statments
  5. Message-ID: <BURLEY.92Jul24125501@geech.gnu.ai.mit.edu>
  6. Date: 24 Jul 92 16:55:01 GMT
  7. References: <1992Jul23.215113.16531@jhunix.hcf.jhu.edu>
  8. Sender: news@ai.mit.edu
  9. Distribution: na
  10. Organization: Free Software Foundation 545 Tech Square Cambridge, MA 02139
  11. Lines: 112
  12. In-reply-to: psun@jhunix.hcf.jhu.edu's message of 23 Jul 92 21:51:13 GMT
  13.  
  14. In article <1992Jul23.215113.16531@jhunix.hcf.jhu.edu> psun@jhunix.hcf.jhu.edu (Peter Sun) writes:
  15.  
  16.    My apologies if this questions seems trivial, but I've tried to find the
  17.    answer in so many places.  I need to know how to give a variable as the
  18.    number of repeat counts to a format field descriptor statement.  For
  19.    example I know on VAX/VMS this can be done with something like:
  20.  
  21.               write (*, '(<n>(I8))')
  22.  
  23.    which will repeat the format descriptor I8 n number of times, but I haven't
  24.    found this portable to other compilers.  Namely, the MIPS Fortran 77
  25.    compiler for the SGI IRIS system.  Is there a standard for this?  Or is it
  26.    just compiler dependent.
  27.  
  28. What you show is nonstandard and probably doesn't work on VAX/VMS either.
  29. Perhaps what you mean is _this_ nonstandard construct:
  30.  
  31.       WRITE (*,10)
  32. 10    FORMAT (<N>(I8))
  33.  
  34. (where N is an integer variable that has the repeat count at the time of the
  35. WRITE statement's execution)
  36.  
  37. It is possible VAX/VMS FORTRAN has been further extended to support the
  38. example you show, because your example has a _constant_ FORMAT expression,
  39. but it wouldn't support a variable FORMAT, as in "WRITE (*,A(1:20))",
  40. containing the "<expr>" construct.
  41.  
  42. To do this sort of thing in ANSI FORTRAN 77 is a real pain.  You have
  43. to convert the expression (the value of N in the example above) to a
  44. character string and insert that string in a character variable containing
  45. the desired format.  Then use the character variable as the run-time format
  46. string for the operation.  And even this isn't really enough to handle the
  47. full capabilities of the VAX/VMS feature -- consider:
  48.  
  49.       READ (*,10) I,J,K
  50. 10    FORMAT (I8,I8,<K-J+1>I8)
  51.  
  52. I'm quite sure (having seen how the compiled code works) that VMS FORTRAN will
  53. compile this so that I and J are read _first_, then K-J+1 is evaluated to
  54. determine the repeat count for the final I8.  You can't possibly do this,
  55. as far as I know, by encoding a single format string for a single I/O
  56. operation -- and as far as I remember in FORTRAN 77 there are indeed I/O
  57. operations that cannot be split into multiple I/O operations (since things
  58. like '$' are nonstandard), though I might be wrong about that.
  59.  
  60. How does VMS FORTRAN handle the construct, asks the curious person?
  61.  
  62. Well, the compiler produces a structure containing the encoded info on the
  63. compile-time FORMAT statement for use by the run-time library when the
  64. I/O statement is encountered.  The structure has basically a list of items
  65. specifying things like repeat counts, and each place where it needs to
  66. represent an integer, it can do so in a variety of ways -- in a compact
  67. form for a short-enough integer, a less-compact form(s) for (a) larger
  68. integer(s), and, interestingly, a pointer to a function for a run-time
  69. evaluation.  (The choice is signaled in the structure itself, of course.)
  70.  
  71. So, when the FORMAT evaluator in the run-time library is called to evaluate
  72. one of these precompiled structures (as vs. a run-time FORMAT evaluator,
  73. which evaluates a character string), it pulls out the necessary values
  74. either by a "load" kind of machine operation or, where appropriate, a
  75. procedure call to the function, which is expected to return the correct
  76. integer value.
  77.  
  78. Thus, the compiler has already made a little internal statement function that
  79. might look like
  80.  
  81.       __FMT1() = N
  82.  
  83. or
  84.  
  85.       __FMT2() = K-J+1
  86.  
  87. that is purely internal (and I'm just showing the Fortran-like representation
  88. of it, since I'm writing this for Fortran people, not C or VAX assembler
  89. people).
  90.  
  91. And the compiler has placed a pointer to the appropriate internal function
  92. in the precompiled structure containing the FORMAT info.
  93.  
  94. Clearly a run-time version of this cannot easily be done, since the expression
  95. can be fairly arbitrary and would have to be evaluated at run time, along with
  96. having available a list of all known variable, array, and function names and
  97. their locations in memory -- and if the run-time expression contained the
  98. only reference in a program to a particular intrinsic or procedure, "selective"
  99. linking would be impossible for the program (since _any_ procedure accessible
  100. to the program would have to be linked in case a run-time format referenced
  101. it).  (Maybe the VMS FORTRAN spec disallows such references anyway; I don't
  102. want to run down to the basement to find out.)
  103.  
  104. A smart compiler (like GNU Fortran might become someday :-) could, if unable
  105. to do the internal-procedure trickier used by VMS FORTRAN, translate many
  106. of the instances of the VMS-supported compile-time FORMAT having a run-time
  107. expression to code that ran on any machine (by compiling direct-evaluation
  108. code in with each I/O statement to take place before the I/O).  Such an
  109. approach wouldn't be able to handle the READ... I,J,K example I gave earlier,
  110. but the compiler should be able to complain about any such failures as being
  111. unsupportable.  But GNU Fortran's front end already supports the syntax
  112. of this feature, and perhaps we might be able to get it to do the right thing
  113. for most or all of the machines to which it ports (which would require using
  114. a run-time library other than f2c or equivalent, since it uses purely
  115. character-string-based FORMAT evaluation at run time).
  116.  
  117. Funny, this question ultimately is much like the question of how to do
  118. run-time evaluation of expressions, which also is fairly easy once you know
  119. what you're doing, and tricky otherwise.  Perhaps the best answer to all
  120. such questions is: use Lisp, Smalltalk, or Prolog instead.  :-)
  121. (REALLY, I'M KIDDING, SEE THE SMILEY, FORTRAN'S FINE! :-)
  122. --
  123.  
  124. James Craig Burley, Software Craftsperson    burley@gnu.ai.mit.edu
  125. Member of the League for Programming Freedom (LPF)
  126.