From f0a0ab100f88d0e2277033418712b881f42aa337 Mon Sep 17 00:00:00 2001 From: Cary R Date: Mon, 8 Dec 2014 12:59:58 -0800 Subject: [PATCH] vlog95: trim binary constants to save space and emit size of undef. consts. This should not change the functionality, but to save space trim any unneeded bits from a binary constant. Also for the case of emitting a signed undefined value when the allow signed flag is not set add the width to the constant. --- tgt-vlog95/numbers.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tgt-vlog95/numbers.c b/tgt-vlog95/numbers.c index 08fea9add..27391008a 100644 --- a/tgt-vlog95/numbers.c +++ b/tgt-vlog95/numbers.c @@ -78,6 +78,7 @@ static void emit_bits(const char *bits, unsigned nbits, unsigned is_signed) { unsigned has_undef = 0; + assert(nbits > 0); /* Check for an undefined bit. */ for (int idx = (int)nbits-1; idx >= 0; idx -= 1) { if ((bits[idx] != '0') && (bits[idx] != '1')) { @@ -91,8 +92,14 @@ static void emit_bits(const char *bits, unsigned nbits, unsigned is_signed) /* Emit as a binary constant. */ if (has_undef || (nbits < 2)) { + int start = nbits - 1; + char sbit = bits[start]; + /* Trim extra leading bits. */ + if (! is_signed && (sbit == '1')) sbit = ' '; + while (start && (sbit == bits[start-1])) start -= 1; + /* Print the trimmed value. */ fprintf(vlog_out, "b"); - for (int idx = (int)nbits-1; idx >= 0; idx -= 1) { + for (int idx = start; idx >= 0; idx -= 1) { fprintf(vlog_out, "%c", bits[idx]); } /* Emit as a hex constant. */ @@ -152,9 +159,9 @@ void emit_number(const char *bits, unsigned nbits, unsigned is_signed, vlog_errors += 1; return; } else if (rtype == -2) { - fprintf(vlog_out, "'bz"); + fprintf(vlog_out, "%u'bz", nbits); } else if (rtype == -3) { - fprintf(vlog_out, "'bx"); + fprintf(vlog_out, "%u'bx", nbits); } else { fprintf(vlog_out, "%"PRId32, value); }