Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ endif()
endif()

# random things to experiment
#target_compile_options(toywasm-lib-core PUBLIC "SHELL:-Xclang -fmerge-functions" "SHELL:-mllvm -mergefunc-use-aliases")
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
target_compile_options(toywasm-lib-core PUBLIC "SHELL:-Xclang -fmerge-functions" "SHELL:-mllvm -mergefunc-use-aliases")
endif()
#target_compile_options(toywasm-lib-core PUBLIC "SHELL:-mllvm -enable-machine-outliner")
#target_compile_options(toywasm-lib-core PUBLIC "SHELL:-mllvm -hot-cold-split")
#target_compile_options(toywasm-lib-core PUBLIC "SHELL:-mllvm --enable-gvn-hoist")
Expand Down
2 changes: 2 additions & 0 deletions lib/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,7 @@ const uint8_t *pc2ptr(const struct module *m, uint32_t pc) __purefunc;
int resulttype_alloc(struct mem_context *mctx, uint32_t ntypes,
const enum valtype *types, struct resulttype **resultp);
void resulttype_free(struct mem_context *mctx, struct resulttype *p);
int fetch_process_next_insn(const uint8_t **pp, const uint8_t *ep,
struct context *ctx);

__END_EXTERN_C
50 changes: 1 addition & 49 deletions lib/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1034,43 +1034,6 @@ do_branch(struct exec_context *ctx, uint32_t labelidx, bool goto_else)
rewind_stack(ctx, height, arity);
}

int
fetch_exec_next_insn(const uint8_t *p, struct cell *stack,
struct exec_context *ctx)
{
#if !(defined(TOYWASM_USE_SEPARATE_EXECUTE) && defined(TOYWASM_USE_TAILCALL))
assert(ctx->p == p);
#endif
assert(ctx->event == EXEC_EVENT_NONE);
assert(ctx->frames.lsize > 0);
#if defined(TOYWASM_ENABLE_TRACING_INSN)
uint32_t pc = ptr2pc(ctx->instance->module, p);
#endif
uint32_t op = *p++;
#if defined(TOYWASM_USE_SEPARATE_EXECUTE)
xlog_trace_insn("exec %06" PRIx32 ": %s (%02" PRIx32 ")", pc,
instructions[op].name, op);
const struct exec_instruction_desc *desc = &exec_instructions[op];
#if defined(TOYWASM_USE_TAILCALL)
__musttail
#endif
return desc->fetch_exec(p, stack, ctx);
#else
const struct instruction_desc *desc = &instructions[op];
if (__predict_false(desc->next_table != NULL)) {
op = read_leb_u32_nocheck(&p);
desc = &desc->next_table[op];
}
xlog_trace_insn("exec %06" PRIx32 ": %s", pc, desc->name);
assert(desc->process != NULL);
struct context common_ctx;
memset(&common_ctx, 0, sizeof(common_ctx));
common_ctx.exec = ctx;
ctx->p = p;
return desc->process(&ctx->p, NULL, &common_ctx);
#endif
}

static int
restart_insn(struct exec_context *ctx)
{
Expand Down Expand Up @@ -1440,8 +1403,6 @@ bool
skip_expr(const uint8_t **pp, bool goto_else)
{
const uint8_t *p = *pp;
struct context ctx;
memset(&ctx, 0, sizeof(ctx));
uint32_t block_level = 0;
if (goto_else) {
assert(*p == FRAME_OP_IF);
Expand All @@ -1450,16 +1411,7 @@ skip_expr(const uint8_t **pp, bool goto_else)
*p == FRAME_OP_IF || *p == FRAME_OP_TRY_TABLE);
}
while (true) {
uint32_t op = *p++;
const struct instruction_desc *desc = &instructions[op];
if (desc->next_table != NULL) {
uint32_t op2 = read_leb_u32_nocheck(&p);
desc = &desc->next_table[op2];
}
assert(desc->process != NULL);
xlog_trace_insn("skipping %s", desc->name);
int ret = desc->process(&p, NULL, &ctx);
assert(ret == 0);
uint32_t op = read_insn_nocheck(&p);
switch (op) {
case FRAME_OP_BLOCK:
case FRAME_OP_LOOP:
Expand Down
119 changes: 0 additions & 119 deletions lib/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,125 +19,6 @@
#include "validation.h"
#include "xlog.h"

static int
read_op(const uint8_t **pp, const uint8_t *ep,
const struct instruction_desc **descp, struct validation_context *vctx)
{
const struct instruction_desc *table = instructions;
size_t table_size = instructions_size;
const char *group = "base";
int ret;
uint8_t inst8;
uint32_t inst;

#if defined(TOYWASM_ENABLE_TRACING_INSN)
uint32_t pc = ptr2pc(vctx->module, *pp);
#endif
ret = read_u8(pp, ep, &inst8);
if (ret != 0) {
goto fail;
}
inst = inst8;
while (true) {
const struct instruction_desc *desc;
if (inst >= table_size) {
goto invalid_inst;
}
desc = &table[inst];
if (desc->next_table != NULL) {
table = desc->next_table;
table_size = desc->next_table_size;
group = desc->name;
/*
* Note: wasm "sub" opcodes are LEB128.
* cf. https://github.com/WebAssembly/spec/issues/1228
*/
ret = read_leb_u32(pp, ep, &inst);
if (ret != 0) {
goto fail;
}
continue;
}
if (desc->name == NULL) {
invalid_inst:
ret = validation_failure(
vctx,
"unimplemented instruction %02" PRIx32
" in group '%s'",
inst, group);
goto fail;
}
*descp = desc;
xlog_trace_insn("inst %06" PRIx32 " %s", pc, desc->name);
break;
}
ret = 0;
fail:
return ret;
}

static int
check_const_instruction(const struct instruction_desc *desc,
struct validation_context *vctx)
{
if (vctx->const_expr && (desc->flags & INSN_FLAG_CONST) == 0) {
return validation_failure(vctx,
"instruction \"%s\" not "
"allowed in a const expr",
desc->name);
}
return 0;
}

#if defined(TOYWASM_USE_SEPARATE_VALIDATE)
int
fetch_validate_next_insn(const uint8_t *p, const uint8_t *ep,
struct validation_context *vctx)
{
xassert(ep != NULL);
const struct instruction_desc *desc;
int ret;

ret = read_op(&p, ep, &desc, vctx);
if (ret != 0) {
goto fail;
}
ret = check_const_instruction(desc, vctx);
if (ret != 0) {
goto fail;
}
#if defined(TOYWASM_USE_TAILCALL)
__musttail
#endif
return desc->validate(p, ep, vctx);
fail:
return ret;
}
#else
int
fetch_process_next_insn(const uint8_t **pp, const uint8_t *ep,
struct context *ctx)
{
xassert(ep != NULL);
struct validation_context *vctx = ctx->validation;

const struct instruction_desc *desc;
int ret;

ret = read_op(pp, ep, &desc, vctx);
if (ret != 0) {
goto fail;
}
ret = check_const_instruction(desc, vctx);
if (ret != 0) {
goto fail;
}
return desc->process(pp, ep, ctx);
fail:
return ret;
}
#endif /* defined(TOYWASM_USE_SEPARATE_VALIDATE) */

static int
read_expr_common(const uint8_t **pp, const uint8_t *ep, struct expr *expr,
uint32_t nlocals, const struct localchunk *locals,
Expand Down
12 changes: 1 addition & 11 deletions lib/expr_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,7 @@ void
parse_expr(const uint8_t **pp, struct parse_expr_context *pctx)
{
const uint8_t *p = *pp;
struct context ctx;
memset(&ctx, 0, sizeof(ctx));
uint32_t op = *p++;
const struct instruction_desc *desc = &instructions[op];
if (desc->next_table != NULL) {
uint32_t op2 = read_leb_u32_nocheck(&p);
desc = &desc->next_table[op2];
}
assert(desc->process != NULL);
int ret = desc->process(&p, NULL, &ctx);
assert(ret == 0);
uint32_t op = read_insn_nocheck(&p);
switch (op) {
case FRAME_OP_BLOCK:
case FRAME_OP_LOOP:
Expand Down
Loading
Loading