From 765d598979b5eb4e1229695cd495d242f9db9337 Mon Sep 17 00:00:00 2001 From: Ben Smith Date: Tue, 23 May 2017 17:01:04 -0700 Subject: Move interpreter stuff into its own namespace (#454) The only names that conflict are `wabt::Opcode` vs. `interpreter::Opcode` and `wabt::Result` vs. `interpreter::Result`. Other names would conflict too (e.g. `FuncSignature`) but those aren't ever included in the interpreter-related code. --- src/binary-reader-interpreter.cc | 1047 +++++++++++++++++++------------------- 1 file changed, 527 insertions(+), 520 deletions(-) (limited to 'src/binary-reader-interpreter.cc') diff --git a/src/binary-reader-interpreter.cc b/src/binary-reader-interpreter.cc index 0c2396ff..cbac7668 100644 --- a/src/binary-reader-interpreter.cc +++ b/src/binary-reader-interpreter.cc @@ -28,14 +28,16 @@ #include "type-checker.h" #include "writer.h" -#define CHECK_RESULT(expr) \ - do { \ - if (WABT_FAILED(expr)) \ - return Result::Error; \ +#define CHECK_RESULT(expr) \ + do { \ + if (WABT_FAILED(expr)) \ + return wabt::Result::Error; \ } while (0) namespace wabt { +using namespace interpreter; + namespace { typedef std::vector IndexVector; @@ -71,8 +73,8 @@ struct DataSegmentInfo { class BinaryReaderInterpreter : public BinaryReaderNop { public: - BinaryReaderInterpreter(InterpreterEnvironment* env, - DefinedInterpreterModule* module, + BinaryReaderInterpreter(Environment* env, + DefinedModule* module, IstreamOffset istream_offset, BinaryErrorHandler* error_handler); @@ -82,120 +84,120 @@ class BinaryReaderInterpreter : public BinaryReaderNop { // Implement BinaryReader. bool OnError(const char* message) override; - Result EndModule() override; - - Result OnTypeCount(Index count) override; - Result OnType(Index index, - Index param_count, - Type* param_types, - Index result_count, - Type* result_types) override; - - Result OnImportCount(Index count) override; - Result OnImport(Index index, - StringSlice module_name, - StringSlice field_name) override; - Result OnImportFunc(Index import_index, - StringSlice module_name, - StringSlice field_name, - Index func_index, - Index sig_index) override; - Result OnImportTable(Index import_index, - StringSlice module_name, - StringSlice field_name, - Index table_index, + wabt::Result EndModule() override; + + wabt::Result OnTypeCount(Index count) override; + wabt::Result OnType(Index index, + Index param_count, + Type* param_types, + Index result_count, + Type* result_types) override; + + wabt::Result OnImportCount(Index count) override; + wabt::Result OnImport(Index index, + StringSlice module_name, + StringSlice field_name) override; + wabt::Result OnImportFunc(Index import_index, + StringSlice module_name, + StringSlice field_name, + Index func_index, + Index sig_index) override; + wabt::Result OnImportTable(Index import_index, + StringSlice module_name, + StringSlice field_name, + Index table_index, + Type elem_type, + const Limits* elem_limits) override; + wabt::Result OnImportMemory(Index import_index, + StringSlice module_name, + StringSlice field_name, + Index memory_index, + const Limits* page_limits) override; + wabt::Result OnImportGlobal(Index import_index, + StringSlice module_name, + StringSlice field_name, + Index global_index, + Type type, + bool mutable_) override; + + wabt::Result OnFunctionCount(Index count) override; + wabt::Result OnFunction(Index index, Index sig_index) override; + + wabt::Result OnTable(Index index, Type elem_type, const Limits* elem_limits) override; - Result OnImportMemory(Index import_index, - StringSlice module_name, - StringSlice field_name, - Index memory_index, - const Limits* page_limits) override; - Result OnImportGlobal(Index import_index, - StringSlice module_name, - StringSlice field_name, - Index global_index, - Type type, - bool mutable_) override; - - Result OnFunctionCount(Index count) override; - Result OnFunction(Index index, Index sig_index) override; - - Result OnTable(Index index, - Type elem_type, - const Limits* elem_limits) override; - - Result OnMemory(Index index, const Limits* limits) override; - - Result OnGlobalCount(Index count) override; - Result BeginGlobal(Index index, Type type, bool mutable_) override; - Result EndGlobalInitExpr(Index index) override; - - Result OnExport(Index index, - ExternalKind kind, - Index item_index, - StringSlice name) override; - - Result OnStartFunction(Index func_index) override; - - Result BeginFunctionBody(Index index) override; - Result OnLocalDeclCount(Index count) override; - Result OnLocalDecl(Index decl_index, Index count, Type type) override; - - Result OnBinaryExpr(Opcode opcode) override; - Result OnBlockExpr(Index num_types, Type* sig_types) override; - Result OnBrExpr(Index depth) override; - Result OnBrIfExpr(Index depth) override; - Result OnBrTableExpr(Index num_targets, - Index* target_depths, - Index default_target_depth) override; - Result OnCallExpr(Index func_index) override; - Result OnCallIndirectExpr(Index sig_index) override; - Result OnCompareExpr(Opcode opcode) override; - Result OnConvertExpr(Opcode opcode) override; - Result OnCurrentMemoryExpr() override; - Result OnDropExpr() override; - Result OnElseExpr() override; - Result OnEndExpr() override; - Result OnF32ConstExpr(uint32_t value_bits) override; - Result OnF64ConstExpr(uint64_t value_bits) override; - Result OnGetGlobalExpr(Index global_index) override; - Result OnGetLocalExpr(Index local_index) override; - Result OnGrowMemoryExpr() override; - Result OnI32ConstExpr(uint32_t value) override; - Result OnI64ConstExpr(uint64_t value) override; - Result OnIfExpr(Index num_types, Type* sig_types) override; - Result OnLoadExpr(Opcode opcode, - uint32_t alignment_log2, - Address offset) override; - Result OnLoopExpr(Index num_types, Type* sig_types) override; - Result OnNopExpr() override; - Result OnReturnExpr() override; - Result OnSelectExpr() override; - Result OnSetGlobalExpr(Index global_index) override; - Result OnSetLocalExpr(Index local_index) override; - Result OnStoreExpr(Opcode opcode, - uint32_t alignment_log2, - Address offset) override; - Result OnTeeLocalExpr(Index local_index) override; - Result OnUnaryExpr(Opcode opcode) override; - Result OnUnreachableExpr() override; - Result EndFunctionBody(Index index) override; - - Result EndElemSegmentInitExpr(Index index) override; - Result OnElemSegmentFunctionIndex(Index index, - Index func_index) override; - - Result OnDataSegmentData(Index index, - const void* data, - Address size) override; - - Result OnInitExprF32ConstExpr(Index index, uint32_t value) override; - Result OnInitExprF64ConstExpr(Index index, uint64_t value) override; - Result OnInitExprGetGlobalExpr(Index index, - Index global_index) override; - Result OnInitExprI32ConstExpr(Index index, uint32_t value) override; - Result OnInitExprI64ConstExpr(Index index, uint64_t value) override; + + wabt::Result OnMemory(Index index, const Limits* limits) override; + + wabt::Result OnGlobalCount(Index count) override; + wabt::Result BeginGlobal(Index index, Type type, bool mutable_) override; + wabt::Result EndGlobalInitExpr(Index index) override; + + wabt::Result OnExport(Index index, + ExternalKind kind, + Index item_index, + StringSlice name) override; + + wabt::Result OnStartFunction(Index func_index) override; + + wabt::Result BeginFunctionBody(Index index) override; + wabt::Result OnLocalDeclCount(Index count) override; + wabt::Result OnLocalDecl(Index decl_index, Index count, Type type) override; + + wabt::Result OnBinaryExpr(wabt::Opcode opcode) override; + wabt::Result OnBlockExpr(Index num_types, Type* sig_types) override; + wabt::Result OnBrExpr(Index depth) override; + wabt::Result OnBrIfExpr(Index depth) override; + wabt::Result OnBrTableExpr(Index num_targets, + Index* target_depths, + Index default_target_depth) override; + wabt::Result OnCallExpr(Index func_index) override; + wabt::Result OnCallIndirectExpr(Index sig_index) override; + wabt::Result OnCompareExpr(wabt::Opcode opcode) override; + wabt::Result OnConvertExpr(wabt::Opcode opcode) override; + wabt::Result OnCurrentMemoryExpr() override; + wabt::Result OnDropExpr() override; + wabt::Result OnElseExpr() override; + wabt::Result OnEndExpr() override; + wabt::Result OnF32ConstExpr(uint32_t value_bits) override; + wabt::Result OnF64ConstExpr(uint64_t value_bits) override; + wabt::Result OnGetGlobalExpr(Index global_index) override; + wabt::Result OnGetLocalExpr(Index local_index) override; + wabt::Result OnGrowMemoryExpr() override; + wabt::Result OnI32ConstExpr(uint32_t value) override; + wabt::Result OnI64ConstExpr(uint64_t value) override; + wabt::Result OnIfExpr(Index num_types, Type* sig_types) override; + wabt::Result OnLoadExpr(wabt::Opcode opcode, + uint32_t alignment_log2, + Address offset) override; + wabt::Result OnLoopExpr(Index num_types, Type* sig_types) override; + wabt::Result OnNopExpr() override; + wabt::Result OnReturnExpr() override; + wabt::Result OnSelectExpr() override; + wabt::Result OnSetGlobalExpr(Index global_index) override; + wabt::Result OnSetLocalExpr(Index local_index) override; + wabt::Result OnStoreExpr(wabt::Opcode opcode, + uint32_t alignment_log2, + Address offset) override; + wabt::Result OnTeeLocalExpr(Index local_index) override; + wabt::Result OnUnaryExpr(wabt::Opcode opcode) override; + wabt::Result OnUnreachableExpr() override; + wabt::Result EndFunctionBody(Index index) override; + + wabt::Result EndElemSegmentInitExpr(Index index) override; + wabt::Result OnElemSegmentFunctionIndex(Index index, + Index func_index) override; + + wabt::Result OnDataSegmentData(Index index, + const void* data, + Address size) override; + + wabt::Result OnInitExprF32ConstExpr(Index index, uint32_t value) override; + wabt::Result OnInitExprF64ConstExpr(Index index, uint64_t value) override; + wabt::Result OnInitExprGetGlobalExpr(Index index, + Index global_index) override; + wabt::Result OnInitExprI32ConstExpr(Index index, uint32_t value) override; + wabt::Result OnInitExprI64ConstExpr(Index index, uint64_t value) override; private: Label* GetLabel(Index depth); @@ -208,64 +210,65 @@ class BinaryReaderInterpreter : public BinaryReaderNop { static void OnTypecheckerError(const char* msg, void* user_data); Index TranslateSigIndexToEnv(Index sig_index); - InterpreterFuncSignature* GetSignatureByEnvIndex(Index sig_index); - InterpreterFuncSignature* GetSignatureByModuleIndex(Index sig_index); + FuncSignature* GetSignatureByEnvIndex(Index sig_index); + FuncSignature* GetSignatureByModuleIndex(Index sig_index); Index TranslateFuncIndexToEnv(Index func_index); Index TranslateModuleFuncIndexToDefined(Index func_index); - InterpreterFunc* GetFuncByEnvIndex(Index func_index); - InterpreterFunc* GetFuncByModuleIndex(Index func_index); + Func* GetFuncByEnvIndex(Index func_index); + Func* GetFuncByModuleIndex(Index func_index); Index TranslateGlobalIndexToEnv(Index global_index); - InterpreterGlobal* GetGlobalByEnvIndex(Index global_index); - InterpreterGlobal* GetGlobalByModuleIndex(Index global_index); + Global* GetGlobalByEnvIndex(Index global_index); + Global* GetGlobalByModuleIndex(Index global_index); Type GetGlobalTypeByModuleIndex(Index global_index); Index TranslateLocalIndex(Index local_index); - Type GetLocalTypeByIndex(InterpreterFunc* func, Index local_index); + Type GetLocalTypeByIndex(Func* func, Index local_index); IstreamOffset GetIstreamOffset(); - Result EmitDataAt(IstreamOffset offset, - const void* data, - IstreamOffset size); - Result EmitData(const void* data, IstreamOffset size); - Result EmitOpcode(Opcode opcode); - Result EmitOpcode(InterpreterOpcode opcode); - Result EmitI8(uint8_t value); - Result EmitI32(uint32_t value); - Result EmitI64(uint64_t value); - Result EmitI32At(IstreamOffset offset, uint32_t value); - Result EmitDropKeep(uint32_t drop, uint8_t keep); - Result AppendFixup(IstreamOffsetVectorVector* fixups_vector, Index index); - Result EmitBrOffset(Index depth, IstreamOffset offset); - Result GetBrDropKeepCount(Index depth, - Index* out_drop_count, - Index* out_keep_count); - Result GetReturnDropKeepCount(Index* out_drop_count, - Index* out_keep_count); - Result EmitBr(Index depth, Index drop_count, Index keep_count); - Result EmitBrTableOffset(Index depth); - Result FixupTopLabel(); - Result EmitFuncOffset(DefinedInterpreterFunc* func, Index func_index); - - Result CheckLocal(Index local_index); - Result CheckGlobal(Index global_index); - Result CheckImportKind(InterpreterImport* import, ExternalKind expected_kind); - Result CheckImportLimits(const Limits* declared_limits, - const Limits* actual_limits); - Result CheckHasMemory(Opcode opcode); - Result CheckAlign(uint32_t alignment_log2, Address natural_alignment); - - Result AppendExport(InterpreterModule* module, - ExternalKind kind, - Index item_index, - StringSlice name); + wabt::Result EmitDataAt(IstreamOffset offset, + const void* data, + IstreamOffset size); + wabt::Result EmitData(const void* data, IstreamOffset size); + wabt::Result EmitOpcode(wabt::Opcode opcode); + wabt::Result EmitOpcode(interpreter::Opcode opcode); + wabt::Result EmitI8(uint8_t value); + wabt::Result EmitI32(uint32_t value); + wabt::Result EmitI64(uint64_t value); + wabt::Result EmitI32At(IstreamOffset offset, uint32_t value); + wabt::Result EmitDropKeep(uint32_t drop, uint8_t keep); + wabt::Result AppendFixup(IstreamOffsetVectorVector* fixups_vector, + Index index); + wabt::Result EmitBrOffset(Index depth, IstreamOffset offset); + wabt::Result GetBrDropKeepCount(Index depth, + Index* out_drop_count, + Index* out_keep_count); + wabt::Result GetReturnDropKeepCount(Index* out_drop_count, + Index* out_keep_count); + wabt::Result EmitBr(Index depth, Index drop_count, Index keep_count); + wabt::Result EmitBrTableOffset(Index depth); + wabt::Result FixupTopLabel(); + wabt::Result EmitFuncOffset(DefinedFunc* func, Index func_index); + + wabt::Result CheckLocal(Index local_index); + wabt::Result CheckGlobal(Index global_index); + wabt::Result CheckImportKind(Import* import, ExternalKind expected_kind); + wabt::Result CheckImportLimits(const Limits* declared_limits, + const Limits* actual_limits); + wabt::Result CheckHasMemory(wabt::Opcode opcode); + wabt::Result CheckAlign(uint32_t alignment_log2, Address natural_alignment); + + wabt::Result AppendExport(Module* module, + ExternalKind kind, + Index item_index, + StringSlice name); PrintErrorCallback MakePrintErrorCallback(); static void OnHostImportPrintError(const char* msg, void* user_data); BinaryErrorHandler* error_handler = nullptr; - InterpreterEnvironment* env = nullptr; - DefinedInterpreterModule* module = nullptr; - DefinedInterpreterFunc* current_func = nullptr; + Environment* env = nullptr; + DefinedModule* module = nullptr; + DefinedFunc* current_func = nullptr; TypeCheckerErrorHandler tc_error_handler; TypeChecker typechecker; std::vector