File: vhier

package info (click to toggle)
libverilog-perl 3.460-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 4,188 kB
  • sloc: perl: 8,640; yacc: 3,362; cpp: 2,262; lex: 1,487; makefile: 8; fortran: 3
file content (579 lines) | stat: -rwxr-xr-x 16,004 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
#!/usr/bin/perl -w
# See copyright, etc in below POD section.
######################################################################

require 5.005;
use FindBin qw($RealBin);
use lib "$RealBin/blib/arch";
use lib "$RealBin/blib/lib";
use lib "$RealBin";

use Getopt::Long;
use IO::File;
use Pod::Usage;

use Verilog::Netlist;
use Verilog::Getopt;
use strict;
use vars qw($Debug $VERSION);

$VERSION = '3.460';

######################################################################
# main

$Debug = 0;
my $opt_output_filename = undef;
my @opt_files;

autoflush STDOUT 1;
autoflush STDERR 1;

# Option parsing
my $Opt = new Verilog::Getopt(filename_expansion=>1);
my $Opt_Cells;
my $Opt_Modules;
my $Opt_ModFiles;
my $Opt_InFiles;
my $Opt_Missing = 1;
my $Opt_Missing_Modules;
my $Opt_TopModule;
my $Opt_ResolveFiles;
my $Opt_Synthesis;
my $Opt_Instance;
my $Opt_Forest;
my $opt_skiplist;
my @Opt_Skiplist_Names;
my $Opt_Xml;

Getopt::Long::config("no_auto_abbrev","pass_through");
GetOptions("debug" => \&debug);  # Snarf --debug ASAP, before parse -f files

@ARGV = $Opt->parameter(@ARGV);
Getopt::Long::config("no_auto_abbrev","no_pass_through");
if (! GetOptions (
		  "help"	=> \&usage,
		  "debug"	=> \&debug,
		  "o=s"		=> \$opt_output_filename,
		  "cells!"	=> \$Opt_Cells,
		  "module-files!"	=> \$Opt_ModFiles,
		  "modules!"		=> \$Opt_Modules,
		  "input-files!"	=> \$Opt_InFiles,
		  "resolve-files!"	=> \$Opt_ResolveFiles,
		  "skiplist=s"		=> \$opt_skiplist,
		  "sv!"			=> sub { shift; Verilog::Language::language_standard("1800-2017"); },
		  "language=s"		=> sub { shift; Verilog::Language::language_standard(shift); },
		  "missing!"		=> \$Opt_Missing,
		  "missing-modules!"	=> \$Opt_Missing_Modules,
		  "synthesis!"		=> \$Opt_Synthesis,
		  "top-module=s"	=> \$Opt_TopModule,
		  "version"	=> sub { print "Version $VERSION\n"; exit(0); },
		  "forest"	=> \$Opt_Forest,
		  "instance"	=> \$Opt_Instance,
		  "xml!"	=> \$Opt_Xml,
		  "<>"		=> \&parameter,
		  )) {
    die "%Error: Bad usage, try 'vhier --help'\n";
}

if (!@opt_files) {
    die "%Error: vhier: No input filenames specified.\n";
}

my $fh = IO::File->new;
if ($opt_output_filename) {
    $fh->open(">$opt_output_filename") or die "%Error: $! $opt_output_filename\n";
} else {
    $fh->open(">-") or die;
}

read_skiplist($opt_skiplist) if $opt_skiplist;

vhier($fh, @opt_files);

exit(0);

######################################################################

sub usage {
    print "Version $VERSION\n";
    pod2usage(-verbose=>2, -exitval=>2, -output=>\*STDOUT, -noperldoc=>1);
    exit(1);
}

sub debug {
    $Debug = 1;
}

sub parameter {
    my $param = shift;
    if ($param =~ /^--?/) {
	die "%Error: vhier: Unknown parameter: $param\n";
    } else {
	push @opt_files, "$param"; # Must quote to convert Getopt to string, bug298
    }
}

######################################################################
#### Creation

sub vhier {
    my $fh = shift;
    my @files = @_;

    my $nl = new Verilog::Netlist(options => $Opt,
				  keep_comments => 0,
				  use_vars => 0,
				  link_read_nonfatal => !$Opt_Missing,
				  synthesis => $Opt_Synthesis,
				  );

    $fh->print("<vhier>\n") if $Opt_Xml;

    if ($Opt_ResolveFiles) {
	$nl->read_libraries();
	$fh->print(" <resolve_files>\n") if $Opt_Xml;
	foreach my $file (@files) {
	    if (my $resolved = $nl->resolve_filename ($file, "all")) {
		$fh->print( "$resolved\n") if !$Opt_Xml;
		$fh->print( "  <file>$resolved</file>\n") if $Opt_Xml;
	    }
	}
	$fh->print(" </resolve_files>\n") if $Opt_Xml;
	return;
    }
    foreach my $file (@files) {
	print "   Reading $file\n" if $Debug;
	$nl->read_file(filename=>$file);
    }
    # Read in any sub-modules
    $nl->link();
    $nl->lint();  # Simplified as use_vars => 0
    $nl->exit_if_error();

    if ($Opt_TopModule) {
	my $topmod = $nl->find_module($Opt_TopModule)
	    or die "%Error: --top-module '$Opt_TopModule' was not found.\n";
	$topmod->is_top(1);

	# We could just pass this to all of the following routines,
	# but each would need a different edit.  Instead, just edit the netlist
	# to contain only the specified tree.
	my %marked_modules;
	_mod_mark_recurse($nl, $topmod, \%marked_modules);

	foreach my $mod ($nl->modules_sorted) {
	    if (!$marked_modules{$mod->name}) {
		$mod->delete;
	    }
	}
    }

    if ($Opt_Cells || $Opt_Forest) {
	$fh->print(" <cells>\n") if $Opt_Xml;
	foreach my $mod ($nl->modules_sorted) {
	    if ($mod->is_top) {
		my %recursing;
		show_hier($fh, \%recursing, $mod, undef, "  ", $mod->name);
	    }
	}
	$fh->print(" </cells>\n") if $Opt_Xml;
    }

    if ($Opt_Modules) {
	show_module_names($nl, $fh);
    }

    if ($Opt_ModFiles) {
	show_mod_files($nl, $fh);
    }

    if ($Opt_InFiles) {
	$fh->print(" <input_files>\n") if $Opt_Xml;
	foreach my $filename ($Opt->depend_files) {
	    $fh->print("  $filename\n") if !$Opt_Xml;
	    $fh->print("  <file>$filename</file>\n") if $Opt_Xml;
	}
	$fh->print(" </input_files>\n") if $Opt_Xml;
    }

    if ($Opt_Missing_Modules) {
	show_missing_module_names($nl,$fh);
    }

    $fh->print("</vhier>\n") if $Opt_Xml;
}

sub show_module_names {
    my $nl = shift;
    my $fh = shift;
    $fh->print(" <modules>\n") if $Opt_Xml;
    foreach my $mod ($nl->modules_sorted) {
	$fh->printf("  %s\n", $mod->name) if !$Opt_Xml;
	$fh->printf("  <module name=\"%s\" />\n", $mod->name) if $Opt_Xml;
    }
    $fh->print(" </modules>\n") if $Opt_Xml;
}

sub show_missing_module_names {
    my $nl = shift;
    my $fh = shift;

    my %miss_names;
    foreach my $mod ($nl->modules) {
	foreach my $cell ($mod->cells_sorted) {
	    if (!$cell->submod && !$cell->gateprim) {
		$miss_names{$cell->submodname} = 1;
	    }
	}
    }

    $fh->print(" <missing_modules>\n") if $Opt_Xml;
    foreach my $key (sort (keys %miss_names)) {
	$fh->printf("  %s\n",$key) if !$Opt_Xml;
	$fh->printf("  <module name=\"%s\" />\n",$key) if $Opt_Xml;
    }
    $fh->print(" </missing_modules>\n") if $Opt_Xml;
}

sub show_mod_files {
    my $nl = shift;
    my $fh = shift;
    # We'll attach a level attribute to each module indicating its maximum depth
    foreach my $mod ($nl->modules, $nl->interfaces) {
	$mod->attributes("_vhier_level", 0);
    }
    # Recurse the tree and determine level
    foreach my $mod ($nl->modules, $nl->interfaces) {
	if ($mod->is_top) {
	    my %recursing;
	    _mod_files_recurse($mod, 1, \%recursing);
	}
    }
    # Make sort key based on numeric level
    my %keys;
    foreach my $mod ($nl->modules) { # No interfaces, it's --module-files implying modules only
	my $key = sprintf("%03d_%s", $mod->attributes("_vhier_level"), $mod->name);
	$keys{$key} = $mod;
    }
    my @files;
    my %files;  # Uniquify the array
    foreach my $key (sort {$b cmp $a} (keys %keys)) {
	my $mod = $keys{$key};
	my $filename = $mod->filename;
	if (!$files{$filename}) {
	    $files{$filename} = 1;
	    push @files, [" "x($mod->attributes("_vhier_level")), $filename];
	}
    }
    $fh->print(" <module_files>\n") if $Opt_Xml;
    foreach my $filespace (reverse @files) {
	$fh->printf("  %s%s\n",$filespace->[0],$filespace->[1]) if !$Opt_Xml;
	$fh->printf("  %s<file>%s</file>\n",$filespace->[0],$filespace->[1]) if $Opt_Xml;
    }
    $fh->print(" </module_files>\n") if $Opt_Xml;
}

sub _mod_mark_recurse {
    my $nl = shift;
    my $mod = shift;
    my $marked = shift;

    return if $marked->{$mod->name}++;
    foreach my $cell ($mod->cells_sorted) {
	if ($cell->submod) {
	    _mod_mark_recurse($nl, $cell->submod, $marked);
	}
    }
}

sub _mod_files_recurse {
    my $mod = shift;
    my $level = shift;
    my $recursing = shift;

    my $name = $mod->name;
    return if $recursing->{$name};
    ++$recursing->{$name};
    if (($mod->attributes("_vhier_level")||0) < $level) {
	$mod->attributes("_vhier_level", $level);
    }
    foreach my $cell ($mod->cells_sorted) {
	if ($cell->submod) {
	    _mod_files_recurse($cell->submod, $level+1, $recursing);
	}
    }
    --$recursing->{$name};
}

sub show_hier {
    my $fh = shift;
    my $recursing = shift;
    my $mod = shift;
    my $parcell = shift;
    my $indent = shift;
    my $hier = shift;
    my $submodname = shift;
    # print the design hierarchy starting at mod (recursive)

    my $name = $mod->name;

    # check if mod name has match in regex list to skip
    if (grep { $name =~ $_ } @Opt_Skiplist_Names) {
        print "Debug: Skipping module $name" if $Debug;
        return;
    }

    return if $recursing->{$name};
    ++$recursing->{$name};

    printf "%-38s %s\n", $indent."Module ".$name,$hier if $Debug;
    my $instance = $parcell ? $parcell->name : $name;

    # print the mod instance
    $Opt_Xml ?
        $fh->printf("%s<cell name=\"%s\" submodname=\"%s\" hier=\"%s\">\n",
            $indent, $parcell ? $parcell->name : $name, $name, $hier) :
            $Opt_Instance ?
                $fh->printf("%s%s %s\n", $indent, $instance, $name) :
                $fh->printf("%s%s\n",    $indent,            $name);

    # print the design hierarchy of each cell in mod
    my $i = 0;
    my $suffix;

    my @cellCount = $mod->cells_sorted; # Returns list of name sorted references to Verilog::Netlist::Cell in the module
    $fh->printf("\t\t%d cells_sorted for %s\n", $#cellCount+1, $name) if $Debug;

    my @subMods = grep($_->submod, $mod->cells_sorted); # list of submods of the current mod

    $fh->printf("\t\t%d submods in cells_sorted.\t", $#subMods+1) if $Debug;
    $fh->printf("\t\tsubmods=%s\n", join(',',map($_->name, @subMods))) if $Debug;

    foreach my $cell ($mod->cells_sorted) {
        if ($cell->submod) {    # Reference to the Verilog::Netlist::Module the cell instantiates
	    # true if the cell refers to a defined module

            # set the suffix characters used to indent lower level of hierarchy
            if ($Opt_Forest) {
                if ($i < $#subMods) {
                    $suffix = "|--";    # indent chars for not last cell in the module
                } else {
                    $suffix = "\\--";    # tweak indent chars for last cell in the module
                }
                $indent =~ tr/-\\/  /;   # clear out higher-level indent chars
            } else {
                $suffix = "  "; # simple indenting with spaces
            }
            show_hier($fh, $recursing, $cell->submod, $cell, $indent.$suffix, $hier.".".$cell->name);
            $i++;
        }
    }
    $fh->printf("%s</cell>\n", $indent) if $Opt_Xml;
    --$recursing->{$name};
}

sub read_skiplist {
    my $filename = shift;
    my $fh = IO::File->new("<$filename") or die "%Error: $! $filename,";
    while (<$fh>) {
            chomp;
            push @Opt_Skiplist_Names, $_;
    }
    $fh->close;
    if ($Debug) {
	print "Debug: skiplist:\n";
	foreach (@Opt_Skiplist_Names) { print "\t$_\n"; }
    }
}

######################################################################
######################################################################
######################################################################

__END__

=pod

=head1 NAME

vhier - Return all files in a verilog hierarchy using Verilog::Netlist

=head1 SYNOPSIS

  vhier --help
  vhier [verilog_options] [-o filename] [verilog_files.v...]

=head1 DESCRIPTION

Vhier reads the Verilog files passed on the command line and outputs a tree
of all of the filenames, modules, and cells referenced by that file.

=head1 VERILOG ARGUMENTS

The following arguments are compatible with GCC, VCS and most Verilog
programs.

=over 4

=item +define+I<var>+I<value>
=item -DI<var>=I<value>

Defines the given preprocessor symbol.

=item -F I<file>

Read the specified file, and act as if all text inside it was specified as
command line parameters.  Any relative paths are relative to the directory
containing the specified file.

=item -f I<file>

Read the specified file, and act as if all text inside it was specified as
command line parameters.  Any relative paths are relative to the current
directory.

=item +incdir+I<dir>
=item -II<dir>

Add the directory to the list of directories that should be searched
for include directories or libraries.

=item +libext+I<ext>+I<ext>...

Specify the extensions that should be used for finding modules.  If for
example module I<x> is referenced, look in I<x>.I<ext>.

=item -sv

Specifies SystemVerilog language features should be enabled; equivalent to
"--language 1800-2017".  This option is selected by default, it exists for
compatibility with other simulators.

=item -y I<dir>

Add the directory to the list of directories that should be searched
for include directories or libraries.

=back

=head1 VHIER ARGUMENTS

=over 4

=item --help

Displays this message and program version and exits.

=item --o I<file>

Use the given filename for output instead of stdout.

=item --cells

Show the module name of all cells in top-down order.

=item --forest

Show "ASCII-art" hierarchy tree of all cells (like ps --forest)

=item --input-files

Show all input filenames.  Copying all of these files should result in only
those files needed to represent the entire design.

=item --instance

With --cells or --forest, show module instance names.

=item --language <1364-1995|1364-2001|1364-2005|1800-2005|1800-2009|1800-2012|1800-2017>

Set the language standard for the files.  This determines which tokens are
signals versus keywords, such as the ever-common "do" (data-out signal,
versus a do-while loop keyword).

=item --resolve-files

Show resolved filenames passed on the command line.  This will convert raw
module and filenames without paths to include the library search path
directory.  Output filenames will be in the same order as passed on the
command line.  Unlike --input-files or --module-files, hierarchy is not
traversed.

=item --module-files

Show all module filenames in top-down order.  Child modules will always
appear as low as possible, so that reversing the list will allow bottom-up
processing of modules.  Unlike input-files, header files are not included.

=item --modules

Show all module names.

=item --no-missing

Do not complain about references to missing modules.

=item --missing-modules

With --nomissing, show all modules that are not found.

=item --skiplist I<file>

Given file contains a list of regular expressions, one per line. If a
module name in the design hierarchy matches one of these expressions, skip
showing that module and any sub-hierarchy.

=item --synthesis

Define SYNTHESIS, and ignore text bewteen "ambit", "pragma", "synopsys" or
"synthesis" translate_off and translate_on meta comments.  Note using
metacomments is discouraged as they have led to silicon bugs (versus ifdef
SYNTHESIS); see
L<http://www.veripool.org/papers/TenIPEdits_SNUGBos07_paper.pdf>.

=item --top-module I<module>

Start the report at the specified module name, ignoring all modules that
are not the one specified with --top-module or below, and report an error
if the --top-module specified does not exist.  Without this option vhier
will report all modules, starting at the module(s) that have no children
below them.

Note this option will not change the result of the --input-files list,
as the files needed to parse any design are independent of which modules
are used.

=item --version

Displays program version and exits.

=item --xml

Create output in XML format.

=back

=head1 DISTRIBUTION

Verilog-Perl is part of the L<http://www.veripool.org/> free Verilog EDA
software tool suite.  The latest version is available from CPAN and from
L<http://www.veripool.org/verilog-perl>.

Copyright 2005-2019 by Wilson Snyder.  This package is free software; you
can redistribute it and/or modify it under the terms of either the GNU
Lesser General Public License Version 3 or the Perl Artistic License Version 2.0.

=head1 AUTHORS

Wilson Snyder <wsnyder@wsnyder.org>

=head1 SEE ALSO

L<Verilog-Perl>,
L<Verilog::Getopt>,
L<Verilog::Preproc>,
L<Verilog::Netlist>

=cut
######################################################################