home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume20 / cfortran / part01 / cfortran.doc < prev   
Encoding:
Text File  |  1991-06-26  |  26.9 KB  |  647 lines

  1. /* cfortran.doc */
  2. /* Burkhard Burow, burow@vxdesy.desy.de, U. of Toronto, 1991. */
  3.  
  4.  
  5.                 CFORTRAN 1.2 for UNIX Machines and for VAX VMS
  6.  
  7. History:
  8. - 1.0 for VAX VMS using C 3.1 and FORTRAN 5.4.                        Oct. '90.
  9. - 1.0 for Silicon Graphics using Mips Computer System 2.0 f77 and cc. Feb. '91.
  10.           [Port of C calls FORTRAN half only.]
  11. - 1.1 for Mips Computer System 2.0 f77 and cc.                        Mar. '91.
  12.           [Runs on at least: Silicon Graphics IRIX 3.3.1
  13.                              DECstations with Ultrix V4.1]
  14. - 1.2 Internals are simpler, smaller, faster, stronger.               May  '91.
  15.       Mips version works on IBM RS/6000, this is now called the unix version.
  16. - 1.3 UNIX and VAX VMS versions are merged into a single cfortran.h.  June '91.
  17.       C can manipulate (arrays of) strings in FORTRAN common blocks.
  18.  
  19.  
  20. I Introduction
  21. --------------
  22.  
  23. CFORTRAN is an easy-to-use powerful bridge between C and FORTRAN. It provides a
  24. completely transparent, machine independant, interface  between C and FORTRAN
  25. routines (= subroutines and/or functions).
  26.  
  27. The complete CFORTRAN package consists of 4 files. They are this introduction,
  28. cfortran.doc, the engine in cfortran.h, examples in cfortest.c and
  29. cfortex.f/or. [cfortex.for under VMS, cfortex.f under UNIX.]
  30.  
  31. To run the example do the following:
  32.  
  33. RS/6000> cc -Drs6000 -c cfortest.c && xlf -o cfortest cfortest.o cfortex.f
  34. RS/6000> cfortest
  35.  
  36. or
  37.  
  38. MIPS> cc -o cfortest cfortest.c cfortex.f -lI77 -lU77 -lF77
  39. MIPS> cfortest
  40.  
  41. or
  42.  
  43. VMS> define lnk$library sys$library:vaxcrtl
  44. VMS> cc cfortest.c
  45. VMS> fortran cfortex.for
  46. VMS> link/exec=cfortest cfortest,cfortex
  47. VMS> run cfortest
  48.  
  49. By changing the SELECTion ifdef of cfortest.c and recompiling you can try out
  50. a dozen different few-line examples.
  51.  
  52.  
  53.  
  54. The benefits of using CFORTRAN include:
  55. 1. Machine independant applications.
  56.  
  57. 2. Identical (within syntax) calls across languages, e.g.
  58. C FORTRAN
  59.       CALL HBOOK1(1,'pT spectrum of pi+',100,0.,5.,0.)
  60. /* C*/
  61.            HBOOK1(1,"pT spectrum of pi+",100,0.,5.,0.);
  62.  
  63. 3. Each routine need ony be set up once in its lifetime. e.g.
  64. /* Setting up a FORTRAN routine to be called by C. Note that ID,...,VMX are
  65. merely the names of arguments. These tags must be unique w.r.t. each other but
  66. are arbitrary. */
  67. PROTOCCALLSFSUB6(hbook1,INT,STRING,INT,FLOAT,FLOAT,FLOAT)
  68. #define HBOOK1(ID,CHTITLE,NX,XMI,XMA,VMX)                 \
  69.      CCALLSFSUB6(hbook1,INT,STRING,INT,FLOAT,FLOAT,FLOAT, \
  70.                ID,CHTITLE,NX,XMI,XMA,VMX)
  71.  
  72. 4. Routines, and the code calling them, can be coded naturally in the language
  73.    of choice. C routines may be coded with the natural assumption that they'll
  74.    only be called by C code. CFORTRAN does all the required work for FORTRAN
  75.    code to call C routines. Similarly CFORTRAN does all the work required for C
  76.    to call FORTRAN routines. Therefore:
  77.      - C programmers need not imbed FORTRAN argument passing mechanisms into
  78.        their code.
  79.      - FORTRAN code need not be converted into C code. i.e. The honed and
  80.        timehonored FORTRAN routines are called by C, not some new translation
  81.        from FORTRAN into C.
  82.  
  83. 5. CFORTRAN is contained within a single C include file, cfortran.h, weighing
  84.    in at ~1100 lines. cfortran.h currently supports VAX VMS, the IBM RS/6000
  85.    and machines using the MIPS RISC compilers. It should be portable to many
  86.    other platforms.
  87.  
  88. 6. STRINGS and VECTORS of STRINGS along with the usual simple arguments to
  89.    routines are supported as are functions returning STRINGS or numbers.
  90.  
  91. 7. CFORTRAN requires each routine to be exported to be explicitly set up. While
  92.    this need usually only be done once in a header file it would be best if
  93.    applications were required to do no work at all in order to cross languages.
  94.    CFORTRAN's simple syntax could be a convinient back-end for a program which
  95.    would export FORTRAN or C routines directly from the source code.
  96.  
  97.  
  98.                                     -----
  99.  
  100. Example 1 - CFORTRAN has been used to make the C header file hbook.h,
  101.             which then gives any C programmer, e.g. example.c, full and
  102.             completely transparent access to CERN's HBOOK library of routines.
  103.             Each HBOOK routine required about 3 lines of simple code in
  104.             hbook.h. The example also demonstrates how FORTRAN common blocks
  105.             are defined and used.
  106.  
  107. /* hbook.h */
  108. #include "cfortran.h"
  109.         :
  110. PROTOCCALLSFSUB6(hbook1,INT,STRING,INT,FLOAT,FLOAT,FLOAT)
  111. #define HBOOK1(ID,CHTITLE,NX,XMI,XMA,VMX) \
  112.      CCALLSFSUB6(hbook1,INT,STRING,INT,FLOAT,FLOAT,FLOAT, \
  113.                ID,CHTITLE,NX,XMI,XMA,VMX)
  114.         :
  115. /* end hbook.h */
  116.  
  117. /* example.c */
  118. #include "hbook.h"
  119.         :
  120. typedef struct {
  121.   int lines;
  122.   int status[SIZE];
  123.   float p[SIZE];  /* momentum */
  124. } FAKE_DEF;
  125. #define FAKE COMMON_BLOCK(fake)
  126. extern FAKE_DEF FAKE;
  127.         :
  128. main ()
  129. {
  130.         :
  131.            HBOOK1(1,"pT spectrum of pi+",100,0.,5.,0.);
  132. /* c.f. the call in FORTRAN:
  133.       CALL HBOOK1(1,'pT spectrum of pi+',100,0.,5.,0.)
  134. */
  135.         :
  136.   FAKE.p[7]=1.0;
  137.         :
  138. }
  139.  
  140. N.B. i) The routine is language independant.
  141.     ii) hbook.h is machine independant.
  142.    iii) Applications using CFORTRAN'd routines are machine independant.
  143.  
  144.                                     -----
  145.  
  146. Example 2 - Many VMS System calls are most easily called from FORTRAN, but
  147.             CFORTRAN now gives you that ease in C.
  148.  
  149. #include "cfortran.h"
  150.  
  151. PROTOCCALLSFSUB3(lib$spawn, STRING,STRING,STRING)
  152. #define LIB$SPAWN(command,input_file,output_file) \
  153.      CCALLSFSUB3(lib$spawn, STRING,STRING,STRING, \
  154.                   command,input_file,output_file)
  155.  
  156. main ()
  157. {
  158. LIB$SPAWN("set term/width=132","","");
  159. }
  160.  
  161. Obviously the 2 CFORTRAN lines above should be put into a header file along
  162. with the description of the other system calls, but as this example shows it's
  163. not much hassle to set up CFORTRAN for even a single call.
  164.  
  165.                                     -----
  166.  
  167. Example 3 - CFORTRAN and the source cstring.c create the cstring.obj library
  168.             which gives FORTRAN access to all the functions in C's system
  169.             library described by the system's C header file string.h.
  170.  
  171. C     EXAMPLE.FOR
  172.       PROGRAM EXAMPLE
  173.       DIMENSION I(20), J(30)
  174.         :
  175.       CMEMCPY(I,J,7)
  176.         :
  177.       END
  178.  
  179. /* cstring.c */
  180. #include <string.h>
  181. #include "cfortran.h"
  182.  
  183. #undef fcallsc
  184. #define fcallsc(NAME) C/**/NAME
  185.  
  186.         :
  187. FCALLSCSUB3(memcpy, PVOID, PVOID, INT)
  188.         :
  189.  
  190. N.B. Other than a possible redefinition of fcallsc, cstring.c is machine
  191. independant. Unfortunately the names of C routines called by FORTRAN may
  192. differ from  the name of the original C routine, e.g. cmemcpy vs. the original
  193. memcpy. This need never be the case if one has:
  194.  
  195.    i) the original C source code for the routine.
  196. OR ii)a FORTRAN compiler, e.g. f77, which 'renames' routines.
  197. OR iii) a case sensitive linker.
  198.  
  199. If all the above fail, CFORTRAN, through fcallsc, makes it easy to ensure that
  200. names of C routines called by FORTRAN are modified from  the original only when
  201. absolutely neccessary, and if they are modified, that it is done consistently
  202. for any given FORTRAN compiler. [More details below in Section VI.]
  203.  
  204.                                     -----
  205.  
  206.  
  207. II Using CFORTRAN
  208. -----------------
  209.  
  210. The user is asked to look at the source files CFORTEX.C and CFORTEX.FOR for
  211. clarification by example.
  212.  
  213. Note: CFORTRAN (ab)uses the null comment, /**/, kludge for the ANSI C
  214. preprocessor concatenation, ##, operator. In MIPS C this kludge is sensitive
  215. to blanks prepending arguments to macros.
  216. THEREFORE IN THE FOLLOWING MACRO DEFINITIONS YOU MAY NOT PREPEND argtype_i NOR
  217. routine_type WITH BLANK, ' ', CHARACTERS.
  218.  
  219. Note: On the RS/6000, currently the only machine supporting ##, a global
  220. replace of /**/ by ## makes cfortran.h ANSI compliant.
  221.  
  222. Note: On the RS/6000, if you use "xlf -qextname ...", which appends an
  223. underscore, '_', to all FORTRAN external references, then you must use,
  224. "cc -Dextname ..." so that cfortran.h also generates these underscores.
  225.  
  226. Note: The calls to FORTRAN subroutines are expanded to code inside {....}. This
  227. has some undesirable side effects, e.g.
  228. if (a==b) A_FORTRAN_SUBROUTINE(1,"Hello")  /* <-- No ';' allowed here. */
  229. else      A_FORTRAN_SUBROUTINE(1,"Hello")
  230. Unfortunately there is no fix in sight. (gcc extensions to C would offer a fix.)
  231.  
  232. Note: At the moment only vectors of fixed length strings are supported in C. I
  233. know how and hope to support vectors of pointers to strings in the near future.
  234.  
  235. Note: For those who wish to use CFORTRAN in large applications.
  236. This release is intended to make it easy to get applications up and running.
  237. This implies that applications are not as efficient as they could be:
  238. - The current mechanism is inefficient if a single header file is used to
  239.   describe a large library of FORTRAN functions. Code for a static wrapper fn.
  240.   is generated in each piece of C source code for each FORTRAN function
  241.   specified with the CCALLSFFUNn statement, irrespective of whether or not the
  242.   function is ever called. I have several ideas for how code for these wrappers
  243.   could be created, compiled and linked only once instead of once for each
  244.   piece of source code.
  245. - Code for several static utility routines internal to CFORTRAN is placed into
  246.   any source code which #include's cfortran.h. These routines should be in a
  247.   library.
  248. - The FORTRAN calls C half of the package could be split from the C calls
  249.   FORTRAN half.
  250.  
  251.  
  252. i) Calling FORTRAN routines from C:
  253.    --------------------------------
  254.  
  255. FORTRAN routines are prototyped by the following two macros.
  256.  
  257. PROTOCCALLSFSUBn(routine_name, argtype_1, ..., argtype_n)
  258.  
  259. or
  260.  
  261. PROTOCCALLSFFUNn(routine_type, routine_name, argtype_1, ..., argtype_n)
  262.  
  263.  
  264. and are defined respectively by the following two macro usages.
  265.  
  266. #define      ROUTINE_NAME(argname_1,...,argname_n) \
  267. CCALLSFSUBn(routine_name, argtype_1,...,argtype_n, \
  268.                           argname_1,...,argname_n)
  269.  
  270. #define      ROUTINE_NAME(argname_1,...,argname_n) \
  271. CCALLSFFUNn(routine_name, argtype_1,...,argtype_n, \
  272.                           argname_1,...,argname_n)
  273.  
  274. Where:
  275. 'n' = 0->7 (easily expanded in CFORTRAN.H to >7) stands for the number of
  276.     arguments to the routine.
  277. ROUTINE_NAME = the C       name of the routine (IN UPPERCASE LETTERS).
  278. routine_name = the FORTRAN name of the routine (IN lowercase LETTERS).
  279. routine_type = the type of argument returned by FORTRAN functions.
  280.              = DOUBLE, FLOAT, INT, LOGICAL, LONG, STRING.
  281. argtype_i    = the type of argument passed to the FORTRAN routine and must be
  282.                consistent in the definition and prototyping of the routine s.a.
  283.              = DOUBLE, FLOAT, INT, LOGICAL, LONG, STRING.
  284.              For vectors, i.e. 1 dim. arrays use
  285.              = DOUBLEV, FLOATV, INTV, LOGICALV, LONGV.
  286.              For vectors of vectors, 2 dim. arrays use
  287.              = DOUBLEVV, FLOATVV, INTVV, LOGICALVV, LONGVV, STRINGV.
  288.              For n-dim. arrays use
  289.              = DOUBLEV..nV's..V, FLOATV..V, INTV..V, LOGICALV..V, LONGV..V.
  290.                 N.B. Array dimensions and types are checked by the C compiler.
  291.              For routines changing the values of an argument, the keyword is
  292.                   prepended by a 'P'.
  293.              = PDOUBLE, PFLOAT, PINT, PLOGICAL, PLONG, PSTRING, PSTRINGV.
  294.              For exceptional arguments which require no massaging to fit the
  295.                   argument passing mechanisms use:
  296.              = PVOID.
  297.                 This is most useful for passing functions as arguments.
  298.                 But note that although PVOID could be used to describe all
  299.                 array arguments on most (all?) machines , it shouldn't be
  300.                 because the C compiler can no longer check the type and
  301.                 dimension of the array.
  302. argname_i    = any valid unique C tag, but must be consistent in the definition
  303.                as shown.
  304.  
  305. Some notes on (P)STRING(V):
  306.  
  307. STRING - If the argument is a fixed length character array, e.g. char ar[8];,
  308. the string is blank, ' ', padded on the right to fill out the array before
  309. being passed to the FORTRAN routine. The useful size of the string is the same
  310. in both languages, e.g. we pass ar[8] as character*7. If the argument is a
  311. pointer, we cannot blank pad, and pass the length as strlen(argument). On
  312. return from the FORTRAN routine, pointer arguments are not disturbed, arrays
  313. have the terminating '\0' replaced to its original position. i.e. The
  314. padding blanks are never visible to the C code.
  315.  
  316. PSTRING - The argument is massaged as with STRING before being passed to the
  317. FORTRAN routine. On return, the argument has all trailing blanks removed,
  318. regardless of whether the argument was a pointer or an array.
  319.  
  320. N.B. Only char arrays are supported for (P)STRINGV. e.g. char bb[6][8];
  321.  
  322. STRINGV - The elements of the argument are copied into space malloc'd, and each
  323. element is padded with blanks. The useful size of each element is the same in
  324. both languages. Therefore char bb[6][8]; is equivalent to character*7 bb(6).
  325. On return from the routine the malloc'd space is simply released.
  326.  
  327. PSTRINGV - Since FORTRAN has no trailing '\0', elements in an array of strings
  328. are contiguous. Therefore we pad each element of the C array with blanks and
  329. strip out C's trailing '\0'. After returning from the routine, we reinsert the
  330. trailing '\0' and kill the trailing blanks in each element.
  331.  
  332. Summary: STRING(V) arguments are blank padded during the call to the FORTRAN
  333. routine, but remain original in the C code. (P)STRINGV arguments are blank
  334. padded for the FORTRAN call, and after returning from FORTRAN trailing blanks
  335. are stripped off.
  336.  
  337.  
  338. PVOID, as noted above, is used to declare that a function will be passed as an
  339. argument. In order to perform the call, CFORTRAN must know the language of the
  340. function to be passed, therefore the when passing C functions to FORTRAN
  341. routines use:
  342.  
  343.     FORTRAN_ROUTINE( ...., C_FUNCTION(some_function), ...)
  344.  
  345. and similarly when passing a FORTRAN routine:
  346.  
  347.     FORTRAN_ROUTINE( ...., FORTRAN_FUNCTION(some_function), ...)
  348.  
  349.  
  350. This list of argument types is not neccessarily complete. CFORTRAN may be
  351. expanded to handle a new type not among the above.
  352.  
  353. N.B. The FORTRAN routines are called using macro expansions, therefore the
  354. usual caveats for expressions in arguments apply. The expressions to the
  355. routines may be evaluated more than once, leading to lower performance and in
  356. the worst case bizzare bugs.
  357.  
  358.  
  359. ii) Calling C routines from FORTRAN:
  360.     --------------------------------
  361.  
  362. Note that each of the following two statements to export a C routine to FORTRAN
  363. create FORTRAN 'wrappers', written in C, which must be compiled and linked
  364. along with the original C routines and with the FORTRAN calling code.
  365.  
  366. VAX VMS user's will have to redefine the one of the macros fcallsc or ccallsc.
  367. See the examples or existing applications for details and information.
  368.  
  369. FCALLSCSUBn(routine_name, argtype_1, ..., argtype_n)
  370.  
  371. or
  372.  
  373. FCALLSCFUNn(routine_type, routine_name, argtype_1, ..., argtype_n)
  374.  
  375. Where:
  376. 'n' = 0->7 (easily expanded to >7) stands for the number of arguments to the
  377.     routine.
  378. routine_name = the FORTRAN name of the routine (IN lowercase LETTERS).
  379. routine_type = the type of argument returned by C functions.
  380.              = DOUBLE, FLOAT, INT, LOGICAL, LONG, STRING.
  381. argtype_i    = the type of argument passed to the FORTRAN routine and must be
  382.                consistent in the definition and prototyping of the routine
  383.              = DOUBLE, FLOAT, INT, LOGICAL, LONG, STRING, STRINGV.
  384.                For arrays or for routines changing the values of any of their
  385.                 arguments; the C routines expect pointers to these arguments,
  386.                 so the keywords are prepended by a 'P'.
  387.              = PDOUBLE, PFLOAT, PINT, PLOGICAL, PLONG, PSTRING, PSTRINGV,
  388.                PVOID.
  389.                The keyword PVOID is a generic form of the nonSTRING types.
  390.                STRINGV refers to vector of strings.
  391.  
  392.  
  393. (P)STRING arguments have any trailing blanks removed before being passed to C,
  394. the same holds true for each element in (P)STRINGV. Space is malloc'd in all
  395. cases big enough to hold the original string (elements) as well as C's
  396. terminatinng '\0'. i.e. The useful size of the string (elements) is the same in
  397. both languages. PSTRING(V) => the string (elements) will be copied from the
  398. malloc'd space back into the FORTRAN bytes.
  399.  
  400. THE FOLLOWING APPLIES TO THE UNIX COMPILERS ONLY:
  401.                              ----
  402. (P)STRINGV for UNIX only: CFORTRAN cannot convert the FORTRAN vector of STRINGS
  403. to the required C vector of STRINGS without explicitly knowing the number of
  404. elements in the vector. The application must do one of the following for each
  405. (P)STRINGV argument in a routine before that routine's FCALLSCFUNn/SUBn is
  406. called:
  407.  
  408. #define routine_name_STRV_Ai NUM_ELEMS(j)
  409.  or
  410. #define routine_name_STRV_Ai NUM_ELEM_ARG(k)
  411.  or
  412. #define routine_name_STRV_Ai TERM_CHARS(l,m)
  413.  
  414. where: routine_name     is as above.
  415.        i [i=1->n.]      specifies the argument number of a STRING VECTOR.
  416.        j                would specify a fixed number of elements.
  417.        k [k=1->n. k!=i] would specify an integer argument which specifies the
  418.                         number of elements.
  419.        l [char]         the terminating character at the beginning of an
  420.                         element, indicating to cfortran that the preceeding
  421.                         elements in the vector are the valid ones.
  422.        m [m=1-...]      the number of terminating characters required to appear
  423.                         at the beginning of the terminating string element.
  424.                         Note that the terminating element is NOT possed on to
  425.                         the C routine.
  426.  
  427. e.g.
  428. CFORTRAN will pass on all elements, in the 1st and only argument to the C
  429. routine ce, of the STRING VECTOR until, but not including, the first string
  430. element beginning with 2 blank, ' ', characters.
  431.  
  432. #define ce_STRV_A1 TERM_CHARS(' ',2)
  433. FCALLSCSUB1(ce,STRINGV)
  434.  
  435. Again the lists of types are not neccessarily complete. CFORTRAN may be
  436. expanded to handle a new type not among the above.
  437.  
  438.  
  439. iii) Using C to manipulate FORTRAN COMMON BLOCKS:
  440.      -------------------------------------------------------
  441.  
  442. FORTRAN common blocks are set up with the following construct:
  443.  
  444. #define COMMON_BLOCK_NAME COMMON_BLOCK(common_block_name)
  445.  
  446. where common_block_name is given in the case shown. This construct exists to
  447. ensure that C code accessing the common block is machine independant.
  448.  
  449.  
  450. C programs can place a string (or a multidimensional array of strings) into a
  451. FORTRAN common block using the following call:
  452.  
  453. C2FCBSTR( CSTR, FSTR, DIMENSIONS);
  454.  
  455. where:
  456.  
  457. CSTR is a pointer to the first element of C's copy of the string (array).
  458.      The C code must use a duplicate of, not the original, common block string,
  459.      because the FORTRAN common block does not allocate space for C strings'
  460.      terminating '\0'.
  461.  
  462. FSTR is a pointer to the first element of the string (array) in the common
  463.      block.
  464.  
  465. DIMENSIONS is the number of dimensions of string array.
  466.      e.g. char a[10]      has DIMENSIONS=0.
  467.           char aa[10][17] has DIMENSIONS=1.
  468.           etc...
  469.  
  470. C2FCBSTR will copy the string (array) from CSTR to FSTR, padding with blanks,
  471. ' ', the trailing characters as required. C2FCBSTR uses DIMENSIONS and FSTR to
  472. determine the lengths of the individual string elements and the total number of
  473. elements in the string array.
  474.  
  475. Note that:
  476. - the number of string elements in CSTR and FSTR are identical.
  477. - for arrays of strings, the useful lengths of strings in CSTR and FSTR must be
  478.   the same. i.e. CSTR elements each have 1 extra character to accomodate the
  479.   terminating '\0'.
  480.  
  481.  
  482. FCB2CSTR( FSTR, CSTR, DIMENSIONS)
  483.  
  484. is the inverse of C2FCBSTR, and shares the same arguments and caveats. Note
  485. that FCB2CSTR copies each string element of FSTR to CSTR, minus FORTRAN
  486. strings' trailing blanks.
  487.  
  488.  
  489. CFORTRAN USERS ARE STRONGLY URGED TO EXAMINE THE COMMON BLOCK EXAMPLES IN
  490. CFORTEST.C AND CFORTEX.FOR. The use of strings in common blocks is
  491. demonstrated, along with a suggested way for C to imitate FORTRAN EQUIVALENCE'd
  492. variables.
  493.  
  494.  
  495.               ===> USER'S OF CFORTRAN NEED READ NO FURTHER <===
  496.  
  497.  
  498. III Some Details of and Comments on CFORTRAN
  499. --------------------------------------------
  500.  
  501. The following notes should be useful to those wishing to port CFORTRAN to new
  502. types of machines.
  503.  
  504.  
  505. CFORTRAN.H consist of about 1000 lines of source code. Only about 300 lines of
  506. CFORTRAN.H are interesting, the rest are slightly modified 'repeats'. Porting
  507. CFORTRAN applications, e.g. the hbook.h and cstring.c mentioned above, to
  508. other machines is trivial.  hbook.h is machine independant, and cstring.c will
  509. at most need to have the 'fcallsc' macro redefined. Porting CFORTRAN itself
  510. requires a solid knowledge of the new machines C preprocessor, and its FORTRAN
  511. argument passing mechanisms. Logically CFORTRAN exists as two halves, a "C
  512. CALLS FORTRAN" and a "FORTRAN CALLS C" utility. In some cases it may be
  513. perfectly reasonable to port only 'one half' of CFORTRAN onto a new system.
  514.  
  515.  
  516. CFORTRAN is simple enough to be used by the most basic of applications, i.e.
  517. making a single C/FORTRAN routine available to the FORTRAN/C programmers. Yet
  518. CFORTRAN is powerful enough to easily make entire C/FORTRAN libraries available
  519. to FORTRAN/C programmers.
  520.  
  521.  
  522. CFORTRAN is the ideal tool for FORTRAN libraries which are being rewritten in
  523. C. It allows the routines to be written in 'natural C', without having to
  524. consider the FORTRAN argument passing mechanisms of any machine. It also allows
  525. C code accessing these rewritten routines, to use the C entry point. Without
  526. CFORTRAN one could fall into the perverse practice of C code calling a C
  527. function using FORTRAN argument passing mechanisms!
  528.  
  529.  
  530. Perhap the philosophy and mechanisms of CFORTRAN could be used and extended
  531. to create other language bridges such as ADAFORTRAN, CPASCAL, COCCAM, etc.
  532.  
  533.  
  534.  
  535. IV Pros, Cons and Improvements to CFORTRAN
  536. ------------------------------------------
  537.  
  538. The C calls FORTRAN half is all pro. A list would include:
  539.  
  540. i) Machine independant and C or FORTRAN independant calls to FORTRAN code.
  541. e.g. C      :       hbook1(1,"pT spectrum of pi+",100,0.,5.,0.);
  542.      FORTRAN:  call hbook1(1,'pT spectrum of pi+',100,0.,5.,0.)
  543. ii) Non-STRING(V) arguments have no, or at most one assignment as overhead.
  544. iii) 'Input only' arguments are protected by using an intermediate value.
  545. iv) I don't think STRING(V)'s can be handled much faster, even in individually
  546. tuned routines.
  547.  
  548.  
  549. The FORTRAN calls C half has the fundamental inelegancy of using an
  550. intermediate function. Perhaps a preprocessor and those %DEF (?) FORTRAN
  551. extensions could help. I don't know, I'm just a C programmer who wants to use
  552. routines written in FORTRAN.
  553.  
  554.  
  555. It might make sense to have separate CFORTRAN and FORTRANC utilities, but I've
  556. left them tied together for the moment.
  557.  
  558.  
  559. Using FCALLSCFUNn and CCALLSFFUNn for a function in the same source code, i.e.
  560. creating the FORTRAN entry to a C function and then allowing C to call this
  561. FORTRAN entry, obviously serves only test purposes. Note that the order given
  562. above is a must, and that a compiler warning is generated because the FORTRAN
  563. function prototype generated by CCALLSFFUNn does not match the entry point
  564. created by FCALLSCFUNn. This might be fixable, see CFORTRAN.H, but since these
  565. combo.'s are used in tests only, I don't think it's worth it. I say might
  566. because I'm not sure one can satisfy the case sensitive compiler here.
  567.  
  568.  
  569.  
  570. V Machine Dependancies of CFORTRAN
  571. ----------------------------------
  572.  
  573. I leave it to the lucky programmer porting CFORTRAN to a new machine, to
  574. discover the FORTRAN argument passing mechanisms. A safe starting point is to
  575. assume that variables and arrays are simply passed by reference as they are in
  576. VAX VMS and UNIX, but I make no guarantees. Strings, and n-dimensional arrays
  577. of strings are a different story. I doubt that any systems do it quite like VAX
  578. VMS does it, so that the UNIX version may provide an easier starting point.
  579.  
  580.  
  581. CFORTRAN uses and abuses the ## operator. Although the ## operator proper does
  582. not exist in  VAX VMS nor in MIPS C, a kludge does; /**/ with no space allowed
  583. between the slashes, '/', and the macros or tags one wishes to concatenate. e.g.
  584. #define concat(a,b) a/**/b   /* works*/
  585. main()
  586. {
  587.   concat(pri,ntf)("hello");           /* e.g. */
  588. }
  589. N.B. I have learnt of an alternate kludge to /**/ which could replace ##.
  590. On some compilers without ##, /**/ may also not work, this new kludge may be a
  591. way out. For more info., porters of CFORTRAN should contact me.
  592.  
  593.  
  594.  
  595. VI Machine Dependancies of CFORTRAN Applications
  596. ------------------------------------------------
  597.  
  598. The only machine dependancy of CFORTRAN Applications I know of are the names of
  599. routines written in C to be called by FORTRAN. This problem arises under VAX
  600. VMS because the 'interpreter' routine, written in C and called by the FORTRAN
  601. code, needs an object code name for itself different from that of the original
  602. C routine it will try to invoke.
  603.  
  604. This problem does not exist with some FORTRAN compilers. E.g. MIPS' f77
  605. appends  each FORTRAN module name with a single underscore, '_', character.
  606. Hence, if all C interpreter routines are prepended with this '_', all is well.
  607. A similar solution may exist when a case sensitive linker is available.
  608.  
  609. For other compilers, which leave the FORTRAN name as the module name, there
  610. exist only two situations.
  611. i)  If the C source code for the routines is NOT available, the calls to
  612.     the C routine from FORTRAN must use a different name. In fortran.h the
  613.     'fcallsc' [f-calls-c] macro exists to modify the names of the interpreter
  614.     routines in a consistent manner.
  615. ii) If the C source code does exist, a decision for one of the two following
  616.     possible resolutions has to be made.
  617.     a) The source code is left alone and an identical approach to i) above is
  618.        taken. This might be preferable for smaller applications, where the
  619.        C expertise doesn't exist or has better things to do.
  620.     b) This is the better method in that it maintains absolute transparency
  621.        at the user level, unfortunately it is more complicated, but with care
  622.        it is just as robust at the user level. In short, the objects compiled
  623.        from the original C modules are renamed. This is done in the header
  624.        file prototyping the original C routines code. Unfortunately this
  625.        translation also has to be done when C code calls the C routine. Since
  626.        the name modification is done in the routines header file, it's hidden
  627.        from the C user, unless they carefully examines the libraries and/or
  628.        object code. In CFORTRAN.H the 'ccallsc' [c-calls-c] macro exists to
  629.        modify the names of the original routines in a consistent manner.
  630.  
  631.  
  632.  
  633.  
  634. THIS SOFTWARE IS PUBLIC DOMAIN. IT MAY BE FREELY COPIED AND USED EVERYWHERE. IT
  635. MAY BE DISTRIBUTED WITH NON-COMMERCIAL PRODUCTS, ASSUMING PROPER CREDIT TO THE
  636. AUTHOR IS GIVEN, BUT IT SHOULD NOT BE RESOLD. IF YOU WANT TO DISTRIBUTE THE
  637. SOFTWARE WITH A COMMERCIAL PRODUCT, CONTACT THE AUTHOR.
  638. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
  639. EXPRESSED OR IMPLIED. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
  640. SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST
  641. OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
  642.  
  643. VAX VMS, Silicon Graphics (SGI), DECstations, Mips RISC and IBM RS/6000
  644. are registered trademarks.
  645.  
  646. /* end: cfortran.doc */
  647.