aboutsummaryrefslogtreecommitdiff
path: root/miscutils/bc.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2018-12-21 22:16:17 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2018-12-21 22:16:17 +0100
commit6ed7fb0a877d550428cb289feb3695dcd2812ab8 (patch)
tree1788936bb39264041ebb6456625b65be5f6981b5 /miscutils/bc.c
parent19eee8ed7cf32de37afa53ce56c62376c880a5a8 (diff)
downloadbusybox-w32-6ed7fb0a877d550428cb289feb3695dcd2812ab8.tar.gz
busybox-w32-6ed7fb0a877d550428cb289feb3695dcd2812ab8.tar.bz2
busybox-w32-6ed7fb0a877d550428cb289feb3695dcd2812ab8.zip
bc: optimize bc_vec_push() usage
function old new delta bc_parse_pushNUM 87 80 -7 zbc_parse_stmt_possibly_auto 1697 1689 -8 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-15) Total: -15 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to '')
-rw-r--r--miscutils/bc.c58
1 files changed, 26 insertions, 32 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c
index 441cb0467..abedbdf0b 100644
--- a/miscutils/bc.c
+++ b/miscutils/bc.c
@@ -1097,24 +1097,25 @@ static void bc_vec_pop_all(BcVec *v)
1097 bc_vec_npop(v, v->len); 1097 bc_vec_npop(v, v->len);
1098} 1098}
1099 1099
1100//TODO: return index of pushed data - many callers can use that 1100static size_t bc_vec_push(BcVec *v, const void *data)
1101static void bc_vec_push(BcVec *v, const void *data)
1102{ 1101{
1103 if (v->len + 1 > v->cap) bc_vec_grow(v, 1); 1102 size_t len = v->len;
1104 memmove(v->v + (v->size * v->len), data, v->size); 1103 if (len >= v->cap) bc_vec_grow(v, 1);
1105 v->len += 1; 1104 memmove(v->v + (v->size * len), data, v->size);
1105 v->len++;
1106 return len;
1106} 1107}
1107 1108
1108static void bc_vec_pushByte(BcVec *v, char data) 1109static size_t bc_vec_pushByte(BcVec *v, char data)
1109{ 1110{
1110 bc_vec_push(v, &data); 1111 return bc_vec_push(v, &data);
1111} 1112}
1112 1113
1113static void bc_vec_pushZeroByte(BcVec *v) 1114static size_t bc_vec_pushZeroByte(BcVec *v)
1114{ 1115{
1115 //bc_vec_pushByte(v, '\0'); 1116 //return bc_vec_pushByte(v, '\0');
1116 // better: 1117 // better:
1117 bc_vec_push(v, &const_int_0); 1118 return bc_vec_push(v, &const_int_0);
1118} 1119}
1119 1120
1120static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx) 1121static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
@@ -3548,14 +3549,11 @@ static void bc_parse_pushNUM(BcParse *p)
3548{ 3549{
3549 char *num = xstrdup(p->l.t.v.v); 3550 char *num = xstrdup(p->l.t.v.v);
3550#if ENABLE_BC && ENABLE_DC 3551#if ENABLE_BC && ENABLE_DC
3551 size_t idx = IS_BC ? p->func->consts.len : G.prog.consts.len; 3552 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
3552 bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
3553#elif ENABLE_BC 3553#elif ENABLE_BC
3554 size_t idx = p->func->consts.len; 3554 size_t idx = bc_vec_push(&p->func->consts, &num);
3555 bc_vec_push(&p->func->consts, &num);
3556#else // DC 3555#else // DC
3557 size_t idx = G.prog.consts.len; 3556 size_t idx = bc_vec_push(&G.prog.consts, &num);
3558 bc_vec_push(&G.prog.consts, &num);
3559#endif 3557#endif
3560 bc_parse_push(p, BC_INST_NUM); 3558 bc_parse_push(p, BC_INST_NUM);
3561 bc_parse_pushIndex(p, idx); 3559 bc_parse_pushIndex(p, idx);
@@ -3652,7 +3650,7 @@ static void bc_program_add_fn(void)
3652 //size_t idx; 3650 //size_t idx;
3653 BcFunc f; 3651 BcFunc f;
3654 bc_func_init(&f); 3652 bc_func_init(&f);
3655 //idx = G.prog.fns.len; 3653 //idx =
3656 bc_vec_push(&G.prog.fns, &f); 3654 bc_vec_push(&G.prog.fns, &f);
3657 //return idx; 3655 //return idx;
3658} 3656}
@@ -4135,12 +4133,13 @@ static BC_STATUS zbc_parse_if(BcParse *p)
4135 if (s) RETURN_STATUS(s); 4133 if (s) RETURN_STATUS(s);
4136 s = zbc_parse_expr(p, BC_PARSE_REL); 4134 s = zbc_parse_expr(p, BC_PARSE_REL);
4137 if (s) RETURN_STATUS(s); 4135 if (s) RETURN_STATUS(s);
4138
4139 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); 4136 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
4140 4137
4141 ip_idx = p->func->labels.len; 4138 // Encode "if zero, jump to ..."
4139 // Pushed value (destination of the jump) is uninitialized,
4140 // will be rewritten to be address of "end of if()" or of "else".
4141 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
4142 bc_parse_pushJUMP_ZERO(p, ip_idx); 4142 bc_parse_pushJUMP_ZERO(p, ip_idx);
4143 bc_vec_push(&p->func->labels, &ip_idx);
4144 4143
4145 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_if); 4144 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_if);
4146 if (s) RETURN_STATUS(s); 4145 if (s) RETURN_STATUS(s);
@@ -4149,15 +4148,14 @@ static BC_STATUS zbc_parse_if(BcParse *p)
4149 if (p->l.t.t == BC_LEX_KEY_ELSE) { 4148 if (p->l.t.t == BC_LEX_KEY_ELSE) {
4150 size_t ip2_idx; 4149 size_t ip2_idx;
4151 4150
4152 ip2_idx = p->func->labels.len; 4151 // Encode "after then_stmt, jump to end of if()"
4153 4152 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4154 dbg_lex("%s:%d after if() body: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx); 4153 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
4155 bc_parse_pushJUMP(p, ip2_idx); 4154 bc_parse_pushJUMP(p, ip2_idx);
4156 4155
4157 dbg_lex("%s:%d rewriting 'if_zero' label to jump to 'else'-> %zd", __func__, __LINE__, p->func->code.len); 4156 dbg_lex("%s:%d rewriting 'if_zero' label to jump to 'else'-> %zd", __func__, __LINE__, p->func->code.len);
4158 rewrite_label_to_current(p, ip_idx); 4157 rewrite_label_to_current(p, ip_idx);
4159 4158
4160 bc_vec_push(&p->func->labels, &ip2_idx);
4161 ip_idx = ip2_idx; 4159 ip_idx = ip2_idx;
4162 4160
4163 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_else); 4161 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_else);
@@ -4184,10 +4182,8 @@ static BC_STATUS zbc_parse_while(BcParse *p)
4184 s = zbc_lex_next(&p->l); 4182 s = zbc_lex_next(&p->l);
4185 if (s) RETURN_STATUS(s); 4183 if (s) RETURN_STATUS(s);
4186 4184
4187 cond_idx = p->func->labels.len; 4185 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
4188 ip_idx = cond_idx + 1; 4186 ip_idx = cond_idx + 1;
4189
4190 bc_vec_push(&p->func->labels, &p->func->code.len);
4191 bc_vec_push(&p->conds, &cond_idx); 4187 bc_vec_push(&p->conds, &cond_idx);
4192 4188
4193 bc_vec_push(&p->exits, &ip_idx); 4189 bc_vec_push(&p->exits, &ip_idx);
@@ -4240,13 +4236,11 @@ static BC_STATUS zbc_parse_for(BcParse *p)
4240 s = zbc_lex_next(&p->l); 4236 s = zbc_lex_next(&p->l);
4241 if (s) RETURN_STATUS(s); 4237 if (s) RETURN_STATUS(s);
4242 4238
4243 cond_idx = p->func->labels.len; 4239 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
4244 update_idx = cond_idx + 1; 4240 update_idx = cond_idx + 1;
4245 body_idx = update_idx + 1; 4241 body_idx = update_idx + 1;
4246 exit_idx = body_idx + 1; 4242 exit_idx = body_idx + 1;
4247 4243
4248 bc_vec_push(&p->func->labels, &p->func->code.len);
4249
4250 if (p->l.t.t != BC_LEX_SCOLON) 4244 if (p->l.t.t != BC_LEX_SCOLON)
4251 s = zbc_parse_expr(p, BC_PARSE_REL); 4245 s = zbc_parse_expr(p, BC_PARSE_REL);
4252 else { 4246 else {
@@ -4272,14 +4266,14 @@ static BC_STATUS zbc_parse_for(BcParse *p)
4272 4266
4273 if (p->l.t.t != BC_LEX_RPAREN) { 4267 if (p->l.t.t != BC_LEX_RPAREN) {
4274 s = zbc_parse_expr(p, 0); 4268 s = zbc_parse_expr(p, 0);
4275 bc_parse_push(p, BC_INST_POP);
4276 if (s) RETURN_STATUS(s); 4269 if (s) RETURN_STATUS(s);
4270 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
4271 bc_parse_push(p, BC_INST_POP);
4277 } else { 4272 } else {
4278 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update"); 4273 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
4279 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s);) 4274 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s);)
4280 } 4275 }
4281 4276
4282 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
4283 bc_parse_pushJUMP(p, cond_idx); 4277 bc_parse_pushJUMP(p, cond_idx);
4284 bc_vec_push(&p->func->labels, &p->func->code.len); 4278 bc_vec_push(&p->func->labels, &p->func->code.len);
4285 4279