Skip to content

Commit

Permalink
reduce code dup a bit
Browse files Browse the repository at this point in the history
the change in "end" instruction might involve some performance regression.
  • Loading branch information
yamt committed Sep 30, 2023
1 parent 1763119 commit 1d80d5e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 50 deletions.
43 changes: 43 additions & 0 deletions lib/insn.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,49 @@ read_memarg_nocheck(const uint8_t **pp, struct memarg *arg)
arg->align = align;
}

static void
schedule_br(struct exec_context *ectx, uint32_t labelidx)
{
ectx->event_u.branch.index = labelidx;
ectx->event_u.branch.goto_else = false;
ectx->event = EXEC_EVENT_BRANCH;
}

static void
schedule_goto_else(struct exec_context *ectx)
{
ectx->event_u.branch.index = 0;
ectx->event_u.branch.goto_else = true;
ectx->event = EXEC_EVENT_BRANCH;
}

static void
schedule_call(struct exec_context *ectx, const struct funcinst *fi)
{
ectx->event_u.call.func = fi;
ectx->event = EXEC_EVENT_CALL;
}

static void
schedule_return(struct exec_context *ectx)
{
assert(ectx->frames.lsize > 0);
const struct funcframe *frame = &VEC_LASTELEM(ectx->frames);
uint32_t nlabels = ectx->labels.lsize - frame->labelidx;
xlog_trace_insn("return as br %" PRIu32, nlabels);
schedule_br(ectx, nlabels);
}

#if defined(TOYWASM_ENABLE_WASM_TAILCALL)
static void
schedule_return_call(struct exec_context *ectx, const struct funcinst *fi)
{
assert(ectx->frames.lsize > 0);
ectx->event_u.call.func = fi;
ectx->event = EXEC_EVENT_RETURN_CALL;
}
#endif

/*
* LOAD_PC: prepare PC on the entry of the function.
*
Expand Down
57 changes: 10 additions & 47 deletions lib/insn_impl_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ INSN_IMPL(if)
struct exec_context *ectx = ECTX;
push_label(ORIG_PC, STACK, ECTX);
if (val_c.u.i32 == 0) {
ectx->event_u.branch.index = 0;
ectx->event_u.branch.goto_else = true;
ectx->event = EXEC_EVENT_BRANCH;
schedule_goto_else(ectx);
/*
* Note: We don't bother to call SAVE_PC as we will
* jump anyway.
Expand Down Expand Up @@ -150,9 +148,7 @@ INSN_IMPL(else)
if (EXECUTING) {
/* equivalent of "br 0" */
struct exec_context *ectx = ECTX;
ectx->event_u.branch.index = 0;
ectx->event_u.branch.goto_else = false;
ectx->event = EXEC_EVENT_BRANCH;
schedule_br(ectx, 0);
} else if (VALIDATING) {
struct validation_context *vctx = VCTX;
struct ctrlframe cframe;
Expand Down Expand Up @@ -188,26 +184,8 @@ INSN_IMPL(end)
if (__predict_true(ectx->labels.lsize > frame->labelidx)) {
VEC_POP_DROP(ectx->labels);
} else {
frame_exit(ectx);
SAVE_STACK_PTR;
#if defined(TOYWASM_USE_SEPARATE_LOCALS)
assert(ectx->stack.lsize ==
frame->height + frame->nresults);
#else
/*
* Note: stack contains >=0 locals.
* rewind_stack() "frees" them as well.
*/
assert(ectx->stack.lsize >=
frame->height + frame->nresults);
#endif
rewind_stack(ectx, frame->height, frame->nresults);
frame_clear(frame);
LOAD_STACK_PTR; /* after rewind_stack */
if (__predict_false(ectx->frames.lsize == 0)) {
INSN_SUCCESS_RETURN;
}
RELOAD_PC; /* after frame_exit */
schedule_return(ectx);
INSN_SUCCESS_RETURN;
}
} else if (VALIDATING) {
struct validation_context *vctx = VCTX;
Expand Down Expand Up @@ -262,9 +240,7 @@ INSN_IMPL(br)
READ_LEB_U32(labelidx);
if (EXECUTING) {
struct exec_context *ectx = ECTX;
ectx->event_u.branch.index = labelidx;
ectx->event_u.branch.goto_else = false;
ectx->event = EXEC_EVENT_BRANCH;
schedule_br(ectx, labelidx);
/*
* Note: We don't bother to call SAVE_PC as we will
* jump anyway.
Expand Down Expand Up @@ -299,9 +275,7 @@ INSN_IMPL(br_if)
if (EXECUTING) {
if (val_l.u.i32 != 0) {
struct exec_context *ectx = ECTX;
ectx->event_u.branch.index = labelidx;
ectx->event_u.branch.goto_else = false;
ectx->event = EXEC_EVENT_BRANCH;
schedule_br(ectx, labelidx);
/*
* Note: We don't bother to call SAVE_PC as we will
* jump anyway.
Expand Down Expand Up @@ -377,9 +351,7 @@ INSN_IMPL(br_table)
}
l--;
}
ectx->event_u.branch.index = idx;
ectx->event_u.branch.goto_else = false;
ectx->event = EXEC_EVENT_BRANCH;
schedule_br(ectx, idx);
/*
* Note: We don't bother to call SAVE_PC as we will
* jump anyway.
Expand Down Expand Up @@ -434,13 +406,7 @@ INSN_IMPL(return)
int ret;
if (EXECUTING) {
struct exec_context *ectx = ECTX;
assert(ectx->frames.lsize > 0);
const struct funcframe *frame = &VEC_LASTELEM(ectx->frames);
uint32_t nlabels = ectx->labels.lsize - frame->labelidx;
xlog_trace_insn("return as br %" PRIu32, nlabels);
ectx->event_u.branch.index = nlabels;
ectx->event_u.branch.goto_else = false;
ectx->event = EXEC_EVENT_BRANCH;
schedule_return(ectx);
} else if (VALIDATING) {
struct validation_context *vctx = VCTX;
ret = pop_valtypes(returntype(vctx), vctx);
Expand All @@ -465,9 +431,7 @@ INSN_IMPL(call)
CHECK(funcidx < m->nimportedfuncs + m->nfuncs);
if (EXECUTING) {
struct exec_context *ectx = ECTX;
ectx->event_u.call.func =
VEC_ELEM(ectx->instance->funcs, funcidx);
ectx->event = EXEC_EVENT_CALL;
schedule_call(ectx, VEC_ELEM(ectx->instance->funcs, funcidx));
} else if (VALIDATING) {
struct validation_context *vctx = VCTX;
const struct functype *ft = module_functype(m, funcidx);
Expand Down Expand Up @@ -508,8 +472,7 @@ INSN_IMPL(call_indirect)
if (__predict_false(ret != 0)) {
goto fail;
}
ectx->event_u.call.func = func;
ectx->event = EXEC_EVENT_CALL;
schedule_call(ectx, func);
} else if (VALIDATING) {
struct validation_context *vctx = VCTX;
const struct tabletype *tab = module_tabletype(m, tableidx);
Expand Down
5 changes: 2 additions & 3 deletions lib/insn_impl_tailcall.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ INSN_IMPL(return_call)
CHECK(funcidx < m->nimportedfuncs + m->nfuncs);
if (EXECUTING) {
struct exec_context *ectx = ECTX;
ectx->event_u.call.func =
VEC_ELEM(ectx->instance->funcs, funcidx);
ectx->event = EXEC_EVENT_RETURN_CALL;
schedule_return_call(ectx,
VEC_ELEM(ectx->instance->funcs, funcidx));
} else if (VALIDATING) {
struct validation_context *vctx = VCTX;
const struct functype *ft = module_functype(m, funcidx);
Expand Down

0 comments on commit 1d80d5e

Please sign in to comment.