fmt,cxxrtl: add option to print numeric base (`0x`, etc).

The option is serialized to RTLIL as `#` (to match Python's and Rust's
option with the same symbol), and sets the `show_base` flag. Because
the flag is called `show_base` and not e.g. `alternate_format` (which
is what Python and Rust call it), in addition to the prefixes `0x`,
`0X`, `0o`, `0b`, the RTLIL option also prints the `0d` prefix.
This commit is contained in:
Catherine 2024-03-28 08:33:29 +00:00 committed by Marcelina Kościelnicka
parent bf5a960668
commit 7b94599162
3 changed files with 21 additions and 0 deletions

View File

@ -1041,6 +1041,7 @@ struct fmt_part {
SPACE_MINUS = 2,
} sign; // = MINUS;
bool hex_upper; // = false;
bool show_base; // = false;
// VLOG_TIME type
bool realtime; // = false;
@ -1103,6 +1104,8 @@ struct fmt_part {
}
if (base == 2) {
if (show_base)
buf += "0b";
for (size_t i = width; i > 0; i--)
buf += (val.bit(i - 1) ? '1' : '0');
} else if (base == 8 || base == 16) {
@ -1113,6 +1116,8 @@ struct fmt_part {
value |= val.bit(index + 3) << 3;
buf += (hex_upper ? "0123456789ABCDEF" : "0123456789abcdef")[value];
}
if (show_base)
buf += (base == 16) ? (hex_upper ? "X0" : "x0") : "o0";
std::reverse(buf.begin(), buf.end());
} else if (base == 10) {
bool negative = signed_ && val.is_neg();
@ -1130,6 +1135,8 @@ struct fmt_part {
buf += '0' + remainder.template trunc<4>().template get<uint8_t>();
xval = quotient;
}
if (show_base)
buf += "d0";
switch (sign) {
case MINUS: buf += negative ? "-" : ""; break;
case PLUS_MINUS: buf += negative ? "-" : "+"; break;

View File

@ -152,6 +152,11 @@ void Fmt::parse_rtlil(const RTLIL::Cell *cell) {
// also accept no sign character and treat like MINUS for compatibility
}
if (fmt[i] == '#') {
part.show_base = true;
++i;
}
if (fmt[i] == 'u')
part.signed_ = false;
else if (fmt[i] == 's')
@ -235,6 +240,7 @@ void Fmt::emit_rtlil(RTLIL::Cell *cell) const {
case FmtPart::PLUS_MINUS: fmt += '+'; break;
case FmtPart::SPACE_MINUS: fmt += ' '; break;
}
fmt += part.show_base ? "#" : "";
fmt += part.signed_ ? 's' : 'u';
} else if (part.type == FmtPart::STRING) {
fmt += 'c';
@ -676,6 +682,7 @@ void Fmt::emit_cxxrtl(std::ostream &os, std::string indent, std::function<void(c
}
os << ", ";
os << part.hex_upper << ", ";
os << part.show_base << ", ";
os << part.realtime;
os << " }.render(";
emit_sig(part.sig);
@ -730,6 +737,8 @@ std::string Fmt::render() const
}
if (part.base == 2) {
if (part.show_base)
buf += "0b";
buf = value.as_string();
} else if (part.base == 8 || part.base == 16) {
size_t step = (part.base == 16) ? 4 : 3;
@ -757,6 +766,8 @@ std::string Fmt::render() const
else
buf += (part.hex_upper ? "0123456789ABCDEF" : "0123456789abcdef")[subvalue.as_int()];
}
if (part.show_base)
buf += (part.base == 16) ? (part.hex_upper ? "X0" : "x0") : "o0";
std::reverse(buf.begin(), buf.end());
} else if (part.base == 10) {
bool has_x = false, all_x = true, has_z = false, all_z = true;
@ -792,6 +803,8 @@ std::string Fmt::render() const
buf += '0' + RTLIL::const_mod(absvalue, 10, false, false, 4).as_int();
absvalue = RTLIL::const_div(absvalue, 10, false, false, absvalue.size());
}
if (part.show_base)
buf += "d0";
switch (part.sign) {
case FmtPart::MINUS: buf += negative ? "-" : ""; break;
case FmtPart::PLUS_MINUS: buf += negative ? "-" : "+"; break;

View File

@ -84,6 +84,7 @@ struct FmtPart {
SPACE_MINUS = 2,
} sign = MINUS;
bool hex_upper = false;
bool show_base = false;
// VLOG_TIME type
bool realtime = false;