home *** CD-ROM | disk | FTP | other *** search
- /*
- C* -- Console output routines used only in SHERLOCK traces.
-
- source: pr.c
- started: May 19, 1986
- version:
- December 15, 1986
- March 7, 1989
-
- PUBLIC DOMAIN SOFTWARE
-
- The CSTAR program was placed in the public domain on June 15, 1991,
- by its author and sole owner,
-
- Edward K. Ream
- 1617 Monroe Street
- Madison, WI 53711
- (608) 257-0802
-
- CSTAR may be used for any commercial or non-commercial purpose.
-
- See cstar.h or cstar.c for a DISCLAIMER OF WARRANTIES.
- */
-
- #include "cstar.h"
-
- #ifdef SHERLOCK
-
- /*
- Externally visible routines:
- */
- void pr_arg (struct node *p);
- void pr_expr (struct node *p);
- void pr_iblock (struct iblock *p);
- void pr_list (struct node *p);
- void pr_loc (struct node *p);
- void pr_sclass (int sclass);
- void pr_type (struct type_node *t);
-
- /* ----- NOT YET
- char * ps_bool (bool b);
- char * ps_ch (int c);
- char * ps_op (en_tokens op);
- char * ps_scope (en_scope scope);
- char * ps_str (char *s);
- char * ps_tok (void);
- ----- */
-
- /*
- Internal routines:
- */
- static void pr_ty1 (register struct type_node * t,
- bool usexpand, bool fnexpand);
- static void pr_stat (struct node *p);
-
- /*
- Output an argument, which is a label or a loc_node.
- */
- void
- pr_arg(struct node * p)
- {
- struct st_node *id;
-
- SL_DISABLE();
-
- /* WARNING: the label nodes and the id nodes aren't the same kind */
- if (p == NULL) {
- return;
- }
-
- switch(p -> c_code) {
-
- case O_LABEL:
- printf("L%s", p -> c_labnum);
- return;
-
- case O_ULABEL:
- /* Internal label. */
- if (p -> c_labnum) {
- printf("U%s", p -> c_labnum);
- }
- else {
- printf(p -> c_labsym);
- }
- break;
-
- case O_LITERAL:
- printf(p -> c_lit);
- return;
-
- case ID_TOK:
- pr_loc(p);
- return;
-
- default:
- printf("bad node %p: c_code=%d %s\n", p,
- p -> c_code, ps_tok(p->c_code));
- }
- }
-
- /*
- Print a list of parse nodes.
-
- See the file par.h for the definitions of these nodes.
- */
- void
- pr_list(struct node * p)
- {
- register int i;
- register int type;
- register struct type_node *id;
- register struct node *q;
-
- SL_DISABLE();
-
- if (p == NULL) {
- printf("<NO CODE LIST>\n");
- return;
- }
-
- do {
-
- switch (p -> n_type) {
-
- case CALL_TOK:
- printf("CALL[");
- pr_expr(p -> n_arg1);
- printf(",[");
- pr_expr(p -> n_arg2);
- printf("]]\n");
- break;
-
- case Z_TOK:
- printf("pseudo: %s: ", xzp_tab [p -> n_ztype]);
- pr_expr(p -> n_zarg1);
- printf("\n");
- break;
-
- case X_TOK:
- type = p -> n_xtype;
- printf("x_tok: %s", xzp_tab [type]);
- printf("(");
- if (type == X_BRA || type == X_BSR || is_bxx(type)) {
- printf("label: %s", p -> n_arg1 -> c_labsym);
- }
- else if (is_dbxx(type)) {
- pr_expr(p -> n_xarg1);
- printf(",label: %s", p -> n_arg2 -> c_labsym);
- }
- else if (type == X_JMP || type == X_JSR) {
- if (p -> n_xarg1) {
- pr_expr(p -> n_xarg1);
- }
- if (p -> n_arg2) {
- printf("label: %s", p -> n_arg2 -> c_labsym);
- }
- }
- else if (p -> n_xarg1) {
- pr_expr(p -> n_xarg1);
- if (p -> n_xarg2) {
- printf(", ");
- pr_expr(p -> n_xarg2);
- }
- }
- printf(")\n");
- break;
-
- case K_IF:
- printf("if (");
- pr_expr(p -> n_ibool);
- printf(")");
- pr_stat(p -> n_ithen);
- printf("\n");
- if (p -> n_ielse) {
- printf("else ");
- pr_stat(p -> n_ielse);
- printf("\n");
- }
- break;
-
- case K_DO:
- printf("do ");
- pr_stat(p -> n_dbdy);
- printf(" while (");
- pr_expr(p -> n_dbool);
- printf(")\n");
- break;
-
- case K_WHILE:
- printf("while (");
- pr_expr(p -> n_wbool);
- printf(")");
- pr_stat(p -> n_wbdy);
- printf("\n");
- break;
-
- case K_FOR:
- printf("for(");
- pr_list(p -> n_f1list);
- printf(";");
- pr_expr(p -> n_fbool);
- printf(";");
- pr_list(p -> n_f2list);
- printf(") ");
- pr_stat(p -> n_fbdy);
- printf("\n");
- break;
-
- case K_SWITCH:
- printf("switch(");
- pr_expr(p -> n_sval);
- printf(") ");
- pr_stat(p -> n_sbdy);
- printf("\n");
- break;
-
- case K_BREAK:
- printf("break;\n");
- break;
-
- case K_CONTINUE:
- printf("continue;\n");
- break;
-
- case K_CASE:
- printf("case %ld:\n", p -> n_ccon);
- break;
-
- case K_DEFAULT:
- printf("default:\n");
- break;
-
- case K_RETURN:
- printf("return");
- if (p -> n_rval) {
- printf("(");
- pr_expr(p -> n_rval);
- printf(")");
- }
- printf(";\n");
- break;
-
- case LABEL_TOK:
- printf("<LABEL> %s:\n", p -> n_plab -> c_labsym);
- break;
-
- case K_GOTO:
- printf("goto %s;\n", p -> n_plab -> c_labsym);
- break;
-
- /* uop_node and binop_node */
- default:
-
- i = p -> n_type;
- if (is_op(i) || i == ID_TOK) {
- pr_expr(p);
- printf("\n");
- }
- else {
- printf("<INAPPROPRIATE NODE %d>\n", p -> n_type);
- }
- break;
- } /* end switch */
- } /* end dowhile */
- while ( (p = p -> n_next) != NULL);
- }
-
- /*
- Print an expression.
- See the file par.h for the definitions of these nodes.
- */
- void
- pr_expr(register struct node * p)
- {
- register struct st_node * id;
- register int i;
-
- SL_DISABLE();
-
- if (p == NULL) {
- printf("<NULL expression>");
- return;
- }
-
- switch (p -> n_type) {
-
- case ID_TOK:
- if (p -> n_cid != NULL) {
- id = p -> n_cid;
- if ((long)id & 1) {
- printf("id: <%p>", id);
- }
- else {
- printf("id: %s", id -> st_name);
- }
- }
- else if (p -> n_reg1) {
- printf("reg: %s", arp_tab[p -> n_reg1]);
- }
- else if (p -> n_cltype != NULL) {
- switch( (p -> n_cltype) -> t_typtok) {
-
- case INT_TYPE:
- i = p -> n_cltype -> t_mclass;
- if (i & UNSIGNED_MOD) {
- printf("(u ");
- }
- else {
- printf("(s ");
- }
-
- if (i & LONG_MOD) {
- printf("long)");
- }
- else if (i & SHORT_MOD) {
- printf("short)");
- }
- else if (i & CHAR_MOD) {
- printf("char)");
- }
- else {
- printf("int)");
- }
-
- if (i & UNSIGNED_MOD) {
- printf("%lu", p -> n_const);
- }
- else {
- printf("%ld", p -> n_const);
- }
- break;
-
- case SELEMENT_TYPE:
- case UELEMENT_TYPE:
- case DELEMENT_TYPE:
- printf("(elt)");
- printf("%lu", p -> n_const);
- break;
-
- case ARRAY_TYPE:
- /* one should possibly check further */
- printf("\"%s\"", p -> n_const);
- break;
-
- case POINTER_TYPE:
- printf("(pointer)%ld", p -> n_const);
- break;
-
- default:
- printf("%ld<unknown type>", p -> n_const);
- break;
- }
- }
- else {
- printf("%ld<missing type>", p -> n_const);
- }
- break;
-
- case CALL_TOK:
- printf("CALL[");
- pr_expr(p -> n_arg1);
- printf(",[");
- pr_expr(p -> n_arg2);
- printf("]]");
- break;
-
- case CC_TOK:
- printf("cc: %s", arp_tab [p -> n_xtype]);
- break;
-
- case SEPARATOR_TOK:
- pr_expr(p -> n_car);
- printf(" @ ");
- pr_expr(p -> n_next); /* n_next acts like n_arg2? */
- break;
-
- case K_CHAR:
- case K_INT:
- case K_LONG:
- case K_STRUCT:
- case K_UNION:
- case K_TYPEDEF:
- printf("pr_expr: unexpected type!!\n");
- pr_type((struct type_node *) p);
- break;
-
- case QUESTION_TOK:
- /* Ternary OP. */
- printf("?:[");
- pr_expr(p -> n_arg1);
- printf(", ");
- pr_expr(p -> n_arg2);
- printf(", ");
- pr_expr(p -> n_arg3);
- printf("]");
- break;
-
- /* uop_node and binop_node */
- default:
-
- i = p -> n_type;
- if (is_op(i)) {
- printf("%s", ps_tok(i));
- if (is_unop(i)) {
- /* Unary OP. */
- if (i == CAST_TOK) {
- printf("(");
- pr_ty1(p -> n_cltype, TRUE, TRUE);
- printf(")");
- }
- printf("[");
- pr_expr(p -> n_arg1);
- printf("]");
- }
- else {
- /* Binary OP. */
- printf("[");
- pr_expr(p -> n_arg1);
- printf(", ");
- pr_expr(p -> n_arg2);
- printf("]");
- }
- }
- else {
- printf(
- "node \"%s\" (%d) doesn't belong in expression\n",
- ps_tok(i), i);
- }
- } /* (end switch) */
- }
-
- /*
- Print a statement list enclosed in curly braces.
- */
- static void
- pr_stat(struct node * p)
- {
- SL_DISABLE();
-
- printf("{");
- if (p != NULL) {
- printf("\n");
- pr_list(p);
- }
- printf("}");
- }
-
- /*
- Print a type.
- */
- void
- pr_type(register struct type_node *t)
- {
- SL_DISABLE();
-
- pr_ty1(t, TRUE, TRUE);
- printf("\n");
- }
-
- static void
- pr_ty1(register struct type_node * t, bool usexpand, bool fnexpand)
- {
- int i;
-
- SL_DISABLE();
-
- for(;;) {
- if (t == NULL) {
- printf("<NULL type>");
- return;
- }
-
- /* first the primary printout */
- i = t -> t_typtok;
- if (t -> t_mclass & CONST_MOD) {
- printf("const ");
- }
- if (t -> t_mclass & VOLATILE_MOD) {
- printf("volatile ");
- }
- switch (i) {
- case NULL_TYPE:
- printf("NULL_TYPE");
- return;
-
- case BOOL_TYPE:
- case FLOAT_TYPE:
- case INT_TYPE:
- case VOID_TYPE:
- if (t -> t_mclass & UNSIGNED_MOD) {
- printf("unsigned ");
- }
- if (t -> t_mclass & CHAR_MOD) {
- if (!(t -> t_mclass & UNSIGNED_MOD)) {
- printf("signed ");
- }
- printf("char");
- }
- if (t -> t_mclass & SHORT_MOD) {
- printf("short ");
- }
- if (t -> t_mclass & LONG_MOD) {
- printf("long ");
- }
- break;
-
- case ARRAY_TYPE:
- printf("array [%ld] of ", t -> t_tdim);
- break;
-
- case FUNCTION_TYPE: printf("function"); break;
- case POINTER_TYPE: printf("pointer to "); break;
- case CAST_TYPE: printf("cast into "); break;
-
- case STRUCT_TYPE:
- printf("struct %p {%ld}:", t, t -> t_tsize);
- break;
-
- case UNION_TYPE:
- printf("union %p {%ld}:", t, t -> t_tsize);
- break;
-
- case DECL_TYPE:
- printf("DECL <%ld>:\n", t -> t_tsize);
- break;
-
- case DELEMENT_TYPE:
- TRACE("v", printf("delement--"));
- break;
-
- case SELEMENT_TYPE:
- TRACE("v", printf(" selement--"));
- break;
-
- case UELEMENT_TYPE:
- TRACE("v", printf(" uelement--"));
- break;
-
- default:
- printf("pr_type: unknown type %d", t -> t_typtok);
- return;
- }
-
- /* then further explanation and linkage */
- switch (i) {
- case DECL_TYPE:
- t = t -> t_list;
- break;
-
- case FUNCTION_TYPE:
- if (t -> t_list == NULL) {
- printf(" returning ");
- }
- else {
- printf("--\n");
- pr_ty1(t -> t_list, usexpand, FALSE);
- printf("\n--which returns ");
- }
- t = t -> t_link;
- break;
-
- case STRUCT_TYPE:
- case UNION_TYPE:
- if (t -> t_list == NULL) {
- printf(" <NO LIST>");
- }
- else if (usexpand) {
- TRACE("v",
- printf("\n");
- pr_ty1(t -> t_list, FALSE, fnexpand);
- );
- }
- else {
- printf(" ...");
- }
- return;
-
- case DELEMENT_TYPE:
- case SELEMENT_TYPE:
- case UELEMENT_TYPE:
-
- TRACE("v",
- if (t -> t_parent) {
- if (t -> t_parent -> st_name) {
- printf("%s ", t -> t_parent -> st_name);
- if (t -> t_parent -> st_alias) {
- printf("/ %s ",
- t -> t_parent -> st_alias);
- }
- }
- printf("@%ld: ", t -> t_parent -> st_offset);
- }
- pr_ty1(t -> t_link, usexpand, fnexpand);
- );
- t = t -> t_list;
-
- if (t == NULL) {
- return;
- }
- if (t -> t_typtok != i) {
- printf("<t_list structure error>\n");
- }
-
- TRACE("v", printf("\n"));
- break;
-
- case ARRAY_TYPE:
- case POINTER_TYPE:
- t = t -> t_link;
- break;
-
- case BOOL_TYPE: printf("bool"); return;
- case VOID_TYPE: printf("void"); return;
-
- case INT_TYPE:
- if (!(t -> t_mclass & CHAR_MOD)) {
- printf("int");
- }
- if (t -> t_link) {
- printf("<<error> link=%p>", t->t_link);
- }
- return;
-
- case FLOAT_TYPE:
- printf("float");
- return;
-
- default:
- printf("pr_type: internal\n");
- return;
- }
- }
- }
-
- /*
- Print out a storage class.
- */
- void
- pr_sclass(int sclass)
- {
- SL_DISABLE();
-
- switch(sclass) {
- case NULL_CLASS: printf("null-class"); break;
- case FORMAL_CLASS: printf("formal"); break;
- case FORMREG_CLASS: printf("formal register"); break;
- case AUTO_CLASS: printf("auto"); break;
- case REGISTER_CLASS: printf("register"); break;
- case GLOBAL_CLASS: printf("global"); break;
- case EXTERN_CLASS: printf("extern"); break;
- case STATICG_CLASS: printf("static global"); break;
- case STATICL_CLASS: printf("static local"); break;
- case CODE_CLASS: printf("code"); break;
- case TYPEDEF_CLASS: printf("typedef"); break;
- case TAG_CLASS: printf("tag"); break;
- case SUE_CLASS: printf("struct/union element"); break;
- default: printf("unknown-class");
- }
- }
-
- void
- pr_loc(register struct node *p)
- {
- register int mode;
- register struct st_node *id;
-
- SL_DISABLE();
-
- if (p == NULL) {
- printf("<NULL LOC>");
- return;
- }
- else if ((long)p & 1) {
- printf("<ODD LOC %p>", p);
- fatal("exit: odd pointer");
- return;
- }
-
- mode = (int) p -> n_mode;
- if (mode == VALUE_MODE) {
- printf("VAL: ");
- }
- else {
- printf("EA: ");
- }
- if (id = p -> n_cid) {
- printf("%s", id -> st_name);
- }
- if (p -> n_const || (!p -> n_cid &&
- ((p -> n_reg1 && p -> n_reg2) || (!p -> n_reg1 && !p -> n_reg2)) ) ) {
- if (p -> n_cid) {
- if (p -> n_const >= 0) {
- printf("+");
- }
- }
- printf("%ld", p -> n_const);
- }
-
- if (p -> n_reg1) {
- if (p -> n_const || p -> n_cid) {
- printf("+");
- }
- if (mode == EAPRD_MODE) {
- printf("<-->");
- }
- printf("%s", arp_tab [p -> n_reg1]);
-
- if (p -> n_reg2) {
- printf("+%s", arp_tab [p -> n_reg2]);
- switch (p -> n_scflag) {
- case X2_WORD:
- printf(".w");
- break;
- case X2_LONG:
- printf(".l");
- break;
- default:
- g_error(p, "pr_loc: internal: unscaled reg2\n");
- break;
- case 0:
- case X2_OK:
- ;
- }
- }
- }
- if (p -> n_reg1) {
- if (mode == EAPSI_MODE) {
- printf("<++>");
- }
- }
- }
-
- void
- pr_iblock(struct iblock *p)
- {
- register unsigned long i;
-
- SL_DISABLE();
-
- printf("iblock %p: link %p, isize %d, idim %lu: ",
- p, p -> ilink, (int)p -> isize, p -> idim);
-
- switch(p -> itype) {
- case 0:
- if (p -> idim) {
- printf(", %lu", p -> idata[0] . icn);
- }
- for (i = 1; i < p -> idim; i++) {
- printf(", %lu", p -> idata[i] . icn);
- }
- break;
-
- case IBSSZB_DEC:
- printf("bssz.b");
- break;
-
- case ISTRA_DEC:
- printf("string array");
- break;
- }
- printf("\n");
- }
-
- #endif /* SHERLOCK */
-