aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--miscutils/bc.c31
1 files changed, 20 insertions, 11 deletions
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)
1106 return len; 1106 return len;
1107} 1107}
1108 1108
1109// G.prog.results often needs "pop old operand, push result" idiom.
1110// Can do this without a few extra ops
1111static size_t bc_result_pop_and_push(const void *data)
1112{
1113 BcVec *v = &G.prog.results;
1114 char *last;
1115 size_t len = v->len - 1;
1116
1117 last = v->v + (v->size * len);
1118 if (v->dtor)
1119 v->dtor(last);
1120 memmove(last, data, v->size);
1121 return len;
1122}
1123
1109static size_t bc_vec_pushByte(BcVec *v, char data) 1124static size_t bc_vec_pushByte(BcVec *v, char data)
1110{ 1125{
1111 return bc_vec_push(v, &data); 1126 return bc_vec_push(v, &data);
@@ -5165,8 +5180,7 @@ static void bc_program_binOpRetire(BcResult *r)
5165{ 5180{
5166 r->t = BC_RESULT_TEMP; 5181 r->t = BC_RESULT_TEMP;
5167 bc_vec_pop(&G.prog.results); 5182 bc_vec_pop(&G.prog.results);
5168 bc_vec_pop(&G.prog.results); 5183 bc_result_pop_and_push(r);
5169 bc_vec_push(&G.prog.results, r);
5170} 5184}
5171 5185
5172static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n) 5186static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
@@ -5190,8 +5204,7 @@ static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
5190static void bc_program_retire(BcResult *r, BcResultType t) 5204static void bc_program_retire(BcResult *r, BcResultType t)
5191{ 5205{
5192 r->t = t; 5206 r->t = t;
5193 bc_vec_pop(&G.prog.results); 5207 bc_result_pop_and_push(r);
5194 bc_vec_push(&G.prog.results, r);
5195} 5208}
5196 5209
5197static BC_STATUS zbc_program_op(char inst) 5210static BC_STATUS zbc_program_op(char inst)
@@ -5684,9 +5697,7 @@ static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
5684 bc_vec_pop(&G.prog.results); 5697 bc_vec_pop(&G.prog.results);
5685 } 5698 }
5686 5699
5687 bc_vec_pop(&G.prog.results); 5700 bc_result_pop_and_push(&res);
5688
5689 bc_vec_push(&G.prog.results, &res);
5690 bc_vec_push(v, &n2); 5701 bc_vec_push(v, &n2);
5691 5702
5692 RETURN_STATUS(BC_STATUS_SUCCESS); 5703 RETURN_STATUS(BC_STATUS_SUCCESS);
@@ -5928,8 +5939,7 @@ static BC_STATUS zbc_program_incdec(char inst)
5928 if (s) RETURN_STATUS(s); 5939 if (s) RETURN_STATUS(s);
5929 5940
5930 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) { 5941 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
5931 bc_vec_pop(&G.prog.results); 5942 bc_result_pop_and_push(&copy);
5932 bc_vec_push(&G.prog.results, &copy);
5933 } 5943 }
5934 5944
5935 RETURN_STATUS(s); 5945 RETURN_STATUS(s);
@@ -6244,8 +6254,7 @@ static BC_STATUS zdc_program_asciify(void)
6244 dup: 6254 dup:
6245 res.t = BC_RESULT_STR; 6255 res.t = BC_RESULT_STR;
6246 res.d.id.idx = idx; 6256 res.d.id.idx = idx;
6247 bc_vec_pop(&G.prog.results); 6257 bc_result_pop_and_push(&res);
6248 bc_vec_push(&G.prog.results, &res);
6249 6258
6250 RETURN_STATUS(BC_STATUS_SUCCESS); 6259 RETURN_STATUS(BC_STATUS_SUCCESS);
6251 num_err: 6260 num_err: