aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2018-12-19 14:57:23 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2018-12-19 14:59:22 +0100
commit684d441f5c8c4eac5962ad0e0b59e8999284fe6a (patch)
tree37e405245526e7b24c2b7dc1006617842b249205
parent085b4202209c7cf9fe12b8823c939e8e028ed775 (diff)
downloadbusybox-w32-684d441f5c8c4eac5962ad0e0b59e8999284fe6a.tar.gz
busybox-w32-684d441f5c8c4eac5962ad0e0b59e8999284fe6a.tar.bz2
busybox-w32-684d441f5c8c4eac5962ad0e0b59e8999284fe6a.zip
bc: make bc_program_addFunc() return new idx, untangle &p->fidx interaction
In: bc_program_addFunc(name, idx); p->func = bc_program_func(p->fidx); in some cases p->fidx was updated by _first_ statement - because passed idx was pointing at it. This was very obscure. function old new delta zdc_parse_expr 653 658 +5 bc_program_addFunc 204 201 -3 zbc_vm_process 594 586 -8 bc_vm_init 663 655 -8 zbc_parse_name 482 472 -10 bc_parse_addFunc 25 - -25 zbc_program_asciify 473 447 -26 ------------------------------------------------------------------------------ (add/remove: 0/1 grow/shrink: 1/5 up/down: 5/-80) Total: -75 bytes text data bss dec hex filename 981482 485 7296 989263 f184f busybox_old 981401 485 7296 989182 f17fe busybox_unstripped Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--miscutils/bc.c90
1 files changed, 43 insertions, 47 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c
index ec7a0838b..d03d38aa9 100644
--- a/miscutils/bc.c
+++ b/miscutils/bc.c
@@ -3439,15 +3439,6 @@ static BC_STATUS zdc_lex_token(BcLex *l)
3439} 3439}
3440#endif // ENABLE_DC 3440#endif // ENABLE_DC
3441 3441
3442static void bc_program_addFunc(char *name, size_t *idx);
3443
3444// Note: takes ownership of 'name' (must be malloced)
3445static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx)
3446{
3447 bc_program_addFunc(name, idx);
3448 p->func = bc_program_func(p->fidx);
3449}
3450
3451static void bc_parse_push(BcParse *p, char i) 3442static void bc_parse_push(BcParse *p, char i)
3452{ 3443{
3453 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, p->func->code.len, i); 3444 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, p->func->code.len, i);
@@ -3721,6 +3712,39 @@ static BC_STATUS zbc_parse_params(BcParse *p, uint8_t flags)
3721#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS) 3712#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
3722 3713
3723// Note: takes ownership of 'name' (must be malloced) 3714// Note: takes ownership of 'name' (must be malloced)
3715static size_t bc_program_addFunc(char *name)
3716{
3717 size_t idx;
3718 BcId entry, *entry_ptr;
3719 BcFunc f;
3720 int inserted;
3721
3722 entry.name = name;
3723 entry.idx = G.prog.fns.len;
3724
3725 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3726 if (!inserted) free(name);
3727
3728 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3729 idx = entry_ptr->idx;
3730
3731 if (!inserted) {
3732 BcFunc *func = bc_program_func(entry_ptr->idx);
3733
3734 // We need to reset these, so the function can be repopulated.
3735 func->nparams = 0;
3736 bc_vec_pop_all(&func->autos);
3737 bc_vec_pop_all(&func->code);
3738 bc_vec_pop_all(&func->labels);
3739 } else {
3740 bc_func_init(&f);
3741 bc_vec_push(&G.prog.fns, &f);
3742 }
3743
3744 return idx;
3745}
3746
3747// Note: takes ownership of 'name' (must be malloced)
3724static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags) 3748static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags)
3725{ 3749{
3726 BcStatus s; 3750 BcStatus s;
@@ -3741,7 +3765,7 @@ static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags)
3741 3765
3742 if (idx == BC_VEC_INVALID_IDX) { 3766 if (idx == BC_VEC_INVALID_IDX) {
3743 // No such function exists, create an empty one 3767 // No such function exists, create an empty one
3744 bc_parse_addFunc(p, name, &idx); 3768 bc_program_addFunc(name);
3745 idx = bc_map_find_exact(&G.prog.fn_map, &entry); 3769 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
3746 } else 3770 } else
3747 free(name); 3771 free(name);
@@ -4249,7 +4273,8 @@ static BC_STATUS zbc_parse_funcdef(BcParse *p)
4249 RETURN_STATUS(bc_error("bad function definition")); 4273 RETURN_STATUS(bc_error("bad function definition"));
4250 4274
4251 name = xstrdup(p->l.t.v.v); 4275 name = xstrdup(p->l.t.v.v);
4252 bc_parse_addFunc(p, name, &p->fidx); 4276 p->fidx = bc_program_addFunc(name);
4277 p->func = bc_program_func(p->fidx);
4253 4278
4254 s = zbc_lex_next(&p->l); 4279 s = zbc_lex_next(&p->l);
4255 if (s) RETURN_STATUS(s); 4280 if (s) RETURN_STATUS(s);
@@ -4741,7 +4766,7 @@ static BC_STATUS zdc_parse_register(BcParse *p)
4741static BC_STATUS zdc_parse_string(BcParse *p) 4766static BC_STATUS zdc_parse_string(BcParse *p)
4742{ 4767{
4743 char *str, *name; 4768 char *str, *name;
4744 size_t idx, len = G.prog.strs.len; 4769 size_t len = G.prog.strs.len;
4745 4770
4746//why pad to 32 zeros?? 4771//why pad to 32 zeros??
4747 name = xasprintf("%032lu", (unsigned long)len); 4772 name = xasprintf("%032lu", (unsigned long)len);
@@ -4750,7 +4775,8 @@ static BC_STATUS zdc_parse_string(BcParse *p)
4750 bc_parse_push(p, BC_INST_STR); 4775 bc_parse_push(p, BC_INST_STR);
4751 bc_parse_pushIndex(p, len); 4776 bc_parse_pushIndex(p, len);
4752 bc_vec_push(&G.prog.strs, &str); 4777 bc_vec_push(&G.prog.strs, &str);
4753 bc_parse_addFunc(p, name, &idx); 4778 bc_program_addFunc(name);
4779 p->func = bc_program_func(p->fidx);
4754 4780
4755 RETURN_STATUS(zbc_lex_next(&p->l)); 4781 RETURN_STATUS(zbc_lex_next(&p->l));
4756} 4782}
@@ -6121,7 +6147,7 @@ static BC_STATUS zbc_program_asciify(void)
6121 //str[1] = '\0'; - already is 6147 //str[1] = '\0'; - already is
6122 6148
6123 str2 = xstrdup(str); 6149 str2 = xstrdup(str);
6124 bc_program_addFunc(str2, &idx); 6150 idx = bc_program_addFunc(str2);
6125 6151
6126 if (idx != len + BC_PROG_REQ_FUNCS) { 6152 if (idx != len + BC_PROG_REQ_FUNCS) {
6127 for (idx = 0; idx < G.prog.strs.len; ++idx) { 6153 for (idx = 0; idx < G.prog.strs.len; ++idx) {
@@ -6318,36 +6344,6 @@ static void bc_program_pushGlobal(char inst)
6318 bc_vec_push(&G.prog.results, &res); 6344 bc_vec_push(&G.prog.results, &res);
6319} 6345}
6320 6346
6321// Note: takes ownership of 'name' (must be malloced)
6322static void bc_program_addFunc(char *name, size_t *idx)
6323{
6324 BcId entry, *entry_ptr;
6325 BcFunc f;
6326 int inserted;
6327
6328 entry.name = name;
6329 entry.idx = G.prog.fns.len;
6330
6331 inserted = bc_map_insert(&G.prog.fn_map, &entry, idx);
6332 if (!inserted) free(name);
6333
6334 entry_ptr = bc_vec_item(&G.prog.fn_map, *idx);
6335 *idx = entry_ptr->idx;
6336
6337 if (!inserted) {
6338 BcFunc *func = bc_program_func(entry_ptr->idx);
6339
6340 // We need to reset these, so the function can be repopulated.
6341 func->nparams = 0;
6342 bc_vec_pop_all(&func->autos);
6343 bc_vec_pop_all(&func->code);
6344 bc_vec_pop_all(&func->labels);
6345 } else {
6346 bc_func_init(&f);
6347 bc_vec_push(&G.prog.fns, &f);
6348 }
6349}
6350
6351static BC_STATUS zbc_program_exec(void) 6347static BC_STATUS zbc_program_exec(void)
6352{ 6348{
6353 BcResult r, *ptr; 6349 BcResult r, *ptr;
@@ -6982,7 +6978,6 @@ static void bc_vm_free(void)
6982 6978
6983static void bc_program_init(void) 6979static void bc_program_init(void)
6984{ 6980{
6985 size_t idx;
6986 BcInstPtr ip; 6981 BcInstPtr ip;
6987 6982
6988 // memset(&G.prog, 0, sizeof(G.prog)); - already is 6983 // memset(&G.prog, 0, sizeof(G.prog)); - already is
@@ -7004,8 +6999,9 @@ static void bc_program_init(void)
7004 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free); 6999 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
7005 bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free); 7000 bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);
7006 7001
7007 bc_program_addFunc(xstrdup("(main)"), &idx); 7002//TODO: with "", dc_strings.dc enters infinite loop, ??!
7008 bc_program_addFunc(xstrdup("(read)"), &idx); 7003 bc_program_addFunc(xstrdup("(m)")); // func #0: main
7004 bc_program_addFunc(xstrdup("(r)")); // func #1: for read()
7009 7005
7010 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free); 7006 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
7011 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free); 7007 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);