Skip to content

Commit

Permalink
PrettyPrint enum values as enum values, rather than merely their bit …
Browse files Browse the repository at this point in the history
…patterns.

Before:
  0020:  let _ = assert_eq(EnumType::FIRST, EnumType::SECOND);
~~~~~~~~~~~~~~~~~~~~~~~^---------------------------------^ FailureError: The program being interpreted failed!
  lhs: u1:0
  rhs: u1:1
  were not equal

After:
0020:  let _ = assert_eq(EnumType::FIRST, EnumType::SECOND);
~~~~~~~~~~~~~~~~~~~~~~~^---------------------------------^ FailureError: The program being interpreted failed!
  lhs: EnumType::FIRST (u1:0)
  rhs: EnumType::SECOND (u1:1)
  were not equal

fixes google#86.

(The titular "signed numbers" portion of the git issue seems to have already been
solved for a while.)

PiperOrigin-RevId: 541067811
  • Loading branch information
juttadegener authored and copybara-github committed Jun 17, 2023
1 parent 2f830b6 commit 93581dc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions xls/dslx/bytecode/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ cc_library(
"//xls/dslx:interp_value",
"//xls/dslx:interp_value_helpers",
"//xls/dslx/frontend:ast",
"//xls/dslx/type_system:concrete_type",
"//xls/dslx/type_system:parametric_env",
"//xls/ir:bits_ops",
],
Expand Down
18 changes: 17 additions & 1 deletion xls/dslx/bytecode/bytecode_interpreter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
#include "absl/memory/memory.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"
#include "absl/strings/match.h"
#include "absl/strings/str_split.h"
#include "absl/types/optional.h"
#include "absl/types/span.h"
Expand All @@ -44,6 +44,7 @@
#include "xls/dslx/frontend/ast.h"
#include "xls/dslx/interp_value.h"
#include "xls/dslx/interp_value_helpers.h"
#include "xls/dslx/type_system/concrete_type.h"
#include "xls/ir/bits_ops.h"

namespace xls::dslx {
Expand Down Expand Up @@ -1437,6 +1438,21 @@ absl::StatusOr<std::string> PrettyPrintValue(const InterpValue& value,
struct_type->nominal_type().identifier(),
absl::StrJoin(members, "\n"), indent_str);
}
if (const auto* enum_type = dynamic_cast<const EnumType*>(type);
enum_type != nullptr) {
const EnumDef& enum_def = enum_type->nominal_type();
int member_index = 0;
for (const InterpValue& member : enum_type->members()) {
if (member == value) {
return absl::StrFormat("%s::%s (%s)", enum_def.identifier(),
enum_def.GetMemberName(member_index), member.ToString());
}
++member_index;
}
XLS_RET_CHECK_FAIL()
<< "Unexpected value " << value.ToString() << " as enum "
<< enum_def.identifier();
}
return value.ToString();
}

Expand Down
19 changes: 19 additions & 0 deletions xls/dslx/bytecode/bytecode_interpreter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,25 @@ fn doomed() {
})"))));
}

TEST(BytecodeInterpreterTest, PrettyPrintsEnums) {
constexpr std::string_view kProgram = R"(
enum Flowers {
ROSES = u24:0xFF007F,
VIOLETS = u24:0xEE82EE,
}
fn doomed() {
let a = Flowers::ROSES;
let b = Flowers::VIOLETS;
assert_eq(a, b)
})";
absl::StatusOr<InterpValue> value = Interpret(kProgram, "doomed");
EXPECT_THAT(value.status(), StatusIs(absl::StatusCode::kInternal,
AllOf(
HasSubstr("Flowers::ROSES (u24:16711807"),
HasSubstr("Flowers::VIOLETS (u24:15631086"))));
}

TEST(BytecodeInterpreterTest, TraceChannels) {
constexpr std::string_view kProgram = R"(
proc incrementer {
Expand Down

0 comments on commit 93581dc

Please sign in to comment.