From 070caf54d2a17168d3de05aa1633979c8545f8f4 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Wed, 2 Jun 2021 01:16:49 -0700 Subject: [PATCH] Refactor rb_vm_insn_addr2insn calls It's been a way too much amount of ifdefs. --- compile.c | 6 +----- internal/compile.h | 1 + iseq.c | 12 ++++++++++++ mjit_compile.c | 25 ++++--------------------- mjit_worker.c | 6 +----- 5 files changed, 19 insertions(+), 31 deletions(-) diff --git a/compile.c b/compile.c index b03e815229..aedc9c55b5 100644 --- a/compile.c +++ b/compile.c @@ -1383,11 +1383,7 @@ update_catch_except_flags(struct rb_iseq_constant_body *body) BREAK/NEXT/REDO catch table entries are used only when `throw` insn is used in the block. */ pos = 0; while (pos < body->iseq_size) { -#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE - insn = rb_vm_insn_addr2insn((void *)body->iseq_encoded[pos]); -#else - insn = (int)body->iseq_encoded[pos]; -#endif + insn = rb_vm_insn_decode(body->iseq_encoded[pos]); if (insn == BIN(throw)) { set_catch_except_p(body); break; diff --git a/internal/compile.h b/internal/compile.h index 9842e0f5e0..c1f2a36685 100644 --- a/internal/compile.h +++ b/internal/compile.h @@ -23,6 +23,7 @@ VALUE rb_insns_name_array(void); /* iseq.c */ int rb_vm_insn_addr2insn(const void *); +int rb_vm_insn_decode(const VALUE encoded); MJIT_SYMBOL_EXPORT_BEGIN /* iseq.c (export) */ diff --git a/iseq.c b/iseq.c index fdaf55ced0..4ad1fc23be 100644 --- a/iseq.c +++ b/iseq.c @@ -3192,6 +3192,18 @@ rb_vm_insn_addr2insn(const void *addr) rb_bug("rb_vm_insn_addr2insn: invalid insn address: %p", addr); } +// Decode `iseq->body->iseq_encoded[i]` to an insn. +int +rb_vm_insn_decode(const VALUE encoded) +{ +#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE + int insn = rb_vm_insn_addr2insn((void *)encoded); +#else + int insn = (int)encoded; +#endif + return insn; +} + static inline int encoded_iseq_trace_instrument(VALUE *iseq_encoded_insn, rb_event_flag_t turnon, bool remain_current_trace) { diff --git a/mjit_compile.c b/mjit_compile.c index c857153e35..afa5e626fa 100644 --- a/mjit_compile.c +++ b/mjit_compile.c @@ -222,18 +222,13 @@ static void compile_insns(FILE *f, const struct rb_iseq_constant_body *body, unsigned int stack_size, unsigned int pos, struct compile_status *status) { - int insn; struct compile_branch branch; branch.stack_size = stack_size; branch.finish_p = false; while (pos < body->iseq_size && !ALREADY_COMPILED_P(status, pos) && !branch.finish_p) { -#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE - insn = rb_vm_insn_addr2insn((void *)body->iseq_encoded[pos]); -#else - insn = (int)body->iseq_encoded[pos]; -#endif + int insn = rb_vm_insn_decode(body->iseq_encoded[pos]); status->stack_size_for_pos[pos] = (int)branch.stack_size; fprintf(f, "\nlabel_%d: /* %s */\n", pos, insn_name(insn)); @@ -406,11 +401,7 @@ inlinable_iseq_p(const struct rb_iseq_constant_body *body) unsigned int pos = 0; while (pos < body->iseq_size) { -#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE - int insn = rb_vm_insn_addr2insn((void *)body->iseq_encoded[pos]); -#else - int insn = (int)body->iseq_encoded[pos]; -#endif + int insn = rb_vm_insn_decode(body->iseq_encoded[pos]); // All insns in the ISeq except `leave` (to be overridden in the inlined code) // should meet following strong assumptions: // * Do not require `cfp->sp` motion @@ -468,11 +459,7 @@ init_ivar_compile_status(const struct rb_iseq_constant_body *body, struct compil status->ivar_serial = 0; while (pos < body->iseq_size) { -#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE - int insn = rb_vm_insn_addr2insn((void *)body->iseq_encoded[pos]); -#else - int insn = (int)body->iseq_encoded[pos]; -#endif + int insn = rb_vm_insn_decode(body->iseq_encoded[pos]); if (insn == BIN(getinstancevariable) || insn == BIN(setinstancevariable)) { IVC ic = (IVC)body->iseq_encoded[pos+2]; IVC ic_copy = &(status->is_entries + ((union iseq_inline_storage_entry *)ic - body->is_entries))->iv_cache; @@ -527,11 +514,7 @@ precompile_inlinable_iseqs(FILE *f, const rb_iseq_t *iseq, struct compile_status const struct rb_iseq_constant_body *body = iseq->body; unsigned int pos = 0; while (pos < body->iseq_size) { -#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE - int insn = rb_vm_insn_addr2insn((void *)body->iseq_encoded[pos]); -#else - int insn = (int)body->iseq_encoded[pos]; -#endif + int insn = rb_vm_insn_decode(body->iseq_encoded[pos]); if (insn == BIN(opt_send_without_block)) { // `compile_inlined_cancel_handler` supports only `opt_send_without_block` CALL_DATA cd = (CALL_DATA)body->iseq_encoded[pos + 1]; const struct rb_callinfo *ci = cd->ci; diff --git a/mjit_worker.c b/mjit_worker.c index f2bfb510cd..3a73f14679 100644 --- a/mjit_worker.c +++ b/mjit_worker.c @@ -734,11 +734,7 @@ set_compiling_iseqs(const rb_iseq_t *iseq) unsigned int pos = 0; while (pos < iseq->body->iseq_size) { -#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE - int insn = rb_vm_insn_addr2insn((void *)iseq->body->iseq_encoded[pos]); -#else - int insn = (int)iseq->body->iseq_encoded[pos]; -#endif + int insn = rb_vm_insn_decode(iseq->body->iseq_encoded[pos]); if (insn == BIN(opt_send_without_block)) { CALL_DATA cd = (CALL_DATA)iseq->body->iseq_encoded[pos + 1]; extern const rb_iseq_t *rb_mjit_inlinable_iseq(const struct rb_callinfo *ci, const struct rb_callcache *cc);