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 <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2018-12-21 23:13:48 +01:00
parent 24e4194635
commit 1dc4de9d9b

View File

@ -1106,6 +1106,21 @@ static size_t bc_vec_push(BcVec *v, const void *data)
return len; 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) static size_t bc_vec_pushByte(BcVec *v, char data)
{ {
return bc_vec_push(v, &data); return bc_vec_push(v, &data);
@ -5165,8 +5180,7 @@ static void bc_program_binOpRetire(BcResult *r)
{ {
r->t = BC_RESULT_TEMP; r->t = BC_RESULT_TEMP;
bc_vec_pop(&G.prog.results); bc_vec_pop(&G.prog.results);
bc_vec_pop(&G.prog.results); bc_result_pop_and_push(r);
bc_vec_push(&G.prog.results, r);
} }
static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n) 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) static void bc_program_retire(BcResult *r, BcResultType t)
{ {
r->t = t; r->t = t;
bc_vec_pop(&G.prog.results); bc_result_pop_and_push(r);
bc_vec_push(&G.prog.results, r);
} }
static BC_STATUS zbc_program_op(char inst) 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_pop(&G.prog.results); bc_result_pop_and_push(&res);
bc_vec_push(&G.prog.results, &res);
bc_vec_push(v, &n2); bc_vec_push(v, &n2);
RETURN_STATUS(BC_STATUS_SUCCESS); RETURN_STATUS(BC_STATUS_SUCCESS);
@ -5928,8 +5939,7 @@ static BC_STATUS zbc_program_incdec(char inst)
if (s) RETURN_STATUS(s); if (s) RETURN_STATUS(s);
if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) { if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
bc_vec_pop(&G.prog.results); bc_result_pop_and_push(&copy);
bc_vec_push(&G.prog.results, &copy);
} }
RETURN_STATUS(s); RETURN_STATUS(s);
@ -6244,8 +6254,7 @@ static BC_STATUS zdc_program_asciify(void)
dup: dup:
res.t = BC_RESULT_STR; res.t = BC_RESULT_STR;
res.d.id.idx = idx; res.d.id.idx = idx;
bc_vec_pop(&G.prog.results); bc_result_pop_and_push(&res);
bc_vec_push(&G.prog.results, &res);
RETURN_STATUS(BC_STATUS_SUCCESS); RETURN_STATUS(BC_STATUS_SUCCESS);
num_err: num_err: