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
|
|
89992
|
use Config; |
|
1
|
|
|
|
|
13
|
|
|
1
|
|
|
|
|
39
|
|
6
|
1
|
|
|
1
|
|
5
|
use IO::File; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
107
|
|
7
|
1
|
|
|
1
|
|
6
|
use File::Path; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
57
|
|
8
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
55
|
|
9
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
4
|
use vars qw($VERSION $Debug); |
|
1
|
|
|
0
|
|
2
|
|
|
1
|
|
|
|
|
2276
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
###################################################################### |
14
|
|
|
|
|
|
|
#### Configuration Section |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
$VERSION = '3.478'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
####################################################################### |
19
|
|
|
|
|
|
|
# CONSTRUCTORS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub new { |
22
|
0
|
|
|
2
|
1
|
0
|
my $class = shift; |
23
|
2
|
|
|
|
|
1815
|
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
|
|
|
|
19
|
$self->{verbose} = 1 if $Debug; |
40
|
2
|
50
|
|
|
|
7
|
$self->{debug} = 1 if $Debug; |
41
|
2
|
|
|
|
|
5
|
bless $self, $class; |
42
|
2
|
|
|
|
|
4
|
return $self; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
###################################################################### |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub read_and_split { |
48
|
2
|
|
|
1
|
1
|
5
|
my $self = shift; |
49
|
1
|
|
|
|
|
27
|
foreach my $filename (@_) { |
50
|
1
|
|
|
|
|
3
|
$self->_read_split_file($filename); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub _read_split_file { |
55
|
1
|
|
|
1
|
|
4
|
my $self = shift; |
56
|
1
|
|
|
|
|
2
|
my $filename = shift; |
57
|
|
|
|
|
|
|
|
58
|
1
|
50
|
|
|
|
2
|
print "Reading $filename...\n" if $self->{verbose}; |
59
|
1
|
50
|
|
|
|
13
|
my $fh = IO::File->new("<$filename") or die "%Error: $! $filename\n"; |
60
|
1
|
|
|
|
|
10
|
(my $basename = $filename) =~ s!^.*/!!; |
61
|
1
|
|
|
|
|
82
|
(my $basemod = $basename) =~ s!\.(v|inc)$!!; |
62
|
|
|
|
|
|
|
|
63
|
1
|
|
|
|
|
7
|
my @header = "// Created by $self->{program} from $basename\n"; |
64
|
1
|
|
|
|
|
55
|
my @trailer = "\n"; |
65
|
|
|
|
|
|
|
|
66
|
1
|
|
|
|
|
3
|
my @lines = (@header); |
67
|
1
|
|
|
|
|
3
|
my $modname; |
68
|
|
|
|
|
|
|
my $ever_module; |
69
|
1
|
|
|
|
|
3
|
my $commented; |
70
|
1
|
|
|
|
|
0
|
while (defined(my $line = $fh->getline)) { |
71
|
1
|
|
|
|
|
39
|
$line =~ s!\r!!mg; |
72
|
26
|
|
|
|
|
615
|
$line =~ s![ \t]+\n$!\n!; |
73
|
26
|
50
|
|
|
|
51
|
if ($self->{translate_synthesis}) { |
74
|
26
|
|
|
|
|
43
|
my $define = $self->{translate_synthesis}; |
75
|
26
|
50
|
|
|
|
33
|
$define = "SYNTHESIS" if $define eq "1"; |
76
|
26
|
|
|
|
|
45
|
$line =~ s!^\s*//\s*(ambit|synopsys|synthesis)\s*translate_off\s*$!`ifndef ${define}\n!; |
77
|
26
|
|
|
|
|
37
|
$line =~ s!^\s*//\s*(ambit|synopsys|synthesis)\s*translate_on\s*$!`endif //${define}\n!; |
78
|
26
|
50
|
|
|
|
32
|
if ($line =~ m!(ambit|synopsys|synthesis)\s*translate!) { |
79
|
26
|
|
|
|
|
48
|
die "%Error: Unhandled translate comment: $line\n"; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
0
|
while ($line =~ m!.*?(/\*|//|\*/)!g) { |
84
|
26
|
100
|
100
|
|
|
76
|
if (!$commented && $1 eq '//') { |
|
|
100
|
66
|
|
|
|
|
|
|
100
|
66
|
|
|
|
|
85
|
7
|
|
|
|
|
41
|
last; |
86
|
|
|
|
|
|
|
} elsif (!$commented && $1 eq '/*') { |
87
|
4
|
|
|
|
|
8
|
$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
|
|
|
|
|
239
|
my $newmodname = $2; |
96
|
3
|
100
|
|
|
|
7
|
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
|
|
|
|
|
3
|
$modname = $newmodname; |
107
|
2
|
|
|
|
|
3
|
$ever_module = 1; |
108
|
2
|
50
|
|
|
|
2
|
print "$basename:$.: module $1\n" if $self->{debug}; |
109
|
2
|
|
|
|
|
5
|
my @afterif; |
110
|
2
|
|
|
|
|
2
|
my @oldlines = (@lines); |
111
|
2
|
|
|
|
|
5
|
@lines = (@header); |
112
|
|
|
|
|
|
|
# Insert our new header before any `ifdef's or `includes |
113
|
2
|
|
|
|
|
4
|
my $gotifdef; |
114
|
2
|
|
|
|
|
3
|
foreach my $oline (@oldlines) { |
115
|
2
|
100
|
|
|
|
3
|
$gotifdef = 1 if $oline =~ /`ifdef\b|`include\b/; |
116
|
12
|
100
|
|
|
|
30
|
if (!$gotifdef) { |
117
|
12
|
|
|
|
|
15
|
push @lines, $oline; |
118
|
|
|
|
|
|
|
} else{ |
119
|
11
|
|
|
|
|
18
|
push @afterif, $oline; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
} |
122
|
1
|
50
|
|
|
|
2
|
push @lines, $self->{include_header} if $self->{include_header}; |
123
|
2
|
50
|
|
|
|
13
|
push @lines, $self->{timescale_header} if $self->{timescale_header}; |
124
|
2
|
50
|
|
|
|
5
|
push @lines, "`celldefine\n" if $self->{celldefine}; |
125
|
2
|
50
|
|
|
|
5
|
push @lines, $self->{lint_header} if $self->{lint_header}; |
126
|
2
|
|
|
|
|
6
|
push @lines, @afterif; |
127
|
|
|
|
|
|
|
} |
128
|
2
|
|
|
|
|
3
|
push @lines, $line; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
elsif (!$commented && $line =~ /^\s*end(module|primitive)\b/) { |
131
|
3
|
50
|
|
|
|
54
|
print "$basename:$.: endmodule $modname\n" if $self->{debug}; |
132
|
2
|
50
|
|
|
|
6
|
$modname or die "%Error: $filename:$.: endmodule without previous module\n"; |
133
|
2
|
|
|
|
|
13
|
push @lines, $line; |
134
|
2
|
50
|
|
|
|
5
|
push @lines, "`endcelldefine\n" if $self->{celldefine}; |
135
|
2
|
|
|
|
|
5
|
push @lines, @trailer; |
136
|
2
|
|
|
|
|
4
|
$self->{_files}{$modname}{created} = 1; |
137
|
2
|
|
|
|
|
5
|
$self->{_files}{$modname}{modname} = $modname; |
138
|
2
|
|
|
|
|
5
|
$self->{_files}{$modname}{lines} = [@lines]; |
139
|
2
|
|
|
|
|
9
|
@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
|
|
|
|
|
40
|
push @lines, $line; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
} |
157
|
19
|
|
|
|
|
343
|
$fh->close; |
158
|
|
|
|
|
|
|
|
159
|
1
|
50
|
|
|
|
38
|
if (!$ever_module) { |
160
|
1
|
0
|
|
|
|
24
|
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
|
|
|
|
|
344
|
mkpath($self->{outdir}); |
175
|
1
|
|
|
|
|
192
|
foreach my $file (sort (keys %{$self->{_files}})) { |
|
1
|
|
|
|
|
4
|
|
176
|
1
|
|
|
|
|
7
|
my $fileref = $self->{_files}{$file}; |
177
|
2
|
50
|
|
|
|
45
|
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
|
|
10
|
my $self = shift; |
184
|
2
|
|
|
|
|
4
|
my $filename = shift; |
185
|
2
|
|
|
|
|
3
|
my $fileref = shift; |
186
|
|
|
|
|
|
|
|
187
|
2
|
50
|
|
|
|
2
|
print "Writing $filename...\n" if $self->{verbose}; |
188
|
|
|
|
|
|
|
|
189
|
2
|
50
|
|
|
|
52
|
my $fh = IO::File->new(">$filename") or die "%Error: $! $filename\n"; |
190
|
2
|
|
|
|
|
18
|
foreach my $line (@{$fileref->{lines}}) { |
|
2
|
|
|
|
|
219
|
|
191
|
2
|
|
|
|
|
6
|
print $fh $line; |
192
|
|
|
|
|
|
|
} |
193
|
34
|
|
|
|
|
61
|
$fh->close; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
sub write_lint { |
197
|
2
|
|
|
1
|
1
|
9
|
my $self = shift; |
198
|
1
|
|
|
|
|
2184
|
my %params = (filename => $self->{outdir}."/0LINT.sh", |
199
|
|
|
|
|
|
|
@_); |
200
|
|
|
|
|
|
|
|
201
|
1
|
50
|
|
|
|
6
|
print "Writing $params{filename}...\n" if $self->{verbose}; |
202
|
|
|
|
|
|
|
|
203
|
1
|
50
|
|
|
|
14
|
my $fh = IO::File->new(">$params{filename}") or die "%Error: $! $params{filename}\n"; |
204
|
1
|
|
|
|
|
10
|
print $fh "#!/bin/bash\n"; |
205
|
1
|
|
|
|
|
109
|
print $fh "# Created by $self->{program}\n"; |
206
|
1
|
|
|
|
|
4
|
foreach my $fileref (sort {$a->{modname} cmp $b->{modname}} values %{$self->{_files}}) { |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
5
|
|
207
|
1
|
50
|
|
|
|
7
|
next if $fileref->{is_include}; |
208
|
2
|
50
|
|
|
|
6
|
next if $fileref->{skip_lint}; |
209
|
2
|
|
|
|
|
5
|
print $fh "echo \"".("*"x70),"\"\n"; |
210
|
2
|
|
|
|
|
5
|
print $fh "echo Lint ".$fileref->{modname},"\n"; |
211
|
2
|
|
|
|
|
6
|
print $fh $self->{lint_command}." \$* ".$fileref->{modname}.$self->{v_suffix},"\n"; |
212
|
|
|
|
|
|
|
} |
213
|
2
|
|
|
|
|
5
|
$fh->close; |
214
|
|
|
|
|
|
|
|
215
|
1
|
|
|
|
|
5
|
chmod 0777, $params{filename}; |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
####################################################################### |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
sub edit_file { |
221
|
1
|
|
|
1
|
1
|
60
|
my $self = shift; |
222
|
|
|
|
|
|
|
my %params = (filename => undef, |
223
|
|
|
|
|
|
|
write_filename => undef, |
224
|
|
|
|
0
|
|
|
cb => sub {}, |
225
|
|
|
|
|
|
|
verbose => $self->{verbose}, |
226
|
1
|
|
|
|
|
298
|
@_); |
227
|
1
|
50
|
|
|
|
22
|
defined $params{filename} or croak "%Error: edit_file not passed filename=>,"; |
228
|
1
|
50
|
|
|
|
7
|
ref $params{cb} or croak "%Error: edit_file cb=> callback is not code,"; |
229
|
1
|
50
|
|
|
|
4
|
$params{write_filename} = $params{filename} if !defined $params{write_filename}; |
230
|
|
|
|
|
|
|
|
231
|
1
|
|
|
|
|
4
|
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
|
|
|
|
|
9
|
local $/; undef $/; |
|
1
|
|
|
|
|
111
|
|
237
|
1
|
|
|
|
|
3
|
$wholefile = <$fh>; |
238
|
1
|
|
|
|
|
29
|
$origwholefile = $wholefile; |
239
|
1
|
|
|
|
|
22
|
$fh->close(); |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
# Edit |
243
|
1
|
|
|
|
|
12
|
$wholefile = &{$params{cb}}($wholefile); |
|
1
|
|
|
|
|
23
|
|
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
# Writeback |
246
|
1
|
50
|
|
|
|
3
|
if ($wholefile ne $origwholefile) { |
247
|
1
|
50
|
|
|
|
18
|
print " $params{write_filename} (Changed)\n" if $params{verbose}; |
248
|
1
|
|
|
|
|
14
|
my ($dev,$ino,$mode) = stat($params{write_filename}); |
249
|
1
|
|
|
|
|
54
|
chmod 0777, $params{filename}; |
250
|
|
|
|
|
|
|
|
251
|
1
|
50
|
|
|
|
62
|
my $fh = IO::File->new (">$params{write_filename}") |
252
|
|
|
|
|
|
|
or croak "%Error: $! writing $params{write_filename},"; |
253
|
1
|
|
|
|
|
11
|
print $fh $wholefile; |
254
|
1
|
|
|
|
|
149
|
$fh->close(); |
255
|
1
|
50
|
|
|
|
5
|
chmod $mode, $params{write_filename} if $mode; # Preserve mode |
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
} |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
####################################################################### |
261
|
|
|
|
|
|
|
1; |
262
|
|
|
|
|
|
|
__END__ |