A Python tool that analyzes SystemVerilog files to extract module instances and generate filelists for compilation.
- 🔍 Automatic Module Discovery: Scans SystemVerilog files and builds dependency trees
- 📦 Library Support: Separates library modules into dedicated filelists
- 📤 Library Extraction: Copies referenced library sources into a clean directory when needed
- 📝 Include File Handling: Properly handles
.svhheaders and SystemVerilog packages - 🏷️ Prefix Support: Renames library modules with custom prefixes to avoid naming conflicts
- 📊 Report Generation: Creates detailed reports in text, JSON, or Markdown format
- ⚡ Smart Parsing: Handles parameterized instances and multiline module declarations
No installation required! Just ensure you have Python 3.6+ installed.
# Clone the repository
git clone <your-repo-url>
cd sv_instance_extractor
# Make the script executable (optional)
chmod +x sv_instance_extractor.pyGenerate a filelist from a top-level module:
./sv_instance_extractor.py -i top.sv -idir ./rtlSeparate library modules into lib.f:
./sv_instance_extractor.py -i top.sv -idir ./rtl -lib ./ip_libraryHandle header files and packages:
./sv_instance_extractor.py -i top.sv -idir ./rtl --include ./include --include ./packagesRename library modules and update all instances:
./sv_instance_extractor.py -i top.sv -idir ./rtl -lib ./ip_library --prefix=IP -o ./outputCurated scripts under examples/ demonstrate common workflows:
examples/run_basic.sh– generatelist.f/lib.fin place.examples/run_with_prefix.sh– apply--prefixand inspect the rewritten RTL.examples/run_export_lib.sh– copy referenced library sources into a clean directory.examples/run_programmatic.py– invokeInstanceExtractordirectly from Python and emit a report.
Run them from the repository root, for example:
bash examples/run_basic.sh
python3 examples/run_programmatic.pyEach script writes artefacts beneath examples/output/; rerun as needed after inspecting the results.
| Option | Description |
|---|---|
-i, --input <file> |
Top-level SystemVerilog file to analyze |
| Option | Description |
|---|---|
-idir <dir> |
Module search directory (can be used multiple times) |
-lib <dir> |
Library module directory - generates separate lib.f (can be used multiple times) |
--include <dir> |
Include file directory for .svh headers and packages (can be used multiple times) |
| Option | Description |
|---|---|
-o, --output <dir> |
Output directory (default: current directory, or ./output_rtl with --prefix) |
--gen-lib <dir> |
Copy referenced library modules into <dir> and emit a lib.f there |
--prefix <string> |
Add prefix to library module names (requires -o or uses ./output_rtl) |
| Option | Description |
|---|---|
--report <file> |
Generate report file (default: print to stdout) |
--report-format <fmt> |
Report format: text (default), json, or markdown |
./sv_instance_extractor.py -i test_example/rtl/top.sv -idir test_example/rtlOutput: list.f
// RTL Files
test_example/rtl/top.sv
test_example/rtl/cpu_core.sv
test_example/rtl/alu.sv
test_example/rtl/register_file.sv
./sv_instance_extractor.py -i test_example/rtl/top.sv \
-idir test_example/rtl \
-lib test_example/libOutput: list.f and lib.f
list.f:
// RTL Files
test_example/rtl/top.sv
...
// Library Files
-f lib.f
lib.f:
// Library Files
test_example/lib/adder.sv
test_example/lib/fifo.sv
./sv_instance_extractor.py -i test_example/rtl/top.sv \
-idir test_example/rtl \
-lib test_example/lib \
--gen-lib ./extracted_libResult:
list.f
extracted_lib/
├── adder.sv
├── fifo.sv
└── lib.f
list.f now references extracted_lib/lib.f, and the copied sources keep only the modules actually used by the design.
./sv_instance_extractor.py -i test_example/rtl/top.sv \
-idir test_example/rtl \
-lib test_example/lib \
--include test_example/include \
--include test_example/packagesOutput: list.f with proper ordering
// SystemVerilog Packages
test_example/packages/common_pkg.sv
// Include Directories
+incdir+test_example/include
+incdir+test_example/packages
// RTL Files
...
// Library Files
-f lib.f
./sv_instance_extractor.py -i test_example/rtl/top.sv \
-idir test_example/rtl \
-lib test_example/lib \
--include test_example/include \
--include test_example/packages \
--prefix=IP \
-o ./isolated_designWhat happens:
- Library modules renamed:
adder→IP_adder,fifo→IP_fifo - All RTL files updated to use new names
- Files copied to
./isolated_design/with proper structure - Include files copied to
./isolated_design/include/ - New filelists generated
Original alu.sv:
adder #(.WIDTH(32)) u_adder (
.a(a), .b(b), .sum(sum)
);Modified isolated_design/alu.sv:
IP_adder #(.WIDTH(32)) u_adder (
.a(a), .b(b), .sum(sum)
);./sv_instance_extractor.py -i test_example/rtl/top.sv \
-idir test_example/rtl \
-lib test_example/lib \
--report=report.json \
--report-format=jsonOutput: report.json with complete analysis data (useful for automation)
-
list.f: Main filelist containing:- SystemVerilog packages (from
--includedirectories) +incdir+directives for header files- RTL module files
- Reference to
lib.f(if library modules exist)
- SystemVerilog packages (from
-
lib.f: Library filelist containing library module files (placed under--gen-libwhen that option is used)
Reports include:
- Summary: Statistics, configuration, generated files
- Details: Module list, file modifications, warnings, errors
Available formats:
- Text: Human-readable console output
- JSON: Machine-readable for automation
- Markdown: Pretty documentation format
- Parse Top File: Reads the top-level SystemVerilog file
- Find Instances: Extracts all module instantiations
- Build Dependency Tree: Recursively finds all required modules
- Scan Directories: Searches
-idirand-libdirectories for module definitions - Process Includes: Handles
.svhfiles and packages from--includedirectories - Generate Filelists: Creates properly ordered compilation lists
- Apply Transformations (optional): Renames modules with
--prefixand updates all instances - Generate Report: Creates detailed analysis report
The repository includes a complete test example:
test_example/
├── include/
│ └── defines.svh # Header file
├── packages/
│ └── common_pkg.sv # SystemVerilog package
├── lib/
│ ├── adder.sv # Library module
│ └── fifo.sv # Library module
└── rtl/
├── top.sv # Top module
├── cpu_core.sv # CPU core
├── alu.sv # ALU (uses adder)
└── register_file.sv # Register file
Run the test:
./sv_instance_extractor.py -i test_example/rtl/top.sv \
-idir test_example/rtl \
-lib test_example/lib \
--include test_example/include \
--include test_example/packagespython3 -m venv .venv
source .venv/bin/activate
python3 -m pip install -r requirements-dev.txt
pytest
deactivatepytest exercises parser utilities and the extractor workflow using temporary directories. A virtual environment keeps development dependencies isolated from the system Python installation.
./run_tests.shThe shell suite validates full CLI scenarios, including report generation and error handling.
- Python 3.6 or higher
- No external dependencies (uses only Python standard library)
- Does not handle SystemVerilog
generateblocks - Does not process
`includedirectives within files - Parameterized modules are treated as single modules
- Does not validate SystemVerilog syntax
See CLAUDE.md for detailed specifications and planned features.
[Your License Here]
Contributions are welcome! Please see CLAUDE.md for implementation details.