Skip to content

Commit

Permalink
wast2json: Support outputting negative numbers for v128 values
Browse files Browse the repository at this point in the history
  • Loading branch information
Enverbalalic committed Jun 10, 2024
1 parent 4beb525 commit 29241a8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
13 changes: 13 additions & 0 deletions include/wabt/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ struct Const {
void set_externref(uintptr_t x) { From(Type::ExternRef, x); }
void set_null(Type type) { From<uintptr_t>(type, kRefNullBits); }

bool is_negative(int lane = 0) const {
return lane < 16 && is_negative_[lane];
}

void set_is_negative(int lane, bool is_negative) {
if (lane < 16) {
is_negative_[lane] = is_negative;
}
}

bool is_expected_nan(int lane = 0) const {
return expected_nan(lane) != ExpectedNan::None;
}
Expand Down Expand Up @@ -198,6 +208,9 @@ struct Const {
Type type_;
Type lane_type_; // Only valid if type_ == Type::V128.
v128 data_;
// Used for wast2json, we need to know if a number is negative to prefix it
// properly.
bool is_negative_[16];
ExpectedNan nan_[4];
};
using ConstVector = std::vector<Const>;
Expand Down
41 changes: 34 additions & 7 deletions src/binary-writer-spec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,14 @@ void BinaryWriterSpec::WriteConst(const Const& const_) {
WriteString("i32");
WriteSeparator();
WriteKey("value");
json_stream_->Writef("\"%u\"", const_.u32());
json_stream_->Writef("\"%" PRIi32 "\"", const_.u32());
break;

case Type::I64:
WriteString("i64");
WriteSeparator();
WriteKey("value");
json_stream_->Writef("\"%" PRIu64 "\"", const_.u64());
json_stream_->Writef("\"%" PRIi64 "\"", const_.u64());
break;

case Type::F32:
Expand Down Expand Up @@ -266,22 +266,49 @@ void BinaryWriterSpec::WriteConst(const Const& const_) {
json_stream_->Writef("[");

for (int lane = 0; lane < const_.lane_count(); ++lane) {
auto negative_char = const_.is_negative(lane) ? "-" : "";
switch (const_.lane_type()) {
case Type::I8:
json_stream_->Writef("\"%u\"", const_.v128_lane<uint8_t>(lane));
if (const_.is_negative(lane)) {
auto converted = UINT8_MAX - const_.v128_lane<uint8_t>(lane) + 1;
json_stream_->Writef("\"-%u\"", converted);

} else {
json_stream_->Writef("\"%s%u\"", negative_char,
const_.v128_lane<uint8_t>(lane));
}
break;

case Type::I16:
json_stream_->Writef("\"%u\"", const_.v128_lane<uint16_t>(lane));
if (const_.is_negative(lane)) {
auto converted =
UINT16_MAX - const_.v128_lane<uint16_t>(lane) + 1;
json_stream_->Writef("\"-%u\"", converted);
} else {
json_stream_->Writef("\"%u\"", const_.v128_lane<uint16_t>(lane));
}
break;

case Type::I32:
json_stream_->Writef("\"%u\"", const_.v128_lane<uint32_t>(lane));
if (const_.is_negative(lane)) {
auto converted =
UINT32_MAX - const_.v128_lane<uint32_t>(lane) + 1;
json_stream_->Writef("\"-%u\"", converted);
} else {
json_stream_->Writef("\"%s%u\"", negative_char,
const_.v128_lane<uint32_t>(lane));
}
break;

case Type::I64:
json_stream_->Writef("\"%" PRIu64 "\"",
const_.v128_lane<uint64_t>(lane));
if (const_.is_negative(lane)) {
auto converted =
UINT64_MAX - const_.v128_lane<uint64_t>(lane) + 1;
json_stream_->Writef("\"-%" PRIu64 "\"", converted);
} else {
json_stream_->Writef("\"%s%" PRIu64 "\"", negative_char,
const_.v128_lane<uint64_t>(lane));
}
break;

case Type::F32:
Expand Down
2 changes: 2 additions & 0 deletions src/wast-parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2644,6 +2644,8 @@ Result WastParser::ParseSimdV128Const(Const* const_,
if (integer) {
std::string_view sv = Consume().literal().text;

const_->set_is_negative(lane, sv[0] == '-');

switch (lane_count) {
case 16: {
uint8_t value = 0;
Expand Down

0 comments on commit 29241a8

Please sign in to comment.