aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2018-12-14 17:51:17 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2018-12-14 17:51:17 +0100
commit2ea53a45b7cbad94d9fffcc56eb488aea882449a (patch)
treeba6c4eb9f4a62336eeb44f2e9c365c943bf57deb
parentf86e960e7c2a0491dc2564fb0bf6cef10ae24ee2 (diff)
downloadbusybox-w32-2ea53a45b7cbad94d9fffcc56eb488aea882449a.tar.gz
busybox-w32-2ea53a45b7cbad94d9fffcc56eb488aea882449a.tar.bz2
busybox-w32-2ea53a45b7cbad94d9fffcc56eb488aea882449a.zip
bc: convert macro bc_parse_push() to function, add debug infrastructure
function old new delta bc_parse_push - 14 +14 zbc_parse_else 132 134 +2 bc_parse_pushName 63 61 -2 zbc_parse_operator 174 170 -4 bc_parse_number 87 83 -4 zbc_parse_string 97 89 -8 bc_parse_pushIndex 68 60 -8 zbc_parse_endBody 339 326 -13 zbc_parse_name 401 387 -14 zdc_parse_mem 107 91 -16 zdc_parse_expr 680 638 -42 zbc_parse_stmt 1502 1456 -46 bc_parse_expr_empty_ok 1838 1788 -50 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 1/11 up/down: 16/-207) Total: -191 bytes text data bss dec hex filename 980129 485 7296 987910 f1306 busybox_old 979938 485 7296 987719 f1247 busybox_unstripped Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--miscutils/bc.c64
1 files changed, 53 insertions, 11 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c
index 8685f2c8e..06fcc8efa 100644
--- a/miscutils/bc.c
+++ b/miscutils/bc.c
@@ -167,10 +167,29 @@
167# include "dc.c" 167# include "dc.c"
168#else 168#else
169 169
170#if 0 170#define DEBUG_LEXER 0
171# define dbg_lex(...) bb_error_msg(__VA_ARGS__) 171
172#if DEBUG_LEXER
173static unsigned lex_indent;
174#define dbg_lex(...) \
175 do { \
176 fprintf(stderr, "%*s", lex_indent, ""); \
177 bb_error_msg(__VA_ARGS__); \
178 } while (0)
179#define dbg_lex_enter(...) \
180 do { \
181 dbg_lex(__VA_ARGS__); \
182 lex_indent++; \
183 } while (0)
184#define dbg_lex_done(...) \
185 do { \
186 lex_indent--; \
187 dbg_lex(__VA_ARGS__); \
188 } while (0)
172#else 189#else
173# define dbg_lex(...) ((void)0) 190# define dbg_lex(...) ((void)0)
191# define dbg_lex_enter(...) ((void)0)
192# define dbg_lex_done(...) ((void)0)
174#endif 193#endif
175 194
176typedef enum BcStatus { 195typedef enum BcStatus {
@@ -2941,7 +2960,7 @@ static BC_STATUS zbc_lex_next(BcLex *l)
2941 // is so the parser doesn't get inundated with whitespace. 2960 // is so the parser doesn't get inundated with whitespace.
2942 s = BC_STATUS_SUCCESS; 2961 s = BC_STATUS_SUCCESS;
2943 do { 2962 do {
2944 dbg_lex("next token:'%.*s'", 2963 dbg_lex("next string to parse:'%.*s'",
2945 (int)(strchrnul(l->buf + l->i, '\n') - (l->buf + l->i)), 2964 (int)(strchrnul(l->buf + l->i, '\n') - (l->buf + l->i)),
2946 l->buf + l->i); 2965 l->buf + l->i);
2947 ERROR_RETURN(s =) zcommon_lex_token(l); 2966 ERROR_RETURN(s =) zcommon_lex_token(l);
@@ -2954,7 +2973,7 @@ static BC_STATUS zbc_lex_next(BcLex *l)
2954# define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__), BC_STATUS_SUCCESS) 2973# define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__), BC_STATUS_SUCCESS)
2955#endif 2974#endif
2956 2975
2957static BC_STATUS zbc_lex_text(BcLex *l, const char *text) 2976static BC_STATUS zbc_lex_text_init(BcLex *l, const char *text)
2958{ 2977{
2959 l->buf = text; 2978 l->buf = text;
2960 l->i = 0; 2979 l->i = 0;
@@ -2963,7 +2982,7 @@ static BC_STATUS zbc_lex_text(BcLex *l, const char *text)
2963 RETURN_STATUS(zbc_lex_next(l)); 2982 RETURN_STATUS(zbc_lex_next(l));
2964} 2983}
2965#if ERRORS_ARE_FATAL 2984#if ERRORS_ARE_FATAL
2966# define zbc_lex_text(...) (zbc_lex_text(__VA_ARGS__), BC_STATUS_SUCCESS) 2985# define zbc_lex_text_init(...) (zbc_lex_text_init(__VA_ARGS__), BC_STATUS_SUCCESS)
2967#endif 2986#endif
2968 2987
2969#if ENABLE_BC 2988#if ENABLE_BC
@@ -3431,7 +3450,11 @@ static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx)
3431 p->func = bc_program_func(p->fidx); 3450 p->func = bc_program_func(p->fidx);
3432} 3451}
3433 3452
3434#define bc_parse_push(p, i) bc_vec_pushByte(&(p)->func->code, (char) (i)) 3453static void bc_parse_push(BcParse *p, char i)
3454{
3455 dbg_lex("%s:%d pushing opcode %d", __func__, __LINE__, i);
3456 bc_vec_pushByte(&p->func->code, i);
3457}
3435 3458
3436static void bc_parse_pushName(BcParse *p, char *name) 3459static void bc_parse_pushName(BcParse *p, char *name)
3437{ 3460{
@@ -3448,6 +3471,7 @@ static void bc_parse_pushIndex(BcParse *p, size_t idx)
3448 size_t mask; 3471 size_t mask;
3449 unsigned amt; 3472 unsigned amt;
3450 3473
3474 dbg_lex("%s:%d pushing index %d", __func__, __LINE__, idx);
3451 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8); 3475 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3452 amt = sizeof(idx); 3476 amt = sizeof(idx);
3453 do { 3477 do {
@@ -3504,7 +3528,7 @@ static BC_STATUS zbc_parse_text_init(BcParse *p, const char *text)
3504 RETURN_STATUS(bc_error("file is not executable")); 3528 RETURN_STATUS(bc_error("file is not executable"));
3505 } 3529 }
3506 3530
3507 RETURN_STATUS(zbc_lex_text(&p->l, text)); 3531 RETURN_STATUS(zbc_lex_text_init(&p->l, text));
3508} 3532}
3509#if ERRORS_ARE_FATAL 3533#if ERRORS_ARE_FATAL
3510# define zbc_parse_text_init(...) (zbc_parse_text_init(__VA_ARGS__), BC_STATUS_SUCCESS) 3534# define zbc_parse_text_init(...) (zbc_parse_text_init(__VA_ARGS__), BC_STATUS_SUCCESS)
@@ -3662,6 +3686,7 @@ static BC_STATUS zbc_parse_params(BcParse *p, uint8_t flags)
3662 bool comma = false; 3686 bool comma = false;
3663 size_t nparams; 3687 size_t nparams;
3664 3688
3689 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
3665 s = zbc_lex_next(&p->l); 3690 s = zbc_lex_next(&p->l);
3666 if (s) RETURN_STATUS(s); 3691 if (s) RETURN_STATUS(s);
3667 3692
@@ -4140,6 +4165,7 @@ static BC_STATUS zbc_parse_if(BcParse *p)
4140 BcStatus s; 4165 BcStatus s;
4141 BcInstPtr ip; 4166 BcInstPtr ip;
4142 4167
4168 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
4143 s = zbc_lex_next(&p->l); 4169 s = zbc_lex_next(&p->l);
4144 if (s) RETURN_STATUS(s); 4170 if (s) RETURN_STATUS(s);
4145 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); 4171 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
@@ -4162,6 +4188,7 @@ static BC_STATUS zbc_parse_if(BcParse *p)
4162 bc_vec_push(&p->func->labels, &ip.idx); 4188 bc_vec_push(&p->func->labels, &ip.idx);
4163 bc_parse_startBody(p, BC_PARSE_FLAG_IF); 4189 bc_parse_startBody(p, BC_PARSE_FLAG_IF);
4164 4190
4191 dbg_lex_done("%s:%d done", __func__, __LINE__);
4165 RETURN_STATUS(BC_STATUS_SUCCESS); 4192 RETURN_STATUS(BC_STATUS_SUCCESS);
4166} 4193}
4167#if ERRORS_ARE_FATAL 4194#if ERRORS_ARE_FATAL
@@ -4173,6 +4200,7 @@ static BC_STATUS zbc_parse_else(BcParse *p)
4173{ 4200{
4174 BcInstPtr ip; 4201 BcInstPtr ip;
4175 4202
4203 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
4176 if (!BC_PARSE_IF_END(p)) RETURN_STATUS(bc_error_bad_token()); 4204 if (!BC_PARSE_IF_END(p)) RETURN_STATUS(bc_error_bad_token());
4177 4205
4178 ip.idx = p->func->labels.len; 4206 ip.idx = p->func->labels.len;
@@ -4238,6 +4266,7 @@ static BC_STATUS zbc_parse_for(BcParse *p)
4238 BcInstPtr ip; 4266 BcInstPtr ip;
4239 size_t cond_idx, exit_idx, body_idx, update_idx; 4267 size_t cond_idx, exit_idx, body_idx, update_idx;
4240 4268
4269 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
4241 s = zbc_lex_next(&p->l); 4270 s = zbc_lex_next(&p->l);
4242 if (s) RETURN_STATUS(s); 4271 if (s) RETURN_STATUS(s);
4243 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); 4272 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
@@ -4521,6 +4550,7 @@ static BC_STATUS zbc_parse_stmt(BcParse *p)
4521{ 4550{
4522 BcStatus s = BC_STATUS_SUCCESS; 4551 BcStatus s = BC_STATUS_SUCCESS;
4523 4552
4553 dbg_lex_enter("%s:%d entered, p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
4524 switch (p->l.t.t) { 4554 switch (p->l.t.t) {
4525 case BC_LEX_NLINE: 4555 case BC_LEX_NLINE:
4526 RETURN_STATUS(zbc_lex_next(&p->l)); 4556 RETURN_STATUS(zbc_lex_next(&p->l));
@@ -4550,6 +4580,7 @@ static BC_STATUS zbc_parse_stmt(BcParse *p)
4550 break; 4580 break;
4551 } 4581 }
4552 4582
4583 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
4553 switch (p->l.t.t) { 4584 switch (p->l.t.t) {
4554 case BC_LEX_OP_INC: 4585 case BC_LEX_OP_INC:
4555 case BC_LEX_OP_DEC: 4586 case BC_LEX_OP_DEC:
@@ -4628,6 +4659,7 @@ static BC_STATUS zbc_parse_stmt(BcParse *p)
4628 break; 4659 break;
4629 } 4660 }
4630 4661
4662 dbg_lex_done("%s:%d done", __func__, __LINE__);
4631 RETURN_STATUS(s); 4663 RETURN_STATUS(s);
4632} 4664}
4633#if ERRORS_ARE_FATAL 4665#if ERRORS_ARE_FATAL
@@ -4638,20 +4670,25 @@ static BC_STATUS zbc_parse_parse(BcParse *p)
4638{ 4670{
4639 BcStatus s; 4671 BcStatus s;
4640 4672
4673 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
4641 if (p->l.t.t == BC_LEX_EOF) 4674 if (p->l.t.t == BC_LEX_EOF)
4642 s = p->flags.len > 0 ? bc_error("block end could not be found") : bc_error("end of file"); 4675 s = p->flags.len > 0 ? bc_error("block end could not be found") : bc_error("end of file");
4643 else if (p->l.t.t == BC_LEX_KEY_DEFINE) { 4676 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
4677 dbg_lex("%s:%d p->l.t.t:BC_LEX_KEY_DEFINE", __func__, __LINE__);
4644 if (!BC_PARSE_CAN_EXEC(p)) 4678 if (!BC_PARSE_CAN_EXEC(p))
4645 RETURN_STATUS(bc_error_bad_token()); 4679 RETURN_STATUS(bc_error_bad_token());
4646 s = zbc_parse_func(p); 4680 s = zbc_parse_func(p);
4647 } else 4681 } else {
4682 dbg_lex("%s:%d p->l.t.t:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->l.t.t);
4648 s = zbc_parse_stmt(p); 4683 s = zbc_parse_stmt(p);
4684 }
4649 4685
4650 if (s || G_interrupt) { 4686 if (s || G_interrupt) {
4651 bc_parse_reset(p); 4687 bc_parse_reset(p);
4652 s = BC_STATUS_FAILURE; 4688 s = BC_STATUS_FAILURE;
4653 } 4689 }
4654 4690
4691 dbg_lex_done("%s:%d done", __func__, __LINE__);
4655 RETURN_STATUS(s); 4692 RETURN_STATUS(s);
4656} 4693}
4657#if ERRORS_ARE_FATAL 4694#if ERRORS_ARE_FATAL
@@ -6919,14 +6956,18 @@ static unsigned bc_vm_envLen(const char *var)
6919 6956
6920static BC_STATUS zbc_vm_process(const char *text) 6957static BC_STATUS zbc_vm_process(const char *text)
6921{ 6958{
6922 BcStatus s = zbc_parse_text_init(&G.prs, text); 6959 BcStatus s;
6923 6960
6961 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
6962 s = zbc_parse_text_init(&G.prs, text);
6924 if (s) RETURN_STATUS(s); 6963 if (s) RETURN_STATUS(s);
6925 6964
6926 while (G.prs.l.t.t != BC_LEX_EOF) { 6965 while (G.prs.l.t.t != BC_LEX_EOF) {
6966 dbg_lex("%s:%d G.prs.l.t.t:%d", __func__, __LINE__, G.prs.l.t.t);
6927 ERROR_RETURN(s =) zcommon_parse(&G.prs); 6967 ERROR_RETURN(s =) zcommon_parse(&G.prs);
6928 if (s) RETURN_STATUS(s); 6968 if (s) RETURN_STATUS(s);
6929 } 6969 }
6970 dbg_lex("%s:%d G.prs.l.t.t:BC_LEX_EOF", __func__, __LINE__);
6930 6971
6931 if (BC_PARSE_CAN_EXEC(&G.prs)) { 6972 if (BC_PARSE_CAN_EXEC(&G.prs)) {
6932 s = zbc_program_exec(); 6973 s = zbc_program_exec();
@@ -6935,6 +6976,7 @@ static BC_STATUS zbc_vm_process(const char *text)
6935 bc_program_reset(); 6976 bc_program_reset();
6936 } 6977 }
6937 6978
6979 dbg_lex_done("%s:%d done", __func__, __LINE__);
6938 RETURN_STATUS(s); 6980 RETURN_STATUS(s);
6939} 6981}
6940#if ERRORS_ARE_FATAL 6982#if ERRORS_ARE_FATAL
@@ -7324,7 +7366,7 @@ static BC_STATUS zbc_vm_exec(void)
7324 // thus error checking is normally disabled. 7366 // thus error checking is normally disabled.
7325# define DEBUG_LIB 0 7367# define DEBUG_LIB 0
7326 bc_lex_file(&G.prs.l); 7368 bc_lex_file(&G.prs.l);
7327 ERROR_RETURN(s =) zbc_vm_process(bc_lib); 7369 s = zbc_vm_process(bc_lib);
7328 if (DEBUG_LIB && s) RETURN_STATUS(s); 7370 if (DEBUG_LIB && s) RETURN_STATUS(s);
7329 } 7371 }
7330#endif 7372#endif