diff --git a/cmake/ToywasmConfig.cmake b/cmake/ToywasmConfig.cmake index 3c29ebad..1886ca55 100644 --- a/cmake/ToywasmConfig.cmake +++ b/cmake/ToywasmConfig.cmake @@ -7,6 +7,7 @@ endif() # TOYWASM_USE_SEPARATE_EXECUTE=ON -> faster execution # TOYWASM_USE_SEPARATE_EXECUTE=OFF -> smaller code option(TOYWASM_USE_SEPARATE_EXECUTE "Use separate execute callback" ON) +option(TOYWASM_USE_SEPARATE_VALIDATE "Use separate validation callback" ON) # TOYWASM_USE_TAILCALL=ON # enable -mtail-call for wasm target diff --git a/lib/insn.c b/lib/insn.c index 7e168edf..adc4b95d 100644 --- a/lib/insn.c +++ b/lib/insn.c @@ -537,8 +537,13 @@ schedule_exception(struct exec_context *ectx) #define EXECUTING (ctx->exec != NULL) #define ECTX (ctx->exec) #endif +#if defined(TOYWASM_USE_SEPARATE_VALIDATE) +#define VALIDATING false +#define VCTX ((struct validation_context *)NULL) +#else #define VALIDATING (ctx->validation != NULL) #define VCTX (ctx->validation) +#endif #define INSN_IMPL(NAME) \ int process_##NAME(const uint8_t **pp, const uint8_t *ep, \ struct context *ctx) @@ -680,6 +685,58 @@ schedule_exception(struct exec_context *ectx) #undef STACK_ADJ #endif /* defined(TOYWASM_USE_SEPARATE_EXECUTE) */ +#if defined(TOYWASM_USE_SEPARATE_VALIDATE) +#define EXECUTING false +#define ECTX ((struct exec_context *)NULL) +#define VALIDATING true +#define VCTX ctx +#define INSN_IMPL(NAME) \ + int validate_##NAME(const uint8_t **pp, const uint8_t *ep, \ + struct validation_context *ctx) +#define LOAD_PC const uint8_t *p __attribute__((__unused__)) = *pp +#define SAVE_PC *pp = p +#define RELOAD_PC +#define SAVE_STACK_PTR +#define LOAD_STACK_PTR +#define ORIG_PC (*pp) +#define INSN_SUCCESS return 0 +#define INSN_SUCCESS_RETURN INSN_SUCCESS +#define PREPARE_FOR_POSSIBLE_RESTART +#define INSN_FAIL_RESTARTABLE(NAME) INSN_FAIL +#define INSN_FAIL \ + assert(ret != 0); \ + assert(!IS_RESTARTABLE(ret)); \ + return ret +#undef push_val +#undef pop_val +#define push_val(v, csz, ctx) do {} while (0) +#define pop_val(v, csz, ctx) do {} while (0) +#define STACK NULL +#define STACK_ADJ(n) do {} while(0) + +#include "insn_impl.h" + +#undef EXECUTING +#undef ECTX +#undef VALIDATING +#undef VCTX +#undef INSN_IMPL +#undef LOAD_PC +#undef SAVE_PC +#undef RELOAD_PC +#undef SAVE_STACK_PTR +#undef LOAD_STACK_PTR +#undef ORIG_PC +#undef PREPARE_FOR_POSSIBLE_RESTART +#undef INSN_SUCCESS +#undef INSN_SUCCESS_RETURN +#undef INSN_FAIL +#undef INSN_FAIL_RESTARTABLE +#undef ep +#undef STACK +#undef STACK_ADJ +#endif /* defined(TOYWASM_USE_SEPARATE_EXECUTE) */ + #if defined(TOYWASM_USE_SEPARATE_EXECUTE) typedef int (*exec_func_t)(const uint8_t *, struct cell *, struct exec_context *); diff --git a/lib/toywasm_config.c.in b/lib/toywasm_config.c.in index 9869d125..6e209597 100644 --- a/lib/toywasm_config.c.in +++ b/lib/toywasm_config.c.in @@ -1,5 +1,6 @@ const char *const toywasm_config_string = "\tTOYWASM_USE_SEPARATE_EXECUTE = @TOYWASM_USE_SEPARATE_EXECUTE@\n" +"\tTOYWASM_USE_SEPARATE_VALIDATE = @TOYWASM_USE_SEPARATE_VALIDATE@\n" "\tTOYWASM_USE_TAILCALL = @TOYWASM_USE_TAILCALL@\n" "\tTOYWASM_FORCE_USE_TAILCALL = @TOYWASM_FORCE_USE_TAILCALL@\n" "\tTOYWASM_USE_SIMD = @TOYWASM_USE_SIMD@\n" diff --git a/lib/toywasm_config.h.in b/lib/toywasm_config.h.in index 1afad7ac..1fd2cd6e 100644 --- a/lib/toywasm_config.h.in +++ b/lib/toywasm_config.h.in @@ -2,6 +2,7 @@ #define _TOYWASM_CONFIG_H #cmakedefine TOYWASM_USE_SEPARATE_EXECUTE +#cmakedefine TOYWASM_USE_SEPARATE_VALIDATE #cmakedefine TOYWASM_USE_TAILCALL #cmakedefine TOYWASM_FORCE_USE_TAILCALL #cmakedefine TOYWASM_USE_SIMD