home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / f2c / src / init.c < prev    next >
C/C++ Source or Header  |  2000-06-23  |  10KB  |  454 lines

  1. /****************************************************************
  2. Copyright 1990 by AT&T Bell Laboratories and Bellcore.
  3.  
  4. Permission to use, copy, modify, and distribute this software
  5. and its documentation for any purpose and without fee is hereby
  6. granted, provided that the above copyright notice appear in all
  7. copies and that both that the copyright notice and this
  8. permission notice and warranty disclaimer appear in supporting
  9. documentation, and that the names of AT&T Bell Laboratories or
  10. Bellcore or any of their entities not be used in advertising or
  11. publicity pertaining to distribution of the software without
  12. specific, written prior permission.
  13.  
  14. AT&T and Bellcore disclaim all warranties with regard to this
  15. software, including all implied warranties of merchantability
  16. and fitness.  In no event shall AT&T or Bellcore be liable for
  17. any special, indirect or consequential damages or any damages
  18. whatsoever resulting from loss of use, data or profits, whether
  19. in an action of contract, negligence or other tortious action,
  20. arising out of or in connection with the use or performance of
  21. this software.
  22. ****************************************************************/
  23.  
  24. #include "defs.h"
  25. #include "output.h"
  26. #include "iob.h"
  27.  
  28. /* State required for the C output */
  29. char *fl_fmt_string;        /* Float format string */
  30. char *db_fmt_string;            /* Double format string */
  31. char *cm_fmt_string;        /* Complex format string */
  32. char *dcm_fmt_string;        /* Double complex format string */
  33.  
  34. chainp new_vars = CHNULL;    /* List of newly created locals in this
  35.                    function.  These may have identifiers
  36.                    which have underscores and more than VL
  37.                    characters */
  38. chainp used_builtins = CHNULL;    /* List of builtins used by this function.
  39.                    These are all Addrps with UNAM_EXTERN
  40.                    */
  41. chainp assigned_fmts = CHNULL;    /* assigned formats */
  42. chainp allargs;            /* union of args in all entry points */
  43. chainp earlylabs;        /* labels seen before enddcl() */
  44. char main_alias[52];        /* PROGRAM name, if any is given */
  45. int tab_size = 4;
  46.  
  47.  
  48. FILEP infile;
  49. FILEP diagfile;
  50.  
  51. FILEP c_file;
  52. FILEP pass1_file;
  53. FILEP initfile;
  54. FILEP blkdfile;
  55.  
  56.  
  57. char token[MAXTOKENLEN];
  58. int toklen;
  59. long lineno;            /* Current line in the input file, NOT the
  60.                    Fortran statement label number */
  61. char *infname;
  62. int needkwd;
  63. struct Labelblock *thislabel    = NULL;
  64. int nerr;
  65. int nwarn;
  66.  
  67. flag saveall;
  68. flag substars;
  69. int parstate    = OUTSIDE;
  70. flag headerdone    = NO;
  71. int blklevel;
  72. int doin_setbound;
  73. int impltype[26];
  74. ftnint implleng[26];
  75. int implstg[26];
  76.  
  77. int tyint    = TYLONG ;
  78. int tylogical    = TYLONG;
  79. int typesize[NTYPES] = {
  80.     1, SZADDR, SZSHORT, SZLONG, SZLONG, 2*SZLONG,
  81.         2*SZLONG, 4*SZLONG, SZLONG, 1, 1, 0,
  82.         4*SZLONG + SZADDR,    /* sizeof(cilist) */
  83.         4*SZLONG + 2*SZADDR,    /* sizeof(icilist) */
  84.         4*SZLONG + 5*SZADDR,    /* sizeof(olist) */
  85.         2*SZLONG + SZADDR,    /* sizeof(cllist) */
  86.         2*SZLONG,        /* sizeof(alist) */
  87.         11*SZLONG + 15*SZADDR    /* sizeof(inlist) */
  88.         };
  89.  
  90. int typealign[NTYPES] = {
  91.     1, ALIADDR, ALISHORT, ALILONG, ALILONG, ALIDOUBLE,
  92.     ALILONG, ALIDOUBLE, ALILONG, 1, 1, 1,
  93.     ALILONG, ALILONG, ALILONG, ALILONG, ALILONG, ALILONG};
  94.  
  95. int type_choice[4] = { TYDREAL, TYSHORT, TYLONG,  TYSHORT };
  96.  
  97. char *typename[] = {
  98.     "<<unknown>>",
  99.     "address",
  100.     "shortint",
  101.     "integer",
  102.     "real",
  103.     "doublereal",
  104.     "complex",
  105.     "doublecomplex",
  106.     "logical",
  107.     "char"    /* character */
  108.     };
  109.  
  110. int type_pref[NTYPES] = { 0, 0, 2, 4, 5, 7, 6, 8, 3, 1 };
  111.  
  112. char *protorettypes[] = {
  113.     "?", "??", "shortint", "integer", "real", "doublereal",
  114.     "C_f", "Z_f", "logical", "H_f", "int"
  115.     };
  116.  
  117. char *casttypes[TYSUBR+1] = {
  118.     "U_fp", "??bug??",
  119.     "J_fp", "I_fp", "R_fp",
  120.     "D_fp", "C_fp", "Z_fp",
  121.     "L_fp", "H_fp", "S_fp"
  122.     };
  123. char *usedcasts[TYSUBR+1];
  124.  
  125. char *dfltarg[] = {
  126.     0, 0,
  127.     "(shortint *)0", "(integer *)0", "(real *)0",
  128.     "(doublereal *)0", "(complex *)0", "(doublecomplex *)0",
  129.     "(logical *)0", "(char *)0"
  130.     };
  131.  
  132. static char *dflt0proc[] = {
  133.     0, 0,
  134.     "(shortint (*)())0", "(integer (*)())0", "(real (*)())0",
  135.     "(doublereal (*)())0", "(complex (*)())0", "(doublecomplex (*)())0",
  136.     "(logical (*)())0", "(char (*)())0", "(int (*)())0"
  137.     };
  138.  
  139. char *dflt1proc[] = { "(U_fp)0", "(??bug??)0",
  140.     "(J_fp)0", "(I_fp)0", "(R_fp)0",
  141.     "(D_fp)0", "(C_fp)0", "(Z_fp)0",
  142.     "(L_fp)0", "(H_fp)0", "(S_fp)0"
  143.     };
  144.  
  145. char **dfltproc = dflt0proc;
  146.  
  147. static char Bug[] = "bug";
  148.  
  149. char *ftn_types[] = { "external", "??",
  150.     "integer*2", "integer", "real",
  151.     "double precision", "complex", "double complex",
  152.     "logical", "character", "subroutine",
  153.     Bug,Bug,Bug,Bug,Bug,Bug,Bug,Bug,Bug, "ftnlen"
  154.     };
  155.  
  156. int init_ac[TYSUBR+1] = { 0,0,0,0,0,0, 1, 1, 0, 2};
  157.  
  158. int proctype    = TYUNKNOWN;
  159. char *procname;
  160. int rtvlabel[NTYPES0];
  161. Addrp retslot;            /* Holds automatic variable which was
  162.                    allocated the function return value
  163.                    */
  164. Addrp xretslot[NTYPES0];    /* for multiple entry points */
  165. int cxslot    = -1;
  166. int chslot    = -1;
  167. int chlgslot    = -1;
  168. int procclass    = CLUNKNOWN;
  169. int nentry;
  170. int nallargs;
  171. int nallchargs;
  172. flag multitype;
  173. ftnint procleng;
  174. long lastiolabno;
  175. int lastlabno;
  176. int lastvarno;
  177. int lastargslot;
  178. int autonum[TYVOID];
  179. char *av_pfix[TYVOID] = {"??TYUNKNOWN??", "a","s","i","r","d","q","z","L","ch",
  180.              "??TYSUBR??", "??TYERROR??","ci", "ici",
  181.              "o", "cl", "al", "ioin" };
  182.  
  183. extern int maxctl;
  184. struct Ctlframe *ctls;
  185. struct Ctlframe *ctlstack;
  186. struct Ctlframe *lastctl;
  187.  
  188. Namep regnamep[MAXREGVAR];
  189. int highregvar;
  190. int nregvar;
  191.  
  192. extern int maxext;
  193. Extsym *extsymtab;
  194. Extsym *nextext;
  195. Extsym *lastext;
  196.  
  197. extern int maxequiv;
  198. struct Equivblock *eqvclass;
  199.  
  200. extern int maxhash;
  201. struct Hashentry *hashtab;
  202. struct Hashentry *lasthash;
  203.  
  204. extern int maxstno;        /* Maximum number of statement labels */
  205. struct Labelblock *labeltab;
  206. struct Labelblock *labtabend;
  207. struct Labelblock *highlabtab;
  208.  
  209. int maxdim    = MAXDIM;
  210. struct Rplblock *rpllist    = NULL;
  211. struct Chain *curdtp    = NULL;
  212. flag toomanyinit;
  213. ftnint curdtelt;
  214. chainp templist[TYVOID];
  215. chainp holdtemps;
  216. int dorange    = 0;
  217. struct Entrypoint *entries    = NULL;
  218.  
  219. chainp chains    = NULL;
  220.  
  221. flag inioctl;
  222. int iostmt;
  223. int nioctl;
  224. int nequiv    = 0;
  225. int eqvstart    = 0;
  226. int nintnames    = 0;
  227.  
  228. struct Literal *litpool;
  229. int nliterals;
  230.  
  231. char dflttype[26];
  232. char hextoi_tab[Table_size], Letters[Table_size];
  233. char *ei_first, *ei_next, *ei_last;
  234. char *wh_first, *wh_next, *wh_last;
  235.  
  236. #define ALLOCN(n,x)    (struct x *) ckalloc((n)*sizeof(struct x))
  237.  
  238. fileinit()
  239. {
  240.     register char *s;
  241.     register int i, j;
  242.     extern void fmt_init(), mem_init(), np_init();
  243.  
  244.     lastiolabno = 100000;
  245.     lastlabno = 0;
  246.     lastvarno = 0;
  247.     nliterals = 0;
  248.     nerr = 0;
  249.  
  250.     infile = stdin;
  251.  
  252.     memset(dflttype, tyreal, 26);
  253.     memset(dflttype + 'i' - 'a', tyint, 6);
  254.     memset(hextoi_tab, 16, sizeof(hextoi_tab));
  255.     for(i = 0, s = "0123456789abcdef"; *s; i++, s++)
  256.         hextoi(*s) = i;
  257.     for(i = 10, s = "ABCDEF"; *s; i++, s++)
  258.         hextoi(*s) = i;
  259.     for(j = 0, s = "abcdefghijklmnopqrstuvwxyz"; i = *s++; j++)
  260.         Letters[i] = Letters[i+'A'-'a'] = j;
  261.  
  262.     ctls = ALLOCN(maxctl+1, Ctlframe);
  263.     extsymtab = ALLOCN(maxext, Extsym);
  264.     eqvclass = ALLOCN(maxequiv, Equivblock);
  265.     hashtab = ALLOCN(maxhash, Hashentry);
  266.     labeltab = ALLOCN(maxstno, Labelblock);
  267.     litpool = ALLOCN(maxliterals, Literal);
  268.     fmt_init();
  269.     mem_init();
  270.     np_init();
  271.  
  272.     ctlstack = ctls++;
  273.     lastctl = ctls + maxctl;
  274.     nextext = extsymtab;
  275.     lastext = extsymtab + maxext;
  276.     lasthash = hashtab + maxhash;
  277.     labtabend = labeltab + maxstno;
  278.     highlabtab = labeltab;
  279.     main_alias[0] = '\0';
  280.     if (forcedouble)
  281.         dfltproc[TYREAL] = dfltproc[TYDREAL];
  282.  
  283. /* Initialize the routines for providing C output */
  284.  
  285.     out_init ();
  286. }
  287.  
  288. hashclear()    /* clear hash table */
  289. {
  290.     register struct Hashentry *hp;
  291.     register Namep p;
  292.     register struct Dimblock *q;
  293.     register int i;
  294.  
  295.     for(hp = hashtab ; hp < lasthash ; ++hp)
  296.         if(p = hp->varp)
  297.         {
  298.             frexpr(p->vleng);
  299.             if(q = p->vdim)
  300.             {
  301.                 for(i = 0 ; i < q->ndim ; ++i)
  302.                 {
  303.                     frexpr(q->dims[i].dimsize);
  304.                     frexpr(q->dims[i].dimexpr);
  305.                 }
  306.                 frexpr(q->nelt);
  307.                 frexpr(q->baseoffset);
  308.                 frexpr(q->basexpr);
  309.                 free( (charptr) q);
  310.             }
  311.             if(p->vclass == CLNAMELIST)
  312.                 frchain( &(p->varxptr.namelist) );
  313.             free( (charptr) p);
  314.             hp->varp = NULL;
  315.         }
  316.     }
  317.  
  318. procinit()
  319. {
  320.     register struct Labelblock *lp;
  321.     struct Chain *cp;
  322.     int i;
  323.     struct memblock;
  324.     extern struct memblock *curmemblock, *firstmemblock;
  325.     extern char *mem_first, *mem_next, *mem_last, *mem0_last;
  326.     extern void frexchain();
  327.  
  328.     curmemblock = firstmemblock;
  329.     mem_next = mem_first;
  330.     mem_last = mem0_last;
  331.     ei_next = ei_first = ei_last = 0;
  332.     wh_next = wh_first = wh_last = 0;
  333.     iob_list = 0;
  334.     for(i = 0; i < 9; i++)
  335.         io_structs[i] = 0;
  336.  
  337.     parstate = OUTSIDE;
  338.     headerdone = NO;
  339.     blklevel = 1;
  340.     saveall = NO;
  341.     substars = NO;
  342.     nwarn = 0;
  343.     thislabel = NULL;
  344.     needkwd = 0;
  345.  
  346.     proctype = TYUNKNOWN;
  347.     procname = "MAIN_";
  348.     procclass = CLUNKNOWN;
  349.     nentry = 0;
  350.     nallargs = nallchargs = 0;
  351.     multitype = NO;
  352.     retslot = NULL;
  353.     for(i = 0; i < NTYPES0; i++) {
  354.         frexpr((expptr)xretslot[i]);
  355.         xretslot[i] = 0;
  356.         }
  357.     cxslot = -1;
  358.     chslot = -1;
  359.     chlgslot = -1;
  360.     procleng = 0;
  361.     blklevel = 1;
  362.     lastargslot = 0;
  363.  
  364.     for(lp = labeltab ; lp < labtabend ; ++lp)
  365.         lp->stateno = 0;
  366.  
  367.     hashclear();
  368.  
  369. /* Clear the list of newly generated identifiers from the previous
  370.    function */
  371.  
  372.     frexchain(&new_vars);
  373.     frexchain(&used_builtins);
  374.     frchain(&assigned_fmts);
  375.     frchain(&allargs);
  376.     frchain(&earlylabs);
  377.  
  378.     nintnames = 0;
  379.     highlabtab = labeltab;
  380.  
  381.     ctlstack = ctls - 1;
  382.     for(i = TYADDR; i < TYVOID; i++) {
  383.         for(cp = templist[i]; cp ; cp = cp->nextp)
  384.             free( (charptr) (cp->datap) );
  385.         frchain(templist + i);
  386.         autonum[i] = 0;
  387.         }
  388.     holdtemps = NULL;
  389.     dorange = 0;
  390.     nregvar = 0;
  391.     highregvar = 0;
  392.     entries = NULL;
  393.     rpllist = NULL;
  394.     inioctl = NO;
  395.     eqvstart += nequiv;
  396.     nequiv = 0;
  397.     dcomplex_seen = 0;
  398.  
  399.     for(i = 0 ; i<NTYPES0 ; ++i)
  400.         rtvlabel[i] = 0;
  401.  
  402.     if(undeftype)
  403.         setimpl(TYUNKNOWN, (ftnint) 0, 'a', 'z');
  404.     else
  405.     {
  406.         setimpl(tyreal, (ftnint) 0, 'a', 'z');
  407.         setimpl(tyint,  (ftnint) 0, 'i', 'n');
  408.     }
  409.     setimpl(-STGBSS, (ftnint) 0, 'a', 'z');    /* set class */
  410.     setlog();
  411. }
  412.  
  413.  
  414.  
  415.  
  416. setimpl(type, length, c1, c2)
  417. int type;
  418. ftnint length;
  419. int c1, c2;
  420. {
  421.     int i;
  422.     char buff[100];
  423.  
  424.     if(c1==0 || c2==0)
  425.         return;
  426.  
  427.     if(c1 > c2) {
  428.         sprintf(buff, "characters out of order in implicit:%c-%c", c1, c2);
  429.         err(buff);
  430.         }
  431.     else {
  432.         c1 = letter(c1);
  433.         c2 = letter(c2);
  434.         if(type < 0)
  435.             for(i = c1 ; i<=c2 ; ++i)
  436.                 implstg[i] = - type;
  437.         else {
  438.             type = lengtype(type, length);
  439.             if(type == TYCHAR) {
  440.                 if (length < 0) {
  441.                     err("length (*) in implicit");
  442.                     length = 1;
  443.                     }
  444.                 }
  445.             else if (type != TYLONG)
  446.                 length = 0;
  447.             for(i = c1 ; i<=c2 ; ++i) {
  448.                 impltype[i] = type;
  449.                 implleng[i] = length;
  450.                 }
  451.             }
  452.         }
  453.     }
  454.