home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / f2c / src / output.c < prev    next >
C/C++ Source or Header  |  2000-06-23  |  38KB  |  1,452 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. #include "defs.h"
  25. #include "names.h"
  26. #include "output.h"
  27.  
  28. #ifndef TRUE
  29. #define TRUE 1
  30. #endif
  31. #ifndef FALSE
  32. #define FALSE 0
  33. #endif
  34.  
  35. char _assoc_table[] = { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 };
  36.  
  37. /* Opcode table -- This array is indexed by the OP_____ macros defined in
  38.    defines.h; these macros are expected to be adjacent integers, so that
  39.    this table is as small as possible. */
  40.  
  41. table_entry opcode_table[] = {
  42.                 { 0, 0, NULL },
  43.     /* OPPLUS 1 */        { BINARY_OP, 12, "%l + %r" },
  44.     /* OPMINUS 2 */        { BINARY_OP, 12, "%l - %r" },
  45.     /* OPSTAR 3 */        { BINARY_OP, 13, "%l * %r" },
  46.     /* OPSLASH 4 */        { BINARY_OP, 13, "%l / %r" },
  47.     /* OPPOWER 5 */        { BINARY_OP,  0, "power (%l, %r)" },
  48.     /* OPNEG 6 */        { UNARY_OP,  14, "-%l" },
  49.     /* OPOR 7 */        { BINARY_OP,  4, "%l || %r" },
  50.     /* OPAND 8 */        { BINARY_OP,  5, "%l && %r" },
  51.     /* OPEQV 9 */        { BINARY_OP,  9, "%l == %r" },
  52.     /* OPNEQV 10 */        { BINARY_OP,  9, "%l != %r" },
  53.     /* OPNOT 11 */        { UNARY_OP,  14, "! %l" },
  54.     /* OPCONCAT 12 */    { BINARY_OP,  0, "concat (%l, %r)" },
  55.     /* OPLT 13 */        { BINARY_OP, 10, "%l < %r" },
  56.     /* OPEQ 14 */        { BINARY_OP,  9, "%l == %r" },
  57.     /* OPGT 15 */        { BINARY_OP, 10, "%l > %r" },
  58.     /* OPLE 16 */        { BINARY_OP, 10, "%l <= %r" },
  59.     /* OPNE 17 */        { BINARY_OP,  9, "%l != %r" },
  60.     /* OPGE 18 */        { BINARY_OP, 10, "%l >= %r" },
  61.     /* OPCALL 19 */        { BINARY_OP, 15, SPECIAL_FMT },
  62.     /* OPCCALL 20 */    { BINARY_OP, 15, SPECIAL_FMT },
  63.  
  64. /* Left hand side of an assignment cannot have outermost parens */
  65.  
  66.     /* OPASSIGN 21 */    { BINARY_OP,  2, "%l = %r" },
  67.     /* OPPLUSEQ 22 */    { BINARY_OP,  2, "%l += %r" },
  68.     /* OPSTAREQ 23 */    { BINARY_OP,  2, "%l *= %r" },
  69.     /* OPCONV 24 */        { BINARY_OP, 14, "%l" },
  70.     /* OPLSHIFT 25 */    { BINARY_OP, 11, "%l << %r" },
  71.     /* OPMOD 26 */        { BINARY_OP, 13, "%l %% %r" },
  72.     /* OPCOMMA 27 */    { BINARY_OP,  1, "%l, %r" },
  73.  
  74. /* Don't want to nest the colon operator in parens */
  75.  
  76.     /* OPQUEST 28 */    { BINARY_OP, 3, "%l ? %r" },
  77.     /* OPCOLON 29 */    { BINARY_OP, 3, "%l : %r" },
  78.     /* OPABS 30 */        { UNARY_OP,  0, "abs(%l)" },
  79.     /* OPMIN 31 */        { BINARY_OP,   0, SPECIAL_FMT },
  80.     /* OPMAX 32 */        { BINARY_OP,   0, SPECIAL_FMT },
  81.     /* OPADDR 33 */        { UNARY_OP, 14, "&%l" },
  82.  
  83.     /* OPCOMMA_ARG 34 */    { BINARY_OP, 15, SPECIAL_FMT },
  84.     /* OPBITOR 35 */    { BINARY_OP,  6, "%l | %r" },
  85.     /* OPBITAND 36 */    { BINARY_OP,  8, "%l & %r" },
  86.     /* OPBITXOR 37 */    { BINARY_OP,  7, "%l ^ %r" },
  87.     /* OPBITNOT 38 */    { UNARY_OP,  14, "~ %l" },
  88.     /* OPRSHIFT 39 */    { BINARY_OP, 11, "%l >> %r" },
  89.  
  90. /* This isn't quite right -- it doesn't handle arrays, for instance */
  91.  
  92.     /* OPWHATSIN 40 */    { UNARY_OP,  14, "*%l" },
  93.     /* OPMINUSEQ 41 */    { BINARY_OP,  2, "%l -= %r" },
  94.     /* OPSLASHEQ 42 */    { BINARY_OP,  2, "%l /= %r" },
  95.     /* OPMODEQ 43 */    { BINARY_OP,  2, "%l %%= %r" },
  96.     /* OPLSHIFTEQ 44 */    { BINARY_OP,  2, "%l <<= %r" },
  97.     /* OPRSHIFTEQ 45 */    { BINARY_OP,  2, "%l >>= %r" },
  98.     /* OPBITANDEQ 46 */    { BINARY_OP,  2, "%l &= %r" },
  99.     /* OPBITXOREQ 47 */    { BINARY_OP,  2, "%l ^= %r" },
  100.     /* OPBITOREQ 48 */    { BINARY_OP,  2, "%l |= %r" },
  101.     /* OPPREINC 49 */    { UNARY_OP,  14, "++%l" },
  102.     /* OPPREDEC 50 */    { UNARY_OP,  14, "--%l" },
  103.     /* OPDOT 51 */        { BINARY_OP, 15, "%l.%r" },
  104.     /* OPARROW 52 */    { BINARY_OP, 15, "%l -> %r"},
  105.     /* OPNEG1 53 */        { UNARY_OP,  14, "-%l" },
  106.     /* OPDMIN 54 */        { BINARY_OP, 0, "dmin(%l,%r)" },
  107.     /* OPDMAX 55 */        { BINARY_OP, 0, "dmax(%l,%r)" },
  108.     /* OPASSIGNI 56 */    { BINARY_OP,  2, "%l = &%r" },
  109.     /* OPIDENTITY 57 */    { UNARY_OP, 15, "%l" },
  110.     /* OPCHARCAST 58 */    { UNARY_OP, 14, "(char *)&%l" },
  111.     /* OPDABS 59 */        { UNARY_OP, 0, "dabs(%l)" },
  112.     /* OPMIN2 60 */        { BINARY_OP,   0, "min(%l,%r)" },
  113.     /* OPMAX2 61 */        { BINARY_OP,   0, "max(%l,%r)" },
  114.  
  115. /* kludge to imitate (under forcedouble) f77's bizarre treatement of OPNEG... */
  116.  
  117.     /* OPNEG KLUDGE */    { UNARY_OP,  14, "-(doublereal)%l" }
  118. }; /* opcode_table */
  119.  
  120. #define OPNEG_KLUDGE (sizeof(opcode_table)/sizeof(table_entry) - 1)
  121.  
  122. static char opeqable[sizeof(opcode_table)/sizeof(table_entry)];
  123.  
  124.  
  125. static void output_prim ();
  126. static void output_unary (), output_binary (), output_arg_list ();
  127. static void output_list (), output_literal ();
  128.  
  129.  
  130. void expr_out (fp, e)
  131. FILE *fp;
  132. expptr e;
  133. {
  134.     if (e == (expptr) NULL)
  135.     return;
  136.  
  137.     switch (e -> tag) {
  138.     case TNAME:    out_name (fp, (struct Nameblock *) e);
  139.             return;
  140.  
  141.     case TCONST:    out_const(fp, &e->constblock);
  142.             goto end_out;
  143.     case TEXPR:
  144.                 break;
  145.  
  146.     case TADDR:    out_addr (fp, &(e -> addrblock));
  147.             goto end_out;
  148.  
  149.     case TPRIM:    warn ("expr_out: got TPRIM");
  150.             output_prim (fp, &(e -> primblock));
  151.             return;
  152.  
  153.     case TLIST:    output_list (fp, &(e -> listblock));
  154.  end_out:        frexpr(e);
  155.             return;
  156.  
  157.     case TIMPLDO:    err ("expr_out: got TIMPLDO");
  158.             return;
  159.  
  160.     case TERROR:
  161.     default:
  162.             erri ("expr_out: bad tag '%d'", e -> tag);
  163.     } /* switch */
  164.  
  165. /* Now we know that the tag is TEXPR */
  166.  
  167. /* Optimize on simple expressions, such as "a = a + b" ==> "a += b" */
  168.  
  169.     if (e -> exprblock.opcode == OPASSIGN && e -> exprblock.rightp &&
  170.     e -> exprblock.rightp -> tag == TEXPR) {
  171.     int opcode;
  172.  
  173.     opcode = e -> exprblock.rightp -> exprblock.opcode;
  174.  
  175.     if (opeqable[opcode]) {
  176.         expptr leftp, rightp;
  177.  
  178.         if ((leftp = e -> exprblock.leftp) &&
  179.         (rightp = e -> exprblock.rightp -> exprblock.leftp)) {
  180.  
  181.         if (same_ident (leftp, rightp)) {
  182.             expptr temp = e -> exprblock.rightp;
  183.  
  184.             e -> exprblock.opcode = op_assign(opcode);
  185.  
  186.             e -> exprblock.rightp = temp -> exprblock.rightp;
  187.             temp->exprblock.rightp = 0;
  188.             frexpr(temp);
  189.         } /* if same_ident (leftp, rightp) */
  190.         } /* if leftp && rightp */
  191.     } /* if opcode == OPPLUS || */
  192.     } /* if e -> exprblock.opcode == OPASSIGN */
  193.  
  194.  
  195. /* Optimize on increment or decrement by 1 */
  196.  
  197.     {
  198.     int opcode = e -> exprblock.opcode;
  199.     expptr leftp = e -> exprblock.leftp;
  200.     expptr rightp = e -> exprblock.rightp;
  201.  
  202.     if (leftp && rightp && (leftp -> headblock.vstg == STGARG ||
  203.         ISINT (leftp -> headblock.vtype)) &&
  204.         (opcode == OPPLUSEQ || opcode == OPMINUSEQ) &&
  205.         ISINT (rightp -> headblock.vtype) &&
  206.         ISICON (e -> exprblock.rightp) &&
  207.         (ISONE (e -> exprblock.rightp) ||
  208.         e -> exprblock.rightp -> constblock.Const.ci == -1)) {
  209.  
  210. /* Allow for the '-1' constant value */
  211.  
  212.         if (!ISONE (e -> exprblock.rightp))
  213.         opcode = (opcode == OPPLUSEQ) ? OPMINUSEQ : OPPLUSEQ;
  214.  
  215. /* replace the existing opcode */
  216.  
  217.         if (opcode == OPPLUSEQ)
  218.         e -> exprblock.opcode = OPPREINC;
  219.         else
  220.         e -> exprblock.opcode = OPPREDEC;
  221.  
  222. /* Free up storage used by the right hand side */
  223.  
  224.         frexpr (e -> exprblock.rightp);
  225.         e->exprblock.rightp = 0;
  226.     } /* if opcode == OPPLUS */
  227.     } /* block */
  228.  
  229.  
  230.     if (is_unary_op (e -> exprblock.opcode))
  231.     output_unary (fp, &(e -> exprblock));
  232.     else if (is_binary_op (e -> exprblock.opcode))
  233.     output_binary (fp, &(e -> exprblock));
  234.     else
  235.     erri ("expr_out: bad opcode '%d'", (int) e -> exprblock.opcode);
  236.  
  237.     free((char *)e);
  238.  
  239. } /* expr_out */
  240.  
  241.  
  242. void out_and_free_statement (outfile, expr)
  243. FILE *outfile;
  244. expptr expr;
  245. {
  246.     if (expr)
  247.     expr_out (outfile, expr);
  248.  
  249.     nice_printf (outfile, ";\n");
  250. } /* out_and_free_statement */
  251.  
  252.  
  253.  
  254. int same_ident (left, right)
  255. expptr left, right;
  256. {
  257.     if (!left || !right)
  258.     return 0;
  259.  
  260.     if (left -> tag == TNAME && right -> tag == TNAME && left == right)
  261.     return 1;
  262.  
  263.     if (left -> tag == TADDR && right -> tag == TADDR &&
  264.         left -> addrblock.uname_tag == right -> addrblock.uname_tag)
  265.     switch (left -> addrblock.uname_tag) {
  266.         case UNAM_NAME:
  267.  
  268. /* Check for array subscripts */
  269.  
  270.         if (left -> addrblock.user.name -> vdim ||
  271.             right -> addrblock.user.name -> vdim)
  272.             if (left -> addrblock.user.name !=
  273.                 right -> addrblock.user.name ||
  274.                 !same_expr (left -> addrblock.memoffset,
  275.                 right -> addrblock.memoffset))
  276.             return 0;
  277.  
  278.         return same_ident ((expptr) (left -> addrblock.user.name),
  279.             (expptr) right -> addrblock.user.name);
  280.         case UNAM_IDENT:
  281.         return strcmp(left->addrblock.user.ident,
  282.                 right->addrblock.user.ident) == 0;
  283.         case UNAM_CHARP:
  284.         return strcmp(left->addrblock.user.Charp,
  285.                 right->addrblock.user.Charp) == 0;
  286.         default:
  287.             return 0;
  288.     } /* switch */
  289.  
  290.     if (left->tag == TEXPR && left->exprblock.opcode == OPWHATSIN
  291.     && right->tag == TEXPR && right->exprblock.opcode == OPWHATSIN)
  292.         return same_ident(left->exprblock.leftp,
  293.                  right->exprblock.leftp);
  294.  
  295.     return 0;
  296. } /* same_ident */
  297.  
  298.  static int
  299. samefpconst(c1, c2, n)
  300.  register Constp c1, c2;
  301.  register int n;
  302. {
  303.     char *s1, *s2;
  304.     if (!c1->vstg && !c2->vstg)
  305.         return c1->Const.cd[n] == c2->Const.cd[n];
  306.     s1 = c1->vstg ? c1->Const.cds[n] : dtos(c1->Const.cd[n]);
  307.     s2 = c2->vstg ? c2->Const.cds[n] : dtos(c2->Const.cd[n]);
  308.     return !strcmp(s1, s2);
  309.     }
  310.  
  311.  static int
  312. sameconst(c1, c2)
  313.  register Constp c1, c2;
  314. {
  315.     switch(c1->vtype) {
  316.         case TYCOMPLEX:
  317.         case TYDCOMPLEX:
  318.             if (!samefpconst(c1,c2,1))
  319.                 return 0;
  320.         case TYREAL:
  321.         case TYDREAL:
  322.             return samefpconst(c1,c2,0);
  323.         case TYCHAR:
  324.             return c1->Const.ccp1.blanks == c2->Const.ccp1.blanks
  325.                 &&       c1->vleng->constblock.Const.ci
  326.                 == c2->vleng->constblock.Const.ci
  327.                 && !memcmp(c1->Const.ccp, c2->Const.ccp,
  328.                     (int)c1->vleng->constblock.Const.ci);
  329.         case TYSHORT:
  330.         case TYINT:
  331.         case TYLOGICAL:
  332.             return c1->Const.ci == c2->Const.ci;
  333.         }
  334.     err("unexpected type in sameconst");
  335.     return 0;
  336.     }
  337.  
  338. /* same_expr -- Returns true only if   e1 and e2   match.  This is
  339.    somewhat pessimistic, but can afford to be because it's just used to
  340.    optimize on the assignment operators (+=, -=, etc). */
  341.  
  342. int same_expr (e1, e2)
  343. expptr e1, e2;
  344. {
  345.     if (!e1 || !e2)
  346.     return !e1 && !e2;
  347.  
  348.     if (e1 -> tag != e2 -> tag || e1 -> headblock.vtype != e2 -> headblock.vtype)
  349.     return 0;
  350.  
  351.     switch (e1 -> tag) {
  352.         case TEXPR:
  353.         if (e1 -> exprblock.opcode != e2 -> exprblock.opcode)
  354.         return 0;
  355.  
  356.         return same_expr (e1 -> exprblock.leftp, e2 -> exprblock.leftp) &&
  357.            same_expr (e1 -> exprblock.rightp, e2 -> exprblock.rightp);
  358.     case TNAME:
  359.     case TADDR:
  360.         return same_ident (e1, e2);
  361.     case TCONST:
  362.         return sameconst(&e1->constblock, &e2->constblock);
  363.     default:
  364.         return 0;
  365.     } /* switch */
  366. } /* same_expr */
  367.  
  368.  
  369.  
  370. void out_name (fp, namep)
  371.  FILE *fp;
  372.  Namep namep;
  373. {
  374.     extern int usedefsforcommon;
  375.     Extsym *comm;
  376.  
  377.     if (namep == NULL)
  378.     return;
  379.  
  380. /* DON'T want to use oneof_stg() here; need to find the right common name
  381.    */
  382.  
  383.     if (namep->vstg == STGCOMMON && !namep->vcommequiv && !usedefsforcommon) {
  384.     comm = &extsymtab[namep->vardesc.varno];
  385.     extern_out(fp, comm);
  386.     nice_printf(fp, "%d.", comm->curno);
  387.     } /* if namep -> vstg == STGCOMMON */
  388.  
  389.     if (namep->vprocclass == PTHISPROC && namep->vtype != TYSUBR)
  390.     nice_printf(fp, xretslot[namep->vtype]->user.ident);
  391.     else
  392.     nice_printf (fp, "%s", namep->cvarname);
  393. } /* out_name */
  394.  
  395.  
  396. static char *Longfmt = "%ld";
  397.  
  398. #define cpd(n) cp->vstg ? cp->Const.cds[n] : dtos(cp->Const.cd[n])
  399.  
  400. void out_const(fp, cp)
  401.  FILE *fp;
  402.  register Constp cp;
  403. {
  404.     static char real_buf[50], imag_buf[50];
  405.     unsigned int k;
  406.     int type = cp->vtype;
  407.  
  408.     switch (type) {
  409.         case TYSHORT:
  410.         nice_printf (fp, "%ld", cp->Const.ci);    /* don't cast ci! */
  411.         break;
  412.     case TYLONG:
  413.         nice_printf (fp, Longfmt, cp->Const.ci);    /* don't cast ci! */
  414.         break;
  415.     case TYREAL:
  416.         nice_printf(fp, "%s", flconst(real_buf, cpd(0)));
  417.         break;
  418.     case TYDREAL:
  419.         nice_printf(fp, "%s", cpd(0));
  420.         break;
  421.     case TYCOMPLEX:
  422.         nice_printf(fp, cm_fmt_string, flconst(real_buf, cpd(0)),
  423.             flconst(imag_buf, cpd(1)));
  424.         break;
  425.     case TYDCOMPLEX:
  426.         nice_printf(fp, dcm_fmt_string, cpd(0), cpd(1));
  427.         break;
  428.     case TYLOGICAL:
  429.         nice_printf (fp, "%s", cp->Const.ci ? "TRUE_" : "FALSE_");
  430.         break;
  431.     case TYCHAR: {
  432.         char *c = cp->Const.ccp, *ce;
  433.  
  434.         if (c == NULL) {
  435.         nice_printf (fp, "\"\"");
  436.         break;
  437.         } /* if c == NULL */
  438.  
  439.         nice_printf (fp, "\"");
  440.         ce = c + cp->vleng->constblock.Const.ci;
  441.         while(c < ce) {
  442.         k = *(unsigned char *)c++;
  443.         nice_printf(fp, str_fmt[k], k);
  444.         }
  445.         for(k = cp->Const.ccp1.blanks; k > 0; k--)
  446.         nice_printf(fp, " ");
  447.         nice_printf (fp, "\"");
  448.         break;
  449.     } /* case TYCHAR */
  450.     default:
  451.         erri ("out_const:  bad type '%d'", (int) type);
  452.         break;
  453.     } /* switch */
  454.  
  455. } /* out_const */
  456. #undef cpd
  457.  
  458.  
  459. /* out_addr -- this routine isn't local because it is called by the
  460.    system-generated identifier printing routines */
  461.  
  462. void out_addr (fp, addrp)
  463. FILE *fp;
  464. struct Addrblock *addrp;
  465. {
  466.     extern Extsym *extsymtab;
  467.     int was_array = 0;
  468.     char *s;
  469.  
  470.  
  471.     if (addrp == NULL)
  472.         return;
  473.     if (doin_setbound
  474.             && addrp->vstg == STGARG
  475.             && addrp->vtype != TYCHAR
  476.             && ISICON(addrp->memoffset)
  477.             && !addrp->memoffset->constblock.Const.ci)
  478.         nice_printf(fp, "*");
  479.  
  480.     switch (addrp -> uname_tag) {
  481.         case UNAM_NAME:
  482.         out_name (fp, addrp -> user.name);
  483.         break;
  484.         case UNAM_IDENT:
  485.         if (*(s = addrp->user.ident) == ' ') {
  486.             if (multitype)
  487.                 nice_printf(fp, "%s",
  488.                     xretslot[addrp->vtype]->user.ident);
  489.             else
  490.                 nice_printf(fp, "%s", s+1);
  491.             }
  492.         else {
  493.             nice_printf(fp, "%s", s);
  494.             }
  495.         break;
  496.         case UNAM_CHARP:
  497.         nice_printf(fp, "%s", addrp->user.Charp);
  498.         break;
  499.         case UNAM_EXTERN:
  500.         extern_out (fp, &extsymtab[addrp -> memno]);
  501.         break;
  502.         case UNAM_CONST:
  503.         switch(addrp->vstg) {
  504.             case STGCONST:
  505.                 out_const(fp, (Constp)addrp);
  506.                 break;
  507.             case STGMEMNO:
  508.                 output_literal (fp, (int)addrp->memno,
  509.                     (Constp)addrp);
  510.                 break;
  511.             default:
  512.             Fatal("unexpected vstg in out_addr");
  513.             }
  514.         break;
  515.         case UNAM_UNKNOWN:
  516.         default:
  517.         nice_printf (fp, "Unknown Addrp");
  518.         break;
  519.     } /* switch */
  520.  
  521. /* It's okay to just throw in the brackets here because they have a
  522.    precedence level of 15, the highest value.  */
  523.  
  524.     if ((addrp->uname_tag == UNAM_NAME && addrp->user.name->vdim
  525.             || addrp->ntempelt > 1 || addrp->isarray)
  526.     && addrp->vtype != TYCHAR) {
  527.     expptr offset;
  528.  
  529.     was_array = 1;
  530.  
  531.     offset = addrp -> memoffset;
  532.     addrp->memoffset = 0;
  533.     if (ONEOF(addrp->vstg, M(STGCOMMON)|M(STGEQUIV)) &&
  534.         addrp -> uname_tag == UNAM_NAME)
  535.         offset = mkexpr (OPMINUS, offset, mkintcon (
  536.             addrp -> user.name -> voffset));
  537.  
  538.     nice_printf (fp, "[");
  539.  
  540.     offset = mkexpr (OPSLASH, offset,
  541.         ICON (typesize[addrp -> vtype] * (addrp -> Field ? 2 : 1)));
  542.     expr_out (fp, offset);
  543.     nice_printf (fp, "]");
  544.     }
  545.  
  546. /* Check for structure field reference */
  547.  
  548.     if (addrp -> Field && addrp -> uname_tag != UNAM_CONST &&
  549.         addrp -> uname_tag != UNAM_UNKNOWN) {
  550.     if (oneof_stg((addrp -> uname_tag == UNAM_NAME ? addrp -> user.name :
  551.         (Namep) NULL), addrp -> vstg, M(STGARG)|M(STGEQUIV))
  552.         && !was_array && (addrp->vclass != CLPROC || !multitype))
  553.         nice_printf (fp, "->%s", addrp -> Field);
  554.     else
  555.         nice_printf (fp, ".%s", addrp -> Field);
  556.     } /* if */
  557.  
  558. /* Check for character subscripting */
  559.  
  560.     if (addrp->vtype == TYCHAR &&
  561.         (addrp->vclass != CLPROC || addrp->uname_tag == UNAM_NAME
  562.             && addrp->user.name->vprocclass == PTHISPROC) &&
  563.         addrp -> memoffset &&
  564.         (addrp -> uname_tag != UNAM_NAME ||
  565.          addrp -> user.name -> vtype == TYCHAR) &&
  566.         (!ISICON (addrp -> memoffset) ||
  567.          (addrp -> memoffset -> constblock.Const.ci))) {
  568.  
  569.     int use_paren = 0;
  570.     expptr e = addrp -> memoffset;
  571.  
  572.     if (!e)
  573.         return;
  574.     addrp->memoffset = 0;
  575.  
  576.     if (ONEOF(addrp->vstg, M(STGCOMMON)|M(STGEQUIV))
  577.      && addrp -> uname_tag == UNAM_NAME) {
  578.         e = mkexpr (OPMINUS, e, mkintcon (addrp -> user.name -> voffset));
  579.  
  580. /* mkexpr will simplify it to zero if possible */
  581.         if (e->tag == TCONST && e->constblock.Const.ci == 0)
  582.         return;
  583.     } /* if addrp -> vstg == STGCOMMON */
  584.  
  585. /* In the worst case, parentheses might be needed OUTSIDE the expression,
  586.    too.  But since I think this subscripting can only appear as a
  587.    parameter in a procedure call, I don't think outside parens will ever
  588.    be needed.  INSIDE parens are handled below */
  589.  
  590.     nice_printf (fp, " + ");
  591.     if (e -> tag == TEXPR) {
  592.         int arg_prec = op_precedence (e -> exprblock.opcode);
  593.         int prec = op_precedence (OPPLUS);
  594.         use_paren = arg_prec && (arg_prec < prec || (arg_prec == prec &&
  595.             is_left_assoc (OPPLUS)));
  596.     } /* if e -> tag == TEXPR */
  597.     if (use_paren) nice_printf (fp, "(");
  598.     expr_out (fp, e);
  599.     if (use_paren) nice_printf (fp, ")");
  600.     } /* if */
  601. } /* out_addr */
  602.  
  603.  
  604. static void output_literal (fp, memno, cp)
  605.  FILE *fp;
  606.  int memno;
  607.  Constp cp;
  608. {
  609.     struct Literal *litp, *lastlit;
  610.     extern char *lit_name ();
  611.  
  612.     lastlit = litpool + nliterals;
  613.  
  614.     for (litp = litpool; litp < lastlit; litp++) {
  615.     if (litp -> litnum == memno)
  616.         break;
  617.     } /* for litp */
  618.  
  619.     if (litp >= lastlit)
  620.     out_const (fp, cp);
  621.     else {
  622.     nice_printf (fp, "%s", lit_name (litp));
  623.     litp->lituse++;
  624.     }
  625. } /* output_literal */
  626.  
  627.  
  628. static void output_prim (fp, primp)
  629. FILE *fp;
  630. struct Primblock *primp;
  631. {
  632.     if (primp == NULL)
  633.     return;
  634.  
  635.     out_name (fp, primp -> namep);
  636.     if (primp -> argsp)
  637.     output_arg_list (fp, primp -> argsp);
  638.  
  639.     if (primp -> fcharp != (expptr) NULL || primp -> lcharp != (expptr) NULL)
  640.     nice_printf (fp, "Sorry, no substrings yet");
  641. }
  642.  
  643.  
  644.  
  645. static void output_arg_list (fp, listp)
  646. FILE *fp;
  647. struct Listblock *listp;
  648. {
  649.     chainp arg_list;
  650.  
  651.     if (listp == (struct Listblock *) NULL || listp -> listp == (chainp) NULL)
  652.     return;
  653.  
  654.     nice_printf (fp, "(");
  655.  
  656.     for (arg_list = listp -> listp; arg_list; arg_list = arg_list -> nextp) {
  657.     expr_out (fp, (expptr) arg_list -> datap);
  658.     if (arg_list -> nextp != (chainp) NULL)
  659.  
  660. /* Might want to add a hook in here to accomodate the style setting which
  661.    wants spaces after commas */
  662.  
  663.         nice_printf (fp, ",");
  664.     } /* for arg_list */
  665.  
  666.     nice_printf (fp, ")");
  667. } /* output_arg_list */
  668.  
  669.  
  670.  
  671. static void output_unary (fp, e)
  672. FILE *fp;
  673. struct Exprblock *e;
  674. {
  675.     if (e == NULL)
  676.     return;
  677.  
  678.     switch (e -> opcode) {
  679.         case OPNEG:
  680.         if (e->vtype == TYREAL && forcedouble) {
  681.             e->opcode = OPNEG_KLUDGE;
  682.             output_binary(fp,e);
  683.             e->opcode = OPNEG;
  684.             break;
  685.             }
  686.     case OPNEG1:
  687.     case OPNOT:
  688.     case OPABS:
  689.     case OPBITNOT:
  690.     case OPWHATSIN:
  691.     case OPPREINC:
  692.     case OPPREDEC:
  693.     case OPADDR:
  694.     case OPIDENTITY:
  695.     case OPCHARCAST:
  696.     case OPDABS:
  697.         output_binary (fp, e);
  698.         break;
  699.     case OPCALL:
  700.     case OPCCALL:
  701.         nice_printf (fp, "Sorry, no OPCALL yet");
  702.         break;
  703.     default:
  704.         erri ("output_unary: bad opcode", (int) e -> opcode);
  705.         break;
  706.     } /* switch */
  707. } /* output_unary */
  708.  
  709.  
  710.  static char *
  711. findconst(m)
  712.  register long m;
  713. {
  714.     register struct Literal *litp, *litpe;
  715.  
  716.     litp = litpool;
  717.     for(litpe = litp + nliterals; litp < litpe; litp++)
  718.         if (litp->litnum ==  m)
  719.             return litp->cds[0];
  720.     Fatal("findconst failure!");
  721.     return 0;
  722.     }
  723.  
  724.  static int
  725. opconv_fudge(fp,e)
  726.  FILE *fp;
  727.  struct Exprblock *e;
  728. {
  729.     /* special handling for ichar and character*1 */
  730.     register expptr lp;
  731.     register union Expression *Offset;
  732.     register char *cp;
  733.     int lt;
  734.     char buf[8];
  735.     unsigned int k;
  736.     Namep np;
  737.  
  738.     if (!(lp = e->leftp))    /* possible with erroneous Fortran */
  739.         return 1;
  740.     lt = lp->headblock.vtype;
  741.     if (lp->addrblock.vtype == TYCHAR) {
  742.         switch(lp->tag) {
  743.             case TNAME:
  744.                 nice_printf(fp, "*");
  745.                 out_name(fp, (Namep)lp);
  746.                 return 1;
  747.             case TCONST:
  748.  tconst:
  749.                 cp = lp->constblock.Const.ccp;
  750.  tconst1:
  751.                 k = *(unsigned char *)cp;
  752.                 sprintf(buf, chr_fmt[k], k);
  753.                 nice_printf(fp, "'%s'", buf);
  754.                 return 1;
  755.             case TADDR:
  756.                 switch(lp->addrblock.vstg) {
  757.                     case STGMEMNO:
  758.                     cp = findconst(lp->addrblock.memno);
  759.                     goto tconst1;
  760.                     case STGCONST:
  761.                     goto tconst;
  762.                     }
  763.                 lt = lp->addrblock.vtype = tyint;
  764.                 Offset = lp->addrblock.memoffset;
  765.                 if (lp->addrblock.uname_tag == UNAM_NAME) {
  766.                     np = lp->addrblock.user.name;
  767.                     if (ONEOF(np->vstg,
  768.                         M(STGCOMMON)|M(STGEQUIV)))
  769.                         Offset = mkexpr(OPMINUS, Offset,
  770.                             ICON(np->voffset));
  771.                     }
  772.                 lp->addrblock.memoffset = Offset ?
  773.                     mkexpr(OPSTAR, Offset,
  774.                         ICON(typesize[tyint]))
  775.                     : ICON(0);
  776.                 lp->addrblock.isarray = 1;
  777.                 /* STGCOMMON or STGEQUIV would cause */
  778.                 /* voffset to be added in a second time */
  779.                 lp->addrblock.vstg = STGUNKNOWN;
  780.                 break;
  781.             default:
  782.                 badtag("opconv_fudge", lp->tag);
  783.             }
  784.         }
  785.     if (lt != e->vtype)
  786.         nice_printf(fp, "(%s) ",
  787.             c_type_decl(e->vtype, 0));
  788.     return 0;
  789.     }
  790.  
  791.  
  792. static void output_binary (fp, e)
  793. FILE *fp;
  794. struct Exprblock *e;
  795. {
  796.     char *format;
  797.     extern table_entry opcode_table[];
  798.     int prec;
  799.  
  800.     if (e == NULL || e -> tag != TEXPR)
  801.     return;
  802.  
  803. /* Instead of writing a huge switch, I've incorporated the output format
  804.    into a table.  Things like "%l" and "%r" stand for the left and
  805.    right subexpressions.  This should allow both prefix and infix
  806.    functions to be specified (e.g. "(%l * %r", "z_div (%l, %r").  Of
  807.    course, I should REALLY think out the ramifications of writing out
  808.    straight text, as opposed to some intermediate format, which could
  809.    figure out and optimize on the the number of required blanks (we don't
  810.    want "x - (-y)" to become "x --y", for example).  Special cases (such as
  811.    incomplete implementations) could still be implemented as part of the
  812.    switch, they will just have some dummy value instead of the string
  813.    pattern.  Another difficulty is the fact that the complex functions
  814.    will differ from the integer and real ones */
  815.  
  816. /* Handle a special case.  We don't want to output "x + - 4", or "y - - 3"
  817. */
  818.     if ((e -> opcode == OPPLUS || e -> opcode == OPMINUS) &&
  819.         e -> rightp && e -> rightp -> tag == TCONST &&
  820.         isnegative_const (&(e -> rightp -> constblock)) &&
  821.         is_negatable (&(e -> rightp -> constblock))) {
  822.  
  823.     e -> opcode = (e -> opcode == OPPLUS) ? OPMINUS : OPPLUS;
  824.     negate_const (&(e -> rightp -> constblock));
  825.     } /* if e -> opcode == PLUS or MINUS */
  826.  
  827.     prec = op_precedence (e -> opcode);
  828.     format = op_format (e -> opcode);
  829.  
  830.     if (format != SPECIAL_FMT) {
  831.     while (*format) {
  832.         if (*format == '%') {
  833.         int arg_prec, use_paren = 0;
  834.         expptr lp, rp;
  835.  
  836.         switch (*(format + 1)) {
  837.             case 'l':
  838.             lp = e->leftp;
  839.             if (lp && lp->tag == TEXPR) {
  840.                 arg_prec = op_precedence(lp->exprblock.opcode);
  841.  
  842.                 use_paren = arg_prec &&
  843.                     (arg_prec < prec || (arg_prec == prec &&
  844.                     is_right_assoc (prec)));
  845.             } /* if e -> leftp */
  846.             if (e->opcode == OPCONV && opconv_fudge(fp,e))
  847.                 break;
  848.             if (use_paren)
  849.                 nice_printf (fp, "(");
  850.                 expr_out(fp, lp);
  851.             if (use_paren)
  852.                 nice_printf (fp, ")");
  853.                 break;
  854.             case 'r':
  855.             rp = e->rightp;
  856.             if (rp && rp->tag == TEXPR) {
  857.                 arg_prec = op_precedence(rp->exprblock.opcode);
  858.  
  859.                 use_paren = arg_prec &&
  860.                     (arg_prec < prec || (arg_prec == prec &&
  861.                     is_left_assoc (prec)));
  862.                 use_paren = use_paren ||
  863.                 (rp->exprblock.opcode == OPNEG
  864.                 && prec >= op_precedence(OPMINUS));
  865.             } /* if e -> rightp */
  866.             if (use_paren)
  867.                 nice_printf (fp, "(");
  868.                 expr_out(fp, rp);
  869.             if (use_paren)
  870.                 nice_printf (fp, ")");
  871.                 break;
  872.             case '\0':
  873.             case '%':
  874.                 nice_printf (fp, "%%");
  875.                 break;
  876.             default:
  877.                 erri ("output_binary: format err: '%%%c' illegal",
  878.                 (int) *(format + 1));
  879.                 break;
  880.         } /* switch */
  881.         format += 2;
  882.         } else
  883.         nice_printf (fp, "%c", *format++);
  884.     } /* while *format */
  885.     } else {
  886.  
  887. /* Handle Special cases of formatting */
  888.  
  889.     switch (e -> opcode) {
  890.         case OPCCALL:
  891.         case OPCALL:
  892.             out_call (fp, (int) e -> opcode, e -> vtype,
  893.                     e -> vleng, e -> leftp, e -> rightp);
  894.             break;
  895.  
  896.         case OPCOMMA_ARG:
  897.             doin_setbound = 1;
  898.             nice_printf(fp, "(");
  899.             expr_out(fp, e->leftp);
  900.             nice_printf(fp, ", &");
  901.             doin_setbound = 0;
  902.             expr_out(fp, e->rightp);
  903.             nice_printf(fp, ")");
  904.             break;
  905.  
  906.         case OPADDR:
  907.         default:
  908.                 nice_printf (fp, "Sorry, can't format OPCODE '%d'",
  909.                 e -> opcode);
  910.                 break;
  911.         }
  912.  
  913.     } /* else */
  914. } /* output_binary */
  915.  
  916.  
  917. out_call (outfile, op, ftype, len, name, args)
  918. FILE *outfile;
  919. int op, ftype;
  920. expptr len, name, args;
  921. {
  922.     chainp arglist;        /* Pointer to any actual arguments */
  923.     chainp cp;            /* Iterator over argument lists */
  924.     Addrp ret_val = (Addrp) NULL;
  925.                 /* Function return value buffer, if any is
  926.                    required */
  927.     int byvalue;        /* True iff we're calling a C library
  928.                    routine */
  929.     int done_once;        /* Used for writing commas to   outfile   */
  930.     int narg, t;
  931.     register expptr q;
  932.     long L;
  933.     Argtypes *at;
  934.     Atype *A, *Ac;
  935.     Namep np;
  936.     extern int forcereal;
  937.  
  938. /* Don't use addresses if we're calling a C function */
  939.  
  940.     byvalue = op == OPCCALL;
  941.  
  942.     if (args)
  943.     arglist = args -> listblock.listp;
  944.     else
  945.     arglist = CHNULL;
  946.  
  947. /* If this is a CHARACTER function, the first argument is the result */
  948.  
  949.     if (ftype == TYCHAR)
  950.     if (ISICON (len)) {
  951.         ret_val = (Addrp) (arglist -> datap);
  952.         arglist = arglist -> nextp;
  953.     } else {
  954.         err ("adjustable character function");
  955.         return;
  956.     } /* else */
  957.  
  958. /* If this is a COMPLEX function, the first argument is the result */
  959.  
  960.     else if (ISCOMPLEX (ftype)) {
  961.     ret_val = (Addrp) (arglist -> datap);
  962.     arglist = arglist -> nextp;
  963.     } /* if ISCOMPLEX */
  964.  
  965. /* Now we can actually start to write out the function invocation */
  966.  
  967.     if (ftype == TYREAL && forcereal)
  968.     nice_printf(outfile, "(real)");
  969.     if (name -> tag == TEXPR && name -> exprblock.opcode == OPWHATSIN) {
  970.     nice_printf (outfile, "(");
  971.     np = (Namep)name->exprblock.leftp; /*expr_out will free name */
  972.     expr_out (outfile, name);
  973.     nice_printf (outfile, ")");
  974.     }
  975.     else {
  976.     np = (Namep)name;
  977.     expr_out(outfile, name);
  978.     }
  979.  
  980.     /* prepare to cast procedure parameters -- set A if we know how */
  981.  
  982.     A = Ac = 0;
  983.     if (at = np->arginfo) {
  984.     if (np->tag == TNAME && at->nargs > 0)
  985.         A = at->atypes;
  986.     if (Ansi && (at->defined || at->nargs > 0))
  987.         Ac = at->atypes;
  988.         }
  989.  
  990.     nice_printf(outfile, "(");
  991.  
  992.     if (ret_val) {
  993.     if (ISCOMPLEX (ftype))
  994.         nice_printf (outfile, "&");
  995.     expr_out (outfile, (expptr) ret_val);
  996.     if (Ac)
  997.         Ac++;
  998.  
  999. /* The length of the result of a character function is the second argument */
  1000. /* It should be in place from putcall(), so we won't touch it explicitly */
  1001.  
  1002.     } /* if ret_val */
  1003.     done_once = ret_val ? TRUE : FALSE;
  1004.  
  1005. /* Now run through the named arguments */
  1006.  
  1007.     narg = -1;
  1008.     for (cp = arglist; cp; cp = cp -> nextp, done_once = TRUE) {
  1009.  
  1010.     if (done_once)
  1011.         nice_printf (outfile, ", ");
  1012.     narg++;
  1013.  
  1014.     if (!( q = (expptr)cp->datap) )
  1015.         continue;
  1016.  
  1017.     if (q->tag == TADDR) {
  1018.         if (q->addrblock.vtype > TYERROR) {
  1019.             /* I/O block */
  1020.             nice_printf(outfile, "&%s", q->addrblock.user.ident);
  1021.             continue;
  1022.             }
  1023.         if (!byvalue && q->addrblock.isarray
  1024.         && q->addrblock.vtype != TYCHAR
  1025.         && q->addrblock.memoffset->tag == TCONST) {
  1026.  
  1027.             /* check for 0 offset -- after */
  1028.             /* correcting for equivalence. */
  1029.             L = q->addrblock.memoffset->constblock.Const.ci;
  1030.             if (ONEOF(q->addrblock.vstg, M(STGCOMMON)|M(STGEQUIV))
  1031.                     && q->addrblock.uname_tag == UNAM_NAME)
  1032.                 L -= q->addrblock.user.name->voffset;
  1033.             if (L)
  1034.                 goto skip_deref;
  1035.  
  1036.             if (Ac && narg < at->dnargs
  1037.              && q->headblock.vtype != (t = Ac[narg].type)
  1038.              && t > TYADDR && t < TYSUBR)
  1039.                 nice_printf(outfile, "(%s*)", typename[t]);
  1040.  
  1041.             /* &x[0] == x */
  1042.             /* This also prevents &sizeof(doublereal)[0] */
  1043.  
  1044.             switch(q->addrblock.uname_tag) {
  1045.                 case UNAM_NAME:
  1046.                 out_name(outfile, q->addrblock.user.name);
  1047.                 continue;
  1048.                 case UNAM_IDENT:
  1049.                 nice_printf(outfile, "%s",
  1050.                     q->addrblock.user.ident);
  1051.                 continue;
  1052.                 case UNAM_CHARP:
  1053.                 nice_printf(outfile, "%s",
  1054.                     q->addrblock.user.Charp);
  1055.                 continue;
  1056.                 case UNAM_EXTERN:
  1057.                 extern_out(outfile,
  1058.                     &extsymtab[q->addrblock.memno]);
  1059.                 continue;
  1060.                 }
  1061.             }
  1062.         }
  1063.  
  1064. /* Skip over the dereferencing operator generated only for the
  1065.    intermediate file */
  1066.  skip_deref:
  1067.     if (q -> tag == TEXPR && q -> exprblock.opcode == OPWHATSIN)
  1068.         q = q -> exprblock.leftp;
  1069.  
  1070.     if (q->headblock.vclass == CLPROC) {
  1071.         if (Castargs && (q->tag != TNAME
  1072.                 || q->nameblock.vprocclass != PTHISPROC))
  1073.         {
  1074.         if (A && (t = A[narg].type) >= 200)
  1075.             t %= 100;
  1076.         else {
  1077.             t = q->headblock.vtype;
  1078.             if (q->tag == TNAME && q->nameblock.vimpltype)
  1079.                 t = TYUNKNOWN;
  1080.             }
  1081.         nice_printf(outfile, "(%s)", usedcasts[t] = casttypes[t]);
  1082.         }
  1083.         }
  1084.     else if (Ac && narg < at->dnargs
  1085.         && q->headblock.vtype != (t = Ac[narg].type)
  1086.         && t > TYADDR && t < TYSUBR)
  1087.         nice_printf(outfile, "(%s*)", typename[t]);
  1088.  
  1089.     if ((q -> tag == TADDR || q-> tag == TNAME) &&
  1090.         (byvalue || q -> headblock.vstg != STGREG)) {
  1091.         if (q -> headblock.vtype != TYCHAR)
  1092.           if (byvalue) {
  1093.  
  1094.         if (q -> tag == TADDR &&
  1095.             q -> addrblock.uname_tag == UNAM_NAME &&
  1096.             ! q -> addrblock.user.name -> vdim &&
  1097.             oneof_stg(q -> addrblock.user.name, q -> addrblock.vstg,
  1098.                     M(STGARG)|M(STGEQUIV)) &&
  1099.             ! ISCOMPLEX(q->addrblock.user.name->vtype))
  1100.             nice_printf (outfile, "*");
  1101.         else if (q -> tag == TNAME
  1102.             && oneof_stg(&q->nameblock, q -> nameblock.vstg,
  1103.                 M(STGARG)|M(STGEQUIV))
  1104.             && !(q -> nameblock.vdim))
  1105.             nice_printf (outfile, "*");
  1106.  
  1107.           } else {
  1108.         expptr memoffset;
  1109.  
  1110.         if (q->tag == TADDR &&
  1111.             !ONEOF (q -> addrblock.vstg, M(STGEXT)|M(STGLENG))
  1112.             && (
  1113.             ONEOF(q->addrblock.vstg,
  1114.                 M(STGCOMMON)|M(STGEQUIV)|M(STGMEMNO))
  1115.             || ((memoffset = q->addrblock.memoffset)
  1116.                 && (!ISICON(memoffset)
  1117.                 || memoffset->constblock.Const.ci)))
  1118.             || ONEOF(q->addrblock.vstg,
  1119.                     M(STGINIT)|M(STGAUTO)|M(STGBSS))
  1120.                 && !q->addrblock.isarray)
  1121.             nice_printf (outfile, "&");
  1122.         else if (q -> tag == TNAME
  1123.             && !oneof_stg(&q->nameblock, q -> nameblock.vstg,
  1124.                 M(STGARG)|M(STGEXT)|M(STGEQUIV)))
  1125.             nice_printf (outfile, "&");
  1126.         } /* else */
  1127.  
  1128.         expr_out (outfile, q);
  1129.     } /* if q -> tag == TADDR || q -> tag == TNAME */
  1130.  
  1131. /* Might be a Constant expression, e.g. string length, character constants */
  1132.  
  1133.     else if (q -> tag == TCONST) {
  1134.         if (tyioint == TYLONG)
  1135.            Longfmt = "%ldL";
  1136.         out_const(outfile, &q->constblock);
  1137.         Longfmt = "%ld";
  1138.         }
  1139.  
  1140. /* Must be some other kind of expression, or register var, or constant.
  1141.    In particular, this is likely to be a temporary variable assignment
  1142.    which was generated in p1put_call */
  1143.  
  1144.     else if (!ISCOMPLEX (q -> headblock.vtype) && !ISCHAR (q)){
  1145.         int use_paren = q -> tag == TEXPR &&
  1146.             op_precedence (q -> exprblock.opcode) <=
  1147.             op_precedence (OPCOMMA);
  1148.  
  1149.         if (use_paren) nice_printf (outfile, "(");
  1150.         expr_out (outfile, q);
  1151.         if (use_paren) nice_printf (outfile, ")");
  1152.     } /* if !ISCOMPLEX */
  1153.     else
  1154.         err ("out_call:  unknown parameter");
  1155.  
  1156.     } /* for (cp = arglist */
  1157.  
  1158.     if (arglist)
  1159.     frchain (&arglist);
  1160.  
  1161.     nice_printf (outfile, ")");
  1162.  
  1163. } /* out_call */
  1164.  
  1165.  
  1166.  char *
  1167. flconst(buf, x)
  1168.  char *buf, *x;
  1169. {
  1170.     sprintf(buf, fl_fmt_string, x);
  1171.     return buf;
  1172.     }
  1173.  
  1174.  char *
  1175. dtos(x)
  1176.  double x;
  1177. {
  1178.     static char buf[64];
  1179.     sprintf(buf, db_fmt_string, x);
  1180.     return buf;
  1181.     }
  1182.  
  1183. char tr_tab[Table_size];
  1184.  
  1185. /* out_init -- Initialize the data structures used by the routines in
  1186.    output.c.  These structures include the output format to be used for
  1187.    Float, Double, Complex, and Double Complex constants. */
  1188.  
  1189. void out_init ()
  1190. {
  1191.     extern int tab_size;
  1192.     register char *s;
  1193.  
  1194.     s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+-.";
  1195.     while(*s)
  1196.     tr_tab[*s++] = 3;
  1197.     tr_tab['>'] = 1;
  1198.  
  1199.     opeqable[OPPLUS] = 1;
  1200.     opeqable[OPMINUS] = 1;
  1201.     opeqable[OPSTAR] = 1;
  1202.     opeqable[OPSLASH] = 1;
  1203.     opeqable[OPMOD] = 1;
  1204.     opeqable[OPLSHIFT] = 1;
  1205.     opeqable[OPBITAND] = 1;
  1206.     opeqable[OPBITXOR] = 1;
  1207.     opeqable[OPBITOR ] = 1;
  1208.  
  1209.  
  1210. /* Set the output format for both types of floating point constants */
  1211.  
  1212.     if (fl_fmt_string == NULL || *fl_fmt_string == '\0')
  1213.     fl_fmt_string = Ansi == 1 ? "%sf" : "(float)%s";
  1214.  
  1215.     if (db_fmt_string == NULL || *db_fmt_string == '\0')
  1216.     db_fmt_string = "%.17g";
  1217.  
  1218. /* Set the output format for both types of complex constants.  They will
  1219.    have string parameters rather than float or double so that the decimal
  1220.    point may be added to the strings generated by the {db,fl}_fmt_string
  1221.    formats above */
  1222.  
  1223.     if (cm_fmt_string == NULL || *cm_fmt_string == '\0') {
  1224.     cm_fmt_string = "{%s,%s}";
  1225.     } /* if cm_fmt_string == NULL */
  1226.  
  1227.     if (dcm_fmt_string == NULL || *dcm_fmt_string == '\0') {
  1228.     dcm_fmt_string = "{%s,%s}";
  1229.     } /* if dcm_fmt_string == NULL */
  1230.  
  1231.     tab_size = 4;
  1232. } /* out_init */
  1233.  
  1234.  
  1235. void extern_out (fp, extsym)
  1236. FILE *fp;
  1237. Extsym *extsym;
  1238. {
  1239.     if (extsym == (Extsym *) NULL)
  1240.     return;
  1241.  
  1242.     nice_printf (fp, "%s", extsym->cextname);
  1243.  
  1244. } /* extern_out */
  1245.  
  1246.  
  1247.  
  1248. static void output_list (fp, listp)
  1249. FILE *fp;
  1250. struct Listblock *listp;
  1251. {
  1252.     int did_one = 0;
  1253.     chainp elts;
  1254.  
  1255.     nice_printf (fp, "(");
  1256.     if (listp)
  1257.     for (elts = listp -> listp; elts; elts = elts -> nextp) {
  1258.         if (elts -> datap) {
  1259.         if (did_one)
  1260.             nice_printf (fp, ", ");
  1261.         expr_out (fp, (expptr) elts -> datap);
  1262.         did_one = 1;
  1263.         } /* if elts -> datap */
  1264.     } /* for elts */
  1265.     nice_printf (fp, ")");
  1266. } /* output_list */
  1267.  
  1268.  
  1269. void out_asgoto (outfile, expr)
  1270. FILE *outfile;
  1271. expptr expr;
  1272. {
  1273.     char *user_label();
  1274.     chainp value;
  1275.     Namep namep;
  1276.     int k;
  1277.  
  1278.     if (expr == (expptr) NULL) {
  1279.     err ("out_asgoto:  NULL variable expr");
  1280.     return;
  1281.     } /* if expr */
  1282.  
  1283.     nice_printf (outfile, Ansi ? "switch (" : "switch ((int)"); /*)*/
  1284.     expr_out (outfile, expr);
  1285.     nice_printf (outfile, ") {\n");
  1286.     next_tab (outfile);
  1287.  
  1288. /* The initial addrp value will be stored as a namep pointer */
  1289.  
  1290.     switch(expr->tag) {
  1291.     case TNAME:
  1292.         /* local variable */
  1293.         namep = &expr->nameblock;
  1294.         break;
  1295.     case TEXPR:
  1296.         if (expr->exprblock.opcode == OPWHATSIN
  1297.          && expr->exprblock.leftp->tag == TNAME)
  1298.             /* argument */
  1299.             namep = &expr->exprblock.leftp->nameblock;
  1300.         else
  1301.             goto bad;
  1302.         break;
  1303.     case TADDR:
  1304.         if (expr->addrblock.uname_tag == UNAM_NAME) {
  1305.             /* initialized local variable */
  1306.             namep = expr->addrblock.user.name;
  1307.             break;
  1308.             }
  1309.     default:
  1310.  bad:
  1311.         err("out_asgoto:  bad expr");
  1312.         return;
  1313.     }
  1314.  
  1315.     for(k = 0, value = namep -> varxptr.assigned_values; value;
  1316.         value = value->nextp, k++) {
  1317.     nice_printf (outfile, "case %d: goto %s;\n", k,
  1318.         user_label((long)value->datap));
  1319.     } /* for value */
  1320.     prev_tab (outfile);
  1321.  
  1322.     nice_printf (outfile, "}\n");
  1323. } /* out_asgoto */
  1324.  
  1325. void out_if (outfile, expr)
  1326. FILE *outfile;
  1327. expptr expr;
  1328. {
  1329.     nice_printf (outfile, "if (");
  1330.     expr_out (outfile, expr);
  1331.     nice_printf (outfile, ") {\n");
  1332.     next_tab (outfile);
  1333. } /* out_if */
  1334.  
  1335.  static void
  1336. output_rbrace(outfile, s)
  1337.  FILE *outfile;
  1338.  char *s;
  1339. {
  1340.     extern int last_was_label;
  1341.     register char *fmt;
  1342.  
  1343.     if (last_was_label) {
  1344.         last_was_label = 0;
  1345.         fmt = ";%s";
  1346.         }
  1347.     else
  1348.         fmt = "%s";
  1349.     nice_printf(outfile, fmt, s);
  1350.     }
  1351.  
  1352. void out_else (outfile)
  1353. FILE *outfile;
  1354. {
  1355.     prev_tab (outfile);
  1356.     output_rbrace(outfile, "} else {\n");
  1357.     next_tab (outfile);
  1358. } /* out_else */
  1359.  
  1360. void elif_out (outfile, expr)
  1361. FILE *outfile;
  1362. expptr expr;
  1363. {
  1364.     prev_tab (outfile);
  1365.     output_rbrace(outfile, "} else ");
  1366.     out_if (outfile, expr);
  1367. } /* elif_out */
  1368.  
  1369. void endif_out (outfile)
  1370. FILE *outfile;
  1371. {
  1372.     prev_tab (outfile);
  1373.     output_rbrace(outfile, "}\n");
  1374. } /* endif_out */
  1375.  
  1376. void end_else_out (outfile)
  1377. FILE *outfile;
  1378. {
  1379.     prev_tab (outfile);
  1380.     output_rbrace(outfile, "}\n");
  1381. } /* end_else_out */
  1382.  
  1383.  
  1384.  
  1385. void compgoto_out (outfile, index, labels)
  1386. FILE *outfile;
  1387. expptr index, labels;
  1388. {
  1389.     char *s1, *s2;
  1390.  
  1391.     if (index == ENULL)
  1392.     err ("compgoto_out:  null index for computed goto");
  1393.     else if (labels && labels -> tag != TLIST)
  1394.     erri ("compgoto_out:  expected label list, got tag '%d'",
  1395.         labels -> tag);
  1396.     else {
  1397.     extern char *user_label ();
  1398.     chainp elts;
  1399.     int i = 1;
  1400.  
  1401.     s2 = /*(*/ ") {\n"; /*}*/
  1402.     if (Ansi)
  1403.         s1 = "switch ("; /*)*/
  1404.     else if (index->tag == TNAME || index->tag == TEXPR
  1405.                 && index->exprblock.opcode == OPWHATSIN)
  1406.         s1 = "switch ((int)"; /*)*/
  1407.     else {
  1408.         s1 = "switch ((int)(";
  1409.         s2 = ")) {\n"; /*}*/
  1410.         }
  1411.     nice_printf(outfile, s1);
  1412.     expr_out (outfile, index);
  1413.     nice_printf (outfile, s2);
  1414.     next_tab (outfile);
  1415.  
  1416.     for (elts = labels -> listblock.listp; elts; elts = elts -> nextp, i++) {
  1417.         if (elts -> datap) {
  1418.         if (ISICON(((expptr) (elts -> datap))))
  1419.             nice_printf (outfile, "case %d:  goto %s;\n", i,
  1420.             user_label(((expptr)(elts->datap))->constblock.Const.ci));
  1421.         else
  1422.             err ("compgoto_out:  bad label in label list");
  1423.         } /* if (elts -> datap) */
  1424.     } /* for elts */
  1425.     prev_tab (outfile);
  1426.     nice_printf (outfile, /*{*/ "}\n");
  1427.     } /* else */
  1428. } /* compgoto_out */
  1429.  
  1430.  
  1431. void out_for (outfile, init, test, inc)
  1432. FILE *outfile;
  1433. expptr init, test, inc;
  1434. {
  1435.     nice_printf (outfile, "for (");
  1436.     expr_out (outfile, init);
  1437.     nice_printf (outfile, "; ");
  1438.     expr_out (outfile, test);
  1439.     nice_printf (outfile, "; ");
  1440.     expr_out (outfile, inc);
  1441.     nice_printf (outfile, ") {\n");
  1442.     next_tab (outfile);
  1443. } /* out_for */
  1444.  
  1445.  
  1446. void out_end_for (outfile)
  1447. FILE *outfile;
  1448. {
  1449.     prev_tab (outfile);
  1450.     nice_printf (outfile, "}\n");
  1451. } /* out_end_for */
  1452.