Skip to content

Commit

Permalink
Ensure signed constants are correctly parsed, represented, and export…
Browse files Browse the repository at this point in the history
…ed in RTLIL. Add a test to check parsing and exporting
  • Loading branch information
RCoeurjoly committed Aug 19, 2024
1 parent d901b28 commit c88acc6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
3 changes: 3 additions & 0 deletions backends/rtlil/rtlil_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ void RTLIL_BACKEND::dump_const(std::ostream &f, const RTLIL::Const &data, int wi
}
}
f << stringf("%d'", width);
if (data.flags & RTLIL::CONST_FLAG_SIGNED) {
f << stringf("s");
}
if (data.is_fully_undef_x_only()) {
f << "x";
} else {
Expand Down
2 changes: 1 addition & 1 deletion frontends/rtlil/rtlil_lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ USING_YOSYS_NAMESPACE
"\\"[^ \t\r\n]+ { rtlil_frontend_yylval.string = strdup(yytext); return TOK_ID; }
"$"[^ \t\r\n]+ { rtlil_frontend_yylval.string = strdup(yytext); return TOK_ID; }

[0-9]+'[01xzm-]* { rtlil_frontend_yylval.string = strdup(yytext); return TOK_VALUE; }
[0-9]+'s?[01xzm-]* { rtlil_frontend_yylval.string = strdup(yytext); return TOK_VALUE; }
-?[0-9]+ {
char *end = nullptr;
errno = 0;
Expand Down
16 changes: 15 additions & 1 deletion frontends/rtlil/rtlil_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,16 @@ constant:
TOK_VALUE {
char *ep;
int width = strtol($1, &ep, 10);
bool is_signed = false;
if (*ep == '\'') {
ep++;
}
if (*ep == 's') {
is_signed = true;
ep++;
}
std::list<RTLIL::State> bits;
while (*(++ep) != 0) {
while (*ep != 0) {
RTLIL::State bit = RTLIL::Sx;
switch (*ep) {
case '0': bit = RTLIL::S0; break;
Expand All @@ -424,7 +432,9 @@ constant:
case 'm': bit = RTLIL::Sm; break;
}
bits.push_front(bit);
ep++;
}

if (bits.size() == 0)
bits.push_back(RTLIL::Sx);
while ((int)bits.size() < width) {
Expand All @@ -438,6 +448,10 @@ constant:
$$ = new RTLIL::Const;
for (auto it = bits.begin(); it != bits.end(); it++)
$$->bits.push_back(*it);
if (is_signed) {
$$->flags |= RTLIL::CONST_FLAG_SIGNED;
log("Setting SIGNED flag for constant with width %d\n", width);
}
free($1);
} |
TOK_INT {
Expand Down
10 changes: 10 additions & 0 deletions tests/various/rtlil_signed_attribute.ys
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
! mkdir -p temp
read_rtlil <<EOT
module \test
attribute \bottom_bound 3's101
wire width 3 output 1 \a
connect \a 3'001
end
EOT
write_rtlil temp/rtlil_signed_attribute.il
! grep -F -q "attribute \bottom_bound 3's101" temp/rtlil_signed_attribute.il

0 comments on commit c88acc6

Please sign in to comment.