home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / f2csrc.zip / f2csrc / src / equiv.c < prev    next >
C/C++ Source or Header  |  1994-02-25  |  10KB  |  407 lines

  1. /****************************************************************
  2. Copyright 1990, 1993, 1994 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.  
  26. static void eqvcommon Argdcl((struct Equivblock*, int, long int));
  27. static void eqveqv Argdcl((int, int, long int));
  28. static int nsubs Argdcl((struct Listblock*));
  29.  
  30. /* ROUTINES RELATED TO EQUIVALENCE CLASS PROCESSING */
  31.  
  32. /* called at end of declarations section to process chains
  33.    created by EQUIVALENCE statements
  34.  */
  35.  void
  36. doequiv(Void)
  37. {
  38.     register int i;
  39.     int inequiv;            /* True if one namep occurs in
  40.                        several EQUIV declarations */
  41.     int comno;        /* Index into Extsym table of the last
  42.                    COMMON block seen (implicitly assuming
  43.                    that only one will be given) */
  44.     int ovarno;
  45.     ftnint comoffset;    /* Index into the COMMON block */
  46.     ftnint offset;        /* Offset from array base */
  47.     ftnint leng;
  48.     register struct Equivblock *equivdecl;
  49.     register struct Eqvchain *q;
  50.     struct Primblock *primp;
  51.     register Namep np;
  52.     int k, k1, ns, pref, t;
  53.     chainp cp;
  54.     extern int type_pref[];
  55.     char *s;
  56.  
  57.     for(i = 0 ; i < nequiv ; ++i)
  58.     {
  59.  
  60. /* Handle each equivalence declaration */
  61.  
  62.         equivdecl = &eqvclass[i];
  63.         equivdecl->eqvbottom = equivdecl->eqvtop = 0;
  64.         comno = -1;
  65.  
  66.  
  67.  
  68.         for(q = equivdecl->equivs ; q ; q = q->eqvnextp)
  69.         {
  70.             offset = 0;
  71.             primp = q->eqvitem.eqvlhs;
  72.             vardcl(np = primp->namep);
  73.             if(primp->argsp || primp->fcharp)
  74.             {
  75.                 expptr offp;
  76.  
  77. /* Pad ones onto the end of an array declaration when needed */
  78.  
  79.                 if(np->vdim!=NULL && np->vdim->ndim>1 &&
  80.                     nsubs(primp->argsp)==1 )
  81.                 {
  82.                     if(! ftn66flag)
  83.                         warni
  84.             ("1-dim subscript in EQUIVALENCE, %d-dim declared",
  85.                             np -> vdim -> ndim);
  86.                     cp = NULL;
  87.                     ns = np->vdim->ndim;
  88.                     while(--ns > 0)
  89.                         cp = mkchain((char *)ICON(1), cp);
  90.                     primp->argsp->listp->nextp = cp;
  91.                 }
  92.  
  93.                 offp = suboffset(primp);
  94.                 if(ISICON(offp))
  95.                     offset = offp->constblock.Const.ci;
  96.                 else    {
  97.                     dclerr
  98.             ("nonconstant subscript in equivalence ",
  99.                         np);
  100.                     np = NULL;
  101.                 }
  102.                 frexpr(offp);
  103.             }
  104.  
  105. /* Free up the primblock, since we now have a hash table (Namep) entry */
  106.  
  107.             frexpr((expptr)primp);
  108.  
  109.             if(np && (leng = iarrlen(np))<0)
  110.             {
  111.                 dclerr("adjustable in equivalence", np);
  112.                 np = NULL;
  113.             }
  114.  
  115.             if(np) switch(np->vstg)
  116.             {
  117.             case STGUNKNOWN:
  118.             case STGBSS:
  119.             case STGEQUIV:
  120.                 if (in_vector(np->cvarname, st_fields,
  121.                         n_st_fields) >= 0) {
  122.                     k = strlen(np->cvarname);
  123.                     strcpy(s = mem(k+2,0), np->cvarname);
  124.                     s[k] = '_';
  125.                     s[k+1] = 0;
  126.                     np->cvarname = s;
  127.                     }
  128.                 break;
  129.  
  130.             case STGCOMMON:
  131.  
  132. /* The code assumes that all COMMON references in a given EQUIVALENCE will
  133.    be to the same COMMON block, and will all be consistent */
  134.  
  135.                 comno = np->vardesc.varno;
  136.                 comoffset = np->voffset + offset;
  137.                 break;
  138.  
  139.             default:
  140.                 dclerr("bad storage class in equivalence", np);
  141.                 np = NULL;
  142.                 break;
  143.             }
  144.  
  145.             if(np)
  146.             {
  147.                 q->eqvoffset = offset;
  148.  
  149. /* eqvbottom   gets the largest difference between the array base address
  150.    and the address specified in the EQUIV declaration */
  151.  
  152.                 equivdecl->eqvbottom =
  153.                     lmin(equivdecl->eqvbottom, -offset);
  154.  
  155. /* eqvtop   gets the largest difference between the end of the array and
  156.    the address given in the EQUIVALENCE */
  157.  
  158.                 equivdecl->eqvtop =
  159.                     lmax(equivdecl->eqvtop, leng-offset);
  160.             }
  161.             q->eqvitem.eqvname = np;
  162.         }
  163.  
  164. /* Now all equivalenced variables are in the hash table with the proper
  165.    offset, and   eqvtop and eqvbottom   are set. */
  166.  
  167.         if(comno >= 0)
  168.  
  169. /* Get rid of all STGEQUIVS, they will be mapped onto STGCOMMON variables
  170.    */
  171.  
  172.             eqvcommon(equivdecl, comno, comoffset);
  173.         else for(q = equivdecl->equivs ; q ; q = q->eqvnextp)
  174.         {
  175.             if(np = q->eqvitem.eqvname)
  176.             {
  177.                 inequiv = NO;
  178.                 if(np->vstg==STGEQUIV)
  179.                     if( (ovarno = np->vardesc.varno) == i)
  180.                     {
  181.  
  182. /* Can't EQUIV different elements of the same array */
  183.  
  184.                         if(np->voffset + q->eqvoffset != 0)
  185.                             dclerr
  186.             ("inconsistent equivalence", np);
  187.                     }
  188.                     else    {
  189.                         offset = np->voffset;
  190.                         inequiv = YES;
  191.                     }
  192.  
  193.                 np->vstg = STGEQUIV;
  194.                 np->vardesc.varno = i;
  195.                 np->voffset = - q->eqvoffset;
  196.  
  197.                 if(inequiv)
  198.  
  199. /* Combine 2 equivalence declarations */
  200.  
  201.                     eqveqv(i, ovarno, q->eqvoffset + offset);
  202.             }
  203.         }
  204.     }
  205.  
  206. /* Now each equivalence declaration is distinct (all connections have been
  207.    merged in eqveqv()), and some may be empty. */
  208.  
  209.     for(i = 0 ; i < nequiv ; ++i)
  210.     {
  211.         equivdecl = & eqvclass[i];
  212.         if(equivdecl->eqvbottom!=0 || equivdecl->eqvtop!=0) {
  213.  
  214. /* a live chain */
  215.  
  216.             k = TYCHAR;
  217.             pref = 1;
  218.             for(q = equivdecl->equivs ; q; q = q->eqvnextp)
  219.                 if ((np = q->eqvitem.eqvname)
  220.                         && !np->veqvadjust) {
  221.                 np->veqvadjust = 1;
  222.                 np->voffset -= equivdecl->eqvbottom;
  223.                 t = typealign[k1 = np->vtype];
  224.                 if (pref < type_pref[k1]) {
  225.                     k = k1;
  226.                     pref = type_pref[k1];
  227.                     }
  228.                 if(np->voffset % t != 0) {
  229.                     dclerr("bad alignment forced by equivalence", np);
  230.                     --nerr; /* don't give bad return code for this */
  231.                     }
  232.                 }
  233.             equivdecl->eqvtype = k;
  234.         }
  235.         freqchain(equivdecl);
  236.     }
  237. }
  238.  
  239.  
  240.  
  241.  
  242.  
  243. /* put equivalence chain p at common block comno + comoffset */
  244.  
  245.  LOCAL void
  246. #ifdef KR_headers
  247. eqvcommon(p, comno, comoffset)
  248.     struct Equivblock *p;
  249.     int comno;
  250.     ftnint comoffset;
  251. #else
  252. eqvcommon(struct Equivblock *p, int comno, ftnint comoffset)
  253. #endif
  254. {
  255.     int ovarno;
  256.     ftnint k, offq;
  257.     register Namep np;
  258.     register struct Eqvchain *q;
  259.  
  260.     if(comoffset + p->eqvbottom < 0)
  261.     {
  262.         errstr("attempt to extend common %s backward",
  263.             extsymtab[comno].fextname);
  264.         freqchain(p);
  265.         return;
  266.     }
  267.  
  268.     if( (k = comoffset + p->eqvtop) > extsymtab[comno].extleng)
  269.         extsymtab[comno].extleng = k;
  270.  
  271.  
  272.     for(q = p->equivs ; q ; q = q->eqvnextp)
  273.         if(np = q->eqvitem.eqvname)
  274.         {
  275.             switch(np->vstg)
  276.             {
  277.             case STGUNKNOWN:
  278.             case STGBSS:
  279.                 np->vstg = STGCOMMON;
  280.                 np->vcommequiv = 1;
  281.                 np->vardesc.varno = comno;
  282.  
  283. /* np -> voffset   will point to the base of the array */
  284.  
  285.                 np->voffset = comoffset - q->eqvoffset;
  286.                 break;
  287.  
  288.             case STGEQUIV:
  289.                 ovarno = np->vardesc.varno;
  290.  
  291. /* offq   will point to the current element, even if it's in an array */
  292.  
  293.                 offq = comoffset - q->eqvoffset - np->voffset;
  294.                 np->vstg = STGCOMMON;
  295.                 np->vcommequiv = 1;
  296.                 np->vardesc.varno = comno;
  297.  
  298. /* np -> voffset   will point to the base of the array */
  299.  
  300.                 np->voffset += offq;
  301.                 if(ovarno != (p - eqvclass))
  302.                     eqvcommon(&eqvclass[ovarno], comno, offq);
  303.                 break;
  304.  
  305.             case STGCOMMON:
  306.                 if(comno != np->vardesc.varno ||
  307.                     comoffset != np->voffset+q->eqvoffset)
  308.                     dclerr("inconsistent common usage", np);
  309.                 break;
  310.  
  311.  
  312.             default:
  313.                 badstg("eqvcommon", np->vstg);
  314.             }
  315.         }
  316.  
  317.     freqchain(p);
  318.     p->eqvbottom = p->eqvtop = 0;
  319. }
  320.  
  321.  
  322. /* Move all items on ovarno chain to the front of   nvarno   chain.
  323.  * adjust offsets of ovarno elements and top and bottom of nvarno chain
  324.  */
  325.  
  326.  LOCAL void
  327. #ifdef KR_headers
  328. eqveqv(nvarno, ovarno, delta)
  329.     int nvarno;
  330.     int ovarno;
  331.     ftnint delta;
  332. #else
  333. eqveqv(int nvarno, int ovarno, ftnint delta)
  334. #endif
  335. {
  336.     register struct Equivblock *neweqv, *oldeqv;
  337.     register Namep np;
  338.     struct Eqvchain *q, *q1;
  339.  
  340.     neweqv = eqvclass + nvarno;
  341.     oldeqv = eqvclass + ovarno;
  342.     neweqv->eqvbottom = lmin(neweqv->eqvbottom, oldeqv->eqvbottom - delta);
  343.     neweqv->eqvtop = lmax(neweqv->eqvtop, oldeqv->eqvtop - delta);
  344.     oldeqv->eqvbottom = oldeqv->eqvtop = 0;
  345.  
  346.     for(q = oldeqv->equivs ; q ; q = q1)
  347.     {
  348.         q1 = q->eqvnextp;
  349.         if( (np = q->eqvitem.eqvname) && np->vardesc.varno==ovarno)
  350.         {
  351.             q->eqvnextp = neweqv->equivs;
  352.             neweqv->equivs = q;
  353.             q->eqvoffset += delta;
  354.             np->vardesc.varno = nvarno;
  355.             np->voffset -= delta;
  356.         }
  357.         else    free( (charptr) q);
  358.     }
  359.     oldeqv->equivs = NULL;
  360. }
  361.  
  362.  
  363.  
  364.  void
  365. #ifdef KR_headers
  366. freqchain(p)
  367.     register struct Equivblock *p;
  368. #else
  369. freqchain(register struct Equivblock *p)
  370. #endif
  371. {
  372.     register struct Eqvchain *q, *oq;
  373.  
  374.     for(q = p->equivs ; q ; q = oq)
  375.     {
  376.         oq = q->eqvnextp;
  377.         free( (charptr) q);
  378.     }
  379.     p->equivs = NULL;
  380. }
  381.  
  382.  
  383.  
  384.  
  385.  
  386. /* nsubs -- number of subscripts in this arglist (just the length of the
  387.    list) */
  388.  
  389.  LOCAL int
  390. #ifdef KR_headers
  391. nsubs(p)
  392.     register struct Listblock *p;
  393. #else
  394. nsubs(register struct Listblock *p)
  395. #endif
  396. {
  397.     register int n;
  398.     register chainp q;
  399.  
  400.     n = 0;
  401.     if(p)
  402.         for(q = p->listp ; q ; q = q->nextp)
  403.             ++n;
  404.  
  405.     return(n);
  406. }
  407.