| Verilog Rename Script
Go To Script Pool |
|
| This script allows you to rename Verilog signals across multiple files #!/usr/local/bin/perl -w ##################################################### # List of signals to never crypt # These defines contain a preprocessor directive ##################################################### @files = (); $result = &GetOptions ( "help" => \&usage, "debug" => \&debug, "crypt!" => \$do_crypt, "change!" => \$do_change, "changefile=s" => \$change_filename, "o=s" => \$output_dir, "read!" => \$do_read, "list!" => \$do_list, "xref!" => \$do_xref, "<>" => \¶meter, ); if ($output_dir ne "" && $output_dir !~ /[\/\\]$/) { $output_dir .= "/"; } if (!$result || !@files) { &usage(); } if ($output_dir eq "" && $do_crypt && $do_change) { print STDERR "You must use -o with -crypt or your uncrypted files will be overwritten.\n"; exit (10); } if ($do_read || $do_change) { changes_read ($change_filename); } if ($do_change) { foreach $file (@files) { verilog_change_sig ($file); } } if ($do_list) { foreach $file (@files) { verilog_read_sig ($file); } if ($do_crypt) { changes_crypt(); } changes_from_loc (); changes_write ($change_filename); } exit (0); ##################################################### sub nop{} sub usage { print "Version: $VERSION\n"; print '$Id: vrename,v 1.12 1998/07/13 19:22:42 $SIG{__WARN__} = \&nop; #pod2text isn't clean. pod2text($0); exit (1); } sub version { print "Version: $VERSION\n"; print '$Id: vrename,v 1.12 1998/07/13 19:22:42 wsnyder Exp $ ', "\n"; exit (1); } sub debug { $debug = 1; $Verilog::Parse::debug = 1; } sub parameter { my $param = shift; push @files, $param; (-r $param) or die "Can't open $param"; } #####################################################
sub changes_from_loc { # If a signal was found, but doesn't have change foreach $sig (sort (keys %signal_locs)) { if (! defined $signal_newname{$sig}) { $signal_newname{$sig} = $sig; } } } ##################################################### sub changes_crypt { # Make random names for signals my $sig; foreach $sig (keys %signal_locs) { if (! defined $signal_newname{$sig} && $sig !~ /\$/ && (! defined $vrename_dont_crypt{$sig}) ) { my $has_encry = 0; my $has_uncry = 0; $has_uncry ||= $dont_decrypt{$sig}; foreach $loc (@{$signal_locs{$sig}}) { $has_encry ||= defined $encrypt{$loc}; $has_uncry ||= ! (defined $encrypt{$loc}); } if ($has_encry && !$has_uncry) { $rand = random_string(); while (defined $used_rand{$rand}) { $rand = random_string(); } $used_rand{$rand} = 1; $signal_newname{$sig} = $rand; } } } } sub random_string { $rand = sprintf ("%c%c%c%c%c%c", (rand (26)) + 65, (rand (26)) + 65, (rand (26)) + 65, (rand (26)) + 65, (rand (26)) + 65, (rand (26)) + 65, (rand (26)) + 65, (rand (26)) + 65); } ##################################################### sub changes_write { # Read in the list of signal names to change my $filename = shift; open (TFILE, ">$filename") or die "Can't write $filename."; my $sigcnt=0; print TFILE "# Generated by vrename on ", scalar(localtime), "\n"; print TFILE "#\n"; print TFILE "# Files read for this analysis:\n"; my $file; foreach $file (@files) { print TFILE "vfile\t\"$file\""; if ($encrypt{$file}) { print TFILE "\t-crypt"; } print TFILE "\n"; } print TFILE "#\n"; print TFILE "#\t\Original Signal Name\t\tName to change to\n"; print TFILE "#\t\--------------------\t\t-----------------\n"; print TFILE "#\n"; my $sig; foreach $sig (sort (keys %signal_newname)) { $sigcnt++; print TFILE "sigren\t\"$sig\""; my $len = 8 + 2 + length $sig; while ($len < 32) { print TFILE "\t"; $len += 8; } print TFILE "\t\"$signal_newname{$sig}\""; if ($do_xref) { my $len = 40 + 2 + length $sig; while ($len < 64) { print TFILE "\t"; $len += 8; } print TFILE "\t#"; foreach $loc (@{$signal_locs{$sig}}) { print TFILE "$loc "; } } print TFILE "\n"; } print TFILE "#\n"; print TFILE "# Use M-x compile in emacs to automatically perform the changes:\n"; print TFILE "## Local Variables: ***\n"; print TFILE "## compile-command: \"$0 -change "; foreach $file (@files) { print TFILE $file, " "; } print TFILE "\" ***\n"; print TFILE "## End: ***\n"; close TFILE; print "Wrote $filename (Changes list, $sigcnt signals)\n"; } sub changes_read { # Write out the list of signals in a format for easy editing my $filename = shift; open (TFILE, "<$filename") or die "Can't read $filename."; my $line; while (<TFILE>) { $line = $_; chop $line; $line =~ s/#.*$//; $line =~ s/^[ \t]+//; if ($line =~ /^$/ ) { # comment } elsif ($line =~ /^sigren\s+\"([^\"]+)\"\s+\"([^\"] print "Rename got $1 $2\n" if ($debug); $signal_newname {$1} = $2; } elsif ($line =~ /^vfile/ ) { # ignore } else { die "$filename $.: Can't parse \"$line\"\n"; } } close TFILE; } ##################################################### sub keyword_cb { # Callback from parser when a keyword occurs my $parser = shift; my $what = shift; my $info = shift; $last_keyword = $info; } sub signal_cb { # Callback from parser when a symbol occurs my $parser = shift; my $what = shift; my $info = shift; #print "Signal callback $what $info\n" if ($debug); my $sig = $info; $sig =~ s/\`//g; # Remove `s from define usages else won't match define declaration if (!$modules_sigs{$sig}) { push @{$signal_locs{$sig}}, $cb_filename; } $modules_sigs{$sig} = 1; if ($do_crypt && ($last_keyword eq "module" || $last_keyword eq "function" || $last_keyword eq "task")) { $dont_decrypt{$sig} = 1; $last_keyword = ""; } } sub verilog_read_sig { # Read all signals in this filename my $filename = shift; local %modules_sigs = (); my $fh = new FileHandle; my $parser = new Verilog::Parse; local $last_keyword = ""; local $cb_filename = $filename; $parser->callback ("SYMBOL", \&signal_cb); $parser->callback ("KEYWORD", \&keyword_cb) $fh->open("<$filename") $parser->Verilog::Parse::parse ($fh); if ($do_crypt) { seek ($fh, 0, 0); local $INPUT_RECORD_SEPARATOR = undef; my $filestrg = <$fh>; # Same test below if ($filestrg =~ /ENCRYPT_ME/) { $encrypt{$filename} = 1; } } $fh->close; } #####################################################
sub crypt_string { my $filestrg = shift; my $magicb = "@@@@@!~SAVEVLB~!@@@@@"; my $magice = "@@@@@!~SAVEVLE~!@@@@@"; $filestrg =~ s/(\/[*\/]\s*)[Vv]erilint\s*([0-9]+)\s*off/$magicb Verilint $2 off $magice$1/g; $filestrg =~ s/\/\/.*\n//g; $filestrg =~ s/\/\*[\000-\377]*?\*\///g; $filestrg =~ s/[ \t]+/ /g; $filestrg =~ s/^[ \t]+//g; $filestrg =~ s/[ \t]+$//g; my $oldstrg = $filestrg; $filestrg = "/*ENCRYPTED:VRENAME*/"; my $pos = 0; my $oldpos; my $literal = 0; my $define = 0; for ($oldpos = 0; $oldpos < length $oldstrg; my $char = substr $oldstrg, $oldpos, 1; if ($char eq "\n") { if ($define || $literal) { $filestrg .= $char; $pos = 0; $define = 0; } else { $filestrg .= " "; $pos++; } } elsif ($char eq "`" && !$literal) { my $defkwd = (substr $oldstrg, $oldpos); $defkwd =~ /^(\`[a-z0-9_]*)/; $defkwd = $1; if (Verilog::Language::is_keyword ($defkwd) || (defined $vrename_left_edge_define {$defkwd})) { $filestrg .= "\n"; $pos = 0; $filestrg .= $char; $pos++; $define = 1; } else { $filestrg .= $char; $pos++; } } elsif ($char eq '"') { $filestrg .= $char; $pos++; $literal = ! $literal; } elsif ($char eq " " && !$literal && !$define) { if ($pos > 80) { $filestrg .= "\n"; $pos = 0; } elsif ($pos != 0) { $filestrg .= $char; $pos++; } } else { $filestrg .= $char; $pos++; } } $filestrg =~ s/[ \t]+/ /g; while ($filestrg =~ /$magicb([\000-\377]*?)$magice/) { my $rep = $1; $rep =~ s/[\n]/ /g; $filestrg =~ s/$magicb([\000-\377]*?)$magice/ \/\*$rep\*\/ /; } $filestrg .= "\n"; return $filestrg; }
#################################################### sub verilog_change_sig { # Rename signals in this filename my $filename = shift; # Read in the whole file in a swath local $INPUT_RECORD_SEPARATOR = undef; open (VFILE, "<$filename") or die "Can't read $filename."; my $filestrg = <VFILE>; close VFILE; if ($do_crypt) { if ($filestrg =~ /ENCRYPT_ME/) { $encrypt{$filename} = 1; } } # If crypting, strip comments if ($encrypt{$filename}) { $filestrg = crypt_string ($filestrg); } # Replace any changed signals my $hadrepl = 0; my $sig; my %signal_magic = (); # pass1: replace with magic replacement string # (two steps so renaming a->b and b->a at the foreach $sig (keys %signal_newname) { my $new = $signal_newname{$sig}; if ($new ne $sig) { my $magic = "@@@@@!~${hadrepl}~!@@@@@"; if ($filestrg =~ s/([^a-zA-Z0-9_\$])$sig(?=[^a-zA-Z0-9_])/$1$magic/g) print "match s$sig n$new m$magic\n" if $debug; $hadrepl ++; $signal_magic{$sig} = $magic; } } } # pass2: magic->new foreach $sig (keys %signal_newname) { if (defined $signal_magic{$sig}) { my $magic = $signal_magic{$sig}; my $new = $signal_newname{$sig}; if ($filestrg =~ s/$magic/$new/g) { print "match s$sig n$new m$magic\n" if $debug; } } } # Save it if ($hadrepl || $do_crypt) { open (VFILE, ">$output_dir$filename") print VFILE $filestrg; close VFILE; if ($encrypt{$filename}) {print "Encrypted ";} else {print "Wrote ";} print "$filename ($hadrepl signals matched)\n"; } } __END__ =head1 NAME vrename - change signal names across many verilog =head1 SYNOPSIS C<vrename> I<filename.v> I<filename.v> ... =head1 DESCRIPTION Vrename will allow a signal to be changed across all levels of the design hiearchy using a three step process. module names, macros, and other definitions,
First, use C<vrename --list file.v file2.v ....> This creates a signals.vrename file which is edited manually. Last, use C<vrename --change file.v file2.v ....> =head1 ARGUMENTS vrename takes the following arguments: =over 4 =item --help Displays this message and program version and exits. =item --version Displays program version and exits. =item --change Take the signals file signals.dat in the current directory and change the signals in the design as specified by the signals file. Either --list or --change must be specified. =item --list Create a list of signals in the design and write to signals.dat. Either --list or --change must be specified. =item --xref Include a cross reference of where the signals are used. --list must also be specified. =item --read Read the changes list, allows --list to append to the changes already read. =item --crypt With --list, randomize the signal renames. With --change, compress spaces and comments and apply those renames listed in the file (presumably created with vrename --list --crypt). The comment /*ENCRYPT_ME*/ must be included in all files that need to be encrypted. =item --o {dir} Use the given directory for output instead of the current directory. =item --changefile {file} Use the given filename instead of "signals.vrename". =back =head1 DISTRIBUTION The latest version is available from C<www.ultranet.com/~wsnyder/veripool/rename.html> =head1 AUTHORS Wilson Snyder <wsnyder@ultranet.com> =cut
|
Other Scripts: Batch Simulation
Products: Undertow Suite
|
| Company
Products News
Assistance Download Contact Us Sales Veritools, Inc. |