home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume8 / prep / part01 next >
Encoding:
Internet Message Format  |  1987-03-01  |  59.2 KB

  1. Subject:  v08i090:  A pre-processor for FORTRAN source, Part01/02
  2. Newsgroups: mod.sources
  3. Approved: mirror!rs
  4.  
  5. Submitted by: cmcl2!bullwinkle!batcomputer!prove (Roger Ove)
  6. Mod.sources: Volume 8, Issue 90
  7. Archive-name: prep/Part01
  8.  
  9. [  Different from RATFOR in some interesting aways, especially the
  10.    parellelizing constructs, if that's a word.  --r$  ]
  11.  
  12. Mod.sources submission:
  13.      This is part 1 of 2  of a preprocessor for fortran, which 
  14. supports macros, flow control extensions, vector statement
  15. shorthand, and automatic loop unrolling for certain classes of
  16. loops.  It is written in generic c and will run on nearly any
  17. machine: ibmpc, Sun, CrayXMP, Definicon dsi20.
  18.  
  19. -----CUT-----HERE-----
  20. # This is a shell archive.  Remove anything before this line,
  21. # then unpack it by saving it in a file and typing "sh file".
  22. # Contents:  prep.doc Makefile makemsc prep.c macro.c vec.c str.c
  23.  
  24. echo x - prep.doc
  25. sed 's/^@//' > "prep.doc" <<'@//E*O*F prep.doc//'
  26.                              PREP v. 2.0
  27.  
  28.                     Copyright (C) 1985,1986 P.R.Ove
  29.                           All rights reserved.
  30.  
  31.      Suggestions and comments regarding this program are welcome,
  32. preferably in the form of code segments.  I will make an effort to
  33. incorporate any suggestions that are deemed worthy and maintain an
  34. "official" version of this program on the net.  At the moment comments
  35. should be directed to 14004@ncsavmsa.bitnet (or equivalently 14004@
  36. ncsaa.cso.uiuc.edu), or prove@uiucmvd.bitnet, or
  37. prove@tcgould.tn.cornell.edu.
  38.  
  39.  
  40.  
  41. Introduction
  42.  
  43.      This documentation describes the use of PREP, a preprocessor for 
  44. fortran.  As an alternative to ratfor, PREP offers some distinct advantages.
  45. These include full macro facilities and a concise shorthand for array
  46. and vector statements.  In addition, all of the standard flow control
  47. constructs of forth are supported.  Some attempts have been made to
  48. avoid ratfor syntax to that both preprocessors can be used, but this
  49. has never been checked fully.  It is probably possible to emulate much of
  50. ratfor's syntax using PREP's macro processor to modify the flow control
  51. commands.  PREP is written is generic c and will run on nearly any
  52. machine/compiler combination.  Currently it runs on IBM pc's and
  53. compatibles, unix machines, the Definicon dsi20 68020 parasite card in
  54. an IBM PC compatible machine, and the Cray XMP.
  55.      PREP does not do everything, and in particular does not offer any
  56. help with the deficiency of data structures in fortran.  It also does not
  57. understand fortran, and will quite happily produce nonsense code if so
  58. instructed.  It will detect errors in its own syntax, but errors in 
  59. fortran will be left for the compiler.  Therefore debugging will 
  60. unfortunately involve looking at the fortran output, which can be quite
  61. ugly.  These problems are shared with ratfor.
  62.      The vector statement notation makes it possible to incorporate do
  63. loop unrolling automatically to any depth, which for certain classes of
  64. loops on certain machines (memory bound loops on vector machines) will
  65. improve performance.  On the Cray XMP performance for certain loops
  66. was increased from the normal 50 Mflops to a maximum of 80 Mflops when
  67. unrolled to a depth of 16.  On machines with many parallel paths to
  68. memory there may also be situations where this is advantageous.
  69.      Although the syntax is similar to forth, the spirit of forth is
  70. totally absent.  The macros are really macros, not colon definitions,
  71. and recursive macro definitions will cause an error during expansion.
  72. Postfix notation would only cause confusion, being in conflict with
  73. fortran conventions, and is not used.
  74.     The macro processor can be considered a pre-preprocessor.  The
  75. order of translation is:
  76.  
  77.     1) file inclusion
  78.     2) macro processing
  79.     3) flow control extensions
  80.     4) vector statements
  81.  
  82. Note that because of this the flow control syntax can be modified
  83. at the macro level.  Although this order of translation holds
  84. rigorously, PREP is a one pass processor and makes no temporary
  85. files.
  86.      Macro definitions can be imbedded in the program file or in
  87. files that can later be included.  Some common definitions mapping
  88. certain symbols ( &, <=, !=, etc ) to their fortran equivalents ( .and.,
  89. @.le., .ne., etc ) are stored in the file prepmac.h.  These can be made
  90. active by placing the statement ' #include "prepmac.h" ' in the program
  91. file, or by using the -i switch from the command line.
  92.     The nesting limit for all loops is defined by an internal constant
  93. NESTING, which is set to a number like 10 or 20 (implementation 
  94. dependent).  The flow control directives are permitted inside vector
  95. loops, but since they will inhibit Cray vectorization of those loops it
  96. may be best to avoid this.  One of the reasons for using the vector
  97. shorthand is that it encourages programming in a style that can be
  98. easily vectorized by the compiler.
  99.     This program attempts to avoid all fixed limits on data structures,
  100. and instead allocates memory when needed.  The flow control directives
  101. do not adhere to this philosophy, since the maximum expansion length
  102. can be determined in advance and processing is faster without continual
  103. reallocation of memory.  Fairly robust memory management is used by the
  104. macro processor and input routines (there is no source line length limit
  105. other than any limitations imposed by the system).  Recursive macro
  106. definitions are accepted during the definition phase, but will cause an
  107. error during expansion.  When a macro is expanded more than the limit
  108. (100 or so per line, but implementation dependent) the program will abort
  109. with a recursion error message, but it is conceivable (if the memory of
  110. the machine is small and the macro definitions are very long) that a
  111. memory allocation error will occur before this.
  112.     In most cases the flow control directives must be the first word
  113. on the line (PREP is line oriented like fortran and unlike c).  The
  114. only exception is that directives and fortran code can be on the same
  115. line after an OF statement.  Any delimiters (){}[]'" may be used in the
  116. logical expressions ( i.e.  leave [ i == 1 ] ).  Macro definitions
  117. must use () for the parameter block however (to allow macro names
  118. containing the character {, for instance), and macro names cannot contain
  119. the open parens or whitespace characters.
  120.  
  121.  
  122. Running PREP
  123.  
  124.      The command line interface and program function is identical 
  125. regardless of the machine (so far).  The syntax is
  126.  
  127. prep -x -x .... <file>
  128.  
  129. where file is the first name of the file and the extension is assumed
  130. to be P.  The output file will have the extension F.  x represents
  131. a command line option:
  132.  
  133.  Switches:
  134.    -c        keep comments (truncated at column 72)
  135.    -u        keep underline characters
  136.    -m        only do macro substitution (==> -c and -u as well, and
  137.         prevents file includes (except -i switch).
  138.    -i    <file>    include <file> before processing
  139.    -U n        unroll vector loops to depth n
  140.    -L n        unroll loops with n or fewer lines
  141.    -?        write message about allowed switches
  142.  
  143.      If no file is present standard input and output are used.  The -i
  144. switch requires the full path name of the include file.
  145.      Normally underline characters and comment records are eliminated
  146. unless overridden with a switch.  Quoted underlines (the fortran quote
  147. character is the apostrophe) are never deleted.  In general quoted
  148. characters are safe from PREP, as is text in comment records.
  149.      The -m switch is useful for converting existing programs to PREP
  150. format.  It turns off all PREP functions except macro substitution.  To
  151. partially convert a fortran program, enter:
  152.  
  153. prep -m -i fix.h <prog.f >prog.p
  154.  
  155. The file fix.h contains the inverse definitions of prepmac.h.  A side
  156. effect of PREP on DOS machines is that the terminating control-z is
  157. removed, which is useful if the fortran code is to be transferred to
  158. another machine.  Running the above command without the -i switch and
  159. without any internal macro definitions, it will do nothing but remove the
  160. control-z.
  161.      If the argument for -U is omitted the default is 8.  If -U is not
  162. present then unrolling will not be done at all unless turned on by an
  163. internal directive.  The command line switch will not override imbedded
  164. unrolling commands.  If -L is omitted the default is 1000, while if the
  165. argument is omitted the default is 1.
  166.      Versions for Intel 80*** based machines come compiled for small
  167. and large memory models.  The large model version is quite large
  168. itself.  It is only necessary to use the large model if the memory
  169. is needed for many very long macro definitions, which are memory resident.
  170. If you have a memory allocation error with the small version try the
  171. other.  The large one is called bigprep.exe.  Since I am now distributing
  172. the source you can do what your want.
  173.  
  174.  
  175.  
  176.  
  177. Summary of Features
  178.  
  179.     The extensions can be broken up into four classes: 1) including
  180. files, 2) macro definition/expansion, 3) flow control directives, and
  181. 4) vector notation.  These will be discussed in this order, which is 
  182. also the order in which they are processed.
  183.  
  184.  
  185.  
  186. Included files
  187.   example:
  188.   #include "prepmac.h"
  189.  
  190.      Normally fortran incudes files with the directive "include". 
  191. Incidentally, using cft and precomp on a cray, files are included with
  192. "use" so if you are using a cray you may find it convenient to define
  193. "include" equivalent to "use" with
  194.     : include(x)    use x ;
  195. so that "include 'file'" will be translated to "use file".  Prep will
  196. include a file if it finds an include directive ( #include "file" )
  197. in the source, or if the -i switch is used from the command line.  Included
  198. files can be nested 10 deep.  Only the current directory is searched,
  199. and PREP will terminate if the file is not found.  To include a file in
  200. another directory the full pathname must be used.
  201.  
  202.  
  203. Macros
  204.      The style is similar to that of c #define macros, except
  205. that : is used instead of #define and ; terminates the macro.  No
  206. special character is needed to continue to the next line.  Non-c syntax
  207. is used to allow both PREP macros and c preprocessor macros in the
  208. same program.
  209.      Recursive definitions are permitted, but will cause an abort
  210. (and possibly a memory allocation error) on expansion.  For each
  211. line submitted to expand_macros, a count of is kept for each
  212. stored macro indicating how many times it has been expanded in
  213. the current line.  When this exceeds MAX_CALLS, the program 
  214. assumes a macro definition is recursive and stops.  Macros
  215. are expanded starting with the one with the longest name, so that
  216. if the definitions
  217.  
  218.    : >=        .ge. ;
  219.    : >        .gt. ;
  220.  
  221. are in effect, >= will be changed to .ge. rather than .gt.=.  This
  222. is only a potential problem when macro names are not fully
  223. alphanumeric, since "arg" will not be flagged if "r" is defined.
  224. The underline character is considered non-alphanumeric here, for
  225. no good reason and perhaps it should not be.
  226.  
  227.      The definition phase is invoked when a leading : is found in
  228. the record.  Text is then taken until the terminating ; is found.  Text
  229. following the ; is ignored (until the next newline).  Multi-line macros
  230. are permitted: they will be converted to at least as many lines in the
  231. fortran program.  The general form of a macro definition is:
  232.  
  233.    : name( parm1, parm2, ... )    text with parameters
  234.                 more text with parameters
  235.                  "    "    "    "    ;
  236.  
  237. with 20 as the maximum number of parameters.  There must be no space
  238. between the macro name and the open delimiter of the parameter block in
  239. a definition, and the delimiters (if present) must be ().  Macro names
  240. can not contain the open parens.  Examples of macros with more than
  241. one line are:
  242.  
  243.    : }
  244.     end do ;
  245.    : {
  246.     ;
  247.  
  248. These will allow translation of ratfor style do loops:
  249.  
  250.     do i = 1, 10 { write(*,*) ' i = ', i }
  251.  
  252. is translated into:
  253.  
  254.     do i = 1, 10
  255.     write(*,*) ' i = ', i
  256.     end do
  257.  
  258. which will be translated into fortran during the flow control processing.
  259. Note that this example relies on the fact the whitespace between the
  260. macro definition and its terminating ; is significant (newline is not
  261. considered whitespace here).  This is not the case for whitespace between
  262. the name and the definition.  Failure to have a terminating ; will define
  263. the entire program to be a macro.  This could cause a memory allocation
  264. failure, as macros are stored in memory.
  265.      While in a definition the open parens must follow the name without
  266. whitespace, in the source code this requirement (and the need to use only
  267. () as delimiters) is relaxed.  Alphanumeric macros must be not be next to
  268. an alpha or number character or they will not be recognized.
  269.      The macro definition routine puts the macro string into a more easily
  270. handled format, replacing parameters in the text with n, where n is a
  271. binary value (128 to 128+MAX_TOKENS).  The macro is placed in a structure
  272. of the form:
  273.  
  274. struct mac {
  275.     char *name ;        macro id tag
  276.     char *text ;        encoded macro text
  277.     int  parmcount ;    number of arguments
  278.     int  callcount ;    recursion check
  279. } macro[MAX_MACROS] ;
  280.  
  281. where the text string has binary symbols where the parms were.  Parmcount
  282. is used to see if a parameter block should be searched for when expanding
  283. a macro.  Callcount is used to stop expansion in case of recursive definitions.
  284.      Caution must be exercised to avoid accidental recursive definitions
  285. involving more than one macro:
  286.  
  287.     : h    i+x ;
  288.     : i(y)    func(y) ;
  289.     : func    h ;
  290.  
  291. This will generate the successive strings (from a = func(x)):
  292.  
  293.     a = h(x)
  294.     a = i+x(x)
  295.     a = func()+x(x)
  296.     a = h()+x(x) .... and so on.  Beware.
  297.  
  298.      Macro names will not be flagged if they are quoted (with apostrophes)
  299. in the source, or if they are in comment records.
  300.      If more parameters are found than were present in the definition, the
  301. trailers are ignored.  If fewer are found they will be inserted where
  302. expected only (the missing parameters will be taken to be null strings).
  303. Parameters are separated by commas, and are only recognized if they are
  304. balanced according to delimiters.  If : MACRO(a,b) a + b ; is defined
  305. and
  306.  
  307.     MACRO " [i,j] "
  308.  
  309. is found in the text, only one parameter will be found and it will be
  310. expanded as:
  311.  
  312.     [i,j] +
  313.  
  314. It is not possible to have unbalanced delimiters in a parameter of a 
  315. macro unless the macro only has one argument.
  316.  
  317.  
  318.  
  319. Flow Control Extensions
  320.      These commands are based on the flow control of forth (except for
  321. the do/end_do construct).  With the exception of the OF and DEFAULT
  322. commands, no other text is allowed on the line.  If trailing text is
  323. present it is ignored, leading text will prevent PREP from seeing the
  324. command.  This includes labels: PREP command lines may not have labels
  325. unless macros are used to define labels to expand as continue statements
  326. and newlines.  The commands end_case, end_do, and leave_do can have a
  327. space instead of the underline, but the space is significant.  Of course
  328. a macro could be defined as    : enddo end_do ;.  Unlike some other
  329. languages (forth and c) where CONTINUE applies to all types of loops,
  330. here there are three CONTINUE statements (continue, continue_do, and
  331. continue_case) which apply to the three classes of loops supported by
  332. PREP.  This avoids some confusion in certain situations with nested
  333. loops of differing types.  In general for the flow control extensions,
  334. if optional expressions are omitted they are taken to be TRUE.
  335.  
  336.  
  337.  Forth style begin/while/until/again construct:
  338.      begin ... again
  339.      begin ... while (exp1) ... again
  340.      begin ... until (exp1)
  341.      leave (optional expression) to exit current level
  342.      continue (optional expression) to got back to begin
  343.  
  344.      Here the ...'s represent lines of PREP and fortran code, not on
  345. the same line with the directives.  A working example of one of these
  346. is:
  347.      begin
  348.         line of code
  349.      while ( SOME_MACRO[i] )   ; the macro evaluates to a logical expression
  350.         line of code
  351.         line of code
  352.      again
  353.  
  354. The begin ... again construct will loop forever.  Usually it will have a
  355. leave command inside ( leave [ EOF ], where EOF is a macro ), or a
  356. return to caller.  These (as with the case construct and do/end_do) may
  357. be nested ten levels deep.  The begin is always necessary, even it the
  358. next statement is while.
  359.  
  360.  
  361.  Case construct:
  362.      case ( optional exp )
  363.      of   ( exp2 )  line of code
  364.                     line of code
  365.                     continue_case ( optional logical exp )
  366.      of   ( exp3 )  line of code
  367.      default        line of code
  368.                     line of code
  369.      end_case
  370.  
  371.      This is processed by converting to if else endif expressions.  It is
  372. somewhat clearer in general.  The expressions here must NOT be logical
  373. (.eq. is used), unless CASE is followed by no parameter in which case 
  374. the OF expressions MUST be logical expressions.  Unfortunately fortran
  375. does not allow comparisons between logical expressions using .eq., so
  376. there is no way around this dilemma without having the preprocessor
  377. understand fortran to determine variable types (which in turn would
  378. require that all fortran include directives be processed).  Of course
  379. if the value is logical there is not much sense in using the case
  380. construct instead of and ordinary if/else/endif.  An example of a 
  381. case construct is
  382.  
  383.      c = getchar()    ; function that returns a character value
  384.      case ( c )
  385.      of ( 'q' )   call exit
  386.      of ( 'd' )   call dump
  387.                   continue_case ( not_done )
  388.      default      write(*,*) 'illegal character, try again'
  389.                   continue_case
  390.      end_case
  391.  
  392. In this example the continue statements pass control back to the case, so
  393. getchar is not reevaluated.  If getchar() were put in the case expression
  394. however, it would be evaluated for each OF statement as
  395.  
  396.      if ( 'q' .eq. getchar() ) etc
  397.  
  398. which is probably not what was intended.  Therefore, continue_case is rather
  399. useless here unless the value of variable c is changed by the OF clause.
  400. The example will write indefinitely if any character other than q or d
  401. is entered.  The right way to do this is by switching the 1st 2 lines:
  402.  
  403.      case ( c )
  404.      c = getchar()    ; function that returns a character value
  405.      of ( 'q' )   call exit
  406.            ...
  407.            ...
  408.      end_case
  409.  
  410. This will evaluate the function getchar on entry and once every time
  411. continue_case is encountered.  An example which uses logical expressions is
  412.  
  413.      case
  414.      c = getchar()
  415.      of ( 'q' == c ) call exit
  416.            ...
  417.      end case
  418.  
  419. The nesting limit for case constructs is again 10.  If continue_case
  420. is too long a command name, it can always be abbreviated with a macro
  421. definition (in prepmac.h the definition ": ->case continue_case ;" does
  422. this).
  423.  
  424.  
  425.  
  426.  do ... end_do
  427.  
  428.      The syntax here is like that of vms fortran, except for the leave_do
  429. which jumps out of the loop if the logical expression is true, and 
  430. continue_do which jumps to the end_do and continues the loop.  An
  431. example:
  432.  
  433.      do i = 1, 10
  434.     line of code
  435.      continue do ( i == 2 )    ; goes to end_do if true
  436.     line of code
  437.     line of code
  438.      leave do ( i*j == 4 )    ; exits loop if true
  439.     line of code
  440.      end do
  441.  
  442. The leave_do and continue_do commands cannot be used in normal labeled
  443. do loops.  If the logical expressions are omitted they are assumed
  444. true.
  445.  
  446.  
  447.  
  448.  
  449. Vector Arithmetic
  450.      When writing large number crunching programs in fortran it often
  451. happens that there are a large number of arrays with the same dimensions.
  452. More than likely the loop parameters will be the same for many loops,
  453. and even a simple routine may be rather long and difficult to read
  454. because of all the excess baggage.  It is therefore helpful to have
  455. a shorthand method for writing loops that use common loop parameters.
  456.      A few examples of the shorthand supported by PREP follow.
  457.  
  458.     a(#,#) = b(#,#) + 1
  459.  
  460. This has the obvious meaning that all of the elements of array a are
  461. set equal to those of b incremented by 1.  Assuming the appropriate
  462. default loop parameters have been set, this will be expanded as
  463.  
  464.     do 10001 i001 = 1, my
  465.     do 10000 i000 = 1, mx
  466.     a(i000,i001) = b(i000,i001) + 1
  467. 10000    continue
  468. 10001    continue
  469.  
  470. The labels will be generated uniquely.  The variables i000 -> i009 are
  471. reserved for this purpose.  PREP assumes that the usual fortran 
  472. conventions hold and that variables beginning with i are integers.
  473. In fortran the first index of an array changes the most rapidly as
  474. one proceeds through the memory, so the loops are always generated
  475. with the innermost loop over the first index.  This is essential for
  476. efficiency on machines with virtual memory (VAX) or those that rely
  477. on sequential addressing for vectorization (Cyber).
  478.      More than one line can be placed in the core of a loop by using
  479. square brackets to group them together.
  480.  
  481.     c(#,#) = exp( d(#,#) ) + c(#,#)
  482. [    a(#,#) = b(#,#,1)*c(#,#) - 100
  483.     x = y
  484.     d(#,#) = e(#,#)         ]
  485.  
  486. is expanded as
  487.  
  488.     do 10001 i001 = 1, my
  489.     do 10000 i000 = 1, mx
  490.     c(i000, i001) = exp( d(i000,i001) ) + c(i000,i001)
  491. 10000    continue
  492. 10001    continue
  493.     do 10003 i001 = 1, my
  494.     do 10002 i000 = 1, mx
  495.     a(i000,i001) = b(i000,i001,1)*c(i000,i001) - 100
  496.     x = y
  497.     d(i000,i001) = e(i000,i001)
  498. 10002    continue
  499. 10003    continue
  500.  
  501. Yes the output can get very ugly, but computers don't care.  PREP will 
  502. always continue to the next line if necessary so there is no need
  503. to worry about line length.
  504.     The above loops use default loop limits, and these must be set
  505. with the do_limits command.  The general form is:
  506.  
  507. do_limits [ (mi, mf, minc), (ni, nf, ninc), .... ]
  508.  
  509. The number of triples (do i000=mi, mf, minc) determines how many
  510. indices will be looped over.  If a triple has only 2 elements they are
  511. assumed to be the initial value and final value and the increment is
  512. taken to be 1.  If a triple has just one element (parens then not needed)
  513. it is assumed to be the final value and the initial value and increment
  514. are both taken to be 1.  Therefore the above examples could have their
  515. limits set with
  516.  
  517. do_limits [ mx, my ]
  518.  
  519. Usually the do_limits statement will be tucked out of the way at the
  520. beginning of the program file or in a PREP #include file.  Again the
  521. underline can be replaced by a blank.
  522.      As a rule the number of # symbols in each array should equal the
  523. number of indices implied by the current default limits.  A common
  524. exception is
  525.  
  526.     a(#) = a(#) + b(#,#)*c(#,#)
  527.  
  528. which expands as
  529.  
  530.     do 10001 i001 = 1, my
  531.     do 10000 i000 = 1, mx
  532.     a(i000) = a(i000) + b(i000,i001)*c(i000,i001)
  533. 10000    continue
  534. 10001    continue
  535.  
  536. This does a lot of dot products in parallel on a vector machine like
  537. the Cray.  The compiler will vectorize the inner loop, but is not 
  538. smart enough to realize that the vector a should be kept in a vector
  539. register from one outer iteration to the next, and does an unnecessary
  540. save and fetch each time.  Because this loop is memory bound (the
  541. performance is limited by the time it takes to fetch and store the
  542. data rather than the floating point speed of the machine because
  543. there are so few operations in the loop) the performance can be 
  544. increased by unrolling the loop.  This is done automatically by PREP
  545. to any depth.  Unrolling this example to a depth of 4 gives
  546.  
  547.       do 10001 i001=1,int((1.0*(( my )-1+1))/(1*4))*1*4+1-1,1*4
  548.       do 10000 i000 = 1, ( mx), 1
  549.       a(i000) = a(i000) + b(i000,i001)*c(i000,i001)
  550.       a(i000) = a(i000) + b(i000,i001+1*1)*c(i000,i001+1*1)
  551.       a(i000) = a(i000) + b(i000,i001+1*2)*c(i000,i001+1*2)
  552.       a(i000) = a(i000) + b(i000,i001+1*3)*c(i000,i001+1*3)
  553. 10000 continue
  554. 10001 continue
  555.       do 10003 i001=int((1.0*(( my )-1+1))/(1*4))*1*4+1,( my ),1
  556.       do 10002 i000 = 1, ( mx), 1
  557.       a(i000) = a(i000) + b(i000,i001)*c(i000,i001)
  558. 10002 continue
  559. 10003 continue
  560.  
  561. The second set of loops is a clean up operation.  This technique
  562. improves performance because now the compiler will see that the
  563. same vector will be used in the next vector statement and therefore
  564. keeps it in a register.  The example above which is not unrolled runs
  565. at about 50 Mflops.  Unrolling to a depth of 16 results in a speed
  566. of 80 Mflops (when mx=my=100)
  567.      Unrolling can be controlled with the command line switches
  568. mentioned earlier and with the command
  569.  
  570. unroll ( 8 )
  571.  
  572. imbedded in the source.  The depth must be explicit of course.
  573. Using the imbedded command individual loops can be controlled
  574. independently.
  575.      Unfortunately, using the same trick on more complicated loops
  576. actually degrades performance, since the loops become too
  577. complicated for the optimizer.  For this reason there is a command
  578. line switch -L n, which inhibits unrolling unless the vector
  579. statement is on n or fewer lines.  Unrolling is always disabled
  580. if the number of indices is not greater than 1, since it would
  581. serve no purpose for 1 index loops on the vector machines for
  582. which it is intended (Unrolling a 1 index loop will inhibit
  583. vectorization).  This should perhaps be a command line option
  584. as well, since scalar machines may derive some benefit for such
  585. loops.
  586.      Loops should never be unrolled unless one is certain that
  587. the result is independent of the order over which the indices are
  588. swept.  Usually if a loop is vectorizable on the Cray and can be
  589. written in this notation, it can be unrolled.  A loop such as
  590.  
  591. [    a(#,#) = i
  592.     i = i + 1
  593. ]
  594.  
  595. is not vectorizable and if unrolled the result will not be
  596. independent of the unrolling depth.  Low precision calculations may
  597. show differences depending on the depth because of round off errors.
  598. For instance, if sum is a 32 bit real and a is an array of 32 or
  599. 64 bit reals with a(i,j)=i+mi*j where the dimensions are large,
  600. the loop
  601.  
  602.     sum = sum + a(#,#)
  603.  
  604. may differ in the least significant digits when unrolled.  This is
  605. because when not unrolling (in this example) small numbers have a
  606. chance to add up before being added to large ones.  The unrolled
  607. loops may add small numbers directly to large ones and lose them.
  608. Of course this is just a precision problem and has nothing to do
  609. with the correctness of the algorithm.  Examples could just as
  610. easily be invented where the unrolled version is more accurate.
  611.      Some performance improvements have been noted for scalar
  612. machines.  Parallel processors have not yet been tested but
  613. may allow the most improvement, since the technique will be
  614. of greater assistance if the number of parallel paths to memory
  615. is increased.  In principle each processor could access a local
  616. memory store simultaneously, and unrolling would allow an 
  617. optimizing compiler to realize more easily that fetches could
  618. be done in parallel.  PREP allows such matters to be investigated
  619. without the need for a great deal of text editing to unroll
  620. loops by hand.
  621.  
  622.      However, unrolling do loops is only a small benefit of this
  623. program.  The main reason for using the vector shorthand (and
  624. for using PREP at all) is that using a more intuitively clear
  625. and concise language greatly reduces the time spent making
  626. and correcting mistakes.
  627.  
  628.  
  629.      If you have used this program and have any comments or 
  630. suggestions, they can be sent via lectric-mail to the addresses
  631. mentioned above.
  632.  
  633. @//E*O*F prep.doc//
  634. chmod u=rw,g=r,o=r prep.doc
  635.  
  636. echo x - Makefile
  637. sed 's/^@//' > "Makefile" <<'@//E*O*F Makefile//'
  638. LIBS      = 
  639. OBJS     = prep.o flow.o vec.o misc.o str.o
  640.  
  641. @.SUFFIXES : 
  642. @.SUFFIXES : .o .c
  643.  
  644. prep :: $(OBJS) macro.o
  645.     cc -o prep $(OBJS) macro.o $(LIBS)
  646.  
  647. @.c.o :
  648.     cc -c -O $*.c
  649.  
  650. macro.o : macro.c prepdf.h prep.h
  651.  
  652. $(OBJS) : prep.h prepdf.h
  653.  
  654. @//E*O*F Makefile//
  655. chmod u=rw,g=r,o=r Makefile
  656.  
  657. echo x - makemsc
  658. sed 's/^@//' > "makemsc" <<'@//E*O*F makemsc//'
  659. #----------------------------------------------------------------------
  660. #  MAKEFILE for PREP, msc version, (Kneller make)
  661. #-----------------------------------------------------------------------
  662.  
  663. LINKFLAGS = /stack:10000
  664. LIBS      = c:\lib\\
  665.  
  666. COBJS     = prep.obj flow.obj vec.obj misc.obj
  667.  
  668. @.SUFFIXES : 
  669. @.SUFFIXES : .exe .obj .c
  670.  
  671. prep.exe :: $(COBJS) macro.obj
  672.     @link $<, $@, NUL, $(LIBS) $(LINKFLAGS)
  673.  
  674. @.c.obj :
  675.     msc $* /AS;
  676.  
  677. $(COBJS) :: prep.h  prepdf.h $*.c
  678.  
  679. macro.obj :: prep.h prepdf.h macro.h $*.c
  680. @//E*O*F makemsc//
  681. chmod u=rw,g=r,o=r makemsc
  682.  
  683. echo x - prep.c
  684. sed 's/^@//' > "prep.c" <<'@//E*O*F prep.c//'
  685. /* Program PREP.C
  686.  *
  687.  * Preprocessor for FORTRAN 77.
  688.  * Adds the additional features:
  689.  *
  690.  *  1) Vector arithmetic:
  691.  *     a(#,#,1) = b(#,#) + 1
  692.  *
  693.  *   [ a(#) = b(#)*c(#) - 100
  694.  *     x = y
  695.  *     d(#) = e(#)         ]
  696.  *
  697.  *  2) Case construct:
  698.  *     case ( exp1 )
  699.  *     of   ( exp2 )  line of code
  700.  *                    line of code
  701.  *                    continue_case
  702.  *     of   ( exp3 )  line of code
  703.  *     default        line of code
  704.  *                    line of code
  705.  *     end_case
  706.  *
  707.  *  3) do i = 1, 10
  708.  *        line of code
  709.  *        line of code
  710.  *     leave_do (optional expression)
  711.  *        line of code
  712.  *     continue_do (optional expression)
  713.  *        line of code
  714.  *     end_do
  715.  *
  716.  *  4) forth style begin/while/until/again construct:
  717.  *     begin ... again
  718.  *     begin ... while (exp1) ... again
  719.  *     begin ... until (exp1)
  720.  *     leave (optional expression) to exit current level
  721.  *     continue (optional expression) to go back to beginning
  722.  *
  723.  *  5) Vector loop unrolling to any depth, for loops 
  724.  *     that can be expressed as in #1 above.
  725.  *
  726.  *  6) Macro processing, defined a macro "name" with:
  727.  *     : name(a,b,c)    a = a + func( c, d ) ;
  728.  *
  729.  *  7) Included files:
  730.  *     #include "filename"
  731.  *
  732.  *    The nesting limit for all loops is defined by the constant
  733.  * NESTING in file prepdefs.h.  All underline characters are removed,
  734.  * as are comments if com_keep is NULL.
  735.  *    Any delimeters (){}[]'" may be used in the logical expressions
  736.  * ( i.e.  leave [i .eq. 1] ).
  737.  *    The flow control directives are permitted inside vector
  738.  * loops, but since they will inhibit Cray vectorization of those
  739.  * loops it may be best to avoid this.  One of the reasons for
  740.  * using the vector shorthand is that it encourages programming
  741.  * in a style that can be easily vectorized.
  742.  *    Some attempts have been made to avoid ratfor syntax to that
  743.  * both preprocessors can be used, but this has never been checked.
  744.  *    The number of parameters allowed in a macro is set by the constant
  745.  * MAX_MAC_PARMS in file prepdefs.h (20 is probably more than enough).
  746.  *    Although the syntax is similar to forth, the spirit of
  747.  * forth is totally absent.  The macros are really macros,
  748.  * not colon definitions, and recursive macro definitions will cause
  749.  * an error during expansion.  Postfix notation would only cause
  750.  * confusion, being in conflict with fortran conventions, and is
  751.  * not used.
  752.  *    The macro processor can be considered a pre-preprocessor.  The
  753.  * order of translation is:
  754.  *
  755.  *    1) file inclusion
  756.  *    2) macro processing
  757.  *    3) flow control extensions
  758.  *    4) vector statements
  759.  *
  760.  * Note that because of this the flow control syntax can be modified
  761.  * at the macro level.
  762.  *
  763.  * Switches:
  764.  *   -c        keep comments (truncated at column 72)
  765.  *   -u        keep underline characters
  766.  *   -m        only do macro substitution (==> -c and -u as well, and
  767.  *        prevents file includes (except -i switch).
  768.  *   -i    <file>    include <file> before processing
  769.  *   -U n    unroll vector loops to depth n
  770.  *   -L n    unroll loops with n or fewer lines
  771.  *   -?        write message about allowed switches
  772.  *
  773.  * P. R. OVE  11/9/85
  774.  */
  775.  
  776. #define    MAIN    1
  777. #include "prep.h"
  778.  
  779. main( argc, argv )
  780. int    argc ;
  781. char    *argv[] ;
  782. {
  783. int     i, j, maxlength, lines ;
  784. char    *text ;
  785.  
  786.  
  787. init() ;
  788. parmer( argc, argv ) ;    /* process command line switches */
  789.  
  790. /* copyright notice */
  791. fprintf( stderr,
  792.     "PREP  Copyright (C) 1985,1986 P.R.Ove.  All rights reserved\n" ) ;
  793.  
  794. /* Main loop, loop until true end of file */
  795. while ( 1 ) {
  796.  
  797.     /* get the next record */
  798.     if ( NULL == get_rec() ) break ;
  799.  
  800.     /* comment and blank line filtering */        
  801.     if ( (*in_buff == 'c') | (*in_buff == 'C') | NOT (IN_BUFF_FULL) ) {
  802.         if ( com_keep ) {
  803.             if ( NOT macro_only ) in_buff[72] = NULL ;
  804.             put_string( in_buff ) ;
  805.         }
  806.         continue ;
  807.     }
  808.  
  809.     /* handle file inclusion if not in macro_only mode */
  810.     if ( NOT macro_only ) {
  811.         preproc( rec_type( 0 ) ) ;
  812.         if ( NOT (IN_BUFF_FULL) ) continue ;
  813.     }
  814.  
  815.     /* expand macros in in_buff, result pointed to by text */
  816.     if ( NULL == (text = mac_proc()) ) continue ;    /* NULL ==> macro def */
  817.  
  818.     /* output text here if only doing macro expansion */
  819.     if ( macro_only ) {
  820.         put_string( text ) ;
  821.         free( text ) ;
  822.         continue ;
  823.     }
  824.  
  825.     /* count lines in text, delimit with NULLs, and find the longest line */
  826.     for ( maxlength=0, i=0, j=0, lines=1;; i++, j++ ) {
  827.         if ( text[i] == '\n' ) {
  828.             text[i] = NULL ;
  829.             if ( j>maxlength ) maxlength = j ;
  830.             j = -1 ;
  831.             lines++ ;
  832.             continue ;
  833.         }
  834.         if ( text[i] == NULL ) {
  835.             if ( j>maxlength ) maxlength = j ;
  836.             break ;
  837.         }
  838.     }
  839.  
  840.     /* if necessary expand the output buffer size */
  841.     if ( maxlength > allocation ) {
  842.         allocation = maxlength + maxlength/10 ;
  843.         if ( NULL == (in_buff = realloc( in_buff, allocation )) )
  844.             abort( "reallocation failed" ) ;
  845.         if ( NULL == (out_buff = realloc( out_buff, 4*allocation )) )
  846.             abort( "reallocation failed" ) ;
  847.     }
  848.  
  849.     /* send each line through the passes */
  850.     for ( j=0, i=0; j<lines; j++, i+=strlen(&text[i])+1 ) {
  851.         strcpy( in_buff, &text[i] ) ;
  852.         passes() ;
  853.     }
  854.     
  855.     /* free the storage created by mac_proc */
  856.     free( text ) ;
  857. }
  858.  
  859. fclose( out ) ;
  860. }
  861.  
  862.  
  863.  
  864. /* Do preprocessor passes 1, 2, and 3 on text in in_buff.  Output is
  865.  * also done here.
  866.  */
  867. passes()
  868. {
  869.  
  870. /* process the statement until it is NULL */
  871. while ( IN_BUFF_FULL ) {
  872.  
  873.     preproc( rec_type( 1 ) ) ;
  874.  
  875.     preproc( rec_type( 2 ) ) ;
  876.  
  877.     preproc( rec_type( 3 ) ) ;
  878. }
  879. }
  880.  
  881.  
  882.  
  883. /* initialization */
  884. init() {
  885. int    i ;
  886.  
  887. /* do loop counter variables and flags */
  888. for ( i = 0; i < NESTING; i++ ) {
  889.     sprintf( var_name[i], "i%03d", i ) ;
  890.     leave_do_flag[i] = FALSE ;
  891. }
  892.  
  893. /* Allocate some space for the buffers */
  894. allocation = DEF_BUFFSIZE ;
  895. GET_MEM( in_buff, allocation ) ;
  896. GET_MEM( out_buff, 4*allocation ) ;
  897. }
  898.  
  899.  
  900.  
  901. /* error exit */
  902. abort( string )
  903. char    *string ;
  904. {
  905.     fprintf( stderr, "%s\n", string ) ;
  906.     fprintf( out, "%s\n", string ) ;
  907.     fclose( out ) ;
  908.     exit() ;
  909. }
  910. @//E*O*F prep.c//
  911. chmod u=rw,g=r,o=r prep.c
  912.  
  913. echo x - macro.c
  914. sed 's/^@//' > "macro.c" <<'@//E*O*F macro.c//'
  915. /* MACRO.c
  916.  *
  917.  *   The routines in this file support the macro processing facilities
  918.  * of PREP.  The style is similar to that of c #define macros, except
  919.  * that : is used instead of #define and ; terminates the macro.  
  920.  * This is done to allow both PREP macros and ratfor macros in the
  921.  * same program.
  922.  *   Recursive definitions are permitted, but will cause an abort
  923.  * (and possibley a memory allocation error) on expansion.  For each
  924.  * line submitted to expand_macros, a count of is kept for each
  925.  * stored macro indicating how many times it has been expanded in
  926.  * the current line.  When this exceeds MAX_CALLS, the program 
  927.  * assumes a macro definition is recursive and stops.  Macros
  928.  * are expanded starting with the one with the longest name, so that
  929.  * if the definitions
  930.  *
  931.  * : >=        .ge. ;
  932.  * : >        .gt. ;
  933.  *
  934.  * are in effect, >= will be changed to .ge. rather than .gt.=.  This
  935.  * is only a potential problem when macro names are not fully
  936.  * alphanumeric, since "arg" will not be flagged if "r" is defined.
  937.  *
  938.  * 11/4/86 P.R.OVE
  939.  */
  940.  
  941. #include "macro.h"
  942.  
  943.  
  944. /* Macro processor.
  945.  *
  946.  *   This routine defines and expands macros.  The definition phase
  947.  * is invoked when a leading : is found in the record.  Text is
  948.  * then taken until the terminating ; is found.  Text following the
  949.  * ; is ignored.  Multiline macros are permitted: they will be
  950.  * converted to at least as many lines in the fortran program.
  951.  * Failure to have a terminating ; will define the entire program
  952.  * to be a macro.
  953.  *   A NULL pointer is returned if a macro has been defined.  Otherwise
  954.  * a pointer to the buffer with the expanded text is returned (even if
  955.  * no macros have been expanded).  The buffer is temporary and should
  956.  * be eliminated by the caller.
  957.  */
  958.  
  959. char    *mac_proc()
  960. {
  961. int    i, j, size ;
  962. char    *text, *def ;
  963.  
  964.  
  965. /* see if this is a definition (look for leading :) */
  966. for ( i=0, text=NULL; in_buff[i] != NULL; i++ ) {
  967.     if ( in_buff[i] == BLANK | in_buff[i] == TAB ) continue ;
  968.     if ( in_buff[i] == ':' ) text = &in_buff[i] ;
  969.     break ;
  970. }
  971.  
  972. if ( text == NULL ) {
  973. /* expand macro if not a definition */
  974.     if ( defined_macros == 0 ) {
  975.         GET_MEM( text, strlen(in_buff) ) ;
  976.         strcpy( text, in_buff ) ;
  977.         return( text ) ;
  978.     }
  979.     else return( expand_macros( in_buff ) ) ;
  980.  
  981. }
  982. else {
  983.  
  984. /* macro definition, get characters until ; */
  985.     GET_MEM( def, strlen(text)+10 ) ;
  986.     strcpy( def, text ) ;
  987.     for ( j=1;; j++ ) {
  988.  
  989.         switch ( def[j] ) {
  990.  
  991.         case ';'  :{    def[j+1] = NULL ;
  992.                 define_macro( def ) ;
  993.                 free( def ) ;
  994.                 return( NULL ) ;
  995.             }
  996.             
  997.         case NULL :{    
  998.                 def[j] = '\n' ;
  999.                 def[j+1] = NULL ;
  1000.                 if ( NULL == get_rec() )
  1001.                     abort("MACRO: EOF in macro def") ;
  1002.                 size = strlen(def) + strlen(in_buff) + 10 ;
  1003.                 if ( NULL == (def=realloc(def,size)) )
  1004.                     abort("MACRO: realloc error") ;
  1005.                 strcat( def, in_buff ) ;
  1006.             }
  1007.         }
  1008.     }
  1009. }
  1010. }
  1011.  
  1012.  
  1013.  
  1014.  
  1015. /* Process the macro definition in the argument string.
  1016.  * A macro has the form:
  1017.  *
  1018.  * : name( parm1, parm2, ... )    text with parms ;
  1019.  *
  1020.  * In a definition the delimeter must follow the name
  1021.  * without whitespace.  In the source code this requirement is
  1022.  * relaxed.  Alphanumeric macros must be not be next to an alpha or 
  1023.  * number character or they will not be recognized.
  1024.  *
  1025.  * This routine puts the macro string into a more easily handled
  1026.  * structure, replacing parms in the text with n, where n is a
  1027.  * binary value (128 to 128+MAX_TOKENS).
  1028.  *
  1029.  * The macro is placed in a structure of the form:
  1030.  *
  1031.  *    struct mac {
  1032.  *    char *name ;        macro id tag
  1033.  *    char *text ;        encoded macro text
  1034.  *    int  parmcount ;    number of arguments
  1035.  *    int  callcount ;    recursion check
  1036.  *    } macro[MAX_MACROS] ;
  1037.  *
  1038.  * where the text string has binary symbols where the parms were.
  1039.  * Returns the macro index.  The number of macros defined is stored
  1040.  * in global variable defined_macros.
  1041.  * 
  1042.  * The macros are entered in order of their name length, so that
  1043.  * the macro expander will expand those with long names first.
  1044.  */
  1045.  
  1046. int    define_macro(string)
  1047. char    *string ;
  1048. {
  1049. char    *pntr, *pntr1, *name, *parms[MAX_TOKENS], *parm, *text,
  1050.     *open_parens, *close_parens ;
  1051. int    i, j, l, parmcount ;
  1052.  
  1053. /* macrop is a pointer to the macro structure that will be used */
  1054.     if ( defined_macros >= MAX_MACROS ) {
  1055.         sprintf(errline,"DEFINE_MACRO: too many macros: %s",string);
  1056.         abort( errline ) ;
  1057.     }
  1058.     macrop = ¯o[defined_macros] ;
  1059.     defined_macros++ ;
  1060.  
  1061. /* get the name */
  1062.     name = strtokp( string, ":; \n\t(" ) ;    /* pointer to the name */
  1063.     GET_MEM( macrop->name, strlen(name) ) ;
  1064.     strcpy( macrop->name, name ) ;
  1065.  
  1066. /* get the parameters */
  1067.     for ( i=0; i<MAX_TOKENS; i++ ) parms[i] = NULL ;
  1068.     open_parens = strmatch(string,name) + strlen(name) ;
  1069.     if ( NULL == line_end( open_parens ) ) {
  1070.         sprintf( errline, "DEFINE_MACRO: unterminated: %s", string ) ;
  1071.         abort( errline ) ;
  1072.     }
  1073.  
  1074.     /* get the text storage here to avoid memory allocation tangles */
  1075.     text = open_parens ;
  1076.     GET_MEM( macrop->text, strlen(text) ) ;
  1077.  
  1078.     if ( strchr( "([{\'\"", *open_parens ) ) {
  1079.         if ( NULL == ( close_parens = mat_del( open_parens ) ) ) {
  1080.             sprintf(errline,"DEFINE_MACRO: missing delimeter: %s",
  1081.                 string ) ;
  1082.             abort( errline ) ;
  1083.         }
  1084.         text = close_parens + 1 ;
  1085.         i = (int)(close_parens - open_parens) - 1 ;
  1086.         pntr = open_parens + 1 ;
  1087.         *close_parens = NULL ;
  1088.         for ( i=0, pntr1 = pntr; i<MAX_TOKENS; i++, pntr1 = NULL ) {
  1089.             if ( NULL == ( parm = strtokp( pntr1, ", \t" ) ) )
  1090.                 break ;
  1091.             GET_MEM( parms[i], strlen(parm) ) ;
  1092.             strcpy( parms[i], parm ) ;
  1093.         }
  1094.     }
  1095.  
  1096.     
  1097. /* get the text, plugging in binary codes for parameters */
  1098.  
  1099.     /* remove leading whitespace */
  1100.     if ( NULL == (text=line_end( text )) ) {
  1101.         sprintf( errline, "DEFINE_MACRO: unterminated: %s", string ) ;
  1102.         abort( errline ) ;
  1103.     }
  1104.  
  1105.     /* remove the trailing ';' but NOT whitespace */
  1106.     for ( i=strlen(text)-1; i>=0; i-- ) {
  1107.         if ( text[i] == ';' ) { text[i] = NULL ; break ; }
  1108.     }
  1109.  
  1110.     strcpy( macrop->text, text ) ;
  1111.     text = macrop->text ;
  1112.  
  1113.     for ( i=0; i<MAX_TOKENS & NULL != (parm = parms[i]); i++ ) {
  1114.  
  1115.         /* replace parm by code, if not next to an alpha or number */
  1116.         l = strlen(parm) ;
  1117.         for ( pntr=text;NULL != (pntr1=strmatch(pntr,parm));
  1118.         pntr=pntr1+1 ) {
  1119.             if ( !( isalnum(*(pntr1-1)) && isalnum(*pntr1) ) &
  1120.                  !( isalnum(*(pntr1+l-1)) && isalnum(*(pntr1+l)))) {
  1121.                      *pntr1 = i + 128 ;
  1122.                 strcpy( pntr1 + 1, pntr1 + strlen(parm) ) ;
  1123.             }
  1124.         }
  1125.     }
  1126.  
  1127.     
  1128. /* count parms and free up temporary storage */
  1129.     macrop->parmcount = 0 ;
  1130.     for ( i=0; i<MAX_TOKENS & NULL != parms[i]; i++ ) {
  1131.         free( parms[i] ) ;
  1132.         macrop->parmcount++ ;
  1133.     }
  1134.  
  1135. /* rearrange the macro table so it is sorted by name length */
  1136.     name = macrop->name ;
  1137.     text = macrop->text ;
  1138.     parmcount = macrop->parmcount ;
  1139.     l = strlen( name ) ;
  1140.     for ( i=0; i<defined_macros-1; i++ ) {
  1141.         if ( l < strlen( macro[i].name ) ) {
  1142.             for ( j=defined_macros-1; j>i; j-- ) {
  1143.                 macro[j].name = macro[j-1].name ;
  1144.                 macro[j].text = macro[j-1].text ;
  1145.                 macro[j].parmcount = macro[j-1].parmcount ;
  1146.             }
  1147.             macro[i].name = name ;
  1148.             macro[i].text = text ;
  1149.             macro[i].parmcount = parmcount ;
  1150.             break ;
  1151.         }
  1152.     }
  1153.     
  1154. /* return the index of the new macro */
  1155.     return(i) ;
  1156. }
  1157.  
  1158.  
  1159.  
  1160. /* Expand the macros in the argument string.  Returns a pointer
  1161.  * to the expanded string, which is likely to be huge.  The memory
  1162.  * should be freed as soon as possible.  The macros are expanded
  1163.  * starting with the one with the highest index.  Recursive macro
  1164.  * definitions will be flagged, but may cause a termination due to
  1165.  * allocation failure before doing so.  Caution must be exercised
  1166.  * to avoid accidental recursive definitions involving
  1167.  * more than one macro:
  1168.  *    : h    i+x ;
  1169.  *    : i(y)    func(y) ;
  1170.  *    : func    h ;
  1171.  * This will generate the successive strings (from a = func(x)):
  1172.  *    a = h(x)
  1173.  *    a = i+x(x)
  1174.  *    a = func()+x(x)
  1175.  *    a = h()+x(x) .... and so on.  Beware.
  1176.  * The string is deallocated by this routine.
  1177.  */
  1178.  
  1179. /* macros to check for being next to an alpha */
  1180. #define FIRSTCHAR ( (pntr1!=text) && (isalnum(*(pntr1-1))&&isalnum(*pntr1)) )
  1181. #define LASTCHAR  ( isalnum(*(pntr1+l-1)) && isalnum(*(pntr1+l)) )
  1182. #define NEXT_TO_ALPHA    ( FIRSTCHAR || LASTCHAR )
  1183.  
  1184. char    *expand_macros(string)
  1185. char    *string ;
  1186. {
  1187. char    *pntr, *pntr1, *name, *text ;
  1188. int    i, hit, l ;
  1189.  
  1190. /* Allocate some initial storage */
  1191.     GET_MEM( text, strlen(string) ) ;
  1192.     strcpy( text, string ) ;
  1193.  
  1194. /* clear the recursion check counters */
  1195.     for ( i=0; i<defined_macros; i++ ) macro[i].callcount = 0 ;
  1196.  
  1197. /* search for macros */
  1198.     do {
  1199.     for ( i=defined_macros-1, hit=0; i>=0; i-- ) {
  1200.         
  1201.     /* See if macro[i] is in the present string.  If the "edges"
  1202.      * of the macro name are alphanumeric, don't accept the string
  1203.      * if the adjacent character is also alphanumeric.  This avoids
  1204.      * having variables such as "sin" flagged if "s" is defined.
  1205.      * Potential macros are also rejected if quoted with '.
  1206.      */
  1207.         name = macro[i].name ;
  1208.         l = strlen(name) ;
  1209.         for ( pntr=text; NULL != (pntr1=strmatch(pntr,name));
  1210.         pntr=pntr1+1 ) {
  1211.             if ( !quoted( pntr1, text ) && !NEXT_TO_ALPHA ) {
  1212.                 hit = 1 ;            /* got one */
  1213.                 text = mac_expand( text, pntr1, i ) ;
  1214.                 break ;
  1215.             }
  1216.         }
  1217.         if ( hit != 0 ) break ;    /* start over if one was found */
  1218.     }
  1219.     } while( hit != 0 ) ; 
  1220.  
  1221.  
  1222.     return( text ) ;
  1223. }
  1224.  
  1225.  
  1226.  
  1227. /* Expand a single macro in a text string, freeing the old storage
  1228.  * and returning * a pointer to the new string.  Name points to the
  1229.  * macro in the string and index is the macro index.
  1230.  */
  1231.  
  1232. char    *mac_expand( text, name, index )
  1233. char    *text, *name ;
  1234. int    index ;
  1235. {
  1236. char    *pntr, *newtext, *parm, *parms[MAX_TOKENS], *temp,
  1237.     *open_parens, *close_parens, *rest_of_text ;
  1238. int    i, j, size ;
  1239. unsigned char     c ;
  1240.  
  1241.     macrop = ¯o[index] ;
  1242.     if ( macrop->callcount++ > MAX_CALLS ) {
  1243.         sprintf( errline,
  1244.         "MAC_EXPAND: possible recursion involving: \'%s\' in\n%s",
  1245.             macrop->name, in_buff ) ;
  1246.         abort( errline ) ;
  1247.     }
  1248.     
  1249.  
  1250. /* get the parameters if there are any for this macro */
  1251.     for ( i=0; i<MAX_TOKENS; i++ ) parms[i] = NULL ;
  1252.     rest_of_text = &name[ strlen( macrop->name ) ] ;
  1253.     if ( macrop->parmcount != 0 ) {
  1254.         open_parens = &rest_of_text[ strspn( rest_of_text, " \t" ) ] ;
  1255.         if ( (NULL != strchr( "([{\'\"", *open_parens )) &
  1256.              (NULL != *open_parens )) {
  1257.             if (NULL == (close_parens=mat_del(open_parens)) ) {
  1258.                 sprintf( errline,
  1259.                 "MAC_EXPAND: missing delimeter: %s", in_buff ) ;
  1260.                 abort( errline ) ;
  1261.             }
  1262.             i = (int)(close_parens - open_parens) - 1 ;
  1263.             pntr = open_parens + 1 ;
  1264.             c = *close_parens ;        /* save *close_parens */
  1265.             *close_parens = NULL ;        /* make parm block a string */
  1266.             i = tokenize( pntr, parms ) ;    /* break out the parms */
  1267.             *close_parens = (char)c ;     /* restore text */
  1268.             rest_of_text = close_parens + 1 ;
  1269.         }
  1270.     }
  1271.  
  1272.     
  1273. /* find out how much memory we will need, then allocate */
  1274.     size = strlen(text) ;
  1275.     if ( NULL != ( pntr = macrop->text ) ) size += strlen(pntr) ;
  1276.     for ( i=0; NULL != (c=pntr[i]); i++ ) {
  1277.         if ( c > 127 & parms[c-128] != NULL )
  1278.             size += strlen(parms[c-128]) ;
  1279.     }
  1280.     GET_MEM( newtext, size ) ;
  1281.  
  1282.  
  1283. /* copy up to macro verbatim */
  1284.     *name = NULL ;
  1285.     strcpy( newtext, text ) ;
  1286.  
  1287. /* expand the macro itself if there is text */
  1288.     if ( NULL != (pntr = macrop->text) ) {
  1289.         for ( i=0, j=strlen(newtext); NULL != (c=pntr[i]); i++, j++ ) {
  1290.             if ( c > 127 ) {
  1291.                 if ( parms[c-128] != NULL ) {
  1292.                     strcat( newtext, parms[c-128] ) ;
  1293.                     j += strlen( parms[c-128] ) - 1 ;
  1294.                 }
  1295.                 else j-- ;
  1296.             }
  1297.             else {        /* keep null terminated */
  1298.                 newtext[j] = c ;
  1299.                 newtext[j+1] = NULL ;
  1300.             }
  1301.         }
  1302.     }
  1303.     
  1304.  
  1305. /* finish off trailing text */
  1306.     strcat( newtext, rest_of_text ) ;
  1307.     
  1308. /* free up temporary storage and return pointer to new allocation */
  1309.     for ( i=0; i<MAX_TOKENS & NULL != parms[i]; i++ ) free( parms[i] ) ;
  1310.     free( text ) ;
  1311.     return( newtext ) ;
  1312. }
  1313.  
  1314.  
  1315.  
  1316.  
  1317. /* isalnum: returns nonzero value if the character argument belongs to the 
  1318.  * sets { a-z, A-Z, 0-9 }.
  1319.  */
  1320.  
  1321. int    isalnum( c )
  1322. char    c ;
  1323. {
  1324.     if ( c >= 97 & c <= 122 ) return (1) ;    /* a-z */
  1325.     if ( c >= 65 & c <= 90 ) return (2) ;    /* A-Z */
  1326.     if ( c >= 48 & c <= 57 ) return (3) ;    /* 0-9 */
  1327.     return(0) ;                /* miss */
  1328. }
  1329.  
  1330.  
  1331.  
  1332.  
  1333. /* Return TRUE is the pointer is quoted in the string (pntr marks
  1334.  * a position in the string).  The quote character the apostrophe.
  1335.  * If pntr is not in the the result will be meaningless.
  1336.  */
  1337.  
  1338. int    quoted( pntr, string )
  1339. char    *pntr, *string ;
  1340. {
  1341. int    i, quote=FALSE ;
  1342.  
  1343.     for ( i=0; NULL != string[i] && &string[i] < pntr; i++ )
  1344.         if ( string[i] == '\'' ) quote = !quote ;
  1345.         
  1346.     return( quote ) ;
  1347. }
  1348. @//E*O*F macro.c//
  1349. chmod u=rw,g=r,o=r macro.c
  1350.  
  1351. echo x - vec.c
  1352. sed 's/^@//' > "vec.c" <<'@//E*O*F vec.c//'
  1353. /* Routines related to vector shorthand extensions */
  1354.  
  1355. #include "prep.h"
  1356.  
  1357.  
  1358.  
  1359.  
  1360. /* Function CSQB_PROC.C
  1361.  *
  1362.  * Process close square brackets.  Abort if called while
  1363.  * not in a vector loop, else finish off vector loop processing
  1364.  * with a call to end_vec.
  1365.  *
  1366.  * P. R. OVE  11/9/85
  1367.  */
  1368.  
  1369. csqb_proc() 
  1370. {
  1371. int    i, quote=1 ;
  1372.  
  1373. /* if vec_flag not set this call is an error */
  1374. if ( NOT vec_flag ) {
  1375.     sprintf( errline, "CSQB: not in vector loop: %s", in_buff ) ;
  1376.     abort( errline ) ;
  1377. }
  1378.                       
  1379. /* see what in_buff contains and replace unquoted ] by NULL */
  1380. for ( i = 0; in_buff[i] != NULL; i++ ) {
  1381.     switch ( in_buff[i] ) {
  1382.     
  1383.     case '\'' :    quote = -quote ;
  1384.             break ;
  1385.     case ']' :    if ( quote == 1 ) {
  1386.                 in_buff[i] = NULL ;
  1387.                 i-- ;        /* force termination */
  1388.                 break ;
  1389.             }
  1390.     }
  1391. }
  1392.  
  1393. dump( in_buff ) ;    /* --> mem_store */
  1394. end_vec();        /* terminate vector loop */
  1395.  
  1396. IN_BUFF_DONE ;
  1397. }
  1398.  
  1399.  
  1400.  
  1401.  
  1402. /* Function DO_LIMITS_PROC
  1403.  *
  1404.  * Process do_limits statements: Parse variable string.
  1405.  *
  1406.  * P. R. OVE  11/9/85
  1407.  */
  1408.  
  1409. char    *tokens[MAX_TOKENS] ;
  1410.  
  1411. do_limits_proc()
  1412. {                  
  1413. int    i, j, k ;
  1414. char    *temp[MAX_TOKENS], *open_parens, *close_parens ;
  1415.  
  1416. /* free allocation from previous call */
  1417. free_loop_vars() ;
  1418.  
  1419. /* find the open and close delimeters */
  1420. open_parens = &in_buff[ strcspn( in_buff, "[({\'\"" ) ] ;
  1421. if ( NULL == ( close_parens = mat_del( open_parens ) ) ) {
  1422.     sprintf( errline, "DO_LIMITS: missing delimeter: %s", in_buff ) ;
  1423.     abort( errline ) ;
  1424. }
  1425. *close_parens = NULL ;    /* make arg string null terminated */
  1426.  
  1427.  
  1428. /* get the (initial,limit,increment) triples */
  1429. var_count = tokenize( open_parens+1, tokens ) ;
  1430.  
  1431. /* handle wierd numbers of tokens */
  1432. if ( var_count <= 0 ) abort( "ERROR: no variables found" ) ;
  1433. for ( i = NESTING; i < var_count; i++ ) {
  1434.     var_count = NESTING ; free( tokens[i] ) ; }
  1435.  
  1436.  
  1437. /* At this stage the tokens are strings like
  1438.  *
  1439.  *  "(initial , limit , increment)  ==>  do i = initial, limit, increment.
  1440.  *
  1441.  * If one is missing it is assumed to be the increment.  If two are
  1442.  * missing the single item is assumed to be the limit.  The parens are
  1443.  * unnecessary if there is only the limit.
  1444.  *
  1445.  * break out the tokens (delimeted by commas)
  1446.  */
  1447. alloc_loop_vars() ;
  1448. for ( i = 0; i < var_count; i++ ) {
  1449.  
  1450.     /* find the open and close delimeters if present, and handle them*/
  1451.     open_parens = &tokens[i][ strcspn( tokens[i], "[({\'\"" ) ] ;
  1452.     if ( NULL != ( close_parens = mat_del( open_parens ) ) ) {
  1453.         *close_parens = NULL ;
  1454.         *open_parens = BLANK ;
  1455.     }
  1456.  
  1457.     k = tokenize( tokens[i], temp ) ;
  1458.  
  1459.     /* case of too many tokens, ignore trailers */
  1460.     for ( j = 3; j < k; j++ ) { k = 3 ; free( temp[j] ) ; }
  1461.  
  1462.     switch ( k ) {
  1463.     case 1:    strcpy(initial_name[i], "1") ;
  1464.         sprintf(limit_name[i], "(%s)", temp[0]) ; free( temp[0] ) ;
  1465.         strcpy(increment_name[i], "1") ;
  1466.         break;
  1467.  
  1468.     case 2:    sprintf(initial_name[i], "(%s)", temp[0]) ; free( temp[0] ) ;
  1469.         sprintf(limit_name[i], "(%s)", temp[1]) ; free( temp[1] ) ;
  1470.         strcpy(increment_name[i], "1") ;
  1471.         break;
  1472.  
  1473.     case 3:    sprintf(initial_name[i], "(%s)", temp[0]) ; free( temp[0] ) ;
  1474.         sprintf(limit_name[i], "(%s)", temp[1]) ; free( temp[1] ) ;
  1475.         sprintf(increment_name[i], "(%s)", temp[2]) ; free( temp[2] ) ;
  1476.         break;
  1477.  
  1478.     default:strcpy(initial_name[i], "1") ;
  1479.         sprintf(limit_name[i], "(%s)", "undefined" ) ;
  1480.         strcpy(increment_name[i], "1") ;
  1481.         break;
  1482.     }
  1483. }                
  1484.  
  1485. IN_BUFF_DONE
  1486. }
  1487.  
  1488. /* release allocation from previous call */
  1489. free_loop_vars() {
  1490. int    i ;
  1491.  
  1492. for ( i = 0; i < var_count; i++ ) {
  1493.     free( tokens[i] ) ;
  1494.     free( initial_name[i] ) ;
  1495.     free( limit_name[i] ) ;
  1496.     free( increment_name[i] ) ;
  1497. }
  1498. }
  1499.  
  1500. /* allocate space for do loop variables */
  1501. alloc_loop_vars() {
  1502. int    i, size ;
  1503.  
  1504. for ( i = 0; i < var_count; i++ ) {
  1505.     size = strlen( tokens[i] ) + 10 ;
  1506.     GET_MEM( initial_name[i], size ) ;
  1507.     GET_MEM( limit_name[i], size ) ;
  1508.     GET_MEM( increment_name[i], size ) ;
  1509. }
  1510. }
  1511.  
  1512.  
  1513.  
  1514.  
  1515. /* Function END_VEC.C
  1516.  *
  1517.  * This routine is called when a cluster of vector arithmetic
  1518.  * is ready to be terminated (a closing ] has been found
  1519.  * or the statement was a single line vector * statement.  The
  1520.  * core of the loop has by now been pushed into MEM_STORE and
  1521.  * will now be extracted and processed.  On completion MEM_STORE
  1522.  * is released.
  1523.  *
  1524.  * P. R. OVE  11/9/85
  1525.  */
  1526.  
  1527. end_vec() 
  1528. {
  1529. int    i, j ;
  1530.  
  1531. /* reset the flag */
  1532. vec_flag = FALSE ;
  1533.  
  1534. make_do() ;    /* write the initial do loop statements */
  1535.  
  1536. if ( NOT UNROLLING ) {
  1537.     /* process all of the pushed statements through transvec */
  1538.     for ( i = 0; i < mem_count; i++ )
  1539.         transvec( mem_store[i], 0 ) ;
  1540.  
  1541.     make_continue() ;    /* write continue statements */
  1542. }
  1543.  
  1544. else {
  1545.     /* process the statements though transvec unroll_depth times */
  1546.     for ( j = 0; j < unroll_depth; j++ ) {
  1547.         for ( i = 0; i < mem_count; i++ )
  1548.             transvec( mem_store[i], j ) ;
  1549.     }
  1550.     make_continue() ;
  1551.  
  1552.     /* write the clean up part of the unrolled loop */
  1553.     make_labels() ;
  1554.     make_clean_do() ;
  1555.     for ( i = 0; i < mem_count; i++ )
  1556.         transvec( mem_store[i], 0 ) ;
  1557.     make_continue() ;
  1558. }
  1559.  
  1560. /* release the memory held by MEM_STORE and return to main level */
  1561. while ( push(NULL) >= 0 ) ;
  1562. IN_BUFF_DONE
  1563. }
  1564.  
  1565.  
  1566.  
  1567.  
  1568. /* Make the initial do statements */
  1569. make_do() {
  1570. int    i ;
  1571.  
  1572. /* outermost do statement is different if unrolling is on */
  1573. i = var_count - 1 ;
  1574.  
  1575. if ( UNROLLING ) {
  1576. /* This section unrolls: do i = a, b, c   (depth = d)   into
  1577.  *
  1578.  *             b-a+c
  1579.  * do i = a, (-------)*(c*d) + a - c, c*d  
  1580.  *              c*d
  1581.  *
  1582.  * for the outermost loop.  Inner loops are unchanged.
  1583.  */
  1584.     sprintf( out_buff,
  1585.     "      do %s %s=%s,int((1.0*(%s-%s+%s))/(%s*%d))*%s*%d+%s-%s,%s*%d",
  1586.         label[i], var_name[i], initial_name[i],
  1587.         limit_name[i], initial_name[i], increment_name[i],
  1588.         increment_name[i], unroll_depth,
  1589.         increment_name[i], unroll_depth,
  1590.         initial_name[i], increment_name[i],
  1591.         increment_name[i], unroll_depth ) ;
  1592.     dump( out_buff ) ; }
  1593. else {
  1594.     sprintf( out_buff, "      do %s %s = %s, %s, %s",
  1595.         label[i], var_name[i],
  1596.         initial_name[i], limit_name[i], increment_name[i] ) ;
  1597.     dump( out_buff ) ; }
  1598.  
  1599. /* handle the rest of the do statements */
  1600. for ( i = var_count-2; i >= 0; i-- ) {
  1601.     sprintf( out_buff, "      do %s %s = %s, %s, %s",
  1602.         label[i], var_name[i],
  1603.         initial_name[i], limit_name[i], increment_name[i] ) ;
  1604.     dump( out_buff ) ; }
  1605. }
  1606.  
  1607.  
  1608.  
  1609.  
  1610. /* make the do statements for the clean up part of the unrolled loop */
  1611. make_clean_do() {
  1612. int    i ;
  1613.  
  1614. /* make the outer do statement.
  1615.  * This section unrolls: do i = a, b, c   (depth = d)   into
  1616.  *
  1617.  *          b-a+c
  1618.  * do i = (-------)*(c*d) + a, b, c
  1619.  *           c*d
  1620.  *
  1621.  * for the outermost loop.  Inner loops are unchanged.  The initial
  1622.  * value is the first element that missed the main do loop */
  1623. i = var_count - 1 ;
  1624. sprintf( out_buff,
  1625.     "      do %s %s=int((1.0*(%s-%s+%s))/(%s*%d))*%s*%d+%s,%s,%s",
  1626.     label[i], var_name[i],
  1627.     limit_name[i], initial_name[i], increment_name[i],
  1628.     increment_name[i], unroll_depth,
  1629.     increment_name[i], unroll_depth,
  1630.     initial_name[i], limit_name[i], increment_name[i] ) ;
  1631. dump( out_buff ) ;
  1632.  
  1633. /* make the remaining do statements */
  1634. for ( i = var_count-2; i >= 0; i-- ) {
  1635.     sprintf( out_buff, "      do %s %s = %s, %s, %s",
  1636.         label[i], var_name[i],
  1637.         initial_name[i], limit_name[i], increment_name[i] ) ;
  1638.     dump( out_buff ) ;
  1639. }
  1640. }
  1641.  
  1642.  
  1643. /* make the continue statements */
  1644. make_continue() {
  1645. int    i ;
  1646.  
  1647. for ( i = 0; i < var_count; i++ ) {
  1648.     sprintf( out_buff, "%s continue", label[i] ) ;
  1649.     dump( out_buff ) ; }
  1650. }
  1651.  
  1652.  
  1653.  
  1654.  
  1655. /* Function MAKE_LABELS.C
  1656.  *
  1657.  * Make var_count labels, starting with label_count
  1658.  * + 10000.
  1659.  *
  1660.  * P. R. OVE  11/9/85
  1661.  */
  1662.  
  1663. make_labels()
  1664. {                  
  1665. int    i, count ;
  1666.                     
  1667. for ( i = 0; i < var_count; i++ ) {
  1668.      
  1669.     count = 10000 + label_count ;
  1670.     label_count++ ;              
  1671.     if ( count > 12499 ) { 
  1672.         sprintf( errline, "MAKE_LABELS: too many labels: %s", in_buff ) ;
  1673.         abort( errline ) ;
  1674.     }
  1675.     sprintf( label[i], "%d", count ) ;
  1676. }
  1677. }
  1678.  
  1679.  
  1680.  
  1681. /* Function OSQB_PROC.C
  1682.  *
  1683.  *   Process open square brackets.  This routine will be
  1684.  * called when an open square bracket is found in the
  1685.  * record (start cluster of vector arithmetic).  It sets
  1686.  * up the labels and sets vec_flag so that dump will direct
  1687.  * output to mem_store instead of the output file.
  1688.  *   The initial do statements are not written here, so that
  1689.  * unrolling can be turned off if there are too many lines
  1690.  * ( > line_limit ) in the loop.  Endvec will write them.
  1691.  *   If a closing ] is also found in the same record then
  1692.  * the statement is passed through transvec immediately, since
  1693.  * it has already been processed by the rest of the preprocessor.
  1694.  *
  1695.  * P. R. OVE  11/9/85
  1696.  */
  1697.  
  1698. osqb_proc() 
  1699. {
  1700. int    i, quote=1 ;
  1701.  
  1702. /* if default loop limits have not been set abort here */
  1703. if ( var_count <= 0 ) {
  1704.     sprintf( errline, "Vector loop without default limits set: %s", in_buff ) ;
  1705.     abort( errline ) ;
  1706. }
  1707.  
  1708. make_labels() ;        /* get a list of labels */
  1709.  
  1710. vec_flag = TRUE ;    /* now force output --> mem_store */
  1711.                       
  1712. /* see what in_buff contains and replace unquoted [] by blanks */
  1713. for ( i = 0; in_buff[i] != NULL; i++ ) {
  1714.  
  1715.     switch ( in_buff[i] ) {
  1716.     
  1717.     case '\'' :    quote = -quote ;
  1718.             break ;
  1719.     case '[' :    if ( quote == 1 ) {
  1720.                 in_buff[i] = BLANK ;
  1721.                 break ;
  1722.             }
  1723.     case ']' :    if ( quote == 1 ) {
  1724.                 vec_flag = FALSE ;
  1725.                 in_buff[i] = BLANK ;
  1726.                 break ;
  1727.             }
  1728.     }
  1729. }
  1730.  
  1731. /* if there is a closing ] process the line now */
  1732. if ( NOT vec_flag ) {
  1733.     vec_flag = TRUE ;    /* force line to mem_store */
  1734.     dump( in_buff ) ;
  1735.     end_vec() ;        /* flag will be reset here */
  1736. }
  1737. else dump( in_buff ) ;        /* this will go to mem_store */
  1738.  
  1739. IN_BUFF_DONE ;
  1740. }
  1741.  
  1742.  
  1743.  
  1744.  
  1745. /* Function TRANSVEC.C
  1746.  *
  1747.  * Translate a record of vectored arithmetic and expand
  1748.  * out the # signs.  The resulting expanded record is
  1749.  * placed in out_buff and dumped.  The second argument
  1750.  * is related to unrolling, and is the amount to be
  1751.  * added to the index of the outermost loop.  This
  1752.  * should be zero if unrolling is off.  Quoted characters
  1753.  * are ignored ( ' is the fortran quote character ).
  1754.  *
  1755.  * P. R. OVE  11/9/85
  1756.  */
  1757.  
  1758. /* copy character verbatim to the output buffer */
  1759. #define    VERBATIM    out_buff[i_out] = string[i_in] ;\
  1760.             out_buff[i_out + 1] = NULL ;    \
  1761.             i_out++ ;
  1762.  
  1763.  
  1764. transvec( string, outer_loop_inc ) 
  1765. char    *string ;
  1766. int    outer_loop_inc ;
  1767. {
  1768. int    i_in, i_out = 0, i_var = 0, quote = 1 ;
  1769. char    *pntr ;
  1770.  
  1771. /* make string version of loop counter increment */
  1772. if ( UNROLLING ) {
  1773.     GET_MEM( pntr, strlen(increment_name[var_count-1])
  1774.              + abs(outer_loop_inc) + 10 ) ;
  1775.     sprintf( pntr, "+%s*%d", increment_name[ var_count - 1 ],
  1776.         outer_loop_inc ) ;
  1777. }
  1778.  
  1779. /* loop over the input record */
  1780. for ( i_in = 0; string[i_in] != NULL; i_in++ ) {
  1781.  
  1782. /* pass characters straight through if quoted */
  1783. if ( string[i_in] == '\'' ) quote = -quote ;
  1784. if ( quote == -1 ) {
  1785.     VERBATIM ;
  1786.     continue ;
  1787. }
  1788.  
  1789. switch( string[i_in] ) {
  1790.  
  1791.     /* replace #'s with variable names */
  1792.     case '#' :    strcat( out_buff, var_name[i_var] ) ;
  1793.             i_out += 4 ;
  1794.             i_var++ ;   
  1795.             if ( i_var >= var_count ) {
  1796.                 i_var = 0 ;
  1797.                 if (UNROLLING & outer_loop_inc != 0) {
  1798.                     strcat( out_buff, pntr ) ;
  1799.                     i_out += strlen( pntr ) ;
  1800.                 }
  1801.             }
  1802.             break ;
  1803.  
  1804.     /* reset variable counter */
  1805.     case ')' :    out_buff[i_out] = ')' ;
  1806.             out_buff[i_out + 1] = NULL ;
  1807.             i_out++ ;
  1808.             i_var = 0 ;
  1809.             break ;
  1810.  
  1811.     /* copy character verbatim */
  1812.     default :     VERBATIM ;
  1813.  
  1814. }
  1815. }
  1816.  
  1817. if (UNROLLING) free( pntr ) ;
  1818. dump( out_buff ) ;
  1819.  
  1820. IN_BUFF_DONE ;
  1821. }
  1822.  
  1823.  
  1824.  
  1825.  
  1826. /* Function UNROLL_PROC
  1827.  *
  1828.  * Change the unrolling depth.  If depth is less than 2 unrolling is off.
  1829.  *
  1830.  * P. R. OVE  6/18/86
  1831.  */
  1832.  
  1833. unroll_proc()     
  1834. {                  
  1835. int    n ;
  1836. char    *open_parens, *close_parens ;
  1837.  
  1838. /* get the expression delimeters */
  1839. open_parens = line_end( first_nonblank + name_length ) ;
  1840. close_parens = mat_del( open_parens ) ;
  1841.                                            
  1842. /* if there is stuff on the line (open_parens != NULL) and no            */
  1843. /* open parens (close_parens == NULL) assume variable name like UNROLLit */
  1844. if ( (open_parens != NULL) & (close_parens == NULL) ) return ;
  1845.  
  1846. /* get the depth if it is there (error ==> depth = 0 (OFF)) */
  1847. if (open_parens != NULL) {
  1848.     n = close_parens - open_parens - 1 ;
  1849.     *close_parens == NULL ;
  1850.     unroll_depth = atoi( open_parens + 1 ) ;
  1851. }
  1852. else {    unroll_depth = DEF_UNROLL_DEPTH ; }
  1853.  
  1854. IN_BUFF_DONE
  1855. }
  1856.  
  1857.  
  1858.  
  1859.  
  1860. /* Function VEC_PROC.C
  1861.  *
  1862.  * This routine's functions when a "naked"
  1863.  * (with out surrounding [ ]) vector statement is found.
  1864.  * The action depends on whether vec_flag is set or not.
  1865.  * If set:
  1866.  *   The record is dumped (to mem_store).
  1867.  * If not:
  1868.  *   It is handled by placing a [ at the beginning and a
  1869.  * ] at the end and then starting over.  OSQB_PROC will
  1870.  * then trap it and pass it to END_VEC to be processed.
  1871.  *
  1872.  * P. R. OVE  11/9/85
  1873.  */
  1874.  
  1875. vec_proc()
  1876. {
  1877. int    i, length ;
  1878.  
  1879. /* if default loop limits have not been set abort here */
  1880. if ( var_count <= 0 ) {
  1881.     sprintf( errline, "Vector loop without default limits set: %s", in_buff ) ;
  1882.     abort( errline ) ;
  1883. }
  1884.                       
  1885. if ( vec_flag ) {
  1886.     dump( in_buff ) ;    /* --> mem_store */
  1887.     IN_BUFF_DONE ;
  1888. }
  1889. else {
  1890.     length = strlen( in_buff ) ;
  1891.     for ( i = length - 1; i >= 0; i-- ) in_buff[i+1] = in_buff[i] ;
  1892.     in_buff[ length + 1 ] = ']' ;
  1893.     in_buff[ length + 2 ] = NULL ;
  1894.     in_buff[ 0 ] = '[' ;
  1895. }
  1896. }
  1897. @//E*O*F vec.c//
  1898. chmod u=rw,g=r,o=r vec.c
  1899.  
  1900. echo x - str.c
  1901. sed 's/^@//' > "str.c" <<'@//E*O*F str.c//'
  1902. /* A few string functions missing from the Sun unix library */
  1903.  
  1904. #include <stdio.h>
  1905. #include "string.h"
  1906.  
  1907. /* Find the first occurrence of c in string */
  1908. char    *strchr( s, c )
  1909. char    *s, c ;
  1910. {
  1911. int    length, i ;
  1912.     length = strlen(s) ;
  1913.  
  1914.     for ( i=0; i<=length; i++ ) if ( s[i] == c ) return( &s[i] ) ;
  1915.     return( NULL ) ;
  1916. }
  1917.  
  1918. /* find the index of the first char in s1 that is not in s2 */
  1919. int    strspn( s1, s2 )
  1920. char    *s1, *s2 ;
  1921. {
  1922. int    i ;
  1923.  
  1924.     for ( i=0 ; s1[i] != NULL ; i++ ) {
  1925.         if ( NULL == strchr(s2,s1[i]) ) break ;
  1926.         }
  1927.     return(i) ;
  1928. }
  1929.  
  1930.  
  1931. /* find the index of the first char in s1 that is in s2 */
  1932. int    strcspn( s1, s2 )
  1933. char    *s1, *s2 ;
  1934. {
  1935. int    i ;
  1936.  
  1937.     for ( i=0 ; s1[i] != NULL ; i++ ) {
  1938.         if ( NULL != strchr(s2,s1[i]) ) break ;
  1939.         }
  1940.     return(i) ;
  1941. }
  1942. @//E*O*F str.c//
  1943. chmod u=rw,g=r,o=r str.c
  1944.  
  1945. echo Inspecting for damage in transit...
  1946. temp=/tmp/shar$$; dtemp=/tmp/.shar$$
  1947. trap "rm -f $temp $dtemp; exit" 0 1 2 3 15
  1948. cat > $temp <<\!!!
  1949.      607    4325   25859 prep.doc
  1950.       16      40     236 Makefile
  1951.       21      55     503 makemsc
  1952.      225    1067    5831 prep.c
  1953.      433    2228   12268 macro.c
  1954.      544    2302   12944 vec.c
  1955.       40     168     728 str.c
  1956.     1886   10185   58369 total
  1957. !!!
  1958. wc  prep.doc Makefile makemsc prep.c macro.c vec.c str.c | sed 's=[^ ]*/==' | diff -b $temp - >$dtemp
  1959. if [ -s $dtemp ]
  1960. then echo "Ouch [diff of wc output]:" ; cat $dtemp
  1961. else echo "No problems found."
  1962. fi
  1963. exit 0
  1964.  
  1965.  
  1966.