From 1dc4de9d9bbdf3f60b76cecf94d756ef9394e25f Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 21 Dec 2018 23:13:48 +0100 Subject: bc: code shrink function old new delta bc_result_pop_and_push - 73 +73 zbc_program_exec 4068 4064 -4 bc_program_binOpRetire 46 32 -14 zdc_program_assignStr 146 126 -20 zdc_program_asciify 395 370 -25 bc_program_retire 35 7 -28 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 0/5 up/down: 73/-91) Total: -18 bytes Signed-off-by: Denys Vlasenko --- miscutils/bc.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'miscutils/bc.c') diff --git a/miscutils/bc.c b/miscutils/bc.c index 2ba530d43..cac3cb734 100644 --- a/miscutils/bc.c +++ b/miscutils/bc.c @@ -1106,6 +1106,21 @@ static size_t bc_vec_push(BcVec *v, const void *data) return len; } +// G.prog.results often needs "pop old operand, push result" idiom. +// Can do this without a few extra ops +static size_t bc_result_pop_and_push(const void *data) +{ + BcVec *v = &G.prog.results; + char *last; + size_t len = v->len - 1; + + last = v->v + (v->size * len); + if (v->dtor) + v->dtor(last); + memmove(last, data, v->size); + return len; +} + static size_t bc_vec_pushByte(BcVec *v, char data) { return bc_vec_push(v, &data); @@ -5165,8 +5180,7 @@ static void bc_program_binOpRetire(BcResult *r) { r->t = BC_RESULT_TEMP; bc_vec_pop(&G.prog.results); - bc_vec_pop(&G.prog.results); - bc_vec_push(&G.prog.results, r); + bc_result_pop_and_push(r); } static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n) @@ -5190,8 +5204,7 @@ static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n) static void bc_program_retire(BcResult *r, BcResultType t) { r->t = t; - bc_vec_pop(&G.prog.results); - bc_vec_push(&G.prog.results, r); + bc_result_pop_and_push(r); } static BC_STATUS zbc_program_op(char inst) @@ -5684,9 +5697,7 @@ static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push) bc_vec_pop(&G.prog.results); } - bc_vec_pop(&G.prog.results); - - bc_vec_push(&G.prog.results, &res); + bc_result_pop_and_push(&res); bc_vec_push(v, &n2); RETURN_STATUS(BC_STATUS_SUCCESS); @@ -5928,8 +5939,7 @@ static BC_STATUS zbc_program_incdec(char inst) if (s) RETURN_STATUS(s); if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) { - bc_vec_pop(&G.prog.results); - bc_vec_push(&G.prog.results, ©); + bc_result_pop_and_push(©); } RETURN_STATUS(s); @@ -6244,8 +6254,7 @@ static BC_STATUS zdc_program_asciify(void) dup: res.t = BC_RESULT_STR; res.d.id.idx = idx; - bc_vec_pop(&G.prog.results); - bc_vec_push(&G.prog.results, &res); + bc_result_pop_and_push(&res); RETURN_STATUS(BC_STATUS_SUCCESS); num_err: -- cgit v1.2.3