bc: in bc_program_exec(), push two variables into inner scope

While at it, delete empty lines.

function                                             old     new   delta
bc_program_exec                                     4179    4152     -27

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2018-12-09 02:24:14 +01:00
parent 8fa1e8e6e7
commit 927a7d6853

View File

@ -6651,8 +6651,6 @@ static void bc_program_addFunc(char *name, size_t *idx)
static BcStatus bc_program_exec(void) static BcStatus bc_program_exec(void)
{ {
BcStatus s = BC_STATUS_SUCCESS;
size_t idx;
BcResult r, *ptr; BcResult r, *ptr;
BcNum *num; BcNum *num;
BcInstPtr *ip = bc_vec_top(&G.prog.stack); BcInstPtr *ip = bc_vec_top(&G.prog.stack);
@ -6660,58 +6658,41 @@ static BcStatus bc_program_exec(void)
char *code = func->code.v; char *code = func->code.v;
bool cond = false; bool cond = false;
while (!s && ip->idx < func->code.len) { while (ip->idx < func->code.len) {
BcStatus s;
char inst = code[(ip->idx)++]; char inst = code[(ip->idx)++];
switch (inst) { switch (inst) {
#if ENABLE_BC #if ENABLE_BC
case BC_INST_JUMP_ZERO: case BC_INST_JUMP_ZERO:
{
s = bc_program_prep(&ptr, &num); s = bc_program_prep(&ptr, &num);
if (s) return s; if (s) return s;
cond = !bc_num_cmp(num, &G.prog.zero); cond = !bc_num_cmp(num, &G.prog.zero);
bc_vec_pop(&G.prog.results); bc_vec_pop(&G.prog.results);
} // Fallthrough.
// Fallthrough. case BC_INST_JUMP: {
case BC_INST_JUMP:
{
size_t *addr; size_t *addr;
idx = bc_program_index(code, &ip->idx); size_t idx = bc_program_index(code, &ip->idx);
addr = bc_vec_item(&func->labels, idx); addr = bc_vec_item(&func->labels, idx);
if (inst == BC_INST_JUMP || cond) ip->idx = *addr; if (inst == BC_INST_JUMP || cond) ip->idx = *addr;
break; break;
} }
case BC_INST_CALL: case BC_INST_CALL:
{
s = bc_program_call(code, &ip->idx); s = bc_program_call(code, &ip->idx);
break; break;
}
case BC_INST_INC_PRE: case BC_INST_INC_PRE:
case BC_INST_DEC_PRE: case BC_INST_DEC_PRE:
case BC_INST_INC_POST: case BC_INST_INC_POST:
case BC_INST_DEC_POST: case BC_INST_DEC_POST:
{
s = bc_program_incdec(inst); s = bc_program_incdec(inst);
break; break;
}
case BC_INST_HALT: case BC_INST_HALT:
{
QUIT_OR_RETURN_TO_MAIN; QUIT_OR_RETURN_TO_MAIN;
break; break;
}
case BC_INST_RET: case BC_INST_RET:
case BC_INST_RET0: case BC_INST_RET0:
{
s = bc_program_return(inst); s = bc_program_return(inst);
break; break;
}
case BC_INST_BOOL_OR: case BC_INST_BOOL_OR:
case BC_INST_BOOL_AND: case BC_INST_BOOL_AND:
#endif // ENABLE_BC #endif // ENABLE_BC
@ -6721,121 +6702,77 @@ static BcStatus bc_program_exec(void)
case BC_INST_REL_NE: case BC_INST_REL_NE:
case BC_INST_REL_LT: case BC_INST_REL_LT:
case BC_INST_REL_GT: case BC_INST_REL_GT:
{
s = bc_program_logical(inst); s = bc_program_logical(inst);
break; break;
}
case BC_INST_READ: case BC_INST_READ:
{
s = bc_program_read(); s = bc_program_read();
break; break;
}
case BC_INST_VAR: case BC_INST_VAR:
{
s = bc_program_pushVar(code, &ip->idx, false, false); s = bc_program_pushVar(code, &ip->idx, false, false);
break; break;
}
case BC_INST_ARRAY_ELEM: case BC_INST_ARRAY_ELEM:
case BC_INST_ARRAY: case BC_INST_ARRAY:
{
s = bc_program_pushArray(code, &ip->idx, inst); s = bc_program_pushArray(code, &ip->idx, inst);
break; break;
}
case BC_INST_LAST: case BC_INST_LAST:
{
r.t = BC_RESULT_LAST; r.t = BC_RESULT_LAST;
bc_vec_push(&G.prog.results, &r); bc_vec_push(&G.prog.results, &r);
break; break;
}
case BC_INST_IBASE: case BC_INST_IBASE:
case BC_INST_SCALE: case BC_INST_SCALE:
case BC_INST_OBASE: case BC_INST_OBASE:
{
bc_program_pushGlobal(inst); bc_program_pushGlobal(inst);
break; break;
}
case BC_INST_SCALE_FUNC: case BC_INST_SCALE_FUNC:
case BC_INST_LENGTH: case BC_INST_LENGTH:
case BC_INST_SQRT: case BC_INST_SQRT:
{
s = bc_program_builtin(inst); s = bc_program_builtin(inst);
break; break;
}
case BC_INST_NUM: case BC_INST_NUM:
{
r.t = BC_RESULT_CONSTANT; r.t = BC_RESULT_CONSTANT;
r.d.id.idx = bc_program_index(code, &ip->idx); r.d.id.idx = bc_program_index(code, &ip->idx);
bc_vec_push(&G.prog.results, &r); bc_vec_push(&G.prog.results, &r);
break; break;
}
case BC_INST_POP: case BC_INST_POP:
{
if (!BC_PROG_STACK(&G.prog.results, 1)) if (!BC_PROG_STACK(&G.prog.results, 1))
s = bc_error_stack_has_too_few_elements(); s = bc_error_stack_has_too_few_elements();
else else
bc_vec_pop(&G.prog.results); bc_vec_pop(&G.prog.results);
break; break;
}
case BC_INST_POP_EXEC: case BC_INST_POP_EXEC:
{
bc_vec_pop(&G.prog.stack); bc_vec_pop(&G.prog.stack);
break; break;
}
case BC_INST_PRINT: case BC_INST_PRINT:
case BC_INST_PRINT_POP: case BC_INST_PRINT_POP:
case BC_INST_PRINT_STR: case BC_INST_PRINT_STR:
{
s = bc_program_print(inst, 0); s = bc_program_print(inst, 0);
break; break;
}
case BC_INST_STR: case BC_INST_STR:
{
r.t = BC_RESULT_STR; r.t = BC_RESULT_STR;
r.d.id.idx = bc_program_index(code, &ip->idx); r.d.id.idx = bc_program_index(code, &ip->idx);
bc_vec_push(&G.prog.results, &r); bc_vec_push(&G.prog.results, &r);
break; break;
}
case BC_INST_POWER: case BC_INST_POWER:
case BC_INST_MULTIPLY: case BC_INST_MULTIPLY:
case BC_INST_DIVIDE: case BC_INST_DIVIDE:
case BC_INST_MODULUS: case BC_INST_MODULUS:
case BC_INST_PLUS: case BC_INST_PLUS:
case BC_INST_MINUS: case BC_INST_MINUS:
{
s = bc_program_op(inst); s = bc_program_op(inst);
break; break;
}
case BC_INST_BOOL_NOT: case BC_INST_BOOL_NOT:
{
s = bc_program_prep(&ptr, &num); s = bc_program_prep(&ptr, &num);
if (s) return s; if (s) return s;
bc_num_init(&r.d.n, BC_NUM_DEF_SIZE); bc_num_init(&r.d.n, BC_NUM_DEF_SIZE);
(!bc_num_cmp(num, &G.prog.zero) ? bc_num_one : bc_num_zero)(&r.d.n); if (!bc_num_cmp(num, &G.prog.zero))
bc_num_one(&r.d.n);
else
bc_num_zero(&r.d.n);
bc_program_retire(&r, BC_RESULT_TEMP); bc_program_retire(&r, BC_RESULT_TEMP);
break; break;
}
case BC_INST_NEG: case BC_INST_NEG:
{
s = bc_program_negate(); s = bc_program_negate();
break; break;
}
#if ENABLE_BC #if ENABLE_BC
case BC_INST_ASSIGN_POWER: case BC_INST_ASSIGN_POWER:
case BC_INST_ASSIGN_MULTIPLY: case BC_INST_ASSIGN_MULTIPLY:
@ -6845,123 +6782,84 @@ static BcStatus bc_program_exec(void)
case BC_INST_ASSIGN_MINUS: case BC_INST_ASSIGN_MINUS:
#endif #endif
case BC_INST_ASSIGN: case BC_INST_ASSIGN:
{
s = bc_program_assign(inst); s = bc_program_assign(inst);
break; break;
}
#if ENABLE_DC #if ENABLE_DC
case BC_INST_MODEXP: case BC_INST_MODEXP:
{
s = bc_program_modexp(); s = bc_program_modexp();
break; break;
}
case BC_INST_DIVMOD: case BC_INST_DIVMOD:
{
s = bc_program_divmod(); s = bc_program_divmod();
break; break;
}
case BC_INST_EXECUTE: case BC_INST_EXECUTE:
case BC_INST_EXEC_COND: case BC_INST_EXEC_COND:
{
cond = inst == BC_INST_EXEC_COND; cond = inst == BC_INST_EXEC_COND;
s = bc_program_execStr(code, &ip->idx, cond); s = bc_program_execStr(code, &ip->idx, cond);
break; break;
} case BC_INST_PRINT_STACK: {
size_t idx;
case BC_INST_PRINT_STACK: for (idx = 0; idx < G.prog.results.len; ++idx) {
{
for (idx = 0; !s && idx < G.prog.results.len; ++idx)
s = bc_program_print(BC_INST_PRINT, idx); s = bc_program_print(BC_INST_PRINT, idx);
if (s) break;
}
break; break;
} }
case BC_INST_CLEAR_STACK: case BC_INST_CLEAR_STACK:
{
bc_vec_pop_all(&G.prog.results); bc_vec_pop_all(&G.prog.results);
break; break;
}
case BC_INST_STACK_LEN: case BC_INST_STACK_LEN:
{
bc_program_stackLen(); bc_program_stackLen();
break; break;
}
case BC_INST_DUPLICATE: case BC_INST_DUPLICATE:
{
if (!BC_PROG_STACK(&G.prog.results, 1)) if (!BC_PROG_STACK(&G.prog.results, 1))
return bc_error_stack_has_too_few_elements(); return bc_error_stack_has_too_few_elements();
ptr = bc_vec_top(&G.prog.results); ptr = bc_vec_top(&G.prog.results);
bc_result_copy(&r, ptr); bc_result_copy(&r, ptr);
bc_vec_push(&G.prog.results, &r); bc_vec_push(&G.prog.results, &r);
break; break;
} case BC_INST_SWAP: {
case BC_INST_SWAP:
{
BcResult *ptr2; BcResult *ptr2;
if (!BC_PROG_STACK(&G.prog.results, 2)) if (!BC_PROG_STACK(&G.prog.results, 2))
return bc_error_stack_has_too_few_elements(); return bc_error_stack_has_too_few_elements();
ptr = bc_vec_item_rev(&G.prog.results, 0); ptr = bc_vec_item_rev(&G.prog.results, 0);
ptr2 = bc_vec_item_rev(&G.prog.results, 1); ptr2 = bc_vec_item_rev(&G.prog.results, 1);
memcpy(&r, ptr, sizeof(BcResult)); memcpy(&r, ptr, sizeof(BcResult));
memcpy(ptr, ptr2, sizeof(BcResult)); memcpy(ptr, ptr2, sizeof(BcResult));
memcpy(ptr2, &r, sizeof(BcResult)); memcpy(ptr2, &r, sizeof(BcResult));
break; break;
} }
case BC_INST_ASCIIFY: case BC_INST_ASCIIFY:
{
s = bc_program_asciify(); s = bc_program_asciify();
break; break;
}
case BC_INST_PRINT_STREAM: case BC_INST_PRINT_STREAM:
{
s = bc_program_printStream(); s = bc_program_printStream();
break; break;
}
case BC_INST_LOAD: case BC_INST_LOAD:
case BC_INST_PUSH_VAR: case BC_INST_PUSH_VAR: {
{
bool copy = inst == BC_INST_LOAD; bool copy = inst == BC_INST_LOAD;
s = bc_program_pushVar(code, &ip->idx, true, copy); s = bc_program_pushVar(code, &ip->idx, true, copy);
break; break;
} }
case BC_INST_PUSH_TO_VAR: {
case BC_INST_PUSH_TO_VAR:
{
char *name = bc_program_name(code, &ip->idx); char *name = bc_program_name(code, &ip->idx);
s = bc_program_copyToVar(name, true); s = bc_program_copyToVar(name, true);
free(name); free(name);
break; break;
} }
case BC_INST_QUIT: case BC_INST_QUIT:
{
if (G.prog.stack.len <= 2) if (G.prog.stack.len <= 2)
QUIT_OR_RETURN_TO_MAIN; QUIT_OR_RETURN_TO_MAIN;
bc_vec_npop(&G.prog.stack, 2); bc_vec_npop(&G.prog.stack, 2);
break; break;
}
case BC_INST_NQUIT: case BC_INST_NQUIT:
{
s = bc_program_nquit(); s = bc_program_nquit();
break; break;
}
#endif // ENABLE_DC #endif // ENABLE_DC
} }
if (s || G_interrupt) { if (s || G_interrupt) {
bc_program_reset(); bc_program_reset();
break; return s;
} }
// If the stack has changed, pointers may be invalid. // If the stack has changed, pointers may be invalid.
@ -6970,7 +6868,7 @@ static BcStatus bc_program_exec(void)
code = func->code.v; code = func->code.v;
} }
return s; return BC_STATUS_SUCCESS;
} }
#if ENABLE_BC #if ENABLE_BC