From 684d441f5c8c4eac5962ad0e0b59e8999284fe6a Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 19 Dec 2018 14:57:23 +0100 Subject: 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 --- miscutils/bc.c | 90 ++++++++++++++++++++++++++++------------------------------ 1 file 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) } #endif // ENABLE_DC -static void bc_program_addFunc(char *name, size_t *idx); - -// Note: takes ownership of 'name' (must be malloced) -static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx) -{ - bc_program_addFunc(name, idx); - p->func = bc_program_func(p->fidx); -} - static void bc_parse_push(BcParse *p, char i) { dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, p->func->code.len, i); @@ -3720,6 +3711,39 @@ static BC_STATUS zbc_parse_params(BcParse *p, uint8_t flags) } #define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS) +// Note: takes ownership of 'name' (must be malloced) +static size_t bc_program_addFunc(char *name) +{ + size_t idx; + BcId entry, *entry_ptr; + BcFunc f; + int inserted; + + entry.name = name; + entry.idx = G.prog.fns.len; + + inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx); + if (!inserted) free(name); + + entry_ptr = bc_vec_item(&G.prog.fn_map, idx); + idx = entry_ptr->idx; + + if (!inserted) { + BcFunc *func = bc_program_func(entry_ptr->idx); + + // We need to reset these, so the function can be repopulated. + func->nparams = 0; + bc_vec_pop_all(&func->autos); + bc_vec_pop_all(&func->code); + bc_vec_pop_all(&func->labels); + } else { + bc_func_init(&f); + bc_vec_push(&G.prog.fns, &f); + } + + return idx; +} + // Note: takes ownership of 'name' (must be malloced) static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags) { @@ -3741,7 +3765,7 @@ static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags) if (idx == BC_VEC_INVALID_IDX) { // No such function exists, create an empty one - bc_parse_addFunc(p, name, &idx); + bc_program_addFunc(name); idx = bc_map_find_exact(&G.prog.fn_map, &entry); } else free(name); @@ -4249,7 +4273,8 @@ static BC_STATUS zbc_parse_funcdef(BcParse *p) RETURN_STATUS(bc_error("bad function definition")); name = xstrdup(p->l.t.v.v); - bc_parse_addFunc(p, name, &p->fidx); + p->fidx = bc_program_addFunc(name); + p->func = bc_program_func(p->fidx); s = zbc_lex_next(&p->l); if (s) RETURN_STATUS(s); @@ -4741,7 +4766,7 @@ static BC_STATUS zdc_parse_register(BcParse *p) static BC_STATUS zdc_parse_string(BcParse *p) { char *str, *name; - size_t idx, len = G.prog.strs.len; + size_t len = G.prog.strs.len; //why pad to 32 zeros?? name = xasprintf("%032lu", (unsigned long)len); @@ -4750,7 +4775,8 @@ static BC_STATUS zdc_parse_string(BcParse *p) bc_parse_push(p, BC_INST_STR); bc_parse_pushIndex(p, len); bc_vec_push(&G.prog.strs, &str); - bc_parse_addFunc(p, name, &idx); + bc_program_addFunc(name); + p->func = bc_program_func(p->fidx); RETURN_STATUS(zbc_lex_next(&p->l)); } @@ -6121,7 +6147,7 @@ static BC_STATUS zbc_program_asciify(void) //str[1] = '\0'; - already is str2 = xstrdup(str); - bc_program_addFunc(str2, &idx); + idx = bc_program_addFunc(str2); if (idx != len + BC_PROG_REQ_FUNCS) { for (idx = 0; idx < G.prog.strs.len; ++idx) { @@ -6318,36 +6344,6 @@ static void bc_program_pushGlobal(char inst) bc_vec_push(&G.prog.results, &res); } -// Note: takes ownership of 'name' (must be malloced) -static void bc_program_addFunc(char *name, size_t *idx) -{ - BcId entry, *entry_ptr; - BcFunc f; - int inserted; - - entry.name = name; - entry.idx = G.prog.fns.len; - - inserted = bc_map_insert(&G.prog.fn_map, &entry, idx); - if (!inserted) free(name); - - entry_ptr = bc_vec_item(&G.prog.fn_map, *idx); - *idx = entry_ptr->idx; - - if (!inserted) { - BcFunc *func = bc_program_func(entry_ptr->idx); - - // We need to reset these, so the function can be repopulated. - func->nparams = 0; - bc_vec_pop_all(&func->autos); - bc_vec_pop_all(&func->code); - bc_vec_pop_all(&func->labels); - } else { - bc_func_init(&f); - bc_vec_push(&G.prog.fns, &f); - } -} - static BC_STATUS zbc_program_exec(void) { BcResult r, *ptr; @@ -6982,7 +6978,6 @@ static void bc_vm_free(void) static void bc_program_init(void) { - size_t idx; BcInstPtr ip; // memset(&G.prog, 0, sizeof(G.prog)); - already is @@ -7004,8 +6999,9 @@ static void bc_program_init(void) bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free); bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free); - bc_program_addFunc(xstrdup("(main)"), &idx); - bc_program_addFunc(xstrdup("(read)"), &idx); +//TODO: with "", dc_strings.dc enters infinite loop, ??! + bc_program_addFunc(xstrdup("(m)")); // func #0: main + bc_program_addFunc(xstrdup("(r)")); // func #1: for read() bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free); bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free); -- cgit v1.2.3