From 618f1e7271cff4f4c16ff750345b7f23c9785a4a Mon Sep 17 00:00:00 2001 From: guzibei Date: Sun, 26 Jan 2025 22:33:10 +0800 Subject: [PATCH 01/13] optimize in for low memory device + add step OnSkipFunctionBodyExpr when read with skip_function_bodies + add virtual op code OpCodeRaw, when read with skip_function_bodies, the virtual struct will save byte code in a uint8 array, and will write directly on output + add macro wabt_expr_make_unique, for override operator new + add MemoryAllocStream can output byte code in a allocated buffer --- include/wabt/binary-reader-logging.h | 1 + include/wabt/binary-reader-nop.h | 1 + include/wabt/binary-reader.h | 1 + include/wabt/expr-visitor.h | 4 + include/wabt/ir.h | 15 ++- include/wabt/stream.h | 33 ++++++ src/binary-reader-ir.cc | 158 +++++++++++++++------------ src/binary-reader-logging.cc | 8 ++ src/binary-writer.cc | 12 ++ src/c-writer.cc | 2 + src/expr-visitor.cc | 25 +++++ src/interp/binary-reader-interp.cc | 6 + src/ir-util.cc | 2 + src/stream.cc | 68 ++++++++++++ src/validator.cc | 7 ++ src/wat-writer.cc | 7 ++ 16 files changed, 281 insertions(+), 69 deletions(-) diff --git a/include/wabt/binary-reader-logging.h b/include/wabt/binary-reader-logging.h index 4a031f74bb..a9f6828ac4 100644 --- a/include/wabt/binary-reader-logging.h +++ b/include/wabt/binary-reader-logging.h @@ -188,6 +188,7 @@ class BinaryReaderLogging : public BinaryReaderDelegate { Result OnDropExpr() override; Result OnElseExpr() override; Result OnEndExpr() override; + Result OnSkipFunctionBodyExpr(std::vector& opcode_buffer) override; Result OnF32ConstExpr(uint32_t value_bits) override; Result OnF64ConstExpr(uint64_t value_bits) override; Result OnV128ConstExpr(v128 value_bits) override; diff --git a/include/wabt/binary-reader-nop.h b/include/wabt/binary-reader-nop.h index fdb7d14a41..0bd0c1793e 100644 --- a/include/wabt/binary-reader-nop.h +++ b/include/wabt/binary-reader-nop.h @@ -259,6 +259,7 @@ class BinaryReaderNop : public BinaryReaderDelegate { Result OnDropExpr() override { return Result::Ok; } Result OnElseExpr() override { return Result::Ok; } Result OnEndExpr() override { return Result::Ok; } + Result OnSkipFunctionBodyExpr(std::vector& opcode_buffer) override { return Result::Ok; } Result OnF32ConstExpr(uint32_t value_bits) override { return Result::Ok; } Result OnF64ConstExpr(uint64_t value_bits) override { return Result::Ok; } Result OnV128ConstExpr(v128 value_bits) override { return Result::Ok; } diff --git a/include/wabt/binary-reader.h b/include/wabt/binary-reader.h index 94b19c1100..5d55461f89 100644 --- a/include/wabt/binary-reader.h +++ b/include/wabt/binary-reader.h @@ -264,6 +264,7 @@ class BinaryReaderDelegate { virtual Result OnDropExpr() = 0; virtual Result OnElseExpr() = 0; virtual Result OnEndExpr() = 0; + virtual Result OnSkipFunctionBodyExpr(std::vector& opcode_buffer) = 0; virtual Result OnF32ConstExpr(uint32_t value_bits) = 0; virtual Result OnF64ConstExpr(uint64_t value_bits) = 0; virtual Result OnV128ConstExpr(v128 value_bits) = 0; diff --git a/include/wabt/expr-visitor.h b/include/wabt/expr-visitor.h index 5be7879391..7348db076b 100644 --- a/include/wabt/expr-visitor.h +++ b/include/wabt/expr-visitor.h @@ -43,6 +43,7 @@ class ExprVisitor { Try, TryTable, Catch, + OpcodeRaw }; Result HandleDefaultState(Expr*); @@ -141,6 +142,7 @@ class ExprVisitor::Delegate { virtual Result OnSimdShuffleOpExpr(SimdShuffleOpExpr*) = 0; virtual Result OnLoadSplatExpr(LoadSplatExpr*) = 0; virtual Result OnLoadZeroExpr(LoadZeroExpr*) = 0; + virtual Result OnOpcodeRawExpr(OpcodeRawExpr*) = 0; }; class ExprVisitor::DelegateNop : public ExprVisitor::Delegate { @@ -222,6 +224,8 @@ class ExprVisitor::DelegateNop : public ExprVisitor::Delegate { Result OnSimdShuffleOpExpr(SimdShuffleOpExpr*) override { return Result::Ok; } Result OnLoadSplatExpr(LoadSplatExpr*) override { return Result::Ok; } Result OnLoadZeroExpr(LoadZeroExpr*) override { return Result::Ok; } + Result OnOpcodeRawExpr(OpcodeRawExpr*) override { return Result::Ok; } + }; } // namespace wabt diff --git a/include/wabt/ir.h b/include/wabt/ir.h index 25264bcd91..ac8c5b0ce4 100644 --- a/include/wabt/ir.h +++ b/include/wabt/ir.h @@ -429,7 +429,8 @@ enum class ExprType { Unreachable, First = AtomicLoad, - Last = Unreachable + Last = Unreachable, + OpCodeRaw = Last + 1, // virual type }; const char* GetExprTypeName(ExprType type); @@ -839,6 +840,18 @@ class AtomicFenceExpr : public ExprMixin { uint32_t consistency_model; }; +class OpcodeRawExpr : public ExprMixin { + public: + explicit OpcodeRawExpr(std::vector&& in_opcode_buffer, FuncSignature& in_func_sig, const Location& loc = Location()) + : ExprMixin(loc), opcode_buffer(std::move(in_opcode_buffer)), is_extracted(false), func_sig(&in_func_sig) { + } + + std::vector opcode_buffer; + bool is_extracted; + ExprList extracted_exprs; + const FuncSignature* func_sig; +}; + struct Tag { explicit Tag(std::string_view name) : name(name) {} diff --git a/include/wabt/stream.h b/include/wabt/stream.h index 67f50975eb..9dded608f7 100644 --- a/include/wabt/stream.h +++ b/include/wabt/stream.h @@ -195,6 +195,39 @@ class MemoryStream : public Stream { std::unique_ptr buf_; }; +class MemoryAllocStream : public wabt::Stream { + public: + typedef void* (*wabt_realloc)(void * __ptr, size_t __size); + + + MemoryAllocStream(uint8_t* _output_data = nullptr, size_t _total_size = 0, wabt_realloc _realloc_fn = nullptr) + : output_data(_output_data), total_size(_total_size), used_size(0) { + if (_realloc_fn) { + realloc_fn = _realloc_fn; + } else { + realloc_fn = std::realloc; + } + } + + wabt::Result WriteDataImpl(size_t dst_offset, + const void* src, + size_t size); + + wabt::Result MoveDataImpl(size_t dst_offset, + size_t src_offset, + size_t size); + + wabt::Result TruncateImpl(size_t size); + inline size_t GetSize() const { return used_size; } + inline uint8_t* GetData() { return output_data; } + + private: + uint8_t* output_data; + size_t total_size; + size_t used_size; + wabt_realloc realloc_fn; +}; + class FileStream : public Stream { public: WABT_DISALLOW_COPY_AND_ASSIGN(FileStream); diff --git a/src/binary-reader-ir.cc b/src/binary-reader-ir.cc index 2029b0f7c4..4b666d6473 100644 --- a/src/binary-reader-ir.cc +++ b/src/binary-reader-ir.cc @@ -31,6 +31,19 @@ namespace wabt { +#ifdef WABT_OVERRIDE_ALLOC_EXPR +template +inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 typename std::__unique_if<_Tp>::__unique_single +wabt_expr_make_unique(_Args&&... __args) { + // static_assert(sizeof(_Tp) <= 128); + return std::unique_ptr<_Tp>(new ((wabt::Module*)(nullptr))_Tp(std::forward<_Args>(__args)...)); +} +extern void* operator new(size_t size, wabt::Module* m); +#else +#define wabt_expr_make_unique std::make_unique +#endif + + namespace { struct LabelNode { @@ -216,6 +229,7 @@ class BinaryReaderIR : public BinaryReaderNop { Result OnDropExpr() override; Result OnElseExpr() override; Result OnEndExpr() override; + Result OnSkipFunctionBodyExpr(std::vector& opcode_buffer) override; Result OnF32ConstExpr(uint32_t value_bits) override; Result OnF64ConstExpr(uint64_t value_bits) override; Result OnV128ConstExpr(v128 value_bits) override; @@ -829,7 +843,7 @@ Result BinaryReaderIR::OnAtomicLoadExpr(Opcode opcode, Index memidx, Address alignment_log2, Address offset) { - return AppendExpr(std::make_unique( + return AppendExpr(wabt_expr_make_unique( opcode, Var(memidx, GetLocation()), 1ull << alignment_log2, offset)); } @@ -837,7 +851,7 @@ Result BinaryReaderIR::OnAtomicStoreExpr(Opcode opcode, Index memidx, Address alignment_log2, Address offset) { - return AppendExpr(std::make_unique( + return AppendExpr(wabt_expr_make_unique( opcode, Var(memidx, GetLocation()), 1ull << alignment_log2, offset)); } @@ -845,7 +859,7 @@ Result BinaryReaderIR::OnAtomicRmwExpr(Opcode opcode, Index memidx, Address alignment_log2, Address offset) { - return AppendExpr(std::make_unique( + return AppendExpr(wabt_expr_make_unique( opcode, Var(memidx, GetLocation()), 1ull << alignment_log2, offset)); } @@ -853,7 +867,7 @@ Result BinaryReaderIR::OnAtomicRmwCmpxchgExpr(Opcode opcode, Index memidx, Address alignment_log2, Address offset) { - return AppendExpr(std::make_unique( + return AppendExpr(wabt_expr_make_unique( opcode, Var(memidx, GetLocation()), 1ull << alignment_log2, offset)); } @@ -861,28 +875,28 @@ Result BinaryReaderIR::OnAtomicWaitExpr(Opcode opcode, Index memidx, Address alignment_log2, Address offset) { - return AppendExpr(std::make_unique( + return AppendExpr(wabt_expr_make_unique( opcode, Var(memidx, GetLocation()), 1ull << alignment_log2, offset)); } Result BinaryReaderIR::OnAtomicFenceExpr(uint32_t consistency_model) { - return AppendExpr(std::make_unique(consistency_model)); + return AppendExpr(wabt_expr_make_unique(consistency_model)); } Result BinaryReaderIR::OnAtomicNotifyExpr(Opcode opcode, Index memidx, Address alignment_log2, Address offset) { - return AppendExpr(std::make_unique( + return AppendExpr(wabt_expr_make_unique( opcode, Var(memidx, GetLocation()), 1ull << alignment_log2, offset)); } Result BinaryReaderIR::OnBinaryExpr(Opcode opcode) { - return AppendExpr(std::make_unique(opcode)); + return AppendExpr(wabt_expr_make_unique(opcode)); } Result BinaryReaderIR::OnBlockExpr(Type sig_type) { - auto expr = std::make_unique(); + auto expr = wabt_expr_make_unique(); SetBlockDeclaration(&expr->block.decl, sig_type); ExprList* expr_list = &expr->block.exprs; CHECK_RESULT(AppendExpr(std::move(expr))); @@ -890,17 +904,17 @@ Result BinaryReaderIR::OnBlockExpr(Type sig_type) { } Result BinaryReaderIR::OnBrExpr(Index depth) { - return AppendExpr(std::make_unique(Var(depth, GetLocation()))); + return AppendExpr(wabt_expr_make_unique(Var(depth, GetLocation()))); } Result BinaryReaderIR::OnBrIfExpr(Index depth) { - return AppendExpr(std::make_unique(Var(depth, GetLocation()))); + return AppendExpr(wabt_expr_make_unique(Var(depth, GetLocation()))); } Result BinaryReaderIR::OnBrTableExpr(Index num_targets, Index* target_depths, Index default_target_depth) { - auto expr = std::make_unique(); + auto expr = wabt_expr_make_unique(); expr->default_target = Var(default_target_depth, GetLocation()); expr->targets.resize(num_targets); for (Index i = 0; i < num_targets; ++i) { @@ -910,18 +924,18 @@ Result BinaryReaderIR::OnBrTableExpr(Index num_targets, } Result BinaryReaderIR::OnCallExpr(Index func_index) { - return AppendExpr(std::make_unique(Var(func_index, GetLocation()))); + return AppendExpr(wabt_expr_make_unique(Var(func_index, GetLocation()))); } Result BinaryReaderIR::OnCallIndirectExpr(Index sig_index, Index table_index) { - auto expr = std::make_unique(); + auto expr = wabt_expr_make_unique(); SetFuncDeclaration(&expr->decl, Var(sig_index, GetLocation())); expr->table = Var(table_index, GetLocation()); return AppendExpr(std::move(expr)); } Result BinaryReaderIR::OnCallRefExpr() { - return AppendExpr(std::make_unique()); + return AppendExpr(wabt_expr_make_unique()); } Result BinaryReaderIR::OnReturnCallExpr(Index func_index) { @@ -931,7 +945,7 @@ Result BinaryReaderIR::OnReturnCallExpr(Index func_index) { current_func_->features_used.tailcall = true; } return AppendExpr( - std::make_unique(Var(func_index, GetLocation()))); + wabt_expr_make_unique(Var(func_index, GetLocation()))); } Result BinaryReaderIR::OnReturnCallIndirectExpr(Index sig_index, @@ -941,7 +955,7 @@ Result BinaryReaderIR::OnReturnCallIndirectExpr(Index sig_index, // expression (outside a function) current_func_->features_used.tailcall = true; } - auto expr = std::make_unique(); + auto expr = wabt_expr_make_unique(); SetFuncDeclaration(&expr->decl, Var(sig_index, GetLocation())); expr->table = Var(table_index, GetLocation()); FuncType* type = module_->GetFuncType(Var(sig_index, GetLocation())); @@ -952,15 +966,15 @@ Result BinaryReaderIR::OnReturnCallIndirectExpr(Index sig_index, } Result BinaryReaderIR::OnCompareExpr(Opcode opcode) { - return AppendExpr(std::make_unique(opcode)); + return AppendExpr(wabt_expr_make_unique(opcode)); } Result BinaryReaderIR::OnConvertExpr(Opcode opcode) { - return AppendExpr(std::make_unique(opcode)); + return AppendExpr(wabt_expr_make_unique(opcode)); } Result BinaryReaderIR::OnDropExpr() { - return AppendExpr(std::make_unique()); + return AppendExpr(wabt_expr_make_unique()); } Result BinaryReaderIR::OnElseExpr() { @@ -1016,43 +1030,51 @@ Result BinaryReaderIR::OnEndExpr() { return PopLabel(); } +Result BinaryReaderIR::OnSkipFunctionBodyExpr(std::vector& opcode_buffer) { + Result result_append = AppendExpr( + wabt_expr_make_unique(std::move(opcode_buffer), current_func_->decl.sig, GetLocation())); + Result result_pop = PopLabel(); + + return (result_append == Result::Ok && result_pop == Result::Ok) ? Result::Ok : Result::Error; +} + Result BinaryReaderIR::OnF32ConstExpr(uint32_t value_bits) { return AppendExpr( - std::make_unique(Const::F32(value_bits, GetLocation()))); + wabt_expr_make_unique(Const::F32(value_bits, GetLocation()))); } Result BinaryReaderIR::OnF64ConstExpr(uint64_t value_bits) { return AppendExpr( - std::make_unique(Const::F64(value_bits, GetLocation()))); + wabt_expr_make_unique(Const::F64(value_bits, GetLocation()))); } Result BinaryReaderIR::OnV128ConstExpr(v128 value_bits) { return AppendExpr( - std::make_unique(Const::V128(value_bits, GetLocation()))); + wabt_expr_make_unique(Const::V128(value_bits, GetLocation()))); } Result BinaryReaderIR::OnGlobalGetExpr(Index global_index) { return AppendExpr( - std::make_unique(Var(global_index, GetLocation()))); + wabt_expr_make_unique(Var(global_index, GetLocation()))); } Result BinaryReaderIR::OnLocalGetExpr(Index local_index) { return AppendExpr( - std::make_unique(Var(local_index, GetLocation()))); + wabt_expr_make_unique(Var(local_index, GetLocation()))); } Result BinaryReaderIR::OnI32ConstExpr(uint32_t value) { return AppendExpr( - std::make_unique(Const::I32(value, GetLocation()))); + wabt_expr_make_unique(Const::I32(value, GetLocation()))); } Result BinaryReaderIR::OnI64ConstExpr(uint64_t value) { return AppendExpr( - std::make_unique(Const::I64(value, GetLocation()))); + wabt_expr_make_unique(Const::I64(value, GetLocation()))); } Result BinaryReaderIR::OnIfExpr(Type sig_type) { - auto expr = std::make_unique(); + auto expr = wabt_expr_make_unique(); SetBlockDeclaration(&expr->true_.decl, sig_type); ExprList* expr_list = &expr->true_.exprs; CHECK_RESULT(AppendExpr(std::move(expr))); @@ -1063,12 +1085,12 @@ Result BinaryReaderIR::OnLoadExpr(Opcode opcode, Index memidx, Address alignment_log2, Address offset) { - return AppendExpr(std::make_unique( + return AppendExpr(wabt_expr_make_unique( opcode, Var(memidx, GetLocation()), 1ull << alignment_log2, offset)); } Result BinaryReaderIR::OnLoopExpr(Type sig_type) { - auto expr = std::make_unique(); + auto expr = wabt_expr_make_unique(); SetBlockDeclaration(&expr->block.decl, sig_type); ExprList* expr_list = &expr->block.exprs; CHECK_RESULT(AppendExpr(std::move(expr))); @@ -1076,129 +1098,129 @@ Result BinaryReaderIR::OnLoopExpr(Type sig_type) { } Result BinaryReaderIR::OnMemoryCopyExpr(Index destmemidx, Index srcmemidx) { - return AppendExpr(std::make_unique( + return AppendExpr(wabt_expr_make_unique( Var(destmemidx, GetLocation()), Var(srcmemidx, GetLocation()))); } Result BinaryReaderIR::OnDataDropExpr(Index segment) { return AppendExpr( - std::make_unique(Var(segment, GetLocation()))); + wabt_expr_make_unique(Var(segment, GetLocation()))); } Result BinaryReaderIR::OnMemoryFillExpr(Index memidx) { return AppendExpr( - std::make_unique(Var(memidx, GetLocation()))); + wabt_expr_make_unique(Var(memidx, GetLocation()))); } Result BinaryReaderIR::OnMemoryGrowExpr(Index memidx) { return AppendExpr( - std::make_unique(Var(memidx, GetLocation()))); + wabt_expr_make_unique(Var(memidx, GetLocation()))); } Result BinaryReaderIR::OnMemoryInitExpr(Index segment, Index memidx) { - return AppendExpr(std::make_unique( + return AppendExpr(wabt_expr_make_unique( Var(segment, GetLocation()), Var(memidx, GetLocation()))); } Result BinaryReaderIR::OnMemorySizeExpr(Index memidx) { return AppendExpr( - std::make_unique(Var(memidx, GetLocation()))); + wabt_expr_make_unique(Var(memidx, GetLocation()))); } Result BinaryReaderIR::OnTableCopyExpr(Index dst_index, Index src_index) { - return AppendExpr(std::make_unique( + return AppendExpr(wabt_expr_make_unique( Var(dst_index, GetLocation()), Var(src_index, GetLocation()))); } Result BinaryReaderIR::OnElemDropExpr(Index segment) { return AppendExpr( - std::make_unique(Var(segment, GetLocation()))); + wabt_expr_make_unique(Var(segment, GetLocation()))); } Result BinaryReaderIR::OnTableInitExpr(Index segment, Index table_index) { - return AppendExpr(std::make_unique( + return AppendExpr(wabt_expr_make_unique( Var(segment, GetLocation()), Var(table_index, GetLocation()))); } Result BinaryReaderIR::OnTableGetExpr(Index table_index) { return AppendExpr( - std::make_unique(Var(table_index, GetLocation()))); + wabt_expr_make_unique(Var(table_index, GetLocation()))); } Result BinaryReaderIR::OnTableSetExpr(Index table_index) { return AppendExpr( - std::make_unique(Var(table_index, GetLocation()))); + wabt_expr_make_unique(Var(table_index, GetLocation()))); } Result BinaryReaderIR::OnTableGrowExpr(Index table_index) { return AppendExpr( - std::make_unique(Var(table_index, GetLocation()))); + wabt_expr_make_unique(Var(table_index, GetLocation()))); } Result BinaryReaderIR::OnTableSizeExpr(Index table_index) { return AppendExpr( - std::make_unique(Var(table_index, GetLocation()))); + wabt_expr_make_unique(Var(table_index, GetLocation()))); } Result BinaryReaderIR::OnTableFillExpr(Index table_index) { return AppendExpr( - std::make_unique(Var(table_index, GetLocation()))); + wabt_expr_make_unique(Var(table_index, GetLocation()))); } Result BinaryReaderIR::OnRefFuncExpr(Index func_index) { module_->used_func_refs.insert(func_index); return AppendExpr( - std::make_unique(Var(func_index, GetLocation()))); + wabt_expr_make_unique(Var(func_index, GetLocation()))); } Result BinaryReaderIR::OnRefNullExpr(Type type) { module_->features_used.exceptions |= (type == Type::ExnRef); - return AppendExpr(std::make_unique(type)); + return AppendExpr(wabt_expr_make_unique(type)); } Result BinaryReaderIR::OnRefIsNullExpr() { - return AppendExpr(std::make_unique()); + return AppendExpr(wabt_expr_make_unique()); } Result BinaryReaderIR::OnNopExpr() { - return AppendExpr(std::make_unique()); + return AppendExpr(wabt_expr_make_unique()); } Result BinaryReaderIR::OnRethrowExpr(Index depth) { - return AppendExpr(std::make_unique(Var(depth, GetLocation()))); + return AppendExpr(wabt_expr_make_unique(Var(depth, GetLocation()))); } Result BinaryReaderIR::OnReturnExpr() { - return AppendExpr(std::make_unique()); + return AppendExpr(wabt_expr_make_unique()); } Result BinaryReaderIR::OnSelectExpr(Index result_count, Type* result_types) { TypeVector results; results.assign(result_types, result_types + result_count); - return AppendExpr(std::make_unique(results)); + return AppendExpr(wabt_expr_make_unique(results)); } Result BinaryReaderIR::OnGlobalSetExpr(Index global_index) { return AppendExpr( - std::make_unique(Var(global_index, GetLocation()))); + wabt_expr_make_unique(Var(global_index, GetLocation()))); } Result BinaryReaderIR::OnLocalSetExpr(Index local_index) { return AppendExpr( - std::make_unique(Var(local_index, GetLocation()))); + wabt_expr_make_unique(Var(local_index, GetLocation()))); } Result BinaryReaderIR::OnStoreExpr(Opcode opcode, Index memidx, Address alignment_log2, Address offset) { - return AppendExpr(std::make_unique( + return AppendExpr(wabt_expr_make_unique( opcode, Var(memidx, GetLocation()), 1ull << alignment_log2, offset)); } Result BinaryReaderIR::OnThrowExpr(Index tag_index) { module_->features_used.exceptions = true; - return AppendExpr(std::make_unique(Var(tag_index, GetLocation()))); + return AppendExpr(wabt_expr_make_unique(Var(tag_index, GetLocation()))); } Result BinaryReaderIR::OnThrowRefExpr() { @@ -1208,11 +1230,11 @@ Result BinaryReaderIR::OnThrowRefExpr() { Result BinaryReaderIR::OnLocalTeeExpr(Index local_index) { return AppendExpr( - std::make_unique(Var(local_index, GetLocation()))); + wabt_expr_make_unique(Var(local_index, GetLocation()))); } Result BinaryReaderIR::OnTryExpr(Type sig_type) { - auto expr_ptr = std::make_unique(); + auto expr_ptr = wabt_expr_make_unique(); // Save expr so it can be used below, after expr_ptr has been moved. TryExpr* expr = expr_ptr.get(); ExprList* expr_list = &expr->block.exprs; @@ -1261,7 +1283,7 @@ Result BinaryReaderIR::OnCatchAllExpr() { Result BinaryReaderIR::OnTryTableExpr(Type sig_type, const CatchClauseVector& catches) { - auto expr_ptr = std::make_unique(); + auto expr_ptr = wabt_expr_make_unique(); TryTableExpr* expr = expr_ptr.get(); expr->catches.reserve(catches.size()); SetBlockDeclaration(&expr->block.decl, sig_type); @@ -1305,15 +1327,15 @@ Result BinaryReaderIR::OnDelegateExpr(Index depth) { } Result BinaryReaderIR::OnUnaryExpr(Opcode opcode) { - return AppendExpr(std::make_unique(opcode)); + return AppendExpr(wabt_expr_make_unique(opcode)); } Result BinaryReaderIR::OnTernaryExpr(Opcode opcode) { - return AppendExpr(std::make_unique(opcode)); + return AppendExpr(wabt_expr_make_unique(opcode)); } Result BinaryReaderIR::OnUnreachableExpr() { - return AppendExpr(std::make_unique()); + return AppendExpr(wabt_expr_make_unique()); } Result BinaryReaderIR::EndFunctionBody(Index index) { @@ -1326,7 +1348,7 @@ Result BinaryReaderIR::EndFunctionBody(Index index) { } Result BinaryReaderIR::OnSimdLaneOpExpr(Opcode opcode, uint64_t value) { - return AppendExpr(std::make_unique(opcode, value)); + return AppendExpr(wabt_expr_make_unique(opcode, value)); } Result BinaryReaderIR::OnSimdLoadLaneExpr(Opcode opcode, @@ -1334,7 +1356,7 @@ Result BinaryReaderIR::OnSimdLoadLaneExpr(Opcode opcode, Address alignment_log2, Address offset, uint64_t value) { - return AppendExpr(std::make_unique( + return AppendExpr(wabt_expr_make_unique( opcode, Var(memidx, GetLocation()), 1ull << alignment_log2, offset, value)); } @@ -1344,20 +1366,20 @@ Result BinaryReaderIR::OnSimdStoreLaneExpr(Opcode opcode, Address alignment_log2, Address offset, uint64_t value) { - return AppendExpr(std::make_unique( + return AppendExpr(wabt_expr_make_unique( opcode, Var(memidx, GetLocation()), 1ull << alignment_log2, offset, value)); } Result BinaryReaderIR::OnSimdShuffleOpExpr(Opcode opcode, v128 value) { - return AppendExpr(std::make_unique(opcode, value)); + return AppendExpr(wabt_expr_make_unique(opcode, value)); } Result BinaryReaderIR::OnLoadSplatExpr(Opcode opcode, Index memidx, Address alignment_log2, Address offset) { - return AppendExpr(std::make_unique( + return AppendExpr(wabt_expr_make_unique( opcode, Var(memidx, GetLocation()), 1ull << alignment_log2, offset)); } @@ -1365,7 +1387,7 @@ Result BinaryReaderIR::OnLoadZeroExpr(Opcode opcode, Index memidx, Address alignment_log2, Address offset) { - return AppendExpr(std::make_unique( + return AppendExpr(wabt_expr_make_unique( opcode, Var(memidx, GetLocation()), 1ull << alignment_log2, offset)); } @@ -1712,7 +1734,7 @@ Result BinaryReaderIR::OnCodeMetadata(Offset offset, Address size) { std::vector data_(static_cast(data), static_cast(data) + size); - auto meta = std::make_unique(current_metadata_name_, + auto meta = wabt_expr_make_unique(current_metadata_name_, std::move(data_)); meta->loc.offset = offset; code_metadata_queue_.push_metadata(std::move(meta)); diff --git a/src/binary-reader-logging.cc b/src/binary-reader-logging.cc index 0c13622576..37f2592acd 100644 --- a/src/binary-reader-logging.cc +++ b/src/binary-reader-logging.cc @@ -781,6 +781,12 @@ Result BinaryReaderLogging::OnGenericCustomSection(std::string_view name, return reader_->name(opcode, memidx, alignment_log2, offset, value); \ } +#define DEFINE_SKIP(name) \ + Result BinaryReaderLogging::name(std::vector& opcode_buffer) { \ + LOGF(#name " length: %lu""\n", opcode_buffer.size()); \ + return reader_->name(opcode_buffer); \ + } + #define DEFINE0(name) \ Result BinaryReaderLogging::name() { \ LOGF(#name "\n"); \ @@ -851,6 +857,8 @@ DEFINE_INDEX_DESC(OnDelegateExpr, "depth"); DEFINE0(OnDropExpr) DEFINE0(OnElseExpr) DEFINE0(OnEndExpr) +DEFINE_SKIP(OnSkipFunctionBodyExpr) + DEFINE_INDEX_DESC(OnGlobalGetExpr, "index") DEFINE_INDEX_DESC(OnGlobalSetExpr, "index") DEFINE_LOAD_STORE_OPCODE(OnLoadExpr); diff --git a/src/binary-writer.cc b/src/binary-writer.cc index 600154b941..382e9334f5 100644 --- a/src/binary-writer.cc +++ b/src/binary-writer.cc @@ -1150,6 +1150,18 @@ void BinaryWriter::WriteExpr(const Func* func, const Expr* expr) { a.entries.emplace_back(code_offset, meta_expr->data); break; } + case ExprType::OpCodeRaw: { + auto* opcode_raw_expr = cast(expr); + if (opcode_raw_expr->is_extracted) { + WriteExprList(func, opcode_raw_expr->extracted_exprs); + } else { + auto& opcode_buffer = opcode_raw_expr->opcode_buffer; + // Opcode::End 0x0b + assert(opcode_buffer[opcode_buffer.size() - 1] == 0x0b); + stream_->WriteData(opcode_buffer.data(), opcode_buffer.size() - 1); + } + } + } } diff --git a/src/c-writer.cc b/src/c-writer.cc index 0e96d5e892..8d939e11fd 100644 --- a/src/c-writer.cc +++ b/src/c-writer.cc @@ -4267,6 +4267,8 @@ void CWriter::Write(const ExprList& exprs) { case ExprType::AtomicWait: case ExprType::AtomicNotify: case ExprType::CallRef: + case ExprType::OpCodeRaw: + UNIMPLEMENTED("..."); break; } diff --git a/src/expr-visitor.cc b/src/expr-visitor.cc index 217d45cc01..97177290ba 100644 --- a/src/expr-visitor.cc +++ b/src/expr-visitor.cc @@ -149,6 +149,20 @@ Result ExprVisitor::VisitExpr(Expr* root_expr) { } break; } + case State::OpcodeRaw: { + auto opcode_raw_expr = cast(expr); + // ERROR_UNLESS(opcode_raw_expr->is_extracted, "opcode_raw could not compile when in visiting") + if (opcode_raw_expr->is_extracted) { + auto& iter = expr_iter_stack_.back(); + if (iter != opcode_raw_expr->extracted_exprs.end()) { + PushDefault(&*iter++); + } else { + PopExprlist(); + } + } + + break; + } } } @@ -449,6 +463,17 @@ Result ExprVisitor::HandleDefaultState(Expr* expr) { case ExprType::Unreachable: CHECK_RESULT(delegate_->OnUnreachableExpr(cast(expr))); break; + case ExprType::OpCodeRaw: { + auto opcode_raw_expr = cast(expr); + CHECK_RESULT(delegate_->OnOpcodeRawExpr(opcode_raw_expr)); + + if (opcode_raw_expr->is_extracted) { + PushExprlist(State::OpcodeRaw, expr, opcode_raw_expr->extracted_exprs); + } else { + // TODO extract or just skip + } + break; + } } return Result::Ok; diff --git a/src/interp/binary-reader-interp.cc b/src/interp/binary-reader-interp.cc index 7e9b8d5e14..cb15b616ef 100644 --- a/src/interp/binary-reader-interp.cc +++ b/src/interp/binary-reader-interp.cc @@ -197,6 +197,7 @@ class BinaryReaderInterp : public BinaryReaderNop { Result OnDropExpr() override; Result OnElseExpr() override; Result OnEndExpr() override; + Result OnSkipFunctionBodyExpr(std::vector& opcode_buffer) override; Result OnF32ConstExpr(uint32_t value_bits) override; Result OnF64ConstExpr(uint64_t value_bits) override; Result OnV128ConstExpr(v128 value_bits) override; @@ -1088,6 +1089,11 @@ Result BinaryReaderInterp::OnEndExpr() { return Result::Ok; } +Result BinaryReaderInterp::OnSkipFunctionBodyExpr(std::vector& opcode_buffer) { + PopLabel(); + return Result::Ok; +} + Result BinaryReaderInterp::OnBrExpr(Index depth) { Index drop_count, keep_count, catch_drop_count; CHECK_RESULT(GetBrDropKeepCount(depth, &drop_count, &keep_count)); diff --git a/src/ir-util.cc b/src/ir-util.cc index 5266a26457..dc5f26b203 100644 --- a/src/ir-util.cc +++ b/src/ir-util.cc @@ -272,6 +272,8 @@ ModuleContext::Arities ModuleContext::GetExprArity(const Expr& expr) const { case ExprType::SimdShuffleOp: return {2, 1}; + case ExprType::OpCodeRaw: + return {0, cast(&expr)->func_sig->GetNumResults()}; } WABT_UNREACHABLE; diff --git a/src/stream.cc b/src/stream.cc index 5f27e01b2e..43d20351ea 100644 --- a/src/stream.cc +++ b/src/stream.cc @@ -229,6 +229,74 @@ Result MemoryStream::TruncateImpl(size_t size) { return Result::Ok; } + +Result MemoryAllocStream::WriteDataImpl(size_t dst_offset, + const void* src, + size_t size) { + if (size == 0) { + return wabt::Result::Ok; + } + size_t end = dst_offset + size; + bool needRealloc = false; + while (end > total_size) { + total_size = total_size << 1; + needRealloc = true; + } + if (needRealloc) { + output_data = static_cast(realloc_fn(output_data, total_size)); + } + + if (end > used_size) { + used_size = end; + } + + memcpy(output_data + dst_offset, src, size); + return wabt::Result::Ok; + +} + +Result MemoryAllocStream::MoveDataImpl(size_t dst_offset, + size_t src_offset, + size_t size) { + if (size == 0) { + return wabt::Result::Ok; + } + size_t src_end = src_offset + size; + size_t dst_end = dst_offset + size; + size_t end = src_end > dst_end ? src_end : dst_end; + + bool needRealloc = false; + while (end > total_size) { + total_size = total_size << 1; + needRealloc = true; + } + if (needRealloc) { + output_data = static_cast(realloc_fn(output_data, total_size)); + } + + if (end > used_size) { + used_size = end; + } + + uint8_t* dst = output_data + dst_offset; + uint8_t* src = output_data + src_offset; + memmove(dst, src, size); + return wabt::Result::Ok; + +} + +Result MemoryAllocStream::TruncateImpl(size_t size) { + if (size > used_size) { + return wabt::Result::Error; + } + used_size = size; + + // will not truncate size, we can truncate it at end + // output_data = (uint8_t*)realloc_fn(output_data, size); + // total_size = size; + return wabt::Result::Ok; +} + FileStream::FileStream(std::string_view filename, Stream* log_stream) : Stream(log_stream), file_(nullptr), offset_(0), should_close_(false) { std::string filename_str(filename); diff --git a/src/validator.cc b/src/validator.cc index 8ccfa4072f..7881f1d45c 100644 --- a/src/validator.cc +++ b/src/validator.cc @@ -166,6 +166,7 @@ class Validator : public ExprVisitor::Delegate { Result OnSimdShuffleOpExpr(SimdShuffleOpExpr*) override; Result OnLoadSplatExpr(LoadSplatExpr*) override; Result OnLoadZeroExpr(LoadZeroExpr*) override; + Result OnOpcodeRawExpr(OpcodeRawExpr* expr) override; private: Type GetDeclarationType(const FuncDeclaration&); @@ -688,6 +689,12 @@ Result Validator::OnLoadZeroExpr(LoadZeroExpr* expr) { return Result::Ok; } +Result Validator::OnOpcodeRawExpr(OpcodeRawExpr* expr) { + // data read from original wasm file, so just return OK; + return Result::Ok; +} + + Validator::Validator(Errors* errors, const Module* module, const ValidateOptions& options) diff --git a/src/wat-writer.cc b/src/wat-writer.cc index f19e3c3c86..33c874ee73 100644 --- a/src/wat-writer.cc +++ b/src/wat-writer.cc @@ -622,6 +622,7 @@ class WatWriter::ExprVisitorDelegate : public ExprVisitor::Delegate { Result OnSimdShuffleOpExpr(SimdShuffleOpExpr*) override; Result OnLoadSplatExpr(LoadSplatExpr*) override; Result OnLoadZeroExpr(LoadZeroExpr*) override; + Result OnOpcodeRawExpr(OpcodeRawExpr*) override; private: WatWriter* writer_; @@ -1156,6 +1157,12 @@ Result WatWriter::ExprVisitorDelegate::OnLoadZeroExpr(LoadZeroExpr* expr) { return Result::Ok; } +Result WatWriter::ExprVisitorDelegate::OnOpcodeRawExpr(OpcodeRawExpr* expr) { + // WatWriter can't run in skip_function_bodies mode + return Result::Error; +} + + void WatWriter::WriteExpr(const Expr* expr) { WABT_TRACE(WriteExprList); ExprVisitorDelegate delegate(this); From 19837d170c8cd893879e40bfa4ae2d22bb5d7de9 Mon Sep 17 00:00:00 2001 From: guzibei Date: Sun, 26 Jan 2025 23:08:34 +0800 Subject: [PATCH 02/13] -format code --- include/wabt/binary-reader-nop.h | 4 +- include/wabt/binary-reader.h | 3 +- include/wabt/expr-visitor.h | 1 - include/wabt/ir.h | 12 +++-- include/wabt/stream.h | 33 ++++++------- src/binary-reader-ir.cc | 35 +++++++------ src/binary-reader-logging.cc | 9 ++-- src/binary-writer.cc | 19 ++++--- src/expr-visitor.cc | 5 +- src/interp/binary-reader-interp.cc | 3 +- src/ir-util.cc | 2 +- src/stream.cc | 79 ++++++++++++++---------------- src/validator.cc | 1 - src/wat-writer.cc | 1 - 14 files changed, 108 insertions(+), 99 deletions(-) diff --git a/include/wabt/binary-reader-nop.h b/include/wabt/binary-reader-nop.h index 0bd0c1793e..6b314ee362 100644 --- a/include/wabt/binary-reader-nop.h +++ b/include/wabt/binary-reader-nop.h @@ -259,7 +259,9 @@ class BinaryReaderNop : public BinaryReaderDelegate { Result OnDropExpr() override { return Result::Ok; } Result OnElseExpr() override { return Result::Ok; } Result OnEndExpr() override { return Result::Ok; } - Result OnSkipFunctionBodyExpr(std::vector& opcode_buffer) override { return Result::Ok; } + Result OnSkipFunctionBodyExpr(std::vector& opcode_buffer) override { + return Result::Ok; + } Result OnF32ConstExpr(uint32_t value_bits) override { return Result::Ok; } Result OnF64ConstExpr(uint64_t value_bits) override { return Result::Ok; } Result OnV128ConstExpr(v128 value_bits) override { return Result::Ok; } diff --git a/include/wabt/binary-reader.h b/include/wabt/binary-reader.h index 5d55461f89..ac966d789d 100644 --- a/include/wabt/binary-reader.h +++ b/include/wabt/binary-reader.h @@ -264,7 +264,8 @@ class BinaryReaderDelegate { virtual Result OnDropExpr() = 0; virtual Result OnElseExpr() = 0; virtual Result OnEndExpr() = 0; - virtual Result OnSkipFunctionBodyExpr(std::vector& opcode_buffer) = 0; + virtual Result OnSkipFunctionBodyExpr( + std::vector& opcode_buffer) = 0; virtual Result OnF32ConstExpr(uint32_t value_bits) = 0; virtual Result OnF64ConstExpr(uint64_t value_bits) = 0; virtual Result OnV128ConstExpr(v128 value_bits) = 0; diff --git a/include/wabt/expr-visitor.h b/include/wabt/expr-visitor.h index 7348db076b..58a3b80bd6 100644 --- a/include/wabt/expr-visitor.h +++ b/include/wabt/expr-visitor.h @@ -225,7 +225,6 @@ class ExprVisitor::DelegateNop : public ExprVisitor::Delegate { Result OnLoadSplatExpr(LoadSplatExpr*) override { return Result::Ok; } Result OnLoadZeroExpr(LoadZeroExpr*) override { return Result::Ok; } Result OnOpcodeRawExpr(OpcodeRawExpr*) override { return Result::Ok; } - }; } // namespace wabt diff --git a/include/wabt/ir.h b/include/wabt/ir.h index ac8c5b0ce4..84e599ac1c 100644 --- a/include/wabt/ir.h +++ b/include/wabt/ir.h @@ -430,7 +430,7 @@ enum class ExprType { First = AtomicLoad, Last = Unreachable, - OpCodeRaw = Last + 1, // virual type + OpCodeRaw = Last + 1, // virual type }; const char* GetExprTypeName(ExprType type); @@ -842,9 +842,13 @@ class AtomicFenceExpr : public ExprMixin { class OpcodeRawExpr : public ExprMixin { public: - explicit OpcodeRawExpr(std::vector&& in_opcode_buffer, FuncSignature& in_func_sig, const Location& loc = Location()) - : ExprMixin(loc), opcode_buffer(std::move(in_opcode_buffer)), is_extracted(false), func_sig(&in_func_sig) { - } + explicit OpcodeRawExpr(std::vector&& in_opcode_buffer, + FuncSignature& in_func_sig, + const Location& loc = Location()) + : ExprMixin(loc), + opcode_buffer(std::move(in_opcode_buffer)), + is_extracted(false), + func_sig(&in_func_sig) {} std::vector opcode_buffer; bool is_extracted; diff --git a/include/wabt/stream.h b/include/wabt/stream.h index 9dded608f7..3b68ac8a0e 100644 --- a/include/wabt/stream.h +++ b/include/wabt/stream.h @@ -196,26 +196,23 @@ class MemoryStream : public Stream { }; class MemoryAllocStream : public wabt::Stream { - public: - typedef void* (*wabt_realloc)(void * __ptr, size_t __size); - - - MemoryAllocStream(uint8_t* _output_data = nullptr, size_t _total_size = 0, wabt_realloc _realloc_fn = nullptr) - : output_data(_output_data), total_size(_total_size), used_size(0) { - if (_realloc_fn) { - realloc_fn = _realloc_fn; - } else { - realloc_fn = std::realloc; - } + public: + typedef void* (*wabt_realloc)(void* __ptr, size_t __size); + + MemoryAllocStream(uint8_t* _output_data = nullptr, + size_t _total_size = 0, + wabt_realloc _realloc_fn = nullptr) + : output_data(_output_data), total_size(_total_size), used_size(0) { + if (_realloc_fn) { + realloc_fn = _realloc_fn; + } else { + realloc_fn = std::realloc; + } } - wabt::Result WriteDataImpl(size_t dst_offset, - const void* src, - size_t size); + wabt::Result WriteDataImpl(size_t dst_offset, const void* src, size_t size); - wabt::Result MoveDataImpl(size_t dst_offset, - size_t src_offset, - size_t size); + wabt::Result MoveDataImpl(size_t dst_offset, size_t src_offset, size_t size); wabt::Result TruncateImpl(size_t size); inline size_t GetSize() const { return used_size; } @@ -225,7 +222,7 @@ class MemoryAllocStream : public wabt::Stream { uint8_t* output_data; size_t total_size; size_t used_size; - wabt_realloc realloc_fn; + wabt_realloc realloc_fn; }; class FileStream : public Stream { diff --git a/src/binary-reader-ir.cc b/src/binary-reader-ir.cc index 4b666d6473..ed9050ac87 100644 --- a/src/binary-reader-ir.cc +++ b/src/binary-reader-ir.cc @@ -33,17 +33,18 @@ namespace wabt { #ifdef WABT_OVERRIDE_ALLOC_EXPR template -inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 typename std::__unique_if<_Tp>::__unique_single -wabt_expr_make_unique(_Args&&... __args) { - // static_assert(sizeof(_Tp) <= 128); - return std::unique_ptr<_Tp>(new ((wabt::Module*)(nullptr))_Tp(std::forward<_Args>(__args)...)); +inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 + typename std::__unique_if<_Tp>::__unique_single + wabt_expr_make_unique(_Args&&... __args) { + // static_assert(sizeof(_Tp) <= 128); + return std::unique_ptr<_Tp>(new ((wabt::Module*)(nullptr)) + _Tp(std::forward<_Args>(__args)...)); } extern void* operator new(size_t size, wabt::Module* m); #else #define wabt_expr_make_unique std::make_unique #endif - namespace { struct LabelNode { @@ -924,7 +925,8 @@ Result BinaryReaderIR::OnBrTableExpr(Index num_targets, } Result BinaryReaderIR::OnCallExpr(Index func_index) { - return AppendExpr(wabt_expr_make_unique(Var(func_index, GetLocation()))); + return AppendExpr( + wabt_expr_make_unique(Var(func_index, GetLocation()))); } Result BinaryReaderIR::OnCallIndirectExpr(Index sig_index, Index table_index) { @@ -1030,12 +1032,15 @@ Result BinaryReaderIR::OnEndExpr() { return PopLabel(); } -Result BinaryReaderIR::OnSkipFunctionBodyExpr(std::vector& opcode_buffer) { - Result result_append = AppendExpr( - wabt_expr_make_unique(std::move(opcode_buffer), current_func_->decl.sig, GetLocation())); +Result BinaryReaderIR::OnSkipFunctionBodyExpr( + std::vector& opcode_buffer) { + Result result_append = AppendExpr(wabt_expr_make_unique( + std::move(opcode_buffer), current_func_->decl.sig, GetLocation())); Result result_pop = PopLabel(); - return (result_append == Result::Ok && result_pop == Result::Ok) ? Result::Ok : Result::Error; + return (result_append == Result::Ok && result_pop == Result::Ok) + ? Result::Ok + : Result::Error; } Result BinaryReaderIR::OnF32ConstExpr(uint32_t value_bits) { @@ -1124,7 +1129,7 @@ Result BinaryReaderIR::OnMemoryInitExpr(Index segment, Index memidx) { Result BinaryReaderIR::OnMemorySizeExpr(Index memidx) { return AppendExpr( - wabt_expr_make_unique(Var(memidx, GetLocation()))); + wabt_expr_make_unique(Var(memidx, GetLocation()))); } Result BinaryReaderIR::OnTableCopyExpr(Index dst_index, Index src_index) { @@ -1187,7 +1192,8 @@ Result BinaryReaderIR::OnNopExpr() { } Result BinaryReaderIR::OnRethrowExpr(Index depth) { - return AppendExpr(wabt_expr_make_unique(Var(depth, GetLocation()))); + return AppendExpr( + wabt_expr_make_unique(Var(depth, GetLocation()))); } Result BinaryReaderIR::OnReturnExpr() { @@ -1220,7 +1226,8 @@ Result BinaryReaderIR::OnStoreExpr(Opcode opcode, Result BinaryReaderIR::OnThrowExpr(Index tag_index) { module_->features_used.exceptions = true; - return AppendExpr(wabt_expr_make_unique(Var(tag_index, GetLocation()))); + return AppendExpr( + wabt_expr_make_unique(Var(tag_index, GetLocation()))); } Result BinaryReaderIR::OnThrowRefExpr() { @@ -1735,7 +1742,7 @@ Result BinaryReaderIR::OnCodeMetadata(Offset offset, std::vector data_(static_cast(data), static_cast(data) + size); auto meta = wabt_expr_make_unique(current_metadata_name_, - std::move(data_)); + std::move(data_)); meta->loc.offset = offset; code_metadata_queue_.push_metadata(std::move(meta)); return Result::Ok; diff --git a/src/binary-reader-logging.cc b/src/binary-reader-logging.cc index 37f2592acd..cad0545011 100644 --- a/src/binary-reader-logging.cc +++ b/src/binary-reader-logging.cc @@ -781,10 +781,13 @@ Result BinaryReaderLogging::OnGenericCustomSection(std::string_view name, return reader_->name(opcode, memidx, alignment_log2, offset, value); \ } -#define DEFINE_SKIP(name) \ +#define DEFINE_SKIP(name) \ Result BinaryReaderLogging::name(std::vector& opcode_buffer) { \ - LOGF(#name " length: %lu""\n", opcode_buffer.size()); \ - return reader_->name(opcode_buffer); \ + LOGF(#name \ + " length: %lu" \ + "\n", \ + opcode_buffer.size()); \ + return reader_->name(opcode_buffer); \ } #define DEFINE0(name) \ diff --git a/src/binary-writer.cc b/src/binary-writer.cc index 382e9334f5..17f531b002 100644 --- a/src/binary-writer.cc +++ b/src/binary-writer.cc @@ -1151,17 +1151,16 @@ void BinaryWriter::WriteExpr(const Func* func, const Expr* expr) { break; } case ExprType::OpCodeRaw: { - auto* opcode_raw_expr = cast(expr); - if (opcode_raw_expr->is_extracted) { - WriteExprList(func, opcode_raw_expr->extracted_exprs); - } else { - auto& opcode_buffer = opcode_raw_expr->opcode_buffer; - // Opcode::End 0x0b - assert(opcode_buffer[opcode_buffer.size() - 1] == 0x0b); - stream_->WriteData(opcode_buffer.data(), opcode_buffer.size() - 1); - } + auto* opcode_raw_expr = cast(expr); + if (opcode_raw_expr->is_extracted) { + WriteExprList(func, opcode_raw_expr->extracted_exprs); + } else { + auto& opcode_buffer = opcode_raw_expr->opcode_buffer; + // Opcode::End 0x0b + assert(opcode_buffer[opcode_buffer.size() - 1] == 0x0b); + stream_->WriteData(opcode_buffer.data(), opcode_buffer.size() - 1); + } } - } } diff --git a/src/expr-visitor.cc b/src/expr-visitor.cc index 97177290ba..76d818af62 100644 --- a/src/expr-visitor.cc +++ b/src/expr-visitor.cc @@ -151,7 +151,8 @@ Result ExprVisitor::VisitExpr(Expr* root_expr) { } case State::OpcodeRaw: { auto opcode_raw_expr = cast(expr); - // ERROR_UNLESS(opcode_raw_expr->is_extracted, "opcode_raw could not compile when in visiting") + // ERROR_UNLESS(opcode_raw_expr->is_extracted, "opcode_raw could not + // compile when in visiting") if (opcode_raw_expr->is_extracted) { auto& iter = expr_iter_stack_.back(); if (iter != opcode_raw_expr->extracted_exprs.end()) { @@ -160,7 +161,7 @@ Result ExprVisitor::VisitExpr(Expr* root_expr) { PopExprlist(); } } - + break; } } diff --git a/src/interp/binary-reader-interp.cc b/src/interp/binary-reader-interp.cc index cb15b616ef..183b393736 100644 --- a/src/interp/binary-reader-interp.cc +++ b/src/interp/binary-reader-interp.cc @@ -1089,7 +1089,8 @@ Result BinaryReaderInterp::OnEndExpr() { return Result::Ok; } -Result BinaryReaderInterp::OnSkipFunctionBodyExpr(std::vector& opcode_buffer) { +Result BinaryReaderInterp::OnSkipFunctionBodyExpr( + std::vector& opcode_buffer) { PopLabel(); return Result::Ok; } diff --git a/src/ir-util.cc b/src/ir-util.cc index dc5f26b203..ee229cbeae 100644 --- a/src/ir-util.cc +++ b/src/ir-util.cc @@ -273,7 +273,7 @@ ModuleContext::Arities ModuleContext::GetExprArity(const Expr& expr) const { case ExprType::SimdShuffleOp: return {2, 1}; case ExprType::OpCodeRaw: - return {0, cast(&expr)->func_sig->GetNumResults()}; + return {0, cast(&expr)->func_sig->GetNumResults()}; } WABT_UNREACHABLE; diff --git a/src/stream.cc b/src/stream.cc index 43d20351ea..122c0d7e34 100644 --- a/src/stream.cc +++ b/src/stream.cc @@ -229,72 +229,69 @@ Result MemoryStream::TruncateImpl(size_t size) { return Result::Ok; } - Result MemoryAllocStream::WriteDataImpl(size_t dst_offset, - const void* src, - size_t size) { + const void* src, + size_t size) { if (size == 0) { - return wabt::Result::Ok; + return wabt::Result::Ok; } size_t end = dst_offset + size; bool needRealloc = false; while (end > total_size) { - total_size = total_size << 1; - needRealloc = true; + total_size = total_size << 1; + needRealloc = true; } if (needRealloc) { - output_data = static_cast(realloc_fn(output_data, total_size)); + output_data = static_cast(realloc_fn(output_data, total_size)); } if (end > used_size) { - used_size = end; + used_size = end; } memcpy(output_data + dst_offset, src, size); return wabt::Result::Ok; - } Result MemoryAllocStream::MoveDataImpl(size_t dst_offset, - size_t src_offset, - size_t size) { - if (size == 0) { - return wabt::Result::Ok; - } - size_t src_end = src_offset + size; - size_t dst_end = dst_offset + size; - size_t end = src_end > dst_end ? src_end : dst_end; - - bool needRealloc = false; - while (end > total_size) { - total_size = total_size << 1; - needRealloc = true; - } - if (needRealloc) { - output_data = static_cast(realloc_fn(output_data, total_size)); - } + size_t src_offset, + size_t size) { + if (size == 0) { + return wabt::Result::Ok; + } + size_t src_end = src_offset + size; + size_t dst_end = dst_offset + size; + size_t end = src_end > dst_end ? src_end : dst_end; - if (end > used_size) { - used_size = end; - } + bool needRealloc = false; + while (end > total_size) { + total_size = total_size << 1; + needRealloc = true; + } + if (needRealloc) { + output_data = static_cast(realloc_fn(output_data, total_size)); + } - uint8_t* dst = output_data + dst_offset; - uint8_t* src = output_data + src_offset; - memmove(dst, src, size); - return wabt::Result::Ok; + if (end > used_size) { + used_size = end; + } + uint8_t* dst = output_data + dst_offset; + uint8_t* src = output_data + src_offset; + memmove(dst, src, size); + return wabt::Result::Ok; } Result MemoryAllocStream::TruncateImpl(size_t size) { - if (size > used_size) { - return wabt::Result::Error; - } - used_size = size; + if (size > used_size) { + return wabt::Result::Error; + } + used_size = size; - // will not truncate size, we can truncate it at end - // output_data = (uint8_t*)realloc_fn(output_data, size); - // total_size = size; - return wabt::Result::Ok; + // will not truncate size, we can truncate it at end + // output_data = (uint8_t*)realloc_fn(output_data, size); + // total_size = size; + return wabt::Result::Ok; } FileStream::FileStream(std::string_view filename, Stream* log_stream) diff --git a/src/validator.cc b/src/validator.cc index 7881f1d45c..333b8ab0f0 100644 --- a/src/validator.cc +++ b/src/validator.cc @@ -694,7 +694,6 @@ Result Validator::OnOpcodeRawExpr(OpcodeRawExpr* expr) { return Result::Ok; } - Validator::Validator(Errors* errors, const Module* module, const ValidateOptions& options) diff --git a/src/wat-writer.cc b/src/wat-writer.cc index 33c874ee73..faad06894e 100644 --- a/src/wat-writer.cc +++ b/src/wat-writer.cc @@ -1162,7 +1162,6 @@ Result WatWriter::ExprVisitorDelegate::OnOpcodeRawExpr(OpcodeRawExpr* expr) { return Result::Error; } - void WatWriter::WriteExpr(const Expr* expr) { WABT_TRACE(WriteExprList); ExprVisitorDelegate delegate(this); From 48e52e587eb911e4821473f331aa55140ee82109 Mon Sep 17 00:00:00 2001 From: guzibei Date: Mon, 27 Jan 2025 00:06:43 +0800 Subject: [PATCH 03/13] - merge call ReadSkipedFunctionBody --- src/binary-reader.cc | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/binary-reader.cc b/src/binary-reader.cc index e1859a1c31..cbd026085d 100644 --- a/src/binary-reader.cc +++ b/src/binary-reader.cc @@ -162,6 +162,7 @@ class BinaryReader { [[nodiscard]] Result ReadFunctionBody(Offset end_offset); // ReadInstructions reads until end_offset or the nesting depth reaches zero. [[nodiscard]] Result ReadInstructions(Offset end_offset, const char* context); + [[nodiscard]] Result ReadSkipedFunctionBody(Offset end_offset); [[nodiscard]] Result ReadNameSection(Offset section_size); [[nodiscard]] Result ReadRelocSection(Offset section_size); [[nodiscard]] Result ReadDylinkSection(Offset section_size); @@ -1939,6 +1940,17 @@ Result BinaryReader::ReadInstructions(Offset end_offset, const char* context) { return Result::Error; } +Result BinaryReader::ReadSkipedFunctionBody(Offset end_offset) { + std::vector opcode_buffer; + opcode_buffer.resize(end_offset - state_.offset); + memcpy(opcode_buffer.data(), state_.data + state_.offset, + opcode_buffer.size()); + CALLBACK(OnSkipFunctionBodyExpr, opcode_buffer); + state_.offset = end_offset; + + return Result::Ok; +} + Result BinaryReader::ReadNameSection(Offset section_size) { CALLBACK(BeginNamesSection, section_size); Index i = 0; @@ -2887,7 +2899,7 @@ Result BinaryReader::ReadCodeSection(Offset section_size) { CALLBACK(EndLocalDecls); if (options_.skip_function_bodies) { - state_.offset = end_offset; + CHECK_RESULT(ReadSkipedFunctionBody(end_offset)); } else { CHECK_RESULT(ReadFunctionBody(end_offset)); } From 01f0c4f382b74ed819003ec2b646d027e7000c25 Mon Sep 17 00:00:00 2001 From: guzibei Date: Mon, 27 Jan 2025 00:25:57 +0800 Subject: [PATCH 04/13] -fix typo --- src/binary-reader.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/binary-reader.cc b/src/binary-reader.cc index cbd026085d..99a8a319b1 100644 --- a/src/binary-reader.cc +++ b/src/binary-reader.cc @@ -162,7 +162,7 @@ class BinaryReader { [[nodiscard]] Result ReadFunctionBody(Offset end_offset); // ReadInstructions reads until end_offset or the nesting depth reaches zero. [[nodiscard]] Result ReadInstructions(Offset end_offset, const char* context); - [[nodiscard]] Result ReadSkipedFunctionBody(Offset end_offset); + [[nodiscard]] Result ReadSkippedFunctionBody(Offset end_offset); [[nodiscard]] Result ReadNameSection(Offset section_size); [[nodiscard]] Result ReadRelocSection(Offset section_size); [[nodiscard]] Result ReadDylinkSection(Offset section_size); @@ -1940,7 +1940,7 @@ Result BinaryReader::ReadInstructions(Offset end_offset, const char* context) { return Result::Error; } -Result BinaryReader::ReadSkipedFunctionBody(Offset end_offset) { +Result BinaryReader::ReadSkippedFunctionBody(Offset end_offset) { std::vector opcode_buffer; opcode_buffer.resize(end_offset - state_.offset); memcpy(opcode_buffer.data(), state_.data + state_.offset, @@ -2899,7 +2899,7 @@ Result BinaryReader::ReadCodeSection(Offset section_size) { CALLBACK(EndLocalDecls); if (options_.skip_function_bodies) { - CHECK_RESULT(ReadSkipedFunctionBody(end_offset)); + CHECK_RESULT(ReadSkippedFunctionBody(end_offset)); } else { CHECK_RESULT(ReadFunctionBody(end_offset)); } From 11e836317accc8cece3b04643a62142cc4733352 Mon Sep 17 00:00:00 2001 From: guzibei Date: Mon, 27 Jan 2025 00:55:35 +0800 Subject: [PATCH 05/13] +add an macro for old skip function version --- include/wabt/ir.h | 2 +- src/binary-reader.cc | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/include/wabt/ir.h b/include/wabt/ir.h index 84e599ac1c..c3dcc26a0b 100644 --- a/include/wabt/ir.h +++ b/include/wabt/ir.h @@ -430,7 +430,7 @@ enum class ExprType { First = AtomicLoad, Last = Unreachable, - OpCodeRaw = Last + 1, // virual type + OpCodeRaw = Last + 1, // virtual type }; const char* GetExprTypeName(ExprType type); diff --git a/src/binary-reader.cc b/src/binary-reader.cc index 99a8a319b1..4faa8522e9 100644 --- a/src/binary-reader.cc +++ b/src/binary-reader.cc @@ -2899,7 +2899,12 @@ Result BinaryReader::ReadCodeSection(Offset section_size) { CALLBACK(EndLocalDecls); if (options_.skip_function_bodies) { +#ifdef WABT_KEEP_OPCODE_WHEN_SKIP_FUNC_BODY CHECK_RESULT(ReadSkippedFunctionBody(end_offset)); +#else + state_.offset = end_offset; + CALLBACK0(OnEndExpr); +#endif } else { CHECK_RESULT(ReadFunctionBody(end_offset)); } From 46b4a1da2c940922320deb642913f5693d3e2a8e Mon Sep 17 00:00:00 2001 From: guzibei Date: Mon, 27 Jan 2025 01:05:15 +0800 Subject: [PATCH 06/13] -remove MemoryAllocStream +make old skip version --- include/wabt/stream.h | 30 -------------------- src/binary-reader.cc | 11 ++++---- src/stream.cc | 65 ------------------------------------------- 3 files changed, 6 insertions(+), 100 deletions(-) diff --git a/include/wabt/stream.h b/include/wabt/stream.h index 3b68ac8a0e..67f50975eb 100644 --- a/include/wabt/stream.h +++ b/include/wabt/stream.h @@ -195,36 +195,6 @@ class MemoryStream : public Stream { std::unique_ptr buf_; }; -class MemoryAllocStream : public wabt::Stream { - public: - typedef void* (*wabt_realloc)(void* __ptr, size_t __size); - - MemoryAllocStream(uint8_t* _output_data = nullptr, - size_t _total_size = 0, - wabt_realloc _realloc_fn = nullptr) - : output_data(_output_data), total_size(_total_size), used_size(0) { - if (_realloc_fn) { - realloc_fn = _realloc_fn; - } else { - realloc_fn = std::realloc; - } - } - - wabt::Result WriteDataImpl(size_t dst_offset, const void* src, size_t size); - - wabt::Result MoveDataImpl(size_t dst_offset, size_t src_offset, size_t size); - - wabt::Result TruncateImpl(size_t size); - inline size_t GetSize() const { return used_size; } - inline uint8_t* GetData() { return output_data; } - - private: - uint8_t* output_data; - size_t total_size; - size_t used_size; - wabt_realloc realloc_fn; -}; - class FileStream : public Stream { public: WABT_DISALLOW_COPY_AND_ASSIGN(FileStream); diff --git a/src/binary-reader.cc b/src/binary-reader.cc index 4faa8522e9..9ff79ec7cc 100644 --- a/src/binary-reader.cc +++ b/src/binary-reader.cc @@ -1941,12 +1941,18 @@ Result BinaryReader::ReadInstructions(Offset end_offset, const char* context) { } Result BinaryReader::ReadSkippedFunctionBody(Offset end_offset) { +#ifdef WABT_KEEP_OPCODE_WHEN_SKIP_FUNC_BODY std::vector opcode_buffer; opcode_buffer.resize(end_offset - state_.offset); memcpy(opcode_buffer.data(), state_.data + state_.offset, opcode_buffer.size()); CALLBACK(OnSkipFunctionBodyExpr, opcode_buffer); state_.offset = end_offset; +#else + // old version with OnEndExpr + state_.offset = end_offset; + CALLBACK0(OnEndExpr); +#endif return Result::Ok; } @@ -2899,12 +2905,7 @@ Result BinaryReader::ReadCodeSection(Offset section_size) { CALLBACK(EndLocalDecls); if (options_.skip_function_bodies) { -#ifdef WABT_KEEP_OPCODE_WHEN_SKIP_FUNC_BODY CHECK_RESULT(ReadSkippedFunctionBody(end_offset)); -#else - state_.offset = end_offset; - CALLBACK0(OnEndExpr); -#endif } else { CHECK_RESULT(ReadFunctionBody(end_offset)); } diff --git a/src/stream.cc b/src/stream.cc index 122c0d7e34..5f27e01b2e 100644 --- a/src/stream.cc +++ b/src/stream.cc @@ -229,71 +229,6 @@ Result MemoryStream::TruncateImpl(size_t size) { return Result::Ok; } -Result MemoryAllocStream::WriteDataImpl(size_t dst_offset, - const void* src, - size_t size) { - if (size == 0) { - return wabt::Result::Ok; - } - size_t end = dst_offset + size; - bool needRealloc = false; - while (end > total_size) { - total_size = total_size << 1; - needRealloc = true; - } - if (needRealloc) { - output_data = static_cast(realloc_fn(output_data, total_size)); - } - - if (end > used_size) { - used_size = end; - } - - memcpy(output_data + dst_offset, src, size); - return wabt::Result::Ok; -} - -Result MemoryAllocStream::MoveDataImpl(size_t dst_offset, - size_t src_offset, - size_t size) { - if (size == 0) { - return wabt::Result::Ok; - } - size_t src_end = src_offset + size; - size_t dst_end = dst_offset + size; - size_t end = src_end > dst_end ? src_end : dst_end; - - bool needRealloc = false; - while (end > total_size) { - total_size = total_size << 1; - needRealloc = true; - } - if (needRealloc) { - output_data = static_cast(realloc_fn(output_data, total_size)); - } - - if (end > used_size) { - used_size = end; - } - - uint8_t* dst = output_data + dst_offset; - uint8_t* src = output_data + src_offset; - memmove(dst, src, size); - return wabt::Result::Ok; -} - -Result MemoryAllocStream::TruncateImpl(size_t size) { - if (size > used_size) { - return wabt::Result::Error; - } - used_size = size; - - // will not truncate size, we can truncate it at end - // output_data = (uint8_t*)realloc_fn(output_data, size); - // total_size = size; - return wabt::Result::Ok; -} - FileStream::FileStream(std::string_view filename, Stream* log_stream) : Stream(log_stream), file_(nullptr), offset_(0), should_close_(false) { std::string filename_str(filename); From 371e4ce98c9684a96e360ee69f2f3987ecbec3c8 Mon Sep 17 00:00:00 2001 From: guzibei Date: Mon, 27 Jan 2025 20:59:32 +0800 Subject: [PATCH 07/13] +impl ExtractOpcodeRawExpr, can extract ExtractOpcodeRawExpr -optimize code on suggestion --- include/wabt/binary-reader-ir.h | 6 +++++- include/wabt/binary-reader.h | 4 ++++ include/wabt/common.h | 2 +- src/binary-reader-ir.cc | 27 ++++++++++++++++++++++++++- src/binary-reader-logging.cc | 16 +++++----------- src/binary-reader.cc | 19 ++++++++++++++++--- 6 files changed, 57 insertions(+), 17 deletions(-) diff --git a/include/wabt/binary-reader-ir.h b/include/wabt/binary-reader-ir.h index 4de0ee7099..5933fab3d7 100644 --- a/include/wabt/binary-reader-ir.h +++ b/include/wabt/binary-reader-ir.h @@ -32,6 +32,10 @@ Result ReadBinaryIr(const char* filename, Errors*, Module* out_module); -} // namespace wabt +class OpcodeRawExpr; +Result ExtractOpcodeRawExpr(OpcodeRawExpr &expr, Module &module, + const ReadBinaryOptions &options); + +} // namespace wabt #endif /* WABT_BINARY_READER_IR_H_ */ diff --git a/include/wabt/binary-reader.h b/include/wabt/binary-reader.h index ac966d789d..e5d6d62111 100644 --- a/include/wabt/binary-reader.h +++ b/include/wabt/binary-reader.h @@ -513,6 +513,10 @@ Result ReadBinary(const void* data, BinaryReaderDelegate* reader, const ReadBinaryOptions& options); +Result ExtractFunctionBody(const void *data, size_t size, Location &loc, + BinaryReaderDelegate *delegate, + const ReadBinaryOptions &options); + size_t ReadU32Leb128(const uint8_t* ptr, const uint8_t* end, uint32_t* out_value); diff --git a/include/wabt/common.h b/include/wabt/common.h index e1411b4870..58734e0fc4 100644 --- a/include/wabt/common.h +++ b/include/wabt/common.h @@ -196,7 +196,7 @@ enum class LabelType { Try, TryTable, Catch, - + ExtractFunc, First = Func, Last = Catch, }; diff --git a/src/binary-reader-ir.cc b/src/binary-reader-ir.cc index ed9050ac87..8f27b3096d 100644 --- a/src/binary-reader-ir.cc +++ b/src/binary-reader-ir.cc @@ -182,6 +182,7 @@ class BinaryReaderIR : public BinaryReaderNop { Result OnFunctionBodyCount(Index count) override; Result BeginFunctionBody(Index index, Offset size) override; + Result BeginExtractFunctionBody(ExprList& extracted_list, Expr* context); Result OnLocalDecl(Index decl_index, Index count, Type type) override; Result OnOpcode(Opcode opcode) override; @@ -816,6 +817,11 @@ Result BinaryReaderIR::BeginFunctionBody(Index index, Offset size) { return PushLabel(LabelType::Func, ¤t_func_->exprs); } +Result BinaryReaderIR::BeginExtractFunctionBody(ExprList &extracted_list, + Expr *context) { + return PushLabel(LabelType::ExtractFunc, &extracted_list, context); +} + Result BinaryReaderIR::OnLocalDecl(Index decl_index, Index count, Type type) { current_func_->local_types.AppendDecl(type, count); @@ -1025,6 +1031,7 @@ Result BinaryReaderIR::OnEndExpr() { case LabelType::InitExpr: case LabelType::Func: case LabelType::Catch: + case LabelType::ExtractFunc: break; } } @@ -1886,4 +1893,22 @@ Result ReadBinaryIr(const char* filename, return ReadBinary(data, size, &reader, options); } -} // namespace wabt +Result ExtractOpcodeRawExpr(OpcodeRawExpr &expr, Module &module, + const ReadBinaryOptions &options) { + if (expr.is_extracted) { + return Result::Ok; + } + Errors errors; + BinaryReaderIR reader(&module, expr.loc.filename.data(), &errors); + CHECK_RESULT(reader.BeginExtractFunctionBody(expr.extracted_exprs, nullptr)); + Result result = + ExtractFunctionBody(expr.opcode_buffer.data(), expr.opcode_buffer.size(), + expr.loc, &reader, options); + if (Succeeded(result)) { + expr.is_extracted = true; + expr.opcode_buffer.clear(); + } + return result; +} + +} // namespace wabt diff --git a/src/binary-reader-logging.cc b/src/binary-reader-logging.cc index cad0545011..4cefb1a9d3 100644 --- a/src/binary-reader-logging.cc +++ b/src/binary-reader-logging.cc @@ -781,15 +781,6 @@ Result BinaryReaderLogging::OnGenericCustomSection(std::string_view name, return reader_->name(opcode, memidx, alignment_log2, offset, value); \ } -#define DEFINE_SKIP(name) \ - Result BinaryReaderLogging::name(std::vector& opcode_buffer) { \ - LOGF(#name \ - " length: %lu" \ - "\n", \ - opcode_buffer.size()); \ - return reader_->name(opcode_buffer); \ - } - #define DEFINE0(name) \ Result BinaryReaderLogging::name() { \ LOGF(#name "\n"); \ @@ -860,8 +851,11 @@ DEFINE_INDEX_DESC(OnDelegateExpr, "depth"); DEFINE0(OnDropExpr) DEFINE0(OnElseExpr) DEFINE0(OnEndExpr) -DEFINE_SKIP(OnSkipFunctionBodyExpr) - +Result BinaryReaderLogging::OnSkipFunctionBodyExpr( + std::vector &opcode_buffer) { + LOGF("OnSkipFunctionBodyExpr length: %lu\n", opcode_buffer.size()); + return reader_->OnSkipFunctionBodyExpr(opcode_buffer); +} DEFINE_INDEX_DESC(OnGlobalGetExpr, "index") DEFINE_INDEX_DESC(OnGlobalSetExpr, "index") DEFINE_LOAD_STORE_OPCODE(OnLoadExpr); diff --git a/src/binary-reader.cc b/src/binary-reader.cc index 9ff79ec7cc..daba707313 100644 --- a/src/binary-reader.cc +++ b/src/binary-reader.cc @@ -72,6 +72,10 @@ namespace { class BinaryReader { public: + friend Result wabt::ExtractFunctionBody(const void *data, size_t size, + Location &loc, + BinaryReaderDelegate *delegate, + const ReadBinaryOptions &options); struct ReadModuleOptions { bool stop_on_first_error; }; @@ -1942,8 +1946,7 @@ Result BinaryReader::ReadInstructions(Offset end_offset, const char* context) { Result BinaryReader::ReadSkippedFunctionBody(Offset end_offset) { #ifdef WABT_KEEP_OPCODE_WHEN_SKIP_FUNC_BODY - std::vector opcode_buffer; - opcode_buffer.resize(end_offset - state_.offset); + std::vector opcode_buffer(end_offset - state_.offset); memcpy(opcode_buffer.data(), state_.data + state_.offset, opcode_buffer.size()); CALLBACK(OnSkipFunctionBodyExpr, opcode_buffer); @@ -3160,4 +3163,14 @@ Result ReadBinary(const void* data, BinaryReader::ReadModuleOptions{options.stop_on_first_error}); } -} // namespace wabt +Result ExtractFunctionBody(const void *data, size_t size, Location &loc, + BinaryReaderDelegate *delegate, + const ReadBinaryOptions &options) { + BinaryReader reader((void *)((Offset)data - loc.offset), size + loc.offset, + delegate, options); + reader.state_.offset = loc.offset; + return reader.ReadInstructions(reader.state_.offset + size, + "extract function body"); +} + +} // namespace wabt From 492a5cb5334596f4b4045bf5ec132bdf95017f63 Mon Sep 17 00:00:00 2001 From: guzibei Date: Mon, 27 Jan 2025 21:28:33 +0800 Subject: [PATCH 08/13] -format code --- include/wabt/binary-reader-ir.h | 5 +++-- include/wabt/binary-reader.h | 8 +++++--- src/binary-reader-ir.cc | 4 ++-- src/binary-reader-logging.cc | 2 +- src/binary-reader.cc | 21 ++++++++++++--------- 5 files changed, 23 insertions(+), 17 deletions(-) diff --git a/include/wabt/binary-reader-ir.h b/include/wabt/binary-reader-ir.h index 5933fab3d7..da4da30c3a 100644 --- a/include/wabt/binary-reader-ir.h +++ b/include/wabt/binary-reader-ir.h @@ -33,8 +33,9 @@ Result ReadBinaryIr(const char* filename, Module* out_module); class OpcodeRawExpr; -Result ExtractOpcodeRawExpr(OpcodeRawExpr &expr, Module &module, - const ReadBinaryOptions &options); +Result ExtractOpcodeRawExpr(OpcodeRawExpr& expr, + Module& module, + const ReadBinaryOptions& options); } // namespace wabt diff --git a/include/wabt/binary-reader.h b/include/wabt/binary-reader.h index e5d6d62111..fc96059cd0 100644 --- a/include/wabt/binary-reader.h +++ b/include/wabt/binary-reader.h @@ -513,9 +513,11 @@ Result ReadBinary(const void* data, BinaryReaderDelegate* reader, const ReadBinaryOptions& options); -Result ExtractFunctionBody(const void *data, size_t size, Location &loc, - BinaryReaderDelegate *delegate, - const ReadBinaryOptions &options); +Result ExtractFunctionBody(const void* data, + size_t size, + Location& loc, + BinaryReaderDelegate* delegate, + const ReadBinaryOptions& options); size_t ReadU32Leb128(const uint8_t* ptr, const uint8_t* end, diff --git a/src/binary-reader-ir.cc b/src/binary-reader-ir.cc index 8f27b3096d..4d479b4aee 100644 --- a/src/binary-reader-ir.cc +++ b/src/binary-reader-ir.cc @@ -817,8 +817,8 @@ Result BinaryReaderIR::BeginFunctionBody(Index index, Offset size) { return PushLabel(LabelType::Func, ¤t_func_->exprs); } -Result BinaryReaderIR::BeginExtractFunctionBody(ExprList &extracted_list, - Expr *context) { +Result BinaryReaderIR::BeginExtractFunctionBody(ExprList& extracted_list, + Expr* context) { return PushLabel(LabelType::ExtractFunc, &extracted_list, context); } diff --git a/src/binary-reader-logging.cc b/src/binary-reader-logging.cc index 4cefb1a9d3..fcebc1158c 100644 --- a/src/binary-reader-logging.cc +++ b/src/binary-reader-logging.cc @@ -852,7 +852,7 @@ DEFINE0(OnDropExpr) DEFINE0(OnElseExpr) DEFINE0(OnEndExpr) Result BinaryReaderLogging::OnSkipFunctionBodyExpr( - std::vector &opcode_buffer) { + std::vector& opcode_buffer) { LOGF("OnSkipFunctionBodyExpr length: %lu\n", opcode_buffer.size()); return reader_->OnSkipFunctionBodyExpr(opcode_buffer); } diff --git a/src/binary-reader.cc b/src/binary-reader.cc index daba707313..a8dc9702d7 100644 --- a/src/binary-reader.cc +++ b/src/binary-reader.cc @@ -72,10 +72,11 @@ namespace { class BinaryReader { public: - friend Result wabt::ExtractFunctionBody(const void *data, size_t size, - Location &loc, - BinaryReaderDelegate *delegate, - const ReadBinaryOptions &options); + friend Result wabt::ExtractFunctionBody(const void* data, + size_t size, + Location& loc, + BinaryReaderDelegate* delegate, + const ReadBinaryOptions& options); struct ReadModuleOptions { bool stop_on_first_error; }; @@ -3163,11 +3164,13 @@ Result ReadBinary(const void* data, BinaryReader::ReadModuleOptions{options.stop_on_first_error}); } -Result ExtractFunctionBody(const void *data, size_t size, Location &loc, - BinaryReaderDelegate *delegate, - const ReadBinaryOptions &options) { - BinaryReader reader((void *)((Offset)data - loc.offset), size + loc.offset, - delegate, options); +Result ExtractFunctionBody(const void* data, + size_t size, + Location& loc, + BinaryReaderDelegate* delegate, + const ReadBinaryOptions& options) { + BinaryReader reader(static_cast(data) - loc.offset, + size + loc.offset, delegate, options); reader.state_.offset = loc.offset; return reader.ReadInstructions(reader.state_.offset + size, "extract function body"); From d69f6ac551d4c2b550fe70ed6cf21dac861f6239 Mon Sep 17 00:00:00 2001 From: guzibei Date: Mon, 27 Jan 2025 21:58:51 +0800 Subject: [PATCH 09/13] -format code --- src/binary-reader.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/binary-reader.cc b/src/binary-reader.cc index a8dc9702d7..c124defa24 100644 --- a/src/binary-reader.cc +++ b/src/binary-reader.cc @@ -3169,8 +3169,8 @@ Result ExtractFunctionBody(const void* data, Location& loc, BinaryReaderDelegate* delegate, const ReadBinaryOptions& options) { - BinaryReader reader(static_cast(data) - loc.offset, - size + loc.offset, delegate, options); + BinaryReader reader(static_cast(data) - loc.offset, size + loc.offset, + delegate, options); reader.state_.offset = loc.offset; return reader.ReadInstructions(reader.state_.offset + size, "extract function body"); From b3ad6773a6b205ea8210b79648d95fc2e6a5775a Mon Sep 17 00:00:00 2001 From: guzibei Date: Mon, 27 Jan 2025 22:03:14 +0800 Subject: [PATCH 10/13] -format code --- src/binary-reader.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/binary-reader.cc b/src/binary-reader.cc index c124defa24..00bf2be1d5 100644 --- a/src/binary-reader.cc +++ b/src/binary-reader.cc @@ -3169,11 +3169,11 @@ Result ExtractFunctionBody(const void* data, Location& loc, BinaryReaderDelegate* delegate, const ReadBinaryOptions& options) { - BinaryReader reader(static_cast(data) - loc.offset, size + loc.offset, - delegate, options); + BinaryReader reader(static_cast(data) - loc.offset, + size + loc.offset, delegate, options); reader.state_.offset = loc.offset; return reader.ReadInstructions(reader.state_.offset + size, "extract function body"); } -} // namespace wabt +} // namespace wabt From 5fcaded588ea4c06d1d9dabc479ddda29222f557 Mon Sep 17 00:00:00 2001 From: guzibei Date: Tue, 28 Jan 2025 01:16:03 +0800 Subject: [PATCH 11/13] +impl pack opcodeRaw --- include/wabt/binary-writer.h | 5 +++++ src/binary-reader-ir.cc | 2 +- src/binary-writer.cc | 29 +++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/include/wabt/binary-writer.h b/include/wabt/binary-writer.h index dedba1bdcf..d4dd14e28f 100644 --- a/include/wabt/binary-writer.h +++ b/include/wabt/binary-writer.h @@ -46,6 +46,11 @@ struct WriteBinaryOptions { Result WriteBinaryModule(Stream*, const Module*, const WriteBinaryOptions&); +class OpcodeRawExpr; +Result PackOpcodeRawExpr(OpcodeRawExpr& expr, + Module& module, + const WriteBinaryOptions& options); + void WriteType(Stream* stream, Type type, const char* desc = nullptr); void WriteStr(Stream* stream, diff --git a/src/binary-reader-ir.cc b/src/binary-reader-ir.cc index 4d479b4aee..1ffa945f62 100644 --- a/src/binary-reader-ir.cc +++ b/src/binary-reader-ir.cc @@ -1911,4 +1911,4 @@ Result ExtractOpcodeRawExpr(OpcodeRawExpr &expr, Module &module, return result; } -} // namespace wabt +} // namespace wabt diff --git a/src/binary-writer.cc b/src/binary-writer.cc index 17f531b002..42797ad2c4 100644 --- a/src/binary-writer.cc +++ b/src/binary-writer.cc @@ -384,6 +384,9 @@ class BinaryWriter { WABT_DISALLOW_COPY_AND_ASSIGN(BinaryWriter); public: + friend Result wabt::PackOpcodeRawExpr(OpcodeRawExpr& expr, + Module& module, + const WriteBinaryOptions& options); BinaryWriter(Stream*, const WriteBinaryOptions& options, const Module* module); @@ -1865,4 +1868,30 @@ Result WriteBinaryModule(Stream* stream, return binary_writer.WriteModule(); } +Result PackOpcodeRawExpr(OpcodeRawExpr& expr, + Module& module, + const WriteBinaryOptions& options) { + if (!expr.is_extracted) { + return Result::Ok; + } + + MemoryStream stream; + BinaryWriter binary_writer(&stream, options, &module); + + binary_writer.WriteExprList(nullptr, expr.extracted_exprs); + WriteOpcode(&stream, Opcode::End); + auto& ob = stream.output_buffer(); + + if (ob.data.size()) { + expr.opcode_buffer = std::move(ob.data); + stream.ReleaseOutputBuffer(); + expr.is_extracted = false; + expr.extracted_exprs.clear(); + return Result::Ok; + } else { + stream.ReleaseOutputBuffer(); + return Result::Error; + } +} + } // namespace wabt From 05db5a1fd65f60a35f7a4c54d4b669a2fd8e9614 Mon Sep 17 00:00:00 2001 From: guzibei Date: Tue, 28 Jan 2025 01:24:43 +0800 Subject: [PATCH 12/13] -format clang --- src/binary-reader-ir.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/binary-reader-ir.cc b/src/binary-reader-ir.cc index 1ffa945f62..ef4c1bc856 100644 --- a/src/binary-reader-ir.cc +++ b/src/binary-reader-ir.cc @@ -1893,8 +1893,9 @@ Result ReadBinaryIr(const char* filename, return ReadBinary(data, size, &reader, options); } -Result ExtractOpcodeRawExpr(OpcodeRawExpr &expr, Module &module, - const ReadBinaryOptions &options) { +Result ExtractOpcodeRawExpr(OpcodeRawExpr& expr, + Module& module, + const ReadBinaryOptions& options) { if (expr.is_extracted) { return Result::Ok; } From 44a34c598498bc3781a84a24d9ff9bbf100aea04 Mon Sep 17 00:00:00 2001 From: guzibei Date: Tue, 28 Jan 2025 17:38:17 +0800 Subject: [PATCH 13/13] -clang format --- src/binary-reader-ir.cc | 7 ++++--- src/expr-visitor.cc | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/binary-reader-ir.cc b/src/binary-reader-ir.cc index ef4c1bc856..8756ed0a4f 100644 --- a/src/binary-reader-ir.cc +++ b/src/binary-reader-ir.cc @@ -28,7 +28,9 @@ #include "wabt/cast.h" #include "wabt/common.h" #include "wabt/ir.h" - +#ifdef WABT_OVERRIDE_ALLOC_EXPR +extern void* operator new(size_t size, wabt::Module* m); +#endif namespace wabt { #ifdef WABT_OVERRIDE_ALLOC_EXPR @@ -37,10 +39,9 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 typename std::__unique_if<_Tp>::__unique_single wabt_expr_make_unique(_Args&&... __args) { // static_assert(sizeof(_Tp) <= 128); - return std::unique_ptr<_Tp>(new ((wabt::Module*)(nullptr)) + return std::unique_ptr<_Tp>(new (static_cast(nullptr)) _Tp(std::forward<_Args>(__args)...)); } -extern void* operator new(size_t size, wabt::Module* m); #else #define wabt_expr_make_unique std::make_unique #endif diff --git a/src/expr-visitor.cc b/src/expr-visitor.cc index 76d818af62..f11d501c9f 100644 --- a/src/expr-visitor.cc +++ b/src/expr-visitor.cc @@ -471,7 +471,7 @@ Result ExprVisitor::HandleDefaultState(Expr* expr) { if (opcode_raw_expr->is_extracted) { PushExprlist(State::OpcodeRaw, expr, opcode_raw_expr->extracted_exprs); } else { - // TODO extract or just skip + // just skip } break; }