Difference between revisions of "Access attributes of ports in parsetree"

From Verific Design Automation FAQ
Jump to: navigation, search
 
Line 1: Line 1:
 
Perl script:
 
Perl script:
 
  <nowiki>
 
  <nowiki>
#!/usr/bin/perl
+
#!/usr/bin/perl -w
 
+
 +
use warnings ;
 
use strict ;
 
use strict ;
 
+
 
push(@INC, "../pm") ;
 
push(@INC, "../pm") ;
 
require "Verific.pm" ;
 
require "Verific.pm" ;
 
+
if (!Verific::veri_file::Read("test.v")) { exit 1 ; }
+
if (!Verific::veri_file::Analyze("test.v")) { exit 1 ; }
 
+
my $mod = Verific::veri_file::GetModule("test") ;
+
ShowAttributes(Verific::veri_file::GetModule("test1")) ;
if (!$mod) { exit 2 ; }
+
ShowAttributes(Verific::veri_file::GetModule("test2")) ;
 
+
# Attributes from parse tree port ids:
+
exit 0;
my $iter = new Verific::VeriIdDefArrayIter($mod->GetPorts()) ;
+
for (my $port = $iter->First(); $iter->GetIndex() < $iter->Size(); $port = $iter->Next()) {
+
sub ShowAttributes {
    if (!$port) { next ; }
+
    my ($mod) = @_ ;
 
+
    if (!$mod) { return ; }
    print "Port: " . $port->Name() . "\n" ;
+
    my $map_iter = Verific::Map::Iterator($port->GetAttributes(), "char", "Verific::VeriExpression") ;
+
    $mod->Info("Checking module " . $mod->Name()) ;
    my $map_key ;
+
    my $map_value ;
+
    # Attributes from parse tree port ids:
    while (($map_key, $map_value) = $map_iter->Next()) {
+
    my $iter = new Verific::VeriIdDefArrayIter($mod->GetPorts()) ;
        print "\tAttribute: $map_key -> " . $map_value->GetPrettyPrintedString() . "\n" ;
+
    for (my $port = $iter->First(); $iter->GetIndex() < $iter->Size(); $port = $iter->Next()) {
 +
        if (!$port) { next ; }
 +
 +
        print "Port: " . $port->Name() . "\n" ;
 +
        my $map_iter = Verific::Map::Iterator($port->GetAttributes(), "char", "Verific::VeriExpression") ;
 +
        my $map_key ;
 +
        my $map_value ;
 +
        while (($map_key, $map_value) = $map_iter->Next()) {
 +
            print "\tAttribute: $map_key -> " . $map_value->GetPrettyPrintedString() . "\n" ;
 +
        }
 
     }
 
     }
}
+
 
+
    # Attributes from port declarations/expression:
# Attributes from parse tree port declarations:
+
    $iter = new Verific::VeriExpressionArrayIter($mod->GetPortConnects()) ;
my $iter = new Verific::VeriModuleItemArrayIter($mod->GetModuleItems()) ;
+
    for (my $port_expr = $iter->First(); $iter->GetIndex() < $iter->Size(); $port_expr = $iter->Next()) {
for (my $mod_item = $iter->First(); $iter->GetIndex() < $iter->Size(); $mod_item = $iter->Next()) {
+
        if (!$port_expr) { next ; }
    if (!$mod_item || !$mod_item->IsDataDecl()) { next ; }
+
 
 
+
        print "Port declaration: " . $port_expr->GetPrettyPrintedString() . "\n" ;
    print "Module item: " . $mod_item->GetPrettyPrintedString() ;
+
        my $map_iter = Verific::Map::Iterator($port_expr->GetAttributes(), "char", "Verific::VeriExpression") ;
    my $map_iter = Verific::Map::Iterator($mod_item->GetAttributes(), "char", "Verific::VeriExpression") ;
+
        my $map_key ;
    my $map_key ;
+
        my $map_value ;
    my $map_value ;
+
        while (($map_key, $map_value) = $map_iter->Next()) {
    while (($map_key, $map_value) = $map_iter->Next()) {
+
            print "\tAttribute: $map_key -> " . $map_value->GetPrettyPrintedString() . "\n" ;
        print "\tAttribute: $map_key -> " . $map_value->GetPrettyPrintedString() . "\n" ;
+
        }
 
     }
 
     }
}
+
 
+
    # Attributes from parse tree port declarations:
# Attributes from netlist ports:
+
    $iter = new Verific::VeriModuleItemArrayIter($mod->GetModuleItems()) ;
my @ports = Verific::FOREACH_PORT_OF_NETLIST(Verific::Netlist::PresentDesign()) ;
+
    for (my $mod_item = $iter->First(); $iter->GetIndex() < $iter->Size(); $mod_item = $iter->Next()) {
foreach my $port ( @ports ) {
+
        if (!$mod_item || !$mod_item->IsDataDecl()) { next ; }
    if (!$port) { next ; }
+
 
 
+
        print "Module item: " . $mod_item->GetPrettyPrintedString() ;
    print "Port: " . $port->Name() . "\n" ;
+
        my $map_iter = Verific::Map::Iterator($mod_item->GetAttributes(), "char", "Verific::VeriExpression") ;
    my @atts = Verific::FOREACH_ATTRIBUTE($port) ;
+
        my $map_key ;
    foreach my $att ( @atts ) {
+
         my $map_value ;
         if (!$att) { next ; }
+
        while (($map_key, $map_value) = $map_iter->Next()) {
        print "\tAttribute: " . $att->Key() . " -> " . $att->Value() . "\n" ;
+
            print "\tAttribute: $map_key -> " . $map_value->GetPrettyPrintedString() . "\n" ;
 +
        }
 
     }
 
     }
 
}
 
}
 
exit 0;
 
 
  </nowiki>
 
  </nowiki>
 
Verilog testcase:
 
Verilog testcase:
 
  <nowiki>
 
  <nowiki>
module test (in, out) ;
+
module test1 (
     (* attr1 = 0, attr2 = 1 *) input in /* verific a */ ;
+
        (* attr1 = 0, attr2 = 1 *) input in ,
     (* attr3 = 2, attr4 = 3 *) output out /* verific b */ ;
+
        (* attr3 = 2, attr4 = 3 *) output out
 
+
    );
 +
 +
    assign out = in ;
 +
endmodule
 +
 +
module test2 (in, out) ;
 +
     (* attr1 = 0, attr2 = 1 *) input in ;
 +
     (* attr3 = 2, attr4 = 3 *) output out ;
 +
 
     assign out = in ;
 
     assign out = in ;
 
endmodule
 
endmodule
Line 68: Line 85:
 
  <nowiki>
 
  <nowiki>
 
-- Analyzing Verilog file 'test.v' (VERI-1482)
 
-- Analyzing Verilog file 'test.v' (VERI-1482)
test.v(1): INFO: compiling module 'test' (VERI-1018)
+
test.v(7): INFO: Checking module test1
 
Port: in
 
Port: in
Attribute: a -> 1
 
 
Port: out
 
Port: out
Attribute: b -> 1
+
Port declaration:
Module item: (* attr1=0, attr2=1 *) input in /* verific a=1 */  ;
+
(* attr1=0, attr2=1 *) input in
Attribute: attr1 -> 0
+
        Attribute: attr1 -> 0
Attribute: attr2 -> 1
+
        Attribute: attr2 -> 1
Module item: (* attr3=2, attr4=3 *) output out /* verific b=1 */  ;
+
Port declaration:
Attribute: attr3 -> 2
+
(* attr3=2, attr4=3 *) output out
Attribute: attr4 -> 3
+
        Attribute: attr3 -> 2
 +
        Attribute: attr4 -> 3
 +
test.v(14): INFO: Checking module test2
 
Port: in
 
Port: in
Attribute: attr1 -> 0
 
Attribute: attr2 -> 1
 
Attribute: a -> 1
 
 
Port: out
 
Port: out
Attribute: attr3 -> 2
+
Port declaration: in
Attribute: attr4 -> 3
+
Port declaration: out
Attribute: b -> 1
+
Module item: (* attr1=0, attr2=1 *) input in ;
 +
        Attribute: attr1 -> 0
 +
        Attribute: attr2 -> 1
 +
Module item: (* attr3=2, attr4=3 *) output out ;
 +
        Attribute: attr3 -> 2
 +
        Attribute: attr4 -> 3
 
  </nowiki>
 
  </nowiki>

Latest revision as of 14:10, 9 July 2020

Perl script:

#!/usr/bin/perl -w
 
use warnings ;
use strict ;
 
push(@INC, "../pm") ;
require "Verific.pm" ;
 
if (!Verific::veri_file::Analyze("test.v")) { exit 1 ; }
 
ShowAttributes(Verific::veri_file::GetModule("test1")) ;
ShowAttributes(Verific::veri_file::GetModule("test2")) ;
 
exit 0;
 
sub ShowAttributes {
    my ($mod) = @_ ;
    if (!$mod) { return ; }
 
    $mod->Info("Checking module " . $mod->Name()) ;
 
    # Attributes from parse tree port ids:
    my $iter = new Verific::VeriIdDefArrayIter($mod->GetPorts()) ;
    for (my $port = $iter->First(); $iter->GetIndex() < $iter->Size(); $port = $iter->Next()) {
        if (!$port) { next ; }
 
        print "Port: " . $port->Name() . "\n" ;
        my $map_iter = Verific::Map::Iterator($port->GetAttributes(), "char", "Verific::VeriExpression") ;
        my $map_key ;
        my $map_value ;
        while (($map_key, $map_value) = $map_iter->Next()) {
            print "\tAttribute: $map_key -> " . $map_value->GetPrettyPrintedString() . "\n" ;
        }
    }
 
    # Attributes from port declarations/expression:
    $iter = new Verific::VeriExpressionArrayIter($mod->GetPortConnects()) ;
    for (my $port_expr = $iter->First(); $iter->GetIndex() < $iter->Size(); $port_expr = $iter->Next()) {
        if (!$port_expr) { next ; }
  
        print "Port declaration: " . $port_expr->GetPrettyPrintedString() . "\n" ;
        my $map_iter = Verific::Map::Iterator($port_expr->GetAttributes(), "char", "Verific::VeriExpression") ;
        my $map_key ;
        my $map_value ;
        while (($map_key, $map_value) = $map_iter->Next()) {
            print "\tAttribute: $map_key -> " . $map_value->GetPrettyPrintedString() . "\n" ;
        }
    }
 
    # Attributes from parse tree port declarations:
    $iter = new Verific::VeriModuleItemArrayIter($mod->GetModuleItems()) ;
    for (my $mod_item = $iter->First(); $iter->GetIndex() < $iter->Size(); $mod_item = $iter->Next()) {
        if (!$mod_item || !$mod_item->IsDataDecl()) { next ; }
  
        print "Module item: " . $mod_item->GetPrettyPrintedString() ;
        my $map_iter = Verific::Map::Iterator($mod_item->GetAttributes(), "char", "Verific::VeriExpression") ;
        my $map_key ;
        my $map_value ;
        while (($map_key, $map_value) = $map_iter->Next()) {
            print "\tAttribute: $map_key -> " . $map_value->GetPrettyPrintedString() . "\n" ;
        }
    }
}
 

Verilog testcase:

module test1 (
        (* attr1 = 0, attr2 = 1 *) input in ,
        (* attr3 = 2, attr4 = 3 *) output out
    );
 
    assign out = in ;
endmodule
 
module test2 (in, out) ;
    (* attr1 = 0, attr2 = 1 *) input in ;
    (* attr3 = 2, attr4 = 3 *) output out ;
 
    assign out = in ;
endmodule
 

Run:

-- Analyzing Verilog file 'test.v' (VERI-1482)
test.v(7): INFO: Checking module test1
Port: in
Port: out
Port declaration:
(* attr1=0, attr2=1 *) input in
        Attribute: attr1 -> 0
        Attribute: attr2 -> 1
Port declaration:
(* attr3=2, attr4=3 *) output out
        Attribute: attr3 -> 2
        Attribute: attr4 -> 3
test.v(14): INFO: Checking module test2
Port: in
Port: out
Port declaration: in
Port declaration: out
Module item: (* attr1=0, attr2=1 *) input in ;
        Attribute: attr1 -> 0
        Attribute: attr2 -> 1
Module item: (* attr3=2, attr4=3 *) output out ;
        Attribute: attr3 -> 2
        Attribute: attr4 -> 3