Difference between revisions of "Pretty-print a module and the packages imported by the module"

From Verific Design Automation FAQ
Jump to: navigation, search
(Created page with "C++: <nowiki> #include <iostream> #include <fstream> #include "veri_file.h" #include "VeriModule.h" #include "VeriId.h" #include "VeriScope.h" #include "Set.h" using names...")
 
Line 19: Line 19:
 
int main(int argc, const char **argv)
 
int main(int argc, const char **argv)
 
{
 
{
     const char *file = (argc > 1) ? argv[1] : "test.v" ;
+
     const char *file = (argc > 1) ? argv[1] : "test.sv" ;
  
 
     // Analyze the files (AnalyzeMultipleFiles API is the recommended one):
 
     // Analyze the files (AnalyzeMultipleFiles API is the recommended one):
Line 26: Line 26:
 
     if (!veri_file::AnalyzeMultipleFiles(&files, veri_file::SYSTEM_VERILOG)) return 1 ;
 
     if (!veri_file::AnalyzeMultipleFiles(&files, veri_file::SYSTEM_VERILOG)) return 1 ;
  
     // Get the module by name:
+
     MapIter mi ;
     VeriModule *test = veri_file::GetModule ("test");
+
     VeriModule *module ;
     if (!test) return 2 ;
+
     FOREACH_VERILOG_MODULE(mi, module){
 +
        if (!module) continue ;
 +
        if (module->IsPackage()) continue ; // no need to dive into package
  
    // Get the scope of the module:
+
        // Get the scope of the module:
    VeriScope *scope = test->GetScope() ;
+
        VeriScope *scope = module->GetScope() ;
  
    std::ofstream f("test_out.v", std::ios::out) ;
+
        char *outputfilename = Strings::save(module->Name(), "_pp_out.v");
 +
        std::ofstream f(outputfilename, std::ios::out) ;
  
    // Get the scope that this module/scope is using:
+
        // Get the scope that this module/scope is using:
    // This also includes the compilation unit in the list, if any/required:
+
        // This also includes the compilation unit in the list, if any/required:
    Set *using_scopes = (scope) ? scope->GetUsing() : 0 ;
+
        Set *using_scopes = (scope) ? scope->GetUsing() : 0 ;
  
    // Print all those scopes/modules before printing the module itself:
+
        // Print all those scopes/modules before printing the module itself:
    SetIter si ;
+
        SetIter si ;
    VeriScope *using_scope ;
+
        VeriScope *using_scope ;
    FOREACH_SET_ITEM(using_scopes, si, &using_scope) {
+
        FOREACH_SET_ITEM(using_scopes, si, &using_scope) {
        VeriIdDef *mod_id = using_scope->GetContainingModule() ;
+
            VeriIdDef *mod_id = using_scope->GetContainingModule() ;
        VeriModule *mod = (mod_id) ? mod_id->GetModule() : 0 ;
+
            VeriModule *mod = (mod_id) ? mod_id->GetModule() : 0 ;
        if (!mod) continue ;
+
            if (!mod) continue ;
        f << "// Printing module " << mod->Name() << endl ;
+
            if (mod->IsPackage()) {
        mod->PrettyPrint(f, 0) ;
+
                std::cout << ">>> Module '" << module->Name() << "' uses package '" << mod->Name() << "' <<<\n";
    }
+
            }
 +
            f << "// Printing package " << mod->Name() << endl ;
 +
            mod->PrettyPrint(f, 0) ;
 +
        }
  
    // Now  print the module:
+
        // Now  print the module:
    f << "// Printing module " << test->Name() << endl ;
+
        f << "// Printing module " << module->Name() << endl ;
    test->PrettyPrint(f, 0) ;
+
        module->PrettyPrint(f, 0) ;
    f.close() ;
+
        f.close() ;
 +
    }
  
 
     return 0 ;
 
     return 0 ;
Line 76: Line 83:
 
Pretty-printed output:
 
Pretty-printed output:
 
  <nowiki>
 
  <nowiki>
// Printing module PKG1
+
// Printing package PKG1
  
 
package PKG1 ;
 
package PKG1 ;
     typedef int my_int ;  
+
     typedef int my_int ;
 
endpackage
 
endpackage
  
  
// Printing module $unit_test_v
+
// Printing package $unit_test_sv
  
typedef byte my_byte ;  
+
typedef byte my_byte ;
  
  
Line 92: Line 99:
 
module test ;
 
module test ;
 
     import PKG1:: * ;
 
     import PKG1:: * ;
     my_int int1 ;  
+
     my_int int1 ;
     my_byte byte1 ;  
+
     my_byte byte1 ;
 
endmodule
 
endmodule
 
  </nowiki>
 
  </nowiki>

Revision as of 15:26, 20 April 2021

C++:

#include <iostream>
#include <fstream>

#include "veri_file.h"
#include "VeriModule.h"
#include "VeriId.h"
#include "VeriScope.h"

#include "Set.h"

using namespace std ;

#ifdef VERIFIC_NAMESPACE
using namespace Verific ;
#endif

int main(int argc, const char **argv)
{
    const char *file = (argc > 1) ? argv[1] : "test.sv" ;

    // Analyze the files (AnalyzeMultipleFiles API is the recommended one):
    Array files(1) ;
    files.InsertLast(file) ;
    if (!veri_file::AnalyzeMultipleFiles(&files, veri_file::SYSTEM_VERILOG)) return 1 ;

    MapIter mi ;
    VeriModule *module ;
    FOREACH_VERILOG_MODULE(mi, module){
        if (!module) continue ;
        if (module->IsPackage()) continue ; // no need to dive into package

        // Get the scope of the module:
        VeriScope *scope = module->GetScope() ;

        char *outputfilename = Strings::save(module->Name(), "_pp_out.v");
        std::ofstream f(outputfilename, std::ios::out) ;

        // Get the scope that this module/scope is using:
        // This also includes the compilation unit in the list, if any/required:
        Set *using_scopes = (scope) ? scope->GetUsing() : 0 ;

        // Print all those scopes/modules before printing the module itself:
        SetIter si ;
        VeriScope *using_scope ;
        FOREACH_SET_ITEM(using_scopes, si, &using_scope) {
            VeriIdDef *mod_id = using_scope->GetContainingModule() ;
            VeriModule *mod = (mod_id) ? mod_id->GetModule() : 0 ;
            if (!mod) continue ;
            if (mod->IsPackage()) {
                std::cout << ">>> Module '" << module->Name() << "' uses package '" << mod->Name() << "' <<<\n";
            }
            f << "// Printing package " << mod->Name() << endl ;
            mod->PrettyPrint(f, 0) ;
        }

        // Now  print the module:
        f << "// Printing module " << module->Name() << endl ;
        module->PrettyPrint(f, 0) ;
        f.close() ;
    }

    return 0 ;
}
 

Input Verilog:

package PKG1 ;
    typedef int my_int ;
endpackage

typedef byte my_byte ;

module test ;
    import PKG1::* ;
    my_int int1 ;
    my_byte byte1 ;
endmodule
 

Pretty-printed output:

// Printing package PKG1

package PKG1 ;
    typedef int my_int ;
endpackage


// Printing package $unit_test_sv

typedef byte my_byte ;


// Printing module test

module test ;
    import PKG1:: * ;
    my_int int1 ;
    my_byte byte1 ;
endmodule