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

  1. /****************************************************************
  2. Copyright 1990, 1991, 1993, 1994 by AT&T Bell Laboratories and Bellcore.
  3.  
  4. Permission to use, copy, modify, and distribute this software
  5. and its documentation for any purpose and without fee is hereby
  6. granted, provided that the above copyright notice appear in all
  7. copies and that both that the copyright notice and this
  8. permission notice and warranty disclaimer appear in supporting
  9. documentation, and that the names of AT&T Bell Laboratories or
  10. Bellcore or any of their entities not be used in advertising or
  11. publicity pertaining to distribution of the software without
  12. specific, written prior permission.
  13.  
  14. AT&T and Bellcore disclaim all warranties with regard to this
  15. software, including all implied warranties of merchantability
  16. and fitness.  In no event shall AT&T or Bellcore be liable for
  17. any special, indirect or consequential damages or any damages
  18. whatsoever resulting from loss of use, data or profits, whether
  19. in an action of contract, negligence or other tortious action,
  20. arising out of or in connection with the use or performance of
  21. this software.
  22. ****************************************************************/
  23.  
  24. #include "defs.h"
  25. #include "p1defs.h"
  26. #include "output.h"
  27. #include "names.h"
  28.  
  29.  
  30. static void p1_addr Argdcl((Addrp));
  31. static void p1_big_addr Argdcl((Addrp));
  32. static void p1_binary Argdcl((Exprp));
  33. static void p1_const Argdcl((Constp));
  34. static void p1_list Argdcl((struct Listblock*));
  35. static void p1_literal Argdcl((long int));
  36. static void p1_name Argdcl((Namep));
  37. static void p1_unary Argdcl((Exprp));
  38. static void p1putd Argdcl((int, long int));
  39. static void p1putdd Argdcl((int, int, int));
  40. static void p1putddd Argdcl((int, int, int, int));
  41. static void p1putdds Argdcl((int, int, int, char*));
  42. static void p1putds Argdcl((int, int, char*));
  43. static void p1putn Argdcl((int, int, char*));
  44.  
  45.  
  46. /* p1_comment -- save the text of a Fortran comment in the intermediate
  47.    file.  Make sure that there are no spurious "/ *" or "* /" characters by
  48.    mapping them onto "/+" and "+/".   str   is assumed to hold no newlines and be
  49.    null terminated; it may be modified by this function. */
  50.  
  51.  void
  52. #ifdef KR_headers
  53. p1_comment(str)
  54.     char *str;
  55. #else
  56. p1_comment(char *str)
  57. #endif
  58. {
  59.     register unsigned char *pointer, *ustr;
  60.  
  61.     if (!str)
  62.     return;
  63.  
  64. /* Get rid of any open or close comment combinations that may be in the
  65.    Fortran input */
  66.  
  67.     ustr = (unsigned char *)str;
  68.     for(pointer = ustr; *pointer; pointer++)
  69.         if (*pointer == '*' && (pointer[1] == '/'
  70.                     || pointer > ustr && pointer[-1] == '/'))
  71.             *pointer = '+';
  72.     /* trim trailing white space */
  73. #ifdef isascii
  74.     while(--pointer >= ustr && (!isascii(*pointer) || isspace(*pointer)));
  75. #else
  76.     while(--pointer >= ustr && isspace(*pointer));
  77. #endif
  78.     pointer[1] = 0;
  79.     p1puts (P1_COMMENT, str);
  80. } /* p1_comment */
  81.  
  82. /* p1_name -- Writes the address of a hash table entry into the
  83.    intermediate file */
  84.  
  85.  static void
  86. #ifdef KR_headers
  87. p1_name(namep)
  88.     Namep namep;
  89. #else
  90. p1_name(Namep namep)
  91. #endif
  92. {
  93.     p1putd (P1_NAME_POINTER, (long) namep);
  94.     namep->visused = 1;
  95. } /* p1_name */
  96.  
  97.  
  98.  
  99.  void
  100. #ifdef KR_headers
  101. p1_expr(expr)
  102.     expptr expr;
  103. #else
  104. p1_expr(expptr expr)
  105. #endif
  106. {
  107. /* An opcode of 0 means a null entry */
  108.  
  109.     if (expr == ENULL) {
  110.     p1putdd (P1_EXPR, 0, TYUNKNOWN);    /* Should this be TYERROR? */
  111.     return;
  112.     } /* if (expr == ENULL) */
  113.  
  114.     switch (expr -> tag) {
  115.         case TNAME:
  116.         p1_name ((Namep) expr);
  117.         return;
  118.     case TCONST:
  119.         p1_const(&expr->constblock);
  120.         return;
  121.     case TEXPR:
  122.         /* Fall through the switch */
  123.         break;
  124.     case TADDR:
  125.         p1_addr (&(expr -> addrblock));
  126.         goto freeup;
  127.     case TPRIM:
  128.         warn ("p1_expr:  got TPRIM");
  129.         return;
  130.     case TLIST:
  131.         p1_list (&(expr->listblock));
  132.         frchain( &(expr->listblock.listp) );
  133.         return;
  134.     case TERROR:
  135.         return;
  136.     default:
  137.         erri ("p1_expr: bad tag '%d'", (int) (expr -> tag));
  138.         return;
  139.     }
  140.  
  141. /* Now we know that the tag is TEXPR */
  142.  
  143.     if (is_unary_op (expr -> exprblock.opcode))
  144.     p1_unary (&(expr -> exprblock));
  145.     else if (is_binary_op (expr -> exprblock.opcode))
  146.     p1_binary (&(expr -> exprblock));
  147.     else
  148.     erri ("p1_expr:  bad opcode '%d'", (int) expr -> exprblock.opcode);
  149.  freeup:
  150.     free((char *)expr);
  151.  
  152. } /* p1_expr */
  153.  
  154.  
  155.  
  156.  static void
  157. #ifdef KR_headers
  158. p1_const(cp)
  159.     register Constp cp;
  160. #else
  161. p1_const(register Constp cp)
  162. #endif
  163. {
  164.     int type = cp->vtype;
  165.     expptr vleng = cp->vleng;
  166.     union Constant *c = &cp->Const;
  167.     char cdsbuf0[64], cdsbuf1[64];
  168.     char *cds0, *cds1;
  169.  
  170.     switch (type) {
  171.     case TYINT1:
  172.         case TYSHORT:
  173.     case TYLONG:
  174. #ifdef TYQUAD
  175.     case TYQUAD:
  176. #endif
  177.     case TYLOGICAL:
  178.     case TYLOGICAL1:
  179.     case TYLOGICAL2:
  180.         fprintf(pass1_file, "%d: %d %ld\n", P1_CONST, type, c->ci);
  181.         break;
  182.     case TYREAL:
  183.     case TYDREAL:
  184.         fprintf(pass1_file, "%d: %d %s\n", P1_CONST, type,
  185.             cp->vstg ? c->cds[0] : cds(dtos(c->cd[0]), cdsbuf0));
  186.         break;
  187.     case TYCOMPLEX:
  188.     case TYDCOMPLEX:
  189.         if (cp->vstg) {
  190.             cds0 = c->cds[0];
  191.             cds1 = c->cds[1];
  192.             }
  193.         else {
  194.             cds0 = cds(dtos(c->cd[0]), cdsbuf0);
  195.             cds1 = cds(dtos(c->cd[1]), cdsbuf1);
  196.             }
  197.         fprintf(pass1_file, "%d: %d %s %s\n", P1_CONST, type,
  198.             cds0, cds1);
  199.         break;
  200.     case TYCHAR:
  201.         if (vleng && !ISICON (vleng))
  202.         erri("p1_const:  bad vleng '%d'\n", (int) vleng);
  203.         else
  204.         fprintf(pass1_file, "%d: %d %lx\n", P1_CONST, type,
  205.             cpexpr((expptr)cp));
  206.         break;
  207.     default:
  208.         erri ("p1_const:  bad constant type '%d'", type);
  209.         break;
  210.     } /* switch */
  211. } /* p1_const */
  212.  
  213.  
  214.  void
  215. #ifdef KR_headers
  216. p1_asgoto(addrp)
  217.     Addrp addrp;
  218. #else
  219. p1_asgoto(Addrp addrp)
  220. #endif
  221. {
  222.     p1put (P1_ASGOTO);
  223.     p1_addr (addrp);
  224. } /* p1_asgoto */
  225.  
  226.  
  227.  void
  228. #ifdef KR_headers
  229. p1_goto(stateno)
  230.     ftnint stateno;
  231. #else
  232. p1_goto(ftnint stateno)
  233. #endif
  234. {
  235.     p1putd (P1_GOTO, stateno);
  236. } /* p1_goto */
  237.  
  238.  
  239.  static void
  240. #ifdef KR_headers
  241. p1_addr(addrp)
  242.     register struct Addrblock *addrp;
  243. #else
  244. p1_addr(register struct Addrblock *addrp)
  245. #endif
  246. {
  247.     int stg;
  248.  
  249.     if (addrp == (struct Addrblock *) NULL)
  250.     return;
  251.  
  252.     stg = addrp -> vstg;
  253.  
  254.     if (ONEOF(stg, M(STGINIT)|M(STGREG))
  255.     || ONEOF(stg, M(STGCOMMON)|M(STGEQUIV)) &&
  256.         (!ISICON(addrp->memoffset)
  257.         || (addrp->uname_tag == UNAM_NAME
  258.             ? addrp->memoffset->constblock.Const.ci
  259.                 != addrp->user.name->voffset
  260.             : addrp->memoffset->constblock.Const.ci))
  261.     || ONEOF(stg, M(STGBSS)|M(STGINIT)|M(STGAUTO)|M(STGARG)) &&
  262.         (!ISICON(addrp->memoffset)
  263.             || addrp->memoffset->constblock.Const.ci)
  264.     || addrp->Field || addrp->isarray || addrp->vstg == STGLENG)
  265.     {
  266.         p1_big_addr (addrp);
  267.         return;
  268.     }
  269.  
  270. /* Write out a level of indirection for non-array arguments, which have
  271.    addrp -> memoffset   set and are handled by   p1_big_addr().
  272.    Lengths are passed by value, so don't check STGLENG
  273.    28-Jun-89 (dmg)  Added the check for != TYCHAR
  274.  */
  275.  
  276.     if (oneof_stg ( addrp -> uname_tag == UNAM_NAME ? addrp -> user.name : NULL,
  277.         stg, M(STGARG)|M(STGEQUIV)) && addrp->vtype != TYCHAR) {
  278.     p1putdd (P1_EXPR, OPWHATSIN, addrp -> vtype);
  279.     p1_expr (ENULL);    /* Put dummy   vleng   */
  280.     } /* if stg == STGARG */
  281.  
  282.     switch (addrp -> uname_tag) {
  283.         case UNAM_NAME:
  284.         p1_name (addrp -> user.name);
  285.         break;
  286.     case UNAM_IDENT:
  287.         p1putdds(P1_IDENT, addrp->vtype, addrp->vstg,
  288.                 addrp->user.ident);
  289.         break;
  290.     case UNAM_CHARP:
  291.         p1putdds(P1_CHARP, addrp->vtype, addrp->vstg,
  292.                 addrp->user.Charp);
  293.         break;
  294.     case UNAM_EXTERN:
  295.         p1putd (P1_EXTERN, (long) addrp -> memno);
  296.         if (addrp->vclass == CLPROC)
  297.         extsymtab[addrp->memno].extype = addrp->vtype;
  298.         break;
  299.     case UNAM_CONST:
  300.         if (addrp -> memno != BAD_MEMNO)
  301.         p1_literal (addrp -> memno);
  302.         else
  303.         p1_const((struct Constblock *)addrp);
  304.         break;
  305.     case UNAM_UNKNOWN:
  306.     default:
  307.         erri ("p1_addr:  unknown uname_tag '%d'", addrp -> uname_tag);
  308.         break;
  309.     } /* switch */
  310. } /* p1_addr */
  311.  
  312.  
  313.  static void
  314. #ifdef KR_headers
  315. p1_list(listp)
  316.     struct Listblock *listp;
  317. #else
  318. p1_list(struct Listblock *listp)
  319. #endif
  320. {
  321.     chainp lis;
  322.     int count = 0;
  323.  
  324.     if (listp == (struct Listblock *) NULL)
  325.     return;
  326.  
  327. /* Count the number of parameters in the list */
  328.  
  329.     for (lis = listp -> listp; lis; lis = lis -> nextp)
  330.     count++;
  331.  
  332.     p1putddd (P1_LIST, listp -> tag, listp -> vtype, count);
  333.  
  334.     for (lis = listp -> listp; lis; lis = lis -> nextp)
  335.     p1_expr ((expptr) lis -> datap);
  336.  
  337. } /* p1_list */
  338.  
  339.  
  340.  void
  341. #ifdef KR_headers
  342. p1_label(lab)
  343.     long lab;
  344. #else
  345. p1_label(long lab)
  346. #endif
  347. {
  348.     if (parstate < INDATA)
  349.         earlylabs = mkchain((char *)lab, earlylabs);
  350.     else
  351.         p1putd (P1_LABEL, lab);
  352.     }
  353.  
  354.  
  355.  
  356.  static void
  357. #ifdef KR_headers
  358. p1_literal(memno)
  359.     long memno;
  360. #else
  361. p1_literal(long memno)
  362. #endif
  363. {
  364.     p1putd (P1_LITERAL, memno);
  365. } /* p1_literal */
  366.  
  367.  
  368.  void
  369. #ifdef KR_headers
  370. p1_if(expr)
  371.     expptr expr;
  372. #else
  373. p1_if(expptr expr)
  374. #endif
  375. {
  376.     p1put (P1_IF);
  377.     p1_expr (expr);
  378. } /* p1_if */
  379.  
  380.  
  381.  
  382.  
  383.  void
  384. #ifdef KR_headers
  385. p1_elif(expr)
  386.     expptr expr;
  387. #else
  388. p1_elif(expptr expr)
  389. #endif
  390. {
  391.     p1put (P1_ELIF);
  392.     p1_expr (expr);
  393. } /* p1_elif */
  394.  
  395.  
  396.  
  397.  
  398.  void
  399. p1_else(Void)
  400. {
  401.     p1put (P1_ELSE);
  402. } /* p1_else */
  403.  
  404.  
  405.  
  406.  
  407.  void
  408. p1_endif(Void)
  409. {
  410.     p1put (P1_ENDIF);
  411. } /* p1_endif */
  412.  
  413.  
  414.  
  415.  
  416.  void
  417. p1else_end(Void)
  418. {
  419.     p1put (P1_ENDELSE);
  420. } /* p1else_end */
  421.  
  422.  
  423.  static void
  424. #ifdef KR_headers
  425. p1_big_addr(addrp)
  426.     Addrp addrp;
  427. #else
  428. p1_big_addr(Addrp addrp)
  429. #endif
  430. {
  431.     if (addrp == (Addrp) NULL)
  432.     return;
  433.  
  434.     p1putn (P1_ADDR, (int)sizeof(struct Addrblock), (char *) addrp);
  435.     p1_expr (addrp -> vleng);
  436.     p1_expr (addrp -> memoffset);
  437.     if (addrp->uname_tag == UNAM_NAME)
  438.     addrp->user.name->visused = 1;
  439. } /* p1_big_addr */
  440.  
  441.  
  442.  
  443.  static void
  444. #ifdef KR_headers
  445. p1_unary(e)
  446.     struct Exprblock *e;
  447. #else
  448. p1_unary(struct Exprblock *e)
  449. #endif
  450. {
  451.     if (e == (struct Exprblock *) NULL)
  452.     return;
  453.  
  454.     p1putdd (P1_EXPR, (int) e -> opcode, e -> vtype);
  455.     p1_expr (e -> vleng);
  456.  
  457.     switch (e -> opcode) {
  458.         case OPNEG:
  459.     case OPNEG1:
  460.     case OPNOT:
  461.     case OPABS:
  462.     case OPBITNOT:
  463.     case OPPREINC:
  464.     case OPPREDEC:
  465.     case OPADDR:
  466.     case OPIDENTITY:
  467.     case OPCHARCAST:
  468.     case OPDABS:
  469.         p1_expr(e -> leftp);
  470.         break;
  471.     default:
  472.         erri ("p1_unary: bad opcode '%d'", (int) e -> opcode);
  473.         break;
  474.     } /* switch */
  475.  
  476. } /* p1_unary */
  477.  
  478.  
  479.  static void
  480. #ifdef KR_headers
  481. p1_binary(e)
  482.     struct Exprblock *e;
  483. #else
  484. p1_binary(struct Exprblock *e)
  485. #endif
  486. {
  487.     if (e == (struct Exprblock *) NULL)
  488.     return;
  489.  
  490.     p1putdd (P1_EXPR, e -> opcode, e -> vtype);
  491.     p1_expr (e -> vleng);
  492.     p1_expr (e -> leftp);
  493.     p1_expr (e -> rightp);
  494. } /* p1_binary */
  495.  
  496.  
  497.  void
  498. #ifdef KR_headers
  499. p1_head(class, name)
  500.     int class;
  501.     char *name;
  502. #else
  503. p1_head(int class, char *name)
  504. #endif
  505. {
  506.     p1putds (P1_HEAD, class, name ? name : "");
  507. } /* p1_head */
  508.  
  509.  
  510.  void
  511. #ifdef KR_headers
  512. p1_subr_ret(retexp)
  513.     expptr retexp;
  514. #else
  515. p1_subr_ret(expptr retexp)
  516. #endif
  517. {
  518.  
  519.     p1put (P1_SUBR_RET);
  520.     p1_expr (cpexpr(retexp));
  521. } /* p1_subr_ret */
  522.  
  523.  
  524.  
  525.  void
  526. #ifdef KR_headers
  527. p1comp_goto(index, count, labels)
  528.     expptr index;
  529.     int count;
  530.     struct Labelblock **labels;
  531. #else
  532. p1comp_goto(expptr index, int count, struct Labelblock **labels)
  533. #endif
  534. {
  535.     struct Constblock c;
  536.     int i;
  537.     register struct Labelblock *L;
  538.  
  539.     p1put (P1_COMP_GOTO);
  540.     p1_expr (index);
  541.  
  542. /* Write out a P1_LIST directly, to avoid the overhead of allocating a
  543.    list before it's needed HACK HACK HACK */
  544.  
  545.     p1putddd (P1_LIST, TLIST, TYUNKNOWN, count);
  546.     c.vtype = TYLONG;
  547.     c.vleng = 0;
  548.  
  549.     for (i = 0; i < count; i++) {
  550.     L = labels[i];
  551.     L->labused = 1;
  552.     c.Const.ci = L->stateno;
  553.     p1_const(&c);
  554.     } /* for i = 0 */
  555. } /* p1comp_goto */
  556.  
  557.  
  558.  
  559.  void
  560. #ifdef KR_headers
  561. p1_for(init, test, inc)
  562.     expptr init;
  563.     expptr test;
  564.     expptr inc;
  565. #else
  566. p1_for(expptr init, expptr test, expptr inc)
  567. #endif
  568. {
  569.     p1put (P1_FOR);
  570.     p1_expr (init);
  571.     p1_expr (test);
  572.     p1_expr (inc);
  573. } /* p1_for */
  574.  
  575.  
  576.  void
  577. p1for_end(Void)
  578. {
  579.     p1put (P1_ENDFOR);
  580. } /* p1for_end */
  581.  
  582.  
  583.  
  584.  
  585. /* ----------------------------------------------------------------------
  586.    The intermediate file actually gets written ONLY by the routines below.
  587.    To change the format of the file, you need only change these routines.
  588.    ----------------------------------------------------------------------
  589. */
  590.  
  591.  
  592. /* p1puts -- Put a typed string into the Pass 1 intermediate file.  Assumes that
  593.    str   contains no newlines and is null-terminated. */
  594.  
  595.  void
  596. #ifdef KR_headers
  597. p1puts(type, str)
  598.     int type;
  599.     char *str;
  600. #else
  601. p1puts(int type, char *str)
  602. #endif
  603. {
  604.     fprintf (pass1_file, "%d: %s\n", type, str);
  605. } /* p1puts */
  606.  
  607.  
  608. /* p1putd -- Put a typed integer into the Pass 1 intermediate file. */
  609.  
  610.  static void
  611. #ifdef KR_headers
  612. p1putd(type, value)
  613.     int type;
  614.     long value;
  615. #else
  616. p1putd(int type, long value)
  617. #endif
  618. {
  619.     fprintf (pass1_file, "%d: %ld\n", type, value);
  620. } /* p1_putd */
  621.  
  622.  
  623. /* p1putdd -- Put a typed pair of integers into the intermediate file. */
  624.  
  625.  static void
  626. #ifdef KR_headers
  627. p1putdd(type, v1, v2)
  628.     int type;
  629.     int v1;
  630.     int v2;
  631. #else
  632. p1putdd(int type, int v1, int v2)
  633. #endif
  634. {
  635.     fprintf (pass1_file, "%d: %d %d\n", type, v1, v2);
  636. } /* p1putdd */
  637.  
  638.  
  639. /* p1putddd -- Put a typed triple of integers into the intermediate file. */
  640.  
  641.  static void
  642. #ifdef KR_headers
  643. p1putddd(type, v1, v2, v3)
  644.     int type;
  645.     int v1;
  646.     int v2;
  647.     int v3;
  648. #else
  649. p1putddd(int type, int v1, int v2, int v3)
  650. #endif
  651. {
  652.     fprintf (pass1_file, "%d: %d %d %d\n", type, v1, v2, v3);
  653. } /* p1putddd */
  654.  
  655.  union dL {
  656.     double d;
  657.     long L[2];
  658.     };
  659.  
  660.  static void
  661. #ifdef KR_headers
  662. p1putn(type, count, str)
  663.     int type;
  664.     int count;
  665.     char *str;
  666. #else
  667. p1putn(int type, int count, char *str)
  668. #endif
  669. {
  670.     int i;
  671.  
  672.     fprintf (pass1_file, "%d: ", type);
  673.  
  674.     for (i = 0; i < count; i++)
  675.     putc (str[i], pass1_file);
  676.  
  677.     putc ('\n', pass1_file);
  678. } /* p1putn */
  679.  
  680.  
  681.  
  682. /* p1put -- Put a type marker into the intermediate file. */
  683.  
  684.  void
  685. #ifdef KR_headers
  686. p1put(type)
  687.     int type;
  688. #else
  689. p1put(int type)
  690. #endif
  691. {
  692.     fprintf (pass1_file, "%d:\n", type);
  693. } /* p1put */
  694.  
  695.  
  696.  
  697.  static void
  698. #ifdef KR_headers
  699. p1putds(type, i, str)
  700.     int type;
  701.     int i;
  702.     char *str;
  703. #else
  704. p1putds(int type, int i, char *str)
  705. #endif
  706. {
  707.     fprintf (pass1_file, "%d: %d %s\n", type, i, str);
  708. } /* p1putds */
  709.  
  710.  
  711.  static void
  712. #ifdef KR_headers
  713. p1putdds(token, type, stg, str)
  714.     int token;
  715.     int type;
  716.     int stg;
  717.     char *str;
  718. #else
  719. p1putdds(int token, int type, int stg, char *str)
  720. #endif
  721. {
  722.     fprintf (pass1_file, "%d: %d %d %s\n", token, type, stg, str);
  723. } /* p1putdds */
  724.