Skip to content

Commit

Permalink
-format code
Browse files Browse the repository at this point in the history
  • Loading branch information
guzibei committed Jan 26, 2025
1 parent 618f1e7 commit 19837d1
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 99 deletions.
4 changes: 3 additions & 1 deletion include/wabt/binary-reader-nop.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>& opcode_buffer) override { return Result::Ok; }
Result OnSkipFunctionBodyExpr(std::vector<uint8_t>& 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; }
Expand Down
3 changes: 2 additions & 1 deletion include/wabt/binary-reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ class BinaryReaderDelegate {
virtual Result OnDropExpr() = 0;
virtual Result OnElseExpr() = 0;
virtual Result OnEndExpr() = 0;
virtual Result OnSkipFunctionBodyExpr(std::vector<uint8_t>& opcode_buffer) = 0;
virtual Result OnSkipFunctionBodyExpr(
std::vector<uint8_t>& 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;
Expand Down
1 change: 0 additions & 1 deletion include/wabt/expr-visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions include/wabt/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -842,9 +842,13 @@ class AtomicFenceExpr : public ExprMixin<ExprType::AtomicFence> {

class OpcodeRawExpr : public ExprMixin<ExprType::OpCodeRaw> {
public:
explicit OpcodeRawExpr(std::vector<uint8_t>&& in_opcode_buffer, FuncSignature& in_func_sig, const Location& loc = Location())
: ExprMixin<ExprType::OpCodeRaw>(loc), opcode_buffer(std::move(in_opcode_buffer)), is_extracted(false), func_sig(&in_func_sig) {
}
explicit OpcodeRawExpr(std::vector<uint8_t>&& in_opcode_buffer,
FuncSignature& in_func_sig,
const Location& loc = Location())
: ExprMixin<ExprType::OpCodeRaw>(loc),
opcode_buffer(std::move(in_opcode_buffer)),
is_extracted(false),
func_sig(&in_func_sig) {}

std::vector<uint8_t> opcode_buffer;
bool is_extracted;
Expand Down
33 changes: 15 additions & 18 deletions include/wabt/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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 {
Expand Down
35 changes: 21 additions & 14 deletions src/binary-reader-ir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,18 @@ namespace wabt {

#ifdef WABT_OVERRIDE_ALLOC_EXPR
template <class _Tp, class... _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)...));
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 {
Expand Down Expand Up @@ -924,7 +925,8 @@ Result BinaryReaderIR::OnBrTableExpr(Index num_targets,
}

Result BinaryReaderIR::OnCallExpr(Index func_index) {
return AppendExpr(wabt_expr_make_unique<CallExpr>(Var(func_index, GetLocation())));
return AppendExpr(
wabt_expr_make_unique<CallExpr>(Var(func_index, GetLocation())));
}

Result BinaryReaderIR::OnCallIndirectExpr(Index sig_index, Index table_index) {
Expand Down Expand Up @@ -1030,12 +1032,15 @@ Result BinaryReaderIR::OnEndExpr() {
return PopLabel();
}

Result BinaryReaderIR::OnSkipFunctionBodyExpr(std::vector<uint8_t>& opcode_buffer) {
Result result_append = AppendExpr(
wabt_expr_make_unique<OpcodeRawExpr>(std::move(opcode_buffer), current_func_->decl.sig, GetLocation()));
Result BinaryReaderIR::OnSkipFunctionBodyExpr(
std::vector<uint8_t>& opcode_buffer) {
Result result_append = AppendExpr(wabt_expr_make_unique<OpcodeRawExpr>(
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) {
Expand Down Expand Up @@ -1124,7 +1129,7 @@ Result BinaryReaderIR::OnMemoryInitExpr(Index segment, Index memidx) {

Result BinaryReaderIR::OnMemorySizeExpr(Index memidx) {
return AppendExpr(
wabt_expr_make_unique<MemorySizeExpr>(Var(memidx, GetLocation())));
wabt_expr_make_unique<MemorySizeExpr>(Var(memidx, GetLocation())));
}

Result BinaryReaderIR::OnTableCopyExpr(Index dst_index, Index src_index) {
Expand Down Expand Up @@ -1187,7 +1192,8 @@ Result BinaryReaderIR::OnNopExpr() {
}

Result BinaryReaderIR::OnRethrowExpr(Index depth) {
return AppendExpr(wabt_expr_make_unique<RethrowExpr>(Var(depth, GetLocation())));
return AppendExpr(
wabt_expr_make_unique<RethrowExpr>(Var(depth, GetLocation())));
}

Result BinaryReaderIR::OnReturnExpr() {
Expand Down Expand Up @@ -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<ThrowExpr>(Var(tag_index, GetLocation())));
return AppendExpr(
wabt_expr_make_unique<ThrowExpr>(Var(tag_index, GetLocation())));
}

Result BinaryReaderIR::OnThrowRefExpr() {
Expand Down Expand Up @@ -1735,7 +1742,7 @@ Result BinaryReaderIR::OnCodeMetadata(Offset offset,
std::vector<uint8_t> data_(static_cast<const uint8_t*>(data),
static_cast<const uint8_t*>(data) + size);
auto meta = wabt_expr_make_unique<CodeMetadataExpr>(current_metadata_name_,
std::move(data_));
std::move(data_));
meta->loc.offset = offset;
code_metadata_queue_.push_metadata(std::move(meta));
return Result::Ok;
Expand Down
9 changes: 6 additions & 3 deletions src/binary-reader-logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>& 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) \
Expand Down
19 changes: 9 additions & 10 deletions src/binary-writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1151,17 +1151,16 @@ void BinaryWriter::WriteExpr(const Func* func, const Expr* expr) {
break;
}
case ExprType::OpCodeRaw: {
auto* opcode_raw_expr = cast<OpcodeRawExpr>(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<OpcodeRawExpr>(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);
}
}

}
}

Expand Down
5 changes: 3 additions & 2 deletions src/expr-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ Result ExprVisitor::VisitExpr(Expr* root_expr) {
}
case State::OpcodeRaw: {
auto opcode_raw_expr = cast<OpcodeRawExpr>(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()) {
Expand All @@ -160,7 +161,7 @@ Result ExprVisitor::VisitExpr(Expr* root_expr) {
PopExprlist();
}
}

break;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/interp/binary-reader-interp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,8 @@ Result BinaryReaderInterp::OnEndExpr() {
return Result::Ok;
}

Result BinaryReaderInterp::OnSkipFunctionBodyExpr(std::vector<uint8_t>& opcode_buffer) {
Result BinaryReaderInterp::OnSkipFunctionBodyExpr(
std::vector<uint8_t>& opcode_buffer) {
PopLabel();
return Result::Ok;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ir-util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ ModuleContext::Arities ModuleContext::GetExprArity(const Expr& expr) const {
case ExprType::SimdShuffleOp:
return {2, 1};
case ExprType::OpCodeRaw:
return {0, cast<OpcodeRawExpr>(&expr)->func_sig->GetNumResults()};
return {0, cast<OpcodeRawExpr>(&expr)->func_sig->GetNumResults()};
}

WABT_UNREACHABLE;
Expand Down
79 changes: 38 additions & 41 deletions src/stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t*>(realloc_fn(output_data, total_size));
output_data = static_cast<uint8_t*>(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<uint8_t*>(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<uint8_t*>(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)
Expand Down
1 change: 0 additions & 1 deletion src/validator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,6 @@ Result Validator::OnOpcodeRawExpr(OpcodeRawExpr* expr) {
return Result::Ok;
}


Validator::Validator(Errors* errors,
const Module* module,
const ValidateOptions& options)
Expand Down
Loading

0 comments on commit 19837d1

Please sign in to comment.