home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 357_01 / cstar1.exe / PR.C < prev    next >
C/C++ Source or Header  |  1991-06-18  |  14KB  |  765 lines

  1. /*
  2.     C* -- Console output routines used only in SHERLOCK traces.
  3.  
  4.     source:  pr.c
  5.     started: May 19, 1986
  6.     version:
  7.         December 15, 1986
  8.         March 7, 1989
  9.  
  10.     PUBLIC DOMAIN SOFTWARE
  11.  
  12.     The CSTAR program was placed in    the public domain on June 15, 1991,
  13.     by its author and sole owner,
  14.  
  15.         Edward K. Ream
  16.         1617 Monroe Street
  17.         Madison, WI 53711
  18.         (608) 257-0802
  19.  
  20.     CSTAR may be used for any commercial or non-commercial purpose.
  21.  
  22.     See cstar.h or cstar.c for a DISCLAIMER OF WARRANTIES.
  23. */
  24.  
  25. #include "cstar.h"
  26.  
  27. #ifdef SHERLOCK
  28.  
  29. /*
  30.     Externally visible routines:
  31. */
  32. void    pr_arg        (struct node *p);
  33. void    pr_expr        (struct node *p);
  34. void    pr_iblock    (struct iblock *p);
  35. void    pr_list        (struct node *p);
  36. void    pr_loc        (struct node *p);
  37. void    pr_sclass    (int sclass);
  38. void    pr_type        (struct type_node *t);
  39.  
  40. /* ----- NOT YET
  41. char *    ps_bool        (bool b);
  42. char *    ps_ch        (int c);
  43. char *    ps_op        (en_tokens op);
  44. char *    ps_scope    (en_scope scope);
  45. char *    ps_str        (char *s);
  46. char *    ps_tok        (void);
  47. ----- */
  48.  
  49. /*
  50.     Internal routines:
  51. */
  52. static void pr_ty1    (register struct type_node * t,
  53.             bool usexpand, bool fnexpand);
  54. static void pr_stat    (struct node *p);
  55.  
  56. /*
  57.     Output an argument, which is a label or a loc_node.
  58. */
  59. void
  60. pr_arg(struct node * p)
  61. {
  62.     struct st_node *id;
  63.  
  64.     SL_DISABLE();
  65.  
  66.     /* WARNING: the label nodes and the id nodes aren't the same kind */
  67.     if (p == NULL) {
  68.         return;
  69.     }
  70.  
  71.     switch(p -> c_code) {
  72.  
  73.     case O_LABEL:
  74.         printf("L%s", p -> c_labnum);
  75.         return;
  76.  
  77.     case O_ULABEL:
  78.         /* Internal label. */
  79.         if (p -> c_labnum) {
  80.             printf("U%s", p -> c_labnum);
  81.         }
  82.         else {
  83.             printf(p -> c_labsym);
  84.         }
  85.         break;
  86.  
  87.     case O_LITERAL:
  88.         printf(p -> c_lit);
  89.         return;
  90.  
  91.     case ID_TOK:
  92.         pr_loc(p);
  93.         return;
  94.  
  95.     default:
  96.         printf("bad node %p: c_code=%d %s\n", p,
  97.             p -> c_code, ps_tok(p->c_code));
  98.     }
  99. }
  100.  
  101. /*
  102.     Print a list of parse nodes.
  103.  
  104.     See the file par.h for the definitions of these nodes.
  105. */
  106. void
  107. pr_list(struct node * p)
  108. {
  109.     register int i;
  110.     register int type;
  111.     register struct type_node *id;
  112.     register struct node *q;
  113.  
  114.     SL_DISABLE();
  115.  
  116.     if (p == NULL) {
  117.         printf("<NO CODE LIST>\n");
  118.         return;
  119.     }
  120.  
  121.     do {
  122.  
  123.     switch (p -> n_type) {
  124.  
  125.     case CALL_TOK:
  126.         printf("CALL[");
  127.         pr_expr(p -> n_arg1);
  128.         printf(",[");
  129.         pr_expr(p -> n_arg2);
  130.         printf("]]\n");
  131.         break;
  132.  
  133.     case Z_TOK:
  134.         printf("pseudo: %s: ", xzp_tab [p -> n_ztype]);
  135.         pr_expr(p -> n_zarg1);
  136.         printf("\n");
  137.         break;
  138.  
  139.     case X_TOK:
  140.         type = p -> n_xtype;
  141.         printf("x_tok: %s", xzp_tab [type]);
  142.         printf("(");
  143.         if (type == X_BRA || type == X_BSR || is_bxx(type)) {
  144.             printf("label: %s", p -> n_arg1 -> c_labsym);
  145.         }
  146.         else if (is_dbxx(type)) {
  147.             pr_expr(p -> n_xarg1);
  148.             printf(",label: %s", p -> n_arg2 -> c_labsym);
  149.         }
  150.         else if (type == X_JMP || type == X_JSR) {
  151.             if (p -> n_xarg1) {
  152.                 pr_expr(p -> n_xarg1);
  153.             }
  154.             if (p -> n_arg2) {
  155.                 printf("label: %s", p -> n_arg2 -> c_labsym);
  156.             }
  157.         }
  158.         else if (p -> n_xarg1) {
  159.             pr_expr(p -> n_xarg1);
  160.             if (p -> n_xarg2) {
  161.                 printf(", ");
  162.                 pr_expr(p -> n_xarg2);
  163.             }
  164.         }
  165.         printf(")\n");
  166.         break;
  167.  
  168.     case K_IF:
  169.         printf("if (");
  170.         pr_expr(p -> n_ibool);
  171.         printf(")");
  172.         pr_stat(p -> n_ithen);
  173.         printf("\n");
  174.         if (p -> n_ielse) {
  175.             printf("else ");
  176.             pr_stat(p -> n_ielse);
  177.             printf("\n");
  178.         }
  179.         break;
  180.  
  181.     case K_DO:
  182.         printf("do ");
  183.         pr_stat(p -> n_dbdy);
  184.         printf(" while (");
  185.         pr_expr(p -> n_dbool);
  186.         printf(")\n");
  187.         break;
  188.  
  189.     case K_WHILE:
  190.         printf("while (");
  191.         pr_expr(p -> n_wbool);
  192.         printf(")");
  193.         pr_stat(p -> n_wbdy);
  194.         printf("\n");
  195.         break;
  196.  
  197.     case K_FOR:
  198.         printf("for(");
  199.         pr_list(p -> n_f1list);
  200.         printf(";");
  201.         pr_expr(p -> n_fbool);
  202.         printf(";");
  203.         pr_list(p -> n_f2list);
  204.         printf(") ");
  205.         pr_stat(p -> n_fbdy);
  206.         printf("\n");
  207.         break;
  208.  
  209.     case K_SWITCH:
  210.         printf("switch(");
  211.         pr_expr(p -> n_sval);
  212.         printf(") ");
  213.         pr_stat(p -> n_sbdy);
  214.         printf("\n");
  215.         break;        
  216.  
  217.     case K_BREAK:
  218.         printf("break;\n");
  219.         break;
  220.  
  221.     case K_CONTINUE:
  222.         printf("continue;\n");
  223.         break;
  224.  
  225.     case K_CASE:
  226.         printf("case %ld:\n", p -> n_ccon);
  227.         break;
  228.  
  229.     case K_DEFAULT:
  230.         printf("default:\n");
  231.         break;
  232.  
  233.     case K_RETURN:
  234.         printf("return");
  235.         if (p -> n_rval) {
  236.             printf("(");
  237.             pr_expr(p -> n_rval);
  238.             printf(")");
  239.         }
  240.         printf(";\n");
  241.         break;
  242.  
  243.     case LABEL_TOK:
  244.         printf("<LABEL> %s:\n", p -> n_plab -> c_labsym);
  245.         break;
  246.  
  247.     case K_GOTO:
  248.         printf("goto %s;\n", p -> n_plab -> c_labsym);
  249.         break;
  250.  
  251.     /* uop_node and binop_node */
  252.     default:
  253.  
  254.         i = p -> n_type;
  255.         if (is_op(i) || i == ID_TOK) {
  256.             pr_expr(p);
  257.             printf("\n");
  258.         }
  259.         else {
  260.             printf("<INAPPROPRIATE NODE %d>\n", p -> n_type);
  261.         }
  262.         break;
  263.     } /* end switch */
  264.     } /* end dowhile */
  265.     while ( (p = p -> n_next) != NULL);
  266. }
  267.  
  268. /*
  269.     Print an expression.
  270.     See the file par.h for the definitions of these nodes.
  271. */
  272. void
  273. pr_expr(register struct node * p)
  274. {
  275.     register struct st_node * id;
  276.     register int i;
  277.  
  278.     SL_DISABLE();
  279.  
  280.     if (p == NULL) {
  281.         printf("<NULL expression>");
  282.         return;
  283.     }
  284.  
  285.     switch (p -> n_type) {
  286.  
  287.     case ID_TOK:
  288.         if (p -> n_cid != NULL) {
  289.             id = p -> n_cid;
  290.             if ((long)id & 1) {
  291.                 printf("id: <%p>", id);
  292.             }
  293.             else {
  294.                 printf("id: %s", id -> st_name);
  295.             }
  296.         }
  297.         else if (p -> n_reg1) {
  298.             printf("reg: %s", arp_tab[p -> n_reg1]);
  299.         }
  300.         else if (p -> n_cltype != NULL) {
  301.             switch( (p -> n_cltype) -> t_typtok) {
  302.  
  303.             case INT_TYPE:
  304.                 i = p -> n_cltype -> t_mclass;
  305.                 if (i & UNSIGNED_MOD) {
  306.                     printf("(u ");
  307.                 }
  308.                 else {
  309.                     printf("(s ");
  310.                 }
  311.  
  312.                 if (i & LONG_MOD) {
  313.                     printf("long)");
  314.                 }
  315.                 else if (i & SHORT_MOD) {
  316.                     printf("short)");
  317.                 }
  318.                 else if (i & CHAR_MOD) {
  319.                     printf("char)");
  320.                 }
  321.                 else {
  322.                     printf("int)");
  323.                 }
  324.  
  325.                 if (i & UNSIGNED_MOD) {
  326.                     printf("%lu", p -> n_const);
  327.                 }
  328.                 else {
  329.                     printf("%ld", p -> n_const);
  330.                 }
  331.                 break;
  332.  
  333.             case SELEMENT_TYPE:
  334.             case UELEMENT_TYPE:
  335.             case DELEMENT_TYPE:
  336.                 printf("(elt)");
  337.                 printf("%lu", p -> n_const);
  338.                 break;
  339.  
  340.             case ARRAY_TYPE:
  341.                 /* one should possibly check further */
  342.                 printf("\"%s\"", p -> n_const);
  343.                 break;
  344.  
  345.             case POINTER_TYPE:
  346.                 printf("(pointer)%ld", p -> n_const);
  347.                 break;
  348.  
  349.             default:
  350.                 printf("%ld<unknown type>", p -> n_const);
  351.                 break;
  352.             }
  353.         }
  354.         else {
  355.             printf("%ld<missing type>", p -> n_const);
  356.         }
  357.         break;
  358.  
  359.     case CALL_TOK:
  360.         printf("CALL[");
  361.         pr_expr(p -> n_arg1);
  362.         printf(",[");
  363.         pr_expr(p -> n_arg2);
  364.         printf("]]");
  365.         break;
  366.  
  367.     case CC_TOK:
  368.         printf("cc: %s", arp_tab [p -> n_xtype]);
  369.         break;
  370.  
  371.     case SEPARATOR_TOK:
  372.         pr_expr(p -> n_car);
  373.         printf(" @ ");
  374.         pr_expr(p -> n_next); /* n_next acts like n_arg2? */
  375.         break;
  376.  
  377.     case K_CHAR:
  378.     case K_INT:
  379.     case K_LONG:
  380.     case K_STRUCT:
  381.     case K_UNION:
  382.     case K_TYPEDEF:
  383.         printf("pr_expr: unexpected type!!\n");
  384.         pr_type((struct type_node *) p);
  385.         break;
  386.  
  387.     case QUESTION_TOK:
  388.         /* Ternary OP. */
  389.         printf("?:[");
  390.         pr_expr(p -> n_arg1);
  391.         printf(", ");
  392.         pr_expr(p -> n_arg2);
  393.         printf(", ");
  394.         pr_expr(p -> n_arg3);
  395.         printf("]");
  396.         break;
  397.  
  398.     /* uop_node and binop_node */
  399.     default:
  400.  
  401.         i = p -> n_type;
  402.         if (is_op(i)) {
  403.             printf("%s", ps_tok(i));
  404.             if (is_unop(i)) {
  405.                 /* Unary OP. */
  406.                 if (i == CAST_TOK) {
  407.                     printf("(");
  408.                     pr_ty1(p -> n_cltype, TRUE, TRUE);
  409.                     printf(")");
  410.                 }
  411.                 printf("[");
  412.                 pr_expr(p -> n_arg1);
  413.                 printf("]");
  414.             }
  415.             else {
  416.                 /* Binary OP. */
  417.                 printf("[");
  418.                 pr_expr(p -> n_arg1);
  419.                 printf(", ");
  420.                 pr_expr(p -> n_arg2);
  421.                 printf("]");
  422.             }
  423.         }
  424.         else {
  425.             printf(
  426.             "node \"%s\" (%d) doesn't belong in expression\n",
  427.             ps_tok(i), i);
  428.         }
  429.     } /* (end switch) */
  430. }
  431.  
  432. /*
  433.     Print a statement list enclosed in curly braces.
  434. */
  435. static void
  436. pr_stat(struct node * p)
  437. {
  438.     SL_DISABLE();
  439.  
  440.     printf("{");
  441.     if (p != NULL) {
  442.         printf("\n");
  443.         pr_list(p);
  444.     }
  445.     printf("}");
  446. }
  447.  
  448. /*
  449.     Print a type.
  450. */
  451. void
  452. pr_type(register struct type_node *t)
  453. {
  454.     SL_DISABLE();
  455.  
  456.     pr_ty1(t, TRUE, TRUE);
  457.     printf("\n");
  458. }
  459.  
  460. static void
  461. pr_ty1(register struct type_node * t, bool usexpand, bool fnexpand)
  462. {
  463.     int i;
  464.  
  465.     SL_DISABLE();
  466.  
  467.     for(;;) {
  468.         if (t == NULL) {
  469.             printf("<NULL type>");
  470.             return;
  471.         }
  472.  
  473.         /* first the primary printout */
  474.         i = t -> t_typtok;
  475.         if (t -> t_mclass & CONST_MOD) {
  476.             printf("const ");
  477.         }
  478.         if (t -> t_mclass & VOLATILE_MOD) {
  479.             printf("volatile ");
  480.         }
  481.         switch (i) {
  482.         case NULL_TYPE:
  483.             printf("NULL_TYPE");
  484.             return;
  485.  
  486.         case BOOL_TYPE:
  487.         case FLOAT_TYPE:
  488.         case INT_TYPE:
  489.         case VOID_TYPE:
  490.             if (t -> t_mclass & UNSIGNED_MOD) {
  491.                 printf("unsigned ");
  492.             }
  493.             if (t -> t_mclass & CHAR_MOD) {
  494.                 if (!(t -> t_mclass & UNSIGNED_MOD)) {
  495.                     printf("signed ");
  496.                 }
  497.                 printf("char");
  498.             }
  499.             if (t -> t_mclass & SHORT_MOD) {
  500.                 printf("short ");
  501.             }
  502.             if (t -> t_mclass & LONG_MOD) {
  503.                 printf("long ");
  504.             }
  505.             break;
  506.  
  507.         case ARRAY_TYPE:
  508.             printf("array [%ld] of ", t -> t_tdim);
  509.             break;
  510.  
  511.         case FUNCTION_TYPE:    printf("function");        break;
  512.         case POINTER_TYPE:    printf("pointer to ");        break;
  513.         case CAST_TYPE:        printf("cast into ");        break;
  514.  
  515.         case STRUCT_TYPE:
  516.             printf("struct %p {%ld}:", t, t -> t_tsize);
  517.             break;
  518.  
  519.         case UNION_TYPE:
  520.             printf("union %p {%ld}:", t, t -> t_tsize);
  521.             break;
  522.  
  523.         case DECL_TYPE:
  524.             printf("DECL <%ld>:\n", t -> t_tsize);
  525.             break;
  526.  
  527.         case DELEMENT_TYPE:
  528.             TRACE("v", printf("delement--"));
  529.             break;
  530.  
  531.         case SELEMENT_TYPE:
  532.             TRACE("v", printf("  selement--"));
  533.             break;
  534.  
  535.         case UELEMENT_TYPE:
  536.             TRACE("v", printf("  uelement--"));
  537.             break;
  538.  
  539.         default:
  540.             printf("pr_type: unknown type %d", t -> t_typtok);
  541.             return;
  542.         }
  543.  
  544.         /* then further explanation and linkage */
  545.         switch (i) {
  546.         case DECL_TYPE:
  547.             t = t -> t_list;
  548.             break;
  549.  
  550.         case FUNCTION_TYPE:
  551.             if (t -> t_list == NULL) {
  552.                 printf(" returning ");
  553.             }
  554.             else {
  555.                 printf("--\n");
  556.                 pr_ty1(t -> t_list, usexpand, FALSE);
  557.                 printf("\n--which returns ");
  558.             }
  559.             t = t -> t_link;
  560.             break;
  561.  
  562.         case STRUCT_TYPE:
  563.         case UNION_TYPE:
  564.             if (t -> t_list == NULL) {
  565.                 printf(" <NO LIST>");
  566.             }
  567.             else if (usexpand) {
  568.                 TRACE("v", 
  569.                     printf("\n");
  570.                     pr_ty1(t -> t_list, FALSE, fnexpand);
  571.                 );
  572.             }
  573.             else {
  574.                 printf(" ...");
  575.             }
  576.             return;
  577.  
  578.         case DELEMENT_TYPE:
  579.         case SELEMENT_TYPE:
  580.         case UELEMENT_TYPE:
  581.  
  582.         TRACE("v",
  583.             if (t -> t_parent) {
  584.                 if (t -> t_parent -> st_name) {
  585.                     printf("%s ", t -> t_parent -> st_name);
  586.                     if (t -> t_parent -> st_alias) {
  587.                         printf("/ %s ",
  588.                          t -> t_parent -> st_alias);
  589.                     }
  590.                 }
  591.                 printf("@%ld: ", t -> t_parent -> st_offset);
  592.             }
  593.             pr_ty1(t -> t_link, usexpand, fnexpand);
  594.         );
  595.             t = t -> t_list;
  596.  
  597.             if (t == NULL) {
  598.                 return;
  599.             }
  600.             if (t -> t_typtok != i) {
  601.                 printf("<t_list structure error>\n");
  602.             }
  603.  
  604.         TRACE("v", printf("\n"));
  605.                         break;
  606.  
  607.         case ARRAY_TYPE:
  608.         case POINTER_TYPE:
  609.             t = t -> t_link;
  610.             break;
  611.  
  612.         case BOOL_TYPE:        printf("bool");    return;
  613.         case VOID_TYPE:        printf("void");    return;
  614.  
  615.         case INT_TYPE:
  616.             if (!(t -> t_mclass & CHAR_MOD)) {
  617.                 printf("int");
  618.             }
  619.             if (t -> t_link) {
  620.                 printf("<<error> link=%p>", t->t_link);
  621.             }
  622.             return;
  623.  
  624.         case FLOAT_TYPE:
  625.             printf("float");
  626.             return;
  627.  
  628.         default:
  629.             printf("pr_type: internal\n");
  630.             return;
  631.         }
  632.     }
  633. }
  634.  
  635. /*
  636.     Print out a storage class.
  637. */
  638. void
  639. pr_sclass(int sclass)
  640. {
  641.     SL_DISABLE();
  642.  
  643.     switch(sclass) {
  644.     case NULL_CLASS:    printf("null-class");        break;
  645.     case FORMAL_CLASS:    printf("formal");        break;
  646.     case FORMREG_CLASS:    printf("formal register");    break;
  647.     case AUTO_CLASS:    printf("auto");            break;
  648.     case REGISTER_CLASS:    printf("register");        break;
  649.     case GLOBAL_CLASS:    printf("global");        break;
  650.     case EXTERN_CLASS:    printf("extern");        break;
  651.     case STATICG_CLASS:    printf("static global");    break;
  652.     case STATICL_CLASS:    printf("static local");        break;
  653.     case CODE_CLASS:    printf("code");            break;
  654.     case TYPEDEF_CLASS:    printf("typedef");        break;
  655.     case TAG_CLASS:        printf("tag");            break;
  656.     case SUE_CLASS:        printf("struct/union element");    break;
  657.     default:        printf("unknown-class");
  658.     }
  659. }
  660.  
  661. void
  662. pr_loc(register struct node *p)
  663. {
  664.     register int mode;
  665.     register struct st_node *id;
  666.  
  667.     SL_DISABLE();
  668.  
  669.     if (p == NULL) {
  670.         printf("<NULL LOC>");
  671.         return;
  672.     }
  673.     else if ((long)p & 1) {
  674.         printf("<ODD LOC %p>", p);
  675.         fatal("exit: odd pointer");
  676.         return;
  677.     }
  678.  
  679.     mode = (int) p -> n_mode;
  680.     if (mode == VALUE_MODE) {
  681.         printf("VAL: ");
  682.     }
  683.     else {
  684.         printf("EA: ");
  685.     }
  686.     if (id = p -> n_cid) {
  687.         printf("%s", id -> st_name);
  688.     }
  689.     if (p -> n_const || (!p -> n_cid && 
  690.     ((p -> n_reg1 && p -> n_reg2) || (!p -> n_reg1 && !p -> n_reg2)) ) ) {
  691.         if (p -> n_cid) {
  692.             if (p -> n_const >= 0) {
  693.                 printf("+");
  694.             }
  695.         }
  696.         printf("%ld", p -> n_const);
  697.     }
  698.  
  699.     if (p -> n_reg1) {
  700.         if (p -> n_const || p -> n_cid) {
  701.             printf("+");
  702.         }
  703.         if (mode == EAPRD_MODE) {
  704.             printf("<-->");
  705.         }
  706.         printf("%s", arp_tab [p -> n_reg1]);
  707.  
  708.         if (p -> n_reg2) {
  709.             printf("+%s", arp_tab [p -> n_reg2]);
  710.             switch (p -> n_scflag) {
  711.             case X2_WORD:
  712.                 printf(".w");
  713.                 break;
  714.             case X2_LONG:
  715.                 printf(".l");
  716.                 break;
  717.             default:
  718.                 g_error(p, "pr_loc: internal: unscaled reg2\n");
  719.                 break;
  720.             case 0:
  721.             case X2_OK:
  722.                 ;
  723.             }
  724.         }
  725.     }
  726.     if (p -> n_reg1) {
  727.         if (mode == EAPSI_MODE) {
  728.             printf("<++>");
  729.         }
  730.     }
  731. }
  732.  
  733. void
  734. pr_iblock(struct iblock *p)
  735. {
  736.     register unsigned long i;
  737.  
  738.     SL_DISABLE();
  739.  
  740.     printf("iblock %p: link %p, isize %d, idim %lu: ",
  741.         p, p -> ilink, (int)p -> isize, p -> idim);    
  742.  
  743.     switch(p -> itype) {
  744.     case 0:
  745.         if (p -> idim) {
  746.             printf(", %lu", p -> idata[0] . icn);
  747.         }
  748.         for (i = 1; i < p -> idim; i++) {
  749.             printf(", %lu", p -> idata[i] . icn);
  750.         }
  751.         break;
  752.  
  753.     case IBSSZB_DEC:
  754.         printf("bssz.b");
  755.         break;
  756.  
  757.     case ISTRA_DEC:
  758.         printf("string array");
  759.         break;
  760.     }
  761.     printf("\n");
  762. }
  763.  
  764. #endif /* SHERLOCK */
  765.