Skip to content

Commit

Permalink
tgt-vlog95: Don't strip leading null-bytes from string literals
Browse files Browse the repository at this point in the history
When a string literal is used in a context where it needs to be wider than
it is it will get left-padded with null-bytes. When the vlog95 backend
emits the string literal it will strip the leading null-bytes as it results
in much more legible code.

Unfortunately there are some corner cases where this results in a change of
behavior of the generated code compared to the original. E.g. if the
context that caused the width expansion has been removed by optimization.
`$display(0 ? "Yes" : "No")` should print " No" due to width expansion, but
when running through the vlog95 backend it will print "No".

Another scenario where there is a change in behavior is when a null byte
was explicitly added at the front of a string literal. E.g. $bits("\000ab")
should print 24, but will print 16 when running through the vlog95 backend.

To mitigate this remove the stripping of the leading null-bytes from the
vlog95 backend. This results in slightly less legible code being generated
in some cases, but makes sure that the code is always correct.

Signed-off-by: Lars-Peter Clausen <[email protected]>
  • Loading branch information
larsclausen committed Jan 3, 2023
1 parent 3cfbd73 commit 2e12e47
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions tgt-vlog95/numbers.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,9 @@ static void remove_two_chars(char* str)
void emit_string(const char* string)
{
char *buffer = strdup(string);
char *bptr = buffer;
char *cptr;
fprintf(vlog_out, "\"");
/* Prune any leading escaped NULL bytes. */
while ((bptr[0] == '\\') && (bptr[1] == '0') &&
(bptr[2] == '0') && (bptr[3] == '0')) bptr += 4;
for (cptr = bptr; *cptr; cptr += 1) {
for (cptr = buffer; *cptr; cptr += 1) {
if (*cptr == '\\') {
/* Replace any \011 with \t */
if ((cptr[1] == '0') && (cptr[2] == '1') &&
Expand Down Expand Up @@ -381,7 +377,7 @@ void emit_string(const char* string)
} else cptr += 3;
}
}
if (*bptr) fprintf(vlog_out, "%s", bptr);
if (*buffer) fprintf(vlog_out, "%s", buffer);
free(buffer);
fprintf(vlog_out, "\"");
}

0 comments on commit 2e12e47

Please sign in to comment.