From afc5c481f97b85b803b9b973d52684deceb715d0 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Tue, 30 Jul 2013 14:26:50 -0400 Subject: Start new branch that adds comments to profiling. --- profile.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'profile.c') diff --git a/profile.c b/profile.c index eae24b1c..5c7555e6 100644 --- a/profile.c +++ b/profile.c @@ -873,6 +873,28 @@ cleanup: indent(pc->exec_count); break; + case Op_comment: + { + char *text; + size_t count; + bool after_newline = false; + + count = pc->memory->stlen; + text = pc->memory->stptr; + + indent(SPACEOVER); + for (; count > 0; count--, text++) { + if (after_newline) { + indent(SPACEOVER); + after_newline = false; + } + putc(*text, prof_fp); + if (*text == '\n') + after_newline = true; + } + } + break; + default: cant_happen(); } -- cgit v1.2.3 From 9a7e5828f29d3f5daba5dc5ef1f6b9cd87f596b8 Mon Sep 17 00:00:00 2001 From: Stephen Davies Date: Sun, 24 Aug 2014 10:24:05 +0930 Subject: Progress in collecting comments and with comments within a block. No joy with comments before BEGIN or END and, before { or after }. Changed my email --- profile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'profile.c') diff --git a/profile.c b/profile.c index 5c7555e6..51ef9715 100644 --- a/profile.c +++ b/profile.c @@ -882,7 +882,7 @@ cleanup: count = pc->memory->stlen; text = pc->memory->stptr; - indent(SPACEOVER); + indent(SPACEOVER); /* is this correct? Where should comments go? */ for (; count > 0; count--, text++) { if (after_newline) { indent(SPACEOVER); -- cgit v1.2.3 From ae131ae925aa5ba54fb7f8ae105e5cbbf3655f06 Mon Sep 17 00:00:00 2001 From: Stephen Davies Date: Wed, 3 Sep 2014 12:23:22 +0930 Subject: ll except functions looking OK --- profile.c | 72 ++++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 24 deletions(-) (limited to 'profile.c') diff --git a/profile.c b/profile.c index 51ef9715..4db29c9a 100644 --- a/profile.c +++ b/profile.c @@ -36,7 +36,8 @@ static bool is_scalar(int type); static int prec_level(int type); static void pp_push(int type, char *s, int flag); static NODE *pp_pop(void); -static void pp_free(NODE *n); +static void pprint(INSTRUCTION *startp, INSTRUCTION *endp, bool in_for_header); +static void print_comment(INSTRUCTION *pc, long in); const char *redir2str(int redirtype); #define pp_str vname @@ -174,9 +175,12 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, bool in_for_header) char *str; NODE *t2; INSTRUCTION *ip; + INSTRUCTION *ic; + INSTRUCTION *i2; NODE *m; char *tmp; int rule; + long lind; static int rule_count[MAXRULE]; for (pc = startp; pc != endp; pc = pc->nexti) { @@ -189,16 +193,30 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, bool in_for_header) rule = pc->in_rule; if (rule != Rule) { + ip = (pc + 1)->firsti; + if (ip->opcode == Op_comment){ + /* print pre-begin/end comments */ + print_comment(ip, 0); + ip = pc->nexti->nexti; + } if (! rule_count[rule]++) fprintf(prof_fp, _("\t# %s block(s)\n\n"), ruletab[rule]); fprintf(prof_fp, "\t%s {\n", ruletab[rule]); - ip = (pc + 1)->firsti; } else { if (! rule_count[rule]++) fprintf(prof_fp, _("\t# Rule(s)\n\n")); - ip = pc->nexti; - indent(ip->exec_count); + ic = ip = pc->nexti; + i2 = (pc + 1)->firsti; + lind = ip->exec_count; + /*print pre-block comments */ + if(ip->opcode == Op_exec_count && ip->nexti->opcode == Op_comment)ip = ip->nexti; + if(ip->opcode == Op_comment){ + print_comment(ip, lind); + if (ip->nexti->nexti == (pc + 1)->firsti) + ip = ip->nexti->nexti; + } if (ip != (pc + 1)->firsti) { /* non-empty pattern */ + indent(lind); pprint(ip->nexti, (pc + 1)->firsti, false); t1 = pp_pop(); fprintf(prof_fp, "%s {", t1->pp_str); @@ -210,7 +228,7 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, bool in_for_header) fprintf(prof_fp, "\n"); } else { - fprintf(prof_fp, "{\n"); + fprintf(prof_fp, "\t{\n"); ip = (pc + 1)->firsti; } ip = ip->nexti; @@ -874,27 +892,11 @@ cleanup: break; case Op_comment: - { - char *text; - size_t count; - bool after_newline = false; - - count = pc->memory->stlen; - text = pc->memory->stptr; - - indent(SPACEOVER); /* is this correct? Where should comments go? */ - for (; count > 0; count--, text++) { - if (after_newline) { - indent(SPACEOVER); - after_newline = false; - } - putc(*text, prof_fp); - if (*text == '\n') - after_newline = true; - } - } + print_comment(pc, 0); break; + case Op_list: + break; default: cant_happen(); } @@ -977,6 +979,28 @@ print_lib_list(FILE *prof_fp) fprintf(prof_fp, "\n"); } +/* print comment text with proper indentation */ +static void +print_comment(INSTRUCTION* pc, long in){ + char *text; + size_t count; + bool after_newline = false; + + count = pc->memory->stlen; + text = pc->memory->stptr; + + indent(in); /* is this correct? Where should comments go? */ + for (; count > 0; count--, text++) { + if (after_newline) { + indent(in); + after_newline = false; + } + putc(*text, prof_fp); + if (*text == '\n') + after_newline = true; + } +} + /* dump_prog --- dump the program */ /* -- cgit v1.2.3 From 4282c7757598300ee050374f3649ffee070ed680 Mon Sep 17 00:00:00 2001 From: Stephen Davies Date: Sun, 7 Sep 2014 17:24:24 +0930 Subject: function comments 1 --- profile.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'profile.c') diff --git a/profile.c b/profile.c index 4db29c9a..8eb31b24 100644 --- a/profile.c +++ b/profile.c @@ -101,10 +101,11 @@ indent(long count) { int i; - if (count == 0) - fprintf(prof_fp, "\t"); - else - fprintf(prof_fp, "%6ld ", count); + if ( do_profile) + if (count == 0) + fprintf(prof_fp, "\t"); + else + fprintf(prof_fp, "%6ld ", count); assert(indent_level >= 0); for (i = 0; i < indent_level; i++) @@ -197,13 +198,13 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, bool in_for_header) if (ip->opcode == Op_comment){ /* print pre-begin/end comments */ print_comment(ip, 0); - ip = pc->nexti->nexti; + ip = ip->nexti->nexti; } - if (! rule_count[rule]++) + if (do_profile && ! rule_count[rule]++) fprintf(prof_fp, _("\t# %s block(s)\n\n"), ruletab[rule]); fprintf(prof_fp, "\t%s {\n", ruletab[rule]); } else { - if (! rule_count[rule]++) + if (do_profile && ! rule_count[rule]++) fprintf(prof_fp, _("\t# Rule(s)\n\n")); ic = ip = pc->nexti; i2 = (pc + 1)->firsti; @@ -1015,7 +1016,8 @@ dump_prog(INSTRUCTION *code) (void) time(& now); /* \n on purpose, with \n in ctime() output */ - fprintf(prof_fp, _("\t# gawk profile, created %s\n"), ctime(& now)); + if (do_profile) + fprintf(prof_fp, _("\t# gawk profile, created %s\n"), ctime(& now)); print_lib_list(prof_fp); pprint(code, NULL, false); } @@ -1515,14 +1517,23 @@ pp_func(INSTRUCTION *pc, void *data ATTRIBUTE_UNUSED) static bool first = true; NODE *func; int pcount; + INSTRUCTION *fp; if (first) { first = false; - fprintf(prof_fp, _("\n\t# Functions, listed alphabetically\n")); + if (do_profile) + fprintf(prof_fp, _("\n\t# Functions, listed alphabetically\n")); } + fp = pc->nexti->nexti; func = pc->func_body; fprintf(prof_fp, "\n"); +/* print any function comment */ + if (fp->opcode == Op_comment && fp->source_line == 0){ + print_comment(fp, 0); + fp = fp->nexti; + } + fprintf(prof_fp, "\t"); indent(pc->nexti->exec_count); fprintf(prof_fp, "%s %s(", op2str(Op_K_function), func->vname); pcount = func->param_cnt; @@ -1534,7 +1545,7 @@ pp_func(INSTRUCTION *pc, void *data ATTRIBUTE_UNUSED) } fprintf(prof_fp, ")\n\t{\n"); indent_in(); - pprint(pc->nexti->nexti, NULL, false); /* function body */ + pprint(fp, NULL, false); /* function body */ indent_out(); fprintf(prof_fp, "\t}\n"); return 0; -- cgit v1.2.3 From a1df6304be3c217877919097d2e4b3b16de6cd02 Mon Sep 17 00:00:00 2001 From: Stephen Davies Date: Mon, 8 Sep 2014 13:46:17 +0930 Subject: profile.c fixes --- profile.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'profile.c') diff --git a/profile.c b/profile.c index 8eb31b24..f1ced7e1 100644 --- a/profile.c +++ b/profile.c @@ -176,8 +176,6 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, bool in_for_header) char *str; NODE *t2; INSTRUCTION *ip; - INSTRUCTION *ic; - INSTRUCTION *i2; NODE *m; char *tmp; int rule; @@ -198,7 +196,7 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, bool in_for_header) if (ip->opcode == Op_comment){ /* print pre-begin/end comments */ print_comment(ip, 0); - ip = ip->nexti->nexti; + ip = ip->nexti; } if (do_profile && ! rule_count[rule]++) fprintf(prof_fp, _("\t# %s block(s)\n\n"), ruletab[rule]); @@ -206,8 +204,7 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, bool in_for_header) } else { if (do_profile && ! rule_count[rule]++) fprintf(prof_fp, _("\t# Rule(s)\n\n")); - ic = ip = pc->nexti; - i2 = (pc + 1)->firsti; + ip = pc->nexti; lind = ip->exec_count; /*print pre-block comments */ if(ip->opcode == Op_exec_count && ip->nexti->opcode == Op_comment)ip = ip->nexti; @@ -1533,7 +1530,8 @@ pp_func(INSTRUCTION *pc, void *data ATTRIBUTE_UNUSED) print_comment(fp, 0); fp = fp->nexti; } - fprintf(prof_fp, "\t"); + if (!do_profile) + fprintf(prof_fp, "\t"); indent(pc->nexti->exec_count); fprintf(prof_fp, "%s %s(", op2str(Op_K_function), func->vname); pcount = func->param_cnt; -- cgit v1.2.3 From ae1c0daca3998e5be8369e632e093f9f6dce1a70 Mon Sep 17 00:00:00 2001 From: Stephen Davies Date: Mon, 8 Sep 2014 15:13:21 +0930 Subject: fix indents --- profile.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'profile.c') diff --git a/profile.c b/profile.c index f1ced7e1..e23a17ac 100644 --- a/profile.c +++ b/profile.c @@ -200,7 +200,7 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, bool in_for_header) } if (do_profile && ! rule_count[rule]++) fprintf(prof_fp, _("\t# %s block(s)\n\n"), ruletab[rule]); - fprintf(prof_fp, "\t%s {\n", ruletab[rule]); + fprintf(prof_fp, "%s {\n", ruletab[rule]); } else { if (do_profile && ! rule_count[rule]++) fprintf(prof_fp, _("\t# Rule(s)\n\n")); @@ -226,7 +226,7 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, bool in_for_header) fprintf(prof_fp, "\n"); } else { - fprintf(prof_fp, "\t{\n"); + fprintf(prof_fp, "{\n"); ip = (pc + 1)->firsti; } ip = ip->nexti; @@ -234,7 +234,7 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, bool in_for_header) indent_in(); pprint(ip, (pc + 1)->lasti, false); indent_out(); - fprintf(prof_fp, "\t}\n\n"); + fprintf(prof_fp, "}\n\n"); pc = (pc + 1)->lasti; break; @@ -1530,7 +1530,7 @@ pp_func(INSTRUCTION *pc, void *data ATTRIBUTE_UNUSED) print_comment(fp, 0); fp = fp->nexti; } - if (!do_profile) + if (do_profile) fprintf(prof_fp, "\t"); indent(pc->nexti->exec_count); fprintf(prof_fp, "%s %s(", op2str(Op_K_function), func->vname); @@ -1541,11 +1541,11 @@ pp_func(INSTRUCTION *pc, void *data ATTRIBUTE_UNUSED) if (j < pcount - 1) fprintf(prof_fp, ", "); } - fprintf(prof_fp, ")\n\t{\n"); + fprintf(prof_fp, ")\n{\n"); indent_in(); pprint(fp, NULL, false); /* function body */ indent_out(); - fprintf(prof_fp, "\t}\n"); + fprintf(prof_fp, "}\n"); return 0; } -- cgit v1.2.3 From d7c8dfd41bd5671ddfa6c81b2ef1779cab06e56b Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Wed, 10 Sep 2014 08:14:35 +0300 Subject: Fix some minor indentation nits in profiling. --- profile.c | 76 +++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 45 insertions(+), 31 deletions(-) (limited to 'profile.c') diff --git a/profile.c b/profile.c index e23a17ac..e2211e1b 100644 --- a/profile.c +++ b/profile.c @@ -101,7 +101,7 @@ indent(long count) { int i; - if ( do_profile) + if (do_profile) if (count == 0) fprintf(prof_fp, "\t"); else @@ -193,22 +193,28 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, bool in_for_header) if (rule != Rule) { ip = (pc + 1)->firsti; - if (ip->opcode == Op_comment){ - /* print pre-begin/end comments */ + + /* print pre-begin/end comments */ + if (ip->opcode == Op_comment) { print_comment(ip, 0); ip = ip->nexti; } - if (do_profile && ! rule_count[rule]++) - fprintf(prof_fp, _("\t# %s block(s)\n\n"), ruletab[rule]); + + if (do_profile) { + if (! rule_count[rule]++) + fprintf(prof_fp, _("\t# %s block(s)\n\n"), ruletab[rule]); + indent(0); + } fprintf(prof_fp, "%s {\n", ruletab[rule]); } else { if (do_profile && ! rule_count[rule]++) fprintf(prof_fp, _("\t# Rule(s)\n\n")); ip = pc->nexti; lind = ip->exec_count; - /*print pre-block comments */ - if(ip->opcode == Op_exec_count && ip->nexti->opcode == Op_comment)ip = ip->nexti; - if(ip->opcode == Op_comment){ + /* print pre-block comments */ + if (ip->opcode == Op_exec_count && ip->nexti->opcode == Op_comment) + ip = ip->nexti; + if (ip->opcode == Op_comment) { print_comment(ip, lind); if (ip->nexti->nexti == (pc + 1)->firsti) ip = ip->nexti->nexti; @@ -234,6 +240,8 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, bool in_for_header) indent_in(); pprint(ip, (pc + 1)->lasti, false); indent_out(); + if (do_profile) + indent(0); fprintf(prof_fp, "}\n\n"); pc = (pc + 1)->lasti; break; @@ -979,24 +987,25 @@ print_lib_list(FILE *prof_fp) /* print comment text with proper indentation */ static void -print_comment(INSTRUCTION* pc, long in){ - char *text; - size_t count; - bool after_newline = false; - - count = pc->memory->stlen; - text = pc->memory->stptr; - - indent(in); /* is this correct? Where should comments go? */ - for (; count > 0; count--, text++) { - if (after_newline) { - indent(in); - after_newline = false; - } - putc(*text, prof_fp); - if (*text == '\n') - after_newline = true; - } +print_comment(INSTRUCTION* pc, long in) +{ + char *text; + size_t count; + bool after_newline = false; + + count = pc->memory->stlen; + text = pc->memory->stptr; + + indent(in); /* is this correct? Where should comments go? */ + for (; count > 0; count--, text++) { + if (after_newline) { + indent(in); + after_newline = false; + } + putc(*text, prof_fp); + if (*text == '\n') + after_newline = true; + } } /* dump_prog --- dump the program */ @@ -1525,13 +1534,13 @@ pp_func(INSTRUCTION *pc, void *data ATTRIBUTE_UNUSED) fp = pc->nexti->nexti; func = pc->func_body; fprintf(prof_fp, "\n"); -/* print any function comment */ - if (fp->opcode == Op_comment && fp->source_line == 0){ + + /* print any function comment */ + if (fp->opcode == Op_comment && fp->source_line == 0) { print_comment(fp, 0); fp = fp->nexti; } - if (do_profile) - fprintf(prof_fp, "\t"); + indent(pc->nexti->exec_count); fprintf(prof_fp, "%s %s(", op2str(Op_K_function), func->vname); pcount = func->param_cnt; @@ -1541,10 +1550,15 @@ pp_func(INSTRUCTION *pc, void *data ATTRIBUTE_UNUSED) if (j < pcount - 1) fprintf(prof_fp, ", "); } - fprintf(prof_fp, ")\n{\n"); + fprintf(prof_fp, ")\n"); + if (do_profile) + indent(0); + fprintf(prof_fp, "{\n"); indent_in(); pprint(fp, NULL, false); /* function body */ indent_out(); + if (do_profile) + indent(0); fprintf(prof_fp, "}\n"); return 0; } -- cgit v1.2.3 From 66479f2ca1fbbf3b96cd2e1b15c0119b209df54a Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Sat, 27 Sep 2014 22:25:57 +0300 Subject: Minor improvement in profiling output. --- profile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'profile.c') diff --git a/profile.c b/profile.c index d07bea4a..b0fbbedb 100644 --- a/profile.c +++ b/profile.c @@ -190,7 +190,7 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, bool in_for_header) if (rule != Rule) { if (! rule_count[rule]++) - fprintf(prof_fp, _("\t# %s block(s)\n\n"), ruletab[rule]); + fprintf(prof_fp, _("\t# %s rules(s)\n\n"), ruletab[rule]); fprintf(prof_fp, "\t%s {\n", ruletab[rule]); ip = (pc + 1)->firsti; } else { -- cgit v1.2.3 From ed633f19bdbb66ac12aaf66cf46b458558eaedbd Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Wed, 1 Oct 2014 22:06:31 +0300 Subject: Some cleanups and doc additions. --- profile.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'profile.c') diff --git a/profile.c b/profile.c index ddb9c357..0a137e29 100644 --- a/profile.c +++ b/profile.c @@ -101,11 +101,12 @@ indent(long count) { int i; - if (do_profile) + if (do_profile) { if (count == 0) fprintf(prof_fp, "\t"); else fprintf(prof_fp, "%6ld ", count); + } assert(indent_level >= 0); for (i = 0; i < indent_level; i++) -- cgit v1.2.3 From 4959339c8a962b54bf33fd9e3d9f46b3f1c3c29e Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Thu, 2 Oct 2014 20:06:36 +0300 Subject: Code review and minor whitespace cleanups. --- profile.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'profile.c') diff --git a/profile.c b/profile.c index 0a137e29..da41a560 100644 --- a/profile.c +++ b/profile.c @@ -912,6 +912,7 @@ cleanup: case Op_list: break; + default: cant_happen(); } @@ -994,7 +995,8 @@ print_lib_list(FILE *prof_fp) fprintf(prof_fp, "\n"); } -/* print comment text with proper indentation */ +/* print_comment --- print comment text with proper indentation */ + static void print_comment(INSTRUCTION* pc, long in) { -- cgit v1.2.3 From 42bcb6246432790af31958d5445cd035fe9966a5 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Sat, 4 Oct 2014 23:29:29 +0300 Subject: Fix --gen-pot with long strings. --- profile.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'profile.c') diff --git a/profile.c b/profile.c index b0fbbedb..84e7f9da 100644 --- a/profile.c +++ b/profile.c @@ -909,11 +909,11 @@ pp_string_fp(Func_print print_func, FILE *fp, const char *in_str, slen = strlen(str); for (count = 0; slen > 0; slen--, str++) { + print_func(fp, "%c", *str); if (++count >= BREAKPOINT && breaklines) { print_func(fp, "%c\n%c", delim, delim); count = 0; - } else - print_func(fp, "%c", *str); + } } efree(s); } -- cgit v1.2.3 From 6b818c7f463e1bd6b9470f4bfb694f240ac3e5c2 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Sun, 5 Oct 2014 20:06:38 +0300 Subject: Minor typo fix in profiling output. --- profile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'profile.c') diff --git a/profile.c b/profile.c index 84e7f9da..a5ed381b 100644 --- a/profile.c +++ b/profile.c @@ -190,7 +190,7 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, bool in_for_header) if (rule != Rule) { if (! rule_count[rule]++) - fprintf(prof_fp, _("\t# %s rules(s)\n\n"), ruletab[rule]); + fprintf(prof_fp, _("\t# %s rule(s)\n\n"), ruletab[rule]); fprintf(prof_fp, "\t%s {\n", ruletab[rule]); ip = (pc + 1)->firsti; } else { -- cgit v1.2.3 From c2cda8d3736b59738f579fce748e94ca109ccc58 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Thu, 30 Oct 2014 21:33:59 +0200 Subject: Fixes in pretty-printer. --- profile.c | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) (limited to 'profile.c') diff --git a/profile.c b/profile.c index a5ed381b..ed17e62b 100644 --- a/profile.c +++ b/profile.c @@ -395,7 +395,8 @@ cleanup: case Op_unary_minus: case Op_not: t1 = pp_pop(); - if (is_binary(t1->type)) + if (is_binary(t1->type) + || (((OPCODE) t1->type) == pc->opcode && pc->opcode == Op_unary_minus)) pp_parenthesize(t1); /* optypes table (eval.c) includes space after ! */ @@ -1000,25 +1001,25 @@ prec_level(int type) case Op_func_call: case Op_K_delete_loop: case Op_builtin: - return 15; + return 16; case Op_field_spec: case Op_field_spec_lhs: - return 14; - - case Op_exp: - case Op_exp_i: - return 13; + return 15; case Op_preincrement: case Op_predecrement: case Op_postincrement: case Op_postdecrement: - return 12; + return 14; + + case Op_exp: + case Op_exp_i: + return 13; case Op_unary_minus: case Op_not: - return 11; + return 12; case Op_times: case Op_times_i: @@ -1026,23 +1027,26 @@ prec_level(int type) case Op_quotient_i: case Op_mod: case Op_mod_i: - return 10; + return 11; case Op_plus: case Op_plus_i: case Op_minus: case Op_minus_i: - return 9; + return 10; case Op_concat: case Op_assign_concat: - return 8; + return 9; case Op_equal: case Op_notequal: case Op_greater: + case Op_less: case Op_leq: case Op_geq: + return 8; + case Op_match: case Op_nomatch: return 7; @@ -1051,7 +1055,6 @@ prec_level(int type) case Op_K_getline_redir: return 6; - case Op_less: case Op_in_array: return 5; @@ -1360,6 +1363,14 @@ pp_list(int nargs, const char *paren, const char *delim) return str; } +/* is_unary_minus --- return true if string starts with unary minus */ + +static bool +is_unary_minus(const char *str) +{ + return str[0] == '-' && str[1] != '-'; +} + /* pp_concat --- handle concatenation and correct parenthesizing of expressions */ static char * @@ -1401,7 +1412,12 @@ pp_concat(int nargs) pl_l = prec_level(pp_args[i]->type); pl_r = prec_level(pp_args[i+1]->type); - if (is_scalar(pp_args[i]->type) && is_scalar(pp_args[i+1]->type)) { + if (i >= 2 && is_unary_minus(r->pp_str)) { + *s++ = '('; + memcpy(s, r->pp_str, r->pp_len); + s += r->pp_len; + *s++ = ')'; + } else if (is_scalar(pp_args[i]->type) && is_scalar(pp_args[i+1]->type)) { memcpy(s, r->pp_str, r->pp_len); s += r->pp_len; } else if (pl_l <= pl_r || is_scalar(pp_args[i+1]->type)) { @@ -1423,7 +1439,7 @@ pp_concat(int nargs) pl_l = prec_level(pp_args[nargs-1]->type); pl_r = prec_level(pp_args[nargs]->type); r = pp_args[nargs]; - if (pl_l >= pl_r && ! is_scalar(pp_args[nargs]->type)) { + if (is_unary_minus(r->pp_str) || ((pl_l >= pl_r && ! is_scalar(pp_args[nargs]->type)))) { *s++ = '('; memcpy(s, r->pp_str, r->pp_len); s += r->pp_len; -- cgit v1.2.3 From c5227d1685aa158e63d4b6a6289063ae985673c1 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Sun, 2 Nov 2014 21:29:20 +0200 Subject: Additional profiling fix. --- profile.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'profile.c') diff --git a/profile.c b/profile.c index ed17e62b..316ba393 100644 --- a/profile.c +++ b/profile.c @@ -1180,6 +1180,26 @@ pp_parenthesize(NODE *sp) sp->flags |= CAN_FREE; } +/* div_on_left_mul_on_right --- have / or % on left and * on right */ + +static bool +div_on_left_mul_on_right(int o1, int o2) +{ + OPCODE op1 = (OPCODE) o1; + OPCODE op2 = (OPCODE) o2; + + switch (op1) { + case Op_quotient: + case Op_quotient_i: + case Op_mod: + case Op_mod_i: + return (op2 == Op_times || op2 == Op_times_i); + + default: + return false; + } +} + /* parenthesize --- parenthesize two nodes relative to parent node type */ static void @@ -1189,9 +1209,11 @@ parenthesize(int type, NODE *left, NODE *right) int lprec = prec_level(left->type); int prec = prec_level(type); - if (lprec < prec) + if (lprec < prec + || (lprec == prec && div_on_left_mul_on_right(left->type, type))) pp_parenthesize(left); - if (rprec < prec) + if (rprec < prec + || (rprec == prec && div_on_left_mul_on_right(type, right->type))) pp_parenthesize(right); } -- cgit v1.2.3