#!/usr/bin/perl -w # # comstrip.pl - strips all comments from Verilog RTL # There is control over synopsys directives, # and you can remove blank lines. # # By Jeff Winston http://www.kwcpa.com/tools tools@kwcpa.com # # For usage help: Type comstrip.pl with no parameters # # Rev. 1.0, 8/16/09 Copyright (C) 2009 # Rev. 1.1, 10/31/09 Copyright (C) 2009 added support for synopsys comments # # --------------------------------------------------------------------- # 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 the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, # USA. or check out http://www.gnu.org/copyleft/gpl.html # --------------------------------------------------------------------- # # print help if ($#ARGV < 1) {print_help();} # parse input line $noblank=0; $nosynopsys=0; while ($#ARGV > 1) { if ($ARGV[0] eq "-n") { $noblank=1; shift @ARGV; } elsif ($ARGV[0] eq "-s") { $nosynopsys=1; shift @ARGV; } } $infile = shift @ARGV; $outfile = shift @ARGV; # open files open (INFIL,"< $infile") or die "Failed to open $infile"; open (OUT ,"> $outfile") or die "Failed to open $outfile"; # clear global flags $in_comment=0; $re_process=0; # process input file while (1) { # if no lines to re-process, and no new lines, we're done if (($re_process == 0) && (!(defined($line=)))) { last; } # need to chomp if new line, if ($re_process == 0) {chomp $line}; $last_reprocess = $re_process; $re_process=0; # clear re-processing flag, store old value if ($in_comment == 1) { # if we're inside a comment and find */, get remainder of line and process if ($line =~ /\*\//) { $in_comment=0; # no longer in comment $cend = index($line,"*/"); $line = substr($line,$cend+2); $re_process=1; } else { # otherwise, if we're inside a comment, do nothing, except if we're re-processing, # terminate line. if ($last_reprocess == 2) {print OUT "\n";} next; } } else { # not currently inside comment # pick first comment, if any... $c0start = index($line,"/*"); $c1start = index($line,"//"); if ($nosynopsys == 0) { # allow synopsys comments $linetmp = substr($line,$c0start); $linetmp =~ s/\s+/\ /g; if (($linetmp =~ /^\/\* synopsys /) || ($linetmp =~ /^\/\* synopsys /)) { $c0start = -1; } $linetmp = substr($line,$c1start); $linetmp =~ s/\s+/\ /g; if (($linetmp =~ /^\/\/ synopsys /) || ($linetmp =~ /^\/\/ synopsys /)) { $c1start = -1; } } # no comment, output line if (($c0start == -1) && ($c1start == -1)) { if (not_all_white($line)) { print OUT $line . "\n"; } } # otherwise, if single-line comment, comes first, output line up to start of comment elsif (($c0start == -1) || (($c1start > -1) && ($c1start < $c0start))) { $subline = substr($line,0,$c1start); if (not_all_white($subline)) { print OUT $subline. "\n"; } # otherwise, start of /* comment. output the line up to start of comment # and re-process line after comment start (if any), but set in-comment flag } elsif ($c0start != -1) { $subline = substr($line,0,$c0start); $line = substr($line,$c0start+2); $in_comment=1; if (not_all_white($subline)) { print OUT $subline; $re_process=2; # 2 means line not terminated } else { $re_process=1; } } } } # finish up close (INFIL); close (OUT); print "$outfile written\n"; exit; # if enabled, flag strings that are 0 length or all whitespace sub not_all_white { if ($noblank == 0) {return(1);} $str = shift @_; if ((length($str) == 0) || ($str =~ /^\s+$/)) {return(0);} return(1); } sub print_help { print "\nUsage: comstrip.pl [-n] [-s] \n"; print " -n means also remove all blank lines\n\n"; print " -s means also remove all synopsys directives\n\n"; exit; }