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