gentran.tex 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. \chapter{GENTRAN: A code generation package}
  2. \label{GENTRAN}
  3. \typeout{{GENTRAN: A code generation package}}
  4. {\footnotesize
  5. \begin{center}
  6. Barbara L. Gates \\
  7. RAND \\
  8. Santa Monica CA 90407-2138 \\
  9. U.S.A. \\[0.1in]
  10. Michael C. Dewar \\
  11. School of Mathematical Sciences, The University of Bath \\
  12. Bath BA2 7AY, England \\[0.05in]
  13. e--mail: mcd@maths.bath.ac.uk
  14. \end{center}
  15. }
  16. \ttindex{GENTRAN}
  17. GENTRAN is an automatic code GENerator and TRANslator which runs under
  18. \REDUCE. It constructs complete numerical programs based on sets of
  19. algorithmic specifications and symbolic expressions. Formatted
  20. FORTRAN, RATFOR, PASCAL or C code can be generated through a series of
  21. interactive commands or under the control of a template processing
  22. routine. Large expressions can be automatically segmented into
  23. subexpressions of manageable size, and a special file-handling
  24. mechanism maintains stacks of open I/O channels to allow output to be
  25. sent to any number of files simultaneously and to facilitate recursive
  26. invocation of the whole code generation process. GENTRAN provides the
  27. flexibility necessary to handle most code generation applications. It
  28. is designed to work with the SCOPE code optimiser.
  29. GENTRAN is a large system with a great many options. This section
  30. will only describe the FORTRAN generation facilities, and in broad
  31. outline only. The full manual is available as part of the \REDUCE\
  32. documentation.
  33. \section{Simple Use}
  34. A substantial subset of all expressions and statements in the \REDUCE{}
  35. programming language can be translated directly into numerical code.
  36. The {\bf GENTRAN} command takes a \REDUCE\ expression, statement, or
  37. procedure definition, and translates it into code in the target
  38. language.
  39. \begin{describe}{Syntax:}
  40. {\bf GENTRAN} {\it stmt} [ {\bf OUT} {\it f1,f2,\dots\ ,fn} ]{\it ;}
  41. \end{describe}
  42. {\it stmt} is any \REDUCE\ expression, statement (simple, compound, or
  43. group), or procedure definition that can be translated by GENTRAN into the
  44. target language.
  45. {\it stmt} may contain any number of calls
  46. to the special functions {\bf EVAL}, {\bf DECLARE}, and {\bf LITERAL}.
  47. {\it f1,f2,\dots\ ,fn } is an optional argument list containing one or more
  48. {\it f}'s, where each {\it f} is one of:
  49. \par
  50. \begin{tabular}{lll}
  51. {\it an atom} &= &an output file\\
  52. {\bf T} &= &the terminal\\
  53. {\bf NIL} &= &the current output file(s)\\
  54. \ttindex{ALL"!*} {\bf ALL!*} &= &all files currently open for output \\
  55. & & by GENTRAN (see section~\ref{GENTRAN:output})\\
  56. \end{tabular}
  57. If the optional part of the command is not given, generated code is simply
  58. written to the current output file. However, if it is
  59. given, then the current output file is temporarily overridden. Generated
  60. code is written to each file represented by
  61. {\it f1,f2,\dots\ ,fn} for this command only. Files which were open prior
  62. to the call to {\bf GENTRAN} will remain open after the call, and files
  63. which did not exist prior to the call will be created, opened, written to,
  64. and closed. The output stack will be exactly the same both before and
  65. after the call.
  66. {\bf GENTRAN} returns the name(s) of the file(s) to which code was
  67. written.
  68. \index{GENTRAN package ! example}
  69. \begin{verbatim}
  70. 1: GENTRANLANG!* := 'FORTRAN$
  71. 2: GENTRAN
  72. 2: FOR I:=1:N DO
  73. 2: V(I) := 0$
  74. DO 25001 I=1,N
  75. V(I)=0.0
  76. 25001 CONTINUE
  77. \end{verbatim}
  78. \section{Precision}
  79. \label{precision}
  80. \index{precision}\index{DOUBLE switch}
  81. By default {\bf GENTRAN} generates constants and type declarations in
  82. single precision form. If the user requires double precision output
  83. then the switch {\bf DOUBLE} must be set {\bf ON}.
  84. \index{PRECISION command}\index{PRINT"!-PRECISION command}
  85. To ensure the correct number of floating point digits are
  86. generated it may be necessary to use either the {\bf PRECISION} or
  87. {\bf PRINT!-PRECISION} commands. The former alters the number of
  88. digits \REDUCE\ calculates, the latter only the number of digits
  89. \REDUCE\ prints. Each takes an integer argument. It is not possible to set
  90. the printed precision higher than the actual precision. Calling {\bf
  91. PRINT!-PRECISION} with a negative argument causes the printed
  92. precision to revert to the actual precision.
  93. \subsection{The EVAL Function}
  94. \label{eval}
  95. \begin{describe}{Syntax:}
  96. {\bf EVAL} {\it exp}
  97. \end{describe}\ttindex{EVAL}
  98. \begin{describe}{Argument:}
  99. {\it exp} is any \REDUCE\ expression or statement which, after evaluation
  100. by \REDUCE, results in an expression that can be translated by
  101. GENTRAN into the target language.
  102. \end{describe}
  103. When {\bf EVAL} is called on an expression which is to be translated, it
  104. tells {\bf GENTRAN} to give the expression to \REDUCE\ for evaluation
  105. first, and then to translate the result of that evaluation.
  106. \begin{verbatim}
  107. f;
  108. 2
  109. 2*X - 5*X + 6
  110. \end{verbatim}
  111. We wish to generate an assignment statement for the quotient
  112. of F and its derivative.
  113. \begin{verbatim}
  114. 1: GENTRAN
  115. 1: Q := EVAL(F)/EVAL(DF(F,X))$
  116. Q=(2.0*X**2-(5.0*X)+6.0)/(4.0*X-5.0)
  117. \end{verbatim}
  118. \subsection{The :=: Operator}
  119. \index{:=:}
  120. \label{rsetq}\index{GENTRAN ! preevaluation}\index{rsetq operator}
  121. In many applications, assignments must be generated in which the
  122. left-hand side is some known variable name, but the
  123. right-hand side is an expression that must be evaluated. For
  124. this reason, a special operator is provided to indicate that the expression
  125. on the right-hand side is to be evaluated prior to translation. This
  126. special operator is {\bf :=:} ({\em i.e.} the usual \REDUCE\ assignment operator
  127. with an extra ``:'' on the right).
  128. \begin{describe}{\example}
  129. \begin{verbatim}
  130. 1: GENTRAN
  131. 1: DERIV :=: DF(X^4-X^3+2*x^2+1,X)$
  132. DERIV=4.0*X**3-(3.0*X**2)+4.0*X
  133. \end{verbatim}
  134. \end{describe}
  135. \subsection{The ::= Operator}
  136. \label{lsetq}
  137. \index{matrices ! in GENTRAN}
  138. When assignments to matrix or array elements must be generated, many
  139. times the indices of the element must be evaluated first. The special
  140. operator\index{::=}\index{lsetq operator}
  141. {\bf ::=} can be used within a call to {\bf GENTRAN}
  142. to indicate that the indices of the matrix or
  143. array element on the left-hand side of the assignment are to
  144. be evaluated prior to translation. (This is the usual \REDUCE{}
  145. assignment operator with an extra ``:'' on the left.)
  146. \begin{describe}{\example}
  147. We wish to generate assignments which assign zeros to all elements
  148. on the main diagonal of M, an n x n matrix.
  149. \begin{verbatim}
  150. 10: FOR j := 1 : 8 DO
  151. 10: GENTRAN
  152. 10: M(j,j) ::= 0$
  153. M(1,1)=0.0
  154. M(2,2)=0.0
  155. :
  156. :
  157. M(8,8)=0.0
  158. \end{verbatim}
  159. \end{describe}
  160. {\bf LSETQ} may be used interchangeably with {\bf ::=} on input.\ttindex{LSETQ}
  161. \subsection{The ::=: Operator}
  162. \label{lrsetq}
  163. \index{::=:} \index{lrsetq operator}
  164. In applications in which evaluated expressions are to be assigned to
  165. array elements with evaluated subscripts, the {\bf ::=:} operator can be
  166. used. It is a combination of the {\bf ::=} and {\bf :=:} operators described
  167. in sections~\ref{rsetq} and ~\ref{lsetq}.
  168. \index{matrices ! in GENTRAN}
  169. \begin{describe}{\example}
  170. The following matrix, M, has been derived symbolically:
  171. \newpage
  172. \begin{verbatim}
  173. ( A 0 -1 1)
  174. ( )
  175. ( 0 B 0 0)
  176. ( )
  177. ( -1 0 C -1)
  178. ( )
  179. ( 1 0 -1 D)
  180. \end{verbatim}
  181. We wish to generate assignment statements for those elements
  182. on the main diagonal of the matrix.
  183. \begin{verbatim}
  184. 10: FOR j := 1 : 4 DO
  185. 10: GENTRAN
  186. 10: M(j,j) ::=: M(j,j)$
  187. M(1,1)=A
  188. M(2,2)=B
  189. M(3,3)=C
  190. M(4,4)=D
  191. \end{verbatim}
  192. \end{describe}
  193. The alternative alphanumeric identifier associated with {\bf ::=:} is
  194. {\bf LRSETQ}.\ttindex{LRSETQ}
  195. \section{Explicit Type Declarations}
  196. \label{explicit:type}
  197. Type declarations are automatically generated each time a subprogram
  198. heading is generated. Type declarations are constructed
  199. from information stored in the GENTRAN symbol table. The user
  200. can place entries into the symbol table explicitly through calls
  201. to the special GENTRAN function {\bf DECLARE}.\index{DECLARE function}
  202. \begin{describe}{Syntax:}
  203. {\bf \ \ DECLARE} {\it v1,v2,\dots\ ,vn} {\bf :} {\it type;}
  204. or
  205. \begin{tabular}{ll}
  206. {\bf DECLARE}\\
  207. {\bf $<$$<$}\\
  208. &{\it v11,v12,\dots\ ,v1n} {\bf :} {\it type1;}\\
  209. &{\it v21,v22,\dots\ ,v2n} {\bf :} {\it type2;}\\
  210. & :\\
  211. & :\\
  212. &{\it vn1,vnn,\dots\ ,vnn} {\bf :} {\it typen;}\\
  213. {\bf $>$$>$}{\it ;}
  214. \end{tabular}
  215. \end{describe}
  216. \begin{describe}{Arguments:}
  217. Each {\it v1,v2,\dots\ ,vn} is a list of one or more variables
  218. (optionally subscripted to indicate array dimensions), or
  219. variable ranges (two letters separated by a ``-''). {\it v}'s are
  220. not evaluated unless given as arguments to {\bf EVAL}.
  221. Each {\it type} is a variable type in the target language. Each
  222. must be an atom, optionally preceded by the atom {\bf IMPLICIT}.
  223. \index{IMPLICIT option}
  224. {\it type}'s are not evaluated unless given as arguments to {\bf EVAL}.
  225. \end{describe}
  226. The {\bf DECLARE} statement can also be used to declare subprogram
  227. types ({\em i.e.\ } {\bf SUBROUTINE} or {\bf FUNCTION}) for
  228. \index{SUBROUTINE}\index{FUNCTION} FORTRAN and RATFOR code, and
  229. function types for all four languages.
  230. \section{Expression Segmentation}
  231. \label{segmentation}\index{segmenting expressions}
  232. Symbolic derivations can easily produce formulas that can be anywhere
  233. from a few lines to several pages in length. Such formulas
  234. can be translated into numerical assignment statements, but unless they
  235. are broken into smaller pieces they may be too long for a compiler
  236. to handle. (The maximum number of continuation lines for one statement
  237. allowed by most FORTRAN compilers is only 19.) Therefore GENTRAN
  238. \index{continuation lines}
  239. contains a segmentation facility which automatically {\it segments},
  240. or breaks down unreasonably large expressions.
  241. The segmentation facility generates a sequence of assignment
  242. statements, each of which assigns a subexpression to an automatically
  243. generated temporary variable. This sequence is generated in such a
  244. way that temporary variables are re-used as soon as possible, thereby
  245. keeping the number of automatically generated variables to a minimum.
  246. The facility can be turned on or off by setting the mode
  247. \index{GENTRANSEG switch} switch {\bf GENTRANSEG} accordingly ({\em
  248. i.e.\ }by calling the \REDUCE\ function {\bf ON} or {\bf OFF} on it). The user
  249. can control the maximum allowable expression size by setting the
  250. \ttindex{MAXEXPPRINTLEN"!*}
  251. variable {\bf MAXEXPPRINTLEN!*} to the maximum number of characters
  252. allowed in an expression printed in the target language (excluding
  253. spaces automatically printed by the formatter). The {\bf GENTRANSEG}
  254. switch is on initially, and {\bf MAXEXPPRINTLEN!*} is initialised to
  255. 800.
  256. \section{Template Processing}\label{GENTRAN:template}
  257. \index{GENTRAN ! templates}\index{templates}\index{code templates}
  258. In some code generation applications pieces of the target numerical
  259. program are known in advance. A {\it template} file containing a
  260. program outline is supplied by the user, and formulas are derived in
  261. \REDUCE, converted to numerical code, and inserted in the corresponding
  262. places in the program outline to form a complete numerical program. A
  263. template processor is provided by GENTRAN for use in these
  264. applications.
  265. \label{templates}\index{GENTRANIN command}
  266. \begin{describe}{Syntax:}
  267. {\bf GENTRANIN} {\it f1,f2,\dots\ ,fm} [{\bf OUT} {\it f1,f2,\dots\
  268. ,fn\/}]{\it ;}
  269. \end{describe}
  270. \begin{describe}{Arguments:}
  271. {\it f1,f2,\dots\ ,fm\/} is an argument list containing one or more
  272. {\it f\/}'s,
  273. where each {\it f\/} is one of:
  274. \begin{center}
  275. \begin{tabular}{lll}
  276. {\it an atom}& = &a template (input) file\\
  277. {\bf T}& = &the terminal\\
  278. \end{tabular}
  279. \end{center}
  280. {\it f1,f2,\dots\ ,fn\/} is an optional argument list containing one or more
  281. {\it f\/}'s, where each {\it f\/} is one of:
  282. \begin{center}
  283. \begin{tabular}{lll}
  284. {\it an atom}& = &an output file\\
  285. {\bf T}& = &the terminal\\
  286. {\bf NIL}& = &the current output file(s)\\
  287. {\bf ALL!*}& = &all files currently open for output \\
  288. & & by GENTRAN (see section~\ref{GENTRAN:output}) \\
  289. \end{tabular}
  290. \end{center}
  291. \end{describe}
  292. {\bf GENTRANIN} processes each template file {\it f1,f2,\dots\ ,fm}
  293. sequentially.
  294. A template file may contain any number of parts, each of which
  295. is either an active or an inactive part. All active parts start with
  296. the character sequence {\bf ;BEGIN;} and end with {\bf ;END;}. The end
  297. of the template file is indicated by an extra {\bf ;END;} character
  298. sequence.\index{;BEGIN; marker} \index{;END; marker}
  299. Inactive parts of template files are assumed to contain code in the
  300. target language. All inactive parts are
  301. copied to the output.
  302. Active parts may contain any number of \REDUCE\ expressions, statements,
  303. and commands. They are not copied directly to the output. Instead,
  304. they are given to \REDUCE\ for evaluation in algebraic mode. All output
  305. generated by each evaluation is sent to the output file(s). Returned
  306. values are only printed on the terminal.\index{GENTRAN ! preevaluation}
  307. Active parts will most likely contain calls to {\bf GENTRAN} to
  308. generate code. This means that the result of processing a
  309. template file will be the original template file with all active
  310. parts replaced by generated code.
  311. If {\bf OUT} {\it f1,f2,\dots\ ,fn} is not given, generated code is simply
  312. written to the current-output file.
  313. However, if {\bf OUT} {\it f1,f2,\dots\ ,fn}
  314. is given, then the current-output file
  315. is temporarily overridden. Generated code is written to each file
  316. represented by {\it f1,f2,\dots\ ,fn} for this command only. Files
  317. which were open prior to the call to {\bf GENTRANIN} will remain open
  318. after the call, and files which did not exist prior to the call will
  319. be created, opened, written to, and closed. The output-stack will be
  320. exactly the same both before and after the call.
  321. {\bf GENTRANIN} returns the names of all files written to by this
  322. command.
  323. \newpage
  324. \begin{describe}{\example}
  325. Suppose we wish to generate a FORTRAN subprogram to compute the
  326. determinant of a 3 x 3 matrix. We can construct a template
  327. file with an outline of the FORTRAN subprogram and \REDUCE\ and
  328. GENTRAN commands to fill it in:
  329. \index{matrices ! in GENTRAN}
  330. Contents of file {\tt det.tem}:
  331. \end{describe}
  332. \begin{verbatim}
  333. REAL FUNCTION DET(M)
  334. REAL M(3,3)
  335. ;BEGIN;
  336. OPERATOR M$
  337. MATRIX MM(3,3)$
  338. MM := MAT( (M(1,1),M(1,2),M(1,3)),
  339. (M(2,1),M(2,2),M(2,3)),
  340. (M(3,1),M(3,2),M(3,3)) )$
  341. GENTRAN DET :=: DET(MM)$
  342. ;END;
  343. RETURN
  344. END
  345. ;END;
  346. \end{verbatim}
  347. \begin{describe}{}
  348. Now we can generate a FORTRAN subprogram with the following
  349. \REDUCE\ session:
  350. \begin{verbatim}
  351. 1: GENTRANLANG!* := 'FORTRAN$
  352. 2: GENTRANIN
  353. 2: "det.tem"
  354. 2: OUT "det.f"$
  355. \end{verbatim}
  356. Contents of file det.f:
  357. \end{describe}
  358. \begin{verbatim}
  359. REAL FUNCTION DET(M)
  360. REAL M(3,3)
  361. DET=M(3,3)*M(2,2)*M(1,1)-(M(3,3)*M(2,1)*M(1,2))-(M(3,2)
  362. . *M(2,3)*M(1,1))+M(3,2)*M(2,1)*M(1,3)+M(3,1)*M(2,3)*M(1
  363. . ,2)-(M(3,1)*M(2,2)*M(1,3))
  364. RETURN
  365. END
  366. \end{verbatim}
  367. \section{Output Redirection}\label{GENTRAN:output}
  368. \index{GENTRAN ! file output}
  369. \index{GENTRANOUT command}\index{GENTRANSHUT command}
  370. The {\bf GENTRANOUT} and {\bf GENTRANSHUT} commands are identical to
  371. the \REDUCE\ {\bf OUT} and {\bf SHUT} commands with the following
  372. exceptions:
  373. \begin{itemize}
  374. \item {\bf GENTRANOUT} and {\bf GENTRANSHUT} redirect {\it only\/}
  375. code which is printed as a side effect of GENTRAN commands.
  376. \item {\bf GENTRANOUT} allows more than one file name to be given
  377. to indicate that generated code is to be sent to two or more
  378. files. (It is particularly convenient to be able to
  379. have generated code sent to
  380. the terminal screen and one or more file simultaneously.)
  381. \item {\bf GENTRANOUT} does not automatically erase existing files; it
  382. prints a warning message on the terminal and asks the user whether the
  383. existing file should be erased or the whole command be aborted.
  384. \end{itemize}