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

  1. /****************************************************************
  2. Copyright 1990, 1991 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. /* INTERMEDIATE CODE GENERATION FOR S. C. JOHNSON C COMPILERS */
  25. /* NEW VERSION USING BINARY POLISH POSTFIX INTERMEDIATE */
  26.  
  27. #include "defs.h"
  28. #include "pccdefs.h"
  29. #include "output.h"        /* for nice_printf */
  30. #include "names.h"
  31. #include "p1defs.h"
  32.  
  33. Addrp realpart();
  34. LOCAL Addrp intdouble(), putcx1(), putcxeq (), putch1 ();
  35. LOCAL putct1 ();
  36.  
  37. expptr putcxop();
  38. LOCAL expptr putcall (), putmnmx (), putcheq(), putcat ();
  39. LOCAL expptr putaddr(), putchcmp (), putpower(), putop();
  40. LOCAL expptr putcxcmp ();
  41. expptr imagpart();
  42. ftnint lencat();
  43.  
  44. #define FOUR 4
  45. extern int ops2[];
  46. extern int types2[];
  47. extern int proc_argchanges, proc_protochanges;
  48. extern int krparens;
  49.  
  50. #define P2BUFFMAX 128
  51.  
  52. /* Puthead -- output the header information about subroutines, functions
  53.    and entry points */
  54.  
  55. puthead(s, class)
  56. char *s;
  57. int class;
  58. {
  59.     if (headerdone == NO) {
  60.         if (class == CLMAIN)
  61.             s = "MAIN__";
  62.         p1_head (class, s);
  63.         headerdone = YES;
  64.         }
  65. }
  66.  
  67. putif(p, else_if_p)
  68.  register expptr p;
  69.  int else_if_p;
  70. {
  71.     register int k;
  72.     int n;
  73.     long where;
  74.  
  75.     if (else_if_p) {
  76.         p1put(P1_ELSEIFSTART);
  77.         where = ftell(pass1_file);
  78.         }
  79.     if( ( k = (p = fixtype(p))->headblock.vtype) != TYLOGICAL)
  80.     {
  81.         if(k != TYERROR)
  82.             err("non-logical expression in IF statement");
  83.         }
  84.     else {
  85.         if (else_if_p) {
  86.             if (ei_next >= ei_last)
  87.                 {
  88.                 k = ei_last - ei_first;
  89.                 n = k + 100;
  90.                 ei_next = mem(n,0);
  91.                 ei_last = ei_first + n;
  92.                 if (k)
  93.                     memcpy(ei_next, ei_first, k);
  94.                 ei_first =  ei_next;
  95.                 ei_next += k;
  96.                 ei_last = ei_first + n;
  97.                 }
  98.             p = putx(p);
  99.             if (*ei_next++ = ftell(pass1_file) > where) {
  100.                 p1_if(p);
  101.                 new_endif();
  102.                 }
  103.             else
  104.                 p1_elif(p);
  105.             }
  106.         else {
  107.             p = putx(p);
  108.             p1_if(p);
  109.             }
  110.         }
  111.     }
  112.  
  113.  
  114. putout(p)
  115. expptr p;
  116. {
  117.     p1_expr (p);
  118.  
  119. /* Used to make temporaries in holdtemps available here, but they */
  120. /* may be reused too soon (e.g. when multiple **'s are involved). */
  121. }
  122.  
  123.  
  124.  
  125. putcmgo(index, nlab, labs)
  126. expptr index;
  127. int nlab;
  128. struct Labelblock *labs[];
  129. {
  130.     if(! ISINT(index->headblock.vtype) )
  131.     {
  132.         execerr("computed goto index must be integer", CNULL);
  133.         return;
  134.     }
  135.  
  136.     p1comp_goto (index, nlab, labs);
  137. }
  138.  
  139.  static expptr
  140. krput(p)
  141.  register expptr p;
  142. {
  143.     register expptr e, e1;
  144.     register unsigned op;
  145.     int t = krparens == 2 ? TYDREAL : p->exprblock.vtype;
  146.  
  147.     op = p->exprblock.opcode;
  148.     e = p->exprblock.leftp;
  149.     if (e->tag == TEXPR && e->exprblock.opcode == op) {
  150.         e1 = (expptr)mktmp(t, ENULL);
  151.         putout(putassign(cpexpr(e1), e));
  152.         p->exprblock.leftp = e1;
  153.         }
  154.     else
  155.         p->exprblock.leftp = putx(e);
  156.  
  157.     e = p->exprblock.rightp;
  158.     if (e->tag == TEXPR && e->exprblock.opcode == op) {
  159.         e1 = (expptr)mktmp(t, ENULL);
  160.         putout(putassign(cpexpr(e1), e));
  161.         p->exprblock.rightp = e1;
  162.         }
  163.     else
  164.         p->exprblock.rightp = putx(e);
  165.     return p;
  166.     }
  167.  
  168. expptr putx(p)
  169.  register expptr p;
  170. {
  171.     int opc;
  172.     int k;
  173.  
  174.     if (p)
  175.       switch(p->tag)
  176.     {
  177.     case TERROR:
  178.         break;
  179.  
  180.     case TCONST:
  181.         switch(p->constblock.vtype)
  182.         {
  183.         case TYLOGICAL:
  184.         case TYLONG:
  185.         case TYSHORT:
  186.             break;
  187.  
  188.         case TYADDR:
  189.             break;
  190.         case TYREAL:
  191.         case TYDREAL:
  192.  
  193. /* Don't write it out to the p2 file, since you'd need to call putconst,
  194.    which is just what we need to avoid in the translator */
  195.  
  196.             break;
  197.         default:
  198.             p = putx( (expptr)putconst((Constp)p) );
  199.             break;
  200.         }
  201.         break;
  202.  
  203.     case TEXPR:
  204.         switch(opc = p->exprblock.opcode)
  205.         {
  206.         case OPCALL:
  207.         case OPCCALL:
  208.             if( ISCOMPLEX(p->exprblock.vtype) )
  209.                 p = putcxop(p);
  210.             else    p = putcall(p, (Addrp *)NULL);
  211.             break;
  212.  
  213.         case OPMIN:
  214.         case OPMAX:
  215.             p = putmnmx(p);
  216.             break;
  217.  
  218.  
  219.         case OPASSIGN:
  220.             if(ISCOMPLEX(p->exprblock.leftp->headblock.vtype)
  221.                 || ISCOMPLEX(p->exprblock.rightp->headblock.vtype)) {
  222.                 (void) putcxeq(p);
  223.                 p = ENULL;
  224.             } else if( ISCHAR(p) )
  225.                 p = putcheq(p);
  226.             else
  227.                 goto putopp;
  228.             break;
  229.  
  230.         case OPEQ:
  231.         case OPNE:
  232.             if( ISCOMPLEX(p->exprblock.leftp->headblock.vtype) ||
  233.                 ISCOMPLEX(p->exprblock.rightp->headblock.vtype) )
  234.             {
  235.                 p = putcxcmp(p);
  236.                 break;
  237.             }
  238.         case OPLT:
  239.         case OPLE:
  240.         case OPGT:
  241.         case OPGE:
  242.             if(ISCHAR(p->exprblock.leftp))
  243.             {
  244.                 p = putchcmp(p);
  245.                 break;
  246.             }
  247.             goto putopp;
  248.  
  249.         case OPPOWER:
  250.             p = putpower(p);
  251.             break;
  252.  
  253.         case OPSTAR:
  254.             /*   m * (2**k) -> m<<k   */
  255.             if(INT(p->exprblock.leftp->headblock.vtype) &&
  256.                 ISICON(p->exprblock.rightp) &&
  257.                 ( (k = log_2(p->exprblock.rightp->constblock.Const.ci))>0) )
  258.             {
  259.                 p->exprblock.opcode = OPLSHIFT;
  260.                 frexpr(p->exprblock.rightp);
  261.                 p->exprblock.rightp = ICON(k);
  262.                 goto putopp;
  263.             }
  264.             if (krparens && ISREAL(p->exprblock.vtype))
  265.                 return krput(p);
  266.  
  267.         case OPMOD:
  268.             goto putopp;
  269.         case OPPLUS:
  270.             if (krparens && ISREAL(p->exprblock.vtype))
  271.                 return krput(p);
  272.         case OPMINUS:
  273.         case OPSLASH:
  274.         case OPNEG:
  275.         case OPNEG1:
  276.         case OPABS:
  277.         case OPDABS:
  278.             if( ISCOMPLEX(p->exprblock.vtype) )
  279.                 p = putcxop(p);
  280.             else    goto putopp;
  281.             break;
  282.  
  283.         case OPCONV:
  284.             if( ISCOMPLEX(p->exprblock.vtype) )
  285.                 p = putcxop(p);
  286.             else if( ISCOMPLEX(p->exprblock.leftp->headblock.vtype) )
  287.             {
  288.                 p = putx( mkconv(p->exprblock.vtype,
  289.                     (expptr)realpart(putcx1(p->exprblock.leftp))));
  290.             }
  291.             else    goto putopp;
  292.             break;
  293.  
  294.         case OPNOT:
  295.         case OPOR:
  296.         case OPAND:
  297.         case OPEQV:
  298.         case OPNEQV:
  299.         case OPADDR:
  300.         case OPPLUSEQ:
  301.         case OPSTAREQ:
  302.         case OPCOMMA:
  303.         case OPQUEST:
  304.         case OPCOLON:
  305.         case OPBITOR:
  306.         case OPBITAND:
  307.         case OPBITXOR:
  308.         case OPBITNOT:
  309.         case OPLSHIFT:
  310.         case OPRSHIFT:
  311.         case OPASSIGNI:
  312.         case OPIDENTITY:
  313.         case OPCHARCAST:
  314.         case OPMIN2:
  315.         case OPMAX2:
  316.         case OPDMIN:
  317.         case OPDMAX:
  318. putopp:
  319.             p = putop(p);
  320.             break;
  321.  
  322.         case OPCONCAT:
  323.             /* weird things like ichar(a//a) */
  324.             p = (expptr)putch1(p);
  325.             break;
  326.  
  327.         default:
  328.             badop("putx", opc);
  329.             p = errnode ();
  330.         }
  331.         break;
  332.  
  333.     case TADDR:
  334.         p = putaddr(p);
  335.         break;
  336.  
  337.     default:
  338.         badtag("putx", p->tag);
  339.         p = errnode ();
  340.     }
  341.  
  342.     return p;
  343. }
  344.  
  345.  
  346.  
  347. LOCAL expptr putop(p)
  348. expptr p;
  349. {
  350.     expptr lp, tp;
  351.     int pt, lt, lt1;
  352.     int comma;
  353.  
  354.     switch(p->exprblock.opcode)    /* check for special cases and rewrite */
  355.     {
  356.     case OPCONV:
  357.         pt = p->exprblock.vtype;
  358.         lp = p->exprblock.leftp;
  359.         lt = lp->headblock.vtype;
  360.  
  361. /* Simplify nested type casts */
  362.  
  363.         while(p->tag==TEXPR && p->exprblock.opcode==OPCONV &&
  364.             ( (ISREAL(pt)&&ONEOF(lt,MSKREAL|MSKCOMPLEX)) ||
  365.             (INT(pt)&&(ONEOF(lt,MSKINT|MSKADDR|MSKCHAR|M(TYSUBR)))) ))
  366.         {
  367.             if(pt==TYDREAL && lt==TYREAL)
  368.             {
  369.                 if(lp->tag==TEXPR
  370.                 && lp->exprblock.opcode == OPCONV) {
  371.                     lt1 = lp->exprblock.leftp->headblock.vtype;
  372.                     if (lt1 == TYDREAL) {
  373.                     lp->exprblock.leftp =
  374.                         putx(lp->exprblock.leftp);
  375.                     return p;
  376.                     }
  377.                     if (lt1 == TYDCOMPLEX) {
  378.                     lp->exprblock.leftp = putx(
  379.                         (expptr)realpart(
  380.                         putcx1(lp->exprblock.leftp)));
  381.                     return p;
  382.                     }
  383.                     }
  384.                 break;
  385.             }
  386.             else if (ISREAL(pt) && ISCOMPLEX(lt)) {
  387.                 p->exprblock.leftp = putx(mkconv(pt,
  388.                     (expptr)realpart(
  389.                         putcx1(p->exprblock.leftp))));
  390.                 break;
  391.                 }
  392.             if(lt==TYCHAR && lp->tag==TEXPR &&
  393.                 lp->exprblock.opcode==OPCALL)
  394.             {
  395.  
  396. /* May want to make a comma expression here instead.  I had one, but took
  397.    it out for my convenience, not for the convenience of the end user */
  398.  
  399.                 putout (putcall (lp, (Addrp *) &(p ->
  400.                     exprblock.leftp)));
  401.                 return putop (p);
  402.             }
  403.             if (lt == TYCHAR) {
  404.                 p->exprblock.leftp = putx(p->exprblock.leftp);
  405.                 return p;
  406.                 }
  407.             frexpr(p->exprblock.vleng);
  408.             free( (charptr) p );
  409.             p = lp;
  410.             if (p->tag != TEXPR)
  411.                 goto retputx;
  412.             pt = lt;
  413.             lp = p->exprblock.leftp;
  414.             lt = lp->headblock.vtype;
  415.         } /* while */
  416.         if(p->tag==TEXPR && p->exprblock.opcode==OPCONV)
  417.             break;
  418.  retputx:
  419.         return putx(p);
  420.  
  421.     case OPADDR:
  422.         comma = NO;
  423.         lp = p->exprblock.leftp;
  424.         free( (charptr) p );
  425.         if(lp->tag != TADDR)
  426.         {
  427.             tp = (expptr)
  428.                 mktmp(lp->headblock.vtype,lp->headblock.vleng);
  429.             p = putx( mkexpr(OPASSIGN, cpexpr(tp), lp) );
  430.             lp = tp;
  431.             comma = YES;
  432.         }
  433.         if(comma)
  434.             p = mkexpr(OPCOMMA, p, putaddr(lp));
  435.         else
  436.             p = (expptr)putaddr(lp);
  437.         return p;
  438.  
  439.     case OPASSIGN:
  440.     case OPASSIGNI:
  441.     case OPLT:
  442.     case OPLE:
  443.     case OPGT:
  444.     case OPGE:
  445.     case OPEQ:
  446.     case OPNE:
  447.         ;
  448.     }
  449.  
  450.     if( ops2[p->exprblock.opcode] <= 0)
  451.         badop("putop", p->exprblock.opcode);
  452.     p -> exprblock.leftp = putx (p -> exprblock.leftp);
  453.     if (p -> exprblock.rightp)
  454.         p -> exprblock.rightp = putx (p -> exprblock.rightp);
  455.     return p;
  456. }
  457.  
  458. LOCAL expptr putpower(p)
  459. expptr p;
  460. {
  461.     expptr base;
  462.     Addrp t1, t2;
  463.     ftnint k;
  464.     int type;
  465.     char buf[80];            /* buffer for text of comment */
  466.  
  467.     if(!ISICON(p->exprblock.rightp) ||
  468.         (k = p->exprblock.rightp->constblock.Const.ci)<2)
  469.         Fatal("putpower: bad call");
  470.     base = p->exprblock.leftp;
  471.     type = base->headblock.vtype;
  472.     t1 = mktmp(type, ENULL);
  473.     t2 = NULL;
  474.  
  475.     free ((charptr) p);
  476.     p = putassign (cpexpr((expptr) t1), base);
  477.  
  478.     sprintf (buf, "Computing %ld%s power", k,
  479.         k == 2 ? "nd" : k == 3 ? "rd" : "th");
  480.     p1_comment (buf);
  481.  
  482.     for( ; (k&1)==0 && k>2 ; k>>=1 )
  483.     {
  484.         p = mkexpr (OPCOMMA, p, putsteq(t1, t1));
  485.     }
  486.  
  487.     if(k == 2) {
  488.  
  489. /* Write the power computation out immediately */
  490.         putout (p);
  491.         p = putx( mkexpr(OPSTAR, cpexpr((expptr)t1), cpexpr((expptr)t1)));
  492.     } else {
  493.         t2 = mktmp(type, ENULL);
  494.         p = mkexpr (OPCOMMA, p, putassign(cpexpr((expptr)t2),
  495.                         cpexpr((expptr)t1)));
  496.  
  497.         for(k>>=1 ; k>1 ; k>>=1)
  498.         {
  499.             p = mkexpr (OPCOMMA, p, putsteq(t1, t1));
  500.             if(k & 1)
  501.             {
  502.                 p = mkexpr (OPCOMMA, p, putsteq(t2, t1));
  503.             }
  504.         }
  505. /* Write the power computation out immediately */
  506.         putout (p);
  507.         p = putx( mkexpr(OPSTAR, cpexpr((expptr)t2),
  508.             mkexpr(OPSTAR, cpexpr((expptr)t1), cpexpr((expptr)t1))));
  509.     }
  510.     frexpr((expptr)t1);
  511.     if(t2)
  512.         frexpr((expptr)t2);
  513.     return p;
  514. }
  515.  
  516.  
  517.  
  518.  
  519. LOCAL Addrp intdouble(p)
  520. Addrp p;
  521. {
  522.     register Addrp t;
  523.  
  524.     t = mktmp(TYDREAL, ENULL);
  525.     putout (putassign(cpexpr((expptr)t), (expptr)p));
  526.     return(t);
  527. }
  528.  
  529.  
  530.  
  531.  
  532.  
  533. /* Complex-type variable assignment */
  534.  
  535. LOCAL Addrp putcxeq(p)
  536. register expptr p;
  537. {
  538.     register Addrp lp, rp;
  539.     expptr code;
  540.  
  541.     if(p->tag != TEXPR)
  542.         badtag("putcxeq", p->tag);
  543.  
  544.     lp = putcx1(p->exprblock.leftp);
  545.     rp = putcx1(p->exprblock.rightp);
  546.     code = putassign ( (expptr)realpart(lp), (expptr)realpart(rp));
  547.  
  548.     if( ISCOMPLEX(p->exprblock.vtype) )
  549.     {
  550.         code = mkexpr (OPCOMMA, code, putassign
  551.             (imagpart(lp), imagpart(rp)));
  552.     }
  553.     putout (code);
  554.     frexpr((expptr)rp);
  555.     free ((charptr) p);
  556.     return lp;
  557. }
  558.  
  559.  
  560.  
  561. /* putcxop -- used to write out embedded calls to complex functions, and
  562.    complex arguments to procedures */
  563.  
  564. expptr putcxop(p)
  565. expptr p;
  566. {
  567.     return (expptr)putaddr((expptr)putcx1(p));
  568. }
  569.  
  570. #define PAIR(x,y) mkexpr (OPCOMMA, (x), (y))
  571.  
  572. LOCAL Addrp putcx1(p)
  573. register expptr p;
  574. {
  575.     expptr q;
  576.     Addrp lp, rp;
  577.     register Addrp resp;
  578.     int opcode;
  579.     int ltype, rtype;
  580.     long ts;
  581.     expptr mkrealcon();
  582.  
  583.     if(p == NULL)
  584.         return(NULL);
  585.  
  586.     switch(p->tag)
  587.     {
  588.     case TCONST:
  589.         if( ISCOMPLEX(p->constblock.vtype) )
  590.             p = (expptr) putconst((Constp)p);
  591.         return( (Addrp) p );
  592.  
  593.     case TADDR:
  594.         resp = &p->addrblock;
  595.         if (addressable(p))
  596.             return (Addrp) p;
  597.         if ((q = resp->memoffset) && resp->isarray
  598.                       && resp->vtype != TYCHAR) {
  599.             if (ONEOF(resp->vstg, M(STGCOMMON)|M(STGEQUIV))
  600.                     && resp->uname_tag == UNAM_NAME)
  601.                 q = mkexpr(OPMINUS, q,
  602.                     mkintcon(resp->user.name->voffset));
  603.             ts = typesize[resp->vtype]
  604.                     * (resp->Field ? 2 : 1);
  605.             q = resp->memoffset = mkexpr(OPSLASH, q, ICON(ts));
  606.             }
  607.         else
  608.             ts = 0;
  609.         resp = mktmp(tyint, ENULL);
  610.         putout(putassign(cpexpr((expptr)resp), q));
  611.         p->addrblock.memoffset = (expptr)resp;
  612.         if (ts) {
  613.             resp = &p->addrblock;
  614.             q = mkexpr(OPSTAR, resp->memoffset, ICON(ts));
  615.             if (ONEOF(resp->vstg, M(STGCOMMON)|M(STGEQUIV))
  616.                 && resp->uname_tag == UNAM_NAME)
  617.                 q = mkexpr(OPPLUS, q,
  618.                     mkintcon(resp->user.name->voffset));
  619.             resp->memoffset = q;
  620.             }
  621.         return (Addrp) p;
  622.  
  623.     case TEXPR:
  624.         if( ISCOMPLEX(p->exprblock.vtype) )
  625.             break;
  626.         resp = mktmp(TYDREAL, ENULL);
  627.         putout (putassign( cpexpr((expptr)resp), p));
  628.         return(resp);
  629.  
  630.     default:
  631.         badtag("putcx1", p->tag);
  632.     }
  633.  
  634.     opcode = p->exprblock.opcode;
  635.     if(opcode==OPCALL || opcode==OPCCALL)
  636.     {
  637.         Addrp t;
  638.         p = putcall(p, &t);
  639.         putout(p);
  640.         return t;
  641.     }
  642.     else if(opcode == OPASSIGN)
  643.     {
  644.         return putcxeq (p);
  645.     }
  646.  
  647. /* BUG  (inefficient)  Generates too many temporary variables */
  648.  
  649.     resp = mktmp(p->exprblock.vtype, ENULL);
  650.     if(lp = putcx1(p->exprblock.leftp) )
  651.         ltype = lp->vtype;
  652.     if(rp = putcx1(p->exprblock.rightp) )
  653.         rtype = rp->vtype;
  654.  
  655.     switch(opcode)
  656.     {
  657.     case OPCOMMA:
  658.         frexpr((expptr)resp);
  659.         resp = rp;
  660.         rp = NULL;
  661.         break;
  662.  
  663.     case OPNEG:
  664.     case OPNEG1:
  665.         putout (PAIR (
  666.             putassign( (expptr)realpart(resp),
  667.                 mkexpr(OPNEG, (expptr)realpart(lp), ENULL)),
  668.             putassign( imagpart(resp),
  669.                 mkexpr(OPNEG, imagpart(lp), ENULL))));
  670.         break;
  671.  
  672.     case OPPLUS:
  673.     case OPMINUS: { expptr r;
  674.         r = putassign( (expptr)realpart(resp),
  675.             mkexpr(opcode, (expptr)realpart(lp), (expptr)realpart(rp) ));
  676.         if(rtype < TYCOMPLEX)
  677.             q = putassign( imagpart(resp), imagpart(lp) );
  678.         else if(ltype < TYCOMPLEX)
  679.         {
  680.             if(opcode == OPPLUS)
  681.                 q = putassign( imagpart(resp), imagpart(rp) );
  682.             else
  683.                 q = putassign( imagpart(resp),
  684.                     mkexpr(OPNEG, imagpart(rp), ENULL) );
  685.         }
  686.         else
  687.             q = putassign( imagpart(resp),
  688.                 mkexpr(opcode, imagpart(lp), imagpart(rp) ));
  689.         r = PAIR (r, q);
  690.         putout (r);
  691.         break;
  692.         } /* case OPPLUS, OPMINUS: */
  693.     case OPSTAR:
  694.         if(ltype < TYCOMPLEX)
  695.         {
  696.             if( ISINT(ltype) )
  697.                 lp = intdouble(lp);
  698.             putout (PAIR (
  699.                 putassign( (expptr)realpart(resp),
  700.                     mkexpr(OPSTAR, cpexpr((expptr)lp),
  701.                     (expptr)realpart(rp))),
  702.                 putassign( imagpart(resp),
  703.                     mkexpr(OPSTAR, cpexpr((expptr)lp), imagpart(rp)))));
  704.         }
  705.         else if(rtype < TYCOMPLEX)
  706.         {
  707.             if( ISINT(rtype) )
  708.                 rp = intdouble(rp);
  709.             putout (PAIR (
  710.                 putassign( (expptr)realpart(resp),
  711.                     mkexpr(OPSTAR, cpexpr((expptr)rp),
  712.                     (expptr)realpart(lp))),
  713.                 putassign( imagpart(resp),
  714.                     mkexpr(OPSTAR, cpexpr((expptr)rp), imagpart(lp)))));
  715.         }
  716.         else    {
  717.             putout (PAIR (
  718.                 putassign( (expptr)realpart(resp), mkexpr(OPMINUS,
  719.                     mkexpr(OPSTAR, (expptr)realpart(lp),
  720.                     (expptr)realpart(rp)),
  721.                     mkexpr(OPSTAR, imagpart(lp), imagpart(rp)))),
  722.                 putassign( imagpart(resp), mkexpr(OPPLUS,
  723.                     mkexpr(OPSTAR, (expptr)realpart(lp), imagpart(rp)),
  724.                     mkexpr(OPSTAR, imagpart(lp),
  725.                     (expptr)realpart(rp))))));
  726.         }
  727.         break;
  728.  
  729.     case OPSLASH:
  730.         /* fixexpr has already replaced all divisions
  731.          * by a complex by a function call
  732.          */
  733.         if( ISINT(rtype) )
  734.             rp = intdouble(rp);
  735.         putout (PAIR (
  736.             putassign( (expptr)realpart(resp),
  737.                 mkexpr(OPSLASH, (expptr)realpart(lp), cpexpr((expptr)rp))),
  738.             putassign( imagpart(resp),
  739.                 mkexpr(OPSLASH, imagpart(lp), cpexpr((expptr)rp)))));
  740.         break;
  741.  
  742.     case OPCONV:
  743.         if( ISCOMPLEX(lp->vtype) )
  744.             q = imagpart(lp);
  745.         else if(rp != NULL)
  746.             q = (expptr) realpart(rp);
  747.         else
  748.             q = mkrealcon(TYDREAL, "0");
  749.         putout (PAIR (
  750.             putassign( (expptr)realpart(resp), (expptr)realpart(lp)),
  751.             putassign( imagpart(resp), q)));
  752.         break;
  753.  
  754.     default:
  755.         badop("putcx1", opcode);
  756.     }
  757.  
  758.     frexpr((expptr)lp);
  759.     frexpr((expptr)rp);
  760.     free( (charptr) p );
  761.     return(resp);
  762. }
  763.  
  764.  
  765.  
  766.  
  767. /* Only .EQ. and .NE. may be performed on COMPLEX data, other relations
  768.    are not defined */
  769.  
  770. LOCAL expptr putcxcmp(p)
  771. register expptr p;
  772. {
  773.     int opcode;
  774.     register Addrp lp, rp;
  775.     expptr q;
  776.  
  777.     if(p->tag != TEXPR)
  778.         badtag("putcxcmp", p->tag);
  779.  
  780.     opcode = p->exprblock.opcode;
  781.     lp = putcx1(p->exprblock.leftp);
  782.     rp = putcx1(p->exprblock.rightp);
  783.  
  784.     q = mkexpr( opcode==OPEQ ? OPAND : OPOR ,
  785.         mkexpr(opcode, (expptr)realpart(lp), (expptr)realpart(rp)),
  786.         mkexpr(opcode, imagpart(lp), imagpart(rp)) );
  787.  
  788.     free( (charptr) lp);
  789.     free( (charptr) rp);
  790.     free( (charptr) p );
  791.     return     putx( fixexpr((Exprp)q) );
  792. }
  793.  
  794. /* putch1 -- Forces constants into the literal pool, among other things */
  795.  
  796. LOCAL Addrp putch1(p)
  797. register expptr p;
  798. {
  799.     Addrp t;
  800.     expptr e;
  801.  
  802.     switch(p->tag)
  803.     {
  804.     case TCONST:
  805.         return( putconst((Constp)p) );
  806.  
  807.     case TADDR:
  808.         return( (Addrp) p );
  809.  
  810.     case TEXPR:
  811.         switch(p->exprblock.opcode)
  812.         {
  813.             expptr q;
  814.  
  815.         case OPCALL:
  816.         case OPCCALL:
  817.  
  818.             p = putcall(p, &t);
  819.             putout (p);
  820.             break;
  821.  
  822.         case OPCONCAT:
  823.             t = mktmp(TYCHAR, ICON(lencat(p)));
  824.             q = (expptr) cpexpr(p->headblock.vleng);
  825.             p = putcat( cpexpr((expptr)t), p );
  826.             /* put the correct length on the block */
  827.             frexpr(t->vleng);
  828.             t->vleng = q;
  829.             putout (p);
  830.             break;
  831.  
  832.         case OPCONV:
  833.             if(!ISICON(p->exprblock.vleng)
  834.                 || p->exprblock.vleng->constblock.Const.ci!=1
  835.                 || ! INT(p->exprblock.leftp->headblock.vtype) )
  836.                 Fatal("putch1: bad character conversion");
  837.             t = mktmp(TYCHAR, ICON(1));
  838.             e = mkexpr(OPCONV, (expptr)t, ENULL);
  839.             e->headblock.vtype = tyint;
  840.             p = putop( mkexpr(OPASSIGN, cpexpr(e), p));
  841.             putout (p);
  842.             break;
  843.         default:
  844.             badop("putch1", p->exprblock.opcode);
  845.         }
  846.         return(t);
  847.  
  848.     default:
  849.         badtag("putch1", p->tag);
  850.     }
  851.     /* NOT REACHED */ return 0;
  852. }
  853.  
  854.  
  855. /* putchop -- Write out a character actual parameter; that is, this is
  856.    part of a procedure invocation */
  857.  
  858. Addrp putchop(p)
  859. expptr p;
  860. {
  861.     p = putaddr((expptr)putch1(p));
  862.     return (Addrp)p;
  863. }
  864.  
  865.  
  866.  
  867.  
  868. LOCAL expptr putcheq(p)
  869. register expptr p;
  870. {
  871.     expptr lp, rp;
  872.     int nbad;
  873.  
  874.     if(p->tag != TEXPR)
  875.         badtag("putcheq", p->tag);
  876.  
  877.     lp = p->exprblock.leftp;
  878.     rp = p->exprblock.rightp;
  879.     frexpr(p->exprblock.vleng);
  880.     free( (charptr) p );
  881.  
  882. /* If s = t // u, don't bother copying the result, write it directly into
  883.    this buffer */
  884.  
  885.     nbad = badchleng(lp) + badchleng(rp);
  886.     if( rp->tag==TEXPR && rp->exprblock.opcode==OPCONCAT )
  887.         p = putcat(lp, rp);
  888.     else if( !nbad
  889.         && ISONE(lp->headblock.vleng)
  890.         && ISONE(rp->headblock.vleng) ) {
  891.         lp = mkexpr(OPCONV, lp, ENULL);
  892.         rp = mkexpr(OPCONV, rp, ENULL);
  893.         lp->headblock.vtype = rp->headblock.vtype = tyint;
  894.         p = putop(mkexpr(OPASSIGN, lp, rp));
  895.         }
  896.     else
  897.         p = putx( call2(TYSUBR, "s_copy", lp, rp) );
  898.     return p;
  899. }
  900.  
  901.  
  902.  
  903.  
  904. LOCAL expptr putchcmp(p)
  905. register expptr p;
  906. {
  907.     expptr lp, rp;
  908.  
  909.     if(p->tag != TEXPR)
  910.         badtag("putchcmp", p->tag);
  911.  
  912.     lp = p->exprblock.leftp;
  913.     rp = p->exprblock.rightp;
  914.  
  915.     if(ISONE(lp->headblock.vleng) && ISONE(rp->headblock.vleng) ) {
  916.         lp = mkexpr(OPCONV, lp, ENULL);
  917.         rp = mkexpr(OPCONV, rp, ENULL);
  918.         lp->headblock.vtype = rp->headblock.vtype = tyint;
  919.         }
  920.     else {
  921.         lp = call2(TYINT,"s_cmp", lp, rp);
  922.         rp = ICON(0);
  923.         }
  924.     p->exprblock.leftp = lp;
  925.     p->exprblock.rightp = rp;
  926.     p = putop(p);
  927.     return p;
  928. }
  929.  
  930.  
  931.  
  932.  
  933.  
  934. /* putcat -- Writes out a concatenation operation.  Two temporary arrays
  935.    are allocated,   putct1()   is called to initialize them, and then a
  936.    call to runtime library routine   s_cat()   is inserted.
  937.  
  938.     This routine generates code which will perform an  (nconc lhs rhs)
  939.    at runtime.  The runtime funciton does not return a value, the routine
  940.    that calls this   putcat   must remember the name of   lhs.
  941. */
  942.  
  943.  
  944. LOCAL expptr putcat(lhs0, rhs)
  945.  expptr lhs0;
  946.  register expptr rhs;
  947. {
  948.     register Addrp lhs = (Addrp)lhs0;
  949.     int n, tyi;
  950.     Addrp length_var, string_var;
  951.     expptr p;
  952.     static char Writing_concatenation[] = "Writing concatenation";
  953.  
  954. /* Create the temporary arrays */
  955.  
  956.     n = ncat(rhs);
  957.     length_var = mktmpn(n, tyioint, ENULL);
  958.     string_var = mktmpn(n, TYADDR, ENULL);
  959.     frtemp((Addrp)cpexpr((expptr)length_var));
  960.     frtemp((Addrp)cpexpr((expptr)string_var));
  961.  
  962. /* Initialize the arrays */
  963.  
  964.     n = 0;
  965.     /* p1_comment scribbles on its argument, so we
  966.      * cannot safely pass a string literal here. */
  967.     p1_comment(Writing_concatenation);
  968.     putct1(rhs, length_var, string_var, &n);
  969.  
  970. /* Create the invocation */
  971.  
  972.     tyi = tyint;
  973.     tyint = tyioint;    /* for -I2 */
  974.     p = putx (call4 (TYSUBR, "s_cat",
  975.                 (expptr)lhs,
  976.                 (expptr)string_var,
  977.                 (expptr)length_var,
  978.                 (expptr)putconst((Constp)ICON(n))));
  979.     tyint = tyi;
  980.  
  981.     return p;
  982. }
  983.  
  984.  
  985.  
  986.  
  987.  
  988. LOCAL putct1(q, length_var, string_var, ip)
  989. register expptr q;
  990. register Addrp length_var, string_var;
  991. int *ip;
  992. {
  993.     int i;
  994.     Addrp length_copy, string_copy;
  995.     expptr e;
  996.     extern int szleng;
  997.  
  998.     if(q->tag==TEXPR && q->exprblock.opcode==OPCONCAT)
  999.     {
  1000.         putct1(q->exprblock.leftp, length_var, string_var,
  1001.             ip);
  1002.         putct1(q->exprblock.rightp, length_var, string_var,
  1003.             ip);
  1004.         frexpr (q -> exprblock.vleng);
  1005.         free ((charptr) q);
  1006.     }
  1007.     else
  1008.     {
  1009.         i = (*ip)++;
  1010.         e = cpexpr(q->headblock.vleng);
  1011.         if (!e)
  1012.             return; /* error -- character*(*) */
  1013.         length_copy = (Addrp) cpexpr((expptr)length_var);
  1014.         length_copy->memoffset =
  1015.             mkexpr(OPPLUS,length_copy->memoffset, ICON(i*szleng));
  1016.         string_copy = (Addrp) cpexpr((expptr)string_var);
  1017.         string_copy->memoffset =
  1018.             mkexpr(OPPLUS, string_copy->memoffset,
  1019.             ICON(i*typesize[TYLONG]));
  1020.         putout (PAIR (putassign((expptr)length_copy, e),
  1021.             putassign((expptr)string_copy, addrof((expptr)putch1(q)))));
  1022.     }
  1023. }
  1024.  
  1025. /* putaddr -- seems to write out function invocation actual parameters */
  1026.  
  1027. LOCAL expptr putaddr(p0)
  1028.  expptr p0;
  1029. {
  1030.     register Addrp p;
  1031.  
  1032.     if (!(p = (Addrp)p0))
  1033.         return ENULL;
  1034.  
  1035.     if( p->tag==TERROR || (p->memoffset!=NULL && ISERROR(p->memoffset)) )
  1036.     {
  1037.         frexpr((expptr)p);
  1038.         return ENULL;
  1039.     }
  1040.     if (p->isarray && p->memoffset)
  1041.         p->memoffset = putx(p->memoffset);
  1042.     return (expptr) p;
  1043. }
  1044.  
  1045.  LOCAL expptr
  1046. addrfix(e)        /* fudge character string length if it's a TADDR */
  1047.  expptr e;
  1048. {
  1049.     return e->tag == TADDR ? mkexpr(OPIDENTITY, e, ENULL) : e;
  1050.     }
  1051.  
  1052.  LOCAL int
  1053. typekludge(ccall, q, at, j)
  1054.  int ccall;
  1055.  register expptr q;
  1056.  Atype *at;
  1057.  int j;    /* alternate type */
  1058. {
  1059.     register int i, k;
  1060.     extern int iocalladdr;
  1061.     register Namep np;
  1062.  
  1063.     /* Return value classes:
  1064.      *    < 100 ==> Fortran arg (pointer to type)
  1065.      *    < 200 ==> C arg
  1066.      *    < 300 ==> procedure arg
  1067.      *    < 400 ==> external, no explicit type
  1068.      *    < 500 ==> arg that may turn out to be
  1069.      *          either a variable or a procedure
  1070.      */
  1071.  
  1072.     k = q->headblock.vtype;
  1073.     if (ccall) {
  1074.         if (k == TYREAL)
  1075.             k = TYDREAL;    /* force double for library routines */
  1076.         return k + 100;
  1077.         }
  1078.     if (k == TYADDR)
  1079.         return iocalladdr;
  1080.     i = q->tag;
  1081.     if ((i == TEXPR && q->exprblock.opcode != OPCOMMA_ARG)
  1082.     ||  (i == TADDR && q->addrblock.charleng)
  1083.     ||   i == TCONST)
  1084.         k = TYFTNLEN + 100;
  1085.     else if (i == TADDR)
  1086.         switch(q->addrblock.vclass) {
  1087.         case CLPROC:
  1088.             if (q->addrblock.uname_tag != UNAM_NAME)
  1089.                 k += 200;
  1090.             else if ((np = q->addrblock.user.name)->vprocclass
  1091.                     != PTHISPROC) {
  1092.                 if (k && !np->vimpltype)
  1093.                     k += 200;
  1094.                 else {
  1095.                     if (j > 200 && infertypes && j < 300) {
  1096.                         k = j;
  1097.                         inferdcl(np, j-200);
  1098.                         }
  1099.                     else k = (np->vstg == STGEXT
  1100.                         ? extsymtab[np->vardesc.varno].extype
  1101.                         : 0) + 200;
  1102.                     at->cp = mkchain((char *)np, at->cp);
  1103.                     }
  1104.                 }
  1105.             else if (k == TYSUBR)
  1106.                 k += 200;
  1107.             break;
  1108.  
  1109.         case CLUNKNOWN:
  1110.             if (q->addrblock.vstg == STGARG
  1111.              && q->addrblock.uname_tag == UNAM_NAME) {
  1112.                 k += 400;
  1113.                 at->cp = mkchain((char *)q->addrblock.user.name,
  1114.                         at->cp);
  1115.                 }
  1116.         }
  1117.     else if (i == TNAME && q->nameblock.vstg == STGARG) {
  1118.         np = &q->nameblock;
  1119.         switch(np->vclass) {
  1120.             case CLPROC:
  1121.             if (!np->vimpltype)
  1122.                 k += 200;
  1123.             else if (j <= 200 || !infertypes || j >= 300)
  1124.                 k += 300;
  1125.             else {
  1126.                 k = j;
  1127.                 inferdcl(np, j-200);
  1128.                 }
  1129.             goto add2chain;
  1130.  
  1131.             case CLUNKNOWN:
  1132.             /* argument may be a scalar variable or a function */
  1133.             if (np->vimpltype && j && infertypes
  1134.             && j < 300) {
  1135.                 inferdcl(np, j % 100);
  1136.                 k = j;
  1137.                 }
  1138.             else
  1139.                 k += 400;
  1140.  
  1141.             /* to handle procedure args only so far known to be
  1142.              * external, save a pointer to the symbol table entry...
  1143.               */
  1144.  add2chain:
  1145.             at->cp = mkchain((char *)np, at->cp);
  1146.             }
  1147.         }
  1148.     return k;
  1149.     }
  1150.  
  1151.  char *
  1152. Argtype(k, buf)
  1153.  int k;
  1154.  char *buf;
  1155. {
  1156.     if (k < 100) {
  1157.         sprintf(buf, "%s variable", ftn_types[k]);
  1158.         return buf;
  1159.         }
  1160.     if (k < 200) {
  1161.         k -= 100;
  1162.         return ftn_types[k];
  1163.         }
  1164.     if (k < 300) {
  1165.         k -= 200;
  1166.         if (k == TYSUBR)
  1167.             return ftn_types[TYSUBR];
  1168.         sprintf(buf, "%s function", ftn_types[k]);
  1169.         return buf;
  1170.         }
  1171.     if (k < 400)
  1172.         return "external argument";
  1173.     k -= 400;
  1174.     sprintf(buf, "%s argument", ftn_types[k]);
  1175.     return buf;
  1176.     }
  1177.  
  1178.  static void
  1179. atype_squawk(at, msg)
  1180.  Argtypes *at;
  1181.  char *msg;
  1182. {
  1183.     register Atype *a, *ae;
  1184.     warn(msg);
  1185.     for(a = at->atypes, ae = a + at->nargs; a < ae; a++)
  1186.         frchain(&a->cp);
  1187.     at->nargs = -1;
  1188.     if (at->changes & 2 && !at->defined)
  1189.         proc_protochanges++;
  1190.     }
  1191.  
  1192.  static char inconsist[] = "inconsistent calling sequences for ";
  1193.  
  1194.  void
  1195. bad_atypes(at, fname, i, j, k, here, prev)
  1196.  Argtypes *at;
  1197.  char *fname, *here, *prev;
  1198.  int i, j, k;
  1199. {
  1200.     char buf[208], buf1[32], buf2[32];
  1201.  
  1202.     sprintf(buf, "%s%.90s,\n\targ %d: %s%s%s %s.",
  1203.         inconsist, fname, i, here, Argtype(k, buf1),
  1204.         prev, Argtype(j, buf2));
  1205.     atype_squawk(at, buf);
  1206.     }
  1207.  
  1208.  int
  1209. type_fixup(at,a,k)
  1210.  Argtypes *at;
  1211.  Atype *a;
  1212.  int k;
  1213. {
  1214.     register struct Entrypoint *ep;
  1215.     if (!infertypes)
  1216.         return 0;
  1217.     for(ep = entries; ep; ep = ep->entnextp)
  1218.         if (at == ep->entryname->arginfo) {
  1219.             a->type = k % 100;
  1220.             return proc_argchanges = 1;
  1221.             }
  1222.     return 0;
  1223.     }
  1224.  
  1225.  
  1226.  void
  1227. save_argtypes(arglist, at0, at1, ccall, fname, stg, nchargs, type, zap)
  1228.  chainp arglist;
  1229.  Argtypes **at0, **at1;
  1230.  int ccall, stg, nchargs, type, zap;
  1231.  char *fname;
  1232. {
  1233.     Argtypes *at;
  1234.     chainp cp;
  1235.     int i, i0, j, k, nargs, *t, *te;
  1236.     Atype *atypes;
  1237.     expptr q;
  1238.     char buf[208];
  1239.     static int initargs[4] = {TYCOMPLEX, TYDCOMPLEX, TYCHAR, TYFTNLEN+100};
  1240.     static int *init_ap[TYSUBR+1] = {0,0,0,0,0,0,
  1241.                 initargs, initargs+1,0,initargs+2};
  1242.     extern int init_ac[TYSUBR+1];
  1243.  
  1244.     i0 = init_ac[type];
  1245.     t = init_ap[type];
  1246.     te = t + i0;
  1247.     if (at = *at0) {
  1248.         *at1 = at;
  1249.         nargs = at->nargs;
  1250.         if (nargs < 0 && type && at->changes & 2 && !at->defined)
  1251.             --proc_protochanges;
  1252.         if (at->dnargs >= 0 && zap != 2)
  1253.             type = 0;
  1254.         if (nargs < 0) { /* inconsistent usage seen */
  1255.             if (type)
  1256.                 goto newlist;
  1257.             return;
  1258.             }
  1259.         atypes = at->atypes;
  1260.         i = nchargs;
  1261.         for(; t < te; atypes++) {
  1262.             if (++i > nargs) {
  1263.  toomany:
  1264.                 i = nchargs + i0;
  1265.                 for(cp = arglist; cp; cp = cp->nextp)
  1266.                     i++;
  1267.  toofew:
  1268.                 sprintf(buf,
  1269.         "%s%.90s:\n\there %d, previously %d args and string lengths.",
  1270.                     inconsist, fname, i, nargs);
  1271.                 atype_squawk(at, buf);
  1272.  retn:
  1273.                 if (type)
  1274.                     goto newlist;
  1275.                 return;
  1276.                 }
  1277.             j = atypes->type;
  1278.             k = *t++;
  1279.             if (j != k)
  1280.                 goto badtypes;
  1281.             }
  1282.         for(cp = arglist; cp; atypes++, cp = cp->nextp) {
  1283.             if (++i > nargs)
  1284.                 goto toomany;
  1285.             j = atypes->type;
  1286.             if (!(q = (expptr)cp->datap))
  1287.                 continue;
  1288.             k = typekludge(ccall, q, atypes, j);
  1289.             if (k >= 300 || k == j)
  1290.                 continue;
  1291.             if (j >= 300) {
  1292.                 if (k >= 200) {
  1293.                     if (k == TYUNKNOWN + 200)
  1294.                         continue;
  1295.                     if (j % 100 != k - 200
  1296.                      && k != TYSUBR + 200
  1297.                      && j != TYUNKNOWN + 300
  1298.                      && !type_fixup(at,atypes,k))
  1299.                         goto badtypes;
  1300.                     }
  1301.                 else if (j % 100 % TYSUBR != k % TYSUBR
  1302.                         && !type_fixup(at,atypes,k))
  1303.                     goto badtypes;
  1304.                 }
  1305.             else if (k < 200 || j < 200)
  1306.                 if (j)
  1307.                     goto badtypes;
  1308.                 else ; /* fall through to update */
  1309.             else if (k == TYUNKNOWN+200)
  1310.                 continue;
  1311.             else if (j != TYUNKNOWN+200)
  1312.                 {
  1313.  badtypes:
  1314.                 bad_atypes(at, fname, i, j, k, "here ",
  1315.                         ", previously");
  1316.                 if (type) {
  1317.                     /* we're defining the procedure */
  1318.                     t = init_ap[type];
  1319.                     te = t + i0;
  1320.                     proc_argchanges = 1;
  1321.                     goto newlist;
  1322.                     }
  1323.                 goto retn;
  1324.                 }
  1325.             /* We've subsequently learned the right type,
  1326.                as in the call on zoo below...
  1327.  
  1328.                 subroutine foo(x, zap)
  1329.                 external zap
  1330.                 call goo(zap)
  1331.                 x = zap(3)
  1332.                 call zoo(zap)
  1333.                 end
  1334.              */
  1335.             atypes->type = k;
  1336.             at->changes |= 1;
  1337.             }
  1338.         if (i < nargs)
  1339.             goto toofew;
  1340.         if (zap == 1 && (at->changes & 5) != 5)
  1341.             at->changes = 0;
  1342.         return;
  1343.         }
  1344.  newlist:
  1345.     i = i0 + nchargs;
  1346.     for(cp = arglist; cp; cp = cp->nextp)
  1347.         i++;
  1348.     k = sizeof(Argtypes) + (i-1)*sizeof(Atype);
  1349.     *at0 = *at1 = at = stg == STGEXT ? (Argtypes *)gmem(k,1)
  1350.                      : (Argtypes *) mem(k,1);
  1351.     at->dnargs = at->nargs = i;
  1352.     at->defined = zap & 2;
  1353.     at->changes = type ? 0 : 4;
  1354.     atypes = at->atypes;
  1355.     for(; t < te; atypes++) {
  1356.         atypes->type = *t++;
  1357.         atypes->cp = 0;
  1358.         }
  1359.     for(cp = arglist; cp; atypes++, cp = cp->nextp) {
  1360.         atypes->cp = 0;
  1361.         atypes->type = (q = (expptr)cp->datap)
  1362.             ? typekludge(ccall, q, atypes, 0)
  1363.             : 0;
  1364.         }
  1365.     for(; --nchargs >= 0; atypes++) {
  1366.         atypes->type = TYFTNLEN + 100;
  1367.         atypes->cp = 0;
  1368.         }
  1369.     }
  1370.  
  1371.  void
  1372. saveargtypes(p)        /* for writing prototypes */
  1373.  register Exprp p;
  1374. {
  1375.     Addrp a;
  1376.     Argtypes **at0, **at1;
  1377.     Namep np;
  1378.     chainp arglist;
  1379.     expptr rp;
  1380.     Extsym *e;
  1381.     char *fname;
  1382.  
  1383.     a = (Addrp)p->leftp;
  1384.     switch(a->vstg) {
  1385.         case STGEXT:
  1386.             switch(a->uname_tag) {
  1387.                 case UNAM_EXTERN:    /* e.g., sqrt() */
  1388.                     e = extsymtab + a->memno;
  1389.                     at0 = at1 = &e->arginfo;
  1390.                     fname = e->fextname;
  1391.                     break;
  1392.                 case UNAM_NAME:
  1393.                     np = a->user.name;
  1394.                     at0 = &extsymtab[np->vardesc.varno].arginfo;
  1395.                     at1 = &np->arginfo;
  1396.                     fname = np->fvarname;
  1397.                     break;
  1398.                 default:
  1399.                     goto bug;
  1400.                 }
  1401.             break;
  1402.         case STGARG:
  1403.             if (a->uname_tag != UNAM_NAME)
  1404.                 goto bug;
  1405.             np = a->user.name;
  1406.             at0 = at1 = &np->arginfo;
  1407.             fname = np->fvarname;
  1408.             break;
  1409.         default:
  1410.      bug:
  1411.             Fatal("Confusion in saveargtypes");
  1412.         }
  1413.     rp = p->rightp;
  1414.     arglist = rp && rp->tag == TLIST ? rp->listblock.listp : 0;
  1415.     save_argtypes(arglist, at0, at1, p->opcode == OPCCALL,
  1416.         fname, a->vstg, 0, 0, 0);
  1417.     }
  1418.  
  1419. /* putcall - fix up the argument list, and write out the invocation.   p
  1420.    is expected to be initialized and point to an OPCALL or OPCCALL
  1421.    expression.  The return value is a pointer to a temporary holding the
  1422.    result of a COMPLEX or CHARACTER operation, or NULL. */
  1423.  
  1424. LOCAL expptr putcall(p0, temp)
  1425.  expptr p0;
  1426.  Addrp *temp;
  1427. {
  1428.     register Exprp p = (Exprp)p0;
  1429.     chainp arglist;        /* Pointer to actual arguments, if any */
  1430.     chainp charsp;        /* List of copies of the variables which
  1431.                    hold the lengths of character
  1432.                    parameters (other than procedure
  1433.                    parameters) */
  1434.     chainp cp;            /* Iterator over argument lists */
  1435.     register expptr q;        /* Pointer to the current argument */
  1436.     Addrp fval;            /* Function return value */
  1437.     int type;            /* type of the call - presumably this was
  1438.                    set elsewhere */
  1439.     int byvalue;        /* True iff we don't want to massage the
  1440.                    parameter list, since we're calling a C
  1441.                    library routine */
  1442.     extern int Castargs;
  1443.     char *s;
  1444.     extern struct Listblock *mklist();
  1445.  
  1446.     type = p -> vtype;
  1447.     charsp = NULL;
  1448.     byvalue =  (p->opcode == OPCCALL);
  1449.  
  1450. /* Verify the actual parameters */
  1451.  
  1452.     if (p == (Exprp) NULL)
  1453.     err ("putcall:  NULL call expression");
  1454.     else if (p -> tag != TEXPR)
  1455.     erri ("putcall:  expected TEXPR, got '%d'", p -> tag);
  1456.  
  1457. /* Find the argument list */
  1458.  
  1459.     if(p->rightp && p -> rightp -> tag == TLIST)
  1460.     arglist = p->rightp->listblock.listp;
  1461.     else
  1462.     arglist = NULL;
  1463.  
  1464. /* Count the number of explicit arguments, including lengths of character
  1465.    variables */
  1466.  
  1467.     for(cp = arglist ; cp ; cp = cp->nextp)
  1468.     if(!byvalue) {
  1469.         q = (expptr) cp->datap;
  1470.         if( ISCONST(q) )
  1471.         {
  1472.  
  1473. /* Even constants are passed by reference, so we need to put them in the
  1474.    literal table */
  1475.  
  1476.         q = (expptr) putconst((Constp)q);
  1477.         cp->datap = (char *) q;
  1478.         }
  1479.  
  1480. /* Save the length expression of character variables (NOT character
  1481.    procedures) for the end of the argument list */
  1482.  
  1483.         if( ISCHAR(q) &&
  1484.         (q->headblock.vclass != CLPROC
  1485.         || q->headblock.vstg == STGARG
  1486.             && q->tag == TADDR
  1487.             && q->addrblock.uname_tag == UNAM_NAME
  1488.             && q->addrblock.user.name->vprocclass == PTHISPROC))
  1489.         {
  1490.         p0 = cpexpr(q->headblock.vleng);
  1491.         charsp = mkchain((char *)p0, charsp);
  1492.         if (q->headblock.vclass == CLUNKNOWN
  1493.          && q->headblock.vstg == STGARG)
  1494.             q->addrblock.user.name->vpassed = 1;
  1495.         else if (q->tag == TADDR
  1496.                 && q->addrblock.uname_tag == UNAM_CONST)
  1497.             p0->constblock.Const.ci
  1498.                 += q->addrblock.user.Const.ccp1.blanks;
  1499.         }
  1500.     }
  1501.     charsp = revchain(charsp);
  1502.  
  1503. /* If the routine is a CHARACTER function ... */
  1504.  
  1505.     if(type == TYCHAR)
  1506.     {
  1507.     if( ISICON(p->vleng) )
  1508.     {
  1509.  
  1510. /* Allocate a temporary to hold the return value of the function */
  1511.  
  1512.         fval = mktmp(TYCHAR, p->vleng);
  1513.     }
  1514.     else    {
  1515.         err("adjustable character function");
  1516.         if (temp)
  1517.             *temp = 0;
  1518.         return 0;
  1519.         }
  1520.     }
  1521.  
  1522. /* If the routine is a COMPLEX function ... */
  1523.  
  1524.     else if( ISCOMPLEX(type) )
  1525.     fval = mktmp(type, ENULL);
  1526.     else
  1527.     fval = NULL;
  1528.  
  1529. /* Write the function name, without taking its address */
  1530.  
  1531.     p -> leftp = putx(fixtype(putaddr(p->leftp)));
  1532.  
  1533.     if(fval)
  1534.     {
  1535.     chainp prepend;
  1536.  
  1537. /* Prepend a copy of the function return value buffer out as the first
  1538.    argument. */
  1539.  
  1540.     prepend = mkchain((char *)putx(putaddr(cpexpr((expptr)fval))), arglist);
  1541.  
  1542. /* If it's a character function, also prepend the length of the result */
  1543.  
  1544.     if(type==TYCHAR)
  1545.     {
  1546.  
  1547.         prepend->nextp = mkchain((char *)putx(mkconv(TYLENG,
  1548.                     p->vleng)), arglist);
  1549.     }
  1550.     if (!(q = p->rightp))
  1551.         p->rightp = q = (expptr)mklist(CHNULL);
  1552.     q->listblock.listp = prepend;
  1553.     }
  1554.  
  1555. /* Scan through the fortran argument list */
  1556.  
  1557.     for(cp = arglist ; cp ; cp = cp->nextp)
  1558.     {
  1559.     q = (expptr) (cp->datap);
  1560.     if (q == ENULL)
  1561.         err ("putcall:  NULL argument");
  1562.  
  1563. /* call putaddr only when we've got a parameter for a C routine or a
  1564.    memory resident parameter */
  1565.  
  1566.     if (q -> tag == TCONST && !byvalue)
  1567.         q = (expptr) putconst ((Constp)q);
  1568.  
  1569.     if(q->tag==TADDR && (byvalue || q->addrblock.vstg!=STGREG) )
  1570.         cp->datap = (char *)putaddr(q);
  1571.     else if( ISCOMPLEX(q->headblock.vtype) )
  1572.         cp -> datap = (char *) putx (fixtype(putcxop(q)));
  1573.     else if (ISCHAR(q) )
  1574.         cp -> datap = (char *) putx (fixtype((expptr)putchop(q)));
  1575.     else if( ! ISERROR(q) )
  1576.     {
  1577.         if(byvalue
  1578.         || q->tag == TEXPR && q->exprblock.opcode == OPCHARCAST)
  1579.         cp -> datap = (char *) putx(q);
  1580.         else {
  1581.         expptr t, t1;
  1582.  
  1583. /* If we've got a register parameter, or (maybe?) a constant, save it in a
  1584.    temporary first */
  1585.  
  1586.         t = (expptr) mktmp(q->headblock.vtype, q->headblock.vleng);
  1587.  
  1588. /* Assign to temporary variables before invoking the subroutine or
  1589.    function */
  1590.  
  1591.         t1 = putassign( cpexpr(t), q );
  1592.         if (doin_setbound)
  1593.             t = mkexpr(OPCOMMA_ARG, t1, t);
  1594.         else
  1595.             putout(t1);
  1596.         cp -> datap = (char *) t;
  1597.         } /* else */
  1598.     } /* if !ISERROR(q) */
  1599.     }
  1600.  
  1601. /* Now adjust the lengths of the CHARACTER parameters */
  1602.  
  1603.     for(cp = charsp ; cp ; cp = cp->nextp)
  1604.     cp->datap = (char *)addrfix(putx(
  1605.             /* in case MAIN has a character*(*)... */
  1606.             (s = cp->datap) ? mkconv(TYLENG,(expptr)s)
  1607.                      : ICON(0)));
  1608.  
  1609. /* ... and add them to the end of the argument list */
  1610.  
  1611.     hookup (arglist, charsp);
  1612.  
  1613. /* Return the name of the temporary used to hold the results, if any was
  1614.    necessary. */
  1615.  
  1616.     if (temp) *temp = fval;
  1617.     else frexpr ((expptr)fval);
  1618.  
  1619.     saveargtypes(p);
  1620.  
  1621.     return (expptr) p;
  1622. }
  1623.  
  1624.  
  1625.  
  1626. /* putmnmx -- Put min or max.   p   must point to an EXPR, not just a
  1627.    CONST */
  1628.  
  1629. LOCAL expptr putmnmx(p)
  1630. register expptr p;
  1631. {
  1632.     int op, op2, type;
  1633.     expptr arg, qp, temp;
  1634.     chainp p0, p1;
  1635.     Addrp sp, tp;
  1636.     char comment_buf[80];
  1637.     char *what;
  1638.  
  1639.     if(p->tag != TEXPR)
  1640.         badtag("putmnmx", p->tag);
  1641.  
  1642.     type = p->exprblock.vtype;
  1643.     op = p->exprblock.opcode;
  1644.     op2 = op == OPMIN ? OPMIN2 : OPMAX2;
  1645.     p0 = p->exprblock.leftp->listblock.listp;
  1646.     free( (charptr) (p->exprblock.leftp) );
  1647.     free( (charptr) p );
  1648.  
  1649.     /* special case for two addressable operands */
  1650.  
  1651.     if (addressable((expptr)p0->datap)
  1652.      && (p1 = p0->nextp)
  1653.      && addressable((expptr)p1->datap)
  1654.      && !p1->nextp) {
  1655.         if (type == TYREAL && forcedouble)
  1656.             op2 = op == OPMIN ? OPDMIN : OPDMAX;
  1657.         p = mkexpr(op2, mkconv(type, cpexpr((expptr)p0->datap)),
  1658.                 mkconv(type, cpexpr((expptr)p1->datap)));
  1659.         frchain(&p0);
  1660.         return p;
  1661.         }
  1662.  
  1663.     /* general case */
  1664.  
  1665.     sp = mktmp(type, ENULL);
  1666.  
  1667. /* We only need a second temporary if the arg list has an unaddressable
  1668.    value */
  1669.  
  1670.     tp = (Addrp) NULL;
  1671.     qp = ENULL;
  1672.     for (p1 = p0 -> nextp; p1; p1 = p1 -> nextp)
  1673.         if (!addressable ((expptr) p1 -> datap)) {
  1674.             tp = mktmp(type, ENULL);
  1675.             qp = mkexpr(op2, cpexpr((expptr)sp), cpexpr((expptr)tp));
  1676.             qp = fixexpr((Exprp)qp);
  1677.             break;
  1678.         } /* if */
  1679.  
  1680. /* Now output the appropriate number of assignments and comparisons.  Min
  1681.    and max are implemented by the simple O(n) algorithm:
  1682.  
  1683.     min (a, b, c, d) ==>
  1684.     { <type> t1, t2;
  1685.  
  1686.         t1 = a;
  1687.         t2 = b; t1 = (t1 < t2) ? t1 : t2;
  1688.         t2 = c; t1 = (t1 < t2) ? t1 : t2;
  1689.         t2 = d; t1 = (t1 < t2) ? t1 : t2;
  1690.     }
  1691. */
  1692.  
  1693.     if (!doin_setbound) {
  1694.         switch(op) {
  1695.             case OPLT:
  1696.             case OPMIN:
  1697.             case OPDMIN:
  1698.             case OPMIN2:
  1699.                 what = "IN";
  1700.                 break;
  1701.             default:
  1702.                 what = "AX";
  1703.             }
  1704.         sprintf (comment_buf, "Computing M%s", what);
  1705.         p1_comment (comment_buf);
  1706.         }
  1707.  
  1708.     p1 = p0->nextp;
  1709.     temp = (expptr)p0->datap;
  1710.     if (addressable(temp) && addressable((expptr)p1->datap)) {
  1711.         p = mkconv(type, cpexpr(temp));
  1712.         arg = mkconv(type, cpexpr((expptr)p1->datap));
  1713.         temp = mkexpr(op2, p, arg);
  1714.         if (!ISCONST(temp))
  1715.             temp = fixexpr((Exprp)temp);
  1716.         p1 = p1->nextp;
  1717.         }
  1718.     p = putassign (cpexpr((expptr)sp), temp);
  1719.  
  1720.     for(; p1 ; p1 = p1->nextp)
  1721.     {
  1722.         if (addressable ((expptr) p1 -> datap)) {
  1723.             arg = mkconv(type, cpexpr((expptr)p1->datap));
  1724.             temp = mkexpr(op2, cpexpr((expptr)sp), arg);
  1725.             temp = fixexpr((Exprp)temp);
  1726.         } else {
  1727.             temp = (expptr) cpexpr (qp);
  1728.             p = mkexpr(OPCOMMA, p,
  1729.                 putassign(cpexpr((expptr)tp), (expptr)p1->datap));
  1730.         } /* else */
  1731.  
  1732.         if(p1->nextp)
  1733.             p = mkexpr(OPCOMMA, p,
  1734.                 putassign(cpexpr((expptr)sp), temp));
  1735.         else {
  1736.             if (type == TYREAL && forcedouble)
  1737.                 temp->exprblock.opcode =
  1738.                     op == OPMIN ? OPDMIN : OPDMAX;
  1739.             if (doin_setbound)
  1740.                 p = mkexpr(OPCOMMA, p, temp);
  1741.             else {
  1742.                 putout (p);
  1743.                 p = putx(temp);
  1744.                 }
  1745.             if (qp)
  1746.                 frexpr (qp);
  1747.         } /* else */
  1748.     } /* for */
  1749.  
  1750.     frchain( &p0 );
  1751.     return p;
  1752. }
  1753.  
  1754.  
  1755.  void
  1756. putwhile(p)
  1757.  expptr p;
  1758. {
  1759.     long where;
  1760.     int k, n;
  1761.  
  1762.     if (wh_next >= wh_last)
  1763.         {
  1764.         k = wh_last - wh_first;
  1765.         n = k + 100;
  1766.         wh_next = mem(n,0);
  1767.         wh_last = wh_first + n;
  1768.         if (k)
  1769.             memcpy(wh_next, wh_first, k);
  1770.         wh_first =  wh_next;
  1771.         wh_next += k;
  1772.         wh_last = wh_first + n;
  1773.         }
  1774.     if( ( k = (p = fixtype(p))->headblock.vtype) != TYLOGICAL)
  1775.         {
  1776.         if(k != TYERROR)
  1777.             err("non-logical expression in DO WHILE statement");
  1778.         }
  1779.     else    {
  1780.         p1put(P1_WHILE1START);
  1781.         where = ftell(pass1_file);
  1782.         p = putx(p);
  1783.         *wh_next++ = ftell(pass1_file) > where;
  1784.         p1put(P1_WHILE2START);
  1785.         p1_expr(p);
  1786.         }
  1787.     }
  1788.