| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # See copyright, etc in below POD section. | 
| 2 |  |  |  |  |  |  | ###################################################################### | 
| 3 |  |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  | package Verilog::EditFiles; | 
| 5 | 1 |  |  | 1 |  | 69942 | use Config; | 
|  | 1 |  |  |  |  | 8 |  | 
|  | 1 |  |  |  |  | 29 |  | 
| 6 | 1 |  |  | 1 |  | 4 | use IO::File; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 105 |  | 
| 7 | 1 |  |  | 1 |  | 6 | use File::Path; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 61 |  | 
| 8 | 1 |  |  | 1 |  | 5 | use Carp; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 37 |  | 
| 9 | 1 |  |  | 1 |  | 4 | use strict; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 32 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 1 |  |  | 1 |  | 5 | use vars qw($VERSION $Debug); | 
|  | 1 |  |  | 0 |  | 2 |  | 
|  | 1 |  |  |  |  | 2100 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | ###################################################################### | 
| 14 |  |  |  |  |  |  | #### Configuration Section | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | $VERSION = '3.480'; | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | ####################################################################### | 
| 19 |  |  |  |  |  |  | # CONSTRUCTORS | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | sub new { | 
| 22 | 0 |  |  | 2 | 1 | 0 | my $class = shift; | 
| 23 | 2 |  |  |  |  | 1369 | my $self = { | 
| 24 |  |  |  |  |  |  | # Options | 
| 25 |  |  |  |  |  |  | program => "Verilog::EditFiles", | 
| 26 |  |  |  |  |  |  | outdir => ".", | 
| 27 |  |  |  |  |  |  | translate_synthesis => 0,	# Name of define or "1" | 
| 28 |  |  |  |  |  |  | lint_header => undef, | 
| 29 |  |  |  |  |  |  | celldefine => undef, | 
| 30 |  |  |  |  |  |  | timescale_header => undef, | 
| 31 |  |  |  |  |  |  | timescale_removal => undef, | 
| 32 |  |  |  |  |  |  | lint_command => 'vlint --brief', | 
| 33 |  |  |  |  |  |  | v_suffix => ".v", | 
| 34 |  |  |  |  |  |  | verbose => 1, | 
| 35 |  |  |  |  |  |  | # Internals | 
| 36 |  |  |  |  |  |  | _files => {},   # Hash of module name, contains list of lines | 
| 37 |  |  |  |  |  |  | @_, | 
| 38 |  |  |  |  |  |  | }; | 
| 39 | 2 | 50 |  |  |  | 14 | $self->{verbose} = 1 if $Debug; | 
| 40 | 2 | 50 |  |  |  | 5 | $self->{debug} = 1 if $Debug; | 
| 41 | 2 |  |  |  |  | 3 | bless $self, $class; | 
| 42 | 2 |  |  |  |  | 2 | return $self; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | ###################################################################### | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | sub read_and_split { | 
| 48 | 2 |  |  | 1 | 1 | 4 | my $self = shift; | 
| 49 | 1 |  |  |  |  | 21 | foreach my $filename (@_) { | 
| 50 | 1 |  |  |  |  | 3 | $self->_read_split_file($filename); | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | sub _read_split_file { | 
| 55 | 1 |  |  | 1 |  | 3 | my $self = shift; | 
| 56 | 1 |  |  |  |  | 1 | my $filename = shift; | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 1 | 50 |  |  |  | 1 | print "Reading $filename...\n" if $self->{verbose}; | 
| 59 | 1 | 50 |  |  |  | 10 | my $fh = IO::File->new("<$filename") or die "%Error: $! $filename\n"; | 
| 60 | 1 |  |  |  |  | 7 | (my $basename = $filename) =~ s!^.*/!!; | 
| 61 | 1 |  |  |  |  | 69 | (my $basemod = $basename) =~ s!\.(v|inc)$!!; | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 1 |  |  |  |  | 5 | my @header = "// Created by $self->{program} from $basename\n"; | 
| 64 | 1 |  |  |  |  | 5 | my @trailer = "\n"; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 1 |  |  |  |  | 2 | my @lines = (@header); | 
| 67 | 1 |  |  |  |  | 1 | my $modname; | 
| 68 |  |  |  |  |  |  | my $ever_module; | 
| 69 | 1 |  |  |  |  | 3 | my $commented; | 
| 70 | 1 |  |  |  |  | 0 | while (defined(my $line = $fh->getline)) { | 
| 71 | 1 |  |  |  |  | 24 | $line =~ s!\r!!mg; | 
| 72 | 26 |  |  |  |  | 489 | $line =~ s![ \t]+\n$!\n!; | 
| 73 | 26 | 50 |  |  |  | 46 | if ($self->{translate_synthesis}) { | 
| 74 | 26 |  |  |  |  | 33 | my $define = $self->{translate_synthesis}; | 
| 75 | 26 | 50 |  |  |  | 27 | $define = "SYNTHESIS" if $define eq "1"; | 
| 76 | 26 |  |  |  |  | 63 | $line =~ s!^\s*//\s*(ambit|synopsys|synthesis)\s*translate_off\s*$!`ifndef ${define}\n!; | 
| 77 | 26 |  |  |  |  | 35 | $line =~ s!^\s*//\s*(ambit|synopsys|synthesis)\s*translate_on\s*$!`endif //${define}\n!; | 
| 78 | 26 | 50 |  |  |  | 27 | if ($line =~ m!(ambit|synopsys|synthesis)\s*translate!) { | 
| 79 | 26 |  |  |  |  | 40 | die "%Error: Unhandled translate comment: $line\n"; | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 0 |  |  |  |  | 0 | while ($line =~ m!.*?(/\*|//|\*/)!g) { | 
| 84 | 26 | 100 | 100 |  |  | 65 | if (!$commented && $1 eq '//') { | 
|  |  | 100 | 66 |  |  |  |  | 
|  |  | 100 | 66 |  |  |  |  | 
| 85 | 7 |  |  |  |  | 33 | last; | 
| 86 |  |  |  |  |  |  | } elsif (!$commented && $1 eq '/*') { | 
| 87 | 4 |  |  |  |  | 5 | $commented = 1; | 
| 88 |  |  |  |  |  |  | } elsif ($commented && $1 eq '*/') { | 
| 89 | 1 |  |  |  |  | 4 | $commented = 0; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 1 | 100 | 66 |  |  | 2 | if (!$commented | 
|  |  | 100 | 66 |  |  |  |  | 
|  |  | 50 | 33 |  |  |  |  | 
|  |  | 100 | 33 |  |  |  |  | 
|  |  |  | 66 |  |  |  |  | 
|  |  |  | 66 |  |  |  |  | 
| 94 |  |  |  |  |  |  | && $line =~ /^\s*(module|primitive)\s+([A-Za-z0-9_]+)/) { | 
| 95 | 26 |  |  |  |  | 199 | my $newmodname = $2; | 
| 96 | 3 | 100 |  |  |  | 6 | if ($modname) { # Already in a module | 
| 97 |  |  |  |  |  |  | # Support code like this | 
| 98 |  |  |  |  |  |  | # `ifdef x | 
| 99 |  |  |  |  |  |  | #   module x (...) | 
| 100 |  |  |  |  |  |  | # `else | 
| 101 |  |  |  |  |  |  | #   module x (...) | 
| 102 | 3 | 50 |  |  |  | 5 | ($newmodname eq $modname) | 
| 103 |  |  |  |  |  |  | or die "%Error: $filename:$.: module without previous endmodule\n"; | 
| 104 | 1 | 50 |  |  |  | 3 | print "$basename:$.:  continue module $1\n" if $self->{debug}; | 
| 105 |  |  |  |  |  |  | } else { | 
| 106 | 1 |  |  |  |  | 2 | $modname = $newmodname; | 
| 107 | 2 |  |  |  |  | 3 | $ever_module = 1; | 
| 108 | 2 | 50 |  |  |  | 2 | print "$basename:$.:  module $1\n" if $self->{debug}; | 
| 109 | 2 |  |  |  |  | 3 | my @afterif; | 
| 110 | 2 |  |  |  |  | 3 | my @oldlines = (@lines); | 
| 111 | 2 |  |  |  |  | 4 | @lines = (@header); | 
| 112 |  |  |  |  |  |  | # Insert our new header before any `ifdef's or `includes | 
| 113 | 2 |  |  |  |  | 4 | my $gotifdef; | 
| 114 | 2 |  |  |  |  | 2 | foreach my $oline (@oldlines) { | 
| 115 | 2 | 100 |  |  |  | 3 | $gotifdef = 1 if $oline =~ /`ifdef\b|`include\b/; | 
| 116 | 12 | 100 |  |  |  | 22 | if (!$gotifdef) { | 
| 117 | 12 |  |  |  |  | 23 | push @lines, $oline; | 
| 118 |  |  |  |  |  |  | } else{ | 
| 119 | 11 |  |  |  |  | 13 | push @afterif, $oline; | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  | } | 
| 122 | 1 | 50 |  |  |  | 2 | push @lines, $self->{include_header} if $self->{include_header}; | 
| 123 | 2 | 50 |  |  |  | 4 | push @lines, $self->{timescale_header} if $self->{timescale_header}; | 
| 124 | 2 | 50 |  |  |  | 2 | push @lines, "`celldefine\n" if $self->{celldefine}; | 
| 125 | 2 | 50 |  |  |  | 5 | push @lines, $self->{lint_header} if $self->{lint_header}; | 
| 126 | 2 |  |  |  |  | 4 | push @lines, @afterif; | 
| 127 |  |  |  |  |  |  | } | 
| 128 | 2 |  |  |  |  | 4 | push @lines, $line; | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  | elsif (!$commented && $line =~ /^\s*end(module|primitive)\b/) { | 
| 131 | 3 | 50 |  |  |  | 47 | print "$basename:$.:  endmodule $modname\n" if $self->{debug}; | 
| 132 | 2 | 50 |  |  |  | 13 | $modname or die "%Error: $filename:$.: endmodule without previous module\n"; | 
| 133 | 2 |  |  |  |  | 4 | push @lines, $line; | 
| 134 | 2 | 50 |  |  |  | 3 | push @lines, "`endcelldefine\n" if $self->{celldefine}; | 
| 135 | 2 |  |  |  |  | 5 | push @lines, @trailer; | 
| 136 | 2 |  |  |  |  | 3 | $self->{_files}{$modname}{created} = 1; | 
| 137 | 2 |  |  |  |  | 9 | $self->{_files}{$modname}{modname} = $modname; | 
| 138 | 2 |  |  |  |  | 5 | $self->{_files}{$modname}{lines} = [@lines]; | 
| 139 | 2 |  |  |  |  | 7 | @lines = (); | 
| 140 |  |  |  |  |  |  | # Prep for next | 
| 141 | 2 |  |  |  |  | 4 | $modname = undef; | 
| 142 |  |  |  |  |  |  | } | 
| 143 |  |  |  |  |  |  | elsif (!$commented | 
| 144 |  |  |  |  |  |  | && $line =~ /^\s*\`timescale\s.*/ | 
| 145 |  |  |  |  |  |  | && $self->{timescale_removal}) { | 
| 146 |  |  |  |  |  |  | # Strip existing timescale | 
| 147 |  |  |  |  |  |  | } | 
| 148 |  |  |  |  |  |  | elsif (!$commented | 
| 149 |  |  |  |  |  |  | && $line =~ /^\s*\`(end)?celldefine\b/ | 
| 150 |  |  |  |  |  |  | && $self->{celldefine}) { | 
| 151 |  |  |  |  |  |  | # Strip existing celldefine, we'll add a new one | 
| 152 |  |  |  |  |  |  | } | 
| 153 |  |  |  |  |  |  | else { | 
| 154 | 2 |  |  |  |  | 33 | push @lines, $line; | 
| 155 |  |  |  |  |  |  | } | 
| 156 |  |  |  |  |  |  | } | 
| 157 | 19 |  |  |  |  | 297 | $fh->close; | 
| 158 |  |  |  |  |  |  |  | 
| 159 | 1 | 50 |  |  |  | 44 | if (!$ever_module) { | 
| 160 | 1 | 0 |  |  |  | 21 | print "$basename:1: No module, must be include file: $basemod\n" if $self->{debug}; | 
| 161 | 0 |  |  |  |  | 0 | push @lines, @trailer; | 
| 162 | 0 |  |  |  |  | 0 | $self->{_files}{$basemod}{created} = 1; | 
| 163 | 0 |  |  |  |  | 0 | $self->{_files}{$basemod}{modname} = $basemod; | 
| 164 | 0 |  |  |  |  | 0 | $self->{_files}{$basemod}{lines} = [@lines]; | 
| 165 | 0 |  |  |  |  | 0 | $self->{_files}{$basemod}{is_include} = 1; | 
| 166 |  |  |  |  |  |  | } | 
| 167 |  |  |  |  |  |  | } | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | ####################################################################### | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | sub write_files { | 
| 172 | 0 |  |  | 1 | 1 | 0 | my $self = shift; | 
| 173 |  |  |  |  |  |  |  | 
| 174 | 1 |  |  |  |  | 266 | mkpath($self->{outdir}); | 
| 175 | 1 |  |  |  |  | 155 | foreach my $file (sort (keys %{$self->{_files}})) { | 
|  | 1 |  |  |  |  | 3 |  | 
| 176 | 1 |  |  |  |  | 7 | my $fileref = $self->{_files}{$file}; | 
| 177 | 2 | 50 |  |  |  | 36 | next if !$fileref->{created}; | 
| 178 | 2 |  |  |  |  | 6 | $self->_write_file($self->{outdir}."/".$fileref->{modname}.$self->{v_suffix}, $fileref); | 
| 179 |  |  |  |  |  |  | } | 
| 180 |  |  |  |  |  |  | } | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | sub _write_file { | 
| 183 | 2 |  |  | 2 |  | 8 | my $self = shift; | 
| 184 | 2 |  |  |  |  | 3 | my $filename = shift; | 
| 185 | 2 |  |  |  |  | 2 | my $fileref = shift; | 
| 186 |  |  |  |  |  |  |  | 
| 187 | 2 | 50 |  |  |  | 2 | print "Writing $filename...\n" if $self->{verbose}; | 
| 188 |  |  |  |  |  |  |  | 
| 189 | 2 | 50 |  |  |  | 60 | my $fh = IO::File->new(">$filename") or die "%Error: $! $filename\n"; | 
| 190 | 2 |  |  |  |  | 17 | foreach my $line (@{$fileref->{lines}}) { | 
|  | 2 |  |  |  |  | 177 |  | 
| 191 | 2 |  |  |  |  | 6 | print $fh $line; | 
| 192 |  |  |  |  |  |  | } | 
| 193 | 34 |  |  |  |  | 47 | $fh->close; | 
| 194 |  |  |  |  |  |  | } | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  | sub write_lint { | 
| 197 | 2 |  |  | 1 | 1 | 7 | my $self = shift; | 
| 198 | 1 |  |  |  |  | 1500 | my %params = (filename => $self->{outdir}."/0LINT.sh", | 
| 199 |  |  |  |  |  |  | @_); | 
| 200 |  |  |  |  |  |  |  | 
| 201 | 1 | 50 |  |  |  | 5 | print "Writing $params{filename}...\n" if $self->{verbose}; | 
| 202 |  |  |  |  |  |  |  | 
| 203 | 1 | 50 |  |  |  | 11 | my $fh = IO::File->new(">$params{filename}") or die "%Error: $! $params{filename}\n"; | 
| 204 | 1 |  |  |  |  | 9 | print $fh "#!/bin/bash\n"; | 
| 205 | 1 |  |  |  |  | 92 | print $fh "# Created by $self->{program}\n"; | 
| 206 | 1 |  |  |  |  | 5 | foreach my $fileref (sort {$a->{modname} cmp $b->{modname}} values %{$self->{_files}}) { | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 4 |  | 
| 207 | 1 | 50 |  |  |  | 6 | next if $fileref->{is_include}; | 
| 208 | 2 | 50 |  |  |  | 6 | next if $fileref->{skip_lint}; | 
| 209 | 2 |  |  |  |  | 3 | print $fh "echo \"".("*"x70),"\"\n"; | 
| 210 | 2 |  |  |  |  | 4 | print $fh "echo Lint ".$fileref->{modname},"\n"; | 
| 211 | 2 |  |  |  |  | 4 | print $fh $self->{lint_command}." \$* ".$fileref->{modname}.$self->{v_suffix},"\n"; | 
| 212 |  |  |  |  |  |  | } | 
| 213 | 2 |  |  |  |  | 5 | $fh->close; | 
| 214 |  |  |  |  |  |  |  | 
| 215 | 1 |  |  |  |  | 3 | chmod 0777, $params{filename}; | 
| 216 |  |  |  |  |  |  | } | 
| 217 |  |  |  |  |  |  |  | 
| 218 |  |  |  |  |  |  | ####################################################################### | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  | sub edit_file { | 
| 221 | 1 |  |  | 1 | 1 | 52 | my $self = shift; | 
| 222 |  |  |  |  |  |  | my %params = (filename => undef, | 
| 223 |  |  |  |  |  |  | write_filename => undef, | 
| 224 |  |  |  | 0 |  |  | cb => sub {}, | 
| 225 |  |  |  |  |  |  | verbose => $self->{verbose}, | 
| 226 | 1 |  |  |  |  | 232 | @_); | 
| 227 | 1 | 50 |  |  |  | 8 | defined $params{filename} or croak "%Error: edit_file not passed filename=>,"; | 
| 228 | 1 | 50 |  |  |  | 4 | ref $params{cb} or croak "%Error: edit_file cb=> callback is not code,"; | 
| 229 | 1 | 50 |  |  |  | 3 | $params{write_filename} = $params{filename} if !defined $params{write_filename}; | 
| 230 |  |  |  |  |  |  |  | 
| 231 | 1 |  |  |  |  | 2 | my $wholefile; | 
| 232 |  |  |  |  |  |  | my $origwholefile; | 
| 233 |  |  |  |  |  |  | {	# Read it | 
| 234 | 1 | 50 |  |  |  | 1 | my $fh = IO::File->new ("<$params{filename}") | 
|  | 1 |  |  |  |  | 2 |  | 
| 235 |  |  |  |  |  |  | or croak "%Error: $! $params{filename},"; | 
| 236 | 1 |  |  |  |  | 5 | local $/; undef $/; | 
|  | 1 |  |  |  |  | 83 |  | 
| 237 | 1 |  |  |  |  | 2 | $wholefile = <$fh>; | 
| 238 | 1 |  |  |  |  | 19 | $origwholefile = $wholefile; | 
| 239 | 1 |  |  |  |  | 19 | $fh->close(); | 
| 240 |  |  |  |  |  |  | } | 
| 241 |  |  |  |  |  |  |  | 
| 242 |  |  |  |  |  |  | # Edit | 
| 243 | 1 |  |  |  |  | 9 | $wholefile = &{$params{cb}}($wholefile); | 
|  | 1 |  |  |  |  | 20 |  | 
| 244 |  |  |  |  |  |  |  | 
| 245 |  |  |  |  |  |  | # Writeback | 
| 246 | 1 | 50 |  |  |  | 4 | if ($wholefile ne $origwholefile) { | 
| 247 | 1 | 50 |  |  |  | 13 | print "  $params{write_filename} (Changed)\n" if $params{verbose}; | 
| 248 | 1 |  |  |  |  | 34 | my ($dev,$ino,$mode) = stat($params{write_filename}); | 
| 249 | 1 |  |  |  |  | 27 | chmod 0777, $params{filename}; | 
| 250 |  |  |  |  |  |  |  | 
| 251 | 1 | 50 |  |  |  | 32 | my $fh = IO::File->new (">$params{write_filename}") | 
| 252 |  |  |  |  |  |  | or croak "%Error: $! writing $params{write_filename},"; | 
| 253 | 1 |  |  |  |  | 8 | print $fh $wholefile; | 
| 254 | 1 |  |  |  |  | 120 | $fh->close(); | 
| 255 | 1 | 50 |  |  |  | 5 | chmod $mode, $params{write_filename} if $mode;  # Preserve mode | 
| 256 |  |  |  |  |  |  | } | 
| 257 |  |  |  |  |  |  | } | 
| 258 |  |  |  |  |  |  |  | 
| 259 |  |  |  |  |  |  |  | 
| 260 |  |  |  |  |  |  | ####################################################################### | 
| 261 |  |  |  |  |  |  | 1; | 
| 262 |  |  |  |  |  |  | __END__ |