diff options
Diffstat (limited to 'src/interpreter.h')
-rw-r--r-- | src/interpreter.h | 277 |
1 files changed, 135 insertions, 142 deletions
diff --git a/src/interpreter.h b/src/interpreter.h index 05e69eb8..d37473d5 100644 --- a/src/interpreter.h +++ b/src/interpreter.h @@ -29,10 +29,10 @@ namespace wabt { -// TODO(binji): move all the Interpreter stuff into its own namespace. - class Stream; +namespace interpreter { + #define FOREACH_INTERPRETER_RESULT(V) \ V(Ok, "ok") \ /* returned from the top-most function */ \ @@ -70,7 +70,7 @@ class Stream; /* the expected export kind doesn't match. */ \ V(ExportKindMismatch, "export kind mismatch") -enum class InterpreterResult { +enum class Result { #define V(Name, str) Name, FOREACH_INTERPRETER_RESULT(V) #undef V @@ -92,73 +92,71 @@ static const IstreamOffset kInvalidIstreamOffset = ~0; #define WABT_TABLE_ENTRY_DROP_OFFSET sizeof(uint32_t) #define WABT_TABLE_ENTRY_KEEP_OFFSET (sizeof(IstreamOffset) + sizeof(uint32_t)) -enum class InterpreterOpcode { +enum class Opcode { /* push space on the value stack for N entries */ -#define WABT_OPCODE(rtype, type1, type2, mem_size, code, Name, text) Name = code, +#define WABT_OPCODE(rtype, type1, type2, mem_size, code, Name, text) \ + Name = code, #include "interpreter-opcode.def" #undef WABT_OPCODE - First = static_cast<int>(Opcode::First), + First = static_cast<int>(::wabt::Opcode::First), Last = DropKeep, }; -static const int kInterpreterOpcodeCount = WABT_ENUM_COUNT(InterpreterOpcode); +static const int kOpcodeCount = WABT_ENUM_COUNT(Opcode); -struct InterpreterFuncSignature { +struct FuncSignature { std::vector<Type> param_types; std::vector<Type> result_types; }; -struct InterpreterTable { - explicit InterpreterTable(const Limits& limits) +struct Table { + explicit Table(const Limits& limits) : limits(limits), func_indexes(limits.initial, kInvalidIndex) {} Limits limits; std::vector<Index> func_indexes; }; -struct InterpreterMemory { - InterpreterMemory() { - WABT_ZERO_MEMORY(page_limits); - } - explicit InterpreterMemory(const Limits& limits) +struct Memory { + Memory() { WABT_ZERO_MEMORY(page_limits); } + explicit Memory(const Limits& limits) : page_limits(limits), data(limits.initial * WABT_PAGE_SIZE) {} Limits page_limits; std::vector<char> data; }; -union InterpreterValue { +union Value { uint32_t i32; uint64_t i64; uint32_t f32_bits; uint64_t f64_bits; }; -struct InterpreterTypedValue { - InterpreterTypedValue() {} - explicit InterpreterTypedValue(Type type): type(type) {} - InterpreterTypedValue(Type type, const InterpreterValue& value) - : type(type), value(value) {} +struct TypedValue { + TypedValue() {} + explicit TypedValue(Type type) : type(type) {} + TypedValue(Type type, const Value& value) : type(type), value(value) {} Type type; - InterpreterValue value; + Value value; }; -struct InterpreterGlobal { - InterpreterGlobal() : mutable_(false), import_index(kInvalidIndex) {} - InterpreterGlobal(const InterpreterTypedValue& typed_value, bool mutable_) +struct Global { + Global() : mutable_(false), import_index(kInvalidIndex) {} + Global(const TypedValue& typed_value, bool mutable_) : typed_value(typed_value), mutable_(mutable_) {} - InterpreterTypedValue typed_value; + TypedValue typed_value; bool mutable_; Index import_index; /* or INVALID_INDEX if not imported */ }; -struct InterpreterImport { - InterpreterImport(); - InterpreterImport(InterpreterImport&&); - InterpreterImport& operator=(InterpreterImport&&); - ~InterpreterImport(); +struct Import { + Import(); + Import(Import&&); + Import& operator=(Import&&); + ~Import(); StringSlice module_name; StringSlice field_name; @@ -177,33 +175,32 @@ struct InterpreterImport { }; }; -struct InterpreterFunc; +struct Func; -typedef Result (*InterpreterHostFuncCallback)( - const struct HostInterpreterFunc* func, - const InterpreterFuncSignature* sig, - Index num_args, - InterpreterTypedValue* args, - Index num_results, - InterpreterTypedValue* out_results, - void* user_data); +typedef Result (*HostFuncCallback)(const struct HostFunc* func, + const FuncSignature* sig, + Index num_args, + TypedValue* args, + Index num_results, + TypedValue* out_results, + void* user_data); -struct InterpreterFunc { - WABT_DISALLOW_COPY_AND_ASSIGN(InterpreterFunc); - InterpreterFunc(Index sig_index, bool is_host) +struct Func { + WABT_DISALLOW_COPY_AND_ASSIGN(Func); + Func(Index sig_index, bool is_host) : sig_index(sig_index), is_host(is_host) {} - virtual ~InterpreterFunc() {} + virtual ~Func() {} - inline struct DefinedInterpreterFunc* as_defined(); - inline struct HostInterpreterFunc* as_host(); + inline struct DefinedFunc* as_defined(); + inline struct HostFunc* as_host(); Index sig_index; bool is_host; }; -struct DefinedInterpreterFunc : InterpreterFunc { - DefinedInterpreterFunc(Index sig_index) - : InterpreterFunc(sig_index, false), +struct DefinedFunc : Func { + DefinedFunc(Index sig_index) + : Func(sig_index, false), offset(kInvalidIstreamOffset), local_decl_count(0), local_count(0) {} @@ -214,36 +211,36 @@ struct DefinedInterpreterFunc : InterpreterFunc { std::vector<Type> param_and_local_types; }; -struct HostInterpreterFunc : InterpreterFunc { - HostInterpreterFunc(const StringSlice& module_name, - const StringSlice& field_name, - Index sig_index) - : InterpreterFunc(sig_index, true), +struct HostFunc : Func { + HostFunc(const StringSlice& module_name, + const StringSlice& field_name, + Index sig_index) + : Func(sig_index, true), module_name(module_name), field_name(field_name) {} StringSlice module_name; StringSlice field_name; - InterpreterHostFuncCallback callback; + HostFuncCallback callback; void* user_data; }; -DefinedInterpreterFunc* InterpreterFunc::as_defined() { +DefinedFunc* Func::as_defined() { assert(!is_host); - return static_cast<DefinedInterpreterFunc*>(this); + return static_cast<DefinedFunc*>(this); } -HostInterpreterFunc* InterpreterFunc::as_host() { +HostFunc* Func::as_host() { assert(is_host); - return static_cast<HostInterpreterFunc*>(this); + return static_cast<HostFunc*>(this); } -struct InterpreterExport { - InterpreterExport(const StringSlice& name, ExternalKind kind, Index index) +struct Export { + Export(const StringSlice& name, ExternalKind kind, Index index) : name(name), kind(kind), index(index) {} - InterpreterExport(InterpreterExport&&); - InterpreterExport& operator=(InterpreterExport&&); - ~InterpreterExport(); + Export(Export&&); + Export& operator=(Export&&); + ~Export(); StringSlice name; ExternalKind kind; @@ -255,71 +252,71 @@ struct PrintErrorCallback { void (*print_error)(const char* msg, void* user_data); }; -struct InterpreterHostImportDelegate { +struct HostImportDelegate { void* user_data; - Result (*import_func)(InterpreterImport*, - InterpreterFunc*, - InterpreterFuncSignature*, + ::wabt::Result (*import_func)(Import*, + Func*, + FuncSignature*, PrintErrorCallback, void* user_data); - Result (*import_table)(InterpreterImport*, - InterpreterTable*, - PrintErrorCallback, - void* user_data); - Result (*import_memory)(InterpreterImport*, - InterpreterMemory*, - PrintErrorCallback, - void* user_data); - Result (*import_global)(InterpreterImport*, - InterpreterGlobal*, - PrintErrorCallback, - void* user_data); + ::wabt::Result (*import_table)(Import*, + Table*, + PrintErrorCallback, + void* user_data); + ::wabt::Result (*import_memory)(Import*, + Memory*, + PrintErrorCallback, + void* user_data); + ::wabt::Result (*import_global)(Import*, + Global*, + PrintErrorCallback, + void* user_data); }; -struct InterpreterModule { - WABT_DISALLOW_COPY_AND_ASSIGN(InterpreterModule); - explicit InterpreterModule(bool is_host); - InterpreterModule(const StringSlice& name, bool is_host); - virtual ~InterpreterModule(); +struct Module { + WABT_DISALLOW_COPY_AND_ASSIGN(Module); + explicit Module(bool is_host); + Module(const StringSlice& name, bool is_host); + virtual ~Module(); - inline struct DefinedInterpreterModule* as_defined(); - inline struct HostInterpreterModule* as_host(); + inline struct DefinedModule* as_defined(); + inline struct HostModule* as_host(); StringSlice name; - std::vector<InterpreterExport> exports; + std::vector<Export> exports; BindingHash export_bindings; Index memory_index; /* kInvalidIndex if not defined */ Index table_index; /* kInvalidIndex if not defined */ bool is_host; }; -struct DefinedInterpreterModule : InterpreterModule { - explicit DefinedInterpreterModule(size_t istream_start); +struct DefinedModule : Module { + explicit DefinedModule(size_t istream_start); - std::vector<InterpreterImport> imports; + std::vector<Import> imports; Index start_func_index; /* kInvalidIndex if not defined */ size_t istream_start; size_t istream_end; }; -struct HostInterpreterModule : InterpreterModule { - HostInterpreterModule(const StringSlice& name); +struct HostModule : Module { + HostModule(const StringSlice& name); - InterpreterHostImportDelegate import_delegate; + HostImportDelegate import_delegate; }; -DefinedInterpreterModule* InterpreterModule::as_defined() { +DefinedModule* Module::as_defined() { assert(!is_host); - return static_cast<DefinedInterpreterModule*>(this); + return static_cast<DefinedModule*>(this); } -HostInterpreterModule* InterpreterModule::as_host() { +HostModule* Module::as_host() { assert(is_host); - return static_cast<HostInterpreterModule*>(this); + return static_cast<HostModule*>(this); } /* Used to track and reset the state of the environment. */ -struct InterpreterEnvironmentMark { +struct EnvironmentMark { size_t modules_size; size_t sigs_size; size_t funcs_size; @@ -329,37 +326,41 @@ struct InterpreterEnvironmentMark { size_t istream_size; }; -struct InterpreterEnvironment { - InterpreterEnvironment(); +struct Environment { + Environment(); - std::vector<std::unique_ptr<InterpreterModule>> modules; - std::vector<InterpreterFuncSignature> sigs; - std::vector<std::unique_ptr<InterpreterFunc>> funcs; - std::vector<InterpreterMemory> memories; - std::vector<InterpreterTable> tables; - std::vector<InterpreterGlobal> globals; + std::vector<std::unique_ptr<Module>> modules; + std::vector<FuncSignature> sigs; + std::vector<std::unique_ptr<Func>> funcs; + std::vector<Memory> memories; + std::vector<Table> tables; + std::vector<Global> globals; std::unique_ptr<OutputBuffer> istream; BindingHash module_bindings; BindingHash registered_module_bindings; }; -struct InterpreterThread { - InterpreterThread(); +struct Thread { + Thread(); - InterpreterEnvironment* env; - std::vector<InterpreterValue> value_stack; + Environment* env; + std::vector<Value> value_stack; std::vector<IstreamOffset> call_stack; - InterpreterValue* value_stack_top; - InterpreterValue* value_stack_end; + Value* value_stack_top; + Value* value_stack_end; IstreamOffset* call_stack_top; IstreamOffset* call_stack_end; IstreamOffset pc; }; -#define WABT_INTERPRETER_THREAD_OPTIONS_DEFAULT \ - { 512 * 1024 / sizeof(InterpreterValue), 64 * 1024, kInvalidIstreamOffset } +// TODO(binji): Remove and use default constructor. +#define WABT_INTERPRETER_THREAD_OPTIONS_DEFAULT \ + { \ + 512 * 1024 / sizeof(::wabt::interpreter::Value), 64 * 1024, \ + ::wabt::interpreter::kInvalidIstreamOffset \ + } -struct InterpreterThreadOptions { +struct ThreadOptions { uint32_t value_stack_size; uint32_t call_stack_size; IstreamOffset pc; @@ -369,40 +370,32 @@ bool is_canonical_nan_f32(uint32_t f32_bits); bool is_canonical_nan_f64(uint64_t f64_bits); bool is_arithmetic_nan_f32(uint32_t f32_bits); bool is_arithmetic_nan_f64(uint64_t f64_bits); -bool func_signatures_are_equal(InterpreterEnvironment* env, +bool func_signatures_are_equal(Environment* env, Index sig_index_0, Index sig_index_1); -void destroy_interpreter_environment(InterpreterEnvironment* env); -InterpreterEnvironmentMark mark_interpreter_environment( - InterpreterEnvironment* env); -void reset_interpreter_environment_to_mark(InterpreterEnvironment* env, - InterpreterEnvironmentMark mark); -HostInterpreterModule* append_host_module(InterpreterEnvironment* env, - StringSlice name); -void init_interpreter_thread(InterpreterEnvironment* env, - InterpreterThread* thread, - InterpreterThreadOptions* options); -InterpreterResult push_thread_value(InterpreterThread* thread, - InterpreterValue value); -void destroy_interpreter_thread(InterpreterThread* thread); -InterpreterResult call_host(InterpreterThread* thread, - HostInterpreterFunc* func); -InterpreterResult run_interpreter(InterpreterThread* thread, - int num_instructions, - IstreamOffset* call_stack_return_top); -void trace_pc(InterpreterThread* thread, Stream* stream); -void disassemble(InterpreterEnvironment* env, +// TODO(binji): Use methods on Environment and Thread instead. +void destroy_environment(Environment* env); +EnvironmentMark mark_environment(Environment* env); +void reset_environment_to_mark(Environment* env, EnvironmentMark mark); +HostModule* append_host_module(Environment* env, StringSlice name); +void init_thread(Environment* env, Thread* thread, ThreadOptions* options); +Result push_thread_value(Thread* thread, Value value); +void destroy_thread(Thread* thread); +Result call_host(Thread* thread, HostFunc* func); +Result run_interpreter(Thread* thread, + int num_instructions, + IstreamOffset* call_stack_return_top); +void trace_pc(Thread* thread, Stream* stream); +void disassemble(Environment* env, Stream* stream, IstreamOffset from, IstreamOffset to); -void disassemble_module(InterpreterEnvironment* env, - Stream* stream, - InterpreterModule* module); +void disassemble_module(Environment* env, Stream* stream, Module* module); -InterpreterExport* get_interpreter_export_by_name(InterpreterModule* module, - const StringSlice* name); +Export* get_export_by_name(Module* module, const StringSlice* name); +} // namespace interpreter } // namespace wabt #endif /* WABT_INTERPRETER_H_ */ |