How pyhdi/pyverilog works
Project Type and Purpose
• Pyverilog is an open-source, Python-based hardware design processing toolkit for Verilog HDL.
• It provides four core capabilities:
1. vparser (code parser → AST)
2. dataflow analyzer (data-dependency analysis and optimization)
3. controlflow analyzer (state-machine and activation condition analysis)
4. AST code generator (turn AST back into Verilog)
File-Structure Analysis
• Top-level directories:
• pyverilog/ ← main library package
– vparser/ ← AST definitions, lexer, parser, preprocessor
– dataflow/ ← data-flow analysis modules (bindvisitor, optimizer, graphgen, etc.)
– controlflow/ ← control-flow analysis modules (active analyzer, state transition, etc.)
– ast_code_generator/ ← Jinja2-based codegen templates and driver
– utils/ ← shared helpers (scope, inference, identifier replacement, etc.)
• examples/ ← sample scripts demonstrating each capability
• scripts/ ← CI/test/deploy helper scripts (test-python2/3, upload_pypi)
• tests/ ← pytest integration and unit tests organized per feature
• verilogcode/ ← Verilog snippets used by tests
• setup.py & Makefile ← installation and build instructions
README Highlights
• Lists toolkit features, dependencies (Python3, PLY, Jinja2, Graphviz), and usage patterns.
• Describes CLI examples for parser, dataflow, controlflow, graphgen, and codegen.
Diagramming the Architecture
• Input Layer
– Verilog source files (user HDL code)
– Configuration (CLI flags, parameters)
• Parser Layer (pyverilog.vparser)
– Preprocessor → Lexer → Parser → AST generator
• Analysis Layer
– Dataflow Analyzer (pyverilog.dataflow)
– Controlflow Analyzer (pyverilog.controlflow)
– Utilities (pyverilog.utils) shared by both
• Code Generation Layer (pyverilog.ast_code_generator)
• Graph Generation (examples/graphgen ↔ Graphviz via Pygraphviz)
• CLI/Script Layer (examples/*.py, scripts/*.sh)
• Packaging & Testing
– setup.py, Makefile
– tests/ (pytest harness)
• External Dependencies
– PLY, Jinja2, Graphviz (optional), Pygraphviz, Icarus Verilog, pytest
– Verilog source → Parser → AST
– AST → Dataflow Analyzer → dataflow graph
– AST → Controlflow Analyzer → state-machine graph
– AST → Code Generator → new Verilog output
– Analysis modules call shared utils → transformations
– Graph modules call Graphviz/Pygraphviz → PNG/SVG outputs
– CLI scripts invoke library modules → produce console/graph outputs
c. Architectural Patterns & Principles
– Modular package per feature (parser, analysis, codegen)
– Clear separation: AST is the common IR for all analyses and codegen
– Use of templates (Jinja2) for code generation → decoupled from logic
d. Key Technologies & Frameworks
– Python3 (≥3.7)
– PLY (lex/yacc) for parser
– Jinja2 for codegen templates
– Graphviz + Pygraphviz for visualization
– pytest for tests
– Icarus Verilog for HDL simulation (optional)