home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / f2c-93.04.28-src.tgz / tar.out / fsf / f2c / src / proc.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  35KB  |  1,601 lines

  1. /****************************************************************
  2. Copyright 1990, 1991, 1992, 1993 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 "names.h"
  26. #include "output.h"
  27. #include "p1defs.h"
  28.  
  29. #define EXNULL (union Expression *)0
  30.  
  31. LOCAL dobss(), docomleng(), docommon(), doentry(),
  32.     epicode(), nextarg(), retval();
  33.  
  34. static char Blank[] = BLANKCOMMON;
  35.  
  36.  static char *postfix[] = { "g", "h", "i", "r", "d", "c", "z", "g", "h", "i" };
  37.  
  38.  chainp new_procs;
  39.  int prev_proc, proc_argchanges, proc_protochanges;
  40.  
  41.  void
  42. changedtype(q)
  43.  Namep q;
  44. {
  45.     char buf[200];
  46.     int qtype, type1;
  47.     register Extsym *e;
  48.     Argtypes *at;
  49.  
  50.     if (q->vtypewarned)
  51.         return;
  52.     q->vtypewarned = 1;
  53.     qtype = q->vtype;
  54.     e = &extsymtab[q->vardesc.varno];
  55.     if (!(at = e->arginfo)) {
  56.         if (!e->exused)
  57.             return;
  58.         }
  59.     else if (at->changes & 2 && qtype != TYUNKNOWN && !at->defined)
  60.         proc_protochanges++;
  61.     type1 = e->extype;
  62.     if (type1 == TYUNKNOWN)
  63.         return;
  64.     if (qtype == TYUNKNOWN)
  65.         /* e.g.,
  66.             subroutine foo
  67.             end
  68.             external foo
  69.             call goo(foo)
  70.             end
  71.         */
  72.         return;
  73.     sprintf(buf, "%.90s: inconsistent declarations:\n\
  74.     here %s%s, previously %s%s.", q->fvarname, ftn_types[qtype],
  75.         qtype == TYSUBR ? "" : " function",
  76.         ftn_types[type1], type1 == TYSUBR ? "" : " function");
  77.     warn(buf);
  78.     }
  79.  
  80.  void
  81. unamstring(q, s)
  82.  register Addrp q;
  83.  register char *s;
  84. {
  85.     register int k;
  86.     register char *t;
  87.  
  88.     k = strlen(s);
  89.     if (k < IDENT_LEN) {
  90.         q->uname_tag = UNAM_IDENT;
  91.         t = q->user.ident;
  92.         }
  93.     else {
  94.         q->uname_tag = UNAM_CHARP;
  95.         q->user.Charp = t = mem(k+1, 0);
  96.         }
  97.     strcpy(t, s);
  98.     }
  99.  
  100.  static void
  101. fix_entry_returns()    /* for multiple entry points */
  102. {
  103.     Addrp a;
  104.     int i;
  105.     struct Entrypoint *e;
  106.     Namep np;
  107.  
  108.     e = entries = (struct Entrypoint *)revchain((chainp)entries);
  109.     allargs = revchain(allargs);
  110.     if (!multitype)
  111.         return;
  112.  
  113.     /* TYLOGICAL should have been turned into TYLONG or TYSHORT by now */
  114.  
  115.     for(i = TYINT1; i <= TYLOGICAL; i++)
  116.         if (a = xretslot[i])
  117.             sprintf(a->user.ident, "(*ret_val).%s",
  118.                 postfix[i-TYINT1]);
  119.  
  120.     do {
  121.         np = e->enamep;
  122.         switch(np->vtype) {
  123.             case TYINT1:
  124.             case TYSHORT:
  125.             case TYLONG:
  126. #ifdef TYQUAD
  127.             case TYQUAD:
  128. #endif
  129.             case TYREAL:
  130.             case TYDREAL:
  131.             case TYCOMPLEX:
  132.             case TYDCOMPLEX:
  133.             case TYLOGICAL1:
  134.             case TYLOGICAL2:
  135.             case TYLOGICAL:
  136.                 np->vstg = STGARG;
  137.             }
  138.         }
  139.         while(e = e->entnextp);
  140.     }
  141.  
  142.  static void
  143. putentries(outfile)    /* put out wrappers for multiple entries */
  144.  FILE *outfile;
  145. {
  146.     char base[IDENT_LEN];
  147.     struct Entrypoint *e;
  148.     Namep *A, *Ae, *Ae1, **Alp, *a, **a1, np;
  149.     chainp args, lengths, length_comp();
  150.     void listargs(), list_arg_types();
  151.     int i, k, mt, nL, type;
  152.     extern char *dfltarg[], **dfltproc;
  153.  
  154.     e = entries;
  155.     if (!e->enamep) /* only possible with erroneous input */
  156.         return;
  157.     nL = (nallargs + nallchargs) * sizeof(Namep *);
  158.     A = (Namep *)ckalloc(nL + nallargs*sizeof(Namep **));
  159.     Ae = A + nallargs;
  160.     Alp = (Namep **)(Ae1 = Ae + nallchargs);
  161.     i = k = 0;
  162.     for(a1 = Alp, args = allargs; args; a1++, args = args->nextp) {
  163.         np = (Namep)args->datap;
  164.         if (np->vtype == TYCHAR && np->vclass != CLPROC)
  165.             *a1 = &Ae[i++];
  166.         }
  167.  
  168.     mt = multitype;
  169.     multitype = 0;
  170.     sprintf(base, "%s0_", e->enamep->cvarname);
  171.     do {
  172.         np = e->enamep;
  173.         lengths = length_comp(e, 0);
  174.         proctype = type = np->vtype;
  175.         if (protofile)
  176.             protowrite(protofile, type, np->cvarname, e, lengths);
  177.         nice_printf(outfile, "\n%s ", c_type_decl(type, 1));
  178.         nice_printf(outfile, "%s", np->cvarname);
  179.         if (!Ansi) {
  180.             listargs(outfile, e, 0, lengths);
  181.             nice_printf(outfile, "\n");
  182.             }
  183.             list_arg_types(outfile, e, lengths, 0, "\n");
  184.         nice_printf(outfile, "{\n");
  185.         frchain(&lengths);
  186.         next_tab(outfile);
  187.         if (mt)
  188.             nice_printf(outfile,
  189.                 "Multitype ret_val;\n%s(%d, &ret_val",
  190.                 base, k); /*)*/
  191.         else if (ISCOMPLEX(type))
  192.             nice_printf(outfile, "%s(%d,%s", base, k,
  193.                 xretslot[type]->user.ident); /*)*/
  194.         else if (type == TYCHAR)
  195.             nice_printf(outfile,
  196.                 "%s(%d, ret_val, ret_val_len", base, k); /*)*/
  197.         else
  198.             nice_printf(outfile, "return %s(%d", base, k); /*)*/
  199.         k++;
  200.         memset((char *)A, 0, nL);
  201.         for(args = e->arglist; args; args = args->nextp) {
  202.             np = (Namep)args->datap;
  203.             A[np->argno] = np;
  204.             if (np->vtype == TYCHAR && np->vclass != CLPROC)
  205.                 *Alp[np->argno] = np;
  206.             }
  207.         args = allargs;
  208.         for(a = A; a < Ae; a++, args = args->nextp)
  209.             nice_printf(outfile, ", %s", (np = *a)
  210.                 ? np->cvarname
  211.                 : ((Namep)args->datap)->vclass == CLPROC
  212.                 ? dfltproc[((Namep)args->datap)->vtype]
  213.                 : dfltarg[((Namep)args->datap)->vtype]);
  214.         for(; a < Ae1; a++)
  215.             if (np = *a)
  216.                 nice_printf(outfile, ", %s_len", np->fvarname);
  217.             else
  218.                 nice_printf(outfile, ", (ftnint)0");
  219.         nice_printf(outfile, /*(*/ ");\n");
  220.         if (mt) {
  221.             if (type == TYCOMPLEX)
  222.                 nice_printf(outfile,
  223.             "r_v->r = ret_val.c.r; r_v->i = ret_val.c.i;\nreturn 0;\n");
  224.             else if (type == TYDCOMPLEX)
  225.                 nice_printf(outfile,
  226.             "r_v->r = ret_val.z.r; r_v->i = ret_val.z.i;\nreturn 0;\n");
  227.             else nice_printf(outfile, "return ret_val.%s;\n",
  228.                 postfix[type-TYINT1]);
  229.             }
  230.         nice_printf(outfile, "}\n");
  231.         prev_tab(outfile);
  232.         }
  233.         while(e = e->entnextp);
  234.     free((char *)A);
  235.     }
  236.  
  237.  static void
  238. entry_goto(outfile)
  239.  FILEP outfile;
  240. {
  241.     struct Entrypoint *e = entries;
  242.     int k = 0;
  243.  
  244.     nice_printf(outfile, "switch(n__) {\n");
  245.     next_tab(outfile);
  246.     while(e = e->entnextp)
  247.         nice_printf(outfile, "case %d: goto %s;\n", ++k,
  248.             user_label((long)(extsymtab - e->entryname - 1)));
  249.     nice_printf(outfile, "}\n\n");
  250.     prev_tab(outfile);
  251.     }
  252.  
  253. /* start a new procedure */
  254.  
  255. newproc()
  256. {
  257.     if(parstate != OUTSIDE)
  258.     {
  259.         execerr("missing end statement", CNULL);
  260.         endproc();
  261.     }
  262.  
  263.     parstate = INSIDE;
  264.     procclass = CLMAIN;    /* default */
  265. }
  266.  
  267.  static void
  268. zap_changes()
  269. {
  270.     register chainp cp;
  271.     register Argtypes *at;
  272.  
  273.     /* arrange to get correct count of prototypes that would
  274.        change by running f2c again */
  275.  
  276.     if (prev_proc && proc_argchanges)
  277.         proc_protochanges++;
  278.     prev_proc = proc_argchanges = 0;
  279.     for(cp = new_procs; cp; cp = cp->nextp)
  280.         if (at = ((Namep)cp->datap)->arginfo)
  281.             at->changes &= ~1;
  282.     frchain(&new_procs);
  283.     }
  284.  
  285. /* end of procedure. generate variables, epilogs, and prologs */
  286.  
  287. endproc()
  288. {
  289.     struct Labelblock *lp;
  290.     Extsym *ext;
  291.  
  292.     if(parstate < INDATA)
  293.         enddcl();
  294.     if(ctlstack >= ctls)
  295.         err("DO loop or BLOCK IF not closed");
  296.     for(lp = labeltab ; lp < labtabend ; ++lp)
  297.         if(lp->stateno!=0 && lp->labdefined==NO)
  298.             errstr("missing statement label %s",
  299.                 convic(lp->stateno) );
  300.  
  301. /* Save copies of the common variables in extptr -> allextp */
  302.  
  303.     for (ext = extsymtab; ext < nextext; ext++)
  304.         if (ext -> extstg == STGCOMMON && ext -> extp) {
  305.             extern int usedefsforcommon;
  306.  
  307. /* Write out the abbreviations for common block reference */
  308.  
  309.             copy_data (ext -> extp);
  310.             if (usedefsforcommon) {
  311.                 wr_abbrevs (c_file, 1, ext -> extp);
  312.                 ext -> used_here = 1;
  313.                 }
  314.             else
  315.                 ext -> extp = CHNULL;
  316.  
  317.             }
  318.  
  319.     if (nentry > 1)
  320.         fix_entry_returns();
  321.     epicode();
  322.     donmlist();
  323.     dobss();
  324.     start_formatting ();
  325.     if (nentry > 1)
  326.         putentries(c_file);
  327.  
  328.     zap_changes();
  329.     procinit();    /* clean up for next procedure */
  330. }
  331.  
  332.  
  333.  
  334. /* End of declaration section of procedure.  Allocate storage. */
  335.  
  336. enddcl()
  337. {
  338.     register struct Entrypoint *ep;
  339.     struct Entrypoint *ep0;
  340.     extern void freetemps();
  341.     chainp cp;
  342.     extern char *err_proc;
  343.     static char comblks[] = "common blocks";
  344.  
  345.     err_proc = comblks;
  346.     docommon();
  347.  
  348. /* Now the hash table entries for fields of common blocks have STGCOMMON,
  349.    vdcldone, voffset, and varno.  And the common blocks themselves have
  350.    their full sizes in extleng. */
  351.  
  352.     err_proc = "equivalences";
  353.     doequiv();
  354.  
  355.     err_proc = comblks;
  356.     docomleng();
  357.  
  358. /* This implies that entry points in the declarations are buffered in
  359.    entries   but not written out */
  360.  
  361.     err_proc = "entries";
  362.     if (ep = ep0 = (struct Entrypoint *)revchain((chainp)entries)) {
  363.         /* entries could be 0 in case of an error */
  364.         do doentry(ep);
  365.             while(ep = ep->entnextp);
  366.         entries = (struct Entrypoint *)revchain((chainp)ep0);
  367.         }
  368.  
  369.     err_proc = 0;
  370.     parstate = INEXEC;
  371.     p1put(P1_PROCODE);
  372.     freetemps();
  373.     if (earlylabs) {
  374.         for(cp = earlylabs = revchain(earlylabs); cp; cp = cp->nextp)
  375.             p1_label((long)cp->datap);
  376.         frchain(&earlylabs);
  377.         }
  378. }
  379.  
  380. /* ROUTINES CALLED WHEN ENCOUNTERING ENTRY POINTS */
  381.  
  382. /* Main program or Block data */
  383.  
  384. startproc(progname, class)
  385. Extsym * progname;
  386. int class;
  387. {
  388.     register struct Entrypoint *p;
  389.  
  390.     p = ALLOC(Entrypoint);
  391.     if(class == CLMAIN) {
  392.         puthead(CNULL, CLMAIN);
  393.         if (progname)
  394.             strcpy (main_alias, progname->cextname);
  395.     } else
  396.         puthead(CNULL, CLBLOCK);
  397.     if(class == CLMAIN)
  398.         newentry( mkname(" MAIN"), 0 )->extinit = 1;
  399.     p->entryname = progname;
  400.     entries = p;
  401.  
  402.     procclass = class;
  403.     fprintf(diagfile, "   %s", (class==CLMAIN ? "MAIN" : "BLOCK DATA") );
  404.     if(progname) {
  405.         fprintf(diagfile, " %s", progname->fextname);
  406.         procname = progname->cextname;
  407.         }
  408.     fprintf(diagfile, ":\n");
  409.     fflush(diagfile);
  410. }
  411.  
  412. /* subroutine or function statement */
  413.  
  414. Extsym *newentry(v, substmsg)
  415.  register Namep v;
  416.  int substmsg;
  417. {
  418.     register Extsym *p;
  419.     char buf[128], badname[64];
  420.     static int nbad = 0;
  421.     static char already[] = "external name already used";
  422.  
  423.     p = mkext(v->fvarname, addunder(v->cvarname));
  424.  
  425.     if(p->extinit || ! ONEOF(p->extstg, M(STGUNKNOWN)|M(STGEXT)) )
  426.     {
  427.         sprintf(badname, "%s_bad%d", v->fvarname, ++nbad);
  428.         if (substmsg) {
  429.             sprintf(buf,"%s\n\tsubstituting \"%s\"",
  430.                 already, badname);
  431.             dclerr(buf, v);
  432.             }
  433.         else
  434.             dclerr(already, v);
  435.         p = mkext(v->fvarname, badname);
  436.     }
  437.     v->vstg = STGAUTO;
  438.     v->vprocclass = PTHISPROC;
  439.     v->vclass = CLPROC;
  440.     if (p->extstg == STGEXT)
  441.         prev_proc = 1;
  442.     else
  443.         p->extstg = STGEXT;
  444.     p->extinit = YES;
  445.     v->vardesc.varno = p - extsymtab;
  446.     return(p);
  447. }
  448.  
  449.  
  450. entrypt(class, type, length, entry, args)
  451. int class, type;
  452. ftnint length;
  453. Extsym *entry;
  454. chainp args;
  455. {
  456.     register Namep q;
  457.     register struct Entrypoint *p;
  458.  
  459.     if(class != CLENTRY)
  460.         puthead( procname = entry->cextname, class);
  461.     else
  462.         fprintf(diagfile, "       entry ");
  463.     fprintf(diagfile, "   %s:\n", entry->fextname);
  464.     fflush(diagfile);
  465.     q = mkname(entry->fextname);
  466.     if (type == TYSUBR)
  467.         q->vstg = STGEXT;
  468.  
  469.     type = lengtype(type, length);
  470.     if(class == CLPROC)
  471.     {
  472.         procclass = CLPROC;
  473.         proctype = type;
  474.         procleng = type == TYCHAR ? length : 0;
  475.     }
  476.  
  477.     p = ALLOC(Entrypoint);
  478.  
  479.     p->entnextp = entries;
  480.     entries = p;
  481.  
  482.     p->entryname = entry;
  483.     p->arglist = revchain(args);
  484.     p->enamep = q;
  485.  
  486.     if(class == CLENTRY)
  487.     {
  488.         class = CLPROC;
  489.         if(proctype == TYSUBR)
  490.             type = TYSUBR;
  491.     }
  492.  
  493.     q->vclass = class;
  494.     q->vprocclass = 0;
  495.     settype(q, type, length);
  496.     q->vprocclass = PTHISPROC;
  497.     /* hold all initial entry points till end of declarations */
  498.     if(parstate >= INDATA)
  499.         doentry(p);
  500. }
  501.  
  502. /* generate epilogs */
  503.  
  504. /* epicode -- write out the proper function return mechanism at the end of
  505.    the procedure declaration.  Handles multiple return value types, as
  506.    well as cooercion into the proper value */
  507.  
  508. LOCAL epicode()
  509. {
  510.     extern int lastwasbranch;
  511.  
  512.     if(procclass==CLPROC)
  513.     {
  514.         if(proctype==TYSUBR)
  515.         {
  516.  
  517. /* Return a zero only when the alternate return mechanism has been
  518.    specified in the function header */
  519.  
  520.             if ((substars || Ansi) && lastwasbranch != YES)
  521.                 p1_subr_ret (ICON(0));
  522.         }
  523.         else if (!multitype && lastwasbranch != YES)
  524.             retval(proctype);
  525.     }
  526.     else if (procclass == CLMAIN && Ansi && lastwasbranch != YES)
  527.         p1_subr_ret (ICON(0));
  528.     lastwasbranch = NO;
  529. }
  530.  
  531.  
  532. /* generate code to return value of type  t */
  533.  
  534. LOCAL retval(t)
  535. register int t;
  536. {
  537.     register Addrp p;
  538.  
  539.     switch(t)
  540.     {
  541.     case TYCHAR:
  542.     case TYCOMPLEX:
  543.     case TYDCOMPLEX:
  544.         break;
  545.  
  546.     case TYLOGICAL:
  547.         t = tylogical;
  548.     case TYINT1:
  549.     case TYADDR:
  550.     case TYSHORT:
  551.     case TYLONG:
  552. #ifdef TYQUAD
  553.     case TYQUAD:
  554. #endif
  555.     case TYREAL:
  556.     case TYDREAL:
  557.     case TYLOGICAL1:
  558.     case TYLOGICAL2:
  559.         p = (Addrp) cpexpr((expptr)retslot);
  560.         p->vtype = t;
  561.         p1_subr_ret (mkconv (t, fixtype((expptr)p)));
  562.         break;
  563.  
  564.     default:
  565.         badtype("retval", t);
  566.     }
  567. }
  568.  
  569.  
  570. /* Do parameter adjustments */
  571.  
  572. procode(outfile)
  573. FILE *outfile;
  574. {
  575.     prolog(outfile, allargs);
  576.  
  577.     if (nentry > 1)
  578.         entry_goto(outfile);
  579.     }
  580.  
  581. /* Finish bound computations now that all variables are declared.
  582.  * This used to be in setbound(), but under -u the following incurred
  583.  * an erroneous error message:
  584.  *    subroutine foo(x,n)
  585.  *    real x(n)
  586.  *    integer n
  587.  */
  588.  
  589.  static void
  590. dim_finish(v)
  591.  Namep v;
  592. {
  593.     register struct Dimblock *p;
  594.     register expptr q;
  595.     register int i, nd;
  596.     extern expptr make_int_expr();
  597.  
  598.     p = v->vdim;
  599.     v->vdimfinish = 0;
  600.     nd = p->ndim;
  601.     doin_setbound = 1;
  602.     for(i = 0; i < nd; i++)
  603.         if (q = p->dims[i].dimexpr) {
  604.             q = p->dims[i].dimexpr = make_int_expr(putx(fixtype(q)));
  605.             if (!ONEOF(q->headblock.vtype, MSKINT|MSKREAL))
  606.                 errstr("bad dimension type for %.70s",
  607.                     v->fvarname);
  608.             }
  609.     if (q = p->basexpr)
  610.         p->basexpr = make_int_expr(putx(fixtype(q)));
  611.     doin_setbound = 0;
  612.     }
  613.  
  614.  static void
  615. duparg(q)
  616.  Namep q;
  617. { errstr("duplicate argument %.80s", q->fvarname); }
  618.  
  619. /*
  620.    manipulate argument lists (allocate argument slot positions)
  621.  * keep track of return types and labels
  622.  */
  623.  
  624. LOCAL doentry(ep)
  625. struct Entrypoint *ep;
  626. {
  627.     register int type;
  628.     register Namep np;
  629.     chainp p, p1;
  630.     register Namep q;
  631.     Addrp mkarg(), rs;
  632.     int it, k;
  633.     extern char dflttype[26];
  634.     Extsym *entryname = ep->entryname;
  635.  
  636.     if (++nentry > 1)
  637.         p1_label((long)(extsymtab - entryname - 1));
  638.  
  639. /* The main program isn't allowed to have parameters, so any given
  640.    parameters are ignored */
  641.  
  642.     if(procclass == CLMAIN || procclass == CLBLOCK)
  643.         return;
  644.  
  645. /* So now we're working with something other than CLMAIN or CLBLOCK.
  646.    Determine the type of its return value. */
  647.  
  648.     impldcl( np = mkname(entryname->fextname) );
  649.     type = np->vtype;
  650.     proc_argchanges = prev_proc && type != entryname->extype;
  651.     entryname->extseen = 1;
  652.     if(proctype == TYUNKNOWN)
  653.         if( (proctype = type) == TYCHAR)
  654.             procleng = np->vleng ? np->vleng->constblock.Const.ci
  655.                          : (ftnint) (-1);
  656.  
  657.     if(proctype == TYCHAR)
  658.     {
  659.         if(type != TYCHAR)
  660.             err("noncharacter entry of character function");
  661.  
  662. /* Functions returning type   char   can only have multiple entries if all
  663.    entries return the same length */
  664.  
  665.         else if( (np->vleng ? np->vleng->constblock.Const.ci :
  666.             (ftnint) (-1)) != procleng)
  667.             err("mismatched character entry lengths");
  668.     }
  669.     else if(type == TYCHAR)
  670.         err("character entry of noncharacter function");
  671.     else if(type != proctype)
  672.         multitype = YES;
  673.     if(rtvlabel[type] == 0)
  674.         rtvlabel[type] = newlabel();
  675.     ep->typelabel = rtvlabel[type];
  676.  
  677.     if(type == TYCHAR)
  678.     {
  679.         if(chslot < 0)
  680.         {
  681.             chslot = nextarg(TYADDR);
  682.             chlgslot = nextarg(TYLENG);
  683.         }
  684.         np->vstg = STGARG;
  685.  
  686. /* Put a new argument in the function, one which will hold the result of
  687.    a character function.  This will have to be named sometime, probably in
  688.    mkarg(). */
  689.  
  690.         if(procleng < 0) {
  691.             np->vleng = (expptr) mkarg(TYLENG, chlgslot);
  692.             np->vleng->addrblock.uname_tag = UNAM_IDENT;
  693.             strcpy (np -> vleng -> addrblock.user.ident,
  694.                 new_func_length());
  695.             }
  696.         if (!xretslot[TYCHAR]) {
  697.             xretslot[TYCHAR] = rs =
  698.                 autovar(0, type, ISCONST(np->vleng)
  699.                     ? np->vleng : ICON(0), "");
  700.             strcpy(rs->user.ident, "ret_val");
  701.             }
  702.     }
  703.  
  704. /* Handle a   complex   return type -- declare a new parameter (pointer to
  705.    a complex value) */
  706.  
  707.     else if( ISCOMPLEX(type) ) {
  708.         if (!xretslot[type])
  709.             xretslot[type] =
  710.                 autovar(0, type, EXNULL, " ret_val");
  711.                 /* the blank is for use in out_addr */
  712.         np->vstg = STGARG;
  713.         if(cxslot < 0)
  714.             cxslot = nextarg(TYADDR);
  715.         }
  716.     else if (type != TYSUBR) {
  717.         if (type == TYUNKNOWN) {
  718.             dclerr("untyped function", np);
  719.             proctype = type = np->vtype =
  720.                 dflttype[letter(np->fvarname[0])];
  721.             }
  722.         if (!xretslot[type])
  723.             xretslot[type] = retslot =
  724.                 autovar(1, type, EXNULL, " ret_val");
  725.                 /* the blank is for use in out_addr */
  726.         np->vstg = STGAUTO;
  727.         }
  728.  
  729.     for(p = ep->arglist ; p ; p = p->nextp)
  730.         if(! (( q = (Namep) (p->datap) )->vknownarg) ) {
  731.             q->vknownarg = 1;
  732.             q->vardesc.varno = nextarg(TYADDR);
  733.             allargs = mkchain((char *)q, allargs);
  734.             q->argno = nallargs++;
  735.             }
  736.         else if (nentry == 1)
  737.             duparg(q);
  738.         else for(p1 = ep->arglist ; p1 != p; p1 = p1->nextp)
  739.             if ((Namep)p1->datap == q)
  740.                 duparg(q);
  741.  
  742.     k = 0;
  743.     for(p = ep->arglist ; p ; p = p->nextp) {
  744.         if(! (( q = (Namep) (p->datap) )->vdcldone) )
  745.             {
  746.             impldcl(q);
  747.             q->vdcldone = YES;
  748.             if(q->vtype == TYCHAR)
  749.                 {
  750.  
  751. /* If we don't know the length of a char*(*) (i.e. a string), we must add
  752.    in this additional length argument. */
  753.  
  754.                 ++nallchargs;
  755.                 if (q->vclass == CLPROC)
  756.                     nallchargs--;
  757.                 else if (q->vleng == NULL) {
  758.                     /* character*(*) */
  759.                     q->vleng = (expptr)
  760.                         mkarg(TYLENG, nextarg(TYLENG) );
  761.                     unamstring((Addrp)q->vleng,
  762.                         new_arg_length(q));
  763.                     }
  764.                 }
  765.             }
  766.         if (q->vdimfinish)
  767.             dim_finish(q);
  768.         if (q->vtype == TYCHAR && q->vclass != CLPROC)
  769.             k++;
  770.         }
  771.  
  772.     if (entryname->extype != type)
  773.         changedtype(np);
  774.  
  775.     /* save information for checking consistency of arg lists */
  776.  
  777.     it = infertypes;
  778.     if (entryname->exproto)
  779.         infertypes = 1;
  780.     save_argtypes(ep->arglist, &entryname->arginfo, &np->arginfo,
  781.             0, np->fvarname, STGEXT, k, np->vtype, 2);
  782.     infertypes = it;
  783. }
  784.  
  785.  
  786.  
  787. LOCAL nextarg(type)
  788. int type;
  789. {
  790.     int k;
  791.     k = lastargslot;
  792.     lastargslot += typesize[type];
  793.     return(k);
  794. }
  795.  
  796.  LOCAL
  797. dim_check(q)
  798.  Namep q;
  799. {
  800.     register struct Dimblock *vdim = q->vdim;
  801.  
  802.     if(!vdim->nelt || !ISICON(vdim->nelt))
  803.         dclerr("adjustable dimension on non-argument", q);
  804.     else if (vdim->nelt->constblock.Const.ci <= 0)
  805.         dclerr("nonpositive dimension", q);
  806.     }
  807.  
  808. LOCAL dobss()
  809. {
  810.     register struct Hashentry *p;
  811.     register Namep q;
  812.     int qstg, qclass, qtype;
  813.     Extsym *e;
  814.  
  815.     for(p = hashtab ; p<lasthash ; ++p)
  816.         if(q = p->varp)
  817.         {
  818.             qstg = q->vstg;
  819.             qtype = q->vtype;
  820.             qclass = q->vclass;
  821.  
  822.             if( (qclass==CLUNKNOWN && qstg!=STGARG) ||
  823.                 (qclass==CLVAR && qstg==STGUNKNOWN) ) {
  824.                 if (!(q->vis_assigned | q->vimpldovar))
  825.                     warn1("local variable %s never used",
  826.                         q->fvarname);
  827.                 }
  828.             else if(qclass==CLVAR && qstg==STGBSS)
  829.             { ; }
  830.  
  831. /* Give external procedures the proper storage class */
  832.  
  833.             else if(qclass==CLPROC && q->vprocclass==PEXTERNAL
  834.                     && qstg!=STGARG) {
  835.                 e = mkext(q->fvarname,addunder(q->cvarname));
  836.                 e->extstg = STGEXT;
  837.                 q->vardesc.varno = e - extsymtab;
  838.                 if (e->extype != qtype)
  839.                     changedtype(q);
  840.                 }
  841.             if(qclass==CLVAR) {
  842.                 if (qstg != STGARG && q->vdim)
  843.                 dim_check(q);
  844.             } /* if qclass == CLVAR */
  845.         }
  846.  
  847. }
  848.  
  849.  
  850.  
  851. donmlist()
  852. {
  853.     register struct Hashentry *p;
  854.     register Namep q;
  855.  
  856.     for(p=hashtab; p<lasthash; ++p)
  857.         if( (q = p->varp) && q->vclass==CLNAMELIST)
  858.             namelist(q);
  859. }
  860.  
  861.  
  862. /* iarrlen -- Returns the size of the array in bytes, or -1 */
  863.  
  864. ftnint iarrlen(q)
  865. register Namep q;
  866. {
  867.     ftnint leng;
  868.  
  869.     leng = typesize[q->vtype];
  870.     if(leng <= 0)
  871.         return(-1);
  872.     if(q->vdim)
  873.         if( ISICON(q->vdim->nelt) )
  874.             leng *= q->vdim->nelt->constblock.Const.ci;
  875.         else    return(-1);
  876.     if(q->vleng)
  877.         if( ISICON(q->vleng) )
  878.             leng *= q->vleng->constblock.Const.ci;
  879.         else return(-1);
  880.     return(leng);
  881. }
  882.  
  883. namelist(np)
  884. Namep np;
  885. {
  886.     register chainp q;
  887.     register Namep v;
  888.     int y;
  889.  
  890.     if (!np->visused)
  891.         return;
  892.     y = 0;
  893.  
  894.     for(q = np->varxptr.namelist ; q ; q = q->nextp)
  895.     {
  896.         vardcl( v = (Namep) (q->datap) );
  897.         if( !ONEOF(v->vstg, MSKSTATIC) )
  898.             dclerr("may not appear in namelist", v);
  899.         else {
  900.             v->vnamelist = 1;
  901.             v->visused = 1;
  902.             v->vsave = 1;
  903.             y = 1;
  904.             }
  905.     np->visused = y;
  906.     }
  907. }
  908.  
  909. /* docommon -- called at the end of procedure declarations, before
  910.    equivalences and the procedure body */
  911.  
  912. LOCAL docommon()
  913. {
  914.     register Extsym *extptr;
  915.     register chainp q, q1;
  916.     struct Dimblock *t;
  917.     expptr neltp;
  918.     register Namep comvar;
  919.     ftnint size;
  920.     int i, k, pref, type;
  921.     extern int type_pref[];
  922.  
  923.     for(extptr = extsymtab ; extptr<nextext ; ++extptr)
  924.     if (extptr->extstg == STGCOMMON && (q = extptr->extp)) {
  925.  
  926. /* If a common declaration also had a list of variables ... */
  927.  
  928.         q = extptr->extp = revchain(q);
  929.         pref = 1;
  930.         for(k = TYCHAR; q ; q = q->nextp)
  931.         {
  932.         comvar = (Namep) (q->datap);
  933.  
  934.         if(comvar->vdcldone == NO)
  935.             vardcl(comvar);
  936.         type = comvar->vtype;
  937.         if (pref < type_pref[type])
  938.             pref = type_pref[k = type];
  939.         if(extptr->extleng % typealign[type] != 0) {
  940.             dclerr("common alignment", comvar);
  941.             --nerr; /* don't give bad return code for this */
  942. #if 0
  943.             extptr->extleng = roundup(extptr->extleng, typealign[type]);
  944. #endif
  945.         } /* if extptr -> extleng % */
  946.  
  947. /* Set the offset into the common block */
  948.  
  949.         comvar->voffset = extptr->extleng;
  950.         comvar->vardesc.varno = extptr - extsymtab;
  951.         if(type == TYCHAR)
  952.             size = comvar->vleng->constblock.Const.ci;
  953.         else
  954.             size = typesize[type];
  955.         if(t = comvar->vdim)
  956.             if( (neltp = t->nelt) && ISCONST(neltp) )
  957.             size *= neltp->constblock.Const.ci;
  958.             else
  959.             dclerr("adjustable array in common", comvar);
  960.  
  961. /* Adjust the length of the common block so far */
  962.  
  963.         extptr->extleng += size;
  964.         } /* for */
  965.  
  966.         extptr->extype = k;
  967.  
  968. /* Determine curno and, if new, save this identifier chain */
  969.  
  970.         q1 = extptr->extp;
  971.         for (q = extptr->allextp, i = 0; q; i++, q = q->nextp)
  972.         if (struct_eq((chainp)q->datap, q1))
  973.             break;
  974.         if (q)
  975.         extptr->curno = extptr->maxno - i;
  976.         else {
  977.         extptr->curno = ++extptr->maxno;
  978.         extptr->allextp = mkchain((char *)extptr->extp,
  979.                         extptr->allextp);
  980.         }
  981.     } /* if extptr -> extstg == STGCOMMON */
  982.  
  983. /* Now the hash table entries have STGCOMMON, vdcldone, voffset, and
  984.    varno.  And the common block itself has its full size in extleng. */
  985.  
  986. } /* docommon */
  987.  
  988.  
  989. /* copy_data -- copy the Namep entries so they are available even after
  990.    the hash table is empty */
  991.  
  992. copy_data (list)
  993. chainp list;
  994. {
  995.     for (; list; list = list -> nextp) {
  996.     Namep namep = ALLOC (Nameblock);
  997.     int size, nd, i;
  998.     struct Dimblock *dp;
  999.  
  1000.     cpn(sizeof(struct Nameblock), list->datap, (char *)namep);
  1001.     namep->fvarname = strcpy(gmem(strlen(namep->fvarname)+1,0),
  1002.         namep->fvarname);
  1003.     namep->cvarname = strcmp(namep->fvarname, namep->cvarname)
  1004.         ? strcpy(gmem(strlen(namep->cvarname)+1,0), namep->cvarname)
  1005.         : namep->fvarname;
  1006.     if (namep -> vleng)
  1007.         namep -> vleng = (expptr) cpexpr (namep -> vleng);
  1008.     if (namep -> vdim) {
  1009.         nd = namep -> vdim -> ndim;
  1010.         size = sizeof(int) + (3 + 2 * nd) * sizeof (expptr);
  1011.         dp = (struct Dimblock *) ckalloc (size);
  1012.         cpn(size, (char *)namep->vdim, (char *)dp);
  1013.         namep -> vdim = dp;
  1014.         dp->nelt = (expptr)cpexpr(dp->nelt);
  1015.         for (i = 0; i < nd; i++) {
  1016.         dp -> dims[i].dimsize = (expptr) cpexpr (dp -> dims[i].dimsize);
  1017.         } /* for */
  1018.     } /* if */
  1019.     list -> datap = (char *) namep;
  1020.     } /* for */
  1021. } /* copy_data */
  1022.  
  1023.  
  1024.  
  1025. LOCAL docomleng()
  1026. {
  1027.     register Extsym *p;
  1028.  
  1029.     for(p = extsymtab ; p < nextext ; ++p)
  1030.         if(p->extstg == STGCOMMON)
  1031.         {
  1032.             if(p->maxleng!=0 && p->extleng!=0 && p->maxleng!=p->extleng
  1033.                 && strcmp(Blank, p->cextname) )
  1034.                 warn1("incompatible lengths for common block %.60s",
  1035.                     p->fextname);
  1036.             if(p->maxleng < p->extleng)
  1037.                 p->maxleng = p->extleng;
  1038.             p->extleng = 0;
  1039.         }
  1040. }
  1041.  
  1042.  
  1043. /* ROUTINES DEALING WITH AUTOMATIC AND TEMPORARY STORAGE */
  1044.  
  1045. frtemp(p)
  1046. Addrp p;
  1047. {
  1048.     /* put block on chain of temps to be reclaimed */
  1049.     holdtemps = mkchain((char *)p, holdtemps);
  1050. }
  1051.  
  1052.  void
  1053. freetemps()
  1054. {
  1055.     register chainp p, p1;
  1056.     register Addrp q;
  1057.     register int t;
  1058.  
  1059.     p1 = holdtemps;
  1060.     while(p = p1) {
  1061.         q = (Addrp)p->datap;
  1062.         t = q->vtype;
  1063.         if (t == TYCHAR && q->varleng != 0) {
  1064.             /* restore clobbered character string lengths */
  1065.             frexpr(q->vleng);
  1066.             q->vleng = ICON(q->varleng);
  1067.             }
  1068.         p1 = p->nextp;
  1069.         p->nextp = templist[t];
  1070.         templist[t] = p;
  1071.         }
  1072.     holdtemps = 0;
  1073.     }
  1074.  
  1075. /* allocate an automatic variable slot for each of   nelt   variables */
  1076.  
  1077. Addrp autovar(nelt0, t, lengp, name)
  1078. register int nelt0, t;
  1079. expptr lengp;
  1080. char *name;
  1081. {
  1082.     ftnint leng;
  1083.     register Addrp q;
  1084.     char *temp_name ();
  1085.     register int nelt = nelt0 > 0 ? nelt0 : 1;
  1086.     extern char *av_pfix[];
  1087.  
  1088.     if(t == TYCHAR)
  1089.         if( ISICON(lengp) )
  1090.             leng = lengp->constblock.Const.ci;
  1091.         else    {
  1092.             Fatal("automatic variable of nonconstant length");
  1093.         }
  1094.     else
  1095.         leng = typesize[t];
  1096.  
  1097.     q = ALLOC(Addrblock);
  1098.     q->tag = TADDR;
  1099.     q->vtype = t;
  1100.     if(t == TYCHAR)
  1101.     {
  1102.         q->vleng = ICON(leng);
  1103.         q->varleng = leng;
  1104.     }
  1105.     q->vstg = STGAUTO;
  1106.     q->ntempelt = nelt;
  1107.     q->isarray = (nelt > 1);
  1108.     q->memoffset = ICON(0);
  1109.  
  1110.     /* kludge for nls so we can have ret_val rather than ret_val_4 */
  1111.     if (*name == ' ')
  1112.         unamstring(q, name);
  1113.     else {
  1114.         q->uname_tag = UNAM_IDENT;
  1115.         temp_name(av_pfix[t], ++autonum[t], q->user.ident);
  1116.         }
  1117.     if (nelt0 > 0)
  1118.         declare_new_addr (q);
  1119.     return(q);
  1120. }
  1121.  
  1122.  
  1123. /* Returns a temporary of the appropriate type.  Will reuse existing
  1124.    temporaries when possible */
  1125.  
  1126. Addrp mktmpn(nelt, type, lengp)
  1127. int nelt;
  1128. register int type;
  1129. expptr lengp;
  1130. {
  1131.     ftnint leng;
  1132.     chainp p, oldp;
  1133.     register Addrp q;
  1134.  
  1135.     if(type==TYUNKNOWN || type==TYERROR)
  1136.         badtype("mktmpn", type);
  1137.  
  1138.     if(type==TYCHAR)
  1139.         if(lengp && ISICON(lengp) )
  1140.             leng = lengp->constblock.Const.ci;
  1141.         else    {
  1142.             err("adjustable length");
  1143.             return( (Addrp) errnode() );
  1144.         }
  1145.     else if (type > TYCHAR || type < TYADDR) {
  1146.         erri("mktmpn: unexpected type %d", type);
  1147.         exit(1);
  1148.         }
  1149. /*
  1150.  * if a temporary of appropriate shape is on the templist,
  1151.  * remove it from the list and return it
  1152.  */
  1153.     for(oldp=CHNULL, p=templist[type];  p  ;  oldp=p, p=p->nextp)
  1154.     {
  1155.         q = (Addrp) (p->datap);
  1156.         if(q->ntempelt==nelt &&
  1157.             (type!=TYCHAR || q->vleng->constblock.Const.ci==leng) )
  1158.         {
  1159.             if(oldp)
  1160.                 oldp->nextp = p->nextp;
  1161.             else
  1162.                 templist[type] = p->nextp;
  1163.             free( (charptr) p);
  1164.             return(q);
  1165.         }
  1166.     }
  1167.     q = autovar(nelt, type, lengp, "");
  1168.     return(q);
  1169. }
  1170.  
  1171.  
  1172.  
  1173.  
  1174. /* mktmp -- create new local variable; call it something like   name
  1175.    lengp   is taken directly, not copied */
  1176.  
  1177. Addrp mktmp(type, lengp)
  1178. int type;
  1179. expptr lengp;
  1180. {
  1181.     Addrp rv;
  1182.     /* arrange for temporaries to be recycled */
  1183.     /* at the end of this statement... */
  1184.     rv = mktmpn(1,type,lengp);
  1185.     frtemp((Addrp)cpexpr((expptr)rv));
  1186.     return rv;
  1187. }
  1188.  
  1189. /* mktmp0 omits frtemp() */
  1190. Addrp mktmp0(type, lengp)
  1191. int type;
  1192. expptr lengp;
  1193. {
  1194.     Addrp rv;
  1195.     /* arrange for temporaries to be recycled */
  1196.     /* when this Addrp is freed */
  1197.     rv = mktmpn(1,type,lengp);
  1198.     rv->istemp = YES;
  1199.     return rv;
  1200. }
  1201.  
  1202. /* VARIOUS ROUTINES FOR PROCESSING DECLARATIONS */
  1203.  
  1204. /* comblock -- Declare a new common block.  Input parameters name the block;
  1205.    s   will be NULL if the block is unnamed */
  1206.  
  1207. Extsym *comblock(s)
  1208.  register char *s;
  1209. {
  1210.     Extsym *p;
  1211.     register char *t;
  1212.     register int c, i;
  1213.     char cbuf[256], *s0;
  1214.  
  1215. /* Give the unnamed common block a unique name */
  1216.  
  1217.     if(*s == 0)
  1218.         p = mkext(Blank,Blank);
  1219.     else {
  1220.         s0 = s;
  1221.         t = cbuf;
  1222.         for(i = 0; c = *t = *s++; t++)
  1223.             if (c == '_')
  1224.                 i = 1;
  1225.         if (i)
  1226.             *t++ = '_';
  1227.         t[0] = '_';
  1228.         t[1] = 0;
  1229.         p = mkext(s0,cbuf);
  1230.         }
  1231.     if(p->extstg == STGUNKNOWN)
  1232.         p->extstg = STGCOMMON;
  1233.     else if(p->extstg != STGCOMMON)
  1234.     {
  1235.         errstr("%.68s cannot be a common block name", s);
  1236.         return(0);
  1237.     }
  1238.  
  1239.     return( p );
  1240. }
  1241.  
  1242.  
  1243. /* incomm -- add a new variable to a common declaration */
  1244.  
  1245. incomm(c, v)
  1246. Extsym *c;
  1247. Namep v;
  1248. {
  1249.     if (!c)
  1250.         return;
  1251.     if(v->vstg != STGUNKNOWN && !v->vimplstg)
  1252.         dclerr(v->vstg == STGARG
  1253.             ? "dummy arguments cannot be in common"
  1254.             : "incompatible common declaration", v);
  1255.     else
  1256.     {
  1257.         v->vstg = STGCOMMON;
  1258.         c->extp = mkchain((char *)v, c->extp);
  1259.     }
  1260. }
  1261.  
  1262.  
  1263.  
  1264.  
  1265. /* settype -- set the type or storage class of a Namep object.  If
  1266.    v -> vstg == STGUNKNOWN && type < 0,   attempt to reset vstg to be
  1267.    -type.  This function will not change any earlier definitions in   v,
  1268.    in will only attempt to fill out more information give the other params */
  1269.  
  1270. settype(v, type, length)
  1271. register Namep  v;
  1272. register int type;
  1273. register ftnint length;
  1274. {
  1275.     int type1;
  1276.  
  1277.     if(type == TYUNKNOWN)
  1278.         return;
  1279.  
  1280.     if(type==TYSUBR && v->vtype!=TYUNKNOWN && v->vstg==STGARG)
  1281.     {
  1282.         v->vtype = TYSUBR;
  1283.         frexpr(v->vleng);
  1284.         v->vleng = 0;
  1285.         v->vimpltype = 0;
  1286.     }
  1287.     else if(type < 0)    /* storage class set */
  1288.     {
  1289.         if(v->vstg == STGUNKNOWN)
  1290.             v->vstg = - type;
  1291.         else if(v->vstg != -type)
  1292.             dclerr("incompatible storage declarations", v);
  1293.     }
  1294.     else if(v->vtype == TYUNKNOWN || v->vimpltype && v->vtype != type)
  1295.     {
  1296.         if( (v->vtype = lengtype(type, length))==TYCHAR )
  1297.             if (length>=0)
  1298.                 v->vleng = ICON(length);
  1299.             else if (parstate >= INDATA)
  1300.                 v->vleng = ICON(1);    /* avoid a memory fault */
  1301.         v->vimpltype = 0;
  1302.  
  1303.         if (v->vclass == CLPROC) {
  1304.             if (v->vstg == STGEXT
  1305.              && (type1 = extsymtab[v->vardesc.varno].extype)
  1306.              &&  type1 != v->vtype)
  1307.                 changedtype(v);
  1308.             else if (v->vprocclass == PTHISPROC
  1309.                     && (parstate >= INDATA
  1310.                         || procclass == CLMAIN)
  1311.                     && !xretslot[type]) {
  1312.                 xretslot[type] = autovar(ONEOF(type,
  1313.                     MSKCOMPLEX|MSKCHAR) ? 0 : 1, type,
  1314.                     v->vleng, " ret_val");
  1315.                 if (procclass == CLMAIN)
  1316.                     errstr(
  1317.                 "illegal use of %.60s (main program name)",
  1318.                     v->fvarname);
  1319.                 /* not completely right, but enough to */
  1320.                 /* avoid memory faults; we won't */
  1321.                 /* emit any C as we have illegal Fortran */
  1322.                 }
  1323.             }
  1324.     }
  1325.     else if(v->vtype!=type) {
  1326.  incompat:
  1327.         dclerr("incompatible type declarations", v);
  1328.         }
  1329.     else if (type==TYCHAR)
  1330.         if (v->vleng && v->vleng->constblock.Const.ci != length)
  1331.             goto incompat;
  1332.         else if (parstate >= INDATA)
  1333.             v->vleng = ICON(1);    /* avoid a memory fault */
  1334. }
  1335.  
  1336.  
  1337.  
  1338.  
  1339.  
  1340. /* lengtype -- returns the proper compiler type, given input of Fortran
  1341.    type and length specifier */
  1342.  
  1343. lengtype(type, len)
  1344. register int type;
  1345. ftnint len;
  1346. {
  1347.     register int length = (int)len;
  1348.     switch(type)
  1349.     {
  1350.     case TYREAL:
  1351.         if(length == typesize[TYDREAL])
  1352.             return(TYDREAL);
  1353.         if(length == typesize[TYREAL])
  1354.             goto ret;
  1355.         break;
  1356.  
  1357.     case TYCOMPLEX:
  1358.         if(length == typesize[TYDCOMPLEX])
  1359.             return(TYDCOMPLEX);
  1360.         if(length == typesize[TYCOMPLEX])
  1361.             goto ret;
  1362.         break;
  1363.  
  1364.     case TYINT1:
  1365.     case TYSHORT:
  1366.     case TYDREAL:
  1367.     case TYDCOMPLEX:
  1368.     case TYCHAR:
  1369.     case TYLOGICAL1:
  1370.     case TYLOGICAL2:
  1371.     case TYUNKNOWN:
  1372.     case TYSUBR:
  1373.     case TYERROR:
  1374. #ifdef TYQUAD
  1375.     case TYQUAD:
  1376. #endif
  1377.         goto ret;
  1378.  
  1379.     case TYLOGICAL:
  1380.         switch(length) {
  1381.             case 0: return tylog;
  1382.             case 1:    return TYLOGICAL1;
  1383.             case 2: return TYLOGICAL2;
  1384.             case 4: goto ret;
  1385.             }
  1386. #if 0 /*!!??!!*/
  1387.         if(length == typesize[TYLOGICAL])
  1388.             goto ret;
  1389. #endif
  1390.         break;
  1391.  
  1392.     case TYLONG:
  1393.         if(length == 0)
  1394.             return(tyint);
  1395.         if (length == 1)
  1396.             return TYINT1;
  1397.         if(length == typesize[TYSHORT])
  1398.             return(TYSHORT);
  1399. #ifdef TYQUAD
  1400.         if(length == typesize[TYQUAD] && use_tyquad)
  1401.             return(TYQUAD);
  1402. #endif
  1403.         if(length == typesize[TYLONG])
  1404.             goto ret;
  1405.         break;
  1406.     default:
  1407.         badtype("lengtype", type);
  1408.     }
  1409.  
  1410.     if(len != 0)
  1411.         err("incompatible type-length combination");
  1412.  
  1413. ret:
  1414.     return(type);
  1415. }
  1416.  
  1417.  
  1418.  
  1419.  
  1420.  
  1421. /* setintr -- Set Intrinsic function */
  1422.  
  1423. setintr(v)
  1424. register Namep  v;
  1425. {
  1426.     int k;
  1427.  
  1428.     if(v->vstg == STGUNKNOWN)
  1429.         v->vstg = STGINTR;
  1430.     else if(v->vstg!=STGINTR)
  1431.         dclerr("incompatible use of intrinsic function", v);
  1432.     if(v->vclass==CLUNKNOWN)
  1433.         v->vclass = CLPROC;
  1434.     if(v->vprocclass == PUNKNOWN)
  1435.         v->vprocclass = PINTRINSIC;
  1436.     else if(v->vprocclass != PINTRINSIC)
  1437.         dclerr("invalid intrinsic declaration", v);
  1438.     if(k = intrfunct(v->fvarname)) {
  1439.         if ((*(struct Intrpacked *)&k).f4)
  1440.             if (noextflag)
  1441.                 goto unknown;
  1442.             else
  1443.                 dcomplex_seen++;
  1444.         v->vardesc.varno = k;
  1445.         }
  1446.     else {
  1447.  unknown:
  1448.         dclerr("unknown intrinsic function", v);
  1449.         }
  1450. }
  1451.  
  1452.  
  1453.  
  1454. /* setext -- Set External declaration -- assume that unknowns will become
  1455.    procedures */
  1456.  
  1457. setext(v)
  1458. register Namep  v;
  1459. {
  1460.     if(v->vclass == CLUNKNOWN)
  1461.         v->vclass = CLPROC;
  1462.     else if(v->vclass != CLPROC)
  1463.         dclerr("invalid external declaration", v);
  1464.  
  1465.     if(v->vprocclass == PUNKNOWN)
  1466.         v->vprocclass = PEXTERNAL;
  1467.     else if(v->vprocclass != PEXTERNAL)
  1468.         dclerr("invalid external declaration", v);
  1469. } /* setext */
  1470.  
  1471.  
  1472.  
  1473.  
  1474. /* create dimensions block for array variable */
  1475.  
  1476. setbound(v, nd, dims)
  1477. register Namep  v;
  1478. int nd;
  1479. struct Dims dims[ ];
  1480. {
  1481.     register expptr q, t;
  1482.     register struct Dimblock *p;
  1483.     int i;
  1484.     extern chainp new_vars;
  1485.     char buf[256];
  1486.  
  1487.     if(v->vclass == CLUNKNOWN)
  1488.         v->vclass = CLVAR;
  1489.     else if(v->vclass != CLVAR)
  1490.     {
  1491.         dclerr("only variables may be arrays", v);
  1492.         return;
  1493.     }
  1494.  
  1495.     v->vdim = p = (struct Dimblock *)
  1496.         ckalloc( sizeof(int) + (3+2*nd)*sizeof(expptr) );
  1497.     p->ndim = nd--;
  1498.     p->nelt = ICON(1);
  1499.     doin_setbound = 1;
  1500.  
  1501.     for(i = 0; i <= nd; ++i)
  1502.     {
  1503.         if( (q = dims[i].ub) == NULL)
  1504.         {
  1505.             if(i == nd)
  1506.             {
  1507.                 frexpr(p->nelt);
  1508.                 p->nelt = NULL;
  1509.             }
  1510.             else
  1511.                 err("only last bound may be asterisk");
  1512.             p->dims[i].dimsize = ICON(1);
  1513.             ;
  1514.             p->dims[i].dimexpr = NULL;
  1515.         }
  1516.         else
  1517.         {
  1518.  
  1519.             if(dims[i].lb)
  1520.             {
  1521.                 q = mkexpr(OPMINUS, q, cpexpr(dims[i].lb));
  1522.                 q = mkexpr(OPPLUS, q, ICON(1) );
  1523.             }
  1524.             if( ISCONST(q) )
  1525.             {
  1526.                 p->dims[i].dimsize = q;
  1527.                 p->dims[i].dimexpr = (expptr) PNULL;
  1528.             }
  1529.             else {
  1530.                 sprintf(buf, " %s_dim%d", v->fvarname, i+1);
  1531.                 p->dims[i].dimsize = (expptr)
  1532.                     autovar(1, tyint, EXNULL, buf);
  1533.                 p->dims[i].dimexpr = q;
  1534.                 if (i == nd)
  1535.                     v->vlastdim = new_vars;
  1536.                 v->vdimfinish = 1;
  1537.             }
  1538.             if(p->nelt)
  1539.                 p->nelt = mkexpr(OPSTAR, p->nelt,
  1540.                     cpexpr(p->dims[i].dimsize) );
  1541.         }
  1542.     }
  1543.  
  1544.     q = dims[nd].lb;
  1545.     if(q == NULL)
  1546.         q = ICON(1);
  1547.  
  1548.     for(i = nd-1 ; i>=0 ; --i)
  1549.     {
  1550.         t = dims[i].lb;
  1551.         if(t == NULL)
  1552.             t = ICON(1);
  1553.         if(p->dims[i].dimsize)
  1554.             q = mkexpr(OPPLUS, t, mkexpr(OPSTAR, cpexpr(p->dims[i].dimsize), q) );
  1555.     }
  1556.  
  1557.     if( ISCONST(q) )
  1558.     {
  1559.         p->baseoffset = q;
  1560.         p->basexpr = NULL;
  1561.     }
  1562.     else
  1563.     {
  1564.         sprintf(buf, " %s_offset", v->fvarname);
  1565.         p->baseoffset = (expptr) autovar(1, tyint, EXNULL, buf);
  1566.         p->basexpr = q;
  1567.         v->vdimfinish = 1;
  1568.     }
  1569.     doin_setbound = 0;
  1570. }
  1571.  
  1572.  
  1573.  
  1574. wr_abbrevs (outfile, function_head, vars)
  1575. FILE *outfile;
  1576. int function_head;
  1577. chainp vars;
  1578. {
  1579.     for (; vars; vars = vars -> nextp) {
  1580.     Namep name = (Namep) vars -> datap;
  1581.     if (!name->visused)
  1582.         continue;
  1583.  
  1584.     if (function_head)
  1585.         nice_printf (outfile, "#define ");
  1586.     else
  1587.         nice_printf (outfile, "#undef ");
  1588.     out_name (outfile, name);
  1589.  
  1590.     if (function_head) {
  1591.         Extsym *comm = &extsymtab[name -> vardesc.varno];
  1592.  
  1593.         nice_printf (outfile, " (");
  1594.         extern_out (outfile, comm);
  1595.         nice_printf (outfile, "%d.", comm->curno);
  1596.         nice_printf (outfile, "%s)", name->cvarname);
  1597.     } /* if function_head */
  1598.     nice_printf (outfile, "\n");
  1599.     } /* for */
  1600. } /* wr_abbrevs */
  1601.