renumbered some jump byte codes for easy conversion between forward jump and backward jump

This commit is contained in:
hyunghwan.chung 2019-08-02 15:59:19 +00:00
parent c30f7466a5
commit 763d7f965e
7 changed files with 136 additions and 155 deletions

View File

@ -773,7 +773,6 @@ static moo_oop_t string_to_fpdec (moo_t* moo, moo_oocs_t* str, int prescaled)
if (scale < xscale)
{
/* need to add more zeros */
moo_ooch_t* tmp;
moo_oow_t explen;
explen = len + xscale - scale;
@ -2719,30 +2718,12 @@ static int patch_forward_jump_instruction (moo_t* moo, moo_oow_t jip, moo_oow_t
if (jt < jip)
{
/* backward jump */
switch (cc->mth.code.ptr[jip])
{
case BCODE_JUMP_FORWARD:
cc->mth.code.ptr[jip] = BCODE_JUMP_BACKWARD;
break;
case BCODE_JUMP_FORWARD_IF_FALSE:
cc->mth.code.ptr[jip] = BCODE_JUMP_BACKWARD_IF_FALSE;
break;
case BCODE_JUMP_FORWARD_IF_TRUE:
cc->mth.code.ptr[jip] == BCODE_JUMP_BACKWARD_IF_TRUE;
break;
case BCODE_JMPOP_FORWARD_IF_FALSE:
cc->mth.code.ptr[jip] = BCODE_JMPOP_BACKWARD_IF_FALSE;
break;
case BCODE_JMPOP_FORWARD_IF_TRUE:
cc->mth.code.ptr[jip] = BCODE_JMPOP_BACKWARD_IF_TRUE;
break;
}
MOO_STATIC_ASSERT (BCODE_JUMP_FORWARD + 10 == BCODE_JUMP_BACKWARD);
MOO_STATIC_ASSERT (BCODE_JUMP_FORWARD_IF_TRUE + 10 == BCODE_JUMP_BACKWARD_IF_TRUE);
MOO_STATIC_ASSERT (BCODE_JUMP_FORWARD_IF_FALSE + 10 == BCODE_JUMP_BACKWARD_IF_FALSE);
MOO_STATIC_ASSERT (BCODE_JMPOP_FORWARD_IF_TRUE + 10 == BCODE_JMPOP_BACKWARD_IF_TRUE);
MOO_STATIC_ASSERT (BCODE_JMPOP_FORWARD_IF_FALSE + 10 == BCODE_JMPOP_BACKWARD_IF_FALSE);
cc->mth.code.ptr[jip] += 10; /* switch to the backward jump instruction */
code_size = jip - jt + (MOO_BCODE_LONG_PARAM_SIZE + 1);
}
else
@ -2876,47 +2857,30 @@ static MOO_INLINE void adjust_all_loop_jumps_for_elimination (moo_t* moo, moo_oo
}
}
static MOO_INLINE void adjust_gotos_for_elimination (moo_t* moo, moo_oow_t start, moo_oow_t end)
static MOO_INLINE void adjust_all_gotos_for_elimination (moo_t* moo, moo_oow_t start, moo_oow_t end)
{
moo_cunit_class_t* cc = (moo_cunit_class_t*)moo->c->cunit;
moo_goto_t* _goto, *prev_goto, * next_goto;
moo_goto_t* _goto;
_goto = cc->mth._goto;
prev_goto = MOO_NULL;
while (_goto)
{
if (_goto->ip >= start && _goto->ip <= end)
{
next_goto = _goto->next;
moo_freemem (moo, _goto);
if (prev_goto)
{
prev_goto->next = next_goto;
}
else
{
cc->mth._goto = next_goto;
}
_goto = next_goto;
/* invalidate this entry since the goto instruction itself is getting eliminated.
* i don't kill this node. the resolver must skip this node. */
_goto->ip = INVALID_IP;
}
else
else if (_goto->ip > end)
{
if (_goto->ip > end)
{
_goto->ip -= end - start + 1;
}
prev_goto = _goto;
_goto = _goto->next;
_goto->ip -= end - start + 1;
}
_goto = _goto->next;
}
}
static MOO_INLINE void adjust_labels_for_elimination (moo_t* moo, moo_oow_t start, moo_oow_t end)
static MOO_INLINE void adjust_all_labels_for_elimination (moo_t* moo, moo_oow_t start, moo_oow_t end)
{
moo_cunit_class_t* cc = (moo_cunit_class_t*)moo->c->cunit;
moo_label_t* _label;
@ -2992,8 +2956,8 @@ static void eliminate_instructions (moo_t* moo, moo_oow_t start, moo_oow_t end)
/* eliminate all instructions starting from the start index.
* setting the length to the start length will achieve this */
adjust_all_loop_jumps_for_elimination (moo, start, last);
adjust_gotos_for_elimination (moo, start, last);
adjust_labels_for_elimination (moo, start, last);
adjust_all_gotos_for_elimination (moo, start, last);
adjust_all_labels_for_elimination (moo, start, last);
cc->mth.code.len = start;
}
else
@ -3003,8 +2967,8 @@ static void eliminate_instructions (moo_t* moo, moo_oow_t start, moo_oow_t end)
/* eliminate a chunk in the middle of the instruction buffer.
* some copying is required */
adjust_all_loop_jumps_for_elimination (moo, start, end);
adjust_gotos_for_elimination (moo, start, end);
adjust_labels_for_elimination (moo, start, end);
adjust_all_gotos_for_elimination (moo, start, end);
adjust_all_labels_for_elimination (moo, start, end);
tail_len = cc->mth.code.len - end - 1;
MOO_MEMMOVE (&cc->mth.code.ptr[start], &cc->mth.code.ptr[end + 1], tail_len * MOO_SIZEOF(cc->mth.code.ptr[0]));
@ -4175,7 +4139,6 @@ static int compile_method_name (moo_t* moo, moo_method_data_t* mth)
* unary-selector := identifier
*/
int n;
moo_oocs_t dummy;
MOO_ASSERT (moo, mth->tmpr_count == 0);
@ -6721,6 +6684,7 @@ static MOO_INLINE int resolve_goto_label (moo_t* moo, moo_goto_t* _goto)
return -1;
}
MOO_ASSERT (moo, _goto->ip != INVALID_IP);
MOO_ASSERT (moo, _goto->ip != _label->ip);
if (patch_forward_jump_instruction(moo, _goto->ip, _label->ip, &_goto->loc) <= -1) return -1;
return 0;
@ -6741,7 +6705,7 @@ static MOO_INLINE int resolve_goto_labels (moo_t* moo)
_goto = cc->mth._goto;
while (_goto)
{
if (resolve_goto_label(moo, _goto) <= -1) return -1;
if (_goto->ip != INVALID_IP && resolve_goto_label(moo, _goto) <= -1) return -1;
_goto = _goto->next;
}
return 0;

View File

@ -271,7 +271,27 @@ int moo_decode (moo_t* moo, moo_oop_method_t mth, const moo_oocs_t* classfqn)
break;
/* -- */
case BCODE_JMPOP_FORWARD_IF_TRUE:
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST_1 (moo, "jmpop_forward_if_true %zu", b1);
break;
case BCODE_JMPOP2_FORWARD_IF_TRUE:
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST_1 (moo, "jmpop2_forward_if_true %zu", b1);
break;
case BCODE_JMPOP_FORWARD_IF_FALSE:
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST_1 (moo, "jmpop_forward_if_false %zu", b1);
break;
case BCODE_JMPOP2_FORWARD_IF_FALSE:
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST_1 (moo, "jmpop2_forward_if_false %zu", b1);
break;
/* -- */
case BCODE_JUMP_BACKWARD:
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST_1 (moo, "jump_backward %zu", b1);
@ -303,28 +323,6 @@ int moo_decode (moo_t* moo, moo_oop_method_t mth, const moo_oocs_t* classfqn)
break;
/* -- */
case BCODE_JMPOP_FORWARD_IF_TRUE:
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST_1 (moo, "jmpop_forward_if_true %zu", b1);
break;
case BCODE_JMPOP2_FORWARD_IF_TRUE:
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST_1 (moo, "jmpop2_forward_if_true %zu", b1);
break;
case BCODE_JMPOP_FORWARD_IF_FALSE:
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST_1 (moo, "jmpop_forward_if_false %zu", b1);
break;
case BCODE_JMPOP2_FORWARD_IF_FALSE:
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST_1 (moo, "jmpop2_forward_if_false %zu", b1);
break;
/* -- */
case BCODE_JMPOP_BACKWARD_IF_TRUE:
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST_1 (moo, "jmpop_backward_if_true %zu", b1);

View File

@ -5523,45 +5523,6 @@ static int __execute (moo_t* moo)
NEXT_INST();
/* -- */
ON_INST(BCODE_JUMP_BACKWARD) /* 0xCA */
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST1 (moo, "jump_backward %zu", b1);
moo->ip -= b1;
NEXT_INST();
ON_INST(BCODE_JUMP2_BACKWARD) /* 0xCB */
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST1 (moo, "jump2_backward %zu", b1);
moo->ip -= MAX_CODE_JUMP + b1;
NEXT_INST();
ON_INST(BCODE_JUMP_BACKWARD_IF_TRUE) /* 0xCC */
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST1 (moo, "jump_backward_if_true %zu", b1);
if (MOO_STACK_GETTOP(moo) != moo->_false) moo->ip -= b1;
NEXT_INST();
ON_INST(BCODE_JUMP2_BACKWARD_IF_TRUE) /* 0xCD */
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST1 (moo, "jump2_backward_if_true %zu", b1);
if (MOO_STACK_GETTOP(moo) != moo->_false) moo->ip -= MAX_CODE_JUMP + b1;
NEXT_INST();
ON_INST(BCODE_JUMP_BACKWARD_IF_FALSE) /* 0xCE */
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST1 (moo, "jump_backward_if_false %zu", b1);
if (MOO_STACK_GETTOP(moo) == moo->_false) moo->ip -= b1;
NEXT_INST();
ON_INST(BCODE_JUMP2_BACKWARD_IF_FALSE) /* 0xCF */
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST1 (moo, "jump2_backward_if_false %zu", b1);
if (MOO_STACK_GETTOP(moo) == moo->_false) moo->ip -= MAX_CODE_JUMP + b1;
NEXT_INST();
/* -- */
ON_INST(BCODE_JMPOP_FORWARD_IF_TRUE)
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST1 (moo, "jmpop_forward_if_true %zu", b1);
@ -5590,6 +5551,43 @@ static int __execute (moo_t* moo)
MOO_STACK_POP (moo);
NEXT_INST();
/* -- */
ON_INST(BCODE_JUMP_BACKWARD) /* 0xCE */
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST1 (moo, "jump_backward %zu", b1);
moo->ip -= b1;
NEXT_INST();
ON_INST(BCODE_JUMP2_BACKWARD) /* 0xCF */
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST1 (moo, "jump2_backward %zu", b1);
moo->ip -= MAX_CODE_JUMP + b1;
NEXT_INST();
ON_INST(BCODE_JUMP_BACKWARD_IF_TRUE) /* 0xD0 */
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST1 (moo, "jump_backward_if_true %zu", b1);
if (MOO_STACK_GETTOP(moo) != moo->_false) moo->ip -= b1;
NEXT_INST();
ON_INST(BCODE_JUMP2_BACKWARD_IF_TRUE) /* 0xD1 */
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST1 (moo, "jump2_backward_if_true %zu", b1);
if (MOO_STACK_GETTOP(moo) != moo->_false) moo->ip -= MAX_CODE_JUMP + b1;
NEXT_INST();
ON_INST(BCODE_JUMP_BACKWARD_IF_FALSE) /* 0xD2 */
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST1 (moo, "jump_backward_if_false %zu", b1);
if (MOO_STACK_GETTOP(moo) == moo->_false) moo->ip -= b1;
NEXT_INST();
ON_INST(BCODE_JUMP2_BACKWARD_IF_FALSE) /* 0xD3 */
FETCH_PARAM_CODE_TO (moo, b1);
LOG_INST1 (moo, "jump2_backward_if_false %zu", b1);
if (MOO_STACK_GETTOP(moo) == moo->_false) moo->ip -= MAX_CODE_JUMP + b1;
NEXT_INST();
/* -- */
ON_INST(BCODE_JMPOP2_BACKWARD_IF_TRUE)
FETCH_PARAM_CODE_TO (moo, b1);

View File

@ -202,16 +202,16 @@
/* 199 */ &&case_BCODE_JUMP2_FORWARD_IF_TRUE,
/* 200 */ &&case_BCODE_JUMP_FORWARD_IF_FALSE,
/* 201 */ &&case_BCODE_JUMP2_FORWARD_IF_FALSE,
/* 202 */ &&case_BCODE_JUMP_BACKWARD,
/* 203 */ &&case_BCODE_JUMP2_BACKWARD,
/* 204 */ &&case_BCODE_JUMP_BACKWARD_IF_TRUE,
/* 205 */ &&case_BCODE_JUMP2_BACKWARD_IF_TRUE,
/* 206 */ &&case_BCODE_JUMP_BACKWARD_IF_FALSE,
/* 207 */ &&case_BCODE_JUMP2_BACKWARD_IF_FALSE,
/* 208 */ &&case_BCODE_JMPOP_FORWARD_IF_TRUE,
/* 209 */ &&case_BCODE_JMPOP2_FORWARD_IF_TRUE,
/* 210 */ &&case_BCODE_JMPOP_FORWARD_IF_FALSE,
/* 211 */ &&case_BCODE_JMPOP2_FORWARD_IF_FALSE,
/* 202 */ &&case_BCODE_JMPOP_FORWARD_IF_TRUE,
/* 203 */ &&case_BCODE_JMPOP2_FORWARD_IF_TRUE,
/* 204 */ &&case_BCODE_JMPOP_FORWARD_IF_FALSE,
/* 205 */ &&case_BCODE_JMPOP2_FORWARD_IF_FALSE,
/* 206 */ &&case_BCODE_JUMP_BACKWARD,
/* 207 */ &&case_BCODE_JUMP2_BACKWARD,
/* 208 */ &&case_BCODE_JUMP_BACKWARD_IF_TRUE,
/* 209 */ &&case_BCODE_JUMP2_BACKWARD_IF_TRUE,
/* 210 */ &&case_BCODE_JUMP_BACKWARD_IF_FALSE,
/* 211 */ &&case_BCODE_JUMP2_BACKWARD_IF_FALSE,
/* 212 */ &&case_BCODE_JMPOP_BACKWARD_IF_TRUE,
/* 213 */ &&case_BCODE_JMPOP2_BACKWARD_IF_TRUE,
/* 214 */ &&case_BCODE_JMPOP_BACKWARD_IF_FALSE,

View File

@ -815,6 +815,12 @@ typedef struct moo_t moo_t;
# undef MOO_HAVE_INLINE
#endif
#if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4))
# define MOO_UNUSED __attribute__((__unused__))
#else
# define MOO_UNUSED
#endif
/**
* The MOO_TYPE_IS_SIGNED() macro determines if a type is signed.
* \code
@ -1026,4 +1032,19 @@ typedef struct moo_t moo_t;
#endif
/* =========================================================================
* STATIC ASSERTION
* =========================================================================*/
#define MOO_STATIC_JOIN_INNER(x, y) x ## y
#define MOO_STATIC_JOIN(x, y) MOO_STATIC_JOIN_INNER(x, y)
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
# define MOO_STATIC_ASSERT(expr) _Static_assert (expr, "invalid assertion")
#elif defined(__cplusplus) && (__cplusplus >= 201103L)
# define MOO_STATIC_ASSERT(expr) static_assert (expr, "invalid assertion")
#else
# define MOO_STATIC_ASSERT(expr) typedef char MOO_STATIC_JOIN(MOO_STATIC_ASSERT_T_, __LINE__)[(expr)? 1: -1] MOO_UNUSED
#endif
#define MOO_STATIC_ASSERT_EXPR(expr) ((void)MOO_SIZEOF(char[(expr)? 1: -1]))
#endif

View File

@ -744,16 +744,17 @@ SHORT INSTRUCTION CODE LONG INSTRUCTION C
199 1100 0111 XXXXXXXX JUMP2_FORWARD_IF_TRUE
200 1100 1000 XXXXXXXX JUMP_FORWARD_IF_FALSE
201 1100 1001 XXXXXXXX JUMP2_FORWARD_IF_FALSE
202 1100 1010 XXXXXXXX JUMP_BACKWARD
203 1100 1011 XXXXXXXX JUMP2_BACKWARD
204 1100 1100 XXXXXXXX JUMP_BACKWARD_IF_TRUE
205 1100 1101 XXXXXXXX JUMP2_BACKWARD_IF_TRUE
206 1100 1110 XXXXXXXX JUMP_BACKWARD_IF_FALSE
207 1100 1111 XXXXXXXX JUMP2_BACKWARD_IF_FALSE
208 1101 0000 XXXXXXXX JMPOP_FORWARD_IF_TRUE
209 1101 0001 XXXXXXXX JMPOP2_FORWARD_IF_TRUE
210 1101 0010 XXXXXXXX JMPOP_FORWARD_IF_FALSE
211 1101 0011 XXXXXXXX JMPOP2_FORWARD_IF_FALSE
202 1100 1010 XXXXXXXX JMPOP_FORWARD_IF_TRUE
203 1100 1011 XXXXXXXX JMPOP2_FORWARD_IF_TRUE
204 1100 1100 XXXXXXXX JMPOP_FORWARD_IF_FALSE
205 1100 1101 XXXXXXXX JMPOP2_FORWARD_IF_FALSE
206 1100 1110 XXXXXXXX JUMP_BACKWARD
207 1100 1111 XXXXXXXX JUMP2_BACKWARD
208 1101 0000 XXXXXXXX JUMP_BACKWARD_IF_TRUE
209 1101 0001 XXXXXXXX JUMP2_BACKWARD_IF_TRUE
210 1101 0010 XXXXXXXX JUMP_BACKWARD_IF_FALSE
211 1101 0011 XXXXXXXX JUMP2_BACKWARD_IF_FALSE
212 1101 0100 XXXXXXXX JMPOP_BACKWARD_IF_TRUE
213 1101 0101 XXXXXXXX JMPOP2_BACKWARD_IF_TRUE
214 1101 0110 XXXXXXXX JMPOP_BACKWARD_IF_FALSE
@ -950,26 +951,26 @@ enum moo_bcode_t
BCODE_POP_INTO_OBJECT_X = 0xBC, /* 188 ## */
BCODE_PUSH_OBJECT_X = 0xC0, /* 192 ## */
/* UNUSED - 0xC0 - 0xC3 */
BCODE_JUMP_FORWARD = 0xC4, /* 196 ## */
BCODE_JUMP2_FORWARD = 0xC5, /* 197 ## */
BCODE_JUMP_FORWARD_IF_TRUE = 0xC6, /* 198 ## */
BCODE_JUMP2_FORWARD_IF_TRUE = 0xC7, /* 199 ## */
BCODE_JUMP_FORWARD_IF_FALSE = 0xC8, /* 200 ## */
BCODE_JUMP2_FORWARD_IF_FALSE = 0xC9, /* 201 ## */
BCODE_JUMP_BACKWARD = 0xCA, /* 202 ## */
BCODE_JUMP2_BACKWARD = 0xCB, /* 203 ## */
BCODE_JUMP_BACKWARD_IF_TRUE = 0xCC, /* 204 ## */
BCODE_JUMP2_BACKWARD_IF_TRUE = 0xCD, /* 205 ## */
BCODE_JUMP_BACKWARD_IF_FALSE = 0xCE, /* 206 ## */
BCODE_JUMP2_BACKWARD_IF_FALSE = 0xCF, /* 207 ## */
/* JMPOP = JUMP + POP */
BCODE_JMPOP_FORWARD_IF_TRUE = 0xD0, /* 208 ## */
BCODE_JMPOP2_FORWARD_IF_TRUE = 0xD1, /* 209 ## */
BCODE_JMPOP_FORWARD_IF_FALSE = 0xD2, /* 210 ## */
BCODE_JMPOP2_FORWARD_IF_FALSE = 0xD3, /* 211 ## */
BCODE_JMPOP_FORWARD_IF_TRUE = 0xCA, /* 202 ## */
BCODE_JMPOP2_FORWARD_IF_TRUE = 0xCB, /* 203 ## */
BCODE_JMPOP_FORWARD_IF_FALSE = 0xCC, /* 204 ## */
BCODE_JMPOP2_FORWARD_IF_FALSE = 0xCD, /* 205 ## */
BCODE_JUMP_BACKWARD = 0xCE, /* 206 ## */
BCODE_JUMP2_BACKWARD = 0xCF, /* 207 ## */
BCODE_JUMP_BACKWARD_IF_TRUE = 0xD0, /* 208 ## */
BCODE_JUMP2_BACKWARD_IF_TRUE = 0xD1, /* 209 ## */
BCODE_JUMP_BACKWARD_IF_FALSE = 0xD2, /* 210 ## */
BCODE_JUMP2_BACKWARD_IF_FALSE = 0xD3, /* 211 ## */
BCODE_JMPOP_BACKWARD_IF_TRUE = 0xD4, /* 212 ## */
BCODE_JMPOP2_BACKWARD_IF_TRUE = 0xD5, /* 213 ## */
BCODE_JMPOP_BACKWARD_IF_FALSE = 0xD6, /* 214 ## */
@ -983,12 +984,12 @@ enum moo_bcode_t
BCODE_STORE_INTO_OBJVAR_X = 0xE8, /* 232 ## */
BCODE_POP_INTO_OBJVAR_X = 0xEC, /* 236 ## */
/* UNUSED 237 */
/* UNUSED 0xED */
BCODE_MAKE_BYTEARRAY = 0xEE, /* 238 */
BCODE_POP_INTO_BYTEARRAY = 0xEF, /* 239 */
BCODE_SEND_MESSAGE_X = 0xF0, /* 240 ## */
/* UNUSED 241 */
/* UNUSED 0xF1 */
BCODE_MAKE_DICTIONARY = 0xF2, /* 242 */
BCODE_POP_INTO_DICTIONARY = 0xF3, /* 243 */
BCODE_SEND_MESSAGE_TO_SUPER_X = 0xF4, /* 244 ## */

View File

@ -1731,7 +1731,6 @@ struct moo_t
# define MOO_ASSERT(moo,expr) ((void)((expr) || ((moo)->vmprim.assertfail(moo, #expr, __FILE__, __LINE__), 0)))
#endif
#if defined(MOO_INCLUDE_COMPILER)
enum moo_iocmd_t
{