vlog95: Don't emit the \n, \t, \" or \\ values as octal constants.

1364-1995 Verilog allows the \n, \t, \" or \\ string escape sequences
so use them when emitting a string.
This commit is contained in:
Cary R 2012-01-06 19:33:19 -08:00
parent 8563218cd2
commit d5b5fbd274
2 changed files with 50 additions and 7 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2011 Cary R. (cygcary@yahoo.com)
* Copyright (C) 2011-2012 Cary R. (cygcary@yahoo.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -500,9 +500,11 @@ static void emit_number_as_string(ivl_net_const_t net_const)
/* Skip any NULL bytes. */
if (val == 0) continue;
/* Print some values that must be escaped. */
/* Print some values that can be escaped. */
if (val == '"') fprintf(vlog_out, "\\\"");
else if (val == '\\') fprintf(vlog_out, "\\\\");
else if (val == '\n') fprintf(vlog_out, "\\n");
else if (val == '\t') fprintf(vlog_out, "\\t");
/* Print the printable characters. */
else if (isprint((int)val)) fprintf(vlog_out, "%c", val);
/* Print the non-printable characters as an octal escape. */

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2011 Cary R. (cygcary@yahoo.com)
* Copyright (C) 2011-2012 Cary R. (cygcary@yahoo.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -317,6 +317,17 @@ int32_t get_int32_from_number(ivl_expr_t expr, int *result_type)
ivl_expr_signed(expr), result_type);
}
/*
* Routine to remove two characters starting at the given address.
*/
static void remove_two_chars(char* str)
{
for (; str[2]; str += 1) {
str[0] = str[2];
}
str[0] = 0;
}
/*
* Routine to print a string value as a string after removing any leading
* escaped NULL bytes.
@ -324,12 +335,42 @@ int32_t get_int32_from_number(ivl_expr_t expr, int *result_type)
void emit_string(const char* string)
{
char *buffer = strdup(string);
char *cptr = buffer;
char *bptr = buffer;
char *cptr;
fprintf(vlog_out, "\"");
/* Prune any leading escaped NULL bytes. */
while ((cptr[0] == '\\') && (cptr[1] == '0') &&
(cptr[2] == '0') && (cptr[3] == '0')) cptr += 4;
if (*cptr) fprintf(vlog_out, "%s", cptr);
while ((bptr[0] == '\\') && (bptr[1] == '0') &&
(bptr[2] == '0') && (bptr[3] == '0')) bptr += 4;
for (cptr = bptr; *cptr; cptr += 1) {
if (*cptr == '\\') {
/* Replace any \011 with \t */
if ((cptr[1] == '0') && (cptr[2] == '1') &&
(cptr[3] == '1')) {
cptr[1] = 't';
remove_two_chars(cptr+2);
cptr += 1;
/* Replace any \012 with \n */
} else if ((cptr[1] == '0') && (cptr[2] == '1') &&
(cptr[3] == '2')) {
cptr[1] = 'n';
remove_two_chars(cptr+2);
cptr += 1;
/* Replace any \042 with \" */
} else if ((cptr[1] == '0') && (cptr[2] == '4') &&
(cptr[3] == '2')) {
cptr[1] = '"';
remove_two_chars(cptr+2);
cptr += 1;
/* Replace any \134 with \\ */
} else if ((cptr[1] == '1') && (cptr[2] == '3') &&
(cptr[3] == '4')) {
cptr[1] = '\\';
remove_two_chars(cptr+2);
cptr += 1;
} else cptr += 3;
}
}
if (*bptr) fprintf(vlog_out, "%s", bptr);
free(buffer);
fprintf(vlog_out, "\"");
}