⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vrename

📁 Verilog Parser in Perl
💻
📖 第 1 页 / 共 2 页
字号:
#!/usr/bin/perl -w# See copyright, etc in below POD section.######################################################################require 5.005;package main;use lib 'blib/arch';use lib 'blib/lib';use lib '.';use Getopt::Long;use IO::File;use IO::Dir;use Pod::Usage;use Verilog::Language;use Verilog::Getopt;use Verilog::Parser;use strict;use vars qw ($VERSION %Vrename_Dont_Crypt %Vrename_Left_Edge_Define	     %Signal_Locs %Signal_Newname %Encrypt	     $Debug $Opt_Xref $Opt_Crypt $Opt_Crypt_All $Opt_Write $Opt_Keywords	     @Files);$VERSION = '3.120';####################################################################### List of signals to never crypt# (Just put temporaries into signals.vrename)foreach (	 'unused_ok',	 '__FILE__',	# Verilator, proposed for Verilog 2005	 '__LINE__',	# Verilator, proposed for Verilog 2005	 'verilator',	 'ms','us','ns','ps','fs',	# Time precision arguments used in `timescale	 'a'..'z', 'A'..'Z',		# Single character names (cryptic enough!)	 ) {    $Vrename_Dont_Crypt{$_} = "";}# These defines contain a preprocessor directive# Thus when crypting we have to keep them at left edge%Vrename_Left_Edge_Define =    ('`time_scale' => "",     '`timescale' => "",     );####################################################################### main# capitalized are globals$Debug = 0;my $opt_change = 0;my $opt_list = 0;$Opt_Xref = 0;$Opt_Crypt = 0;   # Global scope, used in sub-package$Opt_Crypt_All = 0;   # Global scope, used in sub-package$Opt_Write = 1;$Opt_Keywords = 0;my $opt_read = 0;my $change_filename = "signals.vrename";my $output_dir = "";@Files = ();if (! GetOptions (		  "help"	=> \&usage,		  "debug"	=> \&debug,		  "version"	=> sub { print "Version $VERSION\n"; exit(0); },		  "crypt!"	=> \$Opt_Crypt,		  "cryptall!"	=> \$Opt_Crypt_All,		  "change!"	=> \$opt_change,		  "changefile=s"=> \$change_filename,		  "o=s"		=> \$output_dir,		  "read!"	=> \$opt_read,		  "write!"	=> \$Opt_Write,		  "keywords!"	=> \$Opt_Keywords,		  "list!"	=> \$opt_list,		  "xref!"	=> \$Opt_Xref,		  "<>"		=> \&parameter,		  )) {    die "%Error: Bad usage, try 'vrename --help'\n";}$Opt_Crypt = 1 if $Opt_Crypt_All;if ($output_dir ne ""    && $output_dir !~ /[\/\\]$/) {    $output_dir .= "/";}if (!@Files) { &usage(); }if ($output_dir eq "" && $Opt_Crypt && $opt_change) {    print STDERR "You must use -o with -crypt or your uncrypted files will be overwritten.\n";    exit (10);}if ($opt_read || $opt_change) {    changes_read ($change_filename);}if ($opt_change) {    foreach my $file_or_dir (@Files) {	verilog_change_sig ($file_or_dir);    }}if ($opt_list) {    foreach my $file_or_dir (@Files) {	parse_file($file_or_dir);    }    if ($Opt_Crypt) {	changes_crypt();    }    changes_from_loc ();    changes_write ($change_filename);}exit (0);######################################################################sub nop{}sub usage {    print "Version: $VERSION\n";    pod2usage(-verbose=>2, -exitval => 2);    exit (1);}sub debug {    $Debug = 1;    $Verilog::Parser::Debug = $Debug;    $Verilog::Vrename::Reader::Debug = $Debug;}sub parameter {    my $param = shift;    push @Files, $param;    (-r $param) or die "%Error: Can't open $param";}######################################################################sub changes_from_loc {    # If a signal was found, but doesn't have change information, make it    # default to have a change record with replacement same as basename.    foreach my $sig (sort (keys %Signal_Locs)) {	if (! defined $Signal_Newname{$sig}) {	    $Signal_Newname{$sig} = $sig;	}    }}######################################################################sub changes_crypt {    # Make random names for signals    my %used_rand = ();    foreach my $sig (keys %Signal_Locs) {	if (! defined $Signal_Newname{$sig}	    && $sig !~ /\$/	    && (! defined $Vrename_Dont_Crypt{$sig})	    && !Verilog::Language::is_compdirect("`".$sig)	    ) {	    my $has_encry = 0;	    my $has_uncry = 0;	    $has_uncry ||= $main::Dont_Decrypt{$sig};	    if ($Opt_Crypt_All) {		$has_encry = 1;	    } else {		foreach my $loc (@{$Signal_Locs{$sig}}) {		    $has_encry ||= defined $Encrypt{$loc};		    $has_uncry ||= ! (defined $Encrypt{$loc});		}	    }	    if ($has_encry && !$has_uncry) {		my $rand = random_string();		while (defined $used_rand{$rand}) {		    $rand = random_string();		}		$used_rand{$rand} = 1;		$Signal_Newname{$sig} = $rand;	    }	}    }}sub random_string {    while (1) {	my $sig = sprintf ("%c%c%c%c%c%c",			   (rand (26)) + ord('a'),			   (rand (26)) + ord('a'),			   (rand (26)) + ord('a'),			   (rand (26)) + ord('a'),			   (rand (26)) + ord('a'),			   (rand (26)) + ord('a'),			   (rand (26)) + ord('a'),			   (rand (26)) + ord('a'));	return $sig if !Verilog::Language::is_keyword($sig);    }}######################################################################sub changes_write {    # Read in the list of signal names to change    my $filename = shift;    my $fh = IO::File->new(">$filename") or die "%Error: $! $filename.\n";    my $sigcnt=0;    print $fh "# Generated by vrename on ", scalar(localtime), "\n";    print $fh "#\n";    print $fh "# Files read for this analysis:\n";    foreach my $file (@Files) {	print $fh "vfile\t\"$file\"";	if ($Encrypt{$file}) {	    print $fh "\t-crypt";	}	print $fh "\n";    }    print $fh "#\n";    print $fh "#\tOriginal Signal Name\t\tName to change to\n";    print $fh "#\t--------------------\t\t-----------------\n";    print $fh "#\n";    foreach my $sig (sort (keys %Signal_Newname)) {	$sigcnt++;	print $fh "sigren\t\"$sig\"";	my $len = 8 + 2 + length $sig;	while ($len < 32) {	    print $fh "\t";	    $len += 8;	}	print $fh "\t\"$Signal_Newname{$sig}\"";	if ($Opt_Xref) {	    my $len = 40 + 2 + length $sig;	    while ($len < 64) {		print $fh "\t";		$len += 8;	    }	    print $fh "\t#";	    foreach my $loc (@{$Signal_Locs{$sig}}) {		print $fh "$loc ";	    }	}	print $fh "\n";    }    print $fh "#\n";    print $fh "# Use M-x compile in emacs to automatically perform the changes:\n";    print $fh "## Local Variables: ***\n";    print $fh "## compile-command: \"$0 -change ";    foreach my $file (@Files) {	print $fh $file, " ";    }    print $fh "\" ***\n";    print $fh "## End: ***\n";    $fh->close();    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;    print "Read $filename\n";    my $fh = IO::File->new("<$filename") or die "%Error: $! $filename.\n";    while (my $line = $fh->getline()) {	chomp $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 "%Error: $filename $.: Can't parse \"$line\"\n";	}    }    $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/(\/[*\/]\s*)([Ss]ynopsys\s*\S+)/$magicb $2 $magice$1/g;    $filestrg =~ s/\/\*[\000-\377]*?\*\///g;        # block comments    $filestrg =~ s/^\s*\/\/.*\n//g;                 # lines beginning with '//'    $filestrg =~ s/\/\/[^\"\n]*\n//g;               # inline comments with no '"' chars, saves '"////stuf"'    $filestrg =~ s/\/\/[^\"\n]*\"[^\"\n]*\".*\n//g; # inline comments with 2 '"' chars, kills '// "stuff"'    $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; $oldpos++) {	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;	    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -