refactored compile_if_expression() a bit

This commit is contained in:
hyunghwan.chung 2019-08-06 15:04:33 +00:00
parent 569ee2424d
commit b2ac09072b

View File

@ -6299,7 +6299,7 @@ static int compile_if_expression (moo_t* moo)
do do
{ {
int falseblock = 0; enum { COND_NORMAL, COND_TRUE, COND_FALSE } cond_type = COND_NORMAL; /* normal condition */
GET_TOKEN (moo); /* get ( */ GET_TOKEN (moo); /* get ( */
precondpos = cc->mth.code.len; precondpos = cc->mth.code.len;
@ -6313,23 +6313,14 @@ static int compile_if_expression (moo_t* moo)
if (precondpos + 1 == postcondpos && cc->mth.code.ptr[precondpos] == push_true_inst) if (precondpos + 1 == postcondpos && cc->mth.code.ptr[precondpos] == push_true_inst)
{ {
/* got 'if (true)' or 'ifnot (false)' */ /* got 'if (true)' or 'ifnot (false)' */
/* do not generate jump */
jumptonext = INVALID_IP; /* indicate that the jump has not been emitted */ jumptonext = INVALID_IP; /* indicate that the jump has not been emitted */
falseblock = 0; cond_type = COND_TRUE;
/* eliminate PUSH_TRUE as well */
eliminate_instructions (moo, precondpos, cc->mth.code.len - 1);
postcondpos = precondpos;
} }
else if (precondpos + 1 == postcondpos && cc->mth.code.ptr[precondpos] == push_false_inst) else if (precondpos + 1 == postcondpos && cc->mth.code.ptr[precondpos] == push_false_inst)
{ {
/* got 'if (false)' or 'ifnot (true)' */ /* got 'if (false)' or 'ifnot (true)' */
jumptonext = INVALID_IP; /* indicate that the jump has not been emitted */ jumptonext = INVALID_IP; /* indicate that the jump has not been emitted */
cond_type = COND_FALSE; /* mark that the conditional is false. instructions will get eliminated below */
/* mark that the conditional is false. instructions will get eliminated below */
falseblock = 1;
} }
else else
{ {
@ -6344,23 +6335,33 @@ static int compile_if_expression (moo_t* moo)
brace_loc = *TOKEN_LOC(moo); brace_loc = *TOKEN_LOC(moo);
if (compile_braced_block(moo) <= -1) goto oops; if (compile_braced_block(moo) <= -1) goto oops;
if (jumptonext == INVALID_IP) switch (cond_type)
{ {
if (falseblock) case COND_TRUE:
{ MOO_ASSERT (moo, jumptonext == INVALID_IP);
/* the conditional was false. eliminate instructions emitted
* for the block attached to the conditional */ /* eliminate PUSH_TRUE */
eliminate_instructions (moo, precondpos, cc->mth.code.len - 1); eliminate_instructions (moo, precondpos, precondpos);
postcondpos = precondpos; postcondpos = precondpos;
}
else if (endoftrueblock == INVALID_IP) if (endoftrueblock == INVALID_IP)
{ {
/* update the end position of the first true block */ /* update the end position of the first true block */
endoftrueblock = cc->mth.code.len; endoftrueblock = cc->mth.code.len;
} }
} break;
else
{ case COND_FALSE:
MOO_ASSERT (moo, jumptonext == INVALID_IP);
/* the conditional was false. eliminate instructions emitted
* for the block attached to the conditional */
eliminate_instructions (moo, precondpos, cc->mth.code.len - 1);
postcondpos = precondpos;
break;
case COND_NORMAL:
MOO_ASSERT (moo, jumptonext != INVALID_IP);
if (endoftrueblock == INVALID_IP) if (endoftrueblock == INVALID_IP)
{ {
/* emit an instruction to jump to the end */ /* emit an instruction to jump to the end */