home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / f2csrc.zip / f2csrc / src / expr.c < prev    next >
C/C++ Source or Header  |  1994-07-14  |  68KB  |  3,319 lines

  1. /****************************************************************
  2. Copyright 1990 - 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. #include "output.h"
  26. #include "names.h"
  27.  
  28. typedef struct { double dreal, dimag; } dcomplex;
  29.  
  30. static void consbinop Argdcl((int, int, Constp, Constp, Constp));
  31. static void conspower Argdcl((Constp, Constp, long int));
  32. static void zdiv Argdcl((dcomplex*, dcomplex*, dcomplex*));
  33. static tagptr mkpower Argdcl((tagptr));
  34. static tagptr stfcall Argdcl((Namep, struct Listblock*));
  35.  
  36. extern char dflttype[26];
  37. extern int htype;
  38.  
  39. /* little routines to create constant blocks */
  40.  
  41.  Constp
  42. #ifdef KR_headers
  43. mkconst(t)
  44.     register int t;
  45. #else
  46. mkconst(register int t)
  47. #endif
  48. {
  49.     register Constp p;
  50.  
  51.     p = ALLOC(Constblock);
  52.     p->tag = TCONST;
  53.     p->vtype = t;
  54.     return(p);
  55. }
  56.  
  57.  
  58. /* mklogcon -- Make Logical Constant */
  59.  
  60.  expptr
  61. #ifdef KR_headers
  62. mklogcon(l)
  63.     register int l;
  64. #else
  65. mklogcon(register int l)
  66. #endif
  67. {
  68.     register Constp  p;
  69.  
  70.     p = mkconst(tylog);
  71.     p->Const.ci = l;
  72.     return( (expptr) p );
  73. }
  74.  
  75.  
  76.  
  77. /* mkintcon -- Make Integer Constant */
  78.  
  79.  expptr
  80. #ifdef KR_headers
  81. mkintcon(l)
  82.     ftnint l;
  83. #else
  84. mkintcon(ftnint l)
  85. #endif
  86. {
  87.     register Constp p;
  88.  
  89.     p = mkconst(tyint);
  90.     p->Const.ci = l;
  91.     return( (expptr) p );
  92. }
  93.  
  94.  
  95.  
  96.  
  97. /* mkaddcon -- Make Address Constant, given integer value */
  98.  
  99.  expptr
  100. #ifdef KR_headers
  101. mkaddcon(l)
  102.     register long l;
  103. #else
  104. mkaddcon(register long l)
  105. #endif
  106. {
  107.     register Constp p;
  108.  
  109.     p = mkconst(TYADDR);
  110.     p->Const.ci = l;
  111.     return( (expptr) p );
  112. }
  113.  
  114.  
  115.  
  116. /* mkrealcon -- Make Real Constant.  The type t is assumed
  117.    to be TYREAL or TYDREAL */
  118.  
  119.  expptr
  120. #ifdef KR_headers
  121. mkrealcon(t, d)
  122.     register int t;
  123.     char *d;
  124. #else
  125. mkrealcon(register int t, char *d)
  126. #endif
  127. {
  128.     register Constp p;
  129.  
  130.     p = mkconst(t);
  131.     p->Const.cds[0] = cds(d,CNULL);
  132.     p->vstg = 1;
  133.     return( (expptr) p );
  134. }
  135.  
  136.  
  137. /* mkbitcon -- Make bit constant.  Reads the input string, which is
  138.    assumed to correctly specify a number in base 2^shift (where   shift
  139.    is the input parameter).   shift   may not exceed 4, i.e. only binary,
  140.    quad, octal and hex bases may be input.  Constants may not exceed 32
  141.    bits, or whatever the size of (struct Constblock).ci may be. */
  142.  
  143.  expptr
  144. #ifdef KR_headers
  145. mkbitcon(shift, leng, s)
  146.     int shift;
  147.     int leng;
  148.     char *s;
  149. #else
  150. mkbitcon(int shift, int leng, char *s)
  151. #endif
  152. {
  153.     register Constp p;
  154.     register long x;
  155.  
  156.     p = mkconst(TYLONG);
  157.     x = 0;
  158.     while(--leng >= 0)
  159.         if(*s != ' ')
  160.             x = (x << shift) | hextoi(*s++);
  161.     /* mwm wanted to change the type to short for short constants,
  162.      * but this is dangerous -- there is no syntax for long constants
  163.      * with small values.
  164.      */
  165.     p->Const.ci = x;
  166.     return( (expptr) p );
  167. }
  168.  
  169.  
  170.  
  171.  
  172.  
  173. /* mkstrcon -- Make string constant.  Allocates storage and initializes
  174.    the memory for a copy of the input Fortran-string. */
  175.  
  176.  expptr
  177. #ifdef KR_headers
  178. mkstrcon(l, v)
  179.     int l;
  180.     register char *v;
  181. #else
  182. mkstrcon(int l, register char *v)
  183. #endif
  184. {
  185.     register Constp p;
  186.     register char *s;
  187.  
  188.     p = mkconst(TYCHAR);
  189.     p->vleng = ICON(l);
  190.     p->Const.ccp = s = (char *) ckalloc(l+1);
  191.     p->Const.ccp1.blanks = 0;
  192.     while(--l >= 0)
  193.         *s++ = *v++;
  194.     *s = '\0';
  195.     return( (expptr) p );
  196. }
  197.  
  198.  
  199.  
  200. /* mkcxcon -- Make complex contsant.  A complex number is a pair of
  201.    values, each of which may be integer, real or double. */
  202.  
  203.  expptr
  204. #ifdef KR_headers
  205. mkcxcon(realp, imagp)
  206.     register expptr realp;
  207.     register expptr imagp;
  208. #else
  209. mkcxcon(register expptr realp, register expptr imagp)
  210. #endif
  211. {
  212.     int rtype, itype;
  213.     register Constp p;
  214.  
  215.     rtype = realp->headblock.vtype;
  216.     itype = imagp->headblock.vtype;
  217.  
  218.     if( ISCONST(realp) && ISNUMERIC(rtype) && ISCONST(imagp) && ISNUMERIC(itype) )
  219.     {
  220.         p = mkconst( (rtype==TYDREAL||itype==TYDREAL)
  221.                 ? TYDCOMPLEX : tycomplex);
  222.         if (realp->constblock.vstg || imagp->constblock.vstg) {
  223.             p->vstg = 1;
  224.             p->Const.cds[0] = ISINT(rtype)
  225.                 ? string_num("", realp->constblock.Const.ci)
  226.                 : realp->constblock.vstg
  227.                     ? realp->constblock.Const.cds[0]
  228.                     : dtos(realp->constblock.Const.cd[0]);
  229.             p->Const.cds[1] = ISINT(itype)
  230.                 ? string_num("", imagp->constblock.Const.ci)
  231.                 : imagp->constblock.vstg
  232.                     ? imagp->constblock.Const.cds[0]
  233.                     : dtos(imagp->constblock.Const.cd[0]);
  234.             }
  235.         else {
  236.             p->Const.cd[0] = ISINT(rtype)
  237.                 ? realp->constblock.Const.ci
  238.                 : realp->constblock.Const.cd[0];
  239.             p->Const.cd[1] = ISINT(itype)
  240.                 ? imagp->constblock.Const.ci
  241.                 : imagp->constblock.Const.cd[0];
  242.             }
  243.     }
  244.     else
  245.     {
  246.         err("invalid complex constant");
  247.         p = (Constp)errnode();
  248.     }
  249.  
  250.     frexpr(realp);
  251.     frexpr(imagp);
  252.     return( (expptr) p );
  253. }
  254.  
  255.  
  256. /* errnode -- Allocate a new error block */
  257.  
  258.  expptr
  259. errnode(Void)
  260. {
  261.     struct Errorblock *p;
  262.     p = ALLOC(Errorblock);
  263.     p->tag = TERROR;
  264.     p->vtype = TYERROR;
  265.     return( (expptr) p );
  266. }
  267.  
  268.  
  269.  
  270.  
  271.  
  272. /* mkconv -- Make type conversion.  Cast expression   p   into type   t.
  273.    Note that casting to a character copies only the first sizeof(char)
  274.    bytes. */
  275.  
  276.  expptr
  277. #ifdef KR_headers
  278. mkconv(t, p)
  279.     register int t;
  280.     register expptr p;
  281. #else
  282. mkconv(register int t, register expptr p)
  283. #endif
  284. {
  285.     register expptr q;
  286.     register int pt, charwarn = 1;
  287.  
  288.     if (t >= 100) {
  289.         t -= 100;
  290.         charwarn = 0;
  291.         }
  292.     if(t==TYUNKNOWN || t==TYERROR)
  293.         badtype("mkconv", t);
  294.     pt = p->headblock.vtype;
  295.  
  296. /* Casting to the same type is a no-op */
  297.  
  298.     if(t == pt)
  299.         return(p);
  300.  
  301. /* If we're casting a constant which is not in the literal table ... */
  302.  
  303.     else if( ISCONST(p) && pt!=TYADDR && pt != TYCHAR)
  304.     {
  305.         if (ISINT(t) && ISINT(pt) || ISREAL(t) && ISREAL(pt)) {
  306.             /* avoid trouble with -i2 */
  307.             p->headblock.vtype = t;
  308.             return p;
  309.             }
  310.         q = (expptr) mkconst(t);
  311.         consconv(t, &q->constblock, &p->constblock );
  312.         frexpr(p);
  313.     }
  314.     else {
  315.         if (pt == TYCHAR && t != TYADDR && charwarn
  316.                 && (!halign || p->tag != TADDR
  317.                 || p->addrblock.uname_tag != UNAM_CONST))
  318.             warn(
  319.          "ichar([first char. of] char. string) assumed for conversion to numeric");
  320.         q = opconv(p, t);
  321.         }
  322.  
  323.     if(t == TYCHAR)
  324.         q->constblock.vleng = ICON(1);
  325.     return(q);
  326. }
  327.  
  328.  
  329.  
  330. /* opconv -- Convert expression   p   to type   t   using the main
  331.    expression evaluator; returns an OPCONV expression, I think  14-jun-88 mwm */
  332.  
  333.  expptr
  334. #ifdef KR_headers
  335. opconv(p, t)
  336.     expptr p;
  337.     int t;
  338. #else
  339. opconv(expptr p, int t)
  340. #endif
  341. {
  342.     register expptr q;
  343.  
  344.     if (t == TYSUBR)
  345.         err("illegal use of subroutine name");
  346.     q = mkexpr(OPCONV, p, ENULL);
  347.     q->headblock.vtype = t;
  348.     return(q);
  349. }
  350.  
  351.  
  352.  
  353. /* addrof -- Create an ADDR expression operation */
  354.  
  355.  expptr
  356. #ifdef KR_headers
  357. addrof(p)
  358.     expptr p;
  359. #else
  360. addrof(expptr p)
  361. #endif
  362. {
  363.     return( mkexpr(OPADDR, p, ENULL) );
  364. }
  365.  
  366.  
  367.  
  368. /* cpexpr - Returns a new copy of input expression   p   */
  369.  
  370.  tagptr
  371. #ifdef KR_headers
  372. cpexpr(p)
  373.     register tagptr p;
  374. #else
  375. cpexpr(register tagptr p)
  376. #endif
  377. {
  378.     register tagptr e;
  379.     int tag;
  380.     register chainp ep, pp;
  381.  
  382. /* This table depends on the ordering of the T macros, e.g. TNAME */
  383.  
  384.     static int blksize[ ] =
  385.     {
  386.         0,
  387.         sizeof(struct Nameblock),
  388.         sizeof(struct Constblock),
  389.         sizeof(struct Exprblock),
  390.         sizeof(struct Addrblock),
  391.         sizeof(struct Primblock),
  392.         sizeof(struct Listblock),
  393.         sizeof(struct Impldoblock),
  394.         sizeof(struct Errorblock)
  395.     };
  396.  
  397.     if(p == NULL)
  398.         return(NULL);
  399.  
  400. /* TNAMEs are special, and don't get copied.  Each name in the current
  401.    symbol table has a unique TNAME structure. */
  402.  
  403.     if( (tag = p->tag) == TNAME)
  404.         return(p);
  405.  
  406.     e = cpblock(blksize[p->tag], (char *)p);
  407.  
  408.     switch(tag)
  409.     {
  410.     case TCONST:
  411.         if(e->constblock.vtype == TYCHAR)
  412.         {
  413.             e->constblock.Const.ccp =
  414.                 copyn((int)e->constblock.vleng->constblock.Const.ci+1,
  415.                 e->constblock.Const.ccp);
  416.             e->constblock.vleng =
  417.                 (expptr) cpexpr(e->constblock.vleng);
  418.         }
  419.     case TERROR:
  420.         break;
  421.  
  422.     case TEXPR:
  423.         e->exprblock.leftp =  (expptr) cpexpr(p->exprblock.leftp);
  424.         e->exprblock.rightp = (expptr) cpexpr(p->exprblock.rightp);
  425.         break;
  426.  
  427.     case TLIST:
  428.         if(pp = p->listblock.listp)
  429.         {
  430.             ep = e->listblock.listp =
  431.                 mkchain((char *)cpexpr((tagptr)pp->datap), CHNULL);
  432.             for(pp = pp->nextp ; pp ; pp = pp->nextp)
  433.                 ep = ep->nextp =
  434.                     mkchain((char *)cpexpr((tagptr)pp->datap),
  435.                         CHNULL);
  436.         }
  437.         break;
  438.  
  439.     case TADDR:
  440.         e->addrblock.vleng = (expptr)  cpexpr(e->addrblock.vleng);
  441.         e->addrblock.memoffset = (expptr)cpexpr(e->addrblock.memoffset);
  442.         e->addrblock.istemp = NO;
  443.         break;
  444.  
  445.     case TPRIM:
  446.         e->primblock.argsp = (struct Listblock *)
  447.             cpexpr((expptr)e->primblock.argsp);
  448.         e->primblock.fcharp = (expptr) cpexpr(e->primblock.fcharp);
  449.         e->primblock.lcharp = (expptr) cpexpr(e->primblock.lcharp);
  450.         break;
  451.  
  452.     default:
  453.         badtag("cpexpr", tag);
  454.     }
  455.  
  456.     return(e);
  457. }
  458.  
  459. /* frexpr -- Free expression -- frees up memory used by expression   p   */
  460.  
  461.  void
  462. #ifdef KR_headers
  463. frexpr(p)
  464.     register tagptr p;
  465. #else
  466. frexpr(register tagptr p)
  467. #endif
  468. {
  469.     register chainp q;
  470.  
  471.     if(p == NULL)
  472.         return;
  473.  
  474.     switch(p->tag)
  475.     {
  476.     case TCONST:
  477.         if( ISCHAR(p) )
  478.         {
  479.             free( (charptr) (p->constblock.Const.ccp) );
  480.             frexpr(p->constblock.vleng);
  481.         }
  482.         break;
  483.  
  484.     case TADDR:
  485.         if (p->addrblock.vtype > TYERROR)    /* i/o block */
  486.             break;
  487.         frexpr(p->addrblock.vleng);
  488.         frexpr(p->addrblock.memoffset);
  489.         break;
  490.  
  491.     case TERROR:
  492.         break;
  493.  
  494. /* TNAME blocks don't get free'd - probably because they're pointed to in
  495.    the hash table. 14-Jun-88 -- mwm */
  496.  
  497.     case TNAME:
  498.         return;
  499.  
  500.     case TPRIM:
  501.         frexpr((expptr)p->primblock.argsp);
  502.         frexpr(p->primblock.fcharp);
  503.         frexpr(p->primblock.lcharp);
  504.         break;
  505.  
  506.     case TEXPR:
  507.         frexpr(p->exprblock.leftp);
  508.         if(p->exprblock.rightp)
  509.             frexpr(p->exprblock.rightp);
  510.         break;
  511.  
  512.     case TLIST:
  513.         for(q = p->listblock.listp ; q ; q = q->nextp)
  514.             frexpr((tagptr)q->datap);
  515.         frchain( &(p->listblock.listp) );
  516.         break;
  517.  
  518.     default:
  519.         badtag("frexpr", p->tag);
  520.     }
  521.  
  522.     free( (charptr) p );
  523. }
  524.  
  525.  void
  526. #ifdef KR_headers
  527. wronginf(np)
  528.     Namep np;
  529. #else
  530. wronginf(Namep np)
  531. #endif
  532. {
  533.     int c, k;
  534.     warn1("fixing wrong type inferred for %.65s", np->fvarname);
  535.     np->vinftype = 0;
  536.     c = letter(np->fvarname[0]);
  537.     if ((np->vtype = impltype[c]) == TYCHAR
  538.     && (k = implleng[c]))
  539.         np->vleng = ICON(k);
  540.     }
  541.  
  542. /* fix up types in expression; replace subtrees and convert
  543.    names to address blocks */
  544.  
  545.  expptr
  546. #ifdef KR_headers
  547. fixtype(p)
  548.     register tagptr p;
  549. #else
  550. fixtype(register tagptr p)
  551. #endif
  552. {
  553.  
  554.     if(p == 0)
  555.         return(0);
  556.  
  557.     switch(p->tag)
  558.     {
  559.     case TCONST:
  560.         if(ONEOF(p->constblock.vtype,MSKINT|MSKLOGICAL|MSKADDR|
  561.             MSKREAL) )
  562.             return( (expptr) p);
  563.  
  564.         return( (expptr) putconst((Constp)p) );
  565.  
  566.     case TADDR:
  567.         p->addrblock.memoffset = fixtype(p->addrblock.memoffset);
  568.         return( (expptr) p);
  569.  
  570.     case TERROR:
  571.         return( (expptr) p);
  572.  
  573.     default:
  574.         badtag("fixtype", p->tag);
  575.  
  576. /* This case means that   fixexpr   can't call   fixtype   with any expr,
  577.    only a subexpr of its parameter. */
  578.  
  579.     case TEXPR:
  580.         if (((Exprp)p)->typefixed)
  581.             return (expptr)p;
  582.         return( fixexpr((Exprp)p) );
  583.  
  584.     case TLIST:
  585.         return( (expptr) p );
  586.  
  587.     case TPRIM:
  588.         if(p->primblock.argsp && p->primblock.namep->vclass!=CLVAR)
  589.         {
  590.             if(p->primblock.namep->vtype == TYSUBR)
  591.             {
  592.                 err("function invocation of subroutine");
  593.                 return( errnode() );
  594.             }
  595.             else {
  596.                 if (p->primblock.namep->vinftype)
  597.                     wronginf(p->primblock.namep);
  598.                 return( mkfunct(p) );
  599.                 }
  600.         }
  601.  
  602. /* The lack of args makes   p   a function name, substring reference
  603.    or variable name. */
  604.  
  605.         else    return mklhs((struct Primblock *) p, keepsubs);
  606.     }
  607. }
  608.  
  609.  
  610.  int
  611. #ifdef KR_headers
  612. badchleng(p)
  613.     register expptr p;
  614. #else
  615. badchleng(register expptr p)
  616. #endif
  617. {
  618.     if (!p->headblock.vleng) {
  619.         if (p->headblock.tag == TADDR
  620.         && p->addrblock.uname_tag == UNAM_NAME)
  621.             errstr("bad use of character*(*) variable %.60s",
  622.                 p->addrblock.user.name->fvarname);
  623.         else
  624.             err("Bad use of character*(*)");
  625.         return 1;
  626.         }
  627.     return 0;
  628.     }
  629.  
  630.  
  631.  static expptr
  632. #ifdef KR_headers
  633. cplenexpr(p)
  634.     expptr p;
  635. #else
  636. cplenexpr(expptr p)
  637. #endif
  638. {
  639.     expptr rv;
  640.  
  641.     if (badchleng(p))
  642.         return ICON(1);
  643.     rv = cpexpr(p->headblock.vleng);
  644.     if (ISCONST(p) && p->constblock.vtype == TYCHAR)
  645.         rv->constblock.Const.ci += p->constblock.Const.ccp1.blanks;
  646.     return rv;
  647.     }
  648.  
  649.  
  650. /* special case tree transformations and cleanups of expression trees.
  651.    Parameter   p   should have a TEXPR tag at its root, else an error is
  652.    returned */
  653.  
  654.  expptr
  655. #ifdef KR_headers
  656. fixexpr(p)
  657.     register Exprp p;
  658. #else
  659. fixexpr(register Exprp p)
  660. #endif
  661. {
  662.     expptr lp;
  663.     register expptr rp;
  664.     register expptr q;
  665.     int opcode, ltype, rtype, ptype, mtype;
  666.  
  667.     if( ISERROR(p) || p->typefixed )
  668.         return( (expptr) p );
  669.     else if(p->tag != TEXPR)
  670.         badtag("fixexpr", p->tag);
  671.     opcode = p->opcode;
  672.  
  673. /* First set the types of the left and right subexpressions */
  674.  
  675.     lp = p->leftp;
  676.     if (!ISCONST(lp) || lp->constblock.vtype != TYCHAR)
  677.         lp = p->leftp = fixtype(lp);
  678.     ltype = lp->headblock.vtype;
  679.  
  680.     if(opcode==OPASSIGN && lp->tag!=TADDR)
  681.     {
  682.         err("left side of assignment must be variable");
  683.  eret:
  684.         frexpr((expptr)p);
  685.         return( errnode() );
  686.     }
  687.  
  688.     if(rp = p->rightp)
  689.     {
  690.         if (!ISCONST(rp) || rp->constblock.vtype != TYCHAR)
  691.             rp = p->rightp = fixtype(rp);
  692.         rtype = rp->headblock.vtype;
  693.     }
  694.     else
  695.         rtype = 0;
  696.  
  697.     if(ltype==TYERROR || rtype==TYERROR)
  698.         goto eret;
  699.  
  700. /* Now work on the whole expression */
  701.  
  702.     /* force folding if possible */
  703.  
  704.     if( ISCONST(lp) && (rp==NULL || ISCONST(rp)) )
  705.     {
  706.         q = opcode == OPCONV && lp->constblock.vtype == p->vtype
  707.             ? lp : mkexpr(opcode, lp, rp);
  708.  
  709. /* mkexpr is expected to reduce constant expressions */
  710.  
  711.         if( ISCONST(q) ) {
  712.             p->leftp = p->rightp = 0;
  713.             frexpr((expptr)p);
  714.             return(q);
  715.             }
  716.         free( (charptr) q );    /* constants did not fold */
  717.     }
  718.  
  719.     if( (ptype = cktype(opcode, ltype, rtype)) == TYERROR)
  720.         goto eret;
  721.  
  722.     if (ltype == TYCHAR && ISCONST(lp))
  723.         p->leftp =  lp = (expptr)putconst((Constp)lp);
  724.     if (rtype == TYCHAR && ISCONST(rp))
  725.         p->rightp = rp = (expptr)putconst((Constp)rp);
  726.  
  727.     switch(opcode)
  728.     {
  729.     case OPCONCAT:
  730.         if(p->vleng == NULL)
  731.             p->vleng = mkexpr(OPPLUS, cplenexpr(lp),
  732.                     cplenexpr(rp) );
  733.         break;
  734.  
  735.     case OPASSIGN:
  736.         if (rtype == TYREAL || ISLOGICAL(ptype))
  737.             break;
  738.     case OPPLUSEQ:
  739.     case OPSTAREQ:
  740.         if(ltype == rtype)
  741.             break;
  742.         if( ! ISCONST(rp) && ISREAL(ltype) && ISREAL(rtype) )
  743.             break;
  744.         if( ISCOMPLEX(ltype) || ISCOMPLEX(rtype) )
  745.             break;
  746.         if( ONEOF(ltype, MSKADDR|MSKINT) && ONEOF(rtype, MSKADDR|MSKINT)
  747.             && typesize[ltype]>=typesize[rtype] )
  748.                 break;
  749.  
  750. /* Cast the right hand side to match the type of the expression */
  751.  
  752.         p->rightp = fixtype( mkconv(ptype, rp) );
  753.         break;
  754.  
  755.     case OPSLASH:
  756.         if( ISCOMPLEX(rtype) )
  757.         {
  758.             p = (Exprp) call2(ptype,
  759.  
  760. /* Handle double precision complex variables */
  761.  
  762.                 ptype == TYCOMPLEX ? "c_div" : "z_div",
  763.                 mkconv(ptype, lp), mkconv(ptype, rp) );
  764.             break;
  765.         }
  766.     case OPPLUS:
  767.     case OPMINUS:
  768.     case OPSTAR:
  769.     case OPMOD:
  770.         if(ptype==TYDREAL && ( (ltype==TYREAL && ! ISCONST(lp) ) ||
  771.             (rtype==TYREAL && ! ISCONST(rp) ) ))
  772.             break;
  773.         if( ISCOMPLEX(ptype) )
  774.             break;
  775.  
  776. /* Cast both sides of the expression to match the type of the whole
  777.    expression.  */
  778.  
  779.         if(ltype != ptype && (ltype < TYINT1 || ptype > TYDREAL))
  780.             p->leftp = fixtype(mkconv(ptype,lp));
  781.         if(rtype != ptype && (rtype < TYINT1 || ptype > TYDREAL))
  782.             p->rightp = fixtype(mkconv(ptype,rp));
  783.         break;
  784.  
  785.     case OPPOWER:
  786.         rp = mkpower((expptr)p);
  787.         if (rp->tag == TEXPR)
  788.             rp->exprblock.typefixed = 1;
  789.         return rp;
  790.  
  791.     case OPLT:
  792.     case OPLE:
  793.     case OPGT:
  794.     case OPGE:
  795.     case OPEQ:
  796.     case OPNE:
  797.         if(ltype == rtype)
  798.             break;
  799.         if (htype) {
  800.             if (ltype == TYCHAR) {
  801.                 p->leftp = fixtype(mkconv(rtype,lp));
  802.                 break;
  803.                 }
  804.             if (rtype == TYCHAR) {
  805.                 p->rightp = fixtype(mkconv(ltype,rp));
  806.                 break;
  807.                 }
  808.             }
  809.         mtype = cktype(OPMINUS, ltype, rtype);
  810.         if(mtype==TYDREAL && (ltype==TYREAL || rtype==TYREAL))
  811.             break;
  812.         if( ISCOMPLEX(mtype) )
  813.             break;
  814.         if(ltype != mtype)
  815.             p->leftp = fixtype(mkconv(mtype,lp));
  816.         if(rtype != mtype)
  817.             p->rightp = fixtype(mkconv(mtype,rp));
  818.         break;
  819.  
  820.     case OPCONV:
  821.         ptype = cktype(OPCONV, p->vtype, ltype);
  822.         if(lp->tag==TEXPR && lp->exprblock.opcode==OPCOMMA
  823.          && !ISCOMPLEX(ptype))
  824.         {
  825.             lp->exprblock.rightp =
  826.                 fixtype( mkconv(ptype, lp->exprblock.rightp) );
  827.             free( (charptr) p );
  828.             p = (Exprp) lp;
  829.         }
  830.         break;
  831.  
  832.     case OPADDR:
  833.         if(lp->tag==TEXPR && lp->exprblock.opcode==OPADDR)
  834.             Fatal("addr of addr");
  835.         break;
  836.  
  837.     case OPCOMMA:
  838.     case OPQUEST:
  839.     case OPCOLON:
  840.         break;
  841.  
  842.     case OPMIN:
  843.     case OPMAX:
  844.     case OPMIN2:
  845.     case OPMAX2:
  846.     case OPDMIN:
  847.     case OPDMAX:
  848.     case OPABS:
  849.     case OPDABS:
  850.         ptype = p->vtype;
  851.         break;
  852.  
  853.     default:
  854.         break;
  855.     }
  856.  
  857.     p->vtype = ptype;
  858.     p->typefixed = 1;
  859.     return((expptr) p);
  860. }
  861.  
  862.  
  863. /* fix an argument list, taking due care for special first level cases */
  864.  
  865.  int
  866. #ifdef KR_headers
  867. fixargs(doput, p0)
  868.     int doput;
  869.     struct Listblock *p0;
  870. #else
  871. fixargs(int doput, struct Listblock *p0)
  872. #endif
  873.     /* doput is true if constants need to be passed by reference */
  874. {
  875.     register chainp p;
  876.     register tagptr q, t;
  877.     register int qtag;
  878.     int nargs;
  879.  
  880.     nargs = 0;
  881.     if(p0)
  882.         for(p = p0->listp ; p ; p = p->nextp)
  883.         {
  884.             ++nargs;
  885.             q = (tagptr)p->datap;
  886.             qtag = q->tag;
  887.             if(qtag == TCONST)
  888.             {
  889.  
  890. /* Call putconst() to store values in a constant table.  Since even
  891.    constants must be passed by reference, this can optimize on the storage
  892.    required */
  893.  
  894.                 p->datap = doput ? (char *)putconst((Constp)q)
  895.                          : (char *)q;
  896.             }
  897.  
  898. /* Take a function name and turn it into an Addr.  This only happens when
  899.    nothing else has figured out the function beforehand */
  900.  
  901.             else if(qtag==TPRIM && q->primblock.argsp==0 &&
  902.                 q->primblock.namep->vclass==CLPROC &&
  903.                 q->primblock.namep->vprocclass != PTHISPROC)
  904.                 p->datap = (char *)mkaddr(q->primblock.namep);
  905.  
  906.             else if(qtag==TPRIM && q->primblock.argsp==0 &&
  907.                 q->primblock.namep->vdim!=NULL)
  908.                 p->datap = (char *)mkscalar(q->primblock.namep);
  909.  
  910.             else if(qtag==TPRIM && q->primblock.argsp==0 &&
  911.                 q->primblock.namep->vdovar &&
  912.                 (t = (tagptr) memversion(q->primblock.namep)) )
  913.                 p->datap = (char *)fixtype(t);
  914.             else
  915.                 p->datap = (char *)fixtype(q);
  916.         }
  917.     return(nargs);
  918. }
  919.  
  920.  
  921.  
  922. /* mkscalar -- only called by   fixargs   above, and by some routines in
  923.    io.c */
  924.  
  925.  Addrp
  926. #ifdef KR_headers
  927. mkscalar(np)
  928.     register Namep np;
  929. #else
  930. mkscalar(register Namep np)
  931. #endif
  932. {
  933.     register Addrp ap;
  934.  
  935.     vardcl(np);
  936.     ap = mkaddr(np);
  937.  
  938.     /* The prolog causes array arguments to point to the
  939.      * (0,...,0) element, unless subscript checking is on.
  940.      */
  941.     if( !checksubs && np->vstg==STGARG)
  942.     {
  943.         register struct Dimblock *dp;
  944.         dp = np->vdim;
  945.         frexpr(ap->memoffset);
  946.         ap->memoffset = mkexpr(OPSTAR,
  947.             (np->vtype==TYCHAR ?
  948.             cpexpr(np->vleng) :
  949.             (tagptr)ICON(typesize[np->vtype]) ),
  950.             cpexpr(dp->baseoffset) );
  951.     }
  952.     return(ap);
  953. }
  954.  
  955.  
  956.  static void
  957. #ifdef KR_headers
  958. adjust_arginfo(np)
  959.     register Namep np;
  960. #else
  961. adjust_arginfo(register Namep np)
  962. #endif
  963.             /* adjust arginfo to omit the length arg for the
  964.                arg that we now know to be a character-valued
  965.                function */
  966. {
  967.     struct Entrypoint *ep;
  968.     register chainp args;
  969.     Argtypes *at;
  970.  
  971.     for(ep = entries; ep; ep = ep->entnextp)
  972.         for(args = ep->arglist; args; args = args->nextp)
  973.             if (np == (Namep)args->datap
  974.             && (at = ep->entryname->arginfo))
  975.                 --at->nargs;
  976.     }
  977.  
  978.  
  979.  
  980.  expptr
  981. #ifdef KR_headers
  982. mkfunct(p0)
  983.     expptr p0;
  984. #else
  985. mkfunct(expptr p0)
  986. #endif
  987. {
  988.     register struct Primblock *p = (struct Primblock *)p0;
  989.     struct Entrypoint *ep;
  990.     Addrp ap;
  991.     Extsym *extp;
  992.     register Namep np;
  993.     register expptr q;
  994.     extern chainp new_procs;
  995.     int k, nargs;
  996.     int class;
  997.  
  998.     if(p->tag != TPRIM)
  999.         return( errnode() );
  1000.  
  1001.     np = p->namep;
  1002.     class = np->vclass;
  1003.  
  1004.  
  1005.     if(class == CLUNKNOWN)
  1006.     {
  1007.         np->vclass = class = CLPROC;
  1008.         if(np->vstg == STGUNKNOWN)
  1009.         {
  1010.             if(np->vtype!=TYSUBR && (k = intrfunct(np->fvarname))
  1011.                 && (zflag || !(*(struct Intrpacked *)&k).f4
  1012.                     || dcomplex_seen))
  1013.             {
  1014.                 np->vstg = STGINTR;
  1015.                 np->vardesc.varno = k;
  1016.                 np->vprocclass = PINTRINSIC;
  1017.             }
  1018.             else
  1019.             {
  1020.                 extp = mkext(np->fvarname,
  1021.                     addunder(np->cvarname));
  1022.                 extp->extstg = STGEXT;
  1023.                 np->vstg = STGEXT;
  1024.                 np->vardesc.varno = extp - extsymtab;
  1025.                 np->vprocclass = PEXTERNAL;
  1026.             }
  1027.         }
  1028.         else if(np->vstg==STGARG)
  1029.         {
  1030.             if(np->vtype == TYCHAR) {
  1031.             adjust_arginfo(np);
  1032.             if (np->vpassed) {
  1033.                 char wbuf[160], *who;
  1034.                 who = np->fvarname;
  1035.                 sprintf(wbuf, "%s%s%s\n\t%s%s%s",
  1036.                     "Character-valued dummy procedure ",
  1037.                     who, " not declared EXTERNAL.",
  1038.             "Code may be wrong for previous function calls having ",
  1039.                     who, " as a parameter.");
  1040.                 warn(wbuf);
  1041.                 }
  1042.             }
  1043.             np->vprocclass = PEXTERNAL;
  1044.         }
  1045.     }
  1046.  
  1047.     if(class != CLPROC) {
  1048.         if (np->vstg == STGCOMMON)
  1049.             fatalstr(
  1050.              "Cannot invoke common variable %.50s as a function.",
  1051.                 np->fvarname);
  1052.         errstr("%.80s cannot be called.", np->fvarname);
  1053.         goto error;
  1054.         }
  1055.  
  1056. /* F77 doesn't allow subscripting of function calls */
  1057.  
  1058.     if(p->fcharp || p->lcharp)
  1059.     {
  1060.         err("no substring of function call");
  1061.         goto error;
  1062.     }
  1063.     impldcl(np);
  1064.     np->vimpltype = 0;    /* invoking as function ==> inferred type */
  1065.     np->vcalled = 1;
  1066.     nargs = fixargs( np->vprocclass!=PINTRINSIC,  p->argsp);
  1067.  
  1068.     switch(np->vprocclass)
  1069.     {
  1070.     case PEXTERNAL:
  1071.         if(np->vtype == TYUNKNOWN)
  1072.         {
  1073.             dclerr("attempt to use untyped function", np);
  1074.             np->vtype = dflttype[letter(np->fvarname[0])];
  1075.         }
  1076.         ap = mkaddr(np);
  1077.         if (!extsymtab[np->vardesc.varno].extseen) {
  1078.             new_procs = mkchain((char *)np, new_procs);
  1079.             extsymtab[np->vardesc.varno].extseen = 1;
  1080.             }
  1081. call:
  1082.         q = mkexpr(OPCALL, (expptr)ap, (expptr)p->argsp);
  1083.         q->exprblock.vtype = np->vtype;
  1084.         if(np->vleng)
  1085.             q->exprblock.vleng = (expptr) cpexpr(np->vleng);
  1086.         break;
  1087.  
  1088.     case PINTRINSIC:
  1089.         q = intrcall(np, p->argsp, nargs);
  1090.         break;
  1091.  
  1092.     case PSTFUNCT:
  1093.         q = stfcall(np, p->argsp);
  1094.         break;
  1095.  
  1096.     case PTHISPROC:
  1097.         warn("recursive call");
  1098.  
  1099. /* entries   is the list of multiple entry points */
  1100.  
  1101.         for(ep = entries ; ep ; ep = ep->entnextp)
  1102.             if(ep->enamep == np)
  1103.                 break;
  1104.         if(ep == NULL)
  1105.             Fatal("mkfunct: impossible recursion");
  1106.  
  1107.         ap = builtin(np->vtype, ep->entryname->cextname, -2);
  1108.         /* the negative last arg prevents adding */
  1109.         /* this name to the list of used builtins */
  1110.         goto call;
  1111.  
  1112.     default:
  1113.         fatali("mkfunct: impossible vprocclass %d",
  1114.             (int) (np->vprocclass) );
  1115.     }
  1116.     free( (charptr) p );
  1117.     return(q);
  1118.  
  1119. error:
  1120.     frexpr((expptr)p);
  1121.     return( errnode() );
  1122. }
  1123.  
  1124.  
  1125.  
  1126.  static expptr
  1127. #ifdef KR_headers
  1128. stfcall(np, actlist)
  1129.     Namep np;
  1130.     struct Listblock *actlist;
  1131. #else
  1132. stfcall(Namep np, struct Listblock *actlist)
  1133. #endif
  1134. {
  1135.     register chainp actuals;
  1136.     int nargs;
  1137.     chainp oactp, formals;
  1138.     int type;
  1139.     expptr Ln, Lq, q, q1, rhs, ap;
  1140.     Namep tnp;
  1141.     register struct Rplblock *rp;
  1142.     struct Rplblock *tlist;
  1143.  
  1144.     if (np->arginfo) {
  1145.         errstr("statement function %.66s calls itself.",
  1146.             np->fvarname);
  1147.         return ICON(0);
  1148.         }
  1149.     np->arginfo = (Argtypes *)np;    /* arbitrary nonzero value */
  1150.     if(actlist)
  1151.     {
  1152.         actuals = actlist->listp;
  1153.         free( (charptr) actlist);
  1154.     }
  1155.     else
  1156.         actuals = NULL;
  1157.     oactp = actuals;
  1158.  
  1159.     nargs = 0;
  1160.     tlist = NULL;
  1161.     if( (type = np->vtype) == TYUNKNOWN)
  1162.     {
  1163.         dclerr("attempt to use untyped statement function", np);
  1164.         type = np->vtype = dflttype[letter(np->fvarname[0])];
  1165.     }
  1166.     formals = (chainp) np->varxptr.vstfdesc->datap;
  1167.     rhs = (expptr) (np->varxptr.vstfdesc->nextp);
  1168.  
  1169.     /* copy actual arguments into temporaries */
  1170.     while(actuals!=NULL && formals!=NULL)
  1171.     {
  1172.         if (!(tnp = (Namep) formals->datap)) {
  1173.             /* buggy statement function declaration */
  1174.             q = ICON(1);
  1175.             goto done;
  1176.             }
  1177.         rp = ALLOC(Rplblock);
  1178.         rp->rplnp = tnp;
  1179.         ap = fixtype((tagptr)actuals->datap);
  1180.         if(tnp->vtype==ap->headblock.vtype && tnp->vtype!=TYCHAR
  1181.             && (ap->tag==TCONST || ap->tag==TADDR) )
  1182.         {
  1183.  
  1184. /* If actuals are constants or variable names, no temporaries are required */
  1185.             rp->rplvp = (expptr) ap;
  1186.             rp->rplxp = NULL;
  1187.             rp->rpltag = ap->tag;
  1188.         }
  1189.         else    {
  1190.             rp->rplvp = (expptr) mktmp(tnp->vtype, tnp->vleng);
  1191.             rp -> rplxp = NULL;
  1192.             putexpr ( mkexpr(OPASSIGN, cpexpr(rp->rplvp), ap));
  1193.             if((rp->rpltag = rp->rplvp->tag) == TERROR)
  1194.                 err("disagreement of argument types in statement function call");
  1195.         }
  1196.         rp->rplnextp = tlist;
  1197.         tlist = rp;
  1198.         actuals = actuals->nextp;
  1199.         formals = formals->nextp;
  1200.         ++nargs;
  1201.     }
  1202.  
  1203.     if(actuals!=NULL || formals!=NULL)
  1204.         err("statement function definition and argument list differ");
  1205.  
  1206.     /*
  1207.    now push down names involved in formal argument list, then
  1208.    evaluate rhs of statement function definition in this environment
  1209. */
  1210.  
  1211.     if(tlist)    /* put tlist in front of the rpllist */
  1212.     {
  1213.         for(rp = tlist; rp->rplnextp; rp = rp->rplnextp)
  1214.             ;
  1215.         rp->rplnextp = rpllist;
  1216.         rpllist = tlist;
  1217.     }
  1218.  
  1219. /* So when the expression finally gets evaled, that evaluator must read
  1220.    from the globl   rpllist   14-jun-88 mwm */
  1221.  
  1222.     q = (expptr) mkconv(type, fixtype(cpexpr(rhs)) );
  1223.  
  1224.     /* get length right of character-valued statement functions... */
  1225.     if (type == TYCHAR
  1226.      && (Ln = np->vleng)
  1227.      && q->tag != TERROR
  1228.      && (Lq = q->exprblock.vleng)
  1229.      && (Lq->tag != TCONST
  1230.         || Ln->constblock.Const.ci != Lq->constblock.Const.ci)) {
  1231.         q1 = (expptr) mktmp(type, Ln);
  1232.         putexpr ( mkexpr(OPASSIGN, cpexpr(q1), q));
  1233.         q = q1;
  1234.         }
  1235.  
  1236.     /* now generate the tree ( t1=a1, (t2=a2,... , f))))) */
  1237.     while(--nargs >= 0)
  1238.     {
  1239.         if(rpllist->rplxp)
  1240.             q = mkexpr(OPCOMMA, rpllist->rplxp, q);
  1241.         rp = rpllist->rplnextp;
  1242.         frexpr(rpllist->rplvp);
  1243.         free((char *)rpllist);
  1244.         rpllist = rp;
  1245.     }
  1246.  done:
  1247.     frchain( &oactp );
  1248.     np->arginfo = 0;
  1249.     return(q);
  1250. }
  1251.  
  1252.  
  1253. static int replaced;
  1254.  
  1255. /* mkplace -- Figure out the proper storage class for the input name and
  1256.    return an addrp with the appropriate stuff */
  1257.  
  1258.  Addrp
  1259. #ifdef KR_headers
  1260. mkplace(np)
  1261.     register Namep np;
  1262. #else
  1263. mkplace(register Namep np)
  1264. #endif
  1265. {
  1266.     register Addrp s;
  1267.     register struct Rplblock *rp;
  1268.     int regn;
  1269.  
  1270.     /* is name on the replace list? */
  1271.  
  1272.     for(rp = rpllist ; rp ; rp = rp->rplnextp)
  1273.     {
  1274.         if(np == rp->rplnp)
  1275.         {
  1276.             replaced = 1;
  1277.             if(rp->rpltag == TNAME)
  1278.             {
  1279.                 np = (Namep) (rp->rplvp);
  1280.                 break;
  1281.             }
  1282.             else    return( (Addrp) cpexpr(rp->rplvp) );
  1283.         }
  1284.     }
  1285.  
  1286.     /* is variable a DO index in a register ? */
  1287.  
  1288.     if(np->vdovar && ( (regn = inregister(np)) >= 0) )
  1289.         if(np->vtype == TYERROR)
  1290.             return((Addrp) errnode() );
  1291.         else
  1292.         {
  1293.             s = ALLOC(Addrblock);
  1294.             s->tag = TADDR;
  1295.             s->vstg = STGREG;
  1296.             s->vtype = TYIREG;
  1297.             s->memno = regn;
  1298.             s->memoffset = ICON(0);
  1299.             s -> uname_tag = UNAM_NAME;
  1300.             s -> user.name = np;
  1301.             return(s);
  1302.         }
  1303.  
  1304.     if (np->vclass == CLPROC && np->vprocclass != PTHISPROC)
  1305.         errstr("external %.60s used as a variable", np->fvarname);
  1306.     vardcl(np);
  1307.     return(mkaddr(np));
  1308. }
  1309.  
  1310.  static expptr
  1311. #ifdef KR_headers
  1312. subskept(p, a)
  1313.     struct Primblock *p;
  1314.     Addrp a;
  1315. #else
  1316. subskept(struct Primblock *p, Addrp a)
  1317. #endif
  1318. {
  1319.     expptr ep;
  1320.     struct Listblock *Lb;
  1321.     chainp cp;
  1322.  
  1323.     if (a->uname_tag != UNAM_NAME)
  1324.         erri("subskept: uname_tag %d", a->uname_tag);
  1325.     a->user.name->vrefused = 1;
  1326.     a->user.name->visused = 1;
  1327.     a->uname_tag = UNAM_REF;
  1328.     Lb = (struct Listblock *)cpexpr((tagptr)p->argsp);
  1329.     for(cp = Lb->listp; cp; cp = cp->nextp)
  1330.         cp->datap = (char *)putx(fixtype((tagptr)cp->datap));
  1331.     if (a->vtype == TYCHAR) {
  1332.         ep = p->fcharp    ? mkexpr(OPMINUS, cpexpr(p->fcharp), ICON(1))
  1333.                 : ICON(0);
  1334.         Lb->listp = mkchain((char *)ep, Lb->listp);
  1335.         }
  1336.     return (expptr)Lb;
  1337.     }
  1338.  
  1339.  static int doing_vleng;
  1340.  
  1341. /* mklhs -- Compute the actual address of the given expression; account
  1342.    for array subscripts, stack offset, and substring offsets.  The f -> C
  1343.    translator will need this only to worry about the subscript stuff */
  1344.  
  1345.  expptr
  1346. #ifdef KR_headers
  1347. mklhs(p, subkeep)
  1348.     register struct Primblock *p;
  1349.     int subkeep;
  1350. #else
  1351. mklhs(register struct Primblock *p, int subkeep)
  1352. #endif
  1353. {
  1354.     register Addrp s;
  1355.     Namep np;
  1356.  
  1357.     if(p->tag != TPRIM)
  1358.         return( (expptr) p );
  1359.     np = p->namep;
  1360.  
  1361.     replaced = 0;
  1362.     s = mkplace(np);
  1363.     if(s->tag!=TADDR || s->vstg==STGREG)
  1364.     {
  1365.         free( (charptr) p );
  1366.         return( (expptr) s );
  1367.     }
  1368.     s->parenused = p->parenused;
  1369.  
  1370.     /* compute the address modified by subscripts */
  1371.  
  1372.     if (!replaced)
  1373.         s->memoffset = (subkeep && np->vdim
  1374.                 && (np->vdim->ndim > 1 || np->vtype == TYCHAR
  1375.                 && (!ISCONST(np->vleng)
  1376.                   || np->vleng->constblock.Const.ci != 1)))
  1377.                 ? subskept(p,s)
  1378.                 : mkexpr(OPPLUS, s->memoffset, suboffset(p) );
  1379.     frexpr((expptr)p->argsp);
  1380.     p->argsp = NULL;
  1381.  
  1382.     /* now do substring part */
  1383.  
  1384.     if(p->fcharp || p->lcharp)
  1385.     {
  1386.         if(np->vtype != TYCHAR)
  1387.             errstr("substring of noncharacter %s", np->fvarname);
  1388.         else    {
  1389.             if(p->lcharp == NULL)
  1390.                 p->lcharp = (expptr) cpexpr(s->vleng);
  1391.             if(p->fcharp) {
  1392.                 doing_vleng = 1;
  1393.                 s->vleng = fixtype(mkexpr(OPMINUS,
  1394.                         p->lcharp,
  1395.                     mkexpr(OPMINUS, p->fcharp, ICON(1) )));
  1396.                 doing_vleng = 0;
  1397.                 }
  1398.             else    {
  1399.                 frexpr(s->vleng);
  1400.                 s->vleng = p->lcharp;
  1401.             }
  1402.         }
  1403.     }
  1404.  
  1405.     s->vleng = fixtype( s->vleng );
  1406.     s->memoffset = fixtype( s->memoffset );
  1407.     free( (charptr) p );
  1408.     return( (expptr) s );
  1409. }
  1410.  
  1411.  
  1412.  
  1413.  
  1414.  
  1415. /* deregister -- remove a register allocation from the list; assumes that
  1416.    names are deregistered in stack order (LIFO order - Last In First Out) */
  1417.  
  1418.  void
  1419. #ifdef KR_headers
  1420. deregister(np)
  1421.     Namep np;
  1422. #else
  1423. deregister(Namep np)
  1424. #endif
  1425. {
  1426.     if(nregvar>0 && regnamep[nregvar-1]==np)
  1427.     {
  1428.         --nregvar;
  1429.     }
  1430. }
  1431.  
  1432.  
  1433.  
  1434.  
  1435. /* memversion -- moves a DO index REGISTER into a memory location; other
  1436.    objects are passed through untouched */
  1437.  
  1438.  Addrp
  1439. #ifdef KR_headers
  1440. memversion(np)
  1441.     register Namep np;
  1442. #else
  1443. memversion(register Namep np)
  1444. #endif
  1445. {
  1446.     register Addrp s;
  1447.  
  1448.     if(np->vdovar==NO || (inregister(np)<0) )
  1449.         return(NULL);
  1450.     np->vdovar = NO;
  1451.     s = mkplace(np);
  1452.     np->vdovar = YES;
  1453.     return(s);
  1454. }
  1455.  
  1456.  
  1457.  
  1458. /* inregister -- looks for the input name in the global list   regnamep */
  1459.  
  1460.  int
  1461. #ifdef KR_headers
  1462. inregister(np)
  1463.     register Namep np;
  1464. #else
  1465. inregister(register Namep np)
  1466. #endif
  1467. {
  1468.     register int i;
  1469.  
  1470.     for(i = 0 ; i < nregvar ; ++i)
  1471.         if(regnamep[i] == np)
  1472.             return( regnum[i] );
  1473.     return(-1);
  1474. }
  1475.  
  1476.  
  1477.  
  1478. /* suboffset -- Compute the offset from the start of the array, given the
  1479.    subscripts as arguments */
  1480.  
  1481.  expptr
  1482. #ifdef KR_headers
  1483. suboffset(p)
  1484.     register struct Primblock *p;
  1485. #else
  1486. suboffset(register struct Primblock *p)
  1487. #endif
  1488. {
  1489.     int n;
  1490.     expptr si, size;
  1491.     chainp cp;
  1492.     expptr e, e1, offp, prod;
  1493.     struct Dimblock *dimp;
  1494.     expptr sub[MAXDIM+1];
  1495.     register Namep np;
  1496.  
  1497.     np = p->namep;
  1498.     offp = ICON(0);
  1499.     n = 0;
  1500.     if(p->argsp)
  1501.         for(cp = p->argsp->listp ; cp ; cp = cp->nextp)
  1502.         {
  1503.             si = fixtype(cpexpr((tagptr)cp->datap));
  1504.             if (!ISINT(si->headblock.vtype)) {
  1505.                 NOEXT("non-integer subscript");
  1506.                 si = mkconv(TYLONG, si);
  1507.                 }
  1508.             sub[n++] = si;
  1509.             if(n > maxdim)
  1510.             {
  1511.                 erri("more than %d subscripts", maxdim);
  1512.                 break;
  1513.             }
  1514.         }
  1515.  
  1516.     dimp = np->vdim;
  1517.     if(n>0 && dimp==NULL)
  1518.         errstr("subscripts on scalar variable %.68s", np->fvarname);
  1519.     else if(dimp && dimp->ndim!=n)
  1520.         errstr("wrong number of subscripts on %.68s", np->fvarname);
  1521.     else if(n > 0)
  1522.     {
  1523.         prod = sub[--n];
  1524.         while( --n >= 0)
  1525.             prod = mkexpr(OPPLUS, sub[n],
  1526.                 mkexpr(OPSTAR, prod, cpexpr(dimp->dims[n].dimsize)) );
  1527.         if(checksubs || np->vstg!=STGARG)
  1528.             prod = mkexpr(OPMINUS, prod, cpexpr(dimp->baseoffset));
  1529.  
  1530. /* Add in the run-time bounds check */
  1531.  
  1532.         if(checksubs)
  1533.             prod = subcheck(np, prod);
  1534.         size = np->vtype == TYCHAR ?
  1535.             (expptr) cpexpr(np->vleng) : ICON(typesize[np->vtype]);
  1536.         prod = mkexpr(OPSTAR, prod, size);
  1537.         offp = mkexpr(OPPLUS, offp, prod);
  1538.     }
  1539.  
  1540. /* Check for substring indicator */
  1541.  
  1542.     if(p->fcharp && np->vtype==TYCHAR) {
  1543.         e = p->fcharp;
  1544.         e1 = mkexpr(OPMINUS, cpexpr(e), ICON(1));
  1545.         if (!ISCONST(e) && (e->tag != TPRIM || e->primblock.argsp)) {
  1546.             e = (expptr)mktmp(TYLONG, ENULL);
  1547.             putout(putassign(cpexpr(e), e1));
  1548.             p->fcharp = mkexpr(OPPLUS, cpexpr(e), ICON(1));
  1549.             e1 = e;
  1550.             }
  1551.         offp = mkexpr(OPPLUS, offp, e1);
  1552.         }
  1553.     return(offp);
  1554. }
  1555.  
  1556.  
  1557.  
  1558.  
  1559.  expptr
  1560. #ifdef KR_headers
  1561. subcheck(np, p)
  1562.     Namep np;
  1563.     register expptr p;
  1564. #else
  1565. subcheck(Namep np, register expptr p)
  1566. #endif
  1567. {
  1568.     struct Dimblock *dimp;
  1569.     expptr t, checkvar, checkcond, badcall;
  1570.  
  1571.     dimp = np->vdim;
  1572.     if(dimp->nelt == NULL)
  1573.         return(p);    /* don't check arrays with * bounds */
  1574.     np->vlastdim = 0;
  1575.     if( ISICON(p) )
  1576.     {
  1577.  
  1578. /* check for negative (constant) offset */
  1579.  
  1580.         if(p->constblock.Const.ci < 0)
  1581.             goto badsub;
  1582.         if( ISICON(dimp->nelt) )
  1583.  
  1584. /* see if constant offset exceeds the array declaration */
  1585.  
  1586.             if(p->constblock.Const.ci < dimp->nelt->constblock.Const.ci)
  1587.                 return(p);
  1588.             else
  1589.                 goto badsub;
  1590.     }
  1591.  
  1592. /* We know that the subscript offset   p   or   dimp -> nelt   is not a constant.
  1593.    Now find a register to use for run-time bounds checking */
  1594.  
  1595.     if(p->tag==TADDR && p->addrblock.vstg==STGREG)
  1596.     {
  1597.         checkvar = (expptr) cpexpr(p);
  1598.         t = p;
  1599.     }
  1600.     else    {
  1601.         checkvar = (expptr) mktmp(p->headblock.vtype, ENULL);
  1602.         t = mkexpr(OPASSIGN, cpexpr(checkvar), p);
  1603.     }
  1604.     checkcond = mkexpr(OPLT, t, cpexpr(dimp->nelt) );
  1605.     if( ! ISICON(p) )
  1606.         checkcond = mkexpr(OPAND, checkcond,
  1607.             mkexpr(OPLE, ICON(0), cpexpr(checkvar)) );
  1608.  
  1609. /* Construct the actual test */
  1610.  
  1611.     badcall = call4(p->headblock.vtype, "s_rnge",
  1612.         mkstrcon(strlen(np->fvarname), np->fvarname),
  1613.         mkconv(TYLONG,  cpexpr(checkvar)),
  1614.         mkstrcon(strlen(procname), procname),
  1615.         ICON(lineno) );
  1616.     badcall->exprblock.opcode = OPCCALL;
  1617.     p = mkexpr(OPQUEST, checkcond,
  1618.         mkexpr(OPCOLON, checkvar, badcall));
  1619.  
  1620.     return(p);
  1621.  
  1622. badsub:
  1623.     frexpr(p);
  1624.     errstr("subscript on variable %s out of range", np->fvarname);
  1625.     return ( ICON(0) );
  1626. }
  1627.  
  1628.  
  1629.  
  1630.  
  1631.  Addrp
  1632. #ifdef KR_headers
  1633. mkaddr(p)
  1634.     register Namep p;
  1635. #else
  1636. mkaddr(register Namep p)
  1637. #endif
  1638. {
  1639.     Extsym *extp;
  1640.     register Addrp t;
  1641.     int k;
  1642.  
  1643.     switch( p->vstg)
  1644.     {
  1645.     case STGAUTO:
  1646.         if(p->vclass == CLPROC && p->vprocclass == PTHISPROC)
  1647.             return (Addrp) cpexpr((expptr)xretslot[p->vtype]);
  1648.         goto other;
  1649.  
  1650.     case STGUNKNOWN:
  1651.         if(p->vclass != CLPROC)
  1652.             break;    /* Error */
  1653.         extp = mkext(p->fvarname, addunder(p->cvarname));
  1654.         extp->extstg = STGEXT;
  1655.         p->vstg = STGEXT;
  1656.         p->vardesc.varno = extp - extsymtab;
  1657.         p->vprocclass = PEXTERNAL;
  1658.         if ((extp->exproto || infertypes)
  1659.         && (p->vtype == TYUNKNOWN || p->vimpltype)
  1660.         && (k = extp->extype))
  1661.             inferdcl(p, k);
  1662.  
  1663.  
  1664.     case STGCOMMON:
  1665.     case STGEXT:
  1666.     case STGBSS:
  1667.     case STGINIT:
  1668.     case STGEQUIV:
  1669.     case STGARG:
  1670.     case STGLENG:
  1671.  other:
  1672.         t = ALLOC(Addrblock);
  1673.         t->tag = TADDR;
  1674.  
  1675.         t->vclass = p->vclass;
  1676.         t->vtype = p->vtype;
  1677.         t->vstg = p->vstg;
  1678.         t->memno = p->vardesc.varno;
  1679.         t->memoffset = ICON(p->voffset);
  1680.         if (p->vdim)
  1681.             t->isarray = 1;
  1682.         if(p->vleng)
  1683.         {
  1684.             t->vleng = (expptr) cpexpr(p->vleng);
  1685.             if( ISICON(t->vleng) )
  1686.                 t->varleng = t->vleng->constblock.Const.ci;
  1687.         }
  1688.  
  1689. /* Keep the original name around for the C code generation */
  1690.  
  1691.         t -> uname_tag = UNAM_NAME;
  1692.         t -> user.name = p;
  1693.         return(t);
  1694.  
  1695.     case STGINTR:
  1696.  
  1697.         return ( intraddr (p));
  1698.  
  1699.     case STGSTFUNCT:
  1700.  
  1701.         errstr("invalid use of statement function %.64s.", p->fvarname);
  1702.         return putconst((Constp)ICON(0));
  1703.     }
  1704.     badstg("mkaddr", p->vstg);
  1705.     /* NOT REACHED */ return 0;
  1706. }
  1707.  
  1708.  
  1709.  
  1710.  
  1711. /* mkarg -- create storage for a new parameter.  This is called when a
  1712.    function returns a string (for the return value, which is the first
  1713.    parameter), or when a variable-length string is passed to a function. */
  1714.  
  1715.  Addrp
  1716. #ifdef KR_headers
  1717. mkarg(type, argno)
  1718.     int type;
  1719.     int argno;
  1720. #else
  1721. mkarg(int type, int argno)
  1722. #endif
  1723. {
  1724.     register Addrp p;
  1725.  
  1726.     p = ALLOC(Addrblock);
  1727.     p->tag = TADDR;
  1728.     p->vtype = type;
  1729.     p->vclass = CLVAR;
  1730.  
  1731. /* TYLENG is the type of the field holding the length of a character string */
  1732.  
  1733.     p->vstg = (type==TYLENG ? STGLENG : STGARG);
  1734.     p->memno = argno;
  1735.     return(p);
  1736. }
  1737.  
  1738.  
  1739.  
  1740.  
  1741. /* mkprim -- Create a PRIM (primary/primitive) block consisting of a
  1742.    Nameblock (or Paramblock), arguments (actual params or array
  1743.    subscripts) and substring bounds.  Requires that   v   have lots of
  1744.    extra (uninitialized) storage, since it could be a paramblock or
  1745.    nameblock */
  1746.  
  1747.  expptr
  1748. #ifdef KR_headers
  1749. mkprim(v0, args, substr)
  1750.     Namep v0;
  1751.     struct Listblock *args;
  1752.     chainp substr;
  1753. #else
  1754. mkprim(Namep v0, struct Listblock *args, chainp substr)
  1755. #endif
  1756. {
  1757.     typedef union {
  1758.         struct Paramblock paramblock;
  1759.         struct Nameblock nameblock;
  1760.         struct Headblock headblock;
  1761.         } *Primu;
  1762.     register Primu v = (Primu)v0;
  1763.     register struct Primblock *p;
  1764.  
  1765.     if(v->headblock.vclass == CLPARAM)
  1766.     {
  1767.  
  1768. /* v   is to be a Paramblock */
  1769.  
  1770.         if(args || substr)
  1771.         {
  1772.             errstr("no qualifiers on parameter name %s",
  1773.                 v->paramblock.fvarname);
  1774.             frexpr((expptr)args);
  1775.             if(substr)
  1776.             {
  1777.                 frexpr((tagptr)substr->datap);
  1778.                 frexpr((tagptr)substr->nextp->datap);
  1779.                 frchain(&substr);
  1780.             }
  1781.             frexpr((expptr)v);
  1782.             return( errnode() );
  1783.         }
  1784.         return( (expptr) cpexpr(v->paramblock.paramval) );
  1785.     }
  1786.  
  1787.     p = ALLOC(Primblock);
  1788.     p->tag = TPRIM;
  1789.     p->vtype = v->nameblock.vtype;
  1790.  
  1791. /* v   is to be a Nameblock */
  1792.  
  1793.     p->namep = (Namep) v;
  1794.     p->argsp = args;
  1795.     if(substr)
  1796.     {
  1797.         p->fcharp = (expptr) substr->datap;
  1798.         p->lcharp = (expptr) substr->nextp->datap;
  1799.         frchain(&substr);
  1800.     }
  1801.     return( (expptr) p);
  1802. }
  1803.  
  1804.  
  1805.  
  1806. /* vardcl -- attempt to fill out the Name template for variable   v.
  1807.    This function is called on identifiers known to be variables or
  1808.    recursive references to the same function */
  1809.  
  1810.  void
  1811. #ifdef KR_headers
  1812. vardcl(v)
  1813.     register Namep v;
  1814. #else
  1815. vardcl(register Namep v)
  1816. #endif
  1817. {
  1818.     struct Dimblock *t;
  1819.     expptr neltp;
  1820.     extern int doing_stmtfcn;
  1821.  
  1822.     if(v->vclass == CLUNKNOWN) {
  1823.         v->vclass = CLVAR;
  1824.         if (v->vinftype) {
  1825.             v->vtype = TYUNKNOWN;
  1826.             if (v->vdcldone) {
  1827.                 v->vdcldone = 0;
  1828.                 impldcl(v);
  1829.                 }
  1830.             }
  1831.         }
  1832.     if(v->vdcldone)
  1833.         return;
  1834.     if(v->vclass == CLNAMELIST)
  1835.         return;
  1836.  
  1837.     if(v->vtype == TYUNKNOWN)
  1838.         impldcl(v);
  1839.     else if(v->vclass!=CLVAR && v->vprocclass!=PTHISPROC)
  1840.     {
  1841.         dclerr("used as variable", v);
  1842.         return;
  1843.     }
  1844.     if(v->vstg==STGUNKNOWN) {
  1845.         if (doing_stmtfcn) {
  1846.             /* neither declare this variable if its only use */
  1847.             /* is in defining a stmt function, nor complain  */
  1848.             /* that it is never used */
  1849.             v->vimpldovar = 1;
  1850.             return;
  1851.             }
  1852.         v->vstg = implstg[ letter(v->fvarname[0]) ];
  1853.         v->vimplstg = 1;
  1854.         }
  1855.  
  1856. /* Compute the actual storage location, i.e. offsets from base addresses,
  1857.    possibly the stack pointer */
  1858.  
  1859.     switch(v->vstg)
  1860.     {
  1861.     case STGBSS:
  1862.         v->vardesc.varno = ++lastvarno;
  1863.         break;
  1864.     case STGAUTO:
  1865.         if(v->vclass==CLPROC && v->vprocclass==PTHISPROC)
  1866.             break;
  1867.         if(t = v->vdim)
  1868.             if( (neltp = t->nelt) && ISCONST(neltp) ) ;
  1869.             else
  1870.                 dclerr("adjustable automatic array", v);
  1871.         break;
  1872.  
  1873.     default:
  1874.         break;
  1875.     }
  1876.     v->vdcldone = YES;
  1877. }
  1878.  
  1879.  
  1880.  
  1881. /* Set the implicit type declaration of parameter   p   based on its first
  1882.    letter */
  1883.  
  1884.  void
  1885. #ifdef KR_headers
  1886. impldcl(p)
  1887.     register Namep p;
  1888. #else
  1889. impldcl(register Namep p)
  1890. #endif
  1891. {
  1892.     register int k;
  1893.     int type;
  1894.     ftnint leng;
  1895.  
  1896.     if(p->vdcldone || (p->vclass==CLPROC && p->vprocclass==PINTRINSIC) )
  1897.         return;
  1898.     if(p->vtype == TYUNKNOWN)
  1899.     {
  1900.         k = letter(p->fvarname[0]);
  1901.         type = impltype[ k ];
  1902.         leng = implleng[ k ];
  1903.         if(type == TYUNKNOWN)
  1904.         {
  1905.             if(p->vclass == CLPROC)
  1906.                 return;
  1907.             dclerr("attempt to use undefined variable", p);
  1908.             type = dflttype[k];
  1909.             leng = 0;
  1910.         }
  1911.         settype(p, type, leng);
  1912.         p->vimpltype = 1;
  1913.     }
  1914. }
  1915.  
  1916.  void
  1917. #ifdef KR_headers
  1918. inferdcl(np, type)
  1919.     Namep np;
  1920.     int type;
  1921. #else
  1922. inferdcl(Namep np, int type)
  1923. #endif
  1924. {
  1925.     int k = impltype[letter(np->fvarname[0])];
  1926.     if (k != type) {
  1927.         np->vinftype = 1;
  1928.         np->vtype = type;
  1929.         frexpr(np->vleng);
  1930.         np->vleng = 0;
  1931.         }
  1932.     np->vimpltype = 0;
  1933.     np->vinfproc = 1;
  1934.     }
  1935.  
  1936.  LOCAL int
  1937. #ifdef KR_headers
  1938. zeroconst(e)
  1939.     expptr e;
  1940. #else
  1941. zeroconst(expptr e)
  1942. #endif
  1943. {
  1944.     register Constp c = (Constp) e;
  1945.     if (c->tag == TCONST)
  1946.         switch(c->vtype) {
  1947.         case TYINT1:
  1948.         case TYSHORT:
  1949.         case TYLONG:
  1950. #ifdef TYQUAD
  1951.         case TYQUAD:
  1952. #endif
  1953.             return c->Const.ci == 0;
  1954.  
  1955.         case TYREAL:
  1956.         case TYDREAL:
  1957.             if (c->vstg == 1)
  1958.                 return !strcmp(c->Const.cds[0],"0.");
  1959.             return c->Const.cd[0] == 0.;
  1960.  
  1961.         case TYCOMPLEX:
  1962.         case TYDCOMPLEX:
  1963.             if (c->vstg == 1)
  1964.                 return !strcmp(c->Const.cds[0],"0.")
  1965.                     && !strcmp(c->Const.cds[1],"0.");
  1966.             return c->Const.cd[0] == 0. && c->Const.cd[1] == 0.;
  1967.         }
  1968.     return 0;
  1969.     }
  1970.  
  1971.  
  1972. #define ICONEQ(z, c)  (ISICON(z) && z->constblock.Const.ci==c)
  1973. #define COMMUTE    { e = lp;  lp = rp;  rp = e; }
  1974.  
  1975. /* mkexpr -- Make expression, and simplify constant subcomponents (tree
  1976.    order is not preserved).  Assumes that   lp   is nonempty, and uses
  1977.    fold()   to simplify adjacent constants */
  1978.  
  1979.  expptr
  1980. #ifdef KR_headers
  1981. mkexpr(opcode, lp, rp)
  1982.     int opcode;
  1983.     register expptr lp;
  1984.     register expptr rp;
  1985. #else
  1986. mkexpr(int opcode, register expptr lp, register expptr rp)
  1987. #endif
  1988. {
  1989.     register expptr e, e1;
  1990.     int etype;
  1991.     int ltype, rtype;
  1992.     int ltag, rtag;
  1993.     long L;
  1994.     static long divlineno;
  1995.  
  1996.     ltype = lp->headblock.vtype;
  1997.     ltag = lp->tag;
  1998.     if(rp && opcode!=OPCALL && opcode!=OPCCALL)
  1999.     {
  2000.         rtype = rp->headblock.vtype;
  2001.         rtag = rp->tag;
  2002.     }
  2003.     else rtype = 0;
  2004.  
  2005.     etype = cktype(opcode, ltype, rtype);
  2006.     if(etype == TYERROR)
  2007.         goto error;
  2008.  
  2009.     switch(opcode)
  2010.     {
  2011.         /* check for multiplication by 0 and 1 and addition to 0 */
  2012.  
  2013.     case OPSTAR:
  2014.         if( ISCONST(lp) )
  2015.             COMMUTE
  2016.  
  2017.                 if( ISICON(rp) )
  2018.             {
  2019.                 if(rp->constblock.Const.ci == 0)
  2020.                     goto retright;
  2021.                 goto mulop;
  2022.             }
  2023.         break;
  2024.  
  2025.     case OPSLASH:
  2026.     case OPMOD:
  2027.         if( zeroconst(rp) && lineno != divlineno ) {
  2028.             warn("attempted division by zero");
  2029.             divlineno = lineno;
  2030.             }
  2031.         if(opcode == OPMOD)
  2032.             break;
  2033.  
  2034. /* Handle multiplying or dividing by 1, -1 */
  2035.  
  2036. mulop:
  2037.         if( ISICON(rp) )
  2038.         {
  2039.             if(rp->constblock.Const.ci == 1)
  2040.                 goto retleft;
  2041.  
  2042.             if(rp->constblock.Const.ci == -1)
  2043.             {
  2044.                 frexpr(rp);
  2045.                 return( mkexpr(OPNEG, lp, ENULL) );
  2046.             }
  2047.         }
  2048.  
  2049. /* Group all constants together.  In particular,
  2050.  
  2051.     (x * CONST1) * CONST2 ==> x * (CONST1 * CONST2)
  2052.     (x * CONST1) / CONST2 ==> x * (CONST1 / CONST2)
  2053. */
  2054.  
  2055.         if (lp->tag != TEXPR || !lp->exprblock.rightp
  2056.                 || !ISICON(lp->exprblock.rightp))
  2057.             break;
  2058.  
  2059.         if (lp->exprblock.opcode == OPLSHIFT) {
  2060.             L = 1 << lp->exprblock.rightp->constblock.Const.ci;
  2061.             if (opcode == OPSTAR || ISICON(rp) &&
  2062.                     !(L % rp->constblock.Const.ci)) {
  2063.                 lp->exprblock.opcode = OPSTAR;
  2064.                 lp->exprblock.rightp->constblock.Const.ci = L;
  2065.                 }
  2066.             }
  2067.  
  2068.         if (lp->exprblock.opcode == OPSTAR) {
  2069.             if(opcode == OPSTAR)
  2070.                 e = mkexpr(OPSTAR, lp->exprblock.rightp, rp);
  2071.             else if(ISICON(rp) &&
  2072.                 (lp->exprblock.rightp->constblock.Const.ci %
  2073.                 rp->constblock.Const.ci) == 0)
  2074.                 e = mkexpr(OPSLASH, lp->exprblock.rightp, rp);
  2075.             else    break;
  2076.  
  2077.             e1 = lp->exprblock.leftp;
  2078.             free( (charptr) lp );
  2079.             return( mkexpr(OPSTAR, e1, e) );
  2080.             }
  2081.         break;
  2082.  
  2083.  
  2084.     case OPPLUS:
  2085.         if( ISCONST(lp) )
  2086.             COMMUTE
  2087.                 goto addop;
  2088.  
  2089.     case OPMINUS:
  2090.         if( ICONEQ(lp, 0) )
  2091.         {
  2092.             frexpr(lp);
  2093.             return( mkexpr(OPNEG, rp, ENULL) );
  2094.         }
  2095.  
  2096.         if( ISCONST(rp) && is_negatable((Constp)rp))
  2097.         {
  2098.             opcode = OPPLUS;
  2099.             consnegop((Constp)rp);
  2100.         }
  2101.  
  2102. /* Group constants in an addition expression (also subtraction, since the
  2103.    subtracted value was negated above).  In particular,
  2104.  
  2105.     (x + CONST1) + CONST2 ==> x + (CONST1 + CONST2)
  2106. */
  2107.  
  2108. addop:
  2109.         if( ISICON(rp) )
  2110.         {
  2111.             if(rp->constblock.Const.ci == 0)
  2112.                 goto retleft;
  2113.             if( ISPLUSOP(lp) && ISICON(lp->exprblock.rightp) )
  2114.             {
  2115.                 e = mkexpr(OPPLUS, lp->exprblock.rightp, rp);
  2116.                 e1 = lp->exprblock.leftp;
  2117.                 free( (charptr) lp );
  2118.                 return( mkexpr(OPPLUS, e1, e) );
  2119.             }
  2120.         }
  2121.         if (opcode == OPMINUS && (ISINT(etype) || doing_vleng)) {
  2122.             /* check for (i [+const]) - (i [+const]) */
  2123.             if (lp->tag == TPRIM)
  2124.                 e = lp;
  2125.             else if (lp->tag == TEXPR && lp->exprblock.opcode == OPPLUS
  2126.                     && lp->exprblock.rightp->tag == TCONST) {
  2127.                 e = lp->exprblock.leftp;
  2128.                 if (e->tag != TPRIM)
  2129.                     break;
  2130.                 }
  2131.             else
  2132.                 break;
  2133.             if (e->primblock.argsp)
  2134.                 break;
  2135.             if (rp->tag == TPRIM)
  2136.                 e1 = rp;
  2137.             else if (rp->tag == TEXPR && rp->exprblock.opcode == OPPLUS
  2138.                     && rp->exprblock.rightp->tag == TCONST) {
  2139.                 e1 = rp->exprblock.leftp;
  2140.                 if (e1->tag != TPRIM)
  2141.                     break;
  2142.                 }
  2143.             else
  2144.                 break;
  2145.             if (e->primblock.namep != e1->primblock.namep
  2146.                     || e1->primblock.argsp)
  2147.                 break;
  2148.             L = e == lp ? 0 : lp->exprblock.rightp->constblock.Const.ci;
  2149.             if (e1 != rp)
  2150.                 L -= rp->exprblock.rightp->constblock.Const.ci;
  2151.             frexpr(lp);
  2152.             frexpr(rp);
  2153.             return ICON(L);
  2154.             }
  2155.  
  2156.         break;
  2157.  
  2158.  
  2159.     case OPPOWER:
  2160.         break;
  2161.  
  2162. /* Eliminate outermost double negations */
  2163.  
  2164.     case OPNEG:
  2165.     case OPNEG1:
  2166.         if(ltag==TEXPR && lp->exprblock.opcode==OPNEG)
  2167.         {
  2168.             e = lp->exprblock.leftp;
  2169.             free( (charptr) lp );
  2170.             return(e);
  2171.         }
  2172.         break;
  2173.  
  2174. /* Eliminate outermost double NOTs */
  2175.  
  2176.     case OPNOT:
  2177.         if(ltag==TEXPR && lp->exprblock.opcode==OPNOT)
  2178.         {
  2179.             e = lp->exprblock.leftp;
  2180.             free( (charptr) lp );
  2181.             return(e);
  2182.         }
  2183.         break;
  2184.  
  2185.     case OPCALL:
  2186.     case OPCCALL:
  2187.         etype = ltype;
  2188.         if(rp!=NULL && rp->listblock.listp==NULL)
  2189.         {
  2190.             free( (charptr) rp );
  2191.             rp = NULL;
  2192.         }
  2193.         break;
  2194.  
  2195.     case OPAND:
  2196.     case OPOR:
  2197.         if( ISCONST(lp) )
  2198.             COMMUTE
  2199.  
  2200.                 if( ISCONST(rp) )
  2201.             {
  2202.                 if(rp->constblock.Const.ci == 0)
  2203.                     if(opcode == OPOR)
  2204.                         goto retleft;
  2205.                     else
  2206.                         goto retright;
  2207.                 else if(opcode == OPOR)
  2208.                     goto retright;
  2209.                 else
  2210.                     goto retleft;
  2211.             }
  2212.     case OPEQV:
  2213.     case OPNEQV:
  2214.  
  2215.     case OPBITAND:
  2216.     case OPBITOR:
  2217.     case OPBITXOR:
  2218.     case OPBITNOT:
  2219.     case OPLSHIFT:
  2220.     case OPRSHIFT:
  2221.  
  2222.     case OPLT:
  2223.     case OPGT:
  2224.     case OPLE:
  2225.     case OPGE:
  2226.     case OPEQ:
  2227.     case OPNE:
  2228.  
  2229.     case OPCONCAT:
  2230.         break;
  2231.     case OPMIN:
  2232.     case OPMAX:
  2233.     case OPMIN2:
  2234.     case OPMAX2:
  2235.     case OPDMIN:
  2236.     case OPDMAX:
  2237.  
  2238.     case OPASSIGN:
  2239.     case OPASSIGNI:
  2240.     case OPPLUSEQ:
  2241.     case OPSTAREQ:
  2242.     case OPMINUSEQ:
  2243.     case OPSLASHEQ:
  2244.     case OPMODEQ:
  2245.     case OPLSHIFTEQ:
  2246.     case OPRSHIFTEQ:
  2247.     case OPBITANDEQ:
  2248.     case OPBITXOREQ:
  2249.     case OPBITOREQ:
  2250.  
  2251.     case OPCONV:
  2252.     case OPADDR:
  2253.     case OPWHATSIN:
  2254.  
  2255.     case OPCOMMA:
  2256.     case OPCOMMA_ARG:
  2257.     case OPQUEST:
  2258.     case OPCOLON:
  2259.     case OPDOT:
  2260.     case OPARROW:
  2261.     case OPIDENTITY:
  2262.     case OPCHARCAST:
  2263.     case OPABS:
  2264.     case OPDABS:
  2265.         break;
  2266.  
  2267.     default:
  2268.         badop("mkexpr", opcode);
  2269.     }
  2270.  
  2271.     e = (expptr) ALLOC(Exprblock);
  2272.     e->exprblock.tag = TEXPR;
  2273.     e->exprblock.opcode = opcode;
  2274.     e->exprblock.vtype = etype;
  2275.     e->exprblock.leftp = lp;
  2276.     e->exprblock.rightp = rp;
  2277.     if(ltag==TCONST && (rp==0 || rtag==TCONST) )
  2278.         e = fold(e);
  2279.     return(e);
  2280.  
  2281. retleft:
  2282.     frexpr(rp);
  2283.     if (lp->tag == TPRIM)
  2284.         lp->primblock.parenused = 1;
  2285.     return(lp);
  2286.  
  2287. retright:
  2288.     frexpr(lp);
  2289.     if (rp->tag == TPRIM)
  2290.         rp->primblock.parenused = 1;
  2291.     return(rp);
  2292.  
  2293. error:
  2294.     frexpr(lp);
  2295.     if(rp && opcode!=OPCALL && opcode!=OPCCALL)
  2296.         frexpr(rp);
  2297.     return( errnode() );
  2298. }
  2299.  
  2300. #define ERR(s)   { errs = s; goto error; }
  2301.  
  2302. /* cktype -- Check and return the type of the expression */
  2303.  
  2304. #ifdef KR_headers
  2305. cktype(op, lt, rt)
  2306.     register int op;
  2307.     register int lt;
  2308.     register int rt;
  2309. #else
  2310. cktype(register int op, register int lt, register int rt)
  2311. #endif
  2312. {
  2313.     char *errs;
  2314.  
  2315.     if(lt==TYERROR || rt==TYERROR)
  2316.         goto error1;
  2317.  
  2318.     if(lt==TYUNKNOWN)
  2319.         return(TYUNKNOWN);
  2320.     if(rt==TYUNKNOWN)
  2321.  
  2322. /* If not unary operation, return UNKNOWN */
  2323.  
  2324.         if(!is_unary_op (op) && op != OPCALL && op != OPCCALL)
  2325.             return(TYUNKNOWN);
  2326.  
  2327.     switch(op)
  2328.     {
  2329.     case OPPLUS:
  2330.     case OPMINUS:
  2331.     case OPSTAR:
  2332.     case OPSLASH:
  2333.     case OPPOWER:
  2334.     case OPMOD:
  2335.         if( ISNUMERIC(lt) && ISNUMERIC(rt) )
  2336.             return( maxtype(lt, rt) );
  2337.         ERR("nonarithmetic operand of arithmetic operator")
  2338.  
  2339.     case OPNEG:
  2340.     case OPNEG1:
  2341.         if( ISNUMERIC(lt) )
  2342.             return(lt);
  2343.         ERR("nonarithmetic operand of negation")
  2344.  
  2345.     case OPNOT:
  2346.         if(ISLOGICAL(lt))
  2347.             return(lt);
  2348.         ERR("NOT of nonlogical")
  2349.  
  2350.     case OPAND:
  2351.     case OPOR:
  2352.     case OPEQV:
  2353.     case OPNEQV:
  2354.         if(ISLOGICAL(lt) && ISLOGICAL(rt))
  2355.             return( maxtype(lt, rt) );
  2356.         ERR("nonlogical operand of logical operator")
  2357.  
  2358.     case OPLT:
  2359.     case OPGT:
  2360.     case OPLE:
  2361.     case OPGE:
  2362.     case OPEQ:
  2363.     case OPNE:
  2364.         if(lt==TYCHAR || rt==TYCHAR || ISLOGICAL(lt) || ISLOGICAL(rt))
  2365.         {
  2366.             if(lt != rt){
  2367.                 if (htype
  2368.                     && (lt == TYCHAR && ISNUMERIC(rt)
  2369.                      || rt == TYCHAR && ISNUMERIC(lt)))
  2370.                         return TYLOGICAL;
  2371.                 ERR("illegal comparison")
  2372.                 }
  2373.         }
  2374.  
  2375.         else if( ISCOMPLEX(lt) || ISCOMPLEX(rt) )
  2376.         {
  2377.             if(op!=OPEQ && op!=OPNE)
  2378.                 ERR("order comparison of complex data")
  2379.         }
  2380.  
  2381.         else if( ! ISNUMERIC(lt) || ! ISNUMERIC(rt) )
  2382.             ERR("comparison of nonarithmetic data")
  2383.                 return(TYLOGICAL);
  2384.  
  2385.     case OPCONCAT:
  2386.         if(lt==TYCHAR && rt==TYCHAR)
  2387.             return(TYCHAR);
  2388.         ERR("concatenation of nonchar data")
  2389.  
  2390.     case OPCALL:
  2391.     case OPCCALL:
  2392.     case OPIDENTITY:
  2393.         return(lt);
  2394.  
  2395.     case OPADDR:
  2396.     case OPCHARCAST:
  2397.         return(TYADDR);
  2398.  
  2399.     case OPCONV:
  2400.         if(rt == 0)
  2401.             return(0);
  2402.         if(lt==TYCHAR && ISINT(rt) )
  2403.             return(TYCHAR);
  2404.         if (ISLOGICAL(lt) && ISLOGICAL(rt))
  2405.             return lt;
  2406.     case OPASSIGN:
  2407.     case OPASSIGNI:
  2408.     case OPMINUSEQ:
  2409.     case OPPLUSEQ:
  2410.     case OPSTAREQ:
  2411.     case OPSLASHEQ:
  2412.     case OPMODEQ:
  2413.     case OPLSHIFTEQ:
  2414.     case OPRSHIFTEQ:
  2415.     case OPBITANDEQ:
  2416.     case OPBITXOREQ:
  2417.     case OPBITOREQ:
  2418.         if( ISINT(lt) && rt==TYCHAR)
  2419.             return(lt);
  2420.         if (ISLOGICAL(lt) && ISLOGICAL(rt) && op == OPASSIGN)
  2421.             return lt;
  2422.         if(lt==TYCHAR || rt==TYCHAR || ISLOGICAL(lt) || ISLOGICAL(rt))
  2423.             if((op!=OPASSIGN && op != OPPLUSEQ && op != OPMINUSEQ)
  2424.                 || (lt!=rt))
  2425.             {
  2426.                 ERR("impossible conversion")
  2427.             }
  2428.         return(lt);
  2429.  
  2430.     case OPMIN:
  2431.     case OPMAX:
  2432.     case OPDMIN:
  2433.     case OPDMAX:
  2434.     case OPMIN2:
  2435.     case OPMAX2:
  2436.     case OPBITOR:
  2437.     case OPBITAND:
  2438.     case OPBITXOR:
  2439.     case OPBITNOT:
  2440.     case OPLSHIFT:
  2441.     case OPRSHIFT:
  2442.     case OPWHATSIN:
  2443.     case OPABS:
  2444.     case OPDABS:
  2445.         return(lt);
  2446.  
  2447.     case OPCOMMA:
  2448.     case OPCOMMA_ARG:
  2449.     case OPQUEST:
  2450.     case OPCOLON:        /* Only checks the rightmost type because
  2451.                    of C language definition (rightmost
  2452.                    comma-expr is the value of the expr) */
  2453.         return(rt);
  2454.  
  2455.     case OPDOT:
  2456.     case OPARROW:
  2457.         return (lt);
  2458.     default:
  2459.         badop("cktype", op);
  2460.     }
  2461. error:
  2462.     err(errs);
  2463. error1:
  2464.     return(TYERROR);
  2465. }
  2466.  
  2467. /* fold -- simplifies constant expressions; it assumes that e -> leftp and
  2468.    e -> rightp are TCONST or NULL */
  2469.  
  2470.  expptr
  2471. #ifdef KR_headers
  2472. fold(e)
  2473.     register expptr e;
  2474. #else
  2475. fold(register expptr e)
  2476. #endif
  2477. {
  2478.     Constp p;
  2479.     register expptr lp, rp;
  2480.     int etype, mtype, ltype, rtype, opcode;
  2481.     int i, bl, ll, lr;
  2482.     char *q, *s;
  2483.     struct Constblock lcon, rcon;
  2484.     long L;
  2485.     double d;
  2486.  
  2487.     opcode = e->exprblock.opcode;
  2488.     etype = e->exprblock.vtype;
  2489.  
  2490.     lp = e->exprblock.leftp;
  2491.     ltype = lp->headblock.vtype;
  2492.     rp = e->exprblock.rightp;
  2493.  
  2494.     if(rp == 0)
  2495.         switch(opcode)
  2496.         {
  2497.         case OPNOT:
  2498.             lp->constblock.Const.ci = ! lp->constblock.Const.ci;
  2499.  retlp:
  2500.             e->exprblock.leftp = 0;
  2501.             frexpr(e);
  2502.             return(lp);
  2503.  
  2504.         case OPBITNOT:
  2505.             lp->constblock.Const.ci = ~ lp->constblock.Const.ci;
  2506.             goto retlp;
  2507.  
  2508.         case OPNEG:
  2509.         case OPNEG1:
  2510.             consnegop((Constp)lp);
  2511.             goto retlp;
  2512.  
  2513.         case OPCONV:
  2514.         case OPADDR:
  2515.             return(e);
  2516.  
  2517.         case OPABS:
  2518.         case OPDABS:
  2519.             switch(ltype) {
  2520.                 case TYINT1:
  2521.                 case TYSHORT:
  2522.                 case TYLONG:
  2523. #ifdef TYQUAD
  2524.                 case TYQUAD:
  2525. #endif
  2526.                 if ((L = lp->constblock.Const.ci) < 0)
  2527.                     lp->constblock.Const.ci = -L;
  2528.                 goto retlp;
  2529.                 case TYREAL:
  2530.                 case TYDREAL:
  2531.                 if (lp->constblock.vstg) {
  2532.                     s = lp->constblock.Const.cds[0];
  2533.                     if (*s == '-')
  2534.                     lp->constblock.Const.cds[0] = s + 1;
  2535.                     goto retlp;
  2536.                 }
  2537.                 if ((d = lp->constblock.Const.cd[0]) < 0.)
  2538.                     lp->constblock.Const.cd[0] = -d;
  2539.                 case TYCOMPLEX:
  2540.                 case TYDCOMPLEX:
  2541.                 return e;    /* lazy way out */
  2542.                 }
  2543.         default:
  2544.             badop("fold", opcode);
  2545.         }
  2546.  
  2547.     rtype = rp->headblock.vtype;
  2548.  
  2549.     p = ALLOC(Constblock);
  2550.     p->tag = TCONST;
  2551.     p->vtype = etype;
  2552.     p->vleng = e->exprblock.vleng;
  2553.  
  2554.     switch(opcode)
  2555.     {
  2556.     case OPCOMMA:
  2557.     case OPCOMMA_ARG:
  2558.     case OPQUEST:
  2559.     case OPCOLON:
  2560.         goto ereturn;
  2561.  
  2562.     case OPAND:
  2563.         p->Const.ci = lp->constblock.Const.ci &&
  2564.             rp->constblock.Const.ci;
  2565.         break;
  2566.  
  2567.     case OPOR:
  2568.         p->Const.ci = lp->constblock.Const.ci ||
  2569.             rp->constblock.Const.ci;
  2570.         break;
  2571.  
  2572.     case OPEQV:
  2573.         p->Const.ci = lp->constblock.Const.ci ==
  2574.             rp->constblock.Const.ci;
  2575.         break;
  2576.  
  2577.     case OPNEQV:
  2578.         p->Const.ci = lp->constblock.Const.ci !=
  2579.             rp->constblock.Const.ci;
  2580.         break;
  2581.  
  2582.     case OPBITAND:
  2583.         p->Const.ci = lp->constblock.Const.ci &
  2584.             rp->constblock.Const.ci;
  2585.         break;
  2586.  
  2587.     case OPBITOR:
  2588.         p->Const.ci = lp->constblock.Const.ci |
  2589.             rp->constblock.Const.ci;
  2590.         break;
  2591.  
  2592.     case OPBITXOR:
  2593.         p->Const.ci = lp->constblock.Const.ci ^
  2594.             rp->constblock.Const.ci;
  2595.         break;
  2596.  
  2597.     case OPLSHIFT:
  2598.         p->Const.ci = lp->constblock.Const.ci <<
  2599.             rp->constblock.Const.ci;
  2600.         break;
  2601.  
  2602.     case OPRSHIFT:
  2603.         p->Const.ci = lp->constblock.Const.ci >>
  2604.             rp->constblock.Const.ci;
  2605.         break;
  2606.  
  2607.     case OPCONCAT:
  2608.         ll = lp->constblock.vleng->constblock.Const.ci;
  2609.         lr = rp->constblock.vleng->constblock.Const.ci;
  2610.         bl = lp->constblock.Const.ccp1.blanks;
  2611.         p->Const.ccp = q = (char *) ckalloc(ll+lr+bl);
  2612.         p->Const.ccp1.blanks = rp->constblock.Const.ccp1.blanks;
  2613.         p->vleng = ICON(ll+lr+bl);
  2614.         s = lp->constblock.Const.ccp;
  2615.         for(i = 0 ; i < ll ; ++i)
  2616.             *q++ = *s++;
  2617.         for(i = 0 ; i < bl ; i++)
  2618.             *q++ = ' ';
  2619.         s = rp->constblock.Const.ccp;
  2620.         for(i = 0; i < lr; ++i)
  2621.             *q++ = *s++;
  2622.         break;
  2623.  
  2624.  
  2625.     case OPPOWER:
  2626.         if( !ISINT(rtype)
  2627.          || rp->constblock.Const.ci < 0 && zeroconst(lp))
  2628.             goto ereturn;
  2629.         conspower(p, (Constp)lp, rp->constblock.Const.ci);
  2630.         break;
  2631.  
  2632.     case OPSLASH:
  2633.         if (zeroconst(rp))
  2634.             goto ereturn;
  2635.         /* no break */
  2636.  
  2637.     default:
  2638.         if(ltype == TYCHAR)
  2639.         {
  2640.             lcon.Const.ci = cmpstr(lp->constblock.Const.ccp,
  2641.                 rp->constblock.Const.ccp,
  2642.                 lp->constblock.vleng->constblock.Const.ci,
  2643.                 rp->constblock.vleng->constblock.Const.ci);
  2644.             rcon.Const.ci = 0;
  2645.             mtype = tyint;
  2646.         }
  2647.         else    {
  2648.             mtype = maxtype(ltype, rtype);
  2649.             consconv(mtype, &lcon, &lp->constblock);
  2650.             consconv(mtype, &rcon, &rp->constblock);
  2651.         }
  2652.         consbinop(opcode, mtype, p, &lcon, &rcon);
  2653.         break;
  2654.     }
  2655.  
  2656.     frexpr(e);
  2657.     return( (expptr) p );
  2658.  ereturn:
  2659.     free((char *)p);
  2660.     return e;
  2661. }
  2662.  
  2663.  
  2664.  
  2665. /* assign constant l = r , doing coercion */
  2666.  
  2667.  void
  2668. #ifdef KR_headers
  2669. consconv(lt, lc, rc)
  2670.     int lt;
  2671.     register Constp lc;
  2672.     register Constp rc;
  2673. #else
  2674. consconv(int lt, register Constp lc, register Constp rc)
  2675. #endif
  2676. {
  2677.     int rt = rc->vtype;
  2678.     register union Constant *lv = &lc->Const, *rv = &rc->Const;
  2679.  
  2680.     lc->vtype = lt;
  2681.     if (ONEOF(lt, MSKREAL|MSKCOMPLEX) && ONEOF(rt, MSKREAL|MSKCOMPLEX)) {
  2682.         memcpy((char *)lv, (char *)rv, sizeof(union Constant));
  2683.         lc->vstg = rc->vstg;
  2684.         if (ISCOMPLEX(lt) && ISREAL(rt)) {
  2685.             if (rc->vstg)
  2686.                 lv->cds[1] = cds("0",CNULL);
  2687.             else
  2688.                 lv->cd[1] = 0.;
  2689.             }
  2690.         return;
  2691.         }
  2692.     lc->vstg = 0;
  2693.  
  2694.     switch(lt)
  2695.     {
  2696.  
  2697. /* Casting to character means just copying the first sizeof (character)
  2698.    bytes into a new 1 character string.  This is weird. */
  2699.  
  2700.     case TYCHAR:
  2701.         *(lv->ccp = (char *) ckalloc(1)) = rv->ci;
  2702.         lv->ccp1.blanks = 0;
  2703.         break;
  2704.  
  2705.     case TYINT1:
  2706.     case TYSHORT:
  2707.     case TYLONG:
  2708. #ifdef TYQUAD
  2709.     case TYQUAD:
  2710. #endif
  2711.         if(rt == TYCHAR)
  2712.             lv->ci = rv->ccp[0];
  2713.         else if( ISINT(rt) )
  2714.             lv->ci = rv->ci;
  2715.         else    lv->ci = rc->vstg ? atof(rv->cds[0]) : rv->cd[0];
  2716.  
  2717.         break;
  2718.  
  2719.     case TYCOMPLEX:
  2720.     case TYDCOMPLEX:
  2721.         lv->cd[1] = 0.;
  2722.         lv->cd[0] = rv->ci;
  2723.         break;
  2724.  
  2725.     case TYREAL:
  2726.     case TYDREAL:
  2727.         lv->cd[0] = rv->ci;
  2728.         break;
  2729.  
  2730.     case TYLOGICAL:
  2731.     case TYLOGICAL1:
  2732.     case TYLOGICAL2:
  2733.         lv->ci = rv->ci;
  2734.         break;
  2735.     }
  2736. }
  2737.  
  2738.  
  2739.  
  2740. /* Negate constant value -- changes the input node's value */
  2741.  
  2742.  void
  2743. #ifdef KR_headers
  2744. consnegop(p)
  2745.     register Constp p;
  2746. #else
  2747. consnegop(register Constp p)
  2748. #endif
  2749. {
  2750.     register char *s;
  2751.  
  2752.     if (p->vstg) {
  2753.         if (ISCOMPLEX(p->vtype)) {
  2754.             s = p->Const.cds[1];
  2755.             p->Const.cds[1] = *s == '-' ? s+1
  2756.                     : *s == '0' ? s : s-1;
  2757.             }
  2758.         s = p->Const.cds[0];
  2759.         p->Const.cds[0] = *s == '-' ? s+1
  2760.                 : *s == '0' ? s : s-1;
  2761.         return;
  2762.         }
  2763.     switch(p->vtype)
  2764.     {
  2765.     case TYINT1:
  2766.     case TYSHORT:
  2767.     case TYLONG:
  2768. #ifdef TYQUAD
  2769.     case TYQUAD:
  2770. #endif
  2771.         p->Const.ci = - p->Const.ci;
  2772.         break;
  2773.  
  2774.     case TYCOMPLEX:
  2775.     case TYDCOMPLEX:
  2776.         p->Const.cd[1] = - p->Const.cd[1];
  2777.         /* fall through and do the real parts */
  2778.     case TYREAL:
  2779.     case TYDREAL:
  2780.         p->Const.cd[0] = - p->Const.cd[0];
  2781.         break;
  2782.     default:
  2783.         badtype("consnegop", p->vtype);
  2784.     }
  2785. }
  2786.  
  2787.  
  2788.  
  2789. /* conspower -- Expand out an exponentiation */
  2790.  
  2791.  LOCAL void
  2792. #ifdef KR_headers
  2793. conspower(p, ap, n)
  2794.     Constp p;
  2795.     Constp ap;
  2796.     ftnint n;
  2797. #else
  2798. conspower(Constp p, Constp ap, ftnint n)
  2799. #endif
  2800. {
  2801.     register union Constant *powp = &p->Const;
  2802.     register int type;
  2803.     struct Constblock x, x0;
  2804.  
  2805.     if (n == 1) {
  2806.         memcpy((char *)powp, (char *)&ap->Const, sizeof(ap->Const));
  2807.         return;
  2808.         }
  2809.  
  2810.     switch(type = ap->vtype)    /* pow = 1 */
  2811.     {
  2812.     case TYINT1:
  2813.     case TYSHORT:
  2814.     case TYLONG:
  2815. #ifdef TYQUAD
  2816.     case TYQUAD:
  2817. #endif
  2818.         powp->ci = 1;
  2819.         break;
  2820.     case TYCOMPLEX:
  2821.     case TYDCOMPLEX:
  2822.         powp->cd[1] = 0;
  2823.     case TYREAL:
  2824.     case TYDREAL:
  2825.         powp->cd[0] = 1;
  2826.         break;
  2827.     default:
  2828.         badtype("conspower", type);
  2829.     }
  2830.  
  2831.     if(n == 0)
  2832.         return;
  2833.     switch(type)    /* x0 = ap */
  2834.     {
  2835.     case TYINT1:
  2836.     case TYSHORT:
  2837.     case TYLONG:
  2838. #ifdef TYQUAD
  2839.     case TYQUAD:
  2840. #endif
  2841.         x0.Const.ci = ap->Const.ci;
  2842.         break;
  2843.     case TYCOMPLEX:
  2844.     case TYDCOMPLEX:
  2845.         x0.Const.cd[1] =
  2846.             ap->vstg ? atof(ap->Const.cds[1]) : ap->Const.cd[1];
  2847.     case TYREAL:
  2848.     case TYDREAL:
  2849.         x0.Const.cd[0] =
  2850.             ap->vstg ? atof(ap->Const.cds[0]) : ap->Const.cd[0];
  2851.         break;
  2852.     }
  2853.     x0.vtype = type;
  2854.     x0.vstg = 0;
  2855.     if(n < 0)
  2856.     {
  2857.         if( ISINT(type) )
  2858.         {
  2859.             err("integer ** negative number");
  2860.             return;
  2861.         }
  2862.         else if (!x0.Const.cd[0]
  2863.                 && (!ISCOMPLEX(type) || !x0.Const.cd[1])) {
  2864.             err("0.0 ** negative number");
  2865.             return;
  2866.             }
  2867.         n = -n;
  2868.         consbinop(OPSLASH, type, &x, p, &x0);
  2869.     }
  2870.     else
  2871.         consbinop(OPSTAR, type, &x, p, &x0);
  2872.  
  2873.     for( ; ; )
  2874.     {
  2875.         if(n & 01)
  2876.             consbinop(OPSTAR, type, p, p, &x);
  2877.         if(n >>= 1)
  2878.             consbinop(OPSTAR, type, &x, &x, &x);
  2879.         else
  2880.             break;
  2881.     }
  2882. }
  2883.  
  2884.  
  2885.  
  2886. /* do constant operation cp = a op b -- assumes that   ap and bp   have data
  2887.    matching the input   type */
  2888.  
  2889.  LOCAL void
  2890. #ifdef KR_headers
  2891. consbinop(opcode, type, cpp, app, bpp)
  2892.     int opcode;
  2893.     int type;
  2894.     Constp cpp;
  2895.     Constp app;
  2896.     Constp bpp;
  2897. #else
  2898. consbinop(int opcode, int type, Constp cpp, Constp app, Constp bpp)
  2899. #endif
  2900. {
  2901.     register union Constant *ap = &app->Const,
  2902.                 *bp = &bpp->Const,
  2903.                 *cp = &cpp->Const;
  2904.     int k;
  2905.     double ad[2], bd[2], temp;
  2906.  
  2907.     cpp->vstg = 0;
  2908.  
  2909.     if (ONEOF(type, MSKREAL|MSKCOMPLEX)) {
  2910.         ad[0] = app->vstg ? atof(ap->cds[0]) : ap->cd[0];
  2911.         bd[0] = bpp->vstg ? atof(bp->cds[0]) : bp->cd[0];
  2912.         if (ISCOMPLEX(type)) {
  2913.             ad[1] = app->vstg ? atof(ap->cds[1]) : ap->cd[1];
  2914.             bd[1] = bpp->vstg ? atof(bp->cds[1]) : bp->cd[1];
  2915.             }
  2916.         }
  2917.     switch(opcode)
  2918.     {
  2919.     case OPPLUS:
  2920.         switch(type)
  2921.         {
  2922.         case TYINT1:
  2923.         case TYSHORT:
  2924.         case TYLONG:
  2925. #ifdef TYQUAD
  2926.         case TYQUAD:
  2927. #endif
  2928.             cp->ci = ap->ci + bp->ci;
  2929.             break;
  2930.         case TYCOMPLEX:
  2931.         case TYDCOMPLEX:
  2932.             cp->cd[1] = ad[1] + bd[1];
  2933.         case TYREAL:
  2934.         case TYDREAL:
  2935.             cp->cd[0] = ad[0] + bd[0];
  2936.             break;
  2937.         }
  2938.         break;
  2939.  
  2940.     case OPMINUS:
  2941.         switch(type)
  2942.         {
  2943.         case TYINT1:
  2944.         case TYSHORT:
  2945.         case TYLONG:
  2946. #ifdef TYQUAD
  2947.         case TYQUAD:
  2948. #endif
  2949.             cp->ci = ap->ci - bp->ci;
  2950.             break;
  2951.         case TYCOMPLEX:
  2952.         case TYDCOMPLEX:
  2953.             cp->cd[1] = ad[1] - bd[1];
  2954.         case TYREAL:
  2955.         case TYDREAL:
  2956.             cp->cd[0] = ad[0] - bd[0];
  2957.             break;
  2958.         }
  2959.         break;
  2960.  
  2961.     case OPSTAR:
  2962.         switch(type)
  2963.         {
  2964.         case TYINT1:
  2965.         case TYSHORT:
  2966.         case TYLONG:
  2967. #ifdef TYQUAD
  2968.         case TYQUAD:
  2969. #endif
  2970.             cp->ci = ap->ci * bp->ci;
  2971.             break;
  2972.         case TYREAL:
  2973.         case TYDREAL:
  2974.             cp->cd[0] = ad[0] * bd[0];
  2975.             break;
  2976.         case TYCOMPLEX:
  2977.         case TYDCOMPLEX:
  2978.             temp = ad[0] * bd[0]  -  ad[1] * bd[1] ;
  2979.             cp->cd[1] = ad[0] * bd[1]  +  ad[1] * bd[0] ;
  2980.             cp->cd[0] = temp;
  2981.             break;
  2982.         }
  2983.         break;
  2984.     case OPSLASH:
  2985.         switch(type)
  2986.         {
  2987.         case TYINT1:
  2988.         case TYSHORT:
  2989.         case TYLONG:
  2990. #ifdef TYQUAD
  2991.         case TYQUAD:
  2992. #endif
  2993.             cp->ci = ap->ci / bp->ci;
  2994.             break;
  2995.         case TYREAL:
  2996.         case TYDREAL:
  2997.             cp->cd[0] = ad[0] / bd[0];
  2998.             break;
  2999.         case TYCOMPLEX:
  3000.         case TYDCOMPLEX:
  3001.             zdiv((dcomplex*)cp, (dcomplex*)ad, (dcomplex*)bd);
  3002.             break;
  3003.         }
  3004.         break;
  3005.  
  3006.     case OPMOD:
  3007.         if( ISINT(type) )
  3008.         {
  3009.             cp->ci = ap->ci % bp->ci;
  3010.             break;
  3011.         }
  3012.         else
  3013.             Fatal("inline mod of noninteger");
  3014.  
  3015.     case OPMIN2:
  3016.     case OPDMIN:
  3017.         switch(type)
  3018.         {
  3019.         case TYINT1:
  3020.         case TYSHORT:
  3021.         case TYLONG:
  3022. #ifdef TYQUAD
  3023.         case TYQUAD:
  3024. #endif
  3025.             cp->ci = ap->ci <= bp->ci ? ap->ci : bp->ci;
  3026.             break;
  3027.         case TYREAL:
  3028.         case TYDREAL:
  3029.             cp->cd[0] = ad[0] <= bd[0] ? ad[0] : bd[0];
  3030.             break;
  3031.         default:
  3032.             Fatal("inline min of exected type");
  3033.         }
  3034.         break;
  3035.  
  3036.     case OPMAX2:
  3037.     case OPDMAX:
  3038.         switch(type)
  3039.         {
  3040.         case TYINT1:
  3041.         case TYSHORT:
  3042.         case TYLONG:
  3043. #ifdef TYQUAD
  3044.         case TYQUAD:
  3045. #endif
  3046.             cp->ci = ap->ci >= bp->ci ? ap->ci : bp->ci;
  3047.             break;
  3048.         case TYREAL:
  3049.         case TYDREAL:
  3050.             cp->cd[0] = ad[0] >= bd[0] ? ad[0] : bd[0];
  3051.             break;
  3052.         default:
  3053.             Fatal("inline max of exected type");
  3054.         }
  3055.         break;
  3056.  
  3057.     default:      /* relational ops */
  3058.         switch(type)
  3059.         {
  3060.         case TYINT1:
  3061.         case TYSHORT:
  3062.         case TYLONG:
  3063. #ifdef TYQUAD
  3064.         case TYQUAD:
  3065. #endif
  3066.             if(ap->ci < bp->ci)
  3067.                 k = -1;
  3068.             else if(ap->ci == bp->ci)
  3069.                 k = 0;
  3070.             else    k = 1;
  3071.             break;
  3072.         case TYREAL:
  3073.         case TYDREAL:
  3074.             if(ad[0] < bd[0])
  3075.                 k = -1;
  3076.             else if(ad[0] == bd[0])
  3077.                 k = 0;
  3078.             else    k = 1;
  3079.             break;
  3080.         case TYCOMPLEX:
  3081.         case TYDCOMPLEX:
  3082.             if(ad[0] == bd[0] &&
  3083.                 ad[1] == bd[1] )
  3084.                 k = 0;
  3085.             else    k = 1;
  3086.             break;
  3087.         }
  3088.  
  3089.         switch(opcode)
  3090.         {
  3091.         case OPEQ:
  3092.             cp->ci = (k == 0);
  3093.             break;
  3094.         case OPNE:
  3095.             cp->ci = (k != 0);
  3096.             break;
  3097.         case OPGT:
  3098.             cp->ci = (k == 1);
  3099.             break;
  3100.         case OPLT:
  3101.             cp->ci = (k == -1);
  3102.             break;
  3103.         case OPGE:
  3104.             cp->ci = (k >= 0);
  3105.             break;
  3106.         case OPLE:
  3107.             cp->ci = (k <= 0);
  3108.             break;
  3109.         }
  3110.         break;
  3111.     }
  3112. }
  3113.  
  3114.  
  3115.  
  3116. /* conssgn - returns the sign of a Fortran constant */
  3117.  
  3118. #ifdef KR_headers
  3119. conssgn(p)
  3120.     register expptr p;
  3121. #else
  3122. conssgn(register expptr p)
  3123. #endif
  3124. {
  3125.     register char *s;
  3126.  
  3127.     if( ! ISCONST(p) )
  3128.         Fatal( "sgn(nonconstant)" );
  3129.  
  3130.     switch(p->headblock.vtype)
  3131.     {
  3132.     case TYINT1:
  3133.     case TYSHORT:
  3134.     case TYLONG:
  3135. #ifdef TYQUAD
  3136.     case TYQUAD:
  3137. #endif
  3138.         if(p->constblock.Const.ci > 0) return(1);
  3139.         if(p->constblock.Const.ci < 0) return(-1);
  3140.         return(0);
  3141.  
  3142.     case TYREAL:
  3143.     case TYDREAL:
  3144.         if (p->constblock.vstg) {
  3145.             s = p->constblock.Const.cds[0];
  3146.             if (*s == '-')
  3147.                 return -1;
  3148.             if (*s == '0')
  3149.                 return 0;
  3150.             return 1;
  3151.             }
  3152.         if(p->constblock.Const.cd[0] > 0) return(1);
  3153.         if(p->constblock.Const.cd[0] < 0) return(-1);
  3154.         return(0);
  3155.  
  3156.  
  3157. /* The sign of a complex number is 0 iff the number is 0 + 0i, else it's 1 */
  3158.  
  3159.     case TYCOMPLEX:
  3160.     case TYDCOMPLEX:
  3161.         if (p->constblock.vstg)
  3162.             return *p->constblock.Const.cds[0] != '0'
  3163.                 && *p->constblock.Const.cds[1] != '0';
  3164.         return(p->constblock.Const.cd[0]!=0 || p->constblock.Const.cd[1]!=0);
  3165.  
  3166.     default:
  3167.         badtype( "conssgn", p->constblock.vtype);
  3168.     }
  3169.     /* NOT REACHED */ return 0;
  3170. }
  3171.  
  3172. char *powint[ ] = {
  3173.     "pow_ii",
  3174. #ifdef TYQUAD
  3175.           "pow_qi",
  3176. #endif
  3177.           "pow_ri", "pow_di", "pow_ci", "pow_zi" };
  3178.  
  3179.  LOCAL expptr
  3180. #ifdef KR_headers
  3181. mkpower(p)
  3182.     register expptr p;
  3183. #else
  3184. mkpower(register expptr p)
  3185. #endif
  3186. {
  3187.     register expptr q, lp, rp;
  3188.     int ltype, rtype, mtype, tyi;
  3189.  
  3190.     lp = p->exprblock.leftp;
  3191.     rp = p->exprblock.rightp;
  3192.     ltype = lp->headblock.vtype;
  3193.     rtype = rp->headblock.vtype;
  3194.  
  3195.     if (lp->tag == TADDR)
  3196.         lp->addrblock.parenused = 0;
  3197.  
  3198.     if (rp->tag == TADDR)
  3199.         rp->addrblock.parenused = 0;
  3200.  
  3201.     if(ISICON(rp))
  3202.     {
  3203.         if(rp->constblock.Const.ci == 0)
  3204.         {
  3205.             frexpr(p);
  3206.             if( ISINT(ltype) )
  3207.                 return( ICON(1) );
  3208.             else if (ISREAL (ltype))
  3209.                 return mkconv (ltype, ICON (1));
  3210.             else
  3211.                 return( (expptr) putconst((Constp)
  3212.                     mkconv(ltype, ICON(1))) );
  3213.         }
  3214.         if(rp->constblock.Const.ci < 0)
  3215.         {
  3216.             if( ISINT(ltype) )
  3217.             {
  3218.                 frexpr(p);
  3219.                 err("integer**negative");
  3220.                 return( errnode() );
  3221.             }
  3222.             rp->constblock.Const.ci = - rp->constblock.Const.ci;
  3223.             p->exprblock.leftp = lp
  3224.                 = fixexpr((Exprp)mkexpr(OPSLASH, ICON(1), lp));
  3225.         }
  3226.         if(rp->constblock.Const.ci == 1)
  3227.         {
  3228.             frexpr(rp);
  3229.             free( (charptr) p );
  3230.             return(lp);
  3231.         }
  3232.  
  3233.         if( ONEOF(ltype, MSKINT|MSKREAL) ) {
  3234.             p->exprblock.vtype = ltype;
  3235.             return(p);
  3236.         }
  3237.     }
  3238.     if( ISINT(rtype) )
  3239.     {
  3240.         if(ltype==TYSHORT && rtype==TYSHORT && (!ISCONST(lp) || tyint==TYSHORT) )
  3241.             q = call2(TYSHORT, "pow_hh", lp, rp);
  3242.         else    {
  3243.             if(ONEOF(ltype,M(TYINT1)|M(TYSHORT)))
  3244.             {
  3245.                 ltype = TYLONG;
  3246.                 lp = mkconv(TYLONG,lp);
  3247.             }
  3248. #ifdef TYQUAD
  3249.             if (ltype == TYQUAD)
  3250.                 rp = mkconv(TYQUAD,rp);
  3251.             else
  3252. #endif
  3253.             rp = mkconv(TYLONG,rp);
  3254.             if (ISCONST(rp)) {
  3255.                 tyi = tyint;
  3256.                 tyint = TYLONG;
  3257.                 rp = (expptr)putconst((Constp)rp);
  3258.                 tyint = tyi;
  3259.                 }
  3260.             q = call2(ltype, powint[ltype-TYLONG], lp, rp);
  3261.         }
  3262.     }
  3263.     else if( ISREAL( (mtype = maxtype(ltype,rtype)) )) {
  3264.         extern int callk_kludge;
  3265.         callk_kludge = TYDREAL;
  3266.         q = call2(mtype, "pow_dd", mkconv(TYDREAL,lp), mkconv(TYDREAL,rp));
  3267.         callk_kludge = 0;
  3268.         }
  3269.     else    {
  3270.         q  = call2(TYDCOMPLEX, "pow_zz",
  3271.             mkconv(TYDCOMPLEX,lp), mkconv(TYDCOMPLEX,rp));
  3272.         if(mtype == TYCOMPLEX)
  3273.             q = mkconv(TYCOMPLEX, q);
  3274.     }
  3275.     free( (charptr) p );
  3276.     return(q);
  3277. }
  3278.  
  3279.  
  3280. /* Complex Division.  Same code as in Runtime Library
  3281. */
  3282.  
  3283.  
  3284.  LOCAL void
  3285. #ifdef KR_headers
  3286. zdiv(c, a, b)
  3287.     register dcomplex *c;
  3288.     register dcomplex *a;
  3289.     register dcomplex *b;
  3290. #else
  3291. zdiv(register dcomplex *c, register dcomplex *a, register dcomplex *b)
  3292. #endif
  3293. {
  3294.     double ratio, den;
  3295.     double abr, abi;
  3296.  
  3297.     if( (abr = b->dreal) < 0.)
  3298.         abr = - abr;
  3299.     if( (abi = b->dimag) < 0.)
  3300.         abi = - abi;
  3301.     if( abr <= abi )
  3302.     {
  3303.         if(abi == 0)
  3304.             Fatal("complex division by zero");
  3305.         ratio = b->dreal / b->dimag ;
  3306.         den = b->dimag * (1 + ratio*ratio);
  3307.         c->dreal = (a->dreal*ratio + a->dimag) / den;
  3308.         c->dimag = (a->dimag*ratio - a->dreal) / den;
  3309.     }
  3310.  
  3311.     else
  3312.     {
  3313.         ratio = b->dimag / b->dreal ;
  3314.         den = b->dreal * (1 + ratio*ratio);
  3315.         c->dreal = (a->dreal + a->dimag*ratio) / den;
  3316.         c->dimag = (a->dimag - a->dreal*ratio) / den;
  3317.     }
  3318. }
  3319.